POJ-3252 Round Numbers

Round Numbers
原题链接
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15142 Accepted: 6120
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

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
Source

USACO 2006 November Silver

题目大意
寻找A到B之间所有符合转成二进制后,0的个数不小于1的个数的整数的个数。

题解
很容易想到容斥的解法,先求出1~B(符合条件的数字个数),再求出1~A-1,两者的差即为答案。我们设求这个东西的函数为 f,f(c)表示1到c所有符合条件的个数。

对于一个长度不及 c 的二进制数字,除了第一位必须是 1,后面的我们可以随便放(0或1),而题目要求说 0 的个数必须 ≥ 1 的个数,所以我们可以枚举一下 0 的个数,然后利用组合数求出总数(设x=(n+2)/2 整除,二进制数的长度为n+1) Cxn+Cx+1n+Cx+2n+...+Cnn C n x + C n x + 1 + C n x + 2 + . . . + C n n

对于长度等于 c 的二进制数的数,我们要使得排列组合出的数字不大于 c 同时,满足 0 的个数达到一半以上。
我们用 i 从高位枚举到低位(不包括最高位),如果当前这位是 1 ,那么如果另一个二进制数这位是 0(前面一样),那么后面无论怎么样,这个数肯定小于 c,然后枚举 0 的个数累计答案。如果这位是 0 ,那么我们无法判断,但可以确定的是后面随意组合中所需的 0 的个数降低了。(详见代码)

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=2e9+5,maxm=35;
int A,B,a[maxm],c[maxm][maxm];
int ans(int x)
{
    if (!x) return 0;
    int L=0,sum=0,i,j,k;
    for (i=x;i;i>>=1) a[++L]=i&1;a[L+1]=0;
    for (i=2;i<L;i++)
     for (j=i+1>>1;j<i;j++) sum+=c[i-1][j];
    for (i=L-1,j=L+1>>1;i;i--)
    {
        if (!a[i]) {j--;continue;}
        if (i<j) break;//i后面有i-1位可供随意摆放01,如果所需要的0的最少个数
        //也就是j  j≤i-1才会有可行的整数,所以j>i时,肯定无解了。
        for (int k=j-1;k<i;k++) sum+=c[i-1][k];
    }
    return sum;
}
int main()
{
    scanf("%d%d",&A,&B);
    c[0][0]=1;
    for (int i=1;i<=32;i++)
     for (int j=0;j<=i;j++) c[i][j]=c[i-1][j]+c[i-1][j-1];
    printf("%d\n",ans(B+1)-ans(A));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值