E - Round Numbers(POJ3252)

E - Round Numbers

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 rangeStartFinish

Sample Input

2 12

Sample Output

6

分析 :

有组合数学和数位dp两种方法,组合数学比较简单,这里只说数位dp。

dp能解决大部分区间计数问题,用枚举位数和动规来解决问题
dp[0][pos][k]代表000****中满足0-1>=k的种类数
dp[1][pos][k]代表 (1)****中满足0-1>=k的种类数前缀是1
所以状态三个:
1.前缀是否全为0
2.随便的有几位
3.这几位满足0-1>=k的个数

  1. 当前缀有1时候,枚举第pos位是0是1即可。

d p [ 1 ] [ p o s ] [ k ] = d p [ 1 ] [ p o s − 1 ] [ k − 1 ] + d p [ 1 ] [ p o s ] [ k + 1 ] dp[1][pos][k]=dp[1][pos-1][k-1]+dp[1][pos][k+1] dp[1][pos][k]=dp[1][pos1][k1]+dp[1][pos][k+1]

  1. 当前缀前全位0时,枚举第pos位0,1即可

d p [ 0 ] [ p o s ] [ k ] = d p [ 0 ] [ p o s − 1 ] [ k ] + d p [ 1 ] [ p o s − 1 ] [ k + 1 ] dp[0][pos][k]=dp[0][pos-1][k]+dp[1][pos-1][k+1] dp[0][pos][k]=dp[0][pos1][k]+dp[1][pos1][k+1]

#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<cmath>
#include<vector>
#include<cstring>
#include<string>
#include<iostream>
#include<iomanip>
#define mset(a,b)   memset(a,b,sizeof(a))
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxn=1e5+10;
const int branch=26;
const int inf=0x3f;
const int MOD=2520;
int dp[2][40][70];//dp[0/1]第一维意思:满足后pos位数中0-1>=k的个数中,pos位之前是否全为0。      最高31位
int num[40];
int GetDp(int flag,int pos,int k)//-31<=k<=31
{
    return dp[flag][pos][k+31];
}
void  enDp(int flag,int pos,int k,int val)
{
    dp[flag][pos][k+31]=val;
}
int dfs(int pos,int flag,int k,bool Istop)
{
    if(!pos)
        return k<=0;
    if(!Istop&&GetDp(flag,pos,k)!=-1)
        return GetDp(flag,pos,k);
    int endd=Istop?num[pos]:1;
    int res=0;
    for(int i=0;i<=endd;++i)
    {
        if(i==1)
            res+=dfs(pos-1,1,k+1,Istop&&i==endd);
        else
        {
            if(flag==0)
                    res+=dfs(pos-1,0,0,Istop&&i==endd);
            else
                res+=dfs(pos-1,1,k-1,Istop&&i==endd);
        }
    }
    if(!Istop)
        enDp(flag,pos,k,res);
    return res;
}
int calc(int val)//如果前导全为0,就是0 否则为1
{
    int pos=0;
    do{
        num[++pos]=val%2;
        val/=2;
    }
    while(val);
    return dfs(pos,0,0,1);
}
int main()
{
    mset(dp,-1);
    int a,b;
    while(~scanf("%d %d",&a,&b))
    {
        printf("%d\n",calc(b)-calc(a-1));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值