結果

問題 No.1275 綺麗な式
ユーザー firiexpfiriexp
提出日時 2020-10-30 21:35:33
言語 C++17
(gcc 11.2.0 + boost 1.78.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 5,195 bytes
コンパイル時間 908 ms
使用メモリ 9,760 KB
最終ジャッジ日時 2023-02-21 11:15:09
合計ジャッジ時間 3,349 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
9,652 KB
testcase_01 AC 1 ms
7,680 KB
testcase_02 AC 1 ms
7,652 KB
testcase_03 AC 2 ms
7,680 KB
testcase_04 AC 1 ms
7,564 KB
testcase_05 AC 2 ms
7,608 KB
testcase_06 AC 1 ms
9,696 KB
testcase_07 AC 1 ms
7,656 KB
testcase_08 AC 1 ms
9,696 KB
testcase_09 AC 1 ms
9,704 KB
testcase_10 AC 1 ms
9,656 KB
testcase_11 AC 1 ms
7,608 KB
testcase_12 AC 1 ms
9,672 KB
testcase_13 AC 2 ms
7,612 KB
testcase_14 AC 1 ms
9,692 KB
testcase_15 AC 1 ms
9,708 KB
testcase_16 AC 1 ms
9,716 KB
testcase_17 AC 2 ms
9,696 KB
testcase_18 AC 2 ms
7,576 KB
testcase_19 AC 1 ms
7,652 KB
testcase_20 AC 1 ms
7,656 KB
testcase_21 AC 1 ms
7,584 KB
testcase_22 AC 2 ms
9,696 KB
testcase_23 AC 1 ms
9,700 KB
testcase_24 AC 1 ms
7,668 KB
testcase_25 AC 1 ms
7,608 KB
testcase_26 AC 2 ms
7,604 KB
testcase_27 AC 1 ms
7,652 KB
testcase_28 AC 1 ms
7,576 KB
testcase_29 AC 2 ms
7,592 KB
testcase_30 AC 1 ms
9,756 KB
testcase_31 AC 2 ms
7,656 KB
testcase_32 AC 1 ms
9,696 KB
testcase_33 AC 1 ms
9,672 KB
testcase_34 AC 1 ms
7,652 KB
testcase_35 AC 2 ms
7,684 KB
testcase_36 AC 2 ms
7,596 KB
testcase_37 AC 1 ms
7,576 KB
testcase_38 AC 2 ms
9,700 KB
testcase_39 AC 1 ms
9,712 KB
testcase_40 AC 2 ms
9,680 KB
testcase_41 AC 1 ms
9,760 KB
testcase_42 AC 1 ms
7,564 KB
testcase_43 AC 1 ms
9,704 KB
testcase_44 AC 1 ms
9,652 KB
testcase_45 AC 2 ms
9,700 KB
testcase_46 AC 2 ms
9,684 KB
testcase_47 AC 2 ms
7,608 KB
testcase_48 AC 1 ms
7,608 KB
testcase_49 AC 2 ms
9,704 KB
testcase_50 AC 1 ms
7,580 KB
testcase_51 AC 2 ms
7,724 KB
testcase_52 AC 1 ms
9,700 KB
testcase_53 AC 1 ms
7,608 KB
testcase_54 AC 1 ms
7,608 KB
testcase_55 AC 1 ms
7,568 KB
testcase_56 AC 1 ms
7,592 KB
testcase_57 AC 1 ms
9,712 KB
testcase_58 AC 1 ms
7,680 KB
testcase_59 AC 1 ms
7,656 KB
testcase_60 AC 1 ms
9,672 KB
testcase_61 AC 1 ms
7,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

template <u32 M>
struct modint {
    u32 val;
public:
    static modint raw(int v) { modint x; x.val = v; return x; }
    modint() : val(0) {}
    template <class T>
    modint(T v) { ll x = (ll)(v%(ll)(M)); if (x < 0) x += M; val = u32(x); }
    modint(bool v) { val = ((unsigned int)(v) % M); }
    modint& operator++() { val++; if (val == M) val = 0; return *this; }
    modint& operator--() { if (val == 0) val = M; val--; return *this; }
    modint operator++(int) { modint result = *this; ++*this; return result; }
    modint operator--(int) { modint result = *this; --*this; return result; }
    modint& operator+=(const modint& b) { val += b.val; if (val >= M) val -= M; return *this; }
    modint& operator-=(const modint& b) { val -= b.val; if (val >= M) val += M; return *this; }
    modint& operator*=(const modint& b) { u64 z = val; z *= b.val; val = (u32)(z % M); return *this; }
    modint& operator/=(const modint& b) { return *this = *this * b.inv(); }
    modint operator+() const { return *this; }
    modint operator-() const { return modint() - *this; }
    modint pow(long long n) const { modint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }
    modint inv() const { return pow(M-2); }
    friend modint operator+(const modint& a, const modint& b) { return modint(a) += b; }
    friend modint operator-(const modint& a, const modint& b) { return modint(a) -= b; }
    friend modint operator*(const modint& a, const modint& b) { return modint(a) *= b; }
    friend modint operator/(const modint& a, const modint& b) { return modint(a) /= b; }
    friend bool operator==(const modint& a, const modint& b) { return a.val == b.val; }
    friend bool operator!=(const modint& a, const modint& b) { return a.val != b.val; }
};
using mint = modint<MOD>;

template<class H, size_t SIZE>
struct SquareMatrix {
    using T = typename H::T;
    using ar = array<T, SIZE>;
    using mat = array<ar, SIZE>;
    mat A;
    SquareMatrix() = default;
    static SquareMatrix I(){
        SquareMatrix X;
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                if(i == j) X[i][j] = H::one();
                else X[i][j] = H::zero();
            }
        }
        return X;
    }
    static SquareMatrix O(){
        SquareMatrix X;
        for (auto &&i : X.A) fill(i.begin(),i.end(), H::zero());
        return X;
    }

    friend ar operator*=(ar &x, const SquareMatrix &Y) {
        ar ans;
        fill(ans.begin(),ans.end(), H::zero());
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                H::add(ans[j], H::mul(x[i], Y[i][j]));
            }
        }
        x.swap(ans);
        return x;
    }
    friend ar operator*(ar x, const SquareMatrix &Y) { return x *= Y; }

    inline const ar &operator[](int k) const{ return (A.at(k)); }
    inline ar &operator[](int k) { return (A.at(k)); }
    SquareMatrix &operator+= (const SquareMatrix &B){
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                H::add((*this)[i][j], B[i][j]);
            }
        }
        return (*this);
    }

    SquareMatrix &operator-= (const SquareMatrix &B){
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                H::add((*this)[i][j], -B[i][j]);
            }
        }
        return (*this);
    }

    SquareMatrix &operator*=(const SquareMatrix &B) {
        SquareMatrix C = O();
        for (int i = 0; i < SIZE; ++i) {
            for (int k = 0; k < SIZE; ++k) {
                for (int j = 0; j < SIZE; ++j) {
                    H::add(C[i][j],  H::mul((*this)[i][k], B[k][j]));
                }
            }
        }
        A.swap(C.A);
        return (*this);
    }

    SquareMatrix pow(ll n) const {
        SquareMatrix a = (*this), res = I();
        while(n > 0){
            if(n & 1) res *= a;
            a *= a;
            n >>= 1;
        }
        return res;
    }
    SquareMatrix operator+(const SquareMatrix &B) const {return SquareMatrix(*this) += B;}
    SquareMatrix operator-(const SquareMatrix &B) const {return SquareMatrix(*this) -= B;}
    SquareMatrix operator*(const SquareMatrix &B) const {return SquareMatrix(*this) *= B;}
};

struct SemiRing {
    using T = mint;
    static inline T mul(T x, T y){ return x * y; }
    static inline void add(T &x, T y){ x += y; }
    static inline T one(){ return 1; }
    static inline T zero(){ return 0; }
};

using ar = array<SemiRing::T, 2>;
using mat = SquareMatrix<SemiRing, 2>;

int main() {
    int a, b; ll n;
    cin >> a >> b >> n;
    ar x;
    x[0] = 1; x[1] = 0;
    mat A;
    A[0][0] = a, A[1][0] = b;
    A[0][1] = 1; A[1][1] = a;
    A = A.pow(n);
    cout << ((x*A)[0]*2).val << "\n";
    return 0;
}
0