Gray Code]

Source: Internet
Author: User

Gray Code (transferred from M67 Daniel)

Gray Code
If I have four potential GF, I need to decide who will eventually be. A simple method is to interact with each mm for a period of time, and finally select the mm with the highest "Satisfaction. But after reading the theory of DD, things began to become complicated: I can choose to be with multiple mm. In this way, the status to be assessed is changed to 2 ^ 4 = 16 (of course, including 0000, because I may be glass ). Now the question is, in what order should I traverse these 16 states?
The traditional approach is to traverse all possible combinations in the order of binary numbers. That is to say, I need to test each status in the order of 0000-> 0001-> 0010-> 0011->...-> 0100. This order is not scientific, and state transfer is time-consuming in many cases. For example, from 0111 to 1000, I need to temporarily get rid of all the three mm, and then put the 4th mm. At the same time, it is a huge project to change the relationship between all mm and me. Therefore, I want to know whether there is a way to change the relationship between me and a mm (catch up or drop off) from the status of no mm ), after 15 operations, the system just traverses all possible combinations (the final state is not necessarily 1111 ). You can try it first.
The solution to this problem is clever. Let's explain how to obtain the traversal order of N = 3 if we already know the valid traversal order of N = 2. Obviously, the traversal order of N = 2 is as follows:

00
01
11
10

You may have figured out how to extend the traversal order above to n = 3. When n = 3, there are eight statuses. The first four of them copy the traversal order of N = 2, then they are folded and added with 1 at the beginning as the last four states:

000
001
011
010 forbidden
--------
110 bytes
111
101
100

The traversal sequence obtained using this method obviously meets the requirements. First, the above eight States are exactly all eight combinations of N = 3, because they are based on all four combinations of N = 2, we can choose not to select 3rd elements. Then we can see that the status of the latter half should be the same as that of the former half to meet the limit of "only one different adjacent State", while the "mirror" is the first digit at the beginning. Fold the three-level traversal order again, and we get the answer to the question:

0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000

This traversal order exists as a encoding method called the Gray Code (write a Chinese character to let the spider capture the Gray code ). It has a wide range of applications. For example, the gray code of order n is equivalent to the Hamilton loop on the n-dimensional cube. Because one step is taken along the side of the cube, only one value changes in the n-dimensional coordinate. For example, the gray code is equivalent to the Hanoi Tower problem. The gray code changes the number. The HANOI tower should move the plate. For example, the position of the gray code of Level 3 is 1-2-1-3-1-2-1, this is exactly the serial number of the Third-Order HANOI tower every time you move the plate. If we can quickly find the number N of the gray code, we can output the moving steps of the Hanoi Tower after any number of steps. Now I will tell you that the nth Number of the gray code (from 0) is n
XOR (n shr 1), can you figure out why? Think about it first.

Next we will write the binary number and the gray code below. We can see that the number on the left is different or the result of its right shift is equal to the number on the right.

Binary Gray Code
000 000
001
010 011
011 010
100 110
101 111
110 101
111 100

From the perspective of the binary number, the number at the "image" position is the result of the not operation on the original number. For example, the numbers 10, 10, and 3rd are the opposite. If the two numbers are X and Y, the results of x xor (x SHR 1) and y xor (y shr 1) are only a little different: the first of the latter is 1, the first part of the former is 0. This is exactly the method for generating the Gray code. This shows that the nth Number of the gray code is indeed n XOR (N SHR
1 ).

Mashuo showed me this question in April this year, which is a two-dimensional gray code. Write the numbers 0 to 2 ^ (n + M)-1 as 2 ^ n.
* 2 ^ m matrix, so that the binary representation of the adjacent two numbers has only one difference. The answer is actually very simple. All numbers are made up of M-bit gray codes and N-bit gray codes, which need to be completed by the Left shift operation and the or operation. The complete code is as follows:
VaR
X, Y, M, N, u: longint;
Begin
Readln (m, n );
For X: = 0 to 1 M-1 do begin
U: = (x XOR (x SHR 1) shl n; // the left side of the output number is an M-bit gray code.
For Y: = 0 to 1 SHL n-1 do
Write (U or (y xor (y shr 1), ''); // and the last n-bit gray code
Writeln;
End;
End.

PS:

Common binary codes andGray CodeYou can convert each other as follows:

Binary Code->Gray Code(Encoding): Starting from the rightmost one, each bit is different from the one on the left or (XOR ).Gray CodeThe value of this digit. The leftmost digit remains unchanged (equivalent to 0 on the left );

Gray Code-> Binary code (Decoding): Each decoded value is the same or different from the decoded value of the second digit on the left, the decoded value (the leftmost one remains unchanged ).

Is zoj 2531 a typical Gray Code http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 1533

Int togray (int x) {// convert to Gray Code return x ^ (x> 1);} int tobinary (int x) {// Gray Code converted to binary int y = x; while (x >>=1) {y ^ = x;} return y ;}int main () {int N, m; while (scanf ("% d", & N, & M) & (n + M) {int I, J; int beg = tobinary (m); for (I = beg; I <n; I ++) {printf ("% d", togray (I ));} for (I = 0; I <beg; I ++) {printf ("% d", togray (I) ;}puts ("") ;}return 0 ;}

Poj 1832 http://poj.org/problem? A large integer is required for id = 1832.

import java.io.*;import java.math.*;import java.util.*;public class Main{static BigInteger[] base = new BigInteger[130];static void  init(){int i,j;base[0] = BigInteger.ONE;for(i=1;i<128;i++){base[i] = base[i-1].multiply(BigInteger.valueOf(2));}}public static void main(String args[]){init();int t;Scanner cin = new Scanner(System.in);t = cin.nextInt();int[] a = new int[130];int[] b = new int[130];while((t--)!=0){int i,j;int n = cin.nextInt();a[0] = cin.nextInt();for(i=1;i<n;i++){a[i] = cin.nextInt();a[i] = a[i]^a[i-1];}b[0] = cin.nextInt();for(i=1;i<n;i++){b[i] = cin.nextInt();b[i] = b[i]^b[i-1];}BigInteger aa = BigInteger.valueOf(0);BigInteger bb = BigInteger.valueOf(0);for(i=0;i<n;i++){aa = aa.add(base[i].multiply(BigInteger.valueOf(a[n-i-1])));bb = bb.add(base[i].multiply(BigInteger.valueOf(b[n-i-1])));}BigInteger ans = bb.subtract(aa).abs();System.out.println(ans);}}}

  











Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.