TopCoder problem "TheLockDivTwo" used in SRM 445 (Division II Level Three)



Problem Statement

    

John is obsessed with security. Recently he bought a new electronic lock. It is protected by a password containing n digits, where each digit is either zero or one. John decides to change the password every day. On the first day, the password is all zeroes. On each day that follows, he will select one or more digits that all have the same value and change their values (so zeroes become ones, and ones become zeroes). He must select the digits according to the following rules:

  1. During the first 2^n days, he must never use the same password twice.
  2. Each new password must come as early as possible alphabetically while not violating rule 1.

For example, if n is 2, the password on the first day is "00". The next day, he can change one or both 0's to get "01", "10" or "11". Of these possibilities, "01" comes earliest alphabetically. The next day, he can change either the 0 or the 1 to get "11" or "00". He can't choose "00" because it was already used, so he chooses "11". The next day, he can change one or both 1's to get "10", "01" or "00". He has already used "01" and "00", so he must choose "10".

Given ints n and k, return the password that John will choose on the k-th day (where k is a 1-based index).

 

Definition

    
Class:TheLockDivTwo
Method:password
Parameters:int, int
Returns:String
Method signature:String password(int n, int k)
(be sure your method is public)
    
 

Notes

-If A and B are two Strings of the same length, then A comes earlier alphabetically than B if it contains a smaller character at the first position where the Strings differ.
 

Constraints

-n will be between 1 and 10, inclusive.
-k will be between 1 and 2^n, inclusive.
 

Examples

0)
    
2
4
Returns: "10"
This is the example from the statement. The password sequence is the following - "00", "01", "11", "10".
1)
    
4
6
Returns: "0100"
"0000", "0001", "0011", "0010", "0110", "0100", ...
2)
    
10
1
Returns: "0000000000"
The password always consists of all zeroes on the first day.
3)
    
7
100
Returns: "1100001"

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=10513

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13899&pm=10513

Writer:

Vasyl[alphacom]

Testers:

PabloGilberto , Olexiy , ivan_metelsky

Problem categories:

Math, Simulation