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. Java 线程池的原理与实现

    最近在学习线程池.内存控制等关于提高程序运行性能方面的编程技术,在网上看到有一哥们写得不错,故和大家一起分享. 建议:在阅读本文前,先理一理同步的知识,特别是syncronized同步关键字的用法.关 ...

  2. CodeIgniter 定义“全局变量-global variable”,可以在所有controller,model和view中使用

    本文抄自http://www.cnblogs.com/webu/archive/2012/11/20/2779999.html 第一次正儿八经用CodeIgniter框架做项目,结果不会定义全局变量, ...

  3. CSS3中的Transition属性详解(贝赛尔曲线)

    transition语法: transition : [<'transition-property'> || <'transition-duration'> || <'t ...

  4. [f]JQ cookie插件

    jQuery.cookie = function(name, value, options) { if (typeof value != 'undefined') { options = option ...

  5. delphi 获取颜色值的RGB

    前言:http://www.cnblogs.com/studypanp/p/5002953.html 获取的颜色值 前面获取到一个像素点的颜色值后(十六进制),比如说(黄色):FFD1C04C(共八位 ...

  6. PAM禁止root用户登录,限制普通用户使用su切换到root

    系统环境是CentOS 6.5, 介绍下PAM(Pluggable Authentication Modules)在ssh服务上的简单配置过程. 1.必须先添加普通用户,并属于wheel组:保证有除r ...

  7. SQLSERVER 2012之AlwaysOn -- 同步模式下的网卡性能优化

    本文是基于上一篇<SQLServer 2012之AlwaysOn -- 指定数据同步链路,消除网络抖动导致的提交延迟问题>的问题继续进行优化:具体背景请参照上文:     前后折腾了一个多 ...

  8. leveldb.net对象读写封装

    leveldb是一个非常高效的可嵌入式K-V数据库,在.NET下有着基于win实现的包装leveldb.net;不过leveldb.net只提供了基于byte[]和string的处理,这显然会对使用的 ...

  9. Python爬虫 -- 抓取电影天堂8分以上电影

    看了几天的python语法,还是应该写个东西练练手.刚好假期里面看电影,找不到很好的影片,于是有个想法,何不搞个爬虫把电影天堂里面8分以上的电影爬出来.做完花了两三个小时,撸了这么一个程序.反正蛮简单 ...

  10. JavaScript自定义事件

    很多DOM对象都有原生的事件支持,向div就有click.mouseover等事件,事件机制可以为类的设计带来很大的灵活性,相信.net程序员深有体会.随着web技术发展,使用JavaScript自定 ...