AtCoder Grand Contest 046 - Problem-Solving Report
A - Takahashikun, The Strider
A stupid problem, no explanation needed.
// A.cpp
#include <bits/stdc++.h>
using namespace std;
int x;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main()
{
scanf("%d", &x), printf("%d\n", 360 / gcd(x, 360));
return 0;
}
B - Extension
Not hard; just apply inclusion–exclusion directly.
// B.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 3030, mod = 998244353;
int n, m, a, b, dp[MAX_N][MAX_N];
int main()
{
scanf("%d%d%d%d", &a, &b, &n, &m);
dp[a][b] = 1;
for (int i = a; i <= n; i++)
for (int j = b; j <= m; j++)
if (i != a || j != b)
dp[i][j] = (0LL + 1LL * dp[i - 1][j] * j % mod + 1LL * dp[i][j - 1] * i % mod + mod - 1LL * dp[i - 1][j - 1] * (i - 1) % mod * (j - 1) % mod) % mod;
printf("%d\n", dp[n][m]);
return 0;
}
C - Shift
Consider treating 0 as a separator; then the operation can be seen as taking a 1 from one segment and putting it into another segment, producing a new string. This can be done with DP: let dp[i][j][k] be for the first \(i\) units, with \(j\) copies of \(1\) placed and \(k\) operations made. Brute-force transitions + pruning, and it passes.
// C.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 330, mod = 998244353;
int threshold, len, n, pre[MAX_N], dp[MAX_N][MAX_N][MAX_N], units[MAX_N];
char str[MAX_N];
int main()
{
scanf("%s%d", str + 1, &threshold), len = strlen(str + 1);
int last_pos = 0, ones = 0;
for (int i = 1; i <= len; i++)
ones += str[i] == '1';
for (int i = 1; i <= len + 1; i++)
if (str[i] != '1')
units[++n] = i - last_pos - 1, last_pos = i;
for (int i = 1; i <= n; i++)
pre[i] = pre[i - 1] + units[i];
dp[0][0][0] = 1, threshold = min(threshold, ones);
for (int i = 0; i < n; i++)
for (int j = pre[i]; j <= pre[n]; j++)
for (int k = 0; k <= threshold; k++)
if (dp[i][j][k])
{
for (int g = max(j, pre[i + 1]); g <= pre[n]; g++)
if (k + g - j - units[i + 1] <= threshold)
{
if (i + 1 == n && g - j > units[i + 1])
break;
int cost = g - j <= units[i + 1] ? 0 : g - j - units[i + 1];
dp[i + 1][g][k + cost] = (0LL + dp[i + 1][g][k + cost] + dp[i][j][k]) % mod;
}
}
int ans = 0;
for (int i = 0; i <= threshold; i++)
ans = (0LL + ans + dp[n][ones][i]) % mod;
printf("%d\n", ans);
return 0;
}
D - Secret Passage
You can probably figure it out, but it's a bit of a pain to write. First fix the length of the final string, \(|T|\), then among the first \(|S| - |T|\) characters choose half to keep and half to throw away (one of the first two must be chosen, and it has to be valid), then take a suffix of length \(|T| - \frac{|S| - |T|}{2}\) and insert it into \(T\) as a subsequence. Then count the essentially distinct strings. So, first we compute a number of ways: there are \(j\) extra \(0\)s and \(k\) extra \(1\)s, and a suffix of length \(i\) serves as the subsequence. After computing that, we still need to check validity. To check validity, we can maintain a vis array that records, for the first \(i\) characters with \(j\) \(0\)s and \(k\) \(1\)s taken out, how many characters are still left for the remaining prefix of \(|S|-(i-j-k)\). If there is a surplus (meaning greater than or equal to 0), then it's valid.
// D.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 330, mod = 998244353;
int n, dp[MAX_N][MAX_N][MAX_N], vis[MAX_N][MAX_N][MAX_N];
bool verdict[MAX_N][MAX_N][MAX_N];
char str[MAX_N];
int main()
{
scanf("%s", str + 1), n = strlen(str + 1);
dp[n][0][0] = 1;
for (int i = n; i >= 1; i--)
for (int j = 0; j + (n - i) <= n - 1; j++)
for (int k = 0; j + k + (n - i) <= n - 1; k++)
if (str[i] == '0')
{
dp[i - 1][j][k] = (0LL + dp[i - 1][j][k] + dp[i][j][k]) % mod;
dp[i][j][k + 1] = (0LL + dp[i][j][k + 1] + dp[i][j][k]) % mod;
}
else
{
dp[i - 1][j][k] = (0LL + dp[i - 1][j][k] + dp[i][j][k]) % mod;
dp[i][j + 1][k] = (0LL + dp[i][j + 1][k] + dp[i][j][k]) % mod;
}
memset(vis, -1, sizeof(vis)), vis[0][0][0] = 0;
for (int i = 0; i <= n; i++)
for (int j = 0; j <= (i >> 1); j++)
for (int k = 0; j + k <= (i >> 1); k++)
if (vis[i][j][k] != -1)
{
int c = vis[i][j][k];
if (i + 1 <= n && c != 0)
{
if (str[i + 1] == '0')
vis[i + 1][j + 1][k] = max(vis[i + 1][j + 1][k], c - 1);
if (str[i + 1] == '1')
vis[i + 1][j][k + 1] = max(vis[i + 1][j][k + 1], c - 1);
}
if (i + 2 <= n)
{
if (str[i + 1] == '0' || str[i + 2] == '0')
vis[i + 2][j + 1][k] = max(vis[i + 2][j + 1][k], c);
if (str[i + 1] == '1' || str[i + 2] == '1')
vis[i + 2][j][k + 1] = max(vis[i + 2][j][k + 1], c);
vis[i + 2][j][k] = max(vis[i + 2][j][k], c + 1);
}
}
for (int i = 0; i <= n; i++)
for (int j = n; j >= 0; j--)
for (int k = n; k >= 0; k--)
{
verdict[i][j][k] = vis[i][j][k] != -1;
if (i > 0)
verdict[i][j][k] |= verdict[i - 1][j][k];
verdict[i][j][k] |= verdict[i][j + 1][k] || verdict[i][j][k + 1];
}
int ans = 0;
for (int i = 0; i <= n; i++)
for (int j = 0; j + (n - i) <= n; j++)
for (int k = 0; j + k + (n - i) <= n; k++)
if (dp[i][j][k] && (n - i) + j + k != 0 && verdict[i][j][k])
ans = (0LL + ans + dp[i][j][k]) % mod;
printf("%d\n", ans);
return 0;
}
E - Permutation Cover
This problem is really nice.
First we need to know that for a solution to exist, \(2\min\{a_i\} \geq \max\{a_i\}\) must hold: consider that at the boundary where the maximum appears, three identical numbers would show up, and then it's GG.
If a solution exists, we can first consider constructing the first prefix, so that afterwards we can append greedily. Each time we enumerate the length of the segment appended after the previous segment, and take the lexicographically smallest one. If we first subtract this prefix's frequencies from \(a_i\) and then check: if \(2\min\{a_i\} \geq \max\{a_i\}\) holds, then we can just go straight 1, 2, 3, 4 like that, which is lexicographically smallest and only subtracts 1 from \(a_i\). Going on like this, the case \(2\min\{a_i\} + 1 = \max\{a_i\}\) will come up, but this case can be solved too: we can put the \(a_i\)-largest \(i\) before the \(a_j\)-smallest \(j\), and then merge with the remaining items.
// E.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1050;
int n, sum, ai[MAX_N], res[MAX_N], candid[MAX_N], pos[MAX_N];
int upper, lower, sa[MAX_N], sb[MAX_N], ans[MAX_N], ansptr;
bool compare(const int &x, const int &y)
{
if ((ai[x] == lower) == (ai[y] == lower))
return x < y;
else
return (ai[x] == lower) < (ai[y] == lower);
}
void solve(int len)
{
bool found = false;
for (int i = 1; i <= len; i++)
ai[pos[i]]--;
int min_val = *min_element(ai + 1, ai + 1 + n), max_val = *max_element(ai + 1, ai + 1 + n);
if ((min_val << 1) >= max_val)
{
for (int i = 1; i <= len; i++)
res[i] = pos[i];
sort(res + 1, res + 1 + len), found = true;
}
else if (((min_val << 1) | 1) == max_val)
{
upper = max_val, lower = min_val;
int ptra = 1, ptrb = 1, la = 0, lb = 0, ptr = 0;
for (int i = 1; i <= len; i++)
if (ai[pos[i]] == min_val || ai[pos[i]] == max_val)
sa[++la] = pos[i];
else
sb[++lb] = pos[i];
sort(sa + 1, sa + 1 + la, compare), sort(sb + 1, sb + 1 + lb);
// merge;
while (ptra <= la && ptrb <= lb)
if (sa[ptra] < sb[ptrb])
res[++ptr] = sa[ptra++];
else
res[++ptr] = sb[ptrb++];
while (ptra <= la)
res[++ptr] = sa[ptra++];
while (ptrb <= lb)
res[++ptr] = sb[ptrb++];
found = true;
for (int i = len + 1; i <= n; i++)
if (ai[pos[i]] == min_val)
found = false;
found |= (ai[sa[1]] == max_val);
}
for (int i = 1; i <= len; i++)
ai[pos[i]]++;
if (!found)
res[1] = 0;
}
bool lexicographicalCompare(int *s1, int *s2)
{
int ptr = 1;
while (s1[ptr] == s2[ptr])
ptr++;
return s1[ptr] < s2[ptr];
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &ai[i]), sum += ai[i], pos[i] = i;
if (((*min_element(ai + 1, ai + 1 + n)) << 1) < *max_element(ai + 1, ai + 1 + n))
puts("-1"), exit(0);
solve(n);
for (int i = 1; i <= n; i++)
ans[++ansptr] = res[i], ai[res[i]]--, pos[i] = res[i];
while (ansptr < sum)
{
memset(candid, 0, sizeof(candid)), candid[1] = n + 1;
int len = 0;
for (int i = 1; i <= n; i++)
{
solve(i);
if (res[1] != 0 && lexicographicalCompare(res, candid))
{
len = i;
for (int j = 1; j <= i; j++)
candid[j] = res[j];
}
}
for (int i = 1; i + len <= n; i++)
pos[i] = pos[i + len];
for (int i = 1; i <= len; i++)
ans[++ansptr] = pos[n - len + i] = candid[i], ai[candid[i]]--;
}
for (int i = 1; i <= ansptr; i++)
printf("%d ", ans[i]);
puts("");
return 0;
}