hdu 4497 GCD and LCM (唯一分解定理 + 计数)

78 篇文章 0 订阅
2 篇文章 0 订阅

GCD and LCM

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


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
 

Source
2013 ACM-ICPC吉林通化全国邀请赛——题目重现

大体题意:
告诉你G和L,求出有多少个(X,Y,Z)对组合 ,满足, 他们的最大公约数为G,最小公倍数是L。
思路:
比赛时没有做出来,借鉴的学长的博客~~,其实推理的差不多了,就是无法实现最后的一步,还是练的少啊= = !
如果L不能整除G的话,那么答案就是0。
否则可以把G,2G,3G,4G,,,,,L 都除以G,变成1,2,3,4,5,,,n。
所以n = L/G。
把n 质因数分解:
得到: p1^t1 + p2^t2 + p3 ^ t3 + .......
那么符合条件的x,y,z满足:
x = p1^i1 + p2 ^i2 + p3 ^ i3 + .....
y = p1^j1 + p2 ^j2 + p3 ^ j3 + .....
z = p1^k1 + p2 ^k2 + p3 ^ k3 + .....
首先x,y,z最大公约数是1的话,那么i1,j1,k1 至少一个为0,至少一个为t1,但又不能全是:
所以所有的组合情况为:
 0 0 t1 --> 总共3种组合
 0 t1 t1 --> 总共3种组合
t1 0 1~t1-1  总共t1 - 1种组合!
那么总组合为  6*t1 - 6 + 3+ 3 = 6*t1种组合!
那么用唯一分解定理分解n 即可! 枚举t!
n 最大是int 2^ 31  那么只需要枚举 2^16进行分解即可!
详细见代码:
 #include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<list>
typedef long long ll;
typedef unsigned long long llu;
const int maxn = 1000 + 10;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;

int len = 1 << 16;

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int a,b;
        scanf("%d %d",&a,&b);
        if (b % a != 0){
            printf("0\n");
            continue;
        }
        ll ans = 1ll;
        int n = b/a;
        for (int i = 2; i <= len; ++i){
            if (n % i == 0){
                int sum = 0;
                while(n % i == 0)n /= i,++sum;
                ans *= sum*6;
            }
        }
        if (n > 1)ans *= 6;
        printf("%I64d\n",ans);
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值