Colorful Bricks CodeForces - 1081C ( 组合数学 或 DP )
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 kkbricks 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 998244353998244353.
Input
The first and only line contains three integers nn, mm and kk (1≤n,m≤2000,0≤k≤n−11≤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
3 3 0
3
3 2 1
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种颜色,让你求出这n个方块中有k个方块的颜色和左边的方块颜色不同的方案数量。
思路:
可以有两个方法来做这题,
我们先来看第一种方法,组合数学的方式,我们知道第一个方块是不会被计入到这k个数中的,所以无论如何排列,第一个方块都有m种取色的方案。
然后我们只需要在后面的n-1个方块中,选择k个,使之和左边的颜色不同,n-1中取k个,我们可以用组合数学的公式求出。
然后对于每一个被选择作为k个之一的方块,这个方块的唯一颜色限定就是要求和左边的颜色不同,而左边的颜色无论取什么,
对当前的方块来说只是一种取色,那么我们这个方块可以从除了左边的方块颜色中的m-1个颜色中任意选取一个。
而要求和左边颜色相同的方块,它们的颜色不不归属自己的选择,因为要和左边的颜色一样,
那么我们可以得到这题的公式,就是ans=m*C(n-1,k)* ((m-1)^k )
记得取模就好了。
方案1的代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define rt return #define dll(x) scanf("%I64d",&x) #define xll(x) printf("%I64d\n",x) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define db(x) cout<<"== [ "<<x<<" ] =="<<endl; using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} inline void getInt(int* p); const int maxn=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ ll n,m,k; ll dp[2020][2020]; const ll mod=998244353ll; ll quick(ll a,ll b,ll m) { ll ans=1; while(b) { if(b&1) { ans=(a*ans)%m; } a=(a*a)%m; b>>=1; } return ans; } ll inverse(ll a,ll p){return quick(a,p-2,p);} //模素数求逆元 // a 中选b个 ,结果对m取模 ll comp(ll a,ll b,ll m) //组合数取模 { if(a<b) return 0; if(a==b) return 1; ll ans=1,ca=1,cb=1; for(ll i=0;i<b;i++) { ca=(ca*(a-i))%m; cb=(cb*(b-i))%m; } ans=(ca*inverse(cb,m))%m; return ans; } int main() { //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin); //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout); gbtb; cin>>n>>m>>k; ll ans=m; ans=(ans+mod)%mod; ans*=comp(n-1ll,k,mod); ans=(ans+mod)%mod; ans*=powmod(m-1ll,k,mod); ans=(ans+mod)%mod; // ans*=powmod(m,n-k,mod); // ans=(ans+mod)%mod; cout<<ans<<endl; return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ' ' || ch == '\n'); if (ch == '-') { *p = -(getchar() - '0'); while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } } }
方案2:DP
我们定义二维DP的状态如下:
dp[i][j] 表示,到第i个位置时,有j个方块和左边的颜色不同的方案数。
我们再思考dp[i][j]这个状态可以从哪里转移过来。
我们考虑当前第i个位置的两种选择,
1,和左边相同的颜色。
那么应该加上上一个状态的j值,即 dp[i][j]+=dp[i-1][j]
2,和左边的值不相同,
那么当前的dp[i][j] 应该加上 dp[i-1][j-1]*(m-1)
即上一个状态,是j个不容方块时,乘上当前i块可以有m-1种取色。
最后的dp[n][k] 就是我们要得到的答案,
记得取模。
细节见代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define rt return #define dll(x) scanf("%I64d",&x) #define xll(x) printf("%I64d\n",x) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define db(x) cout<<"== [ "<<x<<" ] =="<<endl; using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} inline void getInt(int* p); const int maxn=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ ll n,m,k; ll dp[2020][2020]; const ll mod=998244353ll; int main() { //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin); //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout); gbtb; cin>>n>>m>>k; // dp[i][j] 到第i个位置,有k个和左边不同的 repd(i,1,n) { dp[i][0]=m; } repd(i,2,n) { repd(j,1,k) { dp[i][j]+=dp[i-1][j]; dp[i][j]=(dp[i][j]+mod)%mod; dp[i][j]+=dp[i-1][j-1]*(m-1ll); dp[i][j]=(dp[i][j]+mod)%mod; } } cout<<dp[n][k]<<endl; return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ' ' || ch == '\n'); if (ch == '-') { *p = -(getchar() - '0'); while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } } }