版本比较
Atcoder-Countings1
正文
增加 116 行删除 116 行
16 行未改动1718192021212223242525262727282930315 行未改动373839404142434142434445464713 行未改动6162636465656667686969707171727374757677787980777879808182838423 行未改动10810911011111211211311411511611611711811811912012112212312412512612712812812913013113213313313413513613710 行未改动14814915015115215215315415515615615715815815916016116216316416516616716816816917017117211 行未改动18418518618718818818919019119219219319419419519619719819920020120020120220320420520620520620720820921017 行未改动22822923023123223223323423523623623723823823924024124224324424524624724824624724824925025125222 行未改动2752762772782792792802812822832832842852852862872882897 行未改动2972982993003013013023033043052 行未改动30830931031131231231331431531631631731831831932032132232332432532632732832632732832933033133213 行未改动3463473483493503503513523533543553563573583593603603613623623633643653663 行未改动37037137237337437437537637737815 行未改动39439539639739839839940040140210 行未改动41341441541641741741841942042142142242342342442542642742842943043143242943043143243343443543643643743843944041 行未改动4824834844854864864874884894904914924934944944954964964974984995005 行未改动50650750850951051051151251351451451551651651751851952033 行未改动55455555655755855855956056156256256356456456556656756856957057157157257357357457557657718 行未改动5965975985996006006016026036047 行未改动61261361461561661661761861861962062162262362462562662762862862963063163215 行未改动64864965065165265265365465565665665765865865966066066166266366466566666766866866967067067167267267367467567615 行未改动69269369469569669669769869970070170270270370470470570670770870870971071071171271271371471571671771871971871972072172272342 行未改动76676776876977077077177277377477477577677677777877978019 行未改动8008018028038048048058068068078088088098108118128138148158168178188168178188198208218227 行未改动8308318328338348348358368378388388398408418427 行未改动8508518528538548548558568578588588598608618623 行未改动8668678688698708708718728738743 行未改动87887988088188288288388488488588688688788888989013 行未改动90490590690790890890991091191234 行未改动94794894995095195195295395495521 行未改动97797897998098198198298398498518 行未改动100410051006100710081008100910101011101222 行未改动103510361037103810391039104010411042104310441045104610471047104810491049105010511052105332 行未改动10861087108810891090109010911092109310941094109510961096109710981098109911001101110244 行未改动16 行未改动## AC1 ARC144D AND OR Equation(2468)
开国元勋。意义重大,故另起一篇。
[ARC144D AND OR Equation](https://www.cnblogs.com/Schucking-Sattin/p/16869327.html) [ARC144D AND OR Equation](https://www.cnblogs.com/Schucking-Sattin/p/16869327.html)
## AC2 ABC273G Row Column Sums 2(2521)
[洛谷:ABC273G Row Column Sums 2](https://www.luogu.com.cn/problem/AT_abc273_g) [洛谷:ABC273G Row Column Sums 2](https://www.luogu.com.cn/problem/AT_abc273_g)
[Atcoder:ABC273G Row Column Sums 2](https://atcoder.jp/contests/abc273/tasks/abc273_g) [Atcoder:ABC273G Row Column Sums 2](https://atcoder.jp/contests/abc273/tasks/abc273_g)
### Problem
给定正整数$N$,求有多少个 $N\times N$ 的矩阵满足以下条件:5 行未改动$C_i$ , $R_i$ 都是 $0$ 到 $2$ 之间的正整数。
答案对 $998244353$ 取模。
- $1\ \leq\ N\ \leq\ 5000$ - $0\ \leq\ R_i\ \leq\ 2$ - $0\ \leq\ C_i\ \leq\ 2$ - $1\ \leq\ N\ \leq\ 5000$- $0\ \leq\ R_i\ \leq\ 2$- $0\ \leq\ C_i\ \leq\ 2$
### Solution
显然 $\sum{r} = \sum{c}$,否则答案为 $0$。13 行未改动若 $r_i = 1$:$f[i][x] = f[i - 1][x](y + 1) + f[i - 1][x + 1](x + 1)$。
若 $r_i = 2$:$f[i][x] = f[i - 1][x + 1](x + 1) + f[i - 1][x + 2]\binom{x + 2}{2} + f[i - 1][x]\binom{y + 2}{y} + f[i - 1][x + 1](x + 1)y$。
[ABC273G code](https://atcoder.jp/contests/abc273/submissions/35717030) [ABC273G code](https://atcoder.jp/contests/abc273/submissions/35717030)
## AC3 AGC012D Colorful Balls(2760)
[洛谷:AGC012D Colorful Balls](https://www.luogu.com.cn/problem/AT_agc012_d) [洛谷:AGC012D Colorful Balls](https://www.luogu.com.cn/problem/AT_agc012_d)
[Atcoder:AGC012D Colorful Balls](https://atcoder.jp/contests/agc012/tasks/agc012_d) [Atcoder:AGC012D Colorful Balls](https://atcoder.jp/contests/agc012/tasks/agc012_d)
### Problem
$N$ 个球排成一排,第 $i$ 个球的颜色为 $c_i$,重量为 $w_i$。我们定义「一次操作」为:选择两个颜色**相同**,且重量之和不超过 $X$ 的球,交换它们的位置;或选择两个颜色**不同**,且重量之和不超过 $Y$ 的球,交换它们的位置。问进行任意次操作后,可以得到多少种不同的颜色序列。输出答案对 $10^9+7$ 取模的结果。
- $1\ ≦\ N\ ≦\ 2\ ×\ 10^5$ - $1\ ≦\ X,\ Y\ ≦\ 10^9$ - $1\ ≦\ c_i\ ≦\ N$ - $1\ ≦\ w_i\ ≦\ 10^9$ - $1\ ≦\ N\ ≦\ 2\ ×\ 10^5$- $1\ ≦\ X,\ Y\ ≦\ 10^9$- $1\ ≦\ c_i\ ≦\ N$- $1\ ≦\ w_i\ ≦\ 10^9$
### Solution
推性质:**传递性**:如果 $a$ 和 $b$ 可以交换,$a$ 与 $c$ 可以交换,则 $b$ 与 $c$ 可以交换。讨论一下就可以了,自证不难。23 行未改动总结一下,我们只需计算 $x$ 所在连通块价值。
连边方式,同色连向该色最小重量点,各色最小重量点连向全局最小重量点,全局最小重量点的颜色对应的所有点连向最小重量次小颜色的最小重量点。这样是 $O(n)$ 的。
[code AGC012D](https://atcoder.jp/contests/agc012/submissions/36519094) [code AGC012D](https://atcoder.jp/contests/agc012/submissions/36519094)
## AC4 AGC040C Neither AB nor BA(2740)
[洛谷:AGC040C Neither AB nor BA](https://www.luogu.com.cn/problem/AT_agc040_c) [洛谷:AGC040C Neither AB nor BA](https://www.luogu.com.cn/problem/AT_agc040_c)
[Atcoder:AGC040C Neither AB nor BA](https://atcoder.jp/contests/agc040/tasks/agc040_c) [Atcoder:AGC040C Neither AB nor BA](https://atcoder.jp/contests/agc040/tasks/agc040_c)
### Problem
给出一个大于0的偶数 $N$ 。
请找出长度为 $N$ ,由'`A`','`B`','`C`'这三个字母组成且可以由下列规则把其变为空串的字符串 $s$ 的数量。
- 不断选择$s$中任意除'`AB`'和'`BA`'外的长度为2的子串并删除。
比如'`ABBC`'是$N=4$条件下的一个合法字符串,因为我们可以通过这样的方式将其变为空串: 比如'`ABBC`'是$N=4$条件下的一个合法字符串,因为我们可以通过这样的方式将其变为空串:'`ABBC`'→(删除'`BB`')→'`AC`'→(删除'`AC`')→'`(空串)`'
答案可能很大,所以请将结果对$998244353$取模。
- $2 \le N \le 10^7$ - $2 \le N \le 10^7$- $N$ 是偶数
### Solution
10 行未改动由于 $A$ 和 $B$ 的数量最多只有一个不合法,因此不会算重,减去时直接乘上一个系数 $2$ 。
即 $Ans = 3^{n} - 2\sum\limits_{i = \frac{n}{2} + 1}^{n}2^{n - i}\binom{n}{i}$。
[code AGC040C](https://atcoder.jp/contests/agc040/submissions/36569595) [code AGC040C](https://atcoder.jp/contests/agc040/submissions/36569595)
## AC5 ABC267G Increasing K Times(2561)
[洛谷:ABC267G Increasing K Times](https://www.luogu.com.cn/problem/AT_abc267_g) [洛谷:ABC267G Increasing K Times](https://www.luogu.com.cn/problem/AT_abc267_g)
[Atcoder:ABC267G Increasing K Times](https://atcoder.jp/contests/abc267/tasks/abc267_g) [Atcoder:ABC267G Increasing K Times](https://atcoder.jp/contests/abc267/tasks/abc267_g)
### Problem
给定一个正整数序列 $A=(a_1,a_2,\ldots,a_n)$,问有多少个 $1\sim n$ 的排列 $P=(p_1,p_2,\ldots,p_n)$ 满足:
- 存在恰好 $k$ 个整数 $i(1\leqslant i\leqslant n-1)$ 满足 $a_{p_i}<a_{p_{i+1}}$
对 $998244353$ 取模。
$2\leqslant n\leqslant 5000,0\leqslant k\leqslant n-1,1\leqslant a_i\leqslant n$ $2\leqslant n\leqslant 5000,0\leqslant k\leqslant n-1,1\leqslant a_i\leqslant n$
### Solution
还是等价的思想,将原序列排序后计数,答案是一样的。11 行未改动f_{i, j} = (i - j - x)f_{i - 1, j - 1} + (j + x + 1)f_{i - 1, j}$$边界为 $f_{1, 0} = 1$。甚至还可以滚动数组。
[code ABC267G](https://atcoder.jp/contests/abc267/submissions/36665684) [code ABC267G](https://atcoder.jp/contests/abc267/submissions/36665684)
## AC6 AGC049D Convex Sequence(2602)
[洛谷:AGC049D Convex Sequence](https://www.luogu.com.cn/problem/AT_agc049_d) [洛谷:AGC049D Convex Sequence](https://www.luogu.com.cn/problem/AT_agc049_d)
[Atcoder:AGC049D Convex Sequence](https://atcoder.jp/contests/agc049/tasks/agc049_d) [Atcoder:AGC049D Convex Sequence](https://atcoder.jp/contests/agc049/tasks/agc049_d)
### Problem
给定整数 $N$ 和 $M$,问有多少个长为 $N$ 的非负整数数列 $A$,满足以下条件:
- $A_1+A_2+\ldots+A_N = M$ - 对任意 $i(2 \leq i \leq N-1)$ ,都有 $2A_i \leq A_{i-1} + A_{i+1}$ - $A_1+A_2+\ldots+A_N = M$- 对任意 $i(2 \leq i \leq N-1)$ ,都有 $2A_i \leq A_{i-1} + A_{i+1}$
答案对 $10^9+7$ 取模。
- $1\ \leq\ N\ \leq\ 10^5$ - $1\ \leq\ M\ \leq\ 10^5$ - $1\ \leq\ N\ \leq\ 10^5$- $1\ \leq\ M\ \leq\ 10^5$
### Solution
$2a_i \le a_{i - 1} + a_{i + 1} \iff a_{i} - a_{i + 1} \le a_{i - 1}- a_{i}$。17 行未改动考虑有效的背包物品数量为 $O(\sqrt{m})$,因此时间复杂度没有问题。
这个可以当 **可修改完全背包板子**。
[code AGC049D](https://atcoder.jp/contests/agc049/submissions/38745325) [code AGC049D](https://atcoder.jp/contests/agc049/submissions/38745325)
## AC7 AGC046C Shift(2130)
[洛谷:AGC046C Shift](https://www.luogu.com.cn/problem/AT_agc046_c) [洛谷:AGC046C Shift](https://www.luogu.com.cn/problem/AT_agc046_c)
[Atcoder:AGC046C Shift](https://atcoder.jp/contests/agc046/tasks/agc046_c) [Atcoder:AGC046C Shift](https://atcoder.jp/contests/agc046/tasks/agc046_c)
### Problem
给定一个只由 `0` 和 `1` 组成的序列 $S$ 。求对 $S$ 进行以下的操作 $[0,k]$ 次后可以得到的字符串种类个数模 $998244353$ 后的值。
- 将一个 `1` 移到它左边的任意一个 `0` 的左边。
- $1 \le |S| \le 300$ - $0 \le k \le 10^9$ - $S$ 只包含 `0` 和 `1` - $1 \le |S| \le 300$- $0 \le k \le 10^9$- $S$ 只包含 `0` 和 `1`
### Solution
记共有 $m$ 个 $0$,$a_i$ 表示第 $i - 1$ 个 $0$ 到第 $i$ 个 $0$ 之间的 $1$ 的数量。22 行未改动f[i][j + w][k + \max(w - a_i)] \leftarrow f[i - 1][j][k]$$由于每一层循环都有一定的范围,因此实际上几乎跑不满,$O(n^4)$ 可以艹过去。
[code AGC046C](https://atcoder.jp/contests/agc046/submissions/38739520) [code AGC046C](https://atcoder.jp/contests/agc046/submissions/38739520)
## AC8 ARC071F Infinite Sequence(2336)
[洛谷:ARC071F Infinite Sequence](https://www.luogu.com.cn/problem/AT_arc071_d) [洛谷:ARC071F Infinite Sequence](https://www.luogu.com.cn/problem/AT_arc071_d)
[Atcoder:ARC071F Infinite Sequence](https://atcoder.jp/contests/arc071/tasks/arc071_d) [Atcoder:ARC071F Infinite Sequence](https://atcoder.jp/contests/arc071/tasks/arc071_d)
### Problem
定义 $n-$可爱序列 指无限长的由 $\{1,2...,n\}$ 组成的序列。同时 $a_1,a_2...$满足以下条件:7 行未改动$n\leq{10^6}$。
### Solution
模拟一下,容易发现一个结论,当 $a_i > 1$ 且 $a_{i + 1} > 1$ 时,后面的序列都会固定下来,即 $a_i a_{i + 1}a_{i + 1}a_{i + 1}\cdots$ 模拟一下,容易发现一个结论,当 $a_i > 1$ 且 $a_{i + 1} > 1$ 时,后面的序列都会固定下来,即 $a_i a_{i + 1}a_{i + 1}a_{i + 1}\cdots$
直觉上反向 dp 会好想一些,设 $f_i$ 表示填完第 $i\sim n$ 位合法的方案数。分类讨论:
- $a_i = 1$:后面放什么都无所谓,$f_{i} \leftarrow f_{i + 1}$。2 行未改动 - $a_{i + 1} = 1$:根据题意,位置 $i$ 后为一段固定的长度为 $i$ 的 $1$ 序列,而之后的序列没有关于 $a_{i}$ 和 $a_{i + 1}$ 的限制,可以任意合法放置,因此 $f_{i} \leftarrow f_{i + a_{i} + 1}$。这里枚举 $a_i$ 是 $O(n)$ 的,但发现其取值连续,可以通过维护一个前缀和(后缀和?)$O(1)$ 转移。
$f$ 的边界/越界情况要注意一下。
[code:ARC071F](https://atcoder.jp/contests/arc071/submissions/36690846) [code:ARC071F](https://atcoder.jp/contests/arc071/submissions/36690846)
## AC9 ARC110D Binomial Coefficient is Fun(2078)
[洛谷:ARC110D Binomial Coefficient is Fun](https://www.luogu.com.cn/problem/AT_arc110_d) [洛谷:ARC110D Binomial Coefficient is Fun](https://www.luogu.com.cn/problem/AT_arc110_d)
[Atcoder:ARC110D Binomial Coefficient is Fun](https://atcoder.jp/contests/arc110/tasks/arc110_d) [Atcoder:ARC110D Binomial Coefficient is Fun](https://atcoder.jp/contests/arc110/tasks/arc110_d)
### Problem
我们有一个包含 $N$ 个非负整数的序列 $A$。
对于所有长度为 $N$ 且和不超过 $M$ 的非负整数序列 $B$,求 $\prod_{i = 1}^N {B_i \choose A_i}$ 之和, 对 $(10^9 + 7)$ 取模。
- $1 \le N \le 2000$ - $1 \le M \le 10^9$ - $0 \le A_i \le 2000$ - $1 \le N \le 2000$- $1 \le M \le 10^9$- $0 \le A_i \le 2000$
### Solution 1
越来越菜了,2000 级别的计数想着很困难。13 行未改动然而这么做是假定了 $\sum\limits_{i = 1}^{n}b_i = m$。但实际上 $\sum\limits_{i = 1}^{n}b_i \le m$,因此有一步妙妙的方法,在 $m + n$ 个位置中选 $S + n$ 个位置,相当于用 $n$ 个分界线,其中最后一个分界线在所有所选位置的末尾,专门限制 $\sum\limits_{i = 1}^{n}b_i$ 的大小。
$Ans = \binom{m + n}{S + n}$。
[code ARC110D](https://atcoder.jp/contests/arc110/submissions/36721825) [code ARC110D](https://atcoder.jp/contests/arc110/submissions/36721825)
### Solution 2
普通生成函数。
## AC10 ARC110E Shorten ABC(2973)
[洛谷:ARC110E Shorten ABC](https://www.luogu.com.cn/problem/AT_arc110_e) [洛谷:ARC110E Shorten ABC](https://www.luogu.com.cn/problem/AT_arc110_e)
[Atcoder:ARC110E Shorten ABC](https://atcoder.jp/contests/arc110/tasks/arc110_e) [Atcoder:ARC110E Shorten ABC](https://atcoder.jp/contests/arc110/tasks/arc110_e)
### Problem
给定一个长度为 $N$ 的字符串 $S$,包含 `A,B,C`。3 行未改动- 选定 $i\in [1,n-1]$,满足 $S_i\neq S_{i+1}$,将这两个字符替换为 `A,B,C` 没有出现的那个,比如你可以将 `AB` 替换为 `C`。
求操作后不同串的个数对 $1000000007$ 取模。
- $1 \leq N \leq 10^6$ - $1 \leq N \leq 10^6$
### Solution
[ARC094F] Normalization 与这个题几乎一模一样。那天上午在zh机房用着自己的笔记本电脑做出了这道题,记忆犹新。15 行未改动
转移是这样的:(记 $len_{i, j}$ 为以 $i$ 为开头,形成 $T$ 中字符 $j$ 的 $S$ 中的最短区间长度,$a_i$ 为 $S$ 中字符转化后的数字)$$\begin{aligned}len_{i ,j} = len_{i ,j} =\begin{cases}1, & a_{i} = j \\len_{i + 1, j \oplus a_i} + 1,& a_i \neq j\end{cases}10 行未改动考虑最后如何统计答案。如果只考虑 $f_{n}$,那么有的 $f_{i}$ 因为我们的限制而转移不过去,但实际上可以将后面那段没法单独形成字符的区间归到已形成的串中的最后一个字符。
因此枚举每一个位置,如果 $f_i \oplus f_n = 0$,就计入答案。可见 $f_n$ 其实是正确答案的一部分。
[code ARC110E](https://atcoder.jp/contests/arc110/submissions/36748892) [code ARC110E](https://atcoder.jp/contests/arc110/submissions/36748892)
## AC11 ARC111F Do you like query problems(3119)
[洛谷:ARC111F Do you like query problems](https://www.luogu.com.cn/problem/AT_arc111_f) [洛谷:ARC111F Do you like query problems](https://www.luogu.com.cn/problem/AT_arc111_f)
[Atcoder:ARC111F Do you like query problems](https://atcoder.jp/contests/arc111/tasks/arc111_f) [Atcoder:ARC111F Do you like query problems](https://atcoder.jp/contests/arc111/tasks/arc111_f)
### Problem
给出三个数 $n,m,q$。
你有一个长度为 $n$ 的序列 $a$,初始全为为 $0$,你有三种操作: 操作 $1$:给出 $l,r,v$,让区间 $[l,r]$ 对 $v$ 取 $\min$。 操作 $2$:给出 $l,r,v$,让区间 $[l,r]$ 对 $v$ 取 $\max$。 操作 $3$,给出 $l,r$,求区间和,将其累加进一个叫 $sum$ 的变量里。 你有一个长度为 $n$ 的序列 $a$,初始全为为 $0$,你有三种操作:操作 $1$:给出 $l,r,v$,让区间 $[l,r]$ 对 $v$ 取 $\min$。操作 $2$:给出 $l,r,v$,让区间 $[l,r]$ 对 $v$ 取 $\max$。操作 $3$,给出 $l,r$,求区间和,将其累加进一个叫 $sum$ 的变量里。
**你并不需要维护这个数据结构**,而是统计一共有 $q$ 个操作的情况下,所有不同的操作序列中 $3$ 操作得到的 $sum$ 的总和,对 $998244353$ 取模。你需要保证 $v\in[0,m-1]$。
- $1 \leq N,M,Q \leq 200000$ - $1 \leq N,M,Q \leq 200000$
### Solution 1
**对所有可能的情况求某个值的和,可以考虑用期望解决问题。** 类似的套路还出现在 AGC028B Removing Blocks。41 行未改动Ans = \left( (2m + 1)\frac{n(n + 1)}{2} \right)^q V$$算算就可以了。
[code ARC111F](https://atcoder.jp/contests/arc111/submissions/36747434) [code ARC111F](https://atcoder.jp/contests/arc111/submissions/36747434)
### Solution 2
## AC12 ARC128D Neq Neq(2554)
[洛谷:ARC128D Neq Neq](https://www.luogu.com.cn/problem/AT_arc128_d) [洛谷:ARC128D Neq Neq](https://www.luogu.com.cn/problem/AT_arc128_d)
[Atcoder:ARC128D Neq Neq](https://atcoder.jp/contests/arc128/tasks/arc128_d) [Atcoder:ARC128D Neq Neq](https://atcoder.jp/contests/arc128/tasks/arc128_d)
### Problem
给定长度为 $n$ 的序列 $a$,可以做若干次操作:5 行未改动$n \le 2 \times 10^5$,$a_i \le n$。
### Solution
[code ARC128D](https://atcoder.jp/contests/arc128/submissions/37396196) [code ARC128D](https://atcoder.jp/contests/arc128/submissions/37396196)
## AC13 ARC115D Odd Degree(2325)
[洛谷:ARC115D Odd Degree](https://www.luogu.com.cn/problem/AT_arc115_d) [洛谷:ARC115D Odd Degree](https://www.luogu.com.cn/problem/AT_arc115_d)
[Atcoder:ARC115D Odd Degree](https://atcoder.jp/contests/arc115/tasks/arc115_d) [Atcoder:ARC115D Odd Degree](https://atcoder.jp/contests/arc115/tasks/arc115_d)
### Problem
给定一个 $n$ 个点,$m$ 条边的无向图,求它的奇数度数点数为 $k$ 的生成子图的个数,$k\in [0, n]$。33 行未改动每次枚举一下 $j$ 和偶数 $k$。
时间复杂度 $O(n^2)$。
[code ARC115D](https://atcoder.jp/contests/arc115/submissions/37411837) [code ARC115D](https://atcoder.jp/contests/arc115/submissions/37411837)
## ARC115E LEQ and NEQ(2363)
[洛谷:ARC115E LEQ and NEQ](https://www.luogu.com.cn/problem/AT_arc115_e) [洛谷:ARC115E LEQ and NEQ](https://www.luogu.com.cn/problem/AT_arc115_e)
[Atcoder:ARC115E LEQ and NEQ](https://atcoder.jp/contests/arc115/tasks/arc115_e) [Atcoder:ARC115E LEQ and NEQ](https://atcoder.jp/contests/arc115/tasks/arc115_e)
### Problem
给定一个长度为 $n$ 的序列 $a_1,a_2,\cdots ,a_n$,输出满足如下条件的序列 $x$ 的方案数:
1. $1\leq x_i\leq a_i$2. $x_i\neq x_{i+1}(1\leq i\leq n-1)$ 2. $x_i\neq x_{i+1}(1\leq i\leq n-1)$
$2\le n\le 5\times 10^5,1\le a_i\le 10^9$ $2\le n\le 5\times 10^5,1\le a_i\le 10^9$
### Solution 1
$O(n\log{n})$。18 行未改动具体地,记前缀和 $S = \sum\limits_{k = 1}^{a_{i - 1}}f_{k}$。
$\forall j \in [1, a_{i - 1}]$,$f_{j}$ 变为 $-f_{j}$。
$\forall j\in [1, a_{i}], f_{j} \leftarrow S$。 $\forall j\in [1, a_{i}], f_{j} \leftarrow S$。
$\forall j\in (a_{i}, \infty)$,$f_{j} = 0$。
区间赋值,区间加,区间取相反数,区间求和。7 行未改动
## AC15 AGC036C GP 2(2357)
[洛谷:AGC036C GP 2](https://www.luogu.com.cn/problem/AT_agc036_c) [洛谷:AGC036C GP 2](https://www.luogu.com.cn/problem/AT_agc036_c)
[Atcoder:AGC036C GP 2](https://atcoder.jp/contests/agc036/tasks/agc036_c) [Atcoder:AGC036C GP 2](https://atcoder.jp/contests/agc036/tasks/agc036_c)
### Problem
有一个序列 $a_{1 \cdots n}$,初始时均为 $0$。每一次操作中,可以选择 $i \ne j$,将 $a_i$ 加上 $1$,将 $a_j$ 加上 $2$。操作共进行 $m$ 次,求最终序列有多少种可能的情况。答案对 $998244353$ 取模。
$n \leq 10^6, m \leq 5 \times 10^5$。
### Solution
**不考虑过程,直接分析什么样的数列是合法的结果数列。** **不考虑过程,直接分析什么样的数列是合法的结果数列。**
- $\sum\limits_{i = 1}^{n}a_i = 3m$。- $\max\limits_{i = 1}^{n}a_i \le 2m$。- $\sum\limits_{i = 1}^{n}[a_i \bmod 2] \le m$。15 行未改动Ans = -n\binom{m + n - 2}{n - 1} + \sum\limits_{k = 0}^{m}\binom{n}{k}\binom{p + n - 1}{n - 1}[(3m - k) \bmod 2 = 0]$$其中 $p = \frac{3m - k}{2}$。
[code AGC036C](https://atcoder.jp/contests/agc036/submissions/37444885) [code AGC036C](https://atcoder.jp/contests/agc036/submissions/37444885)
## AC16 ARC028D 注文の多い高橋商店(2997)
[洛谷:ARC028D 注文の多い高橋商店](https://www.luogu.com.cn/problem/AT_arc028_4) [洛谷:ARC028D 注文の多い高橋商店](https://www.luogu.com.cn/problem/AT_arc028_4)
[Atcoder:ARC028D 注文の多い高橋商店](https://atcoder.jp/contests/arc028/tasks/arc028_4) [Atcoder:ARC028D 注文の多い高橋商店](https://atcoder.jp/contests/arc028/tasks/arc028_4)
[ARC028D 独立题解](https://www.cnblogs.com/Schucking-Sattin/p/17080034.html) [ARC028D 独立题解](https://www.cnblogs.com/Schucking-Sattin/p/17080034.html)
### Problem
给定 $n$,$m$。有 $n$ 种商品,编号从 $1$ 到 $n$,第 $i$ 种商品最多能拿 $a_i$ 个。
共 $q$ 次询问。每次询问给定 $k$,$x$,求第 $k$ 种商品 **恰好** 拿走 $x$ 个的前提下,在 $n$ 种商品中一共拿走 $m$ 个商品的方案数。两种方案不同当且仅当存在一种商品在二方案中被拿走的个数不同。输出答案对 $10^9+7$ 取模的结果。
- $1 \leq n,m,a_i \leq 2\times 10^3$ - $1 \leq n,m,a_i \leq 2\times 10^3$
- $1\leq k\leq n$ - $1\leq k\leq n$
- $1\leq x\leq a_k$ - $1\leq x\leq a_k$
### Solution
可以当作 **可撤销背包模板题**。15 行未改动- 仿照上面的做法,倒着处理一遍 dp 数组 $g$,统计答案时排除要求的物品即可。
由于查询时还要枚举左右物品的数量分配,因此时间复杂度为 $O(nm + Qm)$。
[code ARC028D - solution1](https://atcoder.jp/contests/arc028/submissions/38345026) [code ARC028D - solution1](https://atcoder.jp/contests/arc028/submissions/38345026)
- 用生成函数表示多重背包的总方案数,即对于一种数量为 $s$ 的物品,其贡献为 $\sum\limits_{i = 0}^{s}x^{i}$,合起来的生成函数即为若干个分别的生成函数相乘。删除一种物品,即为除去该种物品对应的生成函数,多项式除法,但不用多项式工业,直接暴力即可,$O(nm)$。
- 采用分治思想,递归到叶节点表示删除该种物品。那么如果递归左边,右边就可以背包合并起来;反之。如此,每种物品会被合并 $O(\log{n})$ 次,总时间复杂度为 $O(nm\log{n})$。
[code ARC028D - solution3](https://atcoder.jp/contests/arc028/submissions/38345176) [code ARC028D - solution3](https://atcoder.jp/contests/arc028/submissions/38345176)
## AC17 Span Covering(3012)
[洛谷:Span Covering](https://www.luogu.com.cn/problem/AT_dwacon6th_prelims_e) [洛谷:Span Covering](https://www.luogu.com.cn/problem/AT_dwacon6th_prelims_e)
[Atcoder:Span Covering](https://www.luogu.com.cn/problem/AT_dwacon6th_prelims_e) [Atcoder:Span Covering](https://www.luogu.com.cn/problem/AT_dwacon6th_prelims_e)
[Span Covering 独立题解](https://www.cnblogs.com/Schucking-Sattin/p/17080007.html) [Span Covering 独立题解](https://www.cnblogs.com/Schucking-Sattin/p/17080007.html)
### Problem
有一个区间 $[0, X)$,你有一个数组 $L_1, L_2, \cdots, L_n$。对于每个 $i$,你可以选择一个整数 $j$ 满足 $0 \le j \le X- L_i$,并覆盖 $[j, j + L_i)$ 这个区间。问有多少种方案,使得整个区间都被覆盖,方案数对 $10^9 + 7$ 取模。
- $ 1\ \leq\ N\ \leq\ 100 $ - $ 1\ \leq\ L_i\ \leq\ X\ \leq\ 500 $ - $ 1\ \leq\ N\ \leq\ 100 $- $ 1\ \leq\ L_i\ \leq\ X\ \leq\ 500 $
### Solution
由于放置线段的先后顺序不影响方案数的统计,因此 **先将线段从小到大排序以便于转移状态** 。42 行未改动注意允许不放某些线段的情况。
还有就是,我们放置的时候需要用一条线段人为地把本来挨在一起但并没有被视作同一个段内的两条线段给拼接到一起,即此时属于情况 $3$,$l = 0$。
[code Span Covering](https://atcoder.jp/contests/dwacon6th-prelims/submissions/38328402) [code Span Covering](https://atcoder.jp/contests/dwacon6th-prelims/submissions/38328402)
## ARC154E Reverse and Inversion(3506)
[洛谷:ARC154E Reverse and Inversion](https://www.luogu.com.cn/problem/AT_arc154_e) [洛谷:ARC154E Reverse and Inversion](https://www.luogu.com.cn/problem/AT_arc154_e)
[Atcoder:ARC154E Reverse and Inversion](https://atcoder.jp/contests/arc154/tasks/arc154_e) [Atcoder:ARC154E Reverse and Inversion](https://atcoder.jp/contests/arc154/tasks/arc154_e)
### Problem
给定一个长为 $n$ 的排列,定义一次操作为:任取排列的一段区间,对这段区间进行翻转。19 行未改动
## AC19 ARC153F Tri-Colored Paths(3597)
[洛谷:ARC153F Tri-Colored Paths](https://www.luogu.com.cn/problem/AT_arc153_f) [洛谷:ARC153F Tri-Colored Paths](https://www.luogu.com.cn/problem/AT_arc153_f)
[Atcoder:ARC153F Tri-Colored Paths](https://atcoder.jp/contests/arc153/tasks/arc153_f) [Atcoder:ARC153F Tri-Colored Paths](https://atcoder.jp/contests/arc153/tasks/arc153_f)
[ARC153F 独立题解](https://www.cnblogs.com/Schucking-Sattin/p/17080007.html) [ARC153F 独立题解](https://www.cnblogs.com/Schucking-Sattin/p/17080007.html)
### Problem
给出一张 $N$ 个点 $M$ 条边的简单无向连通图。
你可以将分别每一条边染为 $1,2,3$ 三种颜色之一。问不同的染色方案数,使得图中存在一条路径,其上同时存在三种颜色的边。答案对 $998244353$ 取模。
- $ 3\ \leq\ N\leq\ 2\times\ 10^5 $ - $ 3\ \leq\ M\leq\ 2\times\ 10^5 $ - $ 1\ \leq\ A_i,\ B_i\ \leq\ N $ - $ 3\ \leq\ N\leq\ 2\times\ 10^5 $- $ 3\ \leq\ M\leq\ 2\times\ 10^5 $- $ 1\ \leq\ A_i,\ B_i\ \leq\ N $
### Solution
**分类讨论**。这题要多画图思考。7 行未改动首先,我们可以轻松地得出整个图只有一种或两种颜色的方案。
于是我们只考虑整个图有三种颜色的方案。
#### $n > 3$ #### $n > 3$
环长 $> 3$,图中一定存在三色路径,因此对 $cnt$ 没有贡献。
#### $n = 3$ #### $n = 3$
当环长 $> 3$ 时,断掉重复颜色的那条边,图中一定存在三色路径,对 $cnt$ 无贡献。
所以只考虑三色三元环。7 行未改动
幸运的是这种情况再连一条边就存在三色路径了,因此我们可以对 $n\le 4$ 暴力统计。
#### $n = 2$ #### $n = 2$
画图得知,异色边不论在环内外还是环内(此时异色边将环分成两个部分,且两个部分内的颜色相同),都会出现三色路径。
#### $n = 1$ #### $n = 1$
考虑每一个割点,我们发现一个连通块内不能存在两种颜色的边。
注意我们有每种颜色的边都要出现的条件。3 行未改动所以对于一个割点,统计其分出来的连通块数,分配连通块的染色方案,注意三种颜色都要出现。
具体来说,如果有 $x$ 个连通块,那么对 $cnt$ 的贡献为 $3^x - 3\times 2^{x} + 3$。
[code ARC153F](https://atcoder.jp/contests/arc153/submissions/38503421) [code ARC153F](https://atcoder.jp/contests/arc153/submissions/38503421)
3 行未改动
## AC21 ABC222H Beautiful Binary Tree(3477)
[洛谷:ABC222H Beautiful Binary Tree](https://www.luogu.com.cn/problem/AT_abc222_h) [洛谷:ABC222H Beautiful Binary Tree](https://www.luogu.com.cn/problem/AT_abc222_h)
[Atcoder:ABC222H Beautiful Binary Tree](https://atcoder.jp/contests/abc222/tasks/abc222_h) [Atcoder:ABC222H Beautiful Binary Tree](https://atcoder.jp/contests/abc222/tasks/abc222_h)
[ABC222H 独立题解](https://www.cnblogs.com/Schucking-Sattin/p/17077175.html) [ABC222H 独立题解](https://www.cnblogs.com/Schucking-Sattin/p/17077175.html)
### Problem
定义大小为 $n$ 的 Beautiful Binary Tree 为满足以下条件的有根二叉树:13 行未改动$1 \le n \le 10^7$。
### Solution
#### $O(n^2)$ #### $O(n^2)$
首先,**将操作转化为条件以方便计数**。
我们可以分析出 Beautiful Binary Tree 需满足:34 行未改动同样分为单子节点和双子节点考虑,意义比较显然,与上面类似。
于是可以 $O(n^2)$ 递推出答案。但数据范围不允许我们这样就通过此题,于是展开了多项式工业。
#### $O(n\log{n})$ #### $O(n\log{n})$
记 $A(x)$ 与 $B(x)$ 分别为数列 $\{a_n \}$ 与 $\{b_n \}$ 的普通生成函数。
回顾 OGF 的定义:21 行未改动于是 $A(x) = x(1 + 3A(x) + A(x)^2)^2$。
可以用牛顿迭代做到 $O(n\log{n})$。
#### $O(n)$ #### $O(n)$
运用 **拉格朗日反演**。
由18 行未改动\end{aligned}$$这是直接二项式系数暴力展开。
[code ABC222H - solution1](https://atcoder.jp/contests/abc222/submissions/38485145) [code ABC222H - solution1](https://atcoder.jp/contests/abc222/submissions/38485145)
官方题解还给出了一种 dp 的方法:
记 $T(x) = 1 + 3x + x^2$,$m = 2n$,$U(x) = T(x)^m$。22 行未改动\end{aligned}$$由 $u_0 = 1, u_{1} = 3m$,递推即可。最后带回 $[x^n]A(x) = \frac{1}{n}[x^{n - 1}](1 + 3x + x^2)^{2n} = \frac{u_{n - 1}}{n}$。
[code ABC222H - solution2](https://atcoder.jp/contests/abc222/submissions/38485641) [code ABC222H - solution2](https://atcoder.jp/contests/abc222/submissions/38485641)
## ABC134F Permutation Oddness(2532)
## Three Gluttons(3315)
## AC24 ARC108D AB(1987)
[洛谷:ARC108D AB](https://www.luogu.com.cn/problem/AT_arc108_d) [洛谷:ARC108D AB](https://www.luogu.com.cn/problem/AT_arc108_d)
[Atcoder:ARC108D AB](https://atcoder.jp/contests/arc108/tasks/arc108_d) [Atcoder:ARC108D AB](https://atcoder.jp/contests/arc108/tasks/arc108_d)
### Problem
现有一个初始字符串 $s = AB$。现在有一种操作为在两个字符之间插入一个字符,对 `AA,AB,BA,BB` 分别有给定的插入字符。保证字符集为 `A,B`。问最后长度为 $n$ 的字符串的方案数。答案对 $10^9 + 7$ 取模。32 行未改动 因此有 $f_{0} = 1, f_{1} = 2, ans = f_{n - 3}$。
$c_{AB} = A$ 的情况类似,故不再赘述。
[code ARC108D](https://atcoder.jp/contests/arc108/submissions/38709544) [code ARC108D](https://atcoder.jp/contests/arc108/submissions/38709544)
## AC25 ARC108F Paint Tree(2646)
[洛谷:ARC108F Paint Tree](https://www.luogu.com.cn/problem/AT_arc108_f) [洛谷:ARC108F Paint Tree](https://www.luogu.com.cn/problem/AT_arc108_f)
[Atcoder:ARC108F Paint Tree](https://atcoder.jp/contests/arc108/tasks/arc108_f) [Atcoder:ARC108F Paint Tree](https://atcoder.jp/contests/arc108/tasks/arc108_f)
[ARC108F 独立题解](https://www.cnblogs.com/Schucking-Sattin/p/17106851.html) [ARC108F 独立题解](https://www.cnblogs.com/Schucking-Sattin/p/17106851.html)
### Problem
给定一棵 $n$ 个节点的树。你需要对每个节点黑白染色。44 行未改动