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;
} 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值