HDOJ 4497 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
  
  
2 6 72 7 33
 

Sample Output
  
  
72 0

一.问题分析

这道题很有代表性,通过这道题可以从另一种观点看待gcd和lcm
分析:这题明显要用唯一分解定理来做。gcd(x,y,x)=G,lcm(x,y,z)=L; 
x=p1^a1*p2^a2……ps^as; 
y=p1^b1*p2^b2……ps^bs; 
z=p1^c2*p2^c2……ps^cs; 
G,L已知,满足条件: 
G=p1^min(a1,b1,c1)……ps^min(as,bs,cs)=p1^e1……ps^es 
L=p1^max(a1,b1,c1)……ps^max(as,bs,cs)=p1^h1……ps^hs 
则满足 ei=min(ai,bi,ci) hi=max(ai,bi,bi),考虑组合数。 
考虑有序数对,对于每个(ei,hi) ,让 (x,y,z)固定两个数,让一个数变化有 6种,即 A(3,2)=6,每一种有(hi-ei+1)种。 
然后考虑重复情况,每次,只要(x,y,z)中只出现ei,hi的情况重复,总共重复6次。即 6*(hi-ei+1)-6=6*(hi-ei)种。 
即 6*(h1-e1)*6(h2-e2)……,由于是幂指数相减,考虑L/G就可。如果不能整除,即没有(x,y,z)满足条件。公式推出,题目就好做了!

二.漫长的debug过程

首先我的解决是先构造出来素数表,然后利用素数表来进行。。。。。,然而先不说利用素数表和直接分解质因数那个快,但是这个思路写起来还是很麻烦的。然后还有就是要对数学公式进一步化简之后只用分解L/g就好了,这样就会很简单了啊。。。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=1e7+5;
int factor[maxn],counter;
void init(int n)
{
    counter=0;
    for(int i=2;i<=sqrt(n)+0.0;i++)
    {
        if(n%i==0)
        {
            counter++;
            factor[counter]=0;
            while(n%i==0)
            {
                factor[counter]++;
                n/=i;
            }
        }
    }
    if(n!=1) factor[++counter]=1;
}
int main()
{
    //freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin);
    int T;
    cin>>T;
    for(int t=1;t<=T;t++)
    {
        int l,g;
        cin>>g>>l;
        if(l%g!=0)
        {
            cout<<"0"<<endl;
            continue;
        }
        int k=l/g;
        init(k);
        long long sum=1;
        for(int i=1;i<=counter;i++)
        {
            sum*=6*factor[i];
        }
        cout<<sum<<endl;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值