luogu2522[HAOI2011]Problem b
阿新 • • 發佈:2018-12-11
用一個簡單的容斥就可以求出a<=x <=b, c<=y<=d範圍的答案了
注意1LL呀qwq
#include <bits/stdc++.h> using namespace std; #define N 50005 bool vis[N]; int miu[N],prime[N]; typedef long long LL; LL sum[N],ans; int a,b,c,d,k,T,cnt; void Miu() { miu[1] = 1; cnt = 0; for (int i=2;i<=N-5;i++) { if (!vis[i]) { prime[++cnt] = i; miu[i] = -1; } for (int j = 1;j<=cnt && prime[j] * i <= N-5;j++) { vis[prime[j] * i] = 1; if (i % prime[j] == 0) break; else miu[i * prime[j]] = -miu[i]; } } for (int i=1;i<=N-5;i++) sum[i] = sum[i-1] + (LL)miu[i]; } LL get_ans(int n, int m) { LL ans = 0; if (n > m) swap(n,m); for (int l = 1,r;l <=n;l = r+1) { r = min(n / (n / l), m / (m / l)); ans += 1LL * (n / l) * (m / l) * (sum[r / k] - sum[(l-1)/k]);//注意long long } return ans; } int main() { Miu(); scanf("%d", &T); while (T--) { scanf("%d%d%d%d%d", &a, &b, &c, &d, &k); ans = get_ans(b,d) - get_ans(a-1,d) - get_ans(b,c-1) + get_ans(a-1,c-1); printf("%lld\n", ans); } }