PE642

PE642

EES秒掉辣 https://blog.csdn.net/Feynman1999/article/details/82874491 %%%min_25

// PE 642  最大质因子 前缀和
// author : Feynman1999
// f(1)=0   f(p)=p   f(p^e)=p
// running time: 12.8s    using Intel I5-3470
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9;
ll n,M;
vector<ll> pre[2],hou[2],primes;
ll ff(ll A, ll B){
    return (A+B)%2?(B-A+1)/2%mod*((A+B)%mod)%mod:(A+B)/2%mod*((B-A+1)%mod)%mod;
}
ll dfs(ll res, int last, int f){
    ll ret;
    if(f>0) ret =((res > M ? hou[1][n/res] : pre[1][res]) - pre[1][primes[last]-1]+mod)%mod;
    else ret = hou[1][1];
    for(int i=last;i<(int) primes.size();++i){
        int p = primes[i];
        if((ll)p*p > res) break;
        for(ll q=p,nres=res;q*p<=res;q*=p){
            ret =(ret + dfs(nres/=p,i+1,p))%mod ;
            ret =(ret + p)%mod;
        }
    }
    return ret;
}
ll solve(ll n){
    M=sqrt(n);
    for(int i=0;i<2;++i){
        pre[i].clear();pre[i].resize(M+1);
        hou[i].clear();hou[i].resize(M+1);
    }
    primes.clear();primes.reserve(M+1);
    for(int i=1;i<=M;++i){
        pre[0][i]=i-1;
        pre[1][i]=ff(2,i);
        hou[0][i]=(n/i-1)%mod;
        hou[1][i]=ff(2,n/i);
    }
    for(int p=2;p<=M;++p){
        if(pre[0][p]==pre[0][p-1]) continue;
        primes.push_back(p);
        const ll q=(ll)p*p,m=n/p,pnt0=pre[0][p-1],pnt1=pre[1][p-1];
        const int mid=M/p;
        const int End=min((ll)M,n/q);
        for(int i=1;i<=mid;++i){
            hou[0][i]=(hou[0][i]-(hou[0][i*p]-pnt0))%mod;
            hou[1][i]=(hou[1][i]-(hou[1][i*p]-pnt1)*p%mod)%mod;
        }
        for(int i=mid+1;i<=End;++i){
            hou[0][i]=(hou[0][i]-(pre[0][m/i]-pnt0))%mod;
            hou[1][i]=(hou[1][i]-(pre[1][m/i]-pnt1)*p%mod)%mod;
        }
        for(int i=M;i>=q;--i){
            pre[0][i]=(pre[0][i]-(pre[0][i/p]-pnt0))%mod;
            pre[1][i]=(pre[1][i]-(pre[1][i/p]-pnt1)*p%mod)%mod;
        }
    }
    primes.push_back(M+1);
    return n>1 ? dfs(n,0,0) : 0;
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    cout<<(solve(n)%mod+mod)%mod<<endl<<endl;
    return 0;
}




评论

登录之后就可以评论 / 回复啦(#^.^#)    点此登录    点此注册

评论列表

暂无评论!快写一条吧(๑′ᴗ‵๑)