POJ3252 Round Numbers(不重复全排列)
题目问区间有多少个数字的二进制0的个数大于等于1的个数。
用数学方法求出0到n区间的合法个数,然后用类似数位DP的统计思想。
我大概是这么求的,确定前缀的0和1,然后后面就是若干个0和若干个1的不重复全排列数。。
写得挺痛苦的。。另外A[i][j]表示i个0和j个1的不重复全排列数,即A[i][j]=(i+j)!/i!/j!,这个可以从A[i-1][j]或A[i][j-1]求出来,这样就不用担心乘法溢出了。
#include<cstdio>
#include<cstring>
using namespace std;
long long d[][];
long long cnt(int zero,int one,int len){
long long res=;
for(int i=; i<=len; ++i){
int j=len-i;
if(i+zero<j+one) continue;
res+=d[i][j];
}
return res;
}
long long calu(int a){
int len=;
while(len!=- && ((a>>len)&)==) --len;
if(len==-) return ;
long long res=;
for(int i=; i<len; ++i){
res+=cnt(,,i);
}
int zero=,one=;
for(int i=len-; i>=; --i){
if((a>>i)&){
res+=cnt(zero+,one,i);
++one;
}else{
++zero;
}
}
zero=; one=;
for(int i=; i<=len; ++i){
if((a>>i)&) ++one;
else ++zero;
}
return res+(zero>=one);
}
int main(){
d[][]=;
for(int i=; i<; ++i){
for(int j=; j<; ++j){
d[i+][j]=d[i][j]*(i+j+)/(i+);
d[i][j+]=d[i][j]*(i+j+)/(j+);
}
}
int a,b;
while(~scanf("%d%d",&a,&b)){
printf("%lld\n",calu(b)-calu(a-));
}
return ;
}
POJ3252 Round Numbers(不重复全排列)的更多相关文章
- [BZOJ1662][POJ3252]Round Numbers
[POJ3252]Round Numbers 试题描述 The cows, as you know, have no fingers or thumbs and thus are unable to ...
- poj3252 Round Numbers
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7625 Accepted: 2625 Des ...
- POJ3252 Round Numbers —— 数位DP
题目链接:http://poj.org/problem?id=3252 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Su ...
- poj3252 Round Numbers(数位dp)
题目传送门 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16439 Accepted: 6 ...
- poj3252 Round Numbers (数位dp)
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
- POJ3252 Round Numbers 【数位dp】
题目链接 POJ3252 题解 为什么每次写出数位dp都如此兴奋? 因为数位dp太苟了 因为我太弱了 设\(f[i][0|1][cnt1][cnt0]\)表示到二进制第\(i\)位,之前是否达到上界, ...
- poj3252 Round Numbers[数位DP]
地址 拆成2进制位做dp记搜就行了,带一下前导0,将0和1的个数带到状态里面,每种0和1的个数讨论一下,累加即可. WA记录:line29. #include<iostream> #inc ...
- POJ3252 Round Numbers 题解 数位DP
题目大意: 求区间 \([x,y]\) 范围内有多少数的二进制表示中的'0'的个数 \(\ge\) '1'的个数. 解题思路: 使用 数位DP 解决这个问题. 我们设状态 f[pos][num0][n ...
- 题解【POJ3252】Round Numbers
Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, P ...
随机推荐
- mysql启动报错(mac)
$mysql ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) ...
- 解决android:theme="@android:style/Theme.NoDisplay" 加入这句话后程序不能运行
原因: 原来用的是ActionBarActivity,继承自 ActionBarActivity的类必须指定固定的集中Theme风格,而这些 Theme 风格是需要导入V7中的 appcompat L ...
- 【转】推荐一款Java反编译器,比较好用
转自:http://www.blogjava.net/xmatthew/archive/2008/10/28/237203.html 推荐一款Java反编译器,也使用了挺久的了,感觉还是很好用,就拿出 ...
- Android 启动画面
如果你的程序初始化时间过长,那么在初始化之前,程序会现实一个空白的activity页,十分难看. 添加一个启动画面的方法就是为响应的activity加入自定义的Theme,并在theme中设定 and ...
- (转)SQL Server 中WITH (NOLOCK)浅析
概念介绍 开发人员喜欢在SQL脚本中使用WITH(NOLOCK), WITH(NOLOCK)其实是表提示(table_hint)中的一种.它等同于 READUNCOMMITTED . 具体的功能作用如 ...
- (转)SQL Server 中的事务和锁(三)-Range S-U,X-X 以及死锁
在上一篇中忘记了一个细节.Range T-K 到底代表了什么?Range T-K Lock 代表了在 SERIALIZABLE 隔离级别中,为了保护范围内的数据不被并发的事务影响而使用的一类锁模式(避 ...
- 转MYSQL学习(四) 查询
MySQL中select的基本语法形式: select 属性列表 from 表名和视图列表 [where 条件表达式] [group by 属性名[having 条件表达式]] [order by 属 ...
- 转 MYSQL学习(一)
第一期主要是学习MYSQL的基本语法,陆续还有第二.第三.第四期,大家敬请期待o(∩_∩)o 语法的差异 我这里主要说语法的不同 1.默认约束 区别:mysql里面DEFAULT关键字后面是不用加括号 ...
- Java for LeetCode 031 Next Permutation
Next Permutation Total Accepted: 33595 Total Submissions: 134095 Implement next permutation, whi ...
- UVA11806-Cheerleaders(容斥原理+二进制)
In most professional sporting events, cheerleaders play a major role in entertaining the spectators. ...