K-th string

这两天参加了hihocoder上的小竞赛,下面把自己做的记录一下!(最痛心的是,开始竟然把main函数,写成了mian,浪费了将近一个小时时间,伤不起啊)

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, {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”.

 

样例输入

3
2 2 2
2 2 7
4 7 47

样例输出

0101
Impossible
01010111011

这里给出我的答案

思路: 计算由N个0和M个1组成的数的最小值和最大值,然后判断从最小值开始,计算每个数包含一个的个数,到第K的时候,再将其转为包含N个0,和M个1的字符串,输出。(这是暂时想出的方法,以后会仔细思考一下,如果你有不同的方法,欢迎交流

#include<iostream>
#include<string>
#include<vector>
#include<math.h>
using namespace std;

int fac(int n)
{
    if(n == 1)
        return 1;
    else
        return n*fac(n-1);
}
int numOne(int value)
{
    int res = 0;
    while(value)
    {
        if(value % 2 == 1)
            res++;
        value = value /2;
    }
    return res;
}
string outObj(int value,int len)
{
    string res = "";
    while(value)
    {
        if(value % 2 == 1)
            res.append("1");
        else
            res.append("0");
        value = value / 2;
    }
    reverse(res.begin(),res.end());
    string app="";
    for(int i=0; i < len - res.size(); i++)
        app.append("0");
    app.append(res);
    return app;
}
int main()
{
    int T;
    cin>>T;
    int M,N,K;
    vector<int> result(T);
    int minValue,maxValue;
    for(int i=0; i<T; i++)
    {
        cin>>N>>M>>K;
        int len = M + N;
        minValue=0;
        maxValue=0;
        if(fac(N+M)/(fac(N) * fac(M)) < K)
        {
            cout<<"Impossible"<<endl;
            continue;
        }    
        for(int j=0; j<M; j++)
        {
            minValue = minValue + pow(2, j);
        }
        for(int j=N; j<M+N; j++)
        {
            maxValue = maxValue + pow(2, j);
        }
        int count = 0;
        int temp = 0;
        int obj = 0;
        for(int j = minValue; j <= maxValue; j++)
        {
            temp = numOne(j);
            if(temp == M)
                count++;
            if(count == K)
            {
                obj = j;
                break;
            }
        }
        string res = outObj(obj,len);
        cout<<res<<endl;
    }
    return 0;
}

 

posted @ 2014-04-12 21:59  ywl925  阅读(718)  评论(3编辑  收藏  举报
Copyright at 2012 yangwenlong