Count the Submatrices

3.6

10 votes
Algorithms, Approved, Basic Programming, Basics of Implementation, Grammar-Verified, Hard, Hiring, Implementation, Recruit
Problem

A matrix of size M×N where M and N are integers that denote the number of rows and columns of the matrix respectively. The matrix contains integer elements only. You are given an integer P.

Write a program to determine the number of submatrices of this matrix such that the sum of all the elements of a submatrix is divisible by P.

Input format

  • First line: M, N, and P
  • Next M lines: N

Output format

Print the number of submatrices such that the sum of all the elements in those submatrices is divisible by P.

Constraints

1Elementineachcell200

1M,N,P200

Sample Input
2 2 2
1 2
3 4
Sample Output
5
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

There are 5 possible submatrices of the given matrix such that sum of elements is divisible by 2.

Editor Image

?