您的位置:首页 > 其它

hdu 4759 Poker Shuffle

2013-11-03 19:54 323 查看
读通题意应该要往二进制的方向去想,因为牌的个数是(1<<n)个的,

尝试发现每一次操作都是对数字的循环右移一位,如果是把奇数放到前面则还要异或(1<<n-1);

发现这个规律后,我们可以选循环右移再异或(而且可以异或上任何值,我们可以假设我们已经事先循环右移了n次);

枚举循环右移的次数,然后异或上一个值使它变成我们想要的值,因为 a ^ c = b - > a ^ b = c

这样我们这样比较所得到的c是否相同就可以了;

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
public static void main(String[] arg) {
Solve t = new Solve();
t.main();
}
}
class Solve{
int n;
BigInteger right(BigInteger x) {
BigInteger tp = x.and(BigInteger.valueOf(1));
return x.shiftRight(1).or(tp.shiftLeft(n-1));
}
public void main(){
Scanner cin = new Scanner(System.in);
int T = cin.nextInt();
int cas = 0;
while (T-- != 0) {
n = cin.nextInt();
BigInteger a = cin.nextBigInteger();
BigInteger x = cin.nextBigInteger();
BigInteger b = cin.nextBigInteger();
BigInteger y = cin.nextBigInteger();
BigInteger tp = BigInteger.valueOf(-1);
a = a.add(tp);
x = x.add(tp);
b = b.add(tp);
y = y.add(tp);
int fg = 0;
for (int i = 0; i < n; i++) {
x = right(x);
y = right(y);
BigInteger ta = a.xor(x);
BigInteger tb = b.xor(y);
if (ta.compareTo(tb) == 0) {
fg = 1;
break;
}
}
cas++;
System.out.print("Case "+cas+": ");
if (fg == 1) System.out.println("Yes");
else System.out.println("No");
}

}

}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: