链接:

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(数位dp 处理前导零)

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

  2. POJ 3252 Round Numbers(数位dp&amp;记忆化搜索)

    题目链接:[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. $POJ$3252 $Round\ Numbers$ 数位$dp$

    正解:数位$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(数位DP)

    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(数位dp)

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

  9. poj3252 Round Numbers (数位dp)

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

随机推荐

  1. react-native-typescript-项目环境搭建

    1.yarn global add create-react-native-app //全局安装 2.create-react-native-app 项目名称 3.yarn add typescrip ...

  2. DSL查询与过滤

    1. 什么是DSL查询 由ES提供丰富且灵活的查询语言叫做DSL查询(Query DSL),它允许你构建更加复杂.强大的查询. DSL(Domain Specific Language特定领域语言)以 ...

  3. 1. RDD概念

    1.1 RDD为什么会产生? RDD 是 Spark 的基石,是实现 Spark 数据处理的核心抽象.那么 RDD 为 什么会产生呢? Hadoop 的 MapReduce 是一种基于数据集的工作模式 ...

  4. Android--Facebook Login with LoginButton

    1.Layout文件添加Facebook的LoginBurtton <com.facebook.widget.LoginButton android:id="@+id/authButt ...

  5. 怎么理解js的原型链继承?

    前言 了解java等面向对象语言的童鞋应该知道.面向对象的三大特性就是:封装,继承,多态. 今天,我们就来聊一聊继承.但是,注意,我们现在说的是js的继承. 在js的es6语法出来之前,我们想实现js ...

  6. [LOJ2290] [THUWC2017] 随机二分图

    题目链接 LOJ:https://loj.ac/problem/2290 洛谷:https://www.luogu.org/problemnew/show/P4547 Solution 首先考虑只有第 ...

  7. Spring Boot异步发送邮件和请求拦截器配置

    用户登录流程图: 在spring拦截器中进行鉴权操作: 控制器的拦截: import com.mooc.house.common.model.User; import org.springframew ...

  8. aspnetcore 容器化部属到阿里云全过程记录

    第一次写博客,作为一个全栈er,记录一下从阿里云到产品运维上线的全过程 一.阿里云上的设置 购买阿里云ECS后: 进控制台查看实例公网IP 在控制台.网络与安全->安全组,配置规则 点击进去可以 ...

  9. 可分离滤波器设计高斯滤波 CUDA程序优化, 实验记录

    环境:RTX2060 ,1920X1080p ,循环10次, kernal_size=8 一 .测试前128个线程拷贝到dst数据的性能  ,只测试行卷积, block=(128+2r)X1 1. 使 ...

  10. 从MVC -> MVVM ? 开发模式

    MVVM 到底是什么? view :由 MVC 中的 view 和 controller 组成,负责 UI 的展示,绑定 viewModel 中的属性,触发 viewModel 中的命令: viewM ...