版本比较
论在键盘的大部分标点符号都坏掉的情况下都可以做些什么
正文
增加 495 行删除 20 行
1234556789246 行未改动全部展开256257258259260260261262263264135 行未改动全部展开40040140240340440440540640740813 行未改动42242342442542642742642742842943043143243343443543643643743843944044144144244344444511 行未改动4574584594604614614624634644644654664674686 行未改动4754764774784794794804814824834844854864874884894894904914924936 行未改动50050150250350450450550650750850951051151251251351451451551651651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960012 行未改动61361461561653961761861962062110 行未改动63263363463555863663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469552 行未改动748749750751621752753754755756165 行未改动全部展开92292392492579592692792892993040 行未改动97197297397484497597697797897913 行未改动9939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613473 行未改动(本人平时并不擅长 C++,很多内容和真实语法有偏差,还请见谅。另外,本文中的符号指不能在 C++ 语言中作为变量名的可见字符。作为注释的 `/` 不被算作使用符号。)
## 起因
受到 [机房大佬的文章](https://www.luogu.com.cn/article/t7rofp8j) 启发,我就在想既然排序都能用 `%:<>()` 这 $6$ 种符号实现,那解决 [逆序对](https://www.luogu.com.cn/problem/P1908) 又何尝不行呢?于是我就去寻找逆序对中优秀的实现,找到了可能比较好实现 $3$ 种:受到[机房大佬的文章](https://www.luogu.com.cn/article/t7rofp8j)启发,我就在想既然排序都能用 `%:<>()` 这 $6$ 种符号实现,那解决[逆序对](https://www.luogu.com.cn/problem/P1908)又何尝不行呢?于是我就去寻找逆序对中优秀的实现,找到了可能比较好实现 $3$ 种:+ 归并排序求逆序对。+ 动态开点权值线段树。+ 树状数组+离散化。
246 行未改动全部展开 if (t xor_eq l) <%%> if (l and_eq t xor l) <%%> if (l xor_eq l xor (l << 1)) <%%> %> if (t or_eq l) <%%> if (t or_eq l) <%%> %>%>```
135 行未改动全部展开 %> %> %> if (std::cout << ans) <%%> %> %> %>%>```
13 行未改动%:include<vector>
int main() <% if (std::ios::sync_with_stdio(0)) <%%> for (int n : <%0%>) if (std::cin >> n) for (int B : <%std::sqrt(n)%>) for (int n : <%0%>) if (std::cin >> n) for (int B : <%std::sqrt(n)%>) for (long long ans : <%0%>) for (auto a : std::array<%std::vector<int>(n)%>) for (auto b : <%std::vector<int>(n)%>) for (auto sum : <%std::vector<int>(n)%>) for (auto mp : <%std::vector<int>(n)%>) for (auto pos : <%std::vector<int>(n)%>) for (auto l : <%std::vector<int>(n)%>) for (auto r : <%std::vector<int>(n)%>) <% for (auto bitand i : a) if (std::cin >> i) <%%> for (auto bitand i : a) if (std::cin >> i) <%%> for (int i : std::views::iota(0) bitor std::views::take(n)) if (b<:i:> xor_eq a<:i:>) <%%> for (auto i : <%std::ranges::sort(b)%>) <%%> for (int i : std::views::iota(0) bitor std::views::take(n)) <% for (int l : <%0%>) for (int r : <%n%>) for (int r : <%n%>) for (int res : <%0%>) <% for (int b : <%0xffffffff%>) <% while (r bitand b) <% if (r xor_eq b) <%%> if (b and_eq r xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (r or_eq b) <%%> %> while (l < r or not (l xor r)) <% for (int mid : <%l%>) <%11 行未改动 %> if (a<:i:> xor_eq a<:i:> xor res) <%%> %> %> for (int i : <%0%>) for (int i : <%0%>) for (int t : <%0%>) <% while (i < n) <% for (int b : <%1%>) <% while (t bitand b) <% if (t xor_eq b) <%%> if (b and_eq t xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (t or_eq b) <%%> %>if (l<:t:> xor_eq i) <%%> for (int b : <%1%>) <% while (t bitand b) <% if (t xor_eq b) <%%> if (b and_eq t xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (t or_eq b) <%%> %>if (l<:t:> xor_eq i) <%%> for (int j : <%i%>) <% for (int b : <%B%>) <% while (i bitand b) <% if (i xor_eq b) <%%> if (b and_eq i xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (i or_eq b) <%%> %> while (j < i and j < n) <% if (pos<:j:> xor_eq t) <%%>6 行未改动 for (int i : std::views::iota(0) bitor std::views::take(n)) <% for (long long b : <%i%>) <% while (ans bitand b) <% if (ans xor_eq b) <%%> if (b and_eq ans xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (ans or_eq b) <%%> %> if (pos<:a<:i:>:> xor 1) <% for (int j : <%0%>) while (j < r<:1:> or not (j xor r<:1:>)) <% for (long long b : <%mp<:j:>%>) <% if (b xor_eq b xor compl b) <%%> while (ans bitand b) <% if (ans xor_eq b) <%%> if (b and_eq ans xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (ans or_eq b) <%%> %> for (long long b : <%mp<:j:>%>) <% if (b xor_eq b xor compl b) <%%> while (ans bitand b) <% if (ans xor_eq b) <%%> if (b and_eq ans xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (ans or_eq b) <%%> %> for (int b : <%1%>) <% while (j bitand b) <% if (j xor_eq b) <%%> if (b and_eq j xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (j or_eq b) <%%> %> for (long long b : <%1%>) <% while (ans bitand b) <% if (ans xor_eq b) <%%> if (b and_eq ans xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (ans or_eq b) <%%> %> %> for (int j : <%2%>) while (j < pos<:a<:i:>:>) <% for (long long b : <%sum<:j:>%>) <% if (b xor_eq b xor compl b) <%%> while (ans bitand b) <% if (ans xor_eq b) <%%> if (b and_eq ans xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (ans or_eq b) <%%> %> for (int b : <%1%>) <% while (j bitand b) <% if (j xor_eq b) <%%> if (b and_eq j xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (j or_eq b) <%%> %> for (long long b : <%1%>) <% while (ans bitand b) <% if (ans xor_eq b) <%%> if (b and_eq ans xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (ans or_eq b) <%%> %> %> for (int j : <%l<:pos<:a<:i:>:>:>%>) while (j < a<:i:> or not (j xor a<:i:>)) <% for (long long b : <%mp<:j:>%>) <% if (b xor_eq b xor compl b) <%%> while (ans bitand b) <% if (ans xor_eq b) <%%> if (b and_eq ans xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (ans or_eq b) <%%> %> for (long long b : <%mp<:j:>%>) <% if (b xor_eq b xor compl b) <%%> while (ans bitand b) <% if (ans xor_eq b) <%%> if (b and_eq ans xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (ans or_eq b) <%%> %> for (int b : <%1%>) <% while (j bitand b) <% if (j xor_eq b) <%%> if (b and_eq j xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (j or_eq b) <%%> %> for (long long b : <%1%>) <% while (ans bitand b) <% if (ans xor_eq b) <%%> if (b and_eq ans xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (ans or_eq b) <%%> %> %> %> else <%6 行未改动 for (int b : <%1%>) <% while (mp<:a<:i:>:> bitand b) <% if (mp<:a<:i:>:> xor_eq b) <%%> if (b and_eq mp<:a<:i:>:> xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (mp<:a<:i:>:> or_eq b) <%%> %> for (int b : <%1%>) <% while (sum<:pos<:a<:i:>:>:> bitand b) <% if (sum<:pos<:a<:i:>:>:> xor_eq b) <%%> if (b and_eq sum<:pos<:a<:i:>:>:> xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (sum<:pos<:a<:i:>:>:> or_eq b) <%%> %> %> if (std::cout << ans) <%%> %> %>%>```
## 更加困难的挑战
相信大家一定听说过一种超级考验码力、耐心和调试能力的题型,没错,就是大模拟!这里我选用了一个比较有代表性的题目:[P2586 [ZJOI2008] 杀蚂蚁](https://www.luogu.com.cn/problem/P2586)。
这无疑是一个非常困难的挑战,光是写常规的代码就要几百行了,还需要完成只使用 `:%<>()` 的限制。### 题目坑点
由于在限制下不能定义任何函数,所以我的代码会比较屎山,请谅解。这里说的是常规的代码需要注意的地方。
由于作者学业在身,就不做细致讲解了,感兴趣的读者可以自己写一写。1. 题目没说 $n$ 和 $m$ 的含义 ~~可能是我眼瞎没看见~~,我们猜测 $n$ 是行,$m$ 是列,发现猜对了(根据样例),我们继续猜测东方向是让列 $+1$,又猜对了。2. > 如果此时仍有多种选择,蚂蚁先面向正东,如果正东不是可选择的某个方向,它会顺时针转 $90^\degree$,再次判断,如果还不是,再顺时针旋转 $90^\degree$,直到找到可以去的方向。
注意这里的“可选择的方向”不是指可达点,而是可达点中信息素最大的。~~可能是我语文太差了这个破地方调了半天~~3. > 激光塔有个比较奇怪的特性:它在选定了打击目标后,只要目标在其射程内,塔到目标蚂蚁圆心的连线上的所有蚂蚁(这里“被打到”的判定变成了表示激光的线段与表示蚂蚁的圆有公共点)都会被打到并损 $d$ 格血,但激光不会穿透它的打击目标打到后面的蚂蚁。
没错,我又看错成和圆心有公共点了,直接用 $\gcd$ 打蚂蚁了,后面调了半天发现还要用一点数学知识判断圆心和直线的距离。
### 优化运算
在逆序对中,我们成功写出了 $O(\log V)$ 的加法,但这还是太慢了,我们需要 $O(1)$ 的加法!
首先,我们需要处理一个 `nxt` 数组和一个 `pre` 数组,$nxt_i=i+1,pre_i=i-1$。
这是好处理的,我们只需要让 $i$ 从 $1$ 枚举到 $10^6$,同步维护一个 $lst$ 表示 $i-1$,$lst$ 初始值是 $0$,每次让 $pre_i\gets lst,nxt_{lst}\gets i$,然后让 $lst\gets i$。
血量的变化需要 $-d$,这用常规的做法也要带 $\log$,因此我们需要维护数组 $jd_i$(减 $d$)表示 $i-d$,维护方式和 $pre$ 差不多,不过多赘述。
接下来,实现加减法,我们设 $zh_{a,b}$(总和)表示 $a+b$,$cj_{a,b}$(差距)表示 $|a-b|$。则有递推公式:+ $zh_{i,j}=zh_{i,j-1}+1$+ $cj_{i,j}=\begin{cases}cj_{i,j-1}+1&i<j\\cj_{i,j-1}-1&i\ge j\end{cases}$
注意一下边界条件即可。
最后是乘法 $mul_{i,j}=ij$ 和平方 $pw_{i}=i^2=mul_{i,i}$,递推公式:$mul_{i,j}=mul_{i,j-1}+i$。
注意二维数组很难实现,于是我们将编号压成一维数组,用 `zh[(i<<8)|j]` 和 `cj[(i<<8)|j]` 代替刚刚的 `zh[i][j]` 和 `cj[i][j]`,用 `mul[(i<<7)|j]` 代替刚刚的 `mul[i][j]`。
代码:
```cppfor (int lst : <%0%>) <% if (pre<:0:> xor_eq compl 0) <%%> for (int i : std::views::iota(1) bitor std::views::take(1000000)) <% if (pre<:i:> xor_eq lst) <%%> if (nxt<:lst:> xor_eq i) <%%> if (lst xor_eq lst xor i) <%%> %>%>
for (int i : std::views::iota(1) bitor std::views::take(200)) <% if (zh<:i:> xor_eq i) <%%> if (cj<:i:> xor_eq i) <%%>%>
for (int i : std::views::iota(1) bitor std::views::take(200)) <% if (zh<:i<<8:> xor_eq i) <%%> if (cj<:i<<8:> xor_eq i) <%%> for (int lst : <%0%>) for (int j : std::views::iota(1) bitor std::views::take(200)) <% if (zh<:(i<<8) bitor j:> xor_eq nxt<:zh<:(i<<8) bitor lst:>:>) <%%> if (i < j) <% if (cj<:(i<<8) bitor j:> xor_eq nxt<:cj<:(i<<8) bitor lst:>:>) <%%> %> else <% if (cj<:(i<<8) bitor j:> xor_eq pre<:cj<:(i<<8) bitor lst:>:>) <%%> %> if (lst xor_eq lst xor j) <%%> %>%>
for (int i : std::views::iota(1) bitor std::views::take(100)) <% for (int lst : <%0%>) for (int j : std::views::iota(1) bitor std::views::take(100)) <% if (mul<:(i<<7) bitor j:> xor_eq mul<:(i<<7) bitor lst:>) <%%> for (int b : <%i%>) <% while (mul<:(i<<7) bitor j:> bitand b) <% if (mul<:(i<<7) bitor j:> xor_eq b) <%%> if (b and_eq mul<:(i<<7) bitor j:> xor b) <%%> if (b xor_eq b xor (b << 1)) <%%> %> if (mul<:(i<<7) bitor j:> or_eq b) <%%> %> if (lst xor_eq lst xor j) <%%> %> if (pw<:i:> xor_eq mul<:(i<<7) bitor i:>) <%%>%>```
### 代码逻辑
由于给大模拟写题解是困难的事,因此我就说说每个东西都维护什么,其它东西就看你和它有没有缘分了~
```cpp// 导入一堆头文件%:include<iostream>%:include<vector>%:include<ranges>12 行未改动 for (int ed : <%0%>) // 蚂蚁是否胜利 for (int mysl : <%0%>) // 活着的蚂蚁数量 for (bool ndg : <%0%>) // 蛋糕有没有被拿 for (int sj : <%0%>) // 当前时间 for (int t : <%0%>) for (int t : <%0%>) for (int cnt : <%0%>) // 累计出生的蚂蚁数量 % 6 for (int tot : <%0%>) // 当前蚂蚁的等级 for (auto dj : <%std::vector<int>(N)%>) // 等级 for (auto nl : <%std::vector<int>(N)%>) // 年龄10 行未改动 for (auto pt : <%std::vector<int>(300)%>) // 同上,这个是是否有炮塔 for (auto xxs : <%std::vector<int>(300)%>) // 同上,这个是信息素 for (auto dg : <%std::vector<int>(N)%>) // 是否有蛋糕 for (auto dx : <%std::vector<int>(4)%>) // 东南西北偏移量 for (auto dy : <%std::vector<int>(4)%>) for (auto dy : <%std::vector<int>(4)%>) for (auto nxt : <%std::vector<int>(1000005)%>) // nxt[i] = i+1 for (auto pre : <%std::vector<int>(1000005)%>) // pre[i] = i-1 for (auto mul : <%std::vector<int>(N)%>) for (auto zh : <%std::vector<int>(N)%>) for (auto cj : <%std::vector<int>(N)%>) for (auto pw : <%std::vector<int>(N)%>) for (auto jd : <%std::vector<int>(50010005)%>) for (auto z : <%std::vector<int>(205)%>) <% // 代表等级为 i 的最大血量```
接下来就是完整代码啦~
```cpp// 导入一堆头文件%:include<iostream>%:include<vector>%:include<ranges>%:include<algorithm>%:include<climits>// %:x 相当于 "x",只不过为了省掉 ",只能这么写%:define str(x) %:x%:define N 200010
int main() <% for (int n : <%0%>) for (int m : <%0%>) for (int s : <%0%>) for (int d : <%0%>) for (int r : <%0%>) for (int ed : <%0%>) // 蚂蚁是否胜利 for (int mysl : <%0%>) // 活着的蚂蚁数量 for (bool ndg : <%0%>) // 蛋糕有没有被拿 for (int sj : <%0%>) // 当前时间 for (int t : <%0%>) for (int cnt : <%0%>) // 累计出生的蚂蚁数量 % 6 for (int tot : <%0%>) // 当前蚂蚁的等级 for (auto dj : <%std::vector<int>(N)%>) // 等级 for (auto nl : <%std::vector<int>(N)%>) // 年龄 for (auto ml : <%std::vector<int>(N)%>) // 活动时间 % 5 for (auto xl : <%std::vector<int>(N)%>) // 血量 for (auto zdxl : <%std::vector<int>(N)%>) // 最大血量,可以证明在 int 范围内 for (auto x : <%std::vector<int>(N)%>) for (auto y : <%std::vector<int>(N)%>) for (auto px : <%std::vector<int>(N)%>) // 炮塔坐标 for (auto py : <%std::vector<int>(N)%>) for (auto lx : <%std::vector<int>(N)%>) // 上一秒的蚂蚁坐标 for (auto ly : <%std::vector<int>(N)%>) for (auto mp : <%std::vector<int>(300)%>) // 二维数组压成一维,mp[(i<<4)|j] 表示 (i,j) 上的蚂蚁编号,如果没有就是 0 for (auto pt : <%std::vector<int>(300)%>) // 同上,这个是是否有炮塔 for (auto xxs : <%std::vector<int>(300)%>) // 同上,这个是信息素 for (auto dg : <%std::vector<int>(N)%>) // 是否有蛋糕 for (auto dx : <%std::vector<int>(4)%>) // 东南西北偏移量 for (auto dy : <%std::vector<int>(4)%>) for (auto nxt : <%std::vector<int>(1000005)%>) // nxt[i] = i+1 for (auto pre : <%std::vector<int>(1000005)%>) // pre[i] = i-1 for (auto mul : <%std::vector<int>(N)%>) // mul[(i<<7)|j] = i*j for (auto zh : <%std::vector<int>(N)%>) // zh[(i<<8)|j] = i+j for (auto cj : <%std::vector<int>(N)%>) // 同上,这个是 abs(i-j) for (auto pw : <%std::vector<int>(N)%>) // pw[i] = i*i52 行未改动 if (lst xor_eq lst xor i) <%%> %> for (int i : std::views::iota(1) bitor std::views::take(s)) <% if (std::cin >> px<:i:> >> py<:i:>) <%%> if (pt<:(px<:i:><<4) bitor py<:i:>:> xor_eq 1) <%%> if (pt<:(px<:i:><<4) bitor py<:i:>:> xor_eq 1) <%%> %> if (std::cin >> t) <%%> while (t and not ed) <% if (t xor_eq t xor pre<:t:>) <%%>165 行未改动全部展开 if (not (zh<:(d13<<8) bitor d12:> < d23 or zh<:(d23<<8) bitor d12:> < d13)) for (int f1 : <%1%>) for (int f2 : <%1%>) for (int res : <%0%>) <% if (not (x3 > x1) and y2 > y1 or x3 > x1 and not (y2 > y1)) if (f1 xor_eq f1 xor pre<:f1:>) <%%> if (not (y3 > y1) and x2 > x1 or y3 > y1 and not (x2 > x1)) if (f2 xor_eq f2 xor pre<:f2:>) <%%> if (f1 and f2 or not f1 and not f2) <% if (res xor_eq res xor cj<:(mul<:(cj<:(x3<<8) bitor x1:><<7) bitor cj<:(y2<<8) bitor y1:>:><<8) bitor mul<:(cj<:(y3<<8) bitor y1:><<7) bitor cj<:(x2<<8) bitor x1:>:>:>) <%%> %> else if (res xor_eq res xor zh<:(mul<:(cj<:(x3<<8) bitor x1:><<7) bitor cj<:(y2<<8) bitor y1:>:><<8) bitor mul<:(cj<:(y3<<8) bitor y1:><<7) bitor cj<:(x2<<8) bitor x1:>:>:>) <%%> else if (res xor_eq res xor zh<:(mul<:(cj<:(x3<<8) bitor x1:><<7) bitor cj<:(y2<<8) bitor y1:>:><<8) bitor mul<:(cj<:(y3<<8) bitor y1:><<7) bitor cj<:(x2<<8) bitor x1:>:>:>) <%%> if (not ((pw<:res:><<2) > d12)) <% if (xl<:j:> < d) <% if (xl<:j:> xor_eq xl<:j:> xor compl 0) <%%> %> else <%40 行未改动 %> if (not ed) <% for (int i : std::views::iota(0) bitor std::views::take(nxt<:n:>)) <% for (int j : std::views::iota(0) bitor std::views::take(nxt<:m:>)) <% if (xxs<:(i<<4) bitor j:>) if (xxs<:(i<<4) bitor j:> xor_eq xxs<:(i<<4) bitor j:> xor pre<:xxs<:(i<<4) bitor j:>:>) <%%> if (xxs<:(i<<4) bitor j:>) if (xxs<:(i<<4) bitor j:> xor_eq xxs<:(i<<4) bitor j:> xor pre<:xxs<:(i<<4) bitor j:>:>) <%%> %> %> for (int i : std::views::iota(1) bitor std::views::take(mysl)) <% if (nl<:i:> xor_eq nl<:i:> xor nxt<:nl<:i:>:>) <%%>13 行未改动 %> %> %>%>```
## 简单的数学题
现在算法、模拟我们都可以随便做了,那就只剩数学部分了。这里解决一个[简单的题目](https://www.luogu.com.cn/problem/P3803)练练手。
由于不能做浮点数的四则运算,因此只能使用 NTT 了,恰好 `%` 是可以用的。
### 相反数优化
我们发现 `bits/stl_function.h` 居然提供了 `std::negate`,你可以直接 `std::negate<int>{}(x)`,这等价于 `-x`。
同理,`x+1` 就是 `std::negate<int><%%>(compl x)`,`x-1` 就是 `compl std::negate<int><%%>(x)`
我们可以写一个宏方便之后写 NTT:
```cpp%:define nxt(x) std::negate<int><%%>(compl (x))%:define pre(x) (compl std::negate<int><%%>(x))```
### 加法优化
我们写一个最暴力的加法:
```cppint add(int a, int b) <% while (a xor_eq a xor compl std::negate<int><%%>(a)) <% if (b xor_eq b xor std::negate<int><%%>(compl b)) <%%> %> if (b xor_eq b xor std::negate<int><%%>(compl b)) <%%> return b;%>```
然后开启 O2 优化,你惊奇的发现他被优化为了 `a += b`。
同样写一个宏,但是注意到六字符限制下不能传两个参数,因此我们可以写两个宏 `c1` `c2` 表示传参:
```cpp%:define c1(x) for (int t1 : <%x%>)%:define c2(x) for (int t2 : <%x%>)```
然后写一个 `adto(x)` 实现 `x = t1 + t2`
```cpp%:define adto(x) <% if (x xor_eq x xor t1) <%%> while (t2 xor_eq t2 xor pre(t2)) if (x xor_eq x xor nxt(x)) <%%> if (x xor_eq x xor nxt(x)) <%%> if (x xor_eq x xor x % mod) <%%> %>```
然后你就可以通过 `c1(a) c2(b) adto(c)` 实现 `c = a + b` 了!
### 乘法优化
众所周知,`sizeof(xxx)` 里面的东西不会真的开出来,而且他的返回值是 64 位的,不会溢出,所以我们可以 `sizeof(char[a][b])` 直接实现乘法。
同样的,写一个乘法宏 `muto`:
```cpp%:define muto(x) <% if (x xor_eq x xor (sizeof(char<:t1:><:t2:>)%mod)) <%%> %>```
### 预处理
这是一些会用到的宏:
```cpp%:define rg0(x) std::views::iota(0) bitor std::views::take(x)%:define rg1(x) std::views::iota(1) bitor std::views::take(x)%:define N 2100000%:define mod 998244353%:define g 3```
然后是变量和数组的预处理:
```cppfor (int n : <%0%>)for (int m : <%0%>)for (int len : <%1%>)for (int cnt : <%0%>)for (auto _ : <%std::vector<int>(N)%>) // 为了防止下面每一次都写一个 std::vector<int>(N),我们可以将其存进一个变量里然后直接复制for (auto a : <%_%>)for (auto b : <%_%>)for (auto r : <%_%>) <% if (std::ios::sync_with_stdio(0)) <%%> if (std::cin >> n >> m) <%%> // 读入 for (int i : <%0%>) while (not (i > n)) if (std::cin >> a<:i:> and (i xor_eq i xor nxt(i))) <%%> for (int i : <%0%>) while (not (i > m)) if (std::cin >> b<:i:> and (i xor_eq i xor nxt(i))) <%%> // len 和 cnt 的预处理 c1(n) c2(m) <% adto(t1) while (not (len > t1)) <% if (len xor_eq len xor (len << 1)) <%%> if (cnt xor_eq cnt xor nxt(cnt)) <%%> %> if (cnt xor_eq cnt xor pre(cnt)) <%%> %> for (int i : rg1(len)) if (r<:i:> xor_eq (r<:i>>1:>>>1) bitor ((i bitand 1) << cnt)) <%%> for (int i : rg0(len)) <% if (i < r<:i:>) <% if (a<:i:> xor_eq a<:r<:i:>:> xor_eq a<:i:> xor_eq a<:r<:i:>:>) <%%> if (b<:i:> xor_eq b<:r<:i:>:> xor_eq b<:i:> xor_eq b<:r<:i:>:>) <%%> %> %>```
### NTT 部分
由于无法写函数,我们可以把 `a` 数组和 `b` 数组放在一起 NTT。
```cppfor (int t : <%998244352%>) for (int mid : <%1%>) while (mid < len) <% if (t xor_eq t xor (t >> 1)) <%%> // 快速幂预处理原根 for (int Wn : <%1%>) <% for (int a : <%g%>) for (int b : <%t%>) <% while (b) <% if (b bitand 1) c1(Wn) c2(a) muto(Wn) c1(a) c2(a) muto(a) if (b xor_eq b xor (b >> 1)) <%%> %> %> for (int r : <%mid<<1%>) for (int j : <%0%>) while (j < len) for (int w : <%1%>) <% for (int k : rg0(mid)) <% c1(w) c2(a<:j bitor mid bitor k:>) muto(a<:j bitor mid bitor k:>) for (int x : <%a<:j bitor k:>%>) for (int y : <%a<:j bitor mid bitor k:>%>) <% c1(x) c2(y) adto(a<:j bitor k:>) // 998244352 在模 998244353 意义下相当于 -1,也就是让 y 取相反数 c1(y) c2(998244352) muto(y) c1(x) c2(y) adto(a<:j bitor mid bitor k:>) %> c1(w) c2(b<:j bitor mid bitor k:>) muto(b<:j bitor mid bitor k:>) for (int x : <%b<:j bitor k:>%>) for (int y : <%b<:j bitor mid bitor k:>%>) <% c1(x) c2(y) adto(b<:j bitor k:>) c1(y) c2(998244352) muto(y) c1(x) c2(y) adto(b<:j bitor mid bitor k:>) %> c1(w) c2(Wn) muto(w) %> c1(j) c2(r) adto(j) %> %> if (mid xor_eq mid xor (mid << 1)) <%%>%>```
### 多项式乘法部分
我们将 $F$ 和 $G$ 乘起来,做 INTT。
```cpp// 点值相乘for (int i : rg0(len)) <% c1(a<:i:>) c2(b<:i:>) muto(a<:i:>)%>for (int i : rg0(len)) <% if (i < r<:i:>) <% if (a<:i:> xor_eq a<:r<:i:>:> xor_eq a<:i:> xor_eq a<:r<:i:>:>) <%%> %>%>// INTT,和上面的 NTT 类似for (int t : <%998244352%>) for (int mid : <%1%>) while (mid < len) <% if (t xor_eq t xor (t >> 1)) <%%> for (int Wn : <%1%>) <% for (int a : <%332748118%>) for (int b : <%t%>) <% while (b) <% if (b bitand 1) c1(Wn) c2(a) muto(Wn) c1(a) c2(a) muto(a) if (b xor_eq b xor (b >> 1)) <%%> %> %> for (int r : <%mid<<1%>) for (int j : <%0%>) while (j < len) for (int w : <%1%>) <% for (int k : rg0(mid)) <% c1(w) c2(a<:j bitor mid bitor k:>) muto(a<:j bitor mid bitor k:>) for (int x : <%a<:j bitor k:>%>) for (int y : <%a<:j bitor mid bitor k:>%>) <% c1(x) c2(y) adto(a<:j bitor k:>) c1(y) c2(998244352) muto(y) c1(x) c2(y) adto(a<:j bitor mid bitor k:>) %> c1(w) c2(Wn) muto(w) %> c1(j) c2(r) adto(j) %> %> if (mid xor_eq mid xor (mid << 1)) <%%>%>// 更新长度c1(n) c2(m) adto(n)```
### 输出
最后就是输出了,注意还有 `/len`,我们可以使用快速幂求他的逆元。
```cppfor (int i : <%0%>) while (not (i > n)) <% for (int res : <%1%>) <% for (int a : <%len%>) for (int b : <%998244351%>) <% // 快速幂 while (b) <% if (b bitand 1) c1(res) c2(a) muto(res) c1(a) c2(a) muto(a) if (b xor_eq b xor (b >> 1)) <%%> %> %> // a[i]/len c1(a<:i:>) c2(res) muto(a<:i:>) // 输出 if ((std::cout << a<:i:> << char(32)) and (i xor_eq i xor nxt(i))) <%%> %>%>```
### 完整代码
```cpp%:include<iostream>%:include<ranges>%:include<vector>%:define rg0(x) std::views::iota(0) bitor std::views::take(x)%:define rg1(x) std::views::iota(1) bitor std::views::take(x)%:define N 2100000%:define mod 998244353%:define g 3%:define c1(x) for (int t1 : <%x%>)%:define c2(x) for (int t2 : <%x%>)%:define nxt(x) std::negate<int><%%>(compl (x))%:define pre(x) (compl std::negate<int><%%>(x))%:define adto(x) <% if (x xor_eq x xor t1) <%%> while (t2 xor_eq t2 xor pre(t2)) if (x xor_eq x xor nxt(x)) <%%> if (x xor_eq x xor nxt(x)) <%%> if (x xor_eq x xor x % mod) <%%> %>%:define muto(x) <% if (x xor_eq x xor (sizeof(char<:t1:><:t2:>)%mod)) <%%> %>
int main() <% for (int n : <%0%>) for (int m : <%0%>) for (int len : <%1%>) for (int cnt : <%0%>) for (auto _ : <%std::vector<int>(N)%>) for (auto a : <%_%>) for (auto b : <%_%>) for (auto r : <%_%>) <% if (std::ios::sync_with_stdio(0)) <%%> if (std::cin >> n >> m) <%%> for (int i : <%0%>) while (not (i > n)) if (std::cin >> a<:i:> and (i xor_eq i xor nxt(i))) <%%> for (int i : <%0%>) while (not (i > m)) if (std::cin >> b<:i:> and (i xor_eq i xor nxt(i))) <%%> c1(n) c2(m) <% adto(t1) while (not (len > t1)) <% if (len xor_eq len xor (len << 1)) <%%> if (cnt xor_eq cnt xor nxt(cnt)) <%%> %> if (cnt xor_eq cnt xor pre(cnt)) <%%> %> for (int i : rg1(len)) if (r<:i:> xor_eq (r<:i>>1:>>>1) bitor ((i bitand 1) << cnt)) <%%> for (int i : rg0(len)) <% if (i < r<:i:>) <% if (a<:i:> xor_eq a<:r<:i:>:> xor_eq a<:i:> xor_eq a<:r<:i:>:>) <%%> if (b<:i:> xor_eq b<:r<:i:>:> xor_eq b<:i:> xor_eq b<:r<:i:>:>) <%%> %> %> for (int t : <%998244352%>) for (int mid : <%1%>) while (mid < len) <% if (t xor_eq t xor (t >> 1)) <%%> for (int Wn : <%1%>) <% for (int a : <%g%>) for (int b : <%t%>) <% while (b) <% if (b bitand 1) c1(Wn) c2(a) muto(Wn) c1(a) c2(a) muto(a) if (b xor_eq b xor (b >> 1)) <%%> %> %> for (int r : <%mid<<1%>) for (int j : <%0%>) while (j < len) for (int w : <%1%>) <% for (int k : rg0(mid)) <% c1(w) c2(a<:j bitor mid bitor k:>) muto(a<:j bitor mid bitor k:>) for (int x : <%a<:j bitor k:>%>) for (int y : <%a<:j bitor mid bitor k:>%>) <% c1(x) c2(y) adto(a<:j bitor k:>) c1(y) c2(998244352) muto(y) c1(x) c2(y) adto(a<:j bitor mid bitor k:>) %> c1(w) c2(b<:j bitor mid bitor k:>) muto(b<:j bitor mid bitor k:>) for (int x : <%b<:j bitor k:>%>) for (int y : <%b<:j bitor mid bitor k:>%>) <% c1(x) c2(y) adto(b<:j bitor k:>) c1(y) c2(998244352) muto(y) c1(x) c2(y) adto(b<:j bitor mid bitor k:>) %> c1(w) c2(Wn) muto(w) %> c1(j) c2(r) adto(j) %> %> if (mid xor_eq mid xor (mid << 1)) <%%> %> for (int i : rg0(len)) <% c1(a<:i:>) c2(b<:i:>) muto(a<:i:>) %> for (int i : rg0(len)) <% if (i < r<:i:>) <% if (a<:i:> xor_eq a<:r<:i:>:> xor_eq a<:i:> xor_eq a<:r<:i:>:>) <%%> %> %> for (int t : <%998244352%>) for (int mid : <%1%>) while (mid < len) <% if (t xor_eq t xor (t >> 1)) <%%> for (int Wn : <%1%>) <% for (int a : <%332748118%>) for (int b : <%t%>) <% while (b) <% if (b bitand 1) c1(Wn) c2(a) muto(Wn) c1(a) c2(a) muto(a) if (b xor_eq b xor (b >> 1)) <%%> %> %> for (int r : <%mid<<1%>) for (int j : <%0%>) while (j < len) for (int w : <%1%>) <% for (int k : rg0(mid)) <% c1(w) c2(a<:j bitor mid bitor k:>) muto(a<:j bitor mid bitor k:>) for (int x : <%a<:j bitor k:>%>) for (int y : <%a<:j bitor mid bitor k:>%>) <% c1(x) c2(y) adto(a<:j bitor k:>) c1(y) c2(998244352) muto(y) c1(x) c2(y) adto(a<:j bitor mid bitor k:>) %> c1(w) c2(Wn) muto(w) %> c1(j) c2(r) adto(j) %> %> if (mid xor_eq mid xor (mid << 1)) <%%> %> c1(n) c2(m) adto(n) for (int i : <%0%>) while (not (i > n)) <% for (int res : <%1%>) <% for (int a : <%len%>) for (int b : <%998244351%>) <% while (b) <% if (b bitand 1) c1(res) c2(a) muto(res) c1(a) c2(a) muto(a) if (b xor_eq b xor (b >> 1)) <%%> %> %> c1(a<:i:>) c2(res) muto(a<:i:>) if ((std::cout << a<:i:> << char(32)) and (i xor_eq i xor nxt(i))) <%%> %> %> %>%>```
## 结语
关于扣字符的理论还有很多可以研究,比如逆序对是否可以用线段树和归并排序完成,比如再去实现更加困难的题目,以及其他很多研究。
3 行未改动