AtCoder Beginner Contest 142D题判断素数的模板+求一个数的因子的模板
Posted pengge666
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AtCoder Beginner Contest 142D题判断素数的模板+求一个数的因子的模板相关的知识,希望对你有一定的参考价值。
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 dividingConstraints
- All values in input are integers.
- 1≤A,B≤10121≤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
12 18
Sample Output 1 Copy
3
1212 and 1818 have the following positive common divisors: 11, 22, 33, and 66.
11 and 22 are coprime, 22 and 33 are coprime, and 33 and 11 are coprime, so we can choose 11, 22, and 33, which achieve the maximum result.
Sample Input 2 Copy
420 660
Sample Output 2 Copy
4
Sample Input 3 Copy
1 2019
Sample Output 3 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 }
以上是关于AtCoder Beginner Contest 142D题判断素数的模板+求一个数的因子的模板的主要内容,如果未能解决你的问题,请参考以下文章
AtCoder Beginner Contest 115 题解