标签:function struct break define for oid text bsp div
输入一个数N。(2 <= N <= 10^10)
输出S(n) Mod 1000000007的结果。
5
10
#include<cstdio> #include<iostream> #define N 2000010 #define ha 2333333 #define mod 1000000007 #define ni 500000004 #define lon unsigned long long using namespace std; int phi[N],prime[N],cnt,tot,head[N],vis[N]; lon n,sum[N]; struct node{int pre;lon x,v;}e[N]; void add(int u,lon v,lon x){ e[++cnt].v=v;e[cnt].x=x;e[cnt].pre=head[u];head[u]=cnt; } void get_prime(){ phi[1]=1; for(int i=2;i<N;i++){ if(!vis[i]) vis[i]=1,prime[++tot]=i,phi[i]=i-1; for(int j=1;j<=tot&&i*prime[j]<N;j++){ vis[i*prime[j]]=1; phi[i*prime[j]]=phi[i]*(prime[j]-1); if(i%prime[j]==0){ phi[i*prime[j]]=phi[i]*prime[j]; break; } } } for(int i=1;i<N;i++) sum[i]=(sum[i-1]+phi[i])%mod; } lon solve(lon x){ if(x<N) return sum[x]; lon ans=0,k=x%ha,last; for(int i=head[k];i;i=e[i].pre) if(e[i].v==x) return e[i].x; for(lon i=2;i<=x;i=last+1){ last=x/(x/i); ans=(ans+(last-i+1)%mod*solve(x/i)%mod)%mod; } ans=((x%mod*(x+1)%mod)%mod*ni%mod-ans+mod)%mod; add(k,x,ans); return ans; } int main(){ get_prime(); cin>>n; cout<<solve(n); return 0; }
标签:function struct break define for oid text bsp div
原文地址:http://www.cnblogs.com/harden/p/6528238.html