标签:style blog class code ext color
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
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 )
For each case, output the maximum possible value of (shuffle-A xor shuffle-B) in a separate line.
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容器的运用,布布扣,bubuko.com
codechef Open the Dragon Scroll bitset容器的运用
标签:style blog class code ext color
原文地址:http://blog.csdn.net/kenden23/article/details/24981457