Olympiad in Informatics

AtCoder Grand Contest 038 - Problem-Solving Report

A - 01 Matrix

This problem is pretty brain-dead, so I'll just post the figure from the editorial:

AGC038A
https://img.atcoder.jp/agc038/editorial.pdf
// A.cpp
#include <bits/stdc++.h>

using namespace std;

int main()
{
	int n, m, A, B;
	scanf("%d%d%d%d", &n, &m, &B, &A);
	for (int i = 1; i <= n; i++, puts(""))
		for (int j = 1; j <= m; j++)
			if ((i <= A) ^ (j <= B))
				printf("1");
			else
				printf("0");
	return 0;
}

B - Sorting a Segment

This problem is somewhat interesting, I suppose. First, the answer is at most \(n - k + 1\), and then we can think about whether sorting two intervals has the same effect. There are two cases:

  • In the first case, the two intervals do not intersect, so the necessary and sufficient condition for the sorts to have the same effect is that both intervals are non-decreasing. This is easy to maintain with a sparse table.
  • In the second case, the two intervals intersect, with \(l_1 < l_2 < r_1 < l_2\). Then the two intervals \([l_1, l_2), (r_1, l_2]\) must be non-decreasing; as for \([l_2, r_1]\), its minimum must be no less than the maximum of \([l_1, l_2)\), and its maximum must be no greater than the minimum of \((r_1, l_2]\). If these conditions hold, the sorts clearly have the same effect.

We can observe a conclusion: if two intervals \([i, i + k - 1], [j, j + k - 1], i < j\) have the same effect, then \([i, i + k - 1]\) and \([x, x + k - 1], x \in [i, j]\) have the same effect. Once you know this conclusion, you can just scan in \(\Theta(n)\).

Of course, you also have to watch out for the case where the whole interval is non-decreasing.

// B.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5 + 200;

int n, k, pi[MAX_N], max_val[20][MAX_N], min_val[20][MAX_N], log2_[MAX_N];
bool st[20][MAX_N];

bool query(int l, int r)
{
	bool ret = true;
	for (int i = 19; i >= 0; i--)
		if (l + (1 << i) - 1 <= r)
		{
			ret &= st[i][l] && (l + (1 << i) - 1 == r || pi[l + (1 << i) - 1] <= pi[l + (1 << i)]);
			l += (1 << i);
		}
	return ret;
}

int queryMin(int l, int r)
{
	int d = log2_[r - l + 1];
	return min(min_val[d][l], min_val[d][r - (1 << d) + 1]);
}

int queryMax(int l, int r)
{
	int d = log2_[r - l + 1];
	return max(max_val[d][l], max_val[d][r - (1 << d) + 1]);
}

int main()
{
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; i++)
		scanf("%d", &pi[i]), st[0][i] = true, max_val[0][i] = min_val[0][i] = pi[i];
	for (int i = 2; i <= n; i++)
		log2_[i] = log2_[i >> 1] + 1;
	for (int i = 1; i < 20; i++)
		for (int j = 1; j + (1 << i) - 1 <= n; j++)
		{
			st[i][j] = st[i - 1][j] && st[i - 1][j + (1 << (i - 1))] && (pi[j + (1 << (i - 1)) - 1] <= pi[j + (1 << (i - 1))]);
			min_val[i][j] = min(min_val[i - 1][j], min_val[i - 1][j + (1 << (i - 1))]);
			max_val[i][j] = max(max_val[i - 1][j], max_val[i - 1][j + (1 << (i - 1))]);
		}
	int pl = -1, pr = -1, ans = 0;
	bool flag = false;
	for (int i = 1; i + k - 1 <= n; i++)
		if (pl == -1)
			ans++, pl = i, pr = i + k - 1, flag |= query(pl, pr);
		else if (i <= pr)
		{
			bool verdict = query(pl, i - 1) && query(pr + 1, i + k - 1);
			verdict &= queryMax(pl, i - 1) <= queryMin(i, pr) && queryMax(i, pr) <= queryMin(pr + 1, i + k - 1);
			if (verdict)
				continue;
			else if (!(query(i, i + k - 1) && flag))
				ans++, pl = i, pr = i + k - 1, flag |= query(pl, pr);
		}
		else if (query(pl, pr) && query(i, i + k - 1))
			continue;
		else if (!(query(i, i + k - 1) && flag))
			ans++, pl = i, pr = i + k - 1, flag |= query(pl, pr);
	printf("%d\n", ans);
	return 0;
}

C - LCMs

The editorial's method is pretty strange; I'll translate it here.

Let \(\sum_{d | x} c_d = \frac{1}{x}\); the original \(c_i\) can just be worked out any old way in \(\Theta(n \log n)\).

Consider expanding the original expression:

\[ \sum_{i = 1}^{n - 1} \sum_{j = i + 1}^n \frac{a_i a_j}{\gcd(a_i, a_j)} = \sum_{i = 1}^{n - 1} \sum_{j = i + 1}^n a_i a_j \sum_{d|i, d|j} c_d \]

Swap the order:

\[ \sum_{d = 1}^{upper} c_d \sum_{i = 1}^{n - 1} \sum_{j = i + 1}^n a_i a_j [d | a_i] [d | a_j] \]

Run the sieve of Eratosthenes in one go and that's it.

// C.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e6 + 200, mod = 998244353;

int n, upper, ai[MAX_N], bi[MAX_N], ci[MAX_N];

int fpow(int bas, int tim)
{
	int ret = 1;
	while (tim)
	{
		if (tim & 1)
			ret = 1LL * ret * bas % mod;
		bas = 1LL * bas * bas % mod;
		tim >>= 1;
	}
	return ret;
}

const int inv2 = fpow(2, mod - 2);

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &ai[i]), bi[ai[i]]++;
	upper = *max_element(ai + 1, ai + 1 + n);
	for (int i = 1; i <= upper; i++)
		ci[i] = fpow(i, mod - 2);
	for (int i = 1; i <= upper; i++)
		for (int j = (i << 1); j <= upper; j += i)
			ci[j] = (0LL + ci[j] + mod - ci[i]) % mod;
	int ans = 0;
	for (int d = 1; d <= upper; d++)
	{
		int p1 = 0, p2 = 0;
		for (int i = d; i <= upper; i += d)
			p1 = (0LL + p1 + 1LL * bi[i] * i % mod) % mod, p2 = (0LL + p2 + 1LL * bi[i] * i % mod * i % mod) % mod;
		int pans = (0LL + 1LL * p1 * p1 % mod + mod - p2) % mod * inv2 % mod;
		ans = (0LL + ans + 1LL * pans * ci[d] % mod) % mod;
	}
	printf("%d\n", ans);
	return 0;
}

D - Unique Path

For this problem you can roughly see an approach: first, the shape is a tree made up of a number of biconnected components, so when we are free to mess around, the only criterion for deciding YES or NO is whether \(m\) is within the specified range. We need to compute the lower and upper bounds on the number of edges. Of course, the invalid cases have to be ruled out beforehand.

We can build a DSU over the \(0\) queries. First, if the two vertices of a \(1\) query are in the same connected component, it is definitely invalid; that's obvious.

The reverse graph of this DSU is exactly the valid graph we want, and they clearly correspond one to one. We can accumulate \({k \choose 2}\) over the cliques and then compute the answer interval.

// D.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200;

int n, m, q, mem[MAX_N], siz[MAX_N];

struct clue
{
	int a, b, c;
} clues[MAX_N];

int find(int x) { return mem[x] == x ? x : mem[x] = find(mem[x]); }

int main()
{
	scanf("%d%d%d", &n, &m, &q);
	for (int i = 1; i <= n; i++)
		mem[i] = i;
	for (int i = 1; i <= q; i++)
	{
		scanf("%d%d%d", &clues[i].a, &clues[i].b, &clues[i].c);
		clues[i].a++, clues[i].b++;
		if (clues[i].c == 0 && find(clues[i].a) != find(clues[i].b))
			mem[find(clues[i].a)] = find(clues[i].b);
	}
	int component = 0, c1 = 0;
	for (int i = 1; i <= n; i++)
		component += find(i) == i;
	for (int i = 1; i <= q; i++)
		if (clues[i].c == 1)
		{
			c1++;
			int u = find(clues[i].a), v = find(clues[i].b);
			if (u == v)
				puts("No"), exit(0);
		}
	long long lft = c1 ? max(3, component) : component - 1, rig = 1LL * component * (component - 1) / 2;
	puts((lft <= m - (n - component) && m - (n - component) <= rig) ? "Yes" : "No");
	return 0;
}

E - Gachapon

Now we come to the most interesting problem.

First, we know that this problem asks for the expected number of times until the whole set satisfies \(x_i \geq b_i\). Then, via min-max inclusion–exclusion, this can be converted into finding the expected number of times until at least one satisfies \(x_i \geq b_i\).

So, to compute this: by linearity of expectation, “the expected number of times until at least one satisfies \(x_i \geq b_i\)” equals “the sum of the expected counts of the states \((x_1, x_2, x_3, \dots, x_n), \forall x_i < b_i\), multiplied by their probabilities”.

I know this is pretty black magic; it took me a long time to understand it (nothing special, though).

Let's abstract each state \((x_1, x_2, x_3, \dots, x_n)\) as a vertex of a DAG. Clearly the start is the all-zero state, and the terminal nodes are \((x_1, x_2, x_3, \dots, x_n), \exists x_i = b_i\); for these terminal nodes, we can add a virtual node to take in their transitions. The edges between states are the transitions between states, and each is weighted with the expected number of times to pass through it successfully. Clearly the edges into the virtual node have weight \(0\).

So our goal is to compute \(E(S \to T)\). Normally, you could consider computing it with DP on the DAG. We can write out the probabilities and expectations and get:

\[ (f(u) + w(u, v)) \times P(S \to u) \times P(u \to v) \to f(v) \]

However, we don't need to build the graph to do this; we can consider computing the contribution of each edge directly.

\[ prod = P(S \to u) P(u \to v) P(v \to T) w(u, v) \]

First, this \(P(v \to T)\) is pretty stupid, because every way reaches \(T\), so \(P(v \to T) = 1\).

\[ prod = P(S \to u) P(u \to v) w(u, v) \]

That already looks hard to compute. Let's bundle things up for a vertex \(u\) and compute \(G(u) = \sum_{(u, v) \in E} P(u \to v) w(u, v)\).

How do we compute this? Actually, if you think about what \(G(u)\) means, it is really just the expected number of times to jump out of this state. So:

\[ G(u) = \frac{\sum_{i \in u} a_i }{\sum a_i} \times 1 + (1 - \frac{\sum_{i \in u} a_i }{\sum a_i}) \times (G(u) + 1) \]

We get:

\[ G(u) = \frac{\sum a_i }{\sum_{i \in u} a_i} \]

That's done; back to the part where we were computing contributions:

\[ prod_u = P(S \to u) G(u) \]

Now we need to compute \(P(S \to u)\), which is also simple: consider the output sequence of the random numbers, and then work it out:

\[ P(S \to u) = \frac{(\sum x_i)!}{\prod_{i \in u} x_i !} \prod_{i \in u} (\frac{a_i}{\sum_{j \in u} a_j})^{x_i} \]

So the final single contribution is:

\[ prod = \sum_{u} (\sum a_i) \times (\sum x_i)! (\frac{1}{\sum_{i \in u} a_i})^{1 + \sum x_i} \prod_{i \in u} \frac{a_i^{x_i}}{x_i!} \]

This can be done with a knapsack DP; just remember to add the min-max inclusion–exclusion sign at each transition.

// E.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 440, mod = 998244353;

int n, ai[MAX_N], bi[MAX_N], sa, sb, inv[MAX_N], fac[MAX_N], dp[2][MAX_N][MAX_N];

int fpow(int bas, int tim)
{
	int ret = 1;
	while (tim)
	{
		if (tim & 1)
			ret = 1LL * ret * bas % mod;
		bas = 1LL * bas * bas % mod;
		tim >>= 1;
	}
	return ret;
}

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d%d", &ai[i], &bi[i]), sa += ai[i], sb += bi[i];
	inv[0] = inv[1] = 1;
	for (int i = 2; i < MAX_N; i++)
		inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
	for (int i = fac[0] = 1; i < MAX_N; i++)
		fac[i] = 1LL * fac[i - 1] * i % mod;
	for (int i = 1; i < MAX_N; i++)
		inv[i] = 1LL * inv[i] * inv[i - 1] %
	
	int b = 0;
	dp[0][0][0] = mod - 1;
	for (int i = 1; i <= n; i++)
	{
		b ^= 1;
		for (int s = 0; s <= sa; s++)
			for (int c = 0; c <= sb; c++)
			{
				dp[b][s][c] = dp[!b][s][c];
				if (s < ai[i])
					continue;
				for (int idx = 0, acc = 1; idx < bi[i] && idx <= c; idx++, acc = 1LL * acc * ai[i] % mod)
					dp[b][s][c] = (0LL + dp[b][s][c] + mod - 1LL * acc * inv[idx] % mod * dp[!b][s - ai[i]][c - idx] % mod) % mod;
			}
	}
	int ans = 0;
	for (int s = 0; s <= sa; s++)
	{
		int unit = fpow(s, mod - 2);
		for (int idx = 0, acc = unit; idx <= sb; idx++, acc = 1LL * acc * unit % mod)
			ans = (0LL + ans + 1LL * fac[idx] * acc % mod * dp[b][s][idx] % mod) % mod;
	}
	ans = 1LL * ans * sa % mod, printf("%d\n", ans);
	return 0;
}