HIT 2255 Not Fibonacci(矩阵乘法)
来源:互联网 发布:linux怎么禁ping 编辑:程序博客网 时间:2024/06/14 05:58
题目:http://acm.hit.edu.cn/hoj/problem/view?id=2255
Not Fibonacci
Submitted : 543, Accepted : 150
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 .""""
Great!Let's go!
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
20 1 1 -1 0 30 1 1 1 2 3
Sample Output
23Hint: You should not use int/long when it comes to an integer bigger than 2147483647.
#include <iostream>#include <cstdio>using namespace std;const int mod=1e7;typedef long long LL;struct matrie{ LL m[3][3];};matrie I={ 1,0,0, 0,1,0, 0,0,1};matrie multi(matrie a,matrie b){ matrie c; for(LL i=0;i<3;i++){ for(LL j=0;j<3;j++){ c.m[i][j]=0; for(LL k=0;k<3;k++){ a.m[i][k]=(a.m[i][k]%mod+mod)%mod; b.m[k][j]=(b.m[k][j]%mod+mod)%mod; c.m[i][j]=c.m[i][j]+a.m[i][k]*b.m[k][j]%mod; } c.m[i][j]%=mod; } } return c;}matrie power(matrie a,LL k){ matrie ans=I,tmp=a; while(k){ if(k&1)ans=multi(ans,tmp); tmp=multi(tmp,tmp); k>>=1; } return ans;}int main(){ //freopen("cin.txt","r",stdin); LL n; LL a,b,p,q,s,e; cin>>n; while(n--){ cin>>a>>b>>p>>q>>s>>e; LL q1,q2; matrie A={ p,q,0, 1,0,0, p,q,1 }; if(s==0) q1=0; else if(s==1) q1=(a+mod)%mod; else if(s==2) q1=(a%mod+b%mod+mod)%mod; else if(s>2){ matrie qs1=power(A,s-2); q1=(qs1.m[2][0]*b%mod+qs1.m[2][1]*a%mod+qs1.m[2][2]*(a+b)%mod)%mod; } if(e==0) q2=(a%mod+mod)%mod; else if(e==1) q2=(a%mod+b%mod+mod)%mod; else if(e>1){ matrie qs2=power(A,e-1); q2=(qs2.m[2][0]*b%mod+qs2.m[2][1]*a%mod+qs2.m[2][2]*(a+b)%mod)%mod; } //cout<<q1<<" "<<q2<<endl; LL ans=(q2-q1+mod)%mod; cout<<ans<<endl; } return 0;}
- HIT 2255 Not Fibonacci(矩阵乘法)
- Hit 2255 Not Fibonacci
- HIT 2060 Fibonacci Problem Again(矩阵乘法)
- HIT 2255 Not Fibonacci(矩阵快速幂)
- HIT OJ 2255 Not Fibonacci (矩阵快速幂)
- Fibonacci 矩阵乘法
- 3070 Fibonacci 矩阵乘法
- 【poj3070】 Fibonacci 【矩阵乘法】
- hdu 3117 Fibonacci Numbers(矩阵乘法+fibonacci)
- POJ 3070 Fibonacci 矩阵乘法
- 1250 Fibonacci数列(矩阵乘法)
- POJ 1070 Fibonacci 矩阵乘法
- [hoj 2255]Not Fibonacci[矩阵快速幂]
- HIT OJ 2060 Fibonacci Problem Again(矩阵快速幂)
- poj 3070 Fibonacci (快速矩阵乘法)
- POJ 3070 Fibonacci 矩阵乘法 整数分解
- hdu 1588 Gauss Fibonacci(矩阵乘法,二分)
- nyoj fibonacci数列(二) 矩阵乘法
- iOS SDK详解之NSCoding协议
- 单例模式
- Nginx内存管理
- 很容易学习的JQuery库 : (二) 选择器
- 机会—晋升—改变未来
- HIT 2255 Not Fibonacci(矩阵乘法)
- java学习3
- c++最简单的静态链接库创建
- Android ListView/gridView代码优化ViewHolder,converView使用
- RedHat Enterprise Linux5关于中文显示乱码问题解决
- HD1285 确定比赛名次 【拓扑排序】
- Hadoop之SequenceFile
- 黑马程序员-多态的讲解
- hdu 3986 Harry Potter and the Final Battle spfa变形