点击这里查看原题
问题实际上为求f(n) = f(n−1)+2f∗(n−2)+n^4,其中 f(1)=a,f(2)=b。那么 (n+1)^4 = n^4+4n^3+6n^2+4n+1 所以光 (n+1)^4 这个矩阵就能构造出 5∗5 的一个矩阵来, 然后 f(n) = f(n−1)+2∗f(n−2) 这个是 2∗2 的矩阵,所以构造出来就应该是 7∗7 的转移矩阵 A :
{f[i-1],f[i-2],i^4,i^3,i^2,i,1}*
1 , 1 , 0 , 0 , 0 , 0 , 0
2 , 0 , 0 , 0 , 0 , 0 , 0
1 , 0 , 1 , 0 , 0 , 0 , 0
0 , 0 , 4 , 1 , 0 , 0 , 0
0 , 0 , 6 , 3 , 1 , 0 , 0
0 , 0 , 4 , 3 , 2 , 1 , 0
0 , 0 , 1 , 1 , 1 , 1 , 1
然后使用矩阵快速幂即可。
/*
User:Small
Language:C++
Problem No.:5950
*/
#include<bits/stdc++.h>
#define ll long long
#define inf 999999999
using namespace std;
const ll mod=2147493647;
struct mat{
int n,m;
ll c[10][10];
mat(){
memset(c,0,sizeof(c));
}
}f[105],b;
mat operator*(const mat a,const mat b){
mat c;
c.n=a.n,c.m=b.m;
for(int i=1;i<=c.n;i++)
for(int j=1;j<=c.m;j++)
for(int k=1;k<=a.m;k++)
c.c[i][j]=(c.c[i][j]+a.c[i][k]*b.c[k][j])%mod;
return c;
}
mat pow(mat a,int b){
mat res;
res.n=res.m=a.n;
for(int i=1;i<=res.n;i++) res.c[i][i]=1;
while(b){
if(b&1) res=res*a;
a=a*a;
b>>=1;
}
return res;
}
int main(){
freopen("data.in","r",stdin);//
ios::sync_with_stdio(false);
int t;
cin>>t;
b.n=b.m=7;
b.c[7][7]=b.c[1][1]=b.c[1][2]=b.c[3][1]=1;
b.c[2][1]=2;
for(int j=6;j>=3;j--)
for(int i=7;i>=3;i--)
b.c[i][j]=b.c[i][j+1]+b.c[i+1][j+1];
f[1].n=1,f[1].m=7;
f[1].c[1][7]=1;
for(int i=6;i>=3;i--) f[1].c[1][i]=f[1].c[1][i+1]*3;
while(t--){
ll n,x,y;
cin>>n>>x>>y;
if(n==1) cout<<x<<endl;
else if(n==2) cout<<y<<endl;
else{
f[1].c[1][1]=y;
f[1].c[1][2]=x;
f[2]=f[1]*pow(b,n-2);
cout<<f[2].c[1][1]<<endl;
}
}
return 0;
}