結果

問題 No.1916 Making Palindrome on Gird
ユーザー mkawa2mkawa2
提出日時 2022-04-29 23:28:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,861 bytes
コンパイル時間 2,007 ms
コンパイル使用メモリ 201,432 KB
実行使用メモリ 38,108 KB
最終ジャッジ日時 2023-09-11 15:05:06
合計ジャッジ時間 7,233 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
37,856 KB
testcase_01 AC 12 ms
37,872 KB
testcase_02 AC 12 ms
38,108 KB
testcase_03 AC 11 ms
37,964 KB
testcase_04 AC 12 ms
37,804 KB
testcase_05 AC 13 ms
37,860 KB
testcase_06 AC 12 ms
37,816 KB
testcase_07 AC 12 ms
38,092 KB
testcase_08 AC 12 ms
37,912 KB
testcase_09 AC 12 ms
37,952 KB
testcase_10 AC 12 ms
37,860 KB
testcase_11 AC 12 ms
37,816 KB
testcase_12 AC 12 ms
37,808 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

// #include <atcoder/all>
// using namespace atcoder;
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define popcnt(x) __builtin_popcount(x)
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvll = vector<vector<ll>>;
const int inf = 1e9;
const ll lim = 1e18;
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};

const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x;  // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod - a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint& operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }

mint dp[105][205][205];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  string s[h];
  rep(i, h) cin >> s[i];
  int n = h + w - 1;
  int mid = (n - 1) / 2;

  if (n & 1) {
    rep(i, h) {
      int j = mid - i;
      if (j < 0 || j >= w) continue;
      dp[mid][i][i] = 1;
    }
  } else {
    rep(i, h) {
      int j = mid - i;
      if (j < 0 || j >= w) continue;
      if (i + 1 < h && s[i][j] == s[i + 1][j]) dp[mid][i][i + 1] = 1;
      if (j + 1 < w && s[i][j] == s[i][j + 1]) dp[mid][i][i] = 1;
    }
  }

  for (int d = mid; d > 0; d--) rep(i, h) {
      int j = d - i;
      if (j < 0 || j >= w) continue;
      rep(u, h) {
        mint pre = dp[d][i][u];
        if (pre.x == 0) continue;
        int v = n - 1 - d - u;
        if (v < 0 || v >= w) continue;
        rep(di, 2) rep(du, 2) {
          int ni = i - di, nj = j - 1 + di, nu = u + du, nv = v + 1 - du;
          if (ni < 0 || ni >= h || nj < 0 || nj >= w || nu < 0 || nu >= h ||
              nv < 0 || nv >= w)
            continue;
          if (s[ni][nj] == s[nu][nv]) dp[d - 1][ni][nu] += pre;
        }
      }
    }

  cout << dp[0][0][h - 1].x << endl;

  return 0;
}
0