1. 程式人生 > >[bzoj4407] 於神之怒加強版

[bzoj4407] 於神之怒加強版

Description

給出N,M,K.求

img

Input

輸入有多組資料,輸入資料的第一行兩個正整數T,K,代表有T組資料,K的意義如上所示,下面第二行到第T+1行,每行為兩個正整數N,M,其意義如上式所示。

Output

如題

Sample Input

1 2
3 3 

Sample Output

20

HINT

1<=N,M,K<=5000000,1<=T<=2000

莫比烏斯反演,推下式子:
\[ \begin{align} ans&=\sum _{d=1}^{min(n,m)}d^k \sum _{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum _{j=1}^{\lfloor\frac{m}{d}\rfloor} [gcd(i,j)=1]\\ &=\sum _{d=1}^{min(n,m)}d^k \sum _{i=1}^{\lfloor\frac{n}{d}\rfloor} \sum _{j=1}^{\lfloor\frac{m}{d}\rfloor} \sum _{d^\prime|i\&d^\prime|j} \mu(d^\prime)\\ &=\sum _{d=1}^{min(n,m)}d^k \sum _{d^\prime} \mu(d^\prime)\lfloor\frac{n}{dd^\prime}\rfloor \lfloor\frac{m}{dd^\prime}\rfloor \\ &=\sum _{d=1}^{min(n,m)}d^k \sum _{d|T} \mu(\frac{T}{d})\lfloor\frac{n}{T}\rfloor \lfloor\frac{m}{T}\rfloor \\ &=\sum _{T} \lfloor\frac{n}{T}\rfloor \lfloor\frac{m}{T}\rfloor \sum _{d|T}d^k \mu(\frac{T}{d})\\ \end {align} \]


\(f\)為:
\[ f(n)=\sum_{d|T}d^k\mu(\frac{T}{d}) \]
然後我們可以線篩出\(\mu\)然後大力算\(f\)然後數論分塊,由於調和級數,複雜度為\(O(nlog(n)+q\sqrt{n})\)

然而交一發T掉了,,,可以考慮線篩出\(f\),考慮質數\(p\),對於\(f(n*p)\),展開得:
\[ f(n*p)=\sum_{d|T}d^k\mu(\frac{T*p}{d})+\sum_{d|T}(d*p)^k\mu(\frac{T}{d}) \]
然後若\(p|n\),可得\(f(n*p)=f(n)*p^k\),否則可得\(f(n*p)=f(n)*(p^k-1)\)

然後線篩即可,複雜度\(O(n+q\sqrt{n})\)

暴力算\(f\)的程式碼(TLE):

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

#define int long long 

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

const int maxn = 5e6+1;
const int mod = 1e9+7;

int pri[maxn],vis[maxn],mu[maxn],f[maxn],tot,n,m,k;

int qpow(int a,int x) {
    int res=1;
    for(;x;x>>=1,a=a*a%mod) if(x&1) res=res*a%mod;
    return res;
}

void sieve() {
    mu[1]=1;
    for(int i=2;i<=n;i++) {
        if(!vis[i]) pri[++tot]=i,mu[i]=-1;
        for(int j=1;j<=tot&&i*pri[j]<=n;j++) {
            vis[i*pri[j]]=1;
            if(!(i%pri[j])) {mu[i*pri[j]]=0;break;}
            mu[i*pri[j]]=-mu[i];
        }
    }
    for(int d=1;d<=n;d++) {
        int res=qpow(d,k);
        for(int i=1;i*d<=n;i++) (f[i*d]+=res*mu[i])%=mod;
    }
    for(int i=1;i<=n;i++) f[i]=(f[i]+f[i-1])%mod;
}

signed main() {
    int t;n=maxn-1;read(t),read(k);sieve();
    while(t--) {
        read(n),read(m);
        int T=1,ans=0;
        while(T<=n&&T<=m) {
            int pre=T;T=min(n/(n/T),m/(m/T));
            ans=(ans+(n/T)*(m/T)%mod*(f[T]-f[pre-1])%mod)%mod;
            T++;
        }
        write((ans%mod+mod)%mod);
    }
    return 0;
}

線篩\(f\)

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

#define int long long 

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}

void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

const int maxn = 5e6+1;
const int mod = 1e9+7;

int pri[maxn],vis[maxn],mu[maxn],f[maxn],tot,n,m,k,p[maxn];

int qpow(int a,int x) {
    int res=1;
    for(;x;x>>=1,a=a*a%mod) if(x&1) res=res*a%mod;
    return res;
}

void sieve() {
    f[1]=1;
    for(int i=2;i<=n;i++) {
        if(!vis[i]) pri[++tot]=i,p[tot]=qpow(i,k),f[i]=p[tot]-1;
        for(int j=1;j<=tot&&i*pri[j]<=n;j++) {
            vis[i*pri[j]]=1;
            if(!(i%pri[j])) {f[i*pri[j]]=f[i]*p[j]%mod;break;}
            f[i*pri[j]]=f[i]*(p[j]-1)%mod;
        }
    }for(int i=1;i<=n;i++) f[i]=(f[i]+f[i-1])%mod;
}

signed main() {
    int t;n=maxn-1;read(t),read(k);
    //int PRE=clock();
    sieve();
    //cerr << (double) (clock()-PRE)/CLOCKS_PER_SEC << endl;
    while(t--) {
        read(n),read(m);
        int T=1,ans=0;
        while(T<=n&&T<=m) {
            int pre=T;T=min(n/(n/T),m/(m/T));
            ans=(ans+(n/T)*(m/T)%mod*(f[T]-f[pre-1])%mod)%mod;
            T++;
        }
        write((ans%mod+mod)%mod);
    }
    return 0;
}

注意下bzoj不能用clock函式,否則狂RE不止,別問我怎麼知道的QAQ