Ivoization
Time limit1.5sMemory limit128 MB
Sum the Ivoization values of every K by K submatrix, where Ivoization is the pairwise absolute-difference sum of all K^2 entries, modulo 10007.
- Level
Hard8 of 10
- Topics
- Sorting, Prefix sum, Math, Implementation
- Solved
- No attempts yet
Problem
Ivo likes playing with matrices. His latest invention is an operation on a single square matrix, and he calls it Ivoization.
Ivoization turns one square matrix into one number. That number is the sum of the absolute differences over all pairs of numbers in the matrix. If the entries of the matrix are , the Ivoization value is . Pairs with two equal indices are included, and they contribute 0. For example, the Ivoization value of a square matrix holding the numbers (1, 5, 2, 4) is 28.
Ivo wants to add up the Ivoization values of all square submatrices with exactly rows and exactly columns that lie entirely inside a matrix with rows and columns. Compute that sum.
Input
The first line contains three positive integers , and ().
Each of the next lines contains numbers ().
All are distinct.
Output
Print , the sum of the Ivoization values of all square submatrices with side length , modulo .
If is greater than or greater than , no such submatrix exists, so is .