Discription
This time I need you to calculate the f(n) . (3<=n<=1000000)
f(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n).
Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n-1])
C[n][k] means the number of way to choose k things from n some things.
gcd(a,b) means the greatest common divisor of a and b.
f(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n).
Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n-1])
C[n][k] means the number of way to choose k things from n some things.
gcd(a,b) means the greatest common divisor of a and b.
Input
There are several test case. For each test case:One integer n(3<=n<=1000000). The end of the in put file is EOF.
Output
For each test case:
The output consists of one line with one integer f(n).
Sample Input
3 26983
Sample Output
3 37556486
本来毫无思路,然而打了个表找了找规律,发现Gcd(x)无非两种情况:
1.当x=p^q时,其中p为质数,那么Gcd(x)=p
2.其他的时候Gcd(x)=1
然后就是个水题了
#include<bits/stdc++.h> #define ll long long #define maxn 1000000 using namespace std; int zs[maxn/5],t=0,n; ll f[maxn+5]; bool v[maxn+5]; inline void init(){ for(int i=2;i<=maxn;i++){ if(!v[i]) f[i]=i,zs[++t]=i; for(int j=1,u;j<=t&&(u=zs[j]*i)<=maxn;j++){ v[u]=1; if(!(i%zs[j])){ f[u]=f[i]; break; } f[u]=1; } } for(int i=4;i<=maxn;i++) f[i]+=f[i-1]; } int main(){ init(); while(scanf("%d",&n)==1) printf("%lld ",f[n]); return 0; }