π§© AtCoder Beginner Contest 377
October 26, 2024About 3 min
π§© AtCoder Beginner Contest 377
# Info & Summary
- Date:
2024-10-26
- Completion Status: A β / B β / C β / D β / E β / F β / G β
- Problem Type:
Problem | Type(s) | Data Structure / Algo | π₯ Key Insight / Mark |
---|---|---|---|
D | Coverage Check | Math | βπ₯ |
E | Graph | Math/Modular exponentiation | π§ π |
F | Math | Math | π |
G | DP | 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 - Rearranging ABC

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
string S;
cin >> S;
sort(S.begin(), S.end());
cout << (S == "ABC" ? "Yes" : "No") << endl;
return 0;
}
π B - Avoid Rook Attack

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
set<int> col, row;
for (int i = 0; i < 8; i++)
{
string s;
cin >> s;
for (int j = 0; j < 8; j++)
{
if (s[j] == '#')
{
row.insert(i);
col.insert(j);
}
}
}
cout << (8 - row.size()) * (8 - col.size()) << endl;
return 0;
}
π C - Avoid Knight Attack

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, -2, -2, -1, 1, 2, 2, 1};
const int dy[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long N, M;
cin >> N >> M;
set<pair<long long, long long>> s;
for (int i = 0; i < M; i++)
{
int a, b;
cin >> a >> b;
a--;
b--;
s.insert({a, b});
for (int k = 0; k < 8; k++)
{
long long nx = a + dx[k];
long long ny = b + dy[k];
if (nx < 0 || ny < 0 || nx >= N || ny >= N)
{
continue;
}
s.insert({nx, ny});
}
}
cout << N * N - s.size() << endl;
return 0;
}
π D - Many Segments 2

Tips
An important fact is that, if an integer pair satisfies the conditions, then so does
Thus, there exists such that:
- For a fixed , a pair satisfies the conditions if and only if is an integer between and , inclusive
Suppose that we already know and now we want to find :
- If there is no with , then there are no new constraints, so
- If there exists such , we have , where is the maximum for with
Then, the sought answer can be found as
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
// f[i] will eventually represent the smallest r such that [i,r] is not invalid
vector<int> f(M, M);
for (int i = 0; i < N; i++)
{
int L, R;
cin >> L >> R;
L--;
R--;
// starting from l = L_i, no r β₯ R_i should be valid
// So we store the earliest invalid r for each L_i
f[L] = min(f[L], R);
}
for (int i = M - 2; i >= 0; i--)
{
// for any l, we know the smallest invalid r for any interval starting at or after l
// So the valid range for r is: rβ[l,f[l]β1]
f[i] = min(f[i], f[i + 1]);
}
long long ans = 0;
for (int i = 0; i < M; i++)
{
ans += f[i] - i;
}
cout << ans << endl;
return 0;
}
π E - Permute K times 2

Tips
Consider a directed graph with vertices with edges
In this graph, let us denote by the vertex we reach by advancing from , times
The sought answer is
Tips
The directed graph can be decomposed into cycles. The answer can be obtained by finding the remainder when dividing by the length of the cycle
#include <bits/stdc++.h>
using namespace std;
// Modular exponentiation: a^b \bmod p
int power(int a, long long b, int p)
{
int res = 1;
for (; b; b /= 2, a = 1ll * a * a % p)
{
if (b & 1)
{
res = 1ll * res * a % p;
}
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
long long K;
cin >> N >> K;
vector<int> P(N);
for (int i = 0; i < N; i++)
{
cin >> P[i];
P[i]--;
}
vector<bool> vis(N);
for (int i = 0; i < N; i++)
{
if (vis[i])
{
continue;
}
// For each unvisited node i, we explore its path until we return to a visited node
// this always finds a cycle, which is stored in vertor a
int j = i;
vector<int> a;
while (!vis[j])
{
vis[j] = true;
a.push_back(j);
j = P[j];
}
// Compute 2^K modulo cycle length
// ompute how many steps to rotate within the cycle
long long d = power(2, K, a.size());
// Rotate the cycle
for (int x = 0; x < a.size(); x++)
{
P[a[x]] = a[(x + d) % a.size()];
}
}
for (int i = 0; i < N; i++)
{
cout << P[i] + 1 << " \n"[i == N - 1];
}
return 0;
}
π F - Avoid Queen Attack

Warning
Hard Problems to Tackle Later
// TODO
π G - Edit to Match

Warning
Hard Problems to Tackle Later
// TODO