From f18427f00ac5d5028c5f93468555a025132ee229 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Tue, 3 May 2022 15:00:43 +0800 Subject: [PATCH] =?UTF-8?q?P1939=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E7=9F=A9=E9=98=B5=E5=8A=A0=E9=80=9F=EF=BC=88=E6=95=B0=E5=88=97?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/75060251 --- Luogu/P1939/P1939.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 Luogu/P1939/P1939.cpp diff --git a/Luogu/P1939/P1939.cpp b/Luogu/P1939/P1939.cpp new file mode 100644 index 00000000..411b52ee --- /dev/null +++ b/Luogu/P1939/P1939.cpp @@ -0,0 +1,85 @@ +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 10; +const int mod = 1e9 + 7; + +class Matrix { + private: + int data[N][N]; + + public: + Matrix() { + memset(data, 0x00, sizeof(data)); + } + + int* operator[](int i) { + return data[i]; + } + + void build() { + int n = 3; + + for (int i = 1; i <= n; i++) { + data[i][i] = 1; + } + } + + Matrix operator*(Matrix b) const { + Matrix c; + + int n = 3; + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + for (int k = 1; k <= n; k++) { + c[i][j] = (c[i][j] + 1ll * data[i][k] * b[k][j] % mod) % mod; + } + } + } + + return c; + } +}; + +Matrix binpow(Matrix a, int k) { + Matrix res; + res.build(); + + while (k) { + if (k & 1) res = res * a; + a = a * a; + k >>= 1; + } + + return res; +} + +int t, n; + +int main() { + std::ios::sync_with_stdio(false); + + cin >> t; + + Matrix base, pow; + + base[1][1] = 1; + base[2][1] = 1; + base[3][1] = 1; + + pow[1][1] = 1, pow[1][2] = 0, pow[1][3] = 1; + pow[2][1] = 1, pow[2][2] = 0, pow[2][3] = 0; + pow[3][1] = 0, pow[3][2] = 1, pow[3][3] = 0; + + while (t--) { + cin >> n; + cout << (base * binpow(pow, n - 1))[1][1] << endl; + } + + return 0; +}