Skip to content

Commit f5fd576

Browse files
committed
Add solutions
1 parent ba56c7b commit f5fd576

File tree

2 files changed

+255
-0
lines changed

2 files changed

+255
-0
lines changed

source/_posts/sol-cf2101B.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
categories:
3+
- OI
4+
title: CF2101B (Div. 1) Quartet Swapping
5+
date: 2025-05-19 16:23:32
6+
mathjax: true
7+
tags:
8+
- solution
9+
- sort
10+
- codeforces
11+
---
12+
13+
## 题意
14+
15+
[原题链接](https://codeforces.com/contest/2101/problem/B)
16+
17+
> **中文简化版:**
18+
>
19+
> 给定一个排列 $p$,你可以进行任意次以下操作:
20+
>
21+
> + 选择相邻的四个位置 $i,i+1,i+2,i+3$,交换 $i$ 和 $i+2$,交换 $i+1$ 和 $i+3$;换句话说,若序列原本是 $[\cdots,a_i,a_{i+1},a_{i+2},a_{i+3},\cdots]$,操作后变为 $[\cdots,a_{i+2},a_{i+3},a_{i},a_{i+1},\cdots]$。
22+
>
23+
> 最小化排列的字典序。
24+
25+
## 做法
26+
27+
首先可以糊一个假做法。
28+
29+
注意到每次操作不会将奇数位置与偶数位置交换,而是只会交换奇数列和偶数列相邻位置的数。因此考虑对奇数列和偶数列分别排序。
30+
31+
然后 Wrong Answer on pretest 2。
32+
33+
(好像 AI 会卡在这里)
34+
35+
反例是容易构造的,但是多次模拟可以发现,最多只有最后 $4$ 位无法排序。
36+
37+
注意到,由于序列是一个排列,每个数互不相同,因此交换相邻两个数必定会使逆序对 $+1/-1$。
38+
39+
因此,若奇数列和偶数列的逆序对奇偶性相同,则可以对奇数列和偶数列分别排序,否则只需再交换倒数第 $1$ 位和倒数第 $3$ 位即可。
40+
41+
*听说暴力模拟也能过。*

source/_posts/sol-pe484.md

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
---
2+
categories:
3+
- OI
4+
title: PE484 Arithmetic Derivative
5+
date: 2025-05-19 22:05:17
6+
mathjax: true
7+
password: "8907904768686152599"
8+
tags:
9+
- solution
10+
- number-theory
11+
- sieve
12+
- powerful-number
13+
- project-euler
14+
---
15+
16+
> 定义一个正整数的算术导数:
17+
>
18+
> + $p^\prime=1$,这里 $p$ 为质数;
19+
> + $(ab)^\prime=a^\prime b+ab^\prime$,这里 $a,b$ 为任意正整数。
20+
>
21+
> 例如,$20^\prime=24$。
22+
>
23+
>
24+
>
25+
> $$
26+
> \sum_{i=2}^{N}\gcd(i,i^\prime)
27+
> $$
28+
>
29+
> $N=5\times 10^{15}$。
30+
31+
<!-- more -->
32+
33+
## 思路
34+
35+
### 先考虑 $x^\prime$ 是啥
36+
37+
由唯一分解定理,记
38+
39+
$$
40+
x=\prod p_{i}^{\alpha_{i}}
41+
$$
42+
43+
手玩几组可以发现,
44+
45+
$$
46+
x^\prime=x\sum\dfrac{\alpha_{i}}{p_{i}}
47+
$$
48+
49+
使用定义归纳即证。
50+
51+
### 再考虑 $\gcd(x,x^\prime)$
52+
53+
记 $f(x)=\gcd(x,x^\prime)$。
54+
55+
显然可以逐个质因子统计对结果的影响,因此这个函数是积性函数。
56+
57+
### 考虑 $f(p^e)$ 的值
58+
59+
由于 $(p^e)^\prime=ep^{e-1}$,因此
60+
61+
$$
62+
f(p_e)=\gcd(p_e,ep^{e-1})=
63+
\begin{cases}
64+
p^{e},&p\mid e \\\\
65+
p^{e-1},&\text{otherwise}
66+
\end{cases}
67+
$$
68+
69+
至此,问题转化为求积性函数 $f$ 的前缀和。
70+
71+
## 求和
72+
73+
函数没有很明显的容斥特征,并且不能估算,考虑套亚线性筛。
74+
75+
能求数据量 $5\times 10^{15}$ 的亚线性筛只有 Powerful Number 筛。
76+
77+
### PN 筛
78+
79+
这是一种计算积性函数前缀和的方法。
80+
81+
#### 记号约定
82+
83+
+ 默认 $p$ 的取值范围为质数
84+
+ 对于小写字母表示的数论函数,用大写字母表示其前缀和,如 $F(n)=\sum_{i=1}^nf(i)$。
85+
+ 符号 $\ast$ 表示狄利克雷卷积,即:$(g\ast h)(n)=\sum_{d\mid n}g(d)h\left(\dfrac{n}{d}\right)$。
86+
87+
#### Powerful Number
88+
89+
首先,我们引入 PN。
90+
91+
**定义**:由唯一分解定理,记 $n=\prod p_{i}^{\alpha_{i}}$,$n$ 是 PN 当且仅当对所有 $i$,满足 $\alpha_i>1$。特别地,令 $1$ 也属于 $\mathbf{PN}$。
92+
93+
**引理**:任何 PN 可以表为 $a^2b^3$。
94+
95+
> 结论显然,不证。
96+
97+
**性质**:$n$ 以内的 PN 有 $O(\sqrt{n})$ 个。
98+
99+
> 证:枚举 $a$,考虑满足条件的 $b$ 的个数,用积分估算:
100+
>
101+
> $$
102+
> \int_{1}^{\sqrt{n}} \sqrt[3]{\frac{n}{x^2}} \mathrm{d}x = O\left(\sqrt{n}\right)
103+
> $$
104+
>
105+
106+
因此,预处理 $\sqrt{n}$ 以内质数后,可以 DFS $O(\sqrt{n})$ 枚举 $n$ 以内 PN。
107+
108+
#### 筛法过程
109+
110+
首先,构造积性函数 $g$,满足 $g(p)=f(p)$,最好能 $O(1)$ 求和。
111+
112+
其次,构造积性函数 $h$,满足 $g\ast h=f$。
113+
114+
对于质数 $p$,$f(p)=g(1)h(p)+g(p)h(1)=h(p)+g(p)\Rightarrow h(p)=0$。由 $h(n)$ 是积性函数,得 $h(n)$ 仅在 PN 处取有效值。
115+
116+
由 $f=g\ast h$,有
117+
118+
$$
119+
\begin{aligned}
120+
F(n)
121+
&=\sum_{i=1}^{n}(g\ast h)(i) \\\\
122+
&=\sum_{i=1}^{n}\sum_{d\mid i}g(d)h\left(\dfrac{i}{d}\right) \\\\
123+
&=\sum_{d=1}^{n}\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}h(d)g(i) \\\\
124+
&=\sum_{d=1}^{n}h(d)\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}g(i) \\\\
125+
&=\sum_{d=1}^{n}h(d)G\left(\left\lfloor\dfrac{n}{d}\right\rfloor\right) \\\\
126+
&=\sum_{d\in\mathbf{PN}}^{n}h(d)G\left(\left\lfloor\dfrac{n}{d}\right\rfloor\right)
127+
\end{aligned}
128+
$$
129+
130+
注意倒数第二行的结果与杜教筛的变换一致。
131+
132+
#### 构造
133+
134+
对于本题,由于 $f(p)=1$,构造 $g=1$,则 $g^{-1}=\mu$,$G(n)=n$。
135+
136+
考虑 $h=f\ast\mu$ 在 $p^e$ 处的取值:
137+
138+
$$
139+
h(p^e)=\sum_{i=0}^{e}\mu(p^{i})f(p^{e-i})=f(p^e)-f(p^{e-1})
140+
$$
141+
142+
提前 $O(\sqrt{n}\log n)$ 筛出需要的质数和 $h(p^e)$ 的值,注意这个上界不紧。
143+
144+
接下来 $O(\sqrt{n})$ 枚举 PN 求和即可。
145+
146+
> 若构造的 $g(n)$ 需使用杜教筛求和,时间复杂度退化为 $O(n^{2/3})$。
147+
148+
```cpp
149+
#include <bits/stdc++.h>
150+
using namespace std;
151+
152+
constexpr int64_t isqrt_newton(int64_t n) {
153+
int64_t x = 1ll << ((63 - __builtin_clzll(n)) >> 1);
154+
bool d = false;
155+
for (;;) {
156+
int64_t nx = (x + n / x) >> 1;
157+
if (x == nx || (nx > x && d))
158+
break;
159+
d = nx < x, x = nx;
160+
}
161+
return x;
162+
}
163+
164+
const int64_t N = 0x11c37937e08000; // 5e15
165+
const int64_t S = isqrt_newton(N) + 2;
166+
167+
bitset<S> vis;
168+
vector<vector<int64_t>> primes, h;
169+
void sieve() {
170+
vis[0] = vis[1] = true;
171+
for (int64_t i = 2; i * i < S; ++i)
172+
if (!vis[i])
173+
for (int64_t j = i * i; j < S; j += i)
174+
vis[j] = true;
175+
for (int64_t i = 2; i < S; ++i) {
176+
if (vis[i])
177+
continue;
178+
vector<int64_t> a{1}, b{1}, c{1};
179+
for (int64_t e = 1, pk = i;; ++e, pk *= i) {
180+
b.push_back(e % i ? a.back() : pk);
181+
a.push_back(pk);
182+
c.push_back(b[e] - b[e - 1]);
183+
if (pk > N / i)
184+
break;
185+
}
186+
h.push_back(c);
187+
primes.push_back(a);
188+
}
189+
}
190+
191+
int64_t sum;
192+
void dfs(int64_t now, int64_t mul, int64_t hd) {
193+
sum += hd * (N / mul);
194+
for (int64_t i = now; i < primes.size(); ++i) {
195+
auto &pri = primes[i];
196+
if (pri.size() <= 2)
197+
break;
198+
if (mul > N / pri[2])
199+
break;
200+
for (int64_t j = 2; j < pri.size(); ++j) {
201+
if (mul > N / pri[j])
202+
break;
203+
dfs(i + 1, mul * pri[j], hd * h[i][j]);
204+
}
205+
}
206+
}
207+
208+
int main() {
209+
sieve();
210+
dfs(0, 1, 1);
211+
cout << sum - 1 << endl;
212+
return 0;
213+
}
214+
```

0 commit comments

Comments
 (0)