結果

問題 No.891 隣接3項間の漸化式
ユーザー ForestedForested
提出日時 2020-07-08 17:38:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,642 bytes
コンパイル時間 1,187 ms
コンパイル使用メモリ 111,828 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 00:28:10
合計ジャッジ時間 2,459 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 AC 2 ms
6,944 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,944 KB
testcase_33 AC 2 ms
6,940 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 1 ms
6,940 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 AC 2 ms
6,940 KB
testcase_41 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <cmath>
#include <iomanip>
#include <cassert>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr ll INF = 3000000000000000000;

constexpr int MOD = 1000000007;
struct ModInt {
    ll x;
    ModInt (ll a) : x((a % MOD + MOD) % MOD) {}
    ModInt operator- () const {
        return ModInt(-x);
    }
    ModInt &operator+= (const ModInt &a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    ModInt &operator-= (const ModInt &a) {
        if ((x -= a.x) < 0) x += MOD;
        return *this;
    }
    ModInt &operator*= (const ModInt &a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    ModInt operator+ (const ModInt &a) const {
        ModInt res(*this);
        return res += a;
    }
    ModInt operator- (const ModInt &a) const {
        ModInt res(*this);
        return res -= a;
    }
    ModInt operator* (const ModInt &a) const {
        ModInt res(*this);
        return res *= a;
    }
    ModInt pow(ll t) const {
        if (!t) return 1;
        ModInt a = pow(t / 2);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }
    ModInt inv() const {
        return pow(MOD - 2);
    }
    ModInt &operator/= (const ModInt &a) {
        return (*this) *= a.inv();
    }
    ModInt operator/ (const ModInt &a) const {
        ModInt res(*this);
        return res /= a;
    }
    friend istream &operator>> (istream &is, ModInt &a) {
        is >> a.x;
        return is;
    }
    friend ostream &operator<< (ostream &os, const ModInt &a){
        os << a.x;
        return os;
    }
};

template <typename T>
struct SquareMatrix {
    int L;
    vector<vector<T>> M;
    T e, f;
    SquareMatrix(int n, const T &x, const T &y) : L(n), M(n, vector<T>(n, x)), e(x), f(y) {}
    const vector<T> &operator[](int k) const {
        return M[k];
    }
    vector<T> &operator[](int k) {
        return M[k];
    }
    SquareMatrix &operator+=(const SquareMatrix &x) {
        REP(i, L) REP(j, L) (*this)[i][j] += x[i][j];
        return (*this);
    }
    SquareMatrix &operator-=(const SquareMatrix &x) {
        REP(i, L) REP(j, L) (*this)[i][j] -= x[i][j];
        return (*this);
    }
    SquareMatrix &operator*=(const SquareMatrix &x) {
        SquareMatrix res(L, e, f);
        REP(i, L) REP(j, L) REP(k, L) res[i][j] += ((*this)[i][k] * x[k][j]);
        swap((*this).M, res.M);
        return (*this);
    }
    SquareMatrix &operator^=(ll n) {
        SquareMatrix res(L, e, f);
        REP(i, L) res[i][i] = f;
        while (n) {
            if (n & 1) res *= (*this);
            (*this) *= (*this);
            n >>= 1;
        }
        swap((*this).M, res.M);
        return (*this);
    }
    SquareMatrix operator+(const SquareMatrix &x) const {
        return SquareMatrix(*this) += x;
    }
    SquareMatrix operator-(const SquareMatrix &x) const {
        return SquareMatrix(*this) -= x;
    }
    SquareMatrix operator*(const SquareMatrix &x) const {
        return SquareMatrix(*this) *= x;
    }
    SquareMatrix operator^(ll n) const {
        return SquareMatrix(*this) ^= n;
    }
};

int main() {
    int a, b, n;
    cin >> a >> b >> n;
    SquareMatrix<ModInt> M(2, ModInt(0), ModInt(1));
    M[0][0] = ModInt(a);
    M[0][1] = ModInt(b);
    M[1][0] = ModInt(1);
    M[1][1] = ModInt(0);
    M ^= n;
    cout << M[1][0] << "\n";
    return 0;
}
0