POJ - 3252 - Round Numbers(数位DP)

时间:2022-03-22 11:41:57

链接:

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)的更多相关文章

  1. poj 3252 Round Numbers&lpar;数位dp 处理前导零&rpar;

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

  2. POJ 3252 Round Numbers&lpar;数位dp&amp&semi;amp&semi;记忆化搜索&rpar;

    题目链接:[kuangbin带你飞]专题十五 数位DP E - Round Numbers 题意 给定区间.求转化为二进制后当中0比1多或相等的数字的个数. 思路 将数字转化为二进制进行数位dp,由于 ...

  3. poj 3252 Round Numbers 数位dp

    题目链接 找一个范围内二进制中0的个数大于等于1的个数的数的数量.基础的数位dp #include<bits/stdc++.h> using namespace std; #define ...

  4. &dollar;POJ&dollar;3252 &dollar;Round&bsol; Numbers&dollar; 数位&dollar;dp&dollar;

    正解:数位$dp$ 解题报告: 传送门$w$ 沉迷写博客,,,不想做题,,,$QAQ$口胡一时爽一直口胡一直爽$QAQ$ 先港下题目大意嗷$QwQ$大概就说,给定区间$[l,r]$,求区间内满足二进制 ...

  5. POJ3252 Round Numbers —— 数位DP

    题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Su ...

  6. Round Numbers&lpar;数位DP&rpar;

    Round Numbers http://poj.org/problem?id=3252 Time Limit: 2000MS   Memory Limit: 65536K Total Submiss ...

  7. POJ 3252 Round Numbers(组合)

    题目链接:http://poj.org/problem?id=3252 题意: 一个数的二进制表示中0的个数大于等于1的个数则称作Round Numbers.求区间[L,R]内的 Round Numb ...

  8. 4-圆数Round Numbers&lpar;数位dp&rpar;

    Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14947   Accepted: 6023 De ...

  9. poj3252 Round Numbers &lpar;数位dp)

    Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...

随机推荐

  1. mysql 用source 导入数据库报错

    平时一直使用phpmyadmin或mysqldum进行导出,使用source命令导入数据库. 但换了新版本mysql后,上述导入方法出现以下错误: ERROR: Unknown command '\\ ...

  2. JDK中的并发bug&quest;

    最近研究Java并发,无意中在JDK8的System.console()方法的源码中翻到了下面的一段代码: private static volatile Console cons = null; / ...

  3. 安装及升级node

    一.mac下安装 1. 可直接在官网下载(http://nodejs.cn/),可使用命令查看版本: node -v node --version 同样npm同时也安装下来,可使用下面命令查看: np ...

  4. phpstorm 8 license key

    Learn Programming===== LICENSE BEGIN =====63758-1204201000000Ryqh0NCC73lpRm!XVcxFChJ2gTUR2lZtlLXrPLb ...

  5. Lua内存泄漏应对方法&lbrack;转&rsqb;

    转自http://blog.csdn.net/xocoder/article/details/42685685 由于目前正在负责的项目是一个二次开发项目,而且留给我们的代码质量实在让人无力吐槽,所以遇 ...

  6. deepin软件中心打不开

    想体验下deepin新特性,就修改了软件源为test版本,结果就坑了,软件中心打不开了...无奈之下,看了下论坛,找到了官方源地址,然后替换. sudo gedit /etc/apt/sources. ...

  7. shell脚本调用C语言之字符串切分之strtok函数

    今天上午在写一个需求,要求的比较急,要求当天完成,我大致分析了一下,可以采用从shell脚本中插入一连串的日期,通过调用proc生成的可执行文件,将日期传入后台数据库,在数据库中进行计算.需要切分日期 ...

  8. python基础-------模块与包(一)

    模块与包 Python中的py文件我们拿来调用的为之模块:主要有内置模块(Python解释器自带),第三方模块(别的开发者开发的),自定义模块. 目前我们学习的是内置模块与第三方模块. 通过impor ...

  9. Oracle数据库查询优化方案(处理上百万级记录如何提高处理查询速度)

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引.2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引 ...

  10. day12 python作业&colon;员工信息表

    作业要求: 周末大作业:实现员工信息表文件存储格式如下:id,name,age,phone,job1,Alex,22,13651054608,IT2,Egon,23,13304320533,Tearc ...