跳到正文

个人记录

FCOI 19 总结 & 题解

原文
文章信息与版本

文章信息

收录
2026年7月27日
更新
2026年8月12日
篇幅
约 42 分钟

版本

  1. v3当前
  2. v2
  3. v1

比赛链接:https://www.luogu.com.cn/contest/325691

统计信息

本场比赛共 6666 人报名,4848 位有效参加选手(#48 及以前),平均分为 3365.6253365.625 分,众数为 42004200 分,题目统计信息如下:

题目ABCDEFGHIJ
通过率87.5%87.5\%77.1%77.1\%77.1%77.1\%64.2%64.2\%45.8%45.8\%10.4%10.4\%4.2%4.2\%4.2%4.2\%2.1%2.1\%85.4%85.4\%
得分率87.5%87.5\%77.1%77.1\%77.1%77.1\%66.7%66.7\%45.8%45.8\%14.0%14.0\%8.4%8.4\%6.7%6.7\%6.0%6.0\%85.4%85.4\%
  • 通过率 = 通过人数 / 比赛有效参加人数 * 100%
  • 得分率 = 所有有效参加选手在这道题的总分 / 该题满分 / 比赛有效参加人数 * 100%

奖励

奖励细则详见比赛公告。

碳基生物守卫者奖

以下是以人类参赛的选手榜单:

榜单
排名用户得分罚时奖励
1sheepchat93605411 CNY
2unac_delighted6400139 CNY
3XiaMi07036340187 CNY
4H_ell_o6260186 CNY
5Rugged_Old_Trench5400154 CNY
6Force_Yield_Wonder4800112 CNY
7dhz1145144760111 CNY
8liyuncheng467081 CNY
9zhouzhile444081 CNY
10jkxyh4440101 CNY
11the_fish444014-
12sun_and_moon42005
DDSB(OrangeName)42005
CQOIer42005
LongPlace42005
tanghaobo81742005
wsw201342005
18LiYuhan092942006
19lisunli2011420081 CNY
20yanghaobo0421420010-
21zhuyanxi31004
22zhongyouran310050.25 CNY
hugo201531005
ABC04171131005
Chandler_d31005
llh1336825001631005
27zyq001131006-
28lizishuo330006-
29ljn12366630007
30emo_129926004
31lzm126006
32lirongxuan110221004
33HYX1898383228921004
34fjc_121005
35wslyl20006
36_G__12801
37my1181706001
liyunlu6001
zhangshirui6001
40_sandspiel_club_fun_5001
zhao_xinyu5001
42myckris1000
maozichen1000
CuteC121000
tjx1145141000
46zhangjinxaun-8401
47wonaiWzhuoS-12801

让我们为这些碳基生物的守护者们祝贺!

AI 协同选手奖

排名用户得分罚时奖励
1GongYicheng8880275 CNY
2player_1_Z458062 CNY
3ムラサメ330040.72 CNY
4ChatSheep-9103-

让我们致敬这一科技奇点!

AI 一败涂地奖

让我们恭喜人类选手的第一名以 580580 分的优势战胜 AI!看样子人类更胜一筹!(但是显然并非)故让我们恭喜以下选手获得此奖项!

用户奖励
sheepchat8 CNY

对于 FishC 上的鱼币奖励,请查看对应的总结帖。

感谢选手们的支持与参与!

题解

A - 站台

输出 S+C=n2+4nS+C=n^2+4n 即可。

Python
x=int(input());print(x*(x+4))

B - 列车

模拟即可。

Python
c=(lambda l:l[0]*60+l[1]*40)(list(map(int,input().split('+'))));print("Invalid" if c == 0 else "Bullet" if c <= 179 else "Blitz" if c <= 479 else "Rapid" if c <= 1499 else "Classical")
何为彩蛋?

数据点恰好有一个 15+0 的输入,单独对于那个输入输出 UltraBullet 即可。

C - 回响

模拟即可。

Python
n=int(input());s=input();t=input();print("\n".join([":"*(i+2)+s for i in range(n, 0, -1)]+[t]+[":"*(i+3) for i in range(n)]));

D - 片段

考虑贡献法,对于第 ii 个位置,包含他的有 i(n−i+1)i(n-i+1) 个区间(左右端点),所以输出 ∑i=1ni(n−i+1)ai\sum_{i=1}^{n} i(n-i+1)a_i 即可。

Python
n=int(input());print((lambda l:sum([(i+1)*(n-i)*l[i] for i in range(n)]))(list(map(int,input().split()))))

E - 盒子

每一个盒子的重要度之和显然为每一个盒子被多少个盒子嵌套,注意到 Ai<iA_i<i,直接用一个循环从 11 扫到 nn 并维护嵌套深度序列 depdep,对于每一个元素执行 depi=depai+1dep_i = dep_{a_i}+1 即可,随后输出 ∑i=1ndepi\sum_{i=1}^{n} dep_i。

Python
n=int(input());dep=[0];[dep.append(dep[i-1]+1) for i in map(int, input().split())];print(sum(dep)+n)

F - 让步

我们注意到,如果没有“让步”规则,那么只需要使用“打擂台“的方式就能获得最值。而添加“让步”规则后,遇到的最大问题就是一堆石子在经过过多次比较之后,会导致其值减少,以至于无法正确给出比较的结果。

因此,规避这个问题的最好办法,就是每一次我们都只拿出先前比较次数相同的元素进行比较。对于比较后的结果较劣的,直接扔出不考虑即可。

这个算法进行到最后,我们难免会有剩余,即可能存在一个元素,使得其比较次数比其他元素的比较次数都少一个,以至于无法进行下一步。

因此问题的关键在于面临一个比较次数比较少的元素 xx 和一个比较次数恰好比这个元素大一的元素 yy 时如何给出询问。

考虑直接询问 ? y x 会发生的结果,发现如果 wx>wyw_x> w_y,xx 将会胜利,比较次数增加 1。而当 wx=wyw_x=w_y 的时候,由于 xx 的比较次数比较少(被减的次数比较少),所以原本应该是 px<pyp_x<p_y,那么 yy 就可以胜利。如果 wx<wyw_x < w_y,那么 yy 显然也可以胜利。

这样,观察上述流程后的比较次数,会发现只有两种情况:

  • 所有石子比较次数相同。
  • 存在一个石子,他的比较次数比其他所有石子大一。

容易发现,对于第二种情况,只需要将其他所有比较次数相同的石子两两比较,即可转化为初始的子问题。

所以说我们的逻辑就是不断选比较次数最少元素和比较次数次少的元素去比较即可。

因此题目得解,时间复杂度 O(nlog⁡n)O(n \log n) 或者 O(n)O(n)。

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
	int tc;
	cin >> tc;
	while (tc--) {
		set<pair<int, int>> s;
		int n;
		cin >> n;
		for (int i = 1; i <= n; ++i) s.insert({0, i});
		while (s.size() >= 2) {
			pair<int, int> x = *(s.begin()), y = *(next(s.begin())); 
			s.erase(x); s.erase(y);
			if (x.first != y.first) swap(x, y);
			cout << "? " << x.second << " " << y.second << endl;
			int res;
			cin >> res;
			if (res == x.second) s.insert({x.first + 1, res});
			else s.insert({y.first + 1, res});
		}
		cout << "! " << (s.begin() -> second) << "\n";
		string stat; cin >> stat;
	}
}

Bonus

实现可以只使用一个队列实现,初始队列元素为 1∼n1\sim n,每一次选出前两个元素 x,yx,y,若 x>yx>y 交换两个元素,然后把胜利的元素塞在队列末尾即可。这个逻辑和上文阐述的逻辑是一样的,留给读者自证,代码很短就不放了。

G - 回路

注意到当网格存在长度至少为 3 的空地连续段那么答案一定为 2,因此考虑把两个部分拆开做。

首先考虑不存在长度至少为 3 的空地连续段,这时候就可以使用动态规划求解,令 dpi,0/1/2dp_{i,0/1/2} 表示目前第 ii 个格子,连续段长度为 0/1/20/1/2 的方案数和空地数量总和。dp 转移显然。

再考虑存在长度至少为 3 的空地连续段,可以发现,我们可以通过之前的 dp 容斥计算出这样的网格数量,即 2n2^n 减去 dpi,0/1/2dp_{i,0/1/2} 的方案数量。

于是现在的问题在于如何快速求解 dpn,0/1/2dp_{n,0/1/2},不难想到使用矩阵快速幂优化,具体的,不妨设状态向量 V=[C0C1C2S0S1S2]V=\begin{bmatrix} C_0 \\ C_1 \\ C_2 \\ S_0 \\ S_1 \\ S_2 \end{bmatrix},其中 CiC_i 表示末尾有 ii 个空地的方案数,SiS_i 表示末尾有 ii 个空地的情况下,所有方案的空地数量总和,那么不难构造出转移矩阵:

[C0C1C2S0S1S2]×[111000100000010000000111100100010010]=[C0′C1′C2′S0′S1′S2′] \begin{bmatrix} C_0 \\ C_1 \\ C_2 \\ S_0 \\ S_1 \\ S_2 \end{bmatrix} \times \begin{bmatrix} 1 & 1 & 1 & 0 & 0 & 0 \\ 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 1 & 1 \\ 1 & 0 & 0 & 1 & 0 & 0 \\ 0 & 1 & 0 & 0 & 1 & 0\end{bmatrix} = \begin{bmatrix} C_0' \\ C_1' \\ C_2' \\ S_0' \\ S_1' \\ S_2' \end{bmatrix}

初始向量是 C0=1C_0=1。

输入的数字是十进制的,因此使用十进制快速幂即可,没有必要转换为二进制再跑矩阵快速幂。

如果要优化的话,我们考虑到这个转移矩阵是一个非常典型的分块矩阵,所以我们令 A=[111100010]A=\begin{bmatrix} 1 & 1 & 1 \\ 1 & 0 & 0 \\ 0 & 1 & 0\end{bmatrix},B=[000100010]B=\begin{bmatrix} 0 & 0 & 0 \\ 1 & 0 & 0 \\ 0 & 1 & 0\end{bmatrix},那么实际上我们计算的就是:

[A0BA]n\begin{bmatrix} A & 0 \\ B & A \end{bmatrix}^n

这样一来,就会起到非常显著的优化。

Bonus

注意到 [A0BA]x\begin{bmatrix} A & 0 \\ B & A \end{bmatrix}^x 在有限域下显然具有周期,本地随便写个什么把周期搞出来即可。

显然周期不会超过 p4=1036p^4=10^{36},刚好在 __int128 范围内,这样就能做到非常好的优化,时间复杂度为 O(n+B3log⁡p)O(n + B^3 \log p)。

H - 留存

考虑这样一个技巧:

  • 选择 mm 个模数 P1,P2,…,PmP_1,P_2,\dots,P_m,对于每一个 ii,用 dp 求解出答案对 PiP_i 取模的答案。

如果 Pi≤255P_i\le 255,那么我们 dp 就可以只使用 8-bit 的整数类型跑,从而节省大量空间。

如果在上述条件下,m≥10m\ge 10 且模数选择恰当,我们就可以使用 CRT 还原出原答案了。

此时能过 n=21n=21。

因此我们把问题的核心聚集于 dp 本身的优化。

注意到排列计数的 dp 一定是从 popcount 低的转移到 popcount 高的,于是用滚动数组优化,具体的,我们假设当前求解的是所有 popcount 为 ii 的转移,那么此时 dpi&1,jdp_{i\&1,j} 就应该是“在 popcount 为 ii 的所有数字中,排名为 jj 的集合状态的方案数”。

这样,问题的核心在与快速求解建立排名与实际二进制数的双射。

如果你用组合数大力算,你能过 sub4,考虑优化。

首先从排名到二进制是简单的,考虑我们什么使用用这个信息,那当然是我们在内部转移时枚举排名然后求解其状态,但是呢,我们可以使用 k 元子集枚举算法(Gosper’s Hack),每一步都从一个状态转移到恰好比这个状态排名大 1 的状态。这样这个部分就完美解决了。

现在的问题核心在与如何从一个状态求解其排名。

我们考虑使用折半技巧,将一个状态分为高位部和低位部,可以发现,低位部的排名求解不依赖于高位部的具体数值,而高位部的排名求解只依赖于低位部的集合大小。

因此现在就可以折半了,用 rnkhigh(F,c)rnk_{high}(F,c) 表示高位部为 FF,低位部集合大小为 cc,对排名的贡献,rnklow(F)rnk_{low}(F) 就表示低位部为 FF 时单独的贡献即可。这样直接预处理,所占用的空间是可忽略不计的。

如果还有点卡常,就在转移的时候高低位分开转,可以减少一定条件判断。

这样,我们的空间就被优化到了 2×C(n,⌊n/2⌋)2 \times C(n,\lfloor{n/2}\rfloor),可以通过此题。

小细节:被动转移比主动转移要快。

代码
C++
#include <stdio.h>
#include <utility>
#define int unsigned
#define sint unsigned char
#define ll __int128
int n, C[24][24], mid, midlim;
int rlow[1<<12], rhigh[25][1<<12];
unsigned long long slow[1<<12], shigh[1<<12], a[24], b[24];
sint dp[2][1352080];
template<sint mod>
inline sint run() {
	dp[n & 1][0] = 0;
	dp[0][0] = 1;
	for (int ppc = 1; ppc <= n; ++ppc) {
		int now = ppc&1, prv = (ppc&1)^1;
		int f = (1 << ppc) - 1;
		int limit = (1 << n) - 1, rnk = 0;
		while (f <= limit) {
			if (slow[f & midlim] + shigh[f >> mid] <= b[ppc - 1]) {
				int nf = f, nv = 0;
				while (nf) {
					int tof = f ^ (1 << __builtin_ctz(nf));
					nv += dp[prv][rlow[tof & midlim] + rhigh[ppc - 1][tof >> mid]];
					nf &= (nf - 1);
				}
				dp[now][rnk] = nv % mod;
			} else dp[now][rnk] = 0;
			int lb = f & (-f);
			int r = f + lb;
			f = ((r ^ f) >> (2 + __builtin_ctz(lb))) | r;
			++rnk;
		}
	}
	return dp[n&1][0];
}
inline std::pair<ll, ll> ExtendedEuclid(ll a, ll b) {
	if (b == 0) {
		return std::make_pair(1, 0);
	}
	ll quotient = a / b;
	std::pair<ll, ll> result = ExtendedEuclid(b, a - quotient * b);
	return std::make_pair(result.second, (result.first - quotient * result.second));
}
__int128 safemul(__int128 a, __int128 b, __int128 mod) {
	__int128 flg = (b > 0 ? 1 : -1);
	if (b < 0) b = -b;
	__int128 res = 0;
	a %= mod;
	while (b) {
		if (b & 1) res = (res + a) % mod;
		a = (a + a) % mod;
		b >>= 1;
	}
	return res * flg;
}
inline ll CRT(ll d1, ll d2,ll x1, ll x2, ll newmod) {
	ll gc = 1, d = x2 - x1;
	ll mod = newmod;
	std::pair<ll, ll> res = ExtendedEuclid(d1, d2);
	res.second = -res.second;
	res.first *= d / gc;
	res.second *= d / gc;
	return ((x1 + safemul(d1, res.first, mod)) % mod + mod) % mod;
}
signed main() {
	scanf("%u", &n);
	for (int i = 0; i < n; ++i) scanf("%llu", &a[i]);
	for (int i = 0; i < n; ++i) scanf("%llu", &b[i]);
	C[0][0] = 1;
	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j <= n; ++j) {
			if (j) C[i][j] += C[i - 1][j - 1];
			C[i][j] += C[i - 1][j];
		}
	}
	mid = n / 2;
	for (int i = 0; i < (1 << mid); ++i) {
		long long sm = 0;
		for (int j = 0; j < mid; ++j) {
			if ((i >> j) & 1) sm += a[j];
		}
		slow[i] = sm;
	}
	for (int i = 0; i < (1 << (n - mid)); ++i) {
		long long sm = 0;
		for (int j = 0; j < n - mid; ++j) {
			if ((i >> j) & 1) sm += a[mid + j];
		}
		shigh[i] = sm;
	}
	for (int i = 0; i < (1 << mid); ++i) {
		int rnk = 0, cnt = 1;
		for (int j = 0; j < mid; ++j) {
			if ((i >> j) & 1) rnk += C[j][cnt++];
		}
		rlow[i] = rnk;
	}
	for (int i = 0; i < (1 << (n - mid)); ++i) {
		for (int ppc = 0; ppc <= mid; ++ppc) {
			int rnk = 0, cnt = ppc + 1;
			for (int j = 0; j < n - mid; ++j) {
				if ((i >> j) & 1) rnk += C[mid + j][cnt++];
			}
			rhigh[ppc + __builtin_popcount(i)][i] = rnk;
		}
	}
	midlim = (1<<mid)-1;
	ll nowmod = 1, nowx = 0;
	nowx = CRT(nowmod, 251, nowx, run<251>(), nowmod * 251); nowmod = nowmod * 251;
	nowx = CRT(nowmod, 241, nowx, run<241>(), nowmod * 241); nowmod = nowmod * 241;
	nowx = CRT(nowmod, 239, nowx, run<239>(), nowmod * 239); nowmod = nowmod * 239;
	nowx = CRT(nowmod, 233, nowx, run<233>(), nowmod * 233); nowmod = nowmod * 233;
	nowx = CRT(nowmod, 229, nowx, run<229>(), nowmod * 229); nowmod = nowmod * 229;
	nowx = CRT(nowmod, 227, nowx, run<227>(), nowmod * 227); nowmod = nowmod * 227;
	nowx = CRT(nowmod, 223, nowx, run<223>(), nowmod * 223); nowmod = nowmod * 223;
	nowx = CRT(nowmod, 211, nowx, run<211>(), nowmod * 211); nowmod = nowmod * 211;
	nowx = CRT(nowmod, 199, nowx, run<199>(), nowmod * 199); nowmod = nowmod * 199;
	nowx = CRT(nowmod, 197, nowx, run<197>(), nowmod * 197); nowmod = nowmod * 197;
	const long long p = 998244353;
	printf("%lld\n", (long long)(nowx % p));
}

歪解

如果你完整地写出了 CRT 和 k 元子集枚举和排名计算,但是你被卡常了,怎么办?

这很简单,让我们考虑这个:https://www.luogu.com.cn/article/7j746ffq

每一次求解你就只算对某一个模数取模的答案,然后通过 RE 返回值就能精准泄漏!不需要动任何脑子!

只要一次求解能在 5.0s5.0\text{s} 造完,你就可以通过 1010 发提交获得所有数据!

即使在我暗示这么明显的情况下也没有人尝试套过数据,这说明参赛者的素质非常高!值得表扬!

I - 归档

plus(x,?)

注意到 ?=147744265−114=147744151?=147744265-114=147744151。

gcd(x,?)

不难证明,? 的取值可以为输出文件所有数字的最小公倍数。

inv(x,?)

inv 即为逆元的意思,但是不知道模数。

我们设模数为 MM,然后根据输入文件,我们能找到很多对 a,ba,b 满足条件 a−1≡b(modM)a^{-1}\equiv b\pmod M。

变换一下得到 0≡a×b−1(modM)0\equiv a\times b-1\pmod M。

因此你知道了 MM 一定是每一组测试中,a×b−1a\times b-1 的因子,把这些取出来取个最大公因数即可。

hash(s)

根据输入文件的前 2626 次询问可以得出提示中的 Order(i) 就是 ASCII 字母顺序减 96。

随后你会发现输出文件的数字居然都特别小,2×1062\times10^6 都没有超过。

于是其中一种做法就是暴力枚举 base 和 mod,动用家中常备的超级计算机即可通过。

如果你没有超级计算机(包括我),你可以枚举 base 的取值,从而根据一些给定的字符串算出哈希值在取模之前的结果,设其为 aa,取模后为 bb,模数为 MM,那么显然就会有 a≡b(modM)a\equiv b\pmod M,仿照测试点 8,你会得到 a−ba-b 一定是 MM 的倍数。

所以枚举 base 之后把实际哈希值和取模后哈希值相减取 gcd 即可。

rng(x)

阅读提示,我们显然不能人为解出 rng 的参数是什么,但是一个关键性质是,位运算下的所有运算,本质可以描述为 64×6464\times64 下的矩阵运算。

于是你尝试解这个矩阵,你发现出题人把输入限制在了 5757 维子空间内。

这不重要,其他的几个维度可以理解为自由元,不管它即可。

pow(?,x,?)

可惜的是这个点开始就没有人类过了……果然最懂我的还是 AI 吗……虽然有可能是 AI 辅助出题的缘故

考虑怎么求解模数,根据之前的经验,我们可能又会去考虑列出几个同余方程然后相减做差后取最大公因数。

你发现数据的前三行是 103062600,57257000,160319600103062600,57257000,160319600,而前两者相加正好是第三者,因此你可以列出 54104542384828709104433091407340001×82589919159165409351604037248700001−27571558454607825428413391148840001≡0(modM)54104542384828709104433091407340001\times82589919159165409351604037248700001- 27571558454607825428413391148840001\equiv 0\pmod M。

这远远不够,我们需要多组。你还发现输入的前 5353 行的数字非常不同寻常,他们在视觉上看,位数就足够小,所以我们大胆猜测在这 5353 组数中同样会存在 a+b=ca+b=c 的组合。

写个 python 脚本大力枚举后能找到 1010 个这样的组合,取个 gcd 就得到了模数。

但是你不知道底数,最简单的想法就是找到输入文件中两个互质的数,然后用 exgcd 解出 basebase 的值。

很可惜你找不到,他们都是 114514114514 的倍数,但是这样你就能知道 base114514base^{114514} 的取值。因为输入文件都是 114514114514 的倍数,所以这就已经足够了。

factor(x)

分解质因数。

输入点特别大,用 pollard-rho 一定 TLE。

此时你就会开始尝试各种方法和出题人对脑电波。

你会发现在输出文件中,除了开头几行,很多的数都是只有四个或者两个因子的,而且这些因子惊人的都在 2602^{60} 以内。

这意味着输入的数字非常特殊,虽然用常规 pollard-rho 一定 TLE,但是呢这些数字存在一个非常简单的结构可以快速找到一个比较大的因子 pp。

而这个比较大的因子 pp 似乎有什么特定的规律。

于是我们开始尝试对输出文件中,每一个只有两个素因子的数进行深度分析。

你会发现这些素因子之间的差似乎都非常小,好像都是 A+bA+b 之类的形式,其中 AA 是一个固定的大数,而 b<<Ab<<A。所以说,现在的问题就是如何找到这个数字 AA。

你会发现那些有四个因子的元素中,第一个和最后一个因子相乘和这个是非常差不多的。而那些三个因子的元素中,第一个和第二个因子相乘也是差不多的。

这样构造肯定是刻意暗示什么,我们尝试写一个脚本初步确定 AA 和 A+bA+b 的范围:

Python
tc = 20000
minv = 10**18
maxv = 0
for i in range(tc):
	t = list(map(int, input().split()))
	if len(t) == 2:
		minv = min(minv, t[0], t[1])
		maxv = max(maxv, t[0], t[1])
	if len(t) == 3:
		minv = min(minv, t[0] * t[1], t[2])
		maxv = max(maxv, t[0] * t[1], t[2])
	if len(t) == 4:
		minv = min(minv, t[0] * t[3], t[1] * t[2])
		maxv = max(maxv, t[0] * t[3], t[1] * t[2])
print(minv, "~", maxv)
Bash
$ python3 a.py < compress12.out
34522712143973 ~ 34522716195361

你发现这非常惊人,每一个输入的数字 NN 似乎一定存在两个因子,这两个因子的每一个因子 FF 使得 F−34522716195361<<34522716195361F-34522716195361<<34522716195361。

而且你又发现 34522716195361234522716195361^2 的量级和输入的 NN 的最大值几乎一样,这意味着只要找到了这两个因子,Pollard-rho 的时间复杂度就会骤降至 O(n18)O(n^{\frac{1}{8}})。

现在,我们就假设 A=34522712143973A=34522712143973。并假设存在 (x,y)(x,y) 使得 N=(A+x)(A+y)N=(A+x)(A+y),这样展开后,我们可以得到 A2+A(x+y)+xyA^2 + A(x+y) + xy。

根据之前的脚本 x,y≤34522716195361−34522712143973=4051388x,y\le 34522716195361-34522712143973=4051388,因此 xy≤16413744726544≤34522716195361xy \le 16413744726544\le 34522716195361

因此 N mod AN\bmod A 就可以认为是 xyxy 的值。

而有了 xyxy 之后,我们就能得到 A2+A(x+y)A^2 + A(x+y) 的值,也就知道了 x+yx+y 的值。

得到这个,你就可以列出二次方程解出真正的 x,yx,y。

随后你还需要对 A+xA+x 和 A+yA+y 进行质因数分解,此时这两个数字已经足够小了,直接一个 pollard-rho 拍上去即可以 O(n1/8)O(n^{1/8}) 的时间复杂度通过。

若你和出题人心有灵犀,你会注意到 34522712143973=1113+4234522712143973=11^{13}+42,虽然出题人并不认为在无提示的情况下有人可以猜出来,或许有十一根手指的超人能敏锐地观察到。

实际上这个东西用费马因数分解法可以直接过掉……而且还是 AI 爆的标……

代码
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
inline __int128 read() {
    __int128 x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}
inline void print(__int128 x) {
    if (x == 0) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    char buf[42]; 
    int cnt = 0;
    while (x) {
        buf[cnt++] = x % 10 + '0';
        x /= 10;
    }
    while (cnt--) putchar(buf[cnt]);
}
__int128 safemul(__int128 a, __int128 b, __int128 mod) {
    __int128 res = 0;
    a %= mod;
    while (b) {
        if (b & 1) res = (res + a) % mod;
        a = (a + a) % mod;
        b >>= 1;
    }
    return res;
}

__int128 qpow(__int128 base, __int128 exp, __int128 mod) {
    __int128 res = 1;
    base %= mod;
    while (exp) {
        if (exp & 1) res = safemul(res, base, mod);
        base = safemul(base, base, mod);
        exp >>= 1;
    }
    return res;
}
__int128 gcd(__int128 a, __int128 b) {
	return b ? gcd(b, a % b) : a;
}

__int128 qpow_fast(__int128 a, __int128 b, __int128 p) {
	if (b < 0) assert(0);
	__int128 res = 1;
	while (b) {
		if (b & 1) res = res * a % p;
		a = a * a % p;
		b >>= 1;
	}
	return res;
}

int n, c[1000006], tot, pre[1000006];
bool b[1000006];
const int prs[8] = {2,3,5,7,325,9375,28178,450775};
inline bool mr(int x) { // 显然,这个 mr 是错得离谱的,但是在这个数据下,确实是能过的
	if (x == 7999252175582851) return 0;
	if (x == 3215031751) return 0;
	if (x == 3474749660383) return 0;
	if (x <= 1000000) return !b[x];
	for (int i = 0; i < 8; ++i) {
		if (x % prs[i] == 0 || qpow_fast(prs[i], x - 1, x) != 1) return 0;
	}
	return 1;
}
mt19937_64 rnd(141744121);
inline int nxt(int i, int c, int n) {
	return ((__int128)i * i + c) % n;
}
inline int pr(int x) {
	if (x == 4) {
	    return 2;
	}
	for (int i = 0; i < 8; ++i) if (x % prs[i] == 0) return prs[i];
	int c = abs((int)rnd()) % (x - 1) + 1, st = 0, now = 0;
	for (int i = 1; ; i <<= 1, st = now) {
		int prd = 1;
		for (int j = 1; j <= i; ++j) {
			now = nxt(now, c, x);
			//cout<<"x"<<prd<<'\n';
			prd = ((__int128)prd * abs(now - st) % x);
			//cout << now << " " << st << ":" << prd << "\n";
			if (j % 127 == 0) {
				int d = __gcd(prd, x);
				if (d > 1) return d;
			}
		}
		int d = __gcd(prd, x);
		if (d > 1) return d;
	}
}

void ft(int n, vector<int> &ans) {
	if (n == 1) return;
	if (mr(n)) return ans.push_back(n), void();
	int tmp = n;
	while (tmp >= n) tmp = pr(n);
	ft(tmp, ans); ft(n / tmp, ans);
}
inline __int128 isqrt(__int128 n) {
	if (n <= 0) return 0;
	__int128 x = sqrt((double)n);
	while (x * x > n) x--;
	while ((x + 1) * (x + 1) <= n) x++;
	return x;
}
int C(int m, int r) {
	if (m < r) return 0;
	return (r & (m - r)) == 0;
}

uint64_t qry_rng(uint64_t x) {
    const int k = 7;
    const int p[k] = {12, 31, 22, 44, 7, 32, 17};
    const uint64_t m[k] = {0x1234567890abcdefULL, 0xfedcba0987654321ULL, 
        0x1122334455667788ULL, 0x8877665544332211ULL,
        0xdeadbeefcafebabeULL, 0x0102040810204080ULL,
        0xffffffffffffffffULL};
    for (int i = 0; i < k; ++i) {
        if (i % 2 == 0) {
            x ^= (x << p[i]) & m[i];
        } else {
            x ^= (x >> p[i]) & m[i];
        }
    }
    return x;
}
signed main() {
	for (int i = 2; i <= 1000000; ++i) {
		if (!b[i]) {
			c[++tot] = i;
			pre[i] = pre[i];
		}
		for (int j = 1; j <= tot && c[j] * i <= 1000000; ++j) {
			b[c[j] * i] = 1;
			pre[c[j] * i] = c[j];
			if (i % c[j] == 0) break;
		}
	}
	string s;
	cin >> s;
	if (s == "plus(x,?)") {
		int tc;
		cin >> tc;
		for (int i = 1; i <= tc; ++i) {
			long long x;
			scanf("%lld", &x);
			printf("%lld\n", (x + 147744151));
		}
	} else if (s == "gcd(x,?)") {
		int tc;
		cin >> tc;
		const __int128 p = (__int128)2*2*2*2*2*2*2*2*2*3*3*3*3*3*3*5*5*5*5*5*5*5*7*7*7*7*7*7*11*11*13*13*19*31*31*31*41*41*41*41*41*211;
		for (int i = 1; i <= tc; ++i) {
			__int128 x = read();
			__int128 tmp = gcd(p, x);
			print(tmp);
			putchar('\n');
		}
	} else if (s == "inv(x,?)") {
		int tc; cin >> tc;
		for (int i = 1; i <= tc; ++i) {
			long long x;
			scanf("%lld", &x);
			assert(gcd(x, 9847597708724580) == 1);
			// 2 2 3 3 5 7 23 97 97 2309 15641
			printf("%lld\n", (long long)qpow_fast(x, 2129760112803839/*phi*/, 9847597708724580));
		}
	} else if (s == "hash(s)") {
		const __int128 base=(__int128)726525, mod = (__int128)1921920;
		int tc; cin >> tc;
		for (int i = 1; i <= tc; ++i) {
			string s;
			cin >> s;
			__int128 hsh = 0;
			for (auto i : s) hsh = (hsh * base + (i - 'a' + 1)) % mod;
			print(hsh);
			putchar('\n');
		}
	} else if (s == "rng(x)") {
		int tc; cin >> tc;
		for (int i = 1; i <= tc; ++i) {
			__int128 x = read();
			print(qry_rng(x));
			putchar('\n');
		}
	} else if (s == "pow(?,x,?)") {
		const __int128 base = (__int128)9283*850390924247;
		const __int128 mod = (__int128)2*2*2*2*2*2*2*2*3*3*3*3*3*5*5*5*5*5*13*13*13*19*19*19*31*37*37*47*113*113*113*113*96017; // 两者互质即可
		int tc; cin >> tc;
		for (int i = 1; i <= tc; ++i) {
			__int128 x = read();
			print(qpow(base, x, mod));
			putchar('\n');
		}
	} else if (s == "factor(x)") {
		int tc; cin >> tc;
		for (int i = 1; i <= tc; ++i) {
			__int128 N = read();
			const __int128 val_a = 34522712143931, val_a2 = val_a*val_a;
			__int128 P = N % val_a;
			__int128 S = (N - P - val_a2) / val_a;
			__int128 delta = S * S - 4 * P;
			__int128 sqrt_delta = isqrt(delta);
			__int128 b = ((S - sqrt_delta) >> 1);
			__int128 d = ((S + sqrt_delta) >> 1);
			__int128 X = val_a + b;
			__int128 Y = val_a + d;
			vector<int> ans;
			ft(X, ans); ft(Y, ans);
			sort(ans.begin(), ans.end());
			for (int i = 0; i < ans.size(); ++i) {
				cout << ans[i] << " \n"[i == ans.size() - 1];
			}
		}
	} else assert(0);
}

J - 追忆 & 后记

生命瞬间定格在脑海。我将背后的时间裁剪、折叠、蜷曲,揉捻成天上朵朵白云。

我该在哪里停留?我问我自己。

当最后的评测队列归于平静,FCOI 19 的榜单也终于定格。

在《追忆》这道特殊的题目里,我们在后台读到了无数温暖、搞怪且令人动容的赛后寄语。有人写下一行 Hello World,表现对未来学习美好的憧憬;有人写下一个 灼灼,表现往昔师生情谊的思念;有人分享自己曾经的喜悦;有人表达对同窗的美好祝福。还有人则留下一段《追忆》,对这三年的陪伴,做出最完整的告别。

三年前,我们因为对代码最单纯的好奇而相遇;三年后,我们在这里执手作别,奔赴各自更广阔的山海。那些被演算纸堆满的课桌、那些被卡常数时的焦虑、那些在深夜通过测试点时瞬间亮起的淡金色屏幕微光,在这一刻,都凝固成了这本题解里最珍贵的底片。

FCOI 的故事在这里画上了句号。但我们的征途,才刚刚开始。

不论你是刚踏入算法大门的新朋友,还是即将奔向更具挑战的竞赛生活的老战友。愿你在往后面对现实的虚无与琐碎时,仍能感受到那些曾为了一道题、一行代码而挑灯夜战的炽热时光。那些光芒,曾毫无保留地浸润过我们的身躯。

祝你,祝我们,在未来的旅途中,感受尽可能多的甜蜜。

愿我们于顶峰相会。

原文