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.
  • 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

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 }

原文地址:https://www.cnblogs.com/pengge666/p/11606647.html

时间: 2024-10-11 05:31:48

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

Atcoder Beginner Contest 160 做题记录

A. 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 char s[10]; 4 int main() 5 { 6 scanf("%s",s+1); 7 if(s[3]==s[4]&&s[5]==s[6])puts("Yes"); 8 else puts("No"); 9 } B. 水题 1 #include<bits/stdc++.h> 2 us

AtCoder Beginner Contest 116 C题 【题意:可以在任意区间【L,R】上加1,求通过最少加1次数得到题目给定的区间】】{思维好题}

C - Grand Garden In a flower bed, there are NN flowers, numbered 1,2,......,N1,2,......,N. Initially, the heights of all flowers are 00. You are given a sequence h={h1,h2,h3,......}h={h1,h2,h3,......} as input. You would like to change the height of

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

AtCoder Beginner Contest 155 简要题解

AtCoder Beginner Contest 155 A:签到失败,WA一次. int main() { int a, b, c; cin >> a >> b >> c; if(a == b && b == c) cout << "No"; else if(a == b || a == c || b == c) cout << "Yes"; else cout << &quo

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质)

AtCoder Beginner Contest 152 - F - Tree and Constraints (容斥定理+树上路径的性质) We have a tree with NN vertices numbered 11 to NN. The ii-th edge in this tree connects Vertex aiai and Vertex bibi. Consider painting each of these edges white or black. There ar

【ATcoder】AtCoder Beginner Contest 161 题解

题目链接:AtCoder Beginner Contest 161 原版题解链接:传送门 A - ABC Swap 这题太水,直接模拟即可. 1 #include <iostream> 2 using namespace std; 3 int main() { 4 int a, b, c; 5 cin >> a >> b >> c; 6 swap(a, b); 7 swap(a, c); 8 cout << a << " &

AtCoder Beginner Contest 103 D(贪心)

AtCoder Beginner Contest 103 D 题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连. 按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间. 1 #include <iostream> 2 #include <cstdio> 3 #incl

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one

C语言:输入一个数,输出比这个数小的所有素数,并求出个数。

//C语言:输入一个数,输出比这个数小的所有素数,并求出个数. 1 #include<conio.h> 2 #include<stdio.h> 3 #include<stdlib.h> 4 #define MAX 100 5 int fun(int lim, int aa[MAX]) 6 { 7 int i, j,k=0; 8 for (i = 2; i <= lim; i++)//罗列每个数 9 { 10 for (j = 2; j < i; j++)//