QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#477654#4895. Lovely DogsOFforest_127310 270ms75876kbC++142.4kb2024-07-14 08:15:552024-07-14 08:15:56

Judging History

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

  • [2024-07-14 08:15:56]
  • 评测
  • 测评结果:10
  • 用时:270ms
  • 内存:75876kb
  • [2024-07-14 08:15:55]
  • 提交

answer

#include<bits/stdc++.h>
#define PII pair<int,int>
#define ll long long
using namespace std;
//char buf[1<<23],*p1=buf,*p2=buf;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T> inline void read(T &x){x=0;int f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();x*=f;}
template <typename T> inline void write(T x){if(x>9)write(x/10);putchar(x%10+'0');}
const int N=2e5+10;
int n,m,prime[N],f[N],g[N],mu[N],u[N],v[N],a[N],mx[N],vis[N];
ll t[N],now,s[N];
vector<int> G[N];
basic_string<int> d[N];
basic_string<PII> d2[N];
int fa[N],L[N],R[N],tot,iv[N],siz[N],son[N],ans[N];
basic_string<int> tmp;
void dfs1(int u,int f){
	fa[u]=f,L[u]=++tot,iv[tot]=u,siz[u]=1;
	for(auto v:G[u])if(v^f)dfs1(v,u),siz[u]+=siz[v],son[u]=siz[v]>siz[son[u]]?v:son[u];
	R[u]=tot;
}
void add(int x){if(f[x]){tmp+=x;for(auto y:d[x])s[y]+=g[x];}}
ll ask(int x){
	ll res=0;
	for(auto y:d2[x])res+=mu[y.first]*s[y.second];
	return res*g[x];
}
void dfs2(int u){
	for(auto v:G[u])
		if(v^fa[u]&&v^son[u]){
			ll tp=now;
			dfs2(v),now=tp;
			for(auto x:tmp)for(auto y:d[x])s[y]=0;
			tmp.clear();
		}
	if(son[u])dfs2(son[u]);
	add(u),now+=ask(u);
	for(auto v:G[u])
		if(v^fa[u]&&v^son[u])
			for(int i=L[v];i<=R[v];++i)add(iv[i]),now+=ask(iv[i]);
	ans[u]=now;
}
int main(){
	read(n),read(m);
	for(int i=1;i<n;++i)read(u[i]),read(v[i]);
	for(int i=1;i<=n;++i)read(a[i]);
	for(int i=1;i<n;++i)G[a[u[i]]].push_back(a[v[i]]),G[a[v[i]]].push_back(a[u[i]]);
	g[1]=mu[1]=1;
	for(int i=2;i<=n;++i){
		if(!g[i])g[i]=mu[i]=-1,prime[++prime[0]]=i,mx[i]=i;
		for(int j=1;j<=prime[0]&&prime[j]*i<=n;++j){
			mx[prime[j]*i]=prime[j],g[prime[j]*i]=-g[i];
			if(!(i%prime[j]))break;
			mu[prime[j]*i]=-mu[i];
		}
	}
	for(int i=1;i<=n;++i)for(int j=i;j<=n;j+=i)d[j]+=i;
	for(int i=1;i<=n;++i){
		ll tp=1;
		for(int j=1;j<=m+1;++j)tp*=i;
		if(tp>1ll*n*n)break;
		t[i]=tp;
	}
	for(int i=1;i<=n;++i){//枚举b
		f[i]=1;
		static int cnt[N];
		for(int x=i;x^1;x/=mx[x])++cnt[mx[x]],f[i]&=cnt[mx[x]]<=m;
		for(int x=i;x^1;x/=mx[x])cnt[mx[x]]=0;
		for(auto j:d[i])
			if(t[j]&&mu[j]){
				ll tp=1ll*t[j]/__gcd(t[j],1ll*i);
				if(tp<=n)d2[i]+={j,tp},vis[tp]=1;
			}
		d[i].clear();
	}
	for(int i=1;i<=n;++i)if(vis[i])for(int j=i;j<=n;j+=i)d[j]+=i;
	dfs1(a[1],0),dfs2(a[1]);
	for(int i=1;i<=n;++i)write(ans[a[i]]),putchar('\n');
	return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

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

input:

20 2
18 8
18 11
13 19
10 8
9 11
4 8
9 15
9 17
2 1
13 18
20 18
1 8
12 17
7 16
5 11
16 15
6 19
14 16
1 3
2 15 5 13 20 6 16 18 9 19 17 7 14 10 11 3 1 12 4 8

output:

16
1
1
1
0
1
0
12
3
1
6
1
3
1
2
1
1
7
1
0

result:

ok 20 tokens

Test #2:

score: -10
Wrong Answer
time: 0ms
memory: 32380kb

input:

500 1
287 459
335 297
303 82
427 202
500 158
257 45
410 274
208 19
172 113
274 379
380 65
234 46
161 441
73 488
473 327
474 481
152 67
78 414
260 20
142 385
494 343
446 72
498 296
111 9
349 372
448 217
282 442
412 144
342 44
282 92
337 128
426 201
104 493
278 298
278 145
363 121
92 305
278 379
166 1...

output:

158
-
0
0
/
0
0
0
/
/
.
0
0
1
0
0
0
0
/
-
0
0
1
0
1
0
0
0
0
0
1
6
5
0
0
0
0
0
1
0
0
0
/
2
0
0
0
98
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
/
0
0
1
0
0
0
+
0
0
11
0
0
0
0
/
0
0
1
0
7
.
0
31
0
2
14
-
0
1
0
0
0
0
1
0
0
.
-
0
0
-
0
0
0
0
0
0
0
0
0
/
0
0
/
0
1
0
0
0
2
1
0
-
0
0
0
/
0
0
0
/
0
-
0
0
/
0
0
3
0
0
...

result:

wrong answer 2nd words differ - expected: '-3', found: '-'

Subtask #2:

score: 0
Wrong Answer

Test #24:

score: 0
Wrong Answer
time: 0ms
memory: 32668kb

input:

2000 1
134 1468
867 1750
351 1220
1690 1888
1685 134
585 282
1142 643
206 271
260 1833
1987 770
1029 1667
322 1371
341 518
601 915
119 893
1933 1502
951 1785
1056 1630
1957 1208
96 55
1508 1212
331 427
505 151
1378 1486
1545 697
1459 629
202 997
180 1917
1638 1177
1244 1896
302 658
1433 1605
1318 19...

output:

581
-
0
0
0
0
0
0
0
.
0
0
0
0
0
/
0
0
0
1
0
/
0
0
/
0
0
0
17
.
0
/
.
0
0
0
0
0
0
0
+
0
0
0
0
,
0
/
0
/
0
0
1
1
/
,
0
0
1
0
0
0
3
0
0
0
/
.
0
0
4
0
0
0
0
/
0
1
0
0
0
+
0
0
0
0
/
0
0
0
0
0
0
0
1
/
0
18
0
0
13
.
0
.
0
0
0
0
2
.
2
0
0
3
0
/
0
0
0
0
-
0
0
0
0
0
0
1
/
0
0
0
0
0
/
/
0
0
53
0
0
0
0
0
0
0
0
...

result:

wrong answer 2nd words differ - expected: '-3', found: '-'

Subtask #3:

score: 10
Accepted

Test #45:

score: 10
Accepted
time: 134ms
memory: 60320kb

input:

200000 20
117994 12616
53490 106425
103660 50033
132640 78252
58384 19939
69183 10015
39098 165030
179856 130356
65245 57831
18234 83378
4240 154896
177149 102260
4634 180087
132390 19627
98506 60775
1890 120740
87908 21917
41323 192721
181885 96684
69412 139951
9800 38301
59025 29879
186185 81402
1...

output:

143459
1
6
1
17
19
1
2
3
1
1
26
1
1
1
1
1
1
7
1
1
3
1
2
1
1
1
1
4
3
2
1
3
13
19
1
1
1
1
1
1
34
1
1
1
12
1
1
3
1
24
1
1
1
2
1
1
6
1
3
1
3
1
10
1
1
1
3
4
9
1
2
3
1
1
1
7
2
6
1
1
1
1
1
1
2
3
457
2
1
3
1
1
1
1
12
1
1
1
6
2
1
1
3
1
3
113
1
4
11
2
1
4
2
1
1
1
25
22
2
3
1
1
3
3
1
2
3
1
2
1
1
107
1
1
14
1
1...

result:

ok 200000 tokens

Test #46:

score: 0
Accepted
time: 146ms
memory: 60416kb

input:

200000 20
26219 163867
20331 153212
126612 40599
53814 68996
79701 140933
144374 181902
59705 155221
11230 70725
158998 133605
163268 88141
83507 114091
7736 162046
143360 92662
197974 194981
129770 126237
133117 75376
44213 67464
131083 19290
35473 65770
192299 66427
112908 181240
139699 88439
1103...

output:

143459
1
1
3
3
1
4
1
1
1
17
2
4
2
1
1
1
1
2
1
1
1
2
1
1
1
9
1
2
2
1
1
1
1
2
1
1
3
1
2
7
1
1
1
1
1
3
6
14
20
1
1
1
7
7
1
52
1
6
1
1
24
5
3
3
1
1
1
1
1
12
1
1
16
1
1
4
1
1
1
1
9
1
1
1
1
1
5
1
1
120
1
22
1
1
1
4
15
1
3
3
1
1
1
1
48
1
1
1
1
1
1
13
1
3
3
1
1
7
1
5
3
1
1
1
1
1
1
1
783
3
1
1
1
1
2
1
1
1
1
...

result:

ok 200000 tokens

Test #47:

score: 0
Accepted
time: 144ms
memory: 67644kb

input:

200000 20
151436 178769
79211 141557
185103 181923
173473 84222
84222 31293
159673 17537
72335 131147
59289 141263
84222 23691
163353 84222
76461 84222
80651 84307
76526 12294
199619 84222
59457 177309
19912 1273
164867 44284
41990 26506
40399 84222
98264 145475
84222 48507
84222 88935
84222 176452
...

output:

143459
94518
71442
82904
129477
42163
72542
1
1
45757
1
130757
52193
1
1
1
96408
1
78846
1
57773
1
1
1
86255
119507
1
1
83415
1
1
1
1
1
84437
1
1
1
72508
1
77107
84355
1
1
1
72616
52116
1
136452
1
100206
131528
1
146256
76408
1
1
1
1
50456
1
1
1
1
75137
1
83751
71005
116031
1
52173
1
75120
1
80850
1...

result:

ok 200000 tokens

Test #48:

score: 0
Accepted
time: 152ms
memory: 60416kb

input:

200000 20
76510 76490
88933 21393
126948 187403
137672 130527
82789 167591
134447 15851
54831 5084
196062 114272
151180 77255
51713 92637
179118 81158
109526 64703
34747 40350
96352 50618
67033 44700
33353 157246
193080 130434
169961 20611
11637 109101
191766 55895
98648 132015
126097 100752
187559 ...

output:

143459
1
2
28
1
2
2
1
1
6
1
4
6
2
4
1
2
1
1
1
6
6
72
1
1
1
1
8
1
2
188
2
2
1
4
2
6
4
76
6
6
6
8
2
1
2
28
1
8
1
2
1
6
1
1862
1
1
1
20
28
64
1
1
4
1
1
2
2
1
6
4
1
1
1
1
20
1
1
1
1
8
1
8
1
1
2
4
1
1
48
12
2
1
2
4
6
1
1
8
2
2
1
1
1
1
1
2
1
8
1
1
1
1
1
8
6
2
32
1
1
2
36
1
2
8
1
1
56
1
1
1
1
2
1
1
1
2
2
2...

result:

ok 200000 tokens

Test #49:

score: 0
Accepted
time: 143ms
memory: 60332kb

input:

200000 20
91828 79744
98337 148526
56405 54432
49447 185733
136527 109193
20160 117165
95938 77898
169046 98403
45584 26426
55328 117425
132115 35999
174350 136071
10629 151193
127424 169510
172287 15111
72777 59970
110820 119084
188317 89900
195364 7345
77707 106548
139440 165710
26624 117460
11202...

output:

143459
16
1
1
1
1
1
1
1
1
2
1
2
9
2
1
1
1
1
1
1
1
4
2
2
1
2
1
1
6
1
8
1
1
1
1
1
1
11
2
1
1
1
1
1
1
37
1
3
1
7
6
2
1
1
1
1
1
10
2
1
6
1
15
1
11
66
3
1
1
1
12
3
5
2
1
1
15
1
7
2
7
12
3
1
1
29
3
1
128
1
2
1
1
3
11
1
2
1
3
1
1
1
1
1
1
1
13
1
1
7
1
7
69
1
2
2
1
14
1
1
1
16
1
1
1
1
1
2
2
1
1
1
1
3
42
2
9
...

result:

ok 200000 tokens

Subtask #4:

score: 0
Wrong Answer

Test #50:

score: 0
Wrong Answer
time: 202ms
memory: 75052kb

input:

200000 1
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...

output:

*
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 1st words differ - expected: '-44916', found: '*'

Subtask #5:

score: 0
Wrong Answer

Test #55:

score: 0
Wrong Answer
time: 203ms
memory: 75576kb

input:

200000 1
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...

output:

*
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 1st words differ - expected: '-44916', found: '*'

Subtask #6:

score: 0
Wrong Answer

Test #78:

score: 0
Wrong Answer
time: 47ms
memory: 43948kb

input:

50000 1
8097 41839
17674 41774
40520 8024
5786 38261
20664 43471
1217 49276
11185 40807
14186 25584
31704 14814
42333 41475
13053 39565
45938 30104
5826 39463
5031 10814
43784 6042
58 33849
42978 18978
36307 33276
34769 4351
27884 37532
27528 29431
29451 39345
10946 9667
19016 47269
7911 30103
10308...

output:

.
0
0
0
0
0
0
/
0
/
0
0
1
0
0
0
0
0
0
0
0
.
/
.
0
.
0
2
-
3
0
0
/
0
0
0
0
0
0
0
0
.
/
0
/
0
2
0
1
0
0
0
0
1
0
0
31
0
0
0
0
0
0
-
0
/
26
.
9
/
/
5
0
0
2
0
/
0
0
0
4
/
/
0
/
0
0
0
0
0
/
0
0
.
/
/
0
0
4
0
0
0
0
0
0
8
.
0
0
0
0
0
0
0
9
0
1
0
0
0
0
0
0
0
0
0
0
0
/
1
0
0
0
-
0
0
0
0
0
0
6
0
/
0
0
*
3
1
0
...

result:

wrong answer 1st words differ - expected: '-9152', found: '.'

Subtask #7:

score: 0
Wrong Answer

Test #103:

score: 0
Wrong Answer
time: 270ms
memory: 75876kb

input:

200000 1
118863 188865
188022 168616
118976 119404
178852 33449
81624 40431
151228 160976
68943 136313
57200 117631
147789 139875
100240 55537
164811 145415
103548 186750
15010 168029
155731 107005
69836 1502
86171 122700
83448 131948
189162 94464
128210 2509
49724 183329
174782 192641
27687 71315
1...

output:

*
0
0
0
0
0
0
0
0
0
/
/
0
0
/
0
0
0
.
/
0
0
1
0
0
/
0
0
0
0
0
0
0
0
0
0
0
0
0
5
0
0
/
0
0
0
0
0
0
17
0
0
0
0
0
0
/
0
1
6
41
0
0
0
0
0
0
0
0
0
0
0
3
0
0
/
(
0
0
0
0
0
0
-
0
0
0
0
0
0
0
0
/
0
0
0
+
0
0
0
*
/
0
0
0
0
/
/
0
/
0
(
0
0
0
0
0
0
/
0
0
0
3
0
0
0
0
0
0
0
0
.
0
0
1
0
0
1
0
/
0
0
0
0
-
0
0
0
0
...

result:

wrong answer 1st words differ - expected: '-44916', found: '*'