/* -*- coding: utf-8 -*- * * 891.cc: No.891 隣接3項間の漸化式 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MOD = 1000000007; /* typedef */ typedef long long ll; typedef int mat[2][2]; /* global variables */ mat ma, mc; /* subroutines */ void copymat(const mat a, mat b) { memcpy(b, a, sizeof(mat)); } void initmat(mat a) { a[0][0] = a[1][1] = 1, a[0][1] = a[1][0] = 0; } void mulmat(const mat a, const mat b, mat c) { // a * b => c c[0][0] = ((ll)a[0][0] * b[0][0] + (ll)a[0][1] * b[1][0]) % MOD; c[0][1] = ((ll)a[0][0] * b[0][1] + (ll)a[0][1] * b[1][1]) % MOD; c[1][0] = ((ll)a[1][0] * b[0][0] + (ll)a[1][1] * b[1][0]) % MOD; c[1][1] = ((ll)a[1][0] * b[0][1] + (ll)a[1][1] * b[1][1]) % MOD; } void powmat(const mat a, int b, mat c) { // a^b => c mat s, t; initmat(c); copymat(a, s); while (b > 0) { if (b & 1) { mulmat(c, s, t); copymat(t, c); } mulmat(s, s, t); copymat(t, s); b >>= 1; } } /* main */ int main() { int a, b, n; scanf("%d%d%d", &a, &b, &n); if (n <= 1) printf("%d\n", n); else { ma[0][0] = 0; ma[0][1] = 1; ma[1][0] = b; ma[1][1] = a; powmat(ma, n, mc); printf("%d\n", mc[0][1]); } return 0; }