-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet2036.cpp
More file actions
60 lines (49 loc) · 1.86 KB
/
leet2036.cpp
File metadata and controls
60 lines (49 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
using ll = long long;
class Solution {
public:
const int mod = 1e9 + 7;
vector<vector<ll>> multiplyMatrices(const vector<vector<ll>> &A, const vector<vector<ll>> &B) {
int rowsA = A.size(), colsA = A[0].size(), colsB = B[0].size();
vector<vector<__int128_t>> temp(rowsA, vector<__int128_t>(colsB, 0));
vector<vector<ll>> result(rowsA, vector<ll>(colsB, 0));
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
for (int k = 0; k < colsA; k++) {
temp[i][j] += A[i][k] * B[k][j];
}
result[i][j] = temp[i][j] % mod;
}
}
return result;
}
vector<vector<ll>> powerMatrix(vector<vector<ll>> matrix, ll exponent) {
vector<vector<ll>> result(matrix.size(), vector<ll>(matrix.size(), 0));
for (int i = 0; i < matrix.size(); i++) result[i][i] = 1;
while (exponent > 0) {
if (exponent % 2 == 1) result = multiplyMatrices(result, matrix);
matrix = multiplyMatrices(matrix, matrix);
exponent /= 2;
}
return result;
}
int lengthAfterTransformations(string s, int t, vector<int>& nums) {
vector<vector<ll>> transform(26, vector<ll>(26, 0));
for (int i = 0; i < 26; i++) {
for (int shift = 0; shift < nums[i]; shift++) {
transform[i][(i + 1 + shift) % 26]++;
}
}
transform = powerMatrix(transform, t);
vector<vector<ll>> freq(1, vector<ll>(26, 0));
for (char ch : s) {
freq[0][ch - 'a']++;
}
freq = multiplyMatrices(freq, transform);
int totalLength = 0;
for (int count : freq[0]) {
totalLength += count;
if (totalLength >= mod) totalLength -= mod;
}
return totalLength;
}
};