結果

問題 No.1916 Making Palindrome on Gird
ユーザー cureskolcureskol
提出日時 2022-04-29 21:44:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 4,065 bytes
コンパイル時間 3,323 ms
コンパイル使用メモリ 217,444 KB
実行使用メモリ 546,816 KB
最終ジャッジ日時 2024-06-29 03:05:00
合計ジャッジ時間 8,533 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
86,784 KB
testcase_01 AC 34 ms
81,536 KB
testcase_02 AC 34 ms
81,524 KB
testcase_03 AC 34 ms
81,520 KB
testcase_04 AC 35 ms
81,536 KB
testcase_05 AC 34 ms
81,536 KB
testcase_06 AC 35 ms
81,408 KB
testcase_07 AC 35 ms
81,652 KB
testcase_08 AC 35 ms
81,536 KB
testcase_09 AC 36 ms
81,536 KB
testcase_10 AC 37 ms
81,536 KB
testcase_11 AC 37 ms
81,536 KB
testcase_12 AC 35 ms
81,536 KB
testcase_13 AC 286 ms
88,448 KB
testcase_14 AC 113 ms
85,492 KB
testcase_15 MLE -
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 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:30:3: warning: 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: In function 'int main()':
main.cpp:117:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  117 |       auto [a,b]=que.front();que.pop();
      |            ^
main.cpp:119:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  119 |       for(const auto&[yx,val]:a){
      |                      ^
main.cpp:120:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  120 |         const auto&[y,x]=yx;
      |                    ^
main.cpp:124:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  124 |       for(const auto&[yx,val]:b){
      |                      ^
main.cpp:125:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  125 |         const auto&[y,x]=yx;
      |                    ^
main.cpp:142:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  142 |     auto [a,b]=que.front();que.pop();
      |          ^
main.cpp:143:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  143 |     for(const auto&[yx,val]:a){
      |                    ^
main.cpp:146:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
  146 |         const auto&[y,x]=yx;
      |                    ^

ソースコード

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 ll=long long;
using mint=Mint<ll,1000000007>;

mint Factorial[5000000],Finverse[5000000];
inline void Cinit(){
  Factorial[0]=1;
  for(int i=1;i<5e6;i++)Factorial[i]=Factorial[i-1]*mint(i);
  Finverse[4999999]=mint(1)/Factorial[4999999];
  for(int i=4999998;i>=0;i--)Finverse[i]=mint(i+1)*Finverse[i+1];
}
mint nCk(int n,int k){
  if(n<k)return 0;
  return Factorial[n]*Finverse[n-k]*Finverse[k];
}

struct A{
  map<pair<int,int>,mint> a,b;
};

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int h,w;cin>>h>>w;
  vector<string> s(h);
  cin>>s;
  if(s[0][0]!=s.back().back()){
    cout<<0<<endl;
    return 0;
  }
  //debug(h);
  queue<A> que;
  map<pair<int,int>,mint> a,b;
  a[{0,0}]=b[{h-1,w-1}]=1;
  que.push({a,b});
  int aa=0,bb=h-1+w-1;
  while(bb-aa>1){
    queue<A> nxt;
    while(que.size()){
      auto [a,b]=que.front();que.pop();
      vector<map<pair<int,int>,mint>> aa(26),bb(26);
      for(const auto&[yx,val]:a){
        const auto&[y,x]=yx;
        if(y+1<h)aa[s[y+1][x]-'a'][{y+1,x}]+=val;
        if(x+1<w)aa[s[y][x+1]-'a'][{y,x+1}]+=val;
      }
      for(const auto&[yx,val]:b){
        const auto&[y,x]=yx;
        if(y)bb[s[y-1][x]-'a'][{y-1,x}]+=val;
        if(x)bb[s[y][x-1]-'a'][{y,x-1}]+=val;
      }
      REP(i,26)if(aa[i].size() and bb[i].size()){
        //cerr<<('a'+i)<<endl;
        //for(const auto&[yx,val]:aa[i])debug3(yx.first,yx.second,val);
        //for(const auto&[yx,val]:bb[i])debug3(yx.first,yx.second,val);
        nxt.push(A{aa[i],bb[i]});
      }
    }
    aa++;bb--;
    que=nxt;
  }

  mint ans=0;
  while(que.size()){
    auto [a,b]=que.front();que.pop();
    for(const auto&[yx,val]:a){
      if(aa==bb)ans+=val*b[yx];
      else{
        const auto&[y,x]=yx;
        ans+=val*(b[{y+1,x}]+b[{y,x+1}]);
      }
    }
  }
  cout<<ans<<endl;
}

0