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

6
  

Sample Output

72
0 大概题意: 给出T组数据每组数据有两个数分别为x,y,z的最大公约数和
最小公倍数,让我们求出x,y,z总共有多少组不同组合方式; 具体思路
        考虑先分解最小公倍数。合数分解后,再分解最大公约数,可知,如果最大公约数中有最小公倍数中没有的质因数因子的话,那么答案肯定为0
        然后考虑每一个因子pi有设合数分解最小公倍数的个数为bi合数分解最大公约数的个数为bi
       
  下面有两种考虑方法
 
   1.排列组合
 
         易得三个数中的对于pi的情况必须有一个个数是bi,另一个是ai,然后就可以先选出两个位置一个bi一个ai然后最后一个位置上的个数一定介于ai和bi之间即(bi-ai-1)种情况。
        所以最后的公式为ans *=  A(3,2)*(bi-ai-1) = 6*(bi-ai-1) ;
 
       注意
 
         如果先筛素数的时候筛到1^6 然后如果L除以最后一个素数的时候不等于1,那么说明它(L的最后一个因子)一定是大于10^6的一个素数,因为10^12 = 10^6^2 > x^2>y;如果y存在
       一个非素数的因子k的话,有k*t = y 且k>x,则t<x则t已经被筛掉了。    所以剩下的因子一定是素因子。一开始没有考虑这种特殊情况wa掉了。还要注意只有当(bi-ai-1) 有意义的时候才可以计算,因为如果bi==ai的时候可以发现正确结果是对于这一位应该是只用一种情况,就是三个数都相等,所以要特判一下。
 
 
    2. 容斥定理
 
        同样是考虑每个因子,有所有的情况是每个位置都可以取(bi-ai+1)种情况即(bi-ai+1)^3,要减去没有bi个因子的情况和没有ai个因子的情况即2*(bi-ai)^3
       然后发现减多了,要加上同时没有因子ai和bi的情况即(bi-ai-1)^3 这里同样要注意上面的注意。
 
操作代码如下
 
#include<iostream>
using namespace std;
#define N 100100
#define ll long long
ll d[N][],e[N][],cntn,cntm;
void devide(int n,int m)
{
cntn=cntm=;
for(int i=;i*i<=n;i++)
{
if(n%i==)
{
int num=;
while(n%i==)
{
num++;
n/=i;
}
d[++cntn][]=i,d[cntn][]=num;
}
}
for(int i=;i*i<=m;i++)
{
if(m%i==)
{
int num=;
while(m%i==)
{
num++;
m/=i;
}
e[++cntm][]=i,e[cntm][]=num;
}
}
if(n>)d[++cntn][]=n,d[cntn][]=;
if(m>)e[++cntm][]=m,e[cntm][]=;
}
ll solve(int n,int m)
{
if(m%n!=)
return ;
devide(n,m);
ll ans=,v;
for(int i=;i<=cntm;i++)
{
int flag=;
for(int j=;j<=cntn;j++)
if(e[i][]==d[j][])
{
flag=;
v=j;
break;
}
if(!flag)
ans=ans**e[i][];
else
{
ll t=e[i][]-d[v][];
if(t==)continue;
ans=ans**t;
}
}
return ans;
}
int main()
{
int t;
int n,m;
cin>>t;
while(t--)
{
cin>>n>>m;
ll ans=solve(n,m);
cout<<ans<<endl;
}
return ;
}

实践是检验真理的唯一标准;

GCD and LCM HDU - 4497(质因数分解)的更多相关文章

  1. GCD and LCM HDU 4497 数论

    GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD ...

  2. GCD and LCM HDU - 4497

    题目链接:https://vjudge.net/problem/HDU-4497 题意:求有多少组(x,y,z)满足gcd(x,y,z)=a,lcm(x,y,z)=b. 思路:对于x,y,z都可以写成 ...

  3. HDU 1045(质因数分解)

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description Tomor ...

  4. HDU 1695 GCD 欧拉函数+容斥原理+质因数分解

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...

  5. G - GCD and LCM 杭电

    Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, sa ...

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

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

  7. HDU 4497 GCD and LCM (数学,质数分解)

    题意:给定G,L,分别是三个数最大公因数和最小公倍数,问你能找出多少对. 析:数学题,当时就想错了,就没找出规律,思路是这样的. 首先G和L有公因数,就是G,所以就可以用L除以G,然后只要找从1-(n ...

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

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

  9. 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 ...

随机推荐

  1. vue-cli 本地代理 造成session丢失 而登录不上去 解决办法

    本地代理造成session丢失,登录不成功,是由于代理配置造成的 devServer: { port: 8000, proxy:{ '/qiantai':{ target:'线上地址/qiantai' ...

  2. jmeter从文件中读取参数,并实现循环

    1. 通过BeanShell Sampler获取csv的行数 import java.io.BufferedReader;import java.io.FileReader;BufferedReade ...

  3. JVM——内存结构

    一.程序计数器/PC寄存器 (Program Counter Registe) 用于保存当前正在执行的程序的内存地址(下一条jvm指令的执行地址),由于Java是支持多线程执行的,所以程序执行的轨迹不 ...

  4. Postman官方文档翻译

    本文根据官方文档,为千夜同学的翻译做了一个目录 启动Postman Postman的安装和升级 发送第一个请求 创建第一个集合 Postman导航 Postman账号 同步 设置 新按钮 发送API请 ...

  5. 第三章 python数据规整化

    本章概要 1.去重 2.缺失值处理 3.清洗字符型数据的空格 4.字段抽取 去重 把数据结构中,行相同的数据只保留一行 函数语法: drop_duplicates() #导入pandas包中的read ...

  6. CPU分支预测器

    两篇结合就ok啦 1.https://www.jianshu.com/p/be389eeba589 2.https://blog.csdn.net/edonlii/article/details/87 ...

  7. DataFactory生产手机号码

    表中的数据类型是CHAR()类型的,才会出现,如右图的“Build a composite field”的这个功能: 固定部分设置 剩余变化部分设置 操作成功 数据库查询的结果

  8. 动态库(.so)隐藏函数名

    一.偶遇 error: undefined reference to  xxx 问题 尝试封装通用的接口到一个private.so,然后供客户端使用,private.so编译出来后由sample.cp ...

  9. DML语句

    DML 操作是指对数据库中表记录的操作,主要包括表记录的插入(insert).更新(update).删除(delete)和查询(select),是开发人员日常使用最频繁的操作. 插入记录 表创建好后, ...

  10. Ubuntu14.04+安卓系统4.3+JDK6编译源码

    本博客主要参照: https://www.jianshu.com/p/ecb9c132030f https://blog.csdn.net/gobitan/article/details/243674 ...