CF490D.Chocolate

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Polycarpus likes giving presents to Paraskevi. He has bought two chocolate bars, each of them has the shape of a segmented rectangle. The first bar is _a_1 × _b_1 segments large and the second one is _a_2 × _b_2 segments large.

Polycarpus wants to give Paraskevi one of the bars at the lunch break and eat the other one himself. Besides, he wants to show that Polycarpus's mind and Paraskevi's beauty are equally matched, so the two bars must have the same number of squares.

To make the bars have the same number of squares, Polycarpus eats a little piece of chocolate each minute. Each minute he does the following:

  • he either breaks one bar exactly in half (vertically or horizontally) and eats exactly a half of the bar,
  • or he chips of exactly one third of a bar (vertically or horizontally) and eats exactly a third of the bar.

In the first case he is left with a half, of the bar and in the second case he is left with two thirds of the bar.

Both variants aren't always possible, and sometimes Polycarpus cannot chip off a half nor a third. For example, if the bar is 16 × 23, then Polycarpus can chip off a half, but not a third. If the bar is 20 × 18, then Polycarpus can chip off both a half and a third. If the bar is 5 × 7, then Polycarpus cannot chip off a half nor a third.

What is the minimum number of minutes Polycarpus needs to make two bars consist of the same number of squares? Find not only the required minimum number of minutes, but also the possible sizes of the bars after the process.

波利卡普斯喜欢给帕拉斯克维送礼物。他买了两块巧克力,每块都呈分段矩形形状。第一块巧克力的尺寸为 a1×b1a_1 \times b_1(即由 a1×b1a_1 \times b_1 个小方格组成),第二块为 a2×b2a_2 \times b_2 个小方格。

波利卡普斯打算在午休时将其中一块送给帕拉斯克维,自己吃掉另一块。此外,他希望体现“波利卡普斯的智慧”与“帕拉斯克维的美貌”旗鼓相当,因此这两块巧克力必须包含相同数量的小方格。

为了让两块巧克力的小方格数相等,波利卡普斯每分钟吃掉一小块巧克力。每一分钟,他执行以下两种操作之一:

  • 将某一块巧克力恰好沿水平或竖直方向切成两半,并吃掉其中恰好一半;
  • 或者从某一块巧克力上切下其三分之一(沿水平或竖直方向),并吃掉恰好三分之一。

在第一种情况下,他剩下原巧克力的一半;在第二种情况下,他剩下原巧克力的三分之二。

但并非所有情况都允许执行这两种操作:有时无法切下一半,有时也无法切下三分之一。例如:

  • 若巧克力尺寸为 16×2316 \times 23,则可切下一半,但无法切下三分之一;
  • 若尺寸为 20×1820 \times 18,则既可切下一半,也可切下三分之一;
  • 若尺寸为 5×75 \times 7,则既无法切下一半,也无法切下三分之一。

问:波利卡普斯最少需要多少分钟,才能使两块巧克力所含小方格数相等?请不仅求出所需的最少分钟数,还要求出操作结束后两块巧克力可能的尺寸。

输入格式

The first line of the input contains integers _a_1, _b_1 (1 ≤ _a_1, _b_1 ≤ 109) — the initial sizes of the first chocolate bar. The second line of the input contains integers _a_2, _b_2 (1 ≤ _a_2, _b_2 ≤ 109) — the initial sizes of the second bar.

You can use the data of type int64 (in Pascal), long long (in С++), long (in Java) to process large integers (exceeding 231 - 1).

输入的第一行包含整数 a1a_1、b1b_1(1≤a1,b1≤1091 \leq a_1, b_1 \leq 10^9)—— 第一块巧克力的初始尺寸。
输入的第二行包含整数 a2a_2、b2b_2(1≤a2,b2≤1091 \leq a_2, b_2 \leq 10^9)—— 第二块巧克力的初始尺寸。

你可以使用 int64 类型(Pascal 中)、long long 类型(C++ 中)或 long 类型(Java 中)来处理大整数(超过 231−12^{31} - 1)。

输出格式

In the first line print m — the sought minimum number of minutes. In the second and third line print the possible sizes of the bars after they are leveled in m minutes. Print the sizes using the format identical to the input format. Print the sizes (the numbers in the printed pairs) in any order. The second line must correspond to the first bar and the third line must correspond to the second bar. If there are multiple solutions, print any of them.

If there is no solution, print a single line with integer -1.

第一行输出 m —— 所求的最少分钟数。
第二行和第三行分别输出经过 m 分钟调整后两根棒材可能的尺寸。输出格式需与输入格式完全相同。所输出的尺寸(即输出的数对中的数字)顺序任意。第二行对应第一根棒材,第三行对应第二根棒材。若存在多个解,输出任意一个即可。

若无解,则仅输出一行整数 -1。

输入输出样例

  • 输入#1

    2 6
    2 3

    输出#1

    1
    1 6
    2 3
  • 输入#2

    36 5
    10 16

    输出#2

    3
    16 5
    5 16
  • 输入#3

    3 5
    2 1

    输出#3

    -1

输入解题思路,AI测评打分。不知道怎么写?

首页