题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497

GCD and LCM

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

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
题意:给出三个数的最大公约数和最小公倍数,让求abc这三个数可能的情况,注意ABC的位置不同算不同的情况
考虑先分解最小公倍数。合数分解后,再分解最大公约数,可知,如果最大公约数中有最小公倍数中没有的质因数因子的话,那么答案肯定为0
然后考虑每一个因子pi有设合数分解最小公倍数的个数为bi合数分解最大公约数的个数为bi
下面有两种考虑方法
  排列组合:易得三个数中的对于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的时候可以发现正确结果是对于这一位应该是只用一种情况,就是三个数都相等,所以要特判一下。。。。后来又因为这个wa了一次~~~~(>_<)~~~~
  容斥定理:同样是考虑每个因子,有所有的情况是每个位置都可以取(bi-ai+1)种情况即(bi-ai+1)^3,要减去没有bi个因子的情况和没有ai个因子的情况即2*(bi-ai)^3
然后发现减多了,要加上同时没有因子ai和bi的情况即(bi-ai-1)^3
这里同样要注意上面的注意。
 //合数分解
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn = ;
int prime[maxn];
bool pri[maxn];
int cnt;
void init()
{
cnt = ;
pri[] = pri[] = ;
//prime[0] = 2;
for(int i = ; i < maxn; i++){
if(!pri[i]){ prime[cnt++] = i;
for(int j = i+i; j < maxn; j+=i)
{
pri[j]=;
}
}
}
return;
}
ll m1[],m2[];
ll c1[],c2[];
int main()
{
init();
int T;
ll G,L;
scanf("%d",&T);
while(T--)
{
memset(m1,,sizeof(m1));
memset(c1,,sizeof(c1));
memset(m2,,sizeof(m2));
memset(c2,,sizeof(c2));
scanf("%lld%lld",&G,&L);
int tm = ;
bool in = ;
bool fl = ;
//printf("%d\n",prime[1]);
if(L%G) fl = ;
for(int i = ; i< cnt; i++){
while(prime[i]<=L&&L%prime[i]==){
L = L/prime[i];
m2[tm] = prime[i];
c2[tm]++;
in = ;
}
if(in) tm++;
in = ;
}
if(L!=){
m2[tm] = L;
c2[tm] = ;
tm++;
}
for(int i = ; i < tm; i++){
while(m2[i]<=G&&G%m2[i]==){
G = G/m2[i];
m1[i] = m2[i];
c1[i]++;
}
in = ;
}
if(G!=){
fl = ;
}
/*puts("haha");
for(int i = 0; i < tm; i++){
printf("m1[%d]=%d; m2[%d]=%d;\n",i,m1[i],i,m2[i]);
printf("c1[%d]=%d; c2[%d]=%d;\n",i,c1[i],i,c2[i]);
}
*/
if(fl==) {puts(""); continue;}
ll ans = ;
for(int i = ; i < tm; i++){
//需要特判当c1[i] =c2[i]的情况
ll E;
if(c1[i]==c2[i]) E = ;
else E = c2[i]-c1[i]-;
/*
也可以用排列组合的想法,每次找到两个数包含首尾的两个值,然后中间的一个在从所有可能中选取一个。
这样最后的公式就是A(3,2)*(c2[i]-c1[i]-1);
所以
if(c2[i]>c1[i]) ans = ans*6*(c2[i]-c1[i]-1);
*/
ans = ans*((c2[i]-c1[i]+)*(c2[i]-c1[i]+)*(c2[i]-c1[i]+)-*(c2[i]-c1[i])*(c2[i]-c1[i])*(c2[i]-c1[i])+E*E*E);
}
printf("%lld\n",ans);
}
return ;
}

hdu_4497GCD and LCM(合数分解)的更多相关文章

  1. LightOJ 1236 Pairs Forming LCM 合数分解

    题意:求所有小于等于n的,x,y&&lcm(x,y)==n的个数 分析:因为n是最小公倍数,所以x,y都是n的因子,而且满足这样的因子必须保证互质,由于n=1e14,所以最多大概在2^ ...

  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 4610 Cards (合数分解,枚举)

    Cards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  4. hdu 5317 合数分解+预处理

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  5. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. Perfect Pth Powers pku-1730(筛+合数分解)

    题意:x可以表示为bp, 求这个p的最大值,比如 25=52, 64=26,  然后输入x 输出 p 就是一个质因子分解.算法.(表示数据上卡了2个小时.) 合数质因子分解模板. ]; ]; ; ;n ...

  7. pku1365 Prime Land (数论,合数分解模板)

    题意:给你一个个数对a, b 表示ab这样的每个数相乘的一个数n,求n-1的质数因子并且每个指数因子k所对应的次数 h. 先把合数分解模板乖乖放上: ; ans != ; ++i) { ) { num ...

  8. UVA 10791 Minimum Sum LCM(分解质因数)

    最大公倍数的最小和 题意: 给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 那么找出一个序列,使他们的和最小. 分析: 一系列数字a1,a2,a3 ...

  9. hdu4497 GCD and LCM ——素数分解+计数

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4497 如果G%L != 0,说明一定无解. 把K = G / L质数分解,G / L = p1^t1 ...

随机推荐

  1. js功能代码大全

    1.日期格式化 //化为2017-08-14 function formatDate (date) { var y = date.getFullYear(); var m = date.getMont ...

  2. Locust no-web 模式与参数详解

    读前参考:<性能测试工具Locust > 熟悉 Apache ab 工具的同学都知道,它是没有界面的,通过命令行执行. Locust 同样也提供的命令行运行,好处就是更节省客户端资源. 命 ...

  3. readAsDataURL(file) & readAsText(file, encoding)

      readAsDataURL(file)会把文件内容转换为data类型的URL: data:text/plain;base64,b3JkZXItaWQJb3JkZXItaXRlbS1p... 这种d ...

  4. win10下部署.Net Web项目到IIS10

    本问主要介绍如何将.Net Web项目部署到IIS10下面. 1.确保iis功能已开启 开启步骤如下:控制面板->程序 点击确定,ok,iis功能已开启. 2.打开iis,绑定站点到iis下面 ...

  5. A session had already been started – ignoring session_start() 怎么办?

    php警告提示A session had already been started – ignoring session_start() 解决方案 访问log日志发现有个这样的警告 主要是在TP框架中 ...

  6. php require、require_once和include、include_once的区别

    一.引入php文件路径的方法require '文件路径'; require ('文件路径');require_once '文件路径'; require_once ('文件路径');include 同 ...

  7. CS:APP3e 深入理解计算机系统_3e MallocLab实验

    详细的题目要求和资源可以到 http://csapp.cs.cmu.edu/3e/labs.html 或者 http://www.cs.cmu.edu/~./213/schedule.html 获取. ...

  8. ES6 Generators的异步应用

    ES6 Generators系列: ES6 Generators基本概念 深入研究ES6 Generators ES6 Generators的异步应用 ES6 Generators并发 通过前面两篇文 ...

  9. Tomcat 快速入门

    Tomcat 快速入门 版本说明 本文使用 Tomcat 版本为 Tomcat 8.5.24. Tomcat 8.5 要求 JDK 版本为 1.7 以上. 简介 Tomcat 是什么 Tomcat 是 ...

  10. python3基础(七)函数基础

    Function 函数是一段组织好的能够实现特定功能或者逻辑的代码块,函数代码在文件执行时读入内存并不执行,在调用函数时执行,简单来说是把一段代码封装给一个函数名(可以用变量的概念去理解,即把一段代码 ...