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. 论文翻译:2020_Densely connected neural network with dilated convolutions for real-time speech enhancement in the time domain

    提出了模型和损失函数 论文名称:扩展卷积密集连接神经网络用于时域实时语音增强 论文代码:https://github.com/ashutosh620/DDAEC 引用:Pandey A, Wang D ...

  2. 打开order by的大门,一探究竟《死磕MySQL系列 十二》

    在日常开发工作中,你一定会经常遇到要根据指定字段进行排序的需求. 这时,你的SQL语句类似这样. select id,phone,code from evt_sms where phone like  ...

  3. Python画一个四点连线并计算首尾距离

    import turtle import math #先定义4个坐标 x1,y1=100,100 x2,y2=100,-100 x3,y3=-100,-100 x4,y4=-100,100   #然后 ...

  4. myeclipse激活教程

    1.安装,解压,下一步一下一步,一直到finish..结束 2.汉化破解激活:下载破解压缩包:解压

  5. ES2020新特性链操作符 '?.'和'??'

    ES2020新特性,js中的可选链操作符?. 概述 回想一下,我们是如何访问可能含有空值(null或undefined)属性的嵌套对象,比如访问web api 返回结果的user详情,可以使用嵌套的三 ...

  6. RNA_seq 热图绘制

    若已经拿到表达矩阵exprSet 若差异较大,进行log缩小不同样本的差距 1.热图全体 1 ##加载包 2 library(pheatmap) 3 4 ##缩小表达量差距 5 exprSet < ...

  7. 2019java面试

    1.面向对象的特征有哪些方面?答:面向对象的特征主要有以下几个方面:    抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注 ...

  8. RocketMQ这样做,压测后性能提高30%

    从官方这边获悉,RocketMQ在4.9.1版本中对消息发送进行了大量的优化,性能提升十分显著,接下来请跟着我一起来欣赏大神们的杰作. 根据RocketMQ4.9.1的更新日志,我们从中提取到关于消息 ...

  9. xtrabackup原理

    常用命令 innobackupex --defaults-file=/data/mysql_3306/my.cnf --no-timestamp --slave-info --compress --c ...

  10. Android Loader异步装载

    一.Loader简介: (一).Loader的概念: 装载器从android3.0开始引进.它使得在activity或fragment中异步加载数据变得简单. 当成批显示数据的时候,为了使用户体验更好 ...