P5221 Product(莫比乌斯反演)

CYJian的新春虐题赛- 比赛详情

[题目链接] https://www.luogu.org/problemnew/show/P5221

\(\prod_{i=1}^{N}\prod_{j=1}^{N}\frac{lcm(i,j)}{gcd(i,j)}\)

[题解] https://tbr-blog.blog.luogu.org/solution-p5221

对于分子:

\[ \begin{aligned} &\ \ \ \ \ \ \prod_{i=1}^{N}\prod_{j=1}^{N}i*j \\ &=(n!)^{2n}\\ \end{aligned} \]

对于分母:

\[ \begin{aligned} &\ \ \ \ \ \ \prod_{i=1}^{N}\prod_{j=1}^{N}gcd(i,j) \\ &=\prod_{d=1}^{n}\prod_{i=1}^{n}\prod_{j=1}^{n} [gcd(i,j)==d]\\ &=\prod_{d=1}^{n}d^{\sum_{i=1}^{n}\sum_{j=1}^{n}[gcd(i,j)==d]}\\ &=\prod_{d=1}^{n}d^{\sum_{i=1}^{\frac{n}{d}}\sum_{j=1}^{\frac{n}{d}}[gcd(i,j)==1]}\\ \end{aligned} \]

其中指数

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

就是仪仗队这道题

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define re register
#define debug printf("Now is Line : %d\n",__LINE__)
#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define ll long long
#define mod 104857601
il int read()
{
    re int x=0,f=1;re char c=getchar();
    while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
    while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();
    return x*f;
}
#define maxn 1000000+5
int n,cnt,ans1=1,prim[80000],ans2=1,pai[maxn];
bool vis[maxn];
il int qpow(int a,ll b)
{
    int r=1;
    while(b)
    {
        if(b&1ll) r=1ll*r*a%mod;
        b>>=1ll;
        a=1ll*a*a%mod;
    }
    return r;
}
int main()
{
    n=read();
    pai[1]=1;
    for(re int i=2;i<=n;++i)
    {
        ans1=1ll*ans1*i%mod;
        if(!vis[i]) prim[++cnt]=i,pai[i]=i-1;
        for(re int j=1;j<=cnt;++j)
        {
            if(prim[j]*i>n) break;
            vis[prim[j]*i]=1;
            if(i%prim[j]==0) {pai[i*prim[j]]=pai[i]*prim[j];break;}
            pai[i*prim[j]]=pai[prim[j]]*pai[i];
        }
    }
    for(re int i=1;i<=n;++i) pai[i]=pai[i]*2+pai[i-1]%(mod-1);
    ans1=qpow(ans1,2*n);
    for(re int i=2;i<=n;++i) ans2=1ll*ans2*qpow(i,pai[n/i]-1)%mod;
    printf("%d",(1ll*ans1*qpow(1ll*ans2*ans2%mod,mod-2))%mod);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lizehon/p/10425760.html
今日推荐