1868A. Fill in the Matrix
constructive algorithms, implementation, 1300, https://codeforces.com/problemset/problem/1868/A
cpp
#include<bits/stdc++.h>
using namespace std;
int T,n,m,a;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
if(m==1)
printf("0\n");
else
printf("%d\n",min(n+1,m));
for(int i=1;i<=n;i++){
a=m-i;
if(a<=0)
a=m-1;
for(int j=1;j<=m;j++){
printf("%d ",a);
a=(a+1)%m;
}
printf("\n");
}
}
return 0;
}