-
Notifications
You must be signed in to change notification settings - Fork 0
/
곱셈.cpp
60 lines (49 loc) · 1.5 KB
/
곱셈.cpp
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
50
51
52
53
54
55
56
57
58
59
60
//곱셈 ~ 1629 @ 백준
#include <iostream>
#include <vector>
//using namespace std;
std::vector<std::vector<int>> multMat(std::vector<std::vector<int>> A, std::vector<std::vector<int>> B, int N, int c){
std::vector<std::vector<int>> result;
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
result[i][j] = 0;
for(int k = 0; k < 4; ++k){
result[i][j] += (A[i][k] * B[k][j]) % c;
}
result[i][j] %= c;
}
}
return result;
}
std::vector<std::vector<int>> sqMat(std::vector<std::vector<int>> A, int N, int B, int c){
if(B == 1){
return A;
}else if(B % 2 == 0){
std::vector<std::vector<int>> resultA = sqMat(A, N, B / 2, c);
std::vector<std::vector<int>> resultB = sqMat(A, N, B / 2, c);
return multMat(resultA, resultB, N, c);
}else{
std::vector<std::vector<int>> resultA = sqMat(A, N, B / 2, c);
std::vector<std::vector<int>> resultB = sqMat(A, N, B / 2 + 1, c);
return multMat(resultA, resultB, N, c);
}
}
int main(){
std::vector<std::vector<int>> A(5);
int N;
int B;
std::cin >> N >> B;
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
int t;
std::cin >> t;
A[i].push_back(t);
}
}
std::vector<std::vector<int>> result = sqMat(A, N, B, 1000);
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
std::cout << result[i][j];
}
}
}