Coprime Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
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
一开始是用输入输出流cin、cout进行的操作,最后987msAC。但是,有朋友及时帮我找到了代码中的错误,发现有2 3 6 9 10这样的样例无法通过。下面是我修改后并AC的代码,用时237ms。
谢谢大家及时的指正。今后的路上还请多多指教~
大家一起学习,共同进步!
研究问题研究透并且还能态度谦和的点出别人的错误并指正的学习方法真的是应该好好去学习的!
#include
#include
#include
#include
#include
#include
#include
#define ll long long int
#define maxsize 105000
#define INF 99999999
using namespace std;
ll a[maxsize];
ll yu[maxsize];
ll yv[maxsize];
ll gcd(ll x, ll y)
{
ll c;
c = y%x;
if (c == 0)
return x;
else
{
gcd(c, x);
}
}
int main()
{
int t;
int n;
while (~scanf("%d",&t))
{
while (t--)
{
scanf("%d",&n);
for (int i = 0;i < n;i++)
{
scanf("%d",&a[i]);
}
sort(a, a + n);
int min1 = INF, min2 = INF;
int t1, t2;
for (int i = 2;i < n;i++)
{
yu[i] = gcd(a[0], a[i]);
yv[i] = gcd(a[1], a[i]);
if (min1 > yu[i])
{
min1 = yu[i];
t1 = i;
}
if (min2 > yv[i])
{
min2 = yv[i];
t2 = i;
}
}
if (n==3||min1 != min2)//上述两个排序中没有进行对a[0]与a[1]之间的gcd,但实际上这一步已经将其考虑进去了。
{
printf("%d\n",max(min1, min2));
}
else
{
//cout<
if(t1==t2)//如果t1==t2,说明引起gcd最小值的是同一个元素,那么忽视这个元素,最小gcd就有可能变大
{
yu[t1] = INF;
yv[t2] = INF;
min1 = INF;
min2 = INF;
for (int i = 2;i < n;i++)
{
if (min1 > yu[i])
{
min1 = yu[i];
}
if (min2 > yv[i])
{
min2 = yv[i];
}
}
printf("%d\n",min(min1,min2));
}
else
{
printf("%d\n",min1);//此时min1=min2,通过删除一个元素的操作也无法使得最小gcd变大
}
}
}
}
return 0;
}
正解:
#include
#include
#include
#include
#include
#include
#include
#define ll long long int
#define maxsize 105000
#define INF 99999999
using namespace std;
ll a[100010];
ll yu[maxsize];
ll yv[maxsize];
ll gcd(ll x, ll y)
{
if (x > y)
{
int k;
k = y;
y = x;
x = k;
}
ll c;
c = y%x;
if (c == 0)
return x;
else
{
gcd(c, x);
}
}
int main()
{
int t;
int n;
while (cin >> t)
{
while (t--)
{
cin >> n;
int min1;
memset(yu, 0, sizeof(yu));
memset(yv, 0, sizeof(yv));
for (int i = 1;i <= n;i++)
{
cin >> a[i];
}
yu[1] = a[1];
yu[0] = INF;//重点!
for (int i = 2;i <= n;i++)//正向gcd
{
yu[i] = gcd(yu[i - 1], a[i]);
}
yv[n] = a[n];
yu[n + 1] = INF;//重点!
for (int i = n - 1;i >0;i--)//反向gcd
{
yv[i] = gcd(a[i], yv[i + 1]);
}
for (int i = 1;i <=n;i++)//寻找最小点并删除
{
if (yu[i] == yv[i])
{
min1 = i;
break;
}
}
cout << min(yu[min1 - 1], yv[min1 + 1]) << endl;
}
}
return 0;
}