Artifact Content
Not logged in

Artifact 299e5c03487b074ccbd1c4fc5b2b60faf84e7c21


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class TheLockDivOne { public:
	string password(int n, long long k) 
	{
		--k;
		return lexlast(n-1, k);
	}

	string kth(int i, LL k)
	{
		if( i < 0 )
			return "";

		if( k & (1LL<<i) )
			return "1" + kth(i-1, k==(1LL<<i) ? (1LL<<i)-1 : k-(1LL<<i)-1);
		else
			return "0" + kth(i-1, k);
	}

	string lexlast(int i, LL k)
	{
		if( i < 0 )
			return "";

		if( k & (1LL<<i) ) {
			string a = kth(i-1, (1LL<<i)-1);
			if( k == (1LL<<i) )
				return "1" + a;
			string b = lexlast(i-1, k-(1LL<<i)-1);
			return "1" + max(a,b);
		}
		else
			return "0" + lexlast(i-1, k);
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const string& Expected, const string& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, TheLockDivOne().password(n, k));}
int main(){

CASE(0)
	int n = 2; 
	long long k = 4LL; 
	string _ = "11"; 
END
CASE(1)
	int n = 3; 
	long long k = 8LL; 
	string _ = "111"; 
END
CASE(2)
	int n = 4; 
	long long k = 6LL; 
	string _ = "0110"; 
END
CASE(3)
	int n = 10; 
	long long k = 1LL; 
	string _ = "0000000000"; 
END
CASE(4)
	int n = 7; 
	long long k = 100LL; 
	string _ = "1111110"; 
END
CASE(4)
	int n = 50; 
	long long k = 0xffffffffffffLL; 
	string _ = "1111110"; 
END

}
// END CUT HERE