【莫比乌斯反演】HDU1695_GCD

题目链接: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[1]=1;
    for (ll i=2;i<maxn;i++) miu[i]=-INF;
    for (ll i=2;i<maxn;i++)
    {
      if (miu[i]==-INF)
      {
        miu[i]=-1;
        prime[++pnum]=i;
      }
      for (ll j=1;j<=pnum;j++)
      {
        if (i*prime[j]>=maxn) break;
        if (i%prime[j]==0) miu[i*prime[j]]=0;
        else miu[i*prime[j]]=-miu[i];
      }
    }
    ll a,b,c,d,e;
    for (ll i=1;i<=t;i++)
    {
        cin>>a>>b>>c>>d>>e;
        ll ub,ans1=0,ans2=0;
        ub=min(b/=e,d/=e);
        for (ll k=1;k<=ub;k++)
          ans1+=miu[k]*(b/k)*(d/k);
        for (ll k=1;k<=ub;k++) 
          ans2+=miu[k]*(ub/k)*(ub/k);
        ll ans=ans1-ans2/2;
        cout<<"Case "<<i<<": "<<ans<<endl;
    }  
    return 0;
} 

 

posted @ 2018-03-02 13:03  尹吴潇  阅读(142)  评论(0编辑  收藏  举报