結果

問題 No.3117 Reversible Tile
ユーザー tnakao0123
提出日時 2025-04-21 18:02:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 234 ms / 3,000 ms
コード長 2,115 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 42,624 KB
実行使用メモリ 50,504 KB
最終ジャッジ日時 2025-04-21 18:02:55
合計ジャッジ時間 4,970 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:69:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   69 |   scanf("%d%d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:70:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   70 |   for (int i = 0; i < n; i++) scanf("%d", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 3117.cc:  No.3117 Reversible Tile - yukicoder
 */

#include<cstdio>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_N = 5000;
const int MAX_M = 5000;
const int MAX_H = (MAX_N + 1) / 2;
const int MOD = 998244353;

/* typedef */

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) { if (v < 0) v += MOD; }
  MI(long long _v): v(_v % MOD) { if (v < 0) v += MOD; }

  explicit operator int() const { return v; }
  
  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator-() const { return MI(MOD - v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

using mi = MI<MOD>;

/* global variables */

int as[MAX_N];
mi dp[MAX_M + 1][MAX_H + 1];

/* subroutines */

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  int k0 = 0;
  for (int i = 0; i < n;) {
    int j = i;
    while (i < n && as[j] == as[i]) i++;
    if (as[j] == 1) k0++;
  }

  int h = (n + 1) / 2;
  dp[0][k0] = 1;

  for (int i = 0; i < m; i++)
    for (int j = 0; j <= h; j++)
      if (dp[i][j] != 0) {
	if (j > 0)
	  dp[i + 1][j - 1] += dp[i][j] * (j * (j * 2 - 1));
	dp[i + 1][j] += dp[i][j] * (j * 2 * (n + 1 - j * 2));
	if (j < h)
	  dp[i + 1][j + 1] += dp[i][j] * ((n + 1 - j * 2) * (n - j * 2) / 2);
      }

  printf("%d\n", (int)dp[m][0]);
  
  return 0;
}
0