Count The Carries
时间限制: | 3000 Ms |
内存限制: | 65535 Kb |
问题描述
One day, Implus gets interested in binary addition and binary carry. He will transfer all decimal digits to binary digits to make the addition. Not as clever as Gauss, to make the addition from a to b, he will add them one by one from a to b in order. For example, from 1 to 3 (decimal digit), he will firstly calculate 01 (1)+10 (2), get 11,then calculate 11+11 (3),lastly 110 (binary digit), we can find that in the total process, only 2 binary carries happen. He wants to find out that quickly. Given a and b in decimal, we transfer into binary digits and use Implus's addition algorithm, how many carries are there?
输入说明
About 100000 cases, end with EOF.
Two integers a, b(0<=a<=b<1000000000).
输出说明
For each case, print one integers representing the number of carries per line.
输入样例
1 2 1 3 1 4 1 6
输出样例
0 2 3 6
来源
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <string.h> 3 #define LL long long 4 5 LL sum[100]; 6 LL solve(int a,int b) { 7 memset(sum,0,sizeof(sum)); 8 9 LL cir=2; 10 for(int i=1; i<=40; i++) { 11 if(2*b<cir) break; 12 sum[i]+=b/cir*(cir/2); 13 int temp=b%cir; 14 if(temp>(cir/2)) 15 sum[i]+=temp-(cir/2); 16 cir*=2; 17 } 18 19 cir=2; 20 for(int i=1; i<=40; i++) { 21 if(2*a<cir) break; 22 sum[i]-=a/cir*(cir/2); 23 int temp=a%cir; 24 if(temp>(cir/2)) 25 sum[i]-=temp-(cir/2); 26 cir*=2; 27 } 28 29 LL ret=0; 30 int pos=1; 31 while(sum[pos]!=0) { 32 ret+=sum[pos]/2; 33 sum[pos+1] += sum[pos]/2; 34 pos++; 35 } 36 return ret; 37 } 38 39 int main() { 40 int a,b; 41 while(scanf("%d%d",&a,&b)!=EOF) { 42 printf("%lld\n",solve(a,b+1)); 43 } 44 45 return 0; 46 }