CF1322C.Instant Noodles

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 SS of vertices of the left half we define N(S)N(S) as the set of all vertices of the right half adjacent to at least one vertex in SS, and f(S)f(S) as the sum of all numbers in vertices of N(S)N(S). Find the greatest common divisor of f(S)f(S) for all possible non-empty subsets SS (assume that GCD of empty set is 00).

Wu is too tired after his training to solve this problem. Help him!

吴在一次高强度训练后感到饥饿,于是来到附近一家商店购买他最爱的方便面。结账后,收银员给了他一个有趣的任务。

给定一个二分图,其右侧所有顶点上均标有正整数。对左侧顶点的任意子集 SS,定义 N(S)N(S) 为右侧中至少与 SS 中一个顶点相邻的所有顶点构成的集合,定义 f(S)f(S) 为 N(S)N(S) 中所有顶点上所标数字之和。求所有可能的非空子集 SS 对应的 f(S)f(S) 的最大公约数(约定空集的最大公约数为 00)。

吴训练后太过疲惫,无法解决这个问题。请帮助他!

输入格式

The first line contains a single integer tt (1≤t≤500 0001 \leq t \leq 500\,000) — the number of test cases in the given test set. Test case descriptions follow.

The first line of each case description contains two integers nn and mm (1 ≤ n, m ≤ 500 0001~\leq~n,~m~\leq~500\,000) — the number of vertices in either half of the graph, and the number of edges respectively.

The second line contains nn integers cic_i (1≤ci≤10121 \leq c_i \leq 10^{12}). The ii-th number describes the integer in the vertex ii of the right half of the graph.

Each of the following mm lines contains a pair of integers uiu_i and viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n), describing an edge between the vertex uiu_i of the left half and the vertex viv_i of the right half. It is guaranteed that the graph does not contain multiple edges.

Test case descriptions are separated with empty lines. The total value of nn across all test cases does not exceed 500 000500\,000, and the total value of mm across all test cases does not exceed 500 000500\,000 as well.

第一行包含一个整数 tt(1≤t≤500 0001 \leq t \leq 500\,000)—— 表示给定测试集中测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1 ≤ n, m ≤ 500 0001~\leq~n,~m~\leq~500\,000)—— 分别表示二分图左右两部分的顶点数以及边数。

第二行包含 nn 个整数 cic_i(1≤ci≤10121 \leq c_i \leq 10^{12})。其中第 ii 个数表示二分图右半部分中顶点 ii 上的整数值。

接下来的 mm 行,每行包含一对整数 uiu_i 和 viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n),表示左半部分的顶点 uiu_i 与右半部分的顶点 viv_i 之间存在一条边。保证图中不存在重边。

各测试用例的描述之间以空行分隔。所有测试用例中 nn 的总和不超过 500 000500\,000,所有测试用例中 mm 的总和也不超过 500 000500\,000。

输出格式

For each test case print a single integer — the required greatest common divisor.

对于每个测试用例,输出一个整数——即所求的最大公约数。

输入输出样例

  • 输入#1

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

    输出#1

    2
    1
    12

说明/提示

The greatest common divisor of a set of integers is the largest integer gg such that all elements of the set are divisible by gg.

In the first sample case vertices of the left half and vertices of the right half are pairwise connected, and f(S)f(S) for any non-empty subset is 22, thus the greatest common divisor of these values if also equal to 22.

In the second sample case the subset 1{1} in the left half is connected to vertices 1,2{1, 2} of the right half, with the sum of numbers equal to 22, and the subset 1,2{1, 2} in the left half is connected to vertices 1,2,3{1, 2, 3} of the right half, with the sum of numbers equal to 33. Thus, f(1)=2f({1}) = 2, f(1,2)=3f({1, 2}) = 3, which means that the greatest common divisor of all values of f(S)f(S) is 11.

一组整数的最大公约数是最大的整数 gg,使得该集合中的所有元素均能被 gg 整除。

在第一个样例中,左侧顶点集与右侧顶点集之间两两相连,因此对任意非空子集 SS,均有 f(S)=2f(S) = 2,故这些 f(S)f(S) 值的最大公约数也为 22。

在第二个样例中,左侧子集 {1}\{1\} 与右侧顶点 {1,2}\{1, 2\} 相连,其编号之和为 22;左侧子集 {1,2}\{1, 2\} 与右侧顶点 {1,2,3}\{1, 2, 3\} 相连,其编号之和为 33。因此,f({1})=2f(\{1\}) = 2,f({1,2})=3f(\{1, 2\}) = 3,这意味着所有 f(S)f(S) 值的最大公约数为 11。

输入解题思路,AI测评打分。不知道怎么写?

首页