結果

問題 No.891 隣接3項間の漸化式
ユーザー 👑 NachiaNachia
提出日時 2020-10-31 12:01:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,791 bytes
コンパイル時間 1,724 ms
コンパイル使用メモリ 170,068 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 10:19:44
合計ジャッジ時間 3,729 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

template<ULL M>
struct static_modint {
    ULL x;
    static_modint(ULL val = 0) : x(val) {}

    static_modint& operator+=(static_modint r) { x += r.x; if (x >= M) x -= M; return *this; }
    static_modint operator+(static_modint r) const { static_modint res = x; return res += r; }
    static_modint& operator-=(static_modint r) { x += M - r.x; if (x >= M) x -= M; return *this; }
    static_modint operator-(static_modint r) const { static_modint res = x; return res -= r; }
    static_modint& operator*=(static_modint r) { x = x * r.x % M; return *this; }
    static_modint operator*(static_modint r) const { return static_modint(x * r.x % M); }
    static_modint pow(ULL r) const {
        if (r == 0) return static_modint(1);
        static_modint res = pow(r / 2);
        res *= res;
        if (r % 2) res *= *this;
        return res;
    }
    static_modint& operator/=(static_modint r) { *this *= r.pow(M - 2); return *this; }
    static_modint operator/(static_modint r) const { return *this * (r.pow(M - 2)); }
    ULL& operator*() { return x; }
    const ULL& operator*() const { return x; }
    bool operator==(static_modint r) const { return x == r; }
    bool operator!=(static_modint r) const { return x != r; }
};

template<class Elem, size_t matrix_sz>
struct static_matrix {
    Elem X[matrix_sz][matrix_sz] = {};
    static static_matrix id() { static_matrix res; rep(i, matrix_sz) res[i][i] = 1; return res; }
    Elem* operator[](int x) { return X[x]; }
    const Elem* operator[](int x) const { return X[x]; }
    static_matrix operator+(const static_matrix& r) const {
        static_matrix res;
        rep(i, matrix_sz) rep(j, matrix_sz)
            res[i][j] = X[i][j] + r[i][j];
        return res;
    }
    static_matrix operator-(const static_matrix& r) const {
        static_matrix res;
        rep(i, matrix_sz) rep(j, matrix_sz)
            res[i][j] = X[i][j] - r[i][j];
        return res;
    }
    static_matrix operator*(const static_matrix& r) const {
        static_matrix res;
        rep(i, matrix_sz) rep(j, matrix_sz) rep(k, matrix_sz)
            res[i][j] += X[i][k] * r[k][j];
        return res;
    }
    static_matrix pow(ULL N) const {
        if (N == 0) return id();
        static_matrix res = pow(N / 2);
        res = res * res;
        if (N % 2 == 1) res = res * *this;
        return res;
    }
};

const ULL M = 1000000007;
using MLL = static_modint<M>;
using Mat = static_matrix<MLL, 2>;
Mat G;

int main() {
    MLL a, b; ULL N; cin >> *a >> *b >> N;
    G[0][0] = a;
    G[1][0] = b;
    G[0][1] = 1;
    G[1][1] = 0;
    G = G.pow(N);

    cout << *G[0][1] << endl;

	return 0;
}
0