http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235

这道题需要构造矩阵:F(X)=F(X-1)+F(X-2)*A(X)转化为F(X)*A(X+2)+F(X+1)=F(X+2),然后在构造矩阵

{1, A[x]}  {F(x+1)}  {F(X+2)}

{1,    0 }*{F(X)    }={F(X+1)}

然后用线段数维护乘号左边的乘积;

 #include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 101000
using namespace std;
const int mod=; int t,a[maxn*],n,m;
int l,r;
struct matrix
{
long long a[][];
}; struct node
{
int l;
int r;
matrix c;
} tree[maxn*]; matrix multi(matrix x,matrix y)
{
matrix temp;
for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
temp.a[i][j]=;
for(int k=; k<=; k++)
{
temp.a[i][j]=(x.a[i][k]*y.a[k][j]+temp.a[i][j])%mod;
}
}
}
return temp;
} matrix build(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
if(l==r)
{
tree[i].c.a[][]=;
tree[i].c.a[][]=a[l];
tree[i].c.a[][]=;
tree[i].c.a[][]=;
return tree[i].c;
}
int mid=(l+r)>>;
build(i<<,l,mid);
build(i<<|,mid+,r);
tree[i].c=multi(tree[i<<|].c,tree[i<<].c);
return tree[i].c;
} matrix search1(int i,int l,int r)
{
if(tree[i].l==l&&tree[i].r==r)
{
return tree[i].c;
}
int mid=(tree[i].l+tree[i].r)>>;
if(r<=mid)
{
return search1(i<<,l,r);
}
else if(l>mid)
{
return search1(i<<|,l,r);
}
else
{
return multi(search1(i<<|,mid+,r),search1(i<<,l,mid));
}
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
build(,,n);
while(m--)
{
scanf("%d%d",&l,&r);
if(r-l>=)
{
matrix tm,ans;
tm.a[][]=a[l+];
tm.a[][]=a[l];
ans=multi(search1(,l+,r),tm);
printf("%lld\n",ans.a[][]);
}
else printf("%d\n",a[r]%mod);
}
}
return ;
}

zoj 3772 Calculate the Function的更多相关文章

  1. 线段树 + 矩阵 --- ZOJ 3772 Calculate the Function

    Calculate the Function Problem's Link:   http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...

  2. ZOJ 3772 Calculate the Function 线段树+矩阵

    Calculate the Function Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %ll ...

  3. Z0J 3772 Calculate the Function 线段树+矩阵

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂 ...

  4. zoj Calculate the Function

    Calculate the Function Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a list of num ...

  5. 2014 Super Training #7 E Calculate the Function --矩阵+线段树

    原题:ZOJ 3772 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 这题算是长见识了,还从没坐过矩阵+线段树的题 ...

  6. ZOJ3772 - Calculate the Function(线段树+矩阵)

    题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...

  7. ZOJ 3707 Calculate Prime S 数论

    思路:容易得到s[n]=s[n-1]+s[n-2],也就是fib数. 求第k小的fib质数的也就是第k个质数数-2,当k>2时. 在就是s[n]/x%m=s[n]%(x*m)/x. 代码如下: ...

  8. 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)) ...

  9. 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) = ...

随机推荐

  1. 转摘--如何利用多核CPU来加速你的Linux命令 — awk, sed, bzip2, grep, wc等

    http://www.vaikan.com/use-multiple-cpu-cores-with-your-linux-commands/ 你是否曾经有过要计算一个非常大的数据(几百GB)的需求?或 ...

  2. 【转】win7+ubuntu双系统安装方法--不错

    原文网址:http://blog.csdn.net/lvanneo/article/details/16885121 前段时间又安装一下win7+ubuntu双系统,过段时间就会忘记,这次自己写下来, ...

  3. 利用linux信号机制调试段错误(Segment fault)

    在实际开发过程中,大家可能会遇到段错误的问题,虽然是个老问题,但是其带来的隐患是极大的,只要出现一次,程序立即崩溃中止.如果程序运行在PC中,segment fault的调试相对比较方便,因为可以通过 ...

  4. ACM2136

    /* Problem Description Everybody knows any number can be combined by the prime number. Now, your tas ...

  5. java_IO流小结

    字符流和字节流的主要区别: 1.字节流读取的时候,读到一个字节就返回一个字节:  字符流使用了字节流读到一个或多个字节(中文对应的字节数是两个,在UTF-8码表中是3个字节)时.先去查指定的编码表,将 ...

  6. (转)iOS7界面设计规范(5) - UI基础 - 导航

    通过分页控件(page control)来暗示多个条目或内容视图的存在.该控件可以有效的向用户展示内容单元的数量,以及当前所见的这一条在队列中的位置.请参考分页控件一节,了解更多详情. 注意:虽然工具 ...

  7. CSS3 div水平、垂直居中,IE9以上、Firefox、Chrome均正常

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  8. HDU 2845 Beans (DP)

    Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...

  9. 你必须掌握的Java基础:JSON解析工具-json-lib

    一.简介  json-lib是一个Java类库,提供将Java对象,包括beans,maps,collections,java arrays和xml等转换成JSON,或者反向转换的功能. 二.准备 在 ...

  10. hdu 1240 Asteroids! (三维bfs)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...