Artifact Content
Not logged in

Artifact 56eab9a0d5dca1c426ce6f8b3e5761507711f606


#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 TwoRegisters { public:
	string minProg(int r) 
	{
		int minn = 0x7fffffff;
		vector<int> miny;
		for(int y=1; y<r; ++y)
		{
			int n = cnt(r,y);
			if( n != -1 )
				if( n <= minn ) {
					if( n < minn )
						miny.clear();
					minn = n;
					miny.push_back(y);
				}
		}

		string answer;
		for(int i=0; i<miny.size(); ++i)
		{
			int y = miny[i];
			string s;
			compute(r, y, false, s);
			if( i==0 || s < answer )
				answer = s;
		}
		return answer;
	}

	int cnt(int r, int y)
	{
		if( y==0 )
			return -1;
		if( y==1 )
			return r-1;
		int k = r/y;
		int kk = cnt(y, r%y);
		if( kk == -1 )
			return -1;
		return k+kk;
	}

	void compute(int r, int y, bool isY, string& s)
	{
		if( y==1 ) {
			for(int i=0; i<r-1; ++i)
				s += (isY ? 'Y' : 'X');
			return;
		}
		int k = r/y;
		compute(y, r%y, !isY, s);
		while( k --> 0 )
			s += (isY ? 'Y' : 'X');
	}
};

// 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 string& Expected, const string& 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(_, TwoRegisters().minProg(r));}
int main(){

CASE(0)
	int r = 10; 
	string _ = "XXYYX"; 
END
CASE(1)
	int r = 3; 
	string _ = "XX"; 
END
CASE(2)
	int r = 20; 
	string _ = "XYYYYXX"; 
END
CASE(3)
	int r = 34; 
	string _ = "XYXYXYX"; 
END
CASE(4)
	int r = 1; 
	string _ = ""; 
END
CASE(5)
	int r = 1000000; 
	string _ = "??"; 
END
CASE(6)
	int r = 2; 
	string _ = "X"; 
END
CASE(6)
	int r = 999999; 
	string _ = "??"; 
END
CASE(6)
	int r = 999998; 
	string _ = "??"; 
END
CASE(6)
	int r = 999997; 
	string _ = "??"; 
END
CASE(6)
	int r = 999996; 
	string _ = "??"; 
END
CASE(6)
	int r = 999995; 
	string _ = "??"; 
END
CASE(6)
	int r = 5; 
	string _ = "??"; 
END

}
// END CUT HERE