A bit is a binary digit, taking a logical value of either “1” or “0” (also referred to as “true” or “false”respectively). And every decimal number has a binary representation which is actually a series of bits.If a bit of a number is “1” and its next bit is also “1” then we can say that the number has a 1 adjacentbit. And you have to find out how many times this scenario occurs for all numbers up to N.
Examples:
Number Binary Adjacent Bits
12 1100 1
15 1111 3
27 11011 2
Input
For each test case, you are given an integer number (0 ≤ N ≤ ((263)−2)), as described in the statement.The last test case is followed by a negative integer in a line by itself, denoting the end of input file.
Output
For every test case, print a line of the form ‘Case X: Y ’, where X is the serial of output (startingfrom 1) and Y is the cumulative summation of all adjacent bits from 0 to N.
Sample Input
0
6
15
20
21
22
-1
Sample Output
Case 1: 0
Case 2: 2
Case 3: 12
Case 4: 13
Case 5: 13
Case 6: 14
问题链接:UVA11645 Bits。
问题简述:输入正整数n,求0-n中,有多少个连续的11。
问题分析: 数学计算方法有点没搞懂,先占个位置。程序说明:算出的结果超出了long long的范围,用两个long long存储输出。
题记:(略)
AC的C++语言程序如下:
/* UVA11645 Bits */
#include <iostream>
#include <stdio.h>
using namespace std;
const long long MOD = 1e13;
long long a, b;
inline void add(long long x)
{
b += x;
a += b / MOD;
b %= MOD;
}
inline void solve (long long n) {
long long digit = 1, v = n;
a = b = 0;
while(n) {
add((n >> 2) * digit);
if((n & 3) == 3)
add((v & (digit - 1)) + 1);
digit <<= 1;
n >>= 1;
}
if (a)
printf("%lld%013lld\n", a, b);
else
printf("%lld\n", b);
}
int main()
{
long long n;
int caseno = 0;
while (scanf("%lld", &n) !=EOF && n >= 0) {
printf("Case %d: ", ++caseno);
solve(n);
}
return 0;
}