Codeforces B. Minimum Possible LCM(贪心数论)
题目描述:
B. Minimum Possible LCM
time limit per test
4 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output
You are given an array aconsisting of integers a1,a2,…,a**n
Your problem is to find such pair of indices i,j that lcm(\(a_i\),\(a_j\))is minimum possible.
lcm(x,y) is the least common multiple of and x and y(minimum positive number such that both x and y are divisors of this number).
Input
The first line of the input contains one integer n — the number of elements in a
The second line of the input contains n integers a1,a2,…,an (1≤ai≤107), where ai is the i-th element of a.is the i.
Output
Print two integers i and (1≤i<j≤n is minimum among all valid pairs i,j
思路:
题目是要求一组数中两个数的最小公倍数的最小值。刚开始一个直白的想法就是枚举,把每两个数的gcd求出来,根据gcd求每两个数的lcm。这种做法的时间复杂度为O(\(n^2\log_2 n\)),在看看题目的数据范围,显然不太科学,限时4秒,\(10^{12}log_210^{6}\),会远远超时。怎么办?
我们来想一想,一般lcm问题与gcd问题是挂钩的。怎么样来求,由于数据的范围给定了,考虑枚举数的因子,从1开始到\(10^7\),在数列中找到一因子为最大公约数的两个最小数,就是答案。为什么?
假设现在枚举到了公因子d,数列中是d的倍数的有\(x_1\)<\(x_2\)<\(x_3\)<...<\(x_n\),如果d是\(x_1\),\(x_2\)的gcd,那么也就满足条件,x1,x2的最小公倍数肯定最小(在d为因子时)。如果d不是x1,x2的gcd,那也不是后面数的gcd,那么最大公倍数就不会最小。
由于d是从小到大枚举的,如果在d时满足条件,肯定为局部最优解。如果都不满足d为gcd,d++,继续枚举直到满足。由于算法一定会终止,算法的正确性就有了保障。算法复杂度是O(\(n\log_2 n\))
需要注意的是当元素有重复的情况,那么这种元素的最小公倍数就是本身,而且只可能是最小重复元素的时候,因为如果比它大的重复元素的lcm一定大于它,不会是全局最小lcm,单独在输入的时候不断覆盖,留下最小的一种即可。
注意LLONG_MAX和LONG_MAX是不一样的,我一开始错了,原来因为是数不够大。
代码:
#include <iostream>
#include <climits>
#define INF LLONG_MAX
#define max_n 10000007
using namespace std;
long long a[max_n];
int n;
int pos[max_n];
long long ans = 0;
long long minm = INF;
int x = 0;
int y = 0;
long long gcd(long long a,long long b)
{
return (b==0)?a:gcd(b,a%b);
}
int main()
{
cin >> n;
for(int i = 1;i<=n;i++)
{
int v;
cin >> v;
a[v]++;
if(a[v]>1&&v<minm)
{
minm = v;
x = pos[v];
y = i;
}
pos[v] = i;
}
for(int i = 1;i<max_n;i++)
{
long long v = 0;
for(int j = i;j<max_n;j+=i)
{
if(a[j]==0)
{
continue;
}
if(v==0)
{
v = j;
}
else
{
long long g = gcd(v/i,j/i);
if(g==1)
{
ans = (long long)j/i*v;
if(ans<minm)
{
//cout << "v " << v << " j " << j << endl;
minm = ans;
x = pos[v];
y = pos[j];
}
}
break;
}
}
}
if(x>y) swap(x,y);
cout << x << " " << y << endl;
return 0;
}
参考文章:
KobeDuu,Minimum Possible LCM【枚举】,https://blog.csdn.net/qq_41157137/article/details/89353527
Codeforces B. Minimum Possible LCM(贪心数论)的更多相关文章
- Codeforces 1154G Minimum Possible LCM
题目链接:http://codeforces.com/problemset/problem/1154/G 题目大意: 给定n个数,在这些数中选2个数,使这两个数的最小公倍数最小,输出这两个数的下标(如 ...
- 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 Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)
You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the ...
- 【贪心】codeforces D. Minimum number of steps
http://codeforces.com/contest/805/problem/D [思路] 要使最后的字符串不出现ab字样,贪心的从后面开始更换ab为bba,并且字符串以"abbbb. ...
- Codeforces 515C 题解(贪心+数论)(思维题)
题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...
- Codeforces 798C - Mike and gcd problem(贪心+数论)
题目链接:http://codeforces.com/problemset/problem/798/C 题意:给你n个数,a1,a2,....an.要使得gcd(a1,a2,....an)>1, ...
随机推荐
- Centos7——Firefox浏览器个性化配置调教
因为谷歌浏览器无法正常登陆帐号,只能切换到火狐浏览器 默认浏览器我使用的是bing搜索 1.隐藏顶部标题栏 顶部标题栏真的占地方,所以直接选择隐藏 点击设置->自定义customize-> ...
- cmake安装与使用
CMake(cross platform make)是一个开源的跨平台工具系列,旨在构建,测试和打包软件. 使用指定名为CMakeLists.txt的配置文件可以控制软件的构建.测试和打包等流程. 通 ...
- nginx deny 封IP
官方文档地址:http://nginx.org/en/docs/http/ngx_http_access_module.html#deny Syntax: deny address | CIDR | ...
- 电视CI卡详解
CAM卡中文名视密卡,它是一种数字视频条件接收模块,是一个连接电视机与外部信号源的设备.它可以将压缩的数字信号转成电视内容,并在电视机上显示出来.CAM卡(亦称大卡)和智能卡(亦称小卡)配合使用,插入 ...
- Docker下打包FastDFS镜像以及上传遇到的问题
官方地址:https://github.com/happyfish100/fastdfs 一.先下载个包,然后解压(自己找个目录下载即可) [root@localhost soft]# wget ht ...
- [转帖]linux下安装7z命令及7z命令的使用
linux下安装7z命令及7z命令的使用 https://www.cnblogs.com/yiwd/p/3649094.html yum install p7zip 执行命令为 7za x 或者是 7 ...
- pytest_用例a失败,跳过测试用例b和c并标记失败xfail
前言 当用例a失败的时候,如果用例b和用例c都是依赖于第一个用例的结果,那可以直接跳过用例b和c的测试,直接给他标记失败xfail用到的场景,登录是第一个用例,登录之后的操作b是第二个用例,登录之后操 ...
- golang基础学习-strings包常用函数学习
package main import ( "fmt" "strings" ) //StrFunc 字符串说明 func main() { var testSt ...
- Java单元测试 Http Server Mock框架选型
背景动机 某期优化需要针对通用的HttpClient封装组件--HttpExecutor在保证上层暴露API不动的前提做较多改动,大致包括以下几点: apache http client 版本升级 H ...
- JavaScript字符串转数值
JavaScript字符串转数值:方法主要有三种 转换函数.强制类型转换.利用js变量弱类型转换. 1. 转换函数 js提供了parseInt()和parseFloat()两个转换函数.前者把值转换成 ...