/* -*- coding: utf-8 -*- * * 1275.cc: No.1275 綺麗な式 - 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; struct Mat2 { int a, b, c, d; Mat2(): a(0), b(0), c(0), d(0) {} Mat2(int _a, int _b, int _c, int _d): a(_a), b(_b), c(_c), d(_d) {} Mat2 operator*(const Mat2 m) const { return Mat2(((ll)a * m.a % MOD + (ll)b * m.c % MOD) % MOD, ((ll)a * m.b % MOD + (ll)b * m.d % MOD) % MOD, ((ll)c * m.a % MOD + (ll)d * m.c % MOD) % MOD, ((ll)c * m.b % MOD + (ll)d * m.d % MOD) % MOD); } Mat2 pow(ll n) { Mat2 r(1, 0, 0, 1), a = *this; while (n > 0) { if (n & 1) r = r * a; a = a * a; n >>= 1; } return r; } }; /* global variables */ /* subroutines */ /* main */ // let fn = xn+yn*rb = (a+rb)^n // -> f{n+1} = (a+rb)^(n+1) = (a+rb)^n*(a+rb) // = (xn+yn*rb)*(a+rb) = a*xn+a*yn*rb+xn*rb+yn*rb^2 // = (a*xn+b*yn)+(xn+a*yn)rb // -> x{n+1}=a*xn+b*yn, y{n+1}=xn+a*yn // let gn = xn-yn*rb = (a-rb)^n // -> g{n+1} = (a-rb)^(n+1) = (a-rb)^n*(a-rb) // = (xn-yn*rb)*(a-rb) = a*xn-a*yn*rb-xn*rb+yn*rb^2 // = (a*xn+b*yn)-(xn+a*yn)rb // -> x{n+1}=a*xn+b*yn, y{n+1}=xn+a*yn int main() { int a, b; ll n; scanf("%d%d%lld", &a, &b, &n); Mat2 ma(a, b, 1, a); Mat2 mb = ma.pow(n); printf("%lld\n", 2LL * mb.a % MOD); return 0; }