Poker Shuffle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 226    Accepted Submission(s): 78

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?
 
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.
 
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
 
举个例子,当n=3,每张牌的编号从0开始时:

每次洗牌的时候,奇数在后偶数在前时,只需循环右移一下,如下:

0(000) -->0(000) -->0(000) -->0(000)

1(001) -->4(100) -->2(010) -->1(001)

2(010) -->1(001) -->4(100) -->2(010)

3(011) -->5(101) -->6(110) -->3(011)

4(100) -->2(010) -->1(001) -->4(100)

5(101) -->6(110) -->3(011) -->5(101)

6(110) -->3(011) -->5(101) -->6(110)

7(111) -->7(111) -->7(111) -->7(111)

奇数在前偶数在后时,只需右移一下,高位亦或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)

从上面两个表可以看出,每层的任意2个数异或的结果相同,且都为n。

eg:

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)

每行的红色数字异或,粉红色数字异或,蓝色数字异或------>都为7,所以,你懂得

import java.math.*;
import java.util.*; public class Main{
static int n;
static Scanner cin = new Scanner(System.in); static BigInteger rot(BigInteger x){
BigInteger tmp = x.and(BigInteger.valueOf(1));
return x.shiftRight(1).or(tmp.shiftLeft(n-1));
} public static void main(String[] args){
int t = cin.nextInt(), cases=0;
while(++cases <= t){
n = cin.nextInt();
BigInteger A = cin.nextBigInteger(), X = cin.nextBigInteger();
BigInteger B = cin.nextBigInteger(), Y = cin.nextBigInteger();
A = A.add(BigInteger.valueOf(-1));
B = B.add(BigInteger.valueOf(-1));
X = X.add(BigInteger.valueOf(-1));
Y = Y.add(BigInteger.valueOf(-1));
String ans = "No";
for(int i=0; i <= n; i++){
A = rot(A);
B = rot(B);
BigInteger tmpx = A.xor(X);
BigInteger tmpy = B.xor(Y);
if(tmpx.compareTo(tmpy)==0){
ans = "Yes";
break;
}
}
System.out.println("Case " + cases + ": " + ans);
}
}
}

HDU 4759 Poker Shuffle的更多相关文章

  1. HDU 4759 Poker Shuffle(2013长春网络赛1001题)

    Poker Shuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. 2013长春网赛1001 hdu 4759 Poker Shuffle

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4759 题意:有一堆2^n的牌,牌原先按(1,2,....k)排序,每一次洗牌都将牌分成两种情况:(1, ...

  3. hdu 4759 Poker Shuffle 二进制

    思路:主要是二进制的运用. 为了方便从0开始,首先看下右移一下,高位异或1的规律:(可以从右往左一列一列看) 000(0) -> 100(4) -> 110(6) -> 111(7) ...

  4. hdu 4759 大数+找规律 ***

    题目意思很简单. 就是洗牌,抽出奇数和偶数,要么奇数放前面,要么偶数放前面. 总共2^N张牌. 需要问的是,给了A X B Y  问经过若干洗牌后,第A个位置是X,第B个位置是Y 是不是可能的. Ja ...

  5. hdu4759 Poker Shuffle 2013 ACM/ICPC Asia Regional Changchun Online

    找了很久的规律,只看十进制数字,各种乱七八糟的规律=没规律!看了别人的解题报告,虽然看懂了,可是怎么发现的这个规律呢T.T~想了很久很久~ 以下是转载的别人的图,自己再画太麻烦了~全部看出0~2n-1 ...

  6. 收集了50道基础的java面试题

    下面的内容是对网上原有的Java面试题集及答案进行了全面修订之后给出的负责任的题目和答案,原来的题目中有很多重复题目和无价值的题目,还有不少的参考答案也是错误的,修改后的Java面试题集参照了JDK最 ...

  7. 自动发牌(C#版)

    利用数组实现发牌过程 一副牌去掉大小王,还剩52张.一共东.南.西.北四家,每家随机发13张牌. 提示: 东.南.西.北四家用一维数组表示 每家的牌采用一维数组表示(13张)  花色:enum Sui ...

  8. 转:Java面试题集(1-50)

    Java程序员面试题集(1-50) http://blog.csdn.net/jackfrued/article/details/17403101 一.Java基础部分 1.面向对象的特征有哪些方面? ...

  9. Java程序员面试题集(1-50)(转)

    转:http://blog.csdn.net/jackfrued/article/details/17339393 下面的内容是对网上原有的Java面试题集及答案进行了全面修订之后给出的负责任的题目和 ...

随机推荐

  1. 解决cocos2dx在Xcode中运行时报:convert: iCCP: known incorrect sRGB profile 的问题

    解决cocos2dx在Xcode中运行时报:convert: iCCP: known incorrect sRGB profile 的问题 本文的实践来源是参照了两个帖子完成的: http://dis ...

  2. Eclipse4.6(Neon) + Tomcat8 + MAVEN3.3.9 + SVN项目完整环境搭建

    软件清单 jdk-8u102-windows-x64.exe eclipse-inst-win64.exe (Eclipse4.6 Neon) apache-tomcat-8.5.5-windows- ...

  3. linux下使用SSL代理(SSLedge)

    refer to: https://eurekavpt.com/page/ssledge-on-linux 启动非常简单./ssledge-term-x64 -f config -D 其中的confi ...

  4. Asp.net中HttpRequest.Params与Reques.Item之异同

    今天才注意到HttpRequest.Params与HttpRequest.Item这两个玩意竟然有微妙的不同.上午的时候同事被坑了发现这玩意的说明还真微妙. 场景再现: 前台提交一个POST请求到后台 ...

  5. Yii中设置时间分区

    在wamp环境下,运行一个Php yii的项目 出现问题: Use of undefined constant PRC - assumed 'PRC' 检测我的环境 PHP5.3 检测Php.ini中 ...

  6. JS学习笔记12_优化

    一.可维护性优化 1.添加注释 注释能够增强代码的可读性以及可维护性,当然,理想情况是满满的注释,但这不太现实.所以我们只需要在一些关键的地方添上注释: 函数和方法:尤其是返回值,因为直接看不出来 大 ...

  7. 设计模式之美:Facade(外观)

    索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):用抽象类定义 Facade 而使子类对应于不同的子系统. 意图 为子系统中的一组接口提供一个一致的界面,Facade 模式定义了 ...

  8. [ACM_模拟] The Willy Memorial Program (poj 1073 ,联通水管注水模拟)

    Description Willy the spider used to live in the chemistry laboratory of Dr. Petro. He used to wande ...

  9. 通用对象池ObjectPool的一种简易设计和实现方案

    对象池,最简单直接的作用当然是通过池来减少创建和销毁对象次数,实现对象的缓存和复用.我们熟知的线程池.数据库连接池.TCP连接池等等都是非常典型的对象池. 一个基本的简易对象池的主要功能实现我认为应该 ...

  10. Android Error Message

    JAVA_HOME error. Add environment variable ANDROID_STUDIO, which is the same as %JAVA_HOME%, but one ...