Skip to content

Instantly share code, notes, and snippets.

@juanplopes
Created October 16, 2014 18:50
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 juanplopes/023a1065e6307b9365d0 to your computer and use it in GitHub Desktop.
Save juanplopes/023a1065e6307b9365d0 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
#define ull long long
using namespace std;
ull a(ull n) {
if (n<=2) return n-1;
ull k = floor(log(n-1)/log(2));
ull p, q, m;
if (n==pow(2, k+1)) {
p=0; q=0; m=1;
} else if (pow(2,k)<n and n<pow(2,k)+pow(2,k-1)) {
ull i = n-pow(2, k);
p = k-floor(log(i)/log(2))-1; q=2; m=pow(2, floor(log(i)/log(2)))+i;
} else if (n==pow(2,k)+pow(2,k-1)) {
p=0; q=1; m=1;
} else {
ull j=n-pow(2, k)-pow(2, k-1);
p = k-floor(log(j)/log(2))-1; q=1; m=2*pow(2, floor(log(j)/log(2)))+j;
}
return pow(2, 2*k-q)+1+pow(2, p)*a(m);
}
ull until(ull n) {
if (n<0) return 0;
ull first = 1;
ull last = 100000000;
ull count = last-first;
while (count>0)
{
ull it = first, step=count/2; it+=step;
if (a(it)<n) {
first=++it;
count-=step+1;
}
else count=step;
}
if (a(first) != n) first--;
return first-1;
}
int main() {
ull a, b; char c;
while(cin >> a >> c >> b) {
cout << until(b)-until(a-1) << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment