Hdu4497 GCD and LCM 素数筛法+分解质因数

Problem Description

Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L? 
Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z. 
Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.

Input

First line comes an integer T (T <= 12), telling the number of test cases. 
The next T lines, each contains two positive 32-bit signed integers, G and L. 
It’s guaranteed that each answer will fit in a 32-bit signed integer. 

Output

For each test case, print one line with the number of solutions satisfying the conditions above.

Sample Input


6 72 
7 33 

Sample Output

72 
0


题意: 已知三个数的最小公倍数和最大公约数,求这样的三元组组数,

我们首先可以确定,三元组存在当且仅当 g 为 l 的约数。

在满足这个条件时,我们再去求解,这样能节省时间。

首先分解质因数,假设对于质因数x,三个数分别有ai,bi,ci个,LCM中有zi个,那么这三个数中至少有一个为zi,还至少有一个0,剩下的随便选。

我采用的分解质因数的方法是先用筛法,在进行分解,而素数筛选可以只进行到数范围的根号以内,

因为在这个范围外,至多只可能有一个素因子,那在循环后加一个判断即可解决。


#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
#include <map>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
#define LL long long
#define maxn 100001
int a[maxn];
LL prime[10000],c;
int v[maxn];
void p()
{
    LL i,j,n=maxn,m;
    c=0;
    m=(LL)sqrt(n+0.5);
    memset(v,0,sizeof(v));
    for(i=2;i<=m;i++)
        if(!v[i]){
            for(j=i*i;j<=n;j+=i)
                v[j]=1;
        }
    for(j=2;j<=n;j++){
        if(!v[j]){
            prime[c++]=j;
        }
    }
}

void ad(LL n)
{
    for(int i=0;i<c&&n>1;i++){
        while(n%prime[i]==0){
            n/=prime[i];
            a[i]++;
        }
    }
    if(n>1){
        a[c++]=1;
    }
}
int main()
{
    int t;
    LL g,l;
    p();
    scanf("%d",&t);
    while(t--){
        memset(a,0,sizeof(a));
        scanf("%lld%lld",&g,&l);
        if(l%g!=0||g>l){
            printf("0\n");
        }else{
            l/=g;
            ad(l);
            LL ans=1;
            for(int i=0;i<c;i++){
                if(a[i]>0){
                    ans*=(6*a[i]);
                }
            }
            printf("%lld\n",ans);
        }
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值