Codeforces460D. Little Victor and Set 构造

题解:

k=1k=2k>=4 k = 1 、 k = 2 、 k >= 4 的情况都是很简单的,只需要利用 UOJ#350. 新年的XOR的性质即可,对于 k=3 k = 3 的情况,把 r r 转成二进制,然后若从左到右的第二个1出现在从右往左数的第i位(从1开始数),那么令 t=2i1 t = 2 i − 1 ,若 l<=t l <= t ,那么可以构造出三个数的异或和为 0 0 ,分别为rtr xor t,否则按照 k=2 k = 2 的情况来搞,可以证明这是最优的构造方法。

代码:

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pa pair<int,int>
LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
    return x*f;
}
LL ans=-1,a[10],b[10],len,k;
void dfs(LL x,LL lim,LL s,int ch)
{
    if(x==lim+1)
    {
        if(ch&&ch<=k)
        {
            if(ans==-1||s<ans)
            {
                len=ch;
                ans=s;
                for(int i=1;i<=ch;i++)a[i]=b[i];
            }
        }
        return;
    }
    b[ch+1]=x;dfs(x+1,lim,s^x,ch+1);
    dfs(x+1,lim,s,ch);
}
int main()
{
    LL l=read(),r=read();k=read();
    if(k==1)printf("%lld\n1\n%lld",l,l);
    else if(k==2)
    {
        if(l+1==r)
        {
            if(l<(l^(l+1)))printf("%lld\n1\n%lld",l,l);
            else printf("%lld\n2\n%lld %lld",l^(l+1),l,l+1);
        }
        else if(!(l&1))printf("1\n2\n%lld %lld",l,l+1);
        else printf("1\n2\n%lld %lld",l+1,l+2);
    }
    else if(k==3)
    {
        LL t1=r,t2,c[45],lc=0;
        while(t1)c[++lc]=(t1&1LL),t1>>=1LL;
        for(int i=1;i<=(lc/2);i++)swap(c[i],c[lc-i+1]);
        for(int i=1;i<=lc;i++)
        if(c[i+1]){t2=(1LL<<(lc-i))-1;break;}
        if(l<=t2)printf("0\n3\n%lld %lld %lld\n",r,t2,r^t2);
        else
        {
            if(!(l&1)||l+1==r)printf("1\n2\n%lld %lld",l,l+1);
            else printf("1\n2\n%lld %lld",l+1,l+2);
        }
    }
    else
    {
        if(r-l>=6)
        {
            if(l%4)l+=(4-l%4);
            printf("0\n4\n%lld %lld %lld %lld",l,l+1,l+2,l+3);
        }
        else
        {
            dfs(l,r,0,0);
            printf("%lld\n%lld\n",ans,len);
            for(int i=1;i<=len;i++)printf("%lld ",a[i]);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值