f(n)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 806 Accepted Submission(s): 497
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.
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(n)=gcd(C[n][1],C[n][2],……,C[n][n-1]),通过打表找规律后发现,
n 为质数时 ,Gcd(n) = n;
n 只有一个质因子时,Gcd(n) = 这个质因子
n 有多个质因子时, Gcd(n) = 1;
先素筛打表,然后把每个Gcd(n)的和 预处理出来。
#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define ll long long
const int maxn = 1e6+10;
const int N = 3010;
ll n;
ll a[maxn],pri[maxn];
bool prime[maxn];
void Is_prime(){
memset(prime,true,sizeof(prime));
prime[0] = prime[1] = false;
for(int i=2;i*i<maxn;i++){
if(prime[i]){
for(int j=2;i*j<maxn;j++){
prime[i*j] = false;
}
}
}
ll num = 1;
for(int i=1;i<=maxn;i++){
if(prime[i]){
pri[num++] = i;
}
}
}
int G(ll n){
if(prime[n]) return n;
ll num = 0,ans = 1;
for(int i=1;pri[i]*pri[i]<=n;i++){
if(n%pri[i]==0){
num ++;
ans = pri[i];
while(n%pri[i]==0)n/=pri[i];
}
if(num > 1) return 1;
}
if(n!=1){
num ++ ;ans = n;
}
if(num > 1)return 1;
return ans;
}
void get_num(){
a[0] = a[1] = a[2] = 0;
for(int i=3;i<maxn;i++){
a[i] = G(i) + a[i-1];
}
}
int main(){
Is_prime();
get_num();
while(scanf("%lld",&n)!=EOF){
printf("%lld\n",a[n]);
}
return 0;
}