1239 欧拉函数之和

 https://www.51nod.com/Challenge/Problem.html#problemId=1239

#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <tr1/unordered_map>
#include <cmath>
//#include<bits/stdc++.h>
using namespace std;

#define sfi(i) scanf("%d",&i)
#define sfl(i) scanf("%I64d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define prl(i) printf("%I64d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson (rt<<1)
#define rson (rt<<1|1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

template<typename T>inline void read(T &x)
{
    x=0;
    static int p;p=1;
    static char c;c=getchar();
    while(!isdigit(c)){if(c=='-')p=-1;c=getchar();}
    while(isdigit(c)) {x=(x<<1)+(x<<3)+(c-48);c=getchar();}
    x*=p;
}

const int maxn=5e6+9;
const int mod=1e9+7;

//tr1::unordered_map<ll,ll>w1;
tr1::unordered_map<int,ll>w2;
bool check[maxn+10];
int prime[maxn+10];
//int mu[maxn+10];
int phi[maxn+10];

//ll sum1[maxn+10];
ll sum2[maxn+10];

ll power(ll x,ll n)
{
    ll ans=1;
    while(n)
    {
        if(n&1) ans=ans*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return ans;
}

void Mobius_phi(int N)
{
    mem(check,0);
    //mu[1]=1;
    phi[1]=1;
    int tot=0;
    for(int i=2;i<=N;i++)
    {
        if(!check[i])
        {
            prime[tot++]=i;
            //mu[i]=-1;
            phi[i]=i-1;
        }
        for(int j=0;j<tot;j++)
        {
            if(i*prime[j]>N) break;
            check[i*prime[j]]=true;
            if(i%prime[j]==0)
            {
                //mu[i*prime[j]]=0;
                phi[i*prime[j]]=(ll)phi[i]*prime[j]%mod;
                break;
            }
            else
            {
                //mu[i*prime[j]]=-mu[i];
                phi[i*prime[j]]=(ll)phi[i]*(prime[j]-1)%mod;
            }
        }
    }

    for(int i=1;i<=N;i++)
    {
        //sum1[i]=sum1[i-1]+mu[i];
        sum2[i]=(sum2[i-1]+(ll)phi[i])%mod;
    }

}

//ll djsmu(ll x)
//{
//    if(x<=5000000) return sum1[x];
//    if(w1[x]) return w1[x];
//    ll ans=1;//h(x)前n项和
//    for(ll l=2,r;l<=x;l=r+1)
//    {
//        r=x/(x/l);
//        ans-=(r-l+1)*djsmu(x/l);
//    }
//    w1[x]=ans;
//    return ans;
//}


//ll inv4=power(4LL,mod-2);
//ll inv6=power(6LL,mod-2);
ll inv2=power(2LL,mod-2);
//
//ll S2(ll n)
//{
//    n%=mod;
//    while(n<0) n+=mod;
//    return n*(n+1)%mod*(2*n+1)%mod*inv6%mod;
//}
//
//ll S3(ll n)
//{
//    n%=mod;
//    while(n<0) n+=mod;
//    return n*n%mod*(n+1)%mod*(n+1)%mod*inv4%mod;
//}
//
ll S1(ll n)
{
    n%=mod;
    while(n<0) n+=mod;
    return n*(n+1)%mod*inv2%mod;
}

ll djsphi(ll n)
{
    if(n<=1000000) return sum2[n]%mod;
    if(w2[n]!=0) return w2[n];
    ll ans=S1(n);
    for(ll l=2,r;l<=n;l=r+1)
    {
        r=n/(n/l);
        ans-=(r-l+1)*djsphi(n/l)%mod;
        while(ans<0) ans+=mod;
    }
    return w2[n]=ans;
}

int main()
{
    //FAST_IO;
    //freopen("input.txt","r",stdin);

    //cout<<inv2<<" "<<inv6<<endl;

    Mobius_phi(1000000);

    ll n;
    read(n);

    ll ans=0;

    ans=djsphi(n);

    printf("%lld\n",ans);

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值