GCD Extreme(II)

Input: Standard Input Output:

Standard Output

Given the value of N, you will have to find the value of G. The definition of G is given below:

Here GCD(i,j) means the greatest common divisor of integer i and integer j.   For those who have trouble understanding summation notation, the meaning of G is given in the following code: G=0; for(i=1;i<N;i++) for(j=i+1;j<=N;j++) {     G+=gcd(i,j); } /*Here gcd() is a function that finds the greatest common divisor of the two input numbers*/   Input The input file contains at most 100 lines of inputs. Each line contains an integer N (1<N<4000001). The meaning of N is given in the problem statement. Input is terminated by a line containing a single zero.   Output For each line of input produce one line of output. This line contains the value of G for the corresponding N. The value of G will fit in a 64-bit signed integer.   Sample Input

10 100 200000 0

Output for Sample Input

67 13015 143295493160

Problemsetter: Shahriar Manzoor

Special Thanks: Syed Monowar Hossain

/* 【题意】

求sum(gcd(i,j),1<=i<j<=n)1<n<4000001

【题解】 1.建立递推关系,s(n)=s(n-1)+gcd(1,n)+gcd(2,n)+……+gcd(n-1,n);

2.设f(n)=gcd(1,n)+gcd(2,n)+……+gcd(n-1,n)。

gcd(x,n)=i是n的约数(x<n),按照这个约数进行分类。设满足gcd(x,n)=i的约束有g(n,i)个,则有f(n)=sum(i*g(n,i))。

而gcd(x,n)=i等价于gcd(x/i,n/i)=1,因此g(n,i)等价于phi(n/i).phi(x)为欧拉函数。

3.降低时间复杂度。用筛法预处理phi[x]表

用筛法预处理f(x)->枚举因数,更新其所有倍数求解。

*/

/* 欧拉函数的应用,以后看到互质的数第一个就要想到欧拉函数。今天又学到了好多家伙。 欧拉定理: 欧拉定理表明,若n,a为正整数,且n,a互质,(a,n) = 1,则a^φ(n) ≡ 1 (mod n)   费马小定理: 且(a,p)=1,那么 a^(p-1) ≡1(mod p) 假如p是质数,且a,p互质,那么 a的(p-1)次方除以p的余数恒等于1 */

 //1285ms

 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<vector>
#include<bitset>
using namespace std;
typedef long long ll; const int M=;
int phi[M];
ll s[M],f[M];//f[n]=sum(phi[i]*j) (i*j=n) (去掉n*phi[1],因为gcd(n,n)不符合题意!!) int get(){
char c;
int res=;
while(c=getchar(),!isdigit(c));
do{
res=(res<<)+(res<<)+(c-'');
}while(c=getchar(),isdigit(c));
return res;
} void phi_table()//类似素数刷表!!
{
phi[]=;
for(int i=;i<M;i++)phi[i]=i;
for(int i=;i<M;i+=)phi[i]/=;
for(int i=;i<M;i+=)
if(phi[i]==i)//说明i是素数!!!
for(int j=i;j<M;j+=i)
{
phi[j]-=phi[j]/i;//保证i是素数且是j的素因子!!!
}
} int main()
{
int n,i,j,k;
phi_table();
memset(f,,sizeof(f));
phi[]=;
for(i=;i<(int)sqrt(M);i++)
{
f[i*i]+=phi[i]*i;
for(n=i*i+i;n<M;n+=i)
f[n]+=(ll)(i*phi[n/i]+n/i*phi[i]);//之前令phi[1]=0;因为gcd(n,n)不符合题意!!
}
s[]=;
for( n=;n<M;n++) s[n]=s[n-]+f[n];
while()
{
n=get();
if(n==)break;
printf("%lld\n",s[n]);
}
return ;
} //2382ms
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<vector>
#include<bitset>
using namespace std;
typedef long long ll; const ll M=;
ll phi[M];
ll s[M],f[M]; void phi_table()//类似素数刷表!!
{
phi[]=;
for(int i=;i<M;i++)phi[i]=i;
for(int i=;i<M;i+=)phi[i]/=;
for(int i=;i<M;i+=) if(phi[i]==i)//说明i是素数!!!
for(int j=i;j<M;j+=i)
{
phi[j]-=phi[j]/i;//保证i是素数且是j的素因子!!!
}
}
int main()
{
phi_table();
memset(f,,sizeof(f));
for(int i=;i<M;i++)
for(int n=i+i;n<M;n+=i)
f[n]+=i*phi[n/i];
s[]=;
for(int n=;n<M;n++) s[n]=s[n-]+f[n];
int n;
while(scanf("%d",&n)==&&n)
{
printf("%lld\n",s[n]);
}
return ;
} #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<set>
#include<vector>
#include<bitset>
using namespace std;
typedef long long ll; const ll M=;
ll phi[*M];
ll s[M],f[M]; void phi_table()//类似素数刷表!!
{
for(int i=;i<=M;i++)phi[i]=;
phi[]=;
for(int i=;i<=M;i++)
if(!phi[i])//说明i是素数!!!
for(int j=i;j<=M;j+=i)
{
if(!phi[j])phi[j]=j;
phi[j]=phi[j]/i*(i-);//保证i是素数且是j的素因子!!!
}
}
int main()
{
phi_table();
memset(f,,sizeof(f));
for(int i=;i<=M;i++)
for(int n=i+i;n<=M;n+=i)
f[n]+=i*phi[n/i];
s[]=;
for(int n=;n<=M;n++) s[n]=s[n-]+f[n];
int n;
while(scanf("%d",&n)==&&n)
{
printf("%lld\n",s[n]);
}
return ;
}

UVa10426的更多相关文章

随机推荐

  1. phpstudy composer 安装

    今天突然发现phpstudy 可以安装 composer 一打开php中openssl拓展 坑一  我的phpstudy 是2018最新版本,但是你下载laravel什么之类库会报错,是由于compo ...

  2. iOS应用将强制使用HTTPS安全加密-afn配置https(190926更新)

    WWDC 2016苹果开发者大会上,苹果在讲解全新的iOS10中提到了数据安全这一方面,并且苹果宣布iOS应用将从2017年1月起启用名为App Transport Security的安全传输功能. ...

  3. 用Vue来实现音乐播放器(三十八):歌词滚动列表的问题

    1.频繁切换歌曲时,歌词会跳来跳去 原因: // 歌词跳跃是因为内部有一个currentLyric对像内部有一些功能来完成歌词的跳跃 //每个currentLyric能实现歌曲的播放跳到相应的位置 是 ...

  4. Flink的基本概念

    Stream.Transformation.Operator 用户实现的Flink程序是由Stream和Transformation这两个基本构建块组成,其中Stream是一个中间结果数据,而Tran ...

  5. 编写shell脚本一键启动 重启 停止springboot项目

    #!/bin/bash #设置环境变量 export JAVA_HOME=/usr/local/jdk1.8.0_181 export JRE_HOME=/$JAVA_HOME/jre export ...

  6. 操作系统(5)实验0——makefile的写法

    之前GCC那部分我提到过,gcc啥啥啥啥傻傻的那个指令只能够编译简单的代码,如果要干大事(例如突然心血来潮写个c开头的神经网络库之类的),还是要写Makefile来编译.其实在Windows下通常用I ...

  7. 应用安全 - 工具 | 平台 - Weblogic - 漏洞 - 汇总

    控制台路径 | 弱口令  前置条件 /console CVE-2016-0638  Date 类型远程代码执行 影响范围10.3.6, 12.1.2, 12.1.3, 12.2.1  CVE-2016 ...

  8. 【Qt开发】【Linux开发】调试记录:QFontDatabase::loadFromCache Error

    最近做嵌入式的Qt界面,在移植成功后遇到了一个问题:QFontDatabase::loadFromCache: Font path doesn't match.后面跟着便是两个路径. 解决方案就是对比 ...

  9. Java第一周总结

    通过两周的Java学习最深刻的体会就是Java好像要比C要简单一些. 然后这两周我学习到了很多东西,李老师第一次上课就给我们介绍了Java的发展历程,同时还有Java工具jdk的发展历程. Java语 ...

  10. [19/06/08-星期六] CSS基础_表格&表单

    一.表格 如生活中的Excel表格,用途就是同来表示一些格式化的数据,如课程表.工资条.成绩单. 在网页中也可以创建出不同的表格,在HTML中使用table标签来创建一个表格.table是个块元素. ...