fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. void solve(){
  9. int n, m;
  10. cin >> n >> m;
  11. if(m >= 2 * n){
  12. cout << "NO\n";
  13. return;
  14. }
  15. cout << "YES\n";
  16. vector<vector<int>> ans(2 * n, vector<int>(2 * n - 1));
  17.  
  18. for(int i = 0; i < 2 * n - 1; i++){
  19. int start = i;
  20. int col = 1;
  21. for(int j = 0; j < 2 * n - 1; j++){
  22. ans[i][start] = col;
  23. start++;
  24. start %= (2 * n - 1);
  25. if(j % 2){
  26. col++;
  27. }
  28. }
  29. }
  30.  
  31. int col = 1;
  32.  
  33. for(int i = 0; i < 2 * n - 1; i++){
  34. ans[2 * n - 1][(i + 1) % (2 * n - 1)] = col;
  35. if(i < n - 1 || !(i % 2))col++;
  36. col = min(col, n);
  37. }
  38.  
  39. if(n == 2){
  40. ans[2][0] = 2;
  41. ans[2][1] = 2;
  42. ans[2][2] = 1;
  43. ans[3][0] = 1;
  44. ans[3][1] = 2;
  45. ans[3][2] = 1;
  46. }
  47.  
  48. for(int i = 0; i < 2 * n; i++){
  49. for(int j = 0; j < m; j++){
  50. cout << ans[i][j] << " ";
  51. }
  52. cout << "\n";
  53. }
  54. }
  55.  
  56. int main(){
  57. ios_base::sync_with_stdio(false);
  58. cin.tie(nullptr);
  59.  
  60. int t = 1;
  61. cin >> t;
  62.  
  63. for(int i = 1; i <= t; i++){
  64. solve();
  65. }
  66. return 0;
  67. }
Success #stdin #stdout 0.01s 5276KB
stdin
3
2 2
3 7
5 4
stdout
YES
1 1 
2 1 
2 2 
1 2 
NO
YES
1 1 2 2 
5 1 1 2 
4 5 1 1 
4 4 5 1 
3 4 4 5 
3 3 4 4 
2 3 3 4 
2 2 3 3 
1 2 2 3 
5 1 2 3