forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
78 lines (72 loc) · 2.06 KB
/
Solution.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
class Solution {
const int M = 1000000007;
int add(int x, int y) {
if ((x += y) >= M) {
x -= M;
}
return x;
}
int mul(long long x, long long y) {
return x * y % M;
}
vector<int> getz(const string& s) {
const int n = s.length();
vector<int> z(n);
for (int i = 1, left = 0, right = 0; i < n; ++i) {
if (i <= right && z[i - left] <= right - i) {
z[i] = z[i - left];
} else {
for (z[i] = max(0, right - i + 1); i + z[i] < n && s[i + z[i]] == s[z[i]]; ++z[i])
;
}
if (i + z[i] - 1 > right) {
left = i;
right = i + z[i] - 1;
}
}
return z;
}
vector<vector<int>> mul(const vector<vector<int>>& a, const vector<vector<int>>& b) {
const int m = a.size(), n = a[0].size(), p = b[0].size();
vector<vector<int>> r(m, vector<int>(p));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < p; ++k) {
r[i][k] = add(r[i][k], mul(a[i][j], b[j][k]));
}
}
}
return r;
}
vector<vector<int>> pow(const vector<vector<int>>& a, long long y) {
const int n = a.size();
vector<vector<int>> r(n, vector<int>(n));
for (int i = 0; i < n; ++i) {
r[i][i] = 1;
}
auto x = a;
for (; y; y >>= 1) {
if (y & 1) {
r = mul(r, x);
}
x = mul(x, x);
}
return r;
}
public:
int numberOfWays(string s, string t, long long k) {
const int n = s.length();
const auto dp = pow({{0, 1}, {n - 1, n - 2}}, k)[0];
s.append(t);
s.append(t);
const auto z = getz(s);
const int m = n + n;
int r = 0;
for (int i = n; i < m; ++i) {
if (z[i] >= n) {
r = add(r, dp[!!(i - n)]);
}
}
return r;
}
};