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

BZOJ4916

时间:2017-12-15 23:34:50      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:void   math   return   ++   pos   scanf   www.   bool   can   

http://www.lydsy.com/JudgeOnline/problem.php?id=4916

#include<cstdio>
#include<cmath>
#include<map>
typedef long long ll;
std::map<int,ll>hash;
const int mod=1e9+7,maxn=1e6,N=1e6+11;
int n;
ll phi[N];
bool ip[N];
int pr[N];
inline ll fp(ll a,int b){
	ll ret=1;
	while(b){
		if(b&1)ret=ret*a%mod;
		a=a*a%mod;b>>=1;
	}
	return ret;
}
ll rev6,rev2;
inline void shai_fa(){
	phi[1]=1ll;
	for(register int i=2;i<=maxn;++i){
		if(!ip[i])
			phi[pr[++pr[0]]=i]=i-1;
		for(register int j=1;j<=pr[0]&&pr[j]*i<=maxn;++j){
			ip[i*pr[j]]=1;
			if(i%pr[j]==0){
				phi[i*pr[j]]=phi[i]*pr[j];
				break;
			}
			phi[i*pr[j]]=phi[i]*phi[pr[j]];
		}
	}
	for(register int i=1;i<=maxn;++i)
		phi[i]=(phi[i-1]+1ll*i*phi[i]%mod)%mod;
}
inline ll getphi(int x){
	if(x<=maxn)return phi[x];
	if(hash[x])return hash[x];
	ll ans=1ll*x*(x+1)%mod*(x<<1|1)%mod*rev6%mod;
	for(register int i=2,pos;i<=x;i=pos+1){
		pos=x/(x/i);
		ans=ans-1ll*(pos-i+1)*(i+pos)%mod*rev2%mod*getphi(x/i)%mod;
		ans=(ans+mod)%mod;
	}
	return hash[x]=ans;
}
int main(){
	scanf("%d",&n);
	shai_fa();
	rev6=fp(6,mod-2);
	rev2=fp(2,mod-2);	
	printf("1\n%lld\n",getphi(n));
	return 0;
}

  

BZOJ4916

标签:void   math   return   ++   pos   scanf   www.   bool   can   

原文地址:http://www.cnblogs.com/Stump/p/8045076.html

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