#P12045. Minimizing Cyclic Product Sum over Distinct Positive Integers

    ID: 14151 Type: Default 1000ms 256MiB

Minimizing Cyclic Product Sum over Distinct Positive Integers

Minimizing Cyclic Product Sum over Distinct Positive Integers

Given two integers (m) and (n), consider all ordered (m)-tuples of (m) different positive integers (a_1, a_2, \ldots, a_m) such that their sum is (n). For each such tuple, define the cyclic product sum as [ S = a_1a_2 + a_2a_3 + \cdots + a_{m-1}a_m + a_m a_1. ] Your task is to choose a valid (m)-tuple that minimizes (S) and output this minimum value modulo (998244353).

It can be shown that an optimal strategy is to choose the set [ {1, 2, \ldots, m-1, A},\quad \text{where } A = n - \frac{m(m-1)}{2}, ] and arrange it in a circle so that the largest number (A) is adjacent to the two smallest numbers. In particular, one optimal ordering is

  • For (m = 1): The only number is (n) and the answer is (n^2).
  • For (m = 2): The two numbers are (1) and (n-1) and the answer is (2(n-1)).
  • For (m \ge 3): An optimal ordering is [ [1, A, 2, 3, \ldots, m-1], ] yielding a cyclic sum [ S = 1\cdot A + A\cdot 2 + \sum_{i=2}^{m-2} i(i+1) + (m-1)\cdot 1. ] Equivalently, if we denote (T = \frac{m(m-1)}{2}) and (A = n-T), then for (m\ge3) the answer is computed as [ S = 3A + (m - 1) + \left(\frac{(m-2)(m-1)m}{3} - 2\right) \quad (\text{with the summation term taken as zero when } m < 4). ]

It is guaranteed that the input satisfies the condition needed for a valid solution (in particular, when (m \ge 2), we have (n \ge \frac{m(m+1)}{2})).

inputFormat

The input consists of two space-separated integers (m) and (n) on a single line.

outputFormat

Output a single integer, the minimum possible cyclic product sum modulo (998244353).

sample

1 10
100