您的位置:首页 > 其它

hdu6025(思维僵化)

2017-10-18 19:35 155 查看
Total Submission(s): 1327    Accepted Submission(s): 637


Problem 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≤T≤10),
denoting the number of test cases.

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

Then the following line consists of n integers a1,a2,...,an(1≤ai≤109),
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

 

Source

2017中国大学生程序设计竞赛 - 女生专场

题意:给你n个数,求去掉其中一个数之后整个序列的最大公约数最大为多少。
f17c

思路:由于只去掉一个数,我们可以正着求一遍所有数的gcd,反着求一遍。将正反的结果再来求一遍就可以了。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
int a[maxn],sum1[maxn],sum2[maxn];
int gcd(int x,int y)
{
while(x^=y^=x^=y%=x);
return y;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
memset(sum1,0,sizeof(sum1));
memset(sum2,0,sizeof(sum2));
sum1[1]=a[1],sum2[n]=a[n];
for(int i=2;i<=n;i++) sum1[i]=gcd(sum1[i-1],a[i]);
for(int i=n-1;i>=1;i--) sum2[i]=gcd(sum2[i+1],a[i]);
int ans=sum2[2];
for(int i=2;i<=n-1;i++) ans=max(ans,gcd(sum1[i-1],sum2[i+1]));
ans=max(ans,sum1[n-1]);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: