codeforces 371B - Fox Dividing Cheese
#include<stdio.h>
int count;
int gcd(int a,int b) {
if(b==0)
return a;
return gcd(b,a%b);
}
int seach(int a) {
if(a%2==0) {
count++;
return seach(a/2);
}
if(a%3==0) {
count++;
return seach(a/3);
}
if(a%5==0) {
count++;
return seach(a/5);
}
return a;
}
int main() {
int n,m,i,j,k,a,b,counta,countb,flag,sum;
while(scanf("%d%d",&n,&m)!=EOF) {
if(n==m) {
printf("0\n");
continue;
}
k=gcd(n,m);
a=n/k;
b=m/k;
flag=0;
count=0;sum=0;
counta=seach(a);
sum+=count;
count=0;
countb=seach(b);
sum+=count;
if(counta==1&&countb==1)
printf("%d\n",sum);
else
printf("-1\n");
}
return 0;
}
int count;
int gcd(int a,int b) {
if(b==0)
return a;
return gcd(b,a%b);
}
int seach(int a) {
if(a%2==0) {
count++;
return seach(a/2);
}
if(a%3==0) {
count++;
return seach(a/3);
}
if(a%5==0) {
count++;
return seach(a/5);
}
return a;
}
int main() {
int n,m,i,j,k,a,b,counta,countb,flag,sum;
while(scanf("%d%d",&n,&m)!=EOF) {
if(n==m) {
printf("0\n");
continue;
}
k=gcd(n,m);
a=n/k;
b=m/k;
flag=0;
count=0;sum=0;
counta=seach(a);
sum+=count;
count=0;
countb=seach(b);
sum+=count;
if(counta==1&&countb==1)
printf("%d\n",sum);
else
printf("-1\n");
}
return 0;
}