-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10830.cpp
More file actions
101 lines (79 loc) · 1.49 KB
/
10830.cpp
File metadata and controls
101 lines (79 loc) · 1.49 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <vector>
using namespace std;
int n;
long long int m;
vector<int> box;
int a[6][6];
int b[6][6];
vector<int> mul(vector<int> v, vector<int>s)
{
vector<int> ans;
int sum;
int cnt = 0;
for (int i = 0; i < n; ++i)
{
for(int j = 0; j < n; j++)
{
a[i][j] = v[cnt];
b[i][j] = s[cnt];
cnt++;
}
}
for (int i = 0; i < n; ++i)
{
for(int j = 0; j < n; j++)
{
sum = 0;
for (int k = 0; k < n; ++k)
{
sum = (sum + (a[i][k] * b[k][j])%1000)%1000;
}
ans.push_back(sum);
}
}
return ans;
}
vector<int> solve(long long int pow)
{
vector<int> half;
if(pow==1) return box;
if(pow%2==1)
{
return mul(solve(pow-1),box);
}
half = solve(pow/2);
if(pow%2==0)
{
return mul(half,half);
}
}
int main(int argc, char** argv)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int num;
int cnt = 0;
vector<int> answer;
cin>>n>>m;
for (int i = 0; i < n; ++i)
{
for(int j = 0; j < n; j++)
{
cin>>num;
box.push_back(num);
}
}
answer = solve(m);
for (int i = 0; i < n; ++i)
{
for(int j = 0; j < n; j++)
{
cout<<answer[cnt]%1000<<" ";
cnt++;
}
cout<<"\n";
}
return 0;
}