CF446C DZY Loves Fibonacci Numbers 线段树 + 数学
有两个性质需要知道:
$1.$ 对于任意的 $f[i]=f[i-1]+f[i-2]$ 的数列,都有 $f[i]=fib[i-2]\times f[1]+fib[i-1]\times f[2]$
其中 $fib[i]$ 为第 $i$ 项斐波那契数列.
$2$. 对于任意满足上述条件的数列,都有 $\sum_{i=1}^{n}f[i]=f[n+2]-f[2]$
$3.$ 任意两断满足上述条件的数列每一项依次叠加,依然满足 $g[i]=g[i-1]+g[i-2]$,且上述两个性质都满足.
$4.$ 任何一段斐波那契数列也满足上述所有性质.
有了上述预备知识后,再考虑这道题:
我们用线段树来维护区间和,线段树上每个节点维护 $3$ 个信息,为 $sum,f1,f2$
即节点所维护的区间和,以及该节点及线段树中区间要加上一个前两项为 $f1,f2$ 的上述递推数列.
那么,我们只需考虑如何下传标记,如何查询即可.
假设当前节点已经有了 $f1,f2$,那么将标记下传给左子树是轻松的:直接下传即可,区间和的贡献可按照上述公式 $O(1)$ 求出.
而如果要下传给右儿子的话就不能直接传了,因为右儿子区间开头的两项并不是 $f1,f2$.
而根据上述三条性质,我们知道斐波那契数列的任何一段也是斐波那契数列.
所以,直接算出右儿子的 $f1,f2$ 即 $f1\times fib[mid-l]+f2\times fib[mid-l+1]$ 与 $f1\times fib[mid-l+1]+f2\times fib[mid-l+2]$
然后还知道 $f1,f2$ 都满足叠加性,所以直接叠加到左右儿子的 $f1,f2$ 上即可.
#include <bits/stdc++.h>
#define N 400004
#define LL long long
#define lson now<<1
#define rson now<<1|1
#define setIO(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
const LL mod=1000000009;
int n,m;
LL fib[N<<1],sum[N<<1];
struct node
{
LL f1,f2,sum;
int l,r,len;
}t[N<<2];
void build(int l,int r,int now)
{
t[now].l=l;
t[now].r=r;
t[now].len=r-l+1;
if(l==r) return ;
int mid=(l+r)>>1;
if(l<=mid) build(l,mid,lson);
if(r>mid) build(mid+1,r,rson);
}
void mark(int now,LL f1,LL f2)
{
(t[now].f1+=f1)%=mod;
(t[now].f2+=f2)%=mod;
(t[now].sum+=f1*fib[t[now].len]%mod+f2*fib[t[now].len+1]%mod-f2+mod)%=mod;
}
void pushup(int now)
{
t[now].sum=(t[lson].sum+t[rson].sum)%mod;
}
void pushdown(int now)
{
if(t[now].f1==0&&t[now].f2==0) return;
int mid=(t[now].l+t[now].r)>>1;
mark(lson,t[now].f1,t[now].f2);
if(t[now].r>mid)
mark(rson,t[now].f1*fib[t[lson].len-1]%mod+t[now].f2*fib[t[lson].len]%mod,t[now].f1*fib[t[lson].len]%mod+t[now].f2*fib[t[lson].len+1]%mod);
t[now].f1=t[now].f2=0;
}
void update(int l,int r,int now,int L,int R)
{
if(l>=L&&r<=R)
{
mark(now,fib[l-L+1],fib[l-L+2]);
return;
}
pushdown(now);
int mid=(l+r)>>1;
if(L<=mid) update(l,mid,lson,L,R);
if(R>mid) update(mid+1,r,rson,L,R);
pushup(now);
}
LL query(int l,int r,int now,int L,int R)
{
if(l>=L&&r<=R)
{
return t[now].sum;
}
pushdown(now);
int mid=(l+r)>>1;
LL re=0ll;
if(L<=mid) re+=query(l,mid,lson,L,R);
if(R>mid) re+=query(mid+1,r,rson,L,R);
return re%mod;
}
int main()
{
// setIO("input");
int i,j;
scanf("%d%d",&n,&m);
fib[1]=fib[2]=1;
for(i=3;i<N;++i) fib[i]=(fib[i-1]+fib[i-2])%mod;
for(i=1;i<=n;++i) scanf("%lld",&sum[i]), (sum[i]+=sum[i-1])%=mod;
build(1,n,1);
for(i=1;i<=m;++i)
{
int opt,l,r;
scanf("%d%d%d",&opt,&l,&r);
if(opt==1) update(1,n,1,l,r);
else printf("%lld\n",(query(1,n,1,l,r)+sum[r]-sum[l-1]+mod*2)%mod);
}
return 0;
}
CF446C DZY Loves Fibonacci Numbers 线段树 + 数学的更多相关文章
- ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)
Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...
- codeforces 446C DZY Loves Fibonacci Numbers 线段树
假如F[1] = a, F[2] = B, F[n] = F[n - 1] + F[n - 2]. 写成矩阵表示形式可以很快发现F[n] = f[n - 1] * b + f[n - 2] * a. ...
- Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]
洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...
- Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)
第一次看到段更斐波那契数列的,整个人都不会好了.事后看了题解才明白了一些. 首先利用二次剩余的知识,以及一些数列递推式子有下面的 至于怎么解出x^2==5(mod 10^9+9),我就不知道了,但是要 ...
- 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers
我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...
- cf446C DZY Loves Fibonacci Numbers
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)
题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...
随机推荐
- opencv学习笔记D01
目录 opencv学习笔记D01 一.图片读取 二.图片保存 三.图片展示 四.图片缩放 五.四种常用插值方式的比较 1.最近邻插值 2.双线性插值 3.区域插值 4.三次样条插值 我是尾巴: ope ...
- Logrotate滚动openresty日志
一.摘要 Linux服务器上我们用Logrotate来分割归档日志文件,结合crond我们可以指定每天在某个时间自动整理日志等文档.本文主要说明了Centos下Logrotate的使用和配置的方法. ...
- python3+requests:post请求四种传送正文方式
https://www.cnblogs.com/insane-Mr-Li/p/9145152.html 前言:post请求我在python接口自动化2-发送post请求详解(二)已经讲过一部分了,主要 ...
- tkinter学习笔记_06
12.弹窗 messagebox import tkinter as tk from tkinter import messagebox root = tk.Tk() root.title(" ...
- Ubuntu 18.04 Server 配置静态ip
刚在虚拟机里面状态了一个 Ubunut 18.04 Server 作为我的服务器,我习惯使用静态ip首先再virtualbox中设置虚拟机网络的连接方式为桥接模式进入ubuntu虚拟机根据我的印象直接 ...
- The Day Two 找到一个具有最大和的连续子数组,返回其最大和
""" 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5, ...
- 解决跟Docker私有仓库登陆,推送,拉取镜像出现的报错
出现问题:Error response from daemon: Get https://192.168.186.120/v1/users/: dial tcp 192.168.186.120:443 ...
- mybatis 多个中间表查询映射
最近项目用到中间表,则遇到如何联查映射的问题,之前一直都是一个表头,多个明细或者一对一这样的关系,没遇到这样的问题,所以趁机找了下资料解决了这个问题. 表结构设计如下: 主表: CREATE TABL ...
- docker 入坑3
查看镜像 docker images [OPTIONS] [REPOSITORY[:TAG]] -a, --all=false -f, --filter=[] --no-trunc=false -q, ...
- JavaScript 数组 遍历方法 map( ) 和 forEach( )
let arr = [1, 3, 7, 6, 9]; 不用知道元素的个数,即不用设置开始下标和结束下标. 1:forEach( )会把数组中的每个值进行操作,没有返回值,undefined let j ...