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 ...
随机推荐
- sql语句遇到错误: The used SELECT statements have a different number of columns :
这是因为使用union的两个SQL语句产生的记录的表结构不一致. 必须是结构完全一致的记录集合才可以使用UNION. 以上就是两个表的字段不一样,导致,所以大家可以检查下.
- day13作业
#作业1猜年龄 , 可以让用户最多猜三次! # Auther:bing #!/usr/bin/env python age = 24 print("猜年龄") for i in r ...
- LeetCode OJ:Divide Two Integers(两数相除)
Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...
- react: navigator
1.page js import React from "react"; import {Link} from "react-router-dom"; impo ...
- Golang 编译成 DLL 文件
golang 编译 dll 过程中需要用到 gcc,所以先安装 MinGW. windows 64 位系统应下载 MinGW 的 64 位版本: https://sourceforge.net/pro ...
- win10 135/445端口关闭
135端口关闭 见[在 Windows 下关闭135/139/445端口的图文方法] 445端口关闭 打开控制面板-->网络和共享中心-->更改适配器设置-->右键点击正在使用的网卡 ...
- 运行python代码
IPython IPython 'magic' function documentation
- vue前端开发那些事——前言
如上图所示,用vue开发一个小型网站所涉及到的知识点.这只是前端部分已经这么多了.接下来我分解开来说. 1.Node 当我们开发vue项目的时候,首先要安装Node.js,那么我们即使当时不理解为什么 ...
- LeetCode 616. Add Bold Tag in String
原题链接在这里:https://leetcode.com/problems/add-bold-tag-in-string/description/ 题目: Given a string s and a ...
- 学大伟业DAY2模拟赛
T1忍者钩爪 题目描述 小Q是一名酷爱钩爪的忍者,最喜欢飞檐走壁的感觉,有一天小Q发现一个练习使用钩爪的好地方,决定在这里大显身手. 场景的天花板可以被描述为一个无穷长的数轴,初始小Q挂在原点上.数轴 ...