|
1 | 1 | package g3301_3400.s3337_total_characters_in_string_after_transformations_ii;
|
2 | 2 |
|
3 | 3 | // #Hard #String #Hash_Table #Dynamic_Programming #Math #Counting
|
4 | | -// #2024_10_29_Time_67_ms_(99.31%)_Space_45.4_MB_(45.83%) |
| 4 | +// #2025_05_14_Time_80_ms_(72.97%)_Space_45.62_MB_(24.32%) |
5 | 5 |
|
6 | 6 | import java.util.List;
|
7 | 7 |
|
8 | 8 | public class Solution {
|
9 | | - public static final int MOD = 1000000007; |
10 | | - public static final long M2 = (long) MOD * MOD; |
11 | | - public static final long BIG = 8L * M2; |
| 9 | + private static final int MOD = 1_000_000_007; |
12 | 10 |
|
13 | | - public int lengthAfterTransformations(String s, int t, List<Integer> nums) { |
14 | | - int[][] m = new int[26][26]; |
15 | | - for (int i = 0; i < 26; i++) { |
16 | | - for (int j = 1; j <= nums.get(i); j++) { |
17 | | - m[(i + j) % 26][i]++; |
18 | | - } |
19 | | - } |
20 | | - int[] v = new int[26]; |
| 11 | + public int lengthAfterTransformations(String s, int t, List<Integer> numsList) { |
| 12 | + int[][] localT = buildTransformationMatrix(numsList); |
| 13 | + int[][] tPower = matrixPower(localT, t); |
| 14 | + int[] freq = new int[26]; |
21 | 15 | for (char c : s.toCharArray()) {
|
22 | | - v[c - 'a']++; |
| 16 | + freq[c - 'a']++; |
23 | 17 | }
|
24 | | - v = pow(m, v, t); |
25 | | - long ans = 0; |
26 | | - for (int x : v) { |
27 | | - ans += x; |
| 18 | + long result = 0; |
| 19 | + for (int i = 0; i < 26; i++) { |
| 20 | + long sum = 0; |
| 21 | + for (int j = 0; j < 26; j++) { |
| 22 | + sum = (sum + (long) freq[j] * tPower[j][i]) % MOD; |
| 23 | + } |
| 24 | + result = (result + sum) % MOD; |
28 | 25 | }
|
29 | | - return (int) (ans % MOD); |
| 26 | + |
| 27 | + return (int) result; |
30 | 28 | }
|
31 | 29 |
|
32 | | - // A^e*v |
33 | | - private int[] pow(int[][] a, int[] v, long e) { |
34 | | - for (int i = 0; i < v.length; i++) { |
35 | | - if (v[i] >= MOD) { |
36 | | - v[i] %= MOD; |
37 | | - } |
38 | | - } |
39 | | - int[][] mul = a; |
40 | | - for (; e > 0; e >>>= 1) { |
41 | | - if ((e & 1) == 1) { |
42 | | - v = mul(mul, v); |
| 30 | + private int[][] buildTransformationMatrix(List<Integer> numsList) { |
| 31 | + int[][] localT = new int[26][26]; |
| 32 | + for (int i = 0; i < 26; i++) { |
| 33 | + int steps = numsList.get(i); |
| 34 | + for (int j = 1; j <= steps; j++) { |
| 35 | + localT[i][(i + j) % 26]++; |
43 | 36 | }
|
44 | | - mul = p2(mul); |
45 | 37 | }
|
46 | | - return v; |
| 38 | + return localT; |
47 | 39 | }
|
48 | 40 |
|
49 | | - // int matrix*int vector |
50 | | - private int[] mul(int[][] a, int[] v) { |
51 | | - int m = a.length; |
52 | | - int n = v.length; |
53 | | - int[] w = new int[m]; |
54 | | - for (int i = 0; i < m; i++) { |
55 | | - long sum = 0; |
56 | | - for (int k = 0; k < n; k++) { |
57 | | - sum += (long) a[i][k] * v[k]; |
58 | | - if (sum >= BIG) { |
59 | | - sum -= BIG; |
60 | | - } |
| 41 | + private int[][] matrixPower(int[][] matrix, int power) { |
| 42 | + int size = matrix.length; |
| 43 | + int[][] result = new int[size][size]; |
| 44 | + for (int i = 0; i < size; i++) { |
| 45 | + result[i][i] = 1; |
| 46 | + } |
| 47 | + while (power > 0) { |
| 48 | + if ((power & 1) == 1) { |
| 49 | + result = multiplyMatrices(result, matrix); |
61 | 50 | }
|
62 | | - w[i] = (int) (sum % MOD); |
| 51 | + matrix = multiplyMatrices(matrix, matrix); |
| 52 | + power >>= 1; |
63 | 53 | }
|
64 | | - return w; |
| 54 | + return result; |
65 | 55 | }
|
66 | 56 |
|
67 | | - // int matrix^2 (be careful about negative value) |
68 | | - private int[][] p2(int[][] a) { |
69 | | - int n = a.length; |
70 | | - int[][] c = new int[n][n]; |
71 | | - for (int i = 0; i < n; i++) { |
72 | | - long[] sum = new long[n]; |
73 | | - for (int k = 0; k < n; k++) { |
74 | | - for (int j = 0; j < n; j++) { |
75 | | - sum[j] += (long) a[i][k] * a[k][j]; |
76 | | - if (sum[j] >= BIG) { |
77 | | - sum[j] -= BIG; |
78 | | - } |
| 57 | + private int[][] multiplyMatrices(int[][] a, int[][] b) { |
| 58 | + int size = a.length; |
| 59 | + int[][] result = new int[size][size]; |
| 60 | + for (int i = 0; i < size; i++) { |
| 61 | + for (int k = 0; k < size; k++) { |
| 62 | + if (a[i][k] == 0) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + for (int j = 0; j < size; j++) { |
| 66 | + result[i][j] = (int) ((result[i][j] + (long) a[i][k] * b[k][j]) % MOD); |
79 | 67 | }
|
80 | | - } |
81 | | - for (int j = 0; j < n; j++) { |
82 | | - c[i][j] = (int) (sum[j] % MOD); |
83 | 68 | }
|
84 | 69 | }
|
85 | | - return c; |
| 70 | + return result; |
86 | 71 | }
|
87 | 72 | }
|
0 commit comments