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

思路:构建矩阵

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
ll x,y;
ll f[2][2];
ll tmp[2][2];
ll res[2][2],b[2][2];
void multi(ll f[][2],ll b[][2],ll n)
{memset(tmp,0,sizeof(tmp));for(int i=0;i<2;i++)for(int j=0;j<2;j++)for(int k=0;k<2;k++)tmp[i][j]=((tmp[i][j]+f[i][k]*b[k][j])%mod+mod)%mod;for(int i=0;i<2;i++)for(int j=0;j<2;j++)f[i][j]=tmp[i][j];
}
void Pow(ll n)
{memset(res,0,sizeof(res));for(int i=0;i<2;i++)res[i][i]=1;while(n){if(n&1)multi(res,f,n);multi(f,f,n);n>>=1;}
}
int main()
{ll n;while(~scanf("%lld%lld",&x,&y)){f[0][0]=f[1][0]=1;f[0][1]=-1;f[1][1]=0;scanf("%lld",&n);if(n==1){if(x<0){printf("%lld\n",x+mod);}elseprintf("%lld\n",x);continue;}Pow(n-2);ll ans;ans=res[0][0]*y%mod+res[0][1]*x%mod;printf("%lld\n",(ans%mod+mod)%mod);}return 0;
}

Jzzhu and Sequences CodeForces - 450B 矩阵快速幂相关推荐

  1. 矩阵快速幂2Jzzhu and Sequences Recurrences Contemplation! Algebra Reading comprehension

    来!接着写题解,立个flag:矩阵快速幂题解不写完今晚不睡觉! 此题解是接着矩阵快速幂1写的,此篇有哪里写不清楚的地方导致看不懂的可以先看1:https://blog.csdn.net/qq_4536 ...

  2. 【Codeforces Gym - 101635C Macarons 】【矩阵快速幂+状压】【dfs时间换空间】

    [链接] http://codeforces.com/gym/101635/attachments [题意] 求用1*1,1*2的方格填n*m的矩阵的方法数 [知识点] 状压dfs+矩阵快速幂 [分析 ...

  3. (矩阵快速幂)CF102302H. Log Concave Sequences

    CF102302H. Log Concave Sequences 题意&思路: 有n位数,每位只包含0,1,2,要求对于每个位置,ai2>=ai-1*ai+1.求这样的数有多少. 对于答 ...

  4. Codeforces 621E Wet Shark and Block【dp + 矩阵快速幂】

    题意: 有b个blocks,每个blocks都有n个相同的0~9的数字,如果从第一个block选1,从第二个block选2,那么就构成12,问对于给定的n,b有多少种构成方案使最后模x的余数为k. 分 ...

  5. Codeforces 1106F Lunar New Year and a Recursive Sequence 矩阵快速幂,原根转化模意义下对数,BSGS

    文章目录 题意 题解 对数法转指数线性递推 原根与模意义下求对数 拔山盖世! 最终步骤 Problem Origin 狠搞了一个多星期,做出来之后仍然一知半解,写个博客重理思路. 题意 定义序列fff ...

  6. Codeforces 1182E Product Oriented Recurrence 矩阵快速幂

    Product Oriented Recurrence 先化简原式子 c ^ x * f[x]  = c ^ (x-1) * f[x-1] * c ^ (x-2) * f[x-2] * c ^ (x- ...

  7. Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

    传送门 题目 fn=c2∗n−6fn−1fn−2fn−3\begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{alig ...

  8. H - Log Concave Sequences —— 矩阵快速幂

    This way 题意: 你现在有无穷个0,1,2,现在让你构造一个长度为n的串,使得满足以下条件 对于第i位(2<=i<n),a[i]*a[i]>=a[i-1]*a[i+1] 问你 ...

  9. gym 102302 2019 USP-ICMC H-Log Concave Sequences (dp + 矩阵快速幂优化)

    题目: 传送门 思路:       我们可以先写出转移方程,发现该方程是一个不变的递推式,我们考虑用矩阵快速幂来优化这个递推式. 完结撒花- AC_Code #include <iostream ...

最新文章

  1. dubbo服务发布一之服务暴露
  2. hdu3415单调队列
  3. MATLAB优化问题
  4. stagefright框架(四)-Video Buffer传输流程
  5. oracle数据库安装过程中出现主目录不兼容的问题
  6. Unable to parse the date: 2017-12-30 日期格式转化失败
  7. 前端开发知识点解答-CSS-面试
  8. requestmapping注解访问404_开发人员都必须知道的Spring注解概览
  9. java double 存储_可以存储在double中的最大整数
  10. 在 Java 应用程序中定时执行任务
  11. 安装SQL Server 2005中文开发版后,界面变英文改中文解决方法
  12. Air应用:Splus微博,wing微博
  13. 【有利可图网】PS教程:制作水彩头像画
  14. 详细教程-阿里网盘部署本机
  15. 阿里 P10 是怎样的存在?
  16. Power Query简介(超级查询:获取与整理数据)
  17. Hadoop的安装与配置(非常重要)
  18. JAVA设计模式之——抽象工厂模式
  19. then里面调用 await
  20. ARM使用术语清除(flush)和清理(clean)表示对cache的两种基本操作

热门文章

  1. android10 关闭默认输入法的“更正建议”
  2. NYOJ 118 次小生成树
  3. 2021年南京市高考成绩查询,2021年南京市所有的高中排名,南京市高中高考成绩排名出炉...
  4. php登录用户名可以用中文吗,用户名是什么
  5. H265打包成RTP
  6. keepAlive使用方法
  7. 医疗时鲜资讯:在线咨询!=远程医疗?
  8. react-native 接QQ钱包支付的SDK
  9. 地图着色问题课程设计
  10. NC:南农沈其荣、张瑞福团队揭示多样性激发的确定性细菌装配过程限制群落功能...