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

Description

Jzzhu has invented a kind of sequences, they meet the following property:

\[f_1=x
\]

\[f_2=y
\]

\[f_i=f_{i-1}+f_{i+1}\text {(i>2)}
\]

You are given x and y, please calculate fn modulo 1000000007 (109 + 7).

Input

The first line contains two integers x and y (|x|, |y| ≤ 109). The second line contains a single integer n (1 ≤ n ≤ 2·109).

Output

Output a single integer representing fn modulo 1000000007 (109 + 7).

Sample Input

Input1:

2 3

3

Input2:

0 -1

2

Sample Output

Output1:

1

Output2:

1000000006

Http

CodeForces:https://vjudge.net/problem/CodeForces-450B

Source

矩阵,递推

题目大意

给定f1和f2,要按照$$f_i=f_{i-1}+f_{i+1}\text {(i>2)}$$求出fn

解决思路

刚看到递推式子的时候有些摸不着头脑,这个要推出fi要先推出fi-1和fi+1,怎么做呢?

其实很简单,把式子变一下形:

\[f_{i+1}=f_i-f_{i-1}
\]

于是我们就得到:

\[f_i=f_{i-1}-f_{i-2}
\]

当然,看到题目的数据范围,直接这么傻推是不行的,我们要用矩阵优化!

关于矩阵的基本内容请到我的这一篇文章阅读。

经过推理,我们可以得到本题的转移矩阵T是:

\[T=\begin{bmatrix} 1 & 1 \\ -1 & 0 \end{bmatrix}
\]

然后直接用矩阵快速幂就可以了。

最后要注意的一点就是,题目中给出的x和y都有可能是负数,并且求解过程中也有可能出现负数(因为有减法操作嘛),所以在取膜的时候,要先+Mod,再%Mod(具体请看代码中,已经标记出来了)

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std; #define ll long long//为了防止越界,统一全部用long long存
const ll Mod=1000000007; class Matrix//定义一个矩阵结构体
{
public:
ll A[2][2];
Matrix()//初始化。为了方便下面的运算,这里定义两种初始化,一种是直接全部为0,另一种是用一个二维数组给定其初值
{
memset(A,0,sizeof(A));
}
Matrix(ll arr[2][2])
{
for (int i=0;i<2;i++)
for (int j=0;j<2;j++)
A[i][j]=arr[i][j];
}
}; Matrix operator * (Matrix A,Matrix B)//重载乘法运算
{
Matrix Ans;
for (int i=0;i<2;i++)
for (int j=0;j<2;j++)
for (int k=0;k<2;k++)
Ans.A[i][j]=(Ans.A[i][j]+(A.A[i][k]*B.A[k][j]+Mod)%Mod+Mod)%Mod;//注意这里的取膜操作,防止了负数越界
return Ans;
} int main()
{
ll x,y,n;
cin>>x>>y>>n;
x=(x+Mod)%Mod;//这里因为x和y再输入的时候就有可能是负数(比如样例2),所以先取一次膜
y=(y+Mod)%Mod;
if (n==1)//n为1和2的时候直接输出
{
cout<<x<<endl;
return 0;
}
if (n==2)
{
cout<<y<<endl;
return 0;
}
ll a[2][2]={{y,x},{0,0}};
ll b[2][2]={{1,1},{-1,0}};
Matrix A(a);
Matrix B(b);
n=n-2;//这里要-2是因为1和2的已经处理了,现在只要乘以n-2次
while (n!=0)//矩阵快速幂
{
if (n&1)
A=A*B;
B=B*B;
n=n>>1;
}
cout<<A.A[0][0]<<endl;
return 0;
}

CodeForces 450B Jzzhu and Sequences (矩阵优化)的更多相关文章

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

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

  2. CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

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

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

  4. CodeForces 450B Jzzhu and Sequences

    矩阵快速幂. 首先得到公式 然后构造矩阵,用矩阵加速 取模函数需要自己写一下,是数论中的取模. #include<cstdio> #include<cstring> #incl ...

  5. codeforces 450B. Jzzhu and Sequences 解题报告

    题目链接:http://codeforces.com/problemset/problem/450/B 题目意思:给出 f1 和 f2 的值,以及n,根据公式:fi = fi-1 + fi+1,求出f ...

  6. CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD

    题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...

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

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

  8. Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律

    Jzzhu has invented a kind of sequences, they meet the following property: You are given x and y, ple ...

  9. Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...

随机推荐

  1. 20155217《网络对抗》Exp05 MSF基础应用

    20155217<网络对抗>Exp05 MSF基础应用 实践内容 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 一个主动攻击实践,如ms ...

  2. 20155229《网络对抗技术》Exp8:Web基础

    实验内容 (1).Web前端HTML 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. (2).Web前端javascipt 理解JavaS ...

  3. linux 定时器原理

    内核定时器:    unsigned long timeout = jiffies + (x * HZ);    while(1) {        // Check the condition.   ...

  4. SSIS 事件的向上传递

    在SSIS中,Package是Task组件的有序组合,具有层次结构,Package处于层次结构的顶层(Root Level),对于父子包结构,父包(Parent Package)通过Execute P ...

  5. 详解C#7.0新特性

    1. out 变量(out variables) 以前我们使用out变量必须在使用前进行声明,C# 7.0 给我们提供了一种更简洁的语法 “使用时进行内联声明” .如下所示: 1 var input ...

  6. startActivity时报错Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVI

    原代码如下: Intent intent = new Intent(); intent.setClass(mContext, PhotoView.class); Bundle bundle = new ...

  7. 通过Mysql连接ASP.Net Core2.0(Code First模式)

    ASP.NET Core2.0连接Mysql,首先新建项目 选择Web应用程序 选择需要身份验证: 通过Nuget安装Mysql驱动,这里推荐>Pomelo.EntityFrameworkCor ...

  8. 带WIFI模块布局布线要点。

    带WIFI模块布局布线要求: 1: RF底部不能铺铜要挖空不能有GND否则RF信号会被耦合掉从而无法发送出去. 2:WIFI模块下方不能打孔尽量不走线不打孔避开其他信号穿过下方,要整体的铺铜 3:连接 ...

  9. python图像处理 模式转化简单总结

    图像处理库PIL有九种不同模式:1,L,P,RGB,RGBA,CMYK,YCbCr,I,F 1.模式“1” 模式“1”为二值图像,非黑即白.但是它每个像素用8个bit表示,0表示黑,255表示白. 2 ...

  10. Siki_Unity_2-9_C#高级教程(未完)

    Unity 2-9 C#高级教程 任务1:字符串和正则表达式任务1-1&1-2:字符串类string System.String类(string为别名) 注:string创建的字符串是不可变的 ...