| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3611 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | dXqwq | 3436 |
| 8 | Radewoosh | 3415 |
| 9 | Otomachi_Una | 3413 |
| 10 | Um_nik | 3376 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 164 |
| 2 | adamant | 150 |
| 3 | Um_nik | 146 |
| 4 | Dominater069 | 144 |
| 5 | errorgorn | 141 |
| 6 | cry | 139 |
| 7 | Proof_by_QED | 136 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 9 | TheScrasse | 134 |
| Name |
|---|



Suppose the people were standing in a line, not a circle (people 1 and N weren't adjacent). Then, one way to solve the problem is to let dp(n) be the number of assignments (mod 998244353) for the first n people such that no two adjacent people have the same number. dp(1) = M, since the first person can be assigned any number, and dp(n + 1) = dp(n) * (M — 1), since each successive person can choose any of the M — 1 numbers not selected by the person to their left.
However, this doesn't give us enough information to solve the original problem. In particular, we need the last person to have a different number than the first person, but there is no way to know in how many of the dp(N) solutions the first person and the last person have different numbers. This motivates being slightly more granular in what we compute for each person: instead of just calculating the total number of solutions, divide that into a(n), the number of assignments for people 1 to n where person n's number is equal to person 1's, and b(n), the number of assignments where it is not.
Now, a(1) = M and b(1) = 0, since person 1's number is always equal to itself. a(n + 1) = b(n): you can assign person 1's number to person n + 1 iff it is not equal to person n's number. Moreover, b(n + 1) = b(n) * (M — 2) + a(n) * (M — 1): you have to avoid both person 1's number and person n's number, leaving M — 2 options, unless those are the same, in which case you have M — 1 options. The solution is now b(N), since you have to give person N a different number than person 1. (Of course, all calculations are modulo 998244353.)
Now, I got it. Thank you so much !!
You're truly genius.