[Codeforces] 1051D Bicolorings

\(\color{red}{\mathcal{Description}}\)

You are given a grid, consisting of \(2\) rows and \(n\) 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 \(A\) and \(B\) belong to the same component if they are neighbours, or if there is a neighbour of \(A\) that belongs to the same component with \(B\) .

Let's call some bicoloring beautiful if it has exactly \(k\) components.

Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo \(998244353\) .

Given a \ (2 \ times n \) board, dyed black and white can be above the grid, the number of seek staining Unicom blocks on the board exactly \ (K \) number staining protocol

\(\color{red}{\mathcal{Input\ Format}}\)

The only line contains two integers \(n\) and \(k\) — the number of columns in a grid and the number of components required.

Line two integers \ (n-\) , \ (K \)

\(\color{red}{\mathcal{Output\ Format}}\)

Print a single integer — the number of beautiful bicolorings modulo \(998244353\) .

An integer representing the number of program \ (\ mod 998244353 \) results after

\(\color{red}{\mathcal{DataSize\ Agreement}}\)

1≤n≤1000 $ $, \ (1 \ k and \ 2n and \)

\(\color{red}{\mathcal{Solution}}\)

Topic concise (effect), consider using dynamic programming

We may wish to think of questions from a weaker version, which is only one line of time

Order \ (dp [i] [j ] [k] \) represents the section \ (I \) column, produces \ (J \) a communication block, status \ (K \) number when the program is clearly \ (k \) only two states: \ (0 \) or \ (1 \) , we can get the transfer equation (best drawing understand):

\[dp[i][j][0]=dp[i-1][j][0]+dp[i-1][j-1][1]\]
\[dp[i][j][1]=dp[i-1][j-1][0]+dp[i-1][j][1]\]

Understand this transition equation above, for this question only becomes \ (4 \) states, transition equation naturally came out (the best drawing understand)

\[k:00, 10, 01, 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]\]
\[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])\]
\[dp[i][j][2]=(dp[i-1][j-1][0]+dp[i-1][j-2][1]+dp[i-1][j][2]+dp[i-1][j-1][3])\]
\[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])\]

\ [2 \ leq i \ leq N, 1 \ leq j \ leq 2i \]

Initialization \ (dp [1] [1 ] [0] = dp [1] [2] [1] = dp [1] [2] [2] = dp [1] [1] [3] = 1 \)

\(\color{red}{\mathcal{Code}}\)

#include <bits/stdc++.h>
#define LL long long
#define reg register

using namespace std;

const int kM = 1010, kC = 2010, kS = (1 << 1) + 2, mod = 998244353;

LL dp[kM][kC][kS] = {0}; //kM-列 kC-个数 kS-00 01 10 11
int N, K;

int main() {
  scanf("%d%d", &N, &K);
  dp[1][1][0] = dp[1][2][1] = dp[1][2][2] = dp[1][1][3] = 1;
  for (reg int i = 2; i <= N; ++i) {
    for (reg int j = 1; j <= i * 2; ++j) {
      dp[i][j][0] = (dp[i-1][j][0] % mod + dp[i-1][j][1] % mod + dp[i-1][j][2] % mod + dp[i-1][j-1][3]) % mod;
      dp[i][j][1] = (dp[i-1][j-1][0] % mod + dp[i-1][j][1] % mod + dp[i-1][j-2][2] % mod + dp[i-1][j-1][3] % mod) % mod;
      dp[i][j][2] = (dp[i-1][j-1][0] % mod+ dp[i-1][j-2][1] % mod + dp[i-1][j][2] % mod + dp[i-1][j-1][3] % mod) % mod;
      dp[i][j][3] = (dp[i-1][j-1][0] % mod + dp[i-1][j][1] % mod + dp[i-1][j][2] % mod + dp[i-1][j][3] % mod) % mod;
    }
  }
  printf("%lld\n", (dp[N][K][0] % mod + dp[N][K][1] % mod + dp[N][K][2] % mod + dp[N][K][3] % mod) % mod);
  
  return 0;
}

\(\color{red}{\mathcal{Source}}\)

\(Educational\ Codeforces\ Round 51\ [Rated\ for\ Div. 2]\)

Guess you like

Origin www.cnblogs.com/1miharu/p/11333219.html