• USACO3.2.2Stringsobits


    Stringsobits
    Kim Schrijvers

    Consider an ordered set S of strings of N (1 <= N <= 31) bits. Bits, of course, are either 0 or 1.

    This set of strings is interesting because it is ordered and contains all possible strings of length N that have L (1 <= L <= N) or fewer bits that are `1'.

    Your task is to read a number I (1 <= I <= sizeof(S)) from the input and print the Ith element of the ordered set for N bits with no more than L bits that are `1'.

    PROGRAM NAME: kimbits

    INPUT FORMAT

    A single line with three space separated integers: N, L, and I.

    SAMPLE INPUT (file kimbits.in)

    5 3 19
    

    OUTPUT FORMAT

    A single line containing the integer that represents the Ith element from the order set, as described.

    SAMPLE OUTPUT (file kimbits.out)

    10011
    题解:看到这个果断枚举了。。。速度码出来了。。不过TLE了。。。N=31,枚举量等于2^31。。。线性的枚举也得超时啊。。。这样的话只能把第I个符合要求的数构造出来。
    我们分两步:
    第一步:计算长度为i,1的个数不超过j的01字符串组成的集合的个数,用f[i][j]表示。
    那么DP方程为f[i][j]=f[i-1][j]+f[i-1][j-1](分别表示开头为‘0’和开头为‘1’的情况)。
    边界条件:f[i,0]=1,f[0,j]=1。
    第二步:这部就是利用上述DP方程构造出第I个符合要求的01字符串。估计我说得很渣。。。直接引用nocow上的话好了:设所求串为S,假设S的位中最高位的1在自右向左第K+1位,那么必然满足F[K,L]< I,F[K+1,L] >=I,这样的K是唯一的。所以S的第一个1在从右至左第K+1位.因为有F[K,L]个串第K+1位上为0,所以所求的第I个数的后K位就应该是满足"位数为K且串中1不超过L-1个"这个条件的第I-F[K,L]个数。
    View Code
    /*
    ID:spcjv51
    PROG:kimbits
    LANG:C
    */
    #include<stdio.h>
    #include<string.h>
    int main(void)
    {
        freopen("kimbits.in","r",stdin);
        freopen("kimbits.out","w",stdout);
        long i,j,n,l;
        long long m;
        long f[32][32];
        scanf("%ld%ld%lld",&n,&l,&m);
        memset(f,0,sizeof(f));
        for(i=0;i<=n;i++)
        {
            f[0][i]=1;
            f[i][0]=1;
        }
        for(i=1;i<=n;i++)
        for(j=1;j<=l;j++)
        f[i][j]=f[i-1][j]+f[i-1][j-1];
        for(i=n;i>=1;i--)
        if(f[i-1][l]<m)
        {
            printf("1");
            m-=f[i-1][l];
            l--;
        }
        else
        printf("0");
        printf("\n");
        return 0;
    }
     
  • 相关阅读:
    CPU Cache与缓存行
    linux 查看命令总结
    idea自个常用工具的总结
    《人月神话》
    啊哈,翻转
    Scrapy爬虫之豆瓣TOP250
    87的100次幂除以7的余数是多少
    python之sqlite3 用法详解
    Sublime Text 3 插件SublimeLinter/PEP8安装&配置,检查代码规范
    urlparse之urljoin() 爬虫必备
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2932560.html
Copyright © 2020-2023  润新知