Template C + + 02 number theory algorithm 4 matrix multiplication

Source: Internet
Author: User
Tags mul

Matrix multiplication: Used to seek some kind of recursive relationship.

matrix multiplication is only meaningful if the number of columns in the first matrix is the same as the number of rows in the second matrix .

Defined

Set a to A*m matrix,B is the matrix of the m*b, then the matrix C is the product of matrix A and b , where the line I in Matrix C, the J column element can be expressed as:

As shown below:

Open a 2*2 matrix: mainly for the convenience of fast power, one can and oneself multiply on many times (>=2) matrix only may be square, so to open such a matrix.

"Title description"

A[1]=a[2]=a[3]=1

A[x]=a[x-3]+a[x-1] (x>3)

The value of 1000000007 (10^9+7) of the nth item of the a sequence is obtained.

"Input Format"

The first line is an integer t, which indicates the number of queries.

The following T-line, one positive integer n per line.

"Output Format"

Each row outputs a non-negative integer that represents the answer.

"Sample Input" 3 6 8 10

"Sample Output" 4 9 19

"Data range" for 100% of data t<=100,n<=2*10^9;

Then we use matrix multiplication to recursively push.

If you want to pre-treatment, is also possible, but t<=100, so lazy save space.

structmod{Long Longa[4][4]; MoD () {memset (A,0,sizeof(a)); }};mod Mul (mod a,mod b)//matrix multiplication{mod C;  for(intI=1; i<=3; i++)         for(intj=1; j<=3; j + +)             for(intk=1; k<=3; k++) C.a[i][j]= (C.a[i][j]+a.a[i][k]*b.a[k][j])%1000000007; returnC;}voidMakeintN)    {mod a,c; c.a[1][1]=1; c.a[2][1]=1; c.a[3][1]=1; a.a[1][1]=0; a.a[1][2]=1; a.a[1][3]=0; a.a[2][1]=0; a.a[2][2]=0; a.a[2][3]=1; a.a[3][1]=1; a.a[3][2]=0; a.a[3][3]=1; N++;  while(n>0)//Fast Power    {        if(n&1) C=mul (c,a);//can't be Mul (a,c)A=mul (A,a);//(a^n) *bn>>=1; } printf ("%d\n", c.a[3][3]%1000000007);}intMainintargcChar*argv[]) {    intT;SCANF ("%d",&t);  while(t--)    {        intN; scanf ("%d",&N);    Make (n); }}

Template C + + 02 number theory algorithm 4 matrix multiplication

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.