QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#448900#7775. 【模板】矩阵快速幂wsyear0 828ms14332kbC++173.2kb2024-06-20 11:32:382024-06-20 11:32:39

Judging History

你现在查看的是最新测评结果

  • [2024-06-20 11:32:39]
  • 评测
  • 测评结果:0
  • 用时:828ms
  • 内存:14332kb
  • [2024-06-20 11:32:38]
  • 提交

answer

#include <bits/stdc++.h>

#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T>inline void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T>inline void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

template <int P>
class mod_int {
  using Z = mod_int;

private:
  static int mo(int x) { return x < 0 ? x + P : x; }

public:
  int x;
  int val() const { return x; }
  mod_int() : x(0) {}
  template <class T>
  mod_int(const T &x_) : x(x_ >= 0 && x_ < P ? static_cast<int>(x_) : mo(static_cast<int>(x_ % P))) {}
  bool operator==(const Z &rhs) const { return x == rhs.x; }
  bool operator!=(const Z &rhs) const { return x != rhs.x; }
  Z operator-() const { return Z(x ? P - x : 0); }
  Z pow(long long k) const {
    Z res = 1, t = *this;
    while (k) {
      if (k & 1) res *= t;
      if (k >>= 1) t *= t;
    }
    return res;
  }
  Z &operator++() {
    x < P - 1 ? ++x : x = 0;
    return *this;
  }
  Z &operator--() {
    x ? --x : x = P - 1;
    return *this;
  }
  Z operator++(int) {
    Z ret = x;
    x < P - 1 ? ++x : x = 0;
    return ret;
  }
  Z operator--(int) {
    Z ret = x;
    x ? --x : x = P - 1;
    return ret;
  }
  Z inv() const { return pow(P - 2); }
  Z &operator+=(const Z &rhs) {
    (x += rhs.x) >= P && (x -= P);
    return *this;
  }
  Z &operator-=(const Z &rhs) {
    (x -= rhs.x) < 0 && (x += P);
    return *this;
  }
  Z &operator*=(const Z &rhs) {
    x = 1ULL * x * rhs.x % P;
    return *this;
  }
  Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
#define setO(T, o)                                 \
  friend T operator o(const Z &lhs, const Z &rhs) {\
    Z res = lhs;                                   \
    return res o## = rhs;                          \
  }
  setO(Z, +) setO(Z, -) setO(Z, *) setO(Z, /)
#undef setO
};
const int P = 998244353;
using Z = mod_int<P>;

const int maxn = 310;
const __int128 inf = 1e37;

int n, m;
ll k;

struct mat {
  __int128 a[maxn][maxn];
  friend mat operator*(mat x, mat y) {
    mat res;
    rep (i, 1, n) rep (j, 1, n) res.a[i][j] = inf;
    rep (i, 1, n) rep (k, 1, n) if (x.a[i][k] != inf) rep (j, 1, n) chkmn(res.a[i][j], x.a[i][k] + y.a[k][j]);
    return res;
  }
} e, res;

void print(__int128 x) {
  if (!x) return;
  print(x / 10);
  putchar('0' + x % 10);
}

void work() {
  scanf("%d%d%lld", &n, &m, &k);
  rep (i, 1, n) rep (j, 1, n) e.a[i][j] = inf;
  rep (i, 1, m) {
    int u, v;
    ll w;
    scanf("%d%d%lld", &u, &v, &w);
    chkmn(e.a[u][v], __int128(w));
  }
  rep (i, 1, n) rep (j, 1, n) res.a[i][j] = inf;
  res.a[1][1] = 0;
  while (k) {
    if (k & 1) res = res * e;
    e = e * e, k >>= 1;
  }
  rep (i, 1, n) {
    if (res.a[1][i] == inf) printf("-1 ");
    else print(res.a[1][i]), printf(" ");
  }
  printf("\n");
}

int main() {
  int sid, t;
  scanf("%d%d", &sid, &t);
  while (t--) work();
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 69ms
memory: 14332kb

input:

1
1
100 101 899539897889989959
74 35 910832669819965536
35 85 910832669819965536
85 88 910832669819965536
88 30 910832669819965536
30 58 910832669819965536
58 60 910832669819965536
60 34 910832669819965536
34 8 910832669819965536
8 67 910832669819965536
67 89 910832669819965536
89 32 910832669819965...

output:

819330326804718737265921456766053065 819330326804718737265921456766053054 819330326804718737265921456766053056 819330326804718737265921456766053112 819330326804718737265921456766053066 819330326804718737265921456766053062 819330326804718737265921456766053027 819330326804718737265921456766053105 8193...

result:

wrong output format Expected integer, but "819330326804718737265921456766053065" found

Subtask #2:

score: 0
Wrong Answer

Test #7:

score: 0
Wrong Answer
time: 828ms
memory: 14276kb

input:

2
1
300 598 8179377797889487867988994778539839593376697796496698959964978969
1 2 977880533270721156
2 1 977880533270721156
2 3 977880533270721156
3 2 977880533270721156
3 4 977880533270721156
4 3 977880533270721156
4 5 977880533270721156
5 4 977880533270721156
5 6 977880533270721156
6 5 977880533270...

output:

-1 9019355965953758885826745382478786994 -1 9019355965953758885826745382478776832 -1 9019355965953758885826745382478766670 -1 9019355965953758885826745382478756508 -1 9019355965953758885826745382478746346 -1 9019355965953758885826745382478736184 -1 9019355965953758885826745382478726022 -1 9019355965...

result:

wrong output format Expected integer, but "9019355965953758885826745382478786994" found

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

0%

Subtask #6:

score: 0
Skipped

Dependency #3:

0%