[HDU5663]Hillan and the girl

Posted 租酥雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HDU5663]Hillan and the girl相关的知识,希望对你有一定的参考价值。

题面戳我(题面很鬼畜建议阅读一下)
题意:给出n,m,求
\[\sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)\mbox{不是完全平方数}]\]
多组数据,\(n,m\le10^7\)

sol

SBT
演成这个样子:
\[\sum_{T=1}^{n}\lfloor\frac nT\rfloor\lfloor\frac mT\rfloor\sum_{d|T}\mu(\frac Td)[d\mbox{不是完全平方数}]\]
对于那个\([d\mbox{不是完全平方数}]\),我们先假设这个表达式恒为真,那么后面那一坨就是\[\sum_{d|T}\mu(\frac Td)=\sum_{d|T}\mu(d)=[T==1]\]
然后再把不合法的(完全平方数)在里面减掉。
因为完全平方数只有\(\sqrt{n}\)个所以复杂度完全可以承受

code

#include<cstdio>
#include<algorithm>
using namespace std;
#define ll long long
const int N = 10000000;
int gi()
{
    int x=0,w=1;char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-') w=0,ch=getchar();
    while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return w?x:-x;
}
int pri[N+5],tot,zhi[N+5],mu[N+5],h[N+5];
void Mobius()
{
    zhi[1]=mu[1]=h[1]=1;
    for (int i=2;i<=N;i++)
    {
        if (!zhi[i]) pri[++tot]=i,mu[i]=-1;
        for (int j=1;j<=tot&&i*pri[j]<=N;j++)
        {
            zhi[i*pri[j]]=1;
            if (i%pri[j]) mu[i*pri[j]]=-mu[i];
            else break;
        }
    }
    for (int i=1;i*i<=N;i++)
        for (int j=i*i;j<=N;j+=i*i)
            h[j]-=mu[j/(i*i)];
    for (int i=1;i<=N;i++) h[i]+=h[i-1];
}
int main()
{
    Mobius();
    int T=gi();
    while (T--)
    {
        int n=gi(),m=gi(),i=1;ll ans=0;
        if (n>m) swap(n,m);
        while (i<=n)
        {
            int j=min(n/(n/i),m/(m/i));
            ans+=1ll*(n/i)*(m/i)*(h[j]-h[i-1]);
            i=j+1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

以上是关于[HDU5663]Hillan and the girl的主要内容,如果未能解决你的问题,请参考以下文章

莫比乌斯反演(HDU5663)

hdu 1028 Ignatius and the Princess III

hdu 1028 Ignatius and the Princess III

hdu 1027 Ignatius and the Princess II

hdu1029 Ignatius and the Princess IV

HDU 1027 Ignatius and the Princess II