您的位置:首页 > 其它

HIT 2255 Not Fibonacci(矩阵乘法)

2015-08-14 19:51 316 查看
题目:http://acm.hit.edu.cn/hoj/problem/view?id=2255


Not Fibonacci

My Tags (Edit)
Source : 计算机学院第二届“光熙杯”程序设计大赛
Time limit : 3 secMemory limit : 32 M
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
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: