AtCoder Beginner Contest 142【D题】【判断素数的模板+求一个数的因子的模板】

D - Disjoint Set of Common Divisors

Problem Statement

Given are positive integers AA and BB.

Let us choose some number of positive common divisors of AA and BB.

Here, any two of the chosen divisors must be coprime.

At most, how many divisors can we choose?

Definition of common divisorDefinition of being coprimeDefinition of dividing

Constraints

  • All values in input are integers.
  • 1A,B10121≤A,B≤1012

Input

Input is given from Standard Input in the following format:

AA BB

Output

Print the maximum number of divisors that can be chosen to satisfy the condition.


Sample Input 1 Copy

Copy
12 18

Sample Output 1 Copy

Copy
3

1212 and 1818 have the following positive common divisors: 112233, and 66.

11 and 22 are coprime, 22 and 33 are coprime, and 33 and 11 are coprime, so we can choose 1122, and 33, which achieve the maximum result.


Sample Input 2 Copy

Copy
420 660

Sample Output 2 Copy

Copy
4

Sample Input 3 Copy

Copy
1 2019

Sample Output 3 Copy

Copy
1

11 and 20192019 have no positive common divisors other than 1

思路:找出有多少个公因子,并且公因子必须是素数。然后再加一。【比赛时思路一模一样,代码写挫了QAQ】

AC代码:

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define int long long
 6 bool isprime(int num){ // 判断是否是素数
 7     if(num==1){
 8         return false;
 9     }
10     if(num==2)
11         return true;
12     if(num%2==0)
13         return false;
14     double sqrtNum = sqrt(num);
15     for (int  i = 3; i <= sqrtNum; i += 2)
16     {
17         if (num % i == 0)
18         {
19             return false;
20         }
21     }
22     return true;
23 }
24 vector<int> v;
25 signed main(){
26     int n,m;
27     cin>>n>>m;
28     int temp=min(n,m);
29     for(int i=1;i*i<=temp;i++){ // 求一个数的因子的模板
30         if(temp%i==0){
31             v.push_back(i);
32             if(i*i!=temp){
33                 v.push_back(temp/i);
34             }
35         }
36     }
37     int ans=0;
38     int t=max(n,m);
39     for(int i=0;i<v.size();i++){
40         if(t%v[i]==0&&isprime(v[i]))
41             ans++;
42     }
43     cout<<ans+1;
44     return 0;
45 }

 

 

posted @ 2019-09-29 11:08  pengge666  阅读(412)  评论(0编辑  收藏  举报