题意
有n块砖,m种颜色,问恰有k块砖和它左边的颜色不同的方案数,第一块砖左边没有砖所以不考虑。
Examples | #1 | #2 |
---|---|---|
Input | 3 3 0 | 3 2 1 |
Output | 3 | 4 |
思路
令dp[i][j]为i个砖中恰有j个与左边不同的方案数,对于i-1和i只有相同和不同两种情况,不同时
i和i-1相同时,易得dp[i][j]=dp[i-1][j],不同时,因为只需要和i-1不同,和其他砖块都无关,所以i有m-1种选择。所以dp[i][j]=dp[i-1][j-1]*(m-1);
则有:dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
代码
#include <iostream>
#include <cstring>
#include <cmath>
#include <bitset>
#include <queue>
#include <vector>
#include <cstdio>
#include <queue>
#include <sstream>
#include <string>
#include <algorithm>
#include <map>
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define reps(i, a, b) for (int i = a; i >= b; i--)
#define mk make_pair
using namespace std;
const int N = 1e5 + 7;
const int M = 2e3+ 7;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
typedef long long ll;
ll dp[M][M];
int a[M];
int main()
{
ll n,m,k;
scanf("%lld%lld%lld",&n,&m,&k);
rep(i,1,n){
dp[i][0]=m;
rep(j,1,k){
dp[i][j]=(dp[i-1][j]+dp[i-1][j-1]*(m-1)%mod)%mod;
}
}
printf("%lld\n",dp[n][k]);
return 0;
}