zoj 3772 Calculate the Function
来源:互联网 发布:搜狐微博 知乎 编辑:程序博客网 时间:2024/06/11 12:50
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
此题主要参考人家大神的思路。。。。
链接:http://blog.csdn.net/u013654696/article/details/23037407#comments
#include <iostream>#include <cstdio>#include <climits>#include <cstring>#include <cstdlib>#include <cmath>#include <vector>#include <queue>#include <algorithm>#define esp 1e-6#define inf 0x0f0f0f0f#define LL long longusing namespace std;struct mat { LL a[2][2]; mat() { memset( a, 0, sizeof( a ) ); } mat( LL x ) { a[0][0] = a[1][0] = 1; a[1][1] = 0; a[0][1] = x; } mat operator * ( const mat &b ) const { mat ret; for( int i = 0; i < 2; ++i ) for( int j = 0; j < 2; ++j ) for( int k = 0; k < 2; ++k ) ret.a[i][j] = ( ret.a[i][j] + a[i][k] * b.a[k][j] ) % 1000000007; return ret; }};LL ll[100005<<2], rr[100005<<2];LL num[100005<<2];mat sum[100005<<2];void build(LL cur,LL l,LL r){ ll[cur]=l; rr[cur]=r; if(l==r) { sum[cur]=mat(num[l]); return; } build(2*cur,l,(l+r)/2); build(2*cur+1,(l+r)/2+1,r); sum[cur]=sum[2*cur+1]*sum[2*cur];}mat query(LL cur,LL l,LL r){ if(l<=ll[cur]&&rr[cur]<=r) return sum[cur]; else { mat ans; ans.a[0][0]=ans.a[1][1]=1; ans.a[0][1]=ans.a[1][0]=0; if(r>(ll[cur]+rr[cur])/2) ans=ans*query(2*cur+1,l,r); if(l<=(ll[cur]+rr[cur])/2) ans=ans*query(2*cur,l,r); return ans; }}int main(){ LL t,i,j; LL n,m,a,b; mat ans; scanf("%lld",&t); while(t--) { scanf("%lld%lld",&n,&m); for(i=1;i<=n;i++) scanf("%lld",&num[i]); build(1,1,n); while(m--) { scanf("%lld%lld",&a,&b); if(a==b) { printf("%lld\n",num[a]); continue; } if(b==a+1) { printf("%lld\n",num[b]); continue; } ans=query(1,a+2,b); //printf("***%lld %lld\n%lld %lld\n",ans.a[0][0],ans.a[0][1],ans.a[1][0],ans.a[1][1]); printf("%lld\n",(ans.a[0][0]*num[a+1]+ans.a[0][1]*num[a])%1000000007); } }}
0 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(线段树+矩阵乘法)
- 【美萍超市管理系统】汉码盘点机无缝对接 金蝶盘点机条码数据采集器智能终端PDA
- 非洲小孩
- Number of 1 Bits--LeetCode
- Python对象类型
- 将双向链表转化为二叉堆并有序输出
- zoj 3772 Calculate the Function
- 6. linux文件处理的有用工具
- 求平均成绩 HDU2023
- 我的Html5学习开发历程(一)—— 导航栏的制作
- NIO 入门
- HDU 5203 Rikka with wood sticks
- MBA 论文 物联网,加快产业结构转型升级
- Tomcat 多域名配置
- hdu 2023 求平均成绩