跳到正文

个人记录

8.7

oydfl约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年8月7日
收录
2026年8月7日
更新
2026年11月12日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

AT_abc020_D

直接拆开 lcmlcm 然后推式子:

∑i=1Nlcm(i,K)=∑i=1NiKgcd(i,K)=∑d∣K∑i=1⌊Nd⌋iK[gcd(i,Kd)=1]=K∑d∣K∑x∣Kdμ(x)xS(⌊Ndx⌋)\begin{aligned} \sum_{i=1}^Nlcm(i,K) &= \sum_{i=1}^N\frac{iK}{gcd(i,K)}\\ &= \sum_{d|K}\sum_{i=1}^{\lfloor \frac{N}{d} \rfloor} iK[gcd(i,\frac{K}{d})=1]\\ &= K\sum_{d|K}\sum_{x|\frac{K}{d}} \mu(x)xS(\lfloor \frac{N}{dx} \rfloor) \end{aligned}

直接枚举 d,xd,x,暴力查询单个 μ\mu 并记忆化即可。

C++
#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][1,m] 中选 nn 个正整数,可以重复,使得这些数的 gcdgcd 为 11,求序列数。

可以得到不定方程

∑i=1naixi=1\sum_{i = 1}^{n} a_i x_i = 1

那么根据裴蜀定理,只有当 gcd(a1,a2,…,an,m)=1gcd(a_1,a_2,\dots,a_n,m)=1 时有解。

那么答案即为

∑a1=1m⋯∑an=1m[gcd(a1,…,an,m)=1]=∑d∣mμ(d)(∑d∣a11)…(∑d∣an1)=∑d∣mμ(d)(md)n\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
C++
#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)=∑u∣i∑v∣j∑w∣k[gcd(u,v)=1][gcd(v,w)=1][gcd(w,u)=1]d(ijk) = \sum_{u|i}\sum_{v|j}\sum_{w|k}[gcd(u,v)=1][gcd(v,w)=1][gcd(w,u)=1]

所以,将其代入原式,并同时使用莫比乌斯反演得到

∑x=1aμ(x)∑y=1bμ(y)∑z=1cμ(z)(∑x∣u,z∣ua⌊au⌋)(∑x∣v,y∣vb⌊bv⌋)(∑y∣u,z∣uc⌊cw⌋)\sum_{x=1}^a\mu(x)\sum_{y=1}^b\mu(y)\sum_{z=1}^c\mu(z)\left(\sum_{x|u,z|u}^a\lfloor\frac{a}{u}\rfloor\right)\left(\sum_{x|v,y|v}^b\lfloor\frac{b}{v}\rfloor\right)\left(\sum_{y|u,z|u}^c\lfloor\frac{c}{w}\rfloor\right)

注意到 x∣ux|u 且 z∣uz|u 等价于 lcm(x,z)∣ulcm(x,z)|u,所以我们定义 S(n,m)=∑m∣kn⌊nk⌋=∑t=1⌊n/m⌋⌊nmt⌋S(n,m) = \sum\limits_{m|k}^n\lfloor\frac{n}{k}\rfloor = \sum\limits_{t=1}^{\lfloor n/m \rfloor}\lfloor \frac{n}{mt} \rfloor

则原式变为

∑x=1aμ(x)∑y=1bμ(y)∑z=1cμ(z)S(a,lcm(x,z))S(b,lcm(x,y))S(c,lcm(y,z))\sum_{x=1}^a\mu(x)\sum_{y=1}^b\mu(y)\sum_{z=1}^c\mu(z)S(a,lcm(x,z))S(b,lcm(x,y))S(c,lcm(y,z))

预处理 S(n,m)S(n,m) 数组与任意两数的 lcmlcm 即可。

P10584

首先,任意正整数 xx 都可以唯一表示为 x=dy2x=dy^2 的形式,其中 dd 无平方因子,即 μ2(d)=1\mu^2(d)=1。

则 ij=d1d2(xy)2ij=d_1d_2(xy)^2,那么 ijij 是完全平方数当且仅当 d1=d2d_1=d_2。

所以我们枚举 i,ji,j 的 dd,那么

  • 1≤x≤⌊nd⌋1 \le x \le \lfloor \sqrt{\frac{n}{d}} \rfloor
  • 1≤y≤⌊md⌋1 \le y \le \lfloor \sqrt{\frac{m}{d}} \rfloor

所以答案为

∑d=1min(n,m)μ2(d)⌊nd⌋⌊md⌋\sum_{d=1}^{min(n,m)}\mu^2(d) \lfloor \sqrt{\frac{n}{d}} \rfloor \lfloor \sqrt{\frac{m}{d}} \rfloor

恒等式

μ2(d)=∑k2∣dμ(k)\mu^2(d) = \sum_{k^2|d}\mu(k)

所以,答案又变为

∑d=1min⁡(n,m)(∑k2∣dμ(k))⌊nd⌋⌊md⌋=∑k=1min⁡(n,m)μ(k)⌊nk2t⌋⌊mk2t⌋\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)=∑t=1min⁡(A,B)⌊At⌋⌊Bt⌋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

代入式子化简为

∑k=1min⁡(n,m)μ(k)⋅h(⌊nk2⌋,⌊mk2⌋)\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)

双重整除分块+线性筛求出即可。

原文