結果

問題 No.3742 Re: Verse X
コンテスト
ユーザー caz37OwO
提出日時 2026-10-09 12:31:16
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 102 ms / 2,000 ms
+ 108µs
コード長 5,481 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,554 ms
コンパイル使用メモリ 411,400 KB
実行使用メモリ 10,036 KB
最終ジャッジ日時 2026-10-09 12:31:27
合計ジャッジ時間 11,119 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 57
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using ll=long long;
using ldub=long double;
using lldub=__float128;
using str=string;

using mint=modint;

template<class T=ll>
using tup2=tuple<T,T>;
template<class T=ll>
using tup3=tuple<T,T,T>;
template<class T=ll>
using tup4=tuple<T,T,T,T>;
template<class T=ll>
using tup5=tuple<T,T,T,T,T>;
template<class T=ll>
using tup6=tuple<T,T,T,T,T,T>;
template<class T=ll>
using tup7=tuple<T,T,T,T,T,T,T>;

template<class T=ll>
using vec=vector<T>;
template<class T=ll>
using vec2=vector<vec<T>>;
template<class T=ll>
using vec3=vector<vec2<T>>;
template<class T=ll>
using vec4=vector<vec3<T>>;
template<class T=ll>
using vec5=vector<vec4<T>>;
template<class T=ll>
using vec6=vector<vec5<T>>;

template<class T>
using que=queue<T>;
template<class T>
using Pque=priority_queue<T>;
template<class T>
using pque=priority_queue<T, vector<T>, greater<T>>;

struct Edge{
  ll from,to,w=1,num=-1;
};
using gvec=vector<Edge>;
using gvec2=vector<gvec>;

template<class T>
bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>
bool chmin(T &a,T b){if(b<a){a=b;return 1;}return 0;}

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ins insert
#define tup make_tuple
#define low lower_bound
#define pops(a) __builtin_popcountll(a)
#define ctz(a) __builtin_ctzll(a)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define nperm(v) next_permutation(all(v))
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YN(f) cout << ((f)?"Yes":"No") << endl

const int IINF=2e9;
const ll INF=4e18;

#ifndef LOCAL
#define debug(x)
#endif




// =================================================




void solve(){
  ll N;
  cin >> N;
  vec2 A(N,vec(N));
  for(ll i=0;i<N;i++){
    for(ll j=0;j<N;j++){
      char c;
      cin >> c;
      if(c=='.') A[i][j]=0;
      if(c=='#') A[i][j]=1;
    }
  }
  vec<tup3<ll>> ans;
  auto fff=[&](vec2<ll> &A,ll k,ll r,ll c,bool flg)->void{
    if(flg) ans.pb(tup(k,r,c));
    for(ll i=-k;i<=k;i++){
      A[r+i][c+i]^=1;
      A[r+i][c-i]^=1;
    }
    A[r][c]^=1;
  };
  for(ll i=N-1;i>=10;i--){
    {
      ll f0=0,f1=0;
      for(ll j=0;j<N;j+=2) f0^=A[i-1][j];
      for(ll j=1;j<N;j+=2) f1^=A[i-1][j];
      if(f0) fff(A,1,i-1,2,true);
      if(f1) fff(A,1,i-1,1,true);
    }
    {
      ll f0=0,f1=0;
      for(ll j=0;j<N;j+=4) f0^=A[i][j];
      for(ll j=1;j<N;j+=4) f1^=A[i][j];
      if(f0) fff(A,3,i-3,3,true);
      if(f1) fff(A,3,i-3,4,true);
    }
    {
      for(ll j=N-1;j>=4;j--){
        if(!A[i][j]) continue;
        fff(A,2,i-2,j-2,true);
      }
    }
  }
  for(ll j=N-1;j>=10;j--){
    {
      ll f0=0,f1=0;
      for(ll i=0;i<N;i+=2) f0^=A[i][j-1];
      for(ll i=1;i<N;i+=2) f1^=A[i][j-1];
      if(f0) fff(A,1,2,j-1,true);
      if(f1) fff(A,1,1,j-1,true);
    }
    {
      ll f0=0,f1=0;
      for(ll i=0;i<N;i+=4) f0^=A[i][j];
      for(ll i=1;i<N;i+=4) f1^=A[i][j];
      if(f0) fff(A,3,3,j-3,true);
      if(f1) fff(A,3,4,j-3,true);
    }
    {
      for(ll i=N-1;i>=4;i--){
        if(!A[i][j]) continue;
        fff(A,2,i-2,j-2,true);
      }
    }
  }
  for(ll i=0;i<N;i++){
    for(ll j=0;j<N;j++){
      if(max(i,j)<10||!A[i][j]) continue;
      cout << -1 << endl;
      return;
    }
  }
  chmin(N,10LL);
  vec3 B;
  vec4 C;
  for(ll k=1;2*k<N;k++){
    for(ll r=k;r+k<N;r++){
      for(ll c=k;c+k<N;c++){
        vec2 X(N,vec(N));
        fff(X,k,r,c,false);
        B.pb(X);
        vec3 Y(N,vec2(N,vec(N)));
        Y[k][r][c]=1;
        C.pb(Y);
      }
    }
  }
  ll sz=(ll)B.size();
  {
    ll p=0;
    for(ll i=0;i<N;i++){
      for(ll j=0;j<N;j++){
        ll q=-1;
        for(ll t=p;t<sz;t++) if(B[t][i][j]) q=t;
        if(q==-1||p==sz) continue;
        swap(B[p],B[q]),swap(C[p],C[q]);
        for(ll t=0;t<sz;t++){
          if(t==p||!B[t][i][j]) continue;
          for(ll r=0;r<N;r++){
            for(ll c=0;c<N;c++) B[t][r][c]^=B[p][r][c];
          }
          for(ll r=0;r<N;r++){
            for(ll c=0;c<N;c++){
              for(ll k=0;k<N;k++) C[t][k][r][c]^=C[p][k][r][c];
            }
          }
        }
        p++;
      }
    }
  }
  while(B.back()==vec2(N,vec(N))) B.pob(),C.pob();
  sz=(ll)B.size();
  vec3 F(N,vec2(N,vec(N)));
  for(ll t=0;t<sz;t++){
    ll h=-1,w=-1;
    for(ll i=0;i<N;i++){
      for(ll j=0;j<N;j++){
        if(!B[t][i][j]) continue;
        h=i,w=j;
        break;
      }
      if(h!=-1) break;
    }
    if(!A[h][w]) continue;
    for(ll i=0;i<N;i++){
      for(ll j=0;j<N;j++) A[i][j]^=B[t][i][j];
    }
    for(ll i=0;i<N;i++){
      for(ll j=0;j<N;j++){
        for(ll k=0;k<N;k++) F[k][i][j]^=C[t][k][i][j];
      }
    }
  }
  // for(ll i=0;i<N;i++){
  //   for(ll j=0;j<N;j++){
  //     if(A[i][j]) cerr << '#';
  //     else cerr << '.';
  //   }
  //   cerr << endl;
  // }
  // cerr << endl;
  for(ll i=0;i<N;i++){
    for(ll j=0;j<N;j++){
      if(!A[i][j]) continue;
      cout << -1 << endl;
      return;
    }
  }
  for(ll i=0;i<N;i++){
    for(ll j=0;j<N;j++){
      for(ll k=0;k<N;k++){
        if(!F[k][i][j]) continue;
        ans.pb(tup(k,i,j));
      }
    }
  }
  cout << (ll)ans.size() << endl;
  for(auto [k,r,c]:ans) cout << k << ' ' << r+1 << ' ' << c+1 << endl;
}

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

  mint::set_mod(998244353);
  // mint::set_mod(1000000007);

  ll T=1;
  // cin >> T;
  while(T--) solve();
  return 0;
}
0