Exploration (mathematics) (fast multiplication of matrices)

Source: Internet
Author: User



Sentence Test instructions: three-dimensional space divided into four-dimensional space, the maximum can be divided into how many parts.

We directly think of the four-dimensional bad thinking, but the general problem we consider from the low-dimensional start.

After the hand count we can find:

Set \ (f (x) \) is the most divided section of 0 dimensions (points) Chei Ivi (lines), recursive:\ (f (x) =f (x-1) +1\)

Set \ (g (x) \) is one-dimensional (line) Chei (planar) Most divided section, recursive:\ (g (x) =g (x-1) +f (x-1) \)

Set \ (k (x) \) for two-dimensional (planar) cut three-dimensional (space) the most divided part, recursive:\ (k (x) =k (x-1) +g (x-1) \)

Set \ (h (x) \) for three-dimensional (space) cut four dimensions of the most divided section, recursive:\ (h (x) =h (x-1) +k (x-1) \)

Then we can easily see that this is a recursive. But because the data range is very large, we consider constructing the matrix to perform the fast power acceleration of the matrix. (Will not play Mathjax matrix, can only make a little bit)

1 1 0) 0 0
0 1 1) 0 0
0 0 1) 1 0
0 0 0) 1 1
0 0 0) 0 1

Then the initial matrix [1,f (0), g (0), K (0), H (0)]

But because the data is really large, the matrix fast power to multiply is likely to explode long long, so we use fast multiply to ensure that the number within the MoD range.

The code is as follows:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #define MAXN 40using namespace Std;long long N,m;long long f[maxn];struct node{long long T[MAXN][MAXN];};    Node Init;inline Long Long multi (long long X,long long Y,long long MoD) {long long ans=0;        while (y) {if (y&1) ans= (ans+x)%mod;        x= (x+x)%mod;    y>>=1; } return ans%mod;}    Inline node Mul (node X,node y) {node cur;    for (int i=1;i<=5;i++) for (int j=1;j<=5;j++) cur.t[i][j]=0; for (int i=1;i<=5;i++) for (int. j=1;j<=5;j++) for (int k=1;k<=5;k++) cur.t[i][j]= (c    Ur.t[i][j]+multi (x.t[i][k],y.t[k][j],m))%m; return cur;}    inline void Solve () {long long cur[maxn];    memset (cur,0,sizeof (cur));    for (int j=1;j<=5;j++) for (int k=1;k<=5;k++) cur[j]= (Cur[j]+multi (f[k],init.t[k][j],m))%m; for (int i=1;i<=5;i++) f[i]=cur[i];} int main () {freopen ("discover.in"," R ", stdin);    Freopen ("Discover.out", "w", stdout);    scanf ("%lld%lld", &n,&m);    F[1]=1;    F[2]=1;    F[3]=1;    F[4]=1;    F[5]=1;    for (int i=1;i<=5;i++) init.t[i][i]=1;    Init.t[1][2]=1;    Init.t[2][3]=1;    Init.t[3][4]=1;    Init.t[4][5]=1;        while (n) {if (n&1) solve ();        Init=mul (Init,init);    n>>=1;    } printf ("%lld\n", f[5]%m); return 0;}

Explore (math) (fast multiplication of matrices)

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.