#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
//#include <bits/extc++.h>
//using namespace __gnu_pbds;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
using ll=long long;
using ull=unsigned long long;
using db=long double;
using pii=array<int,2>;
using pll=array<ll,2>;
using pdb=array<db,2>;
using tii=array<int,3>;
using tll=array<ll,3>;
using tdb=array<db,3>;
using ti4=array<int,4>;
using tl4=array<ll,4>;
using td4=array<db,4>;
//using oset=tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//uniform_int_distribution<> gen(1,100);
template <int MOD_> struct modnum{
private:
int v;
public:
static const int MOD = MOD_;
modnum() : v(0) {}
modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int () const { return v; }
friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }
friend bool operator < (const modnum& a, const modnum& b) { return a.v < b.v; }
friend bool operator <= (const modnum& a, const modnum& b) { return a.v <= b.v; }
friend bool operator > (const modnum& a, const modnum& b) { return a.v > b.v; }
friend bool operator >= (const modnum& a, const modnum& b) { return a.v >= b.v; }
modnum& operator += (const modnum& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modnum& operator -= (const modnum& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modnum& operator *= (const modnum& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
friend modnum pow(modnum a, ll p) {
modnum ans = 1;
for (; p; p /= 2, a *= a) if (p&1) ans *= a;
return ans;
}
modnum operator ~ () const {
modnum res;
res.v = pow((modnum)v, MOD - 2).v;
return res;
}
modnum& operator /= (const modnum& o) {
return *this *= (~o);
}
modnum operator-() const { return modnum(-v); }
modnum& operator++() { return *this += 1; }
modnum operator++(int){ modnum tmp=*this; ++*this; return tmp; }
modnum& operator--() { return *this -= 1; }
modnum operator--(int){ modnum tmp=*this; --*this; return tmp; }
friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
friend ostream& operator<<(std::ostream& os, const modnum& o)
{
os << o.v;
return os;
}
friend istream& operator>>(std::istream& is, modnum& o)
{
is >> o.v;
return is;
}
};
const ll mod=998244353,inf=1e18;
const int N=105;
using mi=modnum<mod>;
int n,m,a[N][N],b[N][N];
pii inv[N*N];
vector<tii> ans;
void output(string s=""){
cout<<s<<endl;
for(int i=0;i<n;i++) for(int j=0;j<m;j++) cout<<a[i][j]<<" \n"[j==m-1];
}
void rMove(int i,int r){
r+=n*m;
r%=m;
if(!r) return;
ans.push_back({1,i+1,r});
for(int j=0;j<m;j++) b[i][(j+r)%m]=a[i][j];
for(int j=0;j<m;j++){
a[i][j]=b[i][j];
inv[a[i][j]]={i,j};
}
}
void cMove(int j,int c){
c+=n*m;
c%=n;
if(!c) return;
ans.push_back({2,j+1,c});
for(int i=0;i<n;i++) b[(i+c)%n][j]=a[i][j];
for(int i=0;i<n;i++){
a[i][j]=b[i][j];
inv[a[i][j]]={i,j};
}
}
int main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>m;
vector<int> v(n*m);
for(int i=0;i<n*m;i++) v[i]=i;
shuffle(v.begin(),v.end(),rng);
int p=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
a[i][j]=v[p++];
cin>>a[i][j];
b[i][j]=a[i][j];
inv[a[i][j]]={i,j};
}
}
//output("Start");
// Step 1
for(int i=0;i<n-1;i++){
for(int j=0;j<m-1;j++){
auto [x,y]=inv[(n-2-i)*m+j];
rMove(x,m-1-y);
cMove(m-1,n-1-x);
if(x!=n-1) rMove(x,y+1);
rMove(n-1,j+1);
cMove(j,1);
//output("Step "+to_string(i)+" "+to_string(j));
}
//output("Step "+to_string(i));
}
//output("After Step 1");
uniform_int_distribution<> gen1(0,m-1);
uniform_int_distribution<> gen2(0,n-1);
do{
for(int t=0;t<100;t++){
rMove(n-1,gen1(rng));
cMove(m-1,gen2(rng));
}
// Step 2
for(int j=0;j<m;j++){
auto [x,y]=inv[(n-1)*m+j];
if(x!=n-1){
rMove(n-1,j);
cMove(m-1,n-1-x);
}
rMove(n-1,m-1-y);
cMove(m-1,1);
if(j) rMove(n-1,m-2-inv[(n-1)*m+j-1][1]);
cMove(m-1,n-1);
}
//output("After Step 2");
// Step 3
for(int i=0;i<n;i++){
int x=inv[m*i+m-1][0];
if(!i){
cMove(m-1,n-x);
continue;
}
if(x==i) continue;
cMove(m-1,n-1-x);
rMove(n-1,m/2);
int v=a[n-1][m-1];
x=inv[m*(i-1)+m-1][0];
cMove(m-1,n-2-x);
rMove(n-1,m/2);
x=inv[v][0];
cMove(m-1,n-1-x);
rMove(n-1,m/2);
x=inv[m-1][0];
cMove(m-1,n-x);
//output(to_string(i));
}
//output("After Step 3");
}while(a[n-1][0]!=(n-1)*m);
cout<<ans.size()<<"\n";
for(auto [x,y,z]: ans) cout<<x<<" "<<y<<" "<<z<<"\n";
//output("Result");
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
39 ms |
1660 KB |
Output is correct |
6 |
Correct |
37 ms |
1536 KB |
Output is correct |
7 |
Correct |
40 ms |
1728 KB |
Output is correct |
8 |
Correct |
42 ms |
1560 KB |
Output is correct |
9 |
Correct |
37 ms |
1604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
39 ms |
1660 KB |
Output is correct |
18 |
Correct |
37 ms |
1536 KB |
Output is correct |
19 |
Correct |
40 ms |
1728 KB |
Output is correct |
20 |
Correct |
42 ms |
1560 KB |
Output is correct |
21 |
Correct |
37 ms |
1604 KB |
Output is correct |
22 |
Correct |
38 ms |
1612 KB |
Output is correct |
23 |
Correct |
38 ms |
1656 KB |
Output is correct |
24 |
Correct |
37 ms |
1604 KB |
Output is correct |
25 |
Correct |
38 ms |
1668 KB |
Output is correct |
26 |
Correct |
37 ms |
1688 KB |
Output is correct |
27 |
Correct |
42 ms |
1636 KB |
Output is correct |
28 |
Correct |
36 ms |
1520 KB |
Output is correct |
29 |
Correct |
40 ms |
1680 KB |
Output is correct |
30 |
Correct |
38 ms |
1516 KB |
Output is correct |
31 |
Correct |
37 ms |
1604 KB |
Output is correct |
32 |
Correct |
39 ms |
1584 KB |
Output is correct |
33 |
Correct |
5 ms |
724 KB |
Output is correct |
34 |
Correct |
6 ms |
748 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
24 ms |
1372 KB |
Output is correct |
37 |
Correct |
6 ms |
596 KB |
Output is correct |
38 |
Correct |
5 ms |
724 KB |
Output is correct |
39 |
Correct |
5 ms |
724 KB |
Output is correct |
40 |
Correct |
13 ms |
976 KB |
Output is correct |
41 |
Correct |
6 ms |
660 KB |
Output is correct |
42 |
Correct |
4 ms |
596 KB |
Output is correct |
43 |
Correct |
22 ms |
1164 KB |
Output is correct |
44 |
Correct |
3 ms |
596 KB |
Output is correct |
45 |
Correct |
22 ms |
1312 KB |
Output is correct |
46 |
Correct |
9 ms |
836 KB |
Output is correct |