Luogu

LOJ

分析

下面都假设 $n\leq m$。

方法一

$$
\begin{aligned}
\prod_{i=1}^n\prod_{j=1}^mf_{\gcd(i,j)}&=\prod_{d=1}^nf_d\,^{\sum_{i=1}^n\sum_{j=1}^m[\gcd(i,j)=d]}\\
&=\prod_{d=1}^nf_d\,^{\sum_{i=1}^{n/d}\mu(i)\left\lfloor\frac{n}{id}\right\rfloor\left\lfloor\frac{m}{id}\right\rfloor}\\
&=\prod_{T=1}^n\left(\prod_{d|T}f_d\,^{\mu\left(\frac{T}{d}\right)}\right)^{\left\lfloor\frac{n}{T}\right\rfloor\left\lfloor\frac{m}{T}\right\rfloor}
\end{aligned}
$$

$\mathcal{O}(n\log n)$ 预处理括号内的东西,数论分块计算即可。

方法二

考虑对答案取对数,变为
$$
\begin{aligned}
\sum_{i=1}^n\sum_{j=1}^m\ln f_{\gcd(i,j)}&=\sum_{i=1}^n\sum_{j=1}^m\sum_{d|i,d|j}\sum_{k|d}\mu\left(\frac{d}{k}\right)\ln f_k\\
&=\sum_{d=1}^n\left(\sum_{k|d}\mu\left(\frac{d}{k}\right)\ln f_k\right)\left\lfloor\frac{n}{d}\right\rfloor\left\lfloor\frac{m}{d}\right\rfloor
\end{aligned}
$$
这样子再 exp 回去得到
$$
\prod_{d=1}^n\left(\prod_{k|d}f_k\,^{\mu\left(\frac{d}{k}\right)}\right)^{\left\lfloor\frac{n}{d}\right\rfloor\left\lfloor\frac{m}{d}\right\rfloor}
$$
推出来和上面是一样的。

代码

// ====================================
//   author: M_sea
//   website: https://m-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)
using namespace std;
typedef long long ll;

int read() {
    int X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
    return X*w;
}

const int N=1000000+10;
const int mod=1e9+7;
int qpow(int a,int b) { int c=1;
    for (;b;b>>=1,a=1ll*a*a%mod) if (b&1) c=1ll*c*a%mod;
    return c;
}

int p[N],v[N],cnt=0;
int mu[N],f[N],ivf[N],s[N];
void sieve(int n) {
    mu[1]=1;
    for (int i=2;i<=n;++i) {
        if (!v[i]) p[++cnt]=i,mu[i]=-1;
        for (int j=1;j<=cnt&&i*p[j]<=n;++j) {
            v[i*p[j]]=1;
            if (i%p[j]) mu[i*p[j]]=-mu[i];
            else break;
        }
    }
    f[1]=ivf[1]=1;
    for (int i=2;i<=n;++i)
        f[i]=(f[i-1]+f[i-2])%mod,ivf[i]=qpow(f[i],mod-2);
    for (int i=0;i<=n;++i) s[i]=1;
    for (int i=1;i<=n;++i) {
        if (!mu[i]) continue;
        for (int j=i;j<=n;j+=i)
            s[j]=1ll*s[j]*(mu[i]==1?f[j/i]:ivf[j/i])%mod;
    }
    for (int i=2;i<=n;++i) s[i]=1ll*s[i-1]*s[i]%mod;
}

int main() {
    sieve(1000000);
    for (int T=read();T;--T) {
        int n=read(),m=read(); if (n>m) swap(n,m);
        int ans=1;
        for (int l=1,r;l<=n;l=r+1) {
            r=min(n/(n/l),m/(m/l));
            int t=1ll*s[r]*qpow(s[l-1],mod-2)%mod;
            ans=1ll*ans*qpow(t,1ll*(n/l)*(m/l)%(mod-1))%mod;
        }
        printf("%d\n",ans);
    }
    return 0;
}
最后修改:2020 年 08 月 17 日 04 : 40 PM