fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define FasterIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
  4.  
  5. typedef unsigned long long ull;
  6. typedef long long ll;
  7. typedef double db;
  8.  
  9. #define mem(a,x) memset(a,x,sizeof(a))
  10. #define pb push_back
  11. #define ff first
  12. #define ss second
  13. #define mk make_pair
  14. #define pi pair<int,int>
  15. #define vi vector<int>
  16. #define pL pair<ll,ll>
  17. #define vL vector<ll>
  18. #define vp vector<pi>
  19. #define vpL vector<pL>
  20.  
  21. const int MX=1000;
  22. const ll inf=1e18;
  23. const ll mod=1e9+7;
  24.  
  25.  
  26. int a[MX][MX];
  27.  
  28. void construct(int n, int m)
  29. {
  30. for(int i=0; i<=m; i++) a[0][i]=i;
  31.  
  32. int mex=m;
  33. for(int i=1; i<n; i++)
  34. {
  35. a[i][0]=a[i-1][m];
  36. for(int j=1; j<=m; j++) a[i][j]=a[i-1][j-1];
  37. mex--;
  38. }
  39.  
  40. if(mex>n+1)
  41. {
  42. mex=n+1;
  43. for(int i=n-1; i>=0; i--)
  44. {
  45. for(int j=0; j<m; j++)
  46. {
  47. if(a[i][j]==mex) a[i][j]=mex+1;
  48. }
  49. mex++;
  50. }
  51. }
  52.  
  53. }
  54.  
  55. int main()
  56. {
  57.  
  58. FasterIO;
  59.  
  60. int tc;
  61.  
  62. cin>>tc;
  63. while(tc--)
  64. {
  65. int n, m;
  66. cin>>n>>m;
  67.  
  68. if(n==1 || m==1)
  69. {
  70.  
  71. if(n==1 && m==1) cout<<0<<endl;
  72. else
  73. {
  74. if(n==1)
  75. {
  76. cout<<0<<' '<<1;
  77. for(int i=3; i<=m; i++) cout<<' '<<i;
  78. cout<<endl;
  79. }
  80. else
  81. {
  82. cout<<0<<endl<<1<<endl;
  83. for(int i=3; i<=n; i++) cout<<i<<endl;
  84. }
  85. }
  86. continue;
  87. }
  88.  
  89. if(n<=m)
  90. {
  91. construct(n, m);
  92. if(n==m) a[n-1][m-1]=1;
  93. for(int i=0; i<n; i++)
  94. {
  95. for(int j=0; j<m; j++)
  96. {
  97. cout<<a[i][j];
  98. if(j!=m-1) cout<<' ';
  99. else cout<<endl;
  100. }
  101. }
  102. }
  103. else
  104. {
  105. construct(m, n);
  106. for(int i=0; i<n; i++)
  107. {
  108. for(int j=0; j<m; j++)
  109. {
  110. cout<<a[j][i];
  111. if(j!=m-1) cout<<' ';
  112. else cout<<endl;
  113. }
  114. }
  115. }
  116. }
  117. return 0;
  118. }
  119.  
Success #stdin #stdout 0s 4372KB
stdin
Standard input is empty
stdout
Standard output is empty