题意

给你 \(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. 「ZJOI2006」物流运输

    题目 [内存限制:$256MiB$][时间限制:$1000ms$] [标准输入输出][题目类型:传统][评测方式:文本比较] [题目描述] 物流公司要把一批货物从码头 A 运到码头 B.由于货物量比较 ...

  2. session存取时 getOutputStream()和getWriter()问题

    情况1: 在使用httpResponse的getWriter()会写json是出现 getWriter() has already been called for this response,经我查看 ...

  3. 第1节 storm编程:4、storm环境安装以及storm编程模型介绍

    dataSource:数据源,生产数据的东西 spout:接收数据源过来的数据,然后将数据往下游发送 bolt:数据的处理逻辑单元.可以有很多个,基本上每个bolt都处理一部分工作,然后将数据继续往下 ...

  4. [经验] 项目中 session 过期后弹出的登录窗口无法登录怎么办

    背景: 当session过期后, 按照 系统的设计,  会自动跳转到登录页面, 重新进行登录操作 问题: 由于进入主页后, 其他页面都是嵌入式的模板页, 所以这时的登录页面也是内嵌在index模板下的 ...

  5. 解决maven项目创建过慢的问题,一直resolving dependencies...

    ##方法一: 1.在创建项目时设置其Properties (我大多在创建项目时 就解决这个问题) 创建项目的时候,在Properties属性面板中添加一项:archetypeCatalog = int ...

  6. php 实现店铺装修8

    /** * @title 店铺装修--根据分类获取商品列表 * @param source 是 int 来源(1--h5.2--app) * @param type 是 string 店铺类型--首页 ...

  7. 1-5SpringBoot操作之Spring-Data-Jpa(二)CRUD实现

    本帖来演示下SpringBoot下,实用Spring-Data-Jpa来实现CRUD操作,视图层采用Freemarker 这里我们先把application.properties修改成applicat ...

  8. pgsql数据库 pg_hba.conf 中 METHOD 的说明

    pg_hba.conf 文件是pgsql用于配置访问权限的配置文件,  内容如下: ##############begin#################### #   TYPE    DATABA ...

  9. LINQ---查询表达式的结构

    重要事项: 子句必须按照一定的顺序出现 from子句和select...group子句这两部分是必须的 其他子句是可选的 在LINQ查询表达式中,select子句在表达式最后. 可以后任意多的from ...

  10. 1_01_MSSQL课程_基础入门2

    1.数据库的迁移方案 ->分离 附加 ->权限问题: ->启用Administrator账号 ->把数据库文件放到默认的数据库文件存放目录. ->数据库文件兼容级别,设置 ...