[codeforces]Goodbye_2015

来源:互联网 发布:日式风格照片 知乎 编辑:程序博客网 时间:2024/06/01 03:57

B. New Year and Old Property

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The year 2015 is almost over.

Limak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system — 201510 = 111110111112. Note that he doesn’t care about the number of zeros in the decimal representation.

Limak chose some interval of years. He is going to count all years from this interval that have exactly one zero in the binary representation. Can you do it faster?

Assume that all positive integers are always written without leading zeros.

Input
The only line of the input contains two integers a and b (1 ≤ a ≤ b ≤ 1018) — the first year and the last year in Limak’s interval respectively.

Output
Print one integer – the number of years Limak will count in his chosen interval.
Sample test(s)
input
5 10
output
2
input
2015 2015
output
1
input
100 105
output
0
input
72057594000000000 72057595000000000
output
26
Note
In the first sample Limak’s interval contains numbers 510 = 1012, 610 = 1102, 710 = 1112, 810 = 10002, 910 = 10012 and 1010 = 10102. Two of them (1012 and 1102) have the described property.
题意:给两个数字a,b,求在区间[a, b]上有多少个其二进制表示的数字中恰好只有1个0的个数。
思路:
求出[1,a]和[1,b]其二进制表示的数字中恰好只有1个0的个数再相减。
假设a用二进制表示有6位数,则用5位二进制数表示只有1个0的有10111, 11011,11101,11110, 4个数字。
则用4位二进制数表示只有1个0的有1011, 1101,1110, 3个数字。
如此递推
求数字a二进制表示的位数count,则count-1位的二进制数有count-2个。
则count-2位的二进制数有count-3个,累加得(count-2)(count-1)/2个。
现在考虑位数为count时的情况,通过观察计算从最高位-1算起,有多少个1连续,都能相应得到当前位为0其他为1并都小于a的情况,(举例,a为111110111,则有101111111,110111111,111011111,111101111,4种情况)并判断a是否满足条件,满足+1,从而得到答案
有另一种简单的方法,看完题解之后发现的,就算出位数之后,用(2^count)-1存储为count位2进制数全部为1的数字,
然后用循环减1,2,4,8,….,2^(count-1),则是count位数的所有恰好一个位为0的数判断是否小于a,小于的话就加1,得出答案

#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <iostream>using namespace std;#define rep(i, l, n) for (int i = l; i < n; i++)long long a, b;int lena = 0, lenb = 0, suma = 0, sumb = 0;int counta[68], countb[68], l = 0, r = 0, flag = 0;int main() {    scanf("%lld%lld", &a, &b);    a--;    long long t = a;    int l = 0, r = 0;    for (long long i = 1; i <= a; i = i*2, lena++) {        counta[l++] = t%2;        flag += 1-(t%2);        t /= 2;    }    t = b;    if (flag == 1)        suma++;    flag = 0;    for (long long i = 1; i <= b; i = i*2, lenb++) {        countb[r++] = t%2;        flag += 1-(t%2);        t /= 2;    }    if (flag == 1)        sumb++;    l--;    r--;    for (int i = l-1; i >= 0; i--) {        if (counta[i] == 1)            suma++;        else            break;    }    for (int i = r-1; i >= 0; i--) {        if (countb[i] == 1)            sumb++;        else            break;    }    if (lena > 1)    suma += (lena-1)*(lena-2)/2;    if (lenb > 1)    sumb += (lenb-1)*(lenb-2)/2;    printf("%d\n", sumb-suma);    return 0;}
0 0