POJ-3252 Round Numbers (数位dp 好题)

来源:互联网 发布:淘宝微型打印机打印纸 编辑:程序博客网 时间:2024/06/16 09:17
Round Numbers
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 14622 Accepted: 5876

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


#include <stdio.h>#include <string.h>#include <vector>using namespace std;long long dp[35][2][2][35];void init(){memset(dp, 0, sizeof(dp));dp[1][0][0][1] = 1;dp[1][1][1][1] = 1;for(int i = 2; i <= 33; ++i){dp[i][0][0][0] = dp[i - 1][0][1][1] + dp[i - 1][1][1][1];dp[i][0][1][0] = dp[i - 1][0][1][1] + dp[i - 1][1][1][1];dp[i][1][1][0] = dp[i - 1][0][0][1] + dp[i - 1][1][0][1];dp[i][1][0][0] = dp[i - 1][0][0][1] + dp[i - 1][1][0][1];for(int j = 1; j <= i; ++j){dp[i][0][0][j] = dp[i - 1][0][0][j - 1] + dp[i - 1][1][0][j - 1];dp[i][0][1][j] = dp[i - 1][0][1][j + 1] + dp[i - 1][1][1][j + 1];dp[i][1][0][j] = dp[i - 1][0][0][j + 1] + dp[i - 1][1][0][j + 1];dp[i][1][1][j] = dp[i - 1][0][1][j - 1] + dp[i - 1][1][1][j - 1];}}}long long solve(long long x){if(x == 0) return 0;vector<int>s;while(x){s.push_back(x % 2);x /= 2;}int n = s.size() - 1, cnt;long long ans = 0;for(int i = 1; i <= n; ++i){for(int j = 0; j <= i; ++j){ans += dp[i][1][0][j];}}cnt = 1;for(int i = n - 1; i >= 0; --i){if(s[i] == 1){if(cnt <= 0){for(int j = -cnt; j >= 0; --j){ans += dp[i + 1][0][1][j];}for(int j = 1; j <= i + 1; ++j){ans += dp[i + 1][0][0][j];}}else{for(int j = cnt; j <= i + 1; ++j){ans += dp[i + 1][0][0][j];}}cnt++;}else{cnt--;}}return ans;}int main(){init();long long l, r;while(scanf("%lld %lld", &l, &r) != EOF){printf("%lld\n", solve(r + 1) - solve(l));}}/*题意:数据范围2e9,问有多少个数的二进制中0的个数大于等于1的个数。思路:数位dp,dp[i][j][k][l]表示第i位为j(j==0||j==1)且k(k==0||k==1)的个数多l个。先将dp数组初始化一下,这个很好转移。然后对于每组case,solve一下两个端点即可。处理过程中,最高位一定为0,那么我们让最高位为0,然后统计所有其他位为1的答案,然后将最高位固定为1,然后每当碰到一位为1,就考虑填0,然后维护一下答案。由于前导0会影响答案的正确性,所以需要特殊处理最高位。然后正常数位dp即可。*/


原创粉丝点击