POJ 3252 组合数
来源:互联网 发布:php注入 编辑:程序博客网 时间:2024/06/11 03:30
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
Source
Round Numbers 就是一个表示成二进制的时候0比1多或者相等的正数,注意是正数,所以0就肯定不是了。
题目是给定一个区间,问在这个区间上的Round Numbers有多少个?
首先是求出小于等于n的Round Numbers有多少个。
比如22 = 10110b 如果要求 <=22的Round Numbers,也就是找出1-22有多少个二进制的0不少于1的数的个数。
22的二进制长度是5.
首先找长度比5小的Round Numbers(长度比5小的数肯定小于22啦)
长度为4的话,第一位必须是1,后面三位的话,可以有2个0,3个0
所以就是C(3,2)+C(3,3);
长度为3的Round Numbers,同理有 C(2,2);//注意不要把第一位1忘记了
长度为2的Round Numbers,有 C(1,1)
长度为1的Round Numbers,有 0个
下面是找长度和22相同的Round Numbers。
首先第一位是1.
22的第二位是0,所以第二位不能为1,必须是0
第三位为0的话,(前面有了2个0,1个1),后面两位可以有1个0,2个0
C(2,1)+C(2,2)
接下来把第三位恢复为1,看第四位。假如第四位是0,(前面有2个0,2个1),后面一位必须是0 C(1,1)
所以大致求的过程就如上面所述。
首先先推个公式,就是长度为len的Round Numbers的个数。
长度为len,第一位肯定是1了。
那么后面剩下 len-1位。
如果len-1是偶数。
那么 C(len-1,(len-1)/2+1)+C(len-1,(len-1)/2+2)+````C(len-1,len-1)
= ( 2^(len-1)-C(len-1,(len-1)/2) )/2;
如果len是奇数
那么就是 ( 2^(len-1) )/2
所以上面求比N长度小的Round Numbers是很好求的了。
至于求长度的,则是逐渐把每一位1变为0,去求后面的,就可以保证比n小了。
#include<stdio.h>#include<math.h>int C[33][33];void init(){ C[0][0]=1; C[1][0]=1; C[1][1]=1; for(int i=2; i<33; i++) { C[i][0]=1; for(int j=1; j<i; j++) C[i][j]=C[i-1][j-1]+C[i-1][j]; C[i][i]=1; }}int s[50];int round(int n)///求小于等于n的 Round Numbers{ if(n<=1) return 0; int len=0; while(n) { s[len++]=n%2; n/=2; } int ans=0; for(int i=len-1; i>0; i--) if((i-1)%2==0) ans+=(pow(2,i-1)-C[i-1][(i-1)/2])/2; else ans+=pow(2,i-1)/2; int cns0=0,cns1=0; for(int i=0; i<len; i++) { if(s[i]==0) cns0++; else cns1++; } if(cns0>=cns1) ///n本身是 Round Number ans++; cns0=0; cns1=1; for(int i=len-2; i>=0; i--) { if(s[i]==1) ///后面有i位,第i位当成0 { for(int j=i; j>=0&&j+cns0+1>=cns1+i-j; j--) ans+=C[i][j]; cns1++; } else cns0++; } return ans;}int main(){ init(); int a,b; while(~scanf("%d%d",&a,&b)) { printf("%d\n",round(b)-round(a-1)); } return 0;}
- POJ 3252 组合数
- poj 3252 (组合数)
- poj 组合数 3252
- POJ 3252Round Numbers 组合数
- POJ 3252(数位DP || 组合数)
- POJ 1942组合数
- POJ 1942 组合数
- poj 3088 组合数学 斯特林数+组合数
- poj 1942 组合数问题
- poj--1306 Combinations(组合数)
- POJ - 1850 Code 组合数
- POJ 2249 Binomial Showdown(组合数)
- poj 1416 组合数(DIY碎纸机)
- POJ 2249 Binomial Showdown (连乘整商求组合数)
- poj 1753 dfs递归组合数
- POJ 2245 Lotto(组合数dfs)
- poj 2249/1942 求组合数
- POJ 1850Code 组合数运用
- 简析TCP中的TIME_WAIT与CLOSE_WAIT状态
- windows(64) install mysql
- Error using xlsread (matlab 读取文件报错)
- Java数组最值,以最小值为例
- Android deep links技术实现
- POJ 3252 组合数
- Axis2 WebService(配置、发布、调用)
- UVA12563 Jin Ge Jin Qu hao
- Android中ListView的几种常见优化方法
- Java 对象、引用和指针
- 阿拉伯数字转为中文汉字(非金额转换) Js分享
- 地址转换NAT
- MSSQL之二 Sql Server中管理库与表
- iOS开发(OC)——文字滚动效果