CF 102697016 - Gravity Vehicle Testing
The problem asks us to identify the gravity value of the planet where a gravity vehicle is being tested. The input is a single word describing the planet, either Earth or Mars, and the output is the corresponding gravitational constant. Earth uses a gravity value of 9.
CF 102697016 - Gravity Vehicle Testing
Rating: -
Tags: -
Solve time: 1m 29s
Verified: yes
Solution
Problem Understanding
The problem asks us to identify the gravity value of the planet where a gravity vehicle is being tested. The input is a single word describing the planet, either Earth or Mars, and the output is the corresponding gravitational constant. Earth uses a gravity value of 9.807 m/s², while Mars uses 3.711 m/s².
The input size is constant because there is only one string with one of two possible values. That immediately rules out the need for any advanced algorithm or data structure. The entire task is a direct lookup, so even a solution with constant time complexity is more than sufficient.
The main edge cases come from handling the exact input values correctly. A program that compares the wrong spelling or prints a value with missing digits will fail.
For example, if the input is:
EARTH
the correct output is:
9.807
A careless implementation that prints 9.8 loses required precision and gives the wrong answer.
Another case is:
MARS
with the correct output:
3.711
A solution that assumes Earth as the default answer without checking the input would silently fail on this case.
Approaches
The brute-force approach would be to simulate some kind of gravity calculation or try to derive the value from physical formulas. That is unnecessary because the problem already gives the two possible constants. Such a method would perform extra work without using any additional information, and the exact operation count depends on the unnecessary simulation chosen.
The key observation is that the input is only a label, not a measurement. Since there are exactly two possible labels and each has a fixed answer, the problem reduces to choosing between two stored values. A simple conditional check is enough.
The brute-force works because any method that eventually identifies the planet can produce the answer, but it fails because it solves a harder version of the problem than required. The observation that the mapping from planet name to gravity value is fixed lets us replace all calculation with a constant-time lookup.
| Approach | Time Complexity | Space Complexity | Verdict |
|---|---|---|---|
| Brute Force | O(k) where k depends on unnecessary simulation | O(1) | Too slow in principle and unnecessary |
| Optimal | O(1) | O(1) | Accepted |
Algorithm Walkthrough
- Read the planet name from the input. The only information needed is whether the vehicle is being tested on Earth or Mars.
- Compare the name with
"EARTH". If it matches, output9.807because that is the fixed gravity constant for Earth. - Otherwise, the only remaining valid possibility is
"MARS", so output3.711.
Why it works: the input domain contains only two valid states, and each state has exactly one required output. The algorithm checks those states directly, so there is no possibility of choosing an incorrect value for a valid input.
Python Solution
import sys
input = sys.stdin.readline
def solve():
planet = input().strip()
if planet == "EARTH":
print("9.807")
else:
print("3.711")
if __name__ == "__main__":
solve()
The program reads the single input string and removes the trailing newline using strip(). The comparison is exact because the problem uses uppercase planet names.
The conditional branch handles Earth explicitly. Since the only other valid input is Mars, the else branch can safely print the Mars value. The output is stored as a string instead of a floating point number so the exact three decimal places are preserved.
Worked Examples
For the first example:
Input:
EARTH
The execution trace is:
| Planet | Condition checked | Output |
|---|---|---|
| EARTH | Matches EARTH | 9.807 |
The trace shows the direct lookup behavior. No calculation is performed, so the provided constant is printed exactly.
For the second example:
Input:
MARS
The execution trace is:
| Planet | Condition checked | Output |
|---|---|---|
| MARS | Does not match EARTH | 3.711 |
This demonstrates the second possible branch and confirms that the program does not incorrectly assume Earth.
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(1) | Only one string comparison is performed |
| Space | O(1) | Only the input string and a few variables are stored |
The solution easily fits the limits because it performs a fixed amount of work regardless of the input.
Test Cases
import sys
import io
def solve():
import sys
input = sys.stdin.readline
planet = input().strip()
if planet == "EARTH":
print("9.807")
else:
print("3.711")
def run(inp: str) -> str:
old_stdin = sys.stdin
old_stdout = sys.stdout
sys.stdin = io.StringIO(inp)
sys.stdout = io.StringIO()
solve()
result = sys.stdout.getvalue()
sys.stdin = old_stdin
sys.stdout = old_stdout
return result
assert run("EARTH\n") == "9.807\n", "sample 1"
assert run("MARS\n") == "3.711\n", "sample 2"
assert run("EARTH\n") == "9.807\n", "earth constant"
assert run("MARS\n") == "3.711\n", "mars constant"
assert run("EARTH") == "9.807\n", "input without trailing newline"
assert run("MARS") == "3.711\n", "input without trailing newline"
| Test input | Expected output | What it validates |
|---|---|---|
| EARTH | 9.807 | Earth branch and exact formatting |
| MARS | 3.711 | Mars branch |
| EARTH without newline | 9.807 | Input handling at the boundary |
| MARS without newline | 3.711 | Robustness of string reading |
Edge Cases
The first edge case is the Earth value requiring exact formatting. For the input:
EARTH
the algorithm compares the string successfully and prints:
9.807
A solution that converts the value to a floating point number and prints it with default formatting might produce a different representation, so storing the output directly avoids formatting mistakes.
The second edge case is the Mars branch. For the input:
MARS
the first condition fails, so the algorithm reaches the second case and prints:
3.711
This confirms that the solution does not rely on Earth being the common case. Each valid input maps directly to its required answer.