499. Greatest Greatest Common Divisor
Memory limit: 262144 kilobytes
output: standard
Andrew has just made a breakthrough in sociology: he realized how to predict whether two persons will be good friends or not. It turns out that each person has an innerfriendship number (a positive integer). And the quality of friendship between two persons is equal to the greatest common divisor of their friendship number. That means there are prime people (with a prime friendship number) who just can't find a good friend, andWait, this is irrelevant to this problem. You are given a list of friendship numbers for several people. Find the highest possible quality of friendship among all pairs of given people.
The first line of the input file contains an integer n () — the number of people to process. The next n lines contain one integer each, between 1 and
(inclusive), the friendship numbers of the given people. All given friendship numbers are distinct.
Output one integer — the highest possible quality of friendship. In other words, output the greatest greatest common divisor among all pairs of given friendship numbers.
sample input |
sample output |
4 9 15 25 16 |
5
|
题意:
给出 N(2 ~ 100000)个数 ,后给出这 N 个不同的数(1 ~ 1000000),任意一对数都存在一个最大公因子,求出所有对中的最大的最大公因子数。
思路:
类似于素数筛选的方法,数最大也只是 1000000 ,给出的数任意两个之间是不会一样的,所以最大的公因子上限为 50000。同时用一个数组标记哪些数出现过,筛选的时候从上限开始往下限搜,当一搜到这个因子的倍方数出现两次的时候就马上跳出循环,这时候的公因子数即为答案。
AC:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX = 1000005; int num[MAX]; int main() { int n; scanf("%d", &n); memset(num, 0, sizeof(num)); while (n--) { int ans; scanf("%d", &ans); num[ans] = 1; } int temp = 0; for (int i = MAX / 2; i >= 1; --i) { int sum = 0; for (int j = i; j <= MAX; j += i) { if (num[j]) ++sum; if (sum == 2) { printf("%d\n", i); temp = 1; break; } } if (temp) break; } }
相关推荐
最大公约数(Greatest Common Divisor,简称 GCD),是指两个或多个整数共有最大的正整数因子。例如,考虑两个整数12和16,它们共有的正整数因子包括1、2和4,其中4是最大的一个,因此我们说GCD(12, 16) = 4。 ####...
在计算机科学中,最大公约数(Greatest Common Divisor,GCD)是一个基本的数论概念,用于找出两个或多个整数的最大公共因子。这里我们将深入探讨三种常见的算法来求解最大公约数,并分析它们的优缺点。 1. 辗转...
【公约数】是指两个或多个自然数共有的约数,而【最大公约数】(Greatest Common Divisor, GCD)是这些公约数中最大的一个。例如,12的约数有1、2、3、4、6、12,30的约数有1、2、3、5、6、10、15、30,12和30的公...
首先,让我们来讨论“最大公约数”(Greatest Common Divisor, GCD)。最大公约数是指两个或多个非零整数的最大公共因子。枚举法是一种直观的求解方法,它通过遍历所有可能的因子来找到最大公约数。例如,对于两个数a...
最大公约数(Greatest Common Divisor,GCD)是数学中的一个重要概念,它指的是两个或多个整数共有约数中最大的一个。在编程和算法设计中,计算两个数的最大公约数是一个常见的问题,它涉及到多个领域的应用,比如在...
在数学和程序设计领域,计算两个整数的最大公约数(Greatest Common Divisor, GCD)是一个常见且重要的问题。有多种算法可以解决此问题,它们各有优劣。本文将介绍四种不同的最大公约数求解方案,并对这些方案的时间...
最大公因数(Greatest Common Divisor, GCD)与最小公倍数(Least Common Multiple, LCM)是数学中的基本概念,特别是在整数理论中。它们是解决多个数之间关系的重要工具,常用于简化计算、分解因式以及在编程中处理...
最大公约数(Greatest Common Divisor,GCD)和最小公倍数(Least Common Multiple,LCM)是数学中两个重要的概念,在计算机科学和信息技术领域中有广泛的应用。本文将详细介绍最大公约数和最小公倍数的计算方法,...
首先,我们来看如何使用C语言来求解最大公约数(Greatest Common Divisor, GCD)和最小公倍数(Least Common Multiple, LCM)。这里有两种方法:辗转相除法和穷举法。辗转相除法,也称为欧几里得算法,是一种高效的...
【欧几里德算法】是计算两个正整数最大公约数(Greatest Common Divisor, GCD)的经典算法。该算法基于以下原理:对于任意两个正整数a和b(a>b),它们的最大公约数等于a除以b的余数c和b之间的最大公约数。不断重复...
最大公约数最大公约数的英文是Greatest Common Divisor,常缩写为 gcd。我们首先先了解一下约数的概念:如果存在一个整数kkk,使得a=kda=kda=kd ,则ddd称aaa整除,记做d∣ad|ad∣a,称aaa是ddd的倍数,如果d>0d&...
辗转相除法求最大公约数 Greatest-Common-Divisor 堆排序 Heap-Sort ISAP算法 Improved-Shortest-Augmenting-Path(Naive) 插入排序 Insertion-Sort 字符串匹配(KMP) Knuth-Morris-Pratt 最小生成树(Kruskal...
扩展欧几里德算法通常用于寻找两个整数的最大公约数(Greatest Common Divisor, GCD),并且能求解形如ax + by = GCD(a, b)的线性同余方程的整数解。 首先,我们回顾一下欧几里德算法的基本思想。欧几里德算法基于...
1. 最大公约数(Greatest Common Divisor, GCD)与最小公倍数(Least Common Multiple, LCM): - 最大公约数是两个或多个整数共有的最大正因子,可以使用辗转相除法(欧几里得算法)求解。 - 最小公倍数是能够...
在NOIP2009的第二题中,题目涉及到Gcd(x, a0)和Gcd(x, b0)的概念,这是最大公约数(Greatest Common Divisor,GCD)的计算。通过分解质因数,可以找到x的因子与a0、b0之间的关系,进而确定x的取值范围。对于较大的数...
- **英文术语**:Greatest Common Divisor ##### LCM(最小公倍数) - **定义**:两个或多个整数共有最小的公倍数。 - **计算方法**:通常通过先求出最大公约数,再利用公式`LCM(a, b) = |a * b| / GCD(a, b)`来...
在编程领域,最大公约数(Greatest Common Divisor, GCD)和最小公倍数(Least Common Multiple, LCM)是基本的数学概念,经常在处理整数运算时被用到。这两个概念对于理解计算机科学中的算法和数据结构至关重要。在...
在编程领域,最大公约数(Greatest Common Divisor, GCD)是一个常见的数学概念,它在计算机科学中有着广泛的应用,比如在算法设计、数据结构优化以及加密算法中。本篇我们将聚焦于如何使用Java语言来实现计算两个...
- **题目概述**:该程序用于计算两个整数的最大公约数(Greatest Common Divisor, GCD)。 - **程序逻辑**: - 使用`while`循环实现辗转相除法。 - 在每次循环中,通过`u % v`计算余数,并更新`u`和`v`的值。 - ...
Greatest Common Divisor 最大公约数 - **应用场景**:求两个或多个整数的最大公约数。 - **实现方式**:辗转相除法或更相减损法。 #### 2. Prime 素数判断 - **应用场景**:确定一个数是否为素数。 - **实现方式*...