【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 ...
随机推荐
- hibernate之inverse=true相关配置讲解
首先inverse=”true”是在双向关联里面使用单向关联没有这个配置 inverse – 标记由哪一方来维护关联关系(双向关联中会用到) inverse默认值为false 如果inverse设 ...
- Linux进程间通信(IPC)之信号量
[Linux]进程间通信(IPC)之信号量详解与测试用例 2017年03月22日 17:28:50 阅读数:2255 学习环境centos6.5 Linux内核2.6 进程间通信概述 1. 进程通信机 ...
- redhat网卡设置
在终端中输入:vi /etc/sysconfig/network-scripts/ifcfg-eth0 开始编辑,填写ip地址.子网掩码.网关.DNS等.其中“红框内的信息”是必须得有的. 编 ...
- SQL常见面试题(借书卡表_图书表_借书记录表)
问题描述: 本题用到下面三个关系表: CARD 借书卡: CNO 卡号,NAME 姓名,CLASS 班级 BOOKS 图书: BNO 书号,BNAME 书名,AU ...
- Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum)
Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum) 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. ...
- 关于虚拟机docker 启动mysql 启动成功但未挂载到端口
首先排查了防火墙和其他权限相关问题 然后检查了mysql 用户权限问题 docker logs 查看日志 正常应该是到3306 问题是我的mysql my.cnf 文件是挂在在本地.当第二次启动容器时 ...
- WCF客户端代理
创建类库WCFServiceProxy 添加System.ServiceModel.WCFService(见上篇文章)引用 创建类:BookServiceClient using System; us ...
- 编译安装php7.3
./configure --prefix=/usr/local/php7.3.9 --with-gd --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo ...
- 四则运算计算器的微信小程序_2 运算
js文件: function isOperator(value) { var operatorString = '+-*/()×÷'; return operatorString.indexO ...
- .Net Core Grpc 实现通信
.Net Core 3.0已经把Grpc作为一个默认的模板引入,所以我认为每一个.Net程序员都有学习Grpc的必要,当然这不是必须的. 我在我的前一篇文章中介绍并创建了一个.Net Core 3.0 ...