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 ×2 × 3 \times 3 ×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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值