結果

問題 No.1258 コインゲーム
ユーザー MisterMister
提出日時 2020-10-23 10:13:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 3,614 bytes
コンパイル時間 695 ms
コンパイル使用メモリ 73,560 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-28 15:11:11
合計ジャッジ時間 9,420 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
4,376 KB
testcase_01 AC 37 ms
4,376 KB
testcase_02 AC 14 ms
4,380 KB
testcase_03 AC 118 ms
4,376 KB
testcase_04 AC 133 ms
4,376 KB
testcase_05 AC 22 ms
4,376 KB
testcase_06 AC 97 ms
4,500 KB
testcase_07 AC 11 ms
4,380 KB
testcase_08 AC 10 ms
4,380 KB
testcase_09 AC 49 ms
4,376 KB
testcase_10 AC 117 ms
4,380 KB
testcase_11 AC 130 ms
4,376 KB
testcase_12 AC 95 ms
4,376 KB
testcase_13 AC 30 ms
4,380 KB
testcase_14 AC 10 ms
4,380 KB
testcase_15 AC 113 ms
4,376 KB
testcase_16 AC 24 ms
4,376 KB
testcase_17 AC 129 ms
4,380 KB
testcase_18 AC 48 ms
4,376 KB
testcase_19 AC 45 ms
4,376 KB
testcase_20 AC 96 ms
4,376 KB
testcase_21 AC 123 ms
4,380 KB
testcase_22 AC 85 ms
4,380 KB
testcase_23 AC 63 ms
4,376 KB
testcase_24 AC 24 ms
4,376 KB
testcase_25 AC 59 ms
4,376 KB
testcase_26 AC 51 ms
4,376 KB
testcase_27 AC 118 ms
4,380 KB
testcase_28 AC 32 ms
4,376 KB
testcase_29 AC 34 ms
4,380 KB
testcase_30 AC 147 ms
4,380 KB
testcase_31 AC 37 ms
4,376 KB
testcase_32 AC 17 ms
4,380 KB
testcase_33 AC 99 ms
4,376 KB
testcase_34 AC 122 ms
4,380 KB
testcase_35 AC 43 ms
4,380 KB
testcase_36 AC 119 ms
4,380 KB
testcase_37 AC 47 ms
4,376 KB
testcase_38 AC 113 ms
4,380 KB
testcase_39 AC 33 ms
4,376 KB
testcase_40 AC 151 ms
4,376 KB
testcase_41 AC 151 ms
4,376 KB
testcase_42 AC 150 ms
4,376 KB
testcase_43 AC 152 ms
4,380 KB
testcase_44 AC 151 ms
4,384 KB
testcase_45 AC 151 ms
4,380 KB
testcase_46 AC 151 ms
4,376 KB
testcase_47 AC 151 ms
4,376 KB
testcase_48 AC 150 ms
4,380 KB
testcase_49 AC 150 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <array>
#include <atcoder/modint>

namespace ac = atcoder;

template <class T, int D>
struct Vector {
    using V = std::array<T, D>;

    V vec;

    // constructor
    Vector(T val = 0) { vec.fill(val); }

    // getter
    T& operator[](int i) { return vec[i]; }
    T operator[](int i) const { return vec[i]; }
    typename V::iterator begin() { return vec.begin(); }
    typename V::iterator end() { return vec.end(); }

    // arithmetic
    Vector operator+(const Vector& v) const { return Vector(*this) += v; }
    Vector operator-(const Vector& v) const { return Vector(*this) -= v; }
    T operator*(const Vector& v) const {
        T ret(0);
        for (int i = 0; i < D; ++i) ret += vec[i] * v[i];
        return ret;
    }

    // compound assignment
    Vector& operator+=(const Vector& v) {
        for (int i = 0; i < D; ++i) vec[i] += v[i];
        return *this;
    }
    Vector& operator-=(const Vector& v) {
        for (int i = 0; i < D; ++i) vec[i] -= v[i];
        return *this;
    }
};

template <class T, int D>
struct Matrix {
    using M = std::array<std::array<T, D>, D>;

    M mat;

    // constructor
    Matrix(T val = 0) {
        for (auto& v : mat) v.fill(val);
    }

    static Matrix id() {
        Matrix m;
        for (int i = 0; i < D; ++i) m[i][i] = 1;
        return m;
    }

    // getter
    std::array<T, D>& operator[](int i) { return mat[i]; }
    std::array<T, D> operator[](int i) const { return mat[i]; }
    typename M::iterator begin() { return mat.begin(); }
    typename M::iterator end() { return mat.end(); }

    // arithmetic
    Matrix operator+(const Matrix& m) const { return Matrix(*this) += m; }
    Matrix operator-(const Matrix& m) const { return Matrix(*this) -= m; }
    Matrix operator*(const Matrix& m) const { return Matrix(*this) *= m; }

    template <class U>
    Matrix pow(U k) {
        Matrix ret = id();
        Matrix a = *this;

        while (k > 0) {
            if (k & 1) ret *= a;
            a *= a;
            k >>= 1;
        }
        return ret;
    }

    // compound assignment
    Matrix& operator+=(const Matrix& m) {
        for (int i = 0; i < D; ++i) {
            for (int j = 0; j < D; ++j) {
                mat[i][j] += m[i][j];
            }
        }
        return *this;
    }
    Matrix& operator-=(const Matrix& m) {
        for (int i = 0; i < D; ++i) {
            for (int j = 0; j < D; ++j) {
                mat[i][j] -= m[i][j];
            }
        }
        return *this;
    }
    Matrix& operator*=(const Matrix& m) {
        M nmat;
        for (auto& v : nmat) v.fill(0);

        for (int i = 0; i < D; ++i) {
            for (int j = 0; j < D; ++j) {
                for (int k = 0; k < D; ++k) {
                    nmat[i][j] += mat[i][k] * m[k][j];
                }
            }
        }
        mat = nmat;
        return *this;
    }

    // arithmetic with vector
    using Vec = Vector<T, D>;
    Vec operator*(const Vec& v) {
        Vec ret;
        for (int i = 0; i < D; ++i) {
            for (int j = 0; j < D; ++j) {
                ret[i] += mat[i][j] * v[j];
            }
        }
        return ret;
    }
};

using mint = ac::modint1000000007;
using Mat = Matrix<mint, 2>;

void solve() {
    int n, m, x;
    std::cin >> n >> m >> x;

    auto mat = Mat::id();
    for (int i = 0; i < 2; ++i) mat[i][1 - i] = m;
    mat = mat.pow(n);

    std::cout << (mat[x][0]).val() << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    int q;
    std::cin >> q;
    while (q--) solve();

    return 0;
}
0