[HAOI 2011] Problem b
Posted fly-in-milkyway
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HAOI 2011] Problem b相关的知识,希望对你有一定的参考价值。
Description
(T) 次询问有多少个数对 ((x,y)) 满足 (a≤x≤b,c≤y≤d) 且 (gcd(x,y) = k)。
Solution
假设 (n<m)
[sumlimits_{i=1}^{n}sumlimits_{j=1}^{m}[gcd(i,j)=k]\sumlimits_{i=1}^{lfloorfrac{n}{k} floor}sumlimits_{j=1}^{lfloorfrac{m}{k} floor}[gcd(i,j)=1]\sumlimits_{i=1}^{lfloorfrac{n}{k} floor}sumlimits_{j=1}^{lfloorfrac{m}{k} floor}sumlimits_{d|gcd(i,j)}mu(d)\sumlimits_{d=1}^{lfloorfrac{n}{k} floor}mu(d){leftlfloorfrac{n}{kd} ight floor}{leftlfloorfrac{m}{kd} ight floor}]
设 (n'={leftlfloorfrac{n}{k} ight floor},m'={leftlfloorfrac{m}{k} ight floor})
[sumlimits_{d=1}^{n'}mu(d){leftlfloorfrac{n'}{d} ight floor}{leftlfloorfrac{m'}{d} ight floor}]
欧拉筛 + 整除分块 + 容斥。
Code
#include <cstdio>
const int N = 50005;
int a, b, c, d, k, T, tot, np[N], p[N], mu[N], sum[N];
int min(int x, int y) {
return x < y ? x : y;
}
void getmu(int n) {
np[1] = mu[1] = 1;
for (int i = 2; i <= n; ++i) {
if (!np[i]) p[++tot] = i, mu[i] = -1;
for (int j = 1; j <= tot && p[j] * i <= n; ++j) {
np[p[j]*i] = 1;
if (i % p[j] == 0) {
mu[p[j]*i] = 0;
break;
} mu[p[j]*i] = -mu[i];
}
}
for (int i = 1; i <= n; ++i) sum[i] = sum[i-1] + mu[i];
}
long long calc(int n, int m) {
long long res = 0;
n /= k, m /= k;
if (n > m) n ^= m, m ^= n, n ^= m;
for (int l = 1, r; l <= n; l = r + 1) {
r = min(n / (n / l), m / (m / l));
res += 1LL * (sum[r] - sum[l-1]) * (n / l) * (m / l);
}
return res;
}
int main() {
scanf("%d", &T), getmu(5e4);
while (T--) {
scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
printf("%lld
", calc(b, d) - calc(a - 1, d) - calc(b, c - 1) + calc(a - 1, c - 1));
}
return 0;
}
以上是关于[HAOI 2011] Problem b的主要内容,如果未能解决你的问题,请参考以下文章