HDU 4497

GCD and LCM

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2261    Accepted Submission(s): 999


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
题意:三个数的最大公约数是G,最小公倍数是L,问满足这样的条件的三个数有多少组,1,2,3 1,3,2 这样的算两种。

思路:找规律,很明显这个和L/G有关,L无法整除G就是0个。

设d = L/G; d = 1 ans = 1 1 1 1

d = 2 ans = 6 1 1 2 x 3        1 2 2 x 3

d = 3 ans = 6 1 1 3 x 3     1 3 3 x 3

d = 4 ans = 12 1 1 4 x 3    1 4 4 x 3   1 2  4 x 6

d = 5 ans = 6 1 1 5 x 3 1 5 5 x 3

d = 6 ans = 36  1 1 6 x 3,1 2 6 x 6, 1 3 6 x 6, 1 6 6 x 3,1 2 3 x 6,2 2 3 x 6, 2 3 6 x 6 = 36

推到这基本就能推测出结论了。把d质数分解,一个质数就x6如果这个质数出现了多次,就再乘上这个质数出现的次数,然后这个结论在12(样例)上也能行得通。所以正解就是质数分解,再求乘积。

#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
typedef long long ll;
vector<int> pi;
int vis[102400];
void init()
{
    for(int i = 2;i <= 100000;i++)
    {
        if(!vis[i])
        {
            pi.push_back(i);
            int t = i*2;
            while(t <= 100000)
            {
                vis[t] = 1;
                t+=i;
            }
        }
    }
}
int main()
{
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        ll a,b;
        scanf("%lld%lld",&a,&b);
        if(b%a)
        {
            printf("0\n");
            continue;
        }
        ll tmp = b/a;
        ll sum = 1;
        for(int i = 0;i < pi.size();i++)
        {
            if(tmp%pi[i])continue;
            int num = 0;
            while(tmp%pi[i]==0)
            {
                num++;
                tmp/=pi[i];
            }
            sum*=num*6;
        }
        if(tmp>1)
        {
            sum*=6;
        }
        printf("%I64d\n",sum);
    }
    return 0;
}

 


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值