数位dp-POJ-3252-Round Numbers
最近一直在看书和博客,即使做出几道题来也是看别人题解写的,感觉没自己的东西,所以很久没更新博客
看了很多数位dp的题和题解,发现数位dp题是有固定的模版的,并且终于自己做出来一道。
我觉得用记忆化搜索写数位dp很巧妙,而且容易想,且有一定模版,很好用。
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
Output
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;;
}
}
数位dp-POJ-3252-Round Numbers的更多相关文章
- poj 3252 Round Numbers(数位dp 处理前导零)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- POJ 3252 Round Numbers(数位dp&记忆化搜索)
题目链接:[kuangbin带你飞]专题十五 数位DP E - Round Numbers 题意 给定区间.求转化为二进制后当中0比1多或相等的数字的个数. 思路 将数字转化为二进制进行数位dp,由于 ...
- POJ - 3252 - Round Numbers(数位DP)
链接: https://vjudge.net/problem/POJ-3252 题意: The cows, as you know, have no fingers or thumbs and thu ...
- poj 3252 Round Numbers 数位dp
题目链接 找一个范围内二进制中0的个数大于等于1的个数的数的数量.基础的数位dp #include<bits/stdc++.h> using namespace std; #define ...
- $POJ$3252 $Round\ Numbers$ 数位$dp$
正解:数位$dp$ 解题报告: 传送门$w$ 沉迷写博客,,,不想做题,,,$QAQ$口胡一时爽一直口胡一直爽$QAQ$ 先港下题目大意嗷$QwQ$大概就说,给定区间$[l,r]$,求区间内满足二进制 ...
- POJ 3252 Round Numbers(组合)
题目链接:http://poj.org/problem?id=3252 题意: 一个数的二进制表示中0的个数大于等于1的个数则称作Round Numbers.求区间[L,R]内的 Round Numb ...
- POJ 3252 Round Numbers 数学题解
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- POJ 3252 Round Numbers
组合数学...(每做一题都是这么艰难) Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7607 A ...
- [ACM] POJ 3252 Round Numbers (的范围内的二元0数大于或等于1数的数目,组合)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8590 Accepted: 3003 Des ...
- POJ 3252 Round Numbers 组合数学
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13381 Accepted: 5208 Description The ...
随机推荐
- js--题目二
//如何获取下面代码所有元素. <form> <input name="" type="text" /> <input name= ...
- poj---(2886)Who Gets the Most Candies?(线段树+数论)
Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 10373 Acc ...
- IDEA 创建Java Web项目
发现项目目录没有classes和lib目录,所以自己创建 点击OK,选中"Jar Directroy"-->点击"OK" 然后直接把jar复制到这个目录下 ...
- struts2视频学习笔记 28(OGNL表达式)
课时28 OGNL表达式 OGNL是Object Graphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts 2框架使用OGNL作为默认的表达式语 ...
- HookSSDT 通过HookOpenProcess函数阻止暴力枚举进程
首先要知道Ring3层调用OpenProcess的流程 //当Ring3调用OpenProcess //1从自己的模块(.exe)的导入表中取值 //2Ntdll.dll模块的导出表中执行ZwOpen ...
- [Js]基础知识
一.JavaScript组成 1.ECMAScript 解释器.翻译(提供功能有限,如加减乘除,定义变量.函数等) 几乎没有兼容性问题 2.DOM 有一些兼容性问题 3.BOM(用的少,交互 ...
- 用Navicat for MYsql创建表,插入中文显乱码
段都有编码设置.出现乱码肯定是你现在用的编码混乱造成的 解决办法: 第一步 先改数据库编码 先修改你的数据库,如果你页面用的是UTF-8编码那么你数据库内的编码也需要设置为UTF-8,每个字段都需要设 ...
- 在phpmyadmin中执行sql语句出现的错误:Unknown storage engine 'InnoDB'
在phpmyadmin中执行sql语句出现的错误:Unknown storage engine 'InnoDB' 解决方法:解决方法: 1.关闭MySQL数据库 2 ...
- NOIP 2013提高组day 1 T 1转圈游戏 快速幂
描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此类推 ...
- 戴文的Linux内核专题:01介绍
转自Linux中国 译者按:本文作者戴文.科利尔.约翰逊(Devyn Collier Johnson)今年才19岁,但是他在Linux内核.人工智能.编程语言方面拥有丰富的经验,本文是其在linux. ...