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 Input331 1 152 2 2 3 241 2 4 8 Sample Output122 比赛时数据水,被我水过去啦==(当时思路是:随便找三个数,然后去枚举他们的约数)#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;}相关文章推荐
- HDU6025 Coprime Sequence —— 前缀和 & 后缀和
- HDU 6025 Coprime Sequence 前缀gcd + 后缀gcd
- HDU6025 Coprime Sequence【前缀GCD+后缀GCD】
- HDU6025 Coprime Sequence (前缀后缀处理)
- HDU 6025 Coprime Sequence(前缀后缀GCD问题)
- HDU - 6025 Coprime Sequence(前缀,后缀,gcd)
- HDU6025 Coprime Sequence【前缀GCD+后缀GCD】
- hdu 6025 Coprime Sequence (前后缀GCD)
- HDU 6025 Coprime Sequence (前后缀+GCD)
- Coprime Sequence(前后缀最大公约数)
- 2017广西邀请赛/hdu6186 CS Course (维护前缀和后缀)
- (HDU 6025 女生专场)Coprime Sequence 水题
- hdu6025 Coprime Sequence(2017女生赛)
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D 前缀后缀维护
- HDU - 6025 Coprime Sequence
- HDU 6025 Coprime Sequence
- hdoj 6025 Coprime Sequence
- HDU 6025 Coprime Sequence
- CSP认证训练题——Coprime Sequence HDU - 6025
- Coprime Sequence