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

Example

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

题意 : 显然是矩阵快速幂么

坑点 : 就是数论取模这一块 , 稍不注意就错了

const ll mod = 1e9+7;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a struct mat
{
ll a[2][2];
}; mat mul(mat a, mat b){
mat r;
memset(r.a, 0, sizeof(r.a)); for(int i = 0; i < 2; i++){
for(int k = 0; k < 2; k++){
if (a.a[i][k]){
for(int j = 0; j < 2; j++){
if (b.a[k][j]){
r.a[i][j] += (a.a[i][k] * b.a[k][j] + mod)%mod;
r.a[i][j] = (r.a[i][j] + mod)%mod;
}
}
}
}
}
return r;
} mat pow(mat a, int n){
mat b; for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++)
if (i == j) b.a[i][i] = 1;
else b.a[i][j] = 0; while(n){
if (n & 1) b = mul(a, b);
a = mul(a, a);
n >>= 1;
}
return b;
} int main() {
ll x, y, n; scanf("%lld%lld%lld", &x, &y, &n);
mat a;
a.a[0][0] = a.a[1][0] = 1;
a.a[0][1] = -1;
a.a[1][1] = 0; if (n == 1){
printf("%lld\n", (x+mod)%mod);
}
else if (n == 2){
printf("%lld\n", (y+mod)%mod);
}
else {
a = pow(a, n-2);
ll ans = ((a.a[0][0]*y+mod)%mod + (a.a[0][1]*x+mod)%mod + mod)%mod; // 重点就是这里
//ll ans = (a.a[0][0]*y+a.a[0][1]*x+mod)%mod;
//ans = (ans + mod)%mod;
printf("%lld\n", ans );
} return 0;
}
/*
-9 -11
12345
*/

cf 450b 矩阵快速幂(数论取模 一大坑点啊)的更多相关文章

  1. [CQOI2018]交错序列 (矩阵快速幂,数论)

    [CQOI2018]交错序列 \(solution:\) 这一题出得真的很好,将原本一道矩阵快速幂硬生生加入组合数的标签,还那么没有违和感,那么让人看不出来.所以做这道题必须先知道(矩阵快速幂及如何构 ...

  2. HDU6395 Sequence(矩阵快速幂+数论分块)

    题意: F(1)=A,F(2)=B,F(n)=C*F(n-2)+D*F(n-1)+P/n 给定ABCDPn,求F(n) mod 1e9+7 思路: P/n在一段n里是不变的,可以数论分块,再在每一段里 ...

  3. codefroces 450B矩阵快速幂

    找出递推关系式就好了 (fi+1)=(1  -1)(fi  ) (    fi)=(1   0)(fi-1) 不会打矩阵将就着看吧... 这是第一道矩阵快速幂.细节还是有很多没注意到的 本来想看挑战写 ...

  4. 51nod 1197 字符串的数量 V2(矩阵快速幂+数论?)

    接上一篇,那个递推式显然可以用矩阵快速幂优化...自己随便YY了下就出来了,学了一下怎么用LaTeX画公式,LaTeX真是个好东西!嘿嘿嘿 如上图.(刚画错了一发...已更新 然后就可以过V2了 or ...

  5. 51nod 1013 3的幂的和 - 快速幂&除法取模

    题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013 Konwledge Point: 快速幂:https:/ ...

  6. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  7. CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs

    题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  i < k\\ ...

  8. Luogu3824 [NOI2017]泳池 【多项式取模】【递推】【矩阵快速幂】

    题目分析: 用数论分块的思想,就会发现其实就是连续一段的长度$i$的高度不能超过$\lfloor \frac{k}{i} \rfloor$,然后我们会发现最长的非$0$一段不会超过$k$,所以我们可以 ...

  9. 【BZOJ2432】【NOI2011】兔农(数论,矩阵快速幂)

    [BZOJ2432][NOI2011]兔农(数论,矩阵快速幂) 题面 BZOJ 题解 这题\(75\)分就是送的,我什么都不想写. 先手玩一下,发现每次每次出现\(mod\ K=1\)的数之后 把它减 ...

随机推荐

  1. H3C RIP基本配置举例

  2. 2018-2-13-win10-uwp-隐藏实时可视化

    title author date CreateTime categories win10 uwp 隐藏实时可视化 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 ...

  3. H3C 路由度量值(Metric)

  4. P1036 最大公约数

    题目描述 给你两个正整数A和B,求它们的最大公约数. 输入格式 两个正整数 \(A,B(1 \le A,B \le 10^9)\) . 输出格式 一个整数,表示A和B的最大公约数. 样例输入 6 8 ...

  5. 机器学习——EM

    整理自: https://blog.csdn.net/woaidapaopao/article/details/77806273?locationnum=9&fps=1 EM算法是用于含有隐变 ...

  6. 递归&时间模块&os模块

    递归 递归调用 一个函数,调用了自身,称为递归调用 递归函数:一个会调用自身的函数称为递归函数 凡是循环能干的事,递归都能干 方式: 写出临界条件 找这一次和上一次的关系 假设当前函数已经能用,调用自 ...

  7. C# 任务并行

    . List<int> ids = new List<int>(); ; i < ; i++) { ids.Add(i); } ;//最大并行数量 List<Tas ...

  8. 2019-8-31-dotnet-Framework-源代码-·-Ink

    title author date CreateTime categories dotnet Framework 源代码 · Ink lindexi 2019-08-31 16:55:58 +0800 ...

  9. 2019-2-11-WPF-列表自动换行

    title author date CreateTime categories WPF 列表自动换行 lindexi 2019-02-11 08:55:31 +0800 2019-02-11 08:5 ...

  10. Cannot destructure property `createHash` of 'undefined' or 'null'(next服务端渲染引入next-less错误).

    next中引入@zeit/next-less因next版本过低(webpack4之前的版本)无法执行next-less内置的mini-css-extract-plugin mini-css-extra ...