P6612 LIC-Bidding 题解
gaochunzhen · · 个人记录
博弈论+DP。
设
这样就可以转移了。当
-
-
-
-
-
dp[2i][j]=0 -
dp[3i][j]=0
但是这样的话时空复杂度均为
注意到
设
在函数中,只需要选择一个必败的状态给对手即可。
现在复杂度为
注意一个细节:在枚举
#include <bits/stdc++.h>
using namespace std;
const int N = 30009;
int p2[19], p3[19];
bool dp[19][19][N];
// dp 值为 1 时代表先手必胜,否则后手必胜。
void init(int n) {
p2[0] = p3[0] = 1;
for (int i = 1; i <= 15; i++) p2[i] = p2[i - 1] * 2;
for (int i = 1; i <= 10; i++) p3[i] = p3[i - 1] * 3;
for (int y = n; y >= 0; y--) { // 这一维一定要放在最外面
for (int i = 15; i >= 0; i--) {
for (int j = 10; j >= 0; j--) {
int x = p2[i] * p3[j];
if (x + y >= n) continue;
if (x + y + 1 >= n || x * 3 + y >= n) dp[i][j][y] = 1;
if (!dp[i + 1][j][y] || !dp[i][j + 1][y] || !dp[0][0][x + y]) dp[i][j][y] = 1;
}
}
}
}
bool Init = 1;
extern "C" int _opt(int n, int x, int y) {
if (Init) init(n), Init = 0;
int t2 = 0, t3 = 0, x2 = x;
while (x2 % 2 == 0) x2 /= 2, t2++;
while (x2 % 3 == 0) x2 /= 3, t3++;
// x 即为 2^t2 + 3^t3
// 下面枚举进行哪种操作会变成必败状态
if (x + y + 1 >= n || !dp[0][0][x + y]) return 1;
if (x * 2 + y >= n || !dp[t2 + 1][t3][y]) return 2;
if (x * 3 + y >= n || !dp[t2][t3 + 1][y]) return 3;
return 0;
}