GCD and LCM

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

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
 
Recommend
liuyiding   |   We have carefully selected several
similar problems for you:  6263 6262 6261 6260 6259 
 

题目大意:
  求gcd(x,y,z)=G且lcm(x,y,z)=L的方法数。
题目分析:
  起初这道题一点想法都没有。。看了题解才有些想法。
  首先如果L不能被G整除的话,这样的组合一定不存在。
  当这样的组合存在的时候,所求与  求gcd(x,y,z)=1且lcm(x,y,z)=L/G的方法数是等价的。
  那么:令temp=L/G。
  对temp进行素数分解:temp=p1^t1 * p2^t2 * ……* pn^tn。
  因为temp是这三个数的倍数,因而x,y,z的组成形式为:
  x=p1^i1 * p2^i2 *…… * pn^in;
  y=p1^j1 * p2^j2 *…… * pn^jn;
  z=p1^k1 * p2^k2 * …… * pn^kn;
  对于某一个素因子p:

          因为要满足x,y,z的最大公约数为1,即三个数没有共同的素因子,所以min(i,j,k)=0。
          又因为要满足x,y,z的最小公倍数为temp,即p^t必然要至少存在一个,所以max(i,j,k)=t。
          换言之:至少要有一个p^t,以满足lcm的要求;至多有两个包含p,以满足gcd的要求。
          因而基本的组合方式为(0,p^t,p^k),k=0-->t。
          而因为(1,2,3)和(2,1,3)是不同的方法,所有满足要求的方法中,除了(0,0,t)和(0,t,t)
                     各有3种排列之外,其余都有6种排列。
          对于某一个素因子p总的方法数为6*(t-1)+2*3=6*t。
  在根据组合排列的知识,素数与素数之间是分步的关系,因而总的方法数为:6*(t1+t2+……+tn)

对于某个r,i、j、k里面一定有一个是r,并且一定有一个是0,所以i,j,k有一下3种情况:

r 0 0 ,有C(3,1)种
r 0 r ,有C(3,1)种
r 0 1~r-1 ,有(r-1)*A(3,3)种

所以一共是6*r种。

题外话(2个的时候):给你gcd(a,b)和lcm(a,b),让你找最小的a和b。
因为(a*b)/gcd=lcm.那么(a/gcd*b/gcd)*gcd=lcm因此(a/gcd*b/gcd)=lcm/gcd。题目即可转换为把lcm/gcd分解成两个互质的数
 #include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
int map[];
int n, m, sum;
int main()
{
int i, j, k, sum;
scanf("%d", &k);
while (k--)
{
scanf("%d%d", &n, &m);
if (m%n)
{
printf("0\n");
continue;
}
memset(map, , sizeof(map));
m = m / n;
j = ;
for (i = ; i*i <= m; i++)//分解质因数m,i<sqrt(m)
{
if (m%i == )
{
while (m%i == )
{
map[j]++;
m = m / i;
}
j++;
}
}
if (m != )
map[j++] = ;
//样例6,72,m为12,分解为2个2,1个3,(6*2)*(6*1)
sum = ;
for (i = ; i<j; i++)
sum = sum * * map[i];
printf("%d\n", sum);
}
return ;
}

HDU 4497 GCD and LCM(数论+容斥原理)的更多相关文章

  1. HDU 4497 GCD and LCM (数论)

    题意:三个数x, y, z. 给出最大公倍数g和最小公约数l.求满足条件的x,y,z有多少组. 题解:设n=g/l n=p1^n1*p2^n2...pn^nk (分解质因数 那么x = p1^x1 * ...

  2. 数论——算数基本定理 - HDU 4497 GCD and LCM

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  3. hdu 4497 GCD and LCM 数学

    GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4 ...

  4. HDU 4497 GCD and LCM (合数分解)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  5. hdu 4497 GCD and LCM (非原创)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  6. hdu 4497 GCD and LCM 质因素分解+排列组合or容斥原理

    //昨天把一个i写成1了 然后挂了一下午 首先进行质因数分解g=a1^b1+a2^b2...... l=a1^b1'+a2^b2'.......,然后判断两种不可行情况:1,g的分解式中有l的分解式中 ...

  7. 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,求满 ...

  8. hdu 4497 GCD and LCM(2013 ACM-ICPC吉林通化全国邀请赛——题目重现)

    质分解 + 简单计数.当时去比赛的时候太年轻了...这道题都没敢想.现在回过头来做了一下,发现挺简单的,当时没做这道题真是挺遗憾的.这道题就是把lcm  / gcd 质分解,统计每个质因子的个数,然后 ...

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

    链接 :  http://acm.hdu.edu.cn/showproblem.php?pid=4497 假设G不是L的约数 就不可能找到三个数. L的全部素因子一定包括G的全部素因子 而且次方数 ...

随机推荐

  1. Codeforces Round #419 (Div. 2) A. Karen and Morning(模拟)

    http://codeforces.com/contest/816/problem/A 题意: 给出一个时间,问最少过多少时间后是回文串. 思路: 模拟,先把小时的逆串计算出来: ① 如果逆串=分钟, ...

  2. BZOJ 2342: 【SHOI2011】 双倍回文

    题目链接:双倍回文 回文自动机第二题.构出回文自动机,那么一个回文串是一个“双倍回文”,当且仅当代表这个串的节点\(u\)顺着\(fail\)指针往上跳,可以找到一个节点\(x\)满足\(2len_x ...

  3. [转][osg]osg渲染引擎框架图,流程图(根据《最长一帧》整理)

    转自:http://m.blog.csdn.net/article/details?id=49679731 本文参考<<osg最长一帧>>, <<OpenScene ...

  4. 解决本地项目推送到码云(github),上提示:failed to push some refs to ...

    本地项目上传github 命令如下: 1.git init 2.git add . 3.git commit  -m "init" 4.git remote add origin ...

  5. vue 2.5.14以上版本render函数不支持返回字符串

    vue 2.5.14以上版本render函数不再支持直接返回字符串,必须返回数组或vnode节点,如果返回字符串的话,渲染为空.详情可见源码. function createFunctionalCom ...

  6. bzoj-4870-组合dp+矩阵幂

    4870: [Shoi2017]组合数问题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 829  Solved: 446[Submit][Statu ...

  7. HDU-4511-ac自动机+dp

    小明系列故事——女友的考验 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  8. 正则,String中用法,Pattern Matcher

    package com.正则表达式; import java.util.Scanner; /** * * 校验qq号码 * 1:要求必须是5-15位数字 * 2: 0不能开头 * 分析: * A:键盘 ...

  9. win10中jdk1.8环境配置完,重启之后配置失效

    1.win10操作系统下重启电脑java环境变量失效 解决方式: 右击开始按钮,选择管理员方式的windows powershell,如下图: 在窗口中输入javac,一切正常,即使重启都不会有问题啦 ...

  10. Mysql基本操作(远程登陆,启动,停止,重启,授权)

    1.查看mysql版本 方法一:status; 方法二:select version(); 2.Mysql启动.停止.重启常用命令 a.启动方式 1.使用 service 启动: [root@loca ...