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. SpringServletContainerInitializer的代码流程

    SpringServletContainerInitializer 是spring中的一个class实现了servlet3.0规范的一个接口 implements ServletContainerIn ...

  2. CSS动画--让div动起来

    CSS动画 今天在写代码时候,遇到了css动画效果如何实现的问题,经过查阅和实践,总结出一下结论. transition transition 指定动画变化的对应属性 以及动画的执行时间. 例如:tr ...

  3. 痞子衡嵌入式:深扒IAR启动函数流程之段初始化函数__iar_data_init3实现

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是IAR启动函数流程里的段初始化函数__iar_data_init3实现. 本篇是 <IAR启动函数流程及其__low_level_ ...

  4. Java编程之学习技巧

    **本人博客网站 **IT小神 www.itxiaoshen.com 找到技术点 首先得知道自己要学习技术是什么?不管是来自同事.技术大牛推荐还是通过搜索引擎得到,或者另有出处如.技术交流群.技术论坛 ...

  5. 通过Rainbond的团队管理去管理已有的组织架构

    针对于多团队管理我先列举几个小问题,看看大家有没有共鸣,我们在刚刚接触并使用Rainbond的时候,仅仅创建一个团队,里面创建一大堆应用,看起来特别乱,进行管理的时候呢,也会非常麻烦,尤其是当团队需要 ...

  6. DAS,NAS,SAN,简介

    根据服务器类型分为:封闭系统的存储和开放系统的存储,封闭系统主要指大型机,开放系统指基于Windows.UNIX.Linux等操作系统的服务器;开放系统的存储分为:内置存储和外挂存储;外挂存储根据连接 ...

  7. R语言与医学统计图形-【14】ggplot2几何对象之直方密度图

    ggplot2绘图系统--几何对象之直方图.密度图 1.直方图 参数. geom_histogram(mapping = , data = , stat = 'bin', #统计变换,概率密度为den ...

  8. 【Python小试】根据外显子位置生成CDS序列

    已知 genomic_dna.txt TCGATCGTACCGTCGACGATGCTACGATCGTCGATCGTAGTCGATCATCGATCGATCGACTGATCGATCGATCGATCGATC ...

  9. socket编程:多路复用I/O服务端客户端之select

    其实在之前的TCP之中,我们编程实现了多进程,多线程机制下的TCP服务器,但是对于这种的TCP服务器而言,存在太大的资源局限性.所以我们可以是用I/0模型中的多路复用I/O模型来进行编程. 他的具体思 ...

  10. PHP识别二维码(php-zbarcode)

    PHP识别二维码(php-zbarcode) 标签: php二维码扩展 2015-11-06 17:12 609人阅读 评论(0) 收藏 举报  分类: PHP(1)  Linux 版权声明:本文为博 ...