結果

問題 No.1340 おーじ君をさがせ
ユーザー milanis48663220milanis48663220
提出日時 2021-01-15 22:40:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 3,095 bytes
コンパイル時間 1,050 ms
コンパイル使用メモリ 102,028 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-18 16:40:27
合計ジャッジ時間 4,662 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 21 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 18 ms
4,376 KB
testcase_14 AC 47 ms
4,376 KB
testcase_15 AC 42 ms
4,376 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 14 ms
4,376 KB
testcase_18 AC 27 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 16 ms
4,380 KB
testcase_22 AC 39 ms
4,376 KB
testcase_23 AC 15 ms
4,380 KB
testcase_24 AC 72 ms
4,376 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 17 ms
4,380 KB
testcase_31 AC 72 ms
4,376 KB
testcase_32 AC 71 ms
4,380 KB
testcase_33 AC 70 ms
4,376 KB
testcase_34 AC 65 ms
4,380 KB
testcase_35 AC 75 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 3 ms
4,376 KB
testcase_38 AC 73 ms
4,376 KB
testcase_39 AC 58 ms
4,380 KB
testcase_40 AC 59 ms
4,376 KB
testcase_41 AC 58 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 55 ms
4,380 KB
testcase_47 AC 57 ms
4,376 KB
testcase_48 AC 59 ms
4,376 KB
testcase_49 AC 56 ms
4,380 KB
testcase_50 AC 56 ms
4,376 KB
testcase_51 AC 56 ms
4,380 KB
testcase_52 AC 58 ms
4,380 KB
testcase_53 AC 58 ms
4,380 KB
testcase_54 AC 60 ms
4,376 KB
testcase_55 AC 62 ms
4,376 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 2 ms
4,376 KB
testcase_58 AC 2 ms
4,376 KB
testcase_59 AC 60 ms
4,376 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 54 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

class Bit{
    public:
    bool v;
    Bit(bool _v = false){
        v = _v;
    }
    Bit operator+(bool b){
        return Bit(v|b);
    }
    Bit operator*(bool b){
        return Bit(v&b);
    }

    void operator+=(bool b){
        v |= b;
    }
    void operator*=(bool b){
        v &= b;
    }
    
    
    Bit operator+(Bit b){
        return Bit(v|b.v);
    }
    Bit operator*(Bit b){
        return Bit(v&b.v);
    }

    void operator+=(Bit b){
        v |= b.v;
    }
    void operator*=(Bit b){
        v &= b.v;
    }
};


template <typename T>
struct matrix{
    int n, m;
    vector<vector<T>> dat;
    matrix(int n_, int m_){
        n = n_; m = m_;
        for(int i = 0; i < n; i++){
            dat.push_back(vector<T>(m));
        }
    }
    vector<T>& operator[](int x) {
        return dat[x];
	}
};

template <typename T>
bool prod(matrix<T> a, matrix<T> b, matrix<T> &ans){
    assert(a.m == b.n);
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < b.m; j++){
            ans.dat[i][j] = 0;
            for(int k = 0; k < b.n; k++){
                ans.dat[i][j] += (a.dat[i][k]*b.dat[k][j]);
            }
        }
    }
    return true;
}

template <typename T>
void copy_mat(matrix<T> a, matrix<T> &b){
    assert(a.n == b.n);
    assert(a.m == b.m);
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.m; j++){
            b.dat[i][j] = a.dat[i][j];
        }
    }
}

template <typename T>
void pow_mat(matrix<T> a, ll n, matrix<T> &ans){
    assert(n < ((ll)1<<61));
    matrix<T> buf(a.n, a.n);
    matrix<T> tmp(a.n, a.n);
    copy_mat(a, tmp);

    for(int i = 0; i < a.n; i++) {
        for(int j = 0; j < a.n; j++){
            ans.dat[i][j] = 0;
        }
        ans.dat[i][i] = 1;
    }
    
    for(int i = 0; i <= 60; i++){
        ll m = (ll)1 << i;
        if(m&n){
            prod(tmp, ans, buf);
            copy_mat(buf, ans);
        }
        prod(tmp, tmp, buf);
        copy_mat(buf, tmp);
    }
}

int n, m;
ll t;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> n >> m >> t;
    matrix<Bit> mat(n, n);
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b;
        mat[b][a] = true;
    }
    matrix<Bit> v(n, 1);
    v[0][0] = true;
    matrix<Bit> pw(n, n);
    pow_mat(mat, t, pw);
    matrix<Bit> ans(n, 1);
    prod(pw, v, ans);
    // cout << ans << endl;
    int sum = 0;
    for(int i = 0; i < n; i++) {
        if(ans[i][0].v) sum++;
    }
    cout << sum << endl;
}
0