Artifact Content
Not logged in

Artifact 29ee29a34dc5af35b07b1098cebe25d23bbbb39e


#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 <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class TheLuckyGameDivOne { public:
	vector<LL> all;
	void computeAll()
	{
		for(int len=1; len<=11; ++len)
		{
			for(int pat=0; pat<(1<<len); ++pat)
			{
				LL val = 0;
				for(int i=0; i<len; ++i)
					val = val*10 + ((pat&(1<<i)) ? 7 : 4);
				all.push_back(val);
			}
		}
		sort(all.begin(), all.end());
	}

	int find(long long a, long long b, long long jLen, long long bLen) 
	{
		computeAll();

		pair<LL,LL> rng(a, a+bLen-1);
		vector< pair<pair<LL,LL>,int> > pt;
		pt.push_back(make_pair(rng, count_of(rng)));
		if( rng.second+1 <= b ) {
			pair<LL,LL> rngP1(rng.first+1,rng.second+1);
			pt.push_back(make_pair(rngP1, count_of(rngP1)));
		}
		for(;;) {
			rng = go_next(rng);
			if( rng.second > b )
				break;
			pair<LL,LL> rngM1(rng.first-1,rng.second-1);
			pt.push_back(make_pair(rngM1, count_of(rngM1)));
			pt.push_back(make_pair(rng, count_of(rng)));
			if( rng.second+1 <= b ) {
				pair<LL,LL> rngP1(rng.first+1,rng.second+1);
				pt.push_back(make_pair(rngP1, count_of(rngP1)));
			}
		}
		sort(pt.begin(), pt.end());
		pt.erase(unique(pt.begin(), pt.end()), pt.end());

		const LL xLen = jLen - bLen + 1;
		int theMax = 0;
		for(size_t i=0; i<pt.size(); ++i)
		{
			int theMin = 0x7fffffff;
			for(size_t j=i; j<pt.size() && pt[j].first.first<pt[i].first.first+xLen; ++j)
				theMin = min(theMin, pt[j].second);
			theMax = max(theMax, theMin);
		}
		return theMax;
	}

	pair<LL,LL> go_next(pair<LL,LL> rng)
	{
		LL a = go_next(rng.first);
		LL b = go_next(rng.second);
		LL dif = min(a-rng.first, b-rng.second);
		return make_pair(rng.first+dif, rng.second+dif);
	}

	LL go_next(LL x)
	{
		return *upper_bound(all.begin(), all.end(), x);
	}

	int count_of(pair<LL,LL> rng)
	{
		LL a = rng.first;
		LL b = rng.second;
		vector<LL>::iterator s = lower_bound(all.begin(), all.end(), a);
		vector<LL>::iterator e = upper_bound(all.begin(), all.end(), b);
		return e-s;
	}
};

// 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 int& Expected, const int& 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(_, TheLuckyGameDivOne().find(a, b, jLen, bLen));}
int main(){

CASE(0)
	long long a = 1LL; 
	long long b = 10LL; 
	long long jLen = 2LL; 
	long long bLen = 1LL; 
	int _ = 0; 
END
CASE(1)
	long long a = 1LL; 
	long long b = 100LL; 
	long long jLen = 100LL; 
	long long bLen = 100LL; 
	int _ = 6; 
END
CASE(2)
	long long a = 4LL; 
	long long b = 8LL; 
	long long jLen = 3LL; 
	long long bLen = 2LL; 
	int _ = 1; 
END
CASE(3)
	long long a = 1LL; 
	long long b = 100LL; 
	long long jLen = 75LL; 
	long long bLen = 50LL; 
	int _ = 2; 
END
CASE(4)
	long long a = 1LL; 
	long long b = 1LL; 
	long long jLen = 1LL; 
	long long bLen = 1LL; 
	int _ = 0; 
END
CASE(5)
	long long a = 10000000000LL; 
	long long b = 10000000000LL; 
	long long jLen = 5000000000LL; 
	long long bLen = 2000000000LL; 
	int _ = -1; 
END

}
// END CUT HERE