結果

問題 No.793 うし数列 2
ユーザー algon_320algon_320
提出日時 2019-02-22 21:50:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 4,072 bytes
コンパイル時間 2,104 ms
コンパイル使用メモリ 172,284 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-16 17:53:45
合計ジャッジ時間 2,971 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define FORE(x,c) for(auto &&x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define INSEG(l,x,r) ((l)<=(x)&&(x)<(r))
#define dump(...)
#define pb push_back
#define _ 0
const signed INF_=1001001001; const long long INF=1001001001001001001LL;
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
    is>>p.first>>p.second;return is;}
template<class T, class U> bool chmax(T &a,const U &b){return a<b?a=b,1:0;}
template<class T, class U> bool chmin(T &a,const U &b){return a>b?a=b,1:0;}
template <class T> void PSUM(T& c) {partial_sum(begin(c), end(c), begin(c));}
template<class T> using heap=priority_queue<T,vector<T>,greater<T>>;
struct before_main_function {
    before_main_function() {
        cin.tie(0); ios::sync_with_stdio(0);
        cout << setprecision(15) << fixed;
        // #define endl "\n"
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

template <class SR>
struct Matrix {
    using T = typename SR::T;
    vector<vector<T>> data;

    Matrix() : Matrix(1, 1, SR::zero()) {}
    Matrix(vector<vector<T>> &&A) : data(A) {}
    Matrix(int r, int c, T fill) : data(r, vector<T>(c, fill)) {}

    int rows() const { return data.size(); }
    int cols() const { return data[0].size(); }

    static Matrix<SR> multiple(const Matrix<SR> &A, const Matrix<SR> &B) {
        assert(A.cols() == B.rows());
        Matrix<SR> ret(A.rows(), B.cols(), SR::zero());
        for (int i = 0; i < A.rows(); ++i) {
            for (int j = 0; j < B.cols(); ++j) {
                T tmp = SR::zero();
                for (int k = 0; k < A.cols(); ++k) {
                    T p = SR::mul(A.data[i][k], B.data[k][j]);
                    tmp = SR::add(tmp, p);
                }
                ret.data[i][j] = tmp;
            }
        }
        return ret;
    }

    static Matrix<SR> pow(Matrix<SR> A, int k) {
        assert(A.rows() == A.cols());
        int n = A.rows();
        Matrix<SR> ret(n, n, SR::zero());
        for (int i = 0; i < n; ++i) ret.data[i][i] = SR::identity();
        while (k > 0) {
            if (k & 1) ret = Matrix<SR>::multiple(ret, A);
            A = Matrix<SR>::multiple(A, A);
            k >>= 1;
        }
        return ret;
    }

    Matrix<SR> operator*(const Matrix<SR> &r) {
        return Matrix<SR>::multiple(*this, r);
    }
    Matrix<SR>& operator*=(const Matrix<SR> &r) {
        return *this = *this * r;
    }
    Matrix<SR> operator^(int k) {
        return Matrix<SR>::pow(*this, k);
    }
    Matrix<SR>& operator^=(int k) {
        return *this = *this ^ k;
    }
};

const int MOD = 1e9 + 7;
struct SemiRing {
    using T = int;
    static T add(const T &a, const T &b) {
        return (a + b) % MOD;
    }
    static T mul(const T &a, const T &b) {
        return (a * b) % MOD;
    }
    static T zero() {
        return 0;
    }
    static T identity() {
        return 1;
    }
};
signed main() {
    int N;
    cin >> N;
    if (N == 1) {
        cout << 13 << endl;
        return 0;
    }

    Matrix<SemiRing> A(2, 2, 0);
    A.data[0][0] = 10;
    A.data[0][1] = 3;
    A.data[1][1] = 1;
    Matrix<SemiRing> b(2, 1, 0);
    b.data[0][0] = 13;
    b.data[1][0] = 1;
    A ^= N - 1;
    auto ans = A * b;
    cout << ans.data[0][0] << endl;
    return (0^_^0);
}

0