Codeforces 1154G 枚举
题意:给你一堆数,问其中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 枚举的更多相关文章
- Codeforces 1154G(枚举)
我预处理\(1e7log(1e7)\)的因数被T掉了,就不敢往这个复杂度想了--无奈去看AC代码 结果怎么暴举gcd剪一剪小枝就接近3s卡过去了!vector有锅(确信 const int maxn ...
- Codeforces 1154G Minimum Possible LCM
题目链接:http://codeforces.com/problemset/problem/1154/G 题目大意: 给定n个数,在这些数中选2个数,使这两个数的最小公倍数最小,输出这两个数的下标(如 ...
- codeforces 873E(枚举+rmq)
题意 有n(n<=3000)个人参与acm比赛,每个人都有一个解题数,现在要决定拿金牌的人数cnt1,拿银牌的人数cnt2,拿铜牌的人数cnt3,各自对应一个解题数区间[d1,c1],[d2,c ...
- 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 ...
- Codeforces 1216E2 枚举位数+二分
两个二分 枚举位数 #include <bits/stdc++.h> #define MOD 1000000007 using namespace std; typedef long lo ...
- CodeForces - 748E (枚举+脑洞)
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- Codeforces 965 枚举轮数贪心分糖果 青蛙跳石头最大流=最小割思想 trie启发式合并
A /*#include<cstring>#include<algorithm>#include<queue>#include<vector>#incl ...
- 数论(lcm)
CodeForces - 1154G You are given an array a consisting of n integers a1,a2,…,an . Your problem is to ...
- Codeforces Round #103 (Div. 2) D. Missile Silos(spfa + 枚举边)
题目链接:http://codeforces.com/problemset/problem/144/D 思路:首先spfa求出中心点S到其余每个顶点的距离,统计各顶点到中心点的距离为L的点,然后就是要 ...
随机推荐
- Toxiproxy 网络情况模式代理
1. 介绍 Toxiproxy is a framework for simulating network conditions. It's made specifically to work in ...
- RK3288 查看时钟树
主控端可以通过指令查看时钟树,enable_cnt为1,表示时钟已使能. # cat d/clk/clk_summary cat d/clk/clk_summary clock enable_cnt ...
- Firewalld防火墙与ICMP攻击
原文地址:http://www.excelib.com/article/293/show 提到ICMP大家应该都很熟悉,可能有人会说:不就是ping吗?但是说到ICMP攻击以及相关防御措施可能就有的人 ...
- Java中的三元运算:a = (a > b)?a:b
格式:逻辑值 ? 表达式1 : 表达式2 执行顺序:先执行逻辑值,如果逻辑值为true,则执行表达式1:反之则执行表达式2 a = (a > b)?a:b 如果a>b成立,返回a: 如果a ...
- TimesTen学习(三)安装、连接、远程连接TimesTen数据库
TimesTen学习(三)远程连接TimesTen数据库 <TimesTen学习(一)安装篇>:http://blog.itpub.net/23135684/viewspace-71774 ...
- Fiddler移动端抓包
有关手机抓包之前没有接触过,实际工作用到的不多,因此学了也就忘了. 来了新公司之后,产品主要以移动端为主,抓取线上线下包便成了平常之事,一些开发也会来请教如何在手机上抓包. 回归正题,抓取移动端包有很 ...
- 使用 maven 构建 SpringMVC
引言 最近需要使用SpringMVC做一个小项目,之前对SpringMVC没什么了解,所以先写一个SpringMVC的小Demo练习一下. 使用Maven构建项目 IDE = Eclipse 首先创建 ...
- Ajax显示隐藏
$(function(){ $('#search').click(function(){ if($(".search_div").is(":visible")) ...
- eclipse使用SSH框架出现There is no Action mapped for namespace [/] and action name [] associated with context path错误
eclipse使用SSH框架出现There is no Action mapped for namespace [/] and action name [] associated with conte ...
- JS-用法
JavaScript 用法 HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 & ...