PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GL0000055. Phân số kế tiếp trong dãy hữu tỉ (A Rational Sequence)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Phân số kế tiếp trong dãy hữu tỉ (A Rational Sequence)

A Rational Sequence

Source: Kattis

Version: Phuoc Hung OJ Extended

Problem Statement

Consider the binary tree rooted at 1/1 where the left child of p/q is p/(p+q) and the right child is (p+q)/q. A breadth-first, left-to-right traversal produces a sequence of reduced positive fractions. Given a fraction p/q in this sequence, output the fraction immediately following it.

Input

A single reduced positive fraction p/q, without a dataset identifier.

Output

The next reduced fraction in the form a/b.

Subtasks

  • Subtask 1 (20 points): 1≤p,q≤201\le p,q\le 20, gcd⁡(p,q)=1\gcd(p,q)=1.
  • Subtask 2 (30 points): 1≤p,q≤1051\le p,q\le 10^5, gcd⁡(p,q)=1\gcd(p,q)=1.
  • Subtask 3 (50 points): 1≤p,q≤1091\le p,q\le 10^9, gcd⁡(p,q)=1\gcd(p,q)=1; the next fraction fits in signed 64-bit integers.

Examples

Input

1/1

Output

1/2

Explanation

The breadth-first successor of root 1/1 is its left child 1/2.