Skip to content

Instantly share code, notes, and snippets.

@zsrinivas
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsrinivas/2a89c52b3bef33edb85a to your computer and use it in GitHub Desktop.
Save zsrinivas/2a89c52b3bef33edb85a to your computer and use it in GitHub Desktop.
spoj ⟿ classical ⟿ treenum2 ⟿ The art of tree numbers
#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