USACO: Stringsobits

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


 

解题思路:

1、用一个二维数组cnt[i][j]来记录长度为i的,包含不多于j个1的二进制串的个数,这一步可以这样实现:当j = 0,cnt[i][j] = 1; 当j > i,cnt[i][j] = cnt[i][i];其他情况cnt[i][j] = cnt[i - 1][j] + cnt[i - 1][j - 1]。

2、假设s是第k个长度为i的,包含不多于j个1的二进制串,那么,在长度为i + 1的,包含不多于j + 1个1的二进制串中,0s是第k个,1s是第(k + cnt[i][j])个,利用这个思路的逆向, 我们就可以按顺序输出整个串。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值