Artifact efa7784af0c0c5301f3005579d53d4fae24ec439
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;
class TheDivisionGame { public:
long long countWinningIntervals(int L, int R)
{
// eratos ---------------------------------
vector<int> ps;
{
static const int N = 50000;
vector<bool> isp(N+1, true);
for(int p=2; p<=N; ++p)
if( isp[p] ) {
ps.push_back(p);
for(int q=p+p; q<=N; q+=p)
isp[q] = false;
}
}
// prime decomposition --------------------
vector< vector<int> > pd(R-L+1);
vector<int> rm(R-L+1);
for(int v=L; v<=R; ++v)
rm[v-L] = v;
for(int i=0; i<ps.size(); ++i)
{
int p = ps[i];
for(int q=(L+p-1)/p*p; q<=R; q+=p)
{
int& r = rm[q-L];
int qn = 0;
while(r%p==0) qn++, r/=p;
pd[q-L].push_back(qn);
}
}
for(int v=L; v<=R; ++v)
if(rm[v-L] > 1)
pd[v-L].push_back(1);
// grundy ------------------------------------
vector<int> nim;
for(int i=0; i<pd.size(); ++i) {
nim.push_back(nim_calc(pd[i]));
}
// xorsum ------------------------------------
vector<int> sum;
map<int, LL> hist;
sum.push_back(0);
hist[sum.back()] ++;
for(int i=0; i<nim.size(); ++i) {
sum.push_back(sum.back() ^ nim[i]);
hist[sum.back()] ++;
}
LL lose = 0;
for(map<int,LL>::iterator it=hist.begin(); it!=hist.end(); ++it)
lose += it->second * (it->second-1) / 2;
LL w = R-L+2;
LL total = w*(w-1)/2;
return total - lose;
}
int nim_calc(vector<int> sig)
{
if( sig.empty() )
return 0;
int v = sig[0];
for(int i=1; i<sig.size(); ++i)
v = merge(v, sig[i]);
return v;
}
map<pair<int,int>, int> memo;
int merge(int a, int b)
{
if(a>b) swap(a,b);
if(a==0)
return b;
pair<int,int> key(a,b);
if(memo.count(key))
return memo[key];
vector<bool> used;
for(int x=0; x<=a; ++x)
for(int y=0; y<=b; ++y)
if(x!=a || y!=b)
{
int k = merge(x, y);
if(used.size()<=k) used.resize(k+1);
used[k] = true;
}
used.push_back(false);
return memo[key] = find(used.begin(), used.end(), false) - used.begin();
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, TheDivisionGame().countWinningIntervals(L, R));}
int main(){
CASE(0)
int L = 9;
int R = 10;
long long _ = 2LL;
END
CASE(1)
int L = 2;
int R = 5;
long long _ = 9LL;
END
CASE(2)
int L = 2;
int R = 6;
long long _ = 13LL;
END
CASE(3)
int L = 2;
int R = 100;
long long _ = 4345LL;
END
CASE(4)
int L = 12566125;
int R = 12567777;
long long _ = 1313432LL;
END
CASE(5)
int L = 1000000000;
int R = 1001000000;
long long _ = -1LL;
END
CASE(6)
int L = 2;
int R = 2;
long long _ = 0LL;
END
CASE(6)
int L = 2;
int R = 3;
long long _ = 0LL;
END
CASE(6)
int L = 2;
int R = 4;
long long _ = 3LL;
END
CASE(6)
int L = 4;
int R = 4;
long long _ = 1LL;
END
}
// END CUT HERE