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

· · 题解

P11314 [RMI 2021] 礼物 / Present

Solution

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

  1. 除去 \operatorname{mex}(S) 以下的数。
  2. 将产生的新的 \gcd 加入 S。

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

#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;
}