题意:给你两个等长的数列,让你在两个数列中各选择一个数字,使得这两个数的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初学者-网络桥接篇

    linux初学者-网络桥接篇 在网络的使用中,有时需要搭建网络桥来实现网络桥接.例如在一台主机上制作一台虚拟机,虚拟机是没有物理网卡的,这时虚拟机数据的发送和接收就需要通过主机上的物理网卡,需要主机的 ...

  2. C语言数据类型及变量整理

    数据类型 获取int的字节数大小方法 printf("int bytes:%d",sizeof(int)); 列表整理 类型 字节数 取值范围 char 1 [-128,127]= ...

  3. 【Arduino】37种传感器系列实验(4)---振动传感器模块

    ---恢复内容开始--- 37款传感器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器,依照实践(动手试试)出真知的理念,以学习和交流为 ...

  4. HZOJ 单

    两个子任务真的是坑……考试的时候想到了60分的算法,然而只拿到了20分(各种沙雕错,没救了……). 算法1: 对于测试点1,直接n遍dfs即可求出答案,复杂度O(n^2),然而还是有好多同学跑LCA/ ...

  5. 2019牛客暑期多校训练营(第四场)J-free

    >传送门< 题意:给你n个城市,m条道路,经过每一条要花费这条路的代价,现给你k个机会,使得最多k条路的代价为0,问从起点s到终点t花费的最少代价 思路:分层图最短路经典裸题 方法一 Co ...

  6. Spring MVC中的 权限拦截定义 以及 权限拦截的研发步骤

    权限拦截 (拦截器: 对请求进行区分) 1 实现的价值(作用) 用户未登录:访问没用登录的URL,拦截到以后 跳转回登录 用户未登录:访问登录的URL,直接放行到后续流程处理框架,进行后续的操作 用户 ...

  7. GStreamer基础教程06 - 获取媒体信息

    摘要 在常见的媒体文件中,通常包含一些数据(例如:歌手,专辑,编码类型等),用于描述媒体文件.通常称这些数据为元数据(Metadata:data that provides information a ...

  8. 【Intellij】导出 jar 包

    需要在 Intellij 导出 jar 包,一时不知道该怎么做,后来总算找到了方法,步骤如下: 1. File → Project Structure... → Artifacts → + → jar ...

  9. java中map,set的简单使用

    package test2; import java.util.*; import static java.lang.System.out; public class test2 extends St ...

  10. 统计学习方法6—logistic回归和最大熵模型

    目录 logistic回归和最大熵模型 1. logistic回归模型 1.1 logistic分布 1.2 二项logistic回归模型 1.3 模型参数估计 2. 最大熵模型 2.1 最大熵原理 ...