QOJ.ac

QOJ

Límite de tiempo: 2.0 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓

#7802. 尼姆游戏

Estadísticas

Georgiy 和 Gennady 正在玩一个他们刚发明的新游戏,这个游戏是在学习了经典的 Nim 游戏后设计的。该游戏使用 $n$ 颗石子,分为两个阶段。

在准备阶段,Georgiy 选择一个正整数 $p < n$,并在游戏场上放置一堆包含 $p$ 颗石子的石堆。之后,Gennady 使用剩下的 $(n - p)$ 颗石子,将其分成任意数量的石堆,每堆包含任意数量的石子。

例如,如果 $n = 10$ 且 $p = 2$,Gennady 可以: 分成 8 堆,每堆 1 颗石子, 或者分成一堆 5 颗和一堆 3 颗, 或者分成 2 堆 2 颗和 4 堆 1 颗, 或者分成一堆 8 颗, * 等等。

准备阶段结束后,进入 Nim 阶段。在这个阶段,双方进行 Nim 游戏。玩家轮流操作,由 Georgiy 先手。每一回合,玩家必须从某一堆中移除至少一颗石子,且可以移除任意数量的石子(只要这些石子来自同一堆)。最后取走石子的玩家在 Nim 游戏中获胜,并因此赢得整个游戏。

Georgiy 和 Gennady 刚刚开始游戏,现在正处于准备阶段的中期:Georgiy 已经放置了他的那堆 $p$ 颗石子,但 Gennady 还没有将剩下的 $(n - p)$ 颗石子分成石堆。现在 Gennady 想知道他获胜的机会。

你需要计算 Gennady 有多少种方法将 $(n - p)$ 颗石子分成若干堆,使得他能够获胜,假设双方在 Nim 游戏中都采取最优策略。

众所周知,根据 Sprague-Grundy 定理,Gennady 获胜当且仅当所有石堆大小(Georgiy 的那堆 $p$ 颗石子以及 Gennady 分出的所有石堆)的按位异或(XOR)和等于零。

由于答案可能很大,请计算其对 $m$ 取模的结果。如果对应的石堆大小多重集不同,则视为两种不同的分法——即石堆的顺序无关紧要。

输入格式

输入仅一行,包含三个整数 $n, p, m$,分别表示游戏中的石子总数、Georgiy 选择的石堆大小以及模数 ($1 \le p < n \le 500; 2 \le m \le 10^9$)。

输出格式

输出 Gennady 将剩下的 $(n - p)$ 颗石子分成若干堆使得他能获胜的方法数,对 $m$ 取模。

样例

样例输入 1

8 3 1000

样例输出 1

2

样例输入 2

5 2 1000

样例输出 2

0

说明

在第一个样例中,Gennady 分配剩余 5 颗石子的两种获胜方式为: 一堆 3 颗和 2 堆 1 颗, 或者一堆 2 颗和 3 堆 1 颗。

在第二个样例中,无论 Gennady 如何分配剩下的 3 颗石子,他都必输无疑。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.