GCD and LCM
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2024 Accepted Submission(s): 904
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
Sample Output
题解:首先我们要知道GCD和LCM的性质:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAm8AAACPCAIAAAAjj6t0AAAReElEQVR4nO2dO7abShBFmZETR8zFkddiLg68lobixIzkRdZgeIEk1J/qbyEouHtHtgRN6VTD6a5u6Q4LAAAA6BiODgAAAOD04KYAAABacFMAAAAtuCkAAIAW3BQAAEALbgoAAKAFNwUAANCCmwIAAGjBTQEAALTgpgAAAFpwUwAAAC24KQAAgBbcFAAAQAtuCgAAoAU3BQAA0IKbAgAAaMFNAQAAtOCmAAAAWnDTL8s8DcMwTHPreffbOAzDMIy3+yfCOkMgSNcN0lnm9Nl5cFQ4uOmXpevOmSc798yDx53T/gDQgHTdIJ1lLpKdB/vnCDeFeu630eB9YzOqAJtB2owqwGaQNqPaH8s67B0bbgr1zJPJAbnRsDyMxmg0LA+jMRoNa29My7BzcLjpBXjUZx6jsNeSwTome73p9yr3lFwjfle830bvFee4x8vvF+q78DvA11lOs1UDy3nqHoEiHdLtL90OnDs7yxYJWnbPEW56DR5rF+P47DuPjjeOo98Tve4cL3cEjSSOCHpn0HLXaPBx57gNt6x5hPdzI0iHdHKrWZTS7cC5s7NoE7TsnyPc9BqE3ex567w7YnwXxIsKUV+NThK7p3PaPPX13iiWloaUA1CkQzr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lSqWHbl8Bha6rSVPNVBWhE8w0Z+XnufhVc6abpjKfDyW0Jl22zMLY+vttJU2chqvB7VeBQpWHwgonN3IZAw82Jvy1Y80pVm6XHRd8Pr0COtr/IJvuQ757uMWIJNZ3t2vmX8M48Pb5e5bURXmn1m7ivpu/3w58HcdSx/68zUu4RkRoN4+4bfHv6i9OnIR0yxwfctC5NJOYT1LhptvjllXXS9Z7nO/UvupeWn2deWNMsTM6kuel4u4e/BJN9XB5upt4PSiWSEBzMPRLSpKF/FmI+6dKQAUmWD7hpbZrczh09SqGL2krvUWjuxi1qtGbMtDICDECiL4to6dKh4f02IuDOVKfJ792FvSNQhXU3VVii8GtJ+118MxpDwAEEetLIpMqnSUO32oSEe1KfJu9Jwd7FTTDvpr2Wdr/l/wRJDRasqXFKYCFkczRPq7DSiL7pPd+e2Zn6NL2fFFjpVpzATZdj6q3H13jfUeCmGho1xEoFWvvhioHyzheiIU3eWin52YRzuOmy8yjXwM8fPWgumeGmEU0aYqUi9RrO4d8g32DBBSpp6errk4Il0804jZsuy/LsLZ++M89c+KCypiLeNnN//iViqCQYztmo8ECMsKeXNCk5l5tCCvHXmLg7mkj8ohUqthF8AY3BiEmCCnziC//QBG4KAPCVcMeNL0OVftoG2sBNAQAAtOCmAAAAWnBTAAAALbgpAACAFtwUAABAC24KAACgBTcFAADQgpsCAABowU0BAAC04KYAAABacFOAT/Dnh/Sjvw7ffv13dIwAsB24KcAn+PNj+PFn/ef67/fbW7jpv9/fh59/NQcAwEbgpgCf4OWmwhz126//tnHTvz+HIWeWf38O+QMAYDNwU4BP8OfH8OPXr28P/3zNTP/79e1Z4d3ATf/+HL5/T009//3+PgzD958/mZsC7ANuCvAJnErve4L6dlC1m/77/f3n33Qh99/fv/8WKr0A+/E/ZpodNwf9UTQAAAAASUVORK5CYII=" alt="" width="471" height="108" />
那么对gcd(a,b,c) GCD每个素因子上面也就是 min(xi,yi,zi) LCM 每个素因子上面就是 max(xi,yi,zi),我们先将LCM分解,然后用其每个素因子对GCD进行分解,如果分解之后GCD不为1,那么就肯定没有这样的组合。然后对每个素因子进行询问,假设当前的素因子是 p ,它的GCD指数是G,LCM的指数是L,如果G>L,那么肯定就不存在这样的组合了,如果G<L,那么第二大的数肯定就在 [L,G] 的区间内,当第二个数字位于(L,G)时,这三个数字有6种组合,当第二个数字等于L或者G时,三个数字有三种组合,所以每个因子的数的组合是 6*(L-G-1)+6=6*(L-G)种,如果L==G那么组合就是唯一的。最终的结果就是每个因子对应的组合数相乘。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = ;
int p[N],e[N];
int p1[N],e1[N];
int main()
{
int tcase;
scanf("%d",&tcase);
int a,b;
while(tcase--){
scanf("%d%d",&a,&b);
int id=,id1=;
memset(e,,sizeof(e));
memset(e1,,sizeof(e1));
for(int i=;i*i<=b;i++){
while(b%i==){
p1[id1]=i;
while(b%i==) {b/=i;e1[id1]++;}
id1++;
}
}
if(b>) {p1[id1]=b;e1[id1++]=;}
for(int i=;i<id1;i++){
if(a%p1[i]==){
p[id] = p1[i];
while(a%p1[i]==){
a/=p1[i];
e[id]++;
}
id++;
}
else{
p[id]=p[i];
e[id++] = ;
}
}
if(a!=){
printf("0\n");
continue;
}
int sum = ;
bool flag = false;
for(int i=;i<id1;i++){
if(e[i]<e1[i]){
sum*=*(e1[i]-e[i]);
}
if(e[i]>e1[i]) {
flag = true;
break;
}
}
if(flag) {
sum = ;
}
printf("%d\n",sum);
}
return ;
}
- HDU 1521 排列组合 指数型母函数
排列组合 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status D ...
- Hdu 1521 排列组合
a1 n1 a2 n2 ... ak nkn=n1+n2+...+nk从n个数中选r个排列(不是组合噢)// 指数型母函数// 模板#include <iostream> #include ...
- HDU 1521 排列组合 (母函数)
题目链接 Problem Description 有n种物品,并且知道每种物品的数量.要求从中选出m件物品的排列数.例如有两种物品A,B,并且数量都是1,从中选2件物品,则排列有"AB&qu ...
- hdu 4535(排列组合之错排公式)
吉哥系列故事——礼尚往来 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- hdu 1521 排列组合 —— 指数型生成函数
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1521 标准的指数型生成函数: WA了好几遍,原来是多组数据啊囧: 注意精度,直接强制转换(int)是舍去小 ...
- hdu 4705 排列组合
思路:枚举能是A,B,C在一条简单路径上的中点. 计算多少个几何能满足.在用总数减去 #pragma comment(linker, "/STACK:16777216") #inc ...
- hdu 1521 排列组合【指数型生成函数】
根据套路列出式子:\( \prod_{i=1}^{n}\sum_{j=0}^{c[i]}\frac{x^j}{j!} \),然后暴力展开即可 #include<iostream> #inc ...
- 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,求满 ...
- 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 ...
随机推荐
- JAVA解析XML有哪几种方法?并简述各自的优缺点
DOM: 是用与平台和语言无关的方式表示XML文档的官方W3C标准,分析该结构通常需要加载整个文档和构造层次结构,然后才能做任何工作.是基于信息层次的 优点有:由于树在内存中是持久的,因此可以修改它以 ...
- 闯越自动签到demo版补充说明
demo代码:https://www.cnblogs.com/canmeng/p/11000548.html 定位出错是由于cookie 我重新登录账号过,cookies的值就变了 当时没注意cook ...
- 【104】Maven3.5.0结合eclipse使用,提示Lambda expressions are allowed only at source level 1.8 or above错误的解决方法
错误重现 我的机器上安装了 maven 3.5.0,在 eclipse 中创建 maven 项目.pom.xml配置如下: <project xmlns="http://maven.a ...
- oracle insert用法总结
总结下Oracle 中的Insert用法 1.标准Insert --单表单行插入 语法: INSERT INTO table [(column1,column2,...)] VALUE ...
- A1095 Cars on Campus (30)(30 分)
A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...
- 2017 United Kingdom and Ireland Programming(Gym - 101606)
题目很水.睡过了迟到了一个小时,到达战场一看,俩队友AC五个了.. 就只贴我补的几个吧. B - Breaking Biscuits Gym - 101606B 旋转卡壳模板题.然后敲错了. 代码是另 ...
- iview框架 两侧弹框 出现第二层弹框 一闪而过的问题
分析原因:寡人怀疑可能是,两层弹出框 采用的是一个开关值,发生了覆盖 解决方式 是在第二层弹框外套层计时器 源代码如下: 修改后为:
- TCP/IP网络编程之套接字类型与协议设置
套接字与协议 如果相隔很远的两人要进行通话,必须先决定对话方式.如果一方使用电话,另一方也必须使用电话,而不是书信.可以说,电话就是两人对话的协议.协议是对话中使用的通信规则,扩展到计算机领域可整理为 ...
- alert(1) to win部分解题
XSS在线习题分析 (https://alf.nu/alert1) 1. Warmup function escape(s) { return '<script>console.log(& ...
- 几种常见的Android自动化测试框架及其应用
随着Android应用得越来越广,越来越多的公司推出了自己移动应用测试平台.例如,百度的MTC.东软易测云.Testin云测试平台…….由于自己所在项目组就是做终端测试工具的,故抽空了解了下几种常见的 ...