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 ...
随机推荐
- Dictionary 对象
Dictionary 对象 对象的存储数据键/项对. 语法 Scripting.Dictionary 说明 Dictionary对象相当于 PERL 关联数组. 项目,可以是任何形式的数据,存储在数组 ...
- web前端开发分享-css,js入门篇
学习没有捷径,但学习是有技巧与方法. 一,css入门篇: 推荐书籍:css哪些事儿,精通css. 理由:css那些事儿,他是一本介绍css基础类的书,是入门的经典读物. 系统的介绍了css的选 ...
- 桌面输入法评测报告 之 搜狗拼音输入法vs必应拼音输入法
输入法是电脑用户不可或缺的软件,它几乎无时无刻不陪伴在使用者的身旁.一个优秀的输入法,应该满足客户对使用体验以及效率的需求.我们小队的任务便是对当今最为常用的两种输入法: 搜狗拼音输入法和必应拼音输入 ...
- 剑指offer:二叉树的镜像
题目描述: 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / ...
- spring boot 添加整合ssl使得http变成https方法
1. https出现的背景:(1)都知道http传输协议是裸漏的,明文传输的,极易被黑客拦截,因此,(2)人们想出的使用加密,也就是 对称加密 例如aes,不过这个由于因为对称加密需要每个客户端和服务 ...
- Beat版本冲刺(七)
目录 组员情况 组员1:胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:何宇恒 组员11:刘一好 展示组内最新 ...
- 什么是Asp.net Core?和 .net core有什么区别?
为什么要写这篇文章 写这篇文章有两个原因,第一个是因为新站点创建出来后一直空置着,所以写一篇文章放在这里.第二就是因为近来在做一些基于Asp.net core平台的项目开发,也遇到了一些问题,正好趁此 ...
- Yii 框架的Rbac [权限控制]
转载自 xmlife 的博客 : http://blog.csdn.net/xmlife/article/details/50733451 1.首先我们要在配置文件的组件(component)里面配置 ...
- Restful api 防止重复提交
当前很多网站是前后分离的,前端(android,iso,h5)通过restful API 调用 后端服务器,这就存在一个问题,对于创建操作,比如购买某个商品,如果由于某种原因,手抖,控件bug,网络错 ...
- Delphi中如何实现模拟组合按键,如发送Ctrl+F的按键
利用 keybd_event函数可实现,如下面的代码用以实现在一个公共菜单中模拟Ctrl_F按钮以调用DBGridEH的查找对话框功能:这是在一个ActionList中的某一Action的OnExec ...