hdu 6025 Coprime Sequence

17 篇文章 0 订阅

题目链接:点这里

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

【题意】

给你一列数,让你从中删去一个,问你删掉一个后剩下所有数的最大公约数是多少。

【分析】

显然必须枚举每次删掉哪个数,但是如果每次都计算一下剩下数字的GCD显然不现实,所以可以想到提前弄出GCD的前缀和后缀,然后删掉一个数的意思就是该数前面的前缀和该数后面的后缀的GCD。

【代码】

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<sstream>
using namespace std;
#define rep0(i,l,r) for(int i = (l);i < (r);i++)
#define rep1(i,l,r) for(int i = (l);i <= (r);i++)
#define rep_0(i,r,l) for(int i = (r);i > (l);i--)
#define rep_1(i,r,l) for(int i = (r);i >= (l);i--)
#define MS0(a) memset(a,0,sizeof(a))
#define MS_1(a) memset(a,-1,sizeof(a))
#define MSinf(a) memset(a,0x3f,sizeof(a))
#define MSfalse(a) memset(a,false,sizeof(a))
#define pin1(a) printf("%d",(a))
#define pin2(a,b) printf("%d%d",(a),(b))
#define pin3(a,b,c) printf("%d%d%d",(a),(b),(c))
#define pll1(a) printf("%lld",(a))
#define pll2(a,b) printf("%lld%lld",(a),(b))
#define pll3(a,b,c) printf("%lld%lld%lld",(a),(b),(c))
#define pdo1(a) printf("%f",(a))
#define pdo2(a,b) printf("%f%f",(a),(b))
#define pdo3(a,b,c) printf("%f%f%f",(a),(b),(c))
#define huiche puts("")
#define inf 0x3f3f3f3f
#define lson (ind<<1),l,mid
#define rson ((ind<<1)|1),mid+1,r
#define uint unsigned int
typedef pair<int,int> PII;
#define A first
#define B second
#define pb push_back
#define mp make_pair
#define ll long long
#define eps 1e-8
inline void read1(int &num) {
    char in;
    bool IsN=false;
    in=getchar();
    while(in!='-'&&(in<'0'||in>'9')) in=getchar();
    if(in=='-') {
        IsN=true;
        num=0;
    } else num=in-'0';
    while(in=getchar(),in>='0'&&in<='9') {
        num*=10,num+=in-'0';
    }
    if(IsN) num=-num;
}
inline void read2(int &a,int &b) {
    read1(a);
    read1(b);
}
inline void read3(int &a,int &b,int &c) {
    read1(a);
    read1(b);
    read1(c);
}
inline void read1(ll &num) {
    char in;
    bool IsN=false;
    in=getchar();
    while(in!='-'&&(in<'0'||in>'9')) in=getchar();
    if(in=='-') {
        IsN=true;
        num=0;
    } else num=in-'0';
    while(in=getchar(),in>='0'&&in<='9') {
        num*=10,num+=in-'0';
    }
    if(IsN) num=-num;
}
inline void read2(ll &a,ll &b) {
    read1(a);
    read1(b);
}
inline void read3(ll &a,ll &b,ll &c) {
    read1(a);
    read1(b);
    read1(c);
}
inline void read1(double &num) {
    char in;
    double Dec=0.1;
    bool IsN=false,IsD=false;
    in=getchar();
    while(in!='-'&&in!='.'&&(in<'0'||in>'9'))
        in=getchar();
    if(in=='-') {
        IsN=true;
        num=0;
    } else if(in=='.') {
        IsD=true;
        num=0;
    } else num=in-'0';
    if(!IsD) {
        while(in=getchar(),in>='0'&&in<='9') {
            num*=10;
            num+=in-'0';
        }
    }
    if(in!='.') {
        if(IsN) num=-num;
        return ;
    } else {
        while(in=getchar(),in>='0'&&in<='9') {
            num+=Dec*(in-'0');
            Dec*=0.1;
        }
    }
    if(IsN) num=-num;
}
inline void read2(double &a,double &b) {
    read1(a);
    read1(b);
}
inline void read3(double &a,double &b,double &c) {
    read1(a);
    read1(b);
    read1(c);
}
///-------------------------------------------------------------------------------------------------------------------
int n;
const int maxm = 100000+100;
int num[maxm];
int l[maxm];
int r[maxm];
int main() {
//    freopen("in.txt","r",stdin);
    int T;
    read1(T);
    while(T--){
        read1(n);
        rep1(i,1,n) read1(num[i]);
        rep1(i,1,n) l[i]=__gcd(l[i-1],num[i]);
        rep_1(i,n,1) r[i]=__gcd(r[i+1],num[i]);
        int ans=1;
        rep1(i,1,n) ans=max(ans,__gcd(l[i-1],r[i+1]));
        pin1(ans),huiche;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zuhiul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值