• Codeforces 490D Chocolate


    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 = 1000010;
    const int inf = 1e9+7;
    const double PI = acos(-1.0);
    const double eps = 1e-6 ;
    
    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[2][N];
    
    LL a[4] , num[4][N] , cnt[4][N] , tot[4] , tail1 , tail2 ;
    
    void cut( LL n , int i , int cnts ) {
        if( num_cnt.count(n) > 0 ) return ; num_cnt.insert(n);
        num[i][ tot[i] ] = n , cnt[i][tot[i]] = cnts ; tot[i]++;
        if( n % 2 == 0 )  cut( n / 2 , i , cnts + 1 );
        if( n % 3 == 0 )  cut( n / 3 * 2  , i , cnts + 1 );
    }
    
    void test() {
        for( int i = 0 ; i < tail1 ; i ++ ){ cout << e[0][i].x <<' ' << e[0][i].y <<' '<< e[0][i].area << endl ; } cout << endl ;
        for( int i = 0 ; i < tail2 ; i ++ ){ cout << e[1][i].x <<' ' << e[1][i].y <<' '<< e[1][i].area << endl ; } cout << endl ;
    }
    
    void solve() {
    
        int temp1 = 1 , temp2 = 1 ;
        for( int i = 1 ; i < tail1 ; i ++ ){
            if( e[0][i].area == e[0][ temp1 - 1 ].area ) continue ;
            e[0][temp1++] = e[0][i] ;
        }
        for( int i = 0 ; i < tail2 ; i ++ ){
            if( e[1][i].area == e[1][ temp2 - 1 ].area ) continue ;
            e[1][temp2++] = e[1][i] ;
        }
        tail1 = temp1 , tail2 = temp2 ;
    }
    
    
    int find( LL area )
    {
        int l = 0 , r = tail2 ;
        if( e[1][l].area == area ) return l ;
        else if( e[1][r].area == area ) return r ;
        while( l <= r ){
            int mid = ( l + r ) >> 1 ;
            if( e[1][mid].area == area ){
                return mid ;
            }
            else if( e[1][mid].area < area )
                l = mid + 1 ;
            else
                r = mid - 1 ;
        }
        return -1 ;
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif // LOCAL
        ios::sync_with_stdio(false);
        for( int i = 0 ; i < 4 ; ++i ) {
            cin >> a[i] ; num_cnt.clear();
            cut( a[i] , i , 0 );
        }
    //   for( int i = 0 ; i < 4 ; ++i ) cout << tot[i] << endl ;
        tail1 = 0 , tail2 = 0 ;
        for( int i = 0 ; i < tot[0] ; ++i ){
            for( int j = 0 ; j < tot[1] ; ++j ) {
                e[0][ tail1 ].x = num[0][i] ;
                e[0][ tail1 ].y = num[1][j] ;
                e[0][ tail1 ].area = num[0][i] * num[1][j] ;
                e[0][ tail1 ].cnt = cnt[0][i] + cnt[1][j];
                tail1++ ;
            }
        }
    
        for( int i = 0 ; i < tot[2] ; ++i ){
            for( int j = 0 ; j < tot[3] ; ++j ) {
                e[1][ tail2 ].x = num[2][i] ;
                e[1][ tail2 ].y = num[3][j] ;
                e[1][ tail2 ].area = num[2][i] * num[3][j] ;
                e[1][ tail2 ].cnt = cnt[2][i] + cnt[3][j];
                tail2++ ;
            }
        }
    
        sort( e[0] , e[0] + tail1 ) ; sort( e[1] , e[1] + tail2 ) ;  solve();
    
        LL ans = inf , A1 , A0 , B1 , B0 ;
    
        for( int i = 0 ; i < tail1 ; ++i ){
            if( i > 0 && e[0][i].area == e[0][i-1].area ) continue ;
            if( ans <= e[0][i].cnt ) continue ;
            int pos = find( e[0][i].area );
            if( pos == -1 ) continue ;
            if(  ans > e[0][i].cnt + e[1][pos].cnt ){
                ans = e[0][i].cnt + e[1][pos].cnt ;
                A0 = e[0][i].x , B0 = e[0][i].y;
                A1 = e[1][pos].x , B1 = e[1][pos].y;
            }
        }
        if( ans != inf ) cout << ans << '
    ' << A0 << ' ' << B0 << '
    ' << A1 <<' ' << B1 << endl ;
        else cout << "-1" << endl ;
    }
    View Code



    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    前端JavaScript(2) --常用内置对象,函数,伪数组 arguments,关于DOM的事件操作,DOM介绍
    前端JavaScript(3)-关于DOM操作的相关案例,JS中的面向对象、定时器、BOM、位置信息
    前端JavaScript(1) --Javascript简介,第一个JavaScript代码,数据类型,运算符,数据类型转换,流程控制,百度换肤,显示隐藏
    前端CSS(3)
    CSS标签大全
    前端CSS(2)
    前端CSS(1)
    前端HTML(二/三)
    前端基础(1)
    第一个自定义HTML网页
  • 原文地址:https://www.cnblogs.com/hlmark/p/4117584.html
Copyright © 2020-2023  润新知