题意:给你一堆数,问其中lcm最小的一对数是什么?

思路:因为lcm(a, b) = a * b / gcd(a, b), 所以我们可以考虑暴力枚举gcd, 然后只找最小的a和b,去更新答案即可。

数据范围1e7? 不慌,bitset搞一下, 1e7log(1e7)可以500ms过。

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000010;
bitset<maxn * 10> v, v1;
int a[maxn];
vector<int> ans;
long long res = 1e18;
int main() {
int n, res1, res2, pos1 = -1, pos2 = -1, mx = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
mx = max(mx, a[i]);
if(v[a[i]] == 1) v1[a[i]] = 1;
v[a[i]] = 1;
}
for (int i = 1; i <= mx; i++) {
ans.clear();
for (int j = i; j <= mx; j += i) {
if(v[j]) {
ans.push_back(j);
if(v1[j]) ans.push_back(j);
if(ans.size() >= 2) break;
}
}
if(ans.size() < 2) continue;
if(res > (long long)ans[0] * ans[1] / i) {
res = (long long)ans[0] * ans[1] / i;
res1 = ans[0], res2 = ans[1];
}
}
int flag = 0;
for (int i = 1; i <= n; i++) {
if(pos1 == -1 && res1 == a[i]) {
pos1 = i;
flag |= 1;
continue;
}
if(pos2 == -1 && res2 == a[i]) {
pos2 = i;
flag |= 2;
}
if(flag == 3) break;
}
if(pos1 > pos2) swap(pos1, pos2);
printf("%d %d\n", pos1, pos2);
}

  

Codeforces 1154G 枚举的更多相关文章

  1. Codeforces 1154G(枚举)

    我预处理\(1e7log(1e7)\)的因数被T掉了,就不敢往这个复杂度想了--无奈去看AC代码 结果怎么暴举gcd剪一剪小枝就接近3s卡过去了!vector有锅(确信 const int maxn ...

  2. Codeforces 1154G Minimum Possible LCM

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

  3. codeforces 873E(枚举+rmq)

    题意 有n(n<=3000)个人参与acm比赛,每个人都有一个解题数,现在要决定拿金牌的人数cnt1,拿银牌的人数cnt2,拿铜牌的人数cnt3,各自对应一个解题数区间[d1,c1],[d2,c ...

  4. C. Vasily the Bear and Sequence Codeforces 336C(枚举,思维)

    C. Vasily the Bear and Sequence time limit per test 1 second memory limit per test 256 megabytes inp ...

  5. Codeforces 1216E2 枚举位数+二分

    两个二分 枚举位数 #include <bits/stdc++.h> #define MOD 1000000007 using namespace std; typedef long lo ...

  6. CodeForces - 748E (枚举+脑洞)

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  7. Codeforces 965 枚举轮数贪心分糖果 青蛙跳石头最大流=最小割思想 trie启发式合并

    A /*#include<cstring>#include<algorithm>#include<queue>#include<vector>#incl ...

  8. 数论(lcm)

    CodeForces - 1154G You are given an array a consisting of n integers a1,a2,…,an . Your problem is to ...

  9. Codeforces Round #103 (Div. 2) D. Missile Silos(spfa + 枚举边)

    题目链接:http://codeforces.com/problemset/problem/144/D 思路:首先spfa求出中心点S到其余每个顶点的距离,统计各顶点到中心点的距离为L的点,然后就是要 ...

随机推荐

  1. jdk1.8新特性之函数式接口

    函数式接口就是只有一个抽象方法的接口.如果这个接口里没有或者包含了两个以上的抽象方法,对不起,你不叫函数式接口,只能叫你接口.那这个函数式有啥用呢?如果配合Lambda表达式的话,可以大大的简化代码. ...

  2. HotSpot Stop-and-Copy GC

    rednaxelafx的Cheney算法的伪代码.如果不用forwarding的话,维护一个旧地址到新地址的映射也可以. 其中重点部分: void Heap::collect() { // The f ...

  3. 561. 数组拆分 I

    题目 python class Solution: def arrayPairSum(self, nums): """ :type nums: List[int] :rt ...

  4. Jquery each循环中中断

    在each代码块内不能使用break和continue,要实现break和continue的功能的话,要使用其它的方式 break----用return false; continue --用retu ...

  5. linux中日志文件查找,根据关键字,vi命令,awk和wc

    参考: http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html 当时需求:查看系统日志,统计系统的处理时间(从请求进去系统到系 ...

  6. 【转】利用JMeter进行压力测试

    压力测试以软件响应速度为测试目标,尤其是在较短时间内大量并发用户的同时访问时,软件的性能和抗压能力. JMeter是一款开源的压力测试工具,目前最新Release版本是2.3.4,它不仅可以测试Web ...

  7. python算两个时间之间的天数,将天数转成int型

    import time import datetime #计算两个日期相差天数,自定义函数名,和两个日期的变量名. def Caltime(date1,date2): #%Y-%m-%d为日期格式,其 ...

  8. li布局问题

    问题示意,好多网站一般都有这种布局,如 问题主要原因,第一个li没有margin-left 其余有(这里只考虑一排的情况) 第一种解决方式: <!DOCTYPE html> <htm ...

  9. SpringBoot中RedisTemplate订阅发布对象

    解说 RedisMessageListenerContainer Redis订阅发布的监听容器,你的消息发布.订阅配置都必须在这里面实现 addMessageListener(MessageListe ...

  10. Docker Rest API使用入门

    Docker Rest API使用入门 系统:Centos7.2, Docker版本信息如下: [python] view plain copy Client: Version:      17.03 ...