QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#59091#94. Good Old TablehhblwAC ✓100ms6008kbC++984.7kb2022-10-27 22:18:172022-10-27 22:18:19

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-27 22:18:19]
  • 评测
  • 测评结果:AC
  • 用时:100ms
  • 内存:6008kb
  • [2022-10-27 22:18:17]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <memory.h>

using namespace std;

const int N = 3000;

typedef unsigned int u32;

int n, m;
u32 a[N][(N+31)/32];
u32 sumCross[N][(N+31)/32];
int sumHor[N];
u32 sumVert[(N+31)/32];

inline void set(u32*const a, int n) {
    a[n/32] |= (1u<<(n&31));
}

void read() {
    scanf("%d%d\n", &n, &m);
    for (int i = 0; i < n; i++) {
        char buf[N*2+10];
        gets(buf);
        for (int j = 0; j < m; j++)
            if(buf[j*2] == '2')
                set((u32*)a[i], j);
    }
}

bool get(u32 *a, int n) {
    return (a[n/32] >> n) & 1;
}

int popcnt(u32 *a, int n) {
    int ans = 0;
    for(int i = 0; i < n / 32; ++i)
        ans += __builtin_popcount(a[i]);
    if(n&31)
        ans += __builtin_popcount(a[n/32]&((1<<(n&31))-1));
    return ans;
}

void xor2(u32 *a, u32 *b, int n) {
    for(int i = 0; i < n / 32; ++i)
        a[i] ^= b[i];
    if(n&31)
        a[n/32] ^= b[n/32]&((1<<(n&31))-1);
}

void xor3(u32 *a, u32 *b, u32 *c, int n) {
    for(int i = 0; i < n / 32; ++i)
        a[i] = b[i] ^ c[i];
    if(n&31)
        a[n/32] = (b[n/32] ^ c[n/32])&((1<<(n&31))-1);
}

void inverse(u32 *a, int n) {
    for(int i = 0; i < n/32; ++i)
        a[i] = ~a[i];
    if(n&31)
        a[n/32] ^= ((1<<(n&31))-1);
}

void findSumCross(int n, int m)  {
    memset(sumHor, 0, sizeof(sumHor));
    memset(sumVert, 0, sizeof(sumVert));
    for (int i = 0; i < n; i++) {
        sumHor[i] = popcnt(a[i], m);
        xor2(sumVert, a[i], m);
    }
    for (int i = 0; i < n; i++) {
        xor3(sumCross[i], a[i], sumVert, m);
        if(sumHor[i]&1)
            inverse(sumCross[i], m);
    }
}
void quit(int x) {
    cout << x << "\n";
    exit(0);
}
int main() {
    read();
    if (n == 1 || m == 1) {
        int kol[2] = {0, 0};
        for (int i = 0; i < n; i++) {
            int x = popcnt(a[i], m);
            kol[0] += m - x;
            kol[1] += x;
        }
        if (kol[0] && kol[1])
            quit(-1);
        else if (kol[0])
            quit(0);
        else
            quit(1);
    }
    if (!(n&1) && !(m&1)) {
        findSumCross(n, m);
        int ans = 0;
        for (int i = 0; i < n; i++)
            ans += popcnt(sumCross[i], m);
        quit(ans);
    }
    if (!(n&1) && (m&1)) {
        findSumCross(n, m-1);
        int kolOdd[n];
        memset(kolOdd, 0, sizeof(kolOdd));
        for (int i = 0; i < n; i++)
            kolOdd[i] += popcnt(sumCross[i], m - 1);
        int kolEq[2] = {0, 0};
        for (int i = 0; i < n; i++)
            kolEq[(kolOdd[i]&1)^get(a[i],m-1)]++;
        if (kolEq[0] && kolEq[1])
            quit(-1);
        vector<int> deltas;
        int ans = 1000000000;
        int ans1 = 0;
        for (int i = 0; i < n; i++)
            if (kolEq[0]) {
                ans1 += kolOdd[i];
                int d = m - 1 - 2*kolOdd[i] + 1;
                deltas.push_back(d);
            } else {
                ans1 += m - 1 - kolOdd[i];
                int d = 1 + 2*kolOdd[i] - m + 1;
                deltas.push_back(d);
            }
        if (kolEq[0])
            ans = min(ans, ans1);
        sort(deltas.begin(), deltas.end());
        for (int i = 0; i < n; i++) {
            ans1 += deltas[i];
            if (kolEq[(i+1)&1])
                ans = min(ans, ans1);
        }
        quit(ans);
    }
    if( (n&1) && !(m&1) ) {
        findSumCross(n-1, m);
        u32 kolOdd[m];
        memset(kolOdd, 0, sizeof(kolOdd));
        for (int i = 0; i+1 < n; i++)
            for (int j = 0; j < m; ++j)
                kolOdd[j] += get(sumCross[i], j);
        int kolEq[2] = {0, 0};
        for (int j = 0; j < m; j++)
            kolEq[(kolOdd[j]&1)^get(a[n-1],j)]++;
        if (kolEq[0] && kolEq[1])
            quit(-1);
        vector<int> deltas;
        int ans = 1000000000;
        int ans1 = 0;
        for (int j = 0; j < m; j++)
            if (kolEq[0]) {
                ans1 += kolOdd[j];
                int d = n - 1 - 2*kolOdd[j] + 1;
                deltas.push_back(d);
            } else {
                ans1 += n - 1 - kolOdd[j];
                int d = 1 + 2*kolOdd[j] - n + 1;
                deltas.push_back(d);
            }
        if (kolEq[0])
            ans = min(ans, ans1);
        sort(deltas.begin(), deltas.end());
        for (int i = 0; i < m; i++) {
            ans1 += deltas[i];
            if (kolEq[(i+1)&1])
                ans = min(ans, ans1);
        }
        quit(ans);
    }
    cout << "ERROR: m and n are odd\n";
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 79ms
memory: 5884kb

input:

3000 3000
2 1 2 1 2 1 1 1 1 2 1 1 2 2 1 2 2 2 2 1 1 1 2 1 1 2 2 2 2 2 2 1 1 1 1 2 1 2 1 2 1 2 2 2 2 1 1 2 2 1 1 1 1 2 1 2 1 2 1 2 1 1 1 2 1 1 2 1 2 2 1 1 1 2 2 2 2 1 1 2 2 2 2 2 2 2 1 2 1 1 1 2 2 1 1 1 1 1 2 2 2 2 1 2 2 1 2 1 1 2 2 2 1 1 1 2 1 1 2 1 2 2 2 1 2 2 1 1 2 1 2 2 1 1 1 1 2 1 2 2 1 1 2 2 2 ...

output:

4499318

result:

ok 1 number(s): "4499318"

Test #2:

score: 0
Accepted
time: 100ms
memory: 5952kb

input:

3000 3000
1 2 2 2 1 2 2 1 1 1 2 1 2 1 2 2 2 1 2 1 1 2 2 2 2 2 1 2 2 2 2 2 2 1 2 1 2 2 2 1 1 2 2 1 2 2 1 2 2 2 1 2 1 1 1 2 2 1 2 1 2 1 2 2 1 2 2 1 1 2 2 1 2 2 2 2 2 1 1 2 2 2 1 1 1 1 2 2 1 1 2 1 1 2 1 1 1 1 2 2 2 2 2 2 1 1 2 2 1 1 1 1 2 1 2 2 2 1 2 2 1 2 1 1 1 1 1 2 1 2 2 1 1 2 2 1 2 2 1 1 2 2 2 1 2 ...

output:

4500268

result:

ok 1 number(s): "4500268"

Test #3:

score: 0
Accepted
time: 66ms
memory: 5820kb

input:

3000 3000
1 1 2 2 2 2 1 2 1 1 2 1 2 2 2 2 1 2 1 1 1 1 1 2 2 1 2 2 2 2 2 1 1 2 2 2 2 2 1 2 1 2 1 1 2 2 2 1 2 2 2 1 2 1 1 2 2 2 1 2 2 1 2 2 2 1 2 2 2 2 1 1 1 1 1 2 1 2 1 1 2 2 2 2 2 2 2 1 2 1 2 1 1 2 1 2 1 1 1 2 2 2 1 2 1 1 2 1 1 2 1 1 1 2 2 1 2 2 2 2 2 2 2 1 2 2 2 1 1 1 1 1 2 2 2 2 2 2 1 1 2 2 2 2 2 ...

output:

4499488

result:

ok 1 number(s): "4499488"

Test #4:

score: 0
Accepted
time: 34ms
memory: 5880kb

input:

3000 3000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

output:

0

result:

ok 1 number(s): "0"

Test #5:

score: 0
Accepted
time: 19ms
memory: 5968kb

input:

3000 3000
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...

output:

9000000

result:

ok 1 number(s): "9000000"

Test #6:

score: 0
Accepted
time: 13ms
memory: 4208kb

input:

712 2467
2 1 2 2 2 2 2 2 2 1 1 1 1 2 1 2 2 2 1 1 2 2 1 1 2 1 2 2 1 2 1 2 2 1 1 2 2 2 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 2 2 2 1 1 1 1 1 2 1 2 2 2 2 1 1 2 2 1 1 1 1 2 1 1 1 1 2 1 1 1 2 2 2 2 2 2 1 2 1 1 1 1 1 2 2 2 1 2 2 2 2 1 1 1 1 1 2 2 2 2 1 1 1 1 2 2 1 1 2 1 2 1 1 1 1 1 2 2 2 2 2...

output:

299224

result:

ok 1 number(s): "299224"

Test #7:

score: 0
Accepted
time: 3ms
memory: 4304kb

input:

842 643
1 1 2 1 2 1 2 1 2 1 2 2 1 1 2 2 2 2 2 1 1 1 2 1 1 1 2 1 2 2 1 2 1 1 1 1 1 2 2 1 1 1 1 1 2 1 2 1 2 2 2 1 1 2 2 1 2 1 2 1 1 1 2 1 2 2 2 1 1 1 1 2 2 1 1 2 2 2 2 2 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 2 2 2 1 1 1 1 1 1 2 2 1 2 2 2 2 1 1 2 1 2 1 1 1 2 1 2 1 1 2 1 1 1 2 2 2 2 1 1 1 2 1 1 1 2 2 1 1 1 2 1 ...

output:

184027

result:

ok 1 number(s): "184027"

Test #8:

score: 0
Accepted
time: 14ms
memory: 3976kb

input:

463 1850
2 1 1 2 2 1 2 1 1 1 1 1 1 1 2 1 1 2 2 2 2 2 1 2 2 2 2 2 1 2 2 2 2 1 2 1 2 1 1 1 1 2 1 2 2 2 1 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 1 2 1 1 2 2 1 2 2 1 1 2 1 2 2 1 1 2 2 1 2 1 1 2 2 2 2 2 2 2 1 2 1 2 1 1 2 1 2 1 2 1 2 2 2 1 2 1 1 2 2 2 1 2 2 2 1 2 2 1 1 2 1 1 2 1 1 1 2 2 1 1 1 1 2 1 2 1 2 1 2 1 1 1...

output:

411193

result:

ok 1 number(s): "411193"

Test #9:

score: 0
Accepted
time: 3ms
memory: 3884kb

input:

287 820
1 2 1 2 1 1 1 2 2 1 2 2 1 2 2 2 2 1 1 2 2 2 2 1 1 2 1 1 1 1 1 1 2 1 1 2 1 2 1 1 1 1 2 1 1 2 2 2 2 2 2 1 1 1 2 1 2 2 2 2 2 1 1 2 2 1 1 1 1 2 2 1 1 2 2 1 1 2 1 1 2 2 1 2 1 2 1 1 1 1 2 2 1 2 1 1 1 1 2 1 2 1 1 2 1 1 2 2 1 2 1 1 1 1 2 2 2 1 1 1 2 2 1 1 2 2 1 2 1 2 2 1 2 1 2 1 2 2 2 2 2 1 1 2 2 1 ...

output:

75191

result:

ok 1 number(s): "75191"

Test #10:

score: 0
Accepted
time: 0ms
memory: 3852kb

input:

109 160
2 2 1 2 1 1 2 2 1 1 1 1 2 2 2 2 1 2 1 1 2 2 2 1 1 1 1 2 2 1 1 2 1 1 2 1 2 2 2 1 2 2 1 2 2 2 2 2 2 1 1 2 1 1 1 1 2 1 1 2 1 1 1 2 1 2 1 1 2 2 2 2 2 1 2 2 1 2 1 1 2 2 2 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 2 2 1 1 2 1 1 1 1 2 1 2 1 1 1 2 1 2 1 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 1 2 2 1 2 1 1 2 1 2 1 2 2 1 ...

output:

2630

result:

ok 1 number(s): "2630"

Test #11:

score: 0
Accepted
time: 2ms
memory: 4040kb

input:

565 558
2 1 1 2 2 1 2 2 2 2 2 1 2 2 2 2 1 1 2 1 1 1 1 1 2 2 1 2 1 1 2 1 1 1 2 2 2 2 1 1 2 1 1 1 1 1 2 2 2 2 2 2 1 1 1 2 1 1 2 1 1 1 2 1 1 1 1 1 1 2 2 2 1 2 1 1 1 1 1 2 1 2 2 2 1 1 2 2 2 1 2 1 2 1 2 2 2 1 1 1 1 1 2 2 1 1 1 2 1 2 1 1 1 2 1 2 2 2 2 2 1 2 1 1 2 1 2 2 1 1 2 2 1 2 1 1 1 1 1 2 2 1 2 1 2 1 ...

output:

-1

result:

ok 1 number(s): "-1"

Test #12:

score: 0
Accepted
time: 3ms
memory: 4376kb

input:

1918 1
1
1
1
2
2
1
2
2
2
2
1
1
1
1
1
1
1
1
1
1
2
2
2
1
2
1
2
2
1
2
2
1
1
2
2
2
1
1
2
2
1
2
1
2
1
2
1
2
1
1
1
2
1
2
2
2
1
2
2
2
1
2
1
1
1
1
1
1
2
1
1
2
2
1
1
1
2
1
2
2
1
2
2
1
1
2
2
2
1
2
1
1
2
1
2
1
2
2
2
2
2
1
1
1
1
2
1
2
1
1
1
1
1
2
2
1
1
2
2
2
2
2
1
2
1
1
1
1
2
1
2
1
1
1
2
1
2
2
1
1
1
2
1
2
1
1
2...

output:

-1

result:

ok 1 number(s): "-1"

Test #13:

score: 0
Accepted
time: 3ms
memory: 5004kb

input:

1806 2
2 2
1 2
2 1
2 1
2 2
2 1
2 1
2 1
2 2
2 1
1 1
2 1
2 1
1 1
1 1
1 1
1 1
1 1
2 2
1 2
1 2
1 2
1 2
1 2
1 1
1 2
2 1
2 1
1 1
1 2
2 2
2 2
2 2
2 2
1 1
2 2
2 1
2 2
2 1
2 1
1 1
1 2
1 2
1 1
2 2
2 2
2 2
2 1
2 2
1 2
2 2
1 1
2 2
2 2
1 2
1 2
1 1
2 1
2 1
1 1
1 1
2 1
2 1
2 2
1 1
2 1
1 2
2 2
2 2
1 1
1 1
1 1
1 2
2...

output:

1758

result:

ok 1 number(s): "1758"

Test #14:

score: 0
Accepted
time: 1ms
memory: 4804kb

input:

1544 3
1 2 2
1 1 1
2 1 1
1 2 2
2 1 1
1 2 1
2 1 1
1 2 1
2 2 1
1 2 2
1 2 2
1 2 2
1 1 2
1 2 1
2 1 2
1 2 2
1 1 2
2 1 1
2 1 1
1 2 1
1 1 1
2 2 1
2 1 1
1 2 1
1 1 1
1 1 1
1 1 2
2 1 1
2 2 2
2 2 1
1 1 1
2 1 2
1 1 2
2 2 1
1 1 1
2 1 2
1 2 2
1 1 2
1 2 1
1 2 2
2 2 1
1 1 1
1 2 2
1 2 1
1 2 1
1 1 2
1 1 2
2 2 2
1 2 1...

output:

-1

result:

ok 1 number(s): "-1"

Test #15:

score: 0
Accepted
time: 2ms
memory: 3688kb

input:

3 2
2 1
2 1
1 1

output:

2

result:

ok 1 number(s): "2"

Test #16:

score: 0
Accepted
time: 2ms
memory: 3532kb

input:

4 1
2
2
2
2

output:

1

result:

ok 1 number(s): "1"

Test #17:

score: 0
Accepted
time: 3ms
memory: 3768kb

input:

3 2
1 2
1 2
1 1

output:

2

result:

ok 1 number(s): "2"

Test #18:

score: 0
Accepted
time: 2ms
memory: 3756kb

input:

1 2
1 1

output:

0

result:

ok 1 number(s): "0"

Test #19:

score: 0
Accepted
time: 2ms
memory: 3624kb

input:

3 2
1 2
1 2
1 1

output:

2

result:

ok 1 number(s): "2"

Test #20:

score: 0
Accepted
time: 97ms
memory: 5936kb

input:

3000 3000
1 1 1 1 1 1 2 1 2 1 1 2 1 2 1 1 2 2 2 1 2 1 1 2 1 1 2 1 1 2 1 1 1 2 2 1 1 1 1 2 1 2 2 2 2 1 1 2 2 2 1 2 2 2 1 1 2 1 1 2 2 2 2 1 2 1 1 1 1 2 2 2 2 1 2 2 1 1 1 2 2 2 1 2 2 1 1 1 1 2 1 2 1 2 2 2 1 1 1 1 1 2 2 1 2 2 1 1 1 1 1 1 1 2 2 1 1 2 1 1 1 2 2 2 2 1 2 2 1 1 1 1 1 2 1 2 2 1 2 1 2 2 2 2 1 ...

output:

4499996

result:

ok 1 number(s): "4499996"

Test #21:

score: 0
Accepted
time: 81ms
memory: 5776kb

input:

2999 3000
1 2 1 1 2 1 2 1 2 1 2 1 1 2 1 2 1 1 1 1 2 1 2 1 1 1 1 2 2 1 2 1 1 1 1 1 2 2 1 2 1 2 1 2 2 2 2 2 2 1 2 2 2 1 2 1 1 2 2 2 2 1 1 2 2 2 2 2 1 2 1 2 2 1 2 2 1 2 1 1 1 2 2 1 2 1 2 1 2 1 1 2 1 1 2 2 2 1 1 2 2 1 2 1 1 2 1 1 2 1 2 1 2 2 1 1 1 1 2 2 2 2 2 1 1 1 1 2 2 1 1 2 1 2 1 2 2 1 2 1 1 1 1 2 2 ...

output:

4423060

result:

ok 1 number(s): "4423060"

Test #22:

score: 0
Accepted
time: 84ms
memory: 6008kb

input:

3000 2999
2 2 2 1 2 1 1 2 2 1 1 2 1 1 2 1 1 2 2 1 2 1 2 1 2 1 2 1 1 2 2 1 1 2 2 1 2 2 2 1 2 2 2 2 1 1 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 1 1 2 2 1 1 1 1 1 2 2 2 2 1 2 1 2 2 1 1 1 2 2 2 2 1 2 1 2 1 1 2 2 1 2 1 2 2 1 1 1 1 2 2 2 2 2 1 1 2 1 2 2 1 2 2 2 1 2 2 1 1 1 1 2 1 2 2 2 1 1 2 2 2 2 2 1 1 2 1 1 2 2 1 ...

output:

4434271

result:

ok 1 number(s): "4434271"