# 1025.divisor-game

## Statement

• Difficulty: Easy
• Tag: `脑筋急转弯` `数学` `动态规划` `博弈`

• 选出任一 `x`，满足 `0 < x < n` 且 `n % x == 0` 。
• `n - x` 替换黑板上的数字 `n`

``````输入：n = 2

``````

``````输入：n = 3

``````

• `1 <= n <= 1000`

• Difficulty: Easy
• Tag: `Brainteaser` `Math` `Dynamic Programming` `Game Theory`

Alice and Bob take turns playing a game, with Alice starting first.

Initially, there is a number `n` on the chalkboard. On each player's turn, that player makes a move consisting of:

• Choosing any `x` with `0 < x < n` and `n % x == 0`.
• Replacing the number `n` on the chalkboard with `n - x`.

Also, if a player cannot make a move, they lose the game.

Return `true` if and only if Alice wins the game, assuming both players play optimally.

Example 1:

``````Input: n = 2
Output: true
Explanation: Alice chooses 1, and Bob has no more moves.
``````

Example 2:

``````Input: n = 3
Output: false
Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.
``````

Constraints:

• `1 <= n <= 1000`

## Solution

``````#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\n"
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall rbegin(a), rend(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
#define MP make_pair

using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using VLL = std::vector<ll>;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

template <typename T, typename S>
inline bool chmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}

template <typename T, typename S>
inline bool chmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}

#ifdef LOCAL
#include <debug.hpp>
#else
#define dbg(...)
#endif

class Solution {
public:
bool divisorGame(int n) {
auto f = vector<int>(n + 1, false);
for (int i = 2; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (i % j == 0 && f[i - j] == false) {
f[i] = true;
break;
}
}
}

return f[n];
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````