codechef Open the Dragon Scroll bitset容器的运用

Open the Dragon Scroll

Did you ever hear about ‘Dragon Food‘ ? Its used to refer to the chocolates bought for your loved ones :). Po offers dragon food to master Shifu, who is a famous cook in the valley of food. In return, Shifu hands over the dragon scroll to Po, which is said
to hold the ingredients of the secret recipe. To open the dragon scroll, one has to solve the following puzzle.

1. Consider a N-bit integer A. We call an integer A‘ as shuffle-A, if A‘ can be obtained by shuffling the bits of A in its binary representation. For eg. if N = 5 and A = 6 = (00110)2, A‘ can be any 5-bit integer having exactly two 1s in it i.e.,
any of (00011)2, (00101)2, (00110)2, (01010)2, ...., (11000)2.

2. Given two N-bit integers A and B, find the maximum possible value of (A‘ xor B‘) where A‘ is a shuffle-A, B‘ is a shuffle-B and xor is the bit-wise xor operator.

Given N, A and B, please help Po in opening the dragon scroll.

Notes

1. xor operator takes two bit strings of equal length and performs the logical XOR operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 OR only the second bit is 1, but will be 0 if both are 1 or both are
0. For eg: 5 (0101) xor 3(0011) = 6(0110). In most languages it is represented using ^ symbol. 5 ^ 3 = 6.

2. If the integer actually needs less than N bits to represent in binary, append sufficient number of leading 0 bits. For eg. as shown in the problem statement for N = 5, A = 6 = (00110)2

Input

First line contains an integer T ( number of test cases, around 100 ). T cases follow, each having N A B in a single line, separated by a space. ( 1 <= N <= 30, 0 <= A,B < 2N )

Output

For each case, output the maximum possible value of (shuffle-A xor shuffle-B) in a separate line.

Example

Input:
3
3 5 4
5 0 1
4 3 7

Output:
7
16
14

使用bitset容器来解决果然比较方便,看看bitset是如何使用的:

#pragma once
#include <stdio.h>
#include <bitset>

int OpentheDragonScroll()
{
	int T, N, a, b;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &N);
		scanf("%d %d", &a, &b);
		std::bitset<32> A = a;
		std::bitset<32> B = b;
		int aones = 0, azeros = 0, bones = 0, bzeros = 0;
		for (int i = 0; i < N; i++)
		{
			if (A[i]) aones++;
			else azeros++;
			if (B[i]) bones++;
			else bzeros++;
		}

		int n = std::min(aones+bones, azeros+bzeros);
		A.reset();
		for (int i = N - 1, j = 0; j < n ; i--, j++)
		{
			A[i] = 1;
		}
		n = A.to_ulong();
		printf("%d\n", n);
	}
	return 0;
}

codechef Open the Dragon Scroll bitset容器的运用

时间: 2024-08-03 02:57:58

codechef Open the Dragon Scroll bitset容器的运用的相关文章

C++ 之 bitset

在PC端文件播放器中,多画面管理时,将整个画面切割成N个矩形块.将打开显示与否的信息放在bitset容器中,今天就来对bitset做个小结 1.bitset可以表示二进制的有序集,用bitset来处理程序中的需要保存一组标志的操作很简便,下面总结一下bitset的一些函数. 2.定义和初始化bitset: 定义一个bitset时需要声明它包含多少个二进制位,大小必须是一个常量表达式,bitset中的二进制位是未命名的,可以通过位置来访问.编号从右到左依次从0位开始,从低到高. 例:bitset<

C++ Primer 学习笔记_14_标准模板库_bitset位集合容器

C++ Primer 学习笔记_14_标准模板库_bitset位集合容器 bitset容器是一个bit位元素的序列容器,每个元素只占一个bit位,取值为0或1,因而很节省内存空间.下图是一个bitset的存储示意图,它的10个元素只使用了两个字节的空间. 使用bitset需要声明头文件"#include <bitset>" 1.创建bitset对象 创建bitset对象时,必须要指定容器的大小.bitset对象的大小一经定义,就不能修改了.下面这条语句就定义了bitset对

hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题

题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给定的学生成绩都异或上一次的答案 先将学生按每一门成绩都排一次序 这里将学生分块成sqrt(n)的块数,然后在当前块中用bitset容器来记录含有学生的状态 这里可以记录状态的前缀和,因为比后面成绩好的,必然比前面的学生的成绩也好 查询的时候只要查到正好比他高的学生属于哪一块,这样只要访问sqrt(n

C++ STL 基础及应用(6) 容器

读者可能有这样的经历,自己编写了动态数组类.链表类.集合类和映射类等程序,然后小心地维护着.其实 STL 提供了专家级的几乎我们所需要的各种容器,功能更好,效率更高,复用性更强,所以开发应用系统应该首选 STL 容器类,摒弃自己的容器类,尽管它可能花费了你很多的开发时间. 本章将介绍 STL 中的通用容器 包括 vector.deque.list.queue和stack.priority_queue.bitset.set和multiset.map和multimap等等. 概述 容器分类 (1)序

用vector与bitset分别创建1亿以内的素数表,比较快慢

vector容器: 代码如下: #include<iostream>#include<vector>#include<ctime>using namespace std; int main(){ int num=0; clock_t t=clock(); vector<int> p(100000001,1);   //设置大小为1亿,初始值为1 for(int i=2;i<=10000;i++) if(p[i]) for(int j=i*i;j<

第10章 bit_vector位向量容器

第10章 bit_vector位向量容器   10.1 bit_vector技术原理   10.2 bit_vector应用基础   10.3 本章小结 这本书讲bit_vector,而不讲bitset容器,有点奇怪.   略

标准非STL之bitset

template <size_t N> class bitset; BitsetA bitset stores bits (elements with only two possible values: 0 or 1, true or false, ...).[bitset存储位(元素只能为两种可能的数值,即0或1,true或false,...)]The class emulates an array of bool elements, but optimized for space allo

CH 2101 - 可达性统计 - [BFS拓扑排序+bitset状压]

题目链接:传送门 描述 给定一张N个点M条边的有向无环图,分别统计从每个点出发能够到达的点的数量.N,M≤30000. 输入格式 第一行两个整数N,M,接下来M行每行两个整数x,y,表示从x到y的一条有向边. 输出格式 共N行,表示每个点能够到达的点的数量. 样例输入 10 10 3 8 2 3 2 5 5 9 5 9 2 3 3 9 4 8 2 10 4 9 样例输出 1 6 3 3 2 1 1 1 1 1 题解: 首先,如果用 $f(x)$ 代表从点 $x$ 出发所能到达的所有点的集合,应有

C++移位运算符

关于逻辑移位.算术移位可參见迅雷深大笔试题部分.的一道题. 曾经看到C++标准上说,移位运算符(<<.>>)出界时的行为并不确定: The behavior is undefined if the right operand is negative, orgreater than or equal to the length in bits of the promoted left operand. 我当时也没有深究过这个问题.前几天有个网友来信问起这件事,我才发现,这和Intel