Artifact Content
Not logged in

Artifact 70b57b6d4d4ca3f0ab7f64d1dc50b5baf6fc25c0


#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 PrefixComposite { public:
	vector<long long> minMax(long long A, long long B)
	{
		LL r = solve(B);
		if (r < A)
			return vector<LL>();

		LL L = A - 1, R = r;
		while(R-L>1) {
			LL C = (L + R) / 2;
			LL c = solve(C);
			if (c < A)
				L = C;
			else
				R = c;
		}
		return vector<LL>({ R, r });
	}

	// max such value <= B
	LL solve(LL B) {
		vector<int> bb;
		for(; B!=0; B/=10)
			bb.push_back(B % 10);
		reverse(bb.begin(), bb.end());

		function<LL(LL, int)> rec = [&](LL pref, int i) {
			if (i == bb.size())
				return pref;

			LL pp = pref * 10 + bb[i];
			if(is_composite(pp)) {
				LL r = rec(pp, i + 1);
				if (r)
					return r;
			}

			for (int k = bb[i] - 1; k >= 0; --k) {
				LL pp = pref * 10 + k;
				if (is_composite(pp)) {
					int n = bb.size() - i - 1;
					// get max from pp ++ [0-9]*n
					for (int j = 0; j < n; ++j)
						if (is_composite(pp*10+9))
							pp = pp*10+9;
						else
							pp = pp*10+8;
					return pp;
				}
			}
			return 0LL;
		};
		return rec(0, 0);
	}

	bool is_composite(LL v)
	{
		if (v == 0)
			return true;
		for (LL p = 2; p*p <= v; ++p)
			if (v%p == 0)
				return true;
		return false;
	}
};

// 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 vector<long long>& Expected, const vector<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(_, PrefixComposite().minMax(A, B));}
int main(){

CASE(0)
	long long A = 1LL; 
	long long B = 3LL; 
	vector<long long> _; 
END
CASE(1)
	long long A = 1LL; 
	long long B = 4LL; 
	long long __[] = {4, 4 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(2)
	long long A = 123LL; 
	long long B = 838LL; 
	long long __[] = {400, 828 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(3)
	long long A = 409LL; 
	long long B = 87343LL; 
	long long __[] = {420, 87343 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(4)
	long long A = 979797LL; 
	long long B = 979898LL; 
	vector<long long> _; 
END
CASE(5)
	long long A = 600LL; 
	long long B = 703LL; 
	long long __[] = {600, 699 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(6)
	long long A = 1LL; 
	long long B = 100000000000LL; 
	long long __[] = {4, 99999999999 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(7)
	long long A = 37337999LL; 
	long long B = 37337999LL; 
	vector<long long> _; 
END
CASE(8)
	long long A = 22LL; 
	long long B = 39LL; 
	vector<long long> _; 
END
CASE(9)
	long long A = 600LL; 
	long long B = 699LL; 
	long long __[] = { 600, 699 };
	  vector<long long> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(10)
	long long A = 1LL; 
	long long B = 1LL; 
	vector<long long> _;
END
CASE(10)
long long A = 1LL;
long long B = 6LL;
vector<long long> _;
END
}
// END CUT HERE