hdu 3306
Another kind of Fibonacci
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2563 Accepted Submission(s): 1022
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 <= 2 31 – 1
X : 2<= X <= 2 31– 1
Y : 2<= Y <= 2 31 – 1
Each test case will contain three integers , N, X , Y .
N : 2<= N <= 2 31 – 1
X : 2<= X <= 2 31– 1
Y : 2<= Y <= 2 31 – 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 Output
6 196
Author
wyb
a(n)^2=x*.....即可
要注意X,y一进来就取余
因为n>=2倒不用考虑特殊情况了
#include <iostream>
#include<string.h>using namespace std;
const int maxn=4;
struct matrix
{
long long m[maxn][maxn];
};
const int mod=10007;
long long X,Y,N;
/*matrix P={1,(X*X)%mod,(Y*Y)%mod,(2*X*Y)%mod,
0,(X*X)%mod,(Y*Y)%mod,(2*X*Y)%mod,
0,1,0,0,
0,X%mod,0,Y%mod
};
matrix I={1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1};*/
matrix P,I;
matrix matrixmul(matrix a,matrix b);
matrix quickpow(long long n)
{
matrix m=P,b=I;
while(n>=1)
{
if(n&1)
b=matrixmul(b,m);
n=n>>1;
m=matrixmul(m,m);
}
return b;
}
matrix matrixmul(matrix a,matrix b)
{
int i,j,k;
matrix c;
for(i=0;i<maxn;i++)
{
for(j=0;j<maxn;j++)
{
c.m[i][j]=0;
for(k=0;k<maxn;k++)
c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
c.m[i][j]%