[Mathematical-Tectonic matrix] Nefu 1113

Source: Internet
Author: User

according to test instructions, I have deduced the TN formula, Ti=ti.a+ti.b,ti.a=5*t (i-1). A+4*t ( i-1). B,ti.b=t (i-1). A+t (i-1). b

However, the following can not continue to push the formula SN!!!!

This problem is to find the first n of arbitrary sequence and, when the recurrence formula of SN is not obvious , the matrix is used to solve.

Set matrix a=, matrix f0=

Then set the Matrix s= (a+a2+a3 .... + an) *f0

The final answer is the sum of the two elements in the matrix S.

So how to beg a+a2+a3 .... + an?

You can continue to construct the following block matrix, where I is the unit matrix

Set r=, there are: r2=,r3=

Can be found in the upper right corner is i + A + a^2 + ... + a^n, one more I will be lost on the back.

The r^n can be obtained by fast power;

However, the above method for this problem is still tle, read the code discovery, can be deduced to further reduce the order of the Matrix, I here is the four-order, and the operation of the code is only three order.

Continue thinking:

See if we can directly deduce the formula of the S, see the explanation:

T[i] = dp[i][0]+dp[i][1]

=6*DP[I-1][1]+5*DP[I-1][0]

=6*T[I-1]-DP[I-1][0]

=6*t[i-1]-t[i-2]

According to S[i]=s[i-1]+t[i] can be calculated:

s[i]=s[i-1]+ 6*t[i-1]-t[i-2]

Then there are formulas:

Set r=, get it done!

Summary: The application of the matrix, carefully study the above structure matrix and derivation process, the first method of constructing the block matrix is very useful , it is not good for the SN formula directly constructs the matrix. But if the formula such as above s[i]=s[i-1]+ 6*t[i-1]-t[i-2] can deduce the recursive matrix of SN, it can reduce the complexity.

#include <stdio.h>#include<cstring>#include<cmath>#include<algorithm>#include<queue>using namespacestd;Const Long LongMoD =1000000007; structMa {Long Longm[3][3]; }; Maoperator*(ma A,ma b) {Ma C;  for(intI=0; i<3; i++)          for(intj=0; j<3; J + +) {C.m[i][j]=0;  for(intK =0; K <3; k++) {C.m[i][j]+ = (a.m[i][k]*B.m[k][j]); if(c.m[i][j]>=mod| | C.M[I][J]&LT;=-MOD) c.m[i][j]%=MoD; //C.m[i][j]%=mod;            }         }     returnC;} Ma mm[ $]; Long LongCalLong LongN) {intCur=0; Ma ans= {1,6,-1,              0,6,-1,              0,1,0             };  while(n) {if(n&1) {ans= ans*Mm[cur]; } cur++; N>>=1; }     Long Longtmp= A*ans.m[0][0]%mod+ **ans.m[0][1]%mod+6*ans.m[0][2]%mod+MoD; TMP%=MoD;  while(tmp<0) tmp+=MoD; printf ("%lld\n", TMP); returntmp;} //long Long ans[10000005];intMain () {Ma tmp= {1,6,-1,              0,6,-1,              0,1,0             }; mm[0] =tmp;  for(intI=1; i< -; i++) mm[i]=mm[i-1]*mm[i-1]; Long LongN;  while(SCANF ("%lld", &n)! =EOF) {         if(n==1) puts ("6"); Else if(n==2) puts (" A"); ElseCal (n3); }     return 0; }

[Mathematical-Tectonic matrix] Nefu 1113

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.