結果

問題 No.613 Solitude by the window
ユーザー PachicobuePachicobue
提出日時 2017-12-13 16:34:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,881 bytes
コンパイル時間 2,681 ms
コンパイル使用メモリ 199,524 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-21 02:14:49
合計ジャッジ時間 4,102 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cerr << #x << " = " << x << endl

using namespace std;
using ll = __int128_t;
using ld = long double;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}
template <typename T, std::size_t n>
ostream& operator<<(ostream& os, const array<T, n>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

ll N, M;
struct Matrix {
    Matrix()
    {
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                table[i][j] = 0;
            }
        }
    }
    static Matrix Unit()
    {
        Matrix ans;
        ans.table[0][0] = 1;
        ans.table[1][1] = 1;
        return ans;
    }

    Matrix operator*(const Matrix& mat) const
    {
        Matrix ans;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    ans.table[i][j] += (table[i][k] * mat.table[k][j]) % M;
                    ans.table[i][j] %= M;
                }
            }
        }
        return ans;
    }
    array<array<ll, 2>, 2> table;
};

ll mul(const ll p, const ll n, const ll mod)  //p*n
{
    if (n == 0) {
        return 0;
    }
    if (n % 2 == 1) {
        return (mul(p, n - 1, mod) + p) % mod;
    } else {
        const ll pp = mul(p, n / 2, mod);
        return (pp + pp) % mod;
    }
}
ll power(const ll n, const ll mod)
{
    if (n == 0) {
        return 1;
    }
    if (n % 2 == 1) {
        return (power(n - 1, mod) * 2) % mod;
    } else {
        const ll pp = power(n / 2, mod);
        return mul(pp, pp, mod);
    }
}


Matrix power(const Matrix& mat, const ll n)
{
    if (n == 0) {
        return Matrix::Unit();
    }
    if (n % 2 == 1) {
        return power(mat, n - 1) * mat;
    } else {
        const Matrix pp = power(mat, n / 2);
        return pp * pp;
    }
}


int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    long long N_, M_;
    cin >> N_ >> M_;
    N = N_;
    M = M_;
    if (M == 1) {
        cout << 0 << endl;
    } else {
        Matrix mat;
        mat.table[0][0] = 2;
        mat.table[0][1] = 3;
        mat.table[1][0] = 1;
        mat.table[1][1] = 2;
        const ll mod = M * (M - 1) * (M + 1);
        const Matrix ans = power(mat, power(N, mod));
        cout << (long long)(2 * ans.table[0][0] + M - 2) % (long long)M << endl;
    }
    return 0;
}
0