【瞎搞】 UVALive 6527 Counting ones

题目地址

例:

0000

0001

0010

0011

0100

0101

0110

0111

每一位上的个数都是 每 2^i的数 最后2^(i-1)都为 1   

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define FOR(i,a,b)  for(int i=a;i<=b;i++)
#define IN   freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 10052;
const int MAXM = 6000010;
const int INF = 0x3f3f3f3f;
const int mod = 1e9;
const double eps= 1e-8;
#define lson l,m, rt<<1
#define rson m+1,r,rt<<1|1
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    LL  a,b;
    while(cin>>a>>b)
    {
        a--;
        LL sum1=0,sum2=0;
        LL n=a;
        LL i=0;
        while(n)
        {
            i++;
            LL po=(LL)pow(2,i);
            LL po2=(LL)pow(2,i-1);
            LL moo=a%po;
            if(moo>=po/2)
                sum1+=a/po*po2+moo-(po2-1);
            else sum1+=a/po*po2;
            n/=2;
        }
        n=b;
        i=0;
        while(n)
        {
            i++;
            LL po=(LL)pow(2,i);
            LL po2=(LL)pow(2,i-1);
            LL moo=b%po;
            if(moo>=po/2)
                sum2+=b/po*po2+moo-(po2-1);
            else sum2+=b/po*po2;
            n/=2;
        }
        cout<<sum2-sum1<<endl;
    }
    return 0;
}


posted @ 2014-11-01 14:43  kewowlo  阅读(235)  评论(0编辑  收藏  举报