Not Fibonacci(矩阵连乘)
2016-05-16 22:35
260 查看
|
description |
Maybe ACMers of HIT are always fond of fibonacci numbers, because it is so beautiful. Don't you think so? At the same time, fishcanfly always likes to change and this time he thinks about the following series of numbers which you can guess is derived from the definition of fibonacci number. The definition of fibonacci number is: f(0) = 0, f(1) = 1, and for n>=2, f(n) = f(n - 1) + f(n - 2) We define the new series of numbers as below: f(0) = a, f(1) = b, and for n>=2, f(n) = p*f(n - 1) + q*f(n - 2),where p and q are integers. Just like the last time, we are interested in the sum of this series from the s-th element to the e-th element, that is, to calculate S(n)=f(s)+f(s+1)+...+f(e); |
input |
The first line of the input file contains a single integer t (1 <= t <= 30), the number of test cases, followed by the input data for each test case. Each test case contains 6 integers a,b,p,q,s,e as concerned above. We know that -1000 <= a,b <= 1000,-10 <= p,q <= 10 and 0 <= s <= e <= 2147483647. |
output |
One line for each test case, containing a single interger denoting S MOD (10^7) in the range [0,10^7) and the leading zeros should not be printed. |
sample_input |
2 0 1 1 -1 0 3 0 1 1 1 2 3 |
sample_output |
2 3 |
hint |
Hint: You should not use int/long when it comes to an integer bigger than 2147483647. |
AC代码:
#include<iostream> #include<memory.h> #include<cstdlib> #include<cstdio> #include<cmath> #include<cstring> #include<string> #include<cstdlib> #include<iomanip> #include<vector> #include<list> #include<map> #include<algorithm> typedef long long LL; const LL maxn = 1000+10; const LL mod =10000000; using namespace std; struct matrix { LL m[3][3]; }; matrix A; matrix I={ 1,0,0, 0,1,0, 0,0,1 }; matrix multi(matrix a,matrix b) { matrix c; for(int i=0;i<3;i++) for(int j=0;j<3;j++){ c.m[i][j]=0; for(int k=0;k<3;k++){ c.m[i][j]+=a.m[i][k]*b.m[k][j]%mod; } c.m[i][j]%=mod; } return c; } matrix power(matrix A,LL k) { matrix ans=I,p=A; while(k){ if(k&1){ ans=multi(ans,p); k--; } k>>=1; p=multi(p,p); } return ans; } int main() { int t,a,b,p,q,s,e; scanf("%d",&t); while(t--){ scanf("%d%d%d%d%d%d",&a,&b,&p,&q,&s,&e); A.m[0][0]=1;A.m[0][1]=p;A.m[0][2]=q; A.m[1][0]=0;A.m[1][1]=p;A.m[1][2]=q; A.m[2][0]=0;A.m[2][1]=1;A.m[2][2]=0; s--; int s1,s2; if(s<0) s1=0; else if(s==0) s1=a; else{ matrix ans=power(A,s-1); s1=(ans.m[0][0]%mod*(a+b)%mod+ans.m[0][1]%mod*b%mod+ans.m[0][2]%mod*a%mod)%mod; } if(e==0) s2=a; else{ matrix ans=power(A,e-1); s2=(ans.m[0][0]%mod*(a+b)%mod+ans.m[0][1]%mod*b%mod+ans.m[0][2]%mod*a%mod)%mod; } LL l=((s2-s1)%mod+mod)%mod; cout<<l<<endl; } return 0; }
相关文章推荐
- linux学习大量核心技术视频(全免费观看)-干货!
- 三剑客之SED行天下
- vc++6.0 编译执行opencv1.0
- 【数据结构】树
- iScroll.js 使用方法参考
- 利用KVO刷新UITableView和KVO的实现机理
- 简单谈谈ES6的十大特性
- 作业8:单元测试练习(个人练习)
- Spark中组件Mllib的学习4之examples中的MovieLensALS修改本地运行
- Codeforces 672E:Ultimate Weirdness of an Array 线段树
- 51Nod-1284-2 3 5 7的倍数
- mysql大小写
- LeetCode 5. Longest Palindromic Substring
- Hibernate的三种状态
- LCA(最近公共祖先)--tarjan离线算法 hdu 2586
- 第二次画板
- android html5 的弹出窗设置
- [C++Primer] 第二章 变量和基本类型
- 动态内容爬虫
- 如何才能做到网站高并发访问?