Cat Goose and the Random Matrix
Time limit3sMemory limit1024 MB
Given a random n by m table with entries modulo p, find the largest-sum submatrix whose sum is divisible by p.
- Level
Hard8 of 10
- Topics
- Matrix, Prefix sum, Hash map, Math
- Solved
- No attempts yet
Statement
Cat Goose prepared a rectangular table of size for Nick Fury. Each entry is an integer from to .
Nick Fury immediately realized that every number in the table was chosen uniformly at random from to , independently of the others.
Find a rectangular submatrix of the table whose sum is divisible by , and among all such submatrices, output the one with the largest sum. Formally, find and such that the sum of over and is divisible by , and this sum is maximal among all such submatrices.
Input
The first line contains three integers , , (): the table dimensions and the number that the submatrix sum must be divisible by.
Each of the next lines contains integers, where the -th number in the -th line is ().
It is guaranteed that every number in was chosen independently and uniformly at random from to .
Output
Print one integer: the maximum sum of a rectangular submatrix whose sum is divisible by . If no such submatrix exists, print .