bzoj2226
http://blog.csdn.net/benjaminpmlee/article/details/44947809
有点暴力。。。因为cout而re了两发。。
1 #include<bits/stdc++.h> 2 #define lowbit(a) ((a)&(-(a))) 3 #define clr(a,x) memset(a,x,sizeof(a)) 4 #define rep(i,l,r) for(int i=l;i<(r);i++) 5 typedef long long ll; 6 using namespace std; 7 int read() 8 { 9 char c=getchar(); 10 int ans=0,f=1; 11 while(!isdigit(c)){ 12 if(c=='-') f=-1; 13 c=getchar(); 14 } 15 while(isdigit(c)){ 16 ans=ans*10+c-'0'; 17 c=getchar(); 18 } 19 return ans*f; 20 } 21 const int maxn=1000009; 22 int n,cnt=0,phi[maxn],pri[maxn]; 23 bool p[maxn]; 24 void init(){ 25 clr(p,-1); 26 rep(i,2,maxn){ 27 if(p[i]) pri[cnt++]=i,phi[i]=i-1; 28 rep(j,0,cnt){ 29 if(pri[j]*i>=maxn) break; 30 p[pri[j]*i]=0; 31 if(i%pri[j]==0){ 32 phi[i*pri[j]]=phi[i]*pri[j]; 33 break; 34 }else phi[i*pri[j]]=phi[i]*(pri[j]-1); 35 } 36 } 37 } 38 ll f(int x){ 39 if(x==1) return 1ll; 40 return ll(phi[x])*ll(x)>>1; 41 } 42 int main() 43 { 44 init(); 45 int t=read(); 46 while(t--){ 47 n=read(); 48 ll ans=0; 49 int i; 50 for(i=1;i*i<n;i++){ 51 if(n%i==0){ 52 ans+=f(i)+f(n/i); 53 } 54 } 55 if(i*i==n) ans+=f(i); 56 printf("%lld\n",ans*ll(n)); 57 } 58 return 0; 59 }
2226: [Spoj 5971] LCMSum
Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 889 Solved: 405
[Submit][Status][Discuss]
Description
Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Least Common Multiple of the integers i and n.
Input
The first line contains T the number of test cases. Each of the next T lines contain an integer n.
Output
Output T lines, one for each test case, containing the required sum.
Sample Input
3
1
2
5
1
2
5
Sample Output
1
4
55
4
55
HINT
Constraints
1 <= T <= 300000
1 <= n <= 1000000