正文
增加 366 行删除 20 行
123456789246 行未改动全部展开256257258259260261262263264135 行未改动全部展开40040140240340440540640740813 行未改动42242342442542642742842943043143243343443543643743843944044144244344444511 行未改动4574584594604614624634644654664674686 行未改动4754764774784794804814824834844854864874884894904914924936 行未改动500501502503504505506507508104 行未改动全部展开61361461561661761861962062110 行未改动6326336346356366376386396404 行未改动64564664764864965065165265311 行未改动66566666766866967067167267310 行未改动68468568668768868969069169254 行未改动747748749750751752753754755165 行未改动全部展开92192292392492592692792892940 行未改动97097197297397497597697797815 行未改动994995996997998999100010013 行未改动(本人平时并不擅长 C++,很多内容和真实语法有偏差,还请见谅。另外,本文中的符号指不能在 C++ 语言中作为变量名的可见字符。作为注释的 `/` 不被算作使用符号。)## 起因受到 [机房大佬的文章](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) <%%>%>%>```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 (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 (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 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 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 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 (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 (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) <%%>%>%>```## 更加困难的挑战104 行未改动全部展开for (int ed : <%0%>) // 蚂蚁是否胜利for (int mysl : <%0%>) // 活着的蚂蚁数量for (bool ndg : <%0%>) // 蛋糕有没有被拿for (int sj : <%0%>) // 当前时间for (int t : <%0%>)for (int cnt : <%0%>) // 累计出生的蚂蚁数量 % 6for (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 nxt : <%std::vector<int>(1000005)%>) // nxt[i] = i+1for (auto pre : <%std::vector<int>(1000005)%>) // pre[i] = i-1for (auto mul : <%std::vector<int>(N)%>)for (auto zh : <%std::vector<int>(N)%>)4 行未改动```接下来就是完整代码啦~```cpp// 导入一堆头文件%:include<iostream>%:include<vector>%:include<ranges>%:include<algorithm>11 行未改动for (int ed : <%0%>) // 蚂蚁是否胜利for (int mysl : <%0%>) // 活着的蚂蚁数量for (bool ndg : <%0%>) // 蛋糕有没有被拿for (int sj : <%0%>) // 当前时间for (int t : <%0%>)for (int cnt : <%0%>) // 累计出生的蚂蚁数量 % 6for (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 nxt : <%std::vector<int>(1000005)%>) // nxt[i] = i+1for (auto pre : <%std::vector<int>(1000005)%>) // pre[i] = i-1for (auto mul : <%std::vector<int>(N)%>) // mul[(i<<7)|j] = i*jfor (auto zh : <%std::vector<int>(N)%>) // zh[(i<<8)|j] = i+j54 行未改动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 (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:>:>:>) <%%>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:>:>) <%%>%>%>for (int i : std::views::iota(1) bitor std::views::take(mysl)) <%if (nl<:i:> xor_eq nl<:i:> xor nxt<:nl<:i:>:>) <%%>15 行未改动%>%>```## 结语关于扣字符的理论还有很多可以研究,比如逆序对是否可以用线段树和归并排序完成,比如再去实现更加困难的题目,以及其他很多研究。3 行未改动123456789246 行未改动全部展开256257258259260261262263264135 行未改动全部展开40040140240340440540640740813 行未改动42242342442542642742842943043143243343443543643743843944044144244344444511 行未改动4574584594604614624634644654664674686 行未改动4754764774784794804814824834844854864874884894904914924936 行未改动500501502503504505506507508104 行未改动全部展开61361461561661761861962062110 行未改动6326336346356366376386396404 行未改动64564664764864965065165265365411 行未改动66666766866967067167267367410 行未改动68568668768868969069169269354 行未改动748749750751752753754755756165 行未改动全部展开92292392492592692792892993040 行未改动97197297397497597697797897915 行未改动9959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613473 行未改动(本人平时并不擅长 C++,很多内容和真实语法有偏差,还请见谅。另外,本文中的符号指不能在 C++ 语言中作为变量名的可见字符。作为注释的 `/` 不被算作使用符号。)## 起因受到[机房大佬的文章](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) <%%>%>%>```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 (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 (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 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 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 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 (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 (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) <%%>%>%>```## 更加困难的挑战104 行未改动全部展开for (int ed : <%0%>) // 蚂蚁是否胜利for (int mysl : <%0%>) // 活着的蚂蚁数量for (bool ndg : <%0%>) // 蛋糕有没有被拿for (int sj : <%0%>) // 当前时间for (int t : <%0%>)for (int cnt : <%0%>) // 累计出生的蚂蚁数量 % 6for (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 nxt : <%std::vector<int>(1000005)%>) // nxt[i] = i+1for (auto pre : <%std::vector<int>(1000005)%>) // pre[i] = i-1for (auto mul : <%std::vector<int>(N)%>)for (auto zh : <%std::vector<int>(N)%>)4 行未改动```接下来就是完整代码啦~```cpp// 导入一堆头文件%:include<iostream>%:include<vector>%:include<ranges>%:include<algorithm>11 行未改动for (int ed : <%0%>) // 蚂蚁是否胜利for (int mysl : <%0%>) // 活着的蚂蚁数量for (bool ndg : <%0%>) // 蛋糕有没有被拿for (int sj : <%0%>) // 当前时间for (int t : <%0%>)for (int cnt : <%0%>) // 累计出生的蚂蚁数量 % 6for (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 nxt : <%std::vector<int>(1000005)%>) // nxt[i] = i+1for (auto pre : <%std::vector<int>(1000005)%>) // pre[i] = i-1for (auto mul : <%std::vector<int>(N)%>) // mul[(i<<7)|j] = i*jfor (auto zh : <%std::vector<int>(N)%>) // zh[(i<<8)|j] = i+j54 行未改动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 (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:>:>:>) <%%>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:>:>) <%%>%>%>for (int i : std::views::iota(1) bitor std::views::take(mysl)) <%if (nl<:i:> xor_eq nl<:i:> xor nxt<:nl<:i:>:>) <%%>15 行未改动%>%>```## 简单的数学题现在算法、模拟我们都可以随便做了,那就只剩数学部分了。这里解决一个[简单的题目](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]/lenc1(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 行未改动