【CodeForces - 1051D】Bicolorings (dp,类似状压dp)
題干:
You are given a grid, consisting of?22?rows and?nn?columns. Each cell of this grid should be colored either black or white.
Two cells are considered neighbours if they have a?common border?and share the same color. Two cells?AA?and?BB?belong to the same component if they are neighbours, or if there is a neighbour of?AA?that belongs to the same component with?BB.
Let's call some bicoloring?beautiful?if it has exactly?kk?components.
Count the number of?beautiful?bicolorings. The number can be big enough, so print the answer modulo?998244353998244353.
Input
The only line contains two integers?nn?and?kk?(1≤n≤10001≤n≤1000,?1≤k≤2n1≤k≤2n) — the number of columns in a grid and the number of components required.
Output
Print a single integer — the number of?beautiful?bicolorings modulo?998244353998244353.
Examples
Input
3 4Output
12Input
4 1Output
2Input
1 2Output
2Note
One of possible bicolorings in sample?11:
題目大意:
? ? 有一個2*n的矩形塊,填黑白兩色的格子,問你恰好有k個連通塊的填法的方案數有多少種?輸入n和k。
解題報告:
? ?題干敘述比較啰嗦,,解釋了一大堆,其實就是解釋的連通塊。。那么dp狀態轉移就不難了。。(最近愛上了做dp不知道為啥,,就是喜歡找虐)
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long #define pb push_back #define pm make_pair #define fi first #define se second using namespace std; const int MAX = 2e5 + 5; const ll mod = 998244353; ll dp[1005][2010][4];//dp[i][j][k]截止到第i行,連通塊數為j,且最后一列是狀態k的方案數。 int main() {int n,k;cin>>n>>k;dp[1][1][0]=dp[1][1][3]=1;dp[1][2][1]=dp[1][2][2]=1;for(int i = 2; i<=n; i++) {for(int j = 1; j<=k; j++) {//狀態0-3:00 01 10 11 dp[i][j][0] = (dp[i-1][j][0]+dp[i-1][j][1]+dp[i-1][j][2]+dp[i-1][j-1][3])%mod;dp[i][j][1] = (dp[i-1][j-1][0]+dp[i-1][j][1]+dp[i-1][j-2][2]+dp[i-1][j-1][3])%mod;//或者(dp[i-1][j][0]+1)+...是錯的!! dp[i][j][2] = (dp[i-1][j-1][0]+dp[i-1][j][1]+dp[i-1][j-2][2]+dp[i-1][j-1][3])%mod;dp[i][j][3] = (dp[i-1][j-1][0]+dp[i-1][j][1]+dp[i-1][j][2]+dp[i-1][j][3])%mod;}}ll ans = 0;for(int i = 0; i<4; i++) {ans += dp[n][k][i];ans%=mod;}printf("%lld\n",ans);return 0 ;}總結:
? ?1.這題不能按照后面注釋的那種寫法、、、因為是表示的方案數啊,不是表示的連通塊的個數!!
? ?2.第二層循環寫成for(int j = 1; j<=2*n; j++)也可以,但是這題用不到后面那些狀態,這兩個代碼交上去,前者77ms,后者93ms
總結
以上是生活随笔為你收集整理的【CodeForces - 1051D】Bicolorings (dp,类似状压dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: remupd.exe - remupd是
- 下一篇: 【CodeForces - 289C】P