题意

给你 \(n\) 个数 \(a_i\) ,求出 \(\text{lcm}\) 最小的一对数。

\(n\le 10^6, a_i\le 10^7\)

题解

直接枚举 ,找到当前数最小的两个倍数,统计答案即可。

理论时间复杂度为 \(O(a_i\log a_i)\) ,实际运行效率要远高于此。

代码很好写。

#include<cstdio>
const int N=10000005;
int a[N],a2[N],aid[3],mx,n;
long long ans=1ll<<60;
inline int gi()
{
char c=getchar(); int x=0;
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+c-'0';
return x;
}
int main()
{
n=gi();
for(int i=1;i<=n;++i)
{
int x=gi();
a[x]?a2[x]=i:a[x]=i;
if(x>mx) mx=x;
}
for(int i=1;i<=mx;++i)
{
int v[3],id[3],tot=0;
for(int j=i;tot<2&&j<=mx;j+=i)
if(a[j])
{
v[++tot]=j,id[tot]=a[j];
if(a2[j]&&tot!=2) v[++tot]=j,id[tot]=a2[j];
}
if(tot==2&&ans>1ll*v[1]*v[2]/i)
{
ans=1ll*v[1]*v[2]/i;
aid[1]=id[1],aid[2]=id[2];
}
}
if(aid[1]>aid[2]) aid[1]^=aid[2]^=aid[1]^=aid[2];
printf("%d %d",aid[1],aid[2]);
}

【CF1154G】Minimum Possible LCM的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【POJ2516】Minimum Cost

    [POJ2516]Minimum Cost 题意:有N个收购商.M个供应商.K种物品.对于每种物品,每个供应商的供应量和每个收购商的需求量已知.每个供应商与每个收购商之间运送该物品的运费已知.求满足收 ...

  3. 【51NOD-0】1012 最小公倍数LCM

    [算法]欧几里德算法 #include<cstdio> int gcd(int a,int b) {?a:gcd(b,a%b);} int main() { int a,b; scanf( ...

  4. 【Leetcode】【Easy】Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  5. 【leetcode】Minimum Depth of Binary Tree

    题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

  6. 【leetcode】Minimum Path Sum

    Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...

  7. 【leetcode】Minimum Window Substring (hard) ★

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. 【leetcode】Minimum Depth of Binary Tree (easy)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. 【hdu1394】Minimum Inversion Number

    Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of ...

随机推荐

  1. sqlserver 面试题

    1. --是查询A(ID,Name)表中第11至20条记录,ID作为主键可能是不是连续增长的列,完整的查询语句如下: SELECT TOP 10 * FROM dbo.Employee WHERE E ...

  2. 关于Simulink的sample time的问题

    在对simulink建模的过程中,有时候会遇到sample time出现错误的问题,比如下图是我在使用simulink自带的Recursive least square Estimator最小二乘估计 ...

  3. 2_01_MSSQL课程_查询

    查询 1.查询结果的别名 列 as 别名 (建议用这种) 列 别名 别名=列 Select sum(score1) as 成绩1,sum(score2)as  成绩2 from talScore 2. ...

  4. Angular4之时间管道

    {{时间戳 |date:“yyyy/MM/dd HH:mm:ss”}} “YYYY/MM/DD”不可

  5. springboot#配置文件处理

    1. 加载自定义属性文件 2. 通过bean聚合相关属性 1. 在启动类上通过如下注解可以加载自定义的属性文件 @PropertySource(value = {"classpath:pro ...

  6. SciPy 教程

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  7. django+centos+mariadb读写分离完美实现(上)-mysql主从备份实现

    首先画图一张,用来展示今天要做的事情,读写分离,个人理解就是使用mysql主从备份的原理,让两个数据库同时为自己提供服务.其中主库负责数据保存,从库负责数据展示,可以一主一从,也可以一主多从.从而降低 ...

  8. Tomcat删除时问题——eclipse部署tomcat时弹出Resource'/Servers' does not exist

    如果你删除一个项目的Servers文件,或者相应文件损坏等,会出现错误, Resource '/Servers' does not exist 那么就需要把它在控制台出的Servers下所部署的Tom ...

  9. Ubuntu下安装 Mysql

    MYSQL在ubuntu16.04下的编译安装mysql-5.6.23.tar.gz 为减少安装过程中因权限带来个各种问题,建议全程用root用户编译安装,步骤如下: 1.安装依赖文件  apt-ge ...

  10. Redis Cluster 4.0.9 集群安装搭建

    Redis Cluster 4.0.9集群搭建步骤:yum install -y gcc g++ gcc-c++ make openssl cd redis-4.0.9 make mkdir -p / ...