結果

問題 No.1916 Making Palindrome on Gird
ユーザー cureskolcureskol
提出日時 2022-04-29 22:31:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 450 ms / 3,000 ms
コード長 3,476 bytes
コンパイル時間 2,146 ms
コンパイル使用メモリ 188,660 KB
実行使用メモリ 5,232 KB
最終ジャッジ日時 2023-09-11 13:52:07
合計ジャッジ時間 6,852 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 43 ms
4,376 KB
testcase_16 AC 23 ms
4,376 KB
testcase_17 AC 85 ms
4,376 KB
testcase_18 AC 210 ms
4,380 KB
testcase_19 AC 206 ms
4,380 KB
testcase_20 AC 210 ms
4,376 KB
testcase_21 AC 212 ms
4,380 KB
testcase_22 AC 205 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 446 ms
5,172 KB
testcase_29 AC 450 ms
5,112 KB
testcase_30 AC 449 ms
5,104 KB
testcase_31 AC 447 ms
5,164 KB
testcase_32 AC 447 ms
5,232 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:30:3: 警告: inline variables are only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   30 |   inline static constexpr T mod = MOD;
      |   ^~~~~~
main.cpp: 関数 ‘int main()’ 内:
main.cpp:113:20: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
  113 |     for(const auto&[key,val]:mp){
      |                    ^
main.cpp:114:18: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
  114 |       const auto&[y1,y2] = key;
      |                  ^
main.cpp:132:18: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
  132 |   for(const auto&[key,val]:mp)
      |                  ^

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
#define debug(a) cerr<<#a<<":"<<a<<endl;
#define debug2(a,b) cerr<<"("<<#a<<","<<#b<<"):("<<a<<","<<b<<")"<<endl;
#define debug3(a,b,c) cerr<<"("<<#a<<","<<#b<<","<<#c<<"):("<<a<<","<<b<<","<<c<<")"<<endl;
#define debug4(a,b,c,d) cerr<<"("<<#a<<","<<#b<<","<<#c<<","<<#d<<"):("<<a<<","<<b<<","<<c<<","<<d<<")"<<endl;

template<typename T>
istream& operator>>(istream&is,vector<T>&v){
  for(T&p:v)is>>p;
  return is;
}
template<typename T>
ostream& operator<<(ostream&os,const vector<T>&v){
  if(&os==&cerr)os<<"[";
  for(int i=0;i<v.size();i++){
    os<<v[i];
    if(i+1<v.size())os<<(&os==&cerr?",":" ");
  }
  if(&os==&cerr)os<<"]";
  return os;
}

template<typename T,T MOD=998244353>
struct Mint{
  inline static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0)v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1)res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator+() const{return *this;}
  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }

  friend ostream& operator<<(ostream&os,const Mint &m){os<<m.v;return os;}
  friend istream& operator>>(istream&is,Mint &m){is>>m.v;m.v%=MOD;if(m.v<0)m.v+=MOD;return is;}
};

using mint=Mint<long long,1000000007>;

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

  int h,w;cin>>h>>w;
  vector<string> s(h);
  cin>>s;

  map<pair<int,int>,mint> mp;
  int a=-1,b;
  if((h+w-1)&1){
    REP(i,h)REP(j,w)if((i+1+j+1-1)*2-1 == h+w-1){
      mp[{i,i}]=1;
      a=i+j,b=i+j;
    }
  }
  else{
    REP(i,h)REP(j,w)if((i+1+j+1-1)*2 == h+w-1){
      if(i+1<h and s[i+1][j]==s[i][j])mp[{i,i+1}]=1;
      if(j+1<w and s[i][j+1]==s[i][j])mp[{i,i}]=1;
      a=i+j,b=i+j+1;
    }
  }
  
  if(a<0){
    cout<<0<<endl;
    return 0;
  }

  while(a){
    map<pair<int,int>,mint> nxt;
    for(const auto&[key,val]:mp){
      const auto&[y1,y2] = key;
      int x1=a-y1;
      int x2=b-y2;
      assert(x1>=0 and x2>=0);
      //debug4(y1,x1,y2,x2);
      //debug(val);
      if(x1 and x2+1<w and s[y1][x1-1]==s[y2][x2+1])nxt[{y1,y2}]+=val;
      if(x1 and y2+1<h and s[y1][x1-1]==s[y2+1][x2])nxt[{y1,y2+1}]+=val;
      if(y1 and x2+1<w and s[y1-1][x1]==s[y2][x2+1])nxt[{y1-1,y2}]+=val;
      if(y1 and y2+1<h and s[y1-1][x1]==s[y2+1][x2])nxt[{y1-1,y2+1}]+=val;
    }
    mp=nxt;
    a--;b++;
  }

  assert(a==0 and b==h-1+w-1);

  mint ans=0;
  for(const auto&[key,val]:mp)
    ans+=val;
  cout<<ans<<endl;
}
0