GCD and LCM HDU - 4497

http://acm.hdu.edu.cn/showproblem.php?pid=4497

Decompose the prime factors of gcd and lcm. If the power pi of a prime factor of gcd is greater than the power qi of the corresponding prime factor of lcm, then it cannot be made up because gcd must be the lcm factor

If pi<=qi xyz, one of the three numbers must be pi, and one must be qi, because gcd is to obtain the minimum of the three, lcm, and the maximum power of only one number left. ti can be changed when ti=pi or ti=qi When ti!=pi and ti!=qi can generate 3!/2! possible meanings, it means to arrange all three powers. When ti!=pi and ti!=qi, then there are 3! possible powers. Can

 

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e2+10;

int main()
{
    ll a1[maxn],a2[maxn],p1[maxn],p2[maxn];
    ll gcd,lcm,ans;
    int t,n1,n2,i,j,flag;
    scanf("%d",&t);
    while(t--){
        scanf("%lld%lld",&gcd,&lcm);

        n1=0;
        for(i=2;i*i<=gcd;i++){
            if(gcd%i==0){
                n1++;
                a1[n1]=i,p1[n1]=0;
                while(gcd%i==0){
                    gcd/=i;
                    p1[n1]++;
                }
            }
        }
        if(gcd!=1){
            n1++;
            a1[n1]=gcd,p1[n1]=1;
        }

        n2=0;
        for(i=2;i*i<=lcm;i++){
            if(lcm%i==0){
                n2++;
                a2[n2]=i,p2[n2]=0;
                while(lcm%i==0){
                    lcm/=i;
                    p2[n2]++;
                }
            }
        }
        if(lcm!=1){
            n2++;
            a2[n2]=lcm,p2[n2]=1;
        }
        /*
        printf("***%d***\n",n1);
        for(i=1;i<=n1;i++){
            printf("%lld %lld\n",a1[i],p1[i]);
        }
        printf("***%d***\n",n2);
        for(i=1;i<=n2;i++){
            printf("%lld %lld\n",a2[i],p2[i]);
        }
        */
        i=1,j=1,flag=1,ans=1;
        while(i<=n1&&j<=n2){
            if(a1[i]<a2[j]){
                flag=0;
                break;
            }
            else if(a1[i]==a2[j]){
                if(p1[i]>p2[j]){
                    flag=0;
                    break;
                }
                else if(p1[i]<p2[j]){
                    ans*=6*(p2[j]-p1[i]);
                }
                i++,j++;
            }
            else{
                ans*=6*p2[j];
                j++;
            }
        }
        if(i<=n1) flag=0;
        while(j<=n2){
            ans*=6*p2[j];
            j++;
        }
        if(flag) printf("%lld\n",ans);
        else printf("0\n");
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325480663&siteId=291194637
gcd