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
由题目中给的 F(i)=F(i-1)+F(i+1) 推导可得 F(i)=F(i-1)-F(i-2)
跟斐波那契数列类似,矩阵快速幂,构造矩阵为 {1,-1,1,0}
#include<bits/stdc++.h>
using namespace std;
const int maxn=3000+5;
const int mod=1e9+7;
typedef long long ll;
ll x,y,n;
struct Matrix {
ll a[2][2];
Matrix() { memset(a, 0, sizeof (a)); }
Matrix operator*(const Matrix &b) const {
Matrix res;
for (int i = 0; i <2; ++i)
for (int j = 0; j <2; ++j)
for (int k = 0; k < 2; ++k)
res.a[i][j] = (res.a[i][j] + a[i][k] * b.a[k][j]) % mod;
return res;
}
} ans, base;
void init() {
base.a[0][0] = base.a[1][0] = 1;
base.a[0][1]=-1;
ans.a[0][0] = ans.a[1][1] = 1;
}
void qpow(int b) {
while (b) {
if (b & 1) ans = ans * base;
base = base * base;
b >>= 1;
}
}
int main(int argc, char const *argv[])
{
scanf("%lld%lld",&x,&y);
scanf("%lld",&n);
if(n==1) printf("%lld\n",(x%mod+mod)%mod);
else if(n==2) printf("%lld\n",(y%mod+mod)%mod);
else
{
init();
qpow(n-2);
ll res=(((ans.a[0][0]*y+ans.a[0][1]*x)%mod)+mod)%mod;
printf("%lld\n",res);
}
return 0;
}