-
Notifications
You must be signed in to change notification settings - Fork 166
/
gneck.cpp
82 lines (71 loc) · 1.28 KB
/
gneck.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
//gneck.cpp
//Girlfriend & Necklace
//Weekly Challenges - Week 8
//Author: derekhh
#include<iostream>
#include<cstring>
using namespace std;
const int MOD = 1000000007;
struct Matrix
{
long long val[3][3];
int row, col;
};
Matrix m, b;
Matrix mul(Matrix a, Matrix b)
{
Matrix ret;
ret.row = a.row;
ret.col = b.col;
for (int i = 0; i < a.row; i++)
{
for (int j = 0; j < b.col; j++)
{
ret.val[i][j] = 0;
for (int k = 0; k < a.col; k++)
ret.val[i][j] = (ret.val[i][j] + a.val[i][k] * b.val[k][j]) % MOD;
}
}
return ret;
}
Matrix pow(long long n)
{
Matrix ret;
ret.row = ret.col = 3;
memset(ret.val, 0, sizeof(ret.val));
ret.val[0][0] = ret.val[1][1] = ret.val[2][2] = 1;
Matrix cur = m;
while (n)
{
if (n % 2) ret = mul(ret, cur);
cur = mul(cur, cur);
n /= 2;
}
return ret;
}
int main()
{
int t;
cin >> t;
b.row = 3;
b.col = 1;
memset(b.val, 0, sizeof(b.val));
b.val[0][0] = b.val[1][0] = b.val[2][0] = 1;
while (t--)
{
long long n;
cin >> n;
m.row = 3; m.col = 3;
memset(m.val, 0, sizeof(m.val));
m.val[0][0] = 1;
m.val[0][1] = 1;
m.val[1][2] = 1;
m.val[2][0] = 1;
Matrix ans_m = mul(pow(n - 2), b);
long long ans = 0;
for (int i = 0; i < 3; i++)
ans = (ans + ans_m.val[i][0]) % MOD;
cout << ans << endl;
}
return 0;
}