HITOJ 2255 類似Fibonacci數列求和取模擴充

life4711發表於2014-07-25

http://acm.hit.edu.cn/hoj/problem/view?id=2255

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
題目大意:求出數列第a項到第b項和取模

解題思路:構造一個3*3的矩陣,利用矩陣連乘(注意邊界處理,和取模出現負數的情況)

/*This Code is Submitted by life4711 for Problem 2255 at 2014-07-25 11:59:58*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
typedef long long LL;
const int N=3;
const LL MOD=10000000;
struct Matrix
{
    LL m[N][N];
};
Matrix I=
{
    1,0,0,
    0,1,0,
    0,0,1
};
Matrix multi(Matrix a,Matrix b)
{
    Matrix c;
    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
        {
            c.m[i][j]=0;
            for(int k=0; k<N; k++)
            {
                c.m[i][j]+=a.m[i][k]*b.m[k][j]%MOD;
            }
            c.m[i][j]=c.m[i][j]%MOD;
        }
    return c;
}
Matrix quick_mod(Matrix a,LL k)
{
    Matrix ans=I;
    while(k!=0)
    {
        if(k&1)
        {
            ans=multi(ans,a);
        }
        k>>=1;
        a=multi(a,a);
    }
    return ans;
}
int main()
{
    LL a,b,q,p;
    LL s,e;
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&p,&q,&s,&e);
        Matrix A= {1,p,q,
                   0,p,q,
                   0,1,0
                  };
        if(s==0&&e==0)
        {
            if(a<0)
                a+=MOD;
            printf("%lld\n",a);
        }
        else if(s==0)
        {
            Matrix n=quick_mod(A,e-1);
            LL s2=(n.m[0][0]*(a+b)%MOD+n.m[0][1]*b%MOD+n.m[0][2]*a%MOD)%MOD;
            LL s=s2%MOD;
            if(s<0)
                s+=MOD;
            printf("%lld\n",s);
        }
        else if(s==1)
        {
            Matrix n=quick_mod(A,e-1);
            LL s2=(n.m[0][0]*(a+b)%MOD+n.m[0][1]*b%MOD+n.m[0][2]*a%MOD)%MOD;
            LL s=(s2-a)%MOD;
            if(s<0)
                s+=MOD;
            printf("%lld\n",s);
        }
        else
        {
            Matrix n=quick_mod(A,s-2);
            LL s1=(n.m[0][0]*(a+b)%MOD+n.m[0][1]*b%MOD+n.m[0][2]*a%MOD)%MOD;
            n=quick_mod(A,e-1);
            LL s2=(n.m[0][0]*(a+b)%MOD+n.m[0][1]*b%MOD+n.m[0][2]*a%MOD)%MOD;
            LL s=(s2-s1)%MOD;
            if(s<0)
                s+=MOD;
            printf("%lld\n",s);
        }
    }
    return 0;
}
 


相關文章