hdu 4497 GCD and LCM(唯一分解+容斥原理)

GCD and LCM

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 78 Accepted Submission(s): 43

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

2013
ACM-ICPC吉林通化全国邀请赛——题目重现

题意:

求同时满足gcd(x,y,z)==g&&lcm(x,y,z)==L的(x,y,z)组合数.

题解:

1.很直观的:若l%g!=0||g>l时,答案为0;

2.若果满足l%g==0,用唯一分解定理把个g,l分解,用map保存g,l相同因子个数的差

3.对于x,y,z,其对应的质因子pi应该属于[0,s],s为map[pi],pi的个数,而且x,y,z中必须有一个为0,一个为map[pi],这是为了保证lcm=L,gcd=g;

每个pi对应的种数累乘就是答案了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos(-1.0);
const double E=2.718281828;
typedef long long ll;

const int INF=1000010;

using namespace std;

map<int,int>mp;

void Fenjie(int n,int d)
{
    for(int i=2; i*i<=n; i++)
    {
        if(n%i==0)
        {
            while(n%i==0)
            {
                mp[i]+=d;
                n/=i;
            }
        }
        if(n==1)
            break;
    }
    if(n>1)
        mp[n]+=d;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    while(cin>>n)
    {
        while(n--)
        {
        	mp.clear();
            int g,l;
            cin>>g>>l;
            if(g>l||l%g)
            {
                printf("0\n");
                continue;
            }
            Fenjie(l,1);
            Fenjie(g,-1);
            ll ans=1;
            map<int,int>::iterator it;
            for(it=mp.begin();it!=mp.end();it++)
		{
			if(it->second)
			{
				int x=it->second;
				//ans*=((x+1)*(x+1)*(x+1)-2*x*x*x+(x-1)*(x-1)*(x-1));//容斥原理
				ans*=6*x;//组合原理,c(3,1)*c(2,1)*(x-1+1)
			}
		}
		cout<<ans<<endl;
        }
    }
    return 0;
}

My code:

				
时间: 2024-10-17 18:07:52

hdu 4497 GCD and LCM(唯一分解+容斥原理)的相关文章

hdu 4497 GCD and LCM(排列组合)

题目:hdu 4497 GCD and LCM 题目大意:给出三个数的最大公约数,和最小公倍数,问这三个数的排列组合关系. 解题思路:最小公倍数/最大公约数 ==  三个数不同部分的乘积.这样来考虑的话,三个数都要有最大公约数的部分,其余的部分就是由LCM / GCD 里面的因子构成.这里面的因子可能会有 2 2 3 这样的情况, 不同的因子之间是不会相互干扰的,但是相同的会出现问题,因为,不能同时将相同的因子都放在三个位置上,这样最大公约数就的要乘上这个因子.然后对于单种因子来考虑的话,每种因

hdu 4497 GCD and LCM 数论 素数分解

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 1339    Accepted Submission(s): 607 Problem Description Given two positive integers G and L, could you tell me how many solutions of

hdu 4497 GCD and LCM 数学

GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4497 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

hdu 4497 GCD and LCM

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1092    Accepted Submission(s): 512 Problem Description Given two positive integers G and L, could you tell me how many solutions of (

hdu 4497 GCD and LCM 质因素分解+排列组合or容斥原理

//昨天把一个i写成1了 然后挂了一下午 首先进行质因数分解g=a1^b1+a2^b2...... l=a1^b1'+a2^b2'.......,然后判断两种不可行情况:1,g的分解式中有l的分解式中没有的质因子 2,存在bi>bi',然后剩下的都是可行解,对于每一个质因子三个数中有两个分别bi,bi',第三个的取值可为[bi,bi'],所以对于每一个质因子共有6(bi-bi')种取法(A(2,3)*(b-a+1)+C(2,3)*2分别为取得值在和不在边界上的情况,特殊:如果bi=bi'就只有一

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,求满足要求的(x, y, z)有多少组,并且要考虑顺序. 思路:如果L%G != 0显然不存在这样的(x, y, z),相反肯定存在.具体做法就是将L/G分解质因子,得到:L/G = P1^t1 * P2^t2 * ... * Pk^tk,我们来考虑任意一个因子Pi^ti,此时(x/G, y/G, z/

HDU 4497 GCD and LCM (分解质因数)

链接 : ?? http://acm.hdu.edu.cn/showproblem.php?pid=4497 假设G不是L的约数 就不可能找到三个数. L的全部素因子一定包括G的全部素因子 而且次方数一定大于等于G的.仅仅须要三个数 对于每个素因子的次方数 三个的最小值是G的,最大值是L的.考虑三个相应的次方数都不一样.那么当中两个是确定的 一个是G的一个是L的 剩下的一个在G和L的之间. 算上排列 总共同拥有6种.或者当中两个是一样的,那么也有6种情况. 最后能够合并计算. //#pragma

Hdu 4497 GCD and LCM(数论)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4497 思路:x%G==0,y%G==0,z%G==0,所以L%G==0,若L%G!=0则一定无解. 考虑 L/G=(p1^t1)*(p2^t2)*......*(pn^tn) x'=x/G=(p1^a1)*(p2^a2)*......*(pn^an) y'=y/G=(p1^b1)*(p2^b2)*......*(pn^bn) z'=z/G=(p1^c1)*(p2^c2)*.......*(pn^cn

HDU 4497 GCD and LCM (数学,质数分解)

题意:给定G,L,分别是三个数最大公因数和最小公倍数,问你能找出多少对. 析:数学题,当时就想错了,就没找出规律,思路是这样的. 首先G和L有公因数,就是G,所以就可以用L除以G,然后只要找从1-(n=L/G),即可,那么可以进行质因数分解,假设: n = p1^t1*p2^t2*p3^t3;那么x, y, z,除以G后一定是这样的. x = p1^i1*p2^i2*p3^i3; y = p1^j1*p2^j2*p3^j3; z = p1^k1*p2^k2*p3^k3; 那么我们可以知道,i1,