现在的位置: 首页 > 综合 > 正文

pku 3252 Round Numbers(DP)

2013年02月17日 ⁄ 综合 ⁄ 共 1891字 ⁄ 字号 评论关闭

如果一个数的二进制表示中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种状态,我调了好长时间才调出来。

 

抱歉!评论已关闭.