QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 32 MB 總分: 10

#11187. 卡片 [B]

统计

Alice 和 Bob 在阁楼里发现了几副牌。其中一些布满了灰尘,一些不完整,还有一些包含在普通扑克牌中找不到的奇怪花牌。然而,所有的牌都有一个共同点:它们要么是黑色的,要么是红色的。Alice 和 Bob 是非常有创造力的孩子。他们决定利用找到的所有牌来玩以下游戏。

游戏开始时,孩子们将所有的牌洗匀。之后,他们将牌从牌堆顶端一张一张地打到桌子上。如果打出的第一张牌是黑色,或者某一段连续打出的黑色牌序列前面 $不是$ 紧跟着一段长度为 $k$ 倍的连续红色牌序列,那么 Alice 赢得游戏。反之,在所有牌打完后,Bob 赢得游戏。

Alice 想知道她获胜的概率,因此她想计算有多少种洗牌后的排列方式能让她获胜。我们假设所有同颜色的牌都是不可区分的。Alice 最近听说过中国剩余定理,所以她只需要知道结果对给定的质数 $p$ 取模后的值。

输入格式

标准输入的第一行也是唯一一行包含四个整数 $r, b, k$ 和 $p$ ($1 \le r, b \le 100,000, 1 \le k \le 10, 2 \le p \le 1,000,000,000$),以空格分隔。数字 $r$ 表示牌堆中红色牌的数量,$b$ 表示黑色牌的数量。$p$ 是一个质数。

输出格式

标准输出的第一行也是唯一一行应包含 Alice 获胜的排列方式数量对 $p$ 取模后的结果,其中排列由 $r$ 张红色牌和 $b$ 张黑色牌组成。

样例

输入 1

4 2 1 17

输出 1

6

说明

在这种情况下,如果第一张牌是黑色,或者某一段连续的黑色牌序列前面紧跟着的红色牌序列长度小于其 $k$ 倍,则 Alice 获胜。令 R 表示红色牌,B 表示黑色牌。以下是 Alice 获胜的排列方式(每个排列的第一个字母表示从牌堆顶端打出的牌):BBRRRR, BRBRRR, BRRBRR, BRRRBR, BRRRRBRBBRRR

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.