1141A - Game 23 - CodeForces Solution


implementation math *1000

Please click on ads to support us..

Python Code:

n, m = map(int, input().split())
d = 0
k = 0
if m%n != 0:
    print(-1)
else:
    d = m//n
    while d!= 1:
        if d %2 == 0:
            d =d//2
            k+=1
        elif d%3 == 0:
            d = d // 3
            k += 1
        else:

            d = 1
            k = -1
    print(k)



C++ Code:

using namespace std;
#include<bits/stdc++.h>
#include <iostream>
#include <cmath>
#define _DrIndex_ ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0);
#define clr(x,val) memset(x,val,sizeof(x))
#define read(FILE) freopen(FILE, "r", stdin);
#define write(FILE) freopen(FILE, "w", stdout);
#define ll long long
#define  ul unsigned int
#define ull unsigned long long
#define ld long double
void read_write();
/*===================================================*/
bool preceed(pair<char,int> x , pair<char,int> y) {return x > y;}
/*===================================================*/
bool sortbyCond(const pair<int, int>& a,
                const pair<int, int>& b)
{
    if (a.first != b.first)
        return (a.first < b.first);
    else
        return (a.second > b.second);
}
/*===================================================*/
int factDigits(int n){
    if(n<0)return 0;if(n<=1)return 1;double digits=0;
    for (int i = 2; i <= n; ++i) {
        digits+= log10(i);
    }
    return floor(digits)+1;
}
/*===================================================*/
void primeFactors(int n){int c=2;while(n>1){if(n%c==0){std::cout<<c<<" ";n/=c;}else c++;}}
/*===================================================*/
bool cmp(std::pair<std::string,int>&a,std::pair<std::string,int>&b){return a.second>b.second;}
/*===================================================*/
bool absPreceed(int x , int y){return abs(x)< abs(y);}
/*===================================================*/
ll noOfDigits(ll n){return 1+floor(log10(n));}
/*===================================================*/
ll gcd(ll a, ll b)
{
    if (a == 0) return b;
    return gcd(b%a, a);
}
/*===================================================*/
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b))*b;
}
/*===================================================*/
// kadane's algorithm for getting max sum of sub array in O(n)
int maxSubArraySum(int array[], int size)
{
    int max_so_far = INT_MIN, max_ending_here = 0;

    for (int i = 0; i < size; i++)
    {
        max_ending_here = max_ending_here + array[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;

        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}

/*===================================================*/
long long MOD = 1e9 + 7;

ll fast_power(ll base , ll power){
    ll  res=1;
    while (power > 0){
        if(power & 1){
            res=(res*base)%MOD;
        }
        base = (base*base) % MOD;
        power >>= 1;
    }
    return res;
}
/*===================================================*/
// get no. ones in the binary representation
int popCount(int num){
    int res=0;
    while(num > 0){
        res += num & 1;
        num >>= 1;
    }return res;
}
/*===================================================*/

const int N = 1e6+5;
int n,m,ans=-1;
void gen(int cnt , int num){
    if(num >= m) {if(num == m)ans=cnt;return;}
    gen(cnt+1,num*2);
    gen(cnt+1,num*3);
}
int main() {
    _DrIndex_
    read_write();
    int tt=1;//cin>>tt;
    while (tt--) {
        cin >> n >> m;
        gen(0,n);
        cout << ans;
    }
    return 0;
}
void read_write() {


#ifndef ONLINE_JUDGE
    read("../input.txt")
    //write("../output.txt")
#  endif



}


Comments

Submit
0 Comments
More Questions

1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations
677C - Vanya and Label
1583B - Omkar and Heavenly Tree
1703C - Cypher
1511C - Yet Another Card Deck
1698A - XOR Mixup
1702E - Split Into Two Sets
1703B - ICPC Balloons
1702F - Equate Multisets
1700A - Optimal Path
665C - Simple Strings
1708A - Difference Operations
1703E - Mirror Grid
1042A - Benches
1676B - Equal Candies
1705B - Mark the Dust Sweeper
1711A - Perfect Permutation
1701B - Permutation
1692A - Marathon