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 ...
随机推荐
- easyui_datagrid实现导出Excel
easyui_datagrid实现导出Excel 一.PHPExcel使用方法 先下载PHPExcel类库文件,并引入. 二.利用AJAX实现datagrid导出Excel 原理:前台通过AJAX调用 ...
- win server 检查是否是ntfs文件系统
fsutil fsinfo volumeinfo c: | findstr /i "文件系统名"
- SQL Server 索引优化——无用索引
我们知道,合理的索引能大幅提升性能,但冗余的索引也会降低数据库性能.随着我们业务的发展,数据库的中的表.表结构.查询的内容都有可能发生变化.这样,有的索引就可能不再使用了,需要删除(因为维护索引即浪费 ...
- 关于Java无法解码(ajax编码 Java解码)
今天遇到了一个非常奇~~~~~~葩的问题,无解! 一向前端碰到中文,请求都使用encodeURI(encodeURI("中文"))编码,然后后端使用URLDecoder.decod ...
- 支付宝支付回调方法RSA2验签失败处理方法
支付宝支付签名方式RSA2生成支付时使用的是支付宝公钥和应用私钥, 而不是应用公钥,支付宝公钥的生成是根据上传应用公钥而变动的, 所以在做回调的时候参数ALIPAY_PUBLIC_KEY也需要传支付宝 ...
- c# 异步( Async ) 不是多线程
c# 异步( Async ) 不是多线程 误解 async 在调试 xxxxAsync() 方法的时候,常常会看到调试器界面中会多出一些线程,直觉上误认为 Async 冠名的函数是多线程. 对于 ...
- 博客使用 utterances 作为评论系统
utterances 是一款基于 GitHub issues 的评论工具. 相比同类的工具 gitment.gitalk 以及 disqus 评论工具,优点如下: 极其轻量 加载非常快 配置比较简单 ...
- adb命令获取app布局文件xml
adb shell /system/bin/uiautomator dump --compressed /data/local/tmp/uidump.xml adb pull /data/local/ ...
- js常用的数组去重方法
给出以下数组 var arr1 = new Array('1','2','3','3','5','4','5','6','5','5','88'); 实现对数组的去重 1. 双重for循环去重 Arr ...
- 关于SqlServer数据库数据备份失败的问题
当备份的失败,出现说什么应该支持多少个介质簇,但实际出现了多少介质簇,这个时候就要考虑备份的地址是不是出现问题. 首先,检查备份地址,是不是多于两个以上,那么在备份的时候应该注意,备份地址最好留一个, ...