2

2 5 1 5 1

1 5 1 5 2

14

3

# HINT

100%的数据满足：1≤n≤50000，1≤a≤b≤50000，1≤c≤d≤50000，1≤k≤50000

# 分析

f(d)为d|GCD(x,y)且x<=m,y<=n的数对数

# 代码

#include <bits/stdc++.h>

#define N 100100
#define ll long long

using namespace std;

int tot;

ll mu[N],prime[N];
bool notPrime[N];

void getMu()
{
mu[1] = 1;
for (int i = 2; i <= N; i++)
{
if (!notPrime[i])
{
mu[i] = -1;
prime[++tot] = i;
}
for (int j = 1; j <= tot && prime[j] * i <= N; j++)
{
notPrime[i * prime[j]] = true;
if (i % prime[j] == 0)
{
mu[prime[j] * i] = 0;
break;
}
mu[prime[j] * i] = -mu[i];
}
}

for (int i = 2; i <= N; i++)
{
mu[i] += mu[i - 1];
}
}

ll work(int m,int n,int k)
{
ll result = 0;
int last;
n /= k;
m /= k;
for (int i = 1; i <= n && i <= m; i = last + 1)
{
last = min(n / (n / i),m / (m / i));
result += (mu[last] - mu[i - 1]) * (m / i) * (n / i);
}
return result;
}

int main()
{
int T;
getMu();
scanf("%d",&T);
while (T--)
{
int a,b,c,d,k;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
printf("%lld\n",work(b,d,k) - work(a-1,d,k) - work(b,c-1,k) + work(a-1,c-1,k));
}

return 0;
}


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客