BZOJ2301 / LG2522 "HAOI2011" Problem B Mobius inversion on the block number

Problem Description

BZOJ2301

LG2522


Dirichlet convolution and Möbius function

Today zzk gods talked about Dirichlet convolution, number theory and block Mobius inversion.

Several arithmetical function

\[1(x)=1\]

\[id(x)=x\]

\[id^k(x)=x^k\]

\[\varepsilon(x)=\begin{cases}1&x=1\\0&x\neq1\end{cases}\]

Dirichlet convolution

There function \ (F (X), G (X) \) , if the function \ (h (x) = \ sum \ limits_ {d | x} {f (d) g (\ frac {x} {d} )} \) , called \ (h (x) \) is \ (f (x), g (x) \) convolution.

Referred to as \ (h (x) = f (x) * g (x) \)

Dirichlet convolution has the following properties:

  • Commutative, i.e. \ (f * g = g * f \)

  • The distribution ratio, i.e. \ ((a * b) * c = a * (b * c) \)

  • If \ (f, g \) is a multiplicative function, the \ (f * g \) is the product function, namely \ (f * g (mn) = f * g (m) \ times f * g (n ) ((n, m) = 1) \)

Unit Yuan \ (\ varepsilon \)

If \ (F * G = \ varepsilon \) , then \ (F \) and \ (G \) are inverse

Mobius function

\ (\ mu (x) \ ) represents the Mobius function.

For \ (X \) should have a unique prime decomposition theorem, the \ (X \) is represented as \ (X = \ prod_. 1 = {I} ^ {K} ^ {P_i} C_i \) , there

\[\mu(x)=\begin{cases}0&\exists c_i \ge 2\\(-1)^{k}&\forall c_i=1\end{cases}\]

Mobius function is a multiplicative function, i.e., to satisfy the \ ((x, y) = 1 \) a \ (X, Y \) , there are \ (\ mu (xy) = \ mu (x) \ times \ mu (y) \)

Important properties \ (\ sum \ limits_ {d | x} {\ mu (d)} = \ varepsilon = \ begin {cases} 0 & x \ neq 1 \\ 1 & x = 1 \ end {cases} \)

Mobius inversion

Sets of equations:

\[f(x)=\sum\limits_{d|x}g(d) \Rightarrow g(x)=\sum\limits_{d|x}f(d)\mu(\frac{x}{d})\]

Using Dirichlet convolution to explain, that \ (f = g * 1, g = f * \ mu \)


Block number theory

Simple questions

On the block number is a general problem seeking \ (\ sum_ {d = 1 } ^ n {\ lfloor \ frac {n} {d} \ rfloor} \)


answer

Is intended to claim the title \ (\ sum \ limits_ {i = a} ^ {b} {\ sum \ limits_ {j = c} ^ {d} {[(i, j) == k]}} \)

Obviously by difference, the problem and minimize the \ (\ sum \ limits_ {i = 1} ^ {n} {\ sum \ limits_ {j = 1} ^ {m} {[(i, j) == k] }} \)

On both sides may be removed by simultaneous \ (K \) , to give

\[\sum\limits_{i=1}^{\frac{n}{k}}{\sum\limits_{j=1}^{\frac{m}{k}}{[(i,j)==1]}}\]

Consider the greatest common divisor of \ (1 \) requirements conceivable \ ([(i, j) == 1] \) condition may be changed directly \ (\ varepsilon ((i, j)) \)

And because \ (\ varepsilon ((I, J)) = \ SUM \ limits_ {D | (I, J)} {\ MU (D)} \) , it is converted to the formula

\[\sum\limits_{i=1}^{\frac{n}{k}}{\sum\limits_{j=1}^{\frac{m}{k}}{\sum\limits_{d|(i,j)}{\mu(d)}}}\]


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std;

const int maxn=50000;

int T;

void Init(void){
    scanf("%d",&T);
}

int p[maxn+7],pr[maxn+7],miu[maxn+7],s[maxn+7];
int tot;

void preprocess(){
    miu[1]=1;
    for(int i=2;i<=maxn;i++){
        if(!p[i]) p[i]=i,pr[++tot]=i,miu[i]=-1;
        for(int j=1;j<=tot;j++){
            if(i*pr[j]>maxn||p[i]<pr[j]) break;
            p[i*pr[j]]=pr[j];
            if(i%pr[j]) miu[i*pr[j]]=-miu[i];
            else miu[i*pr[j]]=0;
        }
    }
    for(int i=1;i<=maxn;i++) s[i]=s[i-1]+miu[i];
}

int calc(int x,int y){
    if(x>y) swap(x,y);
    if(x==0||y==0) return 0;
    int res(0);
    for(int l=1,r;l<=x;l=r+1){
        r=min(x/(x/l),y/(y/l));
        res+=(s[r]-s[l-1])*(x/l)*(y/l);
    }
    return res;
}


void Work(void){
    preprocess();
    while(T--){
        int a,b,c,d,k;
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
        --a,--c;
        printf("%d\n",calc(b/k,d/k)+calc(a/k,c/k)-calc(a/k,d/k)-calc(b/k,c/k));
    }
}

int main(){
    Init();
    Work();
    return 0;
}

Guess you like

Origin www.cnblogs.com/liubainian/p/12040455.html