GCD and LCM

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

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
 

对G和L 分别进行合数分解。

很显然,如果G中出现了L中没有的素数,或者指数比L大的话,肯定答案就是0了、

对于素数p,如果L中的指数为num1,G中的指数为num2.

显然必须是num1 >= num2;

3个数p的指数必须在num1~num2之间,而且必须有一个为num1,一个为num2

容斥原理可以求得种数是:

(num1-num2+1)*(num1-num2+1)*(num1-num2+1) -

2*(num1-num2)*(num1-num2)*(num1-num2) +

 (num1-num2-1)*(num1-num2-1)*(num1-num2-1);

然后乘起来就是答案:

 /* ***********************************************
Author :kuangbin
Created Time :2013/8/24 12:48:31
File Name :F:\2013ACM练习\比赛练习\2013通化邀请赛\1005.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; const int MAXN = ;
int prime[MAXN+];
void getPrime()
{
memset(prime,,sizeof(prime));
for(int i = ;i <= MAXN;i++)
{
if(!prime[i])prime[++prime[]] = i;
for(int j = ;j <= prime[] && prime[j] <= MAXN/i;j++)
{
prime[prime[j]*i] = ;
if(i % prime[j] == )break;
}
}
}
long long factor[][];
int fatCnt;
int getFactors(long long x)
{
fatCnt = ;
long long tmp = x;
for(int i = ; prime[i] <= tmp/prime[i];i++)
{
factor[fatCnt][] = ;
if(tmp % prime[i] == )
{
factor[fatCnt][] = prime[i];
while(tmp % prime[i] == )
{
factor[fatCnt][] ++;
tmp /= prime[i];
}
fatCnt++;
}
}
if(tmp != )
{
factor[fatCnt][] = tmp;
factor[fatCnt++][] = ;
}
return fatCnt;
}
int a[],b[];
map<int,int>mp1,mp2;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
getPrime();
//for(int i = 1;i <= 20;i++)
//cout<<prime[i]<<endl;
int n,m;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
mp1.clear();
mp2.clear();
getFactors(m);
int cnt1 = fatCnt;
for(int i = ;i < cnt1;i++)
{
mp1[factor[i][]] = factor[i][];
a[i] = factor[i][];
//printf("%I64d %I64d\n",factor[i][0],factor[i][1]);
}
getFactors(n);
int cnt2 = fatCnt;
bool flag = true;
for(int i = ;i < cnt2;i++)
{
mp2[factor[i][]] = factor[i][];
if(mp1[factor[i][]] < factor[i][])
flag = false;
b[i] = factor[i][];
//printf("%I64d %I64d\n",factor[i][0],factor[i][1]);
}
if(!flag)
{
printf("0\n");
continue;
}
int ans = ;
for(int i = ;i < cnt1;i++)
{
int num1 = mp1[a[i]];
int num2 = mp2[a[i]];
if(num1 == num2)
ans *= ;
else
{
long long tmp = (num1-num2+)*(num1-num2+)*(num1-num2+);
tmp -= *(num1-num2)*(num1-num2)*(num1-num2);
tmp += (num1-num2-)*(num1-num2-)*(num1-num2-);
ans *= tmp;
}
}
printf("%d\n",ans);
}
return ;
}

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

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

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

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

  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 (非原创)

    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 (数论)

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

随机推荐

  1. How to tell your iPhone application that location services are required | The Agile Warrior

    div{padding-bottom:10px}.b_vPanel>div:last-child{padding:0}.banner a{color:#1020d0} --> Below ...

  2. apachebench对网站进行并发测试

    ,安装apache ,打开cmd进入apache安装目录的bin目录(有ab.exe) ,执行ab命令 格式:ab -n -c http://localhost:80/test/test.php 说明 ...

  3. InterSystems Ensemble学习笔记(一) Ensemble介绍及安装

    系列目录 InterSystems Ensemble学习笔记(一) Ensemble介绍及安装InterSystems Ensemble学习笔记(二) Ensemble创建镜像, 实现自动故障转移 一 ...

  4. GUC-6 Callable 接口

    import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.ut ...

  5. Web前端开发最佳实践系列文章汇总

    Web前端开发最佳实践(1):前端开发概述 Web前端开发最佳实践(2):前端代码重构 Web前端开发最佳实践(3):前端代码和资源的压缩与合并 Web前端开发最佳实践(4):在页面中添加必要的met ...

  6. MFC+WinPcap编写一个嗅探器之三(WinPcap)

    介绍程序模块前,这一节再复习一下WinPcap WinPcap开发一个嗅探器的主要步骤如下: (1)获取嗅探设备 WinPcap提供了pcap_findalldevs_ex() 函数来实现这个功能: ...

  7. 【转】TCP建立连接三次握手和释放连接四次握手

    在谈及TCP建立连接和释放连接过程,先来简单认识一下TCP报文段首部格式的的几个名词(这里只是简单说明,具体请查看相关教程) 序列号seq:占4个字节,用来标记数据段的顺序,TCP把连接中发送的所有数 ...

  8. CentOS 使用命令设置代理

    全局的代理设置, vim /etc/profile 添加下面内容 http_proxy = http://username:password@yourproxy:8080/ ftp_proxy = h ...

  9. QT STUDY

  10. 【原创】获取MySQL crash 时的core file

    最近有台服务器的MySQL经常crash,为了进一步定位问题,开启了mysql core file功能,开启步骤如下,供参考 [开启步骤] 1. my.cnf文件中增加2个配置选项 [mysqld] ...