Another kind of Fibonacci
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1919 Accepted Submission(s): 738
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
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
题意:很明确了。。。不多说。
思路:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3306
转载请注明出处:寻找&星空の孩子
#include<cstdio> #include<iostream> #include<cstring> using namespace std; #define LL __int64 #define mod 10007 struct matrix { LL mat[4][4]; }; matrix multiply(matrix a,matrix b) { matrix c; memset(c.mat,0,sizeof(c.mat)); for(int i=0;i<4;i++) { for(int j=0;j<4;j++) { if(a.mat[i][j]==0)continue; for(int k=0;k<4;k++) { if(b.mat[j][k]==0)continue; c.mat[i][k]=(c.mat[i][k]+a.mat[i][j]*b.mat[j][k])%mod; } } } return c; } matrix quickmod(matrix a,LL n) { matrix res; for(int i=0;i<4;i++) for(int j=0;j<4;j++) res.mat[i][j]=(i==j); while(n) { if(n&1) res=multiply(res,a); n>>=1; a=multiply(a,a); } return res; } int main() { LL n,x,y; while(scanf("%I64d%I64d%I64d",&n,&x,&y)!=EOF) { if(n==0||n==1) { printf("%I64d\n",n+1); continue; } matrix ans; ans.mat[0][0]=ans.mat[1][2]=1; ans.mat[0][1]=ans.mat[0][2]=ans.mat[0][3]=0; ans.mat[2][2]=ans.mat[2][3]=ans.mat[3][2]=0; ans.mat[1][0]=ans.mat[1][1]=x*x%mod; ans.mat[2][0]=ans.mat[2][1]=y*y%mod; ans.mat[3][0]=ans.mat[3][1]=2*x*y%mod; ans.mat[1][3]=x%mod; ans.mat[3][3]=y%mod; ans=quickmod(ans,n-1); printf("%I64d\n",(2*ans.mat[0][0]+ans.mat[1][0]+ans.mat[2][0]+ans.mat[3][0])%mod); } return 0; }
加油!少年!
时间: 2024-11-08 23:04:52