BZOJ2226: [Spoj 5971] LCMSum
题解:
考虑枚举gcd,然后问题转化为求<=n且与n互质的数的和。
这是有公式的f[i]=phi[i]*i/2
然后卡一卡时就可以过了。
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 1000000+5 14 #define maxm 100000+5 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 23 #define mod 1000000007 24 using namespace std; 25 inline int read() 26 { 27 int x=0,f=1;char ch=getchar(); 28 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 29 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 30 return x*f; 31 } 32 int tot,p[maxn]; 33 ll fai[maxn]; 34 bool v[maxn]; 35 void get() 36 { 37 fai[1]=1; 38 for2(i,2,1000000) 39 { 40 if(!v[i])p[++tot]=i,fai[i]=i-1; 41 for1(j,tot) 42 { 43 int k=i*p[j]; 44 if(k>1000000)break; 45 v[k]=1; 46 if(i%p[j])fai[k]=fai[i]*(p[j]-1); 47 else {fai[k]=fai[i]*p[j];break;} 48 } 49 } 50 for2(i,3,1000000)(fai[i]*=(ll)i)>>=1; 51 } 52 int main() 53 { 54 freopen("input.txt","r",stdin); 55 freopen("output.txt","w",stdout); 56 get(); 57 int T=read(); 58 while(T--) 59 { 60 int n=read(),m=sqrt(n);ll ans=0; 61 for1(i,m)if(n%i==0)ans+=fai[n/i]+fai[i]; 62 if(m*m==n)ans-=fai[m]; 63 printf("%lld\n",ans*(ll)n); 64 } 65 return 0; 66 }
UPD:其实我们可以预处理出答案,用普通的筛法。
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 1000000+5 14 #define maxm 1000000 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 23 #define mod 1000000007 24 using namespace std; 25 inline int read() 26 { 27 int x=0,f=1;char ch=getchar(); 28 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 29 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 30 return x*f; 31 } 32 int tot,p[maxn]; 33 ll fai[maxn],ans[maxn]; 34 bool v[maxn]; 35 void get() 36 { 37 fai[1]=1; 38 for2(i,2,maxm) 39 { 40 if(!v[i])p[++tot]=i,fai[i]=i-1; 41 for1(j,tot) 42 { 43 int k=i*p[j]; 44 if(k>maxm)break; 45 v[k]=1; 46 if(i%p[j])fai[k]=fai[i]*(p[j]-1); 47 else {fai[k]=fai[i]*p[j];break;} 48 } 49 } 50 for2(i,3,maxm)(fai[i]*=(ll)i)>>=1; 51 for1(i,maxm) 52 for(int j=i;j<=maxm;j+=i) 53 ans[j]+=fai[i]; 54 for1(i,maxm)ans[i]*=(ll)i; 55 } 56 int main() 57 { 58 freopen("input.txt","r",stdin); 59 freopen("output.txt","w",stdout); 60 get(); 61 int T=read(); 62 while(T--)printf("%lld\n",ans[read()]); 63 return 0; 64 }
2226: [Spoj 5971] LCMSum
Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 659 Solved: 292
[Submit][Status]
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