题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695
第一道莫比乌斯反演
感觉很巧妙的就是利用了F(x)=(n/x)*(m/x)
之后的那个去重也挺不错的
代码:
(原题要加个0特判没写)
#include<bits/stdc++.h>
using namespace std;
#define maxn 200000
#define INF 1e8
#define ll long long
ll prime[maxn],pnum,miu[maxn];
int main()
{
freopen("noip.in","r",stdin);
freopen("noip.out","w",stdout);
ll t;
cin>>t;
miu[]=;
for (ll i=;i<maxn;i++) miu[i]=-INF;
for (ll i=;i<maxn;i++)
{
if (miu[i]==-INF)
{
miu[i]=-;
prime[++pnum]=i;
}
for (ll j=;j<=pnum;j++)
{
if (i*prime[j]>=maxn) break;
if (i%prime[j]==) miu[i*prime[j]]=;
else miu[i*prime[j]]=-miu[i];
}
}
ll a,b,c,d,e;
for (ll i=;i<=t;i++)
{
cin>>a>>b>>c>>d>>e;
ll ub,ans1=,ans2=;
ub=min(b/=e,d/=e);
for (ll k=;k<=ub;k++)
ans1+=miu[k]*(b/k)*(d/k);
for (ll k=;k<=ub;k++)
ans2+=miu[k]*(ub/k)*(ub/k);
ll ans=ans1-ans2/;
cout<<"Case "<<i<<": "<<ans<<endl;
}
return ;
}