洛谷 [P3455] ZAP

莫比乌斯函数


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
const int MAXN = 500005;
int T, a, b, k, miu[MAXN], prime[MAXN], tot, pre[MAXN];
bool f[MAXN];
void Euler_chk() {
	f[1] = 1;miu[1] = 1;
	for(int i = 2; i <= 50005; i++) {
		if(!f[i]) {
			prime[++tot] = i;
			miu[i] = -1;
		}
		for(int j = 1; i * prime[j] <= 50005; j++) {
			f[i * prime[j]] = 1;
			if(i % prime[j] == 0) {
				miu[i * prime[j]] = 0;
				break;
			}
			miu[i * prime[j]] = -miu[i];
		}
	}
}
int main() {
	Euler_chk();
	pre[1] = 1;
	for(int i = 1; i <= 50005; i++) {
		pre[i] = pre[i - 1] + miu[i];
	}
	cin >> T;
	while(T--) {
		cin >> a >> b >> k;
		a /= k; b /= k;
		int n = min(a, b);
		ll ans = 0ll;
		for(int i = 1, x; i <= n; i = x + 1) {
			x = min(a / (a / i), b / (b / i));
			ans += (ll)(pre[x] - pre[i - 1]) * (a / i) * (b / i);
		}
		cout << ans << endl;
	}
	return 0;
}
posted @ 2018-03-27 09:33  Mr_Wolfram  阅读(136)  评论(0编辑  收藏  举报