正文
增加 19 行删除 20 行
44 行未改动4546474849505152535412 行未改动676869707172737475766 行未改动83848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251265 行未改动13213313413513613713813914087 行未改动22822923023123223323423523619 行未改动25625725825926026126226326426526626726826927027114 行未改动2862872882892902912922932942952962972982993003013023033043053063073083098 行未改动44 行未改动这个问题很好解决:我们注意到 $a \equiv b \pmod 2$,以及刚才的 $c \ge a \ge b$,写一个好一点的压位就可以过掉了。我的压位是 AI 写的,因为没人想写这个。```cpp#include <bits/stdc++.h>#include <cstring>using namespace std;#define rep(i, a, b) for (int i = (a) ; i <= (b) ; i ++)12 行未改动void Receive(int source);int GetInt(int source);long long GetLL(int source);// ==================== 100% 标准安全的位重解释 ====================inline ll bit_cast_to_ll(ull val) {ll res;std::memcpy(&res, &val, sizeof(ll));return res;6 行未改动}// ==================== 奇偶相同且大于等于约束下的数学 ====================// a >= b 且同奇偶情况下,有序对 (b <= a) 在 [0, a-1] 中的总数量inline ull g(ull a) {ull k = a / 2;if (a % 2 == 0) {return k * (k + 1);} else {return (k + 1) * (k + 1);}}// 计算 c 以下,所有同奇偶且 c >= a >= b 的合法三元组总数ull sum_pairs(ull n) {ull m = n / 2;ull a = m + 1, b = m + 2, c;if (n % 2 == 0) {c = 4 * m + 3;} else {c = 4 * m + 9;}// 安全整除,防止乘法中间步骤溢出if (a % 2 == 0) a /= 2; else b /= 2;if (a % 3 == 0) a /= 3; else if (b % 3 == 0) b /= 3; else c /= 3;return a * b * c;}// 包含 c==0 边界的安全前缀和获取inline ull get_sum_pairs(ull c) {if (c == 0) return 0;return sum_pairs(c - 1);}const ull Threshold_2 = 1024000256000016ULL;// ==================== 核心接口实现 ====================// 压缩:将 (a, b, c, d) 压成 signed long long (ll)ll encode(int a, int b, int c, int d) {ull code = 0;if (c == 0) {ull pair_rank = g(a) + b / 2;5 行未改动return bit_cast_to_ll(code);}// 解压:将 signed long long (ll) 还原为 (a, b, c, d)std::tuple<int, int, int, int> decode(ll S) {ull code = bit_cast_to_ull(S);if (code < Threshold_2) {87 行未改动rc = tmp - now;}if (now < 0) {// 当前字符已不合法,不能计入 ra;ra 和 rc 沿用 i-1 处记录的前缀值hans = max (hans, tot - 1);flg = 1, now = 0, tot = 0;}}19 行未改动hans = max (hs, hans);np = nxt;}}rans = t1, rcnt = t2, hans = max (t3, hans); lp = t4;}else rans = 0, rcnt = 0;int nans, ncnt;// 先继承右侧传递过来的合法答案hans = max(hans, rans);per (i, id * L + L - 1, id * L) {char ch = GetCharAt (i);14 行未改动else nans = ra, ncnt = rc;if (hans < nans) hans = 0;// 确定向左传递的 lp 指针和 hans 状态int send_lp = lp;if (hans > 2 * L && (id != 0)) {send_lp = id; // 本地超长时,更新回传链表头为当前 id}// 1. 严格优先发送给相邻左节点的链条包 (Send to id - 1)if (id != 0) {int real_hans = (hans > 2 * L) ? 0 : hans;ll S = encode (nans, ncnt, real_hans, send_lp);PutLL (id - 1, S);Send (id - 1);}// 2. 随后发送给 Node 0 的直连包 (Send to 0)if (hans > 2 * L && (id != 0)) {PutInt (0, encodep(hans, lp));Send (0);}8 行未改动44 行未改动45464748495051525312 行未改动6667686970717273746 行未改动818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181195 行未改动12512612712812913013113287 行未改动22022122222322422522622719 行未改动24724824925025125225325425525625725825926026114 行未改动2762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073088 行未改动44 行未改动这个问题很好解决:我们注意到 $a \equiv b \pmod 2$,以及刚才的 $c \ge a \ge b$,写一个好一点的压位就可以过掉了。我的压位是 AI 写的,因为没人想写这个。```cpp#include <bits/stdc++.h>#include <cstring>using namespace std;#define rep(i, a, b) for (int i = (a) ; i <= (b) ; i ++)12 行未改动void Receive(int source);int GetInt(int source);long long GetLL(int source);// ==================== 标准安全的位重解释 ====================inline ll bit_cast_to_ll(ull val) {ll res;std::memcpy(&res, &val, sizeof(ll));return res;6 行未改动}// ==================== 奇偶相同且大于等于约束下的数学 ====================inline ull g(ull a) {ull k = a / 2;if (a % 2 == 0) {return k * (k + 1);} else {return (k + 1) * (k + 1);}}ull sum_pairs(ull n) {ull m = n / 2;ull a = m + 1, b = m + 2, c;if (n % 2 == 0) {c = 4 * m + 3;} else {c = 4 * m + 9;}if (a % 2 == 0) a /= 2; else b /= 2;if (a % 3 == 0) a /= 3; else if (b % 3 == 0) b /= 3; else c /= 3;return a * b * c;}inline ull get_sum_pairs(ull c) {if (c == 0) return 0;return sum_pairs(c - 1);}const ull Threshold_2 = 1024000256000016ULL;// ==================== 核心接口实现 ====================ll encode(int a, int b, int c, int d) {ull code = 0;if (c == 0) {ull pair_rank = g(a) + b / 2;5 行未改动return bit_cast_to_ll(code);}std::tuple<int, int, int, int> decode(ll S) {ull code = bit_cast_to_ull(S);if (code < Threshold_2) {87 行未改动rc = tmp - now;}if (now < 0) {hans = max (hans, tot - 1);flg = 1, now = 0, tot = 0;}}19 行未改动hans = max (hs, hans);np = nxt;}}rans = t1, rcnt = t2, hans = max (t3, hans); lp = t4;}else rans = 0, rcnt = 0;int nans, ncnt;hans = max(hans, rans);per (i, id * L + L - 1, id * L) {char ch = GetCharAt (i);14 行未改动else nans = ra, ncnt = rc;if (hans < nans) hans = 0;// 决定 real_hans 与是否需要直连包int real_hans = hans;int send_lp = lp;bool need_direct = false;// 1. 只有当 ncnt == 0 且 nans >= hans 时,nans 才是已完全匹配的更佳解,可以丢弃 hansif (ncnt == 0 && nans >= hans) {real_hans = 0;}// 2. 只有极端情况下 hans > 3000000 且无法被 nans 覆盖时,才走旁路直连else if (hans > 3000000 && (id != 0)) {need_direct = true;send_lp = id;real_hans = 0;}// 严格优先发送给相邻左节点的链条包 (Send to id - 1)if (id != 0) {ll S = encode (nans, ncnt, real_hans, send_lp);PutLL (id - 1, S);Send (id - 1);}// 随后发送给 Node 0 的直连包 (Send to 0)if (need_direct) {PutInt (0, encodep(hans, lp));Send (0);}8 行未改动