結果

問題 No.1112 冥界の音楽
ユーザー keijakkeijak
提出日時 2020-07-10 22:39:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 2,000 ms
コード長 3,526 bytes
コンパイル時間 1,678 ms
コンパイル使用メモリ 175,284 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-19 17:52:23
合計ジャッジ時間 2,670 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 4 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 3 ms
6,944 KB
testcase_18 AC 3 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 5 ms
6,944 KB
testcase_22 AC 4 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 5 ms
6,944 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 6 ms
6,944 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 2 ms
6,940 KB
testcase_35 AC 2 ms
6,940 KB
testcase_36 AC 13 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i)

#ifdef ENABLE_DEBUG
template <typename T>
void debug(T value) {
  cerr << value;
}
template <typename T, typename... Ts>
void debug(T value, Ts... args) {
  cerr << value << ", ";
  debug(args...);
}
#define DEBUG(...)                              \
  do {                                          \
    cerr << " \033[33m (L" << __LINE__ << ") "; \
    cerr << #__VA_ARGS__ << ":\033[0m ";        \
    debug(__VA_ARGS__);                         \
    cerr << endl;                               \
  } while (0)
#else
#define debug(...)
#define DEBUG(...)
#endif

// auto mod int

const i64 MOD = 1'000'000'007;

struct mint {
  long long x;
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint& operator+=(const mint a) {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint& operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }

using MatrixRow = vector<mint>;
using Matrix = vector<MatrixRow>;

Matrix matmul(const Matrix& a, const Matrix& b) {
  int n = a.size();
  assert(int(a[0].size()) == n);
  assert(int(b.size()) == n);
  assert(int(b[0].size()) == n);

  Matrix ret(n, MatrixRow(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int k = 0; k < n; ++k) {
        ret[i][j] += a[i][k] * b[k][j];
      }
    }
  }
  return ret;
}

Matrix matpow(const Matrix& a, long long k) {
  int n = a.size();
  assert(int(a[0].size()) == n);

  if (k == 0) {
    Matrix eye(n, MatrixRow(n));
    for (int i = 0; i < n; ++i) {
      eye[i][i] = 1;
    }
    return eye;
  }

  if (k == 1) {
    return a;
  }

  if (k & 1) {
    return matmul(matpow(a, k - 1), a);
  } else {
    return matpow(matmul(a, a), k / 2);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int K, M;
  cin >> K >> M;
  i64 N;
  cin >> N;
  int kk = K * K;
  Matrix mat(kk, MatrixRow(kk));
  MatrixRow init(kk);
  set<int> out;
  REP(i, M) {
    int p, q, r;
    cin >> p >> q >> r;
    p--;
    q--;
    r--;
    int from = p * K + q;
    int to = q * K + r;
    mat[to][from] = 1;
    if (p == 0) init[to] += 1;
    if (r == 0) out.insert(to);
  }
  //   REP(i, kk) {
  //     REP(j, kk) { cerr << "  " << mat[i][j]; }
  //     cerr << endl;
  //   }
  //   cerr << "----" << endl;

  Matrix mn = matpow(mat, N - 3);
  //   REP(i, kk) {
  //     REP(j, kk) { cerr << "  " << mn[i][j]; }
  //     cerr << endl;
  //   }
  //   cerr << "----" << endl;

  mint ans = 0;
  // REP(i, kk) {
  for (auto x : out) {
    REP(j, kk) { ans += mn[x][j] * init[j]; }
  }
  cout << ans << endl;
}
0