QOJ.ac

QOJ

時間限制: 3 s 記憶體限制: 1024 MB 總分: 100

#16216. Constructing a coloring

统计

Bob has discovered a new interesting game. In this game, Bob has $n$ enemies to defeat, and each enemy has one of $m$ colors. In one turn, Bob can attack a contiguous segment of enemies, provided that all enemies in that segment have the same color.

Bob wants to attack as many enemies as possible in a single turn, but sometimes he is not satisfied. For any given configuration of colors, we define Bob's happiness as the maximum number of enemies he can attack in a single turn. Bob wants to know the sum of his happiness over all $m^n$ possible color configurations of the enemies. Since this number can be very large, you need to output the result modulo a given prime number.

Implementation Details

You should include coloring.h and implement the following procedure:

int totalhappy(int n, int m, int p)

  • $n$: The number of enemies.
  • $m$: The total number of colors.
  • $p$: The modulus. A prime number.
  • The procedure you implement will be called exactly once.
  • You should return the sum of happiness over all coloring schemes modulo $p$: an integer in the range $[0, p-1]$.

Examples

Consider the call: totalhappy(2, 2, 998244353)

If the two enemies have the same color, Bob's happiness is $2$; otherwise, it is $1$. Therefore, the total happiness is $2+2+1+1=6$. A correct program should return $6 \bmod 998244353 = 6$.

Constraints

$1 \le n \le 10^6$

$1 \le m \le 10^9$

$10^8 < p < 10^9$, where $p$ is a prime number.

Subtasks

  1. (10 points) $n \le 10$, $m \le 4$
  2. (20 points) $n \le 100$
  3. (30 points) $n \le 50000$
  4. (20 points) $m \le 10^6$
  5. (20 points) No additional constraints.

Sample Grader

The sample grader reads the input in the following format:

  • One line: $n~m~p$

The sample grader prints your output in the following format:

  • One line: your return value

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.