poj3252——Round Numbers(数位DP)

来源:互联网 发布:淘宝怎么找指定客服 编辑:程序博客网 时间:2024/05/21 09:18
Round Numbers
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 13131 Accepted: 5072

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的个数的正数(注意是正数)
简单数位dp  但是不要犯傻,人家是二进制哟~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <stack>
using namespace std;
typedef long long ll;
#define PI 3.1415926535897932
#define E 2.718281828459045
#define INF 0xffffffff//0x3f3f3f3f
#define mod 997

const int M=1005;
int n,m;
int cnt;
int sx,sy,sz;
int mp[1000][1000];
int pa[M*10],rankk[M];
int head[M*6],vis[M*100];
int dis[M*100];
ll prime[M*1000];
bool isprime[M*1000];
int lowcost[M],closet[M];
char st1[5050],st2[5050];
int len[M*6];
typedef pair<int ,int> ac;
//vector<int> g[M*10];
ll dp[50][50][50];
int has[10500];
int month[13]= {0,31,59,90,120,151,181,212,243,273,304,334,0};
int dir[8][2]= {{0,1},{0,-1},{-1,0},{1,0},{1,1},{1,-1},{-1,1},{-1,-1}};

void getpri()
{
    ll i;
    int j;
    cnt=0;
    memset(isprime,false,sizeof(isprime));
    for(i=2; i<1000000LL; i++)
    {
        if(!isprime[i])prime[cnt++]=i;
        for(j=0; j<cnt&&prime[j]*i<1000000LL; j++)
        {
            isprime[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}
struct node
{
    int v,w;
    node(int vv,int ww)
    {
        v=vv;
        w=ww;
    }
};
vector<int> g[M*100];
char str[100005];
int ind[2550];
int bit[50];

int dfs(int cur,int s0,int s1,int e,int z){
    if(cur<0) return (s0>=s1);
    if(!e&&!z&&dp[cur][s0][s1]!=-1) return dp[cur][s0][s1];
    int endx=e?bit[cur]:1;
    int ans=0;
    for(int i=0;i<=endx;i++){
        if(z&&!i) ans+=dfs(cur-1,s0,s1,e&&i==endx,1);
        else ans+=dfs(cur-1,i==0?(s0+1):s0,i==1?(s1+1):s1,e&&i==endx,0);
    }
    if(!e&&!z) dp[cur][s0][s1]=ans;
    return ans;
}

int solve(int n){
    int len=0;
    while(n){
        bit[len++]=n&1;
        n>>=1;
    }
    return dfs(len-1,0,0,1,1);
}
int main()
{
    int i,j,k,t;
    ll l,r;
    memset(dp,-1,sizeof(dp));
    //scanf("%d",&t);
    while(~scanf("%d%d",&l,&r)){
        printf("%d\n",solve(r)-solve(l-1));
    }
    return 0;
}
1 0
原创粉丝点击