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)转化为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的更多相关文章
- 线段树 + 矩阵 --- 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 ...
- Z0J 3772 Calculate the Function 线段树+矩阵
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂 ...
- zoj Calculate the Function
Calculate the Function Time Limit: 2 Seconds Memory Limit: 65536 KB You are given a list of num ...
- 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 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. 代码如下: ...
- 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) = ...
随机推荐
- NGINX当中的SSL配置(PEM启动密码免输入)
干过的都懂,这个东东好像很碍事. 很影响RELOAD的效率...并且,,,好像这个弄了之后,NGINX作RELOAD之后,可以即时更新配置了..(还是说那个DVROOT.CER影响的??未考证) ht ...
- WPF学习拾遗(二)TextBlock换行
原文:WPF学习拾遗(二)TextBlock换行 下午在帮组里的同事解决一个小问题,为了以后方便,把就把它收集一下吧. 新建一个TextBlock作为最基础的一个控件,他所携带的功能相对于其他的控件要 ...
- 关于selenium中的sendKeys()隔几秒发送一个字符
看一下你的IEDriverServer.exe是不是64位的,我也遇到了这样的问题,换成32位的IEDriverServer.exe,瞬间速度快了
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- java算法之身份证号码验证
调用时直接 new IDCard().verify(身份证id);就可以了 实现代码如下: public class IDCard { private String _codeError; //wi ...
- (step5.1.6)hdu 1272(小希的迷宫——并查集)
题目大意:输入一系列的点,判断这些点组成的图符不符合小希的思路(无环.连通) 解题思路: 1)如果两个节点的根节点相同,那么在这两个节点之间添加1条边以后,这个图肯定有环路. 2)孤立节点:被使用过& ...
- 【转】TI蓝牙BLE 协议栈代码学习
BLE就是低功率蓝牙.要着重了解两种设备: dual-mode双模设备:简单说就是向下兼容. single-mode单模设备:仅仅支持BLE. 关于开发主要讲的是单模设备,它可以只靠纽扣电池即可持 ...
- 关于hibernate中对象的三种状态分析
一,首先hibernate中对象的状态有三种:瞬态.游离态和持久态,三种状态转化的方法都是通过session来调用,瞬态到持久态的方法有save().saveOrUpdate().get().load ...
- 1042. Shuffling Machine (20) - sstream实现数字转字符串
题目例如以下: Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffli ...
- openfire文件夹
插件开发 学习制作第一个 openfire 插件 http://www.cnblogs.com/jying/p/3683409.html 跟我一步一步开发自己的Openfire插件 http://bl ...