Instant Noodles
Time limit2sMemory limit1024 MB
Given a bipartite graph with weights on the right side, find the gcd of the sums of right-vertex values covered by each nonempty subset of left vertices.
- Level
Hard8 of 10
- Topics
- Hash map, Number theory
- Solved
- No attempts yet
Problem
Wu got hungry after an intense training session, and came to a nearby store to buy his favourite instant noodles. After Wu paid for his purchase, the cashier gave him an interesting task.
You are given a bipartite graph with positive integers in all vertices of the right half. For a subset of vertices of the left half we define as the set of all vertices of the right half adjacent to at least one vertex in , and as the sum of all numbers in vertices of . Find the greatest common divisor of for all possible non-empty subsets .
Wu is too tired after his training to solve this problem. Help him!
Input
The first line contains a single integer (), the number of test cases. Test case descriptions follow.
The first line of each case contains two integers and (), the number of vertices in either half of the graph and the number of edges.
The second line contains integers (). The -th number is the integer written on vertex of the right half.
Each of the following lines contains a pair of integers and (), meaning there is an edge between vertex of the left half and vertex of the right half. No multiple edges are given.
Test case descriptions are separated with empty lines. The total of across all test cases does not exceed 500,000, and the total of does not exceed 500,000 as well.
Output
For each test case, print a single integer, the required greatest common divisor.
Hint
The greatest common divisor of a set of integers is the largest integer such that every element of the set is divisible by .
In the first sample case, every left vertex is connected to every right vertex, so for every non-empty subset , and the greatest common divisor is 2.
In the second sample case, the subset of the left half is connected to the right vertices , and their sum is 2. The subset is connected to the right vertices , with sum 3. So and , and the greatest common divisor of all values of is 1.