您的位置:首页 > 产品设计 > UI/UE

2017女生赛 1003 Coprime Sequence【前缀后缀维护】

2017-05-07 14:26 417 查看

Coprime Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Problem DescriptionDo you know what is called ``Coprime Sequence''? That is a sequence consists of n positiveintegers, 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),denoting the number of test cases.In each test case, there is an integer n(3≤n≤100000) inthe 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. OutputFor each test case, print a single line containing a single integer, denoting the maximum GCD. Sample Input
331 1 152 2 2 3 241 2 4 8 Sample Output
122 比赛时数据水,被我水过去啦==(当时思路是:随便找三个数,然后去枚举他们的约数)#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<queue>#include<stack>#include<vector>#include<algorithm>using namespace std;#define ll long long#define ms(a,b)  memset(a,b,sizeof(a))const int M=1e6+10;const int inf=0x3f3f3f3f;int i,j,k,n,m;ll a[M];ll g1[M];ll g2[M];ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}int main(){int T;scanf("%d",&T);while(T--){ll kk;scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%lld",&a[i]);if(i==1)g1[1]=a[1],kk=a[1];else {g1[i]=gcd(kk,a[i]);kk=g1[i];}}for(int i=n;i>=1;i--){if(i==n)g2=a,kk=a;else {g2[i]=gcd(kk,a[i]);kk=g2[i];}}ll ans=1;for(int i=1;i<=n;i++){if(i==1)ans=max(ans,g2[i+1]);else if(i==n)ans=max(ans,g1[i-1]);else ans=max(ans,gcd(g1[i-1],g2[i+1]));}printf("%lld\n",ans);}return 0;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: