[LibreOJ β Round #4]子集

题目大意

给你一个集合,请选出一个子集,使得两两元素的gcd或分别+1后的gcd至少一个不等于1,最大化子集的大小。

最大团

看起来是个最大团模型,裸上random_shuffle+greedy可过。

#include<cstdio>
#include<algorithm>
#include<map>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
map<pi,ll> f;
ll a[500+10],ans[500+10];
int i,j,k,l,t,n,m,now,ca,top;
bool czy;
ll gcd(ll a,ll b){
    if (!b) return a;
    return gcd(b,a%b);
}
int main(){
    scanf("%d",&n);
    fo(i,1,n) scanf("%lld",&a[i]);
    fo(i,1,n)
        if (a[i]%2==0) now++;
    if (now<n-now) now=n-now;
    ca=50;
    while (ca--){
        random_shuffle(a+1,a+n+1);
        top=0;
        fo(i,1,n){
            if (!top) ans[++top]=a[i];
            else{
                czy=1;
                fo(j,1,top)
                    if (gcd(ans[j],a[i])!=1||gcd(ans[j]+1,a[i]+1)!=1) czy=1;
                    else{
                        czy=0;
                        break;
                    }
                if (czy) ans[++top]=a[i];
            }
        }
        now=max(now,top);
    }
    printf("%d\n",now);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值