博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj3252——Round Numbers(数位dp)
阅读量:2344 次
发布时间:2019-05-10

本文共 2607 字,大约阅读时间需要 8 分钟。

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

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 10000005#define Mod 10001using namespace std;int dight[40];long long dp[40][40][40];long long dfs(int pos,bool limit,int zero,int one,int first){ if(pos==0) { if(first) return 1; return zero>=one; } if(!limit&&dp[pos][zero][one]!=-1&&!first) return dp[pos][zero][one]; int end; long long ret=0; if(limit) end=dight[pos]; else end=1; for(int d=0; d<=end; ++d) { if(first) { if(d==1) ret+=dfs(pos-1,limit&&d==end,0,1,0); else ret+=dfs(pos-1,limit&&d==end,0,0,1); } else { if(d==0) ret+=dfs(pos-1,limit&&d==end,zero+1,one,0); else ret+=dfs(pos-1,limit&&d==end,zero,one+1,0); } } if(!limit&&!first) dp[pos][zero][one]=ret; return ret;}long long solve(long long a){ memset(dight,0,sizeof(dight)); int cnt=1; while(a!=0) { dight[cnt++]=a%2; a/=2; } return dfs(cnt-1,1,0,0,1);}int main(){ memset(dp,-1,sizeof(dp)); long long x,y; while(~scanf("%lld%lld",&x,&y)) { printf("%lld\n",solve(y)-solve(x-1)); } return 0;}

转载地址:http://dvcvb.baihongyu.com/

你可能感兴趣的文章
数据全裸时代,你的隐私有多容易获取?
查看>>
分析http代理报错问题
查看>>
Python编程学习笔记 - 列表的各种姿势
查看>>
Python学习教程:Python入门笔记整理
查看>>
天了噜,居然用Python查到了女神的姓名
查看>>
常用排序算法总结
查看>>
Java输入输出
查看>>
MSSQL数据库常见问题
查看>>
Java8 Lambda
查看>>
JAVA面试700问
查看>>
数据库DDL,DML,DCL,TCL
查看>>
各大数据库概述,比较
查看>>
子页面跳转
查看>>
常用算法总结
查看>>
数据库连接池
查看>>
JAVA Webservice
查看>>
Hibernate自动生成实体类
查看>>
Java Memcached
查看>>
JAVA WebSpider
查看>>
XML自动建表/存库
查看>>