zoj 3772 Calculate the Function

来源:互联网 发布:搜狐微博 知乎 编辑:程序博客网 时间:2024/06/11 12:50
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

14 71 2 3 41 11 21 31 42 43 44 4

Sample Output

125131144
此题主要参考人家大神的思路。。。。
链接: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 longusing 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
原创粉丝点击