UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213613#573. t2xuewentao30695ms24364kbC++111.7kb2024-11-12 21:51:442024-11-12 23:56:58

answer

#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e5 + 10;

int n, q;

struct Edge{
	int v, w;
};

vector<Edge> G[maxn];

map<int, int> p[maxn];

int cnt[maxn];

void dfs(int u, int fa) {
	p[u][-1] = 1;
	for (auto tmp: G[u]) {
		int v = tmp.v, w = tmp.w;
		if (v == fa) continue;
		dfs(v, u);
		map<int, int> res;
		for (auto vm: p[v]) {
			for (auto um: p[u]) {
				if (vm.first == -1 && um.first == -1) {
					res[w] += 1;
				} else if (vm.first == -1) {
					res[__gcd(w, um.first)] += um.second;
				} else if (um.first == -1) {
					res[__gcd(w, vm.first)] += vm.second;
				} else {
					res[__gcd(w, __gcd(vm.first, um.first))] += um.second * vm.second;
				}
			}
		}
		for (auto tmp: res) {
			cnt[tmp.first] += tmp.second;
		}
		for (auto vm: p[v]) {
			if (vm.first == -1) {
				p[u][w] += 1;
			} else {
				p[u][__gcd(w, vm.first)] += vm.second;
			}
		}
		p[u][-1] = 1;
	}
	
//	cout << u << " -----" << endl;
//	for (auto tmp: p[u]) {
//		cout << tmp.first << " " << tmp.second << endl;
//	}
}

int main() {
	cin >> n >> q;
	for (int i = 1; i <= n-1; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		G[u].push_back({v, w});
		G[v].push_back({u, w});
	}
	
	dfs(1, 0);
	
//	for (int i = 1; i < 30; i++) cout << i << " " << cnt[i] << endl; // 
	
	for (int i = 1; i < maxn; i++) {
		for (int j = 2*i; j < maxn; j += i) {
			cnt[i] += cnt[j];
		}
	}
	
	
	for (int i = 1; i <= q; i++) {
		int x;
		cin >> x;
		cout << cnt[x] << endl;
	}
}

/*

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

*/

/*

6 1
1 2 4
2 3 4
3 4 10
2 5 2
1 6 5
4

*/

/*

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

*/

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 30
Accepted

Test #1:

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

input:

50 50
48 29 49788
47 48 31142
35 48 28665
10 35 23889
39 35 6411
50 39 66666
43 35 27629
46 10 49173...

output:

2
1
0
0
0
2
0
0
0
0
0
0
1
10
0
0
1
0
0
2
0
1
0
2
0
0
0
0
0
2
0
0
0
0
2
0
0
0
1
0
0
1
0
2
1
2
0
0
0
0

result:

ok 50 tokens

Test #2:

score: 0
Accepted
time: 6ms
memory: 8572kb

input:

50 50
48 29 36145
47 29 82496
35 47 66171
10 47 40597
39 48 64355
50 48 98687
43 39 15472
46 35 3729...

output:

0
0
0
4
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3
1
0
13
0
0
1
0
1
1
0
20
1
1
0
6
0
0
1

result:

ok 50 tokens

Test #3:

score: 0
Accepted
time: 5ms
memory: 8576kb

input:

50 50
48 29 38855
47 29 33850
35 29 87324
10 29 73658
39 48 22299
50 47 14355
43 29 86962
46 47 4177...

output:

0
0
1
0
9
0
0
1
0
0
0
0
0
2
0
0
0
0
1
0
0
2
2
0
0
0
0
0
0
2
0
0
0
0
0
0
0
0
3
58
0
0
0
0
0
3
0
0
0
0

result:

ok 50 tokens

Test #4:

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

input:

50 50
48 29 25212
47 48 68851
35 48 24830
10 47 90366
39 10 96596
50 10 30023
43 47 58452
46 29 2989...

output:

0
6
0
1
7
4
0
3
1
0
7
0
1
0
0
0
0
4
0
6
0
7
0
0
0
4
2
3
0
0
0
6
0
0
0
0
0
4
6
3
1
3
0
0
4
0
0
0
1
6

result:

ok 50 tokens

Test #5:

score: 0
Accepted
time: 4ms
memory: 8560kb

input:

50 50
48 29 27922
47 29 20205
35 29 45983
10 48 7074
39 10 54540
50 29 62044
43 10 29942
46 43 34375...

output:

0
0
0
0
14
0
0
0
2
0
0
2
1
0
0
0
0
0
0
3
0
0
0
2
0
1
1
0
0
18
0
0
0
0
0
0
3
1
1
0
1
0
0
0
2
0
0
25
1...

result:

ok 50 tokens

Test #6:

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

input:

50 50
48 29 14279
47 48 71559
35 48 83489
10 35 23782
39 47 12484
50 47 77712
43 50 1432
46 50 22499...

output:

0
0
1
3
24
0
0
0
1
2
0
10
2
0
0
0
1
0
0
0
0
0
10
0
0
0
0
0
0
1
0
0
1
0
1
2
0
0
0
0
0
0
0
93
0
0
0
0
...

result:

ok 50 tokens

Test #7:

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

input:

50 50
48 29 636
47 29 22913
35 47 4642
10 47 40490
39 29 70428
50 10 9733
43 48 72922
46 10 26976
14...

output:

2
0
0
0
3
0
0
7
0
1
1
0
99
0
0
0
0
0
0
0
0
0
0
0
0
99
0
2
2
0
6
3
0
0
2
0
0
3
4
0
0
3
0
0
1
0
1
1
2
0

result:

ok 50 tokens

Test #8:

score: 0
Accepted
time: 4ms
memory: 8560kb

input:

50 50
48 29 3346
47 48 57914
35 29 42148
10 29 73551
39 29 44725
50 39 25401
43 35 60765
46 35 15100...

output:

0
1225
0
0
0
0
0
0
0
0
0
1
1
0
0
1
0
2
0
0
0
0
0
0
0
0
3
0
0
2
0
0
1
0
0
0
0
0
2
0
0
0
0
4
0
1
0
0
0
2

result:

ok 50 tokens

Test #9:

score: 0
Accepted
time: 5ms
memory: 8568kb

input:

50 50
48 29 89703
47 29 9268
35 47 63301
10 35 90259
39 35 2669
50 48 41069
43 39 32255
46 47 19577
...

output:

0
0
0
2
0
0
0
0
0
1
0
5
1
0
0
1
0
2
0
1
2
0
5
0
6
0
3
4
0
0
0
0
3
0
0
0
5
2
0
3
0
0
1
0
0
0
0
0
0
0

result:

ok 50 tokens

Test #10:

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

input:

50 50
48 29 92413
47 48 60622
35 29 807
10 48 6967
39 35 60613
50 35 73090
43 29 3745
46 29 7701
14 ...

output:

0
0
0
0
0
0
0
0
1
1
1
0
1
0
0
1
2
1
2
0
0
4
0
0
0
0
0
0
0
2
0
0
0
0
7
0
1
0
0
0
0
0
0
0
6
0
2
0
0
0

result:

ok 50 tokens

Subtask #2:

score: 0
Wrong Answer

Test #11:

score: 0
Wrong Answer
time: 305ms
memory: 23912kb

input:

100000 100000
73595 40695 76
13615 40695 96
65545 13615 84
19391 13615 76
2353 73595 27
26730 40695 ...

output:

12815
1065
2028
53298
627586
19060
4345
1010
16097
1032
1044
1054
3191
16097
1055
627586
19060
978
9...

result:

wrong answer 20th words differ - expected: '4999950000', found: '704982704'

Subtask #3:

score: 0
Wrong Answer

Test #21:

score: 0
Wrong Answer
time: 366ms
memory: 24364kb

input:

100000 100000
73595 40695 12816
13615 73595 81821
65545 40695 75866
19391 65545 1165
2353 73595 3737...

output:

8
2270
96901
1
2085
1584
4228
9112
6
0
2
0
11
1035
2833
0
704982704
2
2
1294
6056
2221
4398
1587
497...

result:

wrong answer 17th words differ - expected: '4999950000', found: '704982704'