自从看了矩阵构造专题,再看这种题,腰也不酸了,腿也不疼了,分分钟推出公式,直接手撕代码A掉;
f(n)=f(n−1)+2∗f(n−2)+n4
f
(
n
)
=
f
(
n
−
1
)
+
2
∗
f
(
n
−
2
)
+
n
4
根据矩阵构造原则,构造一个三阶矩阵,然后发现 n4 n 4 转化到 (n+1)4 ( n + 1 ) 4 缺少四项。添加 n3,n2,n,1 n 3 , n 2 , n , 1 四项,剩下的就是简单的运算,就推出矩阵来了。
⎡⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢f(n)f(n−1)(n+1)4(n+1)3(n+1)2n+11⎤⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥=⎡⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢1100000200000010100000041000006310000432100011111⎤⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎡⎣⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢f(n−1)f(n−2)n4n3n2n1⎤⎦⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥
[
f
(
n
)
f
(
n
−
1
)
(
n
+
1
)
4
(
n
+
1
)
3
(
n
+
1
)
2
n
+
1
1
]
=
[
1
2
1
0
0
0
0
1
0
0
0
0
0
0
0
0
1
4
6
4
1
0
0
0
1
3
3
1
0
0
0
0
1
2
1
0
0
0
0
0
1
1
0
0
0
0
0
0
1
]
[
f
(
n
−
1
)
f
(
n
−
2
)
n
4
n
3
n
2
n
1
]
矩阵构造的巨蠢无比,奈何智商的硬伤,就习惯看这种。。。。
反正自身左乘右乘都一样。
矩阵构造的题刚开始做这个题有些难,先做个简单的,然后学会矩阵构造,做起来就舒服多了。
code:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string>
#include <set>
#include<time.h>
//a&3==a%4
using namespace std ;
#define ll long long
#define mem(a) memset(a,0,sizeof(a))
const double eps = 1e-8;
const int maxn = 110010;
const int inf = 0x3f3f3f3f;
const ll mod=2147493647ll;
struct matrix
{
ll mat[10][10];//mat[i][j]=第i行第j列
matrix()
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
mat[i][j]=0ll;
}
}
}
};
void out(matrix a,int n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<a.mat[i][j]<<" ";
}
cout<<endl;
}
}
matrix multiply(matrix a,matrix b,int n)
{
matrix res;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
res.mat[i][j]+=a.mat[i][k]*b.mat[k][j]%mod;
res.mat[i][j]%=mod;
}
}
}
return res;
}
matrix quickmi(matrix a,ll b,int n)
{
matrix E;
matrix res;
for(int i=0;i<n;i++)
{
E.mat[i][i]=1;
res.mat[i][i]=1;
}
E=multiply(a,E,n);
while(b>0)
{
if(b%2==1)
{
res=multiply(res,E,n);
}
b=b/2;
E=multiply(E,E,n);
}
return res;
}
int main()
{
int kase;
scanf("%d",&kase);
matrix sum;
sum.mat[0][0]=1ll;sum.mat[0][1]=2ll;sum.mat[0][2]=1ll;
sum.mat[1][0]=1ll;
sum.mat[2][2]=1ll;sum.mat[2][3]=4ll;sum.mat[2][4]=6ll;sum.mat[2][5]=4ll;sum.mat[2][6]=1ll;
sum.mat[3][3]=1ll;sum.mat[3][4]=3ll;sum.mat[3][5]=3ll;sum.mat[3][6]=1ll;
sum.mat[4][4]=1ll;sum.mat[4][5]=2ll;sum.mat[4][6]=1ll;
sum.mat[5][5]=1ll;sum.mat[5][6]=1ll;
sum.mat[6][6]=1ll;
while(kase--)
{
ll a,b,c;
cin>>a>>b>>c;
if(a==1)
{
cout<<b%mod<<endl;
}
else if(a==2)
{
cout<<c%mod<<endl;
}
else
{
matrix res=quickmi(sum,a-2,7);
cout<<((res.mat[0][0]*c)%mod+(res.mat[0][1]*b)%mod+(res.mat[0][2]*81)%mod+(res.mat[0][3]*27)%mod+(res.mat[0][4]*9)%mod+(res.mat[0][5]*3)%mod+res.mat[0][6])%mod<<endl;;
}
}
return 0;
}