結果
問題 | No.2505 matriX cOnstRuction |
ユーザー |
![]() |
提出日時 | 2023-10-13 23:05:47 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,085 ms / 2,500 ms |
コード長 | 3,440 bytes |
コンパイル時間 | 4,476 ms |
コンパイル使用メモリ | 262,752 KB |
最終ジャッジ日時 | 2025-02-17 07:30:32 |
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 64 |
ソースコード
#include <stdio.h> #include <atcoder/all> #include <bits/stdc++.h> using namespace std; using namespace atcoder; using mint = static_modint<943718401>; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf32 1000000001 #define Inf64 1000000000000000001 template <typename T> struct trie{ T init_value; struct node{ vector<int> next; T v; T sum; int depth; node(int wordSize,T iv,int d){ next.resize(wordSize,-1); v = iv; sum = iv; depth = d; } int link=-1; }; vector<node> nodes; int wordSize; trie(int sz,T iv){ init_value = iv; wordSize = sz; nodes.push_back(node(wordSize,init_value,0)); } void add(string &S,T x,int cPos=0,int cNode=0){ if(cPos==S.size()){ nodes[cNode].v = func(nodes[cNode].v,x); return; } int c = encode(S[cPos]); if(nodes[cNode].next[c]==-1){ nodes[cNode].next[c] = nodes.size(); nodes.push_back(node(wordSize,init_value,nodes[cNode].depth+1)); } int nextNode = nodes[cNode].next[c]; add(S,x,cPos+1,nextNode); } long long dfs(int cNode=0){ long long ret = Inf32; rep(i,2){ if(nodes[cNode].next[i]!=-1)ret = min(ret,dfs(nodes[cNode].next[i])); else ret = min(ret,0LL); } ret += nodes[cNode].v; return ret; } void set_link(){ nodes[0].link = 0; queue<int> Q; Q.push(0); while(Q.size()!=0){ int now = Q.front(); Q.pop(); nodes[now].v = func(nodes[now].v,nodes[nodes[now].link].v); nodes[now].sum = func(nodes[now].sum,nodes[now].v); for(int i=0;i<wordSize;i++){ int to = nodes[now].next[i]; if(to==-1)continue; int x = now; while(x!=0){ x = nodes[x].link; if(nodes[x].next[i]!=-1){ x = nodes[x].next[i]; break; } } nodes[to].link = x; nodes[to].sum = func(nodes[to].sum,nodes[now].sum); Q.push(to); } } } T query(string &S,int cPos = 0,int cNode=0){ T ret = init_value; if(cPos==S.size())return ret; int c = encode(S[cPos]); int nextNode = nodes[cNode].next[c]; if(nextNode==-1){ if(nodes[cNode].link!=-1){ if(cNode!=0)ret = func(ret,query(S,cPos,nodes[cNode].link)); else ret = func(ret,query(S,cPos+1,cNode)); } } else{ ret = func(ret,nodes[nextNode].v); ret = func(ret,query(S,cPos+1,nextNode)); } return ret; } int encode(char c){ return c-'0'; } T func(T a,T b){ return a+b; } }; void solve(){ int n,m; cin>>n>>m; vector<int> r(n),c(m); rep(i,n)cin>>r[i]; rep(i,m)cin>>c[i]; vector a(n,vector<int>(m)); rep(i,n){ rep(j,m){ cin>>a[i][j]; } } vector<int> rx(n),cx(m); rep(i,n)rx[i] = a[i][0]; for(int j=1;j<m;j++){ cx[j] = a[0][j] ^ rx[0]; } rep(i,n){ rep(j,m){ if((rx[i] ^ cx[j])!=a[i][j]){ cout<<-1<<endl; return; } } } trie<long long> T(2,0); int ans = Inf32; rep(_,2){ rep(i,n){ { string s = ""; rep(j,30){ s += '0' + ((rx[i]>>j)&1); } reverse(s.begin(),s.end()); T.add(s,-1); } string s = ""; bool f = false; for(int j=29;j>=0;j--){ if(((r[i]>>j)&1)==0){ if((rx[i]>>j)&1)s += '0'; else s += '1'; if(f)T.add(s,1); else T.add(s,1000000000LL); s.pop_back(); } else f = true; s += (((r[i]>>j)&1) ^ ((rx[i]>>j)&1)) + '0'; } } swap(n,m); swap(rx,cx); swap(r,c); } long long ret = T.dfs() + n + m; if(ret>100000000)ret = -1; cout<<ret<<endl; } int main(){ int _t; cin>>_t; rep(_,_t){ solve(); } return 0; }