https://cn.vjudge.net/problem/HDU-1695
题意 给你a,b,c,d,求有多少 a<=x<=b c<=y<=d 的gcd(x,y) == k;
思路 先整除 k 就是了gcd(x,y) == 1,还要去重(x,x)的情况。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 101000;
bool is[maxn+10];
ll n,ans,sum[maxn+10],tot = 0;
ll pri[maxn+10],miu[maxn+10];
void init() //首先把莫比乌斯函数筛出来
{
miu[1]=1;
for(int i=2; i<=maxn; i++)
{
if(!is[i])
{
pri[++tot]=i;
miu[i]=-1;
}
for(int j=1; j<=tot; j++)
{
int k=pri[j]*i;
if(k>maxn)
break;
is[k]=1;
if(i%pri[j]==0)
{
miu[k]=0;
break;
}
else
miu[k]=-miu[i];
}
}
}
int main()
{
init();
ll t,m;
scanf("%lld",&t);
int ca = 1;
ll a,b,c,d,k;
while(t--)
{
scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);
ll ans,sum;
ans = sum = 0;
if(k==0)
{
printf("Case %d: 0\n",ca++);
continue;
}
b /= k,d /= k;
n = min(b,d);
for(int i = 1;i<=n;i++)
{
ans += (ll)miu[i]*(b/i)*(d/i);
sum += (ll)miu[i]*(n/i)*(n/i);
}
printf("Case %d: %lld\n",ca++,ans-sum/2);
}
}