-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10830.cpp
More file actions
49 lines (49 loc) · 1.09 KB
/
Copy path10830.cpp
File metadata and controls
49 lines (49 loc) · 1.09 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
int N;
long long B;
vector<int> matrixMultiply(vector<int> &A, vector<int> &B)
{
vector<int> C(N * N);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
for (int k = 0; k < N; k++)
{
C[i * N + j] = (C[i * N + j] + A[i * N + k] * B[k * N + j]) % 1000;
}
}
}
return C;
}
vector<int> divideNconquer(vector<int> &A, long long p)
{
if (p == 1)
return A;
vector<int> C = divideNconquer(A, p / 2);
C = matrixMultiply(C, C);
if (p % 2)
C = matrixMultiply(C, A);
return C;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> N >> B;
vector<int> A(N * N), ans;
for (int i = 0; i < N * N; i++)
cin >> A[i];
ans = divideNconquer(A, B);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cout << ans[i * N + j] % 1000 << ' ';
}
cout << '\n';
}
return 0;
}