【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 ...
随机推荐
- python web开发flask框架 安装与环境
# encoding:utf-8 # 从flask这个框架中导入Flask这个类 from flask import Flask # 初始化一个Flask对象 # Flasks() # 需要传递一个参 ...
- JavaScript(2):函数
<!DOCTYPE html> <html> <body> <p>JavaScript 函数</p> <script> // 函 ...
- java:shiroProject
1.backend_system Maven Webapp: LoginController.java: package com.shiro.demo.controller; import org ...
- IntelliJ IDEA 2018 for Mac专业使用技巧
IntelliJ IDEA 2018 for Mac是一个综合性的Java编程环境,被许多开发人员和行业专家誉为市场上最好的IDE,它提供了一系列最实用的的工具组合:智能编码辅助和自动控制,支持J2E ...
- Python3 Selenium自动化web测试 ==> 第二节 页面元素的定位方法 <上>
前置步骤: 上一篇的Python单元测试框架unittest,我认为相当于功能测试测试用例设计中的用例模板,在自动化用例的设计过程中,可以封装一个模板,在新建用例的时候,把需要测试的步骤添加上去即可: ...
- 华为HCNA乱学Round 8:生成树
- Markdown用法说明(用此篇博客做示例)
一份好的博客文档离不开一个优秀的编辑器.借此篇文章介绍一下编写该博客markdown的语法,后续再增加介绍其他语法,方便大家写出更好更漂亮的文档.点击左上角github,有主题源码哦 一份好的博客文档 ...
- 菜鸟系列k8s——k8s集群部署(2)
k8s集群部署 1. 角色分配 角色 IP 安装组件 k8s-master 10.0.0.170 kube-apiserver,kube-controller-manager,kube-schedul ...
- java.sql.SQLException: Access denied for user 'root'@'10.10.7.180' (using password: YES)
1.刚开始连接数据库提示是: java.sql.SQLException: Access denied for user 'root'@'10.10.7.180' (using password: N ...
- idea导入eclipse的web项目
idea导入eclipse的web项目 一.导入自己的web项目 步骤:File->New->Project from Existing Source... 二.选择项目的所在位 ...