結果

問題 No.2505 matriX cOnstRuction
ユーザー 👑 potato167potato167
提出日時 2023-10-17 03:25:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 611 ms / 2,500 ms
コード長 1,717 bytes
コンパイル時間 2,609 ms
コンパイル使用メモリ 222,468 KB
実行使用メモリ 63,772 KB
最終ジャッジ日時 2023-10-17 03:25:58
合計ジャッジ時間 11,105 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 41 ms
4,348 KB
testcase_02 AC 38 ms
4,348 KB
testcase_03 AC 38 ms
4,348 KB
testcase_04 AC 38 ms
4,348 KB
testcase_05 AC 38 ms
4,348 KB
testcase_06 AC 37 ms
4,348 KB
testcase_07 AC 27 ms
4,348 KB
testcase_08 AC 28 ms
4,348 KB
testcase_09 AC 29 ms
4,348 KB
testcase_10 AC 28 ms
4,348 KB
testcase_11 AC 28 ms
4,348 KB
testcase_12 AC 29 ms
4,348 KB
testcase_13 AC 27 ms
4,348 KB
testcase_14 AC 28 ms
4,348 KB
testcase_15 AC 28 ms
4,348 KB
testcase_16 AC 28 ms
4,348 KB
testcase_17 AC 28 ms
4,348 KB
testcase_18 AC 28 ms
4,348 KB
testcase_19 AC 28 ms
4,348 KB
testcase_20 AC 28 ms
4,348 KB
testcase_21 AC 33 ms
4,348 KB
testcase_22 AC 30 ms
4,348 KB
testcase_23 AC 29 ms
4,348 KB
testcase_24 AC 29 ms
4,348 KB
testcase_25 AC 29 ms
4,348 KB
testcase_26 AC 29 ms
4,348 KB
testcase_27 AC 28 ms
4,348 KB
testcase_28 AC 29 ms
4,348 KB
testcase_29 AC 28 ms
4,348 KB
testcase_30 AC 22 ms
4,348 KB
testcase_31 AC 21 ms
4,348 KB
testcase_32 AC 20 ms
4,348 KB
testcase_33 AC 22 ms
4,348 KB
testcase_34 AC 30 ms
4,864 KB
testcase_35 AC 19 ms
4,472 KB
testcase_36 AC 31 ms
5,140 KB
testcase_37 AC 30 ms
4,500 KB
testcase_38 AC 42 ms
7,260 KB
testcase_39 AC 92 ms
14,656 KB
testcase_40 AC 35 ms
6,876 KB
testcase_41 AC 611 ms
59,284 KB
testcase_42 AC 41 ms
7,396 KB
testcase_43 AC 512 ms
61,492 KB
testcase_44 AC 557 ms
59,284 KB
testcase_45 AC 508 ms
61,228 KB
testcase_46 AC 549 ms
59,288 KB
testcase_47 AC 509 ms
61,492 KB
testcase_48 AC 136 ms
18,364 KB
testcase_49 AC 603 ms
63,772 KB
testcase_50 AC 577 ms
63,772 KB
testcase_51 AC 573 ms
4,348 KB
testcase_52 AC 127 ms
15,820 KB
testcase_53 AC 134 ms
13,652 KB
testcase_54 AC 137 ms
15,820 KB
testcase_55 AC 35 ms
4,348 KB
testcase_56 AC 33 ms
4,348 KB
testcase_57 AC 34 ms
4,348 KB
testcase_58 AC 34 ms
4,600 KB
testcase_59 AC 38 ms
4,972 KB
testcase_60 AC 41 ms
5,056 KB
testcase_61 AC 22 ms
4,348 KB
testcase_62 AC 23 ms
4,348 KB
testcase_63 AC 22 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF=2100000000;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
void solve();
int main() {
    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