花火大会,等一下,花火?
2024-10-22 20:25:22
发布于:北京
6阅读
0回复
0点赞
#include <bits/stdc++.h>
template<int m>
class Modint {
private:
unsigned int _v;
static constexpr unsigned int umod() {return m;}
public:
static constexpr int mod() {return m;}
static Modint raw(int v) {
Modint x;
x._v = v;
return x;
}
Modint() : _v(0) {}
template<class T>
Modint(T v) {
long long x = (long long)(v % (long long)(umod()));
if (x < 0) x += umod();
_v = (unsigned int)(x);
}
unsigned int val() const {return _v;}
Modint& operator++() {
_v++;
if (_v == umod()) _v = 0;
return *this;
}
Modint& operator--() {
if (_v == 0) _v = umod();
_v--;
return *this;
}
Modint operator++(int) {
Modint result = *this;
++*this;
return result;
}
Modint operator--(int) {
Modint result = *this;
--*this;
return result;
}
Modint& operator+=(const Modint& rhs) {
_v += rhs._v;
if (_v >= umod()) _v -= umod();
return *this;
}
Modint& operator-=(const Modint& rhs) {
_v -= rhs._v;
if (_v >= umod()) _v += umod();
return *this;
}
Modint& operator*=(const Modint& rhs) {
unsigned long long z = _v;
z *= rhs._v;
_v = (unsigned int)(z % umod());
return *this;
}
Modint& operator/=(const Modint& rhs) {return *this = *this * rhs.inv();}
Modint operator+() const {return *this;}
Modint operator-() const {return Modint() - *this;}
Modint pow(long long n) const {
if (n < 0) return pow(-n).inv();
Modint x = *this, r = 1;
while (n) {
if (n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
Modint inv() const {
assert(_v);
return pow(umod() - 2);
}
friend Modint operator+(const Modint& lhs, const Modint& rhs) {
return Modint(lhs) += rhs;
}
friend Modint operator-(const Modint& lhs, const Modint& rhs) {
return Modint(lhs) -= rhs;
}
friend Modint operator*(const Modint& lhs, const Modint& rhs) {
return Modint(lhs) *= rhs;
}
friend Modint operator/(const Modint& lhs, const Modint& rhs) {
return Modint(lhs) /= rhs;
}
friend bool operator==(const Modint& lhs, const Modint& rhs) {
return lhs._v == rhs._v;
}
friend bool operator!=(const Modint& lhs, const Modint& rhs) {
return lhs._v != rhs._v;
}
};
template<class T>
using Vec = std::vector<T>;
template<class T>
using VVec = Vec<Vec<T>>;
constexpr int dirs[][2] {0, 1, 1, 0, 0, -1, -1, 0};
constexpr int BASE = 233;
int main() {
std::cin.tie(nullptr)->sync_with_stdio(false);
int n, m, k;
std::cin >> n >> m >> k;
Vec<std::tuple<int, int, int>> a(k);
int delta = 1e9;
for (auto &[x, y, z] : a) {
std::cin >> x >> y >> z;
delta = std::min(delta, z);
}
Vec<std::queue<std::pair<int, int>>> q(n + m);
for (auto &[x, y, z] : a)
if (z - delta < n + m)
q[z - delta].emplace(x - 1, y - 1);
VVec<int> dist(n, Vec<int>(m));
for (int i = 0; i < n + m - 1; ++i)
while (!q[i].empty()) {
auto [x, y] = q[i].front(); q[i].pop();
if (dist[x][y]) continue;
dist[x][y] = i + delta;
for (auto &[dx, dy] : dirs) {
int nx = x + dx, ny = y + dy;
if (nx < 0 or nx >= n or ny < 0 or ny >= m) continue;
if (dist[nx][ny]) continue;
q[i + 1].emplace(nx, ny);
}
}
using mint = Modint<998244353>;
mint res = 0, base = BASE;
for (int i = 0; i < m; ++i)
base *= BASE;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
res += dist[i][j] * base;
base *= BASE;
}
std::cout << res.val() << '\n';
return 0;
}
这里空空如也
有帮助,赞一个