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. iconfont-矢量图标字体

    二.矢量图标使用 1.进入:http://www.iconfont.cn/  搜索你图标的关键字,然后将需要的图标字体加入购物车 加入购物车之后,添加到项目 2.点击查看在线连接,生成css代码,把代 ...

  2. Linux入门(二)Shell基本命令

    上一篇讲了普通用户切换到root用户,今天补充一点,对于Debian和Ubuntu用户,安装时候只有一个普通用户注册,在需要root权限时,我们可以在普通用户模式下输入sudo这个命令运行某些相关特权 ...

  3. thinkphp模型实例化

    方法一 方法二

  4. Shell学习笔记:<<EOF子命令

    在shell编程中,“EOF”通常与“<<”结合使用,“<<EOF”表示后续的输入作为子命令或子shell的输入,直到遇到“EOF”,再次返回到主调用shell,可将其理解为分 ...

  5. 微商城三级分销源码公众号开发 微分销 C#源码

    需要源码,请加QQ:858-048-581 ,可以查看演示 运行环境:vs2012+ sql2008r2 [什么是微分销] 微分销是助力企业进军移动电商,完善分销体系搭建微信分销系统.基于微信平台,搭 ...

  6. AndroidStudio3.0到3.1遇到的坑

    原文:https://blog.csdn.net/qq_36676433/article/details/80361064 本以为3.0到3.1仅仅是界面的优化,万万没想到的是这个坑比起2.0到3.0 ...

  7. 2款适合HTML做音频和视频的插件

    Flowplayer-视频播放器 Flowplayer 是一个开源(GPL 3的)WEB视频播放器.您可以将该播放器嵌入您的网页中,如果您是开发人员,您还可以自由定制和配置播放器相关参数以达到您要的播 ...

  8. Bridge 桥接

    意图 将抽象部分与它的实现部分分离,使它们都可以独立地变化. 动机 当一个抽象可能有多个实现时,通常用继承来协调它们.抽象类定义对该抽象的接口,而具体的子类则用不同方式加以实现.但是此方法有时不够灵活 ...

  9. spring boot之使用通用Mapper批量添加数据

    通用Mapper是一款针对mybatis进行扩展的轻量级组件,使用起来非常方便.当调用其针对mysql进行批量添加数据的方法时,发现报错,仔细研究了一番,发现是在使用通用Mapper上出现了问题.下面 ...

  10. Failed to load JavaHL Library. SVN

    以前使用的电脑是32位的,安装的svn可以正常使用,但是现在的电脑室64位的,安装好svn后,把项目提交到svn的过程中,总是弹出来一个错误的对话框: Failed to load JavaHL Li ...