QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#465073#5523. Graph Problem With Small $n$BalintRML 924ms139460kbC++204.3kb2024-07-06 17:11:162024-07-06 17:11:17

Judging History

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

  • [2024-07-06 17:11:17]
  • 评测
  • 测评结果:ML
  • 用时:924ms
  • 内存:139460kb
  • [2024-07-06 17:11:16]
  • 提交

answer

#include <stdio.h>
using namespace std;
#pragma GCC target "avx2"
#pragma GCC optimize "Ofast"
#include <immintrin.h>

typedef __m256i m256;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define fs first
#define sn second
#define ALL(v) begin(v), end(v)
#define SZ(v) ((int) (v).size())
#define lbv(v, x) (lower_bound(ALL(v), x) - (v).begin())
#define ubv(v, x) (upper_bound(ALL(v), x) - (v).begin())
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
#define FR(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) {cerr << #x << ' ' << x << endl;}
#define dbgArr(arr, n) {cerr << #arr; FR(_i, n) cerr << ' ' << (arr)[_i]; cerr << endl;}

#define vload(ptr) _mm256_loadu_si256((m256*) (ptr))
#define vstore(ptr, v) _mm256_storeu_si256((m256*) (ptr), (v))
#define vand(a, b) _mm256_and_si256(a, b)
#define vor(a, b) _mm256_or_si256(a, b)
#define vxor(a, b) _mm256_xor_si256(a, b)
#define vblendPs(a, b, c) (m256) _mm256_blendv_ps((__m256) (a), (__m256) (b), (__m256) (c))

const int C24_11 = 2496144;
const int C24_12 = 2704156;
const int MASK = (1 << 24) - 1;
const int MN = 24;
int n, mask;
int adjMat[MN], iAdjMat[MN];
bool ans[MN][MN];
int dpMem[C24_11+C24_12][8], (*dpMem1)[8] = dpMem, (*dpMem2)[8] = dpMem+C24_11;
int byPop[MN+2];
char mp[3<<MN], popMem[3<<MN];

#define atMp(i) (*(int*) (mp + (i)*3))
#define atPopMem(i) (*(int*) (popMem + (i)*3))

void solve(int pop, int oldDp[][8], int newDp[][8]){
    m256 zeroV = _mm256_setzero_si256();
    m256 allOne = _mm256_set1_epi32(-1);

    m256 vAdj[MN];
    FR(i, MN) vAdj[i] = _mm256_set1_epi32(adjMat[i]);

    FR(i1, byPop[pop+1]-byPop[pop]){
        int s1 = atPopMem(byPop[pop]+i1) & MASK;
        m256 nsv = vxor(_mm256_set1_epi32(s1), allOne);
        m256 tmp = vand(vload(oldDp[i1]), nsv);
        vstore(oldDp[i1], zeroV);

        #define proc(n2){ \
            int s2 = s1 | (1 << n2); \
            int i2 = atMp(s2) & MASK; \
            m256 oldV = vload(newDp[i2]); \
            m256 newV = vor(oldV, vAdj[n2]); \
            m256 maskV = _mm256_slli_epi32(tmp, 31-n2); \
            vstore(newDp[i2], vblendPs(oldV, newV, maskV)); \
        }

        proc(0);
        proc(1);
        proc(2);
        proc(3);
        proc(4);
        proc(5);
        proc(6);
        proc(7);
        proc(8);
        proc(9);
        proc(10);
        proc(11);
        proc(12);
        proc(13);
        proc(14);
        proc(15);
        proc(16);
        proc(17);
        proc(18);
        proc(19);
        proc(20);
        proc(21);
        proc(22);
        proc(23);
    }
}

void solve(int *ord){
    FR(i, n) adjMat[i] = 0;
    FR(i, n) FR(j, n) adjMat[i] |= ((iAdjMat[ord[i]] >> ord[j]) & 1) << j;

    int (*oldMem)[8] = dpMem2, (*newMem)[8] = dpMem1;

    FR(n1, 8) oldMem[0][n1] = adjMat[n1];

    FR(pop, n-2){
        solve(pop, oldMem, newMem);
        auto tmp = oldMem;
        oldMem = newMem, newMem = tmp;
    }

    FR(i, 8) FR(j, n) if(i != j){
        ans[ord[i]][ord[j]] = (oldMem[atMp(mask^(1<<i)^(1<<j)) & MASK][i] >> j) & 1;
    }

    m256 zeroV = _mm256_setzero_si256();
    FR(i, byPop[n-1]-byPop[n-2]) vstore(oldMem+i, zeroV);
}

int choose[MN+1][MN+1];

void init(){
    FR(a, MN+1){
        choose[a][0] = 1;
        FOR(b, 1, a+1) choose[a][b] = choose[a-1][b-1] + choose[a-1][b];
    }
    FR(i, n+1) byPop[i+1] = byPop[i] + choose[n][i];

    int ind[MN+1] = {};

    FR(s, 1<<n){
        int pop = __builtin_popcount(s);
        atMp(s) |= ind[pop];
        atPopMem(byPop[pop] + ind[pop]++) |= s;
    }
}

int main(){
    scanf("%d\n", &n);
    FR(i, n){
        FR(j, n) iAdjMat[i] |= (getchar()-'0') << j;
        getchar();
    }

    //n = 24;
    //FR(i, n) FR(j, i) if(rng() & 1) iAdjMat[i] |= 1 << j, iAdjMat[j] |= 1 << i;

    mask = (1<<n)-1;
    init();

    int ord[MN];
    FR(i, MN) ord[i] = i;
    solve(ord);
    FR(i, n) ord[i] = (i+8) % n;
    solve(ord);
    FR(i, n) ord[i] = (i+16) % n;
    solve(ord);

    FR(i, n){
        FR(j, n) putchar('0'+ans[i][j]);
        putchar('\n');
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4
0110
1010
1101
0010

output:

0001
0001
0000
1100

result:

ok 4 lines

Test #2:

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

input:

6
010001
101000
010100
001010
000101
100010

output:

010001
101000
010100
001010
000101
100010

result:

ok 6 lines

Test #3:

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

input:

4
0111
1011
1101
1110

output:

0111
1011
1101
1110

result:

ok 4 lines

Test #4:

score: 0
Accepted
time: 909ms
memory: 137368kb

input:

23
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
000000000...

output:

00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
000000000000...

result:

ok 23 lines

Test #5:

score: 0
Accepted
time: 903ms
memory: 139460kb

input:

23
00010100000000000101000
00000000010000000001000
00000000000001000000001
10000000000000000010000
00000000000000000000000
10000000000000000000000
00000001000000000000000
00000010000000000010000
00000000000001000000000
01000000000000000000000
00000000000000000000000
00000000000000000000000
000000000...

output:

00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
000000000000...

result:

ok 23 lines

Test #6:

score: 0
Accepted
time: 909ms
memory: 139344kb

input:

23
00001000000000000000000
00001000010001000000000
00000000000101000010000
00001000000100000000000
11010000010011000100000
00000000000100000000000
00000000000000000000001
00000000000000000101000
00000000000000000000000
01001000000000101010010
00000000000000000000101
00110100000010001000000
000010000...

output:

00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
000000000000...

result:

ok 23 lines

Test #7:

score: 0
Accepted
time: 902ms
memory: 139356kb

input:

23
01000000000001101001100
10000001101000000000000
00000100000100010000100
00000000000000001011000
00000100001000000000000
00101000000000001000001
00000000000000000000000
01000000000000000000000
01000000000100000010000
00000000000001000000011
01001000000000010000000
00100000100001000100001
000000000...

output:

00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
00000000000000000000000
000000000000...

result:

ok 23 lines

Test #8:

score: 0
Accepted
time: 911ms
memory: 139292kb

input:

23
00000000010001001001010
00100010001101110000001
01000001000100110000000
00000011010001101100100
00000000010000010001000
00000000000000001001000
01010001000000000000001
00110010000000000000010
00000000011000100100000
10011000101000100000000
01000000110010101010000
01100000000000000000000
000000000...

output:

01111111110111110110111
10011111110111110110111
10011111110111110110111
11101111110110110110111
11110111110111110110111
11111011110111111111111
11111101110111110110111
11111110110111110110111
11111111010111110110111
11111111100110100110111
00000000000010000010100
11111111110011110110111
111111111111...

result:

ok 23 lines

Test #9:

score: 0
Accepted
time: 891ms
memory: 139388kb

input:

23
00001000001001000000000
00101100111110100000000
01001000100001011010000
00000000010000010010000
11100001100001000000010
01000010101010100011011
00000100000100100010000
00001000011000000010001
01101100000000011001001
01010001000010011000000
11000101000110001100000
01000010001000000000010
010001000...

output:

00000000000000000000100
00000000000000000000100
00000000000000000000100
00000000000000000000100
00000000000000000000100
00000000000000000000100
00000000000000000000100
00000000000000000000100
00000000000000000000100
00000000000000000000100
00000000000000000000100
00000000000000000000100
000000000000...

result:

ok 23 lines

Test #10:

score: 0
Accepted
time: 924ms
memory: 139356kb

input:

23
00001011110010000000001
00000100000011000000100
00010011010100000000011
00100011011001010100100
10000101000110100000000
01001000001010001000100
10110000000110000010000
10111000001100010100010
10000000000010001000110
10110000001110100110001
00010101010100001000000
00101011011000100100011
110011101...

output:

00000000000000000001000
00000000000000000001000
00000000000000000001000
00000000000000000001000
00000000000000000001000
00000000000000000001000
00000000000000000001000
00000000000000000001000
00000000000000000001000
00000000000000000001000
00000000000000000001000
00000000000000000001000
000000000000...

result:

ok 23 lines

Test #11:

score: 0
Accepted
time: 905ms
memory: 139436kb

input:

23
00100100001000000100001
00101110110000100100001
11000000000101001000100
00000000010000001111010
01000011010001011001010
11000000010100001001011
01001000001010101000100
00001000001010000000000
01000000000001100001011
01011100001101100000000
10000011010010100000010
00100100010000000001000
000000110...

output:

01111111111111111111111
10111111111111111111111
11011111111111111111111
11101111111111111111111
11110111111111111111111
11111011111111111111111
11111101111111111111111
11111110111111111111111
11111111011111111111111
11111111101111111111111
11111111110111111111111
11111111111011111111111
111111111111...

result:

ok 23 lines

Test #12:

score: 0
Accepted
time: 901ms
memory: 139448kb

input:

23
00000001011001011100100
00000001010000000010100
00000001010010100010000
00001000100111100000000
00010100011000010111001
00001000100001000010010
00000001111001100011000
11100010111100110001001
00010111010000101100110
11101011100000100100100
10001011000010100000010
00010001000001011101110
001100000...

output:

01111111111111111111111
10111111111111111111111
11011111111111111111111
11101111111111111111111
11110111111111111111111
11111011111111111111111
11111101111111111111111
11111110111111111111111
11111111011111111111111
11111111101111111111111
11111111110111111111111
11111111111011111111111
111111111111...

result:

ok 23 lines

Test #13:

score: 0
Accepted
time: 903ms
memory: 139424kb

input:

23
00100100001101000100000
00010111011000100000010
10000010010001111000010
01001011101001000100000
00010000010110000100111
11000000101000011101001
01110001100000010101100
01010010001001010100000
00010110001100010010001
01101000000011000111000
11010101100010010001101
10001000100010001110100
000010000...

output:

01111111111111111111111
10111111111111111111111
11011111111111111111111
11101111111111111111111
11110111111111111111111
11111011111111111111111
11111101111111111111111
11111110111111111111111
11111111011111111111111
11111111101111111111111
11111111110111111111111
11111111111011111111111
111111111111...

result:

ok 23 lines

Test #14:

score: 0
Accepted
time: 892ms
memory: 139352kb

input:

23
01001101001011010101100
10001010111100100001110
00000000010101000111100
00000000001010010100010
11000000100110000111000
10000010001000010101000
01000100100010001100101
10000000010001000110110
01001010000001111100000
01100001000001001101001
11010100000011001010111
01101000000000100100110
100110100...

output:

01111111111111111111111
10111111111111111111111
11011111111111111111111
11101111111111111111111
11110111111111111111111
11111011111111111111111
11111101111111111111111
11111110111111111111111
11111111011111111111111
11111111101111111111111
11111111110111111111111
11111111111011111111111
111111111111...

result:

ok 23 lines

Test #15:

score: 0
Accepted
time: 909ms
memory: 139424kb

input:

23
01100101000101001000001
10111000100000010110010
11011010011101000010010
01100010001111011011111
01100010011110001111100
10000000011000001011010
00111001001000101100111
10000010110110011000000
01000001001110010100100
00101101000100111100001
00111110100110011011010
10111001111010001010000
000110011...

output:

01111111111111111111111
10111111111111111111111
11011111111111111111111
11101111111111111111111
11110111111111111111111
11111011111111111111111
11111101111111111111111
11111110111111111111111
11111111011111111111111
11111111101111111111111
11111111110111111111111
11111111111011111111111
111111111111...

result:

ok 23 lines

Test #16:

score: 0
Accepted
time: 887ms
memory: 139336kb

input:

23
01111001001110001000101
10000000000100111111110
10010101000110100100101
10101000001010010101001
10010010011110101101111
00100000101100011000000
00001000011000010001101
10100000010000000000100
00000100010110001111100
00001011100101010110111
10011110000000010101101
11101100110010010100000
101110001...

output:

01111111111111111111111
10111111111111111111111
11011111111111111111111
11101111111111111111111
11110111111111111111111
11111011111111111111111
11111101111111111111111
11111110111111111111111
11111111011111111111111
11111111101111111111111
11111111110111111111111
11111111111011111111111
111111111111...

result:

ok 23 lines

Test #17:

score: 0
Accepted
time: 909ms
memory: 139440kb

input:

23
01010000000100001110001
10000110010001110010100
00011000101111001010110
10101111011100101100111
00110011111111111011000
01010011100001111011011
01011100001111000011101
00011100001110111010010
00101100011101001000011
01011000100000000000010
00111011100001001000111
10111011100000110100001
001010110...

output:

01111111111111111111111
10111111111111111111111
11011111111111111111111
11101111111111111111111
11110111111111111111111
11111011111111111111111
11111101111111111111111
11111110111111111111111
11111111011111111111111
11111111101111111111111
11111111110111111111111
11111111111011111111111
111111111111...

result:

ok 23 lines

Test #18:

score: 0
Accepted
time: 909ms
memory: 139456kb

input:

23
00100101011111000101011
00100001000110000111101
11010000011110010110011
00101101110110000101110
00010101111001110101110
10011001111101010011101
00000000110011000000100
11011100110111001110110
00011111000000100110010
10111111000111101010010
10101100000111111010100
11110101011010101010010
111100110...

output:

01111111111111111111111
10111111111111111111111
11011111111111111111111
11101111111111111111111
11110111111111111111111
11111011111111111111111
11111101111111111111111
11111110111111111111111
11111111011111111111111
11111111101111111111111
11111111110111111111111
11111111111011111111111
111111111111...

result:

ok 23 lines

Test #19:

score: 0
Accepted
time: 913ms
memory: 137448kb

input:

23
00100001100011011101011
00111011101101101011001
11001101100100001000111
01001010110101101000010
01110011001111000101111
00100011100000111101010
01011100000110101001100
11101100010110110011000
11110100010010000011010
00010001101011011011011
01001000010001001011111
01111011000010101011101
100010111...

output:

01111111111111111111111
10111111111111111111111
11011111111111111111111
11101111111111111111111
11110111111111111111111
11111011111111111111111
11111101111111111111111
11111110111111111111111
11111111011111111111111
11111111101111111111111
11111111110111111111111
11111111111011111111111
111111111111...

result:

ok 23 lines

Test #20:

score: -100
Memory Limit Exceeded

input:

24
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
0000000000000000000000...

output:

000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
000000000000000000000000
...

result: