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.  
  21. if (N mod 2 = 0) and (M mod 2 = 0) then
  22. begin
  23. nbpiece := 0;
  24. for i := 0 to N-1 do
  25. for j := 0 to M-1 do
  26. begin
  27. table[2*i][2*j]:= nbpiece;
  28. table[2*i + 1][2*j] := nbpiece;
  29. table[2*i][2*j + 1] := nbpiece;
  30. table[2*i + 1][2*j + 1] := nbpiece;
  31. if j<M div 2 then inc(nbpiece);
  32. end;
  33. ans:=(N div 2) *(M div 2);
  34. end
  35. else
  36. if N mod 4 = 0 then
  37. begin
  38. nbpiece:= 0;
  39. for j := 0 to M-1 do
  40. for i := 0 to N-1 do
  41. begin
  42. table[i][j] := nbpiece;
  43. if i mod 4 = 3 then inc(nbpiece);
  44. end;
  45. ans:=(N div 4) * M ;
  46. end
  47. else
  48. if M mod 4 = 0 then
  49. begin
  50. nbpiece:= 0;
  51. for i := 0 to N-1 do
  52. for j := 0 to M-1 do
  53. begin
  54. table[i][j] := nbpiece;
  55. if j mod 4 = 3 then inc(nbpiece);
  56. end;
  57. ans:=N *(M div 4)
  58. end
  59. else ans:=-1;
  60. writeln(ans);
  61. if ans <>-1 then
  62. for i:=0 to N-1 do
  63. begin
  64. for j:=0 to M-1 do
  65. write(table[i][j],' ');
  66. writeln;
  67. end;
  68. end.
  69.  
Success #stdin #stdout 0s 5316KB
stdin
2 3
stdout
-1