【Codeforces Round 262 (Div 2)D】【构造】Little Victor and Set 集合最多取k数使得异或值尽可能小

Little Victor and Set
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Victor adores the sets theory. Let us remind you that a set is a group of numbers where all numbers are pairwise distinct. Today Victor wants to find a set of integers S that has the following properties:

  • for all x  the following inequality holds l ≤ x ≤ r;
  • 1 ≤ |S| ≤ k;
  • lets denote the i-th element of the set S as si; value  must be as small as possible.

Help Victor find the described set.

Input

The first line contains three space-separated integers l, r, k (1 ≤ l ≤ r ≤ 1012; 1 ≤ k ≤ min(106, r - l + 1)).

Output

Print the minimum possible value of f(S). Then print the cardinality of set |S|. Then print the elements of the set in any order.

If there are multiple optimal sets, you can print any of them.

Examples
input
8 15 3
output
1
2
10 11
input
8 30 7
output
0
5
14 9 28 11 16
Note

Operation  represents the operation of bitwise exclusive OR. In other words, it is the XOR operation.


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
LL l, r, k, top;
LL a[20], b[20];
LL ans, ansnum;
void dfs(LL p, LL v, LL num)
{
	if (num && v < ans)
	{
		ans = v;
		ansnum = num;
		MC(b, a);
	}
	if (p > top || num == k)return;
	dfs(p + 1, v, num);
	a[num+1] = p;
	dfs(p + 1, v^p, num + 1);
}
int main()
{
	while (~scanf("%lld%lld%lld", &l,&r,&k))
	{
		if (k == 3)
		{
			bool flag = 0;
			for (LL i = 2; i <= r; i <<= 1)
			{
				LL half = i / 2;
				LL z = i - 1;
				LL y = z + half;
				LL x = y^z;
				if (z >= l&&x <= r)
				{
					puts("0");
					puts("3");
					printf("%lld %lld %lld\n", x, y, z);
					flag = 1;
					break;
				}
			}
			if (flag)continue;
		}
		top = min(r, l + 4);
		ans = 1e18;
		dfs(l, 0, 0);
		printf("%lld\n", ans);
		printf("%lld\n", ansnum);
		for (int i = 1; i <= ansnum; ++i)
		{
			printf("%lld ", b[i]);
		}puts("");
	}
	return 0;
}
/*
【题意】
给你一个区间[l,r],区间内最少取1个数,最多取k(1e12)个数。1<=k<=min(1e6,r-l+1)。
我们相使得所取数的异或值尽可能小。

【类型】
暴力

【分析】
首先,连续取2个相邻数的话,异或和很可能就为1
连续取4个相邻数的话,异或和很可能就为0
显然的是,在5个连续数a,b,c,d,e中,
a^b^c^d和b^c^d^e必然有一个为0。
于是我们可以在小范围内,暴力dfs出一个解。
即k>=4和k为1或2的时候,我们都有决策了。
于是,剩下需要注意的就是k==3的时候。
关键就变成——如何取3个数并使得异或值为0。

然后我们可以采取的贪心策略是——
首先,如果最大数和最小数的位数间隙为2或者更大的话,我们就可以有下面这种构造方案:
1100
1000
0100
于是,剩下最大数和最小数的位数间隙为1。
我们可以用以下
1100000……
1011111……
0111111……
的方式做构造,这样3个数的范围会尽可能缩小。而且是上下界同时缩小,就可以了。

诗诗的做法是——
取l最高位的1
	11..0..0
	10..1..0
l:	 1..1..0
然后对于l,如果这一位为1,那么就走01;如果为0,就走00,
显然不影响三个数的大小关系且使得a>b>c中a的值尽可能小
其实本质相同。

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值