跳到正文

题解

题解:P11314 [RMI 2021] 礼物 / Present

xyyqwq约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年11月11日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 3 分钟
题目
P11314

版本

  1. v1当前

P11314 [RMI 2021] 礼物 / Present

Solution

无脑爆搜大力过题。考虑爆搜,假设当前集合为 SS。考虑如下构造下一个集合 S′S'。

  1. S←S∪mex⁡(S)S \gets S \cup \operatorname{mex}(S)。
  2. 除去 mex⁡(S)\operatorname{mex}(S) 以下的数。
  3. 将产生的新的 gcd⁡\gcd 加入 SS。

如此操作那么就有一个 O(K)\mathcal O(K) 的做法。其实这个东西非常快。且最大数其实只有 4040 左右。于是考虑分块打表,每 BB 个输出一次,查询直接在表上暴力跳即可。事实上,连超级慢的大唐高鸿都跑了三分钟不到就跑完了。

C++
#include <bits/stdc++.h>

using namespace std;

const int N = 55, B = 1.5e6;
using ull = unsigned long long;

int n;
vector<pair<int, int> > g[N];
ull tb[] = {0,27398715,58218911,102168543,145427647,195509247,...};

ull nxt(ull S) {
	int lb = __builtin_ctzll(S + 1), lim = __lg(S);
	ull nS = ((S >> lb) << lb) | (1ull << lb);
	for (int i = lb - 1; ~i; i -- ) {
		for (auto [x, y] : g[i]) {
			if (x > lim) continue ;
			if ((nS >> x & 1) && (nS >> y & 1)) {
				nS |= (1ull << i);
				break ;
			}
		}
	} return nS;
}

int main() {
	freopen("presents.in", "r", stdin); freopen("presents.out", "w", stdout);
	for (int i = 1; i <= 50; i ++ ) for (int j = i + 1; j <= 50; j ++ )
		for (int k = j + 1; k <= 50; k ++ ) if (__gcd(j, k) == i)
			g[i - 1].push_back({j - 1, k - 1});
	int T; scanf("%d", &T);
	while (T -- ) {
		int n; scanf("%d", &n);
		ull s = tb[n / B];
		for (int i = 1; i <= n % B; i ++ ) s = nxt(s);
		vector<int> res;
		for (int i = 0; i < 64; i ++ ) if (s >> i & 1) res.push_back(i + 1);
		printf("%d ", res.size()); for (int it : res) printf("%d ", it); puts("");
	} return 0;
}
原文