Question:
Describes another Fibonacci
F [n] = x * F [n-1] + y * F [N-2];
Calculate segma (F [I] ^ 2 );
Train of Thought Analysis:
For details about the construction matrix, stamp me
The construction matrix can be obtained.
The intermediate matrix is
1 1 00
0 x ^ 2 y ^ 2 2 x * y
0 1 0 0
0x0 y
#include
#include
#include #include
#include
#define N 30typedef long long LL;using namespace std;const LL mod = 10007;struct matrix{ int a[4][4];}origin;matrix multiply(matrix x,matrix y){ matrix temp; memset(temp.a,0,sizeof(temp.a)); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { for(int k=0;k<4;k++) { temp.a[i][j]+=x.a[i][k]*y.a[k][j]; temp.a[i][j]=(temp.a[i][j])%mod; } } } return temp;}matrix matmod(matrix a,LL k){ matrix res; memset(res.a,0,sizeof res.a); for(int i=0;i<4;i++)res.a[i][i]=1; while(k) { if(k&1) res=multiply(res,a); k>>=1; a=multiply(a,a); } return res;}int main(){ int n,x,y; while(scanf("%d%d%d",&n,&x,&y)!=EOF) { matrix st; x%=mod; y%=mod; st.a[0][0]=st.a[0][1]=1; st.a[0][2]=st.a[0][3]=0; st.a[1][0]=0; st.a[1][1]=(x*x)%mod; st.a[1][2]=(y*y)%mod; st.a[1][3]=(2*x*y)%mod; st.a[2][0]=st.a[2][2]=st.a[2][3]=0; st.a[2][1]=1; st.a[3][0]=st.a[3][2]=0; st.a[3][1]=x%mod; st.a[3][3]=y%mod; matrix end = matmod(st,n); int ans=0; for(int i=0;i<4;i++) { ans+=end.a[0][i]; ans%=mod; } printf("%d\n",ans%mod); } return 0;}