UVa10426
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的更多相关文章
随机推荐
- ES6 函数参数的解构赋值
function add([x, y]){ return x + y; } add([1, 2]); // 3
- 一款基于jQuery的漂亮弹出层
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- 如何选择EDM电子邮件服务提供商
选择一家好的EDM电子邮件服务商非常重要,因为这可以让我们的EDM营销事半功倍,同时可以达到更好的营销效果.下面博主为大家介绍一下选择标准. 一.服务好不好. 这点很重要,当然这里的服务包括售前和售后 ...
- 吃着火锅唱着歌学会Docker
第一篇:Docker概述 第二篇:Docker之版本与安装 第三篇:Docker之镜像 第四篇:Docker之容器 第五篇:Dcoker之容器与镜像 第六篇:Docker之网络管理 第七篇:Docke ...
- Delphi XE2 之 FireMonkey 入门(37) - 控件基础: TControl 概览
Delphi XE2 之 FireMonkey 入门(37) - 控件基础: TControl 概览 { TControl } public constructor Create(...); ov ...
- 阶段3 1.Mybatis_11.Mybatis的缓存_6 Mybatis中的一级缓存
Mybatis中的一级缓存和二级缓存 一级缓存: 它指的是Mybatis中SqlSession对象的缓存. 当我们执行查询之后,查询的结 ...
- Linux监控命令之==>vmstat
一.使用说明 vmstat 可以对操作系统的内存信息.进程状态.CPU 活动.磁盘等信息进行监控,不足之处是无法对某个进程进行深入分析. 二.用法及参数说明 -a:显示活跃和非活跃内存 -f:显示从系 ...
- html5获取位置信息,h5获取位置信息
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Win密钥.Win7旗舰版
1.windows7旗舰版免费密钥 - Win7之家.html(http://www.windows7en.com/Win7/25762.html) HT6VR-XMPDJ-2VBFV-R9PFY-3 ...
- [LeetCode] 1092. Shortest Common Supersequence
LeetCode刷题记录 传送门 Description Given two strings str1 and str2, return the shortest string that has bo ...