cf074c940a 2022-05-21 kinaba: #include <iostream> cf074c940a 2022-05-21 kinaba: #include <sstream> cf074c940a 2022-05-21 kinaba: #include <iomanip> cf074c940a 2022-05-21 kinaba: #include <vector> cf074c940a 2022-05-21 kinaba: #include <string> cf074c940a 2022-05-21 kinaba: #include <map> cf074c940a 2022-05-21 kinaba: #include <set> cf074c940a 2022-05-21 kinaba: #include <algorithm> cf074c940a 2022-05-21 kinaba: #include <numeric> cf074c940a 2022-05-21 kinaba: #include <iterator> cf074c940a 2022-05-21 kinaba: #include <functional> cf074c940a 2022-05-21 kinaba: #include <complex> cf074c940a 2022-05-21 kinaba: #include <queue> cf074c940a 2022-05-21 kinaba: #include <stack> cf074c940a 2022-05-21 kinaba: #include <cmath> cf074c940a 2022-05-21 kinaba: #include <cassert> cf074c940a 2022-05-21 kinaba: #include <tuple> cf074c940a 2022-05-21 kinaba: using namespace std; cf074c940a 2022-05-21 kinaba: typedef long long LL; cf074c940a 2022-05-21 kinaba: typedef complex<double> CMP; cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: class RearrangeAndIncrement { public: cf074c940a 2022-05-21 kinaba: vector <int> change(int X, int Y) cf074c940a 2022-05-21 kinaba: { cf074c940a 2022-05-21 kinaba: vector<int> ans; cf074c940a 2022-05-21 kinaba: to_one(X, &ans); cf074c940a 2022-05-21 kinaba: from_one(Y, &ans); cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: vector<int> a; cf074c940a 2022-05-21 kinaba: for (int i = 0; i < ans.size(); ++i) { cf074c940a 2022-05-21 kinaba: a.push_back(ans[i]); cf074c940a 2022-05-21 kinaba: for (;;) { cf074c940a 2022-05-21 kinaba: auto it = find(ans.begin() + i + 1, ans.end(), ans[i]); cf074c940a 2022-05-21 kinaba: if (it == ans.end()) break; cf074c940a 2022-05-21 kinaba: i = it - ans.begin(); cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: cerr << "!!!" << a.size() << endl; cf074c940a 2022-05-21 kinaba: return a; cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: void to_one(int X, vector<int>* ans) cf074c940a 2022-05-21 kinaba: { cf074c940a 2022-05-21 kinaba: ans->push_back(X); cf074c940a 2022-05-21 kinaba: while (X != 1) { cf074c940a 2022-05-21 kinaba: if (X % 10 != 0) { cf074c940a 2022-05-21 kinaba: X += 10 - X % 10; cf074c940a 2022-05-21 kinaba: ans->push_back(X); cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: vector<int> Xs = to_vec(X); cf074c940a 2022-05-21 kinaba: sort(Xs.rbegin(), Xs.rend()); cf074c940a 2022-05-21 kinaba: X = to_int(Xs); cf074c940a 2022-05-21 kinaba: ans->push_back(X); cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: void from_one(int Y, vector<int>* ans) cf074c940a 2022-05-21 kinaba: { cf074c940a 2022-05-21 kinaba: vector<int> Ys = to_vec(Y); cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: vector<int> Xs(Ys.size(), 0); cf074c940a 2022-05-21 kinaba: Xs.back() = 1; cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: one_to_base(to_int(Xs), ans); cf074c940a 2022-05-21 kinaba: for (int i=Ys.size()-1; i>=0; --i) { cf074c940a 2022-05-21 kinaba: if (count(Ys.begin(), Ys.begin() + i, 0) == i) { cf074c940a 2022-05-21 kinaba: // i cf074c940a 2022-05-21 kinaba: // 00001 cf074c940a 2022-05-21 kinaba: // 0000Y cf074c940a 2022-05-21 kinaba: if (i == 0) { cf074c940a 2022-05-21 kinaba: Xs[i] = Ys[i]; cf074c940a 2022-05-21 kinaba: ans->push_back(to_int(Xs)); cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: else { cf074c940a 2022-05-21 kinaba: while (Xs[i] != Ys[i]) { cf074c940a 2022-05-21 kinaba: for (int k = i - 1; k >= 1; --k) { cf074c940a 2022-05-21 kinaba: Xs[0] = 9; cf074c940a 2022-05-21 kinaba: ans->push_back(to_int(Xs)); cf074c940a 2022-05-21 kinaba: swap(Xs[0], Xs[k]); cf074c940a 2022-05-21 kinaba: ans->push_back(to_int(Xs)); cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: for (int k = i - 1; k >= 0; --k) cf074c940a 2022-05-21 kinaba: Xs[k] = 0; cf074c940a 2022-05-21 kinaba: Xs[i]++; cf074c940a 2022-05-21 kinaba: ans->push_back(to_int(Xs)); cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: else { cf074c940a 2022-05-21 kinaba: // i cf074c940a 2022-05-21 kinaba: // 00001 cf074c940a 2022-05-21 kinaba: // ****Y cf074c940a 2022-05-21 kinaba: Xs[0] = Ys[i]; cf074c940a 2022-05-21 kinaba: ans->push_back(to_int(Xs)); cf074c940a 2022-05-21 kinaba: rotate(Xs.begin(), Xs.begin() + 1, Xs.begin() + i + 1); cf074c940a 2022-05-21 kinaba: ans->push_back(to_int(Xs)); cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: void one_to_base(int B, vector<int>* ans) cf074c940a 2022-05-21 kinaba: { cf074c940a 2022-05-21 kinaba: int X = 1; cf074c940a 2022-05-21 kinaba: while(X != B) { cf074c940a 2022-05-21 kinaba: if (X == 1) { cf074c940a 2022-05-21 kinaba: X = 10; cf074c940a 2022-05-21 kinaba: ans->push_back(X); cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: else { cf074c940a 2022-05-21 kinaba: vector<int> Xs = to_vec(X); cf074c940a 2022-05-21 kinaba: for (int i = Xs.size() - 1; i >= 1; --i) { cf074c940a 2022-05-21 kinaba: Xs[0] = 9; cf074c940a 2022-05-21 kinaba: ans->push_back(to_int(Xs)); cf074c940a 2022-05-21 kinaba: sort(Xs.begin(), Xs.end()); cf074c940a 2022-05-21 kinaba: ans->push_back(to_int(Xs)); cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: X *= 10; cf074c940a 2022-05-21 kinaba: ans->push_back(X); cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: static vector<int> to_vec(int x) { cf074c940a 2022-05-21 kinaba: vector<int> xs; cf074c940a 2022-05-21 kinaba: while (x) { cf074c940a 2022-05-21 kinaba: xs.push_back(x % 10); cf074c940a 2022-05-21 kinaba: x /= 10; cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: return xs; cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: static int to_int(const vector<int>& xs) { cf074c940a 2022-05-21 kinaba: int x = 0; cf074c940a 2022-05-21 kinaba: int p = 1; cf074c940a 2022-05-21 kinaba: for (int d : xs) { cf074c940a 2022-05-21 kinaba: x += d * p; cf074c940a 2022-05-21 kinaba: p *= 10; cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: return x; cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: }; cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: // BEGIN CUT HERE cf074c940a 2022-05-21 kinaba: #include <ctime> cf074c940a 2022-05-21 kinaba: double start_time; string timer() cf074c940a 2022-05-21 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } cf074c940a 2022-05-21 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) cf074c940a 2022-05-21 kinaba: { os << "{ "; cf074c940a 2022-05-21 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) cf074c940a 2022-05-21 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } cf074c940a 2022-05-21 kinaba: void verify_case(const vector <int>& Expected, const vector <int>& Received) { cf074c940a 2022-05-21 kinaba: bool ok = (Expected == Received); cf074c940a 2022-05-21 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cf074c940a 2022-05-21 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } cf074c940a 2022-05-21 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); cf074c940a 2022-05-21 kinaba: #define END verify_case(_, RearrangeAndIncrement().change(X, Y));} cf074c940a 2022-05-21 kinaba: int main(){ cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: CASE(0) cf074c940a 2022-05-21 kinaba: int X = 10234; cf074c940a 2022-05-21 kinaba: int Y = 1247; cf074c940a 2022-05-21 kinaba: int __[] = {10234, 1234, 1247 }; cf074c940a 2022-05-21 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); cf074c940a 2022-05-21 kinaba: END cf074c940a 2022-05-21 kinaba: CASE(1) cf074c940a 2022-05-21 kinaba: int X = 10234; cf074c940a 2022-05-21 kinaba: int Y = 10248; cf074c940a 2022-05-21 kinaba: int __[] = {10234, 10244, 10248 }; cf074c940a 2022-05-21 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); cf074c940a 2022-05-21 kinaba: END cf074c940a 2022-05-21 kinaba: CASE(2) cf074c940a 2022-05-21 kinaba: int X = 999997; cf074c940a 2022-05-21 kinaba: int Y = 1000001; cf074c940a 2022-05-21 kinaba: int __[] = {999997, 999998, 999999, 1000000, 1000001 }; cf074c940a 2022-05-21 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); cf074c940a 2022-05-21 kinaba: END cf074c940a 2022-05-21 kinaba: CASE(3) cf074c940a 2022-05-21 kinaba: int X = 1000000; cf074c940a 2022-05-21 kinaba: int Y = 1000; cf074c940a 2022-05-21 kinaba: int __[] = {1000000, 1000 }; cf074c940a 2022-05-21 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); cf074c940a 2022-05-21 kinaba: END cf074c940a 2022-05-21 kinaba: CASE(4) cf074c940a 2022-05-21 kinaba: int X = 1111111; cf074c940a 2022-05-21 kinaba: int Y = 1111232; cf074c940a 2022-05-21 kinaba: int __[] = {1111111, 1111122, 1111221, 1111232 }; cf074c940a 2022-05-21 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); cf074c940a 2022-05-21 kinaba: END cf074c940a 2022-05-21 kinaba: CASE(5) cf074c940a 2022-05-21 kinaba: int X = 47; cf074c940a 2022-05-21 kinaba: int Y = 47; cf074c940a 2022-05-21 kinaba: int __[] = {47 }; cf074c940a 2022-05-21 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); cf074c940a 2022-05-21 kinaba: END cf074c940a 2022-05-21 kinaba: CASE(6) cf074c940a 2022-05-21 kinaba: int X = 1; cf074c940a 2022-05-21 kinaba: int Y = 900000000; cf074c940a 2022-05-21 kinaba: int __[] = { -1 }; cf074c940a 2022-05-21 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); cf074c940a 2022-05-21 kinaba: END cf074c940a 2022-05-21 kinaba: CASE(7) cf074c940a 2022-05-21 kinaba: int X = 111111111; cf074c940a 2022-05-21 kinaba: int Y = 900000000; cf074c940a 2022-05-21 kinaba: int __[] = { -1 }; cf074c940a 2022-05-21 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); cf074c940a 2022-05-21 kinaba: END cf074c940a 2022-05-21 kinaba: cf074c940a 2022-05-21 kinaba: } cf074c940a 2022-05-21 kinaba: // END CUT HERE