Comparing Fractions
Time limit3sMemory limit1024 MB
Compare the fractions A/B and C/D using only addition, subtraction, and comparison on a hidden array within 200,001 commands.
- Level
Medium7 of 10
- Topics
- Math, Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
This problem is interactive. See the Interaction Protocol section below.
Today was the first class of CS999.
First, you have learned non-negative integers less than or equal to . You have also learned addition, subtraction, and comparison of two integers.
The homework is to compare two fractions and . The professor said you can solve the homework using only classroom material.
You may use only the following operations:
- Addition: For two integers and , you can calculate . The result must be at most .
- Subtraction: For two integers and , you can calculate . The result must be non-negative.
- Comparison: For two integers and , you can find out whether they are equal or which one is greater.
Using these operations, compare the two fractions.
Constraints
Hint
You cannot get , , , or directly. Instead, you apply operations to a hidden array of length .
Initially , , , , and for .
Use these commands:
- To add two elements, print
+ i j k(). becomes . must be at most , otherwise the verdict isWrong Answer. Nothing is given as input. - To subtract two elements, print
- i j k(). becomes . You must have , otherwise the verdict isWrong Answer. Nothing is given as input. - To compare two elements, print
< i j(). One integer is given as input:-1if ,0if , and1if .
After comparing and , print:
! -1if! 0if! 1if
If you use more than 200,001 commands or make an invalid query, the interactor terminates immediately and the verdict is Wrong Answer.
Scoring
If the comparison is wrong, the score is 0. Otherwise the score depends on three factors:
- : the largest index of used in an operation
- : the number of operations performed (the answer command is not counted)
- : the largest value in during the operations