数论(lcm)

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

(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 n

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.

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

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

时间: 2024-08-30 12:34:03

数论(lcm)的相关文章

bzoj5105 晨跑 数论lcm

“无体育,不清华”.”每天锻炼一小时,健康工作五十年,幸福生活一辈子”在清华,体育运动绝对是同学们生活中 不可或缺的一部分.为了响应学校的号召,模范好学生王队长决定坚持晨跑.不过由于种种原因,每天都早起去跑 步不太现实,所以王队长决定每a天晨跑一次.换句话说,假如王队长某天早起去跑了步,之后他会休息a-1天,然 后第a天继续去晨跑,并以此类推.王队长的好朋友小钦和小针深受王队长坚持锻炼的鼓舞,并决定自己也要坚持 晨跑.为了适宜自己的情况,小钦决定每b天早起跑步一次,而小针决定每c天早起跑步一次.

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

Uva 11388 GCD LCM ( 数论 )

Uva  11388 GCD LCM( 数论 ) 题意: 求是否存在a,b 使得lcm(a,b) = L, gcd(a,b) = G,不存在输出-1,存在输出a,b,且a尽可能小 分析: 强行暴力是不可能的数据很大,要用llu,这里有两种思路 思路一: 由题意可知 a*b = G*L 保证a = G的倍数的情况下,枚举a再判断G*L能否整除a,最后判断b是否为a的倍数.a从G开始扫到sqrt(G*L) //输入两个整数G,L //找出a,b 使得 gcd(a,b) = G lcm(a,b) =

HDU4497 GCD and LCM 数论 素数分解

题意很简单首先以前做最简单的LCM跟CGD的时候都知道先求出两个数A,B的最大公约数GCD,那么LCM可以利用  A*B/GCD来求得,这点一开始脑残了没想到,结果没有进行特盘所以错了,意思就是 题目给的L%G不为0的话就是无解,结果我给判其它的去了,肯定漏了些什么没有发现 然后对于 L/G进行素因子分解,同时任意的数都能够通过素因子分解来表示,所以三个解x,y,z也能分解 L/G = p1^q1*p2^q2.... x = p1^i1*... y = p1^j1*... z = p1^k1*.

数论入门2——gcd,lcm,exGCD,欧拉定理,乘法逆元,(ex)CRT,(ex)BSGS,(ex)Lucas,原根,Miller-Rabin,Pollard-Rho

数论入门2 另一种类型的数论... GCD,LCM 定义\(gcd(a,b)\)为a和b的最大公约数,\(lcm(a,b)\)为a和b的最小公倍数,则有: 将a和b分解质因数为\(a=p1^{a1}p2^{a2}p3^{a3}...pn^{an},b=p1^{b1}p2^{b2}p3^{b3}...pn^{bn}\),那么\(gcd(a,b)=\prod_{i=1}^{n}pi^{min(ai,bi)},lcm(a,b)=\prod_{i=1}^{n}pi^{max(ai,bi)}\)(0和任何

【数论】[CF258C]Little elephant and LCM

题目 分析:枚举最大数,然后找出它所有因数p1--.pk, 从中任意选取一些数,这些数的LCM|这个数且,这些数的最大LCM就是枚举的这个数,且若pi<=aj<=pi+1则前i个数可以放在j这个位置,即j这个位置有cj种选择,总方案数就是c1*c2*--*cj 作为优化,对于每个pi,我们枚举有aj满足pi<=aj<=pi+1的个数记为qi,则有ans=1^qi*2^qi*--*q^qk,但这些方案包含不选择最大数的情况,则最后一项应为q^qk-(q-1)^qk 代码: #incl

ACM数论之旅3---最大公约数gcd和最小公倍数lcm(苦海无边,回头是岸( ̄? ̄))

gcd(a, b),就是求a和b的最大公约数 lcm(a, b),就是求a和b的最小公倍数 然后有个公式 a*b = gcd * lcm     ( gcd就是gcd(a, b), ( •?∀•? ) 简写你懂吗) 解释(不想看就跳过){ 首先,求一个gcd,然后... a / gcd 和 b / gcd 这两个数互质了,也就是 gcd(   a / gcd ,b / gcd  )  =  1,然后... lcm = gcd *  (a / gcd) * (b / gcd) lcm = (a *

hdu 4497 GCD and LCM 数论 素数分解

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1339    Accepted Submission(s): 607 Problem Description Given two positive integers G and L, could you tell me how many solutions of

UVA - 10791 - Minimum Sum LCM (数论相关!)

题目链接:Minimum Sum LCM UVA - 10791 Minimum Sum LCM Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu SubmitStatus Description  Minimum Sum LCM  LCM (Least Common Multiple) of a set of integers is defined as the minimum number, whic

【数论】【中国剩余定理】【LCM】hdu1788 Chinese remainder theorem again

根据题目容易得到N%Mi=Mi-a. 那么可得N%Mi+a=Mi. 两侧同时对Mi取余,可得(N+a)%Mi=0. 将N+a看成一个变量,就可以把原问题转化成求Mi的LCM,最后减去a即可. #include<cstdio> #include<algorithm> #include<iostream> using namespace std; typedef long long ll; int K; ll a; int main(){ ll x; while(1){ c