zoukankan      html  css  js  c++  java
  • Good Bye 2015 B. New Year and Old Property 计数问题

    B. New Year and Old Property
     

    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 and1010 = 10102. Two of them (1012 and 1102) have the described property.

     题意:  算出 a,b 中 二进制下,含有 且 只含有1个0的数 的数目

    题解:   对于  10110010  枚举每一位为0所带来的答案就好了

      或者你可以数位dp

    //meek
    #include<bits/stdc++.h>
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include<map>
    using namespace std ;
    typedef long long ll;
    #define mem(a) memset(a,0,sizeof(a))
    #define pb push_back
    #define fi first
    #define se second
    #define MP make_pair
    
    const int N=400+100;
    const ll INF = 1ll<<61;
    const int inf = 1000000007;
    const int MOD= 1000007;
    
    int d[N];
    int sum[N];
    ll solve(ll n) {
       if(n<0) return 0;
       ll tmp = n;
       int len = 0;   mem(sum);
       while(tmp)  d[++len]= tmp%2,tmp/=2;
       for(int i=len;i>=1;i--) sum[i] = sum[i+1] +d[i];
    
       ll ans = 0;
       int f= 0;
       for(int i=1;i<=len;i++) {
        if((!f||d[i])&&sum[i+1]==len-i) {
            ans+=(len-i);//cout<<ans<<endl;
        }
        else ans+=(len-i==0?0:(len-i-1));
         if(d[i] == 0)f=1;
        // cout<<ans<<endl;
       }
       return ans;
    }
    int main() {
        ll a,b;
         scanf("%lld%lld",&a,&b);
         printf("%lld
    ",solve(b)-solve(a-1));
       //  printf("%lld
    ",);
        return 0;
    }
    代码
  • 相关阅读:
    c++ STL
    unix network programming(3rd)Vol.1 [第1章]《读书笔记系列》
    [面试题] BloomFilter 无序40亿不重复 uint 整数, 给予任意的数,求是否在这40亿之中 + 无序数组中找2个相同的值
    stm32 DAC输出音频
    Go语言项目的错误和异常管理 via 达达
    值传递
    FireFox、chrome通过插件使用IE内核,IE Tab v2
    linux cross toolsChain 交叉编译 ARM(转)
    rfc all download
    VS2005 工程在win7下使用管理员权限运行
  • 原文地址:https://www.cnblogs.com/zxhl/p/5092838.html
Copyright © 2011-2022 走看看