GCD and LCM

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3409    Accepted Submission(s):
1503

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,求满足gcd(x,y,z)=g,lcm(x,y,z)=l的(x,y,z)组合的数量,(1, 2, 3)和(1, 3, 2)算作不同组合
分析:
g是因数,l是倍数,显然可以得到x%g=y%g=z%g=0=l%x=l%y=l%z;
进一步推出l%g=0;若不符合,无解。
 
根据唯一分解定理
将x,y,z分解
x=p1a1*p2a2……psas
y=p1b1*p2b2……psbs
z=p1c1*p2c2……pscs
g是三个数的最大公约数,则g满足g=p1min(a1,b1,c1)……psmin(as,bs,cs)=p1e1……pses 
l是三个数的最小公倍数,则l满足l=p1max(a1,b1,c1)……psmax(as,bs,cs)=p1h1……pshs
ei=min(ai,bi,ci) hi=max(ai,bi,bi)
x,y,z三个数分解后,对于每一个质因子,必然有一个指数是ei,一个指数是hi,先将这两个数固定下来
另一个数可以取ei到hi之间的数,包括ei和hi,设为ti
(1)当ti=ei或者ti=hi时,(ei,ei,hi)只有三种组合方式,(ei,hi,hi)也只有三种组合方式
(2)当ti≠ei并且ti≠hi时,(ei,ti,hi)有6种组合方式
(3)当ei=ti=hi时,只有1种组合方式
当ei≠hi时,则每个质因子全部的组合方式有6*(hi-ei)种,组合数用乘法计算结果,当hi=ei,不累乘0
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<cstring>
#define inf 0x3f3f3f3f
using namespace std;
#define ll long long
const int maxx=1e6+;
int prime[maxx];
int vis[maxx];
ll g,l;
int cnt;
void init()
{
memset(vis,true,sizeof(vis));
vis[]=vis[]=false;
cnt=;
for(int i=;i<=maxx;i++)
{
if(vis[i])
prime[cnt++]=i;
for(int j=;j<cnt && prime[j]*i<=maxx;j++)
{
vis[ i*prime[j] ]=false;
if(i%prime[j]==) break;
}
}
} int main()///hdu4497
{
init();
int t;
scanf("%d",&t);
while(t--)
{
ll ans=;
scanf("%lld%lld",&g,&l);
if(l%g)
printf("0\n");
else
{
for(int i=;i<cnt;i++)
{
int e=,h=;
while(g%prime[i]==)
{
e++;
g=g/prime[i];
}
while(l%prime[i]==)
{
h++;
l=l/prime[i];
}
if(h-e)
{
ans=ans*(h-e)*;
}
}
if(g==l)///32位范围内大素数因子只能有一个,l能被g整除证明这个大素数相同,不累乘
;
else if(l>)///如果g=1,l=大素数,再乘一次6
ans=ans*;
printf("%lld\n",ans);
}
}
return ;
}
 

hdu4497-GCD and LCM-(欧拉筛+唯一分解定理+组合数)的更多相关文章

  1. hdu2421-Deciphering Password-(欧拉筛+唯一分解定理+积性函数+立方求和公式)

    Deciphering Password Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. 2018南京icpc-J-Prime Game (欧拉筛+唯一分解定理)

    题意:给定n个数ai(n<=1e6,ai<=1e6),定义,并且fac(l,r)为mul(l,r)的不同质因数的个数,求 思路:可以先用欧拉筛求出1e6以内的所有质数,然后对所有ai判断, ...

  3. hdu3826-Squarefree number-(欧拉筛+唯一分解定理)

    Squarefree number Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. noip复习——线性筛(欧拉筛)

    整数的唯一分解定理: \(\forall A\in \mathbb {N} ,\,A>1\quad \exists \prod\limits _{i=1}^{s}p_{i}^{a_{i}}=A\ ...

  5. GCD nyoj 1007 (欧拉函数+欧几里得)

    GCD  nyoj 1007 (欧拉函数+欧几里得) GCD 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 The greatest common divisor ...

  6. 欧拉筛,线性筛,洛谷P2158仪仗队

    题目 首先我们先把题目分析一下. emmmm,这应该是一个找规律,应该可以打表,然后我们再分析一下图片,发现如果这个点可以被看到,那它的横坐标和纵坐标应该互质,而互质的条件就是它的横坐标和纵坐标的最大 ...

  7. 【BZOJ 2190】【SDOI 2008】仪仗队 欧拉筛

    欧拉筛模板题 #include<cstdio> using namespace std; const int N=40003; int num=0,prime[N],phi[N]; boo ...

  8. [51NOD1181]质数中的质数(质数筛法)(欧拉筛)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1181 思路:欧拉筛出所有素数和一个数的判定,找到大于n的最小质 ...

  9. 素数筛&&欧拉筛

    折腾了一晚上很水的数论,整个人都萌萌哒 主要看了欧拉筛和素数筛的O(n)的算法 这个比那个一长串英文名的算法的优势在于没有多次计算一个数,也就是说一个数只筛了一次,主要是在%==0之后跳出实现的,具体 ...

随机推荐

  1. asp.net网站中增删文件夹会导致Session或cache等等丢失

    因为这会导致网站资源本身重新加载. 如果要改变文件和文件夹,一般应该是对 app_data 下进行操作.

  2. MySQL中实现DROP USER if EXISTS `test`,即创建新用户时检测用户是否存在

    MySQL中实现DROP USER if EXISTS `test`,即创建新用户时检测用户是否存在    版权声明:本文为博主原创文章,欢迎大家转载,注明出处即可.有问题可留言,会尽快回复,欢迎探讨 ...

  3. JQ获取地址栏参数

    //获取地址栏参数 function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + " ...

  4. redis的5种类型和所用命令

    数据操作 redis是key-value的数据,所以每个数据都是一个键值对 键的类型是字符串 值的类型分为五种: 字符串string 哈希hash 列表list 集合set 有序集合zset 数据操作 ...

  5. jl1.如何设置元素的宽高包含元素的边框和内边距

    方法一: 文档地址:http://www.w3school.com.cn/cssref/pr_box-sizing.asp CSS3 box-sizing属性:    box-sizing: bord ...

  6. 坑爹的myeclipse 的tomcat 重部署 redeploy !

    启动 tomcat 出现: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bea ...

  7. PostgreSQL pg_dump&psql 数据的备份与恢复

    Usage:   pg_dump [OPTION]... [DBNAME] 数据库名放最后,不指定默认是系统变量PGDATABASE指定的数据库. General options:(一般选项)   - ...

  8. mezzanine的page_menu tag

    mezzanine的head 导航条.左侧tree.footer是由page_menu产生的.page_menu的算法,先计算出每一页的孩子,然后再逐页去page_menu. @register.re ...

  9. Vue.js——基于$.ajax实现数据的跨域增删查改

    转自:https://www.cnblogs.com/keepfool/p/5648674.html 概述 之前我们学习了Vue.js的一些基础知识,以及如何开发一个组件,然而那些示例的数据都是loc ...

  10. 机器学习进阶-图像基本处理-视频的读取与处理 1.cv2.VideoCapture(视频的载入) 2.vc.isOpened(载入的视频是否可以打开) 3.vc.read(视频中一张图片的读取) 4.cv2.cvtColor(将图片转换为灰度图)

    1.vc = cv2.VideoCapture('test.mp4') #进行视频的载入 2.vc.isOpened() # 判断载入的视频是否可以打开 3.ret, frame = vc.read( ...