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. mysql命令gruop by报错this is incompatible with sql_mode=only_full_group_by

    在mysql 工具 搜索或者插入数据时报下面错误: ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause ...

  2. Nginx - 压缩模块

    1. 前言 在 Nginx 中与网页压缩相关的模块有两个:一个是 HttpGzipModule,另一个是 HttpGzipStaticModule.前者用于启用在文件传输过程中使用 gzip 压缩,而 ...

  3. SQL Case when 的使用方法 (转)

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ELSE '其他' END ...

  4. Windows平台的rop exp编写

    摘抄自看雪 Windows的ROP与Linux的ROP并不相同,其实Linux下的应该叫做是ret2libc等等.Windows的ROP有明确的执行目标,比如开辟可执行内存然后拷贝shellcode, ...

  5. Elasticsearch的相关知识

    Elasticsearch的备份和恢复 http://keenwon.com/1393.html ETL kettle 数据转成json 发送POST请求 http://blog.csdn.net/a ...

  6. SQL中的注释语句

    SQL中的注释分为单行注释和多行注释.顾名思义,单行注释就是对一行进行注释,多行注释就是同时对多行进行注释. 一.单行注释 SQL语句中的单行注释使用 -- create database datab ...

  7. 《HBase实战》学习笔记

    第二章  入门 HBase写路径: 增加新行和修改已有的行,内部机制是一样的. 写入的时候,会写到预写日志(WAL)和MemStore中. MenmStore是内存里的写入缓冲区.填满后,会将数据刷写 ...

  8. django使用RestFramework的Token认证

    今天实现的想法有点不正规: Django Rest framework的框架的认证,API都运行良好. 现在是要自己写一个function来实现用户的功能. 而不是用Rest 框架里的APIVIEW这 ...

  9. spring-boot分环境打包为war包

    1.启动类修改 @EnableSwagger2 @SpringBootApplication public class CustWebAcApplication extends SpringBootS ...

  10. 从字节码角度分析Byte类型变量b++和++b

    1. 下面是一到Java笔试题: public class Test2 { public void add(Byte b) { b = b++; } public void test() { Byte ...