Artifact Content
Not logged in

Artifact 22cbd7d786057de9f7264891c6e4f11de266d072


#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 ColorfulStrings { public:
	string getKth(int n, int k) 
	{
		// cannot have 11 distinct digits
		// if we have 0, then ...
		// if we have 1, then ...
		if(n>8)
			return "";

		string s = (n>=2 ? "23456789" : "0123456789");
		do {
			if( is_colorful(s,n) )
				if( --k == 0 )
					return s.substr(0,n);
		} while( next_perm(s, n>=2 ? 8-n : 10-n) );
		return "";
	}

	bool is_colorful(string& s, int n) {
		set<int> ps;
		for(int i=0; i<n; ++i) {
			int p = s[i]-'0';
			if( !ps.insert(p).second )
				return false;
			for(int j=i+1; j<n; ++j) {
				p *= s[j]-'0';
				if( !ps.insert(p).second )
					return false;
			}
		}
		return true;
	}

	int fact(int n) {
		return n ? n*fact(n-1) : 1;
	}
	bool next_perm(string& s, int nn) {
		int f = fact(nn);
		while(f--)
			if( !next_permutation(s.begin(), s.end()) )
				return false;
		return true;
	}
};

// 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(_, ColorfulStrings().getKth(n, k));}
int main(){

CASE(0)
	int n = 3; 
	int k = 4; 
	string _ = "238"; 
END
CASE(1)
	int n = 4; 
	int k = 2000; 
	string _ = ""; 
END
CASE(2)
	int n = 5; 
	int k = 1; 
	string _ = "23457"; 
END
CASE(3)
	int n = 2; 
	int k = 22; 
	string _ = "52"; 
END
CASE(4)
	int n = 6; 
	int k = 464; 
	string _ = "257936"; 
END
CASE(5)
	int n = 1; 
	int k = 1; 
	string _ = "0"; 
END
CASE(6)
	int n = 50; 
	int k = 1000000000; 
	string _ = "???"; 
END
CASE(7)
	int n = 2; 
	int k = 57; 
	string _ = "???"; 
END

}
// END CUT HERE