[Miscellaneous Problems] Late January
I've been lazy as hell lately, so I haven't updated the blog much. Let me put together a small collection here.
[Yali Training Camp 2017 Day8] Together
This problem is so badass. If we want to build a tree like this, where \(k\) vertices all have odd depth, then we can totally do it on a bipartite graph: a bipartite graph has no odd cycles, so we can put the odd-depth and even-depth vertices on the two sides, and just count the spanning trees of this graph once. So, the result for counting the spanning trees of a “complete bipartite graph” is \(n^{m - 1}m^{n - 1}\); proof: BZOJ4766: Artsy Computing Princess (Prufer sequence) problem
[Yali Training Camp 2017 Day8] Grandpa
Reading the solution, I was stunned: it really is a damn sqrt decomposition problem. Consider doing sqrt decomposition on the DFS order, then maintaining it with tags. For queries, we can binary search on the answer to find the \(k\)-th smallest; for updates, the basic sqrt decomposition operations are enough. For finding how many numbers in a range are less than or equal to \(mid\), if we use lower_bound, the time complexity comes to \(\Theta(\sqrt{n}n\log n \log \sqrt{n}\), which times out (that's what I wrote during the contest; tested it myself, it gets 70 points). We notice that \(len\) is small, so we can just open an array to record it, and rebuild it periodically.
[Yali Training Camp 2017 Day5] Jewelry
A really nice knapsack + decision monotonicity problem. Monotonicity: we notice that this \(c_i\) is small, so we can sort the items of the same cost by value from largest to smallest, and then we find that the later an item is picked, the less value it brings. With that solved, we can classify the transitions: in the modular domain of \(c_i\), enumerate the value \(w\) after taking the modulus, and then enumerate the source positions, that is, the ones making \(w' \bmod c_i \equiv w\) hold, all such \(w'\). This process is rather harmonic-series. See the code for details; I think this trick of classifying by modular domain and doing the transitions in batches is really badass.
// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_K = 50010;
typedef long long ll;
int n, k, max_price, pos[MAX_K], ptot;
vector<ll> store[330];
ll dp[330][MAX_K];
int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x * f;
}
void solve(int idx, int l, int r, int LB, int RB)
{
if (LB > RB)
return;
int mid = (LB + RB) >> 1, gpt = l;
for (int i = max(l, int(mid - store[idx].size())); i <= min(mid, r); i++)
if (dp[idx][pos[mid]] < dp[idx - 1][pos[i]] + (i == mid ? 0 : store[idx][mid - i - 1]))
gpt = i, dp[idx][pos[mid]] = dp[idx - 1][pos[i]] + (i == mid ? 0 : store[idx][mid - i - 1]);
solve(idx, l, gpt, LB, mid - 1), solve(idx, gpt, r, mid + 1, RB);
}
int main()
{
n = read(), k = read();
for (int i = 1, c, v; i <= n; i++)
c = read(), v = read(), store[c].push_back(v), max_price = max(max_price, c);
for (int i = 1; i <= max_price; i++)
{
sort(store[i].begin(), store[i].end());
reverse(store[i].begin(), store[i].end());
for (int j = 1, siz = store[i].size(); j < siz; j++)
store[i][j] += store[i][j - 1];
}
for (int price = 1; price <= max_price; price++)
for (int start_pos = 0; start_pos < price; start_pos++)
{
ptot = 0;
for (int i = start_pos; i <= k; i += price)
pos[++ptot] = i;
solve(price, 1, ptot, 1, ptot);
}
for (int i = 1; i <= k; i++)
printf("%lld ", dp[max_price][i]);
putchar('\n');
return 0;
}
[CERC2017]Gambling Guide
The transition model of this problem is fairly classic: the approach of doing the transitions with a shortest-path algorithm. We let \(dp[u]\) be the expected number of coins spent going from vertex \(x\) to vertex \(n\). Then, the transition:
\[ dp[u] = \sum_{v \in nearby} \frac{\min\{dp[u], dp[v]\}}{deg[u]} + 1 \]
We can set the minimum to the vertex itself at the start, then just run the transitions with Dijsktra. We need to record the sum of incoming transitions and the number of incoming transitions, and then compute the vertex's own value.
// P4745.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 301000;
typedef pair<double, int> pdi;
int head[MAX_N], n, m, current, deg[MAX_N], cnt[MAX_N];
double dist[MAX_N], sum[MAX_N];
bool vis[MAX_N];
struct edge
{
int to, nxt;
} edges[MAX_N << 1];
void addpath(int src, int dst)
{
edges[current].to = dst, edges[current].nxt = head[src];
head[src] = current++;
}
void shortest_path()
{
priority_queue<pdi> pq;
pq.push(make_pair(0, n));
while (!pq.empty())
{
int u = pq.top().second;
pq.pop();
if (vis[u])
continue;
vis[u] = true;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (vis[edges[i].to] == false)
{
cnt[edges[i].to]++, sum[edges[i].to] += dist[u];
dist[edges[i].to] = double(sum[edges[i].to] + deg[edges[i].to]) / double(cnt[edges[i].to]);
pq.push(make_pair(-dist[edges[i].to], edges[i].to));
}
}
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d", &n, &m);
for (int i = 1, u, v; i <= m; i++)
scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u), deg[u]++, deg[v]++;
shortest_path(), printf("%.10lf\n", dist[1]);
return 0;
}
CF914G Sum the Fibonacci
First, let's look at the condition:
- \((s_a | s_b) \& s_c \& (s_d \oplus s_e) = 2^i, s_a \& s_b = 0\)
First, \(s_a | s_b, s_a \& s_b = 0\) is just subset convolution: expand it with FWT_OR and it's done in \(\Theta(n \log^2 n\); \(s_c\) doesn't need to be considered; for \(s_d \oplus s_e\), just run FWT_XOR on it. Then do these three parts with FWT_AND and that's it.