您的位置:首页 > 其它

UVA11645:Bits

2015-06-15 15:35 453 查看
题意:

求1~n的二进制中连续出现的两个1有多少个

思路:

首先枚举两个1的位置,如果此时枚举的位置与元素一样,则还要多加

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std;

#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 100005
const LL MOD = 1e13;
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

LL a,b;

void add(LL x)
{
    b+=x;
    a+=b/MOD;
    b%=MOD;
}

int main()
{
    LL cas = 1,n;
    while(scanf("%lld",&n),n>=0)
    {
        LL m = 1,t=n;
        a = b = 0;
        while(n)
        {
            add((n>>2)*m);
            if((n&3)==3) add((t&(m-1))+1);
            m<<=1;
            n>>=1;
        }
        printf("Case %lld: ",cas++);
        if(a)
            printf("%lld%013lld\n",a,b);
        else
            printf("%lld\n",b);
    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: