码迷,mamicode.com
首页 > 其他好文 > 详细

欧拉函数求和 解题报告

时间:2016-11-09 22:12:01      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:scanf   pac   tput   name   names   black   bsp   cti   type   

对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目。此函数以其首名研究者欧拉命名,它又称为Euler‘s totient function、φ函数、欧拉商数等。例如:φ(8) = 4(Phi(8) = 4),因为1,3,5,7均和8互质。

 
S(n) = Phi(1) + Phi(2) + ...... Phi(n),给出n,求S(n),例如:n = 5,S(n) = 1 + 1 + 2 + 2 + 4 = 10,定义Phi(1) = 1。由于结果很大,输出Mod 1000000007的结果
Input
输入一个数N。(2 <= N <= 10^10)
Output
输出S(n) Mod 1000000007的结果。
Input示例
5
Output示例
10
代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a) for(int i=last[a];i;i=next[i])
#define N 5000000
using namespace std;
typedef long long ll;
const int Mo=1000000007;
const int mo=2333333;
const int ni=500000004;
int phi[N+5],p[N+5],l;
int last[mo],next[mo];
bool bz[N+5];
ll n,t[mo],v[mo];
void add(int x,ll y,ll z) {
t[++l]=y;v[l]=z;next[l]=last[x];last[x]=l;
}
ll calc(ll x) {
if (x<=N) return phi[x];int k=x%mo;ll ans=0,z=x%Mo;
rep(i,k) if (t[i]==x) return v[i];
for(ll l=2,r;l<=x;l=r+1) r=x/(x/l),(ans+=(r-l+1)%Mo*calc(x/l)%Mo)%=Mo;
ans=(z*(z+1)%Mo*ni%Mo-ans+Mo)%Mo;
add(k,x,ans);
return ans;
}
int main() {
fo(i,2,N) {
if (!bz[i]) p[++p[0]]=i,phi[i]=i-1;
fo(j,1,p[0]) {
int k=i*p[j];if (k>N) break;
bz[k]=1;if (!(i%p[j])) {phi[k]=phi[i]*p[j];break;}
phi[k]=phi[i]*(p[j]-1);
}
}
phi[1]=1;fo(i,1,N) (phi[i]+=phi[i-1])%=Mo;
scanf("%lld",&n);printf("%lld",calc(n));
}

欧拉函数求和 解题报告

标签:scanf   pac   tput   name   names   black   bsp   cti   type   

原文地址:http://www.cnblogs.com/dongbaba/p/6048414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!