poj 3252 Round Numbers(数位dp 处理前导零)

时间:2023-03-08 15:39:43

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

题意:求s到e中二进制数 0的个数大于1的个数 的数的个数(语文不好见谅..)

思路:dp[i][j][k] i为数位 j为0的个数 k为1的个数 主要就是需要处理前导零 因为数位dp不可避免的要遍历前面全是零的情况 所以需要在递归的时候再加一个参数出现一以后再计算

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int bits[];
ll dp[][][];
ll dfs(int len,int numzero,bool ismax,int numone,bool haveone){
if(!len) return numzero>=numone;
if(!ismax&&dp[len][numzero][numone]>=) return dp[len][numzero][numone];
int up=ismax?bits[len]:;
ll ans=; int i;
for(i=;i<=up;i++){
if(i==&&haveone)
ans+=dfs(len-,numzero+,ismax&&i==up,numone,true);
else if(i==)
ans+=dfs(len-,numzero,ismax&&i==up,numone+,true);
else if(i==&&!haveone)
ans+=dfs(len-,numzero,ismax&&i==up,numone,false);
}
if(!ismax) dp[len][numzero][numone]=ans;
return ans;
}
ll solve(ll n){
int len=;
while(n){
bits[++len]=n%;
n/=;
}
return dfs(len,,true,,false);
}
int main(){
ios::sync_with_stdio(false);
ll s,e;
while(cin>>s>>e){
memset(dp,-,sizeof(dp));
cout<<solve(e)-solve(s-)<<endl;
}
return ;
}