poj 3252 Round Numbers

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15952 Accepted: 6550

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

解题思路

数位dp+记忆化 。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdlib>

using namespace std;
const int MAXN = 35;

int f[MAXN][105][2][2];  //f[i][j][0/1][0/1]表示填到倒数第二位,01相差j,
                            //前面是否有限制,是否已经有1 
int l,r,L[MAXN],R[MAXN],lcnt,rcnt;
int ansl,ansr;

int dfsl(int x,int t,int limit,int has){
    if(t+x<50) return 0;
    if(f[x][t][limit][has]) return f[x][t][limit][has];
    if(x==0) return f[x][t][limit][has]=t>=50;
    if(limit) {
        if(L[x]) {
            f[x][t][limit][has]+=dfsl(x-1,t+1*has,0,has);
            f[x][t][limit][has]+=dfsl(x-1,t-1,1,has|1);
        }
        else f[x][t][limit][has]+=dfsl(x-1,t+1*has,1,has);  
    }
    else{
        f[x][t][limit][has]+=dfsl(x-1,t-1,limit,has|1);
        f[x][t][limit][has]+=dfsl(x-1,t+1*has,limit,has);
    }
    return f[x][t][limit][has];
}

int dfsr(int x,int t,int limit,int has){
    if(t+x<50) return 0;
    if(f[x][t][limit][has]) return f[x][t][limit][has];
    if(x==0) return f[x][t][limit][has]=t>=50;
    if(limit) {
        if(R[x]) {
            f[x][t][limit][has]+=dfsr(x-1,t+1*has,0,has);
            f[x][t][limit][has]+=dfsr(x-1,t-1,1,has|1);
        }
        else f[x][t][limit][has]+=dfsr(x-1,t+1*has,1,has);  
    }
    else{
        f[x][t][limit][has]+=dfsr(x-1,t-1,limit,has|1);
        f[x][t][limit][has]+=dfsr(x-1,t+1*has,limit,has);
    }
    return f[x][t][limit][has];
}

int main(){
    scanf("%d%d",&l,&r);l--;
    while(l) L[++lcnt]=(l&1),l>>=1;
    while(r) R[++rcnt]=(r&1),r>>=1;
    ansl=dfsl(lcnt,50,1,0);
    memset(f,0,sizeof(f));
    ansr=dfsr(rcnt,50,1,0);
    cout<<ansr-ansl<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/sdfzsyq/p/9676890.html