af429d4fc8 2012-06-23 kinaba: #include <iostream> af429d4fc8 2012-06-23 kinaba: #include <sstream> af429d4fc8 2012-06-23 kinaba: #include <iomanip> af429d4fc8 2012-06-23 kinaba: #include <vector> af429d4fc8 2012-06-23 kinaba: #include <string> af429d4fc8 2012-06-23 kinaba: #include <map> af429d4fc8 2012-06-23 kinaba: #include <set> af429d4fc8 2012-06-23 kinaba: #include <algorithm> af429d4fc8 2012-06-23 kinaba: #include <numeric> af429d4fc8 2012-06-23 kinaba: #include <iterator> af429d4fc8 2012-06-23 kinaba: #include <functional> af429d4fc8 2012-06-23 kinaba: #include <complex> af429d4fc8 2012-06-23 kinaba: #include <queue> af429d4fc8 2012-06-23 kinaba: #include <stack> af429d4fc8 2012-06-23 kinaba: #include <cmath> af429d4fc8 2012-06-23 kinaba: #include <cassert> af429d4fc8 2012-06-23 kinaba: using namespace std; af429d4fc8 2012-06-23 kinaba: typedef long long LL; af429d4fc8 2012-06-23 kinaba: typedef long double LD; af429d4fc8 2012-06-23 kinaba: typedef complex<LD> CMP; af429d4fc8 2012-06-23 kinaba: af429d4fc8 2012-06-23 kinaba: class KleofasTail { public: af429d4fc8 2012-06-23 kinaba: long long countGoodSequences(long long K, long long A, long long B) af429d4fc8 2012-06-23 kinaba: { af429d4fc8 2012-06-23 kinaba: return solve(K,B) - solve(K,A-1); af429d4fc8 2012-06-23 kinaba: } af429d4fc8 2012-06-23 kinaba: LL solve(LL K, LL U) af429d4fc8 2012-06-23 kinaba: { af429d4fc8 2012-06-23 kinaba: if(U==-1) af429d4fc8 2012-06-23 kinaba: return 0; af429d4fc8 2012-06-23 kinaba: if(K==0) af429d4fc8 2012-06-23 kinaba: return U+1; af429d4fc8 2012-06-23 kinaba: af429d4fc8 2012-06-23 kinaba: LL v = 0; af429d4fc8 2012-06-23 kinaba: for(int i=0; (K<<i)<=U; ++i) { af429d4fc8 2012-06-23 kinaba: if( (K+1<<i)-1 <= U ) af429d4fc8 2012-06-23 kinaba: v += (1LL<<i); af429d4fc8 2012-06-23 kinaba: else af429d4fc8 2012-06-23 kinaba: v += U - (K<<i) + 1; af429d4fc8 2012-06-23 kinaba: } af429d4fc8 2012-06-23 kinaba: if((K&1)==0) { af429d4fc8 2012-06-23 kinaba: ++K; af429d4fc8 2012-06-23 kinaba: for(int i=0; (K<<i)<=U; ++i) { af429d4fc8 2012-06-23 kinaba: if( (K+1<<i)-1 <= U ) af429d4fc8 2012-06-23 kinaba: v += (1LL<<i); af429d4fc8 2012-06-23 kinaba: else af429d4fc8 2012-06-23 kinaba: v += U - (K<<i) + 1; af429d4fc8 2012-06-23 kinaba: } af429d4fc8 2012-06-23 kinaba: } af429d4fc8 2012-06-23 kinaba: return v; af429d4fc8 2012-06-23 kinaba: } af429d4fc8 2012-06-23 kinaba: }; af429d4fc8 2012-06-23 kinaba: af429d4fc8 2012-06-23 kinaba: // BEGIN CUT HERE af429d4fc8 2012-06-23 kinaba: #include <ctime> af429d4fc8 2012-06-23 kinaba: double start_time; string timer() af429d4fc8 2012-06-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } af429d4fc8 2012-06-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) af429d4fc8 2012-06-23 kinaba: { os << "{ "; af429d4fc8 2012-06-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) af429d4fc8 2012-06-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } af429d4fc8 2012-06-23 kinaba: void verify_case(const long long& Expected, const long long& Received) { af429d4fc8 2012-06-23 kinaba: bool ok = (Expected == Received); af429d4fc8 2012-06-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; af429d4fc8 2012-06-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } af429d4fc8 2012-06-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); af429d4fc8 2012-06-23 kinaba: #define END verify_case(_, KleofasTail().countGoodSequences(K, A, B));} af429d4fc8 2012-06-23 kinaba: int main(){ af429d4fc8 2012-06-23 kinaba: CASE(0) af429d4fc8 2012-06-23 kinaba: long long K = 3LL; af429d4fc8 2012-06-23 kinaba: long long A = 4LL; af429d4fc8 2012-06-23 kinaba: long long B = 8LL; af429d4fc8 2012-06-23 kinaba: long long _ = 2LL; af429d4fc8 2012-06-23 kinaba: END af429d4fc8 2012-06-23 kinaba: CASE(1) af429d4fc8 2012-06-23 kinaba: long long K = 1LL; af429d4fc8 2012-06-23 kinaba: long long A = 23457LL; af429d4fc8 2012-06-23 kinaba: long long B = 123456LL; af429d4fc8 2012-06-23 kinaba: long long _ = 100000LL; af429d4fc8 2012-06-23 kinaba: END af429d4fc8 2012-06-23 kinaba: CASE(2) af429d4fc8 2012-06-23 kinaba: long long K = 1234567890123456LL; af429d4fc8 2012-06-23 kinaba: long long A = 10LL; af429d4fc8 2012-06-23 kinaba: long long B = 1000000LL; af429d4fc8 2012-06-23 kinaba: long long _ = 0LL; af429d4fc8 2012-06-23 kinaba: END af429d4fc8 2012-06-23 kinaba: CASE(3) af429d4fc8 2012-06-23 kinaba: long long K = 0LL; af429d4fc8 2012-06-23 kinaba: long long A = 0LL; af429d4fc8 2012-06-23 kinaba: long long B = 2LL; af429d4fc8 2012-06-23 kinaba: long long _ = 3LL; af429d4fc8 2012-06-23 kinaba: END af429d4fc8 2012-06-23 kinaba: CASE(4) af429d4fc8 2012-06-23 kinaba: long long K = 2LL; af429d4fc8 2012-06-23 kinaba: long long A = 3LL; af429d4fc8 2012-06-23 kinaba: long long B = 3LL; af429d4fc8 2012-06-23 kinaba: long long _ = 1LL; af429d4fc8 2012-06-23 kinaba: END af429d4fc8 2012-06-23 kinaba: CASE(5) af429d4fc8 2012-06-23 kinaba: long long K = 13LL; af429d4fc8 2012-06-23 kinaba: long long A = 12345LL; af429d4fc8 2012-06-23 kinaba: long long B = 67890123LL; af429d4fc8 2012-06-23 kinaba: long long _ = 8387584LL; af429d4fc8 2012-06-23 kinaba: END af429d4fc8 2012-06-23 kinaba: CASE(6) af429d4fc8 2012-06-23 kinaba: long long K = 2LL; af429d4fc8 2012-06-23 kinaba: long long A = 0LL; af429d4fc8 2012-06-23 kinaba: long long B = 6LL; af429d4fc8 2012-06-23 kinaba: long long _ = 5LL; af429d4fc8 2012-06-23 kinaba: END af429d4fc8 2012-06-23 kinaba: CASE(7) af429d4fc8 2012-06-23 kinaba: long long K = 2LL; af429d4fc8 2012-06-23 kinaba: long long A = 0LL; af429d4fc8 2012-06-23 kinaba: long long B = 9999999999999LL; af429d4fc8 2012-06-23 kinaba: long long _ = -1LL; af429d4fc8 2012-06-23 kinaba: END af429d4fc8 2012-06-23 kinaba: af429d4fc8 2012-06-23 kinaba: } af429d4fc8 2012-06-23 kinaba: // END CUT HERE