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) = ...
随机推荐
- 670. Maximum Swap
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- 移动端 实现ul横向滚动条
ul { display: flex; width: 100%; height: 3.333333rem; background: #fff; padding: 0.373333rem 0.32rem ...
- UIScrollView之isTracking delaysContentTouches canCancelContentTouches
UIScrollView有一个BOOL类型的tracking属性,用来返回用户是否已经触及内容并打算开始滚动,我们从这个属性开始探究UIScrollView的工作原理: 当手指触摸到UIScrollV ...
- while 语句
/* while循环 格式:while(循环保持条件){需要执行的语句} OC: int i = 0; int sum = 0; while (i <= 10) { sum = i++; } w ...
- 剑指offer四十五之扑克牌顺子(序列是否连续)
一.题目 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决 ...
- (转)Db2数据库一次生产故障详细记录---数据库坏页
原文:http://www.talkwithtrend.com/Article/216335 前言 数据库最严重的故障莫过于数据库损坏.数据库坏页是数据库损坏的一种,如果数据库中有数据页出现损坏,在没 ...
- (转)MySQL 5.6 OOM 问题解决分享
本文来自:杨德华的原创分享 | MySQL 5.6 OOM 问题解决分享 原文:http://www.cnblogs.com/zhoujinyi/p/5763112.html 延伸阅读:Linux的内 ...
- Java之IO(十四)IO包中其它类
转载请注明出处:http://www.cnblogs.com/lighten/p/7267553.html 1.前言 此章介绍IO包中剩余未介绍的几个流和工具类,包括LineNumberReader. ...
- Spring Boot的Servlet简单使用
当使用spring-Boot时,嵌入式Servlet容器通过扫描注解的方式注册Servlet.Filter和Servlet规范的所有监听器(如HttpSessionListener监听器). Spri ...
- Disconf 学习系列之Disconf 的功能特点
不多说,直接上干货! 支持配置(配置项+配置文件)的分布式化管理 配置发布统一化 配置发布.更新统一化(云端存储.发布):配置存储在云端系统,用户统一在平台上进行发布.更新配置. 配置更新自动化:用户 ...