QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#622843 | #7775. 【模板】矩阵快速幂 | nhuang685 | 25 | 78ms | 115668kb | C++23 | 11.9kb | 2024-10-09 07:06:06 | 2024-10-09 07:06:07 |
Judging History
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"
#ifdef LOCAL
#include "dd/debug.h"
#else
#define dbg(...) 42
#define dbg_proj(...) 420
#define dbg_rproj(...) 420420
void nline() {}
void bar() {}
void start_clock() {}
void end_clock() {}
#endif
namespace rs = std::ranges;
namespace rv = std::views;
template <class T> constexpr std::pair<T, T> ex_eucl(T a, T b) {
if (a < b) {
auto [x, y] = ex_eucl(b, a);
return {y, x};
}
if (b == 0) {
assert(a == 1);
return {1, 0};
}
auto [x, y] = ex_eucl(b, a % b);
return {y, x - (a / b) * y};
}
template <class Md, class V = int64_t>
requires std::signed_integral<std::decay_t<decltype(Md::value)>>
struct Mod {
using T = std::decay_t<decltype(Md::value)>;
T val = 0;
static constexpr T normalize(auto val) {
using U = decltype(Md::value + val);
U uval = static_cast<U>(val);
U umd = static_cast<U>(Md::value);
if (uval <= -umd || umd <= uval) {
uval %= umd;
}
if (val < 0) {
uval += umd;
}
return static_cast<T>(uval);
}
constexpr Mod() : val(0) {}
constexpr explicit Mod(auto _val) : val(normalize(_val)) {}
static inline const Mod ZERO = Mod(0);
static inline const Mod ONE = Mod(1);
static inline const Mod TWO = Mod(2);
// addition
constexpr Mod &operator+=(Mod b) {
val += b.val;
if (val >= Md::value) {
val -= Md::value;
}
return *this;
}
friend constexpr Mod operator+(Mod a, Mod b) { return a += b; }
constexpr Mod &operator++() { return *this += Mod(1); }
constexpr Mod operator++(int) {
Mod res = *this;
++(*this);
return res;
}
// subtraction
constexpr Mod &operator-=(Mod b) {
val -= b.val;
if (val < 0) {
val += Md::value;
}
return *this;
}
friend constexpr Mod operator-(Mod a, Mod b) { return a -= b; }
constexpr Mod &operator--() { return *this -= Mod(1); }
constexpr Mod operator--(int) {
Mod res = *this;
--(*this);
return res;
}
// negation
constexpr Mod operator-() const { return Mod(-val); }
// multiplication
constexpr Mod &operator*=(Mod b) {
val = static_cast<T>(static_cast<V>(val) * b.val % Md::value);
return *this;
}
friend constexpr Mod operator*(Mod a, Mod b) { return a *= b; }
constexpr Mod binpow(std::integral auto b) const {
Mod res = Mod(1), a = *this;
while (b > 0) {
if (b % 2 == 1) {
res *= a;
}
a *= a;
b /= 2;
}
return res;
}
// factorial
// align with fft, if code fails to compile make this smaller (if using array)
static constexpr int MXINV = 1 << 22;
static inline bool init = false;
static inline std::vector<Mod> ff, iff;
static void reset_fac() { init = false; }
static void init_fac() {
if (init) {
return;
}
ff.resize(MXINV + 1);
ff[0] = Mod(1);
for (int i = 1; i <= MXINV; ++i) {
ff[i] = ff[i - 1] * Mod(i);
}
iff.resize(MXINV + 1);
iff[MXINV] = ff[MXINV].large_inv();
for (int i = MXINV - 1; i >= 0; --i) {
iff[i] = iff[i + 1] * Mod(i + 1);
}
init = true;
}
static Mod fac(int v) {
if (!init) {
init_fac();
}
return ff[v];
}
static Mod ifac(int v) {
if (!init) {
init_fac();
}
return iff[v];
}
static Mod comb(int n, int k) {
if (n < 0 || k < 0 || n < k) {
return Mod(0);
}
return fac(n) * ifac(n - k) * ifac(k);
}
static Mod perm(int n, int k) {
if (n < 0 || k < 0 || n < k) {
return Mod(0);
}
return fac(n) * ifac(n - k);
}
// inverse
Mod small_inv() const {
return ifac(static_cast<int>(val)) * fac(static_cast<int>(val) - 1);
}
constexpr Mod large_inv() const {
return Mod(ex_eucl(static_cast<V>(val), static_cast<V>(Md::value)).first);
// return binpow(Md::value - 2);
}
Mod inv() const {
if (val <= MXINV) {
return small_inv();
}
return large_inv();
}
// sqrt
std::optional<Mod> sqrt() {
static std::mt19937 rng(
std::chrono::steady_clock::now().time_since_epoch().count()
);
Mod c = Mod::ZERO;
while ((c * c - *this).binpow((Md::value - 1) / 2) == Mod::ONE) {
c = Mod(rng());
}
if (c == Mod::ZERO) {
return std::nullopt;
}
std::pair<Mod, Mod> res(Mod::ONE, Mod::ZERO), a(c, Mod::ONE);
T b = (Md::value + 1) / 2;
auto mul = [&c, this](
const std::pair<Mod, Mod> &u,
const std::pair<Mod, Mod> &v
) -> std::pair<Mod, Mod> {
return {
u.first * v.first + u.second * v.second * (c * c - *this),
u.second * v.first + u.first * v.second
};
};
while (b > 0) {
if (b % 2 == 1) {
res = mul(res, a);
}
a = mul(a, a);
b /= 2;
}
return res.first;
// return std::min(res.first, -res.first);
}
// comparison
constexpr bool operator==(const Mod &b) const = default;
constexpr std::strong_ordering operator<=>(const Mod &b) const = default;
// io
friend std::istream &operator>>(std::istream &in, Mod &a) {
int64_t v;
in >> v;
a = Mod(v);
return in;
}
friend std::ostream &operator<<(std::ostream &out, const Mod &a) {
out << a.val;
return out;
}
// conversion
constexpr T value() const { return val; }
};
#if defined(__cpp_lib_format) && __cplusplus >= 202302L
template <class Md, class V>
requires std::formattable<typename Mod<Md, V>::T, char>
struct std::formatter<Mod<Md, V>, char> {
using T = typename Mod<Md, V>::T;
std::formatter<T, char> underlying;
constexpr formatter() {
if constexpr (requires { underlying.set_debug_format(); }) {
underlying.set_debug_format();
}
}
template <class ParseContext> constexpr auto parse(ParseContext &ctx) {
return underlying.parse(ctx);
}
template <class FormatContext>
auto format(const Mod<Md, V> &val, FormatContext &ctx) const {
return underlying.format(val.value(), ctx);
}
};
#endif
constexpr int MOD = 998'244'353;
using Mint = Mod<std::integral_constant<std::decay_t<decltype(MOD)>, MOD>>;
using i128 = __int128_t;
template <class T>
concept integral = std::integral<T> || std::is_same_v<i128, T>;
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;
};
i128 abs_i128(i128 a) { return a < 0 ? -a : a; }
i128 k = 0;
Mint kmint;
// (ak + b) / c
struct Frac {
i128 a = 0, b = 0;
int c = 1;
Frac &operator+=(i128 rhs) {
b += c * rhs;
return *this;
}
bool is_inf() const { return c == 0; }
auto operator<=>(const Frac &rhs) const {
using ord = std::strong_ordering;
if (is_inf()) {
if (rhs.is_inf()) {
return ord::equal;
}
return ord::greater;
}
if (rhs.is_inf()) {
return ord::less;
}
i128 al = a * rhs.c, bl = b * rhs.c;
i128 ar = rhs.a * c, br = rhs.b * c;
if (al == ar) {
return bl <=> br;
}
if (al < ar && bl <= br) {
return ord::less;
}
if (al > ar && bl >= br) {
return ord::greater;
}
i128 ii = (abs_i128(bl - br) + abs_i128(al - ar) - 1) / abs_i128(al - ar);
if (al < ar) {
if (ii >= k) {
return ord::greater;
}
return ord::less;
}
if (ii < k) {
return ord::greater;
}
return ord::less;
}
Mint to_mint() const { return (Mint{a} * kmint + Mint{b}) * Mint{c}.inv(); }
};
const Frac FINF{0, 1, 0};
const Frac operator+(Frac lhs, i128 rhs) { return lhs += rhs; }
clock_t tot = 0;
clock_t tot2 = 0;
clock_t tot3 = 0;
const std::string infk = []() {
i128 val = INF<i128>;
std::string ans;
while (val > 0) {
ans += static_cast<char>('0' + val % 10);
val /= 10;
}
rs::reverse(ans);
return ans;
}();
void solve() {
auto start = clock();
int n, m;
std::string sk;
std::cin >> n >> m >> sk;
const int thres = n * n;
if (sk.size() > infk.size() || (sk.size() == infk.size() && sk > infk)) {
k = INF<i128>;
} else {
for (char c : sk) {
k = 10 * k + (c - '0');
}
}
std::vector<int> mk(n + 1);
for (char c : sk) {
kmint = kmint * Mint{10} + Mint{c - '0'};
for (int i = 1; i <= n; ++i) {
mk[i] = (mk[i] * 10 + (c - '0')) % i;
}
}
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);
}
}
tot += clock() - start;
start = clock();
if (k <= 3 * thres) {
int kk = static_cast<int>(k);
d0.resize(3 * thres + 1, std::vector<i128>(n, INF<i128>));
for (int i = thres + 1; i <= 3 * thres; ++i) {
for (auto &[u, v, w] : edges) {
d0[i][v] = std::min(d0[i][v], d0[i - 1][u] + w);
}
}
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<Frac>(n, FINF));
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;
}
int rem = (mk[sz] - j - thres + sz - 1) % sz;
if (rem < 0) {
rem += sz;
}
dp[thres - sz + 1 + rem][i] = std::min(
dp[thres - sz + 1 + rem][i],
Frac{mi, d0[j][i] * sz + mi * (-j - thres + sz - 1 - rem), sz}
);
}
}
tot2 += clock() - start;
start = clock();
for (int i = thres - 1; i >= 0; --i) {
for (const auto &[u, v, w] : edges) {
if (!dp[i + 1][u].is_inf()) {
if (dp[i][v].is_inf()) {
dp[i][v] = dp[i + 1][u] + w;
} else {
dp[i][v] = std::min(dp[i][v], dp[i + 1][u] + w);
}
}
}
}
tot3 += clock() - start;
for (int i = 0; i < n; ++i) {
if (dp[0][i].is_inf()) {
std::cout << "-1 ";
} else {
std::cout << Mint(dp[0][i].to_mint()) << ' ';
}
}
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;
auto start = clock();
for (int i = 0; i < t; ++i) {
solve();
k = 0;
kmint = Mint{0};
}
dbg(tot);
dbg(tot2);
dbg(tot3);
dbg(clock() - start);
}
詳細信息
Subtask #1:
score: 10
Accepted
Test #1:
score: 10
Accepted
time: 68ms
memory: 115536kb
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: 69ms
memory: 115584kb
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: 69ms
memory: 115552kb
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: 67ms
memory: 115520kb
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: 10
Accepted
time: 60ms
memory: 41536kb
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:
ok 3575 numbers
Test #6:
score: 10
Accepted
time: 52ms
memory: 38688kb
input:
1 356 32 47 967844399484634837 4 30 776954643355911997 30 20 811634053140142741 20 22 747630229183579429 22 2 806282875388761050 2 26 719793351534499411 26 17 797537828929335673 17 24 890423236992687627 24 21 970792227007588899 21 8 850078803097295262 8 15 958474507028658347 15 1 972636122087215360 ...
output:
890097469 525779071 636798453 776362497 776362497 687961593 158033324 776362497 345910504 380622623 239804834 440670451 137231885 985041116 222869127 137231885 705696901 637534644 347889826 696528073 291555427 146553026 776362497 624486185 137231885 642408114 520519927 137231885 438373632 263924254 ...
result:
ok 4784 numbers
Subtask #2:
score: 0
Memory Limit Exceeded
Test #7:
score: 0
Memory Limit Exceeded
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:
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 15
Accepted
Dependency #1:
100%
Accepted
Test #19:
score: 15
Accepted
time: 69ms
memory: 115588kb
input:
4 1 100 101 6888995999928874698772868926699656683388498575797893294688976887 25 90 495511874996847106 90 84 495511874996847106 84 82 495511874996847106 82 40 495511874996847106 40 97 495511874996847106 97 5 495511874996847106 5 24 495511874996847106 24 16 495511874996847106 16 19 495511874996847106 ...
output:
662900138 662900131 662900188 662900147 662900176 662900221 662900152 662900202 662900130 662900140 662900169 662900199 662900128 662900145 662900192 662900178 662900163 662900150 662900179 662900151 662900139 662900180 662900216 662900177 662900170 662900205 662900210 662900183 662900184 662900125 ...
result:
ok 100 numbers
Test #20:
score: 15
Accepted
time: 78ms
memory: 115564kb
input:
4 1 100 200 7298898492397999688666927949888498969897838287679988999656889979 1 68 716477084362826727 1 70 849254955511480878 68 70 965501875328180109 68 27 922798232695217800 70 27 973650788054328171 70 69 992887836560799260 27 69 912347321604310534 27 41 707737334645887057 69 41 939222694708421463 ...
output:
59219241 402083566 593666306 414807498 258758770 177911843 190858821 427609509 714942754 794670437 266523695 250908431 280340515 973300594 490891479 435411914 570632298 806776572 581872834 661756417 571008187 273666813 634277068 321782154 962526931 884883598 912195600 389101189 783089343 302322065 7...
result:
ok 100 numbers
Test #21:
score: 15
Accepted
time: 67ms
memory: 115668kb
input:
4 1 100 170 8794888769994978795934858981288869698995675273759827929988816678 85 25 955229927087338794 25 69 715916767824027774 69 8 871119978520194455 8 64 918533454985251428 64 73 928715673496434787 73 95 777734955942460360 95 82 937506435422061091 82 21 972958971354009576 21 81 920481916656974333 ...
output:
1429581 1429581 150584952 1429581 1429581 616522075 1429581 1429581 56697037 26641870 455335474 405458157 1429581 119209248 768006956 56697037 1429581 1429581 56697037 825267652 337654936 589008244 691024955 410854366 1429581 1429581 569334223 757829937 1429581 1429581 1429581 387845353 1429581 1429...
result:
ok 100 numbers
Test #22:
score: 15
Accepted
time: 76ms
memory: 115448kb
input:
4 1 100 175 988927424060208873 73 39 871487671517176049 39 12 989592888976857487 12 60 753594598459115125 60 80 804510907944284786 80 87 837728552224523957 87 50 720829671677245259 50 55 955472566241448463 55 56 698073335743612454 56 24 907173313658798936 24 75 812098650937527351 75 23 9749473294852...
output:
120476850 665961545 406533341 950211937 665961545 665961545 665961545 160438797 665961545 860608848 665961545 665961545 665961545 665961545 698344451 665961545 115173142 157625083 665961545 665961545 665961545 817070036 795455857 665961545 615381143 665961545 363716660 579107160 665961545 665961545 ...
result:
ok 100 numbers
Test #23:
score: 15
Accepted
time: 63ms
memory: 41388kb
input:
4 272 40 69 8526848846996917979164958976967645898953979497989749477999798947 16 20 904311791006766339 20 8 861881272650260368 8 38 927651150818983482 38 22 575472375470752507 22 10 995789624005306013 10 17 862458045607914444 17 35 910173488885856948 35 12 897609723109586512 12 23 970232607197909774 ...
output:
631006282 189710405 538148249 980737927 538148249 223767537 200756370 844883815 538148249 538148249 506622768 538148249 278823150 381947526 538148249 594558624 538148249 751430936 664913832 538148249 961366366 945739792 646619933 538148249 538148249 538148249 538148249 831302 107422344 538148249 118...
result:
ok 3782 numbers
Test #24:
score: 15
Accepted
time: 62ms
memory: 38788kb
input:
4 367 32 64 7694675676969676989727597922979586548768678479696836977677459878 26 1 948535512091890169 1 28 927579818325689242 28 18 869862583518920980 18 24 916474447302009020 24 23 592009716422157884 23 12 932781568469333631 12 17 906790845067818370 17 16 733495190715963829 16 5 948217995091033642 5...
output:
242947009 937164220 269122924 473901869 812915310 815200847 138502251 269122924 269122924 269122924 269122924 269122924 269122924 790732730 269122924 269122924 423399215 858100602 269122924 259562134 269122924 613550676 269122924 242947009 269122924 269122924 210817778 269122924 269122924 269122924 ...
result:
ok 4886 numbers
Subtask #5:
score: 0
Memory Limit Exceeded
Dependency #1:
100%
Accepted
Test #25:
score: 0
Memory Limit Exceeded
input:
5 1 300 301 969767789936486493 164 284 964646444984408140 284 241 964646444984408140 241 281 964646444984408140 281 138 964646444984408140 138 242 964646444984408140 242 112 964646444984408140 112 217 964646444984408140 217 170 964646444984408140 170 31 964646444984408140 31 300 964646444984408140 3...
output:
562333388 562333371 562333450 562333457 562333181 562333366 562333433 562333276 562333204 562333354 562333361 562333374 562333436 562333405 562333369 562333286 562333360 562333318 562333396 562333251 562333480 562333220 562333333 562333460 562333359 562333295 562333293 562333335 562333402 562333226 ...
result:
Subtask #6:
score: 0
Skipped
Dependency #3:
0%