Codeforces B. Minimum Possible LCM (greed number theory)

Subject description:

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 a1,a2,…,an (1≤ai≤107), where ai is the i-th element of a.is the i.

Output

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

Ideas:

Entitled minimum required set of numbers in a least common multiple of the two numbers. The beginning of a straightforward idea is to enumerate, to find out the number of two per gcd, lcm gcd of two numbers each according to demand. The time complexity of this approach is O ( \ (n-2 ^ \ n-log_2 \) ), look in the subject data range, clearly not scientific, limit 4 seconds, \ (^ {10} 12 is log_210. 6 ^ { } \) , will be far out. How to do?

Let's think about the general lcm problem with gcd problem is linked. To how to find, since the range of data is given, the number of factors to consider enumeration, to start from 1 \ [10 ^ 7 \] , find a factor of two in the minimum number of the greatest common divisor of the number of columns in the answer . why?

Suppose now enumerate the common factor d, the number of columns is a multiple of d has \ (x_1 \) < \ (x_2 \) < \ (X_3 \) <... < \ (x_n \) , if d is the \ ( x_1 \) , \ (x_2 \) of gcd, then it satisfies the condition, x1, x2 certainly the least common multiple of the minimum (when d is the factor). If d is not x1, x2 of gcd, it was not the number of gcd behind, then the maximum would not be the smallest common multiple.

Since d is from small to large enumeration, if the condition in d, the optimal solution is certainly topical. If you are not satisfied d is gcd, d ++, continue enumeration until satisfied. Since the algorithm will terminate, there is a guarantee correctness of the algorithm. Complexity of the algorithm is O ( \ (n-\ n-log_2 \) )

Note that when there are duplicate elements of the case, then this is the least common multiple of the elements themselves, but can only be repeated when the smallest element, because lcm if it is larger than the repeating elements must be greater than it will not be a global minimum LCM, continuously covering the individual at the time of input, to leave a minimum.

Note LLONG_MAX and LONG_MAX is not the same, I started wrong, because the original number is not big enough.

Code:

#include <iostream>
#include <climits>
#define INF LLONG_MAX
#define max_n 10000007
using namespace std;
long long a[max_n];
int n;
int pos[max_n];
long long ans = 0;
long long minm = INF;
int x = 0;
int y = 0;
long long gcd(long long a,long long b)
{
    return (b==0)?a:gcd(b,a%b);
}
int main()
{
    cin >> n;
    for(int i = 1;i<=n;i++)
    {
        int v;
        cin >> v;
        a[v]++;
        if(a[v]>1&&v<minm)
        {
            minm = v;
            x = pos[v];
            y = i;
        }
        pos[v] = i;
    }
    for(int i = 1;i<max_n;i++)
    {
        long long v = 0;
        for(int j = i;j<max_n;j+=i)
        {
            if(a[j]==0)
            {
                continue;
            }
            if(v==0)
            {
                v = j;
            }
            else
            {
                long long g = gcd(v/i,j/i);
                if(g==1)
                {
                    ans = (long long)j/i*v;
                    if(ans<minm)
                    {
                        //cout << "v " << v << " j " << j << endl;
                        minm = ans;
                        x = pos[v];
                        y = pos[j];
                    }
                }
                break;

            }
        }
    }
    if(x>y) swap(x,y);
    cout << x <<  " " << y << endl;
    return 0;
}

Reference article:

KobeDuu,Minimum Possible LCM【枚举】,https://blog.csdn.net/qq_41157137/article/details/89353527

Guess you like

Origin www.cnblogs.com/zhanhonhao/p/11260616.html