Matrix Fast Power--(recursive expression)

Source: Internet
Author: User

Matrix Fast Power

First know the Matrix

Matrix is a set of complex numbers or real numbers arranged in a rectangular array ;

matrix multiplication:

Definition: Set AAs a matrix of MXP, BFor the PXN matrix, then the matrix of the MXN is called CAs a matrix AAnd BThe product of the C=AXB, where the J column element of line I of Matrix C can be represented as:


knowing that matrix multiplication, such as Fibonacci, is a recursive type,F (n) =f (n-1) +f (n-2), because matrix multiplication, so
set matrix A to                 Matrix B is                  then A*b is f (n) = f (n-1) *1+f (n-2) * * because we need matrices for the a*b matrix to be                               so the B matrix is inverted to the B-matrix.,so the matrix of F (n) is the initial matrix * (b^ (n-2)) n>=3;The resulting matrix a[0][0] is the value of f (n), where the time complexity is optimized in the power exponent that part of the fast power, saving time. so the key is to construct the matrix from a recursive type For example:
The structure of this matrix is
You can write this matrix, you understand; Here's a simple question, practice practiced hand.



Give a simple exercise: Click the Open link

#include <cstdio> #include <cstring> #include <cctype> #include <cmath> #include <set># Include <map> #include <list> #include <queue> #include <deque> #include <stack> #include <string> #include <bitset> #include <vector> #include <iostream> #include <algorithm># Include <stdlib.h>using namespace std;typedef long long ll;const int inf=2e9+1e8;const int Mod=1000007;const int MAX _size=1005;const int mm=3; LL f1,f2,a,b,c,n;struct mat{    LL maze[mm][mm];    void Set_empty ()     {    &NBSP ;   memset (maze,0,sizeof (Maze));   }}; Mat unit_mat={    1,0,0,    0,1,0,    0,0,1};  //  Define a unit matrix, multiplying any matrix by the unit matrix whose value equals itself; Mat operator * (Mat A,mat b)  //overloaded operator  *//  defines multiplication of two matrices, based on matrix multiply The definition of the law to write {    Mat c;    c.set_empty ();  //be sure to set to empty. Qing Zero Group; otherwise, a number will be added to the garbage value     LL i,j,k;    for (i=0;i<mm; i++)     {        for (j=0; j<mm; j + +)         {      &NB Sp     for (k=0; k<mm; k++)             {            & nbsp   C.MAZE[I][J]+=A.MAZE[I][K] * b.maze[k][j];                C.MAZE[I][J]%= M od;           }       }   }    return c;} Mat operator^ (Mat a,ll N)  //  optimization time is here,//  similar to the general int number of the fast power evaluation, the idea is the same. Don't understand can Baidu fast power. {    Mat c=unit_mat;    while (N)     {        if (n&1) c=a*c;  &N Bsp     a=a*a;        n>>=1;   }    return c;} void Solve () {    Mat a,b;    b.set_empty ();    A.set_empty ();    B.maze[0][0]=b, b.maze[1][0]=a,b.maze[2][0]=c;    B.MAZE[0][1]=B.Maze[2][2]=1;    b=b^ (n-2);    a.maze[0][0]=f2,a.maze[0][1]=f1,a.maze[0][2]=1;    LL ans =0;    Mat c=a*b;    printf ("%lld\n", (c.maze[0][0]+mod)%mod);} int main () {    int times;    scanf ("%d", &times);    while (times--)     {         scanf ("%lld%lld%lld%lld%lld%lld", &f1,&f2,&a,&b,&c,&n);  & nbsp     if (n = = 1)             printf ("%lld\n", (f1+mod)%mod);      &NBSP ; else if (n = = 2)             printf ("%lld\n", (f2+mod)%mod);        else so Lve ();  //its upper special sentence because the formula is defined in the field  n>=3;   }    return 0;}


Matrix Fast Power--(recursive expression)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.