Under construction

Still being built. Things may change

Comparing Fractions

Time limit3sMemory limit1024 MB

Summary
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 4×10184 \times 10^{18}. You have also learned addition, subtraction, and comparison of two integers.

The homework is to compare two fractions AB\frac{A}{B} and CD\frac{C}{D}. The professor said you can solve the homework using only classroom material.

You may use only the following operations:

  • Addition: For two integers aa and bb, you can calculate a+ba+b. The result must be at most 4×10184 \times 10^{18}.
  • Subtraction: For two integers aa and bb, you can calculate a−ba-b. The result must be non-negative.
  • Comparison: For two integers aa and bb, you can find out whether they are equal or which one is greater.

Using these operations, compare the two fractions.

Constraints

1≤A,B,C,D≤1091 \leq A, B, C, D \leq 10^9

Hint

You cannot get AA, BB, CC, or DD directly. Instead, you apply operations to a hidden array X1,X2,⋯ ,X106X_1, X_2, \cdots, X_{10^6} of length 10610^6.

Initially X1=AX_1=A, X2=BX_2=B, X3=CX_3=C, X4=DX_4=D, and Xi=0X_i=0 for i>4i>4.

Use these commands:

  • To add two elements, print + i j k (1≤i,j,k≤1061 \le i, j, k \le 10^6). XiX_i becomes Xj+XkX_j+X_k. Xj+XkX_j+X_k must be at most 4×10184 \times 10^{18}, otherwise the verdict is Wrong Answer. Nothing is given as input.
  • To subtract two elements, print - i j k (1≤i,j,k≤1061 \le i, j, k \le 10^6). XiX_i becomes Xj−XkX_j-X_k. You must have Xj≥XkX_j \ge X_k, otherwise the verdict is Wrong Answer. Nothing is given as input.
  • To compare two elements, print < i j (1≤i,j≤1061 \le i, j \le 10^6). One integer is given as input: -1 if Xi<XjX_i<X_j, 0 if Xi=XjX_i=X_j, and 1 if Xi>XjX_i>X_j.

After comparing AB\frac{A}{B} and CD\frac{C}{D}, print:

  • ! -1 if AB<CD\frac{A}{B} < \frac{C}{D}
  • ! 0 if AB=CD\frac{A}{B} = \frac{C}{D}
  • ! 1 if AB>CD\frac{A}{B} > \frac{C}{D}

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:

  • imaxi_{max}: the largest index of XX used in an operation
  • nopn_{op}: the number of operations performed (the answer command is not counted)
  • XmaxX_{max}: the largest value in XX during the operations

Examples2

  1. Example 1

    Input
    -1
    
    Expected output
    + 5 3 1
    - 1 2 1
    < 1 5
    ! 0
    
  2. Example 2

    Input
    1
    -1
    0
    
    Expected output
    < 1 2
    < 4 3
    < 2 2
    ! -1