QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#80484#5442. Referee Without RedAPJifengcWA 430ms167604kbC++204.1kb2023-02-23 22:14:572023-02-23 22:14:59

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-23 22:14:59]
  • 评测
  • 测评结果:WA
  • 用时:430ms
  • 内存:167604kb
  • [2023-02-23 22:14:57]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3000005, P = 998244353;
int T, n, m;
int a[MAXN];
vector<int> vn[MAXN], vm[MAXN];
int cn, cm;
bool vis[MAXN];
int p[MAXN], qn[MAXN], qm[MAXN];
int fa[MAXN];
int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}
bool merge(int x, int y) {
    x = find(x), y = find(y);
    if (x != y) {
        fa[x] = y;
        return true;
    } else {
        return false;
    }
}
int inv[MAXN];
int lcm(int a, int b) { return a / __gcd(a, b) * b; }
int f(int x, int y) { return a[(x - 1) * m + (y - 1)]; }
int s[MAXN], nxt[MAXN];
int kmp(int n) {
    for (int i = 2, j = 0; i <= n; i++) {
        while (j && s[j + 1] != s[i]) j = nxt[j];
        if (s[j + 1] == s[i]) j++;
        nxt[i] = j;
    }
    for (int j = nxt[n]; j; j = nxt[j]) if (n % (n - j) == 0)
        return n - j;
    return n;
}
int cnt[MAXN];
int main() {
    inv[1] = 1;
    for (int i = 2; i <= 3000000; i++)
        inv[i] = P - 1ll * (P / i) * inv[P % i] % P;
    scanf("%d", &T);
    while (T--) {
        cn = cm = 0;
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n * m; i++)
            scanf("%d", &a[i]);
        int o = 0;
        for (int i = 1; i <= n; i += 2) 
            p[i] = ++o, vis[i] = 0;
        for (int i = 2; i <= n; i += 2)
            p[i] = ++o, vis[i] = 0;
        for (int i = 1; i <= n; i++) if (!vis[i]) {
            vn[++cn] = { i };
            for (int j = p[i]; j != i; j = p[j]) {
                vn[cn].push_back(j);
                vis[j] = 1;
            }
        }
        o = 0;
        for (int i = 1; i <= m; i += 2) 
            p[i] = ++o, vis[i] = 0;
        for (int i = 2; i <= m; i += 2)
            p[i] = ++o, vis[i] = 0;
        for (int i = 1; i <= m; i++) if (!vis[i]) {
            vm[++cm] = { i };
            for (int j = p[i]; j != i; j = p[j]) {
                vm[cm].push_back(j);
                vis[j] = 1;
            }
        }
        int ans = 1;
        for (int i = 1; i <= cn; i++) if (vn[i].size() == 1) {
            int w = 1;
            for (int j = 1; j <= cm; j++) {
                int len = 0;
                for (int k : vm[j]) 
                    s[++len] = f(vn[i][0], k);
                w = lcm(w, kmp(len));
            }
            ans = 1ll * ans * w % P;
        }
        for (int i = 1; i <= cm; i++) if (vm[i].size() == 1) {
            int w = 1;
            for (int j = 1; j <= cn; j++) {
                int len = 0;
                for (int k : vn[j]) 
                    s[++len] = f(k, vm[i][0]);
                w = lcm(w, kmp(len));
            }
            ans = 1ll * ans * w % P;
        }
        // printf("ans = %d\n", ans);
        for (int i = 0; i <= cn + cm; i++)
            fa[i] = i;
        for (int i = 1; i <= cn; i++) if (vn[i].size() != 1) {
            for (int j = 1; j <= cm; j++) if (vm[j].size() != 1) {
                bool diff = true;
                int cc = 0;
                for (int x : vn[i]) {
                    for (int y : vm[j]) {
                        // printf("%d, %d\n", x, y);
                        cnt[f(x, y)]++;
                        if (cnt[f(x, y)] > 1) diff = false;
                        ans = 1ll * ans * (++cc) % P * inv[cnt[f(x, y)]] % P;
                    }
                }
                if (diff) {
                    // ans = 1ll * ans * ((P + 1) / 2) % P;
                    int u = (vn[i].size() & 1) ? 0 : cn + j;
                    int v = (vm[i].size() & 1) ? 0 : i;
                    if (!merge(u, v)) ans = 1ll * ans * inv[2] % P;
                }
                for (int x : vn[i]) {
                    for (int y : vm[j]) {
                        cnt[f(x, y)]--;
                    }
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

/*
2
4 4
1 2 3 4
3 4 1 2
1 2 4 1
4 3 3 2

3 9
1 8 1 1 8 1 1 8 1
1 8 8 8 8 8 8 8 1
1 1 1 8 8 8 1 1 1

1 8 1 1 8 1 1 8 1 
1 8 8 8 8 8 8 8 1 
1 1 1 8 8 8 1 1 1 

1 8 1 8 1 1 1 1 8 
1 8 8 8 8 8 1 8 8 
1 1 1 1 8 1 1 8 8 



*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 61ms
memory: 156124kb

input:

2
4 4
1 2 3 4
3 4 1 2
1 2 4 1
4 3 3 2
3 9
1 8 1 1 8 1 1 8 1
1 8 8 8 8 8 8 8 1
1 1 1 8 8 8 1 1 1

output:

96
6336

result:

ok 2 number(s): "96 6336"

Test #2:

score: 0
Accepted
time: 64ms
memory: 155968kb

input:

1
18 16
8 8 1 1 8 8 8 1 8 8 8 1 8 8 8 1
8 1 8 1 8 1 1 1 8 1 1 1 8 1 1 1
8 8 8 1 8 8 8 1 8 8 8 1 8 8 8 1
8 8 1 1 8 1 1 1 8 1 1 1 8 1 1 1
8 1 8 1 8 8 8 1 8 1 1 1 8 8 8 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
8 8 1 1 8 8 8 1 8 8 8 1 7 7 7 1
8 1 8 1 8 1 1 1 8 1 1 1 1 1 7 1
8 8 8 1 8 8 8 1 8 8 8 1 1 7 7 1
8 8 ...

output:

690561281

result:

ok 1 number(s): "690561281"

Test #3:

score: -100
Wrong Answer
time: 430ms
memory: 167604kb

input:

71117
7 8
2868391 1228870 2892937 349733 664891 1675356 1981526 762573
2892937 2892937 664891 1228870 959280 762573 664891 959280
349733 250147 1675356 349733 349733 762573 1675356 250147
1675356 959280 664891 250147 250147 250147 2868391 959280
1675356 664891 250147 1228870 1981526 250147 2868391 2...

output:

462363428
38853786
194740086
215040
40320
322560
32456681
1166400
887214222
542386470
375765881
9
4
361590980
913481201
527607149
85428015
311271219
16
645120
557106771
388800
173057174
232068778
460990604
1
239327783
9
145293043
40098691
97370043
799392782
155415144
1
36
3991680
645120
545661527
55...

result:

wrong answer 53rd numbers differ - expected: '9331200', found: '4665600'