Simple question of Mobius Inversion: Mobius Inversion
Mobius Function
Here we will briefly describe the Mobius function:
If d = 1, then μ (d) = 1
If d = p1p2... Pr (r is a different prime number and the number of times is one) μ (d) = (-1) ^ r
Remaining μ (d) = 0
GCD
Question transfer: HDU-1695-GCD
Evaluate the logarithm of two numbers in [1, n], [1, m] Where gcd is k
Train of Thought: here we can transform the number of mutually qualitative numbers between [1, n/k] and [1, m/k ].
AC code:
#include <map>#include <set>#include <list>#include <cmath>#include <deque>#include <queue>#include <stack>#include <bitset>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <complex>#include <cstdlib>#include <cstring>#include <fstream>#include <sstream>#include <utility>#include <iostream>#include <algorithm>#include <functional>#define LL long long#define INF 0x7fffffffusing namespace std;const int maxn = 1000005;bool vis[maxn];int prime[maxn];int mu[maxn];int tot;void init() { memset(vis, 0, sizeof(vis)); mu[1] = 1; tot = 0; for(int i = 2; i < maxn; i ++) { if(!vis[i]) { prime[tot ++] = i; mu[i] = -1; } for(int j = 0; j < tot; j ++) { if(i * prime[j] >= maxn) break; vis[i * prime[j]] = true; if(i % prime[j] == 0) { mu[i * prime[j]] = 0; break; } else { mu[i * prime[j]] = -mu[i]; } } }}int main() { init(); int T; int a, b, c, d, k; scanf("%d", &T); for(int cas = 1; cas <= T; cas ++) { scanf("%d %d %d %d %d", &a, &b, &c, &d, &k); if(k == 0) { printf("Case %d: 0\n", cas); continue; } b /= k; d /= k; if(b > d) swap(b, d); LL ans = 0; for(int i = 1; i <= b; i ++) { ans += (LL)mu[i] * (b / i) * (d / i); } LL t = 0; for(int i = 1; i <= b; i ++) { t += (LL)mu[i] * (b / i) * (b / i); } ans -= t / 2; printf("Case %d: %I64d\n", cas, ans); } return 0;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.