• 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;
    }
  • 相关阅读:
    python爬虫实例--爬取拉勾网
    面试时,如何回答你还有什么想要了解的?
    深入理解三次握手四次挥手以及使用scapy实现ddos雏形
    解决socket粘包的两种low版模式 os.popen()和struct模块
    浅谈osi模型 三次握手 四次挥手 ddos攻击原理
    渲染相关书籍
    unity 场景编辑器物体加图标
    音乐模拟器
    3d服装制作软件
    uv投影插值
  • 原文地址:https://www.cnblogs.com/CLGYPYJ/p/7028662.html
Copyright © 2020-2023  润新知