Not Fibonacci(矩阵连乘)

Not Fibonacci


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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值