【BZOJ1101】[POI2007]Zap【Mobius函数】

【题目链接】

题解很多就不写啦。

/* Pigonometry */
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long LL;

const int maxn = 50005;

int n, m, d, prime[maxn], cnt, mu[maxn];
bool isnotprime[maxn];

inline int iread() {
	int f = 1, x = 0; char ch = getchar();
	for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;
	for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
	return f * x;
}

inline void getmu() {
	mu[1] = 1;
	for(int i = 2; i < maxn; i++) {
		if(!isnotprime[i]) prime[++cnt] = i, mu[i] = -1;
		for(int j = 1; j <= cnt && i * prime[j] < maxn; j++) {
			isnotprime[i * prime[j]] = 1;
			if(i % prime[j] == 0) {
				mu[i * prime[j]] = 0;
				break;
			}
			mu[i * prime[j]] = -mu[i];
		}
	}
	for(int i = 1; i < maxn; i++) mu[i] += mu[i - 1];
}

int main() {
	getmu();
	for(int T = iread(); T; T--) {
		n = iread(); m = iread(); d = iread();
		if(n > m) swap(n, m);
		n /= d; m /= d;

		int ans = 0;
		for(int i = 1; i <= n; ) {
			int pos = min(n / (n / i), m / (m / i));
			ans += (mu[pos] - mu[i - 1]) * (n / i) * (m / i);
			i = pos + 1;
		}
		printf("%d\n", ans);
	}
}


你可能感兴趣的:(mobius函数)