题解:P17496 [ICPC 2026 Wuhan I] Prime Game

· · 题解

啊?这是青题吗???建议降绿。

首先,我个人认为题目的瓶颈在于:n \sim 2 \times n - 1 这个范围的数太多了,我们一个一个找太慢,所以需要缩小寻找的范围。根据题目很容易想到我们应该尽量让彩叶替换的数的最大质因子尽量小,这样辉夜操作的时候减少的数值才会更小。

那么具体怎么做呢?因为替换范围在 n \sim 2 \times n - 1,所以只选择有质因子 2 的数是不可行的。于是我们考虑每次都选只有质因子 2 和 3 的数,多手搓几个样例或者打个表都能发现这是可行的。

范围缩小了,但是暴搜每个可能的情况还是要超时,因为时间复杂度是跟深度有关的。不过因为只有质因数 2 和 3 的原因,重复的情况很多,所以我们加个记忆化,就能通过了。

AC 代码(赛时)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IL inline
namespace fastIO {
    template <typename T = ll>
    IL T qin () {
        T x = 0; int f = 1; char ch = getchar();
        while (ch < '0' || ch > '9') {if (!~ch) return 0; if (ch == '-') f = -1; ch = getchar();}
        while ('0' <= ch && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
        return x * f;
    }
    template <typename T>
    IL void qout (T x) {
        if (x < 0) {putchar('-'); x = -x;}
        if (x > 9) qout(x / 10);
        putchar(x % 10 + '0');
    }
    IL string strin () {static char buf[1000005]; scanf("%s", buf); return string(buf);}
    IL void strout (const string &s) {fputs(s.c_str(), stdout);}
    #define ENDL putchar('\n')
    #define SPACE putchar(' ')
} using namespace fastIO;
#define lop(i, a, b) for (auto i = (a); i <= (b); i++)
#define pol(i, a, b) for (auto i = (a); i >= (b); i--)
#define lops(i, a, b, c) for (auto i = (a); i <= (b); i += (c))
#define pols(i, a, b, c) for (auto i = (a); i >= (b); i -= (c)) 
#define zmax(a, b) ((a) = ((a) > (b) ? (a) : (b)))
#define zmin(a, b) ((a) = ((a) < (b) ? (a) : (b)))
#define ppl(x, y) memset(x, y, sizeof x)
#define asing(a, l, r) begin(a) + (l), begin(a) + (r) + 1
#define deing(a, l, r) begin(a) + (l), begin(a) + (r) + 1, greater<>()
#define pii pair<int, int>
#define fst first
#define snd second
const int N = 2005;
ll n, prime[10] = {2, 3};
ll a[N], cnt;
void dfs (ll res, int last) {
    lop(i, last, 1) if ((__int128)res * prime[i] <= n * 2 - 1) {
        ll nres = res * prime[i];
        a[++cnt] = nres;
        dfs(nres, i);
    } else break;
}
unordered_map<ll, int> mp;
int solve (ll x) {
    if (x == 1) return 0;
    if (mp.count(x)) return mp[x];
    int step = 0;
    int l = lower_bound(asing(a, 1, cnt), x) - a;
    int r = upper_bound(asing(a, 1, cnt), x * 2 - 1) - a - 1;
    lop(i, l, r) {
        int d = (a[i] % 3 == 0 ? 3 : 2);
        zmax(step, solve(a[i] / d));
    }
    return mp[x] = step + 1;
}
int main () {
    n = qin();
    dfs(1, 0);
    sort(asing(a, 1, cnt));
    int ans = solve(n);
    qout(ans);
    return 0;
}

这是一位自宅警备员的第十二篇题解,可以点个赞吗求求了。