---
id: 2631
lang: en
translation_of: index.md
source_sha256: 4a99dc7168d6d3ec79c14ef0a71df83e31d379931927217920474171d10307bd
title: "AtCoder Grand Contest 036 - Problem-Solving Report"
description: "Solutions to AGC 036 A, B and C: a slanted triangle, binary lifting over repeated string clearing, and counting sequences with inclusion–exclusion."
tags: ["OI","Binary Lifting","Properties"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Title and section headings keep the stored ' - ' hyphen, as in the Chinese archive."
---

## A - Triangle

Just place the triangle at a slant.

```cpp
// A.cpp
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

ll S;

int main()
{
	scanf("%lld", &S);
	ll y = (S + int(1e9) - 1) / int(1e9), x = y * int(1e9) - S;
	printf("%d %d %d %d %lld %lld\n", 0, 0, int(1e9), 1, x, y);
	return 0;
}
```

<!--more-->

## B - Do Not Duplicate

Once you see $k \leq 10^{18}$, you definitely think it has something to do with binary lifting or fast matrix exponentiation.

Let $ans_i$ be the result of appending $a_i, a_{i + 1}, a_{i + 2}, \dots$ to an empty string; in particular, let $ans_0$ be the empty string. At the start the string is $ans_1$. When the second pass reaches $a_j$ and hits $ans_1[0]$, that is, the head element, the whole string gets cleared, so the final answer becomes $ans_{j + 1}$. The crazy iterating can just be done with binary lifting.

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

using namespace std;

const int MAX_N = 2e5 + 200, LOG = 60;

int n, ai[MAX_N], mem[MAX_N], nxt[MAX_N], up[LOG][MAX_N], ansBox[MAX_N];
long long k;

int main()
{
	scanf("%d%lld", &n, &k);
	for (int i = 1; i <= n; i++)
		scanf("%d", &ai[i]);
	for (int i = n; i >= 1; i--)
		mem[ai[i]] = i;
	for (int i = n; i >= 1; i--)
		nxt[i] = mem[ai[i]], mem[ai[i]] = i;
	up[0][0] = 1;
	for (int i = n; i >= 1; i--)
		if (nxt[i] <= i)
			if (nxt[i] == n)
				up[0][i] = 0;
			else
				up[0][i] = nxt[i] + 1;
		else if (nxt[i] == n)
			up[0][i] = 1;
		else
			up[0][i] = up[0][nxt[i] + 1];
	for (int i = 1; i < LOG; i++)
		for (int j = 0; j <= n; j++)
			up[i][j] = up[i - 1][up[i - 1][j]];
	int ans = 0;
	for (int i = LOG - 1; i >= 0; i--)
		if (k & (1LL << i))
			ans = up[i][ans];
	memset(mem, 0, sizeof(mem));
	int tot = 0;
	if (ans == 0)
		return 0;
	for (int i = ans; i <= n; i++)
	{
		if (mem[ai[i]] == 0)
			ansBox[++tot] = ai[i], mem[ai[i]] = tot;
		else
		{
			int pos = mem[ai[i]];
			while (tot >= pos)
				mem[ansBox[tot--]] = 0;
		}
	}
	for (int i = 1; i <= tot; i++)
		printf("%d ", ansBox[i]);
	puts("");
	return 0;
}
```

## C - GP 2

This problem is really interesting, a stack-the-properties problem.

First, there are $m$ chances in total to flip the parity of an $a_i$, so there are at most $m$ odd numbers, and no number exceeds $2m$. So in the end, the answer asks for sequences that satisfy:

- $\sum a_i = 3m$
- $\sum_{i = 1}^n [a_i \bmod 2 \equiv 1] \leq m$
- $\sum_{i = 1}^n [a_i \leq 2m] = 0$

Let's set the third condition aside first and only count the sequences satisfying the first two properties. Clearly, the number of such sequences is:

$$
\sum_{i = 1}^m [i \bmod 2 \equiv 3m \bmod 2] {\frac{3m - i}{2} + n - 1 \choose n - 1} {n \choose i}
$$

This is easy to understand. How do we satisfy the third condition? We can consider fixing one element to be at least $2m + 1$, and then just count with $3m$ changed to $m - 1$. Of course, the part removed by inclusion–exclusion has to be multiplied by $n$.

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

using namespace std;

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

int fac[MAX_N], fac_inv[MAX_N], inv[MAX_N];

void preprocess()
{
	for (int i = fac[0] = 1; i < MAX_N; i++)
		fac[i] = 1LL * fac[i - 1] * i % mod;
	inv[0] = inv[1] = fac_inv[0] = 1;
	for (int i = 2; i < MAX_N; i++)
		inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod;
	for (int i = 1; i < MAX_N; i++)
		fac_inv[i] = 1LL * fac_inv[i - 1] * inv[i] % mod;
}

int binomial(int n_, int k_) { return (n_ < k_) ? 0 : 1LL * fac[n_] * fac_inv[k_] % mod * fac_inv[n_ - k_] % mod; }

int calc(int n, int m, int o)
{
	int ret = 0;
	for (int i = 0; i <= o; i++)
		if (i % 2 == m % 2)
			ret = (0LL + ret + 1LL * binomial((m - i) / 2 + n - 1, n - 1) * binomial(n, i) % mod) % mod;
	return ret;
}

int main()
{
	int n, m;
	preprocess(), scanf("%d%d", &n, &m);
	printf("%lld\n", (0LL + calc(n, 3 * m, m) + mod - 1LL * n * calc(n, m - 1, m) % mod) % mod);
	return 0;
}
```
