51nod 1239 欧拉函数之和

来源:互联网 发布:淘宝退款成功后钱在哪 编辑:程序博客网 时间:2024/06/05 01:55

51nod 1239 欧拉函数之和

链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239

φid1=id0

Sφ(n)=ni=2niSφ(ni)

#include <algorithm>#include <string.h>#include <stdio.h>#include <cmath>#define MAXN 10000000#define SQRT 111111using namespace std;typedef long long LL;const LL P=1e9+7;int phi[MAXN];int tmp[SQRT];void clat(int d,LL n){    int m=sqrt(n)+1,ans=0;    for(int L=1;L<m;L++)    {        ans+=(LL)phi[L]*((n/L-n/(L+1))%P)%P;        if(ans>=P)ans-=P;    }    for(int i=(int)(n/m);i>1;i--)    {        LL u=(n/i);        if(u<MAXN)            ans+=phi[u];        else            ans+=tmp[i*d];        if(ans>=P)ans-=P;    }    if(n&1)        n=(((n+1)>>1)%P)*(n%P)%P;    else        n=((n>>1)%P)*((n+1)%P)%P;    tmp[d]=(n-ans+P)%P;}int solve(LL n){    if(n<MAXN)return phi[n];    for(LL i=(n/MAXN);i;i--)  clat((int)i,n/i);    return tmp[1];}int main (){    phi[1]=1;    for(int i=2;i<MAXN;i++)    {        if(phi[i])continue;        phi[i]=i-1;        LL size=(LL)i*i;        for(int j=i<<1;(LL)j<size&&j<MAXN;j+=i)  phi[j]=(phi[j])?phi[j]*phi[i]:phi[i];        if(size>=MAXN)continue;        for(int j=i*i,k=i;j<MAXN;j+=i,k++)            if(k%i)                phi[j]=(phi[k])?phi[k]*phi[i]:phi[i];            else                phi[j]=(phi[k])?phi[k]*i:i;    }    for(int i=1;i<MAXN;i++)    {        phi[i]+=phi[i-1];        if(phi[i]>=P)phi[i]-=P;    }    LL n;    scanf("%lld",&n);    printf("%d\n",solve(n));    return 0;}