Hdu4497 GCD and LCM 素数筛法+分解质因数
来源:互联网 发布:家具拆单下料软件 编辑:程序博客网 时间:2024/05/22 10:25
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,剩下的随便选。
我采用的分解质因数的方法是先用筛法,在进行分解,而素数筛选可以只进行到数范围的根号以内,
因为在这个范围外,至多只可能有一个素因子,那在循环后加一个判断即可解决。
#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 100001int 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;}
- Hdu4497 GCD and LCM 素数筛法+分解质因数
- HDU4497 GCD and LCM 数论 素数分解
- hdu4497 GCD and LCM
- hdu4497 GCD and LCM
- hdu4497 lcm and gcd
- HDU4497 GCD and LCM
- HDU 4497GCD and LCM(素数分解)
- LightOJ1236 Pairs Forming LCM 素数筛法+分解质因数
- HDU4497:GCD and LCM(数论)
- hdu4497 GCD and LCM 容斥原理
- GCD and LCM 质因数分解与容斥原理
- HDU 4497 GCD and LCM (分解质因数)
- HDOJ 4497 GCD and LCM(质因数分解)
- HDU 4497 GCD and LCM (分解质因数)
- POJ 2429 GCD & LCM Inverse(Pollard_rho法质因数分解)
- hdu 4497 GCD and LCM (素数分解+组合数学)
- hdu 4497 GCD and LCM 数论 素数分解
- hdoj 4497(gcd&lcm分解质因数)
- form表单提交数据编码方式和tomcat接受数据解码方式的思考
- 腾讯Bugly,简单实用的崩溃日志收集
- openstack安装过程中软件包缺失
- Android开发中图片的三级缓存
- 图片水印,图片裁剪和屏幕截图
- Hdu4497 GCD and LCM 素数筛法+分解质因数
- 数据分析系统Hive
- QT工程构建一般环境
- 表单填写 大气的全屏形式概念表单(精)
- centos7 安装 mariadb(mysql的一个分支) 的正确命令
- Git9--从远程库克隆
- Simulator 慢动作问题
- SpringMVC访问静态资源的三种方式
- poj 1276 Cash Machine 多重背包