Do you know what is called ``Coprime Sequence''? That is a sequence consists of nn 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.

InputThe first line of the input contains an integer T(1≤T≤10)T(1≤T≤10), denoting the number of test cases.

In each test case, there is an integer n(3≤n≤100000)n(3≤n≤100000) in the first line, denoting the number of integers in the sequence.

Then the following line consists of nn integers a1,a2,...,an(1≤ai≤109)a1,a2,...,an(1≤ai≤109), denoting the elements in the sequence.OutputFor 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个数,通过删除一个数后使他们的最大公约数最大。这(n-1)个数的最大公约数必定是最小的两个数的因子之一。
 #include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<map>
using namespace std;
map<int,int>::iterator it;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,x=,t=,i,j,a[],ans=,l=;
scanf("%d",&n);map<int,int>mp;
mp.clear();
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]==)
x++;
}
if(x>=)
printf("1\n");
else
{
sort(a,a+n);
while(l--)
{
for(i=;i<=sqrt(a[l]);i++)
{
if(a[l]%i==)
{
mp[i]++;
if(i*i!=a[l])
mp[a[l]/i]++; }
}
}
for(i=;i<n;i++)
for(it=mp.begin();it!=mp.end();it++)
{
if(a[i]%(it->first)==)
it->second++;
}
for(it=mp.begin();it!=mp.end();it++)
if(it->second==n-)
ans=max(ans,it->first);
printf("%d\n",ans);
}
}
return ;
}

coprime Sequence的更多相关文章

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

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

  2. Coprime Sequence(前后缀GCD)

    Description Do you know what is called ``Coprime Sequence''? That is a sequence consists of $n$ posi ...

  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. HDU - 6025 Coprime Sequence(前缀gcd+后缀gcd)

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

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

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

  8. HDU 6025 Coprime Sequence

    枚举,预处理. 预处理前缀$gcd$与后缀$gcd$,枚举删哪一个即可. #include <bits/stdc++.h> using namespace std; int T,n; ]; ...

  9. nyoj CO-PRIME 莫比乌斯反演

    CO-PRIME 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 This problem is so easy! Can you solve it? You are ...

随机推荐

  1. Huffman编码实现文件的压缩与解压缩。

    以前没事的时候写的,c++写的,原理很简单,代码如下: #include <cstdio> #include <cstdlib> #include <iostream&g ...

  2. Linux命令2018-03-01更新

    前言:Linux主要应用于服务器端,嵌入式开发和个人pc桌面端 本人wechat:YWNlODAyMzU5MTEzMTQ=. *** GPL GPL是一个开源许可协议,由自由软件基金会创建的.GPL许 ...

  3. S2SH框架中的无刷新验证码功能实现

    暑假期间在实验室做使用S2SH框架的项目,其中登录和注册需要验证码,实现了一个没有实现刷新验证码功能的简单版本,代码如下: 1 package com.sem.action; 2 3 import j ...

  4. div垂直水平居中的四种方法总结

    5.利用弹性布局 与 margin: <style> .container{ height: 600px; width: 600px; border:1px solid black; di ...

  5. js实现把中文、英文标点转换

    所有英文符号转换成中文的符号 <SCRIPT LANGUAGE="JavaScript"> <!-- function meizz(str) { var tmp ...

  6. Zookeeper在Dubbo中的作用及Zk集群的选举原理

    转自 : https://blog.csdn.net/zh15732621679/article/details/80723358

  7. Java中BIO,NIO,AIO的理解

    在高性能的IO体系设计中,有几个名词概念常常会使我们感到迷惑不解.具体如下: 1 什么是同步? 2 什么是异步? 3 什么是阻塞? 4 什么是非阻塞? 5 什么是同步阻塞? 6 什么是同步非阻塞? 7 ...

  8. Oracle新用户以及授权的若干问题

    Database 实验4 问题: 授权语句 grant create table to user_name; 收回授权语句 revoke create table from user_name; 注意 ...

  9. php curl请求。header头中添加请求信息

    function get_data($key,$authorization,$url){ $headers = array(    'api-key:'.$key,    'authorization ...

  10. Cloudera Manager (centos)安装详细介绍

    文章全部来自:Cloudera Manager (centos)安装详细介绍http://www.aboutyun.com/thread-9190-1-1.html(出处: about云开发) 这里已 ...