Artifact 0f5c0e345a533ed4536cbe1b8eeb16b9eec5cd99
#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>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class AlwaysDefined { public:
long long countIntegers(long long L, long long R, int W)
{
vector<vector<int>> tbl(W);
for(int q=1; q<=W-1; ++q)
for(int r=1; r<=W-1; ++r)
if(q*r%W == r)
tbl[q].push_back(r);
// count all numbers that can be generated by repeating
// x ==> rx where tbl[x%W].has(r)
if(R<=1000000)
{
vector<int> V(R+1, false);
for(int k=0; k*W+1<=R; ++k)
{
queue<int> Q;
if(!V[k*W+1])
Q.push(k*W+1), V[k*W+1]=true;
while(!Q.empty())
{
int s = Q.front();
Q.pop();
for(int r: tbl[s%W]) {
if(s*r<=R && !V[s*r])
Q.push(s*r), V[s*r]=true;
}
}
}
LL cnt = 0;
for(LL X=L; X<=R; ++X)
if(V[X])
++cnt;
return cnt;
}
LL cnt = 0;
for(LL X=L; X<=R; ++X)
{
LL x = X;
for(;;) {
if(x%W==0)
break;
if(x%W==1)
{++cnt; break;}
if(x%(x%W)!=0)
break;
x /= x%W;
}
}
return cnt;
}
};
// 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(_, AlwaysDefined().countIntegers(L, R, W));}
int main(){
CASE(0)
long long L = 10LL;
long long R = 10LL;
int W = 4;
long long _ = 1LL;
END
CASE(1)
long long L = 1LL;
long long R = 99LL;
int W = 2;
long long _ = 50LL;
END
CASE(2)
long long L = 1282LL;
long long R = 1410LL;
int W = 10;
long long _ = 42LL;
END
CASE(178116)
long long L = 1LL;
long long R = 1000000LL;
int W = 30;
long long _ = 154161LL;
END
CASE(3)
long long L = 29195807LL;
long long R = 273209804877LL;
int W = 42;
long long _ = 31415926535LL;
END
CASE(4)
long long L = 1LL;
long long R = 1000000000000000000LL;
int W = 3000;
long long _ = -1LL;
END
CASE(5)
long long L = 1LL;
long long R = 1000000000000000000LL;
int W = 3;
long long _ = -1LL;
END
}
// END CUT HERE