思路:欧拉函数;
欧拉函数,然后用下等差数列公式就行了。
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<queue> 5 #include<math.h> 6 #include<vector> 7 #include<bitset> 8 using namespace std; 9 typedef long long LL; 10 bool prime[5000000]; 11 int ans[1000000]; 12 int oula[5000000]; 13 int main(void) 14 { 15 int i,j; 16 for(i = 0; i < 5000000; i++) 17 { 18 oula[i] = i; 19 } 20 for(i = 2; i <10000 ; i++) 21 { 22 if(!prime[i]) 23 { 24 for(j = i; (i*j) <= 5000000; j++) 25 prime[i*j] = true; 26 } 27 } 28 int cn = 0; 29 for(i = 2 ; i <= 5000000; i++) 30 if(!prime[i]) 31 ans[cn++]=i; 32 oula[0] = 0; 33 oula[1] = 1; 34 for(i = 0; i < cn; i++) 35 { 36 for(j = 1; (ans[i]*j) <= 5000000; j++) 37 { 38 oula[ans[i]*j]/=ans[i]; 39 oula[ans[i]*j]*=ans[i]-1; 40 } 41 } 42 int n; 43 while(scanf("%d",&n),n!=0) 44 { LL sum=0; 45 for(i = 2;i <=n ;i++) 46 { LL ak = (LL)(1+n/i)*(LL)(n/i)/2; 47 sum+=ak*oula[i]; 48 } 49 printf("%lld\n",sum); 50 }return 0; 51 }
时间: 2024-10-15 02:57:08