头部背景图片
Decaku 's Blog |
Decaku 's Blog |

Hdu 1695

描述:

$求i \in [1,m],j \in [1,n],gcd(i,j)=k的pair数。$


思路:

莫比乌斯反演入门题,套个容斥去重。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
bool check[maxn];
ll prime[maxn];
ll mu[maxn];
void mobius()
{
memset(check,false,sizeof(check));
mu[1]=1;
int tot=0;
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;
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
break;
}
else mu[i*prime[j]]=-mu[i];
}
}
}
ll t,a,b,c,d,k;
__int64 ans,ans2;
int main()
{
mobius();
cin>>t;
for(int i=1; i<=t; i++)
{
cin>>a>>b>>c>>d>>k;
if(!k)
{
cout<<"Case "<<i<<": "<<0<<endl;
continue;
}
ans=0;
ans2=0;
int minn=min(b/k,d/k);
for(int i=1; i<=minn; i++)
{
ans+=mu[i]*(b/k/i)*(d/k/i);
}
for(int i=1;i<=minn;i++)
{
ans2+=mu[i]*(minn/i)*(minn/i);
}
printf("Case %d: %I64d\n",i,ans-ans2/2);
//cout<<"Case "<<i<<": "<<ans-ans2/2<<endl;
}
return 0;
}
avatar Decaku 菜菜菜