HIT 2255 Not Fibonacci(矩阵乘法)
2015-08-14 19:51
316 查看
题目:http://acm.hit.edu.cn/hoj/problem/view?id=2255
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
Sample Output
Hint: You should not use int/long when it comes to an integer bigger than 2147483647.
陷阱:a,b可能为负数。
Not Fibonacci
My Tags | (Edit) |
---|
Source : 计算机学院第二届“光熙杯”程序设计大赛 | |||
Time limit : 3 sec | Memory limit : 32 M |
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
2 0 1 1 -1 0 3 0 1 1 1 2 3
Sample Output
2 3
Hint: You should not use int/long when it comes to an integer bigger than 2147483647.
陷阱:a,b可能为负数。
#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; }
相关文章推荐
- Adapter的The content of the adapter has changed问题分析
- centos6.7源码安装mysql
- A. Elections
- 小白学开发(iOS)OC_基本数据类型转换成字符串(2015-08-12)
- OC - NSDictionaryAndNSMutableDictionary
- 黑马程序员-Scanner的使用、string类的概述和使用
- Hadoop之SequenceFile
- Performing User-Managed Database-18.4、Restoring Datafiles and Archived Redo Logs
- c++最简单的静态链接库创建
- 并查集详解 (转)
- RHEL6.5上Oracle ACFS与Linux samba一起使用时遇到的bug
- 详解Hibernate中的一级缓存
- ZedBoard的简单测试
- JDK各个版本的新特性jdk1.5-jdk8
- 小白学开发(iOS)OC_ 字符串的比较判断(2015-08-12)
- leetcode 138 —— Copy List with Random Pointer
- 常用Oracle数据表操作命令
- 解决 Linux Mint 17.2 Nvidia驱动下屏幕亮度无法调节问题
- curl获取图片
- 观察者模式