[hdu4497]分解质因数

题意:求满足gcd(x,y,z)=G,lcm(x,y,z)=L的x,y,z的解的个数。

大致思路:首先如果L % G != 0那么无解,否则令u = L / G,问题变为,gcd(r,s,t)=1,lcm(r,s,t)=u的解的个数。然后将u分解质因数,令u=a1p1*...*akpk,考虑一种质因数ai,它不可能同时出现在r,s,t中,枚举所有情况:(1)只出现在r或s或t中,这3种情况答案都为1 (2)出现在r和s或r和t或s和t中,这3种情况答案都为2(pi-1)+1=2pi-1,所以对每一种因子答案为3*(2pi-1)+3=6pi,由乘法原理,最后答案为6k*p1*p2*...*pk。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
int b, a;
LL solve() {
    int x = b / a;
    int p[100], c = 0;
    for (int i = 2; (LL)i * i <= x; i ++) {
        while (x % i == 0) {
            p[c ++] = i;
            x /= i;
        }
    }
    if (x > 1) p[c ++] = x;
    p[c ++] = 0;
    LL ans = 1;
    int k = 0, last = 0;
    for (int i = 1; i < c; i ++) {
        if (p[i] != p[i - 1]) {
            k ++;
            ans *= (i - last);
            last = i;
        }
    }
    for (int i = 0; i < k; i ++) ans *= 6;
    return ans;
}
int main(){
    int T;
    cin >> T;
    while (T --) {
        cin >> a >> b;
        if (b % a != 0) puts("0");
        else cout << solve() << endl;
    }
    return 0;
}

 

 

posted @ 2015-06-03 01:25  jklongint  阅读(330)  评论(0编辑  收藏  举报