結果

問題 No.226 0-1パズル
ユーザー ytqm3ytqm3
提出日時 2022-10-09 15:26:12
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 3,069 bytes
コンパイル時間 2,264 ms
コンパイル使用メモリ 214,496 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-10-04 12:09:00
合計ジャッジ時間 3,247 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,500 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using u64 = unsigned long long;
using i64 = long long;

template<u64 mod=998244353> struct modint{
  u64 val;
  modint(i64 val_=0):val((val_%i64(mod)+i64(mod))%i64(mod)){}
  modint operator-(){
    return (val==0)?0:mod-val;
  }
  modint operator+(modint rhs){
    return modint(*this)+=rhs;
  }
  modint operator-(modint rhs){
    return modint(*this)-=rhs;
  }
  modint operator*(modint rhs){
    return modint(*this)*=rhs;
  }
  modint operator/(modint rhs){
    return modint(*this)/=rhs;
  }
  modint operator^(i64 rhs){
    return modint(*this)^=rhs;
  }
  modint &operator+=(modint rhs){
    val+=rhs.val,val-=((val>=mod)?mod:0);
    return (*this);
  }
  modint &operator-=(modint rhs){
    val+=((val<rhs.val)?mod:0),val-=rhs.val;
    return (*this);
  }
  modint &operator*=(modint rhs){
    val=val*rhs.val%mod;
    return (*this);
  }
  modint &operator/=(modint rhs){
    return (*this)*=rhs^(mod-2);
  }
  modint &operator^=(i64 rhs){
    modint res=1,now=(*this);
    while(rhs){
      res*=((rhs&1)?now:1),now*=now,rhs>>=1;
    }
    return (*this)=res;
  }
  bool operator==(modint rhs){
    return val==rhs.val;
  }
  bool operator!=(modint rhs){
    return val!=rhs.val;
  }
  friend std::ostream &operator<<(std::ostream& os,modint x){
    return os<<(x.val);
  }
  friend std::istream &operator>>(std::istream& is,modint& x){
    u64 t;
    is>>t,x=t;
    return is;
  }
};

int check(array<int,2>& a){
     if(a[0]+a[1]==0){
          return 2;
     }
     if(a[0] && a[1]){
          return 0;
     }
     return 1;
}

int main(){
     constexpr u64 mod = 1000000007;
     typedef modint<mod> mint;
     int n,m;
     array<int,2> tmp({0,0});
     scanf("%d%d", &n, &m);
     vector<string> S(n);
     for(int i=0;i<n;++i){ cin>>S[i]; }
     vector<int> h(3),w(3);
     h[2]=n,w[2]=m;
     vector<map<int,int>> mph(n+1),mpw(m+1);
     vector<array<int,2>> sth(n+1,{0,0}),stw(m+1,{0,0});
     //mph[x] -> y : t + 1
     for(int i=0;i<n*m;++i){
          int x, y, t;
          x=i/m,y=i%m;
          if(S[x][y]=='?'){ continue; }
          t=S[x][y]-'0';
          if(mph[x][y]){
               h[check(sth[x])]--;
               int s=mph[x][y];
               mph[x][y]=0;
               sth[x][(y+s)%2]--;
               tmp[(x+y+s)%2]--;
               h[check(sth[x])]++;
          }
          if(mpw[y][x]){
               w[check(stw[y])]--;
               int s=mpw[y][x];
               mpw[y][x]=0;
               stw[y][(x+s)%2]--;
               w[check(stw[y])]++;
          }
          if(t==-1){
               continue;
          }
          {
               h[check(sth[x])]--;
               mph[x][y]=t+1;
               sth[x][(y+t+1)%2]++;
               tmp[(x+y+t+1)%2]++;
               h[check(sth[x])]++;
          }
          {
               w[check(stw[y])]--;
               mpw[y][x]=t+1;
               stw[y][(x+t+1)%2]++;
               w[check(stw[y])]++;
          }
     }
     printf("%d\n", ((mint(2)^h[2])*(h[0]?0:1)+(mint(2)^w[2])*(w[0]?0:1)-check(tmp)).val);
}
0