Another kind of Fibonacci

                                                       Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

                                                                                  Total Submission(s): 1691    Accepted Submission(s): 660

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
 

主要是递推矩阵,然后矩阵高速幂:
依据S(n)=S(n-1)+A(n)^2,A(n)^2=X^2*A(n-1)^2+Y^2*A(n-2)^2+2*X*Y*A(n-1)*A(n-2)。A(n)*A(n-1)=Y*A(n-1)*A(n-2)+X*A(n-1)^2,能够构造例如以下的矩阵。然后用二分矩阵的方法求解就可以。

递推矩阵


代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod=10007;
struct matrix
{
long long ma[5][5];
};
matrix multi(matrix x,matrix y)//矩阵相乘
{
matrix ans;
memset(ans.ma,0,sizeof(ans.ma));
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(x.ma[i][j])//稀疏矩阵优化
for(int k=1;k<=4;k++)
{
ans.ma[i][k]=(ans.ma[i][k]+(x.ma[i][j]*y.ma[j][k])%mod)%mod;
}
}
}
return ans;
}
matrix pow(matrix a,long long m)
{
matrix ans;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(i==j)
ans.ma[i][j]=1;
else
ans.ma[i][j]=0;
}
}
while(m)
{
if(m&1)
ans=multi(ans,a);
a=multi(a,a);
m=m>>1;
}
return ans;
}
int main()
{
long long x,y,n;
while(~scanf("%I64d%I64d%I64d",&n,&x,&y))
{
matrix a,b;
memset(a.ma,0,sizeof(a.ma));
memset(b.ma,0,sizeof(b.ma));
a.ma[1][1]=1;
a.ma[1][2]=1;
a.ma[2][2]=(x*x)%mod;
a.ma[2][3]=(y*y)%mod;
a.ma[2][4]=(2*x*y)%mod;
a.ma[3][2]=1;
a.ma[4][2]=x;
a.ma[4][4]=y;
b.ma[1][1]=1;
b.ma[2][1]=1;
b.ma[3][1]=1;
b.ma[4][1]=1;
a=pow(a,n);
a=multi(a,b);
printf("%I64d\n",a.ma[1][1]);
}
return 0;
}

最新文章

  1. pywin32 创建一个窗口
  2. java_js从字符串中截取数字
  3. log4net记录日志到数据库自定义字段
  4. Python——内置类型
  5. IIS 7.0 下 httpMoudle 失效的问题
  6. POJ 1042 Gone Fishing
  7. IOS开发中针对UIImageView的几种常用手势
  8. 会话技术之Cookie 和 Session
  9. [cocos2d demo]新科娘收集水表
  10. (转:亲测)cnblogs博文浏览[推荐、Top、评论、关注、收藏]利器代码片段
  11. AndroidPullToRefresh拉动效果配置
  12. 201521123023《Java程序设计》第10周学习总结
  13. Node.js显示页面
  14. 关于VR开发中的穿墙问题随想
  15. javascript之内置函数
  16. vue enter事件无效,加入native
  17. 20170506计划-----(基于python查询oracle语句)
  18. HTTP中的重定向和请求转发的区别(转)
  19. js和jquery获取textarea输入的内容
  20. Java swing皮肤(look and feel)大全

热门文章

  1. JOISC 2017 Day1 T3 烟花棒
  2. 【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】
  3. css实现一个缺口小三角
  4. Java 大数
  5. ZOJ QS Network
  6. Maven学习笔记4
  7. 数组-reduce方法
  8. 【hdu 1083】Courses
  9. Bag of Features (BOF)图像检索算法
  10. SJTU 3001. 二哥的幸运