4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class NextHomogeneousStrings { public: 4fd800b3a8 2011-02-23 kinaba: string getNext(int d, int n, string seed, long long k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int M = seed.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL L=0, R=1; // [L,R) 4fd800b3a8 2011-02-23 kinaba: while( R-L > 2 ) { 4fd800b3a8 2011-02-23 kinaba: LL C = (R+L)/2; 4fd800b3a8 2011-02-23 kinaba: string t = xth(d,n,C,M); 4fd800b3a8 2011-02-23 kinaba: (t=="" || seed<t ? R : L) = C; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return seed==xth(d,n,L,M) ? xth(d,n,L+k-1,M) : xth(d,n,L+k,M); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: string xth(int d, int n, LL x, int M) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: return rec( string(), x, d, n, M ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: string rec( string hist, LL x, int d, int n, int M ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( M == 0 ) 4fd800b3a8 2011-02-23 kinaba: return ""; 4fd800b3a8 2011-02-23 kinaba: for(char c='a'; c<='z'; ++c) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: string hh = hist + c; 4fd800b3a8 2011-02-23 kinaba: // todo: normalize 4fd800b3a8 2011-02-23 kinaba: if( x < cnt(hh,d,n,M) ) 4fd800b3a8 2011-02-23 kinaba: return c + rec(hist+c, x, d, n, M-1); 4fd800b3a8 2011-02-23 kinaba: x -= cnt(hh,d,n,M); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ""; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL cnt(string hist, int d, int n, int M) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( M == 0 ) 4fd800b3a8 2011-02-23 kinaba: return 1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // todo: memoize 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL ans = 0; 4fd800b3a8 2011-02-23 kinaba: for(char c='a'; c<='z'; ++c) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: char cc = c; 4fd800b3a8 2011-02-23 kinaba: if( find(hist.begin(), hist.end(), c)==hist.end() ) 4fd800b3a8 2011-02-23 kinaba: cc = *max_element(hist.begin(),hist.end()) + 1; // todo: end then 'a' 4fd800b3a8 2011-02-23 kinaba: string h2 = hist+cc; 4fd800b3a8 2011-02-23 kinaba: // todo: check violation 4fd800b3a8 2011-02-23 kinaba: // todo: drop 1st (if needed) 4fd800b3a8 2011-02-23 kinaba: // todo: normalize 4fd800b3a8 2011-02-23 kinaba: //ans += cnt(h2, dd, nn, M-1); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ans; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const string& Expected, const string& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, NextHomogeneousStrings().getNext(d, n, seed, k));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int d = 1; 4fd800b3a8 2011-02-23 kinaba: int n = 2; 4fd800b3a8 2011-02-23 kinaba: string seed = "aaa"; 4fd800b3a8 2011-02-23 kinaba: long long k = 3LL; 4fd800b3a8 2011-02-23 kinaba: string _ = "ddd"; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int d = 2; 4fd800b3a8 2011-02-23 kinaba: int n = 3; 4fd800b3a8 2011-02-23 kinaba: string seed = "abc"; 4fd800b3a8 2011-02-23 kinaba: long long k = 0LL; 4fd800b3a8 2011-02-23 kinaba: string _ = "aca"; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int d = 2; 4fd800b3a8 2011-02-23 kinaba: int n = 4; 4fd800b3a8 2011-02-23 kinaba: string seed = "ttrrzz"; 4fd800b3a8 2011-02-23 kinaba: long long k = 6LL; 4fd800b3a8 2011-02-23 kinaba: string _ = "ttsssc"; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int d = 6; 4fd800b3a8 2011-02-23 kinaba: int n = 8; 4fd800b3a8 2011-02-23 kinaba: string seed = "txyaaxaassaaaarghjsdohasdghususdidisisdodo"; 4fd800b3a8 2011-02-23 kinaba: long long k = 10000000000000000LL; 4fd800b3a8 2011-02-23 kinaba: string _ = "txyaaxaassaaaarghjsgaaaaaaaaadntffiniqrddy"; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int d = 2; 4fd800b3a8 2011-02-23 kinaba: int n = 5; 4fd800b3a8 2011-02-23 kinaba: string seed = "zzzzzaa"; 4fd800b3a8 2011-02-23 kinaba: long long k = 100LL; 4fd800b3a8 2011-02-23 kinaba: string _ = ""; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int d = ; 4fd800b3a8 2011-02-23 kinaba: int n = ; 4fd800b3a8 2011-02-23 kinaba: string seed = ; 4fd800b3a8 2011-02-23 kinaba: long long k = LL; 4fd800b3a8 2011-02-23 kinaba: string _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: int d = ; 4fd800b3a8 2011-02-23 kinaba: int n = ; 4fd800b3a8 2011-02-23 kinaba: string seed = ; 4fd800b3a8 2011-02-23 kinaba: long long k = LL; 4fd800b3a8 2011-02-23 kinaba: string _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE