• Round Numbers


    题目链接:http://poj.org/problem?id=3252

    Round Numbers
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 15688   Accepted: 6408

    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

    题目大意:输入n,m,求区间[n.m]之间有多少个数二进制情况下0的个数大于等于1的个数
    个人思路:其实还是老思路,记忆化,这题要注意的是不仅仅要注意上限的限制,还要注意前导是否为0,如果上一位为0的话,这一位为0,0的个数是不用加1的,大概就是这样了
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<stdio.h>
    #include<string.h>
    #include<cmath>
    #include<math.h>
    #include<algorithm>
    #include<set>
    #include<queue>
    #include<map>
    typedef long long ll;
    using namespace std;
    const ll mod=1e9+7;
    const int maxn=1e8+10;
    const int maxk=100+10;
    const int maxx=1e4+10;
    const ll maxa=43200;
    #define INF 0x3f3f3f3f3f3f
    int dp[35][70];
    int a[35];
    ll dfs(ll pos,ll sta,bool lead,bool limit)
    {
        if(pos==-1)
        return sta>=35;
        if(!limit&&!lead&&dp[pos][sta]!=-1) return dp[pos][sta];//没有位数限制和前导限制的时候才行,切记前导限制
        int up=limit?a[pos]:1;
        ll ans=0;
        for(int i=0;i<=up;i++)
        {
            if(lead&&i==0) ans+=dfs(pos-1,sta,1,limit&&i==up);
            else ans+=dfs(pos-1,sta+(i==0?1:-1),0,limit&&i==up);
        }
        if(!limit&&!lead) dp[pos][sta]=ans;//注意只有在没有位数限制并且没有前导限制的时候才记录
        return ans;
    }
    ll solve(ll n)
    {
        int p=0;
        while(n)
        {
            a[p++]=n&1;
            n>>=1;
        }
        return dfs(p-1,35,1,1);
    }
    int main()
    {
        ll n,m;
        memset(dp,-1,sizeof(dp));//以35为0,防止在移位的时候有负数
        cin>>n>>m;
        cout<<solve(m)-solve(n-1)<<endl;
        return 0;
    }
    

      

    当初的梦想实现了吗,事到如今只好放弃吗~
  • 相关阅读:
    Java 常提到的自然序(Natural Ordering)
    设计模式(三)行为模式
    设计模式(二)结构模式
    设计模式(一)建造者模式
    设计模式的概念以及面向对象设计原则
    Java源码 HashMap<K,V>
    mybatis注解使用
    spring整合mybatis
    数据库中的表批量映射为对象
    返回用户提交的图像工具类
  • 原文地址:https://www.cnblogs.com/caijiaming/p/9365077.html
Copyright © 2020-2023  润新知