P2303 [SDOI2012]Longge的问题 我傻QwQ
莫比乌斯反演学傻了$QwQ$
思路:推式子?
提交:2次
错因:又双叒叕没开$long\space long$
题解:
$\sum_{i=1}^n gcd(i,n)$
$=\sum_{d|n}d\sum_{i=1}^{\frac{n}{d}} [gcd(i,\frac{n}{d})=1]$
注意到$\sum_{i=1}^{\frac{n}{d}} [gcd(i,\frac{n}{d})=1]$就是与$\frac{n}{d}$互质的数的个数。
$=\sum_{d|n}d\varphi(\frac{n}{d})$
代码:
#include<cstdio> #include<iostream> #include<cmath> using namespace std; #define ull unsigned long long #define ll long long #define R register ll #define pause (for(R i=1;i<=10000000000;++i)) #define In freopen("NOIPAK++.in","r",stdin) #define Out freopen("out.out","w",stdout) namespace Fread { static char B[1<<15],*S=B,*D=B; #ifndef JACK #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++) #endif inline ll g() { R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix; if(ch==EOF) return EOF; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix; } inline bool isempty(const char& ch) {return (ch<=36||ch>=127);} inline void gs(char* s) { register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar())); } } using Fread::g; using Fread::gs; namespace Luitaryi { const int N=1<<16+10; int n,cnt; ll ans; int pri[N>>3]; bool vis[N]; inline void PRE(int n) { for(R i=2;i<=n;++i) { if(!vis[i]) pri[++cnt]=i; for(R j=1;j<=cnt&&i*pri[j]<=n;++j) { vis[i*pri[j]]=true; if(i%pri[j]==0) break; } } } inline ll phi(ll x) { R ret=x; for(R i=1;i<=cnt&&1ll*pri[i]*pri[i]<=x;++i) if(x%pri[i]==0) { ret=ret/pri[i]*(pri[i]-1); while(x%pri[i]==0) x/=pri[i]; } if(x>1) ret=ret/x*(x-1); return ret; } inline void main() { n=g(); R lim=sqrt(n); PRE(lim); for(R i=1;i<=lim;++i) if(n%i==0) { ans+=i*phi(n/i); if(i*i!=n) ans+=n/i*phi(i); } printf("%lld\n",ans); } } signed main() { Luitaryi::main(); }
2019.07.18