Codeforces Round #641 Div2 C题解

题目

For the multiset of positive integers s={s1,s2,…,sk}, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of s as follow:

gcd(s) is the maximum positive integer x, such that all integers in s are divisible on x.
lcm(s) is the minimum positive integer x, that divisible on all integers from s.
For example, gcd({8,12})=4,gcd({12,18,6})=6 and lcm({4,6})=12. Note that for any positive integer x, gcd({x})=lcm({x})=x.

Orac has a sequence a with length n. He come up with the multiset t={lcm({ai,aj}) | i<j}, and asked you to find the value of gcd(t) for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence.

Input
The first line contains one integer n (2≤n≤100000).

The second line contains n integers, a1,a2,…,an (1≤ai≤200000).

Output
Print one integer: gcd({lcm({ai,aj}) | i<j}).

题意简单说就是给n个数,求gcd({lcm({ai,aj}) | i<j})即对所有两两数的lcm进行求gcd。
题解
先给出一个结论:对于最终的答案gcd:ans,如果p^k |ans,则在n个数中至少有n-1个数能够整除p^k。
这里不给出证明了。。实际上思考一下在题目中的gcd跟lcm也能够得出结论。
Solution1
定义集合di为a集合中删去ai剩下的数的集合,即d1={a2,a3,…,an}等等。因此对于d1,我们可以知道至少有n-1个数可以整除gcd(di)=gcd(a2,a3,…,an)。故通过前面的结论可以知道gcd(a2,a3,…,an)|ans即gcd(d1)|ans。同理可得gcd(d2)|ans,gcd(d3)|ans,…,gcd(dn)|ans即gcd(di)|ans(1<=i<=n),综上所述ans=lcm(gcd(d1),gcd(d2),gcd(d3)…,gcd(dn))。
对于gcd(di)如何求,切不可暴力求,不然铁GG。记pre(i)=gcd(a1,a2,…,ai),suf(i)=gcd(ai,ai+1,…,an)即前缀gcd,后缀gcd。对于gcd(di)其意义为a集合中除了ai以外所有数的gcd,故gcd(di)=gcd(pre(i-1),suf(i+1))表示为:ai前面的数的gcd与ai后面的数的gcd的gcd即为所求。

猜你喜欢

转载自blog.csdn.net/ylwhxht/article/details/106107443