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 TheInteger { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: long long find(long long n, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: stringstream ss; 4fd800b3a8 2011-02-23 kinaba: ss << n; 4fd800b3a8 2011-02-23 kinaba: string s; 4fd800b3a8 2011-02-23 kinaba: ss >> s; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( s.size() < k ) 4fd800b3a8 2011-02-23 kinaba: s = string(k-s.size(), '0') + s; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL a = solve(s, k); 4fd800b3a8 2011-02-23 kinaba: if( a ) 4fd800b3a8 2011-02-23 kinaba: return a; 4fd800b3a8 2011-02-23 kinaba: return solve("0"+s, k); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL solve(string s, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // no change 4fd800b3a8 2011-02-23 kinaba: if( s[0] != '0' ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: set<char> d; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<s.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: d.insert( s[i] ); 4fd800b3a8 2011-02-23 kinaba: if( d.size() == k ) 4fd800b3a8 2011-02-23 kinaba: return toll(s); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // a-th digit larger 4fd800b3a8 2011-02-23 kinaba: int lim = s[0]=='0' ? 1 : s.size(); 4fd800b3a8 2011-02-23 kinaba: for(int a=lim-1; a>=0; --a) 4fd800b3a8 2011-02-23 kinaba: for(char v=s[a]+1; v<='9'; ++v) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: set<char> d; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<a; ++i) 4fd800b3a8 2011-02-23 kinaba: d.insert(s[i]); 4fd800b3a8 2011-02-23 kinaba: d.insert(v); 4fd800b3a8 2011-02-23 kinaba: if( d.size() > k ) 4fd800b3a8 2011-02-23 kinaba: continue; 4fd800b3a8 2011-02-23 kinaba: else if( d.size() == k ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: s[a] = v; 4fd800b3a8 2011-02-23 kinaba: for(int i=a+1; i<s.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: s[i] = *d.begin(); 4fd800b3a8 2011-02-23 kinaba: return toll(s); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int rest = k - d.size(); 4fd800b3a8 2011-02-23 kinaba: if( s.size()-a-1 < rest ) 4fd800b3a8 2011-02-23 kinaba: continue; 4fd800b3a8 2011-02-23 kinaba: s[a] = v; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int fi = s.size()-rest; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=a+1; i<fi; ++i) 4fd800b3a8 2011-02-23 kinaba: s[i] = '0'; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(char z='0'; z<='9'; ++z) 4fd800b3a8 2011-02-23 kinaba: if( !d.count(z) ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: s[fi++] = z; 4fd800b3a8 2011-02-23 kinaba: if( fi == s.size() ) 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return toll(s); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL toll( string s ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: stringstream ss; ss << s; 4fd800b3a8 2011-02-23 kinaba: LL n; ss >> n; 4fd800b3a8 2011-02-23 kinaba: return n; 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() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const long long &Expected, const long long &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: long long n = 47LL; 4fd800b3a8 2011-02-23 kinaba: int k = 1; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 55LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TheInteger().find(n, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: long long n = 7LL; 4fd800b3a8 2011-02-23 kinaba: int k = 3; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 102LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TheInteger().find(n, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: long long n = 69LL; 4fd800b3a8 2011-02-23 kinaba: int k = 2; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 69LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TheInteger().find(n, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: long long n = 12364LL; 4fd800b3a8 2011-02-23 kinaba: int k = 3; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 12411LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TheInteger().find(n, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: long long n = 1LL; 4fd800b3a8 2011-02-23 kinaba: int k = 10; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 1023456789LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TheInteger().find(n, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: long long n = 111111111111111LL; 4fd800b3a8 2011-02-23 kinaba: int k = 10; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 111111203456789LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TheInteger().find(n, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: long long n = 111111111111111LL; 4fd800b3a8 2011-02-23 kinaba: int k = 8; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 111111112034567LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TheInteger().find(n, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<7>) { 4fd800b3a8 2011-02-23 kinaba: long long n = 100000000000000LL; 4fd800b3a8 2011-02-23 kinaba: int k = 10; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 100000023456789LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TheInteger().find(n, k)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<8>) { 4fd800b3a8 2011-02-23 kinaba: long long n = 100000000000000LL; 4fd800b3a8 2011-02-23 kinaba: int k = 5; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 100000000000234LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, TheInteger().find(n, k)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: