【CF 718C】fibonacci
题意
给你一个长度为 \(n\) 的序列 \(a\),有 \(m\) 次操作,操作分两种
- \(\text{1}\space \text{l}\space \text{r}\space \text{x}\)、区间加;
- \(\text{2}\space \text{l}\space \text{r}\)、求 \(\sum\limits_{i=l}^{r} \text{fib}(a_i) \mod (10^9+7)\)
\(\text{fib}(i)\) 表示斐波那契数列第 \(i\) 项,初始值为 \(\text{fib}(1)=\text{fib}(2)=1\)。
\(n,m\le 10^5,\space x,a_i\le 10^9\)
题解
shabi 模板题,我考试的时候弱智了,十点钟杠 T3 的时候才突然想到这是个线段树维护矩阵的模板题,我他吗前两个小时在睡觉吗
最后 T3 的 150 行代码 A 了,这个 T1 没写完……考后过编译就过了样例,然后交上去 1A 了……
CaO.jpg
好吧这可能不叫题解,但我觉得这种 shabi 题好像不用写题解吧。
#include<bits/stdc++.h>
#define ll long long
#define N 100010
#define mod 1000000007
using namespace std;
inline int read(){
int x=0; bool f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=0;
for(; isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
if(f) return x;
return 0-x;
}
int n,m;
struct Matrix{
int x[2][2];
Matrix(){x[0][0]=x[1][1]=1, x[0][1]=x[1][0]=0;}
Matrix operator + (const Matrix &a)const{
Matrix ret=Matrix();
ret.x[0][0] = (x[0][0] + a.x[0][0]) % mod;
ret.x[0][1] = (x[0][1] + a.x[0][1]) % mod;
return ret;
}
Matrix operator * (const Matrix &a)const{
Matrix ret=Matrix(); int i,j,k;
for(i=0; i<2; ++i)
for(int j=0; j<2; ++j)
ret.x[i][j] = ((ll)x[i][0]*a.x[0][j]%mod + (ll)x[i][1]*a.x[1][j]%mod) % mod;
return ret;
}
bool operator != (const Matrix &a)const{
return x[0][0]!=a.x[0][0] || x[0][1]!=a.x[0][1] || x[1][0]!=a.x[1][0] || x[1][1]!=a.x[1][1];
}
}a[N],A,B,mul;
Matrix matPow(Matrix x, int y){
Matrix ret=Matrix();
while(y){
if(y&1) ret=ret*x;
x=x*x;
y>>=1;
}
return ret;
}
namespace SegTree{
#define ls o<<1
#define rs o<<1|1
struct Tree{Matrix sum,tag;}tr[N<<2];
inline void pushup(int o){
tr[o].sum=tr[ls].sum+tr[rs].sum;
//cout<<"pushup:"<<tr[ls].sum.x[0][0]<<' '<<tr[ls].sum.x[0][1]<<' '<<tr[rs].sum.x[0][0]<<' '<<tr[rs].sum.x[0][1]<<endl;
//cout<<"pushup:"<<tr[o].sum.x[0][0]<<' '<<tr[o].sum.x[0][1]<<endl;
}
inline void pushdown(int o){
if(tr[o].tag!=Matrix()){
tr[ls].sum=tr[ls].sum*tr[o].tag, tr[rs].sum=tr[rs].sum*tr[o].tag;
tr[ls].tag=tr[ls].tag*tr[o].tag, tr[rs].tag=tr[rs].tag*tr[o].tag;
tr[o].tag=Matrix();
}
}
void build(int o, int l, int r){
if(l==r){tr[o].sum=a[l], tr[o].tag=Matrix(); return;}
int mid=l+r>>1;
build(ls,l,mid), build(rs,mid+1,r);
pushup(o);
}
void mdf(int o, int l, int r, int L, int R){
if(L<=l && r<=R){
tr[o].sum=tr[o].sum*mul;
tr[o].tag=tr[o].tag*mul;
return;
}
pushdown(o);
int mid=l+r>>1;
if(L<=mid) mdf(ls,l,mid,L,R);
if(R>mid) mdf(rs,mid+1,r,L,R);
pushup(o);
}
void mdf(int l, int r){
mdf(1,1,n,l,r);
}
int query(int o, int l, int r, int L, int R){
if(L<=l && r<=R) return tr[o].sum.x[0][1];
pushdown(o);
int mid=l+r>>1, ret=0;
if(L<=mid) ret=ret+query(ls,l,mid,L,R);
if(R>mid) ret=ret+query(rs,mid+1,r,L,R);
return ret%mod;
}
int query(int l, int r){
return query(1,1,n,l,r);
}
#undef ls
#undef rs
}using namespace SegTree;
int main(){
n=read(), m=read();
A.x[0][0]=1, A.x[0][1]=0;
B.x[0][0]=B.x[0][1]=B.x[1][0]=1, B.x[1][1]=0;
Matrix C=Matrix()*A;
for(int i=1; i<=n; ++i) a[i]=A*matPow(B,read());
build(1,1,n);
int opt,l,r;
for(int i=1; i<=m; ++i){
opt=read(), l=read(), r=read();
if(opt==1){
mul=matPow(B,read());
mdf(l,r);
}
else printf("%d\n",query(l,r));
}
return 0;
}
【CF 718C】fibonacci的更多相关文章
- 1643【例 3】Fibonacci 前 n 项和
1643:[例 3]Fibonacci 前 n 项和 时间限制: 1000 ms 内存限制: 524288 KB sol:这题应该挺水的吧,就像个板子一样 1 0 01 1 0 * ...
- 一本通1642【例 2】Fibonacci 第 n 项
1642: [例 2]Fibonacci 第 n 项 sol:挺模板的吧,经典题吧qaq (1) 1 0 * 1 1 = 1 1 1 0 (2) 1 1 * 1 ...
- 【CF#338D】GCD Table
[题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...
- 【hdu 1848】Fibonacci again and again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- 【CF#303D】Rotatable Number
[题目描述] Bike是一位机智的少年,非常喜欢数学.他受到142857的启发,发明了一种叫做“循环数”的数. 如你所见,142857是一个神奇的数字,因为它的所有循环排列能由它乘以1,2,...,6 ...
- 【POJ 3070】 Fibonacci
[题目链接] 点击打开链接 [算法] 矩阵乘法快速幂 [代码] #include <algorithm> #include <bitset& ...
- 【HDU 2855】 Fibonacci Check-up (矩阵乘法)
Fibonacci Check-up Problem Description Every ALPC has his own alpc-number just like alpc12, alpc55, ...
- 【CF 463F】Escape Through Leaf
题意 给你一棵 \(n\) 个点的树,每个节点有两个权值 \(a_i,b_i\). 从一个点 \(u\) 可以跳到以其为根的子树内的任意一点 \(v\)(不能跳到 \(u\) 自己),代价是 \(a_ ...
- 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)
A. Little Pony and Expected Maximum time limit per test 1 second memory limit per test 256 megabytes ...
随机推荐
- split 使用
split作用:把字符串变成列表,这个字符串必须是多行文字.如果是单行文字或一个单词是不行的,实例操作如下: In [46]: output=subprocess.check_output(['df' ...
- C基础知识(1):基本数据类型
C的基本数据类型包括整型和浮点型,长度及精度信息如下: #include <stdio.h> #include <limits.h> #include <float.h& ...
- JavaScript DOM 编程艺术(第二版) 有待解决的问题
原书 P181,var repeat = "moveElement('"+elementID+"', "+final_x+", "+fina ...
- MYSQL中重命名procedure的一种方法
最近有用到对存储过程(procedure)重命名的功能,在网上找了一下资料都没有讲到在mysql中是如何实现的,当然可以删掉再重建,但是应该有别的方法,在"mysql"这个数据库( ...
- SQL常见面试题(借书卡表_图书表_借书记录表)
问题描述: 本题用到下面三个关系表: CARD 借书卡: CNO 卡号,NAME 姓名,CLASS 班级 BOOKS 图书: BNO 书号,BNAME 书名,AU ...
- MATLAB灰色关联度分析
目录 1.释名 2.举例 3.操作步骤与原理详解 4.总结 5.附录:MATLAB代码 @ 1.释名 灰色关联度分析(Grey Relation Analysis,GRA),是一种多因素统计分析的方法 ...
- Android开发 所需组件配置
1 Unity中的Android Build Support下载 在Unity中的File>Building Settings>Android>Open Download Page, ...
- Vue实现点击时间获取时间段查询功能
二话不说,先上图 实现如上代码: //获取本周第一天 showWeekFirstDay: function () { let Nowdate = new Date(); let WeekFirstDa ...
- 代码: 0x80131500 win10应用商店崩溃了
网上搜索大部分认同的结果如下 1.打开“运行”输入 inetcpl.cpl (“WINDOWS”+“R”键,输入 inetcpl.cpl亦可) 2.点开高级往下拉,勾上"使用TLS 1.2& ...
- luoguP1045 (Java大数)
题目链接:https://www.luogu.org/problemnew/show/P1045 题意:给定p(1000<p<3100000),求2^p-1的位数和最后500位(若不足高位 ...