Problem:
Problem B:
Bitwise Kingdom
In the Bitwise Kingdom, located somewhere in the universe, there are exactly 2
N
citizens living and each
of them has a unique identification string that represents his or her class in the society. An identification
string is a binary string of length
N
which consists of characters ‘
0
’ or ‘
1
’. The order of classes is defined
among the citizens by the following criteria:
Citizens identified by a string containing a greater number of ones are ranked higher. For example,
       “011” indicates a higher class than “100”.
Among those who have identification strings with the same number of ones, citizens identified by
       a lexicographically greater identification string are ranked higher. For example, “110” indicates a
       higher class than “101”.
For example, if
N
= 3, there are 8 (= 2
3
) people in the country, and their identification strings are “000”,
“001”, “010”, “100”, “011”, “101”, “110”, and “111” (from the lowest class to the highest).
You are given two numbers
N
(1 ≤
N
≤ 60) and
M
(1 ≤
M
≤ 2
N
), and you want to resolve the
identification string of the person of the
M
-th lowest class among 2
N
citizens. Can you write a program
to solve this problem?
Input
The input consists of multiple datasets.
Each dataset consists of a line which contains two integers
N
and
M
in this order, separated with a single
space. The input does not contain any other extra characters such as leading or trailing spaces.
The end of input is indicated by a line with two zeros. This line is not part of any datasets.
Output
For each dataset, print the identification string of the person of the
M
-th lowest class in one line. Your
program may not omit any leading zeros in the answer.
Sample Input
3 3
3 5
0 0
Output for the Sample Input
010
011

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;


//TEMPLATE
#define pb              push_back
#define mp              make_pair
#define ll              long long
#define ld              long double
//#define pi              3.1415926536
#define pi              acos(-1)
//#define mod             1000000007
#define newline         printf("\n")
#define printminusone   printf("-1\n")
#define scl1(n)         scanf("%lld", &n)
#define scl2(a, b)      scanf("%lld %lld", &a, &b)
#define scl3(a, b, c)   scanf("%lld %lld %lld", &a, &b, &c)
#define printl(n)       printf("%lld\n", n)
#define ssl1(str)       scanf(" %[^\n]", str)
#define scd1(n)         scanf("%lf", &n)
#define scd2(a, b)      scanf("%lf %lf", &a, &b)
#define printd(n)       printf("%lf\n", n)
#define printld(n)      printf("%Lf\n", n)
#define printcase       printf("Case %lld: ", i)
#define reversed(s)     reverse(s.begin(), s.end())
#define asorted(s)      sort(s.begin(), s.end())
#define dsorted(s)      sort(s.begin(), s.end(), cmp)
#define for0(i, n)      for (i = 0; i < n; i++)
#define for1(i, n)      for (i = 1; i <= n; i++)
#define fori(itt, stt)  for(auto itt = stt.begin(); itt != stt.end(); itt++)
#define pii             pair <ll, ll>
#define piii            pair <ll, pii>
//#define SIZE            100005
//#define INF             1000000005
#define F               first
#define S               second
#define FR              ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ms(arr, x)      memset(arr, x, sizeof arr)
#define bitcount(n)     __builtin_popcountll(n)

const ll INF = LLONG_MAX;
const ll SIZE = 4e5+5;
const ll mod = 1e9+7;


ll dp[62][2][25][25];
vector <ll> num;
ll cnt, n;

ll com[62][62];
ll cum[62][62];
void cal_ncr() {
    ll i, j;

    com[0][0] = 1;
    for(i = 1; i < 62; i++) {
        for(j = 0; j <= i; j++) {
            if(j == 0) {
                com[i][j] = 1;
                cum[i][j] = 1;
            }
            else {
                com[i][j] = com[i-1][j-1] + com[i-1][j];
                cum[i][j] = com[i][j] + cum[i][j-1];
            }
        }
    }
}

ll pow2[63];
void cal_pow2() {
    ll i, x = 1, y;
    for0(i, 62) {
        y = x;
        pow2[i] = y;
        x *= 2;
    }
}

string dec_to_binar(ll val) {
    string bin;

    while(val) {
        bin += ((val % 2) + '0');
        val /= 2;
    }
    while(bin.size() < n) bin += '0';
    reversed(bin);

    return bin;
}

ll solve(ll pos, ll small, ll c, ll t) {
    if(c > cnt) return 0;
    if(pos == 61) return(c == cnt);
    if(dp[pos][small][c][t] != -1 && small) return dp[pos][small][c][t];

    ll i, res(0), val = num[pos];
    ll en = small ? 1 : val;


    for0(i, en+1) {
        res += solve(pos+1, small | i < num[pos], c+(i == 1), t);
    }

    return dp[pos][small][c][t] = res;
}

ll cal(ll r) {
    num.clear();

    while(r) {
        num.pb(r%2);
        r /= 2;
    }
    while(num.size() < 61) num.pb(0);
    reversed(num);

    ms(dp, -1);

    ll res = solve(0, 0, 0, cnt);
    return res;
}

int main() {
    FR;
    cal_ncr();
    cal_pow2();
//    ms(dp, -1);
    ll t = 0, m, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, c = 0;
    string s, s1, s2;

    while(cin >> n >> m) {
        if(!n && !m) break;

        if(m == 1) {
            for1(i, n) cout << 0;
            cout << "\n";

            continue;
        }

        ll st = 0, en = n;
        cnt = 0;

        while(st <= en) {
            ll mid = (st + en) / 2;

            if(cum[n][mid] >= m) {
                cnt = mid;
                en = mid - 1;
            }
            else st = mid + 1;
        }


        m -= cum[n][cnt-1];
        st = 1, en = pow2[n];

        while(st <= en) {
            ll mid = (st + en) / 2;

            t = cal(mid);

            if(t >= m) {
                ans = mid;
                en = mid - 1;
            }
            else st = mid + 1;
        }

        s = dec_to_binar(ans);
        cout << s << "\n";
    }

    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 3784
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define int long long
#define MOD (1e9+7)
#define yn(f) (f?"Yes":"No")

int N, M;

int com(int n, int m){
  if(m == 0) return 1;
  if(m == 1) return n;
  //m = min(n-m,m);
  return com(n,m-1)*(n-m+1)/m;
}

int huga(int a, int b){
  int ans = 0;
  for(int i = 0; i < b; i++){
    ans += com(a,i);
  }
  return ans;
}

int foo(string &a, int k, int m){//cout<<k<<" "<<m<<endl;
  for(int i = 0, sum = 0; i <= k; i++){// cout<<"i = "<<i<<" "<<com(k,i)<<" "<<sum<<endl;
    sum +=com(k,i);
    
    if(sum == m){
      for(int j = 0; j < i; j++){
	a += '1';
      }
      for(int j = i; j < k; j++){
	a += '0';
      }
      return 0;
    }
    else if(sum >  m){
      if(sum <  m+com(k-1,i-1)){//cout<<"111--"<<huga(k-1,i-1)<<" "<<com(k-1,i-1)<<endl;
	a += "1";
	//return foo(a, k-1,m-com(k,i-1)-com(k-1,i)+huga(k-1,i-1));
       	//return foo(a, k-1,m);
	//return foo(a,k-1,m-com(k-1,i-1)-(sum-com(k,i))+huga(k-1,i-1));
	return foo(a,k-1,m + huga(k-1,i-1)-(sum-com(k,i))-com(k-1,i));
      }else {
	//cout<<"000---"<<" "<<huga(k-1,i)<<" "<<m-(sum-com(k,i))<<endl;
	a += "0";
	//return foo(a,k-1,m-com(k-1,i-1)+huga(k-1,i-2));
	//return foo(a,k-1,huga(k-1,i)+m-com(k,i-1));
	return foo(a,k-1,m-(sum-com(k,i))+huga(k-1,i));
	
      }
    }
  }
  return 0;
}

signed main(){
  while(true){
    string a;
    cin>>N>>M;
    if(!N&&!M) break;
    //cout<<com(n,m)<<endl;
    
    //cout<<foo(a,N,M)<<endl;
    foo(a,N,M);
    cout<<a<<endl;
  }


  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3148
final_rank: 5

code_snippet: #include<bits/stdc++.h>
#define int long long
using namespace std;
int nCr[61][61],rr[61];

int cal(int n,int r){
  int res=1;
  vector<int> v;
  for(int i=0;i<r;i++)v.push_back(i+1);
  
  for(int i=n;i>n-r;i--){
    res*=i;
    for(int j=0;j<r;j++){
      int d=__gcd(res,v[j]);
      if(d!=1)v[j]/=d,res/=d;
    }
  }
  return res;
}

signed main(){
  for(int i=0;i<=60;i++)
    for(int j=0;j<=60;j++){
      nCr[i][j]=cal(i,j);
    }
  rr[0]=1;
  for(int i=1;i<=60;i++)rr[i]=rr[i-1]*2;
  int n,m;
  while(cin>>n>>m,n){
    m=rr[n]-m;
    int k;
    for(int i=n;i>=0;i--){
      k=i;
      if(m-nCr[n][i]<0)break;
      m-=nCr[n][i];
    }
    for(int i=n;i>0;i--){
      if(k==0||m>=nCr[i-1][k-1]){
	if(k)m-=nCr[i-1][k-1];
	cout<<0;
      }
      else k--,cout<<1;
    }
    cout<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 3144
final_rank: 6

code_snippet: #include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef unsigned long long ull;

ull nCr(ull n, ull r){
  if(r == 0) return 1;

  bool used[r+1];
  ull res = 1;
  fill(used,used+r+1,false);
  for(ull i=n,i2=0; i2<r; i--,i2++){
    res *= i;
    for(ull j=1; j<=r; j++){
      if(used[j])continue;
      if(res % j == 0){res /= j, used[j] = true;}
    }
  }
  return res;
}


ull d[64];

int countBit(ull n){
  int ret = 0;
  for(int i = 60 ; i > 0 ; i--){
    if(n >= d[i]){
      ret++;
      n -= d[i];
    }
  }
  return ret;
}

string toBit(ull n, int m){
  string ret = "";
  for(int i = m ; i > 0 ; i--){
    if(n >= d[i]){
      ret += '1';
      n -= d[i];
    }
    else ret += '0';
  }
  return ret;
}

ull getValue(ull a, ull N){
  ull value = 0;
  for(ull i=0; i<N; i++) if((1ull<<i) & a) value += (1ull<<i);
  return value;
}

int main(){
  ull N,M;
    
  for(int i = 0 ; i < 64 ; i++) d[i] = 1;
  for(int i = 2 ; i < 64 ; i++) d[i] = d[i-1]*2;
  
  while(cin >> N >> M,N|M){

    if(M == 1){ cout << toBit(0,N) << endl; continue; }


    vector<ull>v;
    for(int i=0; i<=N; i++){
      if(i == 0)
	v.push_back(nCr(N,i));
      else v.push_back(nCr(N,i)+v[i-1]);
    }

    int bit = 0;
    ull cnt = 0;
    for(int i=0; i<=N; i++){
      if(v[i] >= M){bit = i; cnt = v[i-1]; break;}
    }
    //    cout << "bit = " << bit << " cnt = " << cnt << endl;

    ull value = 0;
    for(ull i=0; i<bit; i++) value += (1ull<<i);

    //    cout << "value = " << value << endl;
    cnt++;

    int C = 0;
    while(true){
      //      C++;
      //      if(C == 20)break;
      for(ull i=N; i>=0; i--){
	if(cnt == M)goto end;
	if(!(value & (1ull<<i)))continue;
	//	cout << "i = " << i << endl;
	//	cout << "value = " << value << endl;
	ull n = i, c = countBit(getValue(value,i));
	//	cout << "value = " << value << endl;
	//	cout << "value2 = " << value-(1<<i) << endl;
	//	cout << "n = " << n << " c = " << c << endl;
	//	cout << "nCr = " << nCr(n,c) << endl;
	if(cnt+nCr(n,c) <= M){
	  cnt += nCr(n,c);
	  value -= (1ull<<i);
	  value += (1ull<<(i+1));
	  //	  cout << "enter" << endl;
	  break;

	}
      }
    }
    
  end:
    cout << toBit(value,N) << endl;
    //    cout << "C = " << C << endl;

  }
  return 0;
}
accuracy: 1
time_ms: 780
memory_kb: 1224
final_rank: 8

code_snippet: #include <algorithm>
#include <vector>
#include <iostream>
#include <string>
using namespace std;

typedef long long int lli;

int N,M;
lli comb(lli n,lli p){
	if(n<p)return 0;
	lli res=1;
	vector<bool> div(p,false);
	if(n-p<=p){
		p=n-p;
	}
	for(int i=n;i>n-p;i--){
		res*=i;
		for(int j=0;j<p;j++){
			if(res%(j+1)==0&&!div[j]){
				res/=(j+1);
				div[j]=true;
			}
		}

	}
	return res;
}
string solve(int n,int p,lli c){
	lli t=comb(n-1,p);
	if(n==0)return "";
	if(c>=t)return "1"+solve(n-1,p-1,c-t);
	return "0"+solve(n-1,p,c);
}
int main(){
	lli N,M;
	for(;(cin>>N>>M),N;){
		M-=1;
		for(int i=0;i<=N;i++){
			lli t=comb(N,i);
			if(t>M){
				cout<<solve(N,i,M)<<endl;
				break;
			}
			M-=t;
		}
	}
}
accuracy: 1
time_ms: 40
memory_kb: 916
final_rank: 3

code_snippet: #include <iostream>
using namespace std;
typedef long long ll;

ll memo[64][64];
ll ncr(ll n , ll r){
	if(memo[n][r])return memo[n][r];
	long long ans = 1;
	int d[64] = {0};
	for(int i = 1 ; i <= r ; i++) d[i] = 1;
	for(int i = 0 ; i < r  ; i++){
		ans *= n-i;
		for(int j = 1 ; j <= r ; j++)
			if(d[j] && ans % j == 0){
				ans /= j;
				d[j] = 0;
			}
	}
	return memo[n][r] = ans;
}

string f(ll n,ll bit,ll m){
	if(m == 0 || bit == 0) return string(n,'0');
	ll cur = 0;
	for(int i = bit ; i <= n ; i++){
		ll next = cur+ncr(i-1,bit-1);
		if(m <= next && m >= cur+1) return f(i-1,bit-1,m-cur) + "1" + string(n-i,'0');
		cur = next;
	}
	return f(n,bit+1,m-cur);
}
int main(){
	ll n,m;
	while(cin >> n >> m && (n||m) ){
		string r = f(n,1,m-1);
		for(int i = 0 ; i < r.size() ; i++) cout << r[r.size()-1-i];
		cout << endl;
	}
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
using namespace std;
typedef long long ll;

ll memo[64][64];
ll ncr(ll n , ll r){
	if(memo[n][r])return memo[n][r];
	long long ans = 1;
	int d[64] = {0};
	for(int i = 1 ; i <= r ; i++) d[i] = 1;
	for(int i = 0 ; i < r  ; i++){
		ans *= n-i;
		for(int j = 1 ; j <= r ; j++)
			if(d[j] && ans % j == 0){
				ans /= j;
				d[j] = 0;
			}
	}
	return memo[n][r] = ans;
}

string f(ll n,ll bit,ll m){
	if(bit == 0) return string(n,'0');
	ll cur = 0;
	for(int i = bit ; i <= n ; i++){
		ll next = cur+ncr(i-1,bit-1);
		if(m <= next && m >= cur+1) return f(i-1,bit-1,m-cur) + "1" + string(n-i,'0');
		cur = next;
	}
}
int main(){
	ll n,m;
	while(cin >> n >> m && (n||m) ){
		int bit = n;
		ll cur = 0;
		for(int i = 0 ; i <= n ; i++){
			ll next = cur + ncr(n,i);
			if(m <= next && m >= cur+1){ bit = i; break;}
			cur = next;
		}
		m -= cur;
		if(bit == 0) cout << string(n,'0') << endl;
		else{
			string r = f(n,bit,m);
			for(int i = 0 ; i < r.size() ; i++) cout << r[r.size()-1-i];
			cout << endl;
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
using namespace std;
typedef long long ll;

ll ncr(ll n , ll r){
	long long ans = 1;
	int d[64] = {0};
	for(int i = 1 ; i <= r ; i++) d[i] = 1;
	for(int i = 0 ; i < r  ; i++){
		ans *= n-i;
		for(int j = 1 ; j <= r ; j++)
			if(d[j] && ans % j == 0){
				ans /= j;
				d[j] = 0;
			}
	}
	return ans;
}

string f(ll n,ll bit,ll m){
	if(bit == 0) return string(n,'0');
	ll cur = 0;
	for(int i = bit ; i <= n ; i++){
		ll next = cur+ncr(i-1,bit-1);
		if(m <= next && m >= cur+1) return f(i-1,bit-1,m-cur) + "1" + string(n-i,'0');
		cur = next;
	}
}
int main(){
	ll n,m;
	while(cin >> n >> m && (n||m) ){
		int bit = n;
		ll cur = 0;
		for(int i = 0 ; i <= n ; i++){
			ll next = cur + ncr(n,i);
			if(m <= next && m >= cur+1){ bit = i; break;}
			cur = next;
		}
		m -= cur;
		if(bit == 0) cout << string(n,'0') << endl;
		else{
			string r = f(n,bit,m);
			for(int i = 0 ; i < r.size() ; i++) cout << r[r.size()-1-i];
			cout << endl;
		}
	}
}
accuracy: 1
time_ms: 140
memory_kb: 924
final_rank: 4