A simple recursive problem (fast power + matrix multiplication optimization + scrolling array)

Source: Internet
Author: User

Question F: A simple recursive problem time limit: 1 Sec memory limit: MB
Submitted by: 546 resolution: 48
Submitted State [Discussion Version] The topic description exists as follows: F (n+1) =a1*f (n) +a2*f (n-1) +...+an*f (1) Finding the result input of the value of the K term to 1000000007 modulo

Single set of test data

First line input two integers n, K (1<=n<=100,n<k<=10000000000)

The second line enters n integers f (1) F (2) ... F (N)

Line three enter n integers A1 A2 ... An

Output

Output an integer

Sample input
2   to   4
Sample output
10


Today to do this problem, found that this problem is the card of the time of the divine problem ... Let me learn a lot.
-The first one is me as usual on the quick power of the template, return a matrix structure, but I found that even run can not run = =, after the discovery is due to the matrix structure in the body opened a 200*200 two-dimensional array, the two functions can not return such a large space structure,
I began to think that metaphysics, has been changed, changed for 1 hours, then the constant changed to 100, did not expect to run, unexpectedly is the reason, and learn to one!

-The second one is I changed after the AC not, has been reminded of the time overrun, and then the group Dalao teach me new posture: optimized matrix multiplication and scrolling array.
Matrix mult (Matrix A,matrix B,intN)    {Matrix temp;  for(intI=0; i<n; i++)    {         for(intk=0; k<n; k++)        {            if(A.v[i][k] = =0)Continue;  for(intj=0; j<n; J + +) {Temp.v[i][j]= (temp.v[i][j]+ (a.v[i][k]*b.v[k][j])%mod)%MoD; }        }    }    returntemp;}

Previous:

 matrix mult (Matrix A,matrix b,int n) {matrix temp;  for  (int  i= 0 ; I<n ; i++  for  (int  j=0 ; J<n ; j++" { for  (int  K=0 ; K<n ; K++) {            TEMP.V[I][J]  = (temp.v[i][j]+ (a.v[i][k]*b.v[k][j])%mod)%MOD; }}}  return   temp;}  

This technique is very useful! Especially when the matrix is high-order and sparse.

The optimization of the scrolling array is really metaphysical ... I don't know why it's so fast.

The coefficient matrix for this problem is:

A1 A2 ... an1   0    00   1   ...  0          .... 0 0     1 0

AC Code:

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<vector>using namespaceStd;typedefLong LongLL;ConstLL mod =1000000007;Const intN =102; LL F[n];structmatrix{LL V[n][n]; Matrix () {memset (V,0,sizeof(v)); }} m[2]; Matrix mult (Matrix A,matrix B,intN)    {Matrix temp;  for(intI=0; i<n; i++)    {         for(intk=0; k<n; k++)        {            if(A.v[i][k] = =0)Continue;  for(intj=0; j<n; J + +) {Temp.v[i][j]= (temp.v[i][j]+ (a.v[i][k]*b.v[k][j])%mod)%MoD; }        }    }    returntemp;}voidpow_mod (LL t,ll N) { for(intI=0; i<n; i++) {m[0].v[i][i] =1; }     while(t) {if(t&1) m[0] = mult (m[0],m[1],n); m[1] = mult (m[1],m[1],n); T>>=1; }}intMain () {intN;    LL K; scanf ("%d%lld",&n,&k);  for(intI=0; i<n; i++) {scanf ("%lld", &f[n-1-i]); }     for(intI=0; i<n; i++) {scanf ("%lld", &m[1].v[0][i]); }     for(intI=1; i<n; i++) {m[1].v[i][i-1] =1; } k= kN;    Pow_mod (K,n); LL ans=0;  for(intI=0; i<n; i++) {ans= (ans+m[0].v[0][I]*F[I]%MOD)%MoD; } printf ("%lld\n", ans); return 0;}

A simple recursive problem (fast power + matrix multiplication optimization + scrolling array)

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.