UVA 11426 GCD - Extreme (II)(神TM GCD大法,欧拉函数)
来源:互联网 发布:如何抢特价机票 知乎 编辑:程序博客网 时间:2024/06/06 23:35
Given the value of N, you will have to findthe value of G. The definition of G is given below:
Here
For those who have trouble understandingsummation notation, the meaning of G is given in the followingcode:
G=0;
for(i=1;i<N;i++)
for(j=i+1;j<=N;j++)
{
}
Input
The input file contains at most 100 lines ofinputs. Each line contains an integer N(1<N<4000001)
Output
For each line of input produce one line ofoutput. This line contains the value of G for the corresponding N.The value of G will fit in a 64-bit signed integer.
SampleInput
10
100
200000
0
Output for SampleInput
67
13015
143295493160
题意:求对于每个i<=N,求f(i)=GCD(1,i)+GCD(2,i)+......GCD(i-1,i)并求前N项和。
对于一个N,设GCD(a,N)=b;则有N=b*x,a=b*y,GCD(a/b,N/b)=1即GCD(x,y)=1;
又依题意得a<N,则Y<X,则满足GCD(a,N)=b且a<N的a的个数等于GCD(y,x)=1且y<x的y的个数,即eluer(x)(小于x且与x互质的数的个数)
因此打个欧拉表再枚举N的约数b即可
代码:
#include<cstdio>#include<cstring>#include<string>#include<iostream>#include<sstream>#include<algorithm>#include<utility>#include<vector>#include<set>#include<map>#include<queue>#include<math.h>#include<iterator>#include<stack>using namespace std;typedef long long LL;typedef unsigned long long ULL;const double eps=1e-8,PI=3.1415926538;const LL MOD=1000000000+7;const LL MAXN=4000007;LL m[MAXN],phi[MAXN],p[MAXN],pt;void make(){ phi[1]=1; LL N=MAXN; LL k; for(int i=2;i<N;i++) { if(!m[i]) p[pt++]=m[i]=i,phi[i]=i-1; for(int j=0;j<pt&&(k=p[j]*i)<N;j++) { m[k]=p[j]; if(m[i]==p[j]) { phi[k]=phi[i]*p[j]; break; } else phi[k]=phi[i]*(p[j]-1); } }}LL gcd(LL a,LL b){ if(b==0)return a; else return gcd(b,a%b);}LL S[MAXN];int main(){ make(); phi[1]=phi[0]=0; memset(S,0,sizeof(S)); LL z=sqrt(MAXN); for(int i=1;i<=z;i++)//枚举N的约数 { for(int j=2;j*i<=MAXN-3;j++)//N=i*j { S[j*i]+=i*phi[j]; if(z<j)S[j*i]+=j*phi[i];//z<span style="font-family:Courier New;"><j时,i*j的约数j枚举不到</span> } } LL N,G; while(scanf("%lld",&N)!=-1&&N!=0) { G=0; for(int i=2;i<=N;i++) { G+=S[i]; } printf("%lld\n",G); } return 0;}
- UVA 11426 GCD - Extreme (II)(神TM GCD大法,欧拉函数)
- UVA 11426 GCD - Extreme (II) (欧拉函数)
- UVA 11426 GCD - Extreme (II)(欧拉函数)
- UVA 11426 GCD - Extreme (II)(欧拉函数)
- 【欧拉】 UVA 11426 GCD - Extreme (II)
- 欧拉 uva 11426 - GCD - Extreme (II)
- uva 11426 - GCD - Extreme (II) 欧拉函数
- UVa 11426 GCD - Extreme (II) / 素数筛选 + 欧拉函数
- UVa 11426 - GCD - Extreme (II) (数学 欧拉函数)
- [欧拉函数] uva 11426 GCD - Extreme (II)
- 【欧拉函数】uva 11426 GCD - Extreme (II)
- UVA 11426 GCD Extreme (II) 欧拉函数
- UVA 11426 GCD - Extreme (II) (数论|欧拉函数)
- uva 11426 - GCD - Extreme (II) 欧拉函数
- Uva 11426 - GCD - Extreme (II) 欧拉函数
- uva 11424 && 11426 GCD - Extreme(数论:欧拉函数)
- GCD - Extreme (II) [欧拉函数]
- UVa 11426 GCD - Extreme (II) (欧拉函数应用·O(N*logN))
- Lock及ReentrantReadWriteLock学习
- 模板设计模式
- SQL Server2008突然连接不到本地数据库
- 内置APK根据包名找到APK路径
- 在Mac上解决Android Studio 鼠标悬停文档显示fetching documentation的问题
- UVA 11426 GCD - Extreme (II)(神TM GCD大法,欧拉函数)
- 搞定各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 -
- eclipse最有用快捷键整理
- MySQL性能优化五之系统配置优化和服务器硬件优化
- iOS的KeyChain(钥匙串)的使用
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- 泄露的cisco ASA漏洞利用可以应用到新的cisco版本上
- Firebase初探:实时数据库(2)
- Yarn---统一资源管理系统