π§© AtCoder Beginner Contest 370
π§© AtCoder Beginner Contest 370
# Info & Summary
- Date:
2024-09-07
- Completion Status: A β / B β / C β / D β / E β / F β / G β
- Problem Type:
Problem | Type(s) | Data Structure / Algo | π₯ Key Insight / Mark |
---|---|---|---|
E | DP | DP & Dynamic ModInt | π§ π οΈπ |
F | Binary Search | Binary Search | π |
G | multiplicative function | Lucy DP | π |
Details
Emoji | Label | Meaning |
---|---|---|
β | Important | Problem is strategically useful or has reusable patterns |
π₯ | Must master | Classic problem or technique that appears often and is worth mastering |
β οΈ | Reattempt | You attempted but failed; worth reviewing and solving again |
π§ | New to me | Introduced a new idea, technique, or pattern you hadnβt seen before |
π οΈ | Template | You built or improved a code template from solving this problem |
π | Too hard now | Beyond current ability to understand/solve; revisit after more practice |
π A - Raise Both Hands

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int l, r;
cin >> l >> r;
if (l == 1 && r == 0)
{
cout << "Yes" << endl;
} else if (l == r)
{
cout << "Invalid" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
π B - Binary Alchemy

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<vector<int>> A(N + 1, vector<int>(N + 1));
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= i; j++)
{
cin >> A[i][j];
}
}
int ans = 1;
for (int i = 1; i <= N; i++)
{
if (ans >= i)
{
ans = A[ans][i];
} else {
ans = A[i][ans];
}
// cout << ans << endl;
}
cout << ans << endl;
return 0;
}
π C - Word Ladder

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
string S, T;
cin >> S >> T;
int N = S.size();
vector<string> ans;
// -------------- answerr 1 -----------------
// while (S != T)
// {
// int rst = -1;
// bool change = false;
// for (int i = 0; i < N; i++)
// {
// if (S[i] < T[i])
// {
// rst = i;
// } else if (S[i] > T[i])
// {
// S[i] = T[i];
// ans.push_back(S);
// change = true;
// break;
// }
// }
// if (!change)
// {
// S[rst] = T[rst];
// ans.push_back(S);
// }
// }
// -------------- answerr 2 -----------------
for (int i = 0; i < N; i++)
{
if (S[i] > T[i])
{
S[i] = T[i];
ans.push_back(S);
}
}
for (int i = N - 1; i >= 0; i--)
{
if (S[i] < T[i])
{
S[i] = T[i];
ans.push_back(S);
}
}
cout << ans.size() << endl;
for (auto s : ans)
{
cout << s << endl;
}
return 0;
}
π D - Cross Explosion

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H, W, Q;
cin >> H >> W >> Q;
vector<set<int>> r(H), c(W);
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
r[i].insert(j);
c[j].insert(i);
}
}
int ans = H * W;
for (int i = 0; i < Q; i++)
{
int R, C;
cin >> R >> C;
R--;
C--;
if (r[R].count(C))
{
r[R].erase(C);
c[C].erase(R);
ans--;
}
else
{
// up
auto it = r[R].lower_bound(C);
if (it != r[R].begin())
{
int t = *prev(it);
r[R].erase(t);
c[t].erase(R);
ans--;
}
if (it != r[R].end())
{
int t = *it;
r[R].erase(t);
c[t].erase(R);
ans--;
}
it = c[C].lower_bound(R);
if (it != c[C].begin())
{
int t = *prev(it);
r[t].erase(C);
c[C].erase(t);
ans--;
}
if (it != c[C].end())
{
int t = *it;
r[t].erase(C);
c[C].erase(t);
ans--;
}
}
}
cout << ans << endl;
return 0;
}
π E - Avoid K Partition

Tips
For explanation, let us rephrase the problem as follows:
There are points arranged in this order. How many ways, modulo , are there to choose some points to put
partitions
on them, so that:
- points and always have a partition, and
- if points and have a partition but not at any points between them, then never equals
Tips
This new version allows us an DP
. Specifically, we can define:
: the number of ways to choose points from point through , while always choosing point
The transition:
- The initial state is
- The answer is
- The transition being:
We try to optimize this DP
Define the cumulative sum table of as:
then the transition above turns into:
Tips
In other words, whether contributes to is solely dependent on the value of
So, we manage the DP table is an associative array (std::map
in C++). Define an associative array by:
: the sum of over with
Also, manage the sum of in a variable all
. Then the transition of the DP can be written as:
π F - Cake Division

Warning
Hard Problems to Tackle Later
// TODO
π G - Divisible by 3

Warning
Hard Problems to Tackle Later
// TODO