ZOJ 3772 Calculate the Function
来源:互联网 发布:apache ab 结果分析 编辑:程序博客网 时间:2024/05/20 18:16
矩阵+线段树。。。
Fx 1 Ar 1 A l+2 F l
Fx-1 = 1 0 * .... * 1 0 * F l+1
注意矩阵乘的顺序。。。。
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
14 71 2 3 41 11 21 31 42 43 44 4
Sample Output
125131144
Author: CHEN, Weijie
Source: The 14th Zhejiang University Programming Contest
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef long long int LL;const int maxn=200000;const LL MOD=1000000007;struct MARTRIX{ LL m[2][2]; MARTRIX operator*(const MARTRIX& b) const { MARTRIX ret; ret.m[0][0]=((m[0][0]*b.m[0][0])%MOD+(m[0][1]*b.m[1][0])%MOD)%MOD; ret.m[0][1]=((m[0][0]*b.m[0][1])%MOD+(m[0][1]*b.m[1][1])%MOD)%MOD; ret.m[1][0]=((m[1][0]*b.m[0][0])%MOD+(m[1][1]*b.m[1][0])%MOD)%MOD; ret.m[1][1]=((m[1][0]*b.m[0][1])%MOD+(m[1][1]*b.m[1][1])%MOD)%MOD; return ret; } MARTRIX(LL a) { m[0][0]=1; m[0][1]=a; m[1][0]=1; m[1][1]=0; } MARTRIX() { memset(m,0,sizeof(m)); }}mrt[maxn<<2];int n,m;LL a[maxn];void build(int l,int r,int rt){ if(l==r) { mrt[rt]=MARTRIX(a[l]); return ; } int m=(l+r)/2; build(lson); build(rson); mrt[rt]=mrt[rt<<1|1]*mrt[rt<<1];}MARTRIX query(int L,int R,int l,int r,int rt){ if(L<=l&&R>=r) { return mrt[rt]; } int m=(l+r)/2; if(R<=m) return query(L,R,lson); if(L>m) return query(L,R,rson); return query(L,R,rson)*query(L,R,lson);}int main(){ int T_T; scanf("%d",&T_T); while(T_T--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%lld",a+i); build(1,n,1); while(m--) { int L,R; scanf("%d%d",&L,&R); if(R<L+2) { printf("%lld\n",a[R]%MOD); continue; } MARTRIX mt=query(L+2,R,1,n,1); printf("%lld\n",((a[L+1]*mt.m[0][0])%MOD+(a[L]*mt.m[0][1])%MOD)%MOD); } } return 0;}
- ZOJ 3772 Calculate the Function
- ZOJ 3772 Calculate the Function
- zoj 3772 Calculate the Function
- ZOJ 3772 Calculate the Function( 线段树 )
- ZOJ 3772 Calculate the Function 解题报告
- zoj 3772 Calculate the Function(矩阵乘法)
- ZOJ 3772Calculate the Function(线段树)
- ZOJ 3772 —— Calculate the Function(矩阵乘法)
- zoj 3772 Calculate the Function monthly 14th
- zoj-3772-Calculate the Function-线段树+矩阵
- ZOJ Problem Set - 3772 Calculate the Function 矩阵+线段树
- zoj 3772 Calculate the Function 线段树+矩阵
- ZOJ 3772 Calculate the Function(线段树 + 矩阵)
- ZOJ 3772 Calculate the Function (线段树 + 矩阵)
- [矩阵+线段树] zoj 3772 Calculate the Function
- zoj 3772 Calculate the Function 矩阵相乘+ 线段树查询
- zoj 3772 Calculate the Function(矩阵+线段树)
- zoj 3772 Calculate the Function(线段树+矩阵乘法)
- 第14届浙江大学程序设计竞赛 B. Continuous Login (ZOJ 3768)
- 坐标旋转公式(推导)
- 用 487 字节的 C 代码实现 2048 游戏
- java桥接模式
- Weblogic学习笔记-Weblogic 域(Domain)(二
- ZOJ 3772 Calculate the Function
- sdk版本号对照表
- android App应用的打包及发布
- java单例模式
- I2C和SPI,UART的区别(转)
- HDU 2203 亲和串(KMP:循环移位)
- java单例模式5中实现方法
- ZOJ-2109
- 微软强调Win8.1 Update照顾惯用键鼠用户