版本比较
题解:P14221 [ICPC 2024 Kunming I] 学而时习之
正文
增加 2 行删除 2 行
7 行未改动89101112121314147 行未改动
- 若 $\gcd(a_1,a_2,\dots a_l)=\gcd(a_1,a_2,\dots a_{l+1})$,则让 $l+1\to l$;- 若 $\gcd(a_r,a_{r+1},\dots a_n)=\gcd(a_{r-1},a_r,\dots a_n)$,则让 $r-1\to r$。
可以发现,由于始终要对前缀和后缀取 gcd,进所以行如上操作后答案一定不会变劣。因此我们只需要对满足 $\gcd(a_1,a_2,\dots a_l)\neq\gcd(a_1,a_2,\dots a_{l-1})$ 的 $l$ 和 满足 $\gcd(a_r,a_{r+1},\dots a_n)\neq\gcd(a_{r+1},a_{r+2},\dots a_n)$ 的 $r$ 进行暴力枚举。众所周知这样的 $l$ 和 $r$ 的个数是 $O(\log V)$ 的[^1],因此我们直接暴力枚举可以做到 $O(n\log n+\log^3 V)$。可以发现,由于始终要对前缀和后缀取 gcd,所以进行如上操作后答案一定不会变劣。因此我们只需要对满足 $\gcd(a_1,a_2,\dots a_l)\neq\gcd(a_1,a_2,\dots a_{l-1})$ 的 $l$ 和 满足 $\gcd(a_r,a_{r+1},\dots a_n)\neq\gcd(a_{r+1},a_{r+2},\dots a_n)$ 的 $r$ 进行暴力枚举。众所周知这样的 $l$ 和 $r$ 的个数是 $O(\log V)$ 的[^1],因此我们直接暴力枚举可以做到 $O(n\log n+\log^3 V)$。
[^1]:证明:对于 $x,y\in \N^*$,满足 $\gcd(x,y)\mid x$,由于最小质因数为 $2$,所以当 $\gcd(x,y)\neq x$ 时必定满足 $gcd(x,y)\le \frac x 2$。因此至多有 $O(\log V)$ 个不同的前缀/后缀 gcd。[^1]:证明:对于 $x,y\in \N^*$,满足 $\gcd(x,y)\mid x$,由于最小质数为 $2$,所以当 $\gcd(x,y)\neq x$ 时必定满足 $gcd(x,y)\le \frac x 2$。因此至多有 $O(\log V)$ 个不同的前缀/后缀 gcd。