当前位置:网站首页>Matrix fast power notes
Matrix fast power notes
2022-06-26 10:42:00 【MervynLammm】
List of articles
Matrix fast power
Conventional exponentiation
seek a n a^n an Value , If you use pure for loop , To cycle n Second ride a.
Time complexity :O(n)
Spatial complexity :S(1)
recursive
Similar to dichotomy .
example :
a 57 = { a 28 { a 14 { a 7 { a 3 { a a a a 3 a a 7 a 14 a 28 a a^{57} = \left \{\begin{matrix} a^{28} & \left \{\begin{matrix} a^{14} & \left \{\begin{matrix} a^{7} & \left \{\begin{matrix} a^{3} & \left \{\begin{matrix} a \\ a \\ a \end{matrix} \right.\\ a^{3} \\ a \end{matrix} \right.\\ a^{7} \\ \end{matrix} \right.\\ a^{14} \\ \end{matrix} \right.\\ a^{28}\\ a \end{matrix} \right. a57=⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧a28a28a⎩⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎧a14a14⎩⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎧a7a7⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧a3a3a⎩⎨⎧aaa
Just divide the power by two , Take the square ( cube a) that will do .
a 2 k = { a k a k a 2 k + 1 = { a k a k a a^{2k}= \begin{cases} a^k\\ a^k\\ \end{cases}\\ a^{2k+1}= \begin{cases} a^k\\ a^k\\ a \end{cases} a2k={ akaka2k+1=⎩⎪⎨⎪⎧akaka
Time complexity :O(logn)
Spatial complexity :O(logn)
Fast power
Yes n Binary decomposition .
example : solve a 57 a^{57} a57
- take 57 Change to binary
111001, For binary is 1 Bit value of , namely
a 57 = a 1 ∗ a 8 ∗ a 16 ∗ a 32 a^{57} = a^{1} * a^{8} * a^{16} * a^{32} a57=a1∗a8∗a16∗a32 - Find the value of each binary :
- initialization
a = 1 - From the bottom , Each binary value is
a *= a
- initialization
- Convert binary to 1 Multiply the bit values of .
public int MOD = 1000000007;
public static int qp(int a, int n) {
int ans = 1;
while (n > 0) {
if ((n&1) == 1) {
ans = (int) (((long)ans * a) % MOD);
}
a = (int) (((long)a * a) % MOD);
n >>= 1;
}
return ans;
}
Matrix fast power
Matrix multiplication to solve recursive state transition
Take the Fibonacci sequence as an example
f ( n ) = { f ( n − 1 ) + f ( n − 2 ) n > 1 1 0 < = n < = 1 f(n) = \begin{cases} f(n-1) + f(n-2) & n > 1 \\ 1 & 0<=n<=1 \end{cases} f(n)={ f(n−1)+f(n−2)1n>10<=n<=1
Using matrix multiplication
[ 0 1 1 1 ] [ f ( n − 2 ) f ( n − 1 ) ] = [ f ( n − 1 ) f ( n ) ] \begin{bmatrix} 0&1\\ 1&1 \end{bmatrix} \begin{bmatrix} f(n-2)\\ f(n-1) \end{bmatrix}= \begin{bmatrix} f(n-1)\\ f(n) \end{bmatrix} [0111][f(n−2)f(n−1)]=[f(n−1)f(n)]
Set the matrix to A
A = [ 0 1 1 1 ] A = \begin{bmatrix} 0&1\\ 1&1 \end{bmatrix} A=[0111]
Then the final matrix can be decomposed into
[ f ( n ) f ( n + 1 ) ] = A n [ f ( 0 ) f ( 1 ) ] \begin{bmatrix} f(n)\\ f(n+1) \end{bmatrix}= A^{n} \begin{bmatrix} f(0)\\ f(1) \end{bmatrix} [f(n)f(n+1)]=An[f(0)f(1)]
Determine the matrix A
Any state transition equation can determine the matrix A, example :
f ( n ) = 3 f ( n − 1 ) + 6 f ( n − 2 ) − 7 f ( n − 3 ) [ 0 1 0 0 0 1 − 7 6 3 ] [ f ( 0 ) f ( 1 ) f ( 2 ) ] = [ f ( 1 ) f ( 2 ) f ( 3 ) ] A = [ 0 1 0 0 0 1 − 7 6 3 ] f(n) = 3f(n-1) + 6f(n-2) - 7f(n-3) \\ \begin{bmatrix} 0&1&0\\ 0&0&1\\ -7&6&3 \end{bmatrix} \begin{bmatrix} f(0)\\ f(1)\\ f(2) \end{bmatrix}= \begin{bmatrix} f(1)\\ f(2)\\ f(3) \end{bmatrix} \\ A = \begin{bmatrix} 0&1&0\\ 0&0&1\\ -7&6&3 \end{bmatrix} f(n)=3f(n−1)+6f(n−2)−7f(n−3)⎣⎡00−7106013⎦⎤⎣⎡f(0)f(1)f(2)⎦⎤=⎣⎡f(1)f(2)f(3)⎦⎤A=⎣⎡00−7106013⎦⎤
java Code implementation
public static int[][] matrix_qp(int n) {
// Take the Fibonacci series for example
// matrix A
//0 1
//1 1
int[][] A = new int[2][2];
A[0][0] = 0;
A[0][1] = A[1][0] = A[1][1] = 1;
// Fibonacci sequence 0 1 matrix
//1 0
//1 0
int[][] F = new int[2][2];
F[0][0] = F[1][0] = 1;
F[0][1] = F[1][1] = 0;
return matrix_multi(matrix_mi(A, n), F);
}
public static int[][] matrix_multi(int[][] a, int[][] b) {
int[][] ans = new int[2][2];
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
for (int k = 0; k < 2; ++k)
ans[i][j] += a[i][k] * b[k][j];
return ans;
}
public static int[][] matrix_mi(int[][] a, int n) {
int[][] ans = new int[2][2];
ans[0][0] = ans[1][1] = 1;
while(n > 0) {
if ((n&1) == 1)
ans = matrix_multi(ans, a);
a = matrix_multi(a, a);
n >>= 1;
}
return ans;
}
public static void show_matrix(int[][] a) {
System.out.println("Matrix");
for (int i = 0; i < a.length; ++i) {
for (int j = 0; j < a[i].length; ++j)
System.out.print(a[i][j] + " ");
System.out.println();
}
}
The above code does not perform modulo operation .
When doing modular operation, you should pay attention to :
When the matrix contains negative numbers , To ensure that the modulus is large and 0
(a%c - b%c + c)%c
Reference material
边栏推荐
- echo $?
- Global and Chinese market of cryogenic bulk tanks 2022-2028: Research Report on technology, participants, trends, market size and share
- Flutter与原生通信(上)
- Basic MySQL
- Renesas electronics launched a complete intelligent sensor solution for Internet of things applications
- What are the symbolic and direct references of the JVM
- SQL Server 基础介绍整理
- 【无标题】
- What is LSP
- 【Leetcode】76. Minimum covering substring
猜你喜欢
随机推荐
開發者,微服務架構到底是什麼?
創建對象的時候堆內存的分配
Searchview click failure
Blog article index Summary - wechat games
Développeur, quelle est l'architecture des microservices?
Progressive Web 应用程序PWA是应用程序开发的未来
Problems encountered in the application and development of Hongmeng and some roast
String constant pool, class constant pool, and runtime constant pool
六月集训(第26天) —— 并查集
磁带库简单记录1
Allocation of heap memory when creating objects
AIX基本操作记录
SQL Server 基础介绍整理
【無標題】
Linux下安装Mysql【详细】
CentOS installs redis multi master multi slave cluster
Cmake / set command
Redis中执行Lua脚本
搜索引擎高级搜索方法记录
Global and Chinese market for baked potato chips 2022-2028: Research Report on technology, participants, trends, market size and share








