# Luo Gu P3390 [template] matrix fast power

### Description

• Given n * n matrix A, A ^ k seek
• n <= 100, k <= 10 ^ 12, | matrix elements | <= 1000

### Input

• The first row, n, k

n + 1 to the second row, each row number n, i + 1 th row and j denotes the number of elements of a matrix row i and column j

### Output

• A ^ k output

A total of n lines, each line number n, the i-th row and j represents the number of i-th element of the matrix row j-th column, each element of the mold 10 ^ 7 + 9

2 1
1 1
1 1

### Sample output

1 1
1 1

• Algorithm as shown in the title.

• What matrices are?

• It is a matrix, literal meaning.
• How matrix operations?

1. Addition: For two shaped as a matrix, like a position corresponding to the sum
3. Division: looks like in practice is there, but is not commonly used in OI, do not discuss
4. Multiplication : Matrix multiplication is the highlight, she had a prerequisite, the multiplication of two matrices, must satisfy the (n, m) * (m , p) = (n, p). (Front row is a number, the number is a column). Like this ↓ (2, 2) * ( 2, 1) = (2, 1)

$\left\{ \begin{matrix} 1 & 1 \\ 0 & 3 \\ \end{matrix} \right\} * \left\{ \begin{matrix} 2 \\ 2 \\ \end{matrix} \right\} = \left\{ \begin{matrix} 1 * 2 + 1 * 2 \\ 0 * 2 + 3 * 2 \\ \end{matrix} \right\}$

\ [\ Left [\ begin {matrix} 2 \\ 2 \\ \ end {matrix} \ right] * \ left [\ begin {matrix} 1 & 1 \\ 0 & 3 \\ \ end {matrix} \ right ]! = \ left [\ begin {matrix} 1 * 2 + 1 * 2 \\ 0 * 2 + 3 * 2 \\ \ end {matrix} \ right] but it is wrong to write! \]

• Because (2, 1) * (2, 2). 1 ! = 2, does not meet the significance matrix multiplication.

• It can also launch it: matrices is not commutative
• Matrix What's the use?

• Recursive optimization.
• How do I know when to optimize recursive?
• Great range of data , such as what power of 18, O (n) 10 are hard on
• How to optimize?
• General optimization is to optimize power on the matrix fast, but how fast optimization After writing power, turn here
• So now the task is to write a matrix to quickly power!
• Fast Matrix Mi:

• First matrix multiplication is easy to get is associative, so the power of fast form is no different with the ordinary power of fast, given the code directly, savor it. (Anyway, I read an article about you understand)
#include <iostream>
#include <cstdio>
#define maxn 105
#define mod 1000000007
#define LL long long
using namespace std;

struct Obj {LL a[maxn][maxn];} x, d;
LL n, p;

Obj mul(Obj x, Obj y)
{
Obj r;
for(LL i = 1; i <= n; i++)
for(LL j = 1; j <= n; j++)
r.a[i][j] = 0;
for(LL i = 1; i <= n; i++)
for(LL j = 1; j <= n; j++)
for(LL k = 1; k <= n; k++)
r.a[i][j] += (x.a[i][k] % mod * y.a[k][j] % mod) % mod,
r.a[i][j] %= mod;
return r;
}

Obj power(Obj x, LL p)
{
Obj r = d, base = x;
while(p)
{
if(p & 1) r = mul(r, base);
base = mul(base, base);
p >>= 1;
}
return r;
}

int main()
{
cin >> n >> p;
for(LL i = 1; i <= n; i++)
for(LL j = 1; j <= n; j++)
cin >> x.a[i][j];
for(LL i = 1; i <= n; i++) d.a[i][i] = 1;
Obj ans = power(x, p);
for(LL i = 1; i <= n; i++)
{
for(LL j = 1; j <= n; j++)
cout << ans.a[i][j] % mod << ' ';
cout << endl;
}
return 0;
}

### Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11203363.html
Recommended
Ranking
Daily