[BZOI 3994] [SDOI2015] about the number and the number of

[BZOI 3994] [SDOI2015] about the number and the number of

Face questions

Set d (x) is the number of the divisor x, given N, M, ask \ (\ sum _ {i = 1} ^ n \ sum_ {i = 1} ^ md (i \ times j) \)

T group asked, \ (N, M, T \ Leq 50000 \)

analysis

First, there is a conclusion

\[d(nm)= \sum _{i |n} \sum _{j|m} [gcd(i,j)=1]\]

This is because the number of about nm can be expressed as (i \ times \ frac {m } {j} \) \ forms and in order not to repeat the count, to ensure that the \ (gcd (i, j) = 1 \)

Therefore, we can begin to push formula

\[ans= \sum_{p=1}^n \sum_{q=1}^m \sum_{i|p} \sum _{j|q} [gcd(i,j)=1] \]

Each of the noted \ ((i, j) \ ) have p, q they generate multiple \ (\ lfloor \ frac {n } {i} \ rfloor \ times \ lfloor \ frac {m} {j} \ rfloor \) contributions

\[= \sum_{i=1}^n \sum_{j=1} ^m [gcd(i,j)=1] \lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{j} \rfloor \]

\[= \sum_{i=1}^n \sum_{j=1} ^m \varepsilon (gcd(i,j)) \lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{j} \rfloor \]

The \ (\ varepsilon (n) = \ sum_ {d | n} \ mu (d) \)

\[= \sum_{i=1}^n \sum_{j=1} ^m \lfloor \frac{n}{i} \rfloor \lfloor \frac{m}{j} \rfloor \sum_{d|gcd(i,j)} \mu(d)\]

Change the order of summation, the first enumeration d, obviously if \ (d | gcd (i, J) \) , then \ (d | i, d | J \) ,

D i directly replace multiple du, j d replace multiple DV ( \ (U, V \ N ^ in +, du \ n-Leq, DV \ Leq m \) )

\[= \sum_{d=1}^{min(n,m)} \mu(d) \sum_{u=1}^{\lfloor n/d \rfloor} \sum_{v=1}^{\lfloor m/d \rfloor} \lfloor \frac{n}{du} \rfloor \lfloor \frac{m}{dv} \rfloor\]

\[= \sum_{d=1}^{min(n,m)} \mu(d) \sum_{u=1}^{\lfloor n/d \rfloor} \sum_{v=1}^{\lfloor m/d \rfloor} \lfloor \frac{ \lfloor n/d \rfloor}{u} \rfloor \lfloor \frac{\lfloor m/d \rfloor}{v} \rfloor\]

\[= \sum_{d=1}^{min(n,m)} \mu(d) \sum_{u=1}^{\lfloor n/d \rfloor} \lfloor \frac{ \lfloor n/d \rfloor}{u} \rfloor \sum_{v=1}^{\lfloor m/d \rfloor} \lfloor \frac{\lfloor m/d \rfloor}{v} \rfloor\]

\(g(n) = \sum _{d=1}^n \lfloor \frac{n}{d} \rfloor\)

\[=\sum_{d=1}^{min(n,m)} \mu(d) g(\lfloor \frac{n}{d} \rfloor) g(\lfloor \frac{m}{d} \rfloor)\]

Consider how quickly evaluated. Single \ (g (n) \) may be employed Number Theory block in the (O (\ sqrt n) \ ) \ determined in time, the total time complexity \ (O (n-\ sqrt n-) \) . Then linear sieve the \ (\ MU \) , dimension \ (\ mu, g \) prefix and

On each interrogation by the number of d enum method can block, the total time complexity \ (O (n \ sqrt n + T \ sqrt n) \)

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 50000
using namespace std;
typedef long long ll;
int t;
int n,m;
int cnt;
bool vis[maxn+5];
int prime[maxn+5];
int mu[maxn+5];
ll sum_mu[maxn+5];
int g[maxn+5];
void sieve(int n){
    mu[1]=1;
    for(int i=2;i<=n;i++){
        if(!vis[i]){
            prime[++cnt]=i;
            mu[i]=-1;
        }
        for(int j=1;j<=cnt&&i*prime[j]<=n;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0){
                mu[i*prime[j]]=0;
                break;
            }else{
                mu[i*prime[j]]=-mu[i];
            }
        }
    }
    for(int i=1;i<=n;i++) sum_mu[i]=sum_mu[i-1]+mu[i];
    for(int i=1;i<=n;i++){
        int l,r;
        for(l=1;l<=i;l=r+1){
            r=i/(i/l);
            g[i]+=(ll)(r-l+1)*(i/l);
        }
    }
}

ll calc(int n,int m){
    int l,r;
    if(n<m) swap(n,m);
    ll ans=0;
    for(l=1;l<=m;l=r+1){
        r=min(n/(n/l),m/(m/l));
        ans+=(sum_mu[r]-sum_mu[l-1])*g[n/l]*g[m/l];
    }
    return ans;
}

int main(){
    sieve(maxn);
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&m);
        printf("%lld\n",calc(n,m));
    }   
}

Guess you like

Origin www.cnblogs.com/birchtree/p/11360921.html
Recommended