C - Whole Product of Pairwise Distances Editorial by evima Editorial for printing
We may assume \(A\) is sorted.
Let \(B_i\) denote the remainder when \(A_i\) is divided by \(N\).
Then, the desired value is \(\prod_{1 \leq i < j \leq N}(B_j - B_i)\).
If any two values among \(B_1, B_2, \dots, B_N\) are equal, the answer is \(0\).
Consider the case where \(B_1, B_2, \dots, B_N\) are all distinct.
In this case, \((B_1, B_2, \dots, B_N)\) is a rearrangement of \((1, 2, \dots, N)\).
Letting \(I\) be the number of inversions of \((B_1, B_2, \dots, B_N)\), we have:
\(\prod_{1 \leq i < j \leq N}(B_j - B_i) = (-1)^I \times \prod_{1 \leq i < j \leq N}(j - i)\).
\(\prod_{1 \leq i < j \leq N}(j - i)\) is the product of \(d^{(N-d)}\) over \(d = 1, 2, \dots, N-1\), so it can be computed.
posted:
last update: