program ioi94day2prb3ver1(input, output, inp, out) ;
{ (c) 1994, Tom Verhoeff, Eindhoven University of Technology }
{ Naive approach, tries almost everything up to max tail }

{ General Section }
 
const
  Test = true;
  Trace = false;
 
var 
  inp, out: text;
 
procedure Init;
  begin
  if Test then
    writeln('IOI''94 - Day 2 - Problem 3: The Circle') ;
  assign(inp, 'input.txt') ;
  reset(inp) ;
  assign(out, 'output.txt') ;
  rewrite(out) ;
  if Test then writeln('Initialized')
  end { Init } ;

procedure Fini;
  begin
  close(inp) ;
  close(out)
  end { Fini } ;
 
 
{ Problem Specific Section }
 
const
  Max_n =  6; { maximum number of sectors }
  Max_k = 20; { maximum lower bound on numbers in sectors }
  Max_m = 20; { maximum number that starts sequence }

type
  Circle = array [1..Max_n] of integer;

var
  n: 1..Max_n; { input value }
  m: 1..Max_m; { input value }
  k: 1..Max_k; { input value }

var
  BestCount: integer ; { # best arrangements so far }
  BestArr: array [1..1000] of Circle;
    { BestArr[1..BestCount] = best arrangements so far }
  BestTail: integer; { maximum tail found so far }

procedure ReadInput;
  begin
  readln(inp, n) ; readln(inp, m) ; readln(inp, k) ;
  if Test then writeln('n, m, k = ', n:1, ', ', m:1, ', ', k:1)
  end { ReadInput } ;

procedure WriteOutput;
  var i, j: integer;
  begin
  writeln(out, BestTail:1) ;
  for i := 1 to BestCount do begin
    for j := 1 to n do write(out, BestArr[i, j]:2, ' ') ;
    writeln(out)
    end { for i } ;
  if Test then writeln('Max tail = ', BestTail:1, '; # arr. = ', BestCount:1)
  end { WriteOutput } ;

var
  Arr: Circle; { Arr[i] is number in sector i of circular arrangement }
  Tail: integer;

procedure ComputeTail; 
  { post: Tail = tail of m for circular arrangement Arr[1..n] }
  var
    a, b, s, u: integer ;
    Creatable: array[1..51] of boolean ; { Creatable[p] = p is creatable }
  begin
  u := 1 + m + (n-1)*n ; { 1 + upper bound on maximum tail }
  for a := 1 to u do Creatable[a] := false ;
  for a := 1 to n do begin
    s := 0 ; { s = sum of Arr[a..b] }
    for b := a to n do begin
      s := s + Arr[b] ;
      if s <= u then Creatable[s] := true
      end { for b } ;
    for b := 1 to a-2 do begin
      s := s + Arr[b] ;
      if s <= u then Creatable[s] := true
      end { for b }
    end { for a } ;
  Tail := m ; { linear search for smallest uncreatable number }
  while Creatable[Tail] do inc(Tail) ;
  dec(Tail)
  end { ComputeTail } ;

procedure CheckArrangement;
  begin
  ComputeTail ;
  if Tail > BestTail then begin { improved arrangement }
    BestCount := 1 ; BestArr[BestCount] := Arr ; BestTail := Tail
    end { then }
  else if Tail = BestTail then begin { equally good arrangement }
    inc(BestCount) ; BestArr[BestCount] := Arr
    end { then }
  end { CheckArrangement } ;

procedure FillRemainder(i: integer);
  { Fill remaining sectors Arr[i..n] in all possible ways }
  { pre: i > 1 }
  var j, u: integer ;
  begin
  if i > n then { all sectors filled, check whether filling is useful }
    CheckArrangement
  else begin { fill sector i in all possible ways }
    if Trace then begin
      for j := 1 to pred(i) do write(Arr[j]:3) ;
      writeln
      end { if } ;
    u := m+(n-1)*n ; { naive upper bound }
    for j := Arr[1] to u do begin
      Arr[i] := j ;
      FillRemainder(succ(i))
      end { for j }
    end { else }
  end { FillRemainder } ;

procedure ComputeAnswer;
  { pre: k <= m }
  var j: integer;
  begin
  BestCount := 0 ; BestTail := m+n-1 ;
  for j := k to m do begin
    Arr[1] := j ; { N.B. this is the smallest number in the circle }
    FillRemainder(2) ;
    end { for j }
  end { ComputeAnswer } ;

begin
Init ;
ReadInput ;
ComputeAnswer ;
WriteOutput ;
Fini
end.
