POJ 3252 Round Number(数位DP)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 6983 | Accepted: 2384 |
Description
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).
Input
Output
Sample Input
2 12
Sample Output
6 题意
求二进制表示中0的个数大于1的数的个数。 分析
简单数位DP,用记忆化搜索
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set> #define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
const int N = 1e6+;
//const int MAXN = 210;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T;
void testcase(){
printf("Case #%d: ",++T);
}
const int MAXN = 1e5+;
const int MAXM = ;
int dp[][][];
int bit[];
//num0-0的个数
//num1-1的个数
//limit-边界标志
//z-前缀零标志
int dfs(int pos,int num0,int num1,bool limit,bool z){
if(pos==-) return num0>=num1;
if(!limit&&dp[pos][num0][num1]!=-) return dp[pos][num0][num1];
int ed = limit?bit[pos]:;
int ans=;
for(int i=;i<=ed;i++){
//(z&&i==0)判断是否前面全为零
ans += dfs(pos-,(z&&i==)?:num0+(i==),(z&&i==)?:num1+(i==),limit&&i==ed,z&&i==);
}
if(!limit) dp[pos][num0][num1]=ans;
return ans;
} int solve(int x){
int xx=x;
int tot=;
while(xx){
bit[tot++]=xx&;
xx>>=;
}
return dfs(tot-,,,,);
}
int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL // init();
int l,r;
scdd(l,r);
mset(dp,-);
cout<<solve(r)-solve(l-)<<endl;
return ;
}
POJ 3252 Round Number(数位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)
链接: https://vjudge.net/problem/POJ-3252 题意: The cows, as you know, have no fingers or thumbs and thu ...
- 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]$,求区间内满足二进制 ...
- 多校5 HDU5787 K-wolf Number 数位DP
// 多校5 HDU5787 K-wolf Number 数位DP // dp[pos][a][b][c][d][f] 当前在pos,前四个数分别是a b c d // f 用作标记,当现在枚举的数小 ...
- POJ Round Numbers(数位DP)
题目大意: Round Number: 将一个整数转化为二进制数字后,(不含前导0) 要是0的个数 大于等于1的个数 则是 Round Number 问从L-R之中有多少个Round Number ...
- 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 ...
随机推荐
- c语言连接mysql数据库的实现方法
C语言连接mysql数据库,需要相应的头文件和lib文件,如果你安装Mysql数据库,会在安装目录下找到这些库文件,如果没有安装,也可以在网上找到 我这里也有一份网上找到的:/201205/other ...
- .Net Core 分布式微服务框架 - Jimu 添加 Swagger 支持
系列文章 .Net Core 分布式微服务框架介绍 - Jimu .Net Core 分布式微服务框架 - Jimu 添加 Swagger 支持 一.前言 最近有空就优化 Jimu (一个基于.Net ...
- ANSYS渡槽槽身动水压力的施加(2)——U型渡槽
U型渡槽动水压力荷载施加命令及说明 程序中需要用到ANSYS重启动,因为需提取前一步加速度结果以施加部分动水压力: 默认Y方向为重力方向,X方向为横槽向,Z方向为纵槽向: 需准备地震波文件: 需先将槽 ...
- Unity之日志管理
1. 目录结构 1. Plugins --> 存放Log4Net动态库文件 2. Scripts --> 存放写日志的脚本 3. StreamingAssets -->存放Log4N ...
- 关于OBS获取显示器黑屏的解决办法
近来看到许多人说OBS获取显示器源的时候黑屏,下面介绍下相关处理办法. 第一种,先尝试把OBS程序的兼容性设置成Win 7和管理员身份,具体操作: 设置成这样,如果能够获取到显示器,那么问题解决,否则 ...
- team330团队铁大兼职网站使用说明
项目名称:铁大兼职网站 项目形式:网站 网站链接:http://39.106.30.16:8080/zhaopinweb/mainpage.jsp 开发团队:team330 网站上线时间:2018年1 ...
- web框架-Struts开始
问题: 为什么有structs 作为一种框架(frameset)可以与传统的mvc进行比较? MVC是一种模式数据处理.显示和数据输入分开,来规范开发,但是却又并不规范.可以这样想:有三家公司,他们对 ...
- 浅谈FPGA
浅谈FPGA 前言 生活中永远都不会缺少「 为什么 」,于最近就被合胜学长了,问了一个看似简单却又极具意义的问题,为什么需要FPGA?FPGA与单片机的区别是什么?瞬间刷新了我入门三天FPGA的冲击感 ...
- python数据分析Titanic_Survived预测
import pandas as pd import matplotlib.pyplot as plt # matplotlib画图注释中文需要设置from matplotlib.font_manag ...
- Week3_代码复审
软件工程师的成长 一口气看完了十多篇的博客,心里的感觉五味陈杂.既有对未来道路的憧憬,也有对自己目前水平的无力感,与那些在这个领域打拼十几年甚至几十年的前辈相比,我不过也就是刚刚迈过行业门槛一条腿而已 ...