题意:给你两个等长的数列,让你在两个数列中各选择一个数字,使得这两个数的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的更多相关文章

  1. Maximum GCD (stringstream)题解

    Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possiblepa ...

  2. UVA 11827 Maximum GCD

    F - Maximum GCD Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Given the ...

  3. 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 ...

  4. 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 ...

  5. Maximum GCD(fgets读入)

    Maximum GCD https://vjudge.net/contest/288520#problem/V Given the N integers, you have to find the m ...

  6. 邝斌带你飞之数论专题--Maximum GCD UVA - 11827

    Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible p ...

  7. UVA - 11827 - Maximum GCD,10200 - Prime Time (数学)

    两个暴力题.. 题目传送:11827 Maximum GCD AC代码: #include <map> #include <set> #include <cmath> ...

  8. UVA11827 Maximum GCD

    /* UVA11827 Maximum GCD https://vjudge.net/contest/153365#problem/V 数论 gcd 水题,然而读入比较坑 * */ #include ...

  9. 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题是存储的当前累加和的 ...

  10. Maximum GCD(UVA 11827)

    Problem:Given the N integers, you have to find the maximum GCD (greatest common divisor) of every po ...

随机推荐

  1. Vue的基本使用(三)

    1.过滤器 1.局部过滤器,在当前组件内部使用过滤器,给某些数据添油加醋. //声明 filters:{ "过滤器的名字":function(val,a,b){ //a就是alex ...

  2. mysql查看视图用户

    select `DEFINER` from  information_schema.VIEWS;

  3. spring与actionMQ整合

    出处:http://www.cnblogs.com/leiOOlei/p/5075402.html 一.配置部分 ActiveMQ的安装这就不说了,很简单, 这个例子采用maven构建,首先看一下po ...

  4. 通过ping命令了解三层转发流程

    ping命令:因特网包探索器.本文主要通过路由器两端不同网段PC互ping来讲解三层转发流程. 例子:PC-A是如何 ping 通 PC-C 的,有几种情况? 说明:1.在条件1阶段PC-C不会刷新a ...

  5. Chrome 跨域 disable-web-security 关闭安全策略

    谷歌浏览器暂时关闭跨域. 当遇到以下情况,则可以简单的使用 关闭Chrome 安全策略跨域 开发时跨域,上线后,部署在一个域名下没有跨域问题 开发时,临时解决跨域问题 只有开发时用这个,其他时候,就不 ...

  6. Fork 多进程 模拟并行访问web service获取响应时间差

    #include <ros/ros.h> #include <iostream> #include <string> #include <cstring> ...

  7. 洛谷P2763题解

    吐槽一下:蜜汁UKE是什么玩意?! 题目分析: 观察题面,对于给定的组卷要求,计算满足要求的组卷方案,可以发现这是一道明显的有条件的二分图匹配问题,于是考虑建模. 建一个超级源点,一个超级汇点:源点与 ...

  8. 用vue2.0+vuex+vue-router+element-ui+mockjs实现后台管理系统的实践探索

    A magical vue element touzi admin. 效果演示地址 更多demo展示 分支说明 master分支:前后端统一开发的版本:可以用于学习nodejs+mongodb+exp ...

  9. UEM“探针”技术及用户体验管理

    随着互联网产品越来越多,用户群体越来越庞大以及用户品位的多样性增加,我们会发现这样的一个规律,就是相同类型的产品,比如播放器中的QQ影音和暴风影音,再比如小游戏平台中的腾讯游戏和联众等等,他们的功能是 ...

  10. [Spring cloud 一步步实现广告系统] 14. 全量索引代码实现

    上一节我们实现了索引基本操作的类以及索引缓存工具类,本小节我们开始实现加载全量索引数据,在加载全量索引数据之前,我们需要先将数据库中的表数据导出到一份文件中.Let's code. 1.首先定义一个常 ...