$CF1141A Game 23$

这题很简单啊 可以用\(DFS\)来打
毕竟是 \(2^x*3^y=m 输出x+y啊\)
这是最简单的做法

#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
inline LL In() { LL res(0),f(1); register char c ;
    while(isspace(c=getchar())) ; c == '-'? f = -1 , c = getchar() : f = 1 ;
    while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(c=getchar())) ; return res * f ;
}
int n , m ;
int ans = 0x7f7f7f7f;
inline void dfs(int x,int step) {//x是当前数值  step是步数
    if(x > m) return ;
    if(x == m) {//符合条件
        cout << step << endl ;//输出
        exit(0);//这个同return 0; 不过适用于函数中
    }
    dfs(x<<1,step+1) , dfs(x*3,step+1) ;
}
signed main () {
    n = In() , m = In() ;
    dfs(n,0) ;
    if(ans == 0x7f7f7f7f) cout << -1 << endl ;
    return 0 ;
}

猜你喜欢

转载自www.cnblogs.com/qf-breeze/p/10564133.html