https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239
51nod 1239
欧拉函数之和
#include<cstdio> #include<map> const int N=10000011,maxn=10000000; const int mod=1e9+7; typedef long long ll; std::map<ll,ll>mp; int pr[N]; bool ip[N]; int phi[N]; int sum[N]; 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]]=pr[j]*phi[i]; break; } phi[i*pr[j]]=phi[i]*phi[pr[j]]; } } for(register int i=1;i<=maxn;++i) sum[i]=(sum[i-1]+phi[i])%mod; } ll T,n; inline ll query(ll x){ if(x<=maxn)return sum[x]; if(mp[x])return mp[x]; ll ans=1ll*x%mod*(x%mod+1)/2; ll pos; for(register ll i=2;i<=x;i=pos+1){ pos=x/(x/i); ans-=1ll*(pos-i+1)*query(x/i)%mod; ans=(ans+mod)%mod; } return mp[x]=ans; } int main(){ shai_fa(); scanf("%lld",&n); printf("%lld ",query(n)); return 0; }
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244
51nod 1244
莫比乌斯函数之和
#include<cstdio> #include<map> const int N=10000011,maxn=10000000; const int mod=1e9+7; typedef long long ll; std::map<ll,ll>mp; std::map<ll,bool>vis; int pr[N]; bool ip[N]; int miu[N]; inline void shai_fa(){ miu[1]=1; for(register int i=2;i<=maxn;++i){ if(!ip[i]) miu[pr[++pr[0]]=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) break; miu[i*pr[j]]=-miu[i]; } } for(register int i=1;i<=maxn;++i) miu[i]=(miu[i-1]+miu[i])%mod; } ll a,b; inline ll query(ll x){ if(x<=maxn)return miu[x]; if(vis[x])return mp[x]; ll ans=1,pos; for(ll i=2;i<=x;i=pos+1){ pos=x/(x/i); ans-=1ll*(pos-i+1)*query(x/i); } vis[x]=1; return mp[x]=ans; } int main(){ shai_fa(); scanf("%lld%lld",&a,&b); printf("%lld ",query(b)-query(a-1)); return 0; }