标签:main std math highlight ios http ret line stream
要求的是小于$n$的和$n$不互质的数字之和...那么我们先求出和$n$互质的数字之和,然后减一减就好了...
$\sum _{i=1}^{n} i[gcd(i,n)==1]=\frac{n\phi(n)}{2}$
考虑$gcd(n,i)=1$,那么必然有$gcd(n,n-i)=1$,然后发现如果把$gcd(n,i)=1$和$gcd(n,n-i)=1$凑到一起会出现$n$,这样的有$\frac{\phi(n)}{2}$对...
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> //by NeighThorn using namespace std; const int mod=1e9+7; int n,ans; inline int phi(int n){ int x=n,m=sqrt(n); for(int i=2;i<=m;i++) if(n%i==0){ x=1LL*x/i*(i-1)%mod; while(n%i==0) n/=i; } if(n>1) x=x/n*(n-1)%mod; return x; } signed main(void){ while(scanf("%d",&n)&&n){ ans=(1LL*n*(n-1)/2)%mod; ans-=(1LL*phi(n)*n/2)%mod; if(ans<0) ans+=mod; printf("%d\n",ans); } return 0; }
By NeighThorn
标签:main std math highlight ios http ret line stream
原文地址:http://www.cnblogs.com/neighthorn/p/6691150.html