题目描述:

B. 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 aconsisting of integers a1,a2,…,a**n

Your problem is to find such pair of indices i,j that lcm(\(a_i\),\(a_j\))is minimum possible.

lcm(x,y) is the least common multiple of and 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 — 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.is the i.

Output

Print two integers i and (1≤i<jn is minimum among all valid pairs i,j

思路:

题目是要求一组数中两个数的最小公倍数的最小值。刚开始一个直白的想法就是枚举,把每两个数的gcd求出来,根据gcd求每两个数的lcm。这种做法的时间复杂度为O(\(n^2\log_2 n\)),在看看题目的数据范围,显然不太科学,限时4秒,\(10^{12}log_210^{6}\),会远远超时。怎么办?

我们来想一想,一般lcm问题与gcd问题是挂钩的。怎么样来求,由于数据的范围给定了,考虑枚举数的因子,从1开始到\(10^7\),在数列中找到一因子为最大公约数的两个最小数,就是答案。为什么?

假设现在枚举到了公因子d,数列中是d的倍数的有\(x_1\)<\(x_2\)<\(x_3\)<...<\(x_n\),如果d是\(x_1\),\(x_2\)的gcd,那么也就满足条件,x1,x2的最小公倍数肯定最小(在d为因子时)。如果d不是x1,x2的gcd,那也不是后面数的gcd,那么最大公倍数就不会最小。

由于d是从小到大枚举的,如果在d时满足条件,肯定为局部最优解。如果都不满足d为gcd,d++,继续枚举直到满足。由于算法一定会终止,算法的正确性就有了保障。算法复杂度是O(\(n\log_2 n\))

需要注意的是当元素有重复的情况,那么这种元素的最小公倍数就是本身,而且只可能是最小重复元素的时候,因为如果比它大的重复元素的lcm一定大于它,不会是全局最小lcm,单独在输入的时候不断覆盖,留下最小的一种即可。

注意LLONG_MAX和LONG_MAX是不一样的,我一开始错了,原来因为是数不够大。

代码:

#include <iostream>
#include <climits>
#define INF LLONG_MAX
#define max_n 10000007
using namespace std;
long long a[max_n];
int n;
int pos[max_n];
long long ans = 0;
long long minm = INF;
int x = 0;
int y = 0;
long long gcd(long long a,long long b)
{
return (b==0)?a:gcd(b,a%b);
}
int main()
{
cin >> n;
for(int i = 1;i<=n;i++)
{
int v;
cin >> v;
a[v]++;
if(a[v]>1&&v<minm)
{
minm = v;
x = pos[v];
y = i;
}
pos[v] = i;
}
for(int i = 1;i<max_n;i++)
{
long long v = 0;
for(int j = i;j<max_n;j+=i)
{
if(a[j]==0)
{
continue;
}
if(v==0)
{
v = j;
}
else
{
long long g = gcd(v/i,j/i);
if(g==1)
{
ans = (long long)j/i*v;
if(ans<minm)
{
//cout << "v " << v << " j " << j << endl;
minm = ans;
x = pos[v];
y = pos[j];
}
}
break; }
}
}
if(x>y) swap(x,y);
cout << x << " " << y << endl;
return 0;
}

参考文章:

KobeDuu,Minimum Possible LCM【枚举】,https://blog.csdn.net/qq_41157137/article/details/89353527

Codeforces B. Minimum Possible LCM(贪心数论)的更多相关文章

  1. Codeforces 1154G Minimum Possible LCM

    题目链接:http://codeforces.com/problemset/problem/1154/G 题目大意: 给定n个数,在这些数中选2个数,使这两个数的最小公倍数最小,输出这两个数的下标(如 ...

  2. Minimum Sum LCM(uva10791+和最小的LCM+推理)

    L - Minimum Sum LCM Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  3. UVA.10791 Minimum Sum LCM (唯一分解定理)

    UVA.10791 Minimum Sum LCM (唯一分解定理) 题意分析 也是利用唯一分解定理,但是要注意,分解的时候要循环(sqrt(num+1))次,并要对最后的num结果进行判断. 代码总 ...

  4. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  5. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  6. codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)

    You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the ...

  7. 【贪心】codeforces D. Minimum number of steps

    http://codeforces.com/contest/805/problem/D [思路] 要使最后的字符串不出现ab字样,贪心的从后面开始更换ab为bba,并且字符串以"abbbb. ...

  8. Codeforces 515C 题解(贪心+数论)(思维题)

    题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...

  9. Codeforces 798C - Mike and gcd problem(贪心+数论)

    题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...

随机推荐

  1. EF的 NoTracking 的一些记录

    NoTracking官方解释 跟踪与非跟踪查询 跟踪行为可控制 Entity Framework Core 是否将有关实体实例的信息保留在其更改跟踪器中. 如果已跟踪某个实体,则该实体中检测到的任何更 ...

  2. maven 国内镜像

    <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given reposi ...

  3. [LOJ3053]希望

    对于一组$s_{1\cdots k}$,合法的$u$构成一个连通块,满足$\left\lvert V\right\rvert-\left\lvert E\right\rvert=1$ 考虑算出算$f_ ...

  4. WIFI-Direct(Wifi直连)、AirPlay、DLAN、Miracast功能介绍

    不知道大家对无线同屏技术有多少了解,当这种技术普及的时候,我想我们的工作与生活又会方便很多吧!下面是目前三种主流同屏技术的介绍: 目前这种将终端信号经由WiFi传输到电视.电视盒的技术有三种:DLNA ...

  5. (三)Python知识图谱

    查看大图

  6. Maven手动导本地jar到项目

    第一步:先把目标jar安装在本地,下面是安装到本地的步骤:在cmd命令中,输入:mvn install:install-file -Dfile=C:\Users\Ter\Desktop\jqd_doc ...

  7. Python Tkinter的学习

    Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.Tk和Tkinter可以在大多数的Unix平台下使用,同样可以应用在Windows和Macinto ...

  8. 【Tyvj2046】掷骰子

    好水一道题 掷骰子Description Rainbow和Freda通过一次偶然的机会来到了魔界.魔界的大门上赫然写着:小盆友们,欢迎来到魔界~!乃们需要解决这样一个问题才能进入哦lala~有N枚骰子 ...

  9. Python之路【第十二篇】:Python面向对象高级

    一.反射 1 什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访问.检测和修改它本身状态或行为的一种能力(自省).这一概念的提出很快引发了计算机科学领域关于应用反射性的研究 ...

  10. Mysql的常见索引

    PRIMARY KEY(主键索引) ALTER TABLE table_name ADD PRIMARY KEY ( col ) 它是一种特殊的唯一索引,不允许有空值: UNIQUE(唯一索引) AL ...