Luogu5221 Product

https://www.luogu.com.cn/problem/P5221

莫比乌斯反演

\[\prod_{i=1}^N \prod_{j=1}^N \frac{lcm(i,j)}{\gcd(i,j)}\\ =\frac{\prod_{i=1}^N \prod_{j=1}^N ij}{\prod_{k=1}^N k^{2(\sum_{p=1}^N \sum_{q=1}^N [\gcd(p,q)=k])}}\\ 处理\prod_{k=1}^N k^{2(\sum_{p=1}^N \sum_{q=1}^N [\gcd(p,q)=k])}\\ 莫比乌斯反演一下,两个数论分块解决\\ \]

本来能自己\(AC\)的,一个愚蠢的错误\(\sum_{i=1}^N \sum_{j=1}^N 1\)算成了\(\frac{n^2(n+1)^2}{4} \quad QAQ\)

注意点:

\(1.\)本题卡空间,空间尽量开小

\(2.a^b\equiv a^{(b \mod (p-1))} (\mod p)\)\((a,p\)互质\()\)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#define smu mu
#define N 1000005
#define ll long long
#define mod (ll)104857601
using namespace std;
bool pri[N];
int cnt,prime[N];
short mu[N];
ll n,ans1,ans2;
ll ksm(ll x,ll y)
{
    ll ans=1;
    while (y)
    {
        if (y&1)
            ans=ans*x%mod;
        x=x*x%mod;
        y >>=1;
    }
    return ans;
}
void Pre()
{
    mu[1]=1;
    for (int i=2;i<=n;i++)
    {
        if (!pri[i])
        {
            prime[++cnt]=i;
            mu[i]=-1;
        }
        for (int j=1;j<=cnt;j++)
        {
            ll g=(ll)i*prime[j];
            if (g>n)
                break;
            pri[g]=true;
            if (i%prime[j]==0)
            {
                mu[g]=0;
                break;
            }
            mu[g]=-mu[i];
        }
    }
    for (int i=1;i<=n;i++)
        smu[i]=smu[i-1]+mu[i];
}
ll solve(ll n)
{
    ll ans=0;
    ll l,r;
    for (l=1;l<=n;l=r+1)
    {
        r=n/(n/l);
        ll w=n/l;
        ll e=w*w;
        ll o=mu[r]-mu[l-1];
        ans=ans+e*o;
    }
    return ans;
}
int main()
{
    scanf("%lld",&n);
    Pre();
    ans1=1;
    for (int i=1;i<=n;i++)
        ans1=ans1*i%mod;
    ans1=ksm(ans1,n+n);
    ans2=1;
    ll l,r;
    for (ll l=1;l<=n;l=r+1)
    {
        r=n/(n/l);
        ll g=solve(n/l);
        g=g+g;
        for (ll i=l;i<=r;i++)
            ans2=ans2*ksm(i,g)%mod;
    }
    ll ans=ans1*ksm(ans2,mod-2)%mod;
    cout << ans << endl;
    return 0;
}
posted @ 2020-07-22 15:57  GK0328  阅读(81)  评论(0编辑  收藏  举报