Gcd HYSBZ - 2818 (莫比乌斯反演)
Gcd
\[Time Limit: 10000 ms\quad Memory Limit: 262144 kB
\]
题意
求 \(gcd\left(x,y\right) = p\) 的对数,其中\(1 \leq x,y \leq n\)且 \(p\)是质数
思路
\(g\left(x\right)\) 表示 \(gcd\left(a, b\right) | x\) 的对数
\(f\left(x\right)\) 表示 \(gcd\left(a, b\right) = x\) 的对数
根据莫比乌斯反演有
\[ f\left(n\right) = \sum_{n|d} g\left(d\right)\\
g\left(n\right) = \sum_{n|d} \mu\left(\frac{d}{n}\right) f\left(d\right) \\
\]
根据题意
\[ f\left(x\right) = \lfloor\frac{n}{x}\rfloor \lfloor\frac{n}{x}\rfloor \\
\]
那么就可以得到
\[\begin{aligned}
ans &= \sum_{prime(p)} g\left(p\right) \\
&= \sum_{prime(p)} \sum_{p|d} \mu\left(\frac{d}{p}\right) \lfloor\frac{n}{d}\rfloor \lfloor\frac{n}{d}\rfloor \\
\end{aligned}
\]
对于每个 \(d\),找到所有满足 \(p|d\) 的 \(prime(p)\),预处理出 \(\sum_{p|d}\mu\left(\frac{d}{p}\right)\)。
- 如果 \(d\) 是质数,容易得到 \(sum[d] = 1\)。
- 如果 \(d\) 不是质数,那么可以把 \(d\) 看成 \(p_{1}^{a1}p_{2}^{a2}...p_{k}^{ak}\),设 \(d = p_{1}x\)
\[\begin{aligned}
\sum_{p|d} \mu\left(\frac{d}{p}\right) &= \mu\left(\frac{d}{p_{1}}\right)+\mu\left(\frac{d}{p_{2}}\right)+...+\mu\left(\frac{d}{p_{k}}\right)\\
&= \mu\left(x\right) + \mu\left(\frac{p_{1}x}{p_{2}}\right)+...+\mu\left(\frac{p_{1}x}{p_{k}}\right) \\
\sum_{p|x}\mu\left(\frac{x}{p}\right) &= \mu\left(\frac{x}{p_{2}}\right)+...+\mu\left(\frac{x}{p_{k}}\right)
\end{aligned}
\]
因为\(p_{k}|d,d=p_{1}x\),则 \(p_{k}|x\)。那么现在的问题就在于 \(p_{1}|x\)。
\(\quad\) 1. 若 \(p1|x\),则对于 \(\left(\frac{p_{1}x}{p_{k}}\right)\),可以发现除完以后,仍然会包括两个及以上 \(p_{1}\) 因子,所以其 \(\mu\) 值为\(0\)。
\(\quad\) 2. 反之,\(\left(\frac{p_{1}x}{p_{k}}\right)\) 在 \(\left(\frac{x}{p_{k}}\right)\)的基础上多了一个 \(p_{1}\) 因子且指数为 \(1\),根据 \(\mu\) 的公式,\(\mu\left(\frac{p_{1}x}{p_{k}}\right) =- \mu\left(\frac{x}{p_{k}}\right)\)。
综合上述
\[\sum_{p|d} \mu\left(\frac{d}{p}\right) =
\begin{cases}
\mu\left(x\right) & p_{1}|x\\
\mu\left(x\right) - \sum_{x|d} \mu\left(\frac{x}{p}\right) &otherwise\\
\end{cases}
\]
/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : 2019年07月17日 星期三 10时20分16秒
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e7 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
ll n, m;
int cas, tol, T;
int pri[maxn], mob[maxn];
bool ispri[maxn];
ll sum[maxn];
void handle() {
mes(sum, 0), mes(pri, 0), mes(ispri, 1);
tol = 0;
mob[1] = 1;
int mx = 1e7;
for(int i=2; i<=mx; i++) {
if(ispri[i]) {
pri[++tol] = i;
mob[i] = -1;
sum[i] = 1;
}
for(int j=1; j<=tol && i*pri[j]<=mx; j++) {
ispri[i*pri[j]] = false;
if(i%pri[j] == 0) {
mob[i*pri[j]] = 0;
sum[i*pri[j]] = mob[i];
break;
} else {
mob[i*pri[j]] = -mob[i];
sum[i*pri[j]] = mob[i] - sum[i];
}
}
}
}
int main() {
handle();
printf("%lld %lld %lld\n", sum[12], sum[6], mob[6]);
scanf("%lld", &n);
ll ans = 0;
for(ll d=2; d<=n; d++) {
ans += 1ll*sum[d]*(n/d)*(n/d);
}
printf("%lld\n", ans);
return 0;
}