CodeForces - 450B Jzzhu and Sequences

题目描述:

Jzzhu has invented a kind of sequences, they meet the following property:

You are given x and y, please calculate f_{n} modulo 1000000007 (10^{9} + 7).

Input

The first line contains two integers x and y (|x|, |y| ≤ 10^{9}). The second line contains a single integer n (1 ≤ n ≤ 2\cdot 10^{9}).

Output

Output a single integer representing f_{n} modulo 1000000007 (10^{9} + 7).

Examples

Input

2 3
3

Output

1

Input

0 -1
2

Output

1000000006

Note

In the first sample, f_{2} = f_{1}+f_{3}, 3 = 2 + f_{3}f_{3} = 1.

In the second sample, f_{2} =  - 1;  - 1 modulo (10^{9} + 7) equals (10^{9} + 6).

解题报告:

1:将所有取模换为   (x%mod+mod)%mod,接下来就是套模板了。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2;
const ll mod = 1000000007;
struct matrix{
    ll num[N][N];
}A, B;
void init(){
    memset(A.num, 0, sizeof(A.num));
    memset(B.num, 0, sizeof(B.num));
    A.num[0][0] =  A.num[1][0] = 1;A.num[0][1] = -1;
}
matrix mul(matrix AA, matrix BB){
    matrix C;
    memset(C.num, 0, sizeof(C.num));
    for(ll i=0; i<N; ++i){
        for(ll j=0; j<N; ++j){
            for(ll k=0; k<N; ++k){
                C.num[i][j] += AA.num[i][k]*BB.num[k][j];
                C.num[i][j] = (C.num[i][j]%mod+mod)%mod;
            }
        }
    }
    return C;
}
ll Pow(ll n, ll x, ll y){
    B.num[0][0] = y%mod, B.num[1][0] = x%mod;
    while(n){
        if(n&1)B = mul(A, B);
        n >>= 1, A = mul(A, A);
    }
    return (B.num[0][0]%mod+mod)%mod;
}
int main(){
    ll n, x, y;
    scanf("%lld%lld%lld", &x, &y, &n);
    init();
    if(n == 1){
        printf("%lld\n", (x%mod+mod)%mod);
        return 0;
    }
    if(n == 0){
        printf("%lld\n", ((x-y)%mod+mod)%mod);
        return 0;
    }
    printf("%lld\n", Pow(n-2, x, y));
    return 0;
}

 

发布了156 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/jun_____/article/details/104072824