zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    LeetCode#1047-Remove All Adjacent Duplicates In String-删除字符串中的所有相邻重复项
    LeetCode#345-Reverse Vowels of a String-反转字符串中的元音字母
    LeetCode#344-Reverse String-反转字符串
    LeetCode#232-Implement Queue using Stacks-用栈实现队列
    LeetCode#225-Implement Stack using Queues-用队列实现栈
    LeetCode#20-Valid Parentheses-有效的括号
    树的遍历
    [leetcode] 树(Ⅰ)
    二叉树图形化显示
    你错在成长于文明的边陲
  • 原文地址:https://www.cnblogs.com/pengge666/p/11606647.html
Copyright © 2011-2022 走看看