Solutions to AtCoder Beginner Contest 055 Problems

Problem A

Each meal costs 800 yen. For every 15 meals purchased, a refund of 200 yen is issued. Given $ x $, the total number of meals consumed, compute the net expenditure.

The formula is: $$ 800x - \left\lfloor \frac{x}{15} \right\rfloor \cdot 200 $$

Problem B

After $ N $ workouts, where initial strength is 1 and each $ i $-th workout multiplies strength by $ i $, compute the final strength modulo $ 10^9 + 7 $.

Since $ N \leq 10^5 $, a simple loop computing the factorial modulo $ 10^9 + 7 $ suffices.

Problem C

Given $ N $ units of item s and $ M $ units of item c, you can form one s from two cs. The goal is to maximize the number of scc triplets.

If $ N \geq \lfloor M / 2 \rfloor $, all available cs can be paired into $ \lfloor M / 2 \rfloor $ s units, limiting the answer to $ \lfloor M / 2 \rfloor $.

Otherwise, use all $ N $ s units, consuming $ 2N $ cs. The remaining $ M - 2N $ cs can form additional s units in groups of 4 (since each new scc requires 1 s and 2 cs, but the s must come from 2 cs → effectively 4 cs per extra scc). Thus, the total becomes: $$ N + \left\lfloor \frac{M - 2N}{4} \right\rfloor $$

long long N, M;
std::cin >> N >> M;
if (N >= M / 2)
    std::cout << M / 2 << '\n';
else
    std::cout << N + (M - 2 * N) / 4 << '\n';

Problem D

$ N $ animals sit in a circle, each either a sheep (S, always truthful) or a wolf (W, always lying). Each is asked: "Are your two neighbors of the same type?" Their responses form a string $ s $ of length $ N $, where o means "yes" and x means "no".

Determine any valid assignment of S/W consistent with all answers, or output -1 if none exists.

Because the arrangement is circular, fixing the types of the first two animals determines the rest via logical propagation. There are only four initial configurations to try: (S,S), (S,W), (W,S), (W,W).

For positions $ 3 $ through $ N $, the type of animal $ i+1 $ is deduced from animal $ i $'s type and response:

  • If animal $ i $ says o (neighbors same):
    • If it's a sheep (S), then neighbors match → animal $ i+1 $ = animal $ i-1 $
    • If it's a wolf (W), then neighbors differ → animal $ i+1 $ ≠ animal $ i-1 $
  • If animal $ i $ says x (neighbors different):
    • If it's a sheep, neighbors differ → animal $ i+1 $ ≠ animal $ i-1 $
    • If it's a wolf, neighbors same → animal $ i+1 $ = animal $ i-1 $

After filling all positions, validate consistency for animals $ 1 $ and $ N $ using their recorded answers and the circular adjacency.

int n;
std::cin >> n;
std::string s;
std::cin >> s;
s = " " + s; // 1-indexing

std::vector<int> state(n + 1);
for (int a = 0; a < 2; ++a) {
    for (int b = 0; b < 2; ++b) {
        state[1] = a;
        state[2] = b;
        for (int i = 2; i < n; ++i) {
            if (s[i] == 'o') {
                state[i + 1] = (state[i] == 0) ? state[i - 1] : 1 - state[i - 1];
            } else {
                state[i + 1] = (state[i] == 0) ? 1 - state[i - 1] : state[i - 1];
            }
        }

        bool valid = true;
        // Check last animal's statement about neighbors (n-1 and 1)
        if (s[n] == 'o') {
            valid &= (state[n] == 0) ? (state[n - 1] == state[1]) : (state[n - 1] != state[1]);
        } else {
            valid &= (state[n] == 0) ? (state[n - 1] != state[1]) : (state[n - 1] == state[1]);
        }
        // Check first animal's statement about neighbors (n and 2)
        if (s[1] == 'o') {
            valid &= (state[1] == 0) ? (state[n] == state[2]) : (state[n] != state[2]);
        } else {
            valid &= (state[1] == 0) ? (state[n] != state[2]) : (state[n] == state[2]);
        }

        if (valid) {
            for (int i = 1; i <= n; ++i)
                std::cout << (state[i] ? 'W' : 'S');
            return 0;
        }
    }
}
std::cout << -1 << '\n';

Tags: AtCoder Competitive Programming algorithm Problem Solving

Posted on Fri, 11 Sep 2026 16:45:57 +0000 by YOUAREtehSCENE