Disjoint Set of Common Divisors(质因子分解)

问题 E: Disjoint Set of Common Divisors
时间限制: 1 Sec 内存限制: 128 MB

题目描述
Given are positive integers A and B.
Let us choose some number of positive common divisors of A and B.
Here, any two of the chosen divisors must be coprime.
At most, how many divisors can we choose?
→Definition of common divisor
·An integer d is said to be a common divisor of integers x and y when d divides both x and y.
→Definition of being coprime
·Integers x and y are said to be coprime when x and y have no positive common divisors other than 1.
→Definition of dividing
·An integer x is said to divide another integer y when there exists an integer α such that y=αx.

Constraints
·All values in input are integers.
·1≤A,B≤1012
输入
Input is given from Standard Input in the following format:

A B

输出
Print the maximum number of divisors that can be chosen to satisfy the condition.
样例输入 Copy
【样例1】
12 18
【样例2】
420 660
【样例3】
1 2019
样例输出 Copy
【样例1】
3
【样例2】
4
【样例3】
1
提示
样例1解释
12 and 18 have the following positive common divisors: 1, 2, 3, and 6.
1 and 2 are coprime, 2 and 3 are coprime, and 3 and 1 are coprime, so we can choose 1, 2, and 3, which achieve the maximum result.
样例3解释
1 and 2019 have no positive common divisors other than 1.

思路:

\(gcd(x,y)\)质因子分解,答案为质因子个数\(+1\)

代码:

ll divide(ll x){
    ll res=0;
    for(ll i=2;i<=x/i;i++)
        if(x%i==0){
            ll s=0;
            while(x%i==0) x/=i,s++;
            res++;
        }
    if(x>1) res++;
    return res;
}


int main()
{
    ll x=read,y=read;
    ll t=__gcd(x,y);
    cout<<divide(t)+1<<endl;
    return 0;
}
posted @ 2021-05-29 10:40  OvO1  阅读(119)  评论(0编辑  收藏  举报