题解:
k=1、k=2、k>=4 k = 1 、 k = 2 、 k >= 4 的情况都是很简单的,只需要利用 UOJ#350. 新年的XOR的性质即可,对于 k=3 k = 3 的情况,把 r r 转成二进制,然后若从左到右的第二个1出现在从右往左数的第位(从1开始数),那么令 t=2i−1 t = 2 i − 1 ,若 l<=t l <= t ,那么可以构造出三个数的异或和为 0 0 ,分别为,否则按照 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]);
}
}
}