码迷,mamicode.com
首页 > 其他好文 > 详细

数论(lcm)

时间:2019-11-08 16:22:08      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:out   com   first   org   type   long   std   ack   absolute   

CodeForces - 1154G

You are given an array a consisting of n integers a1,a2,,an

.

Your problem is to find such pair of indices i,j

(1i<jn) 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

(2n106) — the number of elements in a

.

The second line of the input contains n

integers a1,a2,,an (1ai107), where ai is the i-th element of a

.

Output

Print two integers i

and j (1i<jn) 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.

Examples
Input
5
2 4 8 3 6
Output
1 2
Input
5
5 2 11 3 7
Output
2 4
Input
6
2 5 10 1 10 2
Output
1 4
题意:求最小的lcm(a,b)的下标;
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f3f3f3f3fLL
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
const int N = 1e7 + 5 ;
int p[N] , n;


int main()
{
    int x , flaga , flagb ;//值,左下标,右下标
    ll minv ;//最小lcm值
    while(~scanf("%d" , &n))
    {
        memset(p , 0 , sizeof(p));//标记数组
        minv = INF ;
        for(int i = 1 ; i <= n ; i++)
        {
            scanf("%d" , &x);
            if(p[x] && x < minv)//出现了两次且小于原lcm,更新
            {
                minv = x ;
                flaga = p[x];
                flagb = i ;
            }
            p[x] = i ;//标记该数出现
        }
        for(int i = 1 ; i < N && i < minv ; i++)//遍历1-1e7+5数
        {
            ll v = 0 ;
            int pos ;
            for(int j = i ; j < N && j < minv ; j+=i)//成倍数的遍历。
            {
                if(p[j])//该数出现
                {
                    if(v==0)//第一个数
                    {
                        v = j ;
                        pos = p[j];
                    }
                    else if(v / i * j < minv)//第二个数出现,且小于原lcm更新。
                    {
                        minv = v / i * j ;
                        flaga = pos ;
                        flagb = p[j];
                    }
                    else
                    {
                        break ;
                    }
                }
            }
        }
        if(flaga > flagb) swap(flaga , flagb);
        cout << flaga << " " << flagb << endl;
    }

    return 0 ;
}

 

数论(lcm)

标签:out   com   first   org   type   long   std   ack   absolute   

原文地址:https://www.cnblogs.com/nonames/p/11821002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!