QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 512 MB Total points: 100

#2153. HILO

Statistics

Bessie 知道一个数 $x+0.5$,其中 $x$ 是 $0$ 到 $N$ 之间(包含 $0$ 和 $N$)的某个整数($1 \le N \le 5000$)。

Elsie 正在尝试猜这个数。她可以询问形如“$i$ 是高还是低?”的问题,其中 $i$ 是 $1$ 到 $N$ 之间的某个整数。Bessie 会回答“HI!”(如果 $i > x+0.5$)或“LO!”(如果 $i < x+0.5$)。

Elsie 想出了以下猜数策略。在开始猜测之前,她创建了一个包含 $N$ 个数字的列表,其中 $1$ 到 $N$ 的每个数字恰好出现一次(换句话说,该列表是大小为 $N$ 的一个排列)。然后,她按照列表中的顺序依次猜测数字。但是,Elsie 会跳过任何不必要的猜测。也就是说,如果 Elsie 正准备猜测某个数字 $i$,而她之前已经猜过某个 $j < i$ 且 Bessie 回答了“HI!”,那么 Elsie 就不会猜测 $i$,而是继续列表中的下一个数字。同样,如果她正准备猜测某个数字 $i$,而她之前已经猜过某个 $j > i$ 且 Bessie 回答了“LO!”,那么 Elsie 就不会猜测 $i$,而是继续列表中的下一个数字。可以证明,使用这种策略,无论 Elsie 创建什么样的排列,她总是能唯一确定 $x$。

如果我们把 Bessie 的所有回答(“HI”或“LO”)连接成一个字符串 $S$,那么 Bessie 说出“HILO”的次数就是 $S$ 中长度为 $4$ 且等于“HILO”的子串的数量。

Bessie 知道 Elsie 会使用这种策略,并且已经选定了 $x$ 的值,但她不知道 Elsie 会使用哪种排列。你的目标是计算在 Elsie 可能选择的所有排列中,Bessie 说出“HILO”的总次数,结果对 $10^9+7$ 取模。

输入格式

输入仅一行,包含 $N$ 和 $x$。

输出格式

HILO”出现的总次数,对 $10^9+7$ 取模。

样例

样例输入 1

4 2

样例输出 1

17

在此测试用例中,Bessie 的数是 $2.5$。

例如,如果 Elsie 的排列是 $(4,1,3,2)$,那么 Bessie 会说“HILOHILO”,总共出现两次“HILO”。作为另一个例子,如果 Elsie 的排列是 $(3,1,2,4)$,那么 Bessie 会说“HILOLO”,总共出现一次“HILO”。

样例输入 2

60 10

样例输出 2

508859913

请确保输出结果对 $10^9+7$ 取模。

子任务

  • 测试用例 3-10 满足 $N \le 50$。
  • 测试用例 11-18 满足 $N \le 500$。
  • 测试用例 19-26 无额外限制。

题目来源:Richard Qi

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.