QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#541964#8943. Challenge Matrix Multiplicationucup-team296#ML 876ms185468kbC++142.8kb2024-08-31 21:47:182024-08-31 21:47:20

Judging History

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

  • [2024-08-31 21:47:20]
  • 评测
  • 测评结果:ML
  • 用时:876ms
  • 内存:185468kb
  • [2024-08-31 21:47:18]
  • 提交

answer

#include <bits/stdc++.h>
#define long long long int
#define DEBUG
using namespace std;

// @author: pashka

int mod = 998244353;

struct mint {
    int value = 0;

    constexpr mint() : value() {}

    mint(const long &x) {
        value = normalize(x);
    }

    static long normalize(const long &x) {
        long v = x % mod;
        if (v < 0) v += mod;
        return v;
    }

    bool operator==(const mint &x) { return value == x.value; };

    mint operator+(const mint &x) { return value + x.value; };

    mint operator-(const mint &x) { return value - x.value; };

    mint operator*(const mint &x) { return (long) value * x.value; };

    void operator+=(const mint &x) { value = normalize(value + x.value); };

    void operator-=(const mint &x) { value = normalize(value - x.value); };
};

mint power(mint a, long b) {
    mint res = 1;
    while (b > 0) {
        if (b & 1) {
            res = res * a;
        }
        a = a * a;
        b >>= 1;
    }
    return res;
}

mint inv(mint a) {
    return power(a, mod - 2);
}

int main() {
    ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;
    vector<vector<int>> g(n);
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        g[x].push_back(y);
    }
    vector<vector<pair<int, int>>> pos(n);
    vector<vector<int>> p;
    auto gg = g;
    for (int i = 0; i < n; i++) {
        while (!gg[i].empty()) {
            int x = i;
            p.push_back({x});
            int t = p.size() - 1;
            pos[x].push_back({t, 0});
            while (!gg[x].empty()) {
                int y = gg[x].back();
                gg[x].pop_back();
                x = y;
                pos[x].push_back({t, p.back().size()});
                p.back().push_back(x);
            }
        }
    }
    int k = p.size();
    vector<vector<int>> d(n, vector<int>(k));
    for (int x = n - 1; x >= 0; x--) {
        for (int i = 0; i < k; i++) {
            d[x][i] = p[i].size();
        }
        for (auto [i, j] : pos[x]) {
            d[x][i] = j;
        }
        for (auto y : g[x]) {
            for (int i = 0; i < k; i++) {
                d[x][i] = min(d[x][i], d[y][i]);
            }
        }
    }

    vector<vector<mint>> ps(k);
    for (int i = 0; i < k; i++) {
        int t = p[i].size();
        ps[i].resize(t + 1);
        for (int j = t - 1; j >= 0; j--) {
            int x = p[i][j];
            ps[i][j] = ps[i][j + 1] + inv(pos[x].size());
        }
    }

    for (int i = 0; i < n; i++) {
        mint res = 0;
        for (int j = 0; j < k; j++) {
            res += ps[j][d[i][j]];
        }
        if (res == 0) res = 1;
        cout << res.value << "\n";
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

4 6
1 3
2 3
2 4
1 2
1 3
1 3

output:

4
3
1
1

result:

ok 4 number(s): "4 3 1 1"

Test #2:

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

input:

5 7
1 4
1 5
1 2
2 4
3 4
2 5
1 4

output:

4
3
2
1
1

result:

ok 5 number(s): "4 3 2 1 1"

Test #3:

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

input:

100 900
89 90
34 35
45 46
97 98
41 42
59 61
19 20
25 29
2 3
28 29
54 55
77 78
69 74
60 61
43 44
13 14
92 93
65 66
68 69
72 73
78 81
54 56
55 60
14 15
9 10
92 93
10 11
18 19
16 17
97 98
76 77
39 40
71 72
7 8
63 64
7 8
16 24
13 24
83 84
90 91
1 4
4 5
96 97
81 82
91 92
80 81
66 67
19 20
3 4
9 10
47 48
...

output:

100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1

result:

ok 100 numbers

Test #4:

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

input:

100 900
71 72
22 26
21 22
15 22
97 98
43 44
64 65
87 88
79 81
90 93
54 55
96 97
64 67
64 88
24 25
71 72
47 48
49 51
72 75
12 14
66 67
6 7
90 91
14 15
73 74
99 100
66 73
84 85
34 35
94 95
88 89
16 17
17 20
56 57
13 14
13 14
48 51
80 81
7 9
26 27
42 44
86 87
31 36
82 83
54 55
7 8
20 21
41 42
17 18
91 ...

output:

100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1

result:

ok 100 numbers

Test #5:

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

input:

100 916
93 97
2 3
77 78
47 50
23 24
25 31
31 32
59 61
69 74
8 9
4 5
30 31
73 74
3 4
12 15
36 37
80 84
44 47
84 85
9 18
78 79
74 76
45 46
89 96
76 78
20 21
22 24
35 36
20 22
36 37
25 26
82 83
40 42
95 96
29 30
92 93
93 94
21 22
34 35
65 66
32 33
71 72
9 11
87 88
69 71
12 13
46 47
3 4
3 4
59 62
64 65
...

output:

100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1

result:

ok 100 numbers

Test #6:

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

input:

100 843
62 64
78 80
10 11
31 37
37 48
64 66
24 25
77 79
68 69
10 11
76 78
89 90
37 41
20 21
42 43
30 36
47 48
66 68
10 11
18 21
59 62
30 31
42 49
56 66
78 83
37 39
65 67
96 97
24 73
26 28
21 22
82 83
94 96
39 40
8 10
89 90
51 52
92 93
85 87
34 62
6 7
97 98
13 14
5 6
29 30
7 10
41 42
70 71
21 23
48 5...

output:

100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1

result:

ok 100 numbers

Test #7:

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

input:

100 910
74 75
90 91
66 67
84 85
56 57
86 87
29 30
92 93
30 53
91 92
55 58
43 44
58 59
65 66
75 76
46 47
50 51
99 100
57 58
37 39
75 77
35 36
2 3
39 41
70 71
85 86
4 5
56 57
28 29
67 69
98 99
3 4
80 81
9 12
9 10
79 80
68 70
3 4
72 73
81 82
54 55
97 98
7 8
94 97
69 70
56 57
69 71
6 7
49 50
26 27
80 81...

output:

100
99
98
97
96
95
94
93
92
91
90
89
88
87
86
85
84
83
82
81
80
79
78
77
76
75
74
73
72
71
70
69
68
67
66
65
64
63
62
61
60
59
58
57
56
55
54
53
52
51
50
49
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1

result:

ok 100 numbers

Test #8:

score: 0
Accepted
time: 640ms
memory: 98292kb

input:

300000 1000000
291518 291525
162078 162095
107433 107434
117028 117029
252973 252975
34296 34301
17712 17713
168224 168227
5479 5480
96730 96733
18177 18182
170140 170142
114143 114145
290862 290865
239489 239490
132218 132219
143908 143914
118103 118105
76237 76240
265590 265591
42005 42010
95874 9...

output:

296803
296802
296801
296800
296799
296797
296796
296796
296795
1
296794
296793
296792
296791
296790
296789
296788
296787
296786
296785
296784
296782
296782
296781
296780
296779
296778
296777
296776
296775
296774
296773
296772
296769
296770
296768
296768
296767
296766
296765
296764
296763
296762
2967...

result:

ok 300000 numbers

Test #9:

score: 0
Accepted
time: 876ms
memory: 149868kb

input:

300000 1000000
187606 187608
36565 36566
128950 128958
172669 172683
41451 41458
138127 138132
22729 22733
13633 13640
158041 158044
74504 74505
190220 190247
97877 97879
277477 277483
35531 35584
77716 77731
142348 142349
231107 231110
232636 232640
211702 211714
257036 257048
124062 124078
289660 ...

output:

291171
291174
291182
291188
291184
291166
291172
291157
1
291156
291183
291171
291181
291130
291178
291174
291173
291171
291155
291170
291165
291154
291150
291170
291169
291162
291164
291156
291164
291162
291160
291155
291157
291154
291156
291136
291155
291137
291149
291149
291153
291149
291145
2911...

result:

ok 300000 numbers

Test #10:

score: 0
Accepted
time: 826ms
memory: 136004kb

input:

300000 1000000
127190 127320
86211 86214
154425 154446
21608 21646
295649 295653
49413 49419
274753 274755
272272 272286
248246 248258
167611 167613
99630 99631
118018 118023
233539 233541
76885 76896
272148 272174
115623 115633
250825 250830
142266 142340
291868 291898
54308 54309
159123 159124
261...

output:

291386
291384
291383
291385
291378
291381
291366
291378
291380
291379
291378
291367
291376
291375
291365
291370
291373
291372
291369
291371
291366
291368
291364
291366
291363
291345
291363
291360
291357
291355
291351
291345
291355
291354
291353
291348
291350
291348
291345
291343
291348
291347
291345...

result:

ok 300000 numbers

Test #11:

score: 0
Accepted
time: 343ms
memory: 75128kb

input:

300000 622182
178651 178654
168896 168899
51002 51003
52671 52672
260458 260461
288275 288282
97920 97921
48060 48061
21907 21908
271103 271104
262840 262842
192846 192847
109062 109063
20122 20123
230639 230640
45404 45408
10054 10055
133116 133117
114713 114714
26310 26311
141682 141684
111622 111...

output:

299999
299998
299997
299996
299995
299994
299993
299992
299991
299990
299989
299988
299987
299986
299985
299984
299983
299982
299981
299980
299979
299978
299977
299976
299975
299974
299973
299972
299971
299970
299969
299968
299967
299966
299965
299964
299963
299962
299961
299960
299959
299958
299957...

result:

ok 300000 numbers

Test #12:

score: 0
Accepted
time: 824ms
memory: 185468kb

input:

700000 1000000
238263 238265
271765 271767
535443 535465
91436 91438
44996 45004
491072 491073
182658 182667
204466 204475
425640 425642
490507 490517
654032 654033
695412 695416
573184 573188
178765 178767
550768 550776
191441 191444
422916 422924
200846 200863
686525 686526
23786 23805
432819 4328...

output:

556560
1
556556
556558
556521
556557
556531
1
556556
556555
556555
1
556554
556553
556536
556552
556551
556549
556548
1
556541
556536
556540
556535
556537
556535
1
556536
556535
556531
556530
556241
556534
556530
556520
1
556529
556527
556530
556529
556513
556504
556525
556508
556526
1
556524
556496...

result:

ok 700000 numbers

Test #13:

score: -100
Memory Limit Exceeded

input:

700001 1000000
306352 306425
265123 265158
497711 497717
155684 155790
407614 407617
2099 2259
425834 425842
518856 518924
236978 236981
443915 443961
374152 374389
197908 197911
417118 417128
504272 504442
538611 538618
88243 88266
443775 443788
177312 177318
187300 187375
97432 97435
296402 296410...

output:

538889
1
538877
1
538869
538884
1
1
538877
538884
538851
538873
1
538883
538839
538876
538867
538714
1
1
538871
538850
538848
538845
1
1
538874
538848
538846
538866
538824
538868
538871
538810
538869
538787
538713
1
538867
538844
538825
538845
538845
538868
538826
538866
538866
538865
538844
538841
...

result: