Problem:
Shifting a Matrix
You are given $N \times N$ matrix $A$ initialized with $A_{i,j} = (i - 1)N + j$, where $A_{i,j}$ is the entry of the $i$-th row and the $j$-th column of $A$. Note that $i$ and $j$ are 1-based.
You are also given an operation sequence which consists of the four types of shift operations: left, right, up, and down shifts. More precisely, these operations are defined as follows:
Left shift with $i$: circular shift of the $i$-th row to the left, i.e., setting previous $A_{i,k}$ to new $A_{i,k-1}$ for $2 \leq k \leq N$, and previous $A_{i,1}$ to new $A_{i,N}$.
Right shift with $i$: circular shift of the $i$-th row to the right, i.e., setting previous $A_{i,k}$ to new $A_{i,k+1}$ for $1 \leq k \leq N - 1$, and previous $A_{i,N}$ to new $A_{i,1}$.
Up shift with $j$: circular shift of the $j$-th column to the above, i.e., setting previous $A_{k,j}$ to new $A_{k-1,j}$ for $2 \leq k \leq N$, and previous $A_{1,j}$ to new $A_{N,j}$.
Down shift with $j$: circular shift of the $j$-th column to the below, i.e., setting previous $A_{k,j}$ to new $A_{k+1,j}$ for $1 \leq k \leq N - 1$, and previous $A_{N,j}$ to new $A_{1,j}$.
An operation sequence is given as a string. You have to apply operations to a given matrix from left to right in a given string. Left, right, up, and down shifts are referred as 'L', 'R', 'U', and 'D' respectively in a string, and the following number indicates the row/column to be shifted. For example, "R25" means we should perform right shift with 25. In addition, the notion supports repetition of operation sequences. An operation sequence surrounded by a pair of parentheses must be repeated exactly $m$ times, where $m$ is the number following the close parenthesis. For example, "(L1R2)10" means we should repeat exactly 10 times the set of the two operations:
left shift with 1 and right shift with 2 in this order.
Given operation sequences are guaranteed to follow the following BNF:
<sequence> := <sequence><repetition> | <sequence><operation> | <repetition> | <operation>
<repetition> := '('<sequence>')'<number>
<operation> := <shift><number>
<shift> := 'L' | 'R' | 'U' | 'D'
<number> := <nonzero_digit> |<number><digit>
<digit> := '0' | <nonzero_digit>
<nonzero_digit> := '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
Given $N$ and an operation sequence as a string, make a program to compute the $N \times N$ matrix after operations indicated by the operation sequence.
Input
The input consists of a single test case. The test case is formatted as follows.
$N$ $L$
$S$
The first line contains two integers $N$ and $L$, where $N$ ($1 \leq N \leq 100$) is the size of the given matrix and $L$ ($2 \leq L \leq 1,000$) is the length of the following string. The second line contains a string $S$ representing the given operation sequence. You can assume that $S$ follows the above BNF. You can also assume numbers representing rows and columns are no less than 1 and no more than $N$, and the number of each repetition is no less than 1
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

ll n, L;
string s;
ll pos = 0;
ll number();
vector<ll> f(const vector<ll>& s1, const vector<ll>& s2) {
    vector<ll> res(n*n);
    rep(i, n*n) res[i] = s1[s2[i]];
    return res;
}
vector<ll> E(ll n) {
    vector<ll> res(n*n);
    rep(i, res.size()) res[i] = i;
    return res;
}
vector<ll> power(vector<ll> x, ll m) {
    vector<ll> res = E(n);
    for (ll i = 1; i <= m; i <<= 1) {
        if (i & m) res = f(res, x);
        x = f(x, x);
    }
    return res;
}
void read_char(char c) {
    assert(pos < L && s[pos] == c);
    ++pos;
}
ll ID(ll x, ll y) { return y * n + x; }
vector<ll> S() {
    vector<ll> res = E(n);
    while (pos < L) {
        // cout << s[pos] << endl;
        // op
        if (s[pos] == '(') {
            read_char('(');
            vector<ll> op = S();
            read_char(')');
            ll times = number();
            res = f(res, power(op, times));
        }
        else if (s[pos] == ')') {
            break;
        }
        else {
            ll opc = s[pos++];
            assert(opc == 'L' || opc == 'R' || opc == 'U' || opc == 'D');
            ll idx = number()-1;
            assert(0 <= idx && idx < n);
            if (opc == 'L') {
                ll temp = res[ID(0, idx)];
                rep(x, n) {
                    res[ID(x, idx)] = x == n-1 ? temp : res[ID(x+1, idx)];
                }
            }
            else if (opc == 'R') {
                ll temp = res[ID(n-1, idx)];
                rrep(x, n-1) {
                    res[ID(x+1, idx)] = res[ID(x, idx)];
                }
                res[ID(0, idx)] = temp;
            }
            else if (opc == 'U') {
                ll temp = res[ID(idx, 0)];
                rep(y, n) {
                    res[ID(idx, y)] = y == n-1 ? temp : res[ID(idx, y+1)];
                }
            }
            else if (opc == 'D') {
                ll temp = res[ID(idx, n-1)];
                rrep(y, n-1) {
                    res[ID(idx, y+1)] = res[ID(idx, y)];
                }
                res[ID(idx, 0)] = temp;
            }
        }
    }
    return res;
}
ll number() {
    string str = "";
    while (pos < L && isdigit(s[pos])) {
        str += s[pos++];
    }
    return stoll(str);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> L;
    cin >> s;
    L = s.size();
    vector<ll> A = S();
    vector<vector<ll>> ans(n, vector<ll>(n, -1));
    rep(y, n) rep(x, n) {
        ll k = y*n+x;
        ans[y][x] = A[k]+1;
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 29504
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;
string s;
int n,l,k,m;
struct node
{
	int to[10005];
	int &operator[](int x)
	{
		return to[x];
	};
}zh,ans;
node operator*(node a,node b)
{
	node res;
	for(int i=0;i<m;i++)
		res[i]=a[b[i]];
	return res;
}
int gint()
{
	int res=s[k++]-'0';
	while(isdigit(s[k]))
		res=10*res+s[k++]-'0';
	return res;
}
void dfs(node &a)
{
	char x=s[k++];
	int k=gint()-1;
	if(x=='R')
		for(int j=n-2;j>=0;j--)
			swap(a[k*n+j],a[k*n+j+1]);
	if(x=='L')
		for(int j=0;j<n-1;j++)
			swap(a[k*n+j],a[k*n+j+1]);
	if(x=='D')
		for(int i=n-2;i>=0;i--)
			swap(a[i*n+k],a[(i+1)*n+k]);
	if(x=='U')
		for(int i=0;i<n-1;i++)
			swap(a[i*n+k],a[(i+1)*n+k]);
}
void rec2(node &a);
void rec(node &a)
{
	while(k<l&&s[k]!=')')
	{
		node b=zh;
		if(s[k]=='(')
			rec2(b),a=a*b;
		else 
			dfs(b),a=a*b;
	}
}
void rec2(node &a)
{
	node b=zh;
	k++,rec(b),k++;
	int m=gint();
	a=zh;
	while(m)
	{
		if(m&1)
			a=a*b;
		b=b*b;
		m>>=1;
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>n>>l>>s;
	m=n*n;
	for(int i=0;i<m;i++)
		zh[i]=i;
	ans=zh,rec(ans);
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			cout<<ans[i*n+j]+1<<(j==n-1?"\n":" ");
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 107392
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll m, n;
    cin >> m >> n;
    string s; cin >> s;
    vector<int> right(n);
    stack<int> st;
    for(int i = n - 1; i >= 0; i --) {
        if(s[i] == '(') {
            right[i] = st.top();
            st.pop();
        } else if(s[i] == ')') {
            st.push(i); 
        }
    }
    auto is_digit = [&](char c) -> bool {
        return ('0' <= c and c <= '9');
    };

    using ar = array<int, 10000>;
    auto mul = [&](ar a, ar b) -> ar {
        ar ans;
        rep(i, m * m) ans[i] = b[a[i]];
        return ans;
    };
    auto solve = [&](ar a, ll num) -> ar {
        ar ret;
        rep(i, m * m) ret[i] = i;
        while(num) {
            if(num & 1) {
                ret = mul(ret, a);
            }
            a = mul(a, a);
            num /= 2;
        }
        return ret;
    };
    auto f = [&](auto f, int le, int ri) -> ar {
        ar ret;
        rep(i, m * m) ret[i] = i;
        char c = 'a';
        for(int i = le; i < ri; i ++) {
            if(s[i] == '(') {
                auto r = f(f, i + 1, right[i]);
                i = right[i] + 1;
                string t = "";
                for(; i < ri; i ++) {
                    if(is_digit(s[i])) t += s[i];
                    else break;
                }
                i --;
                ll num = stoll(t);
                r = solve(r, num);
                
                ret = mul(ret, r);
            } else if(is_digit(s[i])) {
                string t = "";
                for(; i < ri; i ++) {
                    if(is_digit(s[i])) t += s[i];
                    else break;
                }
                
                i --;
                ll num = stoll(t) - 1;
                if(c == 'R') {
                    rep(j, m * m) {
                        if(ret[j] / m == num) {
                            ret[j] ++;
                            if(ret[j] % m == 0) ret[j] -= m;
                        }
                    }
                } else if(c == 'L') {
                    rep(j, m * m) {
                        if(ret[j] / m == num) {
                            if(ret[j] % m == 0) ret[j] += m;
                            ret[j] --;
                        }
                    }
                } else if(c == 'U') {
                    rep(j, m * m) {
                        if(ret[j] % m == num) {
                            ret[j] -= m;
                            if(ret[j] < 0) ret[j] += m * m;
                        }
                    }
                } else {
                    rep(j, m * m) {
                        if(ret[j] % m == num) {
                            ret[j] += m;
                            if(ret[j] >= m * m) ret[j] -= m * m;
                        }
                    }
                }
            } else {
                c = s[i];
            }
        }
        return ret;
    };

    auto a = f(f, 0, n);
    vector<int> ans(m * m);
    rep(i, m * m) ans[a[i]] = i + 1;
    rep(i, m) {
        rep(j, m) cout << ans[i * m + j] << (j == m - 1 ? '\n' : ' ');
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 81328
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll m, n;
    cin >> m >> n;
    string s; cin >> s;
    vector<int> right(n);
    stack<int> st;
    for(int i = n - 1; i >= 0; i --) {
        if(s[i] == '(') {
            right[i] = st.top();
            st.pop();
        } else if(s[i] == ')') {
            st.push(i); 
        }
    }
    auto is_digit = [&](char c) -> bool {
        return ('0' <= c and c <= '9');
    };

    using ar = array<array<ll, 100>, 100>;
    auto mul = [&](ar a, ar b) -> ar {
        ar ans{};
        rep(i, m) rep(j, m) ans[i][j] = b[a[i][j] / m][a[i][j] % m];
        return ans;
    };
    auto solve = [&](ar a, ll num) -> ar {
        ar ret{};
        rep(i, m) rep(j, m) ret[i][j] = i * m + j;
        while(num) {
            if(num & 1) {
                ret = mul(ret, a);
            }
            a = mul(a, a);
            num /= 2;
        }
        return ret;
    };
    auto f = [&](auto f, int le, int ri) -> ar {
        ar ret{};
        rep(i, m) rep(j, m) ret[i][j] = i * m + j;
        char c = 'a';
        for(int i = le; i < ri; i ++) {
            if(s[i] == '(') {
                auto r = f(f, i + 1, right[i]);
                i = right[i] + 1;
                string t = "";
                for(; i < ri; i ++) {
                    if(is_digit(s[i])) t += s[i];
                    else break;
                }
                i --;
                ll num = stoll(t);
                r = solve(r, num);
                
                ret = mul(ret, r);
            } else if(is_digit(s[i])) {
                string t = "";
                for(; i < ri; i ++) {
                    if(is_digit(s[i])) t += s[i];
                    else break;
                }
                ar tmp = ret;
                rep(y, m) rep(x, m) ret[y][x] = y * m + x;
                i --;
                ll num = stoll(t) - 1;
                if(c == 'R') {
                    rep(j, m) {
                        ll y = ret[num][j] / m;
                        ll x = ret[num][j] % m;
                        x ++;
                        if(x >= m) x -= m;
                        ret[num][j] = y * m + x;
                    }
                } else if(c == 'L') {
                    rep(j, m) {
                        ll y = ret[num][j] / m;
                        ll x = ret[num][j] % m;
                        x --;
                        if(x < 0) x += m;
                        ret[num][j] = y * m + x;
                    }
                } else if(c == 'U') {
                    rep(j, m) {
                        ll y = ret[j][num] / m;
                        ll x = ret[j][num] % m;
                        y --;
                        if(y < 0) y += m;
                        ret[j][num] = y * m + x;
                    }
                } else {
                    rep(j, m) {
                        ll y = ret[j][num] / m;
                        ll x = ret[j][num] % m;
                        y ++;
                        if(y >= m) y -= m;
                        ret[j][num] = y * m + x;
                    }
                }
                ar ans;
                rep(y, m) rep(x, m) ans[y][x] = ret[tmp[y][x] / m][tmp[y][x] % m];
                swap(ans, ret);
            } else {
                c = s[i];
            }
        }
        return ret;
    };

    auto a = f(f, 0, n);
    vector mat(m, vector(m, 0LL));
    rep(i, m) rep(j, m) mat[i][j] = m * i + j + 1;
    vector ans(m, vector(m, 0LL));
    rep(i, m) rep(j, m) ans[a[i][j] / m][a[i][j] % m] = mat[i][j];
    rep(i, m) {
        rep(j, m) cout << ans[i][j] << (j == m - 1 ? '\n' : ' ');
    }
    return 0;
}
accuracy: 0.9795918367346939
time_ms: 100
memory_kb: 49280
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll m, n;
    cin >> m >> n;
    string s; cin >> s;
    vector<int> right(n);
    stack<int> st;
    for(int i = n - 1; i >= 0; i --) {
        if(s[i] == '(') {
            right[i] = st.top();
            st.pop();
        } else if(s[i] == ')') {
            st.push(i); 
        }
    }
    auto is_digit = [&](char c) -> bool {
        return ('0' <= c and c <= '9');
    };

    using ar = array<array<ll, 100>, 100>;
    auto mul = [&](ar a, ar b) -> ar {
        ar ans;
        rep(i, m) rep(j, m) ans[i][j] = b[a[i][j] / m][a[i][j] % m];
        return ans;
    };
    auto solve = [&](ar a, ll num) -> ar {
        ar ret;
        rep(i, m) rep(j, m) ret[i][j] = i * m + j;
        while(num) {
            if(num & 1) {
                ret = mul(ret, a);
            }
            a = mul(a, a);
            num /= 2;
        }
        return ret;
    };
    auto f = [&](auto f, int le, int ri) -> ar {
        ar ret;
        rep(i, m) rep(j, m) ret[i][j] = i * m + j;
        char c = 'a';
        for(int i = le; i < ri; i ++) {
            if(s[i] == '(') {
                auto r = f(f, i + 1, right[i]);
                i = right[i] + 1;
                string t = "";
                for(; i < ri; i ++) {
                    if(is_digit(s[i])) t += s[i];
                    else break;
                }
                i --;
                ll num = stoll(t);
                r = solve(r, num);
                
                ret = mul(ret, r);
            } else if(is_digit(s[i])) {
                string t = "";
                for(; i < ri; i ++) {
                    if(is_digit(s[i])) t += s[i];
                    else break;
                }
                ar tmp = ret;
                rep(y, m) rep(x, m) ret[y][x] = y * m + x;
                i --;
                ll num = stoll(t) - 1;
                if(c == 'R') {
                    rep(j, m) {
                        ll y = ret[num][j] / m;
                        ll x = ret[num][j] % m;
                        x ++;
                        if(x >= m) x -= m;
                        ret[num][j] = y * m + x;
                    }
                } else if(c == 'L') {
                    rep(j, m) {
                        ll y = ret[num][j] / m;
                        ll x = ret[num][j] % m;
                        x --;
                        if(x < 0) x += m;
                        ret[num][j] = y * m + x;
                    }
                } else if(c == 'U') {
                    rep(j, m) {
                        ll y = ret[j][num] / m;
                        ll x = ret[j][num] % m;
                        y --;
                        if(y < 0) y += m;
                        ret[j][num] = y * m + x;
                    }
                } else {
                    rep(j, m) {
                        ll y = ret[j][num] / m;
                        ll x = ret[j][num] % m;
                        y ++;
                        if(y >= m) y -= m;
                        ret[j][num] = y * m + x;
                    }
                }
                ar ans;
                rep(y, m) rep(x, m) ans[y][x] = ret[tmp[y][x] / m][tmp[y][x] % m];
                swap(ans, ret);
            } else {
                c = s[i];
            }
        }
        return ret;
    };

    auto a = f(f, 0, n);
    vector mat(m, vector(m, 0LL));
    rep(i, m) rep(j, m) mat[i][j] = m * i + j + 1;
    vector ans(m, vector(m, 0LL));
    rep(i, m) rep(j, m) ans[a[i][j] / m][a[i][j] % m] = mat[i][j];
    rep(i, m) {
        rep(j, m) cout << ans[i][j] << (j == m - 1 ? '\n' : ' ');
    }
    return 0;
}
accuracy: 0.9795918367346939
time_ms: 240
memory_kb: 48220
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll m, n;
    cin >> m >> n;
    string s; cin >> s;
    vector<int> right(n);
    stack<int> st;
    for(int i = n - 1; i >= 0; i --) {
        if(s[i] == '(') {
            right[i] = st.top();
            st.pop();
        } else if(s[i] == ')') {
            st.push(i); 
        }
    }
    auto is_digit = [&](char c) -> bool {
        int num = c - '0';
        return (0 <= num and num < 10);
    };

    using ar = array<array<ll, 100>, 100>;
    auto mul = [&](ar a, ar b) -> ar {
        ar ans;
        rep(i, m) rep(j, m) ans[i][j] = b[a[i][j] / m][a[i][j] % m];
        return ans;
    };
    auto solve = [&](ar a, ll num) -> ar {
        ar ret;
        rep(i, m) rep(j, m) ret[i][j] = i * m + j;
        while(num) {
            if(num & 1) {
                ret = mul(ret, a);
            }
            a = mul(a, a);
            num /= 2;
        }
        return ret;
    };
    auto f = [&](auto f, int le, int ri) -> ar {
        ar ret;
        rep(i, m) rep(j, m) ret[i][j] = i * m + j;
        char c = 'a';
        for(int i = le; i < ri; i ++) {
            if(s[i] == '(') {
                auto r = f(f, i + 1, right[i]);
                i = right[i] + 1;
                string t = "";
                for(; i < ri; i ++) {
                    if(is_digit(s[i])) t += s[i];
                    else break;
                }
                i --;
                ll num = stoll(t);
                r = solve(r, num);
                
                ret = mul(ret, r);
            } else if(is_digit(s[i])) {
                string t = "";
                for(; i < ri; i ++) {
                    if(is_digit(s[i])) t += s[i];
                    else break;
                }
                ar tmp = ret;
                rep(y, m) rep(x, m) ret[y][x] = y * m + x;
                i --;
                ll num = stoll(t) - 1;
                if(c == 'R') {
                    rep(j, m) {
                        ll y = ret[num][j] / m;
                        ll x = ret[num][j] % m;
                        x ++;
                        if(x >= m) x -= m;
                        ret[num][j] = y * m + x;
                    }
                } else if(c == 'L') {
                    rep(j, m) {
                        ll y = ret[num][j] / m;
                        ll x = ret[num][j] % m;
                        x --;
                        if(x < 0) x += m;
                        ret[num][j] = y * m + x;
                    }
                } else if(c == 'U') {
                    rep(j, m) {
                        ll y = ret[j][num] / m;
                        ll x = ret[j][num] % m;
                        y --;
                        if(y < 0) y += m;
                        ret[j][num] = y * m + x;
                    }
                } else {
                    rep(j, m) {
                        ll y = ret[j][num] / m;
                        ll x = ret[j][num] % m;
                        y ++;
                        if(y >= m) y -= m;
                        ret[j][num] = y * m + x;
                    }
                }
                ar ans;
                rep(y, m) rep(x, m) ans[y][x] = ret[tmp[y][x] / m][tmp[y][x] % m];
                swap(ans, ret);
            } else {
                c = s[i];
            }
        }
        return ret;
    };

    auto a = f(f, 0, n);
    vector mat(m, vector(m, 0LL));
    rep(i, m) rep(j, m) mat[i][j] = m * i + j + 1;
    vector ans(m, vector(m, 0LL));
    rep(i, m) rep(j, m) ans[a[i][j] / m][a[i][j] % m] = mat[i][j];
    rep(i, m) {
        rep(j, m) cout << ans[i][j] << (j == m - 1 ? '\n' : ' ');
    }
    return 0;
}
accuracy: 0.9795918367346939
time_ms: 240
memory_kb: 48244
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto&& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll m, n;
    cin >> m >> n;
    string s; cin >> s;
    vector<int> right(n);
    stack<int> st;
    for(int i = n - 1; i >= 0; i --) {
        if(s[i] == '(') {
            right[i] = st.top();
            st.pop();
        } else if(s[i] == ')') {
            st.push(i); 
        }
    }
    auto is_digit = [&](char c) -> bool {
        int num = c - '0';
        return (0 <= num and num < 10);
    };

    using ar = array<array<ll, 100>, 100>;
    auto mul = [&](ar a, ar b) -> ar {
        ar ans;
        rep(i, m) rep(j, m) ans[i][j] = b[a[i][j] / m][a[i][j] % m];
        return ans;
    };
    auto solve = [&](ar a, ll num) -> ar {
        ar ret;
        rep(i, m) rep(j, m) ret[i][j] = i * m + j;
        
        while(num) {
            if(num & 1) {
                ret = mul(ret, a);
            }
            a = mul(a, a);
            num /= 2;
            
        }
        return ret;
    };
    auto f = [&](auto f, int le, int ri) -> ar {
        ar ret;
        rep(i, m) rep(j, m) ret[i][j] = i * m + j;
        char c = 'a';
        for(int i = le; i < ri; i ++) {
            if(s[i] == '(') {
                auto r = f(f, i + 1, right[i]);
                // rep(y, 3) rep(x, 3) cout << r[y][x] << " ";
                // cout << endl;
                i = right[i] + 1;
                string t = "";
                for(; i < ri; i ++) {
                    if(is_digit(s[i])) t += s[i];
                    else break;
                }
                i --;
                ll num = stoll(t);
                r = solve(r, num);
                
                ret = mul(ret, r);
            } else if(is_digit(s[i])) {
                string t = "";
                for(; i < ri; i ++) {
                    if(is_digit(s[i])) t += s[i];
                    else break;
                }
                ar tmp = ret;
                rep(y, m) rep(x, m) ret[y][x] = y * m + x;
                i --;
                ll num = stoll(t) - 1;
                if(c == 'R') {
                    rep(j, m) {
                        ll y = ret[num][j] / m;
                        ll x = ret[num][j] % m;
                        x ++;
                        if(x >= m) x -= m;
                        ret[num][j] = y * m + x;
                    }
                } else if(c == 'L') {
                    rep(j, m) {
                        ll y = ret[num][j] / m;
                        ll x = ret[num][j] % m;
                        x --;
                        if(x < 0) x += m;
                        ret[num][j] = y * m + x;
                    }
                } else if(c == 'U') {
                    rep(j, m) {
                        ll y = ret[j][num] / m;
                        ll x = ret[j][num] % m;
                        y --;
                        if(y < 0) y += m;
                        ret[j][num] = y * m + x;
                    }
                } else {
                    rep(j, m) {
                        ll y = ret[j][num] / m;
                        ll x = ret[j][num] % m;
                        y ++;
                        if(y >= m) y -= m;
                        ret[j][num] = y * m + x;
                    }
                }
                ar ans;
                rep(y, m) rep(x, m) ans[y][x] = ret[tmp[y][x] / m][tmp[y][x] % m];
                swap(ans, ret);
            } else {
                c = s[i];
            }
        }
        return ret;
    };

    auto a = f(f, 0, n);
    vector mat(m, vector(m, 0LL));
    rep(i, m) rep(j, m) mat[i][j] = m * i + j + 1;
    vector ans(m, vector(m, 0LL));
    rep(i, m) rep(j, m) ans[a[i][j] / m][a[i][j] % m] = mat[i][j];
    rep(i, m) {
        rep(j, m) cout << ans[i][j] << (j == m - 1 ? '\n' : ' ');
    }
    return 0;
}
accuracy: 0.9795918367346939
time_ms: 240
memory_kb: 48204
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;

using State=string::const_iterator;
void Consume(State &begin,char expected){
	assert(*begin==expected);
	begin++;
}

int N;

vector<vector<pair<int,int>>>RotateR(const vector<vector<pair<int,int>>>&x,int n,int cnt){
	auto ret=x;
	for(int i=0;i<N;i++)ret[n][i]=x[n][(i+cnt)%N];
	return ret;
}

vector<vector<pair<int,int>>>RotateD(const vector<vector<pair<int,int>>>&x,int n,int cnt){
	auto ret=x;
	for(int i=0;i<N;i++)ret[i][n]=x[(i+cnt)%N][n];
	return ret;
}

vector<vector<pair<int,int>>> Merge(const vector<vector<pair<int,int>>>&l,const vector<vector<pair<int,int>>>&r){
	vector<vector<pair<int,int>>> ret(N,vector<pair<int,int>>(N));
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			auto [x,y]=l[i][j];
			ret[i][j]=r[x][y];
		}
	}
	return ret;
}

vector<vector<pair<int,int>>> Pow(vector<vector<pair<int,int>>>a,ll n){
	vector<vector<pair<int,int>>> ret(N,vector<pair<int,int>>(N));
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)ret[i][j]={i,j};

	while(n){
		if(n&1){
			ret=Merge(ret,a);
		}
		a=Merge(a,a);
		n/=2;
	}

	return ret;
}

vector<vector<pair<int,int>>> Func(State &itr){
	if(*itr!='('){
		vector<vector<pair<int,int>>>ret(N,vector<pair<int,int>>(N));
		for(int i=0;i<N;i++)for(int j=0;j<N;j++)ret[i][j]={i,j};
		char d=*itr;
		itr++;
		string num;
		while(isdigit(*itr)){
			num+=*itr;
			itr++;
		}
		int n=stoi(num)-1;
		if(d=='R')ret=RotateR(ret,n,1);
		if(d=='L')ret=RotateR(ret,n,N-1);
		if(d=='U')ret=RotateD(ret,n,N-1);
		if(d=='D')ret=RotateD(ret,n,1);

		return ret;
	}

	Consume(itr,'(');
	auto ret=Func(itr);
	while(*itr!=')'){
		ret=Merge(ret,Func(itr));
	}
	Consume(itr,')');

	string num;
	while(isdigit(*itr)){
		num+=*itr;
		itr++;
	}
	ll m=stoll(num);
	//ret^m
	ret=Pow(ret,m);
	return ret;
}

int main(){
	int L;
	string S;
	cin>>N>>L>>S;
	S="("+S+")1";

	State itr=S.begin();
	auto ret=Func(itr);

	vector<vector<int>>ans(N,vector<int>(N));
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			auto[x,y]=ret[i][j];
			ans[x][y]=i*N+j+1;
		}
	}

	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			if(j)cout<<' ';
			cout<<ans[i][j];
		}
		cout<<endl;
	}
}
accuracy: 1
time_ms: 60
memory_kb: 5248
final_rank: 1

code_snippet: #include <iostream>
#include <cassert>
#include <vector>
#include <string>
#include <numeric>
#include <cassert>
void print(const std::vector<std::vector<int>>& A) {
    for (int i{} ; i < (int)A.size() ; i++) {
        for (int j{} ; j < (int)A[i].size() ; j++) std::cout << A[i][j] << (j + 1 == (int)A[i].size() ? '\n' : ' ');
    }
}
// G(F)
std::vector<std::vector<int>> permute(std::vector<std::vector<int>> F, std::vector<std::vector<int>> G) {
    // print(F);
    // std::cout << "---------------------------\n";
    // print(G);
    // std::cout << "---------------------------\n";
    std::vector res(F.size(), std::vector<int>(F[0].size(), -1));
    for (int i{} ; i < (int)G.size() ; i++) {
        for (int j{} ; j < (int)G[i].size() ; j++) {
            int row{G[i][j] / (int)G.size()}, col{G[i][j] % (int)G.size()};
            res[i][j] = F[row][col];
        }
    }
    return res;
}
std::vector<std::vector<int>> pow(std::vector<std::vector<int>> F, int K) {
    std::vector res(F.size(), std::vector<int>(F.size()));
    for (int i{} ; i < (int)F.size() ; i++) {
        for (int j{} ; j < (int)F.size() ; j++) res[i][j] = i * (int)F.size() + j;
    }
    while (K) {
        if (K & 1) res = permute(res, F);
        F = permute(F, F);
        K >>= 1;
    }
    return res;
}
struct Perser {
    int it{}, N{};
    std::string S;
    Perser(int n, std::string s) : N{n}, S{s} {
    }
    std::vector<std::vector<int>> left(int p) const {
        std::vector res(N, std::vector<int>(N));
        for (int i{} ; i < N ; i++) for (int j{} ; j < N ; j++) res[i][j] = i * N + j;
        for (int j{1} ; j < N ; j++) 
            res[p][j - 1] = res[p][j];
        res[p][N - 1] = p * N;
        return res;
    }
    std::vector<std::vector<int>> right(int p) const {
        std::vector res(N, std::vector<int>(N));
        for (int i{} ; i < N ; i++) for (int j{} ; j < N ; j++) res[i][j] = i * N + j;
        for (int j{N - 2} ; j >= 0 ; j--)
            res[p][j + 1] = res[p][j];
        res[p][0] = (p + 1) * N - 1;
        return res;
    }
    std::vector<std::vector<int>> up(int p) const {
        std::vector res(N, std::vector<int>(N));
        for (int i{} ; i < N ; i++) for (int j{} ; j < N ; j++) res[i][j] = i * N + j;
        for (int i{1} ; i < N ; i++)
            res[i - 1][p] = res[i][p];
        res[N - 1][p] = p;
        return res;
    }
    std::vector<std::vector<int>> down(int p) const {
        std::vector res(N, std::vector<int>(N));
        for (int i{} ; i < N ; i++) for (int j{} ; j < N ; j++) res[i][j] = i * N + j;
        for (int i{N - 2} ; i >= 0 ; i--)
            res[i + 1][p] = res[i][p];
        res[0][p] = (N - 1) * N + p;
        return res;
    }
    bool is_digit(int i) {
        return 0 <= i and i < (int)S.size() and '0' <= S[i] and S[i] <= '9';
    }
    int number() {
        int res{};
        while (is_digit(it)) {
            res = res * 10 + (S[it] - '0');
            it++;
        }
        return res;
    }
    std::vector<std::vector<int>> rec() {
        if (it == (int)S.size()) {
            std::vector res(N, std::vector<int>(N));
            for (int i{} ; i < N ; i++) for (int j{} ; j < N ; j++) res[i][j] = i * N + j;
            return res;
        }
        if (S[it] == ')') {
            std::vector res(N, std::vector<int>(N));
            for (int i{} ; i < N ; i++) for (int j{} ; j < N ; j++) res[i][j] = i * N + j;
            return res;
        }
        if (S[it] == '(') {
            it++;
            auto cur{rec()};
            assert(it < (int)S.size() and S[it] == ')');
            it++;
            int rep{number()};
            auto res{pow(cur, rep)};
            return permute(res, rec());
        }
        else if (S[it] == 'L') {
            it++;
            assert(is_digit(it));
            int p{number()};
            return permute(left(p - 1), rec());
        }
        else if (S[it] == 'R') {
            it++;
            assert(is_digit(it));
            int p{number()};
            return permute(right(p - 1), rec());
        }
        else if (S[it] == 'U') {
            it++;
            assert(is_digit(it));
            int p{number()};
            return permute(up(p - 1), rec());
        }
        else if (S[it] == 'D') {
            it++;
            assert(is_digit(it));
            int p{number()};
            // std::cout << p << "!" << std::endl;
            return permute(down(p - 1), rec());
        }
        assert(false);
        return std::vector<std::vector<int>>{};
    }
};
int N, L;
std::string S;
void solve() {
    auto ans{Perser{N, S}.rec()};
    for (int i{} ; i < N ; i++) {
        for (int j{} ; j < N ; j++) std::cout << ans[i][j] + 1 << (j + 1 == N ? '\n' : ' ');
    }
}
int main() {
    std::cin >> N >> L >> S;
    solve();
}
accuracy: 1
time_ms: 110
memory_kb: 4068
final_rank: 8

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int N, L, K;

vector<int> e() {
    vector<int> Ret(K);
    iota(ALL(Ret),0);
    return Ret;
}

vector<int> Mul(vector<int> A, vector<int> B) {
    vector<int> Ret(K);
    rep(i,0,K) Ret[i] = A[B[i]];
    return Ret;
}

vector<int> Pow(vector<int> A, int X) {
    if (X == 0) {
        return e();
    }
    if (X % 2 == 0) {
        auto Ret = Pow(A,X/2);
        return Mul(Ret,Ret);
    }
    return Mul(A,Pow(A,X-1));
}

string S;

vector<int> Seq(int&);
vector<int> Op(char, int);
int Num(int&);

vector<int> Seq(int& ID) {
    if (ID == S.size() || S[ID] == ')') return e();
    if (S[ID] == '(') {
        ID++;
        auto A = Seq(ID);
        ID++;
        int X = Num(ID);
        auto B = Seq(ID);
        return Mul(Pow(A,X),B);
    }
    else {
        char C = S[ID];
        ID++;
        int X = Num(ID);
        auto B = Seq(ID);
        return Mul(Op(C,X),B);
    }
}

vector<int> Op(char C, int X) {
    vector<vector<int>> A(N,vector<int>(N));
    rep(i,0,N) rep(j,0,N) A[i][j] = i*N+j;
    X--;
    if (C == 'L') {
        rep(j,0,N) A[X][j] = X*N+((j+1)%N);
    }
    if (C == 'R') {
        rep(j,0,N) A[X][j] = X*N+((j+N-1)%N);
    }
    if (C == 'U') {
        rep(i,0,N) A[i][X] = ((i+1)%N)*N+X;
    }
    if (C == 'D') {
        rep(i,0,N) A[i][X] = ((i+N-1)%N)*N+X;
    }
    vector<int> Ret(K);
    rep(i,0,N) rep(j,0,N) Ret[i*N+j] = A[i][j];
    return Ret;
}

int Num(int& ID) {
    int Ret = 0;
    while(ID < S.size() && isdigit(S[ID])) {
        Ret *= 10;
        Ret += S[ID] - '0';
        ID++;
    }
    return Ret;
}

int main() {
    cin >> N >> L >> S;
    K = N*N;
    int it = 0;
    vector<int> ANS = Seq(it);
    rep(i,0,N) {
        rep(j,0,N) {
            cout << ANS[i*N+j]+1 << (j==N-1 ? '\n' : ' ');
        }
    }
}
accuracy: 1
time_ms: 80
memory_kb: 5516
final_rank: 4

code_snippet: #include <bits/stdc++.h>

struct Mapping {
    std::vector<std::vector<std::pair<int, int>>> data;
    Mapping(int n): data(n, std::vector<std::pair<int, int>>(n)) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                data[i][j] = {i, j};
            }
        }
    }

    Mapping(int n, char c, int k): data(n, std::vector<std::pair<int, int>>(n)) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                data[i][j] = {i, j};
            }
        }

        if (c == 'R') {
            for (int i = 0; i < n; i++) {
                data[k][i] = {k, (i + 1) % n};
            }
        } else if (c == 'L') {
            for (int i = 0; i < n; i++) {
                data[k][i] = {k, (i - 1 + n) % n};
            }
        } else if (c == 'U') {
            for (int i = 0; i < n; i++) {
                data[i][k] = {(i - 1 + n) % n, k};
            }
        } else {
            for (int i = 0; i < n; i++) {
                data[i][k] = {(i + 1) % n, k};
            }
        }
    }
};

Mapping operator*(const Mapping& lhs, const Mapping& rhs) {
    int n = lhs.data.size();
    Mapping res(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            auto [i1, j1] = lhs.data[i][j];
            res.data[i][j] = rhs.data[i1][j1];
        }
    }
    return res;
}

Mapping pow(Mapping base, long long exp) {
    Mapping res(base.data.size());
    while (exp > 0) {
        if (exp & 1) {
            res = res * base;
        }
        base = base * base;
        exp >>= 1;
    }
    return res;
}

struct Parser {
    using CopyIter = std::string::const_iterator;
    using Iter = CopyIter&;
    std::string line;
    int n;
    Parser(std::string s): line(s) {}

    void skip(Iter it, char c) {
        ++it;
    }

    void set_n(int n) {
        this->n = n;
    }

    Mapping parse(int n) {
        auto it = line.cbegin();
        set_n(n);
        return sequence(it);
    }

    Mapping sequence(Iter it) {
        Mapping res(n);
        if (*it == '(') {
            res = repetition(it);
        } else {
            res = operation(it);
        }

        while (it != line.end() && (*it == '(' || std::isupper(*it))) {
            if (*it == '(') {
                res = res * repetition(it);
            } else {
                res = res * operation(it);
            }
        }

        return res;
    }

    Mapping repetition(Iter it) {
        skip(it, '(');
        auto res = sequence(it);
        skip(it, ')');
        return pow(res, number(it));
    }

    Mapping operation(Iter it) {
        auto res = shift(it);
        return Mapping(n, res, number(it) - 1);
    }

    char shift(Iter it) {
        char c = *it;
        skip(it, c);

        return c;
    }

    long long number(Iter it) {
        long long res = 0;
        while (it != line.end() && std::isdigit(*it)) {
            res = 10 * res + *it - '0';
            it++;
        }
        return res;
    }
};

int main() {
    int n, l;
    std::cin >> n >> l;

    std::vector<std::vector<int>> matrix(n, std::vector<int>(n));

    std::cin.ignore();
    std::string line;
    std::getline(std::cin, line);
    Parser parser(line);

    auto res = parser.parse(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            auto [i1, j1] = res.data[i][j];
            matrix[i1][j1] = i * n + j + 1;
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cout << matrix[i][j] << (j + 1 == n ? '\n' : ' ');
        }
    }
}
accuracy: 1
time_ms: 80
memory_kb: 30740
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

vector<int> unit(int N) {
	vector<int> res(N * N);
	for (int i = 0; i < N * N; i++) {
		res[i] = i;
	}
	return res;
}

vector<int> merge(int N, const vector<int>& p1, const vector<int>& p2) {
	vector<int> res(N * N);
	for (int i = 0; i < N * N; i++) {
		res[i] = p2[p1[i]];
	}
	return res;
}

vector<int> repeat(int N, const vector<int>& p, int K) {
	vector<int> res(N * N, -1);
	for (int i = 0; i < N * N; i++) {
		if (res[i] == -1) {
			int pos = i;
			vector<int> seq;
			do {
				seq.push_back(pos);
				pos = p[pos];
			} while (pos != i);
			for (int j = 0; j < seq.size(); j++) {
				res[seq[j]] = seq[(j + K) % seq.size()];
			}
		}
	}
	return res;
}

vector<int> shift_row(int N, int row, int cnt) {
	vector<int> res(N * N, -1);
	for (int i = 0; i < N * N; i++) {
		if (i / N != row) {
			res[i] = i;
		}
		else {
			res[i] = i / N * N + (i % N + cnt) % N;
		}
	}
	return res;
}

vector<int> shift_col(int N, int col, int cnt) {
	vector<int> res(N * N, -1);
	for (int i = 0; i < N * N; i++) {
		if (i % N != col) {
			res[i] = i;
		}
		else {
			res[i] = (i / N + cnt) % N * N + i % N;
		}
	}
	return res;
}

vector<int> calc(int N, string S) {
	if (S[0] == '(') {
		int pos = 0, depth = 0;
		do {
			if (S[pos] == '(') {
				depth += 1;
			}
			if (S[pos] == ')') {
				depth -= 1;
			}
			pos += 1;
		} while (depth != 0);
		int r = pos;
		while (r != S.size() && '0' <= S[r] && S[r] <= '9') {
			r += 1;
		}
		vector<int> res1 = calc(N, S.substr(1, pos - 2));
		vector<int> res2 = repeat(N, res1, stoi(S.substr(pos, r - pos)));
		vector<int> res3 = (r != S.size() ? calc(N, S.substr(r)) : unit(N));
		vector<int> res4 = merge(N, res2, res3);
		return res4;
	}
	else {
		int r = 1;
		while (r != S.size() && '0' <= S[r] && S[r] <= '9') {
			r += 1;
		}
		int x = stoi(S.substr(1, r - 1)) - 1;
		vector<int> res1;
		if (S[0] == 'L' || S[0] == 'R') {
			res1 = shift_row(N, x, S[0] == 'R' ? 1 : N - 1);
		}
		else {
			res1 = shift_col(N, x, S[0] == 'D' ? 1 : N - 1);
		}
		vector<int> res2 = (r != S.size() ? calc(N, S.substr(r)) : unit(N));
		vector<int> res3 = merge(N, res1, res2);
		return res3;
	}
}

int main() {
	int N, L; string S;
	cin >> N >> L >> S;
	vector<int> res = calc(N, S);
	vector<int> answer(N * N, -1);
	for (int i = 0; i < N * N; i++) {
		answer[res[i]] = i;
	}
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (j != 0) {
				cout << ' ';
			}
			cout << answer[i * N + j] + 1;
		}
		cout << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 10688
final_rank: 2

code_snippet: #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <variant>
#include <vector>

#define rep(i, s, n) for (int i = int(s); i < int(n); i++)
#define rrep(i, s, n) for (int i = int(n) - 1; i >= int(s); i--)
#define all(v) v.begin(), v.end()

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <class T> bool chmin(T &a, T b) {
    if (a <= b) return false;
    a = b;
    return true;
}

template <class T> bool chmax(T &a, T b) {
    if (a >= b) return false;
    a = b;
    return true;
}

#define debug(...)                                                     \
    std::cerr << "LINE: " << __LINE__ << " [" << #__VA_ARGS__ << "]:", \
        debug_out(__VA_ARGS__)

void debug_out() {
    std::cerr << std::endl;
}

template <typename Head, typename... Tail> void debug_out(Head h, Tail... t) {
    std::cerr << " " << h;
    if (sizeof...(t) > 0) std::cerr << ",";
    debug_out(t...);
}

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {
    return os << pa.first << " " << pa.second;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
    for (std::size_t i = 0; i < vec.size(); i++)
        os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    return os;
}

typedef std::string::const_iterator State;

bool expect(State &begin, char expected) {
    return *begin == expected;
}

void consume(State &begin, char expected) {
    assert(*begin == expected);
    begin++;
}

bool isdigit(char c) {
    return '0' <= c && c <= '9';
}

bool isAlpha(char c) {
    return 'A' <= c && c <= 'Z';
}

bool isalpha(char c) {
    return 'a' <= c && c <= 'z';
}

using Matrix = std::vector<std::vector<std::pair<int,int>>>;

int n;

ll number(State &begin) {
    assert(isdigit(*begin));
    ll ret = 0;
    while(isdigit(*begin)) {
        ret *= 10;
        ret += *begin - '0';
        consume(begin, *begin);
    }
    return ret;
}

Matrix sequence(State &begin) {
    Matrix now(n, std::vector<std::pair<int,int>>(n));
    rep(i,0,n) rep(j,0,n) now[i][j] = {i, j};
    while(isAlpha(*begin) || expect(begin, '(')) {
        if(isAlpha(*begin)) {
            char c = *begin;
            consume(begin, c);
            ll num = number(begin) - 1;
            assert(0 <= num && num < n);
            rep(i,0,n) rep(j,0,n) {
                auto [x, y] = now[i][j];
                if(c == 'L') {
                    if(x == num) {
                        now[i][j] = {x, (y - 1 + n) % n};
                    }
                }
                else if(c == 'R') {
                    if(x == num) {
                        now[i][j] = {x, (y + 1) % n};
                    }
                }
                else if(c == 'U') {
                    if(y == num) {
                        now[i][j] = {(x - 1 + n) % n, y};
                    }
                }
                else if(c == 'D') {
                    if(y == num) {
                        now[i][j] = {(x + 1) % n, y};
                    }
                }
                else assert(0);
            }
        }
        else {
            consume(begin, '(');
            auto p = sequence(begin);
            consume(begin, ')');
            ll num = number(begin);
            int log = 40;
            std::vector table(log + 1, std::vector(n, std::vector<std::pair<int,int>>(n)));
            table[0] = p;
            rep(l,0,log) {
                rep(i,0,n) rep(j,0,n) {
                    auto [x, y] = table[l][i][j];
                    table[l+1][i][j] = table[l][x][y];
                }
            }
            rep(l,0,log) {
                if((num >> l) & 1) {
                    rep(i,0,n) rep(j,0,n) {
                        auto [x, y] = now[i][j];
                        now[i][j] = table[l][x][y];
                    }
                }
            }
        }
    }
    return now;
}

int main() {
    int l;
    std::string s;
    std::cin >> n >> l >> s;
    State begin = s.begin();
    auto p = sequence(begin);
    std::vector ans(n, std::vector<int>(n));
    rep(i,0,n) rep(j,0,n) {
        auto [x, y] = p[i][j];
        ans[x][y] = i * n + j + 1;
    }
    rep(i,0,n) rep(j,0,n) {
        std::cout << ans[i][j] << " \n"[j == n-1];
    }
}
accuracy: 1
time_ms: 270
memory_kb: 33752
final_rank: 15

code_snippet: # include <bits/stdc++.h>
# define len(v) (ll(std::size(v)))
# define all(v) std::begin(v), std::end(v)
# define rep(i, a, b) for (ll i = a; i < ll(b); i++)
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
string erase_all_space(string s) { s.erase(remove_if(all(s), [](char c){ return isspace(c); }), s.end()); return s; }

constexpr ll MOD = ll(1e9) + 7;
using M = vector<vector<ll>>;

struct Parser {
   string s;
   string::const_iterator it;
   ll n;
   Parser() = default;
   Parser(const string& _s, ll _n) : s(_s), n(_n) {
      it = s.cbegin();
   }

   M parse() {
      return expr();
   }

   M expr() {
      M acc = term();
      while (it != s.end() && *it != ')') {
         M other = term();
         acc = plus(acc, other);
      }
      return acc;
   }

   M term() {
      if (*it == '(') {
         assert(*it++ == '(');
         M res = expr();
         assert(*it++ == ')');
         ll num = number();

         M ret = iota2();
         while (num) {
            if (num & 1) {
               ret = plus(ret, res);
            }
            res = plus(res, res);
            num >>= 1;
         }

         return ret;
      }
      else {
         char c = *it++;
         M ret = iota2();
         ll num = number() - 1;
         if (c == 'L') {
            int i = num;
            int old = ret[i][0];
            rep (j, 0, n - 1) ret[i][j] = ret[i][j + 1];
            ret[i][n - 1] = old;
         }
         else if (c == 'R') {
            int i = num;
            int old = ret[i].back();
            for (int j = n - 1; j >= 1; j--) ret[i][j] = ret[i][j - 1];
            ret[i][0] = old;
         }
         else if (c == 'U') {
            int j = num;
            int old = ret[0][j];
            rep (i, 0, n - 1) ret[i][j] = ret[i + 1][j];
            ret[n - 1][j] = old;
         }
         else {
            assert(c == 'D');
            int j = num;
            int old = ret[n - 1][j];
            for (int i = n - 1; i >= 1; i--) ret[i][j] = ret[i - 1][j];
            ret[0][j] = old;
         }
         return ret;
      }
   }

   M plus(const M& a, const M& b) {
      M ret = a;
      rep (i, 0, n) {
         rep (j, 0, n) {
            int v = b[i][j];
            int r = v / n;
            int c = v % n;
            ret[i][j] = a[r][c];
         }
      }
      return ret;
   }

   M iota2() {
      M mat(n, vector(n, 0LL));
      rep (i, 0, n * n) {
         auto [r, c] = std::div(i, n);
         mat[r][c] = i;
      }
      return mat;
   }

   ll number() {
      ll ret = 0;
      for (; isdigit(*it); ++it) {
         ret = 10LL * ret + (*it - '0');
      }
      return ret;
   }
};

int main() {
   ll n, l;
   cin >> n >> l;
   string s;
   cin >> s;

   M perm = Parser(s, n).parse();
   rep (i, 0, n) {
      rep (j, 0, n) {
         cout << perm[i][j] + 1 << " \n"[j == n - 1];
      }
   }
}
accuracy: 1
time_ms: 240
memory_kb: 4988
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define rng(i, l, r) for(int i = int(l); i < int(r); i++)
#define rep(i, n) rng(i, 0, n)
#define sz(v) int(v.size())
#define foa(s, v) for(auto &s : v)
#define all(v) v.begin(), v.end()

// clang-format off
template <class T> using V = vector<T>;
template <class T> bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template <class T> bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
// clang-format on

#ifdef LOCAL
#define debug(x) cerr << __LINE__ << ", " << #x << ": " << x << endl
#else
#define debug(x) void(0)
#endif

template <class T>
ostream &operator<<(ostream &os, vector<T> vec) {
	os << "{ ";
	foa(c, vec) os << c << ", ";
	os << "}";
	return os;
}

using conv = vector<int>;
conv unit;
// a -> b
conv operator*(conv a, conv b) {
	conv ret(sz(a));
	rep(i, sz(a)) { ret[i] = b[a[i]]; }
	return ret;
}

conv pow(conv a, ll n) {
	debug(a);
	debug(n);
	conv ret(sz(a));
	iota(all(ret), 0);
	while(n) {
		if(n & 1) ret = ret * a;
		a = a * a;
		n >>= 1;
	}

	return ret;
}

int n;
int n_sq;
vector<vector<int>> ids;
string s;

set<char> dirs = {
	'L',
	'R',
	'U',
	'D',
};

struct parser {
	string::const_iterator it;
	parser() : it(s.begin()) {}

	conv lshift(int row, ll number) {
		number %= n;
		if(number < 0) number += n;
		conv ret = unit;
		rep(j, n) {
			int from_place = ids[row][j];
			int to_place = ids[row][(j - number + n + n) % n];
			ret[from_place] = to_place;
		}
		return ret;
	}
	conv rshift(int row, ll number) { return lshift(row, -number); }
	conv ushift(int col, ll number) {
		number %= n;
		if(number < 0) number += n;
		conv ret = unit;
		rep(i, n) {
			int from_place = ids[i][col];
			int to_place = ids[(i - number + n + n) % n][col];
			ret[from_place] = to_place;
		}
		return ret;
	}
	conv dshift(int col, ll number) { return ushift(col, -number); }

	conv sequence() {
		conv ret(unit);
		while(it != s.end()) {
			debug(place());
			debug(ret);
			conv right = unit;
			if(dirs.count(*it)) {
				right = operation();
			} else if(*it == '(') {
				right = repetition();
			} else {
				break;
			}
			debug(place());
			debug(right);
			ret = ret * right;
			debug(place());
			debug(ret);
		}

		return ret;
	}

	void assume(char c) {
		if(it != s.end() && *it == c) {
			it++;
			return;
		}
		cerr << "error\n";
		assert(false);
	}

	conv repetition() {
		assume('(');
		conv seq = sequence();
		assume(')');
		ll nm = num();
		return pow(seq, nm);
	}

	int place() { return int(it - s.begin()); }

	conv operation() {
		debug(place());
		auto c = *it++;
		debug(place());
		debug(c);
		assert(dirs.count(c));
		int row_or_col = int((num() - 1 + n) % n);

		if(c == 'U') return ushift(row_or_col, 1LL);
		if(c == 'D') return dshift(row_or_col, 1LL);
		if(c == 'R') return rshift(row_or_col, 1LL);
		if(c == 'L') return lshift(row_or_col, 1LL);
		assert(false);
	}

	ll num() {
		assert(isdigit(*it));
		ll ret = 0;
		while(it != s.end() && isdigit(*it)) {
			ret *= 10;
			ret += *(it++) - '0';
			// ret %= n;
		}
		return ret;
	}

	conv solve() {
		conv ret = sequence();
		if(it != s.end()) {
			debug("fail");
			debug(place());
			assert(false);
		}
		return ret;
	}
};

void solve() {
	n_sq = n * n;
	int len;
	cin >> len;
	cin >> s;

	ids.assign(n, vector<int>(n, 0));
	int ver = 0;
	foa(r, ids) foa(c, r) c = ver++;
	unit.assign(n_sq, 0);
	iota(all(unit), 0);

	debug(ids);
	debug(unit);

	parser ps;
	auto res = ps.solve();
	vector<vector<int>> ans(n, vector<int>(n));
	rep(i, n) rep(j, n) {
		int to_id = res[ids[i][j]];
		int to_row = to_id / n;
		int to_col = to_id % n;
		ans[to_row][to_col] = ids[i][j] + 1;
	}

	foa(row, ans) {
		rep(j, n) { cout << row[j] << (j == n - 1 ? '\n' : ' '); }
	}
	return;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	while(cin >> n && n) solve();
}
accuracy: 1
time_ms: 30
memory_kb: 29356
final_rank: 6

code_snippet: #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

/* macro */

#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (i64 i = (i64)(a); i < (i64)(n); i++)
#define RRep(i, a, n) for (i64 i = ((i64)(n - i64(1))); i >= (i64)(a); i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define Bit(n) (1LL << (n))

/* macro end */

/* template */

namespace ebi {

#ifdef LOCAL
#define debug(...)                                                      \
    std::cerr << "LINE: " << __LINE__ << "  [" << #__VA_ARGS__ << "]:", \
        debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << std::endl; }

template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
    std::cerr << " " << h;
    if (sizeof...(t) > 0) std::cout << " :";
    debug_out(t...);
}

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &pa) {
    return os << pa.first << " " << pa.second;
}

template <typename T1, typename T2>
std::istream &operator>>(std::istream &os, std::pair<T1, T2> &pa) {
    return os >> pa.first >> pa.second;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
    for (std::size_t i = 0; i < vec.size(); i++)
        os << vec[i] << (i + 1 == vec.size() ? "" : " ");
    return os;
}

template <typename T>
std::istream &operator>>(std::istream &os, std::vector<T> &vec) {
    for (T &e : vec) std::cin >> e;
    return os;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const std::optional<T> &opt) {
    if (opt) {
        os << opt.value();
    } else {
        os << "invalid value";
    }
    return os;
}

using std::size_t;
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;

template <class T, T init>
auto make_vector(int n) {
    return std::vector<T>(n, init);
}

template <class T, T init, typename Head, typename... Tail>
auto make_vector(Head n, Tail... ts) {
    return std::vector(n, make_vector<T, init>(ts...));
}

template <class T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T pow(T x, i64 n) {
    T res = 1;
    while (n > 0) {
        if (n & 1) res = res * x;
        x = x * x;
        n >>= 1;
    }
    return res;
}

template <class T>
T mod_pow(T x, i64 n, i64 mod) {
    T res = 1;
    while (n > 0) {
        if (n & 1) res = (res * x) % mod;
        x = (x * x) % mod;
        n >>= 1;
    }
    return res;
}

template <class T>
T scan() {
    T val;
    std::cin >> val;
    return val;
}

template <class T>
struct Edge {
    int to;
    T cost;
    Edge(int _to, T _cost = 1) : to(_to), cost(_cost) {}
};

template <class T>
struct Graph : std::vector<std::vector<Edge<T>>> {
    using std::vector<std::vector<Edge<T>>>::vector;
    void add_edge(int u, int v, T w, bool directed = false) {
        (*this)[u].emplace_back(v, w);
        if (directed) return;
        (*this)[v].emplace_back(u, w);
    }
};

struct graph : std::vector<std::vector<int>> {
    using std::vector<std::vector<int>>::vector;
    void add_edge(int u, int v, bool directed = false) {
        (*this)[u].emplace_back(v);
        if (directed) return;
        (*this)[v].emplace_back(u);
    }
};

constexpr i64 LNF = std::numeric_limits<i64>::max() / 4;

constexpr int INF = std::numeric_limits<int>::max() / 2;

const std::vector<int> dy = {1, 0, -1, 0, 1, 1, -1, -1};
const std::vector<int> dx = {0, 1, 0, -1, 1, -1, 1, -1};

}  // namespace ebi

/*
    reference: https://gist.github.com/draftcode/1357281
*/

namespace ebi {

typedef std::string::const_iterator State;
class ParseError {};

bool expect(State &begin, char expected) {
    if(*begin == expected) {
        return true;
    }
    else {
        return false;
    }
}

// beginがexpectedを指していたらbeginを一つ進める。
void consume(State &begin, char expected) {
    if (*begin == expected) {
        begin++;
    } else {
        std::cerr << "Expected '" << expected << "' but got '" << *begin << "'"
            << std::endl;
        std::cerr << "Rest string is '";
        while (*begin) {
            std::cerr << *begin++;
        }
        std::cerr << "'" << std::endl;
        throw ParseError();
    }
}

bool isdigit(char c) {
    return '0' <= c && c <= '9';
}

bool isAlpha(char c) {
    return 'A' <= c && c <= 'Z';
}

bool isalpha(char c) {
    return 'a' <= c && c <= 'z';
}

i64 num(State &begin) {
    i64 res = 0;
    while(isdigit(*begin)) {
        res *= 10;
        res += *begin - '0';
        consume(begin, *begin);
    }
    return res;
}

}

namespace ebi {

std::vector<std::vector<std::pair<int,int>>> sequence(State&);
std::vector<std::vector<std::pair<int,int>>> repetition(State&);
std::pair<int,i64> operation(State&);

int n;

std::vector<std::vector<std::pair<int,int>>> mul(const std::vector<std::vector<std::pair<int,int>>> &lhs, const std::vector<std::vector<std::pair<int,int>>> &rhs) {
    std::vector res(n, std::vector<std::pair<int,int>>(n));
    rep(i,0,n) rep(j,0,n) {
        auto [y, x] = rhs[i][j];
        res[i][j] = lhs[y][x];
    }
    return res;
}

std::vector<std::vector<std::pair<int,int>>> sequence(State &begin) {
    std::vector table(n, std::vector<std::pair<int,int>>(n));
    rep(i,0,n) rep(j,0,n) table[i][j] = {i, j}; 
    while(!(expect(begin, ')') || expect(begin, ';'))) {
        if(expect(begin, '(')) {
            table = mul(table, repetition(begin));
        }
        else {
            auto [idx, val] = operation(begin);
            if(idx % 2 == 0) {
                std::vector<std::pair<int,int>> nxt(n);
                rep(i,0,n) {
                    nxt[i] = table[(i+dy[idx]+n)%n][val];
                }
                rep(i,0,n) table[i][val] = nxt[i];
            }
            else {
                std::vector<std::pair<int,int>> nxt(n);
                rep(j,0,n) {
                    nxt[j] = table[val][(j+dx[idx]+n)%n];
                }
                table[val] = nxt;
            }
        }
    }
    return table;
}

std::vector<std::vector<std::pair<int,int>>> repetition(State &begin) {
    consume(begin, '(');
    auto table = sequence(begin);
    consume(begin, ')');
    i64 val = num(begin);
    // ダブリングする
    std::vector res(n, std::vector<std::pair<int,int>>(n));
    rep(i,0,n) rep(j,0,n) res[i][j] = {i, j};
    while(val > 0) {
        if(val & 1) {
            res = mul(res, table);
        }
        val >>= 1;
        table = mul(table, table);
    }
    return res;
}

std::pair<int,i64> operation(State &begin) {
    if(expect(begin, 'L')) {
        consume(begin, 'L');
        return {1, num(begin)-1};
    }
    else if(expect(begin, 'R')) {
        consume(begin, 'R');
        return {3, num(begin)-1};
    }
    else if(expect(begin, 'U')) {
        consume(begin, 'U');
        return {0, num(begin)-1};
    }
    else if(expect(begin, 'D')) {
        consume(begin, 'D');
        return {2, num(begin)-1};
    }
    else assert(0);
}

void main_() {
    int len;
    std::cin >> n >> len;
    std::string s;
    std::cin >> s;
    s += ';';
    State begin = s.begin();
    auto table = sequence(begin);
    rep(i,0,n) rep(j,0,n) {
        auto [y, x] = table[i][j];
        int ans = y*n + x + 1;
        std::cout << ans << " \n"[j == n-1];
    }
}

}  // namespace ebi

int main() {
    std::cout << std::fixed << std::setprecision(15);
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t = 1;
    // std::cin >> t;
    while (t--) {
        ebi::main_();
    }
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 30632
final_rank: 10

code_snippet: #include <bits/stdc++.h>

using namespace std;

#include <iostream>
#include <optional>
#include <string>

namespace suisen::parsing {
    using State = std::string::const_iterator;

    struct ParseError {
        ParseError(const std::string& message = "") {
            std::cerr << message << std::endl;
        }
    };

    namespace internal {
        void print_rest_of_string(State it) {
            cerr << "Rest string is '";
            while (*it) cerr << *it++;
            cerr << "'" << endl;
        }
    }

    void consume(State& it, char expected) {
        if (*it == expected) {
            *it++;
        } else {
            cerr << "Expected '" << expected << "' but got '" << *it << "'" << endl;
            internal::print_rest_of_string(it);
            throw ParseError{};
        }
    }

    bool in(const State& it, char l, char r) {
        return l <= *it and *it <= r;
    }
    bool is(const State& it, char c) {
        return *it == c;
    }

    void assert_range(const State& it, char lo, char hi) {
        if (in(it, lo, hi)) {
            cerr << "Expected [" << lo << "-" << hi << "] but got '" << *it << "'" << endl;
            internal::print_rest_of_string(it);
            throw ParseError{};
        }
    }
    void assert_exact(const State& it, char c) {
        if (not is(it, c)) {
            cerr << "Expected '" << c << "' but got '" << *it << "'" << endl;
            internal::print_rest_of_string(it);
            throw ParseError{};
        }
    }

    long long nonnegative_number(State& it) {
        long long res = 0;
        assert_range(it, '0', '9');
        while (in(it, '0', '9')) res = res * 10 + (*it++ - '0');
        return res;
    }
    long long number(State& it) {
        long long res = 0;
        bool neg = false;
        while (is(it, '-')) neg = not neg, consume(it, '-');
        while (in(it, '0', '9')) res = res * 10 + (*it++ - '0');
        if (neg) res = -res;
        return res;
    }

    namespace normal_expression {
        namespace internal {
            long long expr(State& it);
            long long term(State& it);
            long long factor(State& it);

            long long expr(State& it) {
                long long res = term(it);
                while (true) {
                    if (*it == '+') {
                        consume(it, '+');
                        res = res + term(it);
                    } else if (*it == '-') {
                        consume(it, '-');
                        res = res - term(it);
                    } else break;
                }
                return res;
            }
            long long term(State& it) {
                long long res = factor(it);
                while (true) {
                    if (*it == '*') {
                        consume(it, '*');
                        res = res * factor(it);
                    } else if (*it == '/') {
                        consume(it, '/');
                        res = res / factor(it);
                    } else break;
                }
                return res;
            }
            long long factor(State& it) {
                bool neg = false;
                while (is(it, '-')) neg = not neg, consume(it, '-');
                long long res;
                if (is(it, '(')) {
                    consume(it, '(');
                    res = expr(it);
                    consume(it, ')');
                } else {
                    res = nonnegative_number(it);
                }
                if (neg) res = -res;
                return res;
            }
        }

        long long parse(State& it) {
            return internal::expr(it);
        }
    }
}
using namespace suisen::parsing;

int n;

int mod(long long x) {
    return (x %= n) < 0 ? x + n : x;
}

vector<int> operator*(const vector<int> &p, const vector<int> &q) {
    vector<int> r(n * n);
    for (int i = 0; i < n * n; ++i) r[i] = q[p[i]];
    return r;
}
vector<int> pow(vector<int> a, long long b) {
    vector<int> res(n * n);
    iota(res.begin(), res.end(), 0);
    for (; b; b >>= 1) {
        if (b & 1) res = res * a;
        a = a * a;
    }
    return res;
}

vector<int> seq(State& it) {
    vector<int> p(n * n);
    iota(p.begin(), p.end(), 0);
    while (*it and *it != ')') {
        vector<int> q(n * n);
        iota(q.begin(), q.end(), 0);
        if (*it == '(') {
            consume(it, '(');
            q = seq(it);
            consume(it, ')');
            long long v = number(it);
            p = p * pow(q, v);
        } else {
            char op = *it++;
            int k = number(it) - 1;
            if (op == 'L') {
                for (int j = 0; j < n - 1; ++j) q[k * n + (j + 1)] = k * n + j;
                q[k * n + 0] = k * n + (n - 1);
            } else if (op == 'R') {
                for (int j = 0; j < n - 1; ++j) q[k * n + j] = k * n + (j + 1);
                q[k * n + (n - 1)] = k * n + 0;
            } else if (op == 'U') {
                for (int i = 0; i < n - 1; ++i) q[(i + 1) * n + k] = i * n + k;
                q[0 * n + k] = (n - 1) * n + k;
            } else if (op == 'D') {
                for (int i = 0; i < n - 1; ++i) q[i * n + k] = (i + 1) * n + k;
                q[(n - 1) * n + k] = 0 * n + k;
            }
            else assert(false);
            p = p * q;
        }
    }
    return p;
}

int main() {
    int l;
    string s;
    cin >> n >> l >> s;
    auto it = s.cbegin();
    auto p = seq(it);

    vector<int> a(n * n);
    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
        int v = i * n + j + 1;
        a[p[i * n + j]] = v;
    }

    for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
        cout << a[i * n + j] << " \n"[j == n - 1];
    }
}
accuracy: 1
time_ms: 20
memory_kb: 29348
final_rank: 3

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=1167167167167167167;
const int INF=1050000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}


void solve();
// oddloop
int main() {
	
	solve();
}

void solve(){
	int N,L;
	cin>>N>>L;
	string S;
	cin>>S;
	vector<int> e(N*N);
	rep(i,N*N) e[i]=i;
	vector<char> C={'R','L','D','U'};
	vector<vector<vector<int>>> p(4,vector<vector<int>>(N,e));
	rep(j,N){
		rep(k,N-1){
			swap(p[0][j][j*N+k],p[0][j][j*N+k+1]);
			swap(p[1][j][N+j*N-1-k],p[1][j][N+j*N-2-k]);
			swap(p[2][j][j+k*N],p[2][j][j+k*N+N]);
			swap(p[3][j][N*N-N-k*N+j],p[3][j][N*N-2*N-k*N+j]);
		}
	}
	auto merge=[&](vector<int> A,vector<int> B)->vector<int>{
		auto tmp=e;
		rep(i,N*N) tmp[i]=B[A[i]];
		return tmp;
	};
	auto g=[&](vector<int> s,int L)->vector<int>{
		auto tmp=e;
		while(L){
			if(L&1){
				tmp=merge(tmp,s);
			}
			L>>=1;
			s=merge(s,s);
		}
		return tmp;
	};
	int ind=0;
	auto f=[&](auto self)->vector<int>{
		auto ans=e;
		while(ind<L){
			if(S[ind]==')') return ans;
			if(S[ind]=='('){
				ind++;
				auto tmp=self(self);
				ind++;
				int L=0;
				while('0'<=S[ind]&&S[ind]<='9'){
					L*=10;
					L+=(int)(S[ind]-'0');
					ind++;
				}
				ans=merge(ans,g(tmp,L));
			}else{
				int x=-1,y=0;
				rep(i,4){
					if(S[ind]==C[i]) x=i;
				}
				ind++;
				while('0'<=S[ind]&&S[ind]<='9'){
					y*=10;
					y+=(int)(S[ind]-'0');
					ind++;
				}
				y--;
				ans=merge(ans,p[x][y]);
			}
		}
		return ans;
	};
	auto ans=f(f);
	vector<int> rev(N*N);
	rep(i,N*N) rev[ans[i]]=i;
	rep(i,N){
		rep(j,N){
			if(j) cout<<" ";
			cout<<rev[i*N+j]+1;
		}
		cout<<"\n";
	}
}
accuracy: 1
time_ms: 20
memory_kb: 32224
final_rank: 5

code_snippet: #include "bits/stdc++.h"
using namespace std;

typedef string::const_iterator State;
#define eps 1e-8L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
#define REP(a, b) for (long long(a) = 0; (a) < (b); ++(a))
#define ALL(x) (x).begin(), (x).end()

#define int long long
int num(State &x){
  int ans = 0;
  while (isdigit(*x))
  {
    ans *= 10;
    ans += *x - '0';
    x++;
  }
  return ans;
}
int n, l;
vector<vector<pair<int, int>>> apply(vector<vector<pair<int, int>>> A, vector<vector<pair<int, int>>> B)
{
  vector<vector<pair<int, int>>> C = A;
  REP(i,n){
    REP(q,n){
      pair<int, int> target = A[i][q];
      C[i][q] = B[target.first][target.second];
    }
  }
  return C;
}

vector<vector<pair<int, int>>> check(State &x){
  vector<vector<pair<int, int>>> base(n);
  REP(i,n){
    REP(q,n){
      base[i].push_back({i, q});
    }
  }
  while(*x != ')'){
    if(*x == '('){
      // repetition
      x++;
      vector<vector<pair<int, int>>> now = check(x);
      x++;
      int reps = num(x);
      while(reps){
        if(reps % 2)
          base = apply(base, now);
        now = apply(now, now);
        reps /= 2;
      }
    }else{
      // operation
      State y = x;
      x++;
      vector<vector<pair<int, int>>> now(n);
      int A = num(x) - 1;
      REP(i,n){
        REP(q,n){
          now[i].push_back({i, q});
        }
      }
      if (*y == 'L')
      {
        for (int q = n - 1; q >= 1;--q){
          swap(now[A][q], now[A][q - 1]);
        }
      }else if(*y == 'R'){
        for (int q = 0; q < n-1;++q){
          swap(now[A][q], now[A][q + 1]);
        }
      }else if(*y == 'D'){
        for (int q = 0; q < n-1;++q){
          swap(now[q][A], now[q+1][A]);
        }
      }else if(*y == 'U'){
        for (int q = n - 1; q >= 1;--q){
          swap(now[q][A], now[q - 1][A]);
        }
      }
      base = apply(base,now);
    }
  }
  return base;
}

void solve(){
  cin >> n >> l;
  string s;
  cin >> s;
  s.push_back(')');
  State x = s.begin();
  vector<vector<pair<int, int>>> next = check(x);
  vector<vector<int>> inputs(n, vector<int>(n, 0));
  REP(i,n){
    REP(q,n){
      inputs[next[i][q].first][next[i][q].second] = 1 + q + i * n;
    }
  }
  REP(i,n){
    REP(q,n){
      if(q)
        cout << " ";
      cout << inputs[i][q];
    }
    cout << endl;
  }
}
#undef int

// generated by oj-template v4.7.2
// (https://github.com/online-judge-tools/template-generator)
int main() {
  // Fasterize input/output script
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(100);
  // scanf/printf user should delete this fasterize input/output script

  int t = 1;
  //cin >> t; // comment out if solving multi testcase
  for (int testCase = 1; testCase <= t; ++testCase) {
    solve();
  }
  return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 71824
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

vector<vector<int>> prod(vector<vector<int>> &mat, vector<vector<int>> &op) {
    int N = mat.size();
    vector<vector<int>> mat2(N, vector<int>(N));
    rep(i, N) rep(j, N) mat2[i][j] = mat[op[i][j] / N][op[i][j] % N];
    return mat2;
}

vector<vector<int>> matpow(vector<vector<int>> &A, ll k) {
    int N = A.size();
    vector<vector<int>> E(N, vector<int>(N));
    rep(i, N) rep(j, N) E[i][j] = i * N + j;
    while (k) {
        if (k & 1) E = prod(E, A);
        A = prod(A, A);
        k >>= 1;
    }
    return E;
}

vector<vector<int>> rec(string &S, int l, int &r, int N) {
    int L = S.size();
    if (S[l] == 'L' || S[l] == 'R' || S[l] == 'U' || S[l] == 'D') {
        int d = 0, idx = l + 1;
        while ('0' <= S[idx] && S[idx] <= '9') d = 10 * d + S[idx++] - '0';
        r = idx;
        d--;
        vector<vector<int>> ret(N, vector<int>(N));
        rep(i, N) rep(j, N) ret[i][j] = i * N + j;
        if (S[l] == 'L') {
            rep(j, N) ret[d][j] = d * N + (j + 1) % N;
        } else if (S[l] == 'R') {
            rep(j, N) ret[d][j] = d * N + (j - 1 + N) % N;
        } else if (S[l] == 'U') {
            rep(i, N) ret[i][d] = (i + 1) % N * N + d;
        } else {
            rep(i, N) ret[i][d] = (i - 1 + N) % N * N + d;
        }
        return ret;
    } else if (S[l] == '(') {
        vector<vector<int>> E(N, vector<int>(N));
        rep(i, N) rep(j, N) E[i][j] = i * N + j;
        l++;
        while (S[r] != ')') {
            vector<vector<int>> op = rec(S, l, r, N);
            l = r;
            E = prod(E, op);
        }
        ll d = 0, idx = r + 1;
        while ('0' <= S[idx] && S[idx] <= '9') d = 10 * d + S[idx++] - '0';
        r = idx;
        return matpow(E, d);
    } else {
        assert(0);
    }
}

vector<vector<int>> rec1(string &S, int l, int &r, int N) {
    int L = S.size();
    vector<vector<int>> E(N, vector<int>(N));
    rep(i, N) rep(j, N) E[i][j] = i * N + j;
    while (r < L) {
        vector<vector<int>> op = rec(S, l, r, N);
        l = r;
        E = prod(E, op);
    }
    return E;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N, L;
    cin >> N >> L;
    string S;
    cin >> S;
    int l = 0, r = 0;
    vector<vector<int>> mat = rec1(S, l, r, N);
    rep(i, N) {
        rep(j, N) {
            if (j != N - 1)
                cout << mat[i][j] + 1 << " ";
            else
                cout << mat[i][j] + 1 << "\n";
        }
    }
    return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 17572
final_rank: 9