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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章