POJ-3252 Round Numbers

来源:互联网 发布:知柏地黄丸怎么服用 编辑:程序博客网 时间:2024/05/15 16:11
Round Numbers
Time Limit: 2000MS Memory Limit: 65536K   

Description

The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets to be milked first. They can't even flip a coin because it's so hard to toss using hooves.

They have thus resorted to "round number" matching. The first cow picks an integer less than two billion. The second cow does the same. If the numbers are both "round numbers", the first cow wins,
otherwise the second cow wins.

A positive integer N is said to be a "round number" if the binary representation of N has as many or more zeroes than it has ones. For example, the integer 9, when written in binary form, is 1001. 1001 has two zeroes and two ones; thus, 9 is a round number. The integer 26 is 11010 in binary; since it has two zeroes and three ones, it is not a round number.

Obviously, it takes cows a while to convert numbers to binary, so the winner takes a while to determine. Bessie wants to cheat and thinks she can do that if she knows how many "round numbers" are in a given range.

Help her by writing a program that tells how many round numbers appear in the inclusive range given by the input (1 ≤ Start < Finish ≤ 2,000,000,000).

Input

Line 1: Two space-separated integers, respectively Start and Finish.

Output

Line 1: A single integer that is the count of round numbers in the inclusive range Start..Finish

Sample Input

2 12

Sample Output

6
————————————————————集训18.3的分割线————————————————————
思路:从十进制填数字变成了二进制填数字。难度稍微提升了。状态:
1. 长度
2. 0的个数和1的个数之差
3. 1是否出现过
在之前的填数字中,最高位可以是0,因为我们认为所有数字的位数都是最大数的位数,然后从最高位向下填。但是现在不行了,在第一个1前面的0都是无效的。只能在第一个1出现之后再统计。
另外要注意防止下标出现负值(1比0多)
代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <climits>#include <iostream>#define INF 0x3f3f3f3fusing namespace std;/****************************************/int dp[35][70][2];int num[35], cnt;int dfs(int len, int diff, bool one, bool bnd){if(len == 0) {if(diff >= 35) {return 1;}return 0;}if(!bnd && dp[len][diff][one] != -1)return dp[len][diff][one];int lim = (bnd ? num[len] : 1);int ret = 0;for(int i = 0; i <= lim; i++) {int nd;if(one||i)//这里WA了一次,因为用了one(前一个数是否出现1)来判断nd = (i == 0 ? 1 : -1);elsend = 0;ret += dfs(len-1, diff + nd, one || i, bnd&&i==lim);}if(!bnd) dp[len][diff][one] = ret;return ret;}int Solve(int x){cnt = 0;while(x) {num[++cnt] = x&1;x >>= 1;}return dfs(cnt, 35, false, true);//以35作为初始值,防止下标为负}int main(){#ifdef J_Sure//freopen("000.in", "r", stdin);//freopen(".out", "w", stdout);#endifint l, r;memset(dp, -1, sizeof(dp));while(~scanf("%d%d", &l, &r)) {printf("%d\n", Solve(r) - Solve(l-1));}return 0;}


0 0
原创粉丝点击