CF 1141-A Game 23(简单数学)

题目: 传送门
思路: 要保证 a 能通过 乘法得到 b ,则 最大公约数GCD(a.b) == a.否则一定不能 得到,同时, b/a 的值一定并只能 是 二或 三的倍数,否则就不能得到.

#include <iostream>
#include <algorithm>

using namespace std;

long long gcd(long long a,long long b) {
    return b==0? a:gcd(b,a%b);
}

int main() {
    long long n,m;
    cin>>n>>m;
    long long mods = gcd(m,n);
    long long k = m/mods;
    if(mods!=n) cout<<"-1"<<endl;
    else if(m==n){
        cout<<"0"<<endl;
    }
    else {
        int ans = 0;
        while(k!=1) {
            if(k%2==0) {
                k/=2;
                ans++;
            }
            else if(k%3==0){
                k/=3;
                ans++;
            }
            else {
                ans = -1;
                break;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43305984/article/details/89206096
今日推荐