Poj3252--round Numbers (Combination chapter 1 to N of the number of binary numbers in the number of 0 is greater than 1)

Source: Internet
Author: User

Round Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9525 Accepted: 3420


The cows, as you know, has no fingers or thumbs and thus is 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 has thus resorted to "round number" matching. The first cow picks an integer less than and the billion. The second cow does the same. If The numbers is 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 ofN have as many or more Zeroes than it has ones. For example, the integer 9, when written in binary form, is 1001. 1001 has zeroes and ones; Thus, 9 is a round number. The integer is 11010 in binary; Since it has both 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 and to determine. Bessie wants to cheat and thinks she can doing if she knows how many "round numbers" is in a given range.

Help she by writing a program that tells how many round numbers appear in the inclusive range given by the input (1≤S Tart < Finish ≤2,000,000,000).


Line 1:two space-separated integers, respectively Startand Finish.


Line 1: A single integer So is the count of round numbers in the inclusive range Start.. Finish

Sample Input

2 12

Sample Output



Usaco 2006 November Silver

Test instructions: Gives the number of round from N to M in N to M, and the number of 0 after conversion to binary is greater than or equal to 1 to the number.

Convert to count 1 to n,1 to M, then subtract

Convert a number x to a binary number, then the highest bit of this number must be 1, the number of record bits cnt,

1, from the low start to cnt-1, calculate the first bit of the 1,i bit before all the number of 0 o'clock.

2, the calculation of the first CNT bit, from the high to low traverse, when the first bit of 1 o'clock, the cumulative number of the bit is 0 o'clock.

#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define LL __int64ll c[33    ][33]; int digit[40], cnt; void Get_c () {LL I, J;    Memset (C,0,sizeof (c));    C[0][0] = 1;        for (i = 1; i <; i++) {c[i][0] = 1;        for (j = 1; j < I; j + +) C[i][j] = C[i-1][j-1] + c[i-1][j];    C[I][J] = 1; } return;    ll solve (ll temp) {ll ans = 0, I, J, NUM0, num1, k = 0;    memset (digit,0,sizeof (digit));    CNT = 0;        while (temp) {digit[++cnt] = temp%2;        if (k = = 0 && digit[cnt] = = 1) k = CNT;    Temp/= 2;        } for (i = 1; i < cnt; i++) {//current bit put 1 NUM0 = 0;        NUM1 = 1;            for (j = 0; J < i; J + +) {if (Num1+j > (i-1-j) +NUM0) break;        Ans + = c[i-1][j];    }} NUM0 = 0;    NUM1 = 1;         for (j = i-1; j > 0; j--) {if (digit[j] = = 0)//If this is 0 {   num0++;        Continue;        }//This is 1, first add 0, then put 1 num0++;            for (k = 0, k < J; k++) {if (Num1+k > (j-1-k) +NUM0) break;        Ans + = c[j-1][k];        } num0--;    num1++;    } if (NUM0 >= num1) ans++; return ans;}    int main () {LL n, m;    Get_c ();    while (scanf ("%i64d%i64d", &n, &m)! = EOF) {printf ("%i64d\n", Solve (m)-solve (n-1)); } return 0;}

Poj3252--round Numbers (Combination chapter 1 to N of the number of binary numbers in the number of 0 is greater than 1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.