hud 6030 Happy Necklace (矩阵快速幂)
作者:互联网
Happy NecklaceTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2402 Accepted Submission(s): 984 Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single strings composed of multiple red and blue beads.
Input The first line of the input contains an integer T(1≤T≤10000) , denoting the number of test cases.
Output For each test case, print a single line containing a single integer, denoting the answer modulo 10 9 +7 .
Sample Input 2 2 3
Sample Output 3 4
Source
|
a(n) = a(n-1) + a(n-3)
手算前几项找规律
#include<bits/stdc++.h>
using namespace std;
using lom = long long;
const lom mod = 1e9 +7;
void matrix(lom A[3][3], lom B[3][3], lom C[3][3])
{
lom temp[3][3];
memset(temp,0,sizeof(temp));
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
for( int k = 0; k < 3; k++)
// for( int t = 0; t < 3; t++)
temp[i][j] = ( temp[i][j] + A[i][k]*B[k][j]%mod ) % mod;
// cout<<endl;
}
memcpy(C,temp,sizeof(temp));//copy
}
lom slove( lom n )
{
if(n==0) return 3;
if(n==1) return 4;
lom ans[3][3] = {6,0,0,4,0,0,3,0,0};
lom A[3][3] = {1,0,1,1,0,0,0,1,0};
while( n > 0 )
{
if( n & 1 ) matrix( A, ans, ans );
n >>= 1;
matrix( A, A, A );
}
return ans[2][0];
}
int main()
{
lom n,t;
cin >> t;
while(t--)
{
cin >> n;
cout << slove( n-2 ) <<endl;
}
return 0;
}
标签:hud,beads,temp,int,number,lom,necklace,Necklace,6030 来源: https://blog.csdn.net/qq_41431457/article/details/98507552