zoukankan      html  css  js  c++  java
  • Codeforces 371BB. Fox Dividing Cheese

    Two little greedy bears have found two pieces of cheese in the forest of weight a and b grams, correspondingly. The bears are so greedy that they are ready to fight for the larger piece. That's where the fox comes in and starts the dialog: "Little bears, wait a little, I want to make your pieces equal" "Come off it fox, how are you going to do that?", the curious bears asked. "It's easy", said the fox. "If the mass of a certain piece is divisible by two, then I can eat exactly a half of the piece. If the mass of a certain piece is divisible by three, then I can eat exactly two-thirds, and if the mass is divisible by five, then I can eat four-fifths. I'll eat a little here and there and make the pieces equal".

    The little bears realize that the fox's proposal contains a catch. But at the same time they realize that they can not make the two pieces equal themselves. So they agreed to her proposal, but on one condition: the fox should make the pieces equal as quickly as possible. Find the minimum number of operations the fox needs to make pieces equal.

    Input

    The first line contains two space-separated integers a and b (1 ≤ a, b ≤ 109).

    Output

    If the fox is lying to the little bears and it is impossible to make the pieces equal, print -1. Otherwise, print the required minimum number of operations. If the pieces of the cheese are initially equal, the required number is 0.

    Examples
    input
    15 20
    output
    3
    input
    14 8
    output
    -1
    input
    6 6
    output
    0

     其实这就是一道数学题,题意可以看成“给两个数a,b,让它们除以2,3,5,最后相等,最少除几次”。

      那么我们先求出他们的最大公约数c(因为要出的次数尽可能小),再看从原数到C分别用几步,或者能否除到C。

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdio> 
    #include<queue>
    #include<math.h>
    using namespace std;
    int a,b,ans;
    int maxyin;
    void chu(int a,int b)
    {
        int aa=a%b;
        if(aa==0)    maxyin=b;
        else chu(b,aa);
    }
    int main()
    {
        cin>>a>>b;    
        int c;
        if(b>a)
        {
            c=a;
            a=b;
            b=c;
        }
        
        if(a==b)//特判    
        {
            cout<<0;
            return 0; 
        }
        if(a%b==0)//特潘 
        {
            c=a/b;        
            while(c%5==0)
                c/=5,ans++;
            while(c%3==0)
                c/=3,ans++;
            while(c%2==0)
                c/=2,ans++;
            if(c==1)
                cout<<ans;
            else cout<<-1;
            return 0;
        }
        chu(a,b);
        c=a/maxyin;int d =b/maxyin;
        if(((c%5)&&(c%3)&&(c%2))||((d%5)&&(d%3)&&(d%2)))    
        {
            cout<<-1;
            return 0;
        }
        ans=0;
        while(c%5==0)
            c/=5,ans++;
        while(c%3==0)
            c/=3,ans++;
        while(c%2==0)
            c/=2,ans++;
        while(d%5==0)
            d/=5,ans++;
        while(d%3==0)
            d/=3,ans++;
        while(d%2==0)
            d/=2,ans++;
        cout<<ans;
        return 0;
    }

     来个好看的代码

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdio> 
    #include<queue>
    #include<math.h>
    using namespace std;
    int a,b;
    int a2,a3,a5,b2,b3,b5;
    int x;
    void gcd(int a,int b)
    {
        int aa=a%b;
        if(aa==0)    {x=b;return ;} 
        else gcd(b,aa); 
    }
    int main()
    {
        cin>>a>>b;
        if(a==b)
        {
            cout<<0;
            return 0;
        }
        gcd(a,b);
        int m=a/x,n=b/x;
        while(m%3==0)    m/=3,a3++;
        while(m%2==0)    m/=2,a2++;
        while(m%5==0)    m/=5,a5++;
        while(n%3==0)    n/=3,b3++;
        while(n%2==0)    n/=2,b2++;
        while(n%5==0)    n/=5,b5++;
        if(m*n==1)
        {
            cout<<(a3+a2+a5+b2+b3+b5);
            return 0;
        }
        cout<<-1;
        return 0;
    }

     下面是个搜索的

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdio> 
    #include<queue>
    #include<math.h>
    #include<vector>
    using namespace std;
    int  a,b;
    struct ab{
        int a;int b;
        int ans;
    }k;
    queue<ab>q;
    int f,ans;
    void bfs()
    {
        ab g,n;    
        g=q.front();q.pop();
        while(g.a!=g.b)
        {
            f=0;
            if(g.a>g.b)
            {
                n=g;
                if(g.a%2==0)
                {    n=g;
                    n.ans++;
                    n.a=g.a/2;
                    q.push(n);
                    f=1;
                }
                if(g.a %3 ==0)
                {    n=g;
                    n.ans++;
                    n.a=g.a/3;
                    q.push(n);
                    f=1;
                }
                if(g.a %5==0)
                {    n=g;
                    n.ans++;
                    n.a=g.a/5;
                    q.push(n);
                    f=1;
                }
            }else
            {
            
                if(g.b%2==0)
                {    n=g;
                    n.ans++;
                    n.b=g.b/2;
                    q.push(n);
                    f=1;
                }
                if(g.b %3 ==0)
                {    n=g;
                    n.ans++;
                    n.b=g.b/3;
                    q.push(n);
                    f=1;
                }
                if(g.b %5==0)
                {    n=g;
                    n.ans++;
                    n.b=g.b/5;
                    q.push(n);
                    f=1;
                }
            }
            g=q.front();ans=g.ans;
            q.pop();
            if(!f)
                return ;
        }
        
    }
    int main( )
    {
        cin>>k.a>>k.b;
        if(k.a==k.b)
        {
            cout<<0;
            return 0;
        }    
        k.ans=0;
        q.push(k);
        bfs();
        if(!f){
            cout<<-1;
            return 0;
        }else
        cout<<ans;
        return 0;
    }
  • 相关阅读:
    WebService cxf提供接口
    在文件系统的某一个目录中查找某一个字符串
    在notepad++中插件安装的办法
    windows中的oracle12SE后启动的系统服务的列表
    在windows环境初步了解tuxedo
    使用MS VS的命令来编译C++程序
    我所常用的git命令
    使用python对文件中的数值进行累加
    C++中继承关系中的同名隐藏和对策
    用eclipse来制作并使用可执行的jar文件
  • 原文地址:https://www.cnblogs.com/CLGYPYJ/p/7028662.html
Copyright © 2011-2022 走看看