D. Chocolate
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 a1 × b1 segments large and the second one is a2 × b2 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 is5 × 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.

Input

The first line of the input contains integers a1, b1 (1 ≤ a1, b1 ≤ 109) — the initial sizes of the first chocolate bar. The second line of the input contains integers a2, b2 (1 ≤ a2, b2 ≤ 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).

Output

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.

Sample test(s)
input
2 6
2 3
output
1
1 6
2 3
input
36 5
10 16
output
3
16 5
5 16
input
3 5
2 1
output
-1

给两个巧克力。 大小分别是 a1 * b1 , a2 * b2 ;
然后可以横竖切走二分之一 , 三分之一 , 这样。
问能否通过最小步数使得两块巧克力的面积一样 。 我的做法很恶心。
直接将4个数分解。
得出各条边能够通过切割形成的边长 ( 用set去掉重复的 ),并且记录切割次数 。 发现边集规模不大 。
然后我就暴力构造出前两个边集能够形成的矩阵。
用后两个边集也构出矩阵集合。
然后再枚举第一个矩阵集合 e[0][i] , 在第二个矩阵集合中进行二分, 看看是否有矩阵跟 e[0][i] 的面积相同 ,有就取一个切割次数最少的 。
然后再更新一下答案这样子。 貌似正解是分解完数后 , 通过一些计算即可得出答案 。 没必要如此暴力~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
using namespace std; typedef long long LL;
const int N = ;
const int inf = 1e9+;
const double PI = acos(-1.0);
const double eps = 1e- ; set<LL>num_cnt; struct node{
LL x , y , area , cnt ;
bool operator < ( const node &a ) const{
if( area != a.area ) return area < a.area ;
else return cnt < a.cnt ;
}
}e[][N]; LL a[] , num[][N] , cnt[][N] , tot[] , tail1 , tail2 ; void cut( LL n , int i , int cnts ) {
if( num_cnt.count(n) > ) return ; num_cnt.insert(n);
num[i][ tot[i] ] = n , cnt[i][tot[i]] = cnts ; tot[i]++;
if( n % == ) cut( n / , i , cnts + );
if( n % == ) cut( n / * , i , cnts + );
} void test() {
for( int i = ; i < tail1 ; i ++ ){ cout << e[][i].x <<' ' << e[][i].y <<' '<< e[][i].area << endl ; } cout << endl ;
for( int i = ; i < tail2 ; i ++ ){ cout << e[][i].x <<' ' << e[][i].y <<' '<< e[][i].area << endl ; } cout << endl ;
} void solve() { int temp1 = , temp2 = ;
for( int i = ; i < tail1 ; i ++ ){
if( e[][i].area == e[][ temp1 - ].area ) continue ;
e[][temp1++] = e[][i] ;
}
for( int i = ; i < tail2 ; i ++ ){
if( e[][i].area == e[][ temp2 - ].area ) continue ;
e[][temp2++] = e[][i] ;
}
tail1 = temp1 , tail2 = temp2 ;
} int find( LL area )
{
int l = , r = tail2 ;
if( e[][l].area == area ) return l ;
else if( e[][r].area == area ) return r ;
while( l <= r ){
int mid = ( l + r ) >> ;
if( e[][mid].area == area ){
return mid ;
}
else if( e[][mid].area < area )
l = mid + ;
else
r = mid - ;
}
return - ;
} int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
for( int i = ; i < ; ++i ) {
cin >> a[i] ; num_cnt.clear();
cut( a[i] , i , );
}
// for( int i = 0 ; i < 4 ; ++i ) cout << tot[i] << endl ;
tail1 = , tail2 = ;
for( int i = ; i < tot[] ; ++i ){
for( int j = ; j < tot[] ; ++j ) {
e[][ tail1 ].x = num[][i] ;
e[][ tail1 ].y = num[][j] ;
e[][ tail1 ].area = num[][i] * num[][j] ;
e[][ tail1 ].cnt = cnt[][i] + cnt[][j];
tail1++ ;
}
} for( int i = ; i < tot[] ; ++i ){
for( int j = ; j < tot[] ; ++j ) {
e[][ tail2 ].x = num[][i] ;
e[][ tail2 ].y = num[][j] ;
e[][ tail2 ].area = num[][i] * num[][j] ;
e[][ tail2 ].cnt = cnt[][i] + cnt[][j];
tail2++ ;
}
} sort( e[] , e[] + tail1 ) ; sort( e[] , e[] + tail2 ) ; solve(); LL ans = inf , A1 , A0 , B1 , B0 ; for( int i = ; i < tail1 ; ++i ){
if( i > && e[][i].area == e[][i-].area ) continue ;
if( ans <= e[][i].cnt ) continue ;
int pos = find( e[][i].area );
if( pos == - ) continue ;
if( ans > e[][i].cnt + e[][pos].cnt ){
ans = e[][i].cnt + e[][pos].cnt ;
A0 = e[][i].x , B0 = e[][i].y;
A1 = e[][pos].x , B1 = e[][pos].y;
}
}
if( ans != inf ) cout << ans << '\n' << A0 << ' ' << B0 << '\n' << A1 <<' ' << B1 << endl ;
else cout << "-1" << endl ;
}

最新文章

  1. MVC和WebForm 中国省市区三级联动
  2. Mapreduce体系架构
  3. iTOP-4412嵌入式开发板ioremap控制GPIO寄存器
  4. 东大OJ 2SAT 异或
  5. CodeForces 209C Trails and Glades
  6. SQL 数据类型,增删改查语句
  7. 浅谈Objective-C编译器指令
  8. Android如何创建背景透明的Dialog
  9. CentOS下成功修复了Windows的grub引导
  10. python基础(7):字符编码
  11. ERROR: JDWP Unable to get JNI 1.2 environment, jvm-&gt;GetEnv() return code = -2
  12. 使用mescroll来实现移动端页面上拉刷新, 下拉加载更多功能
  13. elasticsearch6.6.2在Centos6.9的安装
  14. 第1章 HTTP协议基本介绍了解
  15. 8.Django
  16. Java课程寒假之开发记账本软件(网页版)之二
  17. 安装Eclipse Maven插件的方法
  18. [转]@PathVariable和@RequestParam的区别
  19. suiyi
  20. 【转】C# Socket通信编程

热门文章

  1. java树的遍历
  2. bzoi1152 [CTSC2006]歌唱王国Singleland
  3. MariaDB学习笔记(二)
  4. Dagger2 探索记3——两大进阶组件(一)
  5. 【Tensorflow】slim.arg_scope()的使用
  6. windows下基于(QPC)实现的微秒级延时
  7. 你了解SEO中的时效性吗?
  8. CF963E Circles of Waiting
  9. 在scite编辑器中使用astyle格式化c代码
  10. 【leetcode】921. Minimum Add to Make Parentheses Valid