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

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; typedef long long LL; int mod=;
int ax[];
struct node
{
LL a,b,c,d;
int l,r;
} f[]; void build(int l,int r,int n)
{
int mid=(l+r)/;
f[n].l=l;
f[n].r=r;
if(l==r)
{
f[n].a=;
f[n].b=ax[l];
f[n].c=;
f[n].d=;
return;
}
build(l,mid,n*);
build(mid+,r,n*+);
f[n].a=((f[n<<].a*f[(n<<)+].a)%mod+f[n<<].b*f[(n<<)+].c)%mod;
f[n].b=((f[n<<].a*f[(n<<)+].b)%mod+f[n<<].b*f[(n<<)+].d)%mod;
f[n].c=((f[n<<].c*f[(n<<)+].a)%mod+f[n<<].d*f[(n<<)+].c)%mod;
f[n].d=((f[n<<].c*f[(n<<)+].b)%mod+f[n<<].d*f[(n<<)+].d)%mod;
}
node serch1(int l,int r,int n)
{
int mid=(f[n].l+f[n].r)/;
node n1,n2,n3; if(f[n].l==l && f[n].r==r)
{
return f[n];
}
if(mid>=r)
return serch1(l,r,n*);
else if(mid<l)
return serch1(l,r,n*+);
else
{
n1=serch1(l,mid,n*);
n2=serch1(mid+,r,n*+);
n3.a=((n1.a*n2.a)%mod+(n1.b*n2.c)%mod)%mod;
n3.b=((n1.a*n2.b)%mod+(n1.b*n2.d)%mod)%mod;
n3.c=((n1.c*n2.a)%mod+(n1.d*n2.c)%mod)%mod;
n3.d=((n1.c*n2.b)%mod+(n1.d*n2.d)%mod)%mod;
}
return n3;
}
int main()
{
int T;
int i,j,n,m,x,y;
LL sum1;
node cur;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=; i<=n; i++)
scanf("%d",&ax[i]);
build(,n,);
for(j=; j<=m; j++)
{
scanf("%d%d",&x,&y);
if(y-x<)
{
printf("%d\n",ax[y]);
}
else
{
cur=serch1(x+,y,);
sum1=((cur.b*ax[x])%mod+(cur.d*ax[x+])%mod)%mod;
printf("%lld\n",sum1);
}
}
}
return ;
}

zoj Calculate the Function的更多相关文章

  1. 线段树 + 矩阵 --- ZOJ 3772 Calculate the Function

    Calculate the Function Problem's Link:   http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...

  2. ZOJ 3772 Calculate the Function 线段树+矩阵

    Calculate the Function Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %ll ...

  3. zoj 3772 Calculate the Function

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这道题需要构造矩阵:F(X)=F(X-1)+F(X-2)*A(X)转化为 ...

  4. 2014 Super Training #7 E Calculate the Function --矩阵+线段树

    原题:ZOJ 3772 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 这题算是长见识了,还从没坐过矩阵+线段树的题 ...

  5. ZOJ3772 - Calculate the Function(线段树+矩阵)

    题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...

  6. 【ZOJ 4070】Function and Function

    [链接] 我是链接,点我呀:) [题意] [题解] 递归一会. 会发现最后肯定是0,1一直循环. 开始循环之后就直接返回结果就好. [代码] #include <bits/stdc++.h> ...

  7. Z0J 3772 Calculate the Function 线段树+矩阵

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂 ...

  8. Codeforces 837E. Vasya's Function

    http://codeforces.com/problemset/problem/837/E   题意: f(a, 0) = 0; f(a, b) = 1 + f(a, b - gcd(a, b)) ...

  9. Codeforces 837E Vasya's Function - 数论

    Vasya is studying number theory. He has denoted a function f(a, b) such that: f(a, 0) = 0; f(a, b) = ...

随机推荐

  1. 守护进程与Supervisor

    博客链接:http://www.cnblogs.com/zhenghongxin/p/8676565.html 消息队列处理后台任务带来的问题 在系统稍微大些的时候,我们经常会用到消息队列(实现的方式 ...

  2. 801. Minimum Swaps To Make Sequences Increasing

    We have two integer sequences A and B of the same non-zero length. We are allowed to swap elements A ...

  3. 程序猿的日常——Java基础之clone、序列化、字符串、数组

    其实Java还有很多其他的基础知识,在日常工作技术撕逼中也是经常被讨论的问题. 深克隆与浅克隆 在Java中创建对象有两种方式: 一种是new操作符,它创建了一个新的对象,并把对应的各个字段初始化成默 ...

  4. AndroidManifest.xml文件安全探索

    本文作者:i春秋签约作家——icq8756c1a2 最近在做一些apk的安全检测,对AndroidManifest.xml文件进行了研究和探讨,介绍AndroidManifest.xml文件的作用和架 ...

  5. [JavaScript] css将footer置于页面最底部

    <!-- 父层 --> <div id="wapper"> <!-- 主要内容 --> <div id="main-conten ...

  6. [JavaScript] 时间戳格式化为yyyy-MM-dd日期

    function formateDate(timestamp){ var date = new Date(timestamp); var y = 1900+date.getYear(); var m ...

  7. WIKI常用的表格设计模板

    域名服务器管理表格     数据库管理表格     软件路径说明表格   开发测试环境虚拟机表格        

  8. Spring+Mybatis整合时 Failed to read candidate component class,Caused by:IllegalArgumentException

    Spring+Mybatis整合时Caused by: java.lang.IllegalArgumentException错误 org.springframework.beans.factory.B ...

  9. IKAnalyzer 独立使用 配置扩展词典

    有三点要注意(要不然扩展词典始终不生效): 后缀名.dic的词典文件,必须如使用文档里所说的 无BOM的UTF-8编码保存的文件.如果不确定什么是  无BOM的UTF-8编码,最简单的方式就是 用No ...

  10. 【链表】Rotate List(三个指针)

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...