QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#421876#6463. Touristsnitrousoxide#AC ✓391ms44788kbC++141.3kb2024-05-26 08:54:182024-05-26 08:54:18

Judging History

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

  • [2024-05-26 08:54:18]
  • 评测
  • 测评结果:AC
  • 用时:391ms
  • 内存:44788kb
  • [2024-05-26 08:54:18]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 210000;

vector<int> g[maxn];
int lca[20][maxn];
bool vis[maxn];
int dep[maxn];

void dfs(int u){
    vis[u] = true;
    for (auto v : g[u]) {
        if (!vis[v]) {
            lca[0][v] = u;
            dep[v] = dep[u] + 1;
            for (int i = 0; lca[i][v]; i++) {
                lca[i+1][v] = lca[i][lca[i][v]];
            }
            dfs(v);
        }
    }
}

int getLCA(int x, int y){ 
    if (dep[x] < dep[y]) swap(x,y);
    int delta = (dep[x] - dep[y]);
    for (int i = 17; i >= 0; i--) {
        if (delta & (1 << i)) {
            x = lca[i][x];
        }
    }
    if (x == y) {
        return x;
    }
    for (int i = 17; i >= 0; i--) {
        if (lca[i][x] != lca[i][y]) {
            x = lca[i][x];
            y = lca[i][y];
        }
    }
    return lca[0][x];
}

int main() {
    int n;
    cin >> n;
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1);
    ll tot = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 2*i; j <= n; j += i) {
            tot += dep[i] + dep[j] - 2*dep[getLCA(i,j)] + 1;
        }
    }
    cout << tot << endl;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 12052kb

input:

10
3 4
3 7
1 4
4 6
1 10
8 10
2 8
1 5
4 9

output:

55

result:

ok single line: '55'

Test #2:

score: 0
Accepted
time: 131ms
memory: 18232kb

input:

200000
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
1 27
1 28
1 29
1 30
1 31
1 32
1 33
1 34
1 35
1 36
1 37
1 38
1 39
1 40
1 41
1 42
1 43
1 44
1 45
1 46
1 47
1 48
1 49
1 50
1 51
1 52
1 53
1 54
1 55
1 56
1 57
1 58
1 59
1 60
1 61
1...

output:

6616340

result:

ok single line: '6616340'

Test #3:

score: 0
Accepted
time: 208ms
memory: 44744kb

input:

200000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52
52 5...

output:

224315931656

result:

ok single line: '224315931656'

Test #4:

score: 0
Accepted
time: 391ms
memory: 38300kb

input:

200000
1 99999
1 100000
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 ...

output:

214317137054

result:

ok single line: '214317137054'

Test #5:

score: 0
Accepted
time: 35ms
memory: 15560kb

input:

35763
1 2
1 3
3 4
4 5
2 6
6 7
3 8
3 9
5 10
8 11
10 12
4 13
8 14
1 15
15 16
7 17
5 18
5 19
7 20
12 21
14 22
5 23
4 24
14 25
9 26
21 27
11 28
8 29
14 30
2 31
8 32
27 33
1 34
11 35
31 36
20 37
33 38
17 39
7 40
39 41
32 42
10 43
9 44
33 45
37 46
38 47
31 48
23 49
47 50
18 51
30 52
23 53
31 54
41 55
46 5...

output:

5102133

result:

ok single line: '5102133'

Test #6:

score: 0
Accepted
time: 124ms
memory: 21168kb

input:

116097
1 2
1 3
2 4
2 5
5 6
3 7
5 8
1 9
6 10
4 11
4 12
1 13
8 14
11 15
11 16
9 17
10 18
9 19
1 20
4 21
6 22
2 23
23 24
8 25
21 26
24 27
26 28
3 29
14 30
27 31
9 32
14 33
3 34
15 35
26 36
1 37
23 38
6 39
21 40
27 41
21 42
10 43
31 44
26 45
41 46
39 47
41 48
36 49
48 50
32 51
1 52
35 53
45 54
49 55
19 ...

output:

20004825

result:

ok single line: '20004825'

Test #7:

score: 0
Accepted
time: 205ms
memory: 21540kb

input:

187284
1 2
1 3
2 4
3 5
4 6
2 7
2 8
8 9
9 10
7 11
8 12
9 13
8 14
13 15
9 16
5 17
3 18
10 19
12 20
1 21
17 22
3 23
14 24
13 25
10 26
4 27
21 28
25 29
14 30
17 31
6 32
9 33
33 34
34 35
29 36
28 37
17 38
17 39
15 40
39 41
3 42
7 43
19 44
35 45
2 46
33 47
34 48
6 49
31 50
38 51
15 52
5 53
51 54
27 55
44 ...

output:

35985759

result:

ok single line: '35985759'

Test #8:

score: 0
Accepted
time: 225ms
memory: 24028kb

input:

196565
1 2
2 3
3 4
1 5
2 6
3 7
4 8
5 9
5 10
8 11
10 12
6 13
10 14
5 15
9 16
1 17
17 18
9 19
19 20
6 21
15 22
5 23
4 24
6 25
7 26
20 27
15 28
3 29
7 30
3 31
21 32
7 33
21 34
30 35
35 36
13 37
37 38
19 39
25 40
37 41
31 42
40 43
1 44
14 45
8 46
13 47
43 48
36 49
25 50
38 51
45 52
41 53
16 54
12 55
54 ...

output:

37243461

result:

ok single line: '37243461'

Test #9:

score: 0
Accepted
time: 87ms
memory: 18568kb

input:

87405
1 2
2 3
2 4
4 5
5 6
5 7
1 8
6 9
7 10
3 11
10 12
10 13
6 14
9 15
6 16
11 17
10 18
7 19
10 20
6 21
8 22
8 23
18 24
6 25
25 26
24 27
2 28
9 29
11 30
20 31
1 32
18 33
13 34
3 35
31 36
31 37
21 38
11 39
18 40
26 41
13 42
20 43
27 44
9 45
29 46
23 47
31 48
7 49
41 50
41 51
40 52
18 53
10 54
35 55
32...

output:

15693718

result:

ok single line: '15693718'

Test #10:

score: 0
Accepted
time: 25ms
memory: 15236kb

input:

27274
1 2
2 3
1 4
4 5
3 6
3 7
5 8
7 9
2 10
7 11
7 12
4 13
1 14
6 15
1 16
13 17
5 18
17 19
5 20
4 21
17 22
19 23
21 24
23 25
14 26
1 27
21 28
24 29
21 30
21 31
11 32
22 33
27 34
10 35
18 36
33 37
35 38
1 39
38 40
12 41
40 42
41 43
24 44
13 45
5 46
32 47
22 48
14 49
37 50
7 51
19 52
50 53
3 54
38 55
5...

output:

3645267

result:

ok single line: '3645267'

Test #11:

score: 0
Accepted
time: 182ms
memory: 23208kb

input:

174788
1 2
1 3
1 4
1 5
2 6
4 7
5 8
1 9
5 10
10 11
6 12
10 13
3 14
2 15
6 16
1 17
4 18
15 19
17 20
9 21
5 22
19 23
15 24
9 25
20 26
16 27
26 28
4 29
19 30
29 31
2 32
27 33
5 34
26 35
19 36
28 37
36 38
11 39
28 40
19 41
39 42
37 43
13 44
5 45
15 46
37 47
22 48
30 49
24 50
34 51
47 52
32 53
51 54
12 55...

output:

31738189

result:

ok single line: '31738189'

Test #12:

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

input:

79220
1 2
2 3
1 4
4 5
5 6
5 7
5 8
7 9
3 10
4 11
10 12
8 13
7 14
9 15
3 16
8 17
12 18
4 19
9 20
11 21
8 22
17 23
2 24
19 25
1 26
9 27
21 28
6 29
7 30
1 31
3 32
12 33
10 34
20 35
32 36
14 37
18 38
3 39
21 40
33 41
36 42
37 43
41 44
25 45
1 46
26 47
11 48
27 49
30 50
14 51
50 52
49 53
16 54
51 55
9 56
...

output:

13420070

result:

ok single line: '13420070'

Test #13:

score: 0
Accepted
time: 175ms
memory: 22848kb

input:

163173
1 2
2 3
3 4
4 5
1 6
3 7
6 8
6 9
2 10
6 11
11 12
12 13
9 14
12 15
6 16
11 17
14 18
10 19
6 20
16 21
17 22
15 23
23 24
24 25
14 26
16 27
3 28
20 29
8 30
1 31
27 32
17 33
18 34
32 35
1 36
10 37
11 38
3 39
7 40
5 41
15 42
41 43
16 44
3 45
32 46
7 47
37 48
29 49
27 50
15 51
36 52
31 53
8 54
51 55
...

output:

29469183

result:

ok single line: '29469183'

Test #14:

score: 0
Accepted
time: 76ms
memory: 18036kb

input:

77349
1 2
1 3
1 4
4 5
1 6
4 7
6 8
2 9
5 10
8 11
1 12
1 13
9 14
1 15
7 16
7 17
6 18
11 19
6 20
2 21
20 22
6 23
13 24
14 25
9 26
25 27
5 28
14 29
10 30
11 31
4 32
11 33
22 34
33 35
35 36
16 37
16 38
19 39
22 40
40 41
12 42
5 43
42 44
38 45
11 46
12 47
24 48
36 49
41 50
9 51
2 52
4 53
32 54
19 55
3 56
...

output:

12102741

result:

ok single line: '12102741'

Test #15:

score: 0
Accepted
time: 213ms
memory: 21540kb

input:

181401
2018 3743
58313 87620
71083 127924
75939 170929
73691 118367
66424 69360
2370 5841
42552 137072
25224 76933
57996 60869
69350 78813
88398 122281
5613 85323
112443 158855
22637 166313
41464 62941
14346 95684
79753 180274
9611 129274
49677 62129
89498 119560
12775 181071
139244 162786
41908 126...

output:

32688764

result:

ok single line: '32688764'

Test #16:

score: 0
Accepted
time: 132ms
memory: 19952kb

input:

127736
17453 19497
11899 77499
1467 39808
3627 6007
62923 84124
68451 127621
820 32603
81275 91703
61574 89624
21874 35015
72445 73044
21217 30518
75270 90334
26187 72684
91422 109363
36742 51425
6231 29118
42721 70853
6224 6342
15173 109349
23093 78089
24995 70049
10412 91697
8983 15094
17426 56087...

output:

21471936

result:

ok single line: '21471936'

Test #17:

score: 0
Accepted
time: 106ms
memory: 19180kb

input:

107974
3833 9812
10343 20970
8469 18524
68674 105528
12689 34560
50126 60527
23188 96464
956 41114
34981 41877
29089 99235
1026 2182
10002 21705
3873 31817
50702 103881
29015 73565
1877 57373
59452 89373
54907 79296
18280 32839
4547 11166
23366 53397
77050 83504
10728 44990
21329 33847
40127 45091
1...

output:

17647582

result:

ok single line: '17647582'

Test #18:

score: 0
Accepted
time: 54ms
memory: 16372kb

input:

58031
9411 9900
16496 34807
22253 22767
18111 52654
4400 28718
4843 8458
39644 47033
368 4323
47996 48867
6637 22051
772 2013
12003 17827
20090 24107
28216 47468
18678 41040
507 3085
3215 15105
9071 32761
3641 10441
558 2014
18781 29068
3944 4221
10446 48365
1117 18821
21726 39699
23377 43462
7902 1...

output:

9160123

result:

ok single line: '9160123'

Test #19:

score: 0
Accepted
time: 132ms
memory: 19368kb

input:

122570
7522 88444
1401 17315
13031 14412
89072 101502
26046 29720
3202 40782
17704 56651
44771 89596
26827 40917
39828 67449
34197 84923
981 3707
85417 87233
2116 8015
70119 109790
38379 122279
22186 37310
21627 84848
1733 6700
11553 21246
38391 75766
43644 87410
9981 31975
7792 10130
9506 15651
889...

output:

20825175

result:

ok single line: '20825175'

Test #20:

score: 0
Accepted
time: 150ms
memory: 20276kb

input:

140134
6040 7128
31347 107759
26824 82743
5916 10913
11885 73264
14542 101220
32392 113999
23577 116453
8987 53481
45379 109872
127729 133153
39886 44941
36178 88416
79420 98612
88033 88163
39539 91911
86775 91829
94067 103740
4212 28873
59322 60728
42533 113171
108844 128402
82537 124184
332 4982
2...

output:

25189579

result:

ok single line: '25189579'

Test #21:

score: 0
Accepted
time: 217ms
memory: 22028kb

input:

196449
140866 141631
35321 59660
44839 173279
136705 148386
65380 99050
12661 138446
12201 16567
21522 135457
146091 188670
147664 149002
2496 52148
78531 88878
93840 97817
31739 134274
14549 67886
181363 184134
88837 103274
152104 176633
6547 150693
1451 22689
24560 40184
46529 56896
104666 108918
...

output:

39219893

result:

ok single line: '39219893'

Test #22:

score: 0
Accepted
time: 169ms
memory: 20112kb

input:

149287
17872 57462
83501 122442
16513 59109
21147 61588
15116 18061
98273 124075
12825 15303
2074 10412
4718 48884
119366 119832
58981 131335
23424 62725
59074 114170
111477 118430
3613 30507
5142 35108
15564 137800
6245 95089
59542 120857
32007 73756
15798 84045
17771 87589
179 143282
4168 21091
48...

output:

27603401

result:

ok single line: '27603401'

Test #23:

score: 0
Accepted
time: 68ms
memory: 16992kb

input:

71461
7125 43657
15050 48959
8156 26730
22694 23369
19734 26396
14264 24993
36755 70597
66401 68379
28878 42154
10469 20478
21373 65334
43846 53353
335 3665
28603 35625
10109 48392
4353 5706
2525 60636
19323 21985
11306 64640
16191 33867
2698 54808
2567 3703
294 14314
19690 30039
3808 5681
38427 604...

output:

11924048

result:

ok single line: '11924048'

Test #24:

score: 0
Accepted
time: 30ms
memory: 15576kb

input:

39120
9046 30369
3299 32136
12834 27093
9944 15367
12180 32978
7875 36517
27992 30073
945 15995
29887 36931
7440 19552
14265 15889
226 1972
4592 5738
6612 22554
24016 35880
6320 16101
21777 25459
3763 35690
14624 19298
8378 8726
22728 27679
24919 25038
8841 23991
4510 14529
23280 24264
220 6915
7488...

output:

5551407

result:

ok single line: '5551407'

Test #25:

score: 0
Accepted
time: 213ms
memory: 44788kb

input:

200000
4186 4187
126727 126728
146702 146703
104082 104083
44048 44049
43320 43321
109664 109665
103952 103953
19164 19165
111798 111799
96447 96448
44617 44618
32871 32872
131247 131248
108267 108268
93889 93890
177452 177453
86715 86716
39525 39526
190408 190409
67115 67116
176772 176773
27940 279...

output:

224315931656

result:

ok single line: '224315931656'

Test #26:

score: 0
Accepted
time: 219ms
memory: 44684kb

input:

200000
188414 188415
7790 7791
55030 55031
16502 16503
48049 48050
85546 85547
134143 134144
68179 68180
36289 36290
52627 52628
160057 160058
128308 128309
124720 124721
138636 138637
32609 32610
182416 182417
33689 33690
176937 176938
82004 82005
108626 108627
192014 192015
192114 192115
188413 18...

output:

224315731659

result:

ok single line: '224315731659'

Test #27:

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

input:

2
1 2

output:

2

result:

ok single line: '2'