牛客挑战赛57 A 构造题
题目描述
思路
因为数据比较水,这题有太多玄学做法了(同机房有人第二个样例没过直接AC的)
首先找到数组中的最小值
a
m
i
n
a_{min}
amin,答案一定小于等于
a
m
i
n
+
1
a_{min}+1
amin+1
然后用
n
o
w
now
now 表示当前的最大答案,向后循环时
n
o
w
=
m
a
x
(
g
c
d
(
n
o
w
,
a
[
i
]
)
,
g
c
d
(
n
o
w
,
a
[
i
]
+
1
)
)
now=max(gcd(now,a[i]),gcd(now,a[i]+1))
now=max(gcd(now,a[i]),gcd(now,a[i]+1))
建议先将数组排序
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<vector>
#include<cmath>
#include<ctime>
// #define r register
#define rep(i,x,y) for(ll i=x;i<=y;++i)
#define per(i,x,y) for(ll i=x;i>=y;--i)
using namespace std;
typedef long long ll;
const ll V=1e6+100;
ll n,a[V],now1,now2;
inline ll in()
{
ll res=0,f=1;
char ch;
while((ch=getchar())<'0'||ch>'9')
if(ch=='-') f=-1;
res=res*10+ch-48;
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-48;
return res*f;
}
ll gcd(ll x,ll y)
{
return y==0?x:gcd(y,x%y);
}
int main()
{
n=in();
rep(i,1,n) a[i]=in();
sort(a+1,a+n+1);
now1=a[1],now2=a[1]+1;
rep(i,2,n)
{
now1=max(gcd(now1,a[i]),gcd(now1,a[i]+1));
now2=max(gcd(now2,a[i]),gcd(now2,a[i]+1));
}
cout<<max(now1,now2);
return 0;
}