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
模版选手的痛(![大哭](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wail.gif)
![大哭](http://static.blog.csdn.net/xheditor/xheditor_emot/default/wail.gif)
)
#include <iostream> #define Mod 10000000 using namespace std; const int MAX=3; typedef struct { long long m[MAX][MAX]; }Matrix; Matrix P={0,0,0, 1,0,0, 0,0,1}; Matrix I={1,0,0, 0,1,0, 0,0,1}; Matrix matrixmul(Matrix a,Matrix b) { Matrix c; for(int i=0;i<MAX;i++) for(int j=0;j<MAX;j++) { c.m[i][j]=0; for(int k=0;k<MAX;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]+=(a.m[i][k]*b.m[k][j])%Mod; } c.m[i][j]=(c.m[i][j]%Mod+Mod)%Mod; } return c; } Matrix quickpow(long long n) { Matrix m=P,b=I; while(n>=1) { if(n&1) b=matrixmul(b,m); n=n>>1; m=matrixmul(m,m); } return b; } int main() { long long a,b,p,q,s,e; long long sum1,sum2; Matrix tmp1,tmp2; int t; cin>>t; while(t--) { cin>>a>>b>>p>>q>>s>>e; P.m[0][0]=p,P.m[0][1]=q; P.m[2][0]=p,P.m[2][1]=q; if(e>=2) { tmp1=quickpow(e-1); sum1=(tmp1.m[2][0]*b)%Mod+(tmp1.m[2][1]*a)%Mod+(tmp1.m[2][2]*(a+b))%Mod; sum1=(sum1%Mod+Mod)%Mod; } if(e==0) { sum1=(a%Mod+Mod)%Mod; } if(e==1) { sum1=((a+b)%Mod+Mod)%Mod; } if(s>=3) { tmp2=quickpow(s-2); sum2=(tmp2.m[2][0]*b)%Mod+(tmp2.m[2][1]*a)%Mod+(tmp2.m[2][2]*(a+b))%Mod; sum2=(sum2%Mod+Mod)%Mod; } if(s==0) sum2=0; if(s==1) sum2=(a%Mod+Mod)%Mod; if(s==2) sum2=((a+b)%Mod+Mod)%Mod; cout<<((sum1-sum2)%Mod+Mod)%Mod<<endl; } return 0; }