codechef Open the Dragon Scroll bitset容器的运用
来源:互联网 发布:数控加工工艺编程实例 编辑:程序博客网 时间:2024/06/08 07:18
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:33 5 45 0 14 3 7Output:71614
使用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容器的运用
- Open Blue Dragon的测试
- 特殊容器bitset的用法
- 1573 美丽的集合 bitset的运用
- 微信小程序的视图容器-- scroll-view
- c++stl的bitset位集合容器
- Open Blue Dragon,一个免费的cfm服务器
- BZOJ-3687 Bitset运用
- STL 容器的运用场合
- UVA-11995 I Can Guess the Data Structure!(STL:各容器的简单运用)
- STL容器之 bitset
- C++中的bitset容器
- Bitset位集合容器
- STL容器之bitset
- STL容器之bitset
- bitset位集合容器
- bitset 位容器集合
- The Dragon of Loowater
- 第九、十周研究报告
- 老板凭啥提拔你:如何得到老板赏识
- ZOJ 1122 Clock
- 使用eclipse调试cocos2d-x C++ 代码(编写+真机调试,放弃VS)
- centos 安装eclipse
- codechef Open the Dragon Scroll bitset容器的运用
- 页面上插入视频播放器时,经常会遇到视频挡住js弹框的问题:
- 推荐几款不错的报表工具
- 关于EditText的inputType属性
- Oracle WebCenter 介绍
- leetcode Best Time to Buy and Sell Stock II
- [2013山东ACM省赛] Contest Print Server (模拟)
- 答读者问(2):有关研究生论文及实习等问题
- 转贴:svnserve作为Windows服务