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) = ...
随机推荐
- Day6 ,周期末考试试题
Python基础数据类型考试题 考试时间:两个半小时 满分100分(80分以上包含80分及格) 一,基础题. 1,简述变量命名规范(3分) 2,字节和位的关系 ...
- Comparable接口——容器中自定义类排序
1.容器TreeMap,默认根据Key对象中某个属性的从小到大排列元素. (1)如下代码示例,Key是整型数字,所以按照其从小到大的顺序排列 public class TestTreeMap { pu ...
- python 利用from ... import * 的特性实现文件的覆盖
在Python中, 如果使用 from module import * 这样方式进行导包, 就会把module模块里所有的变量导入进来, 并且可以直接使用(其实导包时 module 模块已经被从头到尾 ...
- jzoj4724
DJL为了避免成为一只咸鱼,来找czgj学习Fibonacci数列. 通过czgj的谆谆教导,DJL明白了Fibonacci数列是这样定义的: F(1)=1;F(2)=1;F(n)=F(n-1)+F( ...
- 微信小程序与vueJs的异同
简而言之,所有的框架都是建立在原生javascript基础之上的,所以对于有一定js基础的同学来说,各种框架都是比较容易入手的,但不同的框架之间又有一定的差别,有时候切换使用时就会掉入坑了. 一.微信 ...
- webpack快速入门——实战技巧:webpack优化黑技能
1.抽离jquery,vue(多个第三方类库抽离) 修改入口文件(webpack.config.js中) entry: { entry: './src/entry.js', jquery:'jquer ...
- jmeter制造大批量的用户数据数据
需求:因测试需要,要造100w用户数据,通过用户名.手机号.密码可新增用户,其中用户名和电话号码要求100w用户不能重复 要点: 1.通过Bean shell Sampler实现用户名和手机号的足够随 ...
- Adapter as a WCF Binding - In Depth
WCF LOB Adapter SDK surfaces an adapter as a custom WCF Binding. A WCF Bindingcorresponds to the “H ...
- subscripts(下标)
/* subscripts(下标): 访问对象中数据的快捷方式 所谓下标脚本语法就是能够通过, 实例[索引值]来访问实例中的数据 类似于以前我们访问数字和字典, 其实Swift中的数组和字典就是一个结 ...
- 关于Oracle中的字符的比较
1.Oracle比较字符串是根据ASCII码来的,第一个字母的ASCII大小比较如果相等再比较下一个: 函数来说明: CREATE OR REPLACE FUNCTION MinOrMax(para1 ...