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

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,a,b) for(int i=(a); i<(b); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
#define mod 1000000007
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e5 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
#define ll long long
ll x,y,n,ans;
ll a[2][2],b[2][2];
ll res[2][2];
ll e[2][2],c[2][2]; void mul(ll a[2][2], ll b[2][2])
{ ms(c,0);
rep(i,0,2) rep(j,0,2) rep(k,0,2) c[i][j]+=(a[i][k]*b[k][j])%mod, c[i][j]%=mod;
rep(i,0,2) rep(j,0,2) a[i][j]=c[i][j];
} void Pow(ll n)
{ ms(e,0);
rep(i,0,2) e[i][i]=1;
while(n)
{
if(n&1)
mul(e,a);
mul(a,a);
n>>=1;
}
} int main()
{ while(cin>>x>>y>>n)
{
a[0][0]=1;a[0][1]=-1;
a[1][0]=1;a[1][1]=0;
if (n==1)
printf("%lld\n", (x % mod + mod) % mod);
else if(n==2)
printf("%lld\n",(y % mod + mod) % mod);
else{
Pow(n-2);
ll res=e[0][0]*y%mod + e[0][1]*x%mod;
printf("%lld\n", (res%mod+mod)%mod);
}
}
return 0;
}

CodeForces 450B Jzzhu and Sequences 【矩阵快速幂】的更多相关文章

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

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

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

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

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

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

    CodeForces 450B Jzzhu and Sequences (矩阵优化) Description Jzzhu has invented a kind of sequences, they ...

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

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

  6. Educational Codeforces Round 60 D dp + 矩阵快速幂

    https://codeforces.com/contest/1117/problem/D 题意 有n个特殊宝石(n<=1e18),每个特殊宝石可以分解成m个普通宝石(m<=100),问组 ...

  7. Codeforces 1067D - Computer Game(矩阵快速幂+斜率优化)

    Codeforces 题面传送门 & 洛谷题面传送门 好题. 首先显然我们如果在某一次游戏中升级,那么在接下来的游戏中我们一定会一直打 \(b_jp_j\) 最大的游戏 \(j\),因为这样得 ...

  8. codeforces 678D Iterated Linear Function 矩阵快速幂

    矩阵快速幂的题要多做 由题可得 g[n]=A*g[n-1]+B 所以构造矩阵  { g[n] }    =  {A   B}  * { g[n-1]} {   1   }         {0   1 ...

  9. CodeForces 450B Jzzhu and Sequences

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

  10. Educational Codeforces Round 14E. Xor-sequences(矩阵快速幂)

    传送门 题意 给定序列,从序列中选择k(1≤k≤1e18)个数(可以重复选择),使得得到的排列满足\(x_i与x_{i+1}\)异或的二进制表示中1的个数是3的倍数.问长度为k的满足条件的序列有多少种 ...

随机推荐

  1. redis系列文章目录

    redis系列文章目录 使用spring-data-redis实现incr自增 Redis 利用Hash存储节约内存 Redis学习笔记(九)redis实现时时直播列表缓存,支持分页[热点数据存储] ...

  2. 【Perceptron Learning Algorithm】林轩田机器学习基石

    直接跳过第一讲.从第二讲Perceptron开始,记录这一讲中几个印象深的点: 1. 之前自己的直觉一直对这种图理解的不好,老按照x.y去理解. a) 这种图的每个坐标代表的是features:fea ...

  3. NGUI注册事件的三种方式

    1.第一种方式 当一个元素要执行某个方法,而这个方法在此元素赋予的脚本上有,那么直接会调用此方法,但此方法的名称必须是内置的固定名称,例如OnClick,OnMouseOver,OnMouseOut等 ...

  4. android 使用LruCache缓存网络图片

    加载图片,图片如果达到一定的上限,如果没有一种合理的机制对图片进行释放必然会引起程序的崩溃. 为了避免这种情况,我们可以使用Android中LruCache来缓存下载的图片,防止程序出现OOM.   ...

  5. hexo 配置文件 实例

    # Hexo Configuration ## Docs: https://hexo.io/docs/configuration.html ## Source: https://github.com/ ...

  6. 【noip2016d2t3】状压DP+巧妙优化

    题意可以简单这样考虑 给出n^2个集合(每个集合的元素不超过n),包含某个元素的集合至少有n个,选出最少的集合,使这些集合的并包含n个元素 n最大只有18 可以考虑状压n个元素,然后枚举n^2个集合 ...

  7. 2151: 种树 - BZOJ

    Description A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树.园林部门得到指令后,初步规划出n个种树的位置,顺时针编号1到n.并且每个位置都有一个美观度 ...

  8. BZOJ3668 [Noi2014]起床困难综合症 【贪心】

    题目 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争.通过研究相关文献,他找到了该病的发病原因 ...

  9. libaio.so.1: cannot open shared object file

    <pre code_snippet_id="275763" snippet_file_name="blog_20140404_1_5530152" nam ...

  10. POJ3522 Slim Span

    Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7462   Accepted: 3959 Descrip ...