码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces Round 279 D Chocolate

时间:2014-11-23 23:06:07      阅读:380      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   ar   color   os   sp   java   

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;
}

 

CodeForces Round 279 D Chocolate

标签:des   style   blog   io   ar   color   os   sp   java   

原文地址:http://www.cnblogs.com/chensunrise/p/4117651.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!