bzoj2154: Crash的数字表格 莫比乌斯反演

题意:求\(\sum_{i=1}^n \sum_{j=1}^m\frac{i*j}{gcd(i,j)}\)
题解:\(ans=\sum_{i=1}^n\sum_{j=1}^m \frac{i*j}{gcd(i,j)}\)
\(=\sum_{d=1}^{min(n,m)}d\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}\sum_{j=1}^{\lfloor \frac{m}{d} \rfloor}i*j[gcd(i,j)==1]\)
\(=\sum_{d=1}^{min(n,m)}d\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}\sum_{j=1}^{\lfloor \frac{m}{d} \rfloor}i*j \sum_{x|gcd(i,j)}\mu(x)\)
\(=\sum_{d=1}^{min(n,m)}d\sum_{x=1}^{min(\lfloor \frac{n}{d} \rfloor,\lfloor \frac{m}{d} \rfloor)}x^2\mu(x)\sum_{i=1}^{\lfloor \frac{n}{d*x} \rfloor}\sum_{j=1}^{\lfloor \frac{m}{d*x} \rfloor}i*j\)
最后里层和外层都能整除分块,sum可以处理成\(mu*i^2\)的前缀和,
取模太多会T

/**************************************************************
    Problem: 2154
    User: walfy
    Language: C++
    Result: Accepted
    Time:18528 ms
    Memory:167308 kb
****************************************************************/
 
//#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 20101009
#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=10000000+10,maxn=400000+10,inf=0x3f3f3f3f;
 
int prime[N],cnt,mu[N];
ll sum[N];
bool mark[N];
void init()
{
    mu[1]=1;
    for(int i=2;i<N;i++)
    {
        if(!mark[i])prime[++cnt]=i,mu[i]=-1;
        for(int j=1;j<=cnt&&prime[j]*i<N;j++)
        {
            mark[prime[j]*i]=1;
            mu[prime[j]*i]=-mu[i];
            if(i%prime[j]==0){mu[prime[j]*i]=0;break;}
        }
    }
    for(ll i=1;i<N;i++)
    {
        sum[i]=sum[i-1]+i*i%mod*mu[i];
        add(sum[i],mod);
    }
}
ll F(ll n,ll m)
{
    if(n>m)swap(n,m);
    ll ans=0;
    for(ll i=1,j;i<=n;i=j+1)
    {
        j=Min(n/(n/i),m/(m/i));
        ll t1=n/i,t2=m/i;
        t1=t1*(t1+1)/2;t2=t2*(t2+1)/2;
        t1%=mod,t2%=mod;
        ll te=(sum[j]-sum[i-1]);add(te,mod);
        add(ans,te*t1%mod*t2%mod);
    }
    return ans;
}
int main()
{
    init();
    ll n,m;scanf("%lld%lld",&n,&m);
    if(n>m)swap(n,m);
    ll ans=0;
    for(ll i=1,j;i<=n;i=j+1)
    {
        j=Min(n/(n/i),m/(m/i));
        add(ans,((j+i)*(j-i+1)/2)%mod*F(n/i,m/i)%mod);
    }
    printf("%lld\n",ans);
    return 0;
}
/********************
 
********************/
posted @ 2018-08-11 19:21  walfy  阅读(120)  评论(0编辑  收藏  举报