首页 > 其他 > 详细

ZOJ 3772 Calculate the Function

时间:2014-04-07 01:17:18      阅读:430      评论:0      收藏:0      [点我收藏+]


矩阵+线段树。。。

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


ZOJ 3772 Calculate the Function,布布扣,bubuko.com

ZOJ 3772 Calculate the Function

原文:http://blog.csdn.net/ck_boss/article/details/23053047

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!