Hdu 4497

题目链接

已知 gcd(x, y, z) = G, lcm(x, y, z) = L,
求有多少种组合(x, y, z)可以满足条件。G, L都在32位int范围内。

思路: 素数分解 + 容斥

L : p1^t1 * p2^t2 ... * pi^ti

G: q1^s1 * q2^s2... * qi^si

若 L % G 不为0, 则不存在解;

否则 L分解结果中素因子的长度一定不小于G分解结果的素因子个数,

且对应的素数的指数部分前者(L的分解结果)一定不小于后者(G的分解结果)。

比如,对于共同的一个质因子pi, L和G分解结果中对应的指数分别为 b和c, 那么b >= c,

且三个数(也就是x, y,z)的分解因式中一定有一个pi对应的指数为c, 同时也有一个为c

另一个为b~c中任意一个数。

利用容斥: 结果 = 任意3个b~c中的数的组合个数(即(b-c+1)^3) - 没有出现b的组合个数(即(b-c)^3)

      - 没有出现c的组合个数(即(b-c)^3) + 没有出现b也没有出现c的组合个数((b-c-1)^3).

附上代码:


 1 /*************************************************************************
2 > File Name: 4497.cpp
3 > Author: Stomach_ache
4 > Mail: [email protected]
5 > Created Time: 2014年05月20日 星期二 09时40分42秒
6 > Propose: 素数分解 + 容斥
7 ************************************************************************/
8
9 #include <cmath>
10 #include <string>
11 #include <cstdio>
12 #include <vector>
13 #include <fstream>
14 #include <cstring>
15 #include <iostream>
16 #include <algorithm>
17 using namespace std;
18
19 typedef long long LL;
20 typedef pair<int, int> pii;
21 #define X first
22 #define Y second
23 const int MAX_N = (100005);
24 int prime[10000], cnt = 0 // 记录素数;
25 bool vis[MAX_N+1];
26 vector<pii> cnt1, cnt2; // 记录素数分解结果
27
28 // 素数筛
29 void
30 get_prime() {
31 memset(vis, true, sizeof(vis));
32 for (int i = 2; i < MAX_N; i++) {
33 if (vis[i]) {
34 prime[cnt++] = i;
35 for (LL j = (LL)i*i; j < MAX_N; j += i) {
36 vis[j] = false;
37 }
38 }
39 }
40 }
41
42 // 素数分解
43 void
44 split(int n, vector<pii> &ret) {
45 ret.clear();
46 for (int i = 0; i < cnt && prime[i] <= n/prime[i]; i++) {
47 if (n % prime[i] == 0) {
48 int count = 0;
49 while (n % prime[i] == 0) {
50 count++;
51 n /= prime[i];
52 }
53 ret.push_back(pii(prime[i], count));
54 }
55 }
56 if (n != 1) {
57 ret.push_back(pii(n, 1));
58 }
59 }
60
61 int
62 main(void) {
63 //素数筛
64 get_prime();
65 int t;
66 scanf("%d", &t);
67 while (t--) {
68 int G, L;
69 scanf("%d %d", &G, &L);
70 if (L % G) {
71 puts("0");
72 continue;
73 }
74 //素数分解
75 split(G, cnt1);
76 split(L, cnt2);
77 int ans = 1;
78 int len1 = cnt1.size(), len2 = cnt2.size();
79 for (int i = 0, j = 0; i < len1; i++, j++){
80 while (cnt1[i].first != cnt2[j].first) {
81 j++;
82 }
83 cnt2[j].second -= cnt1[i].second;
84 }
85 for (size_t i = 0; i < len2; i++) {
86 if(cnt2[i].second == 0) {
87 ans += 0;
88 } else {
89 int c = cnt2[i].second;
90 //容斥
91 ans *= ((LL)c+1)*(c+1)*(c+1) - 2*((LL)c)*(c)*(c) + ((LL)c-1)*(c-1)*(c-1);
92 }
93 }
94 printf("%d\n", ans);
95 }
96
97 return 0;
98 }

时间: 2024-08-13 19:58:48

Hdu 4497的相关文章

hdu 4497 GCD and LCM(排列组合)

题目:hdu 4497 GCD and LCM 题目大意:给出三个数的最大公约数,和最小公倍数,问这三个数的排列组合关系. 解题思路:最小公倍数/最大公约数 ==  三个数不同部分的乘积.这样来考虑的话,三个数都要有最大公约数的部分,其余的部分就是由LCM / GCD 里面的因子构成.这里面的因子可能会有 2 2 3 这样的情况, 不同的因子之间是不会相互干扰的,但是相同的会出现问题,因为,不能同时将相同的因子都放在三个位置上,这样最大公约数就的要乘上这个因子.然后对于单种因子来考虑的话,每种因

HDU 4497 GCD and LCM (分解质因数)

链接 : ?? http://acm.hdu.edu.cn/showproblem.php?pid=4497 假设G不是L的约数 就不可能找到三个数. L的全部素因子一定包括G的全部素因子 而且次方数一定大于等于G的.仅仅须要三个数 对于每个素因子的次方数 三个的最小值是G的,最大值是L的.考虑三个相应的次方数都不一样.那么当中两个是确定的 一个是G的一个是L的 剩下的一个在G和L的之间. 算上排列 总共同拥有6种.或者当中两个是一样的,那么也有6种情况. 最后能够合并计算. //#pragma

hdu 4497 GCD and LCM 数论 素数分解

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1339    Accepted Submission(s): 607 Problem Description Given two positive integers G and L, could you tell me how many solutions of

Hdu 4497 GCD and LCM(数论)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4497 思路:x%G==0,y%G==0,z%G==0,所以L%G==0,若L%G!=0则一定无解. 考虑 L/G=(p1^t1)*(p2^t2)*......*(pn^tn) x'=x/G=(p1^a1)*(p2^a2)*......*(pn^an) y'=y/G=(p1^b1)*(p2^b2)*......*(pn^bn) z'=z/G=(p1^c1)*(p2^c2)*.......*(pn^cn

hdu 4497 GCD and LCM 数学

GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4497 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

HDU 4497 GCD and LCM(分解质因子+排列组合)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满足要求的(x, y, z)有多少组,并且要考虑顺序. 思路:如果L%G != 0显然不存在这样的(x, y, z),相反肯定存在.具体做法就是将L/G分解质因子,得到:L/G = P1^t1 * P2^t2 * ... * Pk^tk,我们来考虑任意一个因子Pi^ti,此时(x/G, y/G, z/

hdu 4497 GCD and LCM

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1092    Accepted Submission(s): 512 Problem Description Given two positive integers G and L, could you tell me how many solutions of (

HDU 4497 素数筛,合数分解

题意:给你两个数,G和L ,它们是一组数的GCD(最大公约数)和LCM(最小公倍数),求出满足条件的组合数,每个组合有三个数,排序不同,也算不同组合. L : p1^t1 * p2^t2 ... * pi^ti G: q1^s1 * q2^s2... * qi^si (pi和qii都是素数ii) G(最大公约数)里出现的所有数都要在L(最小公倍数)里出现,或者说L mod G=0,有人要问为什么了?其实仔细想想就清楚了,如果a,b,c的最小公倍数肯定可以被a,b,c任意一个整除,而他们的最大公约

hdu 4497 GCD and LCM(唯一分解+容斥原理)

GCD and LCM Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 78 Accepted Submission(s): 43 Problem Description Given two positive integers G and L, could you tell me how many solutions of (x, y, z)