http://poj.org/problem?id=3252
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).
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
Line 1: Two space-separated integers, respectively Start and Finish.
Output
Line 1: A single integer that is the count of round numbers in the inclusive range Start..Finish
Sample Input
2 12
Sample Output
6
这题的约束就是一个数的二进制中0的数量要不能少于1的数量,通过上一题,这题状态就很简单了,dp[pos][num],到当前数位pos,0的数量减去1的数量不少于num的方案数,一个简单的问题,中间某个pos位上num可能为负数(这不一定是非法的,因为我还没枚举完嘛,只要最终的num>=0才能判合法,中途某个pos就不一定了),这里比较好处理,Hash嘛,最小就-32吧(好像),直接加上32,把32当0用。这题主要是要想讲一下lead的用法,显然我要统计0的数量,前导零是有影响的。至于!lead&&!limit才能dp,都是类似的,自己慢慢体会吧。
const double R=0.5772156649015328606065120900;
const int N=1e5+5;
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double pi=acos(-1.0);
typedef long long ll;
int dp[35][66];
int a[66];
int dfs(int pos,int sta,bool lead,bool limit)
{
if(pos==-1)
return sta>=32;
if(!limit && !lead && dp[pos][sta]!=-1) return dp[pos][sta];
int up=limit?a[pos]:1;
int ans=0;
for(int i=0;i<=up;i++)
{
if(lead && i==0) ans+=dfs(pos-1,sta,lead,limit && i==a[pos]);//有前导零就不统计在内
else ans+=dfs(pos-1,sta+(i==0?1:-1),lead && i==0,limit && i==a[pos]);
}
if(!limit && !lead ) dp[pos][sta]=ans;
return ans;
}
int solve(int x)
{
int pos=0;
while(x)
{
a[pos++]=x&1;
x>>=1;
}
return dfs(pos-1,32,true,true);
}
dp[pos][num(0)-num(1)+32]
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])
#define mem(a,x) memset(a,x,sizeof a)
#define ysk(x) (1<<(x))
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
int bit[35],dp[35][70];
int dfs(int pos,int num,int lead,int limit)
{
if(pos==-1) return num>=32;
if(!lead&&!limit&&~dp[pos][num]) return dp[pos][num];
int ans=0;
int up=limit?bit[pos]:1;
for0(i,up+1)
{
if(lead&&!i ) ans+=dfs(pos-1,num ,lead,limit&&i==bit[pos] );
//如果现在为0,因为之前lead为0,那么lead以后为0。限制方面可能从有限制到无限制。
else ans+=dfs(pos-1,num+ (i?-1:1),lead&&!i ,limit&&i==bit[pos]);
}
if(!lead&&!limit) dp[pos][num]=ans;
return ans;
}
int solve(int x)
{
int nbit=0;
while(x)
{
bit[nbit++]=x&1;
x>>=1;
}
return dfs(nbit-1,32,1,1);
}
https://yymelo.com/poj-3252-round-numbers-%EF%BC%88%E7%BB%84%E5%90%88%E6%95%B0%E5%AD%A6-%EF%BC%89/
给出一个区间, 问在这个区间内有多少个数满足二进制表示下0的数量大于等于1的数量。
给出一个区间, 问在这个区间内有多少个数满足二进制表示下0的数量大于等于1的数量。
求二进制下0的0多于1的情况可以用组合来做,关键在于如何求出区间[n,m]的数呢。
首先,我们可以先求满足[1,m]的结果,然后求[1,n-1],前者减去后者便是结果。
那么再来分析如何求小于m的结果:
1. 二进制位数小于m的所有数,可以直接求组合数。
2. 二进制位数等于m的所有数,从高位向低位枚举,如果当前位为1,那么我们假设当前位为0,然后对后面的若干位求组合数。
扔一个组合公式(dalao请无视):C(n,m)=C(n-1,m-1)+C(n-1,m)