• 数位dp-POJ-3252-Round Numbers


    最近一直在看书和博客,即使做出几道题来也是看别人题解写的,感觉没自己的东西,所以很久没更新博客

    看了很多数位dp的题和题解,发现数位dp题是有固定的模版的,并且终于自己做出来一道。

    我觉得用记忆化搜索写数位dp很巧妙,而且容易想,且有一定模版,很好用。

    Round Numbers
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 7659   Accepted: 2637

    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

     
    #include <iostream>
    #include<cstring>
    using namespace std;
    typedef long long LL;
    
    LL table[64][164];
    int digit[64];
    
    LL DFS(int len,int on,int ze,bool bound,bool zero)     //len代表位数为len的情况,zero代表是否有前导零
    {
        if(len==0)                                         //递归边界
            return (on<=ze)?1:0;                            
        if(!zero&&!bound&&table[len][on+100-ze]!=-1)       //用on+100-zero代表len位的高位上0和1的相对数量,100代表相等,比100大代表1多,反之0多
            return table[len][on+100-ze];                  //如果该种情况已经搜索过结果了就直接return
        int up=bound?digit[len]:1;                         //代表循环的上届,如果一直都是以每位数字为上界的话,就继续用该位的数字为上界,否则以1为上界
        LL ret=0;
        for(int i=0;i<=up;i++)
        {
            if(on==ze+len&&i==1)                          //如果已经不能出现0的个数>=1的个数的情况就剪枝
                continue;
            ret+=DFS(len-1,(i==1)?(on+1):on,(!zero&&i==0)?(ze+1):ze,bound&&i==up,zero&&i==0);
        }
        if(!bound&&!zero)                                  //保存已经搜索到的结果
            table[len][on+100-ze]=ret;
        return ret;
    }
    
    LL fun(LL n)
    {
        memset(digit,0,sizeof(digit));
        if(n==-1)
            return 0;
        int len=0;
        while(n)
        {
            digit[++len]=n%2;
            n/=2;
        }
        return DFS(len,0,0,true,true)+1;
    }
    
    int main()
    {
        memset(table,-1,sizeof(table));
        LL A,B;
        while(cin>>A>>B)
        {
            cout<<fun(B)-fun(A-1)<<endl;;
        }
    }
    
  • 相关阅读:
    C#函数式编程
    三种观察者模式的C#实现
    使用C#设计Fluent Interface
    02.Python网络爬虫第二弹《http和https协议》
    03.Python网络爬虫第一弹《Python网络爬虫相关基础概念》
    第七章:Python基础のXML操作和面向对象(一)
    第五章:Python基础の生成器、迭代器、序列化和虚拟环境的应用
    第六章:Python基础の反射与常用模块解密
    第四章:Python基础の快速认识內置函数和操作实战
    第三章:Python基础の函数和文件操作实战
  • 原文地址:https://www.cnblogs.com/aljxy/p/3371687.html
Copyright © 2020-2023  润新知