Round Numbers(poj 3252)
题意:算出区间内二进制中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)的更多相关文章
- Round Numbers (排列组合)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7558 Accepted: 2596 Description The c ...
- poj3252 Round Numbers(数位dp)
题目传送门 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16439 Accepted: 6 ...
- POJ 3252 Round Numbers(数位dp)
题意:给定区间[l,r],l < r ,求区间中满足条件的正整数的个数:二进制表示下0的个数不少于1的个数. 分析:f(x)表示<=x时满足条件的数的个数,所求问题即为f(r)-f(l-1 ...
- Greedy:Fence Repair(POJ 3252)
Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...
- 1120 Friend Numbers (20 分)
1120 Friend Numbers (20 分) Two integers are called "friend numbers" if they share the same ...
- pat 1100 Mars Numbers(20 分)
1100 Mars Numbers(20 分) People on Mars count their numbers with base 13: Zero on Earth is called &qu ...
- 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 ...
- 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 ...
- pat 1120 Friend Numbers(20 分)
1120 Friend Numbers(20 分) Two integers are called "friend numbers" if they share the same ...
随机推荐
- JSP标准标签库(JSTL)--SQL标签库 sql
了解即可.SQL标签库 No. 功能分类 标签名称 描述 1 数据源标签 <sql:setDataSource> 设置要使用的数据源名称 2 数据库操作标签 <sql:query&g ...
- di
dependency inversion依赖反转和dependency inversion依赖注入 di反转指的是不依赖于具体的实现而是依赖于抽象的接口,那么运行应用的main方法里如果创造具体实现的 ...
- c++ 显示调用dll
首先需要引入:#include<windows.h> 否则会出现 HINSTANCE 未定义的错误
- int main(int argc,char *argv[])参数的应用
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/sta ...
- iOS开发中控制器切换方式Modal
简介 在iPhone开发中 Modal是一种常见的切换控制器的方式 默认是从屏幕底部往上弹出,直到完全盖住后面的内容为止 在iPad开发中 Modal的使用频率也是非常高的 对比iPhone开发,Mo ...
- JSON对象和字符串转换
- 关于源码编译每次提示有错误 要make update-api
最近编译newline的版本的时候..同事修改了andoid默认输入法为百度.这是系统自动提供的API,所以每次编译会提示 此时在编译源码生成SDK的过程中会出现这个问题:************** ...
- DHCP详解
概述 DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络服务供应商自动分 ...
- jar2exe 配置jre
http://www.regexlab.com/zh/jar2exe/support.htm
- js 如何动态添加数组_百度知道
1.数组的创建var arrayObj = new Array(); //创建一个数组var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长 ...