From f7270d1874dc034ff56de2a07e86fd96411799d3 Mon Sep 17 00:00:00 2001 From: Hiyabye Date: Mon, 15 Jan 2024 21:03:00 +0900 Subject: [PATCH] Add 11444.cpp --- 11xxx/11444.cpp | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/11xxx/11444.cpp b/11xxx/11444.cpp index 66eaddae..5845ca4b 100644 --- a/11xxx/11444.cpp +++ b/11xxx/11444.cpp @@ -1,24 +1,33 @@ #include -#include +#include #define MOD 1000000007 using namespace std; +using Matrix = vector>; -map dp; - -long long f(long long n) { - if (dp.find(n) != dp.end()) return dp[n]; +Matrix matmul(Matrix a, Matrix b) { + Matrix c(2, vector(2)); + for (int i=0; i<2; i++) for (int j=0; j<2; j++) for (int k=0; k<2; k++) { + c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % MOD; + } + return c; +} - dp[n] = (f(n/2+1) * f(n-n/2) + f(n/2) * f(n-n/2-1)) % MOD; - return dp[n]; +Matrix matpow(Matrix base, long long exp) { + Matrix ret(2, vector(2, 0)); + for (int i=0; i<2; i++) ret[i][i] = 1; + while (exp) { + if (exp & 1) ret = matmul(ret, base); + base = matmul(base, base); + exp >>= 1; + } + return ret; } void solve(void) { long long n; cin >> n; - dp[0] = 0; - dp[1] = 1; - dp[2] = 1; - cout << f(n); + Matrix ans = matpow({{1, 1}, {1, 0}}, n); + cout << ans[0][1]; } int main(void) {