結果

問題 No.533 Mysterious Stairs
ユーザー FlkanjinFlkanjin
提出日時 2019-12-11 16:13:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 9,330 bytes
コンパイル時間 1,457 ms
コンパイル使用メモリ 117,100 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-24 07:41:45
合計ジャッジ時間 2,312 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 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,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;
using VD = vector<double>;
using VLL = vector<ll>;
using VS = vector<string>;
using VC = vector<char>;
using VB = vector<bool>;

const int MOD = 1000000007;
const int INF = 1000000000;

template<class T>
bool chmax(T &a, const T &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T>
bool chmin(T &a, const T &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}


template <class T>
class Matrix{
private:
    vector<vector<T>> A;
public:
    Matrix(){}

    Matrix(size_t m, size_t n): A(m, vector<T>(n, 0)) {}

    Matrix(size_t n): A(n, vector<T>(n, 0)) {}

    Matrix(size_t m, size_t n, T a): A(m, vector<T>(n, a)) {}

    Matrix(const Matrix& mat){if(this != &mat) A = mat.A;}

    Matrix(vector<vector<T>> v){
        size_t m = v.size();
        assert(m != 0);
        size_t  n = v[0].size();
        REP(i, m) assert(v[i].size() == n);
        A = v;
    }

    Matrix(vector<T> v){
        size_t m = v.size();
        assert(m != 0);
        A.resize(1);
        A[0] = v;
    }

    ~Matrix() {}

    size_t height() const{return A.size();}

    size_t width() const{
        if(!height()) return 0;
        return A[0].size();
    }

    void setHeight(int h){A.resize(h, vector<T>(width(), 0));}

    void setWidth(int w){
        size_t m = height();
        REP(i, m) A[i].resize(w, 0);
    }

    void setSize(int h, int w){
        setHeight(h); setWidth(w);
    }

    void inputNoChangeSize(){
        size_t m = height(), n = width();
        REP(i, m) REP(j, n) cin >> A[i][j];
    }

    void input(int h, int w){
        setSize(h, w);
        inputNoChangeSize();
    }

    void input(){
        int h, w; cin >> h >> w;
        input(h, w);
    }

    bool sameSize(const Matrix& mat) const{
        return height() == mat.height() && width() == mat.width();
    }

    inline const vector<T> &operator[](int idx) const{return A.at(idx);}

    inline vector<T> &operator[](int k){return (A.at(k));}

    void print2D(int w){
        size_t m = height(), n = width();
        REP(i, m){
            REP(j, n){
                if(j) cout << " ";
                cout << setw(w) << (*this)[i][j];
            }
            cout << "\n";
        }
    }

    void print2D(){
        size_t m = height(), n = width();
        REP(i, m){
            REP(j, n){
                if(j) cout << " ";
                cout << (*this)[i][j];
            }
            cout << "\n";
        }
    }

    friend ostream& operator<<(ostream& os, const Matrix& B){
        size_t m = B.height(), n = B.width();
        REP(i, m){
            REP(j, n){
                if(j) os << " ";
                os << B[i][j];
            }
            os << "\n";
        }
        return (os);
    }

    static Matrix identity(size_t n){
        Matrix ret(n);
        REP(i, n) ret[i][i] = 1;
        return ret;
    }

    Matrix transpose() const{
        size_t n = height(), m = width();
        Matrix ret(m, n);
        REP(i, m) REP(j, n) ret[i][j] = (*this)[j][i];
        return ret;
    }

    Matrix operator+() const{return *this;}

    Matrix operator-() const{
        size_t m = height(), n = width();
        Matrix temp(height(), width());
        REP(i, m) REP(j, n) temp[i][j] = -(*this)[i][j];
        return temp;
    }

    Matrix& operator=(const Matrix& B){
        if(this != &B){
            A = B.A;
        }
        return *this;
    }

    Matrix& operator+=(const Matrix& B){
        assert(sameSize(B));
        size_t m = height(), n = width();
        REP(i, m) REP(j, n) (*this)[i][j] += B[i][j];
        return *this;
    }

    Matrix& operator-=(const Matrix& B){
        assert(sameSize(B));
        size_t m = height(), n = width();
        REP(i, m) REP(j, n) (*this)[i][j] -= B[i][j];
        return *this;
    }

    Matrix& operator*=(const Matrix& B){
        size_t m = height(), n = width(), l = B.width();
        assert(n == B.height());
        vector<vector<T>> C(m, vector<T>(l, 0));
        REP(i, m) REP(j, l) REP(k, n)
            C[i][j] = (C[i][j] + (*this)[i][k] * B[k][j]);
        A.swap(C);
        return *this;
    }

    Matrix& operator*=(const T a){
        size_t m = height(), n = width();
        REP(i, m) REP(j, n) (*this)[i][j] *= a;
        return *this;
    }

    Matrix& operator%=(const ll &mod){
        size_t m = height(), n = width();
        REP(i, m) REP(j, n) (*this)[i][j] %= mod;
        return *this;
    }

    Matrix& operator^=(const Matrix &B){
        assert(sameSize(B));
        size_t m = height(), n = width();
        REP(i, m) REP(j, n) (*this)[i][j] ^= B[i][j];
        return *this;
    }

    Matrix& operator|=(const Matrix &B){
        assert(sameSize(B));
        size_t m = height(), n = width();
        REP(i, m) REP(j, n) (*this)[i][j] |= B[i][j];
        return *this;
    }

    Matrix& operator&=(const Matrix &B){
        size_t m = height(), n = width(), l = B.width();
        assert(n == B.height());
        vector<vector<T>> C(m, vector<T>(l, 0));
        REP(i, m) REP(j, l) REP(k, n)
            C[i][j] = (C[i][j] ^ ((*this)[i][k] & B[k][j]));
        A.swap(C);
        return *this;
    }

    Matrix operator+(const Matrix& mat) const{
        Matrix temp(*this);
        return temp += mat;
    }

    Matrix operator-(const Matrix& mat) const{
        Matrix temp(*this);
        return temp -= mat;
    }

    Matrix operator*(const Matrix& mat) const{
        Matrix temp(*this);
        return temp *= mat;
    }

    Matrix operator*(const T& a) const{
        Matrix temp(*this);
        return temp *= a;
    }

    friend Matrix operator*(T a, const Matrix& B){
        return B * a;
    }

    Matrix operator%(const ll mod) const{
        Matrix temp(*this);
        return temp %= mod;
    }

    Matrix operator^(const Matrix& mat) const{
        Matrix temp(*this);
        return temp ^= mat;
    }

    Matrix operator|(const Matrix& mat) const{
        Matrix temp(*this);
        return temp |= mat;
    }

    Matrix operator&(const Matrix& mat) const{
        Matrix temp(*this);
        return temp &= mat;
    }

    bool operator==(const Matrix& mat) const{
        if(!sameSize(mat)) return false;
        size_t m = height(), n = width();
        REP(i, m) REP(j, n) if((*this)[i][j] != mat[i][j]) return false;
        return true;
    }

    bool operator!=(const Matrix& mat) const{return !(*this == mat);}

    Matrix powWithoutMod(ll b){
        size_t n = height();
        assert(n == width());
        Matrix ret = identity(n);
        Matrix a = *this;
        while(b){
            if(b & 1) ret = ret * a;
            a = a * a;
            b /= 2;
        }
        return ret;
    }

    Matrix power(ll b, ll mod){
        size_t n = height();
        assert(n == width());
        Matrix ret = identity(n);
        Matrix a = *this;
        while(b){
            if(b & 1) ret = (ret * a) % mod;
            a = (a * a) % mod;
            b /= 2;
        }
        return ret;
    }

    Matrix power(ll b) {
        return power(b, MOD);
    }

    Matrix powXorAnd(ll b){
        size_t n = height();
        assert(n == width());
        Matrix ret = identity(n);
        ret *= -1;
        Matrix a = *this;
        while(b){
            if(b & 1) ret = ret & a;
            a = a & a;
            b /= 2;
        }
        return ret;
    }
};

ll solve(int n){
    if(n == 1) return 1;
    if(n == 2) return 1;
    if(n == 3) return 3;
    Matrix<ll> A(9);
    A[0][1] = A[0][2] = A[1][3] = A[1][5] = A[2][6] = A[2][7] = 1;
    FOR(i, 3, 9) A[i][i-3] = 1;
  //  cout << A << "\n\n";
    A = A.power((ll)n - 3);
 //   cout << A << "\n\n";
    Matrix<ll> v(VLL{1, 1, 1, 0, 1, 0, 1, 0, 0});
    v = v.transpose();
    v = A * v; v %= MOD;
    return ((v[0][0] + v[1][0]) % MOD + v[2][0]) % MOD;
}

int main(){
    int n; cin >> n;
    
    cout << solve(n) << "\n";
    return 0;
}
0