# 题目大意：

$1≤n≤50000，1≤a≤b≤50000$
$1≤c≤d≤50000，1≤k≤50000$

## 分析：

$d_{x,y}$表示$\sum_{i=1}^{x}\sum_{j=1}^{y}[gcd(i,j)=k]$

$d_{x,y}$
=$\sum_{i=1}^{x}\sum_{j=1}^{y}gcd(i,j)=k$
$=\sum_{i=1}^{\left \lfloor \frac{x}{k} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{y}{k} \right \rfloor}[gcd(i,j)=1]$

$\sum_{d|n}μ(d)=[n=1]$

$\sum_{i=1}^{\left \lfloor \frac{x}{k} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{y}{k} \right \rfloor}[gcd(i,j)=1]$

$=\sum_{i=1}^{\left \lfloor \frac{x}{k} \right \rfloor}\sum_{j=1}^{\left \lfloor \frac{y}{k} \right \rfloor} \sum_{d|gcd(i,j)}μ(d)$

https://www.cnblogs.com/NaVi-Awson/p/8318709.html

## 代码：

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#define N 50005

using namespace std;

typedef long long ll;

int prime[N], check[N], sum[N], mu[N], T, a, b, c, d, k, cnt;

void Get_mobius()
{
mu[1] = 1;
for (int i = 2; i <= 50000; i++)
{
if (!check[i])
{
prime[++cnt] = i;
mu[i] = -1;
}
for (int j = 1; j <= cnt; j++)
{
if (prime[j] * i > 50000) break;
check[prime[j] * i] = 1;
if (i % prime[j] == 0) { mu[prime[j] * i] = 0; break; }
else mu[prime[j] * i] = - mu[i];
}
}
for (int i = 1; i <= 50000; i++) sum[i] = sum[i - 1] + mu[i];
}

ll Cal(int x, int y)
{
ll num = 0;
if (x > y) swap(x, y);
for (int i = 1, last; i <= x; i = last + 1)
{
last = min(x / (x / i), y / (y / i));
num += (ll)(sum[last] - sum[i - 1]) * (x  / i) * (y / i);
}
return num;
}

int main()
{
Get_mobius();
scanf("%d", &T);
while (T--)
{
scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
ll ans = Cal(b / k, d / k) - Cal((a - 1) / k, d / k) - Cal(b / k, (c - 1) / k) + Cal((a - 1) / k, (c - 1) / k);
printf("%lld\n", ans);
}
return 0;
}
﻿


©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客