牛客挑战赛57 A 构造题【最大公约数】【玄学?】

牛客挑战赛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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值