HDU 5015 233 Matrix fast power, hdu5015
Link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5015
Give a matrix of n * m (n ≤ 10, m ≤ 109). The A10, A20, a30 ..., a01 = 233, A02 = 2333, A03 = 23333... for Aij, Aij = Ai-1j + Aij-1, ask how much Anm is in the matrix.
Idea: the number of rows is only 10, so we can perform column-by-column recursion and perform 109 recursion requires the rapid power of a matrix.
Recursive matrix: initial matrix:
Code:
# Include <algorithm> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstring> # include <ctime> # include <ctype. h> # include <iostream> # include <map> # include <queue> # include <set> # include <stack> # include <string> # include <vector> # define eps 1e-8 # define INF 0x7fffffff # define maxn 10005 # define PI acos (-1.0) # define seed 31 // 131,1313 typedef long LL; typedef unsigned long ULL; using names Pace std; # define MOD 10000007 # define maxn 15 # define maxm 15 struct Matrix {int n, m; LL a [maxn] [maxm]; void init () {n = m = 0; memset (a, 0, sizeof (a);} void change (int c, int d) {n = c; m = d ;} matrix operator + (const Matrix & B) const {Matrix tmp; tmp. n = n; tmp. m = m; for (int I = 0; I <n; I ++) for (int j = 0; j <m; j ++) tmp. a [I] [j] = a [I] [j] + B. a [I] [j]; return tmp;} Matrix operator-(const Matrix & B) const {Matrix Tmp; tmp. n = n; tmp. m = m; for (int I = 0; I <n; I ++) for (int j = 0; j <m; j ++) tmp. a [I] [j] = a [I] [j]-B. a [I] [j]; return tmp;} Matrix operator * (const Matrix & B) const {Matrix tmp; tmp. n = n; tmp. m = B. m; for (int I = 0; I <n; I ++) {for (int j = 0; j <B. m; j ++) tmp. a [I] [j] = 0 ;}for (int I = 0; I <n; I ++) for (int j = 0; j <B. m; j ++) for (int k = 0; k <m; k ++) {tmp. a [I] [j] + = a [I] [k] * B. a [k] [j]; tmp. a [I] [j] % = MOD;} return tmp;} void Copy (const Matrix & x) {n = x. n; m = x. m; for (int I = 0; I <n; I ++) for (int j = 0; j <m; j ++) a [I] [j] = x. a [I] [j] ;}}; Matrix M_quick_pow (Matrix m, int k) {Matrix tmp; tmp. n = m. n; tmp. m = m. m; // m = n can be used for Fast Power (int I = 0; I <tmp. n; I ++) {for (int j = 0; j <tmp. n; j ++) {if (I = j) tmp. a [I] [j] = 1; else tmp. a [I] [j] = 0 ;}while (k) {if (k & 1) tmp. copy (tmp * m); k> = 1; m. copy (m * m);} return tmp;} int main () {# ifdef LOCAL freopen ("in. t Xt "," r ", stdin); // freopen (" out.txt "," w ", stdout); # endif Matrix A, B; int n, m; while (~ Scanf ("% d", & n, & m) {. init (); B. init ();. n = n + 2;. m = n + 2;. a [0] [0] = 10;. a [0] [n + 1] = 1; for (int I = 1; I <= n; I ++) {. a [I] [0] = 10; for (int j = 1; j <= I; j ++). a [I] [j] = 1;. a [I] [n + 1] = 1;}. a [n + 1] [n + 1] = 1; A = M_quick_pow (A, m); B. n = n + 2; B. m = 1; B. a [0] [0] = 23; for (int I = 1; I <= n; I ++) {scanf ("% I64d", & B. a [I] [0]); B. a [I] [0] % = MOD;} B. a [n + 1] [0] = 3; B. copy (A * B); if (n = 0 & m = 0) {puts ("0"); continue;} printf ("% I64d \ n ", b. a [n] [0]);} return 0 ;}
The following is a program for getting the remainder of the nth entry of the Fibonacci series in the matrix's fast power solution, but it always runs an error.
According to the normal logic, only a [2] [2] = {1, 1, 1, 0} The n (a [0] [1]) of the Fibonacci series can be obtained from the Npower of the matrix. However, if you ignore this, you are requesting a [0] [1], a [1] [0], when you set the value of a [1] [1], your value of a [0] [0] has actually changed, as a result, the value of a [0] [1] You obtained is incorrect, which affects the values of a [1] [0] And a [1] [1.
Therefore, when traversing these four values, we cannot change any of them. We can only change the value after the change. Therefore, we can use several variables to first store the obtained new matrix values, as shown below:
# Include <stdio. h>
Int a [2] [2] = {1, 1, 0}, B [2] [2] = {1, 1, 0 }; // use two-dimensional arrays to represent the matrix used by the fast power algorithm
Int main ()
{
Int n, m, I, j, t, u;
Int a1, a2, a3, a4;
While (scanf ("% d", & n, & m )! = EOF)
{
If (m =-1 & n =-1) // end the entire algorithm when the input m. n value is-1.
Return 0;
/* The following is the calculation of the nth Fibonacci number */
If (n = 0)
A [0] [0] = 0;
Else if (n = 1 | n = 2)
A [0] [0] = 1;
Else
{
For (I = 3; I <= n; I ++)
{
A1 = a [0] [0] * B [0] [0] + a [0] [1] * B [1] [0];
A2 = a [0] [0] * B [0] [1] + a [0] [1] * B [1] [1];
A3 = a [1] [0] * B [0] [0] + a [1] [1] * B [1] [0];
A4 = a [1] [0] * B [0] [1] + a [1] [1] * B [1] [1];
A [0] [0] = a1;
A [0] [1] = a2;
A [1] [0] = a3;
A [1] [1] = a4;
}
}
T = a [0] [0];
A [0] [0] = 1; // resets the matrix.
A [0] [1] = 1;
A [1] [0] = 1;
A [1] [1] = 0;
If (m = 0)
A [0] [0] = 0;
Else if (m = 1 | m = 2)
A [0] [0] = 1;
Else
{
For (j = 3; j <= m; j ++)
{
A1 = a [0] [0] * B [0] [0] + a [0] [1] * B [1] [0];
A2 = a [0] [0] * B [0] [1] + a [0] [1] * B [1] [1];
A3 = a [1] [0] * B [0] [0] + a [1] [1] * B [1] [0];
A4 = a [1] [0] * B [0] [1] + a [1] [1] * B [1] [1];
A [0] [0] = a1;
A [0] [1] = a2;
A [1] [0] = a3;
A [1] [1] = a4;
}
}
U = a [0] [0];
A [0] [0] = 1; // resets the matrix.
A [0] [1] = 1;
A [1] [0] = 1;
A [1] [1] = 0;
T = t % u;
Printf ("% d \ n", t );
}
Return 0;
}
Another point is that the two items after the multiplication of your matrix are wrong. Let's make a comparison.
In this way, you can get the desired result.
... Remaining full text>
Why am I wrong when I asked Daniel hdu 1575Tr A's binary matrix?
Examples/l. c, nmk {jhxfjdghjkhjkhgd} zghjsbfhz [jshdjgnsjhjg} jhgsdfbhvhbhkdfk (jhsdjkfgb) jgfusbjkbfbniamj