zoj 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 + 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的更多相关文章
- 线段树 + 矩阵 --- ZOJ 3772 Calculate the Function
Calculate the Function Problem's Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...
- ZOJ 3772 Calculate the Function 线段树+矩阵
Calculate the Function Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %ll ...
- 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)转化为 ...
- 2014 Super Training #7 E Calculate the Function --矩阵+线段树
原题:ZOJ 3772 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 这题算是长见识了,还从没坐过矩阵+线段树的题 ...
- ZOJ3772 - Calculate the Function(线段树+矩阵)
题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...
- 【ZOJ 4070】Function and Function
[链接] 我是链接,点我呀:) [题意] [题解] 递归一会. 会发现最后肯定是0,1一直循环. 开始循环之后就直接返回结果就好. [代码] #include <bits/stdc++.h> ...
- Z0J 3772 Calculate the Function 线段树+矩阵
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂 ...
- 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)) ...
- 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) = ...
随机推荐
- 程序媛计划——mysql索引
定义: 索引是一种单独的.物理的对数据库表中一列或多列的值进行排序的一种存储结构 #为字段创建索引 #在表中的字段中创建索引mysql> create index ind_score on ...
- 在linux下搭建python+django环境
下载python3,进行编译安装,运行django程序 在 /opt目录中安装 cd /opt 1.解决python编译安装所需的软件依赖 yum install gcc patch libffi-d ...
- express form/ajax 后端获取前端数据
-------------------2017/12/02补充:缺了一个重要条件... var bodyParser = require('body-parser');var app = expres ...
- Elasticsearch java API (23)查询 DSL Geo查询
地理查询编辑 Elasticsearch支持两种类型的地理数据: geo_point纬度/经度对字段的支持,和 geo_shape领域,支持点.线.圆.多边形.多等. 这组查询: geo_shape ...
- Elasticsearch Java API简介
加入依赖 我本地的Elasticsearch的版本是2.1.0,因此加入相应的maven依赖 <dependency> <groupId>org.elasticsearch&l ...
- 如何使不同时区的时间与京8区一致?(JS实现)
如何使不同时区的时间与京8区一致?(JS实现) Update:2019/1/28 更简单的是使用这个函数(toDate): // 自定义日期格式如下(年月日都必须提供): // "2011- ...
- Python小白学习之路(十七)—【内置函数二】
序列操作类函数 all() 功能:判断可迭代对象的每个元素是否都为True值注意:If the iterable is empty, return True.(举例3) 回顾:None '' ...
- 【GDKOI2017】 两个胖子萌萌哒 小学奥数题
题目大意:给你一个$n\times m$的网格,你要在这个网格上画三角形. 三角形的顶点只能在网格的整点上,且至少有一条边平行于$x$或$y$轴,且三角形面积为整数.问你能画多少个不同的三角形. 两个 ...
- windows本地调试安装hadoop(idea) : ERROR util.Shell: Failed to locate the winutils binary in the hadoop binary path
1,本地安装hadoop https://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/ 下载hadoop对应版本 (我本意是想下载hadoop ...
- rabbitmq实现一台服务器同时给所有的consumer发送消息(tp框架)(第四篇)
之前的学习了把消息直接publish到queue里面,然后consume掉, 真实的情况,我们会把消息先发送到exchange里面,由它来处理,是发给某一个队列,还是发给某些队列,还是丢弃掉? exc ...