矩阵+线段树。。。
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:
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.
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).
For each test case, output the remainder of the answer divided by 1000000007.
1 4 7 1 2 3 4 1 1 1 2 1 3 1 4 2 4 3 4 4 4
1 2 5 13 11 4 4
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef 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,布布扣,bubuko.com
ZOJ 3772 Calculate the Function
原文:http://blog.csdn.net/ck_boss/article/details/23053047