题意:算出区间内二进制中0的个数大于等于1的个数的数字有多少个

/*
本来以为用数位DP搞,但是组合数更简单。
我们设n的二进制长度为len。
①:先考虑长度小于len的数字。
这里以数字22为例,二进制拆成10110,len=5。
len=1时,只能是1(题目要求是正数);
len=2时,第一位是1,剩下的1位,至少有1个0,ans+=C(1,1);
……
len=k时,第一位是1,剩下的len-k位,如果至少要有p个0,那么ans+=C(len-k,p)+...+C(len-k,len-k)。
②:再考虑长度等于len的数字。
第一位是1。
第二位是0,所以第二位不能为1,必须是0。
第三位为0的话,后面两位可以有1个0,2个0,ans+=C(2,1)+C(2,2)。
接下来把第三位恢复为1,看第四位。假如第四位是0,后面一位必须是0,ans+=C(1,1)。
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 51
#define lon long long
using namespace std;
int c[N][N];
void getc(){
for(int i=;i<N;i++)
for(int j=;j<=i;j++){
if(j==||i==j) c[i][j]=;
else c[i][j]=c[i-][j]+c[i-][j-];
}
}
lon solve(lon n){
if(n==) return ;
lon ans=;
int a[N]={},len=;
while(n){
a[++len]=n%;
n>>=;
}
reverse(a+,a+len+);
for(int i=;i<len;i++)
for(int j=(i-)/+;j<i;j++)
ans+=(lon)c[i-][j];
int p0=,p1=;
for(int i=;i<len;i++){
if(!a[i]) {p0++;continue;}
for(int j=len-i;*j+p0+>=p1+len-i;j--)
ans+=(lon)c[len-i][j];
p1++;
}
if(a[len]&&p0+>=p1) ans++;
return ans;
}
int main(){
getc();
lon a,b;
cin>>a>>b;
cout<<solve(b+)-solve(a);
return ;
}
 

Round Numbers(poj 3252)的更多相关文章

  1. Round Numbers (排列组合)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7558   Accepted: 2596 Description The c ...

  2. poj3252 Round Numbers(数位dp)

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

  3. POJ 3252 Round Numbers(数位dp)

    题意:给定区间[l,r],l < r ,求区间中满足条件的正整数的个数:二进制表示下0的个数不少于1的个数. 分析:f(x)表示<=x时满足条件的数的个数,所求问题即为f(r)-f(l-1 ...

  4. Greedy:Fence Repair(POJ 3252)

    Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...

  5. 1120 Friend Numbers (20 分)

    1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...

  6. pat 1100 Mars Numbers(20 分)

    1100 Mars Numbers(20 分) People on Mars count their numbers with base 13: Zero on Earth is called &qu ...

  7. pat 1069 The Black Hole of Numbers(20 分)

    1069 The Black Hole of Numbers(20 分) For any 4-digit integer except the ones with all the digits bei ...

  8. pat 1023 Have Fun with Numbers(20 分)

    1023 Have Fun with Numbers(20 分) Notice that the number 123456789 is a 9-digit number consisting exa ...

  9. pat 1120 Friend Numbers(20 分)

    1120 Friend Numbers(20 分) Two integers are called "friend numbers" if they share the same ...

随机推荐

  1. MVC3 Razor 根据不同页面使用不同Layout

    _ViewStart.cshtml运行于每一Page前, 所以通常在这里先设置Layout   下面代码为特定的controller指定Index,Edit,Create的模板 即Index对应  _ ...

  2. Nginx 502/504 Gateway time-out错误完美解决方案【转发】

      在安装完Nginx+PHP-fpm+Mysql后,跑PHP的应用会经常出现504 Gateway Time-out 或者502 Bad Gateway的情况. Nginx 504 Gateway ...

  3. Android Audio Focus的应用(requestAudioFocus)

    网址:http://blog.csdn.net/dadoneo/article/details/8252933 FROM: http://www.linuxidc.com/Linux/2012-04/ ...

  4. 白话解释IIS并发连接数

    做负载均衡的时候会发现有很多并发数的参数.其中有一个"IIS并发连接数"是我们要关注的. 假设"IIS并发连接数"显示为1000,这并不代表有1000个客户端在 ...

  5. Win7+QTP10.0+IE9无法启动IE的解决方法

    请安装QTP补丁QTP_00626. 更多补丁请查阅:http://www.cnblogs.com/testware/archive/2010/10/13/1849985.html

  6. PAT (Advanced Level) 1061. Dating (20)

    简单模拟. #include<stdio.h> #include<string.h> ],s2[],s3[],s4[]; ][]={"MON ", &quo ...

  7. 读写锁的实现原理(pthread_rwlock_t)

    引言 不同的锁之间的语义是不一样的,没有一劳永逸的锁,只有更适合的锁. 如果是同一进程里的不同线程共享读写锁,那么读写锁变量的维护是在进程内部即可.如果是不同进程共享读写锁,那么读写锁变量的维护是在共 ...

  8. Unexpected exception 'Cannot run program ... error=2, No such file or directory' ... adb'

    Eclipse ADT Unexpected exception 'Cannot run program' up vote 8 down vote favorite 4 I have installe ...

  9. 5个简单的步骤把 WordPress 打造成 CMS

    可能网站的首页一直是一成不变的博客样子,有时候也会挺闷的,个人觉得首页就是应该把博客中最好最重要的内容展现给读者,基于这个想法,我们可以把博客的首页改成一个非常简单的 CMS 首页. 基于 WordP ...

  10. Ant自动构建

    Ant+jenkins+tomcat <project name="buildWar" default="clean"> <property ...