P1939 [TEMPLATE] matrix acceleration (series), p1939 Matrix

Source: Internet
Author: User

P1939 [TEMPLATE] matrix acceleration (series), p1939 Matrix
Description

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

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

Returns the remainder value of the nth clause of Series a on 1000000007 (10 ^ 9 + 7.

Input/output format:

 

The first line is an integer T, indicating the number of queries.

The following T rows have a positive integer n in each row.

 

Output Format:

 

Each row outputs a non-negative integer to indicate the answer.

 

Input and Output sample input sample #1:
36810
Output sample #1:
4919
Description

For 30% of Data n <= 100;

For 60% of data, n <= 2*10 ^ 7;

For 100% of data T <= 100, n <= 2*10 ^ 9;

 
 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 using namespace std; 5 const int MAXN=101; 6 inline void read(int &n){char c='+';bool flag=0;n=0;     7 while(c<'0'||c>'9') c=='-'?flag=1,c=getchar():c=getchar();     8 while(c>='0'&&c<='9') n=n*10+c-48,c=getchar();flag==1?n=-n:n=n;} 9 struct matrix10 {11     long long  m[11][11];12     int h,l;matrix(){memset(m,0,sizeof(m));h=l=0;}13 };14 matrix ma;15 int limit;16 const int mod=1000000007;17 matrix mul(matrix a,matrix b)18 {19     matrix c;c.h=c.l=3;20     for(int k=0;k<a.l;k++)21         for(int i=0;i<a.h;i++)22             for(int j=0;j<b.l;j++)23                 c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j]))%mod;24     return c;25 }26 matrix fast_martix_pow(matrix ma,int p)27 {28     matrix bg;29     for(int i=0;i<3;i++)30         for(int j=0;j<3;j++)31             bg.m[i][j]=(i==j);32     bg.h=bg.l=3;33     while(p)34     {35         if(p&1)    bg=mul(bg,ma);36         ma=mul(ma,ma);37         p>>=1;38     }39     return bg;40 }41 int main()42 {43     int T;read(T);44     while(T--)45     {46         int m;47         read(m);48         if (m<=3&&m>0) {cout<<1<<endl; continue;} else if (m<=0) {cout<<0<<endl; continue;} 49         matrix a,b;50         a.m[0][0]=1;51         a.m[0][2]=1;52         a.m[1][0]=1;53         a.m[2][1]=1;54         b.m[0][0]=1;55         b.m[1][0]=1;56         b.m[2][0]=1;57         a.h=a.l=3;58         b.h=3;b.l=1;59         a=fast_martix_pow(a,m-3);60         a=mul(a,b);61         printf("%d\n",a.m[0][0]%mod);62     }63     return 0;64 }

 

Related Article

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.