C. Interesting Game

Codeforces
IDCF87C
Time2000ms
Memory256MB
Difficulty
dpgamesmath
English · Original
Chinese · Translation
Formal · Original
Two best friends Serozha and Gena play a game. Initially there is one pile consisting of _n_ stones on the table. During one move one pile should be taken and divided into an arbitrary number of piles consisting of _a_1 > _a_2 > ... > _a__k_ > 0 stones. The piles should meet the condition _a_1 - _a_2 = _a_2 - _a_3 = ... = _a__k_ - 1 - _a__k_ = 1. Naturally, the number of piles _k_ should be no less than two. The friends play in turns. The player who cannot make a move loses. Serozha makes the first move. Who will win if both players play in the optimal way? ## Input The single line contains a single integer _n_ (1 ≤ _n_ ≤ 105). ## Output If Serozha wins, print _k_, which represents the minimal number of piles into which he can split the initial one during the first move in order to win the game. If Gena wins, print "-1" (without the quotes). [samples]
两个最好的朋友 Serozha 和 Gena 玩一个游戏。 最初桌上有且仅有一堆包含 #cf_span[n] 个石子的石堆。每一步,玩家必须选择一堆石子,并将其分割成任意数量的石堆,这些石堆的石子数满足 #cf_span[a1 > a2 > ... > ak > 0],且必须满足条件 #cf_span[a1 - a2 = a2 - a3 = ... = ak - 1 - ak = 1]。显然,石堆的数量 #cf_span[k] 必须不少于两个。 两位玩家轮流进行操作,无法操作的一方输掉游戏。Serozha 先手。如果双方都采取最优策略,谁会获胜? 输入仅包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 105])。 如果 Serozha 获胜,请输出 #cf_span[k],表示他在第一步中将初始石堆分割成的最少石堆数量以确保获胜。 如果 Gena 获胜,请输出 "-1"(不含引号)。 ## Input 输入仅包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 105])。 ## Output 如果 Serozha 获胜,请输出 #cf_span[k],表示他在第一步中将初始石堆分割成的最少石堆数量以确保获胜。如果 Gena 获胜,请输出 "-1"(不含引号)。 [samples]
Let $ n \in \mathbb{N} $, $ 1 \leq n \leq 10^5 $. A move consists of replacing a pile of size $ n $ with $ k \geq 2 $ piles of sizes $ a_1 > a_2 > \dots > a_k > 0 $ such that: $$ a_i - a_{i+1} = 1 \quad \text{for all } 1 \leq i < k. $$ This implies the pile sizes form a consecutive decreasing sequence: $ a_i = a_1 - (i-1) $, and the total sum is: $$ \sum_{i=1}^k a_i = \sum_{j=0}^{k-1} (a_1 - j) = k a_1 - \frac{k(k-1)}{2} = n. $$ Thus, for a valid move, there must exist integers $ k \geq 2 $ and $ a_1 \geq k $ such that: $$ k a_1 - \frac{k(k-1)}{2} = n. $$ Rearranged: $$ a_1 = \frac{n + \frac{k(k-1)}{2}}{k} = \frac{2n + k(k-1)}{2k}. $$ This must be an integer, so $ 2n + k(k-1) \equiv 0 \pmod{2k} $. Define the game as an impartial combinatorial game under normal play: positions are pile sizes, moves are as above, and a player unable to move loses. Let $ G(n) $ denote the Grundy number of a pile of size $ n $. The base case: $ G(1) = 0 $ (no valid move). For $ n \geq 2 $, define: $$ G(n) = \mathrm{mex} \left\{ \bigoplus_{i=1}^k G(a_i) \mid \text{valid partition of } n \text{ into } k \geq 2 \text{ consecutive decreasing piles} \right\}. $$ But since the piles in a move are **consecutive integers**, and the game is played on a single pile (no multiple independent piles initially), and each move replaces one pile with multiple piles, the resulting position is a **disjunctive sum** of the new piles. So from $ n $, moving to piles $ a_1, a_2, \dots, a_k $ leads to position with Grundy number: $$ G(a_1) \oplus G(a_2) \oplus \dots \oplus G(a_k). $$ Thus: $$ G(n) = \mathrm{mex} \left\{ \bigoplus_{i=1}^k G(a_i) \ \middle|\ \exists k \geq 2, \ a_i = a_1 - (i-1), \ \sum_{i=1}^k a_i = n \right\}. $$ The first player (Serozha) wins if $ G(n) \ne 0 $; loses if $ G(n) = 0 $. If Serozha wins, output the **minimal** $ k \geq 2 $ such that: 1. The sequence $ a_1 > a_2 > \dots > a_k > 0 $ with $ a_i - a_{i+1} = 1 $ sums to $ n $, 2. The resulting position $ \bigoplus_{i=1}^k G(a_i) = 0 $ (i.e., leaves Gena in a losing position). If no such $ k $ exists (i.e., Gena wins), output $-1$. --- **Formal Output Specification:** Let $ S(n) $ be the set of all $ k \geq 2 $ such that there exists integer $ a_1 \geq k $ satisfying: $$ n = \sum_{i=0}^{k-1} (a_1 - i) = k a_1 - \frac{k(k-1)}{2}. $$ For each $ k \in S(n) $, define the pile sizes $ a_i = a_1 - (i-1) $, $ i = 1, \dots, k $, and compute: $$ g_k = \bigoplus_{i=1}^k G(a_i). $$ Then: - If $ G(n) = 0 $, output $-1$. - Else, output $ \min \{ k \in S(n) \mid g_k = 0 \} $. Where $ G(m) $ is recursively defined for $ m = 1 $ to $ n $ as: $$ G(1) = 0, $$ $$ G(m) = \mathrm{mex} \left\{ \bigoplus_{i=1}^k G(a_i) \ \middle|\ k \geq 2,\ \sum_{i=1}^k a_i = m,\ a_i = a_1 - (i-1),\ a_1 \in \mathbb{Z}^+ \right\}. $$
Samples
Input #1
3
Output #1
2
Input #2
6
Output #2
\-1
Input #3
100
Output #3
8
API Response (JSON)
{
  "problem": {
    "name": "C. Interesting Game",
    "description": {
      "content": "Two best friends Serozha and Gena play a game. Initially there is one pile consisting of _n_ stones on the table. During one move one pile should be taken and divided into an arbitrary number of pile",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF87C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Two best friends Serozha and Gena play a game.\n\nInitially there is one pile consisting of _n_ stones on the table. During one move one pile should be taken and divided into an arbitrary number of pile...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "两个最好的朋友 Serozha 和 Gena 玩一个游戏。\n\n最初桌上有且仅有一堆包含 #cf_span[n] 个石子的石堆。每一步,玩家必须选择一堆石子,并将其分割成任意数量的石堆,这些石堆的石子数满足 #cf_span[a1 > a2 > ... > ak > 0],且必须满足条件 #cf_span[a1 - a2 = a2 - a3 = ... = ak - 1 - ak = 1]。显然,石...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ n \\in \\mathbb{N} $, $ 1 \\leq n \\leq 10^5 $.\n\nA move consists of replacing a pile of size $ n $ with $ k \\geq 2 $ piles of sizes $ a_1 > a_2 > \\dots > a_k > 0 $ such that:\n$$\na_i - a_{i+1} = 1 \\q...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments