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
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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=50;
ll dp[50][50][50];
int a[maxn];
ll dfs(int pos,int n0,int n1,bool lead,bool limit)
{
if(pos==0)
{
if(lead) return 1;
return n0>=n1;
}
if(!limit&&!lead&&dp[pos][n0][n1]!=-1) return dp[pos][n0][n1];
int up=limit?a[pos]:1;
ll ans=0;
for(int i=0;i<=up;i++)
{
if(lead)
{
if(i) ans+=dfs(pos-1,n0,n1+1,0,limit&&i==a[pos]);
else ans+=dfs(pos-1,0,0,1,limit&&i==a[pos]);
}
else
{
if(i) ans+=dfs(pos-1,n0,n1+1,0,limit&&i==a[pos]);
else ans+=dfs(pos-1,n0+1,n1,0,limit&&i==a[pos]);
}
}
if(!limit&&!lead) dp[pos][n0][n1]=ans;
return ans;
}
ll solve(ll x)
{
int pos=0;
while(x)
{
a[++pos]=x%2;
x/=2;
}
return dfs(pos,0,0,1,1);
}
int main()
{
ll l,r;
int t;
while(~scanf("%lld%lld",&l,&r)&&(l+r))
{
memset(dp,-1,sizeof(dp));
printf("%lld\n",solve(r)-solve(l-1));
}
}
在没有手指的情况下,牛群采用了一种独特的决策方式:通过选择一个二进制表示中零的数量不少于一的数量的整数来进行比赛。这种游戏称为“圆数”匹配,胜利者将在给定范围内找到最多“圆数”的牛。
9266

被折叠的 条评论
为什么被折叠?



