QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#72037#2980. 求和Cyh29hao100 ✓456ms206744kbC++141.3kb2023-01-13 18:40:532023-01-13 18:40:57

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-13 18:40:57]
  • 评测
  • 测评结果:100
  • 用时:456ms
  • 内存:206744kb
  • [2023-01-13 18:40:53]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long 
using namespace std;
const int mx=3e5+5,mk=52,mod=998244353;
int n,u,v,m,k;
int to[mx<<1],head[mx],nxt[mx<<1],cnt=1,dep[mx],anc[mx][mk],Log2[mx];
ll sum[mx][mk];

inline void add_edge(int u,int v){
	to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;
}
inline void dfs(int u,int fa){
	dep[u]=dep[fa]+1;anc[u][0]=fa;
	for(int i=1;i<=Log2[n];++i)anc[u][i]=anc[anc[u][i-1]][i-1];
	
	for(ll i=1,now=dep[u];i<=50;++i,(now*=dep[u])%=mod)sum[u][i]=(sum[fa][i]+now)%mod;
	
	for(int i=head[u];i;i=nxt[i]){
		int v=to[i];if(v==fa)continue;
		dfs(v,u);
	}
}
inline int LCA(int x,int y){
	if(dep[x]<dep[y])swap(x,y);
	for(int i=Log2[n];i>=0;--i)
		if(dep[anc[x][i]]>=dep[y])x=anc[x][i];
	if(x==y)return x;
	for(int i=Log2[n];i>=0;--i)
		if(anc[x][i]!=anc[y][i])x=anc[x][i],y=anc[y][i];
	return anc[x][0];
}

inline int query(int u,int v,int k){
	int lca=LCA(u,v);
	ll ret=(sum[u][k]+sum[v][k]-sum[lca][k]+mod-sum[anc[lca][0]][k]+mod)%mod;
	//printf("  u=%d,v=%d,lca=%d\n",u,v,lca);
	return (int)ret;
}

int main()
{
	scanf("%d",&n);
	for(int i=2;i<=n;++i)Log2[i]=Log2[i/2]+1;
	for(int i=1;i<n;++i)scanf("%d%d",&u,&v),add_edge(u,v),add_edge(v,u);
	dep[0]=-1;dfs(1,0);
	scanf("%d",&m);
	while(m--)scanf("%d%d%d",&u,&v,&k),printf("%d\n",query(u,v,k));
	return 0;
}

詳細信息

Test #1:

score: 10
Accepted
time: 4ms
memory: 7968kb

input:

100
1 2
1 3
1 4
2 5
3 6
3 7
4 8
2 9
3 10
8 11
8 12
5 13
9 14
9 15
13 16
10 17
16 18
9 19
4 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 53
53 54
54...

output:

840509765
22411553
990198666
294733105
731031188
872705924
785605909
970109824
117144437
268340429
224967921
858931263
543897136
897979419
251301541
97930
35095520
386548627
433832399
48512839
631652206
735922605
61251333
843826535
637803314
114190242
146872113
893242466
866908606
747057542
32492602...

result:

ok 100 lines

Test #2:

score: 10
Accepted
time: 1ms
memory: 7764kb

input:

100
1 2
1 3
2 4
3 5
3 6
2 7
3 8
5 9
4 10
5 11
3 12
12 13
9 14
6 15
15 16
7 17
11 18
12 19
3 20
3 21
5 22
20 23
2 24
20 25
17 26
7 27
9 28
6 29
20 30
1 31
19 32
4 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 53
53 54
54 55
55 ...

output:

146493924
2085
721518752
960332372
583203962
238526233
129979
510851220
820932948
688912786
421673670
353672281
60744368
374766750
85358
552
53397
182853387
46901575
817738059
354507217
16907908
280419061
17253785
293734447
431780652
914802849
250675186
29375
893744147
125564417
8689425
562290578
64...

result:

ok 100 lines

Test #3:

score: 10
Accepted
time: 1ms
memory: 11820kb

input:

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

output:

873005520
32904299
460594088
299199537
289507498
311679839
68638294
403512452
189984019
664
691721933
128646235
839391218
443873276
214382068
1077
407044351
129904180
104686256
986777354
584623831
938722686
546643362
824523294
357581916
432597073
622730710
4552696
831957720
668982244
258083576
41242...

result:

ok 100 lines

Test #4:

score: 10
Accepted
time: 0ms
memory: 12244kb

input:

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

output:

603862151
474053789
893426054
866727044
530544935
334673238
192890486
784423755
805465176
430804063
277428842
177909289
18487551
87666211
435025934
251684048
862058145
238552464
1395
887767290
931334270
197441049
160873390
758827538
11077016
235487555
76153196
954056566
348033142
22262207
237248830
...

result:

ok 1000 lines

Test #5:

score: 10
Accepted
time: 1ms
memory: 8304kb

input:

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

output:

218982075
949795257
301989885
78941082
452578683
26417015
172272535
759552160
955449160
357661015
818126289
471036789
908119349
730011871
482664268
142456799
522729678
363866588
530149955
987075398
112118851
769037925
303493250
568527012
396864228
150060456
975271731
474579394
75882976
805222180
378...

result:

ok 1000 lines

Test #6:

score: 10
Accepted
time: 2ms
memory: 10356kb

input:

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

output:

851077264
159276774
248375210
964904392
274726197
590733429
878211216
444094440
131015999
242032357
854690781
281320150
737244756
340965559
879604036
318307410
439893592
669290235
282737141
410388642
576987268
276818143
325640748
441926045
37837096
927405257
599989624
342013250
461516576
704835471
1...

result:

ok 1000 lines

Test #7:

score: 10
Accepted
time: 405ms
memory: 206744kb

input:

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

output:

310134819
455975361
295384894
336067608
876113597
10261
728202657
1660649
181801709
793673436
947061827
248692948
868173339
424957191
995004740
637548712
1016049
480497449
505155640
403167691
340599291
27741981
181632517
118695832
871657083
502065460
321535960
547059090
620178207
349740446
184338857...

result:

ok 300000 lines

Test #8:

score: 10
Accepted
time: 423ms
memory: 203792kb

input:

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

output:

646984924
49369224
872676392
283855113
948527314
318544348
997469826
862040117
232223548
846818063
710493753
735743122
669769123
783128870
336319166
659710121
149960271
214523202
851219849
688745539
81
322336450
296499701
725165896
75951193
540812959
42369189
239638464
3321
985753441
41209
504756414...

result:

ok 300000 lines

Test #9:

score: 10
Accepted
time: 400ms
memory: 201328kb

input:

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

output:

5030
754392925
199513551
682118127
555704036
694394442
552374757
541706186
333372209
3466
688745539
952889294
524023090
187419484
610385342
704528591
134144510
310132072
719721141
10601787
53887910
78386153
748755973
66
788646825
333372209
11583274
486037060
599043720
20009
932152885
65
772750175
30...

result:

ok 300000 lines

Test #10:

score: 10
Accepted
time: 456ms
memory: 205428kb

input:

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

output:

5655
682138835
856584352
529544029
560250665
842392309
893057446
7499931
123118358
616193631
854637985
625679276
828429145
404042469
953233683
686759
243702320
892505566
401840558
538477908
301560720
571506308
17515053
546542165
6756399
739183055
711687348
91479829
234935734
883320078
766051132
8895...

result:

ok 300000 lines