Official

G - Row Column Sums 2 Editorial by en_translator


If \(\sum_{i = 1}^N R_i \neq \sum_{i = 1}^N C_i\), then the answer is obviously \(0\). Hereinafter, we assume \(\sum_{i = 1}^N R_i = \sum_{i = 1}^N C_i\).

Consider constructing a matrix \(A\) by filling one row by one: \(1\)-st row, \(2\)-nd row, \(\ldots\), and \(N\)-th row. When filling the \(i\)-th row, we determine \(A_{i, 1}, A_{i, 2}, \ldots, A_{i_N}\) so that

  • \(\sum_{j = 1}^N A_{i, j} = R_i\) and
  • for all column \(j\), \(C'_{i, j} := C_j - \sum_{k = 1}^i A_{k, j} \geq 0\).

\(C'_{i, j}\) can be considered as the residual sum of the \(i\)-th row when filling up to the \(i\)-th row. Note that \(C'_{i, j}\) is one of \(0\), \(1\), or \(2\). The answer is the number of ways to fill all the \(N\) rows so that, for all column \(j\), it holds that \(C'_{N, j} = 0\).

We solve this problem with Dynamic Programming (DP). Let us define DP table by

\(\mathrm{dp}[i][x] =\) (the number of ways to fill the first \(i\) rows of \(A\) so that there are \(x\) columns \(j\) such that \(C'_{i, j} = 2\)).

When the first \(i\) lines of \(A\) have been filled, if we know that there are \(x\) \(j\)’s such that \(C'_{i, j}= 2\), then we can also determine \(y\), the number of columns \(j\) such that \(C'_{i, j} = 1\), from the constants given in the input and \(x\). Indeed, since

\[\sum_{j = 1}^N C'_{i, j} = y + 2x,\]

we have

\[ \begin{aligned} y &= \sum_{j = 1}^N C'_{i, j} -2x\\ &= \sum_{j = 1}^N (C_j - \sum_{k = 1}^i A_{k, j}) -2x\\ &= \sum_{j = 1}^N C_j - \sum_{j = 1}^N \sum_{k = 1}^i A_{k, j} - 2x\\ &= \sum_{j = 1}^N C_j - \sum_{k = 1}^i \sum_{j = 1}^N A_{k, j} - 2x\\ &= \sum_{j = 1}^N C_j - \sum_{k = 1}^i R_k - 2x   \tag{1} \end{aligned} \]

Here, to derive the last equation we used the fact that we have filled each \(i\)-th row so that \(\sum_{j = 1}^N A_{i, j} = R_i\).

Now we will describe a specific way to perform DP. First, let us consider the initial state of DP. The initial state is

\[ \mathrm{dp}[0][x] = \begin{cases} 1 &(x = x_0)\\ 0 &(x \neq x_0), \end{cases} \]

where \(x_0\) denotes the number of \(j\)’s such that \(C'_{0, j} = C_j = 2\).

Next, let us consider the transitions of the DP. Consider finding \(\mathrm{dp}[i][x]\) when \(\mathrm{dp}[i-1][\ast]\) is known. Suppose that there are \(x\) \(j\)’s such that \(C'_{i, j} = 2\) as a result of filling the first \(i\) rows of \(A\). First, we find \(y\), the number of columns \(j\) such that \(C'_{i, j} = 1\), by the equation (1). Then we split into the following three cases depending on the value \(R_i\):

(i) If \(R_i = 0\)

There are only one possible way to fill the \(i\)-th row, as follows:

  • we filled the \(i\)-th row entirely with \(0\) when there were \(x\) and \(y\) rows \(j\) such that \(C'_{i-1, j} = 1\) and \(2\), respectively.

Therefore,

\[\mathrm{dp}[i][x] = \mathrm{dp}[i-1][x].\]

(ii) If \(R_i = 1\)

There are two possible ways to fill the \(i\)-th row, as follows:

  • we filled one column \(j_1\) such that \(C'_{i-1} = 2\) with \(A_{i, j_1} = 1\) and the others with \(0\) when there were \(x+1\) and \(y-1\) columns \(j\) such that \(C'_{i-1, j} = 1\) and \(2\), respectively; or
  • we filled one column \(j_1\) such that \(C'_{i-1} = 1\) with \(A_{i, j_1} = 1\) and the other with \(0\) when there were \(x\) and \(y+1\) columns \(j\) such that \(C'_{i-1, j} = 1\) and \(2\), respectively.

Therefore,

\[\mathrm{dp}[i][x] = \mathrm{dp}[i-1][x+1]\cdot (x+1) + \mathrm{dp}[i-1][x] \cdot (y+1).\]

(iii) If \(R_i = 2\)

There are four possible ways to fill the \(i\)-th row, as follows:

  • we filled one column \(j_1\) such that \(C'_{i-1} = 2\) with \(A_{i, j_1} = 2\) and the others with \(0\) when there were \(x+1\) and \(y\) columns \(j\) such that \(C'_{i-1, j} = 1\) and \(2\), respectively;
  • we filled two columns \(j_1\) and \(j_2\) such that \(C'_{i-1} = 2\) with \(A_{i, j_1} = A_{i, j_2} = 1\) and the others with \(0\) when there were \(x+2\) and \(y-2\) columns \(j\) such that \(C'_{i-1, j} = 1\) and \(2\), respectively;
  • we filled two columns \(j_1\) and \(j_2\) such that \(C'_{i-1} = 1\) with \(A_{i, j_1} = A_{i, j_2} = 1\) and the others with \(0\) when there were \(x\) and \(y+2\) columns \(j\) such that \(C'_{i-1, j} = 1\) and \(2\), respectively; or
  • we filled one column \(j_1\) such that \(C'_{i-1} = 1\) and another column \(j_2\) such that \(C'_{i-1} = 2\) with \(A_{i, j_1} = A_{i, j_2} = 1\), and the others with \(0\), when there were \(x+1\) and \(y\) columns \(j\) such that \(C'_{i-1, j} = 1\) and \(2\), respectively.

Therefore,

\[\mathrm{dp}[i][x] = \mathrm{dp}[i-1][x+1]\cdot (x+1) + \mathrm{dp}[i-1][x+2]\cdot \binom{x+2}{2} + \mathrm{dp}[i-1][x]\cdot \binom{y+2}{2} + \mathrm{dp}[i-1][x+1]\cdot (x+1)y.\]

By the initialization and transitions above, we can fill the DP table. By the assumption that \(\sum_{i = 1}^N R_i = \sum_{i = 1}^N C_i\), if the number \(x\) of \(j\)’s such that \(C'_{N, j} = 2\) is \(0\), then the number \(y\) of \(j\)’s such that \(C'_{N, j} = 1\) is also \(0\), so the answer to this problem can be found as \(\mathrm{dp}[N][0]\).

The following is a sample code in C++ language.

#include <iostream>
#include <atcoder/modint>
using namespace std;
using namespace atcoder;
using mint = modint998244353;

int n;
int r[5005], c[5005];
mint dp[5005][5005];

int main(void){
  cin >> n;
  for(int i = 1; i <= n; i++) cin >> r[i];
  for(int i = 1; i <= n; i++) cin >> c[i];
  
  int x0 = 0, sum = 0;
  for(int i = 1; i <= n; i++){
    sum += c[i];
    if(c[i] == 2) x0++;
  }
  
  dp[0][x0] = 1;
  for(int i = 1; i <= n; i++){
    sum -= r[i];
    for(int x = 0; x <= n; x++){
      int y = sum - 2*x;
      if(y < 0) break;
      if(r[i] == 0) dp[i][x] = dp[i-1][x];
      if(r[i] == 1) dp[i][x] = dp[i-1][x+1]*(x+1) + dp[i-1][x]*(y+1);
      if(r[i] == 2) dp[i][x] = dp[i-1][x+1]*(x+1) + dp[i-1][x+2]*((x+2)*(x+1)/2) + dp[i-1][x]*((y+2)*(y+1)/2) + dp[i-1][x+1]*(x+1)*y;
    }
  }
  
  if(sum) cout << 0 << endl;
  else cout << dp[n][0].val() << endl;
  
  return 0;
}

posted:
last update: