poj3252 数位dp

来源:互联网 发布:操盘手训练软件 编辑:程序博客网 时间:2024/05/16 18:05
Round Numbers
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 11955 Accepted: 4512

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
题意: 如果一个数的二进制数0的个数大于等于1的个数,则称这个数为轮数,为[l,r]之间有多少个轮数
思路 ,我们要求l-r之间的轮数个数,那么要想到如何求到1-P 之间的轮数个数,设f(x)=1-x 的轮数个数
那么ans=f(r+1)-f(l),为什么是r+1,因为我们的f(x)是不包含x的,但是[l,r]是闭区间啊,所以r+1
说是dp题,其实也不算真正的dp题目,是对二进制数的操作以及组合数的操作
将x 写成二进制数,长度为len,那么长度比len小的数都小于x

那么首先计算长度小于lenRN数有多少(由于这些数长度小于len,那么他们的值一定小于k,因此在进行组合时就无需考虑组合所得的数与k之间的大小了)

for(i=1;i<bin[0]-1;i++)         //bin[0]记录的是二进制数的长度len

              for(j=i/2+1;j<=i;j++)

                     sum+=c[i][j];

可以看到,i<len-1 ,之所以减1,是因为这些长度比len小的数,最高位一定是1,那么剩下可供放入数字的位数就要再减少一个了

这条程序得到的sum

然后计算和len长度一样的数但是比x小的数的个数

int zero=0;  //从高位向低位搜索过程中出现0的位的个数

       for(i=bin[0]-1;i>=1;i--)

              if(bin[i])   //当前位为1

                     for(j=(bin[0]+1)/2-(zero+1);j<=i-1;j++)

                            sum+=c[i-1][j];

              else

                     zero++;

之所以初始化i=bin[0]-1,是因为bin[]是逆向存放k的二进制的,因此要从高位向低位搜索,就要从bin[]后面开始,

而要 bin[0]-1 是因为默认以后组合的数长度为len,且最高位为1,因此最高位不再搜索了。

那么问题的关键就是怎样使得以后组合的数小于k

这个很简单:

从高位到低位搜索过程中,遇到当前位为0,则不处理,但要用计数器zero累计当前0出现的次数

遇到当前位为1,则先把它看做为0zero+1,那么此时当前位 后面的 所有低位任意组合都会比k小,

找出这些组合中RN的个数,统计完毕后把当前位恢复为原来的1,然后zero-1,继续向低位搜索

 

 

那么问题就剩下 当当前位为1时,把它看做0之后,怎样去组合后面的数了

此时组合要考虑2个方面:

(1)当前位置i后面允许组合的低位有多少个,我的程序由于bin是从bin[1]开始存储二进制数的,

因此 当前位置i后面允许组合的低位有i-1

(2)组合前必须要除去前面已出现的0的个数zero

我的程序中初始化j=(bin[0]+1)/2-(zero+1) j本来初始化为(bin[0]+1)/2就可以了,

表示对于长度为bin[0]的二进制数,当其长度为偶数时,至少其长度一半的位数为0,它才是RN,当其长度为奇数时,

至少其长度一半+1的位数为0,它才是RN

但是现在还必须考虑前面出现了多少个0,根据前面出现的0的个数,j的至少取值会相应地减少。  -(zero+1) 

之所以+1是因为要把当前位bin[i]看做0

#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;typedef long long LL;int C[35][35]= {0};int tot,bin[35];void get_C()///得到组合数{    for(int i=0; i<=32; i++)    {        for(int j=0; j<=i; j++)        {            if(j==0||j==i)                C[i][j]=1;            else                C[i][j]=C[i-1][j]+C[i-1][j-1];        }    }}void get_bin(int n)///得到二进制序列{    bin[0]=0;    while(n)    {        bin[++bin[0]]=n%2;        n=n/2;    }}int solve(int n){    int ans=0;    get_bin(n);    for(int j=1; j<bin[0]-1; j++)///从 1位到长度为len-1位的数都小于n ,由于首位必须是1,所以     {                            /// 可以任意放的 数的位数 从1-len-2         for(int k=j/2+1; k<=j; k++)            ans+=C[j][k];    }    int zero=0;    ///接下来是长度等于len 的数且比n小的数    for(int j=bin[0]-1; j>=1; j--)///第一位肯定是1 所以从len-1位 开始讨论    {        if(bin[j])///如果该位为1,把这一位置为0,那么这一位后面的 j-1 位可以任意填        {            ///现在有已经有 zero+1 个0 不要忘了当前位为0            for(int k=(bin[0]+1)/2-zero-1; k<=j-1; k++)                ans+=C[j-1][k];        }        else            zero++;    }    return ans;}int main(void){    get_C();    int  a,b;    int  ans1,ans2;    cin>>a>>b;    cout<<solve(b+1)-solve(a)<<endl;    return 0;}





0 0