题意:给你两个等长的数列,让你在两个数列中各选择一个数字,使得这两个数的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. Linux操作系统和Windows操作系统的区别

    1.免费与收费 在中国,windows和linux都是免费的,至少对个人用户是如此,如果那天国内windows真的严打盗版了,那linux的春天就到了!但现在linux依然是任重道远,前路漫漫. 2. ...

  2. python课堂整理18---文件操作(下)

    一.b模式,字节方式(二进制的单位),rb wb ab f = open('test.py', 'rb', encoding = 'utf-8') 报错,因为用了b模式,就不能再指定编码格式了,已经指 ...

  3. MVC WebApi 实现Token验证

    基于令牌的认证 我们知道WEB网站的身份验证一般通过session或者cookie完成的,登录成功后客户端发送的任何请求都带上cookie,服务端根据客户端发送来的cookie来识别用户. WEB A ...

  4. [NLP] 相对位置编码(一) Relative Position Representatitons (RPR) - Transformer

    对于Transformer模型的positional encoding,最初在Attention is all you need的文章中提出的是进行绝对位置编码,之后Shaw在2018年的文章中提出了 ...

  5. 嵌套&匿名&高阶函数

    嵌套&匿名&高阶函数 嵌套函数 函数可以嵌套定义并调用函数 name = "小明" def change(): name = "小明,你好" d ...

  6. [填坑] ubuntu检测不到外接显示器

    笔记本是win10+ubuntu18双系统,今天ubuntu(开启nivida独显状态)突然无法连外接屏幕,但切换win10就可以显示. 贴吧找到的简单解决方法,不需要重装驱动,记录分享在这里: su ...

  7. java 第二章

    变量:变量就是代表程序运行时存放数据的地方 数据存放在:磁盘,内存卡,U盘,光盘,内存条,固态硬盘,机械硬盘等 字节:8个二进制位构成1个"字节(Byte)",它是存储空间的基本计 ...

  8. H3C软件开发笔试面试总结

    注:我目前是陕西师范大学计算机科学学院本科生,在西安参加笔试以及面试 先是笔试,我选择的是JAVA方向,笔试选择题目主要是一些基础性的题目,然后简答题问了final.finally.finallize ...

  9. kali,ubuntu, debain DNS 配置

    kali 是基于 debain 的一个 Linux 发行版 DNS 的配置 是在文件 /etc/resolv.conf  下. 但是,我们会发现 /etc/resolv.conf 每次重启都会失效, ...

  10. Java基础的一些知识点(一):接口interface

    1.接口的含义 接口可以理解成统一的协议, 而接口中的属性也属于协议中的内容.但是接口的属性都是公共的,静态的,最终的. 接口的成员特点: 1.成员变量只能是常量,默认修饰符 public stati ...