-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.cpp
More file actions
34 lines (30 loc) · 853 Bytes
/
solution.cpp
File metadata and controls
34 lines (30 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution
{
public:
vector<vector<int>> constructProductMatrix(vector<vector<int>> &grid)
{
const int MOD = 12345;
int n = grid.size();
int m = grid[0].size();
vector<vector<int>> ans(n, vector<int>(m, 1));
long long prefix = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
ans[i][j] = (int)prefix;
prefix = (prefix * grid[i][j]) % MOD;
}
}
long long suffix = 1;
for (int i = n - 1; i >= 0; i--)
{
for (int j = m - 1; j >= 0; j--)
{
ans[i][j] = (int)((1LL * ans[i][j] * suffix) % MOD);
suffix = (suffix * grid[i][j]) % MOD;
}
}
return ans;
}
};