fork download
  1. // Mohib Manva.
  2. #include <bits/stdc++.h>
  3. #include <random>
  4. using namespace std;
  5.  
  6. #define ll long long
  7. #define all(vc) vc.begin(), vc.end()
  8.  
  9. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  10.  
  11. int main() {
  12. int t = 1;
  13. scanf("%d", &t);
  14. while (t--) {
  15. int n, m;
  16. scanf("%d %d", &n, &m);
  17.  
  18. if (m > 2 * n) {
  19. puts("NO");
  20. continue;
  21. }
  22.  
  23.  
  24. puts("YES");
  25. if (m <= n) {
  26. for (int i = 1; i <= 2 * n; i++) {
  27. for (int j = 1; j <= m; j++) {
  28. printf("%d ", j);
  29. }
  30. puts("");
  31. }
  32. continue;
  33. }
  34.  
  35. for (int i = 1; i <= n; i++) {
  36. int st1 = i;
  37. int st2 = 1;
  38. for (int j = 1; j <= m; j += 2) {
  39. printf("%d ", st1);
  40. if (j + 1 <= m) {
  41. printf("%d ", st2);
  42. }
  43. if (st1 == n) {
  44. st1 = 1;
  45. } else {
  46. st1++;
  47. }
  48. if (st2 == n) {
  49. st2 = 1;
  50. } else {
  51. st2++;
  52. }
  53. }
  54. puts("");
  55.  
  56. st1 = i;
  57. st2 = 2;
  58. for (int j = 1; j <= m; j += 2) {
  59. printf("%d ", st1);
  60. if (j + 1 <= m) {
  61. printf("%d ", st2);
  62. }
  63. if (st1 == n) {
  64. st1 = 1;
  65. } else {
  66. st1++;
  67. }
  68. if (st2 == n) {
  69. st2 = 1;
  70. } else {
  71. st2++;
  72. }
  73. }
  74. puts("");
  75. }
  76. }
  77. return 0;
  78. }
Success #stdin #stdout 0s 5284KB
stdin
1
3 4
stdout
YES
1 1 2 2 
1 2 2 3 
2 1 3 2 
2 2 3 3 
3 1 1 2 
3 2 1 3