coprime Sequence
``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的更多相关文章
- HDU6205 Coprime Sequence 2017-05-07 18:56 36人阅读 评论(0) 收藏
Coprime Sequence Time Limit: 2000/1000 MS (Ja ...
- Coprime Sequence(前后缀GCD)
Description Do you know what is called ``Coprime Sequence''? That is a sequence consists of $n$ posi ...
- HDU - 6025 Coprime Sequence(gcd+前缀后缀)
Do you know what is called ``Coprime Sequence''? That is a sequence consists of nnpositive integers, ...
- HDU6025 Coprime Sequence —— 前缀和 & 后缀和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6025 Coprime Sequence Time Limit: 2000/1000 MS (Java/ ...
- HDU6025 Coprime Sequence(gcd)
HDU6025 Coprime Sequence 处理出数列的 \(gcd\) 前缀和后缀,删除一个数后的 \(gcd\) 为其前缀和后缀的 \(gcd\) . 遍历数列取 \(max\) 即为答案. ...
- HDU - 6025 Coprime Sequence(前缀gcd+后缀gcd)
题意:去除数列中的一个数字,使去除后数列中所有数字的gcd尽可能大. 分析:这个题所谓的Coprime Sequence,就是个例子而已嘛,题目中没有任何语句说明给定的数列所有数字gcd一定为1→_→ ...
- Coprime Sequence (HDU 6025)前缀和与后缀和的应用
题意:给出一串数列,这串数列的gcd为1,要求取出一个数使取出后的数列gcd最大. 题解:可以通过对数列进行预处理,求出从下标为1开始的数对于前面的数的gcd(数组从下标0开始),称为前缀gcd,再以 ...
- HDU 6025 Coprime Sequence
枚举,预处理. 预处理前缀$gcd$与后缀$gcd$,枚举删哪一个即可. #include <bits/stdc++.h> using namespace std; int T,n; ]; ...
- nyoj CO-PRIME 莫比乌斯反演
CO-PRIME 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 This problem is so easy! Can you solve it? You are ...
随机推荐
- LeetCode OJ:Permutations II(排列II)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【SQL查询】日期的转换_to_date/to_char
1. 日期转换为字符 select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual; 2. 字符转换为日期 select to_date('200 ...
- setInterval(callback(),time)
最近在写一个需求的时候,出了点小小的问题,在这做个记录. 对于定时函数setInterval()大家应该都不陌生,setInterval(callback(),time)就是说设置一个定时器,每隔ti ...
- 第二章 Linux目录介绍
一级目录 /bin (普通用户)二进制命令所在目录 备注 /boot LINUX内核及系统引导程序所需的文件目录 常见分区:128M swap内存的1.5倍 /dev 设备文件的目录 比如声卡.磁盘. ...
- 一个高性能RPC框架原理剖析
业务与底层网络通信分离 Server大部分主要分为两层: 网络接收层:负责监听端口,负责收包,编码,解码工作,负责将响应包回传给客户端. 业务处理层:负责接收网络接收层完整的包,如果是RPCserve ...
- squid http_access中的逻辑关系
http_access通过acl实现访问控制,方法 acl A acltype argument acl B acltype argument 逻辑关系:或 http_access allow|den ...
- JDBC 1 利用Statement对数据库进行增删改查
准备工作 1新建po类:User private int id; private String name; private String pwd; set,get方法省略 2 新建UserDao类, ...
- js对象原型链
JavaScript 规定,每一个构造函数都有一个 prototype 属性,指向另一个对象.这个对象的所有属性和方法,都会被构造函数的所拥有. 这也就意味着,我们可以把所有对象实例需要共享的属性和方 ...
- 积累 ---- PHP可能会遇到的面试题
1.白盒测试和黑盒测试的区别 2.Bootstrap是什么 3.OOP是什么意思 4.git和svn的使用 5.常用的git命令 6.lamp开发环境 7.高内聚,低耦合
- PHP 操作XML文档
<<<操作符需PHP5.3以上版本才能支持,下面程序在wamp环境下测试完成. <?php // Set the content type to be XML, so that ...