#abc297d. D - Count Subtractions

D - Count Subtractions

Score : 400400 points

问题描述

你已知两个正整数 AABB

你需要重复以下操作,直到 A=BA=B

  • 比较 AABB,执行以下两种操作之一:
    • A>BA > B,则用 ABA-B 替换 AA
    • A<BA < B,则用 BAB-A 替换 BB

请问需要重复多少次才能使得 A=BA=B?可以保证有限次的重复操作后,一定能使得 A=BA=B

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given positive integers AA and BB.

You will repeat the following operation until A=BA=B:

  • compare AA and BB to perform one of the following two:
    • if A>BA > B, replace AA with ABA-B;
    • if A<BA < B, replace BB with BAB-A.

How many times will you repeat it until A=BA=B? It is guaranteed that a finite repetition makes A=BA=B.

Constraints

  • 1A,B10181 \le A,B \le 10^{18}
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

AA BB

Output

Print the answer.

Sample Input 1

3 8

Sample Output 1

4

Initially, A=3A=3 and B=8B=8. You repeat the operation as follows:

  • A<BA<B, so replace BB with BA=5B-A=5, making A=3A=3 and B=5B=5.
  • A<BA<B, so replace BB with BA=2B-A=2, making A=3A=3 and B=2B=2.
  • A>BA>B, so replace AA with AB=1A-B=1, making A=1A=1 and B=2B=2.
  • A<BA<B, so replace BB with BA=1B-A=1, making A=1A=1 and B=1B=1.

Thus, you repeat it four times.

Sample Input 2

1234567890 1234567890

Sample Output 2

0

Note that the input may not fit into a 32-bit integer type.

Sample Input 3

1597 987

Sample Output 3

15

update @ 2024/3/10 12:20:29