QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#604265#7775. 【模板】矩阵快速幂nhuang6850 1395ms1808724kbC++238.5kb2024-10-02 03:28:022024-10-02 03:28:02

Judging History

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

  • [2024-10-02 03:28:02]
  • 评测
  • 测评结果:0
  • 用时:1395ms
  • 内存:1808724kb
  • [2024-10-02 03:28:02]
  • 提交

answer

/**
 * @author n685
 * @brief
 * @date 2024-09-20 21:20:01
 *
 *
 */
#ifndef LOCAL
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif
#include "bits/stdc++.h"

using i128 = __int128_t;

namespace rs = std::ranges;
namespace rv = std::views;

constexpr int MOD = 998'244'353;

template <class T>
concept integral = std::integral<T> || std::is_same_v<T, i128>;

struct BigInt {
  using T = i128;
  static constexpr T BASE = 1000000000000;
  static constexpr int LG = 12;
  std::vector<T> digits;
  // std::array<T, 8> digits{};
  BigInt() = default;
  explicit BigInt(integral auto val) {
    // int ind = 0;
    while (val > 0) {
      digits.push_back(val % BASE);
      // digits[ind++] = val % BASE;
      val /= BASE;
    }
  }
  explicit BigInt(std::string s) {
    // for (char c : s | rv::reverse) {
    //   digits.push_back(c - '0');
    // }
    std::reverse(s.begin(), s.end());
    s.resize((s.size() + LG - 1) / LG * LG, '0');
    // int ind = -1;
    for (int i = 0; i < std::ssize(s); i += LG) {
      digits.push_back(0);
      // ++ind;
      T mul = 1;
      for (int j = 0; j < LG && i + j < std::ssize(s); ++j, mul *= 10) {
        digits.back() += (s[i + j] - '0') * mul;
        // digits[ind] += (s[i + j] - '0') * mul;
      }
    }
  }
  void trim() {
    while (!digits.empty() && digits.back() == 0) {
      digits.pop_back();
    }
  }
  void carry() {
    for (int i = 0; i < std::ssize(digits); ++i) {
      if (digits[i] < BASE) {
        continue;
      }
      if (i == std::ssize(digits) - 1) {
        digits.push_back(0);
      }
      digits[i + 1] += digits[i] / BASE;
      digits[i] %= BASE;
    }
  }
  // value needs to be nonnegative
  void deneg() {
    for (int i = 0; i < std::ssize(digits) - 1; ++i) {
      if (digits[i] >= 0) {
        continue;
      }
      T val = ((BASE - 1) - digits[i]) / BASE;
      digits[i + 1] -= val;
      digits[i] += BASE * val;
    }
    trim();
  }
  BigInt &operator+=(const BigInt &rhs) {
    digits.resize(std::max(digits.size(), rhs.digits.size()));
    for (int i = 0; i < std::min(std::ssize(digits), std::ssize(rhs.digits));
         ++i)
    {
      digits[i] += rhs.digits[i];
    }
    carry();
    return *this;
  }
  BigInt &operator+=(integral auto rhs) {
    // for (int i = 0; rhs > 0; ++i) {
    //   if (i == std::ssize(digits)) {
    //     digits.push_back(rhs % BASE);
    //   } else {
    //     digits[i] += rhs % BASE;
    //   }
    //   rhs /= BASE;
    // }
    // if (digits.empty()) {
    //   digits.push_back(0);
    // }
    digits[0] += rhs;
    carry();
    return *this;
  }
  BigInt &operator-=(const BigInt &rhs) {
    assert(digits.size() >= rhs.digits.size());
    for (int i = 0; i < std::ssize(rhs.digits); ++i) {
      digits[i] -= rhs.digits[i];
    }
    deneg();
    return *this;
  }
  BigInt &operator-=(integral auto rhs) {
    for (int i = 0; rhs > 0; ++i) {
      digits[i] -= rhs % BASE;
      rhs /= BASE;
    }
    deneg();
    return *this;
  }
  BigInt operator*(const BigInt &rhs) {
    if (digits.empty() || rhs.digits.empty()) {
      return BigInt{};
    }
    BigInt ans;
    ans.digits.resize(digits.size() + rhs.digits.size() - 1);
    for (int i = 0; i < std::ssize(digits); ++i) {
      for (int j = 0; j < std::ssize(rhs.digits); ++j) {
        ans.digits[i + j] += digits[i] * rhs.digits[j];
      }
    }
    ans.carry();
    ans.trim();
    return ans;
  }
  BigInt &operator*=(integral auto rhs) {
    for (T &i : digits) {
      i *= rhs;
    }
    carry();
    return *this;
  }
  std::pair<BigInt, int> div(int rhs) const {
    BigInt ans;
    T rem = 0;
    // int ind = 0;
    for (T i : digits | rv::reverse) {
      rem = rem * BASE + i;
      ans.digits.push_back(rem / rhs);
      // ans.digits[ind++] = rem / rhs;
      rem %= rhs;
    }
    rs::reverse(ans.digits);
    return {ans, static_cast<int>(rem)};
  }
  int operator%(int rhs) const {
    int val = 0;
    for (T i : digits | rv::reverse) {
      val
        = static_cast<int>((static_cast<int64_t>(val) * BASE % rhs + i) % rhs);
    }
    return val;
  }
  bool operator==(const BigInt &rhs) const = default;
  bool operator!=(const BigInt &rhs) const = default;
  auto operator<=>(const BigInt &rhs) const {
    if (digits.size() != rhs.digits.size()) {
      return digits.size() <=> rhs.digits.size();
    }
    for (int i = static_cast<int>(digits.size()) - 1; i >= 0; --i) {
      if (digits[i] != rhs.digits[i]) {
        return digits[i] <=> rhs.digits[i];
      }
    }
    return std::strong_ordering::equal;
  }
};
BigInt operator+(BigInt a, const BigInt &b) { return a += b; }
BigInt operator+(BigInt a, integral auto b) { return a += b; }
BigInt operator-(BigInt a, const BigInt &b) { return a -= b; }
BigInt operator-(BigInt a, integral auto b) { return a -= b; }
BigInt operator*(BigInt a, integral auto b) { return a *= b; }

template <class T> constexpr T INF = T{};
template <std::floating_point T>
constexpr T INF<T> = std::numeric_limits<T>::infinity();
template <> constexpr int INF<int> = 0x3f3f3f3f; // 1061109567
template <>
constexpr int64_t INF<int64_t> = 0x3f3f3f3f3f3f3f3f; // 4557430888798830399
template <>
constexpr i128 INF<i128> = (static_cast<i128>(INF<int64_t>) << 64)
  | INF<int64_t>; // 84069761239290679208598432424319205183

struct Edge {
  int u, v;
  i128 w;
};

clock_t tot = 0;
clock_t tot2 = 0;
clock_t tot3 = 0;

void solve() {
  static_assert(integral<i128>);
  int n, m;
  std::string sk;
  std::cin >> n >> m >> sk;
  const int THRES = n * n / 2;
  BigInt k(sk);

  std::vector<Edge> edges(m);
  for (auto &[u, v, w] : edges) {
    int64_t ww;
    std::cin >> u >> v >> ww;
    --u;
    --v;
    w = ww;
  }

  std::vector dist(n + 1, std::vector(n, std::vector<i128>(n, INF<i128>)));
  std::vector d0(THRES + 1, std::vector<i128>(n, INF<i128>));
  d0[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    dist[0][i][i] = 0;
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < n; ++j) {
      for (auto &[u, v, w] : edges) {
        dist[i][j][v] = std::min(dist[i][j][v], dist[i - 1][j][u] + w);
      }
    }
    d0[i] = dist[i][0];
  }
  for (int i = n + 1; i <= THRES; ++i) {
    for (auto &[u, v, w] : edges) {
      d0[i][v] = std::min(d0[i][v], d0[i - 1][u] + w);
    }
  }
  if (k <= BigInt{THRES}) {
    int kk = std::stoi(sk);
    for (int i = 0; i < n; ++i) {
      if (d0[kk][i] == INF<i128>) {
        std::cout << "-1 ";
      } else {
        std::cout << static_cast<int>(d0[kk][i] % MOD) << ' ';
      }
    }
    std::cout << '\n';
    return;
  }

  std::vector dp(THRES + 1, std::vector<BigInt>(n));
  std::vector isinf(THRES + 1, std::vector<bool>(n, true));
  for (int i = 0; i < n; ++i) {
    i128 mi = INF<i128> / n;
    int sz = 1;
    for (int j = 1; j <= n; ++j) {
      if (dist[j][i][i] != INF<i128> && dist[j][i][i] * sz < mi * j) {
        mi = dist[j][i][i];
        sz = j;
      }
    }
    if (mi == INF<i128> / n) {
      continue;
    }
    for (int j = THRES - n; j <= THRES; ++j) {
      if (d0[j][i] == INF<i128>) {
        continue;
      }
      auto [times, rem] = (k - (j + THRES)).div(sz);
      int rest = THRES;
      if (rem > 0) {
        times += 1;
        rest -= sz - rem;
      }
      BigInt res = times * mi + d0[j][i];
      if (isinf[rest][i]) {
        dp[rest][i] = res;
      } else {
        dp[rest][i] = std::min(dp[rest][i], res);
      }
      isinf[rest][i] = false;
    }
  }
  std::vector<std::tuple<int, int, BigInt>> edges2;
  edges2.reserve(edges.size());
  for (auto [u, v, w] : edges) {
    edges2.emplace_back(u, v, w);
  }
  for (int i = THRES - 1; i >= 0; --i) {
    for (const auto &[u, v, w] : edges2) {
      if (!isinf[i + 1][u]) {
        if (isinf[i][v]) {
          dp[i][v] = dp[i + 1][u] + w;
        } else {
          dp[i][v] = std::min(dp[i][v], dp[i + 1][u] + w);
        }
        isinf[i][v] = false;
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (isinf[0][i]) {
      std::cout << "-1 ";
    } else {
      std::cout << dp[0][i] % MOD << ' ';
    }
  }
  std::cout << '\n';
}

int main() {
#ifndef LOCAL
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif
  // std::freopen("input.txt", "r", stdin);

  int s, t;
  std::cin >> s >> t;
  for (int i = 0; i < t; ++i) {
    solve();
  }
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Runtime Error

Test #1:

score: 10
Accepted
time: 51ms
memory: 64072kb

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:

395495792 395495781 395495783 395495839 395495793 395495789 395495754 395495832 395495845 395495755 395495823 395495773 395495753 395495800 395495782 395495763 395495847 395495761 395495794 395495791 395495786 395495821 395495798 395495765 395495772 395495770 395495757 395495819 395495843 395495828 ...

result:

ok 100 numbers

Test #2:

score: 10
Accepted
time: 101ms
memory: 71208kb

input:

1
1
100 200 998858598565699977
89 61 596014036562538539
89 84 921297646113897322
61 84 946923234442637386
61 35 641628261157284465
84 35 979893473772327497
84 78 700172488379560611
35 78 963617193748189613
35 54 951598888254521423
78 54 680825215292116806
78 21 737055858973038555
54 21 7491794406112...

output:

590375247 265938345 203065828 597548045 369717762 226160283 377877020 360218254 956162456 408060901 387231165 759578975 67601808 790211315 608425007 343195480 177353482 436533546 717630459 417099733 542227025 861764246 913806375 587268602 989846681 435016550 66609901 817090566 256847656 844441854 94...

result:

ok 100 numbers

Test #3:

score: 10
Accepted
time: 80ms
memory: 71204kb

input:

1
1
100 181 348568663892999968
25 19 990622898175774733
19 94 871060999389241529
94 24 969317630558501400
24 43 908457844888427461
43 52 816088481082287266
52 62 978618931332609685
62 99 761714433396732044
99 85 741344935503895668
85 64 964684335126604843
64 69 988098065125373655
69 31 7506975506815...

output:

916998469 916998469 916998469 76035207 62461893 916998469 389136594 916998469 916998469 173423529 164423356 822964468 626456020 916998469 744111524 916998469 398953850 916998469 342238577 916998469 255074799 784015663 916998469 740933556 587088671 811719512 916998469 916998469 916998469 916998469 14...

result:

ok 100 numbers

Test #4:

score: 10
Accepted
time: 82ms
memory: 71364kb

input:

1
1
100 189 295064635124730243
18 50 754672892083203214
50 88 962632394366987404
88 15 906700334097319336
15 26 967741400981618572
26 91 996214498763867892
91 35 882157548994344280
35 68 983621159612138407
68 51 563935036482744182
51 75 991205513962219551
75 72 974025375183814852
72 11 7979447663592...

output:

663199381 739882534 663199381 28600701 663199381 944601671 836329160 894091561 629507606 663199381 246830507 663199381 491987421 663199381 802123884 663199381 663199381 663199381 414785533 989396289 663199381 663199381 663199381 663199381 663199381 663199381 663199381 663199381 663199381 663199381 4...

result:

ok 100 numbers

Test #5:

score: 0
Runtime Error

input:

1
254
40 74 997173688939799978
38 6 890721839505665075
6 10 992308491267087930
10 29 960202932780090595
29 20 952827125924298715
20 34 868314670055961466
34 31 756448635709788087
31 14 857625921909632516
14 18 917667459973696862
18 21 985939328882662624
21 1 734882468602343649
1 11 66102593854575036...

output:

177014577 177014577 177014577 885341552 472856470 177014577 363547548 177014577 499847464 653076748 177014577 177014577 177014577 177014577 487939796 177014577 213466543 586729345 244952763 177014577 177014577 177014577 177014577 890105934 177014577 177014577 890105934 177014577 177014577 798890006 ...

result:


Subtask #2:

score: 0
Wrong Answer

Test #7:

score: 15
Accepted
time: 1332ms
memory: 1808724kb

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 313446627 -1 313436465 -1 313426303 -1 313416141 -1 313405979 -1 313395817 -1 313385655 -1 313375493 -1 313365331 -1 313355169 -1 313345007 -1 313334845 -1 313324683 -1 313314521 -1 313304359 -1 313294197 -1 313284035 -1 313273873 -1 313263711 -1 313253549 -1 313243387 -1 313233225 -1 313223063 -...

result:

ok 300 numbers

Test #8:

score: 15
Accepted
time: 1387ms
memory: 1808644kb

input:

2
1
300 598 9284745978997975899894787995823975998931999649789777849997467689
1 2 946893593823801228
2 1 946893593823801228
2 3 761384824565158999
3 2 761384824565158999
3 4 642721010434291429
4 3 642721010434291429
4 5 936762490761905983
5 4 936762490761905983
5 6 785485094128355256
6 5 785485094128...

output:

-1 613575042 -1 416269325 -1 387291578 -1 980556870 -1 491367967 -1 221793101 -1 191668085 -1 356035653 -1 428450970 -1 964149805 -1 511723806 -1 423081033 -1 947783979 -1 325795034 -1 115778037 -1 86469999 -1 111666379 -1 386592847 -1 223100328 -1 381885001 -1 23001328 -1 84087613 -1 517941041 -1 9...

result:

ok 300 numbers

Test #9:

score: 15
Accepted
time: 1395ms
memory: 1808644kb

input:

2
1
300 598 7877597936928589688789427798322599997378688496694695996269389696
1 2 866412995946330002
2 1 866412995946330002
2 3 866412995946330002
3 2 866412995946330002
3 4 866412995946330002
4 3 866412995946330002
4 5 866412995946330002
5 4 866412995946330002
5 6 866412995946330002
6 5 866412995946...

output:

708443714 -1 708438498 -1 708433282 -1 708428066 -1 708422850 -1 708417634 -1 708412418 -1 708407202 -1 708401986 -1 708396770 -1 708391554 -1 708386338 -1 708381122 -1 708375906 -1 708370690 -1 708365474 -1 708360258 -1 708355042 -1 708349826 -1 708344610 -1 708339394 -1 708334178 -1 708328962 -1 7...

result:

ok 300 numbers

Test #10:

score: 15
Accepted
time: 1157ms
memory: 1386696kb

input:

2
1
300 598 74686617152792803
1 2 920869599353968456
2 1 920869599353968456
2 3 920869599353968456
3 2 920869599353968456
3 4 920869599353968456
4 3 920869599353968456
4 5 920869599353968456
5 4 920869599353968456
5 6 920869599353968456
6 5 920869599353968456
6 7 920869599353968456
7 6 9208695993539...

output:

-1 537762223 -1 537752459 -1 537742695 -1 537732931 -1 537723167 -1 537713403 -1 537703639 -1 537693875 -1 537684111 -1 537674347 -1 537664583 -1 537654819 -1 537645055 -1 537635291 -1 537625527 -1 537615763 -1 537605999 -1 537596235 -1 537586471 -1 537576707 -1 537566943 -1 537557179 -1 537547415 -...

result:

ok 300 numbers

Test #11:

score: 0
Wrong Answer
time: 1173ms
memory: 119816kb

input:

2
40
120 238 7647979978895986883485788838258737687493899697379499657768989994
1 2 940784508355800649
2 1 940784508355800649
2 3 940784508355800649
3 2 940784508355800649
3 4 940784508355800649
4 3 940784508355800649
4 5 940784508355800649
5 4 940784508355800649
5 6 940784508355800649
6 5 94078450835...

output:

383704267 -1 383701847 -1 383699427 -1 383697007 -1 383694587 -1 383692167 -1 383689747 -1 383687327 -1 383684907 -1 383682487 -1 383680067 -1 383677647 -1 383675227 -1 383672807 -1 383670387 -1 383667967 -1 383665547 -1 383663127 -1 383660707 -1 383658287 -1 383655867 -1 383653447 -1 383651027 -1 3...

result:

wrong answer 1720th numbers differ - expected: '-1', found: '221443193'

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%