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 浪费了太多时间!!!

微软2014实习生招聘笔试第2题 the k-th string的更多相关文章

  1. 优酷土豆2014校园招聘笔试题目之Java开发类

    先总体说下题型,共有20道选择题,4道简答题,3道编程题和1道扩展题,题目都比较简单,限时一小时完成. 一.选择题 选择题非常简单,都是基础题,什么死锁发生的条件.HashMap和HashSet查找插 ...

  2. 【微软2014实习生及秋令营技术类职位在线測试】题目2 : K-th string

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description Consider a string set that each of them consists of ...

  3. 微软2014实习生及秋令营技术类职位在线测试(题目1 : String reorder)

    题目1 : String reorder 时间限制:10000ms 单点时限:1000ms 内存限制:256MB Description For this question, your program ...

  4. 2019_京东JAVA实习生招聘机试第一题

    题意抽象出来就是,求根节点的所有子节点中,以这些子节点为根的子树的最大节点数. 已有向图的方式来保存无向图,所以叶子结点i的eage[i].size()==1. import java.util.Ar ...

  5. 九度OJ 1525 子串逆序打印 -- 2012年Google校园招聘笔试题目

    题目地址:http://ac.jobdu.com/problem.php?pid=1525 题目描述: 小明手中有很多字符串卡片,每个字符串中都包含有多个连续的空格,而且这些卡片在印刷的过程中将字符串 ...

  6. S2 深入.NET和C#编程 笔试测试错题积累

    ---恢复内容开始--- <深入.NET平台和C#编程>内部测试题-笔试试卷错题积累 1: 1) 以下关于序列化和反序列化的描述错误的是( C). a) 序列化是将对象的状态存储到特定存储 ...

  7. 乘风破浪:LeetCode真题_023_Merge k Sorted Lists

    乘风破浪:LeetCode真题_023_Merge k Sorted Lists 一.前言 上次我们学过了合并两个链表,这次我们要合并N个链表要怎么做呢,最先想到的就是转换成2个链表合并的问题,然后解 ...

  8. 金山网络2014春季Android实习生招聘-成都站-笔试第二题

    一个文件名为input.txt的文件当中,每一行都有一个单词,要求统计单词出现的频率,并且按照从小到大出现次数打印,次数相同的按照首字母顺序排序. package jinshanwangluo.exa ...

  9. 金山网络2014春季Android实习生招聘-成都站-笔试第一题

    实现单例模式,并实现方法int getResult(float a),将a*8后返回. package jinshanwangluo.exam; /** * @author guoxm * @date ...

随机推荐

  1. Shachar Fleishma的论文,做点云重建的几篇论文都不错

    http://www.sci.utah.edu/~shachar/ 几篇论文都不错,但貌似05年之后就没有什么动作了.

  2. NIO编程介绍

    代码: package bhz.nio; import java.io.IOException; import java.net.InetSocketAddress; import java.nio. ...

  3. mongodb(四)

    Count+Distinct+Group数据库命令操作固定集合特性GridFS文件系统补充服务器端脚本 db.runCommand({group:{ ns:"persons", k ...

  4. spring-boot-actuator健康监控

    #健康监控 management.security.enabled=false health.mail.enabled =false http://localhost:54001/autoconfig ...

  5. Linux环境下安装myeclipse+破解

    1.下载myeclipse安装包,下载myeclipse破解文件. 2.修改myeclipse-pro-2014-GA-offline-installer-linux.run的权限 sudo chmo ...

  6. Nginx负载均衡高可用

    1.   Nginx负载均衡高可用 首先介绍一下Keepalived,它是一个高性能的服务器高可用或热备解决方案,Keepalived主要来防止服务器单点故障的发生问题,可以通过其与Nginx的配合实 ...

  7. CMDB-客户端

    配置文件的设置 大体思路: 1,通过开始文件将用户配置信息的文件放置到环境变量中. 2,在lib文件中的config文件中,从环境变量中获取到用户的配置,通过importlib模块导入用户配置文件,通 ...

  8. 高性能Web服务器Nginx的配置与部署研究(8)核心模块之事件模块

    一.事件模块的作用是什么? 用来设置Nginx处理链接请求. 二.相关指令 1. accept_mutex 含义:设置是否使用连接互斥锁进行顺序的accept()系统调用. 语法:accept_mut ...

  9. Unity3D中暂停时的动画及粒子效果实现

    暂停是游戏中经常出现的功能,而Unity3D中对于暂停的处理并不是很理想.一般的做法是将Time.timeScale设置为0.Unity的文档中对于这种情况有以下描述: The scale at wh ...

  10. linux系统中的单引号和双引号

    一.为什么用quoting 前面说到,命令行在内容上主要由literal(一般文字)和meta(元字符)组成,而meta又由IFS.CR和其他字符组成(如=,$, > 等),通常这些原元字符都有 ...