[欧拉函数] Bzoj P2226 LCMSum
Posted comfortable
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[欧拉函数] Bzoj P2226 LCMSum相关的知识,希望对你有一定的参考价值。
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.
题解
代码
1 #include <cstdio> 2 #include <iostream> 3 #define N 1000010 4 #define ll long long 5 using namespace std; 6 ll T,bz[N],phi[N],q[N],ans[N]; 7 int main() 8 9 scanf("%lld",&T),phi[1]=1; 10 for (ll i=2;i<=N;i++) 11 12 if (!bz[i]) q[++q[0]]=i,phi[i]=i-1; 13 for (ll j=1;j<=q[0]&&i*q[j]<=N;j++) 14 if (i*q[j]<N) 15 16 bz[i*q[j]]=1; 17 if (i%q[j]==0) phi[i*q[j]]=phi[i]*q[j]; break; 18 phi[i*q[j]]=phi[i]*(q[j]-1); 19 20 21 for (int i=1;i<=N;i++) for (int j=1;i*j<=N;j++) ans[i*j]+=j*phi[j]/2; 22 for (ll i=1;i<=N;i++) ans[i]=i*ans[i]+i; 23 for (ll i=1,x;i<=T;i++) scanf("%lld",&x),printf("%lld\\n",ans[x]); 24
以上是关于[欧拉函数] Bzoj P2226 LCMSum的主要内容,如果未能解决你的问题,请参考以下文章