http://acm.hdu.edu.cn/showproblem.php?pid=3353
题目其实就是要把A B分解质因数,X是它们质因数的并集,D是质因数指数的和(如果有相同的质因数,把它们的指数做减法求绝对值)
水题,wa了好多次,手抖最后把while敲成if...
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <algorithm> #include <queue> #include <cmath> #include <stack> #include <set> using namespace std; int a[105],ac[105],b[105],bc[105]; int ABS(int x){ return x>0?x:-x; } int main(){ int A,B; int cas=1; while(~scanf("%d%d",&A,&B)){ if(!A && !B)break; int st1,st2; st1=st2=0; memset(ac,0,sizeof(ac)); memset(bc,0,sizeof(bc)); for(int i=2;i*i<=A;i++){ if(A%i==0){ a[st1]=i; while(A%i==0){ ac[st1]++; A/=i; } st1++; } } if(A>1){ a[st1]=A; ac[st1++]=1; } for(int i=2;i*i<=B;i++){ if(B%i==0){ b[st2]=i; while(B%i==0){ bc[st2]++; B/=i; } st2++; } } if(B>1){ b[st2]=B; bc[st2++]=1; } int i,j; i=j=0; int X,D; X=D=0; while(i<st1 && j<st2){ if(a[i]==b[j]){ X++; D+=ABS(ac[i]-bc[j]); i++;j++; } else if(a[i]<b[j]){ X++; D+=ac[i]; i++; } else{ X++; D+=bc[j]; j++; } } while(i==st1 && j<st2){ X++; D+=bc[j]; j++; } while(j==st2 && i<st1){ X++; D+=ac[i]; i++; } printf("%d. %d:%d\n",cas++,X,D); } return 0; }