POJ - 3252 - Round Numbers(数位DP)
链接:
https://vjudge.net/problem/POJ-3252
题意:
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).
思路:
转为二进制表示去DP,注意前缀0的处理。前缀0要减少有效长度。
代码:
// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;
LL F[40][40][40];
LL dig[40];
LL a, b;
LL Dfs(int pos, int cnt, bool zer, bool lim, int len)
{
if (pos == -1)
return cnt >= (len+1)/2;
if (!lim && F[pos][len][cnt] != -1)
return F[pos][len][cnt];
int up = lim ? dig[pos]: 1;
LL sum = 0;
for (int i = 0;i <= up;i++)
{
if (i == 0 && !zer)
sum += Dfs(pos-1, cnt+1, false, lim && i == up, len);
else if (i == 0 && zer)
sum += Dfs(pos-1, cnt, true, lim && i == up, len-1);
else
sum += Dfs(pos-1, cnt, false, lim && i == up, len);
}
if (!lim)
F[pos][len][cnt] = sum;
return sum;
}
LL Solve(LL x)
{
int p = 0;
while(x)
{
dig[p++] = x%2;
x /= 2;
}
return Dfs(p-1, 0, true, true, p);
}
int main()
{
// freopen("test.in", "r", stdin);
memset(F, -1, sizeof(F));
while(~scanf("%lld%lld", &a, &b))
{
printf("%lld\n", Solve(b)-Solve(a-1));
}
return 0;
}
POJ - 3252 - Round Numbers(数位DP)的更多相关文章
- 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
题目链接 找一个范围内二进制中0的个数大于等于1的个数的数的数量.基础的数位dp #include<bits/stdc++.h> using namespace std; #define ...
- $POJ$3252 $Round\ Numbers$ 数位$dp$
正解:数位$dp$ 解题报告: 传送门$w$ 沉迷写博客,,,不想做题,,,$QAQ$口胡一时爽一直口胡一直爽$QAQ$ 先港下题目大意嗷$QwQ$大概就说,给定区间$[l,r]$,求区间内满足二进制 ...
- POJ3252 Round Numbers —— 数位DP
题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Su ...
- Round Numbers(数位DP)
Round Numbers http://poj.org/problem?id=3252 Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- POJ 3252 Round Numbers(组合)
题目链接:http://poj.org/problem?id=3252 题意: 一个数的二进制表示中0的个数大于等于1的个数则称作Round Numbers.求区间[L,R]内的 Round Numb ...
- 4-圆数Round Numbers(数位dp)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14947 Accepted: 6023 De ...
- poj3252 Round Numbers (数位dp)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
随机推荐
- [转帖]Merkle树
Merkle树 https://www.jianshu.com/p/fc439a8fd0de 所谓比特币交易就是从一个比特币钱包向另一个中转账,每笔交易都有数字签名来保证安全.一个交易一旦发生那么就是 ...
- Python基础学习:字符串认知与应用
一.len() 家电维修 len:全写是length,是计算容量的函数:例如a="1234",len(a)=4 如果是中文字符,比如a="哈",len(a)= ...
- day36——死锁、递归锁、信号量、GIL、多线程实现socket通信、线程池和进程池
day36 死锁现象与递归锁 死锁现象 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这 ...
- centos7简单部署rancher
rancher官网文档地址 https://www.cnrancher.com/docs/rancher/v2.x/cn/overview/ 准备机器 两台虚拟机 192.168.56.100 192 ...
- UI单据字段值查看方式
1.单据界面右键属性,获取当前单据URL连接:http://172.16.168.12/U9/erp/display.aspx?lnk=SCM.INV.INV2020_10&sId=3017n ...
- centos7+ 在线yum安装docker-ce
yum install -y yum-utils //扩展yum功能 yum-config-manager --add-repo http://mirrors.aliyun.com/docker- ...
- Java多线程编程之读写锁【ReentrantReadWriteLock】
有时候我们需要有这样的需求: 对于同一个文件进行读和写操作,普通的锁是互斥的,这样读的时候会加锁,只能单线程的读,我们希望多线程的进行读操作,并且读的时候不能进行写操作,写的时候不能进行 ...
- WPF 的 Application.Current.Dispatcher 中,Dispatcher 属性一定不会为 null
原文:WPF 的 Application.Current.Dispatcher 中,Dispatcher 属性一定不会为 null 在 WPF 程序中,可能会存在 Application.Curren ...
- BUAA OO 2019 第四单元作业总结
目录 第四单元总结 总 UML UML 类图 UML 时序图 UML 状态图 架构设计 第十三次作业 第十四次作业 课程总结 历次作业总结 架构设计 面向对象方法理解 测试方法理解与实践 改进建议 尽 ...
- 论PM与团队与敏捷开发
敏捷开发是每个有追求的PM都会去读的书 敏捷开发是很少程序会去读的书 敏捷开发是团体其他人很少会读的书 然而, 据我的 所见, 所闻, 所论 敏捷开发在大家的脑袋里分为很多种版本 既有可以一辩的新鲜思 ...