Summer D17 T2 simple question 1 (Du teach sieve)

Title Description

Jia one hundred glass just learned Du teach sieve, want a very simple question practice your hand. Gives N, requirements:

To 100% of the data, N <= 1e10.

answer

Use morning talk can be obtained

 

And then directly set template like

 

#include<bits/stdc++.h>
using namespace std;

#define ll long long
const int mod=1000000007;
const ll inv=500000004;
const int maxn=6000000;
ll n;
ll cnt,prime[maxn+6],mu[maxn+6],s[maxn+6];
bool not_prime[maxn+6];
//h=f*g
//f(x)=x*mu(x)
//g(x)=x
//h(x)=n*[n==1]

void init(){
    s[1]=mu[1]=1;
    for(int i=2;i<=maxn;i++){
        if(!not_prime[i]){
            prime[++cnt]=i;
            mu[i]=-1;
        }
        s[i]=((s[i-1]+i*mu[i])%mod+mod)%mod;
        for(int j=1;prime[j]*i<=maxn;j++){
            not_prime[i*prime[j]]=true;
            if(i%prime[j]) mu[i*prime[j]]=-mu[i];
            else {
                mu[i*prime[j]]=0;
                break;
            }
        }
    }
}

map<ll,ll> mp;

ll djs(ll n){
    if(n<=maxn) return s[n];
    if(mp[n]) return mp[n];
    ll ans=1;
    for(ll l=2,r;l<=n;l=r+1){
        r=n/(n/l);
        ll cx=((l+r)%mod)*((r-l+1)%mod)%mod*inv%mod;
        ans=(ans-cx*djs(n/l)%mod)%mod;
    }
    return mp[n]=(ans+mod)%mod;
}

int main(){
    freopen("b.in","r",stdin);
    freopen("b.out","w",stdout);
    scanf("%lld",&n);
    init();
    printf("%lld",djs(n));
}
View Code

CAUTION burst long long, l, r may reach 1E10;

Operation is also divisible block inside.

 

Guess you like

Origin www.cnblogs.com/sto324/p/11272422.html