HackerRank - maximum-gcd-and-sum
题意:给你两个等长的数列,让你在两个数列中各选择一个数字,使得这两个数的gcd是这n * n种组合中最大的。
思路:如果上来就考虑分解因式什么的,就想偏了,假设数列1的最大数为max1,数列2的最大数为max2,我们知道,这个max_gcd一定是在
1~max(max1,max2)中间的。我们一 一 枚举(从大到小)来验证。
我们在验证 i 的时候,是否要用数列中的每一个数来 % 一下 i ,看看是否能整除呢?
答案是不用的,我们可以仅仅验证 i 的倍数的数字在这个数列中是否存在。这样扫一遍的复杂度为 n / i 。
验证时总的复杂度为nlogn。
详见代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
const int maxn2 = ; bool mark1[maxn],mark2[maxn];
int store1[maxn2],store2[maxn2]; int main(){
int n;
scanf("%d",&n);
for(int i = ;i < n;++i){
scanf("%d",&store1[i]);
mark1[store1[i]] = true;
}
for(int i = ;i < n;++i){
scanf("%d",&store2[i]);
mark2[store2[i]] = true;
}
sort(store1,store1 + n);
sort(store2,store2 + n);
int maxx = max(store1[n - ],store2[n - ]);
int ans = ;
for(int i = maxx;i >= ;+--i){
int a = ,b = ;
int temp = i;
while(temp <= maxx){
if(mark1[temp]) ++a;
if(mark2[temp]) ++b;
if(a > && b > ){
ans = i;
break;
}
temp += i;
}
if(a > && b > ) break;
}
int out = ;
for(int i = n - ;i >= ;--i){
if(store1[i] % ans == ){
out += store1[i];
break;
}
}
for(int i = n - ;i >= ;--i){
if(store2[i] % ans == ){
out += store2[i];
break;
}
}
printf("%d\n",out);
return ;
}
HackerRank - maximum-gcd-and-sum的更多相关文章
- Maximum GCD (stringstream)题解
Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possiblepa ...
- UVA 11827 Maximum GCD
F - Maximum GCD Time Limit:1000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Given the ...
- Subarray Sum & Maximum Size Subarray Sum Equals K
Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...
- Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K
Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...
- Maximum GCD(fgets读入)
Maximum GCD https://vjudge.net/contest/288520#problem/V Given the N integers, you have to find the m ...
- 邝斌带你飞之数论专题--Maximum GCD UVA - 11827
Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible p ...
- UVA - 11827 - Maximum GCD,10200 - Prime Time (数学)
两个暴力题.. 题目传送:11827 Maximum GCD AC代码: #include <map> #include <set> #include <cmath> ...
- UVA11827 Maximum GCD
/* UVA11827 Maximum GCD https://vjudge.net/contest/153365#problem/V 数论 gcd 水题,然而读入比较坑 * */ #include ...
- leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- Maximum GCD(UVA 11827)
Problem:Given the N integers, you have to find the maximum GCD (greatest common divisor) of every po ...
随机推荐
- 解决Windows10下安装Ubuntu16.04双系统后开机没有Ubuntu引导
转载 https://blog.csdn.net/qq_27838307/article/details/79149791 1.按照网上教程在磁盘中压缩硬盘并且不需要给他新建卷标,就让他显示空闲就好了 ...
- jmeter性能小试全流程
大纲: 1.添加线程组:虚拟用户 2.添加测试对象:比如http请求 3.查看结果 一.添加线程组. 1.线程是what: JMeter是由Java实现的,并且使用一个Java线程来模拟一个用户,因此 ...
- 如何在Eclipse中查看Java类库的源代码
你的JDK安装目录下%Java_home%/src.zip文件就是源码,解压缩找到对应包下面的类即可. 如果是Eclipse开发,ctr+鼠标左击,出现不了源码的话,在弹出的视图中点击attach s ...
- Java EE.Servlet.处理请求
Servlet的核心工作便是处理客户端提交的请求信息,生成动态响应信息返回客户端. 1.请求参数 POST方法一般用于更新服务器上的资源,当时用POST方法时,提交的数据包含在HTTP实体内,而GET ...
- 重复造轮子系列——基于Ocelot实现类似支付宝接口模式的网关
重复造轮子系列——基于Ocelot实现类似支付宝接口模式的网关 引言 重复造轮子系列是自己平时的一些总结.有的轮子依赖社区提供的轮子为基础,这里把使用过程的一些觉得有意思的做个分享.有些思路或者方法在 ...
- 关于Hack术语方面
1.肉鸡 所谓“肉鸡”是一种很形象的比喻,比喻那些可以随意被我们控制的电脑,对方可以是WINDOWS系统,也可以是UNIX/LINUX系统,可以是普通的个人电脑,也可以是大型的服务器,我们 ...
- mysql协议分析2---认证包
主人看到navicat和mysql在那嘻嘻哈哈,眉来眼去的,好不快乐,忽然也想自己写个程序,直接去访问Mysql,虽然现在已经有很多现成的中间件可以直接拿来用了,程序只要负责写sql语句就行了,但是主 ...
- 这 3 个 Set 集合的实现有点简单,那来做个总结吧
Set 接口是 Java Collections Framework 中的一员,它的特点是:不能包含重复的元素,允许且最多只有一个 null 元素.Java 中有三个常用的 Set 实现类: Hash ...
- docker部署xxl-job 通用反射执行器
原因 最近在公司写一些job,公司使用的是spring boot提供的注解形式实现的. 这样在自测的时候很麻烦,而且测试提测的时候需要修改cron表达式->提交git->jenkins打包 ...
- 【iOS】UIImage 等比率缩放
这两天处理引导页面的时候遇到了图片略大的问题,上网查找后找到了解决方法.用的是 UIImage 的等比率缩放,虽然不难,但之前没接触过,故记之. 代码如下: - (UIImage *)scaleIma ...