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) = ...
随机推荐
- JS打印、预览(IE,Chrome)
IE下: 调用IE内置打印组件完成web打印方案.IE调用ActiveX实现打印. 重点: 注意: 1.CSS对打印的控制: .Noprint{display:none;} .PageNext{pag ...
- BZOJ2101: [Usaco2010 Dec]Treasure Chest 藏宝箱
2101: [Usaco2010 Dec]Treasure Chest 藏宝箱 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 327 Solved: ...
- BZOJ1116: [POI2008]CLO
1116: [POI2008]CLO Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 565 Solved: 303[Submit][Status] ...
- hdu5080:几何+polya计数(鞍山区域赛K题)
/* 鞍山区域赛的K题..当时比赛都没来得及看(反正看了也不会) 学了polya定理之后就赶紧跑来补这个题.. 由于几何比较烂写了又丑又长的代码,还debug了很久.. 比较感动的是竟然1Y了.. * ...
- 找出最小的k个数
•已知数组中的n个正数,找出其中最小的k个数. •例如(4.5.1.6.2.7.3.8),k=4,则最小的4个数是1,2,3,4 •要求: –高效: –分析时空效率 •扩展:能否设计出适合在海量数据中 ...
- 升级 pip版本
安装第三方库:pip install Pillow 出现 You are using pip version 7.1.2, however version 9.0.1 is available. Yo ...
- 原生javascript 获得css样式有几种方法?
css 样式分为行内样式和 外部样式: 1.javascript 获得行内样式 : 可以使用 ele.style."属性名称"(如果遇到属性名称带有"-", ...
- 【InversionCount 逆序对数 + MergeSort】
Definition of Inversion: Let (A[0], A[1] ... A[n], n <= 50) be a sequence of n numbers. If i < ...
- [RxJS] Reactive Programming - Clear data while loading with RxJS startWith()
In currently implemention, there is one problem, when the page load and click refresh button, the us ...
- html5图片标签与属性
标记: 标 记 说 明 <lmg> 图像 <Map> 图像映射 <Area> 图像映射中定义区域 <lmg>标记属性: 属 性 说 明 Src ...