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 ...
随机推荐
- 百度小程序上传失败 ServerError:30010的原因
最近通过uniapp编译百度智能小程序后上传遇到了报错,错误码为30010. 原因很简单开发者工具和版本库产生了冲突. 两个解决方案,升级开发者工具,降低发布时的版本库 exit;
- c++基础(五)——关联容器
1.关联容器 关联容器中的元素时按照关键字来保存和访问的,与之相对的,顺序容器中的元素时按它们在容器中的位置来顺序保存和访问的.两个主要关联容器是 map 和 set.标准库提供了8个关联容器,这8个 ...
- python 安装pytorch 及 安装失败解决办法
python 安装pytorch 及 安装失败解决办法 [转] pytorch安装失败解决办法 [转] 一分钟在win10终端成功安装pytorch pytorch 的安装方法有2种,一种是pip安装 ...
- mongdb基本使用
mongodb创建用户,设置密码 参考:https://www.jianshu.com/p/237a0c5ad9fa MongoDB内置的数据库角色有: 1. 数据库用户角色:read.readWri ...
- Django模型层之更多操作
Django模型层之更多操作 一 .ORM字段 1.1 常用字段 AutoField int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为 ...
- @FeignClient 情况下header的传递方法,RestTemplate情况下Header传递方法
今天因为要调用另一个服务,因为我们用的是SpringCloud框架,所以通过Fegin调用,正好另一方服务有权限校验,需要传递token和设备ID,这两个参数都需要放到Header中, 用 @Requ ...
- Gym 102055B Balance of the Force
大意: $n$个骑士, 第$i$个骑士若加入光明阵营, 那么能力值$L_i$, 加入黑暗阵营, 能力值$D_i$. 给定$m$个限制$(u_i,v_i)$, 表示$u_i,v_i$不能在同一阵营. 求 ...
- webapi session
webapi中使用session 修改global.cs里面的内容 using System; using System.Web; using System.Web.Routing; using Sy ...
- Java调用WebService方法总结(3)--wsimport调用WebService
wsimport是JDK自带的把WSDL转成Java的工具,可以很方便的生成调用WebService的代码.文中所使用到的软件版本:Java 1.8.0_191. 1.准备 参考Java调用WebSe ...
- 8. :before 和 ::before 区别?【CSS】
单冒号(:)用于CSS3伪类 双冒号(::)用于CSS3伪元素 对于CSS2之前已有的伪元素,比如:before,单冒号和双冒号的写法::before作用是一样的.