BZOJ-3231 [SDOI2008]递归数列
转成矩阵连乘后,矩阵快速幂加速解决。
一开始没把需要longlong的变量补全。。而且没初始化2333
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <iostream>
#define rep(i, l, r) for(int i=l; i<=r; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define maxn 20
#define ll long long
#define l(x) x*2
#define r(x) x*2+1
using namespace std;
inline ll read()
{
ll x=0, f=1; char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while (isdigit(ch)) x=x*10+ch-'0', ch=getchar();
return x*f;
}
int n;
ll q;
ll m[maxn][maxn], m2[maxn][maxn], b[maxn], c[maxn], a[maxn], a2[maxn]; ll Query(ll k)
{
if (k<=n) {ll now=0; rep(i, 1, k) now+=b[i]; return now;} else k-=n;
clr(m, 0);
rep(i, 1, n) m[n][n+1-i]=m[n+1][n+1-i]=c[i];
rep(i, 1, n-1) m[i][i+1]=1; m[n+1][n+1]=1;
rep(i, 1, n+1) a[i]=b[i];
while (k)
{
if (k&1)
{
clr(a2, 0);
rep(i, 1, n+1) rep(j, 1, n+1) a2[i]=(a2[i]+a[j]*m[i][j])%q;
rep(i, 1, n+1) a[i]=a2[i];
}
clr(m2, 0);
rep(i, 1, n+1) rep(j, 1, n+1) rep(o, 1, n+1) m2[i][j]=(m2[i][j]+m[i][o]*m[o][j])%q;
rep(i, 1, n+1) rep(j, 1, n+1) m[i][j]=m2[i][j];
k=k>>1;
}
return a[n+1];
} int main()
{
n=read();
rep(i, 1, n) b[i]=read(), b[n+1]+=b[i];
rep(i, 1, n) c[i]=read();
ll x=read(), y=read(); q=read();
rep(i, 1, n) b[i]%=q, c[i]%=q;
printf("%lld", (Query(y)-Query(x-1)+2*q)%q);
return 0;
}
BZOJ-3231 [SDOI2008]递归数列的更多相关文章
- BZOJ 3231: [Sdoi2008]递归数列( 矩阵快速幂 )
矩阵乘法裸题..差分一下然后用矩阵乘法+快速幂就可以了. ----------------------------------------------------------------------- ...
- BZOJ 3231: [Sdoi2008]递归数列 (JZYZOJ 1353) 矩阵快速幂
http://www.lydsy.com/JudgeOnline/problem.php?id=3231 和斐波那契一个道理在最后加一个求和即可 #include<cstdio> #i ...
- bzoj 3231 [Sdoi2008]递归数列——矩阵乘法
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3231 矩阵乘法裸题. 1018是10^18.别忘了开long long. #include& ...
- bzoj 3231: [Sdoi2008]递归数列【矩阵乘法】
今天真是莫名石乐志 一眼矩阵乘法,但是这个矩阵的建立还是挺有意思的,就是把sum再开一列,建成大概这样 然后记!得!开!long!long!! #include<iostream> #in ...
- BZOJ:3231: [Sdoi2008]递归数列
题解: 矩阵乘法,在矩阵中构造当前前缀和: 注意:for(int/long long ;;); #include<iostream> #include<cstdio> #inc ...
- BZOJ3231: [Sdoi2008]递归数列
BZOJ3231: [Sdoi2008]递归数列 Description 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + ...
- BZOJ_3231_[Sdoi2008]递归数列_矩阵乘法
BZOJ_3231_[Sdoi2008]递归数列_矩阵乘法 Description 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1a ...
- 开始玩矩阵了!先来一道入门题![SDOI2008]递归数列
[SDOI2008]递归数列 题目描述 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 + ... + c ...
- P2461 [SDOI2008]递归数列
题目描述 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 + ... + ckai-k 其中bj 和 cj ...
- [bzoj3231][SDOI2008]递归数列——矩阵乘法
题目大意: 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 + ... + ckai-k 其中bj和 cj ...
随机推荐
- 网络编程——UDP协议和通信
第1章 UDP与TCP协议 在介绍TCP/IP结构时,提到传输层的两个重要的高级协议,分别是UDP和TCP,其中UDP是User Datagram Protocol的简称,称为用户数据报协议,TCP是 ...
- 使用dotnet-dump 查找 .net core 3.0 占用CPU 100%的原因
公司的产品一直紧跟 .net core 3.0 preview 不断升级, 部署到 Linux 服务器后, 偶尔会出现某个进程CPU占用100%. 由于服务部署在云上, 不能使用远程调试; 在局域网内 ...
- C#的接口基础教程之二 定义接口
定义接口 从技术上讲,接口是一组包含了函数型方法的数据结构.通过这组数据结构,客户代码可以调用组件对象的功能. 定义接口的一般形式为: [attributes] [modifiers] interfa ...
- https及其背后的加密原理阅读总结
https是以安全为目标的http通道,简单讲是http的安全版.当我们往服务器发送比较隐私的数据(比如说你的银行卡,身份证)时,如果使用http进行通信.那么安全性将得不到保障. 首先数据在传输的过 ...
- 【luogu题解】P1546 最短网络 Agri-Net
题目 约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其他农场.为了用最小的消费,他想铺设最短的光纤去连接所有的农场. 你将得到一份各农场之间连接费用的列表,你必须找出能连接所有农场并 ...
- 九、Linux 磁盘管理
Linux 磁盘管理 Linux磁盘管理好坏直接关系到整个系统的性能问题. Linux磁盘管理常用三个命令为df.du和fdisk. df:列出文件系统的整体磁盘使用量 du:检查磁盘空间使用量 fd ...
- linux三剑客之sed深度实践
参数: -a:追加文本到指定行后 -i:插入文本到指定行前 1.单行增加 [root@redhat~]# sed ' 2a 6,f ' linux.tet 1,a 2,b 6,f 3,c 4 ...
- 【linux】CPU,内存对网站的影响
如果读写非常多,建议内存大点 如果涉及到的计算非常多,那就升级CPU
- 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8
a,b,c,d,e,f,g=1,2,3,4,5,8,9 m = a > b and c < d or c > e n = b > a or g < f x = m and ...
- A1046 Shortest Distance (20)(20 分)
1046 Shortest Distance (20)(20 分)提问 The task is really simple: given N exits on a highway which form ...