#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <iostream> using namespace std; struct part { int qq; int q1,q2,q3; }; part get_part(int a) { part temp; int tmp=0; while(a%2==0) { tmp++; a=a/2; } temp.q1=tmp; tmp=0; while(a%3==0) { tmp++; a=a/3; } temp.q2=tmp; tmp=0; while(a%5==0) { tmp++; a=a/5; } temp.q3=tmp; temp.qq=a; return temp; } int main() { int a,b; scanf("%d%d",&a,&b); part aa=get_part(a); part bb=get_part(b); if (aa.qq!=bb.qq) { printf("-1\n"); } else { printf("%d\n",abs(aa.q1-bb.q1)+abs(aa.q2-bb.q2)+abs(aa.q3-bb.q3)); } /* printf("%d\n",abs(aa.q1-bb.q2) ); printf("%d\n",abs(aa.q2-bb.q2)); printf("%d\n",abs(aa.q3-bb.q3)); */ return 0; }
理解了就很好做了。。。可惜之前没理解。。解析 据说还可以“
第2题BFS,搜索规模取决于a和b含有的2,3,5的个数,不超过32个。
371B - Fox Dividing Cheese
It is easy to see that the fox can do three type of operations: divide by 2, divide by 3 and divide by 5. Let’s write both given numbers in form a = x·2a2·3a3·5a5, b = y·2b2·3b3·5b5, where x and y are not dibisible by 2, 3 and 5. If x ≠ y the fox can’t make numbers equal and program should print -1
. If x = y then soluion exists. The answer equals to |a2 - b2| + |a3 - b3| + |a5 - b5|, because |a2 - b2| is the minimal number of operations to have 2 in the same power in both numbers, |a3 - b3| is the minimal number of operations to have 3 in the same power in both numbers, and|a5 - b5| is the same for 5.