hdu 3306 Another kind of Fibonacci
Another kind of Fibonacci
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3509 Accepted Submission(s): 1401
Problem Description
As we all known , the Fibonacci series : F(0) = 1, F(1) = 1, F(N) = F(N - 1) + F(N - 2) (N >= 2).Now we define another kind of Fibonacci : A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2).And we want to Calculate S(N) , S(N) = A(0)2 +A(1)2+……+A(n)2
Input
There are several test cases.
Each test case will contain three integers , N, X , Y .
N : 2<= N <= 231 – 1
X : 2<= X <= 231– 1
Y : 2<= Y <= 231 – 1
Each test case will contain three integers , N, X , Y .
N : 2<= N <= 231 – 1
X : 2<= X <= 231– 1
Y : 2<= Y <= 231 – 1
Output
For each test case , output the answer of S(n).If the answer is too big , divide it by 10007 and give me the reminder.
Sample Input
2 1 1
3 2 3
Sample Out
6 196
题意:多组数据。每组数据有N,X,Y,A(0)=1 ,A(1)=1 ,A(N)=X*A(N-1)+Y*A(N-2)(N >= 2),求S(N)=A(0)2+A(1)2+…+A(n)2
题解:利用A(N)=X*A(N-1)+Y*A(N-2),S(N)=S(N-1)+A(N)2,A(N)2=X2*A(N-1)2+2XY*A(N-1)A(N-2)+Y2*A(N-2)2构造矩阵。
代码:
#include<bits/stdc++.h>
using namespace std;
const int mod=10007;
struct node
{
long long Martix[4][4];
node operator *(const node&n) const
{
int i,j,k;node sum;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
sum.Martix[i][j]=0;
for(k=0;k<4;k++)
sum.Martix[i][j]=(sum.Martix[i][j]+Martix[i][k]*n.Martix[k][j])%mod;
}
return sum;
}
};
int main()
{
long long n,x,y;
node B,ans;
while(~scanf("%lld%lld%lld",&n,&x,&y))
{
fill(B.Martix[0],B.Martix[0]+16,0);
fill(ans.Martix[0],ans.Martix[0]+16,0);
for(int i=0;i<4;i++) ans.Martix[i][i]=1;
B.Martix[0][0]=B.Martix[1][0]=B.Martix[1][2]=1;
B.Martix[1][1]=(x*x)%mod;
B.Martix[2][1]=(y*y)%mod;
B.Martix[3][1]=(2*x*y)%mod;
B.Martix[1][3]=x%mod;
B.Martix[3][3]=y%mod;
while(n)
{
if(n&1) ans=ans*B;
n>>=1;
B=B*B;
}
printf("%lld\n",(ans.Martix[0][0]+ans.Martix[1][0]+ans.Martix[2][0]+ans.Martix[3][0])%mod);
}
system("pause");
return 0;
}