G. 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 aa consisting of nn integers a1,a2,…,ana1,a2,…,an.
Your problem is to find such pair of indices i,ji,j (1≤i<j≤n1≤i<j≤n) that lcm(ai,aj)lcm(ai,aj) is minimum possible.
lcm(x,y)lcm(x,y) is the least common multiple of xx and yy (minimum positive number such that both xx and yy are divisors of this number).
Input
The first line of the input contains one integer nn (2≤n≤1062≤n≤106) — the number of elements in aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1071≤ai≤107), where aiai is the ii-th element of aa.
Output
Print two integers ii and jj (1≤i<j≤n1≤i<j≤n) such that the value of lcm(ai,aj)lcm(ai,aj) is minimum among all valid pairs i,ji,j. If there are multiple answers, you can print any.
Examples
input
Copy
5 2 4 8 3 6
output
Copy
1 2
input
Copy
5 5 2 11 3 7
output
Copy
2 4
input
Copy
6 2 5 10 1 10 2
output
Copy
1 4
分析:LCM(i,j)=i*j/gcd,a[i]<=1e7,那么gcd也<=1e7,我们就可以枚举gcd,记录一下a[i]是否出现,然后用gcd筛出i,j。
总复杂度应该是。
#include "bits/stdc++.h"
using namespace std;
int vis[10000004];
int main() {
int n;
int x;
cin >> n;
memset(vis, 0, sizeof(vis));
int ansi, ansj;
long long ans = 1e14;
for (int i = 1; i <= n; ++i) {
scanf("%d", &x);
if (!vis[x])vis[x] = i;
else {
if (ans > x) {
ansi = vis[x];
ansj = i;
ans = x;
}
}
}
int ti, tj;
for (int i = 1; i < 10000004; ++i) {
ti = tj = 0;
int temp;
for (int j = i; j < 10000004; j += i) {
if (!vis[j])continue;
if (!ti) {
ti = vis[j];
temp = j;
} else {
tj = vis[j];
if (ans > 1LL * temp * j / i) {
ans = 1LL * temp * j / i;
ansi = ti;
ansj = tj;
}
}
}
}
if (ansi > ansj)swap(ansi, ansj);
printf("%d %d\n", ansi, ansj);
}