pku 3252 Round Numbers(DP)

如果一个数的二进制表示中0的个数大于1的个数,这个数就是一个round numbers。给定一个范围,问这个范围内有多少个round numbers。

 

本来以为是位运算,有什么好办法可以快速统计出每个串中0和1的个数。自己想了一下,没什么结果。

后来看到一个官方提示,用DP做。

 

First note that you only need to count the number of round numbers not exceeding Start-1 and subtract that from the number of round numbers not exceeding Finish. So we are only concerned with counting the number of round numbers not exceeding some number X. Since X can be up to a two billion, we cannot count round numbers one by one.

Consider X in binary. Let N be the number of digits in X.

Let A(x,y) be the number of binary strings with x '0's and y '1's that begin with 0. Let B(x,y) be the number of binary strings with x '0's and y '1's that begin with 1. Let C(x,y) be the number of binary strings with x '0's and y '1's that begin with 0 where each string represents a number not exceeding the number represented by the last x+y digits of X. Let D(x,y) be the number of binary strings with x '0's and y '1's that begin with 1 where each string represents a number not exceeding the number represented by the last x+y digits of X.

We update A, B, C, D using dynamic programming bottom up for each x+y=k, incrementing k until k = N.

The desired result is the sum of all D(x,y) where x+y = N and x>=y, plus the sum of all B(x,y) with x+y<N and x>=y.

Do this procedure for both Finish and Start-1 and subtract to get answer.

 

虽然有提示,但说实话这个DP也不好做,4种状态,我调了好长时间才调出来。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值