CF633G.Yash And Trees
省选/NOI-
通过率:0%
时间限制:4.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Yash loves playing with trees and gets especially excited when they have something to do with prime numbers. On his 20th birthday he was granted with a rooted tree of n nodes to answer queries on. Hearing of prime numbers on trees, Yash gets too intoxicated with excitement and asks you to help out and answer queries on trees for him. Tree is rooted at node 1. Each node i has some value a__i associated with it. Also, integer m is given.
There are queries of two types:
- for given node v and integer value x, increase all a__i in the subtree of node v by value x
- for given node v, find the number of prime numbers p less than m, for which there exists a node u in the subtree of v and a non-negative integer value k, such that a__u = p + m·k.
亚什热爱玩树,尤其当树与素数有关时,他格外兴奋。在他20岁生日那天,他获得了一棵含 n 个节点的有根树,用于回答各种查询。一听说树上涉及素数,亚什便激动得难以自持,于是请求你帮忙为他回答这些关于树的查询。该树以节点 1 为根。每个节点 i 都关联一个值 ai。此外,还给定一个整数 m。
查询分为两类:
- 给定节点 v 和整数值 x,将节点 v 的子树中所有 ai 均增加 x;
- 给定节点 v,求满足以下条件的素数 p(p<m)的个数:存在节点 u(u 在 v 的子树中)以及非负整数 k,使得 au=p+m⋅k。
输入格式
The first of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ 1000) — the number of nodes in the tree and value m from the problem statement, respectively.
The second line consists of n integers a__i (0 ≤ a__i ≤ 109) — initial values of the nodes.
Then follow n - 1 lines that describe the tree. Each of them contains two integers u__i and v__i (1 ≤ u__i, v__i ≤ n) — indices of nodes connected by the i-th edge.
Next line contains a single integer q (1 ≤ q ≤ 100 000) — the number of queries to proceed.
Each of the last q lines is either 1 v x or 2 v (1 ≤ v ≤ n, 0 ≤ x ≤ 109), giving the query of the first or the second type, respectively. It's guaranteed that there will be at least one query of the second type.
输入的第一行包含两个整数 n 和 m(1≤n≤100000,1≤m≤1000),分别表示树中节点的数量以及题目描述中给出的参数 m。
第二行包含 n 个整数 ai(0≤ai≤109),表示各节点的初始值。
接下来 n−1 行描述该树的结构。每行包含两个整数 ui 和 vi(1≤ui,vi≤n),表示第 i 条边所连接的两个节点的编号。
下一行包含一个整数 q(1≤q≤100000),表示需要处理的查询数量。
最后 q 行中的每一行均为形如 1 v x 或 2 v 的查询(其中 1≤v≤n,0≤x≤109),分别表示第一类或第二类查询。保证至少存在一个第二类查询。
输出格式
For each of the queries of the second type print the number of suitable prime numbers.
对于每个第二类查询,输出符合条件的质数的个数。
输入输出样例
输入#1
8 20 3 7 9 8 4 11 7 3 1 2 1 3 3 4 4 5 4 6 4 7 5 8 4 2 1 1 1 1 2 5 2 4
输出#1
3 1 1
输入#2
5 10 8 7 5 1 0 1 2 2 3 1 5 2 4 3 1 1 0 1 1 2 2 2
输出#2
2
输入解题思路,AI测评打分。不知道怎么写?