矩阵连乘(3)
2015-08-17 00:25
381 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2 1 1
3 2 3
[align=left]Sample Output[/align]
6
196
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.
[align=left]Input[/align]
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
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2 1 1
3 2 3
[align=left]Sample Output[/align]
6
196
#include <iostream> #include <cstdio> #include <cmath> using namespace std; const long long mod=10007; typedef struct { long long m[4][4]; }mat; mat I={1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1}; mat calc(mat a,mat b) { int i,j,k; mat c; for(i=0;i<4;i++) for(j=0;j<4;j++) { c.m[i][j]=0; for(k=0;k<4;k++) { c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod; } c.m[i][j]=c.m[i][j]%mod; } return c; } mat matirx(mat P,long long n) { mat m=P,b=I; while(n>=1) { if(n&1) b=calc(b,m); n>>=1; m=calc(m,m); } return b; } int main() { long long n,x,y; while(scanf("%lld%lld%lld",&n,&x,&y)!=EOF) { long long sum=0; x=x%mod; y=y%mod; //2*x*y可能会溢出 mat P={x*x,2*x*y,y*y,0,x,y,0,0,1,0,0,0,1,0,0,1}; mat a; a=matirx(P,n); sum=(a.m[3][0]+a.m[3][1]+a.m[3][2]+a.m[3][3])%mod; printf("%lld\n",sum); } return 0; }
相关文章推荐
- PHP 扩展开发[从零开始编写第一个PHP扩展]
- HDU - 2121 Ice_cream’s world II(朱刘算法+虚根)
- [LeetCode][JavaScript]Binary Tree Paths
- HDU 3530 RMQ+twopointer/单调队列
- 逆序对问题
- 老电脑如果从windows7升级到windows10不断重启进不了系统,还是想用windows10,怎么办?
- poj3984广搜c语言
- LeetCode Divide Two Integers
- LeetCode Divide Two Integers
- 文件对象-读操作
- Xcode7 采用空白模版运行错误
- cordova配置文件config.xm
- Linux 下 eclipse 创建快捷方式
- ORA-00257:archiver error问题处理方法
- Python的替换函数——strip(),replace()和re.sub()
- codeforces 566B Replicating Processes
- jQuery AJAX
- ORA-00257:archiver error问题处理方法
- linux添加静态路由
- Linux常用命令002之搜索命令locate、whereis、which、find、grep