結果

問題 No.1916 Making Palindrome on Gird
ユーザー mkawa2mkawa2
提出日時 2022-04-29 23:29:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 3,000 ms
コード長 2,861 bytes
コンパイル時間 2,078 ms
コンパイル使用メモリ 202,248 KB
実行使用メモリ 71,036 KB
最終ジャッジ日時 2023-09-11 15:06:28
合計ジャッジ時間 4,431 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
70,672 KB
testcase_01 AC 21 ms
70,668 KB
testcase_02 AC 20 ms
70,736 KB
testcase_03 AC 20 ms
70,732 KB
testcase_04 AC 20 ms
70,716 KB
testcase_05 AC 21 ms
70,784 KB
testcase_06 AC 20 ms
70,716 KB
testcase_07 AC 20 ms
70,796 KB
testcase_08 AC 20 ms
70,688 KB
testcase_09 AC 20 ms
70,728 KB
testcase_10 AC 20 ms
70,668 KB
testcase_11 AC 20 ms
70,668 KB
testcase_12 AC 20 ms
70,680 KB
testcase_13 AC 23 ms
70,844 KB
testcase_14 AC 23 ms
70,696 KB
testcase_15 AC 30 ms
70,700 KB
testcase_16 AC 26 ms
70,696 KB
testcase_17 AC 38 ms
70,720 KB
testcase_18 AC 45 ms
70,732 KB
testcase_19 AC 45 ms
70,764 KB
testcase_20 AC 46 ms
70,720 KB
testcase_21 AC 47 ms
70,724 KB
testcase_22 AC 45 ms
70,724 KB
testcase_23 AC 26 ms
70,728 KB
testcase_24 AC 26 ms
70,796 KB
testcase_25 AC 26 ms
70,780 KB
testcase_26 AC 26 ms
71,036 KB
testcase_27 AC 26 ms
70,796 KB
testcase_28 AC 59 ms
70,796 KB
testcase_29 AC 60 ms
70,712 KB
testcase_30 AC 58 ms
70,788 KB
testcase_31 AC 59 ms
70,736 KB
testcase_32 AC 59 ms
70,776 KB
権限があれば一括ダウンロードができます

ソースコード

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[205][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