結果

問題 No.1340 おーじ君をさがせ
ユーザー Manuel1024Manuel1024
提出日時 2021-03-21 13:41:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,365 bytes
コンパイル時間 674 ms
コンパイル使用メモリ 76,164 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-05-01 22:37:02
合計ジャッジ時間 15,262 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,624 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 883 ms
5,376 KB
testcase_12 AC 122 ms
5,376 KB
testcase_13 AC 662 ms
5,376 KB
testcase_14 AC 1,887 ms
5,376 KB
testcase_15 AC 1,549 ms
5,376 KB
testcase_16 AC 93 ms
5,376 KB
testcase_17 AC 450 ms
5,376 KB
testcase_18 AC 901 ms
5,376 KB
testcase_19 AC 35 ms
5,376 KB
testcase_20 AC 8 ms
5,376 KB
testcase_21 AC 816 ms
5,376 KB
testcase_22 TLE -
testcase_23 AC 829 ms
5,376 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using ll = long long int;

template<typename T>
class Matrix{
private:
    vector<vector<T>> mat;
    int row;
    int column;
public:
    Matrix(int r = 1, int c = 1){
        this->row = r;
        this->column = c;
        this->mat = vector<vector<T>>(this->row, vector<T>(this->column, 0));
    }

    Matrix(vector<vector<T>> initmat){
        this->mat = initmat;
        this->row = this->mat.size();
        this->column = this->mat[0].size();
    }

    vector<T> &operator[](const int rowind){
        return mat[rowind];
    }

    vector<T> operator[](const int rowind) const {
        return mat[rowind];
    }

    const int getrow() const {
        return this->row;
    }

    const int getcol() const {
        return this->column;
    }

    Matrix operator+(const Matrix &other) const {
        if(this->row != other.getrow() || this->column != other.getcol()){
            cerr << "Matrix calc error(add)" << endl;
            abort();
        }
        Matrix ans(this->row, this->column);
        for(int i = 0; i < this->row; i++){
            for(int j = 0; j < this->column; j++) ans[i][j] = this->mat[i][j] + other[i][j];
        }
        return ans;
    }

    Matrix operator-(const Matrix &other) const {
        if(this->row != other.getrow() || this->column != other.getcol()){
            cerr << "Matrix calc error(sub)" << endl;
            abort();
        }
        Matrix ans(this->row, this->column);
        for(int i = 0; i < this->row; i++){
            for(int j = 0; j < this->column; j++) ans[i][j] = this->mat[i][j] - other[i][j];
        }
        return ans;
    }

    Matrix operator*(const Matrix &other) const {
        if(this->column != other.getrow()){
            cerr << "Matrix calc error(mult)" << endl;
            abort();
        }
        Matrix ans(this->row, other.getcol());
        for(int i = 0; i < this->row; i++){
            for(int j = 0; j < other.getcol(); j++){
                for(int k = 0; k < this->column; k++) ans[i][j] += this->mat[i][k] * other[k][j];
            }
        }
        return ans;
    }
};

template<typename T>
Matrix<T> modpow(Matrix<T> a, ll x, ll mod){
    if(a.getrow() != a.getcol()){
        cerr << "Matrix calc error(pow)" << endl;
        abort();
    }
    Matrix<T> ans(a.getrow(), a.getcol());
    for(int i = 0; i < ans.getrow(); i++) ans[i][i] = 1;
    auto modmult = [](const Matrix<T> &a, const Matrix<T> &b, const ll mod){
        Matrix<T> res(a.getrow(), b.getcol());
        for(int i = 0; i < a.getrow(); i++){
            for(int j = 0; j < b.getcol(); j++){
                for(int k = 0; k < a.getcol(); k++){
                    res[i][j] += a[i][k] * b[k][j];
                }
                if(res[i][j] > 0) res[i][j] = 1;
            }
        }
        return res;
    };
    while(x > 0){
        if(x & 1){
            ans = modmult(ans, a, mod);
        }
        a = modmult(a, a, mod);
        x >>= 1;
    }
    return ans;
}

int main(){
    ll n, m, t;
    cin >> n >> m >> t;
    Matrix<int> mat(n, n);
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        mat[a][b] = 1;
    }
    mat = modpow(mat, t, 1000000007);
    int ans = 0;
    for(int i = 0; i < n; i++){
        ans += mat[0][i];
    }
    cout << ans << endl;
    return 0;
}
0