zoukankan      html  css  js  c++  java
  • CF 371B Fox Dividing Cheese[数论]

    B. Fox Dividing Cheese
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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

    分蛋糕,可以吃掉剩下1/2,1/3,1/5,求相同的最小次数


    官方题解
    It is easy to see that the fox can do three type of operations: divide by 2, divide by 3 and divide by 5. Let’s write both given numbers in form a = x·2a2·3a3·5a5, b = y·2b2·3b3·5b5, where x and y are not dibisible by 2, 3 and 5. If x ≠ y the fox can’t make numbers equal and program should print -1. If x = y then soluion exists. The answer equals to |a2 - b2| + |a3 - b3| + |a5 - b5|, because |a2 - b2| is the minimal number of operations to have 2 in the same power in both numbers, |a3 - b3| is the minimal number of operations to have 3 in the same power in both numbers, and |a5 - b5| is the same for 5.

    稍微不同的思路

    如果有解,a b除了2,3,5之外的因子一定在最后的相同里,并且2,3,5因子相同也可以不分,那么最后分成的一定是最大公约数

    //
    //  main.cpp
    //  cf371b
    //
    //  Created by Candy on 9/15/16.
    //  Copyright © 2016 Candy. All rights reserved.
    //
    
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    using namespace std;
    int a,b;
    inline int fac(int &x,int p){
        int cnt=0;
        while(x%p==0) {x/=p;cnt++;}
        return cnt;
    }
    int main(int argc, const char * argv[]) {
        scanf("%d%d",&a,&b);
        if(a==b){printf("0");return 0;}
        
        int a2=fac(a,2),a3=fac(a,3),a5=fac(a,5);
        int b2=fac(b,2),b3=fac(b,3),b5=fac(b,5);
        if(a!=b){printf("-1");return 0;}
        
        printf("%d",abs(a2-b2)+abs(a3-b3)+abs(a5-b5));
        return 0;
    }
  • 相关阅读:
    支付扣款 并发 异步
    Floyd-Warshall算法
    black arch
    ChromeDriver only supports characters in the BMP
    Getting console.log output with Selenium Python API bindings
    微信公众号文章批量采集系统的构建
    node npm Bower
    PyPy CPython C++ connects programs written in C and C++ with a variety of high-level programming languages
    timeout connect 10000 # default 10 second time out if a backend is not found
    timeout connect 10000 # default 10 second time out if a backend is not found
  • 原文地址:https://www.cnblogs.com/candy99/p/5874349.html
Copyright © 2011-2022 走看看