CF450B Jzzhu and Sequences 题解
Content
有一个长度为 \(n\) 的数列 \(\{a_1,a_2,\dots,a_n\}\),满足如下的递推公式:
- \(i=1\) 时,\(a_1=x\)。
- \(i=2\) 时,\(a_2=y\)。
- \(i\geqslant 3\) 时,\(a_i=a_{i-1}+a_{i+1}\)。
求 \(a_n\bmod 10^9+7\) 的值。
数据范围:\(1\leqslant n\leqslant 2\times 10^9\),\(|x|,|y|\leqslant 10^9\)。
Solution
对于 \(i\geqslant 3\),我么不妨将这个式子移项,得到 \(a_{i+1}=a_i-a_{i-1}\)。然后先写下如下式子:
\]
我们发现,当 \(i=7\) 的时候,\(a_7\) 的值又变回了 \(a_1\)。因此我们发现了一个长度为 \(6\) 的循环节。那么 \(a_i\) 也就不难表示出来了:
\]
直接根据这个公式计算 \(a_n\) 即可,即为 \(a_{n\bmod 6}\),注意对负数取模时,先加上模数再去取模。
Code
const int mod = 1e9 + 7;
int f[7];
int main() {
int x = Rint, y = Rint, n = Rint;
f[1] = x, f[2] = y, f[3] = y - x, f[4] = -x, f[5] = -y, f[6] = x - y;
return write((f[(n - 1) % 6 + 1] % mod + mod) % mod), 0;
}
CF450B Jzzhu and Sequences 题解的更多相关文章
- CF450B Jzzhu and Sequences(矩阵加速)
CF450B Jzzhu and Sequences 大佬留言:这.这.不就是矩乘的模板吗,切掉它!! You are given xx and yy , please calculate $f_{n ...
- CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂
题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...
- CodeForces 450B Jzzhu and Sequences (矩阵优化)
CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...
- Codeforces Round #257 (Div. 2 ) B. Jzzhu and Sequences
B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)
题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces450 B. Jzzhu and Sequences
B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 数学 找规律 Jzzhu and Sequences
A - Jzzhu and Sequences Jzzhu has invented a kind of sequences, they meet the following property: ...
- Codeforces Round #257(Div. 2) B. Jzzhu and Sequences(矩阵高速幂)
题目链接:http://codeforces.com/problemset/problem/450/B B. Jzzhu and Sequences time limit per test 1 sec ...
- CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解
思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...
随机推荐
- 一文带你吃透CLR垃圾回收机制
前言 今天我们来共同学习一下CLR的垃圾回收机制,这对我们写出健壮性的代码很有帮助,也许有人会认为多此一举,认为垃圾回收交给CLR就行,我不用关心这个,诚然,大多数情况下是这样的,但是,我们今天讨论的 ...
- Ubuntu下的磁盘管理
采用fat的磁盘存储,插入后采用相同命令会出现sdb和sdb1 sdb:磁盘 sdb1:磁盘分区标号为1 命令 df:显示磁盘使用情况 du:查询某个文件的大小读 du-h 或du -h --max- ...
- C# Pechkin初始化一次后被锁住的问题
Pechkin.dll可用于pdf的生成,常规用法网上都有介绍:https://www.cnblogs.com/felixnet/p/5143934.html 但是当在一个页面上执行过一次之后,再次就 ...
- Redis概述以及Linux安装
Redis 概述 Redis是什么 Redis,Remote Dictionary Server,远程字典服务.是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.key-v ...
- 模数不超过 long long 范围时的快速乘
笔者的话:使用前请确保评测系统的long double严格为16B ! 模数不在 int 范围内的乘法在 OI 中运用广泛,例如Millar-Rabin,Pollard-Rho等等.这样的乘法,直接乘 ...
- Topcoder 14719 - RatingProgressAward(最小割)
题面传送门 神仙最小割--好久没写过网络流了,故写题解以祭之( 首先考虑一个非常 trivial 的问题:如果知道排列顺序之后怎样计算最大值,用脚趾头想一下就能知道是原序列的最大子段和,因为每个课程之 ...
- 洛谷 P4646 - [IOI2007] flood 洪水(拆点+bfs)
题面传送门 一道挺有意思的题(?) orz djq yyds %%%%%%%%%%%%%%%%%% 首先一个很直观的想法是将每个房间看作一个节点,在有墙的房间旁边连边权为 \(1\) 的边然后 bfs ...
- 【Linux】tmux安装(非root)及其使用
tmux(terminal multiplexer)是Linux上的终端复用神器. 1. 安装 (1)下载 下载及其依赖软件. wget -c https://github.com/tmux/tmux ...
- 【模板】滑动窗口最值(单调队列)/洛谷P1886
题目链接 https://www.luogu.com.cn/problem/P1886 题目大意 有一个长为 \(n\) 的序列 \(a\) ,以及一个大小为 \(k\) 的窗口.现在这个从左边开始向 ...
- linux系统中安装JDK
安装之前的准备工作 查看系统中之前安装好的JDK java –version rpm -qa | grep java 卸载JDK (以java-1.7.0-openjdk-1.7.0.45-2.4.3 ...