Artifact Content
Not logged in

Artifact eb6666379b3e48bb738912f74e552de90612f4af


#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 FavouriteDigits { public:
	long long findNext(long long N, int digit1, int count1, int digit2, int count2)
	{
		LL d = 1;
		for(int _=0; _<15; ++_) d *= 10;

		LL answer = 0;

		bool zeros = true;
		for(int rest=15; d>=1; d/=10, --rest) {
			for(int v=0; v<=9; ++v) {
				int c1 = max(0, count1 - (v==digit1 && (v!=0 || !zeros) ? 1 : 0));
				int c2 = max(0, count2 - (v==digit2 && (v!=0 || !zeros) ? 1 : 0));
				bool zz = zeros && (v==0);
				if( rest >= c1 + c2 ) {
					LL cur = answer + v*d + largest(rest, digit1, c1, digit2, c2);
					if( cur >= N ) {
						answer += v*d;
						count1 = c1;
						count2 = c2;
						zeros = zz;
						break;
					}
				}
			}
		}

		return answer;
	}

	LL largest(int rest, int d1, int c1, int d2, int c2)
	{
		if(rest==0)
			return 0;
		if(d1>d2) swap(d1,d2), swap(c1,c2);
		string s;
		for(int i=0; i<rest-c1-c2; ++i)
			s += '9';
		for(int i=0; i<c2; ++i)
			s += char('0'+d2);
		for(int i=0; i<c1; ++i)
			s += char('0'+d1);
		stringstream sin(s);
		LL n;
		sin >> n;
		return n;
	}
};

// 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(_, FavouriteDigits().findNext(N, digit1, count1, digit2, count2));}
int main(){

CASE(0)
	long long N = 47LL; 
	int digit1 = 1; 
	int count1 = 0; 
	int digit2 = 2; 
	int count2 = 0; 
	long long _ = 47LL; 
END
CASE(1)
	long long N = 47LL; 
	int digit1 = 5; 
	int count1 = 0; 
	int digit2 = 9; 
	int count2 = 1; 
	long long _ = 49LL; 
END
CASE(2)
	long long N = 47LL; 
	int digit1 = 5; 
	int count1 = 0; 
	int digit2 = 3; 
	int count2 = 1; 
	long long _ = 53LL; 
END
CASE(3)
	long long N = 47LL; 
	int digit1 = 2; 
	int count1 = 1; 
	int digit2 = 0; 
	int count2 = 2; 
	long long _ = 200LL; 
END
CASE(4)
	long long N = 123456789012345LL; 
	int digit1 = 1; 
	int count1 = 2; 
	int digit2 = 2; 
	int count2 = 4; 
	long long _ = 123456789012422LL; 
END
CASE(5)
	long long N = 92LL; 
	int digit1 = 1; 
	int count1 = 1; 
	int digit2 = 0; 
	int count2 = 0; 
	long long _ = 100LL; 
END
/*
CASE(6)
	long long N = LL; 
	int digit1 = ; 
	int count1 = ; 
	int digit2 = ; 
	int count2 = ; 
	long long _ = LL; 
END
*/
CASE(7)
	long long N = 1000000000000000LL - 1; 
	int digit1 = 5; 
	int count1 = 5; 
	int digit2 = 5; 
	int count2 = 5; 
	long long _ = -1LL; 
END

}
// END CUT HERE