以前写过类似的,这个应该比那个难些
(x/gcd(x,y,z))*(y/gcd(x,y,z))*(z*gcd(x,y,z)=lcm(x,y,z)/gcd(x,y,z);
对lcm(x,y,z)/gcd(x,y,z)唯一分解;
对于每个pi^ai,x/gcd(x,y,z),y/gcd(x,y,z),z/gcd(x,y,z)中必有一个取0,一个取ai,另一个任意;
容斥原理,除去重复的;
(ai+1)*(ai+1)*(ai+1)-2*ai*ai*ai+(ai-1)*(ai-1)*(ai-1);
ps:ai=1时,容斥得6;
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 int sign[100000]; 7 int pri[100000]; 8 int tot; 9 10 void getpri (){ 11 memset (sign,0,sizeof sign); 12 for (int i=2;i<100000;i++) 13 if (!sign[i]) 14 for (int j=i*2;j<100000;j+=i) 15 sign[j]=1; 16 tot=0;//cout<<"error"; 17 for (int i=2;i<100000;i++) 18 if (!sign[i]) 19 pri[tot++]=i; 20 } 21 22 int mm[100000]; 23 24 int getpow (int n){ 25 return n*n*n; 26 } 27 28 void solved (int s){ 29 int num=0; 30 int ans=1; 31 int x=s; 32 memset (mm,0,sizeof mm); 33 for (int i=0;i<tot;i++){//cout<<x<<endl; 34 if (x%pri[i]==0){ 35 while (x%pri[i]==0){ 36 mm[i]++; 37 x/=pri[i]; 38 } 39 ans*=getpow(mm[i]+1)-2*getpow(mm[i])+getpow(mm[i]-1);//cout<<mm[i]<<" "<<pri[i]<<endl; 40 num++; 41 } 42 } 43 if (x!=1) 44 ans*=6; 45 cout<<ans<<endl; 46 47 } 48 49 int main (){ 50 getpri(); 51 int t; 52 int g,l; 53 cin>>t; 54 while (t--){ 55 cin>>g>>l; 56 if (l%g!=0){ 57 cout<<"0"<<endl; 58 continue ; 59 } 60 solved (l/g); 61 62 } 63 return 0; 64 }