-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0464.cpp
More file actions
32 lines (25 loc) · 757 Bytes
/
Copy path0464.cpp
File metadata and controls
32 lines (25 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
static int8_t dp[1 << 20];
int rec(int maxi, int tgt, int k) {
if (dp[k] != 0) return dp[k] > 0;
if (tgt <= 0) return false;
for (int i = 1, mask = 1; i <= maxi; mask <<= 1, i++) {
if (k & mask) continue;
if (rec(maxi, tgt - i, k | mask)) continue;
dp[k] = 1;
return true;
}
dp[k] = -1;
return false;
}
public:
bool canIWin(int maxi, int tgt) {
if (tgt <= maxi) return true;
const int sum = maxi * (maxi + 1) / 2;
if (sum < tgt) return false;
if (sum == tgt) return maxi % 2;
memset(dp, 0x00, sizeof(dp));
return rec(maxi, tgt, 0);
}
};
int8_t Solution::dp[1 << 20];