371B - Fox Dividing Cheese - CodeForces Solution


math number theory *1300

Please click on ads to support us..

Python Code:

def line2int(linea):
    temp = ""
    for i in linea:
        if i == " ":
            if temp != "":
                val1 = int(temp)
                temp = ""
        else:
            temp = temp + i
    val2 = int(temp)
    return (val1,val2)

(a,b) = line2int(input())
num2a = num2b = 0
num3a = num3b = 0
num5a = num5b = 0
while a%2==0:
    a = int(a/2)
    num2a = num2a + 1
while a%3==0:
    a = int(a/3)
    num3a = num3a + 1
while a%5==0:
    a = int(a/5)
    num5a = num5a + 1
while b%2==0:
    b = int(b/2)
    num2b = num2b + 1
while b%3==0:
    b = int(b/3)
    num3b = num3b + 1
while b%5==0:
    b = int(b/5)
    num5b = num5b + 1
if a==b: print(abs(num2a-num2b)+abs(num3a-num3b)+abs(num5a-num5b))
else: print(-1)
	 						 		 				 			 			 			 		

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int N=1e7+10;
#define ll long long int
#define pb push_back
//#define getUnique(v) {sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());}
ll gcd(ll a,ll b){if(a%b==0)return b;return gcd(b,a%b);}
ll binexp(ll n,ll p){ll res = 1;while (p>0){if(p&1)res=res*n;n=n*n;p>>=1;}return res;}
void solve(){
    ll a,b;
    cin>>a>>b;
    ll sum=0;
    ll x=0,y=0;
    while(a%2==0)a/=2,x++;
    while(b%2==0)b/=2,y++;
    sum+=abs(y-x);
    x=0,y=0;
    while(a%3==0)a/=3,x++;
    while(b%3==0)b/=3,y++;
    sum+=abs(y-x);
    x=0,y=0;
    while(a%5==0)a/=5,x++;
    while(b%5==0)b/=5,y++;
    sum+=abs(y-x);
    if(a!=b)cout<<"-1\n";
    else cout<<sum<<"\n";
}
int main(){ 
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("error.txt", "w", stderr);
freopen("output.txt", "w", stdout);
#endif
    int t;t=1;
    while(t--)
        solve();
}


Comments

Submit
0 Comments
More Questions

810A - Straight A
1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement
903A - Hungry Student Problem
52B - Right Triangles
1712A - Wonderful Permutation
1712D - Empty Graph
1712B - Woeful Permutation
1712C - Sort Zero
1028B - Unnatural Conditions
735B - Urbanization
746C - Tram
1278B - A and B
1353D - Constructing the Array