aboutsummaryrefslogtreecommitdiff
path: root/Semestr 4/aisd/themis/FIB.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Semestr 4/aisd/themis/FIB.cpp')
-rw-r--r--Semestr 4/aisd/themis/FIB.cpp58
1 files changed, 58 insertions, 0 deletions
diff --git a/Semestr 4/aisd/themis/FIB.cpp b/Semestr 4/aisd/themis/FIB.cpp
new file mode 100644
index 0000000..dd38c5c
--- /dev/null
+++ b/Semestr 4/aisd/themis/FIB.cpp
@@ -0,0 +1,58 @@
+#include <bits/stdc++.h>
+using namespace std;
+typedef long long ll;
+
+class matrix
+{
+public:
+ ll a, b, c, d;
+ static ll m;
+
+ matrix(ll _a = 0, ll _b = 0, ll _c = 0, ll _d = 0) : a(_a), b(_b), c(_c), d(_d) {}
+
+ matrix operator*(const matrix &M) const
+ {
+ matrix res;
+ res.a = (a * M.a + b * M.c) % m;
+ res.b = (a * M.b + b * M.d) % m;
+ res.c = (c * M.a + d * M.c) % m;
+ res.d = (c * M.b + d * M.d) % m;
+ return res;
+ }
+};
+
+ll matrix::m = 1;
+
+matrix fast_pow(matrix M, int w)
+{
+ matrix res(1, 0, 0, 1);
+ while (w)
+ {
+ if (w % 2 == 1)
+ res = res * M;
+ M = M * M;
+ w >>= 1;
+ }
+ return res;
+}
+
+void solve()
+{
+ int n, m;
+ cin >> n >> m;
+ matrix M(0, 1, 1, 1);
+ matrix::m = m;
+ cout << fast_pow(M, n + 1).a << "\n";
+}
+
+int main()
+{
+ ios_base::sync_with_stdio(false);
+ cin.tie();
+ int t;
+ cin >> t;
+ while (t--)
+ {
+ solve();
+ }
+} \ No newline at end of file