Python Solution for Codeforces Problem 791A - Bear and Big Brother

791a - bear and big brother codeforces python solution


Introduction

Codeforces 791A, also known as "Bear and Big Brother," presents a simple yet intriguing problem that involves the growth rates of two bears, Limak and Bob. In this article, we'll dissect the provided Python code to unravel the story behind their growth and explain the logic that determines when Limak surpasses Bob in size.


The Problem Scenario

Imagine two bears, Limak and Bob, with their initial weights represented by the variables 'limak' and 'bob,' respectively. The bears undergo a growth process each year, with Limak growing three times as fast as his current weight and Bob growing twice as fast. The objective is to determine how many years it takes for Limak to become larger than Bob.


The Code Walkthrough

Let's break down the code to understand how it tackles this problem.


limak, bob = input().split()
limak = int(limak)
bob = int(bob)
year_count = 0
while True:
    if limak > bob:
        print(year_count)
        break
    else:
        limak = 3 * limak
        bob = 2 * bob
        year_count += 1
 


1. Input Reading:
   - The first two lines of code take user input, assuming it's given as two space-separated integers representing the initial weights of Limak and Bob.

2. Initialization:
   - The input values are converted from strings to integers and assigned to the variables 'limak' and 'bob.'
   - 'year_count' is initialized to 0, representing the number of years passed.

3. While Loop:
   - The code enters a while loop that continues indefinitely (thanks to `while True`).
   - Inside the loop, there's an if-else block checking whether Limak's weight is greater than Bob's.

4. Condition Check:
   - If Limak's weight is greater than Bob's, the loop breaks, and the number of years (year_count) is printed.
   - If Limak's weight is not yet greater than Bob's, the else block is executed.

5. Growth Calculation:
   - Limak's weight is updated by multiplying it by 3, simulating his growth for the year.
   - Bob's weight is updated by multiplying it by 2, simulating his growth for the year.
   - The year_count is incremented by 1.

6. Repeat:
   - The loop continues until Limak's weight surpasses Bob's, and the number of years is printed.

Explanation of the Growth

Limak grows three times faster than his current weight, and Bob grows two times faster than his current weight. This leads to Limak catching up and eventually surpassing Bob in size.


Conclusion

In summary, Codeforces 791A captures the playful concept of bear growth, illustrating how the speed of growth influences their relative sizes. The code efficiently simulates this growth process and provides a clear answer to the question of how many years it takes for Limak to become larger than Bob.

Post a Comment

0 Comments