1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <cstdio> using namespace std; typedef long long LL; const int mod = 1e9 + 7; const int N = 3; template <class Type> void op (Type &x, Type &a, Type &b) { (x += (LL)a * b % mod) %= mod; } template <class Type> struct Matrix { int n, m; Type w[N][N]; Matrix() { } Matrix(int x, int y) { n = x, m = y; } Matrix(int x, int y, Type k) { n = x, m = y; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) w[i][j] = i == j ? k : 0; } friend Matrix operator*(Matrix x, Matrix y) { Matrix res(x.n, y.m, Type(0)); for (int i = 1; i <= res.n; i++) for (int j = 1; j <= res.m; j++) for (int k = 1; k <= x.m; k++) op(res.w[i][j], x.w[i][k], y.w[k][j]); return res; } friend Matrix operator^(Matrix b, int k) { Matrix res(b.n, b.m, Type(1)); for (; k; k >>= 1, b = b * b) if (k & 1) res = res * b; return res; } }; int main () { char c; while ((c = getchar()) < '0' || c > '9') c = getchar(); int n = c - '0'; while ((c = getchar()) >= '0' && c <= '9') n = (10ll * n + c - '0') % (mod - 1); Matrix <int> A(1, 2), B(2, 2); A.w[1][1] = 1, A.w[1][2] = 2; B.w[1][1] = 0, B.w[1][2] = 1, B.w[2][1] = 1, B.w[2][2] = 2; printf("%d", (A * (B ^ n - 1)).w[1][1]); return 0; }
|