题解:P17496 [ICPC 2026 Wuhan I] Prime Game
Hey3_smallZ · · 题解
啊?这是青题吗???建议降绿。
首先,我个人认为题目的瓶颈在于:
那么具体怎么做呢?因为替换范围在
范围缩小了,但是暴搜每个可能的情况还是要超时,因为时间复杂度是跟深度有关的。不过因为只有质因数
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;
}
这是一位自宅警备员的第十二篇题解,可以点个赞吗求求了。