cf552 G Minimum Possible LCMn個數,求最小得一對lcm

 

You are given an array a consisting of n integers a1,a2,…,an
Your problem is to find such pair of indices i,j  (1≤i<j≤n ) that lcm(ai,aj) is minimum possible.
lcm(x,y)  is the least common multiple of 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 (2≤n≤106 ) — the number of elements in a .
The second line of the input contains nn integers a1,a2,…,an (1≤ai≤107 ), where ai  is the  i-th element of a .
Output
Print two integers  i and  j (1≤i<j≤n ) such that the value of lcm(ai,aj) is minimum among all valid pairs i,j  If there are multiple answers, you can print any.
5
2 4 8 3 6
1 2
5
5 2 11 3 7
2 4
6
2 5 10 1 10 2
1 4
優美的暴力

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e7 + 5;
typedef long long ll;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n, x, p1, p2, vis[maxn], a[maxn];
ll q[maxn];
int gcd(int a,int b){
	if(!b)return a;
	return gcd(b,a%b);
}
int main(){
	while(scanf("%d",&n)!=EOF){
		ll ans=inf;
		int l,r;
		memset(vis,0,sizeof(vis));
		memset(a,0,sizeof(a));
		memset(q,0,sizeof(q));
		for(int i=1;i<=n;++i){
			scanf("%d",&x);
			a[i]=x;
			if(vis[x]&&x<ans){
				ans=x;
				l=vis[x];
				r=i;
			}
			vis[x]=i;
		}
		sort(a+1,a+n+1);
		int m=unique(a+1,a+n+1)-a-1;
		for(int i=1;i<=a[m];++i){
			int cur=-1;
			for(int j=i;j<maxn;j+=i){
				if(vis[j]) {
					q[++cur]=j;
					if(cur>=1)break;
				}
			}  
			if(cur>=1){
			ll d= gcd(q[0],q[1]);  
			   if(ans>q[0]/d*q[1]){
					ans= q[0]/d*q[1];
					l=vis[q[0]];
					r=vis[q[1]];
				}
			}
		}
		if(l>r)swap(l,r);
		
		cout<<l<<' '<<r<<endl;
	}
	return 0;
} 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章