题意:给你一堆数,问其中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. Android中关于JNI 的学习(五)在C文件里使用LogCat

    Log是开发过程中.对于我们调试程序非常重要的一个工具,有非常多时候,我们正是通过Log才干够看清楚程序是不是真的依照我们想像中的模式在跑,从而定位到问题所在的地方.而在Android开发中,毫无疑问 ...

  2. tar 打包处理文件

    基本格式:tar [Options] file_archive //注意tar的第一参数必须为命令选项,即不能直接接待处理文件 Option是由三部分组成,分别是操作类型(创建,查看,解压),压缩处理 ...

  3. dubbo相关的知识点总结

    dubbo最近提交到了apache,成为了apache的孵化项目,又开始活跃起来了.就官方在git上面的说明文档和其他资料,学习总结以下dubbo的一些知识点. .The dubbo protocol ...

  4. 020:Buffer Pool 、压缩页、CheckPoint、Double Write、Change Buffer

    一. 缓冲池(Buffer Pool) 1.1 缓冲池介绍 每次读写数据都是通过 Buffer Pool : 当Buffer Pool 中没有用户所需要的数据时,才去硬盘中获取: 通过 innodb_ ...

  5. supervisor 管理

    Supervisor是用Python开发的一套通用的进程管理程序,能将一个普通的命令行进程变为后台daemon,并监控进程状态,异常退出时能自动重启.它是通过fork/exec的方式把这些被管理的进程 ...

  6. 转 - 使用from __future__ import unicode_literals时要注意的问题

    原文链接: http://www.cnblogs.com/ajianbeyourself/p/4471035.html 使用from __future__ import unicode_literal ...

  7. Springmvc ajax请求400

    转载做记录 传JSON对象 前端 function test () { var param = {username : "yitop"}; $.ajax({ timeout : 2 ...

  8. 梯度下降(Gradient descent)

    首先,我们继续上一篇文章中的例子,在这里我们增加一个特征,也即卧室数量,如下表格所示: 因为在上一篇中引入了一些符号,所以这里再次补充说明一下: x‘s:在这里是一个二维的向量,例如:x1(i)第i间 ...

  9. 华为OJ机试题目:两个大整数相乘(纯C语言实现两个大整数相乘,两种方法实现大数相乘)

    题目描述: 输出两个不超过100位的大整数的乘积. 输入: 输入两个大整数,如1234567 123 输出: 输出乘积,如:151851741 样例输入: 1234567 123 样例输出: 1518 ...

  10. 24_java之转换流和缓冲流

    01转换流概述 * A: 转换流概述 * a: 转换流概述 * OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节 * 将字符串按照指 ...