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 s​uch 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, {0011, 0101, 0110, 1001, 1010, 1100}, 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

求全排列的问题.

使用STL #include<algorithm>  ,可能超时

 #include <iostream>
#include <algorithm>
#include <string>
using namespace std; // 计算组合数
long long com(int n,int r)
{
if(n-r < r) r= n-r; //减少计算量
int i,j,s=;
for(i=0,j=;i<r;++i)
    {
s*=(n-i);
for(;j<=r && s%j==; ++j) s/=j; // 防止溢出
}
return s;
} int main()
{
string str;
int T,a,b,i,j;
long long m,Count;
cin>>T;
while(T--)
{
Count=;
str.clear();
cin>>a>>b>>m;
for(i=;i<a;i++)
str+="";
for(j=;j<b;j++)
str+="";
if(m>com(a+b,b))
cout<<"Impossible"<<endl;
else
{
while (next_permutation(str.begin(), str.end()))
{
++Count;
if(Count+==m)
{
cout<<str<<endl;
break;
}
} }
}
return ;
}

方法二: 回溯法

 #include <stdio.h>
#define MAX_N 33 int n,m=;
long long Count=,times; // 共有n个数,其中互不相同的有m个
int rcd[MAX_N]; //记录每个位置填的数字
int used[MAX_N]; //标记m个数可以使用的次数
int num[MAX_N]; // 存放互不相同的m个数 0,1 long long com(int n, int r)
{
if(n-r < r) r= n-r; // 减少计算量
int i,j,s=;
for(i=0,j=;i<r;++i)
     {
s*=(n-i);
for(;j<=r && s%j==; ++j) s/=j; // 尽量避免越界
}
return s;
} void unrepeat_permutation(int l)
{
int i;
if(l==n)
{
Count++;
if(Count==times) //查找到所需的数字
{
for(i=;i<n;i++)
{
printf("%d",rcd[i]);
if(i<n-) printf(" ");
}
printf("\n"); }
return;
}
for(i=;i<m;i++) //回溯求解
{
if(used[i]>)
{
used[i]--;
rcd[l] = num[i];
unrepeat_permutation(l+);
used[i]++;
}
}
} int main()
{
int a,b,i,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%lld",&a,&b,&times);
n=a+b;
if(times>com(n,a)) // 判断是否超出组合范围
{
printf("Impossible\n");
continue;
}
used[]=a; used[]=b; //记录0,1的个数
num[]=; num[]=; //记录可能出现的数字
Count=;
unrepeat_permutation();
}
return ;
}

第一题scanf,gets 浪费了太多时间!!!

最新文章

  1. pagination 分页
  2. 关于Android四大组件的学习总结
  3. linux 添加用户、权限
  4. 基于nodejs的终端天气查询
  5. 四、优化及调试--网站优化--Yahoo军规中
  6. JAVA while循环,do-while循环,for循环
  7. QQ消息99+形成--第三方开源--BezierView
  8. Android 贝塞尔曲线
  9. for语句的嵌套(示例及练习)
  10. JQuery日记_5.13 Sizzle选择器(六)选择器的效率
  11. Linux下搭建ntp时间同步服务器
  12. Unity塔防游戏开发
  13. Spring:Bean生命周期
  14. FFmpeg源代码简单分析:avcodec_open2()
  15. ip本地查询
  16. [math][mathematica] mathematica入门
  17. 前端学习笔记之CSS属性设置
  18. C#--Switch Case语句的返回
  19. forward declaration of class 错误
  20. [LeetCode 题解]:Gray Code

热门文章

  1. 在线浏览office 文件
  2. 正则表达式RE与扩展正则表达式ERE——grep与egrep
  3. CBV 验证装饰器的使用
  4. Docker remote api 开启
  5. 黑暗之光 Day2
  6. UGUI 自动布局的重叠BUG
  7. An Intuitive Explanation of Fourier Theory
  8. 在Core环境下用WebRequest连接上远程的web Api 实现数据的简单CRUD(附Git地址)
  9. shell编程——sed用法
  10. zoj1001-A + B Problem