#include using namespace std; using ll = long long; /********************************************************* * XOR Printer (yukicoder No.5022) * Prefix-multi-stage strategy: * - Build an XOR basis on the current board. * - Fix an order of basis vectors (<=20). After k-th COPY block, * s = prefix_xor[0..k]. We allow WRITE at each intermediate s. * - For every cell (except those used in any COPY later), choose the best * stage k (0..m) where A^s_k is maximal. Cells used for COPY are written * only after their last COPY (safe) or skipped. * - Compare the total score with the best single-s baseline (2^20 exhaustive). * If worse, fallback to the baseline solution. Non-degrading. *********************************************************/ // ===== XOR Basis over GF(2) for 20-bit ints ===== struct XorBasis { static const int B = 20; int v[B]; // basis value vector mask[B]; // indices used to form v[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]; // symmetric-difference between c and mask[b] vector a = mask[b]; sort(c.begin(), c.end()); sort(a.begin(), a.end()); vector res; res.reserve(c.size()+a.size()); size_t i=0,j=0; while(i {made_value, indices list} pair> 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]; sort(sel.begin(), sel.end()); sort(a.begin(), a.end()); vector res; res.reserve(sel.size()+a.size()); size_t i=0,j=0; while(i& A, int s){ long long sum=0; for(int x:A){ int y=x^s; sum += (y>x?y:x);} return sum; } // Greedy visit of given cell list (indices 0..NN-1). Emits opch to ops. static void visit_cells(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; donetr){ ops.push_back('U'); --r; } while(ctc){ 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; vector> A2(N, vector(N)); for(int i=0;i>A2[i][j]; // Flatten vector flatA(NN); for(int i=0;i bestScore_single){ bestScore_single=sc; bestS_single=s; } } // We'll build ops for this baseline if multi-stage fails. auto build_single_ops = [&](int S)->vector{ // Build basis on original board XorBasis B; for(int i=0;i{idx}); } auto [S_real, cellsC] = B.build(S); if(S_real != S){ // try to use realizable one if not worse long long sc = eval_single(flatA, S_real); if(sc >= bestScore_single){ bestScore_single=sc; S=S_real; } else { auto tmp=B.build(S); cellsC = tmp.second; } } vector ret; ret.reserve(1200); int r=0,c=0; visit_cells(N,r,c,cellsC,ret,'C'); // W beneficial cells vector wlist; for(int id=0; ida) wlist.push_back(id); } visit_cells(N,r,c,wlist,ret,'W'); if((int)ret.size()>T) ret.resize(T); return ret; }; // ---------- Multi-stage prefix plan ---------- // 1) Build basis on original board and collect independent vectors XorBasis B0; for(int i=0;i{idx}); } // Collect non-zero vectors struct VecInfo{ int val; vector cells; int msb; }; vector vecs; for(int b=19;b>=0;b--){ if(B0.v[b]) vecs.push_back({B0.v[b], B0.mask[b], b}); } int M = (int)vecs.size(); // prefix s list s_k (k:0..M) with s_0=0 vector prefixS(M+1,0); for(int k=1;k<=M;k++) prefixS[k] = prefixS[k-1] ^ vecs[k-1].val; // union of all C cells vector isC(NN,0); for(auto &vi:vecs){ for(int id:vi.cells) isC[id]=1; } // 2) For each cell decide best stage to write (0..M). If isC, postpone to last stage M only. vector bestStage(NN,0); // 0=no write vector baseA = flatA; for(int id=0; id bestVal){ bestVal=v; bestK=k; } } bestStage[id] = bestK; } // compute total score for plan long long score_plan = 0; for(int id=0; id ops; ops.reserve(2000); int r=0,c=0; int curS = 0; for(int k=1;k<=M;k++){ // COPY for this vector // realize vecs[k-1].val increment (since prefix) visit_cells(N, r, c, vecs[k-1].cells, ops, 'C'); curS ^= vecs[k-1].val; // WRITE cells assigned to this stage k vector wlist; wlist.reserve(NN); for(int id=0; id T) ops.resize(T); // safeguard for(char ch: ops) cout<