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

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

Examples
input
2 3
3
output
1
input
0 -1
2
output
1000000006
Note

In the first sample, f2 = f1 + f3, 3 = 2 + f3, f3 = 1.

In the second sample, f2 =  - 1;  - 1 modulo (109 + 7) equals (109 + 6).

题意:给出一个递推式和前两项,求第n项模1e9+7后的值。

题解:这题其实本来是很水的..只是最近都在尝试写一些矩阵快速幂的题目,最难的在于化递推式并构造矩阵上,而这道题直接给出了递推式,心痒想使用矩阵。_(:3」∠)_

由f(i)=f(i+1)+f(i-1)可以得出f(i+1)=f(i)-f(i-1)

又由于i>=2,从f(1)开始,于是

f(3)=(1) * f(2) + (-1) * f(1)

f(2)=(1) * f(1) + (0) * f(0)

另外要注意的是,得到的值是负数还得再处理一下。(最近总WA在这上)

 #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#define ll __int64
using namespace std; const int mod = ;
struct matrix
{
ll x[][];
};
matrix mul(matrix a, matrix b)
{
matrix c;
c.x[][] = c.x[][] = c.x[][] = c.x[][] = ;
for( int i = ; i < ; i++)
for(int j = ; j < ; j++)
{
for(int k = ; k < ; k++)
{
c.x[i][j] += a.x[i][k] * b.x[k][j];
}
c.x[i][j] %= mod;
}
return c;
}
matrix powe(matrix x, ll n)
{
matrix r;
r.x[][] = r.x[][] = ; //注意初始化
r.x[][] = r.x[][] = ;
while(n)
{
if(n & )
r = mul(r , x);
x = mul(x , x);
n >>= ;
}
return r;
}
int main()
{ ll x, y, n, ans;
while(~scanf("%I64d%I64d%I64d", &x, &y, &n))
{
if(n == )
printf("%I64d\n",(y%mod + mod)%mod); //负数情况下的考虑
else if(n == )
printf("%I64d\n",(x%mod + mod)%mod);
else
{
matrix d;
d.x[][] = ;
d.x[][] = -;
d.x[][] = ;
d.x[][] = ; d = powe(d, n - );
ans = d.x[][] * y +d.x[][]*x;
printf("%I64d\n", (ans%mod+mod)%mod );
} }
}

Codeforces 450B div.2 Jzzhu and Sequences 矩阵快速幂or规律的更多相关文章

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

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

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

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

  3. Codeforces Round #518 (Div. 1) Computer Game 倍增+矩阵快速幂

    接近于死亡的选手没有水平更博客,所以现在每五个月更一篇. 这道题呢,首先如果已经有权限升级了,那么后面肯定全部选的是 \(p_ib_i\) 最高的. 设这个值为 \(M=\max \limits_i ...

  4. Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)

    题目链接:http://codeforces.com/problemset/problem/678/D 简单的矩阵快速幂模版题 矩阵是这样的: #include <bits/stdc++.h&g ...

  5. hdu 1005 Number Sequence(矩阵快速幂,找规律,模版更通用)

    题目 第一次做是看了大牛的找规律结果,如下: //显然我看了答案,循环节点是48,但是为什么是48,据说是高手打表出来的 #include<stdio.h> int main() { ], ...

  6. 51nod-1537 1537 分解(矩阵快速幂+找规律)

    题目链接: 1537 分解  问(1+sqrt(2)) ^n  能否分解成 sqrt(m) +sqrt(m-1)的形式  如果可以 输出 m%1e9+7 否则 输出no Input 一行,一个数n.( ...

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

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

  8. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  9. Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂

    https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...

随机推荐

  1. 01慕课网《vue.js2.5入门》——基础知识

    前端框架 Vue.js2.5 2018-05-12 Vue官网:https://cn.vuejs.org/ 基础语法+案例实践+TodoList+Vue-cli构建工具+TodoList Vue基础语 ...

  2. 智能客服 对话实现--python aiml包

    利用了python的aiml包进行应答 什么是AIML? AIML是Richard Wallace开发的. 他开发了一个叫A.L.I.C.E(Artificial Linguistics Intern ...

  3. 模拟登入教务处(header)

    import HTMLParser import urlparse import urllib import urllib2 import cookielib import string import ...

  4. Spring学习(七)——增强类

    Spring 切点 什么是切点?切点(Pointcut),每个程序类都拥有多个连接点,如一个拥有两个方法的类,这两个方法都是连接点,即连接点是程序类中客观存在的事物.但在这为数从多的连接点中,如何定位 ...

  5. Mac下使用svn命令

    Mac系统自带svn命令,能够很方便的同步更新代码,使用方法: 1.导入项目svn import /Users/username/Desktop/Project1 svn://192.168.1.12 ...

  6. 【Linux】- 六个超赞的字符画生成器

    ASCII是一个非常吸引人的字符编码系统,在计算机,通讯设备,以及其他设备中,通过它来用代码表示字符.新生代的人可能会觉得它已经过时了,但是那些熟悉它的人会懂得ASCII是多么的独特.我们在这里为你准 ...

  7. 第129天:node.js安装方法

    node.js安装方法 第一步:双击node.js安装包开始安装,注意64位和32位,按照自己的进行安装 第二步:在安装过程中一直选择next,在选择安装目录时,大多数默认安装在C盘,我安装在了D盘, ...

  8. Mining Your Own Business UVALive - 5135(点双联通分量)

    these days I‘m tired!,but very happy... #include<cstdio> #include<cstring> #include<s ...

  9. 【刷题】BZOJ 3172 [Tjoi2013]单词

    Description 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. Input 第一个一个整数N,表示有多少个单词,接下来N ...

  10. 【比赛】HNOI2018 转盘

    通过这题,我发现了我最大的缺陷,就是题目中重要的性质发现不了,所以导致后期根本做不了.还是要多做题,培养思维 对于这道题,来发现性质吧 对于每一条路线,因为它有用的就是最终的时刻,所以我们都可以把它变 ...