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

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[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1};
int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
int bits[100];
ll dp[100][100][100];
ll dfs(int len,int numzero,bool ismax,int numone,bool haveone){
    if(!len) return numzero>=numone;
    if(!ismax&&dp[len][numzero][numone]>=0) return dp[len][numzero][numone];
    int up=ismax?bits[len]:1;
    ll ans=0; int i;
    for(i=0;i<=up;i++){
        if(i==0&&haveone)
            ans+=dfs(len-1,numzero+1,ismax&&i==up,numone,true);
        else if(i==1)
            ans+=dfs(len-1,numzero,ismax&&i==up,numone+1,true);
        else if(i==0&&!haveone)
            ans+=dfs(len-1,numzero,ismax&&i==up,numone,false);
    }
    if(!ismax) dp[len][numzero][numone]=ans;
    return ans;
}
ll solve(ll n){
    int len=0;
    while(n){
        bits[++len]=n%2;
        n/=2;
    }
    return dfs(len,0,true,0,false);
}
int main(){
    ios::sync_with_stdio(false);
    ll s,e;
    while(cin>>s>>e){
        memset(dp,-1,sizeof(dp));
        cout<<solve(e)-solve(s-1)<<endl;
    }
    return 0;
}

 

猜你喜欢

转载自www.cnblogs.com/wmj6/p/10646372.html
今日推荐