AtCoder Beginner Contest 142: Problem-Solving Guide

Updated on Nov 03,2025

Table of Contents

AtCoder Beginner Contest 142 features a diverse set of programming problems designed to test fundamental coding skills. This article provides a detailed walkthrough of the first four problems (A, B, C, and D), offering insights into problem analysis and efficient coding approaches. Whether you're a beginner or an experienced coder, understanding these solutions can significantly enhance your competitive programming prowess.

Key Points

Odds of Oddness (Problem A): Calculating probability with basic arithmetic.

Roller Coaster (Problem B): Filtering elements based on height constraints.

Go to School (Problem C): Reconstructing the order of students entering a classroom.

Disjoint Set of Common Divisors (Problem D): Finding coprime common divisors.

Detailed Problem Analysis and Solutions

A - Odds of Oddness: Probability Calculation

The Odds of Oddness problem requires calculating the probability that a randomly chosen integer from 1 to N is odd. Given an integer N, the task is to find the number of odd integers within the range [1, N] and divide it by N. The constraint 1 ≤ N ≤ 100 makes it feasible to use a brute-force approach.

To solve this, we simply count the number of odd integers in the given range. If N is even, there are N/2 odd integers. If N is odd, there are (N+1)/2 odd integers. The solution can be implemented directly with a conditional statement to handle both cases.

#include <iostream>

int main() {
    int N;
    std::cin >> N;

    int oddCount = (N % 2 == 0) ? N / 2 : (N + 1) / 2;

    double probability = (double)oddCount / N;
    std::cout << probability << std::endl;

    return 0;
}

This approach is efficient due to the problem's small constraints, and the conditional logic ensures accurate calculation of the probability.

B - Roller Coaster: Filtering Based on Height

The Roller Coaster problem involves determining how many of Takahashi's friends can ride a roller coaster based on their height. Given N friends, each with a height Hi, and a minimum height requirement K, the task is to count the friends with height greater than or equal to K. The constraints 1 ≤ N ≤ 10^5 and 1 ≤ K, Hi ≤ 500 allow for a straightforward linear-time solution.

To solve this problem, we iterate through the heights of each friend and increment a counter if the friend's height is greater than or equal to K. This approach ensures we only count those who meet the minimum height requirement.

#include <iostream>
#include <vector>

int main() {
    int N, K;
    std::cin >> N >> K;

    std::vector<int> heights(N);
    for (int i = 0; i < N; ++i) {
        std::cin >> heights[i];
    }

    int rideCount = 0;
    for (int height : heights) {
        if (height >= K) {
            rideCount++;
        }
    }

    std::cout << rideCount << std::endl;
    return 0;
}

Due to the problem's constraints, the linear-time complexity of this solution is efficient enough to solve it within the time limit.

C - Go to School: Reconstructing Entry Order

The Go to School problem is about reconstructing the order in which N students entered a classroom, given the number of students present when each student entered. The key is to understand that the first student must have entered when there was only one student in the classroom, the second when there were two, and so on.

The solution involves iterating through the input and mapping each count to the corresponding student number. Since students entered at different times, each Ai is unique, which allows for direct mapping. By tracing these numbers in order we are essentially tracing the numbers in the opposite order, and output the original order. This approach can be implemented as follows:

#include <iostream>
#include <vector>

int main() {
    int N;
    std::cin >> N;

    std::vector<int> counts(N);
    std::vector<int> order(N);

    for (int i = 0; i < N; ++i) {
        std::cin >> counts[i];
        order[counts[i] - 1] = i + 1;
    }

    for (int i = 0; i < N; ++i) {
        std::cout << order[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

The reverse mapping approach constructs the students' original entry orders by looking up what they were when each student entered the room and storing them in a new array before printing that array to the console. This allows us to have the students' order that they came into the room.

D - Disjoint Set of Common Divisors: Coprime Divisors

The Disjoint Set of Common Divisors problem requires finding the maximum number of positive common divisors of two given integers, A and B, such that any two chosen divisors must be coprime. This problem involves number theory concepts, specifically, finding the greatest common divisor (GCD) and its prime factorization.

To solve this, we first find the GCD of A and B. Then, we find all the distinct prime factors of this GCD. We select these prime numbers which are coprime. One factor of that greatest factor is '1', so that factor must be included.

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>

long long gcd(long long a, long long b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int main() {
    long long A, B;
    std::cin >> A >> B;

    long long commonGCD = gcd(A, B);
    std::vector<long long> distinctFactors;

    for (long long i = 1; i * i <= commonGCD; ++i) {
        if (commonGCD % i == 0) {
            if (i * i == commonGCD) {
                distinctFactors.push_back(i);
            } else {
                distinctFactors.push_back(i);
                distinctFactors.push_back(commonGCD / i);
            }
        }
    }

    std::sort(distinctFactors.begin(), distinctFactors.end());
    distinctFactors.erase(std::unique(distinctFactors.begin(), distinctFactors.end()), distinctFactors.end());

    int count = 0;
    if (!distinctFactors.empty()) {
      count = distinctFactors.size();
    }

    std::cout << count << std::endl;
    return 0;
}

Finding coprime pairs in the factor set of the overall greatest factor by simply finding out what the possible different factors there are in the greatest factor, and not limiting our subset with another set of rules. Also including one with that possible set of factors guarantees that our final subset will have every factor being completely coprime. This solution runs efficiently since factorizing GCD to its maximum number of prime factors can run on linear time.

Frequently Asked Questions

What is competitive programming?
Competitive programming is a mind sport where participants write code to solve algorithmic problems within given constraints, often competing for the best solutions in terms of speed and efficiency.
How can I improve my competitive programming skills?
Practice regularly, study algorithms and data structures, participate in contests, analyze solutions from top-performing participants, and learn from coding communities.

Related Questions

What are some other AtCoder Beginner Contests that I can practice?
Explore other AtCoder Beginner Contests, such as ABC 143, ABC 144, and ABC 145. These contests offer a range of problems suited for beginners and intermediate programmers. You may also want to practice easier problems before the more complex problems to gain more confidence and to ease the learning process.

Most people like