Codeforces 1154G Minimum Possible LCM
题目链接:http://codeforces.com/problemset/problem/1154/G
题目大意:
给定n个数,在这些数中选2个数,使这两个数的最小公倍数最小,输出这两个数的下标(如果有多组解,任意输出一组即可)。
分析:
直接2个循环两两配对一下肯定要超时的,这里可以考虑枚举公因数,因为LCM是与最大公因数相关的,如果我们枚举了所有的公因数,那么我们就枚举了所有的LCM。
对于每一个公因数d,含有因子d的数从小到大排序为x1,x2,x3……xn,共有n个。
首先计算一下前两个数的GCD,有2种可能:
1:如果GCD(x1,x2) == d,那就没必要再计算GCD(xi,xj) (1 < i < j <= n)了,只要计算一下LCM(x1,x2)即可,因为如果GCD(xi,xj) == d,LCM(xi,xj)肯定大于LCM(x1,x2);如果GCD(xi,xj) == d_ > d,LCM(xi,xj)有可能小于LCM(x1,x2),不过这个等d枚举到了d_再算也是一样的。
2:如果GCD(x1,x2) == d_ > d,没必要再计算LCM(x1,x2),可以等到枚举到d_再计算,也没必要再计算GCD(xi,xj),因为如果GCD(xi,xj) == d,LCM(xi,xj) = xi * xj / d > x1 * x2 / d > x1 * x2 / d_ = LCM(x1,x2);如果GCD(xi,xj) > d,也不需要。
代码如下:
#include <bits/stdc++.h>
using namespace std; #define Rep(i, n) for (int i = 0; i < (n); ++i)
#define rRep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define For(i, s, t) for (int i = (s); i <= (t); ++i)
#define rFor(i, t, s) for (int i = (t); i >= (s); --i)
#define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define rforeach(i, c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i) #define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
#define EL() printf("\n")
#define GL(s) getline(cin, (s))
#define SHOW_VECTOR(v) {std::cerr << #v << "\t:"; for(const auto& xxx : v){std::cerr << xxx << " ";} std::cerr << "\n";}
#define SHOW_MAP(v) {std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}} template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
} template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
} template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
} #define LOWBIT(x) ((x)&(-x)) #define ALL(x) x.begin(),x.end() #define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,inf,sizeof(a))
#define msM(a) memset(a,-1,sizeof(a)) #define MP make_pair
#define PB push_back
#define ft first
#define sd second inline int gc(){
static const int BUF = 1e7;
static char buf[BUF], *bg = buf + BUF, *ed = bg; if(bg == ed) fread(bg = buf, , BUF, stdin);
return *bg++;
} inline int ri(){
int x = , f = , c = gc();
for(; c<||c>; f = c=='-'?-:f, c=gc());
for(; c>&&c<; x = x* + c - , c=gc());
return x*f;
} typedef long long LL;
typedef pair< int, int > PII;
typedef pair< LL, LL > PLL;
typedef map< int, int > MII;
const double EPS = 1e-;
const double PI = acos(-1.0);
const LL mod = 1e9 + ;
const LL inf = 0x7fffffff;
const LL infLL = 0x7fffffffffffffffLL;
const LL ONE = ;
const int maxN = 1e7 + ; struct A{
int pos;
int cnt = ;
}; LL n, ret = infLL;
A a[maxN];
PLL ans; inline LL gcd(LL x,LL y){
LL t;
if(!(x && y))return -;
while(y){
t=x%y;
x=y;
y=t;
}
return x;
} inline LL lcm(LL x,LL y){
LL t = gcd(x,y);
if(t == -)return -;
return x/t*y;
} int main(){
INIT();
cin >> n;
For(i, , n) {
LL x;
cin >> x;
if(a[x].cnt == ) {
a[x].pos = i;
++a[x].cnt;
}
else if(a[x].cnt == ) {
if(x < ret) {
ret = x;
ans = MP(a[x].pos, i);
}
++a[x].cnt;
}
} // Enumerate common factor d
For(d, , maxN) {
LL x1 = ;
for(int x2 = d; x2 < maxN; x2 += d){
if(!a[x2].cnt) continue;
if(!x1) x1 = x2;
else{
// x1,x2为前2个有公因子d的数
if(gcd(x1, x2) == d){
LL tmp = x2 / d * x1;
if(tmp < ret){
ret = tmp;
ans = MP(a[x2].pos, a[x1].pos);
}
}
break;
}
}
} if(ans.ft > ans.sd) swap(ans.ft, ans.sd);
cout << ans.ft << " " << ans.sd;
return ;
}
Codeforces 1154G Minimum Possible LCM的更多相关文章
- Codeforces B. Minimum Possible LCM(贪心数论)
题目描述: B. Minimum Possible LCM time limit per test 4 seconds memory limit per test 1024 megabytes inp ...
- Minimum Sum LCM(uva10791+和最小的LCM+推理)
L - Minimum Sum LCM Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- UVA.10791 Minimum Sum LCM (唯一分解定理)
UVA.10791 Minimum Sum LCM (唯一分解定理) 题意分析 也是利用唯一分解定理,但是要注意,分解的时候要循环(sqrt(num+1))次,并要对最后的num结果进行判断. 代码总 ...
- Codeforces Round #552:G. Minimum Possible LCM
官方题解是时间复杂度为O(nd)的.这里给出一个简单实现但是时间复杂度为O(NlogN) (N=1e7) 因为 a*b/gcd(a,b)=lcm(a,b) 所以我们可以枚举每一个因子,然后找到存在这个 ...
- F - Minimum Sum LCM
LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multip ...
- UVA 10791 Minimum Sum LCM(分解质因数)
最大公倍数的最小和 题意: 给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 那么找出一个序列,使他们的和最小. 分析: 一系列数字a1,a2,a3 ...
- codeforces 496A. Minimum Difficulty 解题报告
题目链接:http://codeforces.com/contest/496/problem/A 题目意思:给出有 n 个数的序列,然后通过删除除了第一个数和最后一个数的任意一个位置的数,求出删除这个 ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- Minimum Sum LCM UVA - 10791(分解质因子)
对于一个数n 设它有两个不是互质的因子a和b 即lcm(a,b) = n 且gcd为a和b的最大公约数 则n = a/gcd * b: 因为a/gcd 与 b 的最大公约数也是n 且 a/gcd ...
随机推荐
- centos7下安装docker(18.1docker日志---logging driver)
将容器的日志发送到STDOUT和STDERR是docker的默认日志行为.实际上,docker提供了多种日志机制帮助用户从容器中提取日志,这些机制被称为logging driver docker的默认 ...
- SDOI2016 R1做题笔记
SDOI2016 R1做题笔记 经过很久很久的时间,shzr终于做完了SDOI2016一轮的题目. 其实没想到竟然是2016年的题目先做完,因为14年的六个题很早就做了四个了,但是后两个有点开不动.. ...
- 【angularjs】使用angular搭建项目,pc端实现网页中的内容不可复制
实现目标:不可复制页面内容 js: <script language="javascript"> if (typeof(document.onselectstart) ...
- 转载 1-EasyNetQ介绍(黄亮翻译) https://www.cnblogs.com/HuangLiang/p/7105659.html
EasyNetQ 是一个容易使用,坚固的,针对RabbitMQ的 .NET API. 假如你尽可能快的想去安装和运行RabbitMQ,请去看入门指南.EasyNetQ是为了提供一个尽可能简洁的适用与R ...
- nn.ReLU(inplace=True)中inplace的作用
在文档中解释是: 参数: inplace-选择是否进行覆盖运算 意思是是否将得到的值计算得到的值覆盖之前的值,比如: x = x + 即对原值进行操作,然后将得到的值又直接复制到该值中 而不是覆盖运算 ...
- bzoj-1787-洛谷-4281(LCA板子题)
传送门(bzoj) 传送门(洛谷) 可以说这道也是一个板子题 由于题中是三个人需经过的路径最短 就会有一点点不太一样 那么 就两两求LCA 这样之后就会出现两种状况 一.所得到的三个LCA是相等的 那 ...
- 记一次Dubbo服务注册异常
公司项目重构,把dubbo版本从2.5.8升级为2.6.2.升级后在本地运行一点问题都没有:可是通过公司自研的发布系统将项目发布到测试环境的linux服务器下面后,出现了dubbo服务 ...
- 记上海技术交流会之行备忘录(superset与odoo整合)
像每个早上一样,早起跑步回来冲个热水澡,简单的吃下早饭,看书到8:50的样子,准备赶10:02分的火车.在我看到周总的微信时,我知道这将是一个新的起点,在自己过往的2年时间,将更多的精力和时间用在了英 ...
- H5 24-CSS三大特性之继承性
24-CSS三大特性之继承性 我是段落 我是段落 我是超链接 我是大标题 <!DOCTYPE html> <html lang="en"> <head ...
- 牛客练习赛B题 筱玛的排列(找递推规律)
链接:https://ac.nowcoder.com/acm/contest/342/B来源:牛客网 筱玛的排列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他语 ...