1419: We Love 01
Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lldSubmitted: 30 Accepted: 7
[ Submit][ Status][ Web Board]
Description
You have N ‘1’ and M ‘0’. You can use all of them to construct a binary number. For example you have 2 ‘1’ and 2 ‘0’. You can get 6 numbers “0110” “1100” “1010” “0011” “0101” “1001”.
Now, the problem is what’s the K-th smallest number?
Input
First line contains a number T, represent the number of test case.
Each case contains 3 numbers N M and K.
Output
For each case, output the K-th smallest number.
Sample Input
32 2 33 2 41 2 4
Sample Output
0110
01110
Impossible
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define eps 1e-8
//typedef __int64 ll;
#define fre(i,a,b) for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define ssf(n) scanf("%s", n)
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define bug pf("Hi\n")
using namespace std;
#define INF 0x3f3f3f3f
#define N 55
int c[N][N];
void inint()
{
int i,j;
fre(i,0,N)
fre(j,0,i+1)
if(j==0||i==j) c[i][j]=1;
else c[i][j]=c[i-1][j-1]+c[i-1][j];
}
void dfs(int n,int m,int k)
{
int i,j;
if(n==0)
{
fre(i,0,m) pf("1");
return ;
}
if(m==0)
{
fre(i,0,n) pf("0");
return ;
}
int temp=c[n+m-1][m];
if(temp>=k)
{
pf("0");
dfs(n-1,m,k);
}
else
{
pf("1");
dfs(n,m-1,k-temp);
}
}
int main()
{
int i,j,n,m,k,t;
inint();
sf(t);
while(t--)
{
sfff(m,n,k);
if(c[n+m][m]<k)
{
pf("Impossible\n");
continue;
}
dfs(n,m,k);
pf("\n");
}
pf("\n");
}
/*
3
2 2 3
3 2 4
1 2 4
*/