BZOJ4916

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;
}

  

posted @ 2017-12-15 22:19  Stump  阅读(248)  评论(0编辑  收藏  举报