Skip to content

Commit

Permalink
HDOJ: 2024“钉耙编程”中国大学生算法设计超级联赛(9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Macesuted committed Aug 19, 2024
1 parent bb107f2 commit b128a2f
Show file tree
Hide file tree
Showing 4 changed files with 225 additions and 0 deletions.
70 changes: 70 additions & 0 deletions HDOJ/7529.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @file 7529.cpp
* @author Macesuted ([email protected])
* @date 2024-08-19
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

#define maxn 200005
#define mod 998244353

vector<int> graph[maxn];
bool g[maxn];
int64_t f[maxn], fac[maxn], ifac[maxn], pow2[maxn];

int64_t qpow(int64_t a, int64_t x) {
int64_t ans = 1;
while (x) {
if (x & 1) ans = ans * a % mod;
a = a * a % mod, x >>= 1;
}
return ans;
}
int64_t inv(int64_t x) { return qpow(x, mod - 2); }
int64_t C(int n, int m) { return fac[n] * ifac[m] % mod * ifac[n - m] % mod; }

void dfs(int p) {
if (graph[p].empty()) return f[p] = 1, g[p] = true, void();
f[p] = 1, g[p] = false;
int cnt[2] = {0, 1};
for (auto i : graph[p]) dfs(i), f[p] = f[p] * f[i] % mod, cnt[g[i]]++;
if (cnt[1] & 1) {
g[p] = true;
if (--cnt[1]) f[p] = f[p] * (C(cnt[1], cnt[1] / 2) + C(cnt[1], cnt[1] / 2 - 1)) % mod;
} else
f[p] = f[p] * C(cnt[1], cnt[1] >> 1) % mod;
return;
}

void solve(void) {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) graph[i].clear();
for (int i = 2, x; i <= n; i++) cin >> x, graph[x].push_back(i);
dfs(1);
if (g[1]) f[1] = f[1] * 2 % mod;
cout << qpow(f[1], k) << endl;
return;
}

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);

fac[0] = ifac[0] = pow2[0] = 1;
for (int i = 1; i < maxn; i++) fac[i] = fac[i - 1] * i % mod, pow2[i] = pow2[i - 1] * 2 % mod;
ifac[maxn - 1] = inv(fac[maxn - 1]);
for (int i = maxn - 2; i; i--) ifac[i] = ifac[i + 1] * (i + 1) % mod;

int _ = 1;
cin >> _;
while (_--) solve();

return 0;
}
45 changes: 45 additions & 0 deletions HDOJ/7533.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @file 7533.cpp
* @author Macesuted ([email protected])
* @date 2024-08-19
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

int64_t read() {
int64_t res = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') {
res = res * 10 + ch - '0';
ch = getchar();
}
return res;
}

void solve(void) {
bool a[2] = {false, false};
int64_t k = read(), x = read(), y = read();
if (x > y) swap(x, y);
int64_t n = (k - 1) / y + 1;
int t = n & 1;
a[!t] = true;
if (n * x < k) a[t] = true;
for (int i = 0; i < 2; i++) cout << (a[i] ? "Yes" : "No") << endl;
return;
}

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

int _ = read();
while (_--) solve();

return 0;
}
39 changes: 39 additions & 0 deletions HDOJ/7535.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @file 7535.cpp
* @author Macesuted ([email protected])
* @date 2024-08-19
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

#define maxn 200005

int a[maxn], b[maxn];

bool solve(void) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
sort(a + 1, a + n + 1), sort(b + 1, b + n + 1);
int cnt = 0;
for (int i = 1; i < n; i++) cnt += (a[i] == a[i + 1]);
if (cnt == 0) return false;
return true;
}

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);

int _ = 1;
cin >> _;
while (_--) cout << (solve() ? "shuishui" : "sha7dow") << endl;

return 0;
}
71 changes: 71 additions & 0 deletions HDOJ/7537.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @file 7537.cpp
* @author Macesuted ([email protected])
* @date 2024-08-19
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define maxn 4005

bool mem1;

typedef pair<int, int> pii;

const int64_t INF = 1e18;

int b[maxn], siz[maxn];
int64_t K;
vector<int64_t> f[2][maxn];
vector<vector<pii>> graph;

int64_t& F(int x, int y, int t) { return f[t][x][y + (f[t][x].size() >> 1)]; }

void dfs(int p, int pre = -1) {
// cerr << "! " << p << endl;
f[0][p] = f[1][p] = vector<int64_t>((siz[p] = 1) * 2 + 1, INF);
F(p, 0, 0) = F(p, 0, 1) = F(p, 1, 0) = F(p, 1, 1) = 0;
if (b[p]) F(p, -1, 1) = 0;
for (auto [q, d] : graph[p]) {
if (q == pre) continue;
dfs(q, p);
f[0][0] = f[0][p], f[1][0] = f[1][p];
f[0][p] = f[1][p] = vector<int64_t>((siz[p] + siz[q]) * 2 + 1, INF);
for (int i = -siz[p]; i <= siz[p]; i++)
for (int j = -siz[q]; j <= siz[q]; j++) {
F(p, i + j, 0) = min(F(p, i + j, 0), F(0, i, 0) + F(q, j, 0) + abs(j) * K);
F(p, i + j, 1) = min(F(p, i + j, 1), F(0, i, 1) + min(F(q, j, 0), F(q, j, 1) + d) + abs(j) * K);
}
siz[p] += siz[q];
}
F(p, 0, 0) = INF;
return;
}

void solve(void) {
int n;
cin >> n >> K, K *= 2;
for (int i = 1; i <= n; i++) cin >> b[i];
graph.clear(), graph.resize(n + 1);
for (int i = 1, x, y, w; i < n; i++) cin >> x >> y >> w, w <<= 1, graph[x].emplace_back(y, w), graph[y].emplace_back(x, w);
dfs(1);
cout << F(1, 0, 1) << endl;
return;
}

bool mem2;

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);

int _ = 1;
cin >> _;
while (_--) solve();

return 0;
}

0 comments on commit b128a2f

Please sign in to comment.