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) = ...
随机推荐
- Android 美学设计基础 <3>
本期接着对Android的美学设计的分享. 1.3 Light and shadows 光学与阴影 1.3.1 Light 在素材设计的环境中,我们会用虚拟的光来照亮UI界面.主灯光会产生尖锐,有方向 ...
- Linux命令学习与使用2
1.Ctrl+a:跳到命令行首 Ctrl+E: 跳到命令行尾 Ctrl+L:清屏2.切换用户 su - 用户名3.更换yum镜像源 1.进入/etc/yum.repos.d 备份CentOS-Base ...
- checkbox选中事件
在前端中,往往需要根据后台数据的返回选中多选框.可以根据后台返回的数据转化为数组,然后又val([数组])进行选中. 例子: html代码: <!DOCTYPE html> <htm ...
- 【已解决】wepy中使用分包加载报错
问题: "xxx.js 出现脚本错误后者未正确调用Page()" 最近看小程序启动时间(性能监控),启动时间比较长,所以考虑使用分包加载. 但在使用过程中遇 ...
- D11——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D11 20180908内容纲要: 1.RabbitMQ消息队列 (1)RabbitMQ安装 (2)Rabbits示例 模式一:fanout 模式二:direct ...
- Java基础梳理(一)
List和Set比较,各自的子类比较 对比一:Arraylist与LinkedList的比较 1.ArrayList是实现了基于动态数组的数据结构,因为地址连续,一旦数据存储好了,查询操作效率会比较高 ...
- An assembly specified in the application dependencies manifest
.Net Core 运行的时候报错 An assembly specified in the application dependencies manifest (xxx.deps.json) was ...
- php生成markdown格式的数据库字典
<?php /** * 生成mysql数据字典 */ //数据库配置 $config = [ 'host' => '192.168.43.134', 'user' => 'root' ...
- [转]ASP.NET MVC 4 最佳实践宝典
原文:http://www.cnblogs.com/sonykings/archive/2013/05/30/3107531.html ASP.NET MVC最佳实践 本文档提供了一套旨在帮助创建最佳 ...
- MyEclipse *的下载
找到MyEclipse的各种历史版本下载页面 : MyEclipse官方中文网 欢迎大家,加入我的微信公众号:大数据躺过的坑 人工智能躺过的坑 同时,大家可以关注我的个人博客 ...