标签:style blog http io os ar for sp on
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3501
题意:求小于n的与n不互质的数的和;
分析:
欧拉函数的推广:
小于n的与n互质的数为phi(n),小于n的与n互质的数的和为phi(n)*n/2;
代码如下:
#include <iostream> #include <cstring> #include <cstdio> using namespace std; typedef long long LL; const int mod = 1000000007; LL phi(LL n) { LL rea=n; for(int i=2;i*i<=n;i++){ if(n%i==0){ rea-=rea/i; while(n%i==0) n/=i; } } if(n>1) rea-=rea/n; return rea; } int main() { LL n; while(cin>>n){ if(n==0) break; cout<<((n-1)*n/2%mod-phi(n)*n/2%mod+mod)%mod<<endl; } return 0; }
标签:style blog http io os ar for sp on
原文地址:http://blog.csdn.net/bigbigship/article/details/40627017