版本比较
8.7
正文
增加 151 行删除 2 行
9 行未改动1011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757617777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614 行未改动161162163164451651661671681691701711721731741751761771781791801811821831841851861871881891901911921931949 行未改动$$
直接枚举 $d,x$,暴力查询单个 $\mu$ 并记忆化即可。
```cpp#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
int n, k;unordered_map<int, int> mp;
int calc(int x) { return (x + 1) * x / 2 % mod;}
int mu(int x) { if (mp.count(x)) return mp[x]; int ans = 1, tmp = x; for (int i = 2; i * i <= x; i++) if (x % i == 0) { int cnt = 0; while (x % i == 0) x /= i, cnt++; if (cnt > 1) return 0; ans = -ans; } if (x > 1) ans = -ans; mp[tmp] = ans; return ans;}
int S(int x, int y) { int ans = 0; for (int i = 1; i * i <= y; i++) if (y % i == 0) { ans = (ans + mu(i) * i % mod * calc(x / i) % mod + mod) % mod; if (i * i != y) ans = (ans + mu(y / i) * (y / i) % mod * calc(x / (y / i)) % mod + mod) % mod; } return ans;}
signed main() { cin >> n >> k; int ans = 0; for (int i = 1; i * i <= k; i++) if (k % i == 0) { ans = (ans + S(n / i, k / i)) % mod; if (i * i != k) ans = (ans + S(n / (k / i), i)) % mod; } cout << ans * k % mod; return 0;} ```
## P2231首先转化一下题意,可以理解为从 $[1,m]$ 中选 $n$ 个正整数,可以重复,使得这些数的 $gcd$ 为 $1$,求序列数。
那么根据裴蜀定理,只有当 $gcd(a_1,a_2,\dots,a_n,m)=1$ 时符合条件。可以得到不定方程
$$\sum_{i = 1}^{n} a_i x_i = 1$$
那么根据裴蜀定理,只有当 $gcd(a_1,a_2,\dots,a_n,m)=1$ 时有解。
那么答案即为
$$\sum_{a_1=1}^m\dots\sum_{a_n=1}^m[gcd(a_1,\dots,a_n,m)=1] = \sum_{d|m}\mu(d)\left(\sum_{d|a_1}1\right)\dots\left(\sum_{d|a_n}1\right) = \sum_{d|m}\mu(d)\left(\frac{m}{d}\right)^n$$
```cpp#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m;unordered_map<int, int> mp;
int mu(int x) { if (mp.count(x)) return mp[x]; int ans = 1, tmp = x; for (int i = 2; i * i <= x; i++) if (x % i == 0) { int cnt = 0; while (x % i == 0) x /= i, cnt++; if (cnt > 1) return 0; ans = -ans; } if (x > 1) ans = -ans; mp[tmp] = ans; return ans;}
int fpow(int a, int b) { int res = 1; while (b) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res;}
signed main() { cin >> n >> m; int ans = 0; for (int i = 1; i * i <= m; i++) if (m % i == 0) { ans += mu(i) * fpow(m / i, n); if (i * i != m) ans += mu(m / i) * fpow(i, n); } cout << ans; return 0;} ```
## CF235E根据P3227,我们可以扩展得到
$$d(ijk) = \sum_{u|i}\sum_{v|j}\sum_{w|k}[gcd(u,v)=1][gcd(v,w)=1][gcd(w,u)=1]$$14 行未改动首先,任意正整数 $x$ 都可以唯一表示为 $x=dy^2$ 的形式,其中 $d$ 无平方因子,即 $\mu^2(d)=1$。
则 $ij=d_1d_2(xy)^2$,那么 $ij$ 是完全平方数当且仅当 $d_1=d_2$。
所以我们枚举 $i,j$ 的 $d$。所以我们枚举 $i,j$ 的 $d$,那么
- $1 \le x \le \lfloor \sqrt{\frac{n}{d}} \rfloor$- $1 \le y \le \lfloor \sqrt{\frac{m}{d}} \rfloor$
所以答案为
$$\sum_{d=1}^{min(n,m)}\mu^2(d) \lfloor \sqrt{\frac{n}{d}} \rfloor \lfloor \sqrt{\frac{m}{d}} \rfloor$$
### 恒等式$$\mu^2(d) = \sum_{k^2|d}\mu(k)$$
所以,答案又变为
$$\begin{align*}\sum_{d=1}^{\min(n,m)} \left( \sum_{k^2|d} \mu(k) \right) \left\lfloor \sqrt{\frac{n}{d}} \right\rfloor \left\lfloor \sqrt{\frac{m}{d}} \right\rfloor &= \sum_{k=1}^{\min(n,m)} \mu(k) \left\lfloor \sqrt{\frac{n}{k^2 t}} \right\rfloor \left\lfloor \sqrt{\frac{m}{k^2 t}} \right\rfloor\end{align*}$$
定义:
$$h(A, B) = \sum_{t=1}^{\min(A,B)} \left\lfloor \sqrt{\frac{A}{t}} \right\rfloor \left\lfloor \sqrt{\frac{B}{t}} \right\rfloor$$
代入式子化简为
$$\sum_{k=1}^{\min(n,m)} \mu(k) \cdot h\left(\left\lfloor \frac{n}{k^2} \right\rfloor, \left\lfloor \frac{m}{k^2} \right\rfloor\right)$$
双重整除分块+线性筛求出即可。