-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path138-special-isosceles-triangles.js
More file actions
95 lines (81 loc) · 1.86 KB
/
138-special-isosceles-triangles.js
File metadata and controls
95 lines (81 loc) · 1.86 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
/**
* Special Isosceles Triangles
* Time Complexity: O(T * log(N))
* Space Complexity: O(1)
*/
const fs = require("fs");
function solve() {
const buffer = fs.readFileSync(0);
let bufferIdx = 0;
function readString() {
let start = bufferIdx;
while (bufferIdx < buffer.length && buffer[bufferIdx] <= 32) {
bufferIdx++;
}
if (bufferIdx >= buffer.length) return null;
start = bufferIdx;
while (bufferIdx < buffer.length && buffer[bufferIdx] > 32) {
bufferIdx++;
}
return buffer.toString("utf8", start, bufferIdx);
}
const MOD = 1000000007n;
function multiply(A, B) {
const C = [
[0n, 0n, 0n],
[0n, 0n, 0n],
[0n, 0n, 0n],
];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
let sum = 0n;
for (let k = 0; k < 3; k++) {
sum += A[i][k] * B[k][j];
}
C[i][j] = sum % MOD;
}
}
return C;
}
function power(A, p) {
let res = [
[1n, 0n, 0n],
[0n, 1n, 0n],
[0n, 0n, 1n],
];
let base = A;
let exp = p;
while (exp > 0n) {
if ((exp & 1n) === 1n) res = multiply(res, base);
base = multiply(base, base);
exp >>= 1n;
}
return res;
}
const T_MAT = [
[18n, MOD - 1n, 0n],
[1n, 0n, 0n],
[1n, 0n, 1n],
];
const numTestCasesStr = readString();
if (!numTestCasesStr) return;
const T = parseInt(numTestCasesStr, 10);
for (let i = 0; i < T; i++) {
const nStr = readString();
if (!nStr) break;
const N = BigInt(nStr);
if (N === 1n) {
console.log("17");
continue;
}
const matPow = power(T_MAT, N - 1n);
const L2 = 305n;
const L1 = 17n;
const S1 = 17n;
let ans = (matPow[2][0] * L2 + matPow[2][1] * L1 + matPow[2][2] * S1) % MOD;
console.log(ans.toString());
}
};
if (require.main === module) {
solve();
};