Comet OJ - Contest #5 简要题解

题目链接

https://cometoj.com/contest/46

题解

A. 迫真字符串

$$s_i$$ 表示数字 $$i$$ 出现的次数，答案为 $$\min\{\lfloor\frac{s_1}{3}\rfloor, \lfloor\frac{s_4}{2}\rfloor, s_5\}$$

#include<bits/stdc++.h>

using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
int a = 0, b = 0, c = 0, n = s.length();
for (int i = 0; i < n; ++i) {
if (s[i] == '1') {
++a;
} else if (s[i] == '4') {
++b;
} else if (s[i] == '5') {
++c;
}
}
cout << min(min(a / 3, b / 2), c) << '\n';
return 0;
}


B. 迫真数论

#include<bits/stdc++.h>

using namespace std;

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt;
long long n;
cin >> tt;
while (tt--) {
cin >> n;
auto f = [&] (int x) {
int result = 0;
while (x) {
result += x % 10;
x /= 10;
}
return result;
};
for (int i = 1; i <= 200; ++i) {
if (n % i == 0 && f(i) == (i >> 1)) {
}
}
}
return 0;
}


C. 迫真小游戏

#include<bits/stdc++.h>

using namespace std;

const int N = 567890;

int n, a[N], depth[N];
bool visit[N];

void dfs(int x, int f) {
nodes[depth[x] = depth[f] + 1].push_back(x);
for (auto y : adj[x]) {
if (y != f) {
dfs(y, x);
}
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1, x, y; i < n; ++i) {
cin >> x >> y;
}
dfs(1, 0);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
multiset<int> s;
priority_queue<int, vector<int>, greater<int>> q;
for (int i = 2; i <= n; ++i) {
s.insert(a[i]);
}
cout << 1 << " \n"[n == 1];
int j = 1, tt = 1;
while (tt != n) {
while (s.size() && j < *s.begin()) {
++j;
for (auto x : nodes[j]) {
q.push(x);
}
}
int x = q.top();
s.erase(s.find(a[x]));
q.pop();
cout << x << " \n"[++tt == n];
}
return 0;
}


D. 迫真图论

#include<bits/stdc++.h>

using namespace std;

const int N = 1 << 18, sq = 2000, mod = 998244353;

int n, m, q, tt, degree[N], a[N], x[N], y[N], z[N], ch[N * 100][2], root[N], tag[N], now_tag;
long long sum[N * 100];
bool type[N];

class bit {
long long a[N];

public:
bit() {
memset(a, 0, sizeof a);
}

void add(int x, int y) {
if (!x) {
a[0] += y;
return;
}
while (x < N) {
a[x] += y;
x += x & -x;
}
}

long long sum(int x) {
long long result = a[0];
while (x) {
result += a[x];
x -= x & -x;
}
return result;
}
} tree;

void insert(int& x, int d, int y, int z, int now_tag) {
if (!x) {
x = ++tt;
}
sum[x] += z;
if (!~d) {
return;
}
insert(ch[x][(y >> d & 1) ^ (now_tag >> d & 1)], d - 1, y, z, now_tag);
}

long long query(int x, int d, int y, int now_tag) {
if (!x) {
return 0;
}
if (!~d) {
return sum[x];
}
if (y >> d & 1) {
if (now_tag >> d & 1) {
return sum[ch[x][1]] + query(ch[x][0], d - 1, y, now_tag);
} else {
return sum[ch[x][0]] + query(ch[x][1], d - 1, y, now_tag);
}
} else {
if (now_tag >> d & 1) {
return query(ch[x][1], d - 1, y, now_tag);
} else {
return query(ch[x][0], d - 1, y, now_tag);
}
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= m; ++i) {
cin >> x[i] >> y[i] >> z[i];
++degree[x[i]];
++degree[y[i]];
}
for (int i = 1; i <= n; ++i) {
if (degree[i] <= sq) {
type[i] = false;
} else {
type[i] = true;
}
}
vector<int> snodes;
for (int i = 1; i <= n; ++i) {
if (type[i]) {
tag[i] = a[i];
snodes.push_back(i);
for (auto p : adj[i]) {
if (type[p.first]) {
}
}
}
}
for (int i = 1; i <= n; ++i) {
for (auto p : adj[i]) {
int j = p.first;
if (type[i] == type[j] && i < j) {
} else if (type[i] && !type[j]) {
insert(root[i], 16, a[i] ^ a[j], z[p.second], tag[i]);
}
}
}
for (int i = 1, op, u, v; i <= q; ++i) {
cin >> op >> u >> v;
if (op == 1) {
if (!type[u]) {
for (auto p : adj[u]) {
if (!type[p.first]) {
} else {
insert(root[p.first], 16, a[u] ^ a[p.first], -z[p.second], tag[p.first]);
insert(root[p.first], 16, v ^ a[p.first], z[p.second], tag[p.first]);
}
}
} else {
tag[u] ^= a[u] ^ v;
for (auto p : sadj[u]) {
}
}
a[u] = v;
} else if (op == 2) {
int s = x[u], t = y[u];
if (type[s] == type[t]) {
} else {
if (type[t]) {
swap(s, t);
}
insert(root[s], 16, a[s] ^ a[t], -z[u], tag[s]);
insert(root[s], 16, a[s] ^ a[t], v, tag[s]);
}
z[u] = v;
} else {
--u;
long long answer = tree.sum(v) - (~u ? tree.sum(u) : 0);
for (auto x : snodes) {
answer += query(root[x], 16, v, tag[x]) - (~u ? query(root[x], 16, u, tag[x]) : 0);
}
cout << (answer % mod) << '\n';
}
}
return 0;
}


E. 迫真大游戏

#include<bits/stdc++.h>

using namespace std;

const int N = 567890, mod = 998244353, root = 3;

void add(int& x, int y) {
x += y;
if (x >= mod) {
x -= mod;
}
}

int mul(int x, int y) {
return (long long) x * y % mod;
}

int qpow(int x, int y) {
int result = 1;
for (; y; y >>= 1, x = mul(x, x)) {
if (y & 1) {
result = mul(result, x);
}
}
return result;
}

int n, a, b, p, rev[N], f[N], fac[N], ifac[N], inv[N];

void dft(vector<int>& buffer, bool inv = false) {
int n = buffer.size();
for (int i = 0; i < n; ++i) {
if (i < rev[i]) {
swap(buffer[i], buffer[rev[i]]);
}
}
for (int i = 1; i < n; i <<= 1) {
int x = qpow(root, inv ? mod - 1 - (mod - 1) / (i << 1) : (mod - 1) / (i << 1));
for (int j = 0; j < n; j += i << 1) {
int y = 1;
for (int k = 0; k < i; ++k, y = mul(y, x)) {
int p = buffer[j + k], q = mul(y, buffer[i + j + k]);
buffer[j + k] = (p + q) % mod;
buffer[i + j + k] = (p - q + mod) % mod;
}
}
}
if (inv) {
int x = qpow(n, mod - 2);
for (int i = 0; i < n; ++i) {
buffer[i] = mul(buffer[i], x);
}
}
}

vector<int> pmul(vector<int> x, vector<int> y) {
int n = x.size() + y.size() - 1, len = 0;
for (; (1 << len) < n; ++len);
for (int i = 0; i < (1 << len); ++i) {
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << len - 1);
}
x.resize(1 << len);
y.resize(1 << len);
dft(x);
dft(y);
for (int i = 0; i < (1 << len); ++i) {
x[i] = mul(x[i], y[i]);
}
dft(x, true);
x.resize(n);
return x;
}

void solve(int l, int r) {
if (l == r) {
if (l == 1) {
f[1] = 1;
} else {
f[l] = mul(f[l], fac[l - 1]);
f[l] = mul(f[l], qpow(1 - qpow(1 - p + mod, l) + mod, mod - 2));
}
} else {
int mid = l + r >> 1;
solve(l, mid);
vector<int> foo(mid - l + 1), bar(r - l);
for (int i = l; i <= mid; ++i) {
foo[i - l] = mul(f[i], mul(qpow(1 - p + mod, i), ifac[i - 1]));
}
for (int i = 1; i <= r - l; ++i) {
bar[i - 1] = mul(qpow(p, i), ifac[i]);
}
foo = pmul(foo, bar);
for (int i = mid + 1; i <= r; ++i) {
add(f[i], foo[i - l - 1]);
}
solve(mid + 1, r);
}
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> a >> b;
p = mul(a, qpow(b, mod - 2));
fac[0] = ifac[0] = inv[1] = fac[1] = ifac[1] = 1;
for (int i = 2; i <= n; ++i) {
inv[i] = mul(mod - mod / i, inv[mod % i]);
fac[i] = mul(fac[i - 1], i);
ifac[i] = mul(ifac[i - 1], inv[i]);
}
solve(1, n);
vector<int> foo(n), bar(n);
for (int i = 0; i < n; ++i) {
foo[i] = mul(f[n - i], mul(qpow(p, i), ifac[i]));
bar[i] = mul(qpow(1 - p + mod, i), ifac[i]);
}
foo = pmul(foo, bar);
for (int i = 0; i < n; ++i) {
cout << mul(foo[i], fac[i]) << '\n';
}
return 0;
}


F. 迫真树

#include<bits/stdc++.h>

using namespace std;

const int N = 123456;
const long long llinf = 1e18;

int n, m, tt, a[N], heavy[N], dfn[N], maxd[N];
long long dp0[N];

class segment_t {
long long a[N << 2], tag[N << 2];

public:
segment_t() {
fill(a, a + (N << 2), llinf);
memset(tag, 0, sizeof tag);
}

void mark(int x, long long y) {
tag[x] += y;
a[x] += y;
}

void push(int x) {
if (tag[x]) {
mark(x << 1, tag[x]);
mark(x << 1 | 1, tag[x]);
tag[x] = 0;
}
}

void modify(int l, int r, int x, int ql, int qr, long long y) {
if (ql <= l && r <= qr) {
mark(x, y);
} else {
int mid = l + r >> 1;
push(x);
if (ql <= mid) {
modify(l, mid, x << 1, ql, qr, y);
}
if (qr > mid) {
modify(mid + 1, r, x << 1 | 1, ql, qr, y);
}
a[x] = min(a[x << 1], a[x << 1 | 1]);
}
}

void modify(int l, int r, int x, int p, long long y) {
if (l == r) {
a[x] = min(a[x], y);
} else {
int mid = l + r >> 1;
push(x);
if (p <= mid) {
modify(l, mid, x << 1, p, y);
} else {
modify(mid + 1, r, x << 1 | 1, p, y);
}
a[x] = min(a[x << 1], a[x << 1 | 1]);
}
}

long long query(int l, int r, int x, int ql, int qr) {
if (ql <= l && r <= qr) {
return a[x];
} else {
int mid = l + r >> 1;
long long result = llinf;
push(x);
if (ql <= mid) {
result = min(result, query(l, mid, x << 1, ql, qr));
}
if (qr > mid) {
result = min(result, query(mid + 1, r, x << 1 | 1, ql, qr));
}
return result;
}
}
};

void dfs(int x, int f) {
maxd[x] = -1;
for (auto y : adj[x]) {
if (y != f) {
dfs(y, x);
if (maxd[y] > maxd[x]) {
heavy[x] = y;
maxd[x] = maxd[y];
}
}
}
++maxd[x];
}

bool check(int k) {
segment_t tree;
auto dp = [&] (int x, int y) {
return !y ? dp0[x] : tree.query(1, n, 1, dfn[x] + y - 1, dfn[x] + y - 1);
};
function<void (int, int)> dfs = [&] (int x, int f) {
dp0[x] = a[x];
dfn[x] = ++tt;
if (maxd[x]) {
dfs(heavy[x], x);
dp0[x] += min(dp(heavy[x], 0), tree.query(1, n, 1, dfn[heavy[x]], dfn[heavy[x]] + min(maxd[heavy[x]], k - 1)));
tree.modify(1, n, 1, dfn[x], dp(heavy[x], 0));
} else {
tree.modify(1, n, 1, dfn[x], 0);
}
for (auto y : adj[x]) {
if (y != f && y != heavy[x]) {
dfs(y, x);
dp0[x] += min(dp(y, 0), tree.query(1, n, 1, dfn[y], dfn[y] + min(maxd[y], k - 1)));
vector<long long> foo;
vector<pair<int, long long>> bar;
for (int j = 1; j <= min(maxd[y] + 2, k); ++j) {
int l = 1, r = min(j, k - j + 1);
if (l > r) {
break;
}
long long t = dp(y, j - 1);
foo.push_back(t + tree.query(1, n, 1, dfn[x] + l - 1, dfn[x] + r - 1));
if (!bar.size() || (bar.size() && t < bar.back().second)) {
bar.emplace_back(j - 1, t);
}
}
long long last = 0;
for (auto p : bar) {
int l = p.first + 1, r = min(maxd[x] + 1, k - p.first);
if (l <= r) {
tree.modify(1, n, 1, dfn[x] + l - 1, dfn[x] + r - 1, p.second - last);
last = p.second;
}
}
for (int i = 0; i < foo.size(); ++i) {
tree.modify(1, n, 1, dfn[x] + i, foo[i]);
}
}
}
};
tt = 0;
dfs(1, 0);
return min(dp(1, 0), tree.query(1, n, 1, dfn[1], dfn[1] + min(maxd[1], k - 1))) <= m;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
long long all = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
all += a[i];
}
if (all <= m) {
cout << 0 << '\n';
exit(0);
}
for (int i = 1, x, y; i < n; ++i) {
cin >> x >> y;
}
dfs(1, 0);
int l = 1, r = n;
while (l != r) {
int mid = l + r >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
cout << l << '\n';
return 0;
}

posted @ 2019-06-18 16:33  ImagineC  阅读(496)  评论(2编辑  收藏  举报