Round Numbers(数位DP)

Round Numbers

http://poj.org/problem?id=3252

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17293   Accepted: 7188

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


 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 #define lson l,mid,rt<<1
 5 #define rson mid+1,r,rt<<1|1
 6 #define sqr(x) ((x)*(x))
 7 #define pb push_back
 8 #define eb emplace_back
 9 #define maxn 13000005
10 #define eps 1e-8
11 #define pi acos(-1.0)
12 #define rep(k,i,j) for(int k=i;k<j;k++)
13 typedef long long ll;
14 typedef pair<int,int> pii;
15 typedef pair<long long,int>pli;
16 typedef pair<char,int> pci;
17 typedef pair<pair<int,string>,pii> ppp;
18 typedef unsigned long long ull;
19 const long long MOD=1e9+7;
20 /*#ifndef ONLINE_JUDGE
21         freopen("1.txt","r",stdin);
22 #endif */
23 
24 int dp[35][65];
25 int a[65];
26 int num;
27 
28 
29 int dfs(int pos,int sta,bool lead,bool limit){
30     if(pos==-1) return sta>=32;
31     if(!limit&&!lead&&dp[pos][sta]!=-1) return dp[pos][sta];
32     int up=limit?a[pos]:1;
33     int ans=0;
34     for(int i=0;i<=up;i++){
35         if(lead&&i==0) ans+=dfs(pos-1,sta,lead,limit&&i==a[pos]);
36         else ans+=dfs(pos-1,sta+(i==0?1:-1),lead&&i==0,limit&&i==a[pos]);
37     }
38     if(!limit&&!lead) dp[pos][sta]=ans;
39     return ans;
40 }
41 int solve(int x){
42     int pos=0;
43     while(x){
44         a[pos++]=x%2;
45         x>>=1;
46     }
47     return dfs(pos-1,32,true,true);
48 }
49 
50 int main(){
51     #ifndef ONLINE_JUDGE
52      //   freopen("1.txt","r",stdin);
53     #endif
54     std::ios::sync_with_stdio(false);
55     int n,m;
56     memset(dp,-1,sizeof(dp));
57     while(cin>>n>>m){
58         cout<<solve(m)-solve(n-1)<<endl;
59     }
60 }
View Code
原文地址:https://www.cnblogs.com/Fighting-sh/p/10518939.html