1. 程式人生 > >Avito Cool Challenge 2018 C. Colorful Bricks 【排列組合】

Avito Cool Challenge 2018 C. Colorful Bricks 【排列組合】

傳送門:http://codeforces.com/contest/1081/problem/C

C. Colorful Bricks time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.

There are nn bricks lined in a row on the ground. Chouti has got mm paint buckets of different colors at hand, so he painted each brick in one of those mm colors.

Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are 

k">kk bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).

So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 

998244353">998244353998244353.

Input

The first and only line contains three integers nn, mm and kk (1n,m2000,0kn11≤n,m≤2000,0≤k≤n−1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.

Output

Print one integer — the number of ways to color bricks modulo 998244353998244353.

Examples input Copy
3 3 0
output Copy
3
input Copy
3 2 1
output Copy
4
Note

In the first example, since k=0k=0, the color of every brick should be the same, so there will be exactly m=3m=3 ways to color the bricks.

In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 44 possible colorings.

 

 

題意概括:

N 個 方塊, M 種顏色,存在 K 個方塊使得它與相鄰左邊的方塊顏色不同。

求塗色方案數。

 

解題思路:

換角度思考,其實就是 求把 N塊方塊分成 K+1塊與相鄰左邊塗色不同的方案數。

楊輝三角求組合數 C(N-1, K), 因為第一塊不考慮與左邊顏色的關係 有 M 種可能,其餘的都要去掉左邊那一塊的顏色,所以只有 M-1種可能,即 M*(M-1)*(M-1)*......*(M-1) ;

分塊方案數 * 顏色方案數 即最後答案。

 

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #include <cmath>
 7 #define INF 0x3f3f3f3f
 8 #define LL long long
 9 using namespace std;
10 const int MAXN = 2e3+10;
11 const LL MOD = 998244353;
12 LL c[MAXN][MAXN];
13 
14 LL qpow(LL x, LL n)
15 {
16     LL res = 1LL;
17     while(n){
18         if(n&1) res = ((res%MOD*x%MOD)+MOD)%MOD;
19         x = x*x%MOD;
20         n>>=1LL;
21     }
22     return res;
23 }
24 
25 int main()
26 {
27     LL N, M, K;
28     cin >> N >> M >> K;
29     //memset(c, 1LL, sizeof(1LL));
30     c[0][0] = c[1][0] = c[1][1] = 1LL;
31 
32     for(int i = 2; i <= N; i++){
33         c[i][0] = 1LL;
34         for(int j = 1; j < i; j++){
35             c[i][j] = (c[i-1][j-1] + c[i-1][j])%MOD;
36         }
37         c[i][i] = 1LL;
38     }
39 
40     //cout << c[N-1][K];
41 
42     LL ans = 1LL;
43     ans = (M%MOD*c[N-1][K]%MOD*qpow(M-1LL, K)%MOD + MOD)%MOD;
44     cout << ans << endl;
45     return 0;
46 
47 }