zoj 3772 Calculate the Function

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
 
此题主要参考人家大神的思路。。。。
链接:http://blog.csdn.net/u013654696/article/details/23037407#comments
 
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define inf 0x0f0f0f0f
#define LL long long
using namespace std;
struct mat {
    LL a[2][2];
    mat() {
        memset( a, 0, sizeof( a ) );
    }
    mat( LL x ) {
        a[0][0] = a[1][0] = 1;
        a[1][1] = 0;
        a[0][1] = x;
    }
    mat operator * ( const mat &b ) const {
        mat ret;
        for( int i = 0; i < 2; ++i )
            for( int j = 0; j < 2; ++j )
                for( int k = 0; k < 2; ++k )
                    ret.a[i][j] = ( ret.a[i][j] + a[i][k] * b.a[k][j] ) % 1000000007;
        return ret;
    }
};
LL ll[100005<<2], rr[100005<<2];
LL num[100005<<2];
mat sum[100005<<2];
void build(LL cur,LL l,LL r)
{
    ll[cur]=l;
    rr[cur]=r;
    if(l==r)
    {
        sum[cur]=mat(num[l]);
        return;
    }
    build(2*cur,l,(l+r)/2);
    build(2*cur+1,(l+r)/2+1,r);
    sum[cur]=sum[2*cur+1]*sum[2*cur];
}
mat query(LL cur,LL l,LL r)
{
    if(l<=ll[cur]&&rr[cur]<=r)
        return sum[cur];
    else
    {
        mat ans;
        ans.a[0][0]=ans.a[1][1]=1;
        ans.a[0][1]=ans.a[1][0]=0;
        if(r>(ll[cur]+rr[cur])/2)
            ans=ans*query(2*cur+1,l,r);
        if(l<=(ll[cur]+rr[cur])/2)
            ans=ans*query(2*cur,l,r);
        return ans;
    }
}
int main()
{
    LL t,i,j;
    LL n,m,a,b;
    mat ans;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld%lld",&n,&m);
        for(i=1;i<=n;i++)
            scanf("%lld",&num[i]);
        build(1,1,n);
        while(m--)
        {
            scanf("%lld%lld",&a,&b);
            if(a==b)
            {
                printf("%lld\n",num[a]);
                continue;
            }
            if(b==a+1)
            {
                printf("%lld\n",num[b]);
                continue;
            }
            ans=query(1,a+2,b);
            //printf("***%lld %lld\n%lld %lld\n",ans.a[0][0],ans.a[0][1],ans.a[1][0],ans.a[1][1]);
            printf("%lld\n",(ans.a[0][0]*num[a+1]+ans.a[0][1]*num[a])%1000000007);
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值