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. lintcode-491-回文数

    491-回文数 判断一个正整数是不是回文数. 回文数的定义是,将这个数反转之后,得到的数仍然是同一个数. 注意事项 给的数一定保证是32位正整数,但是反转之后的数就未必了. 样例 11, 121, 1 ...

  2. Struts2:<s:action>的使用

    <s:action name=”actionName” namespace=”/” executeResult=”true”> <s:action>可以在jsp中直接调用act ...

  3. Struts2(三)

    以下内容是基于导入struts2-2.3.32.jar包来讲的 1.全局视图配置 xml标签:<global-results> <result name="error&qu ...

  4. Mac & how to uninstall LANDesk

    Mac & how to uninstall LANDesk http://eddiejackson.net/wp/?p=9036 https://community.ivanti.com/d ...

  5. 深入解析ThreadLocal类

    先了解一下ThreadLocal类提供的几个方法: public T get() { } public void set(T value) { } public void remove() { } p ...

  6. HDU4811_Ball

    又是数学题. 每次放入一个球所得到的的分数为x1+x2(x1表示左边的球中颜色的种数,x2表示右边) 其实如果一个球的数量超过了2,那么剩下的就是一个乘法了. 这个理解很简单,因为超过了2的话,说明最 ...

  7. HDU4497——GCD and LCM

    这个题目挺不错的,看到是通化邀请赛的题目,是一个很综合的数论题目. 是这样的,给你三个数的GCD和LCM,现在要你求出这三个数有多少种可能的情况. 对于是否存在这个问题,直接看 LCM%GCD是否为0 ...

  8. React安装React Devtools调试工具

    在运行一个React项目的时候浏览器控制台会提醒你去安装react devtools调试工具. Download the React DevTools for a better development ...

  9. linux(二) 基本使用命令

    一.常用命令归纳分类 课外网站  http://man.linuxde.net/               http://www.jb51.net/linux/               http ...

  10. [ACM][2018南京预赛]Sum

    一.题面 样例输入: 2 5 8 样例输出: 8 14 二.思路 关键词:线性筛 在Zed的帮助下知道了这是一道线性筛的比较裸的题了.考试过程中肝这道题的时间最久,费了心思找到递推式后,发现根本不是在 ...