eolymp
bolt
Try our new interface for solving problems
Məsələlər

Gray Code

Gray Code

A Gray code is a binary numbering system where two successive values differ only in one bit position. Each value in an \textbf{N}-bit Gray code has \textbf{N} bits. The most obvious case is for \textbf{N = 1}, when the Gray code values are just \textbf{0} and \textbf{1}. If we have a list of the \textbf{N}-bit Gray code values, then the \textbf{N+1}-bit Gray code can be obtained relatively simply. List the existing values in a column, draw a line under the last value, and "reflect" the existing values below the line. That is, duplicate the list in the opposite order. Then add a \textbf{0} bit in front of the original values and a \textbf{1} bit in front of the new "reflected" values. (Incidentally, this is why the Gray code is also called the "reflected binary code.") For example, here is an illustration showing how we might generate the Gray code for \textbf{N = 2}: \includegraphics{https://static.e-olymp.com/content/e7/e71a245aa70a214e0054bbbd15b01a2c816a6189.jpg} So the list of Gray codes for \textbf{N = 2} is \textbf{00}, \textbf{01}, \textbf{11}, and \textbf{10}. Here is how we might generate the Gray code for \textbf{N = 3}: \includegraphics{https://static.e-olymp.com/content/2a/2a82306f7bc85e05c8bd4d2760df1a23f2eb5df4.jpg} Your task in this problem is to compute and display only the \textbf{k}-th value in the \textbf{N}-bit Gray code, suitably labeled. Assume the values are numbered starting with \textbf{0}. So if \textbf{N = 3} and \textbf{k = 4} your program will display \textbf{110}. The Java BigInteger class may \textbf{not} be used in a solution to this problem. \InputFile There will be multiple input cases. For each case, the input will consist of a single line containing integer values for \textbf{N }and \textbf{k} separated by one or more blanks and followed by an end of line character. \textbf{N} will be between \textbf{1} and \textbf{72}, and \textbf{k} will be between \textbf{0} and \textbf{2^N−1}. The line following the input line for the last case will contain two zeroes. If it isn't immediately obvious from the preceding text, note that the value of \textbf{k} may not fit in a \textbf{32} bit or even a \textbf{64}bit integer. It should also be noted that you will not want to compute all \textbf{2^N} gray code values, but focus your attention on the computation of \textbf{only} the \textbf{k}-th such value. \OutputFile For each input case display the case number (\textbf{1}, \textbf{2}, …), the values of \textbf{N} and \textbf{k}, and the \textbf{k}-th Gray code value (labeled as "\textbf{g}'). Follow the format shown in the samples below.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
3 4
4 13
10 1023
12 1000
0 0
Çıxış verilənləri #1
Case 1:
  N = 3
  k = 4
  g = 110

Case 2:
  N = 4
  k = 13
  g = 1011

Case 3:
  N = 10
  k = 1023
  g = 1000000000

Case 4:
  N = 12
  k = 1000
  g = 001000011100

Mənbə ACM North Central North America Regional Programming Contest, November 12, 2011