Hdu4497 GCD and LCM 素数筛法+分解质因数
2016-07-13 13:05
387 查看
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
题意: 已知三个数的最小公倍数和最大公约数,求这样的三元组组数,
我们首先可以确定,三元组存在当且仅当 g 为 l 的约数。
在满足这个条件时,我们再去求解,这样能节省时间。
首先分解质因数,假设对于质因数x,三个数分别有ai,bi,ci个,LCM中有zi个,那么这三个数中至少有一个为zi,还至少有一个0,剩下的随便选。
我采用的分解质因数的方法是先用筛法,在进行分解,而素数筛选可以只进行到数范围的根号以内,
因为在这个范围外,至多只可能有一个素因子,那在循环后加一个判断即可解决。
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
题意: 已知三个数的最小公倍数和最大公约数,求这样的三元组组数,
我们首先可以确定,三元组存在当且仅当 g 为 l 的约数。
在满足这个条件时,我们再去求解,这样能节省时间。
首先分解质因数,假设对于质因数x,三个数分别有ai,bi,ci个,LCM中有zi个,那么这三个数中至少有一个为zi,还至少有一个0,剩下的随便选。
我采用的分解质因数的方法是先用筛法,在进行分解,而素数筛选可以只进行到数范围的根号以内,
因为在这个范围外,至多只可能有一个素因子,那在循环后加一个判断即可解决。
#include <iostream> #include <cstdio> #include <map> #include <cmath> #include <map> #include <vector> #include <algorithm> #include <cstring> #include <string> using namespace std; #define LL long long #define maxn 100001 int a[maxn]; LL prime[10000],c; int v[maxn]; void p() { LL i,j,n=maxn,m; c=0; m=(LL)sqrt(n+0.5); memset(v,0,sizeof(v)); for(i=2;i<=m;i++) if(!v[i]){ for(j=i*i;j<=n;j+=i) v[j]=1; } for(j=2;j<=n;j++){ if(!v[j]){ prime[c++]=j; } } } void ad(LL n) { for(int i=0;i<c&&n>1;i++){ while(n%prime[i]==0){ n/=prime[i]; a[i]++; } } if(n>1){ a[c++]=1; } } int main() { int t; LL g,l; p(); scanf("%d",&t); while(t--){ memset(a,0,sizeof(a)); scanf("%lld%lld",&g,&l); if(l%g!=0||g>l){ printf("0\n"); }else{ l/=g; ad(l); LL ans=1; for(int i=0;i<c;i++){ if(a[i]>0){ ans*=(6*a[i]); } } printf("%lld\n",ans); } } return 0; }
相关文章推荐
- Ambari服务依赖关系图生成脚本
- pagerank
- caffe BasePrefetchingDataLayer 学习
- 查询数据库每张表记录条数
- django crsf not set问题
- “暑假”学习开学典礼
- 关于苹果手机的真机调试
- UI开发学习指南
- PHP通用分页(Pager)类
- MVC学习 (一)
- 转载 asp.net中ViewState的用法详解
- VXLAN大二层实现穿越互联网通过DHCP服务器获取IP地址
- 2015ACM-ICPC 北京赛区 Problem I. Snake Carpet
- 阅读Logback文档笔记--Logback的Configuration配置
- 阅读Logback文档笔记--LogBack架构
- HDU 3068 最长回文 最长回文子串
- 1009. 说反话 (20)
- 图片 CSS3图片拖拽碰撞感应特效
- Redis之——CentOS 6.5安装配置redis
- 【抚琴煮酒】我们的网站压力究竟在哪里?