You have successfully completed all the basic lessons. Now, you have to pass an exam to become a ninja. The subject of exam is based on location pointing, a must-need skill for ninjas. The problem statement of the exam is as follows:
You are given a large square matrix A
of dimensions n2×n2
. The large matrix is divided into small square submatrices, each of size n×n
. The large matrix can be thought of as a grid of these smaller submatrices, arranged like tiles on a floor.
You are given a specific cell in the large matrix, identified by coordinates (x
,y
) where:
x
is the row index.
y
is the column index.
Your task is to determine which small n×n
submatrix contains the cell located at (x
,y
) and then compute the sum of all the numbers in that particular submatrix.
Input
The first line contains three integer n
(1≤n≤50
), x
and y
(1≤x,y≤n2
) — the size of the small square submatrices, and the coordinates of the cell in the large matrix.
Then n2
lines follows, the i
-th of them contains n2
integers Ai,1,Ai,2,...,Ai,n2
(1≤Ai,j≤109
) — the elements of the i
-th row inside the large matrix.
Output
Print the sum of all elements in the n×n
submatrix that contains the cell at position (x,y).
#include
using namespace std;
int main() {
long long n, x, y;
cin >> n >> x >> y;
long long matrix[n * n][n * n];
long long f = 0;
for (long long i = 0; i < n * n; ++i) {
for (long long j = 0; j < n * n; ++j) {
cin >> matrix[i][j];
f++;
}
}
long long startRow = (x - 1) / n;
long long startCol = (y - 1) / n;
long long sum = 0;
for (long long i = startRow * n; i < (startRow + 1) * n; i++) {
for (long long j = startCol * n; j < (startCol + 1) * n; j++) {
sum += matrix[i][j];
}
}
cout << sum << endl;
return 0;}
this code fails on a certain test case