Description

Do you know what is called ``Coprime Sequence''? That is a sequence consists of $n$ positive integers, and the GCD (Greatest Common Divisor) of them is equal to 1. 
``Coprime Sequence'' is easy to find because of its restriction. But we can try to maximize the GCD of these integers by removing exactly one integer. Now given a sequence, please maximize the GCD of its elements.
 

Input

The first line of the input contains an integer $T(1\leq T\leq10)$, denoting the number of test cases. 
In each test case, there is an integer $n(3\leq n\leq 100000)$ in the first line, denoting the number of integers in the sequence. 
Then the following line consists of $n$ integers $a_1,a_2,...,a_n(1\leq a_i\leq 10^9)$, denoting the elements in the sequence.
 

Output

For each test case, print a single line containing a single integer, denoting the maximum GCD.
 

Sample Input

3
3
1 1 1
5
2 2 2 3 2
4
1 2 4 8
 

Sample Output

1
2
2
 
 
 
 
 
题目意思:给出n个数,去掉其中的一个数,得到一组数,使其最大公约数最大。第一组样例,去掉一个1,得到最大的最大公约数是1。第二组样例,去掉3,得到最大的最大公约数为2。第三组样例,去掉1,得到的最大的最大公约数是2。
 
解题思路:这里使用了之前我没有用到过的一种方法。依次遍历每个数,算出这个数左边的一堆数的最大公约数x1,再算出这个数右边的一堆数的最大公约数x2,gcd(x1,x2)即为删去当前数后剩下的数的最大公约数。遍历每个数得到的最大gcd即为所求。而每个数左边的gcd和右边的gcd可以用O(n)的时间复杂度预处理,遍历每个数是跟前面并列的O(n)复杂度,故可线性解决。
 
 #include<stdio.h>
#include<algorithm>
using namespace std;
int gcd(int a,int b) ///基础 辗转
{
int r;
while(b>)
{
r=a%b;
a=b;
b=r;
}
return a;
}
int a[],b[],c[];
int main()
{
int t,n,i,ans;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
b[]=a[];
c[n-]=a[n-];
for(i=;i<n;i++)///求前缀GCD
{
b[i]=gcd(b[i-],a[i]);
}
for(i=n-;i>=;i--)///求后缀GCD
{
c[i]=gcd(c[i+],a[i]);
}
ans=max(c[],b[n-]);
for(i=;i<n-;i++)
{
ans=max(ans,gcd(b[i-],c[i+]));
}
printf("%d\n",ans); }
return ;
}
 
 
 
 
 
 
 
 
 
 
 
 

Coprime Sequence(前后缀GCD)的更多相关文章

  1. HDU - 6025 Coprime Sequence(前缀gcd+后缀gcd)

    题意:去除数列中的一个数字,使去除后数列中所有数字的gcd尽可能大. 分析:这个题所谓的Coprime Sequence,就是个例子而已嘛,题目中没有任何语句说明给定的数列所有数字gcd一定为1→_→ ...

  2. 2017中国大学生程序设计竞赛 - 女生专场C【前后缀GCD】

    C HDU - 6025 [题意]:去除数列中的一个数字,使去除后的数列中所有数字的gcd尽可能大. [分析]: 数组prefixgcd[],对于prefixgcd[i]=g,g为a[0]-a[i]的 ...

  3. HDU - 6025 Coprime Sequence(gcd+前缀后缀)

    Do you know what is called ``Coprime Sequence''? That is a sequence consists of nnpositive integers, ...

  4. HDU6025 Coprime Sequence —— 前缀和 & 后缀和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6025 Coprime Sequence Time Limit: 2000/1000 MS (Java/ ...

  5. HDU6025 Coprime Sequence(gcd)

    HDU6025 Coprime Sequence 处理出数列的 \(gcd\) 前缀和后缀,删除一个数后的 \(gcd\) 为其前缀和后缀的 \(gcd\) . 遍历数列取 \(max\) 即为答案. ...

  6. Coprime Sequence (HDU 6025)前缀和与后缀和的应用

    题意:给出一串数列,这串数列的gcd为1,要求取出一个数使取出后的数列gcd最大. 题解:可以通过对数列进行预处理,求出从下标为1开始的数对于前面的数的gcd(数组从下标0开始),称为前缀gcd,再以 ...

  7. HDU6205 Coprime Sequence 2017-05-07 18:56 36人阅读 评论(0) 收藏

    Coprime Sequence                                                        Time Limit: 2000/1000 MS (Ja ...

  8. codeforces 579D D. "Or" Game(前后缀+贪心)

    题目链接: D. "Or" Game time limit per test 2 seconds memory limit per test 256 megabytes input ...

  9. HDU 6186 CS Course【前后缀位运算枚举/线段树】

    [前后缀枚举] #include<cstdio> #include<string> #include<cstdlib> #include<cmath> ...

随机推荐

  1. tctip打赏小插件

    tctip是一个js插件,作用是在web网页右侧生成一个打赏浮动窗 使用方法 页面使用(多数人的使用方式) 插件下载地址 第一步,引入js 一般引入min版本,即引入tctip-版本号.min.js文 ...

  2. 开发自己的网上支付案例代码(易宝支付php)

    1.简单的图解(如上所示) 易宝支付与支付宝是不一样的,但也有类似之处,支付宝是专门为淘宝软件开发的一套机制,资金会在中间支付公司(支付宝)停留,等待顾客确认,当顾客确认后,才会真正扣钱.而易宝支付时 ...

  3. Java 面试题 百度/参考的答案

    "a=b"和"a.equals(b)"有什么区别? 如果 a 和 b 都是对象,则 a==b 是比较两个对象的引用,只有当 a 和 b 指向的是堆中的同一个对象 ...

  4. IE8 如何 不显示 选项 卡,直接在任务显示 各个页面?

    IE8  如何 不显示 选项 卡,直接在任务显示 各个页面? 在  工具->Internet 选项(o) ->常规--选项卡-设置->把第一个勾去掉  “启用选项卡浏览(需要重新启动 ...

  5. ZYNQ的Linux Linaro系统镜像制作SD卡启动

    ZYNQ的Linux Linaro系统镜像制作SD卡启动 0. 概述 ZYNQ生成uboot的时候和正常的ARM设备不太一样,ZYNQ属于二次辅助启动uboot然后由uboot启动内核,大概意思就是 ...

  6. mvc:interceptor拦截器使用

    在spring-mvc.xml里面配置标签 <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/* ...

  7. 【转】PG数据库高级用法 之 12306 -- 链接

    这么牛的文章很少见,仅附链接,以示敬仰. https://github.com/digoal/blog/blob/master/201611/20161124_02.md

  8. 记一次 Android 客户端(CJYYKT)的逆向

    主角: 描述: 湖南省教育局推的一款大学生 App,需要每个学生看完里面的一个课程的视频,共 8 章,每章 10 - 23 个视频(连续播放大约 24 小时),每个视频每隔不定时间就会弹出一个选择题答 ...

  9. android stadio 编译报错:download fastutil-7.2.0.jar

    在Ubuntu上面,新安装的stadio,第一次编译项目的时候, 一直开在下载 fastutil-7.2.0.jar 原因是需要FQ.那么改一下你的buil.gradle buildscript { ...

  10. CLR via C#读书笔记二:类型基础

    1.CLR允许将对象转换为它的(实际)类型或者它的任何基类型. 2.is操作符检测对象是否兼容于指定类型,is操作符永远不抛出异常. 3.as操作符返回对同一个对象的非null引用.如果对象不兼容,a ...