CodeForces Round 279 D Chocolate

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.

题意为两块巧克力,每次可以吃掉一半或者1/3,问最少使者两块面积一样的次数。

看到这个题,感觉bfs的话会有很多状态,可能会暴,但是考虑到是对一个数2分和3分,大致计算一下复杂度为O(lg(a*b)^2)。瞬间感觉世界好美妙,这样的话a=10^9,b=10^9,也可以瞬间出解

代码写的好丑,不过这是比赛时写的,贴一下

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<sstream>
#define inf 0x3f3f3f3f
#define PI acos(-1.0)
#define eps 1e-6
#define LL long long
#define MEM(a,b) memset(a,b,sizeof(a))
#define PB push_back
#define MP make_pair
#define PR printf
#define SC scanf
#define eee EOF
#define PQ priority_queue
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);
#define BUG printf("bug************bug************bug\n");

using namespace std;
typedef map<int,int>::iterator MII;
typedef map<LL,LL>::iterator MLL;
typedef pair<int,int> PII;
typedef set<int>::iterator SI;
typedef set<LL>::iterator SL;
typedef map<string,int>::iterator MSI;

const int maxn=3000+10;
const int mod=2000000000;

map<LL,int>G1;
map<LL,int>G2;
struct node
{
        LL x,y;
        int t;
        node(){}
        node(LL x,LL y,int t):x(x),y(y),t(t){}
};
map<LL,int>S;
map<LL,LL>S1,S11;
map<LL,LL>S2,S22;
map<LL,int>S3,S33;
map<LL,int>SS;

int main()
{
        LL a1,b1,a2,b2;
        while(scanf("%I64d%I64d",&a1,&b1)!=EOF)
        {
                G1.clear(); G2.clear(); S.clear();S1.clear(); S2.clear(); S3.clear(); SS.clear();
                S11.clear(); S22.clear(); S33.clear();
                scanf("%I64d%I64d",&a2,&b2);
                if (a1*b1==a2*b2){printf("0\n%I64d %I64d\n%I64d %I64d\n",a1,b1,a2,b2); continue; }
                queue<node>Q;
                Q.push(node(a1,b1,0));
                G1[a1*mod+b1]=1;
                while(!Q.empty())
                {
                        node tp=Q.front(); Q.pop();
                        LL x=tp.x, y=tp.y;
                        int t=tp.t;
                        LL ans=x*y;
                        if (!S[ans]) {S[ans]=1; S1[ans]=x; S2[ans]=y; S3[ans]=t;}
                        else
                        {
                                if (S3[ans]<t)
                                {S[ans]=1; S1[ans]=x; S2[ans]=y; S3[ans]=t;}
                        }
                        if (x && x%2==0 && ((x%2)%2==0|| (x%2)%3==0) && x/2!=0)
                        {
                                LL tpp=(x/2)*mod+y;
                                if (!G1[tpp])
                                Q.push(node(x/2,y,t+1)),G1[tpp]=1;
                        }
                        if (x && x%3==0 && ((x%3)%2==0|| (x%3)%3==0) && x/3!=0)
                        {
                                LL tpp=(x/3*2)*mod+y;
                                if (!G1[tpp])
                                Q.push(node(x/3*2,y,t+1)),G1[tpp]=1;
                        }

                        if (y && y%2==0 && ((y%2)%2==0|| (y%2)%3==0) && y/2!=0)
                        {
                                LL tpp=x*mod+y/2;
                                if (!G1[tpp])
                                Q.push(node(x,y/2,t+1)),G1[tpp]=1;
                        }
                        if (y && y%3==0 && ((y%3)%2==0|| (y%3)%3==0) && y/3!=0)
                        {
                                LL tpp=x*mod+y/3*2;
                                if (!G1[tpp])
                                Q.push(node(x,y/3*2,t+1)),G1[tpp]=1;
                        }
                }
                bool flag=0;
                queue<node>q;
                q.push(node(a2,b2,0));
                G2[a2*mod+b2]=1;
                int a=inf;
                LL b,c,d,e;
                while(!q.empty())
                {
                        node tp=q.front(); q.pop();
                        LL x=tp.x, y=tp.y;
                        int t=tp.t;
                        LL ans=x*y;
                        if (!SS[ans]) {SS[ans]=1; S11[ans]=x; S22[ans]=y; S33[ans]=t;}
                        else
                        {
                                if (S33[ans]<t)
                                {SS[ans]=1; S11[ans]=x; S22[ans]=y; S33[ans]=t;}
                        }
                        if (S[ans])
                        {
                                flag=true;
                                if (a>S3[ans]+S33[ans])
                                {
                                        a=S3[ans]+S33[ans];
                                        b=S1[ans]; c=S2[ans];
                                        d=S11[ans]; e=S22[ans];
                                }
                        }
                        if (x && x%2==0 && ((x%2)%2==0|| (x%2)%3==0) && x/2!=0)
                        {
                                LL tpp=(x/2)*mod+y;
                                if (!G2[tpp])
                                q.push(node(x/2,y,t+1)),G2[tpp]=1;
                        }
                        if (x && x%3==0 && ((x%3)%2==0|| (x%3)%3==0) && x/3!=0)
                        {
                                LL tpp=(x/3*2)*mod+y;
                                if (!G2[tpp])
                                q.push(node(x/3*2,y,t+1)),G2[tpp]=1;
                        }

                        if (y && y%2==0 && ((y%2)%2==0|| (y%2)%3==0) && y/2!=0)
                        {
                                LL tpp=x*mod+y/2;
                                if (!G2[tpp])
                                q.push(node(x,y/2,t+1)),G2[tpp]=1;
                        }
                        if (y && y%3==0 && ((y%3)%2==0|| (y%3)%3==0) && y/3!=0)
                        {
                                LL tpp=x*mod+y/3*2;
                                if (!G2[tpp])
                                q.push(node(x,y/3*2,t+1)),G2[tpp]=1;
                        }
                }
                if (!flag) printf("-1\n");
                else
                {
                        printf("%d\n%I64d %I64d\n%I64d %I64d\n",a,b,c,d,e);
                }
        }
        return 0;
}
时间: 2024-08-24 14:46:56

CodeForces Round 279 D Chocolate的相关文章

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #279 (Div. 2) b

/**  * @brief Codeforces Round #279 (Div. 2) b  * @file b.cpp  * @author mianma  * @created 2014/11/27 15:29  * @edited  2014/11/27 20:40  * @type   * @note  */ #include <cstdio> #include <stack> #include <cstring> #include <vector>

Codeforces Round #279 (Div. 2) a

/**  * @brief Codeforces Round #279 (Div. 2) a  * @file a.cpp  * @author 面码  * @created 2014/11/26 17:05  * @edited  2014/11/26 17:05  * @type   *  */ #include <cstdio> #include <stack> #include <cstring> using namespace std; #define max

Codeforces Round #279 (Div. 2) d

/**  * @brief Codeforces Round #279 (Div. 2) d  * @file d.cpp  * @author 面码  * @created 2014/12/09 10:58  * @edited  2014/12/09 10:58  * @type math greedy  * @note 自己的AC不了,参考别人的,重点是2和3都是质数,所以可以使用贪心求解.  */ #include <fstream> #include <iostream>

Codeforces Round #279 (Div. 2) 解题报告

A - Team Olympiad 贪心水题..都从第一个开始取即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <map

Codeforces Round #279 (Div. 2) 题解集合

终于有场正常时间的比赛了...毛子换冬令时还正是好啊233 做了ABCD,E WA了3次最后没搞定,F不会= = 那就来说说做的题目吧= = A. Team Olympiad 水题嘛= = 就是个贪心什么的乱搞,貌似放A题难了 1 #include <cstdio> 2 #include <algorithm> 3 4 using namespace std; 5 const int N = 5005; 6 7 int cnt[5], first[5], next[N]; 8 9

Codeforces Round #279 (Div. 2)

A. Team Olympiad 水题英文还这么长... 1 #include<bits/stdc++.h> 2 #define eps 1e-9 3 #define FOR(i,j,k) for(int i=j;i<=k;i++) 4 using namespace std; 5 typedef long long LL; 6 int i,j,k,n,m,x,y,T,ans,big,cas; 7 bool flag; 8 vector <int> a[5]; 9 int n

Codeforces Round #279 (Div. 2) F. Treeland Tour(lis+dfs)

题目链接: huangjing 题意:告诉一个无向无环图,然后求在联通的路上的lis. 思路:枚举起点求lis 复杂度是n^2logn,貌似这复杂度对时间有点玄,估计是数据有点弱... 首先枚举起点,然后求lis,主要是用dfs求的,要用到回溯的思想,我觉得是只要更新了,就要保存那个操作,然后一旦这一次的搜索完成,那么就要立即回复g数组的值,因为有很多不同的路线,所以一条路走完后,就要回复以前的状态哦,免得影响另外一条路..我觉得尽管他们都说是暴力,但是我觉得这个题还是蛮好的... 题目: F.

Codeforces Round #279 (Div. 2)B. Queue(构造法,数组下标的巧用)

这道题不错,思维上不难想到规律,但是如何写出优雅的代码比较考功力. 首先第一个人的序号可以确定,那么接下来所有奇数位的序号就可以一个连一个的确定了.然后a[i].first==0时的a[i].secod就是第二个人的序号,然后偶数位的序号也可以一个连一个的确定了. 用一个next数组,其下标就是a[i].first,其值就是a[i].second,这样巧妙地使用数组下标就解决了“串链子”这个难点,我之前想的每次用二分来找真是弱爆了.. 而在找第一个人的序号时也是妙用flag数组下标. #incl