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}\)。然后先写下如下式子:

\[\begin{aligned}a_3=a_2-a_1&=y-x\\a_4=a_3-a_2&=(y-x)-y=-x\\a_5=a_4-a_3&=-x-(y-x)=-y\\a_6=a_5-a_4&=-y-(-x)=x-y\\a_7=a_6-a_5&=x-y-(-y)=x\color{red}=a_1\\a_8=a_7-a_6&=x-(x-y)=y\color{Red}=a_2\end{aligned}
\]

我们发现,当 \(i=7\) 的时候,\(a_7\) 的值又变回了 \(a_1\)。因此我们发现了一个长度为 \(6\) 的循环节。那么 \(a_i\) 也就不难表示出来了:

\[a_i=\begin{cases}x&i\bmod 6=1\\y&i\bmod 6=2\\y-x&i\bmod 6=3\\-x&i\bmod 6=4\\-y&i\bmod 6=5\\x-y&i\bmod 6=0\end{cases}
\]

直接根据这个公式计算 \(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 题解的更多相关文章

  1. CF450B Jzzhu and Sequences(矩阵加速)

    CF450B Jzzhu and Sequences 大佬留言:这.这.不就是矩乘的模板吗,切掉它!! You are given xx and yy , please calculate $f_{n ...

  2. CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂

    题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...

  3. CodeForces 450B Jzzhu and Sequences (矩阵优化)

    CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...

  4. 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 ...

  5. codeforces 450B B. Jzzhu and Sequences(矩阵快速幂)

    题目链接: B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input ...

  6. Codeforces450 B. Jzzhu and Sequences

    B. Jzzhu and Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. 数学 找规律 Jzzhu and Sequences

    A - Jzzhu and Sequences   Jzzhu has invented a kind of sequences, they meet the following property: ...

  8. 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 ...

  9. CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解

    思路: 之前那篇完全没想清楚,给删了,下午一上班突然想明白了. 讲一下这道题的大概思路,应该就明白矩阵快速幂是怎么回事了. 我们首先可以推导出 学过矩阵的都应该看得懂,我们把它简写成T*A(n-1)= ...

随机推荐

  1. C#中使用protobuf-net进行序列化

    前一篇文章我们看到使用Google.Protobuf有诸多不便(参考<如何在C#中使用Google.Protobuf工具>),这次我们来看看另一个工具的使用体验. 相关资料.链接: git ...

  2. Nocalhost 为 KubeSphere 提供更强大的云原生开发环境

    作者简介 张海立(驭势科技云平台研发总监):开源爱好者,云原生社区上海站 PMC 成员,KubeSphere Ambassador:日常云原生领域工作涉及 Kubernetes.DevOps.可观察性 ...

  3. HDU 6987 - Cycle Binary(找性质+杜教筛)

    题面传送门 首先 mol 一发现场 AC 的 csy 神仙 为什么这题现场这么多人过啊啊啊啊啊啊 继续搬运官方题解( 首先对于题目中的 \(k,P\)​,我们有若存在字符串 \(k,P,P'\)​ 满 ...

  4. Atcoder Grand Contest 020 F - Arcs on a Circle(DP+小技巧)

    Atcoder 题面传送门 & 洛谷题面传送门 一道难度 unavailable 的 AGC F 哦 首先此题最棘手的地方显然在于此题的坐标可以为任意实数,无法放入 DP 的状态,也无法直接计 ...

  5. DirectX12 3D 游戏开发与实战第八章内容(下)

    DirectX12 3D 游戏开发与实战第八章内容(下) 8.9.材质的实现 下面是材质结构体的部分代码: // 简单的结构体来表示我们所演示的材料 struct Material { // 材质唯一 ...

  6. 【R】爬虫案例

    爬取豆瓣相册 library(RCurl) library(XML) myHttpheader <- c("User-Agent"="Mozilla/5.0 (Wi ...

  7. 【ThermoRawFileParser】质谱raw格式转换mgf

    众所周知,Proteowizard MSconvert用于质谱原始数据的格式转换,但主要平台是windows,要想在Linux上运行需要打Docker或Wine,对于普通用户来说还是很困难的,想想质谱 ...

  8. Pyquery解析库的安装和使用

    Pyquery同样是一个强大的网页解析工具,它提供了和jQuery类似的语法来解析HTML文档,支持CSS选择器,使用非常方便.GitHub:https://github.com/gawel/pyqu ...

  9. 5分钟6步强制删除kubernetes NameSpace小技巧

    在使用kubernetes过程中,我们经常会遇到无法删除NameSpace的情况,但是如果一一去删除NameSpace中资源比较麻烦.下面我们给大家介绍强制删除NameSpace的方法. 一.查看已存 ...

  10. 禁止点击、禁止button触发【c#】

    bts.Attributes["onclick"] = "return false; ";