luogu2257 YY的GCD--莫比乌斯反演

link

给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对

多组数据T = 10000

N, M <= 10000000

推式子

\(\sum_{i=1}^n\sum_{j=1}^m[\gcd(i,j)=p]\)

\(=\sum_p\sum_{i=1}^{n/p}\sum_{j=1}^{m/p}[\gcd(i,j)=1]\)

\(=\sum_p\sum_{i=1}^{n/p}\sum_{j=1}^{m/p}\sum_{d|i,d|j}\mu(d)\)

\(=\sum_{d=1}^n\mu(d)\sum_p\lfloor\frac n{dp}\rfloor\lfloor\frac m{dp}\rfloor\)

\(q=dp\)

\(=\sum_{q=1}^n(\sum_{p|q}\mu(\frac q p))\lfloor\frac nq\rfloor\lfloor\frac mq\rfloor\)

\(\mu\)线性筛

然后在对于质数枚举倍数求对于每个\(i\)\(\sum_{p|i}\mu(\frac i p)\)

然后打数论分块就行了

#include <cstdio>
#include <functional>
using namespace std;

const int fuck = 10000000;
int prime[10000010], tot;
bool vis[10000010];
int mu[10000010], sum[10000010];

int main()
{
	mu[1] = 1;
	for (int i = 2; i <= fuck; i++)
	{
		if (vis[i] == false) prime[++tot] = i, mu[i] = -1;
		for (int j = 1; j <= tot && i * prime[j] <= fuck; j++)
		{
			vis[i * prime[j]] = true;
			if (i % prime[j] == 0) break;
			mu[i * prime[j]] = -mu[i];
		}
	}
	for (int i = 1; i <= tot; i++)
		for (int j = 1; j * prime[i] <= fuck; j++)
			sum[j * prime[i]] += mu[j];
	for (int i = 1; i <= fuck; i++)
		sum[i] += sum[i - 1];
	int t; scanf("%d", &t);
	while (t --> 0)
	{
		int n, m;
		long long ans = 0; //别忘了初始化。。。
		scanf("%d%d", &n, &m);
		if (n > m) {int t = m; m = n; n = t; }
		for (int i = 1, j; i <= n; i = j + 1)
		{
			j = min(n / (n / i), m / (m / i));
			ans += (sum[j] - sum[i - 1]) * (long long)(n / i) * (m / i);
		}
		printf("%lld\n", ans);
	}
	return 0;
}
posted @ 2019-01-20 18:42  ghj1222  阅读(132)  评论(0编辑  收藏  举报