Sorry for the late reply.
Let me post my solution (You need not know anything about Latin squares to understand this :) )
For N, let me consider a matrix where the first row and column are 1...N
1 2 3 ... N
2
3
.
.
N
Now we can fill the inner matrix using brute force...and find out the number of ways to fill it. Let it be M.
Now... These M matrices are the first M matrices.
Now consider all permutation of first row except the first element....
1 2 3 4 .... N
2
3
.
N
There are (n-1)! arrangement for this... If you arrange these (n-1)! permutations in lexicographical order...
For each permutation you can get M number of inner matrices matrices... (Fill the inner matrix from our previous calculation before permutation and when you permute... permute entire columns. Now this new matrix will not violate the conditions)
you will get the next M * (n-1)! matrices of the original sequence by this method...
Now consider all permutations of first column
1 2 3 4 ... N
2
3
.
N
We have n! arrangements for this... Now for each permutation of this we have (n-1)! * M matrices...
(Again first form the inner matrix and permute entire rows when you permute first column)
Now the algo.
0. Find M for a given N (This should be done using brute force. But here N <= 6 so find all values before and store in an array so that your time complexity will increase) So this part will not come in the algo.
1. Let A = K%M
2. Let B = K/M
3. Let C = B%(n-1)!
4. Let D = B/(n-1)!
5. Find the Ath inner matrix.
5. Find the Cth permutation of first row and permute the inner matrix accordingly
6. Find the Dth permutation of first column and permute the inner matrix accordingly
7. output the combined result!
This was the only idea I can think of. Any better algos... post it!
Regards,
Prunthaban