we often use a tow-dimensional prefix sum to speed up sub-matrix summation. The problem is described as follows:
For matrix $a$ of the n-row m-column, $a_{i,j}$ represents the number of column j in row i. Now give the sub-matrix upper-left $(x_0,y_0)$ and bottom-right $(x_1,y_1)$; you need to quicky work out:
$$\sum_{i=x_0}^{x_1}\sum_{j=y_0}^{y_1} a_{i,j}$$
To solve this problem, we can introduce a two-dimensional array b, where $b_{i_0,j_0} = \sum_{i=1}^{i_0}\sum_{j=1}^{j_0}a_{i,j}$,so there is the following calculation:
$$\sum_{i=x_0}^{x_1}\sum_{j=y_0}^{y_1} a_{i,j} = b_{x_1,y_1}-b_{x_1,y_{0}}-b_{x_{0},{y_1}}+b_{x_{0},y_{0-1}}$$
After learning this, hyd asked his good friend lwq such a question: given a matrix of n-row m-columns, ask how many sub-matrices are sumd up to be integer times k. lwq is busy doing his homework, can you help hime copy with hyd?