結果

問題 No.1916 Making Palindrome on Gird
ユーザー umezoumezo
提出日時 2022-04-29 23:49:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 4,015 bytes
コンパイル時間 2,704 ms
コンパイル使用メモリ 228,008 KB
実行使用メモリ 814,816 KB
最終ジャッジ日時 2023-09-11 15:32:45
合計ジャッジ時間 7,059 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,284 KB
testcase_01 AC 5 ms
7,332 KB
testcase_02 AC 4 ms
7,328 KB
testcase_03 AC 5 ms
7,468 KB
testcase_04 AC 35 ms
11,812 KB
testcase_05 AC 15 ms
9,144 KB
testcase_06 AC 7 ms
7,660 KB
testcase_07 AC 9 ms
8,128 KB
testcase_08 AC 20 ms
9,672 KB
testcase_09 AC 5 ms
7,388 KB
testcase_10 AC 4 ms
7,288 KB
testcase_11 AC 7 ms
7,684 KB
testcase_12 AC 5 ms
7,432 KB
testcase_13 MLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

#include <bits/stdc++.h>
using namespace std;

const int MOD=1e9+7;
map<string,ll> dp1[205][205],dp2[205][205];

int main(){
  int h,w;
  cin>>h>>w;
  vector<string> S(h);
  rep(i,h) cin>>S[i];
 
  if(h==1 && w==1){
    cout<<1<<endl;
    return 0;
  }
  
  string ss1,ss2;
  ss1+=S[0][0],ss2+=S[h-1][w-1];
  if(ss1!=ss2){
    cout<<0<<endl;
    return 0;
  }
  dp1[0][0][ss1]=1,dp2[0][0][ss2]=1;
  int s=h+w-2;
  if(s%2==1){
    s=(s-1)/2;
    rep(i,h){
      rep(j,w){
        if(i+j>s) break;
        if(i<h-1 && j<w-1){
          for(auto [a,b]:dp1[i][j]){
            dp1[i+1][j][a+S[i+1][j]]=(dp1[i+1][j][a+S[i+1][j]]+b)%MOD;
            dp1[i][j+1][a+S[i][j+1]]=(dp1[i][j+1][a+S[i][j+1]]+b)%MOD;
          }
        }
        else if(i<h-1){
          for(auto [a,b]:dp1[i][j]){
            dp1[i+1][j][a+S[i+1][j]]=(dp1[i+1][j][a+S[i+1][j]]+b)%MOD;
          }
        }
        else{
          for(auto [a,b]:dp1[i][j]){
            dp1[i][j+1][a+S[i][j+1]]=(dp1[i][j+1][a+S[i][j+1]]+b)%MOD;
          }
        }
      }
    }
    rep(i,h){
      rep(j,w){
        if(i+j>s) break;
        swap(S[i][j],S[h-1-i][w-1-j]);
      }
    }
    rep(i,h){
      rep(j,w){
        if(i+j>s) break;
        if(i<h-1 && j<w-1){
          for(auto [a,b]:dp2[i][j]){
            dp2[i+1][j][a+S[i+1][j]]=(dp2[i+1][j][a+S[i+1][j]]+b)%MOD;
            dp2[i][j+1][a+S[i][j+1]]=(dp2[i][j+1][a+S[i][j+1]]+b)%MOD;
          }
        }
        else if(i<h-1){
          for(auto [a,b]:dp2[i][j]){
            dp2[i+1][j][a+S[i+1][j]]=(dp2[i+1][j][a+S[i+1][j]]+b)%MOD;
          }
        }
        else{
          for(auto [a,b]:dp2[i][j]){
            dp2[i][j+1][a+S[i][j+1]]=(dp2[i][j+1][a+S[i][j+1]]+b)%MOD;
          }
        }
      }
    }
    ll ans=0;
    rep(i,h){
      int j=s-i;
      if(j<0 || j>=w) continue;
      map<string,ll> m2;
      if(h-i-2>=0) for(auto [a,b]:dp2[h-1-i-1][w-1-j]) m2[a]=(m2[a]+b)%MOD;
      if(w-j-2>=0) for(auto [a,b]:dp2[h-1-i][w-1-j-1]) m2[a]=(m2[a]+b)%MOD;
      for(auto [a,b]:dp1[i][j]) ans=(ans+b*m2[a]%MOD)%MOD;
    }
    cout<<ans<<endl;

  }
  else{
    s=s/2-1;
    rep(i,h){
      rep(j,w){
        if(i+j>s) break;
        if(i<h-1 && j<w-1){
          for(auto [a,b]:dp1[i][j]){
            dp1[i+1][j][a+S[i+1][j]]=(dp1[i+1][j][a+S[i+1][j]]+b)%MOD;
            dp1[i][j+1][a+S[i][j+1]]=(dp1[i][j+1][a+S[i][j+1]]+b)%MOD;
          }
        }
        else if(i<h-1){
          for(auto [a,b]:dp1[i][j]){
            dp1[i+1][j][a+S[i+1][j]]=(dp1[i+1][j][a+S[i+1][j]]+b)%MOD;
          }
        }
        else{
          for(auto [a,b]:dp1[i][j]){
            dp1[i][j+1][a+S[i][j+1]]=(dp1[i][j+1][a+S[i][j+1]]+b)%MOD;
          }
        }
      }
    }
    rep(i,h){
      rep(j,w){
        if(i+j>s) break;
        swap(S[i][j],S[h-1-i][w-1-j]);
      }
    }
    rep(i,h){
      rep(j,w){
        if(i+j>s) break;
        if(i<h-1 && j<w-1){
          for(auto [a,b]:dp2[i][j]){
            dp2[i+1][j][a+S[i+1][j]]=(dp2[i+1][j][a+S[i+1][j]]+b)%MOD;
            dp2[i][j+1][a+S[i][j+1]]=(dp2[i][j+1][a+S[i][j+1]]+b)%MOD;
          }
        }
        else if(i<h-1){
          for(auto [a,b]:dp2[i][j]){
            dp2[i+1][j][a+S[i+1][j]]=(dp2[i+1][j][a+S[i+1][j]]+b)%MOD;
          }
        }
        else{
          for(auto [a,b]:dp2[i][j]){
            dp2[i][j+1][a+S[i][j+1]]=(dp2[i][j+1][a+S[i][j+1]]+b)%MOD;
          }
        }
      }
    }
    s++;
    ll ans=0;
    rep(i,h){
      int j=s-i;
      if(j<0 || j>=w) continue;
      map<string,ll> m1,m2;
      if(i-1>=0) for(auto [a,b]:dp1[i-1][j]) m1[a]=(m1[a]+b)%MOD;
      if(j-1>=0) for(auto [a,b]:dp1[i][j-1]) m1[a]=(m1[a]+b)%MOD;
      if(h-i-2>=0) for(auto [a,b]:dp2[h-1-i-1][w-1-j]) m2[a]=(m2[a]+b)%MOD;
      if(w-j-2>=0) for(auto [a,b]:dp2[h-1-i][w-1-j-1]) m2[a]=(m2[a]+b)%MOD;
      for(auto [a,b]:m1) ans=(ans+b*m2[a]%MOD)%MOD;
    }
    cout<<ans<<endl;
  }
  
  return 0; 
}
0