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

#GL0000368. Ước chung lớn nhất của tổng láng giềng (Instant Noodles — GCD of Neighborhood Sums)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Ước chung lớn nhất của tổng láng giềng (Instant Noodles — GCD of Neighborhood Sums)

Instant Noodles — GCD of Neighborhood Sums

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a bipartite graph with nn left vertices L1,…,LnL_1,\ldots,L_n and nn right vertices R1,…,RnR_1,\ldots,R_n. Every right vertex RiR_i has a positive integer weight cic_i.

For a nonempty subset SS of left vertices, let N(S)N(S) be the set of right vertices adjacent to at least one vertex in SS. Each right vertex is counted once, even if it has multiple neighbors in SS. Define

f(S)=∑Ri∈N(S)ci.f(S)=\sum_{R_i\in N(S)}c_i.

Find the GCD of f(S)f(S) over all nonempty subsets SS of the left side. If every such sum is zero, output zero.

Edges are distinct. PHOJ adaptation: unlike the original multi-test input, each input file contains one case only, without an initial test count tt.

Input

  • Line 1: n,mn,m, where mm is the number of edges.
  • Line 2: the weights c1,…,cnc_1,\ldots,c_n of the right vertices.
  • Next mm lines: u,vu,v, an edge between left vertex LuL_u and right vertex RvR_v.

Output

Print one integer: the GCD of the values f(S)f(S) over all nonempty left subsets SS.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le8; 1≤m≤n21\le m\le n^2; 1≤ci≤1001\le c_i\le100.
  • Subtask 2 (30 points): 1≤n,m≤25001\le n,m\le2500; 1≤ci≤1061\le c_i\le10^6.
  • Subtask 3 (50 points): 1≤n,m≤5000001\le n,m\le500000; 1≤ci≤10121\le c_i\le10^{12}.

All edge indices lie in [1,n][1,n] and there are no duplicate edges.

Examples

Example 1

Input

2 4
1 1
1 1
1 2
2 1
2 2

Output

2

Explanation

Either left vertex reaches both right vertices, so every nonempty SS has f(S)=1+1=2f(S)=1+1=2. The GCD is 22.

Example 2

Input

4 7
36 31 96 29
1 2
1 3
1 4
2 2
2 4
3 1
4 3

Output

12

Explanation

Right vertices have left-neighbor sets {3}\{3\}, {1,2}\{1,2\}, {1,4}\{1,4\}, and {1,2}\{1,2\} respectively. Grouping equal neighbor sets gives total weights 3636, 31+29=6031+29=60, and 9696. The answer is gcd⁡(36,60,96)=12\gcd(36,60,96)=12.