f(n)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 264 Accepted Submission(s): 163
Problem Description
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).
The output consists of one line with one integer f(n).
Sample Input
3 26983
Sample Output
3 37556486
转自:http://blog.youkuaiyun.com/q411307827/article/details/7859530
Gcd(n)可以直接算出:
1,当n为质数时,Gcd(n)=n;
2,当n为某个质数k的q次方时(q>=2) Gcd(n)=k;
3,其它情况都为1。
根据以上规律,我先把所有的数+1,再把质数或质数的次方加上质数本身-1
我的代码:
/*
program hdu_2582
author:BlackAndWhite
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
__int64 n;
__int64 i,ans;
bool is[1000005];
int prm[1000005];
int getprm(int n)
{
int i, j, k = 0;
int s, e = (int)(sqrt(0.0 + n) + 1);
memset(is, 1, sizeof(is));
prm[k++] = 2; is[0] = is[1] = 0;
for (i = 4; i < n; i += 2) is[i] = 0;
for (i = 3; i < e; i += 2) if (is[i])
{
prm[k++] = i;
for (s = i * 2, j = i * i; j < n; j += s)
is[j] = 0;
}
for ( ; i < n; i += 2) if (is[i]) prm[k++] = i;
return k;
}
__int64 fun(int x)
{
__int64 i,z=x;
for(i=0;;i++)
if(z<=n) z*=x;
else break;
return (i)*(x-1);
}
int main()
{
getprm(1000001);
while(~scanf("%I64d",&n))
{
ans=n-2;
for(i=0;prm[i];i++)//从2加起
{
if(prm[i]>n) break;
ans+=fun(prm[i]);
}
printf("%I64d\n",ans-1);
}
return 0;
}