"Bzoj" "3240" "NOI2013" Matrix game

Source: Internet
Author: User

Decimal fast Power + matrix multiplication + constant optimization

I heard that this problem can also be forced to calculate the recurrence of the type ... Then take multiplication to calculate it ...

However Konjac Konjac opted for a more violent approach = =

We find that this recursive process is linear, so we can use matrix multiplication to represent, $x =a*x+b$ such a recursive we can say: $$\begin{bmatrix} x& 1 \end{bmatrix} * \begin{bmatrix} A & 0 \ b& 1 \end{bmatrix} $$

Then we can make $s_1$ express xa+b, $s _2$ xc+d, then we have $ $ans =v * (({s_1}^{n-1}*s_2) ^{m-1} * {s_1}^{n-1}) $$

But I gave Tle a direct count ...

  

Here's a look at constant optimization:

We noticed that when matrix multiplication was: $$\begin{bmatrix} a& 0 \ b& 1 \end{bmatrix} * \begin{bmatrix} c& 0 \ d& 1 \end{bmatrix} = \begin{bmatrix} a*c& 0 \ a*d+b& 1 \end{bmatrix}$$

That is to say: The second column of 0 and 1 is not moving ... Then we can optimize the matrix multiplication process of most $o (n^3) $ to $o (n^2) $.

Here we ${s_1}^{n-1}$ two times, then we can use an intermediate variable to save first, can reduce the first operation (after all, the main part of the algorithm is to calculate the power)

1 /**************************************************************2 problem:32403 User:tunix4 language:c++5 result:accepted6 time:7980 Ms7 memory:3232 KB8 ****************************************************************/9  Ten //Bzoj 3240 One#include <vector> A#include <cstdio> -#include <cstring> -#include <cstdlib> the#include <iostream> -#include <algorithm> - #defineRep (i,n) for (int i=0;i<n;++i) - #defineF (i,j,n) for (int i=j;i<=n;++i) + #defineD (i,j,n) for (int i=j;i>=n;--i) - #definePB Push_back + using namespacestd; AInlineintGetint () { at     intv=0, sign=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') sign=-1; Ch=GetChar ();} -      while(ch>='0'&&ch<='9') {v=v*Ten+ch-'0'; Ch=GetChar ();} -     returnv*Sign ; - } - Const intn=1e6+Ten, inf=~0u>>2, p=1e9+7; intypedefLong LongLL; - /******************tamplate*********************/ to   + structmatrix{ -     intv[2][2]; theMatrix (intx=0) {F (I,0,1) F (J,0,1)if(i==j) v[i][j]=x;Elsev[i][j]=0;} *     int*operator[] (intx) {returnv[x];} $ }s1,s2,v;Panax NotoginsengInline Matrixoperator*(Matrix A,matrix b) { - Matrix C; the     if(a[0][1]==0&& a[1][1]==1&& b[0][1]==0&& b[1][1]==1){ +c[0][0]= (LL) a[0][0]*b[0][0]%P; Ac[0][1]=0; thec[1][0]= (LL) a[0][0]*b[1][0]+ (LL) a[1][0])%P; +c[1][1]=1; -         returnC; $     } $F (k,0,1) F (I,0,1) F (J,0,1) -c[i][j]= (LL) c[i][j]+ (LL) a[i][k]*b[k][j]%p)%P; -     returnC; the } -The inline matrix Pow (matrix A,intb) {WuyiMatrix C (1); theF (I,1, b) c=c*A; -     returnC; Wu } -Inline matrix Power (matrix A,Char*s) { AboutMatrix R (1);intL=strlen (s); $D (i,l-1,0){ -         if(s[i]-'0') R=r*pow (a,s[i]-'0'); -A=pow (A,Ten); -     } A     returnR; + } the CharN[n],m[n]; - intMain () { $ #ifndef Online_judge theFreopen ("3240.in","R", stdin); theFreopen ("3240.out","W", stdout); the #endif thescanf"%s", n); scanf"%s", m); -     intL1=strlen (N)-1; in      while(n[l1]=='0') n[l1--]='9'; then[l1]--; theL1=strlen (M)-1; About      while(m[l1]=='0') m[l1--]='9'; them[l1]--; the //printf ("%s%s\n", n,m); the     inta,b,c,d; +A=getint (); B=getint (); C=getint (); D=getint (); -v[0][0]=v[0][1]=1; v[1][0]=v[1][1]=0; thes1[0][0]=a; s1[0][1]=0; s1[1][0]=b; s1[1][1]=1;Bayis2[0][0]=c; s2[0][1]=0; s2[1][0]=d; s2[1][1]=1; theMatrix s3=Power (s1,m); thev=v* (Power (s3*s2,n) *S3); -printf"%d\n", v[0][0]); -     return 0; the}
View Code 3240: [Noi2013] Matrix game time limit:10 Sec Memory limit:256 MB
submit:890 solved:390
[Submit] [Status] [Discuss] Description

Tingting is a love matrix of the children, one day she wants to use a computer to generate a huge n-row m-column matrix (you do not have to worry about how she stored). The matrix that she generates satisfies a magical nature: if you use F[i][j] to represent the element in column J of the Matrix, then F[i][j] satisfies the following recursion:

F[1][1]=1
F[i,j]=a*f[i][j-1]+b (J!=1)
F[i,1]=c*f[i-1][m]+d (I!=1)
The a,b,c,d in a recursive style is a given constant.

Now tingting want to know f[n][m] The value is how much, please help her. Since the end result can be large, you only need to output F[N][M] divided by 1,000,000,007 remainder.

Input

A row has six integer n,m,a,b,c,d. The meaning title said

Output

Contains an integer that represents the remainder of f[n][m] divided by 1,000,000,007

Sample Input3 4 1 3 2 6Sample Output -HINT

The matrix in the sample is:

1 4 7 10

26 29 32 35

76 79 82 85



1<=n,m<=10^1000 000,a<=a,b,c,d<=10^9

Source [Submit] [Status] [Discuss]

"Bzoj" "3240" "NOI2013" Matrix game

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.