Ultraviolet A-11645 bits

Source: Internet
Author: User

Description

Problem J
Bits
Input:
Standard Input

Output:Standard output

 

ABitIs 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 it's next bit is also "1" then we can say that the number has a 1 adjacent bit. and you have to find out how many times this scenario occurs for all numbers upN.

 

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 <= (2 ^ 63)-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 (starting from 1) and y is the cumulative summation of all adjacent bits from 0 to n.

Sample input output for sample input

0

6

15

20

21

22

-1

Case 1: 0

Case 2: 2

Case 3: 12

Case 4: 13

Case 5: 13

Case 6: 14

Calculate the total number of two consecutive 1 numbers for each number between 0 and N.

The idea is the same as the idea. It is also the place where two 1 values are enumerated. Note that when the location of 11 is the same as that of the original number, you must consider the situation of + 1 or + 2, and use two large numbers to store the results.

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;long long aa,bb;void cal(long long n) {bb += n;if (bb >= (1000000000000ll)) {aa += bb / (1000000000000ll);bb %= (1000000000000ll);}}int main() {int cas = 1;long long n;long long a,b,c,m;while (cin >> n && n >= 0) {aa = bb = 0;m = 1, a = n;for (int i = 0; i < 62; i++) {cal((n>>2)*m);if ((n & 3) == 3)cal((a&((1ll<<i)-1))+1);m <<= 1;n >>= 1;}printf("Case %d: ", cas++);if(aa) {cout << aa;printf("%012lld\n",bb);}else cout<<bb<<endl;}return 0;}




Ultraviolet A-11645 bits

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.