function counts = candy_count_matrix ( c, m, n )

%*****************************************************************************80
%
%% candy_count_matrix() counts candy types in a matrix.
%
%  Discussion:
%
%    We are given a box of candy containing C distinct types, and
%    asked to report how many of each type there are.
%
%    In this case, the box is an M by N matrix represented as A(I,J).  
%    We will assume that rows and columns are indexed by I and J.
%
%    The box entry in row I, column J will store candy type C, where
%    A(i,j) = mod ( I + J - 2, C ) + 1.
%    (If we start I, J and C indexing at 0, this simplifies to
%    mod(I+J,C)! )
%
%    The effect of this numbering scheme is that the candy type is
%    constant along diagonal lines.
%
%    The task is to determine, for a given set of C, M and N,
%    the number of candies of each type.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    22 June 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer c: the number of types of candy.
%
%    integer m, n: the number of rows and columns in the candy box.
%
%  Output:
%
%    integer counts(c): the number of each type of candy in the box.
%
  counts = zeros ( c, 1 );

  a = mod ( m, c );
  b = mod ( n, c );

  nmin = floor ( ( m * n - a * b ) / c );

  for i = 1 : c
    if ( i <= a + b - 1 - c )
      counts(i) = nmin + a + b - c;
    elseif ( i < min ( a, b ) )
      counts(i) = nmin + i;
    elseif ( i <= max ( a, b ) )
      counts(i) = nmin + min ( a, b );
    elseif ( i < a + b -1 )
      counts(i) = nmin + a + b - i;
    else
      counts(i) = nmin;
    end
  end

  return
end

