首页 > 其他 > 详细

zoj Calculate the Function

时间:2014-04-08 22:25:14      阅读:556      评论:0      收藏:0      [点我收藏+]
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 + 2, Fi(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 N, M (1 <= N, M <= 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 Li, Ri (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

 

bubuko.com,布布扣
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using namespace std;
 6 
 7 typedef long long LL;
 8 
 9 int mod=1000000007;
10 int ax[100002];
11 struct node
12 {
13     LL a,b,c,d;
14     int l,r;
15 } f[400008];
16 
17 void build(int l,int r,int n)
18 {
19     int mid=(l+r)/2;
20     f[n].l=l;
21     f[n].r=r;
22     if(l==r)
23     {
24         f[n].a=0;
25         f[n].b=ax[l];
26         f[n].c=1;
27         f[n].d=1;
28         return;
29     }
30     build(l,mid,n*2);
31     build(mid+1,r,n*2+1);
32     f[n].a=((f[n<<1].a*f[(n<<1)+1].a)%mod+f[n<<1].b*f[(n<<1)+1].c)%mod;
33     f[n].b=((f[n<<1].a*f[(n<<1)+1].b)%mod+f[n<<1].b*f[(n<<1)+1].d)%mod;
34     f[n].c=((f[n<<1].c*f[(n<<1)+1].a)%mod+f[n<<1].d*f[(n<<1)+1].c)%mod;
35     f[n].d=((f[n<<1].c*f[(n<<1)+1].b)%mod+f[n<<1].d*f[(n<<1)+1].d)%mod;
36 }
37 node serch1(int l,int r,int n)
38 {
39     int mid=(f[n].l+f[n].r)/2;
40     node n1,n2,n3;
41 
42     if(f[n].l==l && f[n].r==r)
43     {
44         return f[n];
45     }
46     if(mid>=r)
47         return serch1(l,r,n*2);
48     else if(mid<l)
49         return serch1(l,r,n*2+1);
50     else
51     {
52         n1=serch1(l,mid,n*2);
53         n2=serch1(mid+1,r,n*2+1);
54         n3.a=((n1.a*n2.a)%mod+(n1.b*n2.c)%mod)%mod;
55         n3.b=((n1.a*n2.b)%mod+(n1.b*n2.d)%mod)%mod;
56         n3.c=((n1.c*n2.a)%mod+(n1.d*n2.c)%mod)%mod;
57         n3.d=((n1.c*n2.b)%mod+(n1.d*n2.d)%mod)%mod;
58     }
59     return n3;
60 }
61 int main()
62 {
63     int T;
64     int i,j,n,m,x,y;
65     LL sum1;
66     node cur;
67     scanf("%d",&T);
68     while(T--)
69     {
70         scanf("%d%d",&n,&m);
71         for(i=1; i<=n; i++)
72             scanf("%d",&ax[i]);
73         build(1,n,1);
74         for(j=1; j<=m; j++)
75         {
76             scanf("%d%d",&x,&y);
77             if(y-x<2)
78             {
79                 printf("%d\n",ax[y]);
80             }
81             else
82             {
83                 cur=serch1(x+2,y,1);
84                 sum1=((cur.b*ax[x])%mod+(cur.d*ax[x+1])%mod)%mod;
85                 printf("%lld\n",sum1);
86             }
87         }
88     }
89     return 0;
90 }
bubuko.com,布布扣

 

zoj Calculate the Function,布布扣,bubuko.com

zoj Calculate the Function

原文:http://www.cnblogs.com/tom987690183/p/3651216.html

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