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

来源:互联网 发布:家具拆单下料软件 编辑:程序博客网 时间:2024/05/22 10:25

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 100001int 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 0
原创粉丝点击