#include using namespace std; using ll = long long; /**************************************************** * XOR Printer – Multi-scale Block Split (Non-degrading) * * 1. Start with global best single-s (2^20 exhaustive). * 2. Then iterate block sizes sz ∈ {5,4,3,2}. * For each block (tiling the board), do: * - Exhaustively search the best s for ONLY that block. * - Realize diff via XOR-basis built on current board. * - Apply W only inside the block. * - Commit only if global score strictly increases and ops ≤ T. * 3. Stop when no block improves or time/ops limit. * * Hand-tuned for N=10, T=1000. ****************************************************/ struct XorBasis { static const int B = 20; int v[B]; vector mask[B]; XorBasis(){ memset(v,0,sizeof(v)); } void add(int x, const vector& cells){ vector c = cells; for(int b=B-1;b>=0;--b){ if(((x>>b)&1)==0) continue; if(!v[b]){ v[b]=x; mask[b]=c; return; } x ^= v[b]; vector a = mask[b], res; res.reserve(c.size()+a.size()); sort(c.begin(),c.end()); sort(a.begin(),a.end()); size_t i=0,j=0; while(i> build(int target) const{ int t=target, made=0; vector sel; for(int b=B-1;b>=0;--b){ if(((t>>b)&1)==0) continue; if(!v[b]) continue; t ^= v[b]; made ^= v[b]; vector a = mask[b], res; res.reserve(sel.size()+a.size()); sort(sel.begin(),sel.end()); sort(a.begin(),a.end()); size_t i=0,j=0; while(i& A, int s){ ll sum=0; for(int x:A){ int y=x^s; sum += (y>x?y:x);} return sum; } static void greedy_visit(int N, int &r, int &c, const vector& ids, vector& ops, char opch){ if(ids.empty()) return; vector used(ids.size(),0); for(size_t done=0; donerr){ ops.push_back('U'); --r; } while(ccc){ ops.push_back('L'); --c; } ops.push_back(opch); } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,T; if(!(cin>>N>>T)) return 0; // N=10, T=1000 const int NN=N*N, BITS=20; vector> Ain(N, vector(N)); for(int i=0;i>Ain[i][j]; // current board & state vector board(NN); for(int i=0;i ops; ops.reserve(4000); int r=0,c=0; // cursor (0-indexed) auto start = chrono::steady_clock::now(); const double TL = 1.90; // seconds auto time_ok = [&](){ return chrono::duration(chrono::steady_clock::now()-start).count() < TL; }; // ===== Global best single-s first ===== ll baseSum=0; for(int v:board) baseSum+=v; int bestS=0; ll bestGain=0; for(int s=0;s<(1<x?y:x);} ll g=tot-baseSum; if(g>bestGain){bestGain=g; bestS=s;} } if(bestGain>0){ XorBasis B; for(int i=0;i{idx}); } int diff = curS ^ bestS; auto [diffReal, needC] = B.build(diff); int newS = curS ^ diffReal; vector wCells; wCells.reserve(NN); ll afterSum=0; for(int id=0; ida){ wCells.push_back(id); afterSum+=b; } else afterSum+=a; } if(afterSum>baseSum){ greedy_visit(N,r,c,needC,ops,'C'); curS=newS; greedy_visit(N,r,c,wCells,ops,'W'); for(int id:wCells) board[id]^=curS; } } // ===== Block sizes loop ===== vector sizes = {5,4,3,2}; for(int sz : sizes){ bool any_improve = true; while(any_improve && time_ok() && (int)ops.size() <= T-200){ any_improve = false; for(int rs=0; rs ids; ids.reserve(sz*sz); for(int i=rs;ia?b:a);} ll g=tot-blkSum; if(g>gain){gain=g; ts=s;} } if(gain<=0) continue; // build basis on whole board XorBasis B; for(int i=0;i{idx}); } int diff = curS ^ ts; auto [diffReal, needC] = B.build(diff); int newS = curS ^ diffReal; // recompute real gain on this block vector wCells; wCells.reserve(ids.size()); ll newBlk=0; for(int id:ids){ int a=board[id]; int b=a^newS; if(b>a){ wCells.push_back(id); newBlk+=b; } else newBlk+=a; } ll realGain = newBlk - blkSum; if(realGain<=0) continue; // simulate ops vector tmp; tmp.reserve(needC.size()*6 + wCells.size()*6 + 10); int tr=r, tc=c; greedy_visit(N,tr,tc,needC,tmp,'C'); greedy_visit(N,tr,tc,wCells,tmp,'W'); if((int)ops.size() + (int)tmp.size() > T) continue; // commit ops.insert(ops.end(), tmp.begin(), tmp.end()); r=tr; c=tc; curS=newS; for(int id:wCells) board[id]^=curS; any_improve = true; if(!time_ok() || (int)ops.size()>T-200) break; } if(!time_ok() || (int)ops.size()>T-200) break; } } } if((int)ops.size()>T) ops.resize(T); for(char ch: ops) cout<