【CF446C】DZY Loves Fibonacci Numbers (线段树 + 斐波那契数列)
Description
看题戳我 给你一个序列,要求支持区间加斐波那契数列和区间求和。\(~n \leq 3 \times 10 ^ 5, ~fib_1 = fib_2 = 1~\).
Solution
先来考虑一段斐波那契数列如何快速求和,根据性质有
fib_n &= fib_{n - 1} + fib_{n - 2} \\
&= fib_ {n - 2} + fib_{n - 3} + fib_{n - 2} \\
&= fib_{n - 3} + fib_{n - 4} + fib_{n - 3} + fib_{n - 2} \\
&= \dots \\
&= fib_2 + \sum_{i = 1}^{n - 2} {fib_i}
\end {align}
\]
可以发现这里有个\(~\sum_{i = 1} ^ {n - 2} {fib_i}\),转换一下就是\(~\sum_{i = 1} ^ {n}fib_i = fib_{n + 2} - fib_2\).而两个斐波那契数列对应项加起来之后还是一个类斐波那契数列,记为\(~S_i\),设这个类斐波那契数列的起始项\(S_1 = a, S_2 = b\),显然有\(~S_i = a \times fib_{i - 2} + b \times fib_{i - 1}\).那么对于一段类斐波那契数列的求和,我们只要记起始的两项和这段数列的长度即可。现在可以用简单的线段树区间加来维护了,\(~PushDown~\)操作有一点细节,注意要分开算区间的前两项。具体看代码。。
Code
#include<bits/stdc++.h>
#define For(i, j, k) for(int i = j; i <= k; ++i)
#define Forr(i, j, k) for(int i = j; i >= k; --i)
using namespace std;
inline int read() {
int x = 0, p = 1; char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') p = -1;
for(; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x *= p;
}
inline void File() {
freopen("cf446c.in", "r", stdin);
freopen("cf446c.out", "w", stdout);
}
const int N = 3e5 + 10, mod = 1e9 + 9;
int n, m, fib[N];
inline int add(int a, int b) { return (a += b) >= mod ? a - mod : a; }
namespace SGT {
#define lc (rt << 1)
#define rc (rt << 1 | 1)
#define mid (l + r >> 1)
#define lson lc, l, mid
#define rson rc, mid + 1, r
int tr[N << 2], t1[N << 2], t2[N << 2];
inline void pushup(int rt) { tr[rt] = (tr[lc] + tr[rc]) % mod; }
inline int S(int a, int b, int x) {
return x == 1 ? a : (x == 2 ? b : (1ll * a * fib[x - 2] + 1ll * b * fib[x - 1]) % mod);
}
inline int sum(int a, int b, int x) {
return x == 1 ? a : (x == 2 ? add(a, b) : (S(a, b, x + 2) - b + mod) % mod);
}
inline void pushdown(int rt, int l, int r) {
if (t1[rt]) {
t1[lc] = add(t1[lc], t1[rt]), t2[lc] = add(t2[lc], t2[rt]);
tr[lc] = add(tr[lc], sum(t1[rt], t2[rt], mid - l + 1));
int T1 = S(t1[rt], t2[rt], mid - l + 2), T2 = S(t1[rt], t2[rt], mid - l + 3);
t1[rc] = add(t1[rc], T1), t2[rc] = add(t2[rc], T2);
tr[rc] = add(tr[rc], sum(T1, T2, r - mid));
t1[rt] = t2[rt] = 0;
}
}
inline void build(int rt, int l, int r) {
if (l == r) tr[rt] = read();
else build(lson), build(rson), pushup(rt);
}
inline void update(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) {
tr[rt] = add(tr[rt], sum(fib[l - L + 1], fib[l - L + 2], r - l + 1));
t1[rt] = add(t1[rt], fib[l - L + 1]); t2[rt] = add(t2[rt], fib[l - L + 2]);
return ;
}
pushdown(rt, l, r);
if (L <= mid) update(lson, L, R);
if (R > mid) update(rson, L, R);
pushup(rt);
}
inline int query(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) return tr[rt];
pushdown(rt, l, r); int res = 0;
if (L <= mid) res = add(res, query(lson, L, R));
if (R > mid) res = add(res, query(rson, L, R));
return pushup(rt), res;
}
#undef lc
#undef rc
#undef mid
#undef lson
#undef rson
}
int main() {
File();
n = read(), m = read();
fib[1] = fib[2] = 1;
For(i, 3, n + 5) fib[i] = (fib[i - 1] + fib[i - 2]) % mod;
using namespace SGT;
build(1, 1, n);
while (m --) {
int opt = read(), l = read(), r = read();
opt == 1 ? update(1, 1, n, l, r), 1 : printf("%d\n", query(1, 1, n, l, r)), 1;
}
return 0;
}
【CF446C】DZY Loves Fibonacci Numbers (线段树 + 斐波那契数列)的更多相关文章
- 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]$ 其中 ...
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
- 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 ...
- 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. ...
- Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)
第一次看到段更斐波那契数列的,整个人都不会好了.事后看了题解才明白了一些. 首先利用二次剩余的知识,以及一些数列递推式子有下面的 至于怎么解出x^2==5(mod 10^9+9),我就不知道了,但是要 ...
- Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]
洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...
- 【思维题 线段树】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 ...
随机推荐
- 把玩Alpine linux(一):安装
导读 Alpine Linux是一个面向安全应用的轻量级Linux发行版.它采用了musl libc和busybox以减小系统的体积和运行时资源消耗,同时还提供了自己的包管理工具apk.Alpine ...
- openstack-KVM-vCPU
一.KVM基础功能 (1)支持 硬件支持 VT-x VT-d 系统支持 kernel > 3.5 (2)计算机系统的子系统 CPU 处理器 Memory 内存 Storage 存储 Networ ...
- scrapy之环境安装
scrapy之环境安装 在之前我安装了scrapy,但是在pycharm中却无法使用. 具体情况是: 我的电脑上存在多个python,有python2,python3,anaconda,其中anaco ...
- Python之参数类型、变量
一.参数类型 (一)形参与实参 要使用局部变量时,只能通过return的方式返回 def my(name): #函数体 return name my('lrx') #name是形参,lrx是实参 不写 ...
- 【学习总结】Master课程 之 虚拟化与云计算
Section 1- Cloud Computing Introduction-云计算介绍 1-What can Cloud Computing do? - 云计算可以做什么? 服务模式:美国国家标准 ...
- 认识Debian
Debian -- 通用操作系统https://www.debian.org/ DebianStretch - Debian Wikihttps://wiki.debian.org/DebianStr ...
- node-sass下载失败 关于webpack
安装node-sass提示没有vendor目录的解决办法在node-sass目录下面新建一个vendor的空目录,然后运行npm/cnpm rebuild node-sass --save-dev即可 ...
- React-Native之轮播组件looped-carousel的介绍与使用
React-Native之轮播组件looped-carousel的介绍与使用 一,关于react-native轮播组件的介绍与对比 1,react-native-swiper在动态使用网页图片,多张图 ...
- React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)
React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton) 一,需求与简单介绍 在开发项目时发现RN没有给提供RadioButton和Rad ...
- Windows10 等 administrator 打开IE 或者edge的方法
gpedit.msc 组策略处理即可