Multiplication Program

All submissions for this problem are available.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Bear Limak hopes you can solve the following problem. You are given an integer B which is equal to 3, 5 or 7. Your goal will be to print a sequence of moves that are able to multiply any two 2digit numbers X and Y in base B.
Consider a computer memory consisting of 10^{5} cells, numbered 0 through 10^{5}1, each storing one digit (in base B, a digit must be between 0 and B  1, inclusive). Let t_{i} denote the digit in the ith cell. Initially:
 t_{i} = i for i < B
 t_{B} and t_{B+1} are digits of the number X, so X = t_{B} * B + t_{B+1}
 t_{B+2} and t_{B+3} are digits of the number Y, so Y = t_{B+2} * B + t_{B+3}
 t_{i} = 0 for all i > B+3
The first of the two drawings above shows the initial situation for B = 5 and multiplying numbers 8 and 4 (represented in base 5 as 13 and 04 respectively). After applying your sequence of moves, the result of the multiplication should be stored in cells B+4, B+5, B+6, B+7, which is shown in the second drawing (please note that 8 * 4 = 32 and this number is represented as 0112 in base 5). Except for those 4 cells containing the answer, the final value of t_{i} doesn't matter. In particular, you are allowed to change values in the first B+4 cells.
You should choose one function F : [0,B1] × [0,B1] > [0,B1]. It means that the function F should take two digits and return one, where each digit is in the set {0, 1, ..., B1}. One example of F is addition F(x, y) = (x + y) % B. We don't require any particular properties from F (e.g. associativity or symmetricity).
You should create a sequence of at most 10^{5} moves. In each move, choose three cells c1, c2 and c3, not necessarily distinct. The result of F for digits in cells c1 and c2 will be computed, and then written in the cell c3. In other words, in C++ or Python it's the operation t[c3] = F(t[c1], t[c2]). For every possible pair of 2digit numbers X and Y, your sequence of moves should produce the correct answer X * Y as described above.
Input
The only line of the input contains a single integer B, denoting the base.
Output
You should first print the description of the function F that you choose, and then the chosen sequence of moves.
First print B lines. The ith line should contain B spaceseparated integers F(i,0), F(i,1), ..., F(i,B1).
In the next line, print a single integer K, denoting the number of moves in the sequence.
In the ith of the next K lines, print three spaceseparated integers c1, c2, c3 (each between 0 and 10^{5}1 inclusive), describing the ith move.
Subtasks
There are 5 subtasks, each worth 20 points. B = 3 and the checker checks only if your program works correctly for X and Y consisting of digits 0 and 1 only (in base B).
 B = 3
 B = 5
 B = 5 and the first digit (in base B) of X and the first digit of Y are both 0. In other words: X, Y < B.
 B = 7
Example
Input: 3 Output (please note that it's incorrect): 1 1 2 2 2 2 0 1 0 2 1 6 7 0 0 0
Explanation
The provided output has valid formatting but isn't correct. Let's analyze the printed moves to multiply X = 7 and Y = 5. These X and Y in base 3 are 21 and 12 respectively, so the initial numbers in cells are: 0, 1, 2, 2, 1, 1, 2, 0, 0, ...
The first operation is t[7] = F(t[1], t[6]) = F(1, 2) = 2, so now we have cells: 0, 1, 2, 2, 1, 1, 2, 2, 0, ...
The second operation is t[0] = F(t[0], t[0]) = F(0, 0) = 1, so we will have: 1, 1, 2, 2, 1, 1, 2, 2, 0, ...
After those two operations, cells 7, 8, 9, 10 should contain the product X * Y = 35, while in fact the contain digits 2, 0, 0, 0, what represents a number 2 * 3^{3} = 2 * 27 = 54. The printed sequence of moves should correctly multiply any two 2digit numbers X, Y, so this particular output would get verdict Wrong Answer.
Author:  errichto 
Editorial  https://discuss.codechef.com/problems/MULDIG 
Tags  errichto, implementation, july17, mediumhard, simulation 
Date Added:  1052017 
Time Limit:  2 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 