欧拉心算
Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 408 Solved: 244
[Submit][Status][Discuss]
Description
给出一个数字N
Input
Output
Sample Input
10
Sample Output
HINT
Source
1 #include<cstring> 2 #include<cmath> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstdio> 6 7 #define ll long long 8 #define N 10000007 9 using namespace std; 10 inline int read() 11 { 12 int x=0,f=1;char ch=getchar(); 13 while(ch<'0'||ch>'9'){if (ch=='-')f=-1;ch=getchar();} 14 while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} 15 return x*f; 16 } 17 18 int n; 19 int cnt,pri[N],phi[N]; 20 ll sum[N]; 21 bool flag[N]; 22 23 void init_phi() 24 { 25 phi[1]=1; 26 for (int i=2;i<=10000000;i++) 27 { 28 if (!flag[i])pri[++cnt]=i,phi[i]=i-1; 29 for (int j=1;j<=cnt&&pri[j]*i<=10000000;j++) 30 { 31 flag[pri[j]*i]=1; 32 if (i%pri[j]==0) 33 { 34 phi[pri[j]*i]=pri[j]*phi[i]; 35 break; 36 } 37 else phi[pri[j]*i]=phi[i]*phi[pri[j]]; 38 } 39 } 40 for (int i=1;i<=10000000;i++)sum[i]=sum[i-1]+phi[i]; 41 } 42 void solve(ll n) 43 { 44 ll last,ans=0; 45 for (int i=1;i<=n;i=last+1) 46 { 47 last=n/(n/i);//琛ㄧずn涓埌杈惧摢閲屼负姝㈤兘鏄痭/i鐨? 48 ans+=(sum[last]-sum[i-1])*((sum[n/i]<<1)-1); 49 } 50 printf("%lld\n",ans); 51 } 52 int main() 53 { 54 init_phi(); 55 int T=read(); 56 while(T--) 57 { 58 n=read(); 59 solve(n); 60 } 61 }