BZOJ 3994: [SDOI2015] about the number and the number of inversion Möbius

title

BZOJ 3994

LUOGU 3327

Description

Set \ (d (x) \) to (X \) \ number divisors, given \ (N, M \) , seeking \ (\ sum ^ N_ {i = 1} \ sum ^ M_ {j = D}. 1 (ij of) \) . \ (. 1 \ leqslant N, M, T \ leqslant. 5 \ ^ 10. 4 Times \) .

analysis

First by a magical formula very beginning . 1 : \
[D (I, J) = \ sum_ {X | I} \ sum_ {Y | J} [\ GCD (X, Y) =. 1] \]

Therefore we are in demand this thing:
\ [\ SUM ^ N_ {i = 1} \ SUM ^ M_ {J = 1} \ sum_ {the X-| i} \ sum_ {the y-| J} [\ gcd (the X-, the y- ) = 1] \]

Changing the order of summation, to enumerate factor \ (X \) and \ (Y \) : \
[\ SUM of N_ {X ^}. 1 = \ ^ SUM of M_. 1} = {Y \ lfloor \ {N} {X FRAC } \ rfloor \ lfloor \ frac { M} {y} \ rfloor [\ gcd (x, y) = 1] \]

The \ (X \) and \ (Y \) into \ (I \) and \ (J \) it:
\ [\ {I SUM of N_ ^. 1} = \ ^ SUM. 1 of M_ {J} = \ lfloor \ frac {N} {i} \ rfloor \ lfloor \ frac {M} {j} \ rfloor [\ gcd (i, j) = 1] \]

Mobius inversion begins below, provided:
\ [F (X) = \ {I SUM of N_ ^. 1} = \ ^ SUM. 1 of M_ {J} = \ lfloor \ FRAC {N} {I} \ rfloor \ lfloor \ frac {M} {j} \ rfloor [\ gcd (i, j) = x] \\ g (x) = \ sum_ {x | d} f (d) \]

则有:
\[ g(x)=\sum^N_{i=1}\sum^M_{j=1}\lfloor\frac{N}{i}\rfloor\lfloor\frac{M}{j}\rfloor[x|\gcd(i,j)] \]

We \ (X \) proposed can be eliminated \ (\ GCD \) affected:
\ [G (X) = \ SUM ^ {\ FRAC {N} {X}} _ {I =. 1} \ SUM ^ {\ frac {M} { y}} _ {j = 1} \ lfloor \ frac {N} {ix} \ rfloor \ lfloor \ frac {M} {jx} \ rfloor \]

Then according to \ (f (x) \) defined, to give the answer is \ (F (. 1) \) , and because:
\ [F (n-) = \ sum_ {n-| D} \ MU (\ FRAC {D} {n}) g (d) \]

所以
\[ f(1)=\sum_{1|d}\mu(\frac{d}{1})g(d)=\sum_{i=1}\mu(i)g(i) \]

Next, consider how to find \ (G (X) \) , we can first calculate \ (s (x) = \ sum \ limits_ {i = 1} ^ {x} \ left \ lfloor \ frac {x} {i } \ right \ rfloor \) , can be \ (\ Theta (1) \ ) is calculated \ (G (X) \) .

Time complexity: \ (\ Theta (T \ n-sqrt {}) \) .

code

#include<bits/stdc++.h>

typedef long long ll;
const int maxn=5e4;
typedef int iarr[maxn+10];

namespace IO
{
    char buf[1<<15],*fs,*ft;
    inline char getc() { return (ft==fs&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),ft==fs))?0:*fs++; }
    template<typename T>inline void read(T &x)
    {
        x=0;
        T f=1, ch=getchar();
        while (!isdigit(ch) && ch^'-') ch=getchar();
        if (ch=='-') f=-1, ch=getchar();
        while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
        x*=f;
    }

    char Out[1<<24],*fe=Out;
    inline void flush() { fwrite(Out,1,fe-Out,stdout); fe=Out; }
    template<typename T>inline void write(T x,char str)
    {
        if (!x) *fe++=48;
        if (x<0) *fe++='-', x=-x;
        T num=0, ch[20];
        while (x) ch[++num]=x%10+48, x/=10;
        while (num) *fe++=ch[num--];
        *fe++=str;
    }
}

using IO::read;
using IO::write;

iarr mu,d,cnt,prime;
bool isnotpr[maxn+10];
int tot;
inline void pre()
{
    mu[1]=d[1]=1;
    for (int i=2; i<=maxn; ++i)
    {
        if (!isnotpr[i]) prime[++tot]=i, mu[i]=-1, d[i]=2, cnt[i]=1;
        for (int j=1; prime[j]*i<=maxn; ++j)
        {
            isnotpr[prime[j]*i]=1;
            if (i%prime[j]==0)
            {
                mu[prime[j]*i]=0, d[prime[j]*i]=d[i]/(cnt[i]+1)*(cnt[i]+2), cnt[prime[j]*i]=cnt[i]+1;
                break;
            }
            mu[prime[j]*i]=-mu[i], d[prime[j]*i]=d[i]<<1, cnt[prime[j]*i]=1;
        }
    }
    for (int i=1; i<=maxn; ++i) mu[i]+=mu[i-1], d[i]+=d[i-1];
}

int main()
{
    pre();
    int T;read(T);
    while (T--)
    {
        int n,m;read(n);read(m);
        if (n>m) std::swap(n,m);
        ll ans=0;
        for (int l=1,r; l<=n; l=r+1)
        {
            r=std::min(n/(n/l),m/(m/l));
            ans+=1ll*(mu[r]-mu[l-1])*d[n/l]*d[m/l];
        }
        write(ans,'\n');
    }
    IO::flush();
    return 0;
}

  1. See proof of this formula Siyuan .

Guess you like

Origin www.cnblogs.com/G-hsm/p/11417797.html