#include using namespace std; using ll = long long; using ld = long double; // ===== XOR Basis over GF(2) for 20-bit ints ===== struct XorBasis { static const int B = 20; // bits 0..19 int v[B]; // basis vectors (value) bitset<128> used[B]; // which cells compose this vector XorBasis(){ memset(v, 0, sizeof(v)); } void add(int x, const bitset<128>& cells){ bitset<128> c = cells; for(int b=B-1;b>=0;--b){ if(((x>>b)&1)==0) continue; if(v[b]==0){ v[b]=x; used[b]=c; return; } x ^= v[b]; c ^= used[b]; } } // return (built_value, cells_bitset) pair> build(int target) const { int t = target; int built = 0; bitset<128> ans; ans.reset(); for(int b=B-1;b>=0;--b){ if(((t>>b)&1)==0) continue; if(v[b]==0) continue; // can't set this bit t ^= v[b]; built ^= v[b]; ans ^= used[b]; } return {built, ans}; } }; // Move helper: append ops to go from (r1,c1) -> (r2,c2) static inline void move_to(int &r1, int &c1, int r2, int c2, vector& ops){ while(r1 < r2){ ops.push_back('D'); ++r1; } while(r1 > r2){ ops.push_back('U'); --r1; } while(c1 < c2){ ops.push_back('R'); ++c1; } while(c1 > c2){ ops.push_back('L'); --c1; } } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, T; if(!(cin>>N>>T)) return 0; vector> A(N, vector(N)); for(int i=0;i>A[i][j]; // ===== 1. Count per-bit gains ===== const int B = 20; vector cnt0(B,0), cnt1(B,0); for(int i=0;i>b)&1) cnt1[b]++; else cnt0[b]++; } } vector gain(B); for(int b=0;b 0) s2_target |= (1<=15 first. If none, take top few positive bits by gain. int s1_target = 0; const int TH = 15; // upper-bit threshold (0-indexed) for(int b=TH;b 0) s1_target |= (1<> cand; for(int b=0;b0) cand.push_back({gain[b], b}); sort(cand.rbegin(), cand.rend()); for(int i=0;i<(int)cand.size() && i<6;i++) s1_target |= (1< bs; bs.reset(); bs.set(i*N + j); basis.add(A[i][j], bs); } } // Build s1 and s2 from basis auto [s1_real, cells1] = basis.build(s1_target); auto [s2_real, cells2] = basis.build(s2_target); // diff cells needed to go from s1 -> s2 bitset<128> diff = cells1 ^ cells2; // ===== 5. Precompute best choice per cell ===== // choices: 0=none, 1=W at stage1 only, 2=W at stage2 only, 3=both vector choice(N*N, 0); for(int i=0;i bestv){ bestv=v1; bestc=1; } if(v2 > bestv){ bestv=v2; bestc=2; } if(v3 > bestv){ bestv=v3; bestc=3; } choice[idx] = bestc; } } vector ops; int r=0, c=0; // start (0,0) // ===== 6. Phase C1: build s1 ===== vector targets1; for(int idx=0; idx used1(targets1.size(), 0); for(size_t done=0; done> snake; snake.reserve(N*N); for(int i=0;i=0;j--) snake.emplace_back(i,j); } } if(!snake.empty()){ move_to(r,c,snake[0].first, snake[0].second, ops); for(size_t idx=0; idx targets2; for(int idx=0; idx used2(targets2.size(), 0); for(size_t done=0; done T){ // Simple fallback: cut to T (may invalidate path, but keeps WA away) ops.resize(T); } for(char ch: ops) cout << ch << '\n'; return 0; }