这题不是用10进制储存的,要转化成2进制再计算

dp[i][j][k]   i是位数,j是1的个数,k是0的个数

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; int dp[N][N][N],digit[N];
int dfs(int len,int one,int zero,bool fi,bool fp)
{
if(!len)
{
if(fi)return ;
return zero>=one;
}
if(!fp&&!fi&&dp[len][one][zero]!=-)return dp[len][one][zero];
int ans=,fpmax=fp ? digit[len] : ;
for(int i=;i<=fpmax;i++)
{
if(fi)
{
if(i==)ans+=dfs(len-,,,fi,fp&&i==fpmax);
else ans+=dfs(len-,one+,zero,fi&,fp&&i==fpmax);
}
else
{
if(i==)ans+=dfs(len-,one,zero+,fi&,fp&&i==fpmax);
else ans+=dfs(len-,one+,zero,fi&,fp&&i==fpmax);
}
}
if(!fp&&!fi)dp[len][one][zero]=ans;
return ans;
}
ll solve(ll x)
{
int len=;
while(x)
{
digit[++len]=x&;
x/=;
}
return dfs(len,,,,);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int a,b;
memset(dp,-,sizeof dp);
cin>>a>>b;
cout<<solve(b)-solve(a-)<<endl;
return ;
}
/******************** ********************/

写法1

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; int dp[N][N][N],digit[N];
int dfs(int len,int one,int zero,bool fi,bool fp)
{
if(!len)
{
return !fi&&zero>=one;
}
if(!fp&&!fi&&dp[len][one][zero]!=-)return dp[len][one][zero];
int ans=,fpmax=fp ? digit[len] : ;
for(int i=;i<=fpmax;i++)
{
if(fi)
{
if(i==)ans+=dfs(len-,,,fi,fp&&i==fpmax);
else ans+=dfs(len-,one+,zero,fi&,fp&&i==fpmax);
}
else
{
if(i==)ans+=dfs(len-,one,zero+,fi&,fp&&i==fpmax);
else ans+=dfs(len-,one+,zero,fi&,fp&&i==fpmax);
}
}
if(!fp&&!fi)dp[len][one][zero]=ans;
return ans;
}
ll solve(ll x)
{
int len=;
while(x)
{
digit[++len]=x&;
x/=;
}
return dfs(len,,,,);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int a,b;
memset(dp,-,sizeof dp);
cin>>a>>b;
cout<<solve(b)-solve(a-)<<endl;
return ;
}
/******************** ********************/

写法2

poj3252 数位dp的更多相关文章

  1. [poj3252]Round Numbers_数位dp

    Round Numbers poj3252 题目大意:求一段区间内Round Numbers的个数. 注释:如果一个数的二进制表示中0的个数不少于1的个数,我们就说这个数是Round Number.给 ...

  2. POJ3252 Round Numbers 【数位dp】

    题目链接 POJ3252 题解 为什么每次写出数位dp都如此兴奋? 因为数位dp太苟了 因为我太弱了 设\(f[i][0|1][cnt1][cnt0]\)表示到二进制第\(i\)位,之前是否达到上界, ...

  3. poj3252(数位dp)(模板)

    题目链接:https://vjudge.net/problem/POJ-3252 题意:求[l,r]之间的Round Number数,RN数即化为二进制后0的个数不少于1的个数的数. 思路:之前用组合 ...

  4. 【poj3252】 Round Numbers (数位DP+记忆化DFS)

    题目大意:给你一个区间$[l,r]$,求在该区间内有多少整数在二进制下$0$的数量$≥1$的数量.数据范围$1≤l,r≤2*10^{9}$. 第一次用记忆化dfs写数位dp,感觉神清气爽~(原谅我这个 ...

  5. POJ3252 Round Numbers —— 数位DP

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

  6. poj3252 Round Numbers(数位dp)

    题目传送门 Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16439   Accepted: 6 ...

  7. POJ3252 Round Numbers 题解 数位DP

    题目大意: 求区间 \([x,y]\) 范围内有多少数的二进制表示中的'0'的个数 \(\ge\) '1'的个数. 解题思路: 使用 数位DP 解决这个问题. 我们设状态 f[pos][num0][n ...

  8. 浅谈数位DP

    在了解数位dp之前,先来看一个问题: 例1.求a~b中不包含49的数的个数. 0 < a.b < 2*10^9 注意到n的数据范围非常大,暴力求解是不可能的,考虑dp,如果直接记录下数字, ...

  9. 专题训练之数位DP

    推荐以下一篇博客:https://blog.csdn.net/wust_zzwh/article/details/52100392 1.(HDOJ2089)http://acm.hdu.edu.cn/ ...

随机推荐

  1. 控制台程序的中文输出乱码问题(export LC_CTYPE=zh_CN.GBK,或者修改/etc/sysconfig/i18n为zh_CN.GBK。使用setlocale(LC_CTYPE, "");会使用默认办法。编译器会将源码做转换成Unicode格式,或者指定gcc的输入文件的编码参数-finput-charset=GBK。Linux下应该用wprintf(L"%ls/n",wstr))

    今天发现用securecrt登陆时,gcc编译出错时会出现乱码,但直接在主机的窗口界面下用Shell编译却没有乱码.查看了一下当时的错误描述,发现它的引号是中文引号,导致在SecureCRT中显示出错 ...

  2. hive 安装警告 WARN conf.HiveConf: HiveConf of name hive.metastore.local does not exist

    解决方法: 在0.10  0.11或者之后的HIVE版本 hive.metastore.local 属性不再使用. 在配置文件里面:  <property>  <name>hi ...

  3. PyNest——part 2: populations of neurons

    part 2: populations of neurons introduction 在这篇讲义中,我们着眼于创建和参数化神经元批次,并将它们连接起来. 当你完成这些材料时,你会知道如何: 创建具有 ...

  4. PyQt4 颜色选择,字体选择代码

    # -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...

  5. Python之 Django 初级

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  6. iOS 认识runtime 中的三个指针 isa , IMP , SEL

    runtime中函数调用经常被提及的三个概念 isa,IMP,SEL 一  isa:是类指针,之所以说isa是指针是因为Class其实是一个指向objc_class结构体的指针,而isa 是它唯一的私 ...

  7. 【leetcode刷题笔记】Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  8. 关于pycharm中的requirements.txt文件

    作用:记录所有所依赖的第三方模块,方便迁移到不同的环境中后,防止缺少模块,或因为所依赖的第三方模块版本不同引起不必要的问题 生成命令:pip freeze > requirements.txt ...

  9. jq .attr()和.prop()方法的区别

    今天在nodejs交流群里面遇到别人在里面说面试的时候遇到了这个问题,没回答出来,面试官讲的他也不明白,这个问题看着很简单,但是往深的解释就很难了. 对于HTML元素本身就带有的固有属性,在处理时,使 ...

  10. 使用bedtools的一个问题

    问题:有两个平行测序样本,分别得到1.vcf和2.vcf两个文件,想知道这两个文件有多少个重合点. [wangjq@mgmt CHG029194]$ cat t1 chr1 10 10 chr1 11 ...