[51nod][积性函数][杜教筛]最小公倍数之和 V3

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1238

sol:

https://www.cnblogs.com/Blue233333/p/8320009.html
比较套路就不推了(虽然我做了2天)
具体可以见我杜教筛小结的那个博客,套路题
就是化到求i^2phi(i)的前缀和的时候卷一下id^2,就是个很好求前缀和的函数了。
从今天开始要记录一下自己每天debug的时候的一些错误
这题ll的时候b-i+1和b+i是超过mod的范围的,所以要mod,getsum里面也是要提前mod了。

#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;

ll n;
ll m;
ll ans;
const ll pyz=1e9+7;
inline ll ksm(ll s,ll t)
{
    ll res=1;
    while(t)
    {
        if(t&1) res=(ll)res*s%pyz;
        s=(ll)s*s%pyz;
        t>>=1;
    }
    return res;
}
const ll N=1e6;
ll s[N+7],phi[N+7],inv6,inv2;
inline ll sqr(ll x)
{
    return (ll)x*x%pyz;
}
inline ll gsum(ll x)
{
    x%=pyz;
    return sqr((ll)x*(x+1)%pyz*inv2%pyz);
}
inline ll get_sum(ll x)
{
    x%=pyz;
    return (ll)x*(x+1)%pyz*(x<<1|1)%pyz*inv6%pyz;
}
inline ll get_s(ll x)
{
    ll ans=0;
    if(x<=N) return phi[x];
    if(s[n/x]) return s[n/x];
    s[n/x]=gsum(x);
    ll a,b;
    for(ll i=2;i<=x;++i)
    {
        a=x/i;
        b=x/a;
        s[n/x]=(s[n/x]-(ll)(get_sum(b)-get_sum(i-1)+pyz)%pyz*get_s(a)%pyz+pyz)%pyz;
        i=b;
    }
    if(s[n/x]<0)
    cout<<x<return s[n/x];
}
bool is[N+7];
ll prime[N];
int main()
{
//  freopen("1238.in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n;
    inv2=ksm(2,pyz-2);
    inv6=ksm(6,pyz-2);
    phi[1]=1;
    for(ll i=2;i<=N;++i)
    {
        if(!is[i]) prime[++prime[0]]=i,phi[i]=i-1;
        for(ll j=1;j<=prime[0]&&i*prime[j]<=N;++j)
        {
            is[i*prime[j]]=1;
            if(!(i%prime[j]))
            {
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }
            phi[i*prime[j]]=phi[i]*(prime[j]-1);
        }
        phi[i]=(phi[i-1]+(ll)i*i*phi[i]%pyz)%pyz;
    }
    ll ans=0;
    ll a,b;
    for(ll i=1;i<=n;++i)
    {
        a=n/i;
        b=n/a;
        ans=(ans+(ll)(b-i+1)%pyz*((i+b)%pyz)%pyz*inv2%pyz*get_s(a)%pyz)%pyz;
        i=b;
    }
    cout<

你可能感兴趣的:(积性函数,51nod,杜教筛)