【POJ 3252】Round Numbers(数位dp)

Round Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16160 Accepted: 6650

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

Source
USACO 2006 November Silver
题意:统计l~r区间内二进制形式0的个数>=1的个数的数的个数
solve:把区间转换成为2进制形式进行区间dp
在这里我们需要注意的是一个二进制的首位一定是1,因此我们需要加入一个
变量来判断当前首位是否为1。
d p [ a ] [ b ] [ c ] 来表示二进制长度为 a , b 0 , c 1
进行数位dp即可。

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef int INT; 
#define int long long
const int maxn = 1e6+10;
const int INF = 0x3f3f3f3f;
int digit[100];
int dp[100][100][100];//dp[a][b][c]代表长度为a,b个0,c个1的个数
int dfs(int pos,int cnt0,int cnt1,bool first,bool limit){
    if(pos<0) return (first||cnt0>cnt1);
    if(!limit&&!first&&dp[pos][cnt0][cnt1]) return dp[pos][cnt0][cnt1];
    int ans=0;
    int up=limit?digit[pos]:1;
    for(int i=0;i<=up;i++){
        if(first){//无前导1
            if(i){
                ans+=dfs(pos-1,0,0,0,limit&&i==up);
            }
            else{
                ans+=dfs(pos-1,0,0,1,limit&&i==up);
            }
        }
        else{
            if(i) ans+=dfs(pos-1,cnt0,cnt1+1,0,limit&&i==up);
            else ans+=dfs(pos-1,cnt0+1,cnt1,0,limit&&i==up);
        }
    }
    if(!limit&&!first) dp[pos][cnt0][cnt1]=ans;
    return ans;
}
int solve(int x){
    int cnt=0;
    while(x){
        digit[cnt++]=x&1;
        x>>=1;
    }
    return dfs(cnt-1,0,0,1,1);
}
INT main(){
        int s,t;
        cin>>s>>t;
        memset(digit,-1,sizeof(digit));
        memset(dp,0,sizeof(dp));
        cout<<solve(t)-solve(s-1)<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/duanghaha/article/details/82630130
今日推荐