poj 3252 Round Numbers(数位dp 处理前导零)
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
题意:求s到e中二进制数 0的个数大于1的个数 的数的个数(语文不好见谅..)
思路:dp[i][j][k] i为数位 j为0的个数 k为1的个数 主要就是需要处理前导零 因为数位dp不可避免的要遍历前面全是零的情况 所以需要在递归的时候再加一个参数出现一以后再计算
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int bits[];
ll dp[][][];
ll dfs(int len,int numzero,bool ismax,int numone,bool haveone){
if(!len) return numzero>=numone;
if(!ismax&&dp[len][numzero][numone]>=) return dp[len][numzero][numone];
int up=ismax?bits[len]:;
ll ans=; int i;
for(i=;i<=up;i++){
if(i==&&haveone)
ans+=dfs(len-,numzero+,ismax&&i==up,numone,true);
else if(i==)
ans+=dfs(len-,numzero,ismax&&i==up,numone+,true);
else if(i==&&!haveone)
ans+=dfs(len-,numzero,ismax&&i==up,numone,false);
}
if(!ismax) dp[len][numzero][numone]=ans;
return ans;
}
ll solve(ll n){
int len=;
while(n){
bits[++len]=n%;
n/=;
}
return dfs(len,,true,,false);
}
int main(){
ios::sync_with_stdio(false);
ll s,e;
while(cin>>s>>e){
memset(dp,-,sizeof(dp));
cout<<solve(e)-solve(s-)<<endl;
}
return ;
}
poj 3252 Round Numbers(数位dp 处理前导零)的更多相关文章
- 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]$,求区间内满足二进制 ...
- 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 ...
- 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 ...
- POJ 3252 Round Numbers(组合)
题目链接:http://poj.org/problem?id=3252 题意: 一个数的二进制表示中0的个数大于等于1的个数则称作Round Numbers.求区间[L,R]内的 Round Numb ...
随机推荐
- 关于PHP的那些坑
因为PHP是弱类型语言,常常会发生许多意想不到的问题,所以,我们再次一一细数这些我们踏过的坑!!! 1) foreach中自动回将key为数值的转化成整型,造成无法匹配 function transl ...
- h5-canvas(其他api)
###1.使用图片(需要image对象) drawImage(image,x,y,width,height) 其中image是image或者canvas对象,x和y 是其在目标canvas的起始坐标 ...
- SQL常见问题积累
SQL积累--仅适用于SQL Server 1.sql中,字符串保存序号,按照数字顺序进行排序 ))),) asc --householdNo 为要排序字段 2.控制小数位数 ,),,)))+'%' ...
- Prime Permutation
Prime Permutation 原题地址: http://codeforces.com/problemset/problem/123/A 题目大意: 给你一个字符串(只包含小写字母),从1开始存放 ...
- java随笔4 java中接参整形转字符串
通过+‘’来实现
- C# Note4:XML序列化和反序列化(含加密解密等)
前言 在项目中,我们经常用到各种配置文件,比如xml文件.binary文件等等,这里主要根据实践经验介绍下xml文件的序列化和反序列化(毕竟最常用). 实践背景:我要做一个用户管理功能,用户账号信息存 ...
- rpm和yum
RMP(红帽软件包管理器) RPM有点像Windows系统中的控制面板,会建立统一的数据库文件,详细记录软件信息并能够自动分析依赖关系. YUM(软件仓库)
- 解决mybatis generator警告Cannot obtain primary key information from the database, generated objects may be incomplete
使用 mybatis generator 生成pojo.dao.mapper时 经常出现 Cannot obtain primary key information from the database ...
- 定位linux jdk安装路径
如何在一台Linux服务器上查找JDK的安装路径呢? 有那些方法可以查找定位JDK的安装路径?是否有一些局限性呢? 下面总结了一下如何查找JDK安装路径的方法. 1:echo $JAVA_HOME 使 ...
- poj-1459(网络流-最大流)
题意:给你n个点的电网系统,有一些点是电站,能提供p的电能,有些点是用户,能消耗c的电能,有些是过渡站,不消耗不产生(等于没用),然后m条电线(x,y,w),代表x可以向y运输w的电能,问你这个电网系 ...