Last active
August 29, 2015 14:22
-
-
Save zsrinivas/2a89c52b3bef33edb85a to your computer and use it in GitHub Desktop.
spoj ⟿ classical ⟿ treenum2 ⟿ The art of tree numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#ifdef __mr__ | |
#include "prettyprint.hpp" | |
#endif | |
#define endl ('\n') | |
using namespace std; | |
int testcases; | |
inline unsigned int tnum(unsigned long long int n) { | |
unsigned long long int nth = 0, p2x = 1; | |
unsigned int p3x = 1, sigx = 0, sum = 0; | |
while (n) { | |
if (n % 2) { | |
sum += (nth + 1)*p3x + sigx*(p2x / 2); | |
nth += p2x; | |
} | |
sigx += p3x; | |
p2x *= 2; | |
p3x *= 3; | |
n /= 2; | |
} | |
return sum; | |
} | |
int main() { | |
scanf("%d", &testcases); | |
while(testcases--) { | |
unsigned long long int l, r; | |
scanf("%llu%llu", &l, &r); | |
printf("%u\n", tnum(r) - tnum(l - 1)); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment