QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#490129 | #6161. 作业题 | pavement# | 100 ✓ | 999ms | 6544kb | C++20 | 19.4kb | 2024-07-25 11:34:12 | 2024-07-25 11:34:12 |
Judging History
answer
#line 1 "main.cpp"
// @brief Counting Spanning Trees (Undirected)
#define PROBLEM "https://judge.yosupo.jp/problem/counting_spanning_tree_undirected"
#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("tune=native")
#line 1 "cp-algorithms-aux/cp-algo/linalg/matrix.hpp"
#line 1 "cp-algorithms-aux/cp-algo/random/rng.hpp"
#include <chrono>
#include <random>
namespace cp_algo::random {
uint64_t rng() {
static std::mt19937_64 rng(
std::chrono::steady_clock::now().time_since_epoch().count()
);
return rng();
}
}
#line 1 "cp-algorithms-aux/cp-algo/math/common.hpp"
#include <functional>
#include <cstdint>
namespace cp_algo::math {
#ifdef CP_ALGO_MAXN
const int maxn = CP_ALGO_MAXN;
#else
const int maxn = 1 << 19;
#endif
const int magic = 64; // threshold for sizes to run the naive algo
auto bpow(auto const& x, int64_t n, auto const& one, auto op) {
if(n == 0) {
return one;
} else {
auto t = bpow(x, n / 2, one, op);
t = op(t, t);
if(n % 2) {
t = op(t, x);
}
return t;
}
}
auto bpow(auto x, int64_t n, auto ans) {
return bpow(x, n, ans, std::multiplies{});
}
template<typename T>
T bpow(T const& x, int64_t n) {
return bpow(x, n, T(1));
}
}
#line 1 "cp-algorithms-aux/cp-algo/linalg/vector.hpp"
#line 1 "cp-algorithms-aux/cp-algo/math/modint.hpp"
#line 4 "cp-algorithms-aux/cp-algo/math/modint.hpp"
#include <iostream>
namespace cp_algo::math {
template<typename modint>
struct modint_base {
static int64_t mod() {
return modint::mod();
}
modint_base(): r(0) {}
modint_base(int64_t rr): r(rr % mod()) {
r = std::min(r, r + mod());
}
modint inv() const {
return bpow(to_modint(), mod() - 2);
}
modint operator - () const {return std::min(-r, mod() - r);}
modint& operator /= (const modint &t) {
return to_modint() *= t.inv();
}
modint& operator *= (const modint &t) {
if(mod() <= uint32_t(-1)) {
r = r * t.r % mod();
} else {
r = __int128(r) * t.r % mod();
}
return to_modint();
}
modint& operator += (const modint &t) {
r += t.r; r = std::min(r, r - mod());
return to_modint();
}
modint& operator -= (const modint &t) {
r -= t.r; r = std::min(r, r + mod());
return to_modint();
}
modint operator + (const modint &t) const {return modint(to_modint()) += t;}
modint operator - (const modint &t) const {return modint(to_modint()) -= t;}
modint operator * (const modint &t) const {return modint(to_modint()) *= t;}
modint operator / (const modint &t) const {return modint(to_modint()) /= t;}
auto operator <=> (const modint_base &t) const = default;
int64_t rem() const {return 2 * r > (uint64_t)mod() ? r - mod() : r;}
// Only use if you really know what you're doing!
uint64_t modmod() const {return 8ULL * mod() * mod();};
void add_unsafe(uint64_t t) {r += t;}
void pseudonormalize() {r = std::min(r, r - modmod());}
modint const& normalize() {
if(r >= (uint64_t)mod()) {
r %= mod();
}
return to_modint();
}
uint64_t& setr() {return r;}
uint64_t getr() const {return r;}
private:
uint64_t r;
modint& to_modint() {return static_cast<modint&>(*this);}
modint const& to_modint() const {return static_cast<modint const&>(*this);}
};
template<typename modint>
std::istream& operator >> (std::istream &in, modint_base<modint> &x) {
return in >> x.setr();
}
template<typename modint>
std::ostream& operator << (std::ostream &out, modint_base<modint> const& x) {
return out << x.getr();
}
template<typename modint>
concept modint_type = std::is_base_of_v<modint_base<modint>, modint>;
template<int64_t m>
struct modint: modint_base<modint<m>> {
static constexpr int64_t mod() {return m;}
using Base = modint_base<modint<m>>;
using Base::Base;
};
struct dynamic_modint: modint_base<dynamic_modint> {
static int64_t mod() {return m;}
static void switch_mod(int64_t nm) {m = nm;}
using Base = modint_base<dynamic_modint>;
using Base::Base;
// Wrapper for temp switching
auto static with_mod(int64_t tmp, auto callback) {
struct scoped {
int64_t prev = mod();
~scoped() {switch_mod(prev);}
} _;
switch_mod(tmp);
return callback();
}
private:
static int64_t m;
};
int64_t dynamic_modint::m = 0;
}
#line 6 "cp-algorithms-aux/cp-algo/linalg/vector.hpp"
#include <algorithm>
#include <valarray>
#line 9 "cp-algorithms-aux/cp-algo/linalg/vector.hpp"
#include <iterator>
#include <cassert>
namespace cp_algo::linalg {
template<class vec, typename base>
struct valarray_base: std::valarray<base> {
using Base = std::valarray<base>;
using Base::Base;
valarray_base(base const& t): Base(t, 1) {}
auto begin() {return std::begin(to_valarray());}
auto begin() const {return std::begin(to_valarray());}
auto end() {return std::end(to_valarray());}
auto end() const {return std::end(to_valarray());}
bool operator == (vec const& t) const {return std::ranges::equal(*this, t);}
bool operator != (vec const& t) const {return !(*this == t);}
vec operator-() const {return Base::operator-();}
static vec from_range(auto const& R) {
vec res(std::ranges::distance(R));
std::ranges::copy(R, res.begin());
return res;
}
Base& to_valarray() {return static_cast<Base&>(*this);}
Base const& to_valarray() const {return static_cast<Base const&>(*this);}
};
template<class vec, typename base>
vec operator+(valarray_base<vec, base> const& a, valarray_base<vec, base> const& b) {
return a.to_valarray() + b.to_valarray();
}
template<class vec, typename base>
vec operator-(valarray_base<vec, base> const& a, valarray_base<vec, base> const& b) {
return a.to_valarray() - b.to_valarray();
}
template<class vec, typename base>
struct vec_base: valarray_base<vec, base> {
using Base = valarray_base<vec, base>;
using Base::Base;
static vec ei(size_t n, size_t i) {
vec res(n);
res[i] = 1;
return res;
}
virtual void add_scaled(vec const& b, base scale, size_t i = 0) {
for(; i < size(*this); i++) {
(*this)[i] += scale * b[i];
}
}
virtual vec const& normalize() {
return static_cast<vec&>(*this);
}
virtual base normalize(size_t i) {
return (*this)[i];
}
void read() {
for(auto &it: *this) {
std::cin >> it;
}
}
void print() const {
std::ranges::copy(*this, std::ostream_iterator<base>(std::cout, " "));
std::cout << "\n";
}
static vec random(size_t n) {
vec res(n);
std::ranges::generate(res, random::rng);
return res;
}
// Concatenate vectors
vec operator |(vec const& t) const {
vec res(size(*this) + size(t));
res[std::slice(0, size(*this), 1)] = *this;
res[std::slice(size(*this), size(t), 1)] = t;
return res;
}
// Generally, vec shouldn't be modified
// after its pivot index is set
std::pair<size_t, base> find_pivot() {
if(pivot == size_t(-1)) {
pivot = 0;
while(pivot < size(*this) && normalize(pivot) == base(0)) {
pivot++;
}
if(pivot < size(*this)) {
pivot_inv = base(1) / (*this)[pivot];
}
}
return {pivot, pivot_inv};
}
void reduce_by(vec &t) {
auto [pivot, pinv] = t.find_pivot();
if(pivot < size(*this)) {
add_scaled(t, -normalize(pivot) * pinv, pivot);
}
}
private:
size_t pivot = -1;
base pivot_inv;
};
template<typename base>
struct vec: vec_base<vec<base>, base> {
using Base = vec_base<vec<base>, base>;
using Base::Base;
};
template<math::modint_type base>
struct vec<base>: vec_base<vec<base>, base> {
using Base = vec_base<vec<base>, base>;
using Base::Base;
void add_scaled(vec const& b, base scale, size_t i = 0) override {
for(; i < size(*this); i++) {
(*this)[i].add_unsafe(scale.getr() * b[i].getr());
}
if(++counter == 8) {
for(auto &it: *this) {
it.pseudonormalize();
}
counter = 0;
}
}
vec const& normalize() override {
for(auto &it: *this) {
it.normalize();
}
return *this;
}
base normalize(size_t i) override {
return (*this)[i].normalize();
}
private:
size_t counter = 0;
};
}
#line 7 "cp-algorithms-aux/cp-algo/linalg/matrix.hpp"
#include <optional>
#line 9 "cp-algorithms-aux/cp-algo/linalg/matrix.hpp"
#include <vector>
#include <array>
namespace cp_algo::linalg {
template<typename base_t>
struct matrix: valarray_base<matrix<base_t>, vec<base_t>> {
using base = base_t;
using Base = valarray_base<matrix<base>, vec<base>>;
using Base::Base;
matrix(size_t n): Base(vec<base>(n), n) {}
matrix(size_t n, size_t m): Base(vec<base>(m), n) {}
size_t n() const {return size(*this);}
size_t m() const {return n() ? size(row(0)) : 0;}
auto dim() const {return std::array{n(), m()};}
auto& row(size_t i) {return (*this)[i];}
auto const& row(size_t i) const {return (*this)[i];}
matrix& operator *=(base t) {for(auto &it: *this) it *= t; return *this;}
matrix operator *(base t) const {return matrix(*this) *= t;}
// Make sure the result is matrix, not Base
matrix& operator*=(matrix const& t) {return *this = *this * t;}
void read() {
for(auto &it: *this) {
it.read();
}
}
void print() const {
for(auto const& it: *this) {
it.print();
}
}
static matrix block_diagonal(std::vector<matrix> const& blocks) {
size_t n = 0;
for(auto &it: blocks) {
assert(it.n() == it.m());
n += it.n();
}
matrix res(n);
n = 0;
for(auto &it: blocks) {
for(size_t i = 0; i < it.n(); i++) {
res[n + i][std::slice(n, it.n(), 1)] = it[i];
}
n += it.n();
}
return res;
}
static matrix random(size_t n, size_t m) {
matrix res(n, m);
std::ranges::generate(res, std::bind(vec<base>::random, m));
return res;
}
static matrix random(size_t n) {
return random(n, n);
}
static matrix eye(size_t n) {
matrix res(n);
for(size_t i = 0; i < n; i++) {
res[i][i] = 1;
}
return res;
}
// Concatenate matrices
matrix operator |(matrix const& b) const {
assert(n() == b.n());
matrix res(n(), m()+b.m());
for(size_t i = 0; i < n(); i++) {
res[i] = row(i) | b[i];
}
return res;
}
matrix submatrix(auto slicex, auto slicey) const {
matrix res = (*this)[slicex];
for(auto &row: res) {
row = vec<base>(row[slicey]);
}
return res;
}
matrix T() const {
matrix res(m(), n());
for(size_t i = 0; i < n(); i++) {
for(size_t j = 0; j < m(); j++) {
res[j][i] = row(i)[j];
}
}
return res;
}
matrix operator *(matrix const& b) const {
assert(m() == b.n());
matrix res(n(), b.m());
for(size_t i = 0; i < n(); i++) {
for(size_t j = 0; j < m(); j++) {
res[i].add_scaled(b[j], row(i)[j]);
}
}
return res.normalize();
}
vec<base> apply(vec<base> const& x) const {
return (matrix(x) * *this)[0];
}
matrix pow(uint64_t k) const {
assert(n() == m());
return bpow(*this, k, eye(n()));
}
matrix& normalize() {
for(auto &it: *this) {
it.normalize();
}
return *this;
}
enum gauss_mode {normal, reverse};
template<gauss_mode mode = normal>
matrix& gauss() {
for(size_t i = 0; i < n(); i++) {
row(i).normalize();
for(size_t j = (mode == normal) * i; j < n(); j++) {
if(j != i) {
row(j).reduce_by(row(i));
}
}
}
return normalize();
}
template<gauss_mode mode = normal>
auto echelonize(size_t lim) {
return gauss<mode>().sort_classify(lim);
}
template<gauss_mode mode = normal>
auto echelonize() {
return echelonize<mode>(m());
}
size_t rank() const {
if(n() > m()) {
return T().rank();
}
return size(matrix(*this).echelonize()[0]);
}
base det() const {
assert(n() == m());
matrix b = *this;
b.echelonize();
base res = 1;
for(size_t i = 0; i < n(); i++) {
res *= b[i][i];
}
return res;
}
std::optional<matrix> inv() const {
assert(n() == m());
matrix b = *this | eye(n());
if(size(b.echelonize<reverse>(n())[0]) < n()) {
return std::nullopt;
}
for(size_t i = 0; i < n(); i++) {
b[i] *= base(1) / b[i][i];
}
return b.submatrix(std::slice(0, n(), 1), std::slice(n(), n(), 1));
}
// Can also just run gauss on T() | eye(m)
// but it would be slower :(
auto kernel() const {
auto A = *this;
auto [pivots, free] = A.template echelonize<reverse>();
matrix sols(size(free), m());
for(size_t j = 0; j < size(pivots); j++) {
base scale = base(1) / A[j][pivots[j]];
for(size_t i = 0; i < size(free); i++) {
sols[i][pivots[j]] = A[j][free[i]] * scale;
}
}
for(size_t i = 0; i < size(free); i++) {
sols[i][free[i]] = -1;
}
return sols;
}
// [solution, basis], transposed
std::optional<std::array<matrix, 2>> solve(matrix t) const {
matrix sols = (*this | t).kernel();
if(sols.n() < t.m() || sols.submatrix(
std::slice(sols.n() - t.m(), t.m(), 1),
std::slice(m(), t.m(), 1)
) != -eye(t.m())) {
return std::nullopt;
} else {
return std::array{
sols.submatrix(std::slice(sols.n() - t.m(), t.m(), 1),
std::slice(0, m(), 1)),
sols.submatrix(std::slice(0, sols.n() - t.m(), 1),
std::slice(0, m(), 1))
};
}
}
private:
// To be called after a gaussian elimination run
// Sorts rows by pivots and classifies
// variables into pivots and free
auto sort_classify(size_t lim) {
size_t rk = 0;
std::vector<size_t> free, pivots;
for(size_t j = 0; j < lim; j++) {
for(size_t i = rk + 1; i < n() && row(rk)[j] == base(0); i++) {
if(row(i)[j] != base(0)) {
std::swap(row(i), row(rk));
row(rk) = -row(rk);
}
}
if(rk < n() && row(rk)[j] != base(0)) {
pivots.push_back(j);
rk++;
} else {
free.push_back(j);
}
}
return std::array{pivots, free};
}
};
}
#line 6 "main.cpp"
#include <bits/stdc++.h>
using namespace std;
using namespace cp_algo::math;
using namespace cp_algo::linalg;
const int mod = 998244353;
using base = modint<mod>;
void solve() {
int n, m;
cin >> n >> m;
matrix<base> a(n);
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
a[u][v] -= 1;
a[v][u] -= 1;
a[v][v] += 1;
a[u][u] += 1;
}
for(int i = 0; i < n; i++) {
a[0][i] = a[i][0] = 0;
}
a[0][0] = 1;
cout << a.det() << "\n";
}
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
#define eb emplace_back
const int w_lim = 152501;
vector<ii> edges[w_lim + 5];
base f[w_lim + 5];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 0, u, v, w; i < m; i++) {
cin >> u >> v >> w;
edges[w].eb(u - 1, v - 1);
}
base ans = 0;
for (int i = w_lim; i >= 1; i--) {
matrix<base> a(n);
vector<iii> cur_edges;
for (int j = i; j <= w_lim; j += i) {
for (auto [u, v] : edges[j]) {
cur_edges.eb(u, v, j);
a[u][v] -= 1;
a[v][u] -= 1;
a[v][v] += 1;
a[u][u] += 1;
}
}
for (int i = 0; i < n; i++) {
a[0][i] = a[i][0] = 0;
}
a[0][0] = 1;
base cur = 0;
base tot = a.det();
if (tot > 0) {
for (int r = 0; r < (int)cur_edges.size(); r++) {
matrix<base> b(n);
for (int i = 0; i < (int)cur_edges.size(); i++) {
if (i == r) {
continue;
}
auto [u, v, w] = cur_edges[i];
b[u][v] -= 1;
b[v][u] -= 1;
b[v][v] += 1;
b[u][u] += 1;
}
for (int i = 0; i < n; i++) {
b[0][i] = b[i][0] = 0;
}
b[0][0] = 1;
auto [u_o, v_o, w_o] = cur_edges[r];
cur += (tot - b.det()) * get<2>(cur_edges[r]);
}
}
f[i] = cur;
for (int j = i + i; j <= w_lim; j += i) {
f[i] -= f[j];
}
ans += (base)i * f[i];
}
cout << ans << '\n';
}
詳細信息
Pretests
Final Tests
Test #1:
score: 10
Accepted
time: 108ms
memory: 6032kb
input:
5 10 1 2 113400 1 3 131040 1 4 131040 1 5 126000 2 3 131040 2 4 90720 2 5 138600 3 4 110880 3 5 110880 4 5 95760
output:
549298858
result:
ok 1 number(s): "549298858"
Test #2:
score: 10
Accepted
time: 120ms
memory: 6076kb
input:
6 15 1 2 110880 1 3 141120 1 4 141120 1 5 131040 1 6 151200 2 3 137280 2 4 105840 2 5 138600 2 6 150480 3 4 138600 3 5 131040 3 6 137280 4 5 85680 4 6 151200 5 6 131040
output:
627752274
result:
ok 1 number(s): "627752274"
Test #3:
score: 10
Accepted
time: 743ms
memory: 4824kb
input:
25 24 1 2 147840 1 3 98280 2 4 120960 3 5 128520 5 6 128520 6 7 143640 1 8 138600 7 9 131040 3 10 147840 10 11 120120 5 12 120120 4 13 131040 8 14 151200 13 15 151200 5 16 110880 11 17 120960 6 18 151200 6 19 147840 5 20 147840 1 21 120960 14 22 110880 13 23 120120 18 24 98280 1 25 131040
output:
623404094
result:
ok 1 number(s): "623404094"
Test #4:
score: 10
Accepted
time: 885ms
memory: 6192kb
input:
28 28 1 2 128520 2 3 98280 1 4 138600 4 5 98280 2 6 98280 1 7 131040 3 8 138600 2 9 147840 6 10 120120 1 11 120960 7 12 138600 6 13 147840 3 14 120960 6 15 120960 9 16 120120 4 17 143640 3 18 128520 15 19 147840 9 20 120120 6 21 120960 20 22 147840 10 23 131040 5 24 138600 7 25 143640 5 26 120960 4 ...
output:
640377458
result:
ok 1 number(s): "640377458"
Test #5:
score: 10
Accepted
time: 999ms
memory: 4884kb
input:
30 30 1 2 110880 1 3 120960 3 4 131040 3 5 120120 3 6 138600 5 7 98280 3 8 120960 5 9 128520 1 10 128520 2 11 138600 5 12 98280 10 13 98280 11 14 143640 3 15 120960 10 16 98280 16 17 138600 5 18 138600 13 19 147840 18 20 120960 13 21 131040 15 22 151200 22 23 131040 2 24 128520 6 25 120960 20 26 138...
output:
309797364
result:
ok 1 number(s): "309797364"
Test #6:
score: 10
Accepted
time: 752ms
memory: 6304kb
input:
25 25 1 2 131040 2 3 120120 2 4 98280 3 5 110880 5 6 120120 4 7 120960 1 8 128520 4 9 110880 3 10 147840 7 11 120960 4 12 120960 2 13 138600 8 14 120960 1 15 128520 8 16 131040 10 17 110880 14 18 98280 9 19 131040 7 20 147840 8 21 138600 14 22 138600 5 23 151200 7 24 131040 13 25 143640 12 1 143640
output:
302382070
result:
ok 1 number(s): "302382070"
Test #7:
score: 10
Accepted
time: 875ms
memory: 6544kb
input:
28 376 1 2 152501 1 3 152501 1 4 152501 1 5 152501 1 6 152501 1 7 152501 1 8 152501 1 9 152501 1 10 152501 1 11 152501 1 12 152501 1 13 152501 1 14 152501 1 15 152501 1 16 152501 1 17 152501 1 18 152501 1 19 152501 1 20 152501 1 21 152501 1 22 152501 1 23 152501 1 24 152501 1 25 152501 1 26 152501 1...
output:
493255263
result:
ok 1 number(s): "493255263"
Test #8:
score: 10
Accepted
time: 978ms
memory: 5996kb
input:
30 431 1 2 152501 1 3 152501 1 4 152501 1 5 152501 1 6 152501 1 7 152501 1 8 152501 1 9 152501 1 10 152501 1 11 152501 1 12 152501 1 13 152501 1 14 152501 1 15 152501 1 16 152501 1 17 152501 1 18 152501 1 19 152501 1 20 152501 1 21 152501 1 22 152501 1 23 152501 1 24 152501 1 25 152501 1 26 152501 1...
output:
441996120
result:
ok 1 number(s): "441996120"
Test #9:
score: 10
Accepted
time: 868ms
memory: 4804kb
input:
28 372 1 2 152501 1 3 152501 1 4 152501 1 5 152501 1 6 152501 1 7 152501 1 8 152501 1 9 152501 1 10 152501 1 11 152501 1 12 152501 1 13 152501 1 14 152501 1 15 152501 1 16 152501 1 17 152501 1 18 152501 1 19 152501 1 20 152501 1 21 152501 1 22 152501 1 23 152501 1 24 152501 1 25 152501 1 26 152501 1...
output:
179882346
result:
ok 1 number(s): "179882346"
Test #10:
score: 10
Accepted
time: 990ms
memory: 5092kb
input:
30 432 1 2 152501 1 3 152501 1 4 152501 1 5 152501 1 6 152501 1 7 152501 1 8 152501 1 9 152501 1 10 152501 1 11 152501 1 12 152501 1 13 152501 1 14 152501 1 15 152501 1 16 152501 1 17 152501 1 18 152501 1 19 152501 1 20 152501 1 21 152501 1 22 152501 1 23 152501 1 24 152501 1 25 152501 1 26 152501 1...
output:
45748263
result:
ok 1 number(s): "45748263"
Extra Test:
score: 0
Extra Test Passed