HDU 4759 Poker Shuffle 二进制分析(2013 ACM/ICPC Asia Regional Changchun Online A)
你为何这么叼!
Poker Shuffle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 214 Accepted Submission(s): 70
Problem Description
Jason is not only an ACMer, but also a poker nerd. He is able to do a perfect shuffle. In a perfect shuffle, the deck containing K cards, where K is an even number, is split into equal halves of K/2 cards which are then pushed together in a certain way so as to make them perfectly interweave. Suppose the order of the cards is (1, 2, 3, 4, …, K-3, K-2, K-1, K). After a perfect shuffle, the order of the cards will be (1, 3, …, K-3, K-1, 2, 4, …, K-2, K) or (2, 4, …, K-2, K, 1, 3, …, K-3, K-1).
Suppose K=2^N and the order of the cards is (1, 2, 3, …, K-2, K-1, K) in the beginning, is it possible that the A-th card is X and the B-th card is Y after several perfect shuffles?
Suppose K=2^N and the order of the cards is (1, 2, 3, …, K-2, K-1, K) in the beginning, is it possible that the A-th card is X and the B-th card is Y after several perfect shuffles?
Input
Input to this problem will begin with a line containing a single integer T indicating the number of datasets.
Each case contains five integer, N, A, X, B, Y. 1 <= N <= 1000, 1 <= A, B, X, Y <= 2^N.
Each case contains five integer, N, A, X, B, Y. 1 <= N <= 1000, 1 <= A, B, X, Y <= 2^N.
Output
For each input case, output “Yes” if it is possible that the A-th card is X and the B-th card is Y after several perfect shuffles, otherwise “No”.
Sample Input
3
1 1 1 2 2
2 1 2 4 3
2 1 1 4 2
Sample Output
Case 1:
Yes
Case 2:
Yes
Case 3:
No
Source
Recommend
liuyiding
思路:主要是二进制的运用。---by(随心所欲)
为了方便从0开始,首先看下右移一下,高位异或1的规律:(可以从右往左一列一列看)
000(0) -> 100(4) -> 110(6) -> 111(7) -> 011(3) -> 001(1) -> 000(0)
001(1) -> 000(0) -> 100(4) -> 110(6) -> 111(7) -> 011(3) -> 001(1)
010(2) -> 101(5) -> 010(2) -> 101(5) -> 010(2) -> 101(5) -> 010(2)
011(3) -> 001(1) -> 000(0) -> 100(4) -> 110(6) -> 111(7) -> 011(3)
100(4) -> 110(6) -> 111(7) -> 011(3) -> 001(1) -> 000(0) -> 100(4)
101(5) -> 010(2) -> 101(5) -> 010(2) -> 101(5) -> 010(2) -> 101(5)
110(6) -> 111(7) -> 011(3) -> 001(1) -> 000(0) -> 100(4) -> 110(6)
111(7) -> 011(3) -> 001(1) -> 000(0) -> 100(4) -> 110(6) -> 111(7)
每次洗牌的时候,奇数在后偶数在前时,只需右移一下;奇数在前偶数在后时,只需右移一下,高位亦或1.
而且每层的任意2个数异或的结果相同。
对于给定的n,a,b,x,y;只需判断a^b==x^y(此处是异或运算符)既可。
1 import java.math.*; 2 import java.util.*; 3 public class Main { 4 public static void main(String arg[]){ 5 BigInteger a,b,c,x,y; 6 Scanner cin=new Scanner(System.in); 7 int t=1,n,tt; 8 c=BigInteger.ONE; 9 tt=cin.nextInt(); 10 while(tt-->0){ 11 n=cin.nextInt(); 12 a=cin.nextBigInteger();a=a.subtract(c); 13 x=cin.nextBigInteger();x=x.subtract(c); 14 b=cin.nextBigInteger();b=b.subtract(c); 15 y=cin.nextBigInteger();y=y.subtract(c); 16 a=a.xor(b); 17 x=x.xor(y); 18 boolean flag=false; 19 for(int i=0;i<n;i++){ 20 if(a.equals(x)){ 21 flag=true; 22 break; 23 } 24 if(a.testBit(0)){ 25 a=a.shiftRight(1); 26 a=a.setBit(n-1); 27 }else a=a.shiftRight(1); 28 } 29 if(flag) System.out.println("Case "+t+": Yes"); 30 else System.out.println("Case "+t+": No"); 31 t++; 32 } 33 } 34 }
浙公网安备 33010602011771号