zoukankan      html  css  js  c++  java
  • UVA11645 Bits【位运算+大数】

    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
    ", a, b);
        else
            printf("%lld
    ", b);
    }
    
    int main()
    {
        long long n;
        int caseno = 0;
    
        while (scanf("%lld", &n) !=EOF && n >= 0) {
            printf("Case %d: ", ++caseno);
            solve(n);
        }
    
        return 0;
    }



  • 相关阅读:
    linux的软连接和硬连接
    各种Python简单功能代码
    《财报就像一本故事书》刘顺仁(二) ——财务报表
    Atitit .h5文件上传 v3
    Atitti. 语法树AST、后缀表达式、DAG、三地址代码
    Atitit.在线充值功能的设计
    Atitit。数据库 安全性 重要敏感数据加密存储解决方案
    atitit.数据验证db数据库数据验证约束
    Atitit.提升电子商务安全性 在线充值功能安全方面的设计
    Atitit.antlr实现词法分析
  • 原文地址:https://www.cnblogs.com/tigerisland/p/7563618.html
Copyright © 2011-2022 走看看