Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 226 Accepted Submission(s): 78
php
每次洗牌的時候,奇數在後偶數在前時,只需循環右移一下,以下:java
0(000) -->0(000) -->0(000) -->0(000)this
1(001) -->4(100) -->2(010) -->1(001)spa
2(010) -->1(001) -->4(100) -->2(010)code
3(011) -->5(101) -->6(110) -->3(011)blog
4(100) -->2(010) -->1(001) -->4(100)ip
5(101) -->6(110) -->3(011) -->5(101)ci
6(110) -->3(011) -->5(101) -->6(110)get
7(111) -->7(111) -->7(111) -->7(111)input
奇數在前偶數在後時,只需右移一下,高位亦或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); } } }