题意:给你两个等长的数列,让你在两个数列中各选择一个数字,使得这两个数的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. 【git】Git的使用

    一.安装git 1.windows下安装一个Git 2.lInux下yum(apt-get) install git 二.使用git连接github 使用git连接github时,需要将linux下产 ...

  2. ACM线性基学习笔记

    https://www.cnblogs.com/31415926535x/p/11260897.html 概述 最近的几场多校出现了好几次线性基的题目,,会想起之前在尝试西安区域赛的一道区间异或和最大 ...

  3. 一个完整的产品设计流程——家庭安全管家

    不管是产品设计,还是前后端开发,始终都应该做出来才能够有很好的提高锻炼.书看得再多,如果不配合实际练习始终得不到实质性的进展. 接下来的案例是和几位学弟学妹一起做的,契机是参加一个用户体验设计比赛,从 ...

  4. PHP与ECMAScript_5_常用数组相关函数

    PHP ECMAScript 长度 $length = count($array) length = array.length       增 array_unshift($array, new1,n ...

  5. 关于STM32F103+ESP8266+阿里云过程之设备状态更新至阿里云(三)

    设备与阿里云完成发布订阅的功能,接下来就是将设备状态如温湿度,PM2.5值上报更新至阿里云. 1.查看Topic. 在阿里云平台上 设备->Topic列表中查看.在产品中也可以看到对应的Topi ...

  6. spring @Required注解

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-required-ann ...

  7. 【Android】error: Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light.NoActionBar'.

    问题: res 文件夹下的 values 下的 styles.xml <style name="Sherlock.Light.NoActionBar" parent=&quo ...

  8. Eclipse "Adb failed to restart !"

    今天遇到这个问题,如图所示: 上网找了下,原来是电脑上的各种手机助手抢占了手机链接.http://blog.csdn.net/zhufuing/article/details/19398125 说得很 ...

  9. 用机智云做PWM占空比控制电机,物联网智能家居应用

      因为是新申请的博客,所以申请了总想往里面加点东西,所以把我之前在机智云写的帖子复制了过来 (各位抱歉,由于之前上传的文件可能有错误,之前上传的文件PWM不能用,那么我又重新上传了一个文件,这个文件 ...

  10. Kotlin学习快速入门(5)——空安全

    介绍 kotlin中,对象可分为两种类型,可为空的对象和不可为空对象 默认为不可为空对象,代码检测如果发现不可为空对象赋予了null,则会标红报错. 可为空的对象,如果调用了方法,代码检测也会标红报错 ...