Codeforces B. Minimum Possible LCM

题目描述:

B. Minimum Possible LCM

time limit per test

4 seconds

memory limit per test

1024 megabytes

input

standard input

output

standard output

You are given an array aconsisting of integers a1,a2,…,*a**n*

Your problem is to find such pair of indices i,j that lcm(\[a_i\],**\[a_j\]*)is minimum possible.

lcm(x,y) is the least common multiple of and x and y(minimum positive number such that both x and y are divisors of this number).

Input

The first line of the input contains one integer n — the number of elements in a

The second line of the input contains n

integers (1≤*a**i≤107 is the i*.

Output

Print two integers i and (1≤i<jn is minimum among all valid pairs i,j

思路:

题目是要求一组数中两个数的最小公倍数的最小值。刚开始一个直白的想法就是枚举,把每两个数的gcd求出来,根据gcd求每两个数的lcm。这种做法的时间复杂度为O(\[n^2\log_2 n\]),在看看题目的数据范围,显然不太科学,限时4秒,\(10^{12}log_210^{6}\),会远远超时。怎么办?

我们来想一想,一般lcm问题与gcd问题是挂钩的。怎么样来求,由于数据的范围给定了,考虑枚举数的因子,从1开始到\[10^7\],在数列中找到一因子为最大公约数的两个最小数,就是答案。为什么?

假设现在枚举到了公因子d,数列中是d的倍数的有\(x_1\)<\(x_2\)<\(x_3\)<...<\(x_n\),如果d是\(x_1\),\(x_2\)的gcd,那么也就满足条件,x1,x2的最小公倍数肯定最小(在d为因子时)。如果d不是x1,x2的gcd,那也不是后面数的gcd,那么最大公倍数就不会最小。

由于d是从小到大枚举的,如果在d时满足条件,肯定为局部最优解。如果都不满足d为gcd,d++,继续枚举直到满足。由于算法一定会终止,算法的正确性就有了保障。算法复杂度是O(\(n\log_2 n\))

需要注意的是当元素有重复的情况,那么这种元素的最小公倍数就是本身,而且只可能是最小重复元素的时候,因为如果比它大的重复元素的lcm一定大于它,不会是全局最小lcm,单独在输入的时候不断覆盖,留下最小的一种即可。

注意LLONG_MAX和LONG_MAX是不一样的,我一开始错了,原来因为是数不够大。

上一篇:洛咕 【P1891】疯狂LCM & 三倍经验


下一篇:[欧拉函数] Bzoj P2226 LCMSum