Title Address: http://acm.hdu.edu.cn/showproblem.php?pid=4588
Ideas:
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
10 1010
It is observed that the first bit has (1<<i) a 0, and (1<<i) 1, in the length of (1<< (i+1)) length of the cycle. For the number of n, you can find the number of 1 to n 1 num2[i], the number of carry is sum (Num[i]/2), pay attention to the high carry each time (Num2[i+1]+=num2[i]/2).
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std; typedef long Long LL; ll Num1[65],num2[65];void solve (ll num[],ll N) {for (int i=0;i<63;i++) num[i]=0; n++; for (int i=0;i<63;i++) {LL tmp1= (1LL) << (i+1); LL tmp2= (1LL) <<i; Num[i]+=n/tmp1* (1<<i); if (N%TMP1>TMP2) num[i]+=n%tmp1-tmp2; }/*for (int i=0;i<63;i++) cout<<i<< "" <<num[i]<<endl;*/}int Main () {LL A, B; Solve (num1,20); Solve (num2,6); while (scanf ("%i64d%i64d", &a,&b) ==2) {solve (num1,a-1); Solve (NUM2,B); for (int i=0;i<63;i++) num2[i]-=num1[i]; LL ans=0; for (int i=0;i<63;i++) {ANS+=NUM2[I]/2; NUM2[I+1]+=NUM2[I]/2; } printf ("%i64d\n", ans); } return 0;}
Hdu 4588 Count the carries (regular)