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. BDD行为驱动简介及Pytest-bdd基础使用

    目录 BDD介绍 需求描述/用户场景 场景解析/实现 场景测试 Pytest-bdd的参数化 运行环境: pip insall pytest pytest-bdd pytest-selenium BD ...

  2. Apache反向代理解析二级目录/泛目录教程/apache反向代理/apache泛目录反向代理

    同nginx一样,apache解析目录不需要安装任何东西,在配置文件里加入解析规则即可.解析规则: <IfModule mod_proxy.c> ProxyPreserveHost On ...

  3. linux虚拟机安装centos6.x

    安装系统,作为每一个it技术控们的基本功,对于各位大神和技术大牛们应该是易如反掌或者是家常便饭啦,都是从无数次安装,重装,刷机中一步步走来的.那么今天,我也分享一套装机教程,共各位和我一样的小白参考或 ...

  4. gitlab使用指南

    gitlab是公司内部搭建的用于管理代码项目的类似于github的系统. 登录注册 注册时使用的名称和邮箱请按照公司内部格式进行信息填写. 在注册完成以后有可能会向邮箱里发送一个注册邮件,如果要求发送 ...

  5. Arcengine获得arcgis安装的版本

    ESRI.ArcGIS.RuntimeManager.ActiveRuntime.Version  //gsioracle MessageBox.Show(ESRI.ArcGIS.RuntimeMan ...

  6. css3_1

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. [go]包管理

    vendor方式 //包管理发展 go get(无版本概念) -> vendor(godep)(无版本概念) -> go modules go get github.com/tools/g ...

  8. HTTP 与 HTTPS协议

    HTTP 协议 通讯协议:服务器和客户端进行数据交互的形式 HTTP 工作原理:HTTP 协议工作于客户端-服务端架构为上.浏览器作为 HTTP 客户端通过 URL 向 HTTP 服务端即 Web 服 ...

  9. layui-简单的登录注册界面【转载】

    register.html 源代码: 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 ...

  10. java序列化与反序列化操作redis

     笔者在使用SSM框架项目部分功能进行测试需要使用到对象的序列化与反序列化 第一种方式:jackson Demo package com.dznfit.service; import com.dznf ...