結果

問題 No.1340 おーじ君をさがせ
ユーザー Manuel1024Manuel1024
提出日時 2021-03-21 13:41:37
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 3,365 bytes
コンパイル時間 797 ms
コンパイル使用メモリ 74,948 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2024-11-22 05:55:48
合計ジャッジ時間 55,562 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48 TLE * 11
権限があれば一括ダウンロードができます

ソースコード

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