#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef __int128 lll; const int L = 26; const int MN = 1000000; struct S { int size=0; int st[L/2+2]; int operator[](int i) const { return st[i]; } int& operator[](int i) { return st[i]; } bool operator==(const S& s) const { return size==s.size && equal(st,st+size,s.st); } bool operator<(const S& s) const { if (size!=s.size) return size C; struct C { static constexpr int size = 24; unsigned char data[size]; U undo; unsigned char operator[](int i) const { return data[i]; } unsigned char& operator[](int i) { return data[i]; } unsigned char* begin(){return data;} unsigned char* end(){return data+size;} bool operator==(const C& c) const { for(int i=0; i struct hash { size_t operator()(const S& s) const { size_t r=s.size; const int P = 1e9+7; for(int i=0;i struct hash { size_t operator()(const C& s) const { size_t r=0; const int P = 1e9+7; bool mid=0; for(int i=0;i<(int)s.size && (mid || s[i]!=0); ++i) { r = P*r + s[i]; mid |= s[i]&128; } return r; } }; }; ostream& operator<<(ostream& o, const C& s) { o<<'['; bool mid=0; for(int i=0; i<(int)s.size && (mid || s[i]!=0); ++i) o<<(int)s[i]<<' ',mid=s[i]&128; o<<']'; return o; } C pack(const S& s) { C c={}; int j=0; for(int i=0; i0) { if (j>=c.size) return {}; c[j++] = char(x%128); x /= 128; } reverse(c.begin()+sj,c.begin()+j); while(sj+1c.size) cout<<"too big "< nxt; struct From { ull hash; U undo; bool operator<(const From& f) const { return hash < f.hash; } bool operator==(const From& f) const { return hash==f.hash; } }; From toFrom(const C& c) { ull p = 999999999999999989; ull h = 0; for(int i: c.data) { h = p*h + i; } return {h, c.undo}; } //unordered_map from; deque from; //deque> from; int maxL=L; void add(const S& prev, const S& st, char op) { if (st.size > maxL) return; int sum=0; for(int i=0; i= MN) return; #if 0 C pst = pack(st); cout<<"st: "<0); #if 0 auto& u = from[pack(st)]; if (!u.op) { u={op,prev[prev.size-1]}; nxt.push_back(st); } #else C c = pack(st); if (c[0]==0) return; if (binary_search(from.begin(),from.end(),toFrom(c))) return; c.undo = {op, prev[prev.size-1]}; nxt.push_back(c); #endif } void go(const S& st) { #if 0 cout<<"go "<=2) { // OVER s[c] = s[c-2]; add(st,s,'O'); #if 1 if (c>=3) { // ROT s.resize(c); s[c-3] = st[c-2]; s[c-2] = st[c-1]; s[c-1] = st[c-3]; add(st,s,'R'); s[c-3] = st[c-3]; s[c-2] = st[c-2]; s[c-1] = st[c-1]; } #endif #if 1 if (st[c-1]!=st[c-2]) { // SWAP s.resize(c); swap(s[c-2],s[c-1]); add(st,s,'S'); swap(s[c-2],s[c-1]); } #endif s.resize(c-1); // + s[c-2] += st[c-1]; if (s[c-2]1 || s[0]>1) { #if 0 cout<<"path st "<undo.apply(s); res += it->undo.op; #endif } reverse(res.begin(),res.end()); return res; } int main() { cout<)<<'\n'; deque cur; S s0; s0+=1; cur.push_back(pack(s0)); for(int i=0; i0); if (x0) { //cout< "<