結果

問題 No.2505 matriX cOnstRuction
ユーザー 👑 potato167potato167
提出日時 2023-10-17 03:23:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 587 ms / 2,500 ms
コード長 3,297 bytes
コンパイル時間 2,757 ms
コンパイル使用メモリ 225,372 KB
実行使用メモリ 63,644 KB
最終ジャッジ日時 2023-10-17 03:23:49
合計ジャッジ時間 9,789 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 15 ms
4,348 KB
testcase_02 AC 14 ms
4,348 KB
testcase_03 AC 13 ms
4,348 KB
testcase_04 AC 13 ms
4,348 KB
testcase_05 AC 12 ms
4,348 KB
testcase_06 AC 14 ms
4,348 KB
testcase_07 AC 9 ms
4,348 KB
testcase_08 AC 9 ms
4,348 KB
testcase_09 AC 9 ms
4,348 KB
testcase_10 AC 10 ms
4,348 KB
testcase_11 AC 9 ms
4,348 KB
testcase_12 AC 9 ms
4,348 KB
testcase_13 AC 9 ms
4,348 KB
testcase_14 AC 9 ms
4,348 KB
testcase_15 AC 10 ms
4,348 KB
testcase_16 AC 9 ms
4,348 KB
testcase_17 AC 9 ms
4,348 KB
testcase_18 AC 9 ms
4,348 KB
testcase_19 AC 9 ms
4,348 KB
testcase_20 AC 10 ms
4,348 KB
testcase_21 AC 9 ms
4,348 KB
testcase_22 AC 10 ms
4,348 KB
testcase_23 AC 10 ms
4,348 KB
testcase_24 AC 9 ms
4,348 KB
testcase_25 AC 10 ms
4,348 KB
testcase_26 AC 9 ms
4,348 KB
testcase_27 AC 9 ms
4,348 KB
testcase_28 AC 10 ms
4,348 KB
testcase_29 AC 9 ms
4,348 KB
testcase_30 AC 16 ms
4,348 KB
testcase_31 AC 15 ms
4,348 KB
testcase_32 AC 14 ms
4,348 KB
testcase_33 AC 15 ms
4,348 KB
testcase_34 AC 21 ms
4,732 KB
testcase_35 AC 9 ms
4,348 KB
testcase_36 AC 20 ms
5,108 KB
testcase_37 AC 16 ms
4,452 KB
testcase_38 AC 30 ms
6,872 KB
testcase_39 AC 74 ms
14,264 KB
testcase_40 AC 22 ms
6,344 KB
testcase_41 AC 505 ms
59,156 KB
testcase_42 AC 25 ms
6,872 KB
testcase_43 AC 490 ms
61,364 KB
testcase_44 AC 516 ms
59,156 KB
testcase_45 AC 492 ms
61,144 KB
testcase_46 AC 530 ms
59,156 KB
testcase_47 AC 477 ms
61,364 KB
testcase_48 AC 121 ms
18,236 KB
testcase_49 AC 587 ms
63,644 KB
testcase_50 AC 537 ms
63,644 KB
testcase_51 AC 462 ms
4,348 KB
testcase_52 AC 105 ms
15,708 KB
testcase_53 AC 102 ms
13,484 KB
testcase_54 AC 99 ms
15,708 KB
testcase_55 AC 22 ms
4,348 KB
testcase_56 AC 21 ms
4,348 KB
testcase_57 AC 23 ms
4,348 KB
testcase_58 AC 25 ms
4,524 KB
testcase_59 AC 29 ms
4,940 KB
testcase_60 AC 31 ms
5,024 KB
testcase_61 AC 10 ms
4,348 KB
testcase_62 AC 11 ms
4,348 KB
testcase_63 AC 15 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
const ll ILL=2167167167167167167;
const int INF=2100000000;
const int mod=998244353;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p,int ty=0){
if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";}
else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(a&1),a>>=1;}return res;}



void solve();
// oddloop
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int t=1;
    cin>>t;
    rep(i,0,t) solve();
}

void solve(){
    int N,M;
    cin>>N>>M;
    vector<int> p(N),q(M);
    rep(i,0,N) cin>>p[i];
    rep(i,0,M) cin>>q[i];
    vector<vector<int>> A(N,vector<int>(M));
    rep(i,0,N) rep(j,0,M) cin>>A[i][j];
    vector<int> X(N),Y(M);
    Y=A[0];
    rep(i,0,N) X[i]=(A[i][0]^Y[0]);
    rep(i,0,N) rep(j,0,M){
        if(A[i][j]^X[i]^Y[j]){
            cout<<"-1\n";
            return;
        }
    }
    rep(i,0,M) p.push_back(q[i]),X.push_back(Y[i]);
    map<int,int> m;
    map<int,int> dp;
    rep(i,0,N+M){
        int tmp1=p[i],tmp2=X[i],tmp3=1;
        tmp1++;
        while(tmp3<tmp1) tmp3*=2;
        for(int d=29;d>=0;d--){
            if(tmp1&(1<<d)){
                int l=(((1<<30)-(1<<d))&(tmp1^tmp2^(1<<d)));
                int r=l+(1<<d);
                dp[l]--,dp[r]++;
            }
            if(tmp3&(1<<d)){
                int l=(((1<<30)-(1<<d))&(tmp3^tmp2^(1<<d)));
                int r=l+(1<<d);
                dp[l]+=2,dp[r]-=2;
                m[l]++,m[r]--;
            }
        }
        if(tmp1==(1<<30)) dp[0]--,dp[1<<30]++;
        if(tmp3==(1<<30)) dp[0]+=2,dp[1<<30]-=2,m[0]++,m[1<<30]--;
        dp[X[i]]--,dp[X[i]+1]++;
    }
    int ans=INF;
    int tmp=0;
    int v=0;
    for(auto x:dp){
        tmp+=m[x.first];
        v+=x.second;
        //cout<<x.first<<" "<<tmp<<" "<<v<<endl;
        if(tmp==N+M) chmin(ans,v);
    }
    if(ans!=INF) cout<<ans<<"\n";
    else cout<<"-1\n";
}
0