/*********** Problem 1--Gray Codes ************************************
 This problem was pretty straightforward:  the algorithm was given to
 you in the problem description.
 **********************************************************************/

#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main (int argc, char **argv);
unsigned int getnormalposition (int k, int A);
int nthbit (unsigned int i, int n);
void printgrayconverted (unsigned int i, int k, int cs);

ifstream in ("prob1.in");
ofstream out ("prob1.out");

/****** main **********************************************************
 This program repeatedly reads in the number of bits in a Gray code
 and the angle to which a drum is rotated, and prints out the Gray code
 for that region of the drum.
 **********************************************************************/

int main (int argc, char **argv) {

  int k, A,    // the k and A values
      cs=0;    // the case number

 while (1) {
  in >> k >> A;   // read in k and A until both are zero
  if (k==0 && A==0) break;
                              // print the pattern
  printgrayconverted (getnormalposition(k,A),k,++cs);
 }
 return EXIT_SUCCESS;
}

/******* getnormalposition ********************************************
 getnormalposition accepts k and A and returns the number corresponding
 to its position.  There are 2^k positions, and the angle is measured
 in degrees.
 **********************************************************************/

unsigned int getnormalposition (int k, int A) {

 return (unsigned int) (A*pow(2,k)/360);
}

/******* nthbit *******************************************************
 nthbit accepts an unsigned integer and a bit position, and returns
 the value of that bit in that integer.  It uses C's built-in
 right-shift command.
 **********************************************************************/

int nthbit (unsigned int i, int n) {

 return (i>>n) %2;
}

/********** printgrayconverted ****************************************
 printgrayconverted takes an unsigned integer, the number of bit
 positions and the case number, and prints out the gray code for that
 integer using the algorithm described in the problem.
 **********************************************************************/

void printgrayconverted (unsigned int i, int k, int cs) {

 out << cs << ": " << nthbit(i,k-1) << " ";
                                // printing the leftmost bit
 for (int j=k-2;j>=0;j--)
                     // totalling adjacent bits to print the next
  out << (nthbit(i,j+1)+nthbit(i,j))%2 << " "; // Gray code
 out << endl;
}
