求调莫比乌斯变换模板题
  • 板块灌水区
  • 楼主xin20110426
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/1/18 17:21
  • 上次更新2025/1/18 20:40:07
查看原帖
求调莫比乌斯变换模板题
429491
xin20110426楼主2025/1/18 17:21

悬2关

求调P2522

#include <bits/stdc++.h>
#define int long long
using namespace std;
int mu[1000005];
int flg[1000005];
int p[1000005];
int sum[1000005];
int tot;
void getMu(int n) {
  mu[1] = 1;
  for (int i = 2; i <= n; ++i) {
    if (!flg[i]) p[++tot] = i, mu[i] = -1;
    for (int j = 1; j <= tot && i * p[j] <= n; ++j) {
      flg[i * p[j]] = 1;
      if (i % p[j] == 0) {
        mu[i * p[j]] = 0;
        break;
      }
      mu[i * p[j]] = -mu[i];
    }
  }
  for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + mu[i];
}
signed main() {
	getMu(50000);
	int t; cin >> t;
	for (int i = 1; i <= t; i++) {
		int a, b, c, d, k; cin >> a >> b >> c >> d >> k;
		--a; --c;
		a /= k; b /= k; c /= k; d /= k;
		int ans = 0;
		for (int l = 1, r; l <= min(b, d); l = r + 1) {
			r = min(b / (b / l), d / (d / l));
			ans += (sum[r] - sum[l - 1]) * (b / l - a / l) * (d / l - c / l);
		}
		cout << ans << endl;	
	}
	return 0;
}

2025/1/18 17:21
加载中...