#pragma region header #include // デバッグ用マクロ:https://naskya.net/post/0002/ #ifdef LOCAL #include #define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (static_cast(0)) #endif using namespace std; using ll = long long; using vi = vector; using vl = vector; using vs = vector; using vc = vector; using vb = vector; using vpii = vector>; using vpll = vector>; using vvi = vector>; using vvl = vector>; using vvc = vector>; using vvb = vector>; using vvvi = vector>>; using pii = pair; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define INT(...) \ int __VA_ARGS__; \ in(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ in(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ in(__VA_ARGS__) #define Sort(a) sort(all(a)) #define VEC(type, name, size) \ vector name(size); \ in(name) [[maybe_unused]] void print() {} template void print(const T& t, const Ts&... ts); template void out(const Ts&... ts) { print(ts...); cout << '\n'; } namespace IO { #define VOID(a) decltype(void(a)) struct S { S() { cin.tie(nullptr)->sync_with_stdio(0); fixed(cout).precision(12); } } S; template struct P : P {}; template <> struct P<0> {}; template void i(T& t) { i(t, P<3>{}); } void i(vector::reference t, P<3>) { int a; i(a); t = a; } template auto i(T& t, P<2>) -> VOID(cin >> t) { cin >> t; } template auto i(T& t, P<1>) -> VOID(begin(t)) { for (auto&& x : t) i(x); } template void ituple(T& t, index_sequence) { in(get(t)...); } template auto i(T& t, P<0>) -> VOID(tuple_size{}) { ituple(t, make_index_sequence::value>{}); } template void o(const T& t) { o(t, P<4>{}); } template void o(const char (&t)[N], P<4>) { cout << t; } template void o(const T (&t)[N], P<3>) { o(t[0]); for (size_t i = 1; i < N; i++) { o(' '); o(t[i]); } } template auto o(const T& t, P<2>) -> VOID(cout << t) { cout << t; } template auto o(const T& t, P<1>) -> VOID(begin(t)) { bool first = 1; for (auto&& x : t) { if (first) first = 0; else o(' '); o(x); } } template void otuple(const T& t, index_sequence) { print(get(t)...); } template auto o(T& t, P<0>) -> VOID(tuple_size{}) { otuple(t, make_index_sequence::value>{}); } #undef VOID } // namespace IO #define unpack(a) \ (void)initializer_list { (a, 0)... } template void in(Ts&... t) { unpack(IO::i(t)); } template void print(const T& t, const Ts&... ts) { IO::o(t); unpack(IO::o((cout << ' ', ts))); } #undef unpack // #define MAX 10000 #define INFTY (1 << 30) // 浮動小数点の誤差を考慮した等式 #define EPS (1e-10) #define equal(a, b) (fabs((a) - (b)) < EPS) template inline bool chmax(T& a, T b) { return ((a < b) ? (a = b, true) : (false)); } template inline bool chmin(T& a, T b) { return ((a > b) ? (a = b, true) : (false)); } #define YESNO(yes, no) \ void yes(bool i = 1) { out(i ? #yes : #no); } \ void no() { out(#no); } YESNO(first, second) YESNO(First, Second) YESNO(Yes, No) YESNO(YES, NO) YESNO(possible, impossible) YESNO(POSSIBLE, IMPOSSIBLE) #pragma endregion header // #include // using namespace atcoder; struct Solver { void solve() { /* input */ INT(N, MOD); // 行列積 auto matMul = [](vvl& A, vvl& B, ll mod = 4e18) { vvl ret(A.size(), vl(B[0].size())); rep(i, A.size()) rep(j, B[0].size()) rep(k, B.size()) { (ret[i][j] += A[i][k] * B[k][j]) %= mod; } return ret; }; // 行列累乗 A^N auto matPow = [matMul](vvl& A, ll n, ll mod = 4e18) { vvl ret(A.size(), vl(A.size())); rep(i, A.size()) ret[i][i] = 1; // 単位行列で初期化 while (n > 0) { if (n & 1) ret = matMul(A, ret, mod); A = matMul(A, A, mod); n >>= 1; } return ret; }; /* solve */ vvl mat(2, vl(2)); mat[0][0] = 1, mat[0][1] = 1, mat[1][0] = 1, mat[1][1] = 0; // {fib(n+2), fib(n+1)} = (mat^n){fib2, fib1} mat = matPow(mat, N - 1, MOD); // {fib(n+2), fib(n+1)} = {{fib(n+1),fib(n)},{fib(n),fib(n-1)}} ll fibN = mat[0][1]; // debug(mat); /* output */ out(fibN); } }; int main() { int ts = 1; rep(ti, ts) { Solver solver; solver.solve(); } return 0; }