fork download
  1. program tetris;
  2. { constraints }
  3. const
  4. MAXN = 500;
  5.  
  6. { input data }
  7. var
  8. N, M, i, j, nbpiece, ans : longint;
  9. table : array[0..MAXN-1,0..MAXN-1] of longint;
  10.  
  11. begin
  12. {
  13.   uncomment the following lines if you want to read/write from files
  14.   assign(input, 'input.txt'); reset(input);
  15.   assign(output, 'output.txt'); rewrite(output);
  16. }
  17.  
  18. { read numbers N and M in a single line }
  19. readln(N, M);
  20. for i:=0 to N-1 do
  21. for j:=0 to M-1 do
  22. table[i,j]:=0;
  23.  
  24. if (N mod 2 = 0) and (M mod 2 = 0) then
  25. begin
  26. nbpiece := 0;
  27. for i := 0 to N-1 do
  28. for j := 0 to M-1 do
  29. begin
  30. table[2*i][2*j]:= nbpiece;
  31. table[2*i + 1][2*j] := nbpiece;
  32. table[2*i][2*j + 1] := nbpiece;
  33. table[2*i + 1][2*j + 1] := nbpiece;
  34. if j<M div 2 then inc(nbpiece);
  35. end;
  36. ans:=(N div 2) *(M div 2);
  37. end
  38. else
  39.  
  40. if N mod 4 = 0 then
  41. begin
  42. nbpiece:= 0;
  43. for j := 0 to M-1 do
  44. for i := 0 to N-1 do
  45. begin
  46. table[i][j] := nbpiece;
  47. if i mod 4 = 3 then inc(nbpiece);
  48. end;
  49. ans:=(N div 4) * M ;
  50. end
  51. else
  52. if M mod 4 = 0 then
  53. begin
  54. nbpiece:= 0;
  55. for i := 0 to N-1 do
  56. for j := 0 to M-1 do
  57. begin
  58. table[i][j] := nbpiece;
  59. if j mod 4 = 3 then inc(nbpiece);
  60. end;
  61. ans:=N *(M div 4)
  62. end
  63. else ans:=-1;
  64. writeln(ans);
  65. if ans <>-1 then
  66. for i:=0 to N-1 do
  67. begin
  68. for j:=0 to M-1 do
  69. write(table[i][j],' ');
  70. writeln;
  71. end;
  72. end.
  73.  
Success #stdin #stdout 0s 5316KB
stdin
5 5
stdout
-1