From e92d65cbe0651420a63421b1b98c64b80c71d27f Mon Sep 17 00:00:00 2001 From: Ren Baoshuo Date: Fri, 20 Aug 2021 18:42:22 +0800 Subject: [PATCH] =?UTF-8?q?1304.=20=E4=BD=B3=E4=BD=B3=E7=9A=84=E6=96=90?= =?UTF-8?q?=E6=B3=A2=E9=82=A3=E5=A5=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/7249810/ --- AcWing/1304/1304.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 AcWing/1304/1304.cpp diff --git a/AcWing/1304/1304.cpp b/AcWing/1304/1304.cpp new file mode 100644 index 00000000..f55fb679 --- /dev/null +++ b/AcWing/1304/1304.cpp @@ -0,0 +1,37 @@ +#include + +using namespace std; + +int n, m, k; + +void mul(int c[][4], int a[][4], int b[][4]) { + int t[4][4]; + memset(t, 0x00, sizeof(t)); + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + for (int k = 0; k < 4; k++) { + t[i][j] = (t[i][j] + 1ll * a[i][k] * b[k][j]) % m; + } + } + } + memcpy(c, t, sizeof(t)); +} + +int main() { + cin >> n >> m; + k = n - 1; + int f[4][4] = {1, 1, 1, 0}; + int a[4][4] = { + {0, 1, 0, 0}, + {1, 1, 1, 0}, + {0, 0, 1, 1}, + {0, 0, 0, 1}, + }; + while (k) { + if (k & 1) mul(f, f, a); + mul(a, a, a); + k >>= 1; + } + cout << ((1ll * n * f[0][2] - f[0][3]) % m + m) % m << endl; + return 0; +}