week10作业签到

问题描述

东东在玩游戏“Game23”。

在一开始他有一个数字n,他的目标是把它转换成m,在每一步操作中,他可以将n乘以2或乘以3,他可以进行任意次操作。输出将n转换成m的操作次数,如果转换不了输出-1。

Input

输入的唯一一行包括两个整数n和m(1<=n<=m<=5*10^8).

Output

输出从n转换到m的操作次数,否则输出-1.

Sample input & output

Sample input1
120 51840
Sample output1
7
Sample input2
42 42
Sample output2
0
Sample intput3
48 72
Sample output3
-1

解题思路

签到题,开始看到 × 2 \times 2 × 3 \times 3 下意识以为是刚刚看过的动态规划的改编版,写着写着发现就是一个纯签到,毕竟2和3没有公因子。

完整代码

//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

int n,m,ans;
int getint(){
    int x=0,s=1; char ch=' ';
    while(ch<'0' || ch>'9'){ ch=getchar(); if(ch=='-') s=-1;}
    while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar();}
    return x*s;
}
int main(){
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    n=getint(); m=getint();
    if(m%n!=0) {
        printf("-1\n"); return 0;
    }
    m/=n;
    while(m!=1){
        if(m%2==0) m/=2,ans++;
        else if(m%3==0) m/=3,ans++;
        else { printf("-1\n"); return 0;}
    }
    printf("%d\n",ans);
    return 0;
}
原创文章 61 获赞 58 访问量 6480

猜你喜欢

转载自blog.csdn.net/weixin_43347376/article/details/105735326