結果

問題 No.2505 matriX cOnstRuction
ユーザー 沙耶花沙耶花
提出日時 2023-10-13 23:05:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 990 ms / 2,500 ms
コード長 3,440 bytes
コンパイル時間 6,001 ms
コンパイル使用メモリ 270,988 KB
実行使用メモリ 138,964 KB
最終ジャッジ日時 2023-10-13 23:06:07
合計ジャッジ時間 17,097 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 107 ms
4,352 KB
testcase_02 AC 83 ms
4,352 KB
testcase_03 AC 85 ms
4,352 KB
testcase_04 AC 87 ms
4,352 KB
testcase_05 AC 87 ms
4,348 KB
testcase_06 AC 115 ms
4,348 KB
testcase_07 AC 40 ms
4,352 KB
testcase_08 AC 39 ms
4,348 KB
testcase_09 AC 39 ms
4,352 KB
testcase_10 AC 37 ms
4,352 KB
testcase_11 AC 39 ms
4,348 KB
testcase_12 AC 38 ms
4,348 KB
testcase_13 AC 37 ms
4,348 KB
testcase_14 AC 39 ms
4,348 KB
testcase_15 AC 38 ms
4,352 KB
testcase_16 AC 38 ms
4,348 KB
testcase_17 AC 38 ms
4,352 KB
testcase_18 AC 38 ms
4,348 KB
testcase_19 AC 38 ms
4,348 KB
testcase_20 AC 37 ms
4,352 KB
testcase_21 AC 38 ms
4,348 KB
testcase_22 AC 39 ms
4,348 KB
testcase_23 AC 39 ms
4,348 KB
testcase_24 AC 39 ms
4,348 KB
testcase_25 AC 37 ms
4,352 KB
testcase_26 AC 37 ms
4,348 KB
testcase_27 AC 40 ms
4,348 KB
testcase_28 AC 39 ms
4,352 KB
testcase_29 AC 40 ms
4,352 KB
testcase_30 AC 25 ms
4,348 KB
testcase_31 AC 24 ms
4,352 KB
testcase_32 AC 22 ms
4,348 KB
testcase_33 AC 24 ms
4,348 KB
testcase_34 AC 36 ms
6,372 KB
testcase_35 AC 20 ms
5,376 KB
testcase_36 AC 33 ms
8,408 KB
testcase_37 AC 30 ms
6,236 KB
testcase_38 AC 43 ms
12,344 KB
testcase_39 AC 101 ms
36,396 KB
testcase_40 AC 38 ms
13,164 KB
testcase_41 AC 602 ms
135,460 KB
testcase_42 AC 41 ms
12,596 KB
testcase_43 AC 544 ms
138,656 KB
testcase_44 AC 613 ms
135,572 KB
testcase_45 AC 591 ms
138,964 KB
testcase_46 AC 572 ms
135,216 KB
testcase_47 AC 563 ms
137,304 KB
testcase_48 AC 516 ms
135,224 KB
testcase_49 AC 561 ms
136,232 KB
testcase_50 AC 619 ms
135,676 KB
testcase_51 AC 990 ms
4,352 KB
testcase_52 AC 294 ms
73,236 KB
testcase_53 AC 298 ms
70,048 KB
testcase_54 AC 299 ms
73,588 KB
testcase_55 AC 42 ms
4,352 KB
testcase_56 AC 37 ms
4,348 KB
testcase_57 AC 36 ms
4,352 KB
testcase_58 AC 39 ms
6,592 KB
testcase_59 AC 42 ms
8,104 KB
testcase_60 AC 47 ms
8,844 KB
testcase_61 AC 40 ms
4,736 KB
testcase_62 AC 37 ms
4,700 KB
testcase_63 AC 25 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0