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.
#include <iostream>
//#define Mod 10000000
using namespace std;
const int MAX = 3;
long Mod=10000000;
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)
{
int i,j,k;
Matrix c;
for (i = 0 ; i < MAX; i++)
for (j = 0; j < MAX;j++)
{
c.m[i][j] = 0;
for (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 sum,sum1;
int t;
Matrix tmp,tmp1;
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)
{tmp=quickpow(e-1);
sum=(tmp.m[2][0]*b)%Mod+(tmp.m[2][1]*a)%Mod+(tmp.m[2][2]*(a+b))%Mod;
sum=(sum%Mod+Mod)%Mod;
}
if (e==1)
sum=((a+b)%Mod+Mod)%Mod;
if (e==0) sum=(a%Mod+Mod)%Mod;
if (s>=3)
{
tmp1= quickpow(s-2);
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 (s==0) sum1=0;
if (s==1) sum1=a;
if (s==2) sum1=((a+b)%Mod+Mod)%Mod;
long long sum_2=((sum-sum1)+Mod)%Mod;
cout<<sum_2<<endl;
}
return 0;
}