π§© AtCoder Beginner Contest 387
January 4, 2025About 3 min
π§© AtCoder Beginner Contest 387
# Info & Summary
- Date:
2025-01-04
- Completion Status: A β / B β / C β / D β / E β / F β / G β
- Problem Type:
Problem | Type(s) | Data Structure / Algo | π₯ Key Insight / Mark |
---|---|---|---|
D | Shortest Distance | BFS | βπ₯ |
E | Constructive | Math | π |
F | DP | DP | π |
F | FPS | FPS | π |
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 - Happy New Year 2025

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long a, b;
cin >> a >> b;
cout << a * a + 2 * a * b + b * b << endl;
return 0;
}
π B - 9x9 Sum

#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int x;
cin >> x;
int ans = 0;
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
ans += (i * j == x ? 0 : i * j);
}
}
cout << ans << endl;
return 0;
}
π C - Snake Numbers

Tips
In the problem statement, Snake numbers are only defined for integers at least , but in this editorial, we define that every positive integer less than is also a Snake number
Define as the number of integers less than or equal to , then the sought count can be represented as
Suppose that has digits in decimal, and its -th () significant digit is . Then, every positive integer less than or equal to satisfies exactly one of the following conditions. Conversely, every positive integer satisfies any of the following is less than or equal to :
- If for all (), there is one Snake number
- has digits. Its first () digits coincide with those of , and the -th one is less than
- If for some (), then there are none
- Otherwise, the -th digit should be less than and , and the -th and succeeding digits should be less than , so there are of them
- has digits. Its first digit is less than
- The first digit should be between (inclusive) and (exclusive), and the other digits should be less than it, so there are of them
- has digits ()
- The first digit should be between and (inclusive), and the other digits should be less than it, so there are
// Better version
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long L, R;
cin >> L >> R;
auto calc = [&](long long n)
{
auto s = to_string(n);
long long cnt = 0;
for (int i = 1; i < s.size(); i++)
{
for (int x = 1; x <= 9; x++)
{
long long res = 1;
for (int j = 0; j < i - 1; j++)
{
res *= x;
}
cnt += res;
}
}
for (int i = 0; i < s.size(); i++)
{
for (int x = (i == 0 ? 1 : 0); x < s[i] - '0'; x++)
{
if (i == 0 || x < s[0] - '0')
{
int v = i == 0 ? x : s[0] - '0';
long long res = 1;
for (int j = i + 1; j < s.size(); j++)
{
res *= v;
}
cnt += res;
}
}
if (i && s[i] >= s[0])
{
break;
}
}
return cnt;
};
cout << calc(R + 1) - calc(L) << endl;
return 0;
}
π D - Snaky Walk

#include <bits/stdc++.h>
using namespace std;
constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H, W;
cin >> H >> W;
vector<string> grid(H);
array<int, 2> start;
for (int i = 0; i < H; i++)
{
cin >> grid[i];
for (int j = 0; j < W; j++)
{
if (grid[i][j] == 'S')
{
start = {i, j};
}
}
}
vector<vector<array<int, 4>>> dist(H, vector<array<int, 4>>(W, array<int, 4>{-1, -1, -1, -1}));
queue<tuple<int, int, int>> q;
for (int d = 0; d < 4; d++)
{
dist[start[0]][start[1]][d] = 0;
q.push({start[0], start[1], d});
}
while (!q.empty())
{
auto [x, y, d] = q.front();
q.pop();
if (grid[x][y] == 'G')
{
cout << dist[x][y][d] << endl;
return 0;
}
for (auto k : {d ^ 1, d ^ 3})
{
int nx = x + dx[k];
int ny = y + dy[k];
if (nx < 0 || ny < 0 || nx >= H || ny >= W)
{
continue;
}
if (grid[nx][ny] == '#')
{
continue;
}
if (dist[nx][ny][k] == -1)
{
dist[nx][ny][k] = dist[x][y][d] + 1;
q.push({nx, ny, k});
}
}
}
cout << -1 << endl;
return 0;
}
π E - Digit Sum Divisible 2

Warning
Hard Problems to Tackle Later
// TODO
π F - Count Arrays

Warning
Hard Problems to Tackle Later
// TODO
π G - Prime Circuit

Warning
Hard Problems to Tackle Later
// TODO