Pow: *** should be added to the POW of a namespace ::****
1 namespace POW {
2 typedef int t; // you can change "int" to the Data Type stored in the matrix.
3 const t mod = T (1e9 + 7); // change to the modulus required by the fast power.
4
5 template <class T>
6 T powmod (t a, int N, t mod ){
7 t ans =;
8 -- N;
9 While (n ){
10 if (N & 1) ans = ans * A % MOD;
11 A = A * A % MOD;
12 N> = 1;
13}
14 return ans;
15}
16 // powmod (t a, int N, t mod)
17
18 struct mat {
19 t a, B, c, d; // you can change it to a two-dimensional array and modify the following function if necessary.
20 MAT (t w, t x, t y, t Z): A (W), B (X), C (Y), D (z ){}
21 mat operator * (MAT & B) const {
22 t w, x, y, z;
23 W = (LL) a * B. a) % mod + (LL) B * B. C) % mod) % MOD;
24 x = (LL) a * B. B) % mod + (LL) B * B. D) % mod) % MOD;
25 y = (LL) C * B. a) % mod + (LL) D * B. C) % mod) % MOD;
26 z = (LL) C * B. B) % mod + (LL) D * B. D) % mod) % MOD;
27 return MAT (w, x, y, z );
28}
29}; // struct mat
30
31} // namespace pow
Quick power (including second-order square matrix) template