4977026daf 2012-01-14 kinaba: #include <iostream> 4977026daf 2012-01-14 kinaba: #include <sstream> 4977026daf 2012-01-14 kinaba: #include <iomanip> 4977026daf 2012-01-14 kinaba: #include <vector> 4977026daf 2012-01-14 kinaba: #include <string> 4977026daf 2012-01-14 kinaba: #include <map> 4977026daf 2012-01-14 kinaba: #include <set> 4977026daf 2012-01-14 kinaba: #include <algorithm> 4977026daf 2012-01-14 kinaba: #include <numeric> 4977026daf 2012-01-14 kinaba: #include <iterator> 4977026daf 2012-01-14 kinaba: #include <functional> 4977026daf 2012-01-14 kinaba: #include <complex> 4977026daf 2012-01-14 kinaba: #include <queue> 4977026daf 2012-01-14 kinaba: #include <stack> 4977026daf 2012-01-14 kinaba: #include <cmath> 4977026daf 2012-01-14 kinaba: #include <cassert> 4977026daf 2012-01-14 kinaba: #include <cstring> 4977026daf 2012-01-14 kinaba: #ifdef __GNUC__ 4977026daf 2012-01-14 kinaba: #include <ext/hash_map> 4977026daf 2012-01-14 kinaba: #define unordered_map __gnu_cxx::hash_map 4977026daf 2012-01-14 kinaba: #else 4977026daf 2012-01-14 kinaba: #include <unordered_map> 4977026daf 2012-01-14 kinaba: #endif 4977026daf 2012-01-14 kinaba: using namespace std; 4977026daf 2012-01-14 kinaba: typedef long long LL; 4977026daf 2012-01-14 kinaba: typedef complex<double> CMP; 4977026daf 2012-01-14 kinaba: 4977026daf 2012-01-14 kinaba: class TheDictionary { public: 4977026daf 2012-01-14 kinaba: string find(int n, int m, int k) 4977026daf 2012-01-14 kinaba: { 4977026daf 2012-01-14 kinaba: if( k <= C(n+m,n) ) 4977026daf 2012-01-14 kinaba: return rec(n, m, k); 4977026daf 2012-01-14 kinaba: return ""; 4977026daf 2012-01-14 kinaba: } 4977026daf 2012-01-14 kinaba: 4977026daf 2012-01-14 kinaba: string rec(int n, int m, int k) 4977026daf 2012-01-14 kinaba: { 4977026daf 2012-01-14 kinaba: if( n+m == 0 ) 4977026daf 2012-01-14 kinaba: return ""; 4977026daf 2012-01-14 kinaba: if( n==0 ) 4977026daf 2012-01-14 kinaba: return "z" + rec(n, m-1, k); 4977026daf 2012-01-14 kinaba: if( m==0 ) 4977026daf 2012-01-14 kinaba: return "a" + rec(n-1, m, k); 4977026daf 2012-01-14 kinaba: 4977026daf 2012-01-14 kinaba: LL c = C(n+m-1, m); 4977026daf 2012-01-14 kinaba: if( k <= c ) 4977026daf 2012-01-14 kinaba: return "a" + rec(n-1, m, k); 4977026daf 2012-01-14 kinaba: return "z" + rec(n, m-1, k - c); 4977026daf 2012-01-14 kinaba: } 4977026daf 2012-01-14 kinaba: 4977026daf 2012-01-14 kinaba: LL C(int n, int k) 4977026daf 2012-01-14 kinaba: { 4977026daf 2012-01-14 kinaba: k = min(k, n-k); 4977026daf 2012-01-14 kinaba: LL v = 1; 4977026daf 2012-01-14 kinaba: for(int i=1; i<=k; ++i) { 4977026daf 2012-01-14 kinaba: v = v * (n+1-i) / i; 4977026daf 2012-01-14 kinaba: if( v >= (1LL<<50) ) 4977026daf 2012-01-14 kinaba: return v; 4977026daf 2012-01-14 kinaba: } 4977026daf 2012-01-14 kinaba: return v; 4977026daf 2012-01-14 kinaba: } 4977026daf 2012-01-14 kinaba: }; 4977026daf 2012-01-14 kinaba: 4977026daf 2012-01-14 kinaba: // BEGIN CUT HERE 4977026daf 2012-01-14 kinaba: #include <ctime> 4977026daf 2012-01-14 kinaba: double start_time; string timer() 4977026daf 2012-01-14 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4977026daf 2012-01-14 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4977026daf 2012-01-14 kinaba: { os << "{ "; 4977026daf 2012-01-14 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4977026daf 2012-01-14 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4977026daf 2012-01-14 kinaba: void verify_case(const string& Expected, const string& Received) { 4977026daf 2012-01-14 kinaba: bool ok = (Expected == Received); 4977026daf 2012-01-14 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4977026daf 2012-01-14 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4977026daf 2012-01-14 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4977026daf 2012-01-14 kinaba: #define END verify_case(_, TheDictionary().find(n, m, k));} 4977026daf 2012-01-14 kinaba: int main(){ 4977026daf 2012-01-14 kinaba: 4977026daf 2012-01-14 kinaba: CASE(0) 4977026daf 2012-01-14 kinaba: int n = 2; 4977026daf 2012-01-14 kinaba: int m = 2; 4977026daf 2012-01-14 kinaba: int k = 2; 4977026daf 2012-01-14 kinaba: string _ = "azaz"; 4977026daf 2012-01-14 kinaba: END 4977026daf 2012-01-14 kinaba: CASE(1) 4977026daf 2012-01-14 kinaba: int n = 2; 4977026daf 2012-01-14 kinaba: int m = 2; 4977026daf 2012-01-14 kinaba: int k = 6; 4977026daf 2012-01-14 kinaba: string _ = "zzaa"; 4977026daf 2012-01-14 kinaba: END 4977026daf 2012-01-14 kinaba: CASE(2) 4977026daf 2012-01-14 kinaba: int n = 10; 4977026daf 2012-01-14 kinaba: int m = 10; 4977026daf 2012-01-14 kinaba: int k = 1000000000; 4977026daf 2012-01-14 kinaba: string _ = ""; 4977026daf 2012-01-14 kinaba: END 4977026daf 2012-01-14 kinaba: CASE(3) 4977026daf 2012-01-14 kinaba: int n = 7; 4977026daf 2012-01-14 kinaba: int m = 4; 4977026daf 2012-01-14 kinaba: int k = 47; 4977026daf 2012-01-14 kinaba: string _ = "aaazazaazaz"; 4977026daf 2012-01-14 kinaba: END 4977026daf 2012-01-14 kinaba: /* 4977026daf 2012-01-14 kinaba: CASE(4) 4977026daf 2012-01-14 kinaba: int n = ; 4977026daf 2012-01-14 kinaba: int m = ; 4977026daf 2012-01-14 kinaba: int k = ; 4977026daf 2012-01-14 kinaba: string _ = ; 4977026daf 2012-01-14 kinaba: END 4977026daf 2012-01-14 kinaba: CASE(5) 4977026daf 2012-01-14 kinaba: int n = ; 4977026daf 2012-01-14 kinaba: int m = ; 4977026daf 2012-01-14 kinaba: int k = ; 4977026daf 2012-01-14 kinaba: string _ = ; 4977026daf 2012-01-14 kinaba: END 4977026daf 2012-01-14 kinaba: */ 4977026daf 2012-01-14 kinaba: } 4977026daf 2012-01-14 kinaba: // END CUT HERE