只需A的全部质因数包含在B中即可。
#include <iostream> #include <cstdio> #define LL __int64 #include <algorithm> using namespace std; LL gcd(LL a,LL b){ if (b==0) return a; return gcd(b,a%b); } int main(){ int T,kase=0; LL A,B; scanf("%d",&T); while(T--){ scanf("%I64d%I64d",&A,&B); LL g=gcd(A,B); LL t=A/g; LL tmp=gcd(g,t); while(1!=tmp){ t/=tmp; tmp=gcd(g,t); } if(t==1) printf("Case #%d: YES ",++kase); else printf("Case #%d: NO ",++kase); } }