UVA11645:Bits

来源:互联网 发布:怎样在淘宝客服发图片 编辑:程序博客网 时间:2024/05/02 01:56

题意:

求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 100005const 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;}


1 0
原创粉丝点击