码迷,mamicode.com
首页 > 其他好文 > 详细

51nod1220 约数之和

时间:2018-10-05 18:56:06      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:tor   51nod   int   i+1   temp   a*   fas   ***   lld   

题意:求\(\sum_{i=1}^n\sum_{j=1}^nD(ij),D是约数和函数\)
题解:首先有个结论\(D(ij)=\sum_{x|i}\sum_{y|j}\frac{x*j}{y}*[(x,y)==1]\)
\(=\sum_{i=1}^n\sum_{j=1}^n\sum_{x|i}\sum_{y|j}\sum_{d|(x,y)}\frac{x*j}{y}*\mu(d)\)
\(=\sum_{d=1}^n\mu(d)\sum_{d|x}\sum_{d|y}\sum_{x|i}\sum_{y|j}\frac{x*j}{y}\)
\(=\sum_{d=1}^n\mu(d)\sum_{d|xd}\sum_{d|yd}\sum_{x|id}\sum_{y|jd}\frac{x*j}{y}\)
\(=\sum_{d=1}^n\mu(d)\sum_{x=1}^{\lfloor \frac{n}{d} \rfloor}\sum_{y=1}^{\lfloor \frac{n}{d} \rfloor}\sum_{i=1}^{\lfloor \frac{n}{dx} \rfloor}\sum_{j=1}^{\lfloor \frac{n}{dy} \rfloor}\frac{x*j*y*d}{y}\)
\(=\sum_{d=1}d*\mu(d)\sum_{x=1}^{\lfloor \frac{n}{d} \rfloor}x*{\frac{n}{dx}}\sum_{y=1}^{{\lfloor \frac{n}{d} \rfloor}}\sum_{j=1}^{{\lfloor \frac{n}{dy} \rfloor}}j\)
\(=\sum_{d=1}d*\mu(d)\sum_{x=1}^{\lfloor \frac{n}{d} \rfloor}x*{\frac{n}{dx}}\sum_{y=1}^{\lfloor \frac{n}{d} \rfloor}y*{\frac{n}{dy}}\)
后半部分就是D的前缀和,\(\sum_{d=1}^nd*\mu(d){\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}D(i)}^2\)
到这里我们需要处理出\(f(i)=i*\mu(i)\)\(D(i)=\sum_{d|i}d\)的前缀和,对于f和id卷积即可,对于\(D=id*I\),\(D*\mu=id*I*\mu=id*e\)

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;

const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=2000000+10,maxn=3000000+10,inf=0x3f3f3f3f;

int prime[N],cnt;
ll D[N],num[N],mu[N],f[N],inv6=qp(6,mod-2),inv2=qp(2,mod-2);
bool mark[N];
map<ll,ll>DD,ff,muu;
void init()
{
    mu[1]=D[1]=1;
    for(int i=2;i<N;i++)
    {
        if(!mark[i])prime[++cnt]=i,mu[i]=-1,D[i]=num[i]=1+i;
        for(int j=1;j<=cnt&&i*prime[j]<N;j++)
        {
            mark[i*prime[j]]=1;
            if(i%prime[j]==0)
            {
                mu[i*prime[j]]=0;
                num[i*prime[j]]=num[i]*prime[j]+1;
                D[i*prime[j]]=D[i]/num[i]*num[i*prime[j]];
                break;
            }
            mu[i*prime[j]]=-mu[i];
            D[i*prime[j]]=D[i]*(1+prime[j]);
            num[i*prime[j]]=1+prime[j];
        }
    }
    for(ll i=1;i<N;i++)
    {
        add(D[i],D[i-1]);
        mu[i]=(mu[i]+mod)%mod;
        f[i]=mu[i]*i%mod;
        add(f[i],f[i-1]);
        add(mu[i],mu[i-1]);
    }
}
ll getmu(ll n)
{
    if(n<N)return mu[n];
    if(muu.find(n)!=muu.end())return muu[n];
    ll ans=1;
    for(ll i=2,j;i<=n;i=j+1)
    {
        j=n/(n/i);
        sub(ans,(j-i+1)%mod*getmu(n/i)%mod);
    }
    return muu[n]=ans;
}
ll getf(ll n)
{
    if(n<N)return f[n];
    if(ff.find(n)!=ff.end())return ff[n];
    ll ans=1;
    for(ll i=2,j;i<=n;i=j+1)
    {
        j=n/(n/i);
        ll te=(j-i+1)%mod*((i+j)%mod)%mod*inv2%mod;
        sub(ans,te*getf(n/i)%mod);
    }
    return ff[n]=ans;
}
ll getD(ll n)
{
    if(n<N)return D[n];
    if(DD.find(n)!=DD.end())return DD[n];
    ll ans=n%mod*(n%mod+1)%mod*inv2%mod;
    for(ll i=2,j;i<=n;i=j+1)
    {
        j=n/(n/i);
        ll te=(getmu(j)-getmu(i-1)+mod)%mod;
        sub(ans,te*getD(n/i)%mod);
    }
    return DD[n]=ans;
}
int main()
{
    init();
    ll n;scanf("%lld",&n);
    ll ans=0;
    for(ll i=1,j;i<=n;i=j+1)
    {
        j=n/(n/i);
        ll te=(getf(j)-getf(i-1)+mod)%mod;
        add(ans,te*getD(n/i)%mod*getD(n/i)%mod);
    }
    printf("%lld\n",ans);
    return 0;
}
/********************

********************/

51nod1220 约数之和

标签:tor   51nod   int   i+1   temp   a*   fas   ***   lld   

原文地址:https://www.cnblogs.com/acjiumeng/p/9745345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!