您的位置:首页 > 其它

hdu1569 莫比乌斯反演

2015-09-10 10:52 225 查看
hdu 1695 莫比乌斯反演给出a,b,c,d,k, 求满足a <= x <= b && c <= y <= d && gcd(x,y)=k 的数对(x,y)的对数。a=c=1; 0 < b,c <= 1e5; (n1,n2) 和 (n2,n1) 算为同种情况其实是求满足1 <= x <= b/k && 1 <= y <= d/k && gcd(x,y)=1 的 数对(x,y)的对数。
莫比乌斯反演入门题
设f(k)为gcd(x,y)=k的数对(x,y)的对数,我们要求的是f(1)
设F(k)为gcd(x,y)为k的倍数的数对(x,y)的对数,可以想到F(k)=floor(b/k)*floor(d/k),
由莫比乌斯反演得:
令lim=min(b/k,d/k)
f(1)=mu[1]*F(1) + mu[2]*F[2] + ... + mu[lim]*F(lim)
因为(n1,n2)和(n2,n1)算为同一种情况,所以最后结果还要减掉重复的情况。

#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=1000005;
bool check[maxn];
int mu[maxn];
int prime[maxn];
void Moblus()
{
memset(check,false,sizeof(check));
int tot=0;
mu[1]=1;
for(int i=2; i<maxn; i++)
{
if(!check[i])
{
prime[tot++]=i;
mu[i]=-1;
}
for(int j=0; j<tot; j++)
{
if(i * prime[j]>=maxn)break;
check[i*prime[j]]=true;
mu[i*prime[j]]=-mu[i];
if(i%prime[j] == 0)
{
mu[i*prime[j]]=0;
break;
}
}
}
}
int main()
{
Moblus();
int a,b,c,d,k;
int cas;
scanf("%d",&cas);
for(int cc=1; cc<=cas; cc++)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
if(k==0)
{
printf("Case %d: 0\n",cc);
continue;
}
b/=k;
d/=k;
if(b>d)swap(d,b);
long long ans1=0;
for(int i=1; i<=b; i++)
ans1+=(long long )mu[i]*(b/i)*(d/i);
long long ans2=0;
for(int i=1; i<=b; i++)
ans2+=(long long )mu[i]*(b/i)*(b/i);
ans1-=ans2/2;
printf("Case %d: %I64d\n",cc,ans1);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: