M個1和N個0的排列
阿新 • • 發佈:2019-02-04
Time Limit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 256MB
Description
Consider a string set that each of them consists of {0, 1} only. All strings in the set have the same number of 0s and 1s. Write a program to find and output the K-th string according to the dictionary order. If such a string doesn’t exist, or the input is not valid, please output “Impossible”. For example, if we have two ‘0’s and two ‘1’s, we will have a set with 6 different strings, {strcmp(stc,dest);{1100,1010,1001,0110,0101,0011}and the 4th string is 1001.
The first line of the input file contains a single integer t (1 ≤ t ≤ 10000), the number of test cases, followed by the input data for each test case.Each test case is 3 integers separated by blank space: N, M(2 <= N + M <= 33 and N , M >= 0), K(1 <= K <= 1000000000). N stands for the number of ‘0’s, M stands for the number of ‘1’s, and K stands for the K-th of string in the set that needs to be printed as output.
Output
For each case, print exactly one line. If the string exists, please print it, otherwise print “Impossible”.
Sample In
3
2 2 2
2 2 7
4 7 47
Sample Out
0101
Impossible
Case Time Limit: 1000ms
Memory Limit: 256MB
Description
Consider a string set that each of them consists of {0, 1} only. All strings in the set have the same number of 0s and 1s. Write a program to find and output the K-th string according to the dictionary order. If such a string doesn’t exist, or the input is not valid, please output “Impossible”. For example, if we have two ‘0’s and two ‘1’s, we will have a set with 6 different strings, {strcmp(stc,dest);{1100,1010,1001,0110,0101,0011}and the 4th string is 1001.
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10000), the number of test cases, followed by the input data for each test case.Each test case is 3 integers separated by blank space: N, M(2 <= N + M <= 33 and N , M >= 0), K(1 <= K <= 1000000000). N stands for the number of ‘0’s, M stands for the number of ‘1’s, and K stands for the K-th of string in the set that needs to be printed as output.
Output
For each case, print exactly one line. If the string exists, please print it, otherwise print “Impossible”.
Sample In
3
2 2 2
2 2 7
4 7 47
Sample Out
0101
Impossible
01010111011
#include<iostream> #include<string> #include<vector> using std::cout; using std::cin; using std::endl; using std::string; using std::vector; int main() { void getInput(int &N,int &M,int &K); bool isPossible(int N,int M,int K); void getString(int N,int M,int K,string&); int f(int,int); int _case=0; while(cin>>_case) { if(1<<_case && _case<=10000) break; } vector<string> _vec; int n,m,k; for(int i=0;i<_case;++i) { getInput(n,m,k); if(!isPossible(n,m,k)) _vec.push_back("impossible"); else { string str; getString(n,m,k,str); _vec.push_back(str); } } for(vector<string>::iterator it=_vec.begin();it!=_vec.end();++it) cout<<(*it)<<endl; } void getInput(int &N,int &M,int &K) { while(true) { cin>>N; cin>>M; cin>>K; if(2<=(N+M) && (N+M)<=33 && N>=0 && M>=0 && (1<=K && K<=1000000000)) break; } } bool isPossible(int N,int M,int K) { int f(int,int); return f(N,M)>=K; } int f(int m,int n) { if(m==0 || n==0) return 1; return f(m-1, n) * (n+m)/m; } void getString(int N,int M,int K,string& str) { if(N==0) { for(int i=0;i<M;++i) str.push_back('1'); return; } if(M==0) { for(int i=0;i<N;++i) str.push_back('0'); return; } int i;//左端0的個數 for(i=N;i>0;--i) { if(f(N-i,M)>=K) break; } for(int j=0;j<i;++j) str.push_back('0'); if(i<=0)//左端沒有0 { str.push_back('1'); K-=f(N-1,M); getString(N,M-1,K,str); }else { getString(N-i,M,K,str); } }