Day 15: 791A - Bear and Big Brother
Problem
Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob.
Right now, Limak and Bob weigh a and b respectively. It's guaranteed that Limak's weight is smaller than or equal to his brother's weight.
Limak eats a lot and his weight is tripled after every year, while Bob's weight is doubled after every year.
After how many full years will Limak become strictly larger (strictly heavier) than Bob?
Link to CodeForces
Input
The only line of the input contains two integers \( a \) and \( b \) (\( 1 \leq a \leq b \leq 10 \)) — the weight of Limak and the weight of Bob respectively.
Output
Print one integer, denoting the integer number of years after which Limak will become strictly larger than Bob.
Examples
Input 1
Link to file
4 7
Output 1
Link to file
2
Input 2
Link to file
4 9
Output 2
Link to file
3
Input 3
Link to file
1 1
Output 3
Link to file
1
Analysis
According to the problem statement, Limak's weight is tripled every year while Bob's weight is double every year. Then, we ackowledge that on the \( i \)-th year Limak weights \( a \cdot 3^i \), while Bob weights \( b \cdot 2^i \). Assume that on the \( k \)-th year Limak's weight is larger than or equal to Bob's weight, then we have \( a \cdot 3^k \ge b \cdot 2^k \), where \( k \) is a positive interger. By applying logithm operator to both sides of the equation, we get \( lg(a) + k \cdot lg(3) \ge lg(b) + k \cdot lg(2) \) and further \( k \ge (lg(b) - lg(a))/(lg(3) - lg(2)) \). In this case, the smallest integer \( k \) (\( k \ge 1 \)) is what we should output. The tricky part of the solution is about handling the rounding error. Python has a slightly higher numeric precision in calculation than Go, so we can use \( lg_2 \) operator in Python instead using \( lg_e \) in Go.
Solutions
Constraints
- Time limit per test: 1 second
- Memory limit per test: 256 megabytes
- Input: standard input
- Output: standard output
Python 3.8.10
| Problem | Lang | Verdict | Time | Memory |
| 791A - 11 | Python | Accepted | 46 ms | 0 KB |
Link to source code
import math
def solution():
limak, bob = [float(x) for x in input().split()]
result = (math.log2(bob) - math.log2(limak)) / (math.log2(3) - math.log2(2))
if abs(result - int(result)) < 1e-8:
result += 1
else:
result = int(result) + 1
print(int(result))
if __name__ == "__main__":
solution()
Go 1.17.5
| Problem | Lang | Verdict | Time | Memory |
| 791A - 11 | Go | Accepted | 31 ms | 68 KB |
Link to source code
package main
import (
"bufio"
"fmt"
"math"
"os"
)
var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func Fprintf(format string, x ...interface{}) {
fmt.Fprintf(writer, format, x...)
}
func Fscanf(format string, x ...interface{}) {
fmt.Fscanf(reader, format, x...)
}
func Solution() {
var limak, bob float64
Fscanf("%f %f\n", &limak, &bob)
result := (math.Log(bob) - math.Log(limak)) / (math.Log(3) - math.Log(2))
if result == float64(int(result)) {
result++
} else {
result = float64(int(result) + 1)
}
Fprintf("%d\n", int(result))
}
func main() {
Solution()
writer.Flush()
}
更多推荐

所有评论(0)