Link: https://www.nowcoder.com/acm/contest/105/G
Source: Niuke.com
Topic description
This is an enhanced version of the Fibonacci sequence.
Given recursion
Find the value of F(n). Since this value may be too large, take modulo 10
9 +7.
Enter description:
The first line is an integer T (1 ≤ T ≤ 1000), representing the number of samples.18
After each sample line, is an integer n (1 ≤ n ≤ 10
)。
Output description:
Each example outputs one line, an integer representing F(n) mod 1000000007.
Example 1
enter
4 1 2 3 100
output
1 16 57 558616258
This question is a template question for the fast power of a matrix, which means that the first time you encounter the fast power of the matrix and then learn it by the way, in fact, it is similar to the fast power.
The difficulty of matrix fast power is generally to construct a matrix, but the recursive formula of this question gives us it is very easy to construct a matrix.
The difficulty is generally to let us deduce the formula ourselves, and then write the required matrix through the formula.
The recurrence formula gives us,
is to find a matrix A [f(i),f(i-1),(i+1)^3,(i+1)^2,(i+1),1] * A =[f(i-1),f(i-2),i^3,i^2,i,1];
If you have learned line generation, this is very easy to deduce.
Then through the properties of the matrix, F[n]=A^(n-1)*F[1];
Knowing this, you will find that this is not an SB problem?
1 #include <cstdio> 2 #include <algorithm> 3 #include <vector> 4 #include <queue> 5 #include <cstring> 6 #include <string> 7 using namespace std; 8 const int maxn = 1e2 + 10; 9 const int mod = 1e9 + 7; 10 typedef long long LL ; 11 struct mat 12 { 13 LL m[maxn][maxn]; 14 mat() { 15 memset(m,0,sizeof(m)); 16 } 17 mat operator * (mat &a ) { 18 mat ans; 19 for (int i=0 ;i<6 ;i++ ){ 20 for (int j=0 ;j<6 ;j++) { 21 for (int k=0 ;k<6 ;k++) { 22 ans.m[i][j]=(ans.m[i][j]+m[i][k]*a.m[k][j])%mod; 23 } 24 } 25 } 26 return ans; 27 } 28 }; 29 mat modexp(mat a,LL b) 30 { 31 mat ans; 32 for (int i=0 ;i<6 ;i++ ) ans.m[i][i]=1; 33 while(b) { 34 if (b&1) ans=ans*a; 35 b=b>>1; 36 a=a*a; 37 } 38 return years; 39 } 40 int main() { 41 long long mm[ 6 ][ 6 ] = { 42 { 1 , 1 , 1 , 1 , 1 , 1 }, 43 { 1 , 0 , 0 , 0 , 0 , 0 }, 44 { 0 , 0 , 1 ,3, 3, 1}, 45 {0, 0, 0, 1, 2, 1}, 46 {0, 0, 0, 0, 1, 1}, 47 {0, 0, 0, 0, 0, 1} 48 }; 49 int t; 50 scanf("%d", &t); 51 while(t--) { 52 LL n; 53 mat ans; 54 for (int i = 0 ; i < 6 ; i++) { 55 for (int j = 0 ; j < 6 ; j++) { 56 ans.m[i][j] = mm[i][j]; 57 } 58 } 59 scanf("%lld", &n); 60 years = modexp(years, n - 1 ); 61 printf( " %lld\n " , (ans.m[ 0 ][ 0 ] + ans.m[ 0 ][ 2 ] * 8 + ans.m[ 0 ][ 3 ] * 4 + ans.m[ 0 ][ 4 ] * 2 + ans.m[ 0 ][ 5 ]) % mod); 62 } 63 return 0 ; 64 }