ZOJ 3772 Calculate the Function


矩阵+线段树。。。

Fx            1  Ar                            1   A l+2          F l

Fx-1   =    1   0     *     ....    *      1    0            *   F l+1


注意矩阵乘的顺序。。。。


Calculate the Function

Time Limit: 2 Seconds       Memory Limit: 65536 KB

You are given a list of numbers A1 A2 .. AN and M queries. For the i-th query:

  • The query has two parameters Li and Ri.
  • The query will define a function Fi(x) on the domain [Li, Ri] ∈ Z.
  • Fi(Li) = ALi
  • Fi(Li + 1) = A(Li + 1)
  • for all x >= Li + 2Fi(x) = Fi(x - 1) + Fi(x - 2) × Ax

You task is to calculate Fi(Ri) for each query. Because the answer can be very large, you should output the remainder of the answer divided by 1000000007.

Input

There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:

The first line contains two integers NM (1 <= NM <= 100000). The second line contains N integers A1 A2 .. AN (1 <= Ai <= 1000000000).

The next M lines, each line is a query with two integer parameters LiRi (1 <= Li <= Ri <= N).

Output

For each test case, output the remainder of the answer divided by 1000000007.

Sample Input
1
4 7
1 2 3 4
1 1
1 2
1 3
1 4
2 4
3 4
4 4
Sample Output
1
2
5
13
11
4
4

Author:  CHEN, Weijie
Source:  The 14th Zhejiang University Programming Contest



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long int LL;

const int maxn=200000;
const LL MOD=1000000007;

struct MARTRIX
{
    LL m[2][2];
    MARTRIX operator*(const MARTRIX& b) const
    {
        MARTRIX ret;
        ret.m[0][0]=((m[0][0]*b.m[0][0])%MOD+(m[0][1]*b.m[1][0])%MOD)%MOD;
        ret.m[0][1]=((m[0][0]*b.m[0][1])%MOD+(m[0][1]*b.m[1][1])%MOD)%MOD;
        ret.m[1][0]=((m[1][0]*b.m[0][0])%MOD+(m[1][1]*b.m[1][0])%MOD)%MOD;
        ret.m[1][1]=((m[1][0]*b.m[0][1])%MOD+(m[1][1]*b.m[1][1])%MOD)%MOD;
        return ret;
    }
    MARTRIX(LL a)
    {
        m[0][0]=1; m[0][1]=a;
        m[1][0]=1; m[1][1]=0;
    }
    MARTRIX()
    {
        memset(m,0,sizeof(m));
    }
}mrt[maxn<<2];

int n,m;
LL a[maxn];

void build(int l,int r,int rt)
{
    if(l==r)
    {
        mrt[rt]=MARTRIX(a[l]);
        return ;
    }

    int m=(l+r)/2;
    build(lson); build(rson);
    mrt[rt]=mrt[rt<<1|1]*mrt[rt<<1];
}

MARTRIX query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return mrt[rt];
    }
    int m=(l+r)/2;
    if(R<=m)
        return query(L,R,lson);
    if(L>m)
        return query(L,R,rson);
    return query(L,R,rson)*query(L,R,lson);
}

int main()
{
    int T_T;
    scanf("%d",&T_T);
    while(T_T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) scanf("%lld",a+i);
        build(1,n,1);
        while(m--)
        {
            int L,R;
            scanf("%d%d",&L,&R);
            if(R<L+2)
            {
                printf("%lld\n",a[R]%MOD);
                continue;
            }
            MARTRIX mt=query(L+2,R,1,n,1);
            printf("%lld\n",((a[L+1]*mt.m[0][0])%MOD+(a[L]*mt.m[0][1])%MOD)%MOD);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值