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,反着求一遍。将正反的结果再来求一遍就可以了。
代码:
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; }
相关文章推荐
- 【犯二记录】链表结点换位引发的思维僵化,太可怕,智商 == 0
- 洛谷八连测——关于取模与思维僵化
- 小学教材失实一事体现出僵化的教育思维
- 小学教材失实一事体现出僵化的教育思维
- 软件开发中的思维僵化
- 考察思维的灵活性,僵化
- 软件开发中的思维僵化
- 思维僵化
- 是什么使我思维僵化
- 程序员思维定势
- hdu5922 2016CCPC东北地区大学生程序设计竞赛 A题 Minimum’s Revenge(思维,水题)
- IT项目管理的六种错误思维
- HDU-2136-Largest prime factor【素数表】【思维】
- 怀疑思维
- Codeforces 22B Bargaining Table【暴力+思维】
- 说说《降世神通:最后的气宗》:恐怖分子的思维模式 推荐
- HDU-5908 Abelian Period(思维)
- 沈坤商业思维3:破解理性,让你的顾客充满感性
- 移动互联时代背景下的分析方法及设计思维
- IT项目管理的六种错误思维!!