@@ -4,67 +4,51 @@ using namespace std;
44
55typedef long long ll;
66
7- template <int n , int m >
7+ template <int N , int M >
88struct Matrix {
9- ll v[n][m ];
9+ int v[N][M ];
1010
11- static Matrix<n, m> ones () {
12- Matrix<n, m> ans{};
13- for (int i = 0 ; i < n; i++)
14- ans.v [i][i] = 1 ;
15- return ans;
16- }
11+ static Matrix ones () {
12+ Matrix ans{};
13+ for (int i = 0 ; i < N; i++) ans.v [i][i] = 1 ;
14+ return ans;
15+ }
1716};
1817
19- ll const mod = 1e9 +7 ;
18+ template <int N, int M, int P>
19+ Matrix<N, P> operator *(Matrix<N, M> const & a, Matrix<M, P> const & b) {
20+ Matrix<N, P> ans{};
2021
21- template <int n, int m, int p>
22- Matrix<n, p> operator *(Matrix<n, m> const & a, Matrix<m, p> const & b)
23- {
24- auto ans = Matrix<n, p>{};
25- for (int i = 0 ; i < n; i++)
26- for (int j = 0 ; j < p; j++)
27- for (int k = 0 ; k < m; k++)
28- ans.v [i][j] = (ans.v [i][j] + a.v [i][k]*b.v [k][j])%mod;
29- return ans;
30- }
22+ for (int i = 0 ; i < N; i++) {
23+ for (int j = 0 ; j < P; j++) {
24+ for (int k = 0 ; k < M; k++) {
25+ add (ans.v [i][j], (ll)a.v [i][k] * b.v [k][j] % mod);
26+ }
27+ }
28+ }
3129
32- template <int n>
33- Matrix<n, n> power (Matrix<n, n> a, ll b)
34- {
35- auto ans = Matrix<n, n>::ones ();
36- while (b) {
37- if (b&1 ) ans = ans*a;
38- a = a*a;
39- b /= 2 ;
40- }
41- return ans;
30+ return ans;
4231}
4332
44- template <int n, int m >
45- ostream& operator << (ostream& out, Matrix<n, m> const & mat)
46- {
47- for ( int i = 0 ; i < n; i++ ) {
48- for ( int j = 0 ; j < m; j++)
49- out << mat. v [i][j] << " " ;
50- out << " \n " ;
51- }
52- return out ;
33+ template <int N >
34+ Matrix<N, N> fexp ( Matrix<N, N> a, ll b) {
35+ auto ans = Matrix<N, N>:: ones ();
36+ while (b ) {
37+ if (b& 1 ) ans = ans*a;
38+ a = a*a ;
39+ b /= 2 ;
40+ }
41+ return ans ;
5342}
5443
55- // Muro (OBI - 2018)
56- int main ()
57- {
58- Matrix<3 , 1 > base = {1 , 1 , 5 };
59-
60- ll n;
61- cin >> n;
62-
63- Matrix<3 ,3 > exp = {0 , 1 , 0 ,
64- 0 , 0 , 1 ,
65- 2 , 4 , 1 };
66-
67- auto ans = power (exp, n)*base;
68-
69- cout << ans.v [0 ][0 ] << " \n " ;
70- }
44+ template <int N>
45+ Matrix<N, N> coeff (vector<int > const & c) {
46+ Matrix<N, N> ans{};
47+ for (int i = 0 ; i < N-1 ; i++) {
48+ ans.v [i+1 ][i] = 1 ;
49+ }
50+ for (int i = 0 ; i < N; i++) {
51+ ans.v [0 ][i] = c[i];
52+ }
53+ return ans;
54+ }
0 commit comments