-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathADAMONEY.cpp
105 lines (93 loc) · 2.6 KB
/
ADAMONEY.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
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
102
103
104
105
//
// main.cpp
// practice
//
// Created by Mahmud on 11/09/17.
// Copyright © 2017 Mahmud. All rights reserved.
//
/*
O(T * log(N)) solution using matrix exponentiation
This is how transformation matrix has been constructed:
| F(n - 4) | | 0 1 0 0 0 | | F(n - 3) |
| F(n - 3) | | 0 0 1 0 0 | | F(n - 2) |
| F(n - 2) | * | 0 0 0 1 0 | = | F(n - 1) |
| F(n - 1) | | 0 0 0 0 1 | | F(n) |
| F(n) | | 1 5 0 2 1 | | F(n + 1) |
*/
#include <iostream>
#include <cassert>
using namespace std;
const int MAX = 6;
const int MODULO = 1000000007;
struct matrix{
int rowSize = MAX - 1;
int columnSize = MAX - 1;
int data[MAX][MAX];
matrix () {
for (int i = 1; i <= rowSize; i ++) {
for (int j = 1; j <= columnSize; j ++) {
data[i][j] = 0;
}
}
}
matrix operator * (const matrix other) const{
assert(columnSize == other.rowSize);
matrix result = matrix();
for (int i = 1; i <= rowSize; i ++) {
for (int j = 1; j <= other.columnSize; j ++) {
for (int k = 1; k <= columnSize; k ++) {
result.data[i][j] = (result.data[i][j] + 1LL * data[i][k] * other.data[k][j] % MODULO) % MODULO;
}
}
}
result.rowSize = rowSize;
result.columnSize = other.columnSize;
return result;
}
matrix power(long long e) {
if (e == 1) {
return *this;
}
if (e & 1) {
return (*this) * power(e - 1);
}
matrix half = power(e >> 1);
return half * half;
}
};
int T;
long long N;
int coefficients[MAX];
int main() {
matrix transformation;
transformation.rowSize = 5;
transformation.columnSize = 5;
for (int i = 2; i <= 5; i ++) {
transformation.data[i - 1][i] = 1;
}
transformation.data[5][1] = 1;
transformation.data[5][2] = 5;
transformation.data[5][3] = 0;
transformation.data[5][4] = 2;
transformation.data[5][5] = 1;
cin >> T;
for (int cases = 0; cases < T; cases ++) {
for (int i = 0; i < 5; i ++) {
cin >> coefficients[i];
}
cin >> N;
if (N < 5) {
cout << coefficients[N] << endl;
continue;
}
matrix identity;
identity.rowSize = 5;
identity.columnSize = 1;
for (int i = 1; i <= 5; i ++) {
identity.data[i][1] = coefficients[i - 1];
}
matrix result = transformation.power(N) * identity;
cout << result.data[1][1] << endl;
}
return 0;
}