PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SP0000243. Yash và cây nguyên tố (Yash And Trees)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Yash và cây nguyên tố (Yash And Trees)

Yash And Trees

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a tree with nn vertices rooted at vertex 11. Each vertex ii holds a non-negative integer aia_i, and a positive integer mm is also given. The subtree of vv consists of vv and all its descendants under this rooting.

Process queries in order:

  • 1 v x: increase aua_u by xx for every vertex uu in the subtree of vv.
  • 2 v: count the distinct primes p<mp<m for which there exists a vertex uu in the subtree of vv and a non-negative integer kk such that au=p+mka_u=p+mk.

Each prime is counted only once even when several vertices witness it; 11 is not prime. The tree topology never changes.

Input

  • The first line contains n,mn,m.
  • The second line contains exactly nn integers a1,…,ana_1,\ldots,a_n.
  • Each of the next n−1n-1 lines specifies an undirected tree edge u,vu,v.
  • The next line contains integer qq.
  • Each of the final qq lines contains 1 v x or 2 v. Vertices are one-based. At least one query is of type 2.

Output

For each 2 v query, print the number of distinct suitable primes p<mp<m on its own line. Type 1 queries produce no output.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le30; 1≤m≤201\le m\le20; 0≤ai≤1040\le a_i\le10^4; a connected loop-free tree with n−1n-1 edges; 1≤q≤501\le q\le50; 1≤v≤n1\le v\le n; 0≤x≤1040\le x\le10^4; at least one type 2 query.

  • Subtask 2 (30 points): 1≤n≤30001\le n\le3000; 1≤m≤2001\le m\le200; 0≤ai≤1060\le a_i\le10^6; a connected loop-free tree with n−1n-1 edges; 1≤q≤50001\le q\le5000; 1≤v≤n1\le v\le n; 0≤x≤1060\le x\le10^6; at least one type 2 query.

  • Subtask 3 (50 points): 1≤n≤1051\le n\le10^5; 1≤m≤10001\le m\le1000; 0≤ai≤1090\le a_i\le10^9; a connected loop-free tree with n−1n-1 edges; 1≤q≤1051\le q\le10^5; 1≤v≤n1\le v\le n; 0≤x≤1090\le x\le10^9; at least one type 2 query.

Examples

Input

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

Output

3
1
1

Explanation

The primes below 2020 are 2,3,5,7,11,13,17,192,3,5,7,11,13,17,19. Query 2 1 checks all nodes and sees three distinct prime residues 3,7,113,7,11, so it prints 3. Operation 1 1 1 raises every value by 11, producing [4,8,10,9,5,12,8,4][4,8,10,9,5,12,8,4]. The subtree of vertex 55 contains vertices 55 and 88 with values 5,45,4, giving only residue 55: print 1. The subtree of vertex 44 contains vertices 4,5,6,7,84,5,6,7,8 with values 9,5,12,8,49,5,12,8,4, again giving only prime residue 55: print 1.