2015年3月1日 星期日

[UVA] 12208 - How Many Ones Needed?

#include<bits/stdc++.h>
using namespace std;
typedef long long int LL;
LL cnt(LL k){
    if(k<=0) return 0;
    LL ans=0,tmp=1;
    while(tmp*2<=k){
        ans=ans*2+tmp;
        tmp*=2;
    }
    ans+=cnt(k-tmp)+k-tmp;
    return ans;
}
int main(){
    LL n,m,C=0;
    while(scanf("%lld%lld",&n,&m)!=EOF && n+m){
        printf("Case %lld: %lld\n",++C,cnt(m+1)-cnt(n));
    }
}

沒有留言:

張貼留言