BestCoder Round #38 1002.Greatest Greatest Common Divisor

原题链接:

http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=577&pid=1002

Greatest Greatest Common Divisor

 
 Accepts: 271
 
 Submissions: 1138
 Time Limit: 4000/2000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
Problem Description

Pick two numbers  ai,aj(ij)  from a sequence to maximize the value of their greatest common divisor.

Input

Multiple test cases. In the first line there is an integer  T , indicating the number of test cases. For each test cases, the first line contains an integer  n , the size of the sequence. Next line contains  n  numbers, from  a1  to  an 1T100,2n105,1ai105 . The case for  n104  is no more than  10 .

Output

For each test case, output one line. The output format is Case # x ans x  is the case number, starting from  1 ans  is the maximum value of greatest common divisor.

Sample Input
2
4
1 2 3 4
3
3 6 9
Sample Output
Case #1: 2
Case #2: 3


题意:和简单,n个数,求其中任意两个数的最大公约数的最大值。

思路:100000的复杂度,肯定是n,nlogn或者n根号n可能都能过。

从最大数nmax开始从大到小枚举x,如果存在两个及以上数都是x的倍数的话,x就是答案了。复杂度nlogn。


注意:可能会有重复的数字出现,所以存放val[num]的时候,不能用只用1,0,需要存放具体个数。

一开始想到了这个做法,但是觉得会超时。这种问题一直不能想的很透彻为什么是nlogn的复杂度,总觉得最坏是n*n,后来看了题解才知道可以这样做。

代码:

#include "stdio.h"
#include "iostream"
#include "string.h"
#include "stdlib.h"
#include "algorithm"
#include "math.h"
#include "map"
#include "queue"
#include "stack"
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=100005;

typedef long long LL;

int T,n,val[100010],cnt,nmax,ans;

int main()
{
	int a;
	cin>>T;
	for(int i=1;i<=T;i++)
	{
		scanf("%d",&n);
		nmax=-1;
		ans=0;
		memset(val,0,sizeof(val));
		while(n--)
		{
			scanf("%d",&a);
			val[a]++;
			
			nmax=max(nmax,a);
		
		}
		for(int i=nmax;i>=1&&ans==0;i--)    //以i为最大公约数假设
		{
			cnt=0;
			for(int j=i;j<=nmax;j+=i)       //枚举i的1倍,2倍等
			{
				if(val[j])     
					cnt+=val[j];
				if(cnt>=2)
				{
					ans=i;
					break;
				}
			}
		}
		printf("Case #%d: ",i);
		printf("%d\n",ans);
	}
	return 0;
}



### 算法实现 在IT领域,有多种算法可以实现最大公约数Greatest Common Divisor)的计算,以下是常见的算法及其不同编程语言的实现: - **欧几里得算法(辗转相除法)**:该算法思路简单且实现代码量少,其基本原理是 `gcd(a, b) = gcd(b, a % b)`,当 `b` 为 0 时,`a` 就是最大公约数。 - **Go语言实现**: ```go package main import "fmt" func gcd(a, b int) int { for b != 0 { a, b = b, a%b } return a } func main() { num1 := 48 num2 := 18 result := gcd(num1, num2) fmt.Printf("The greatest common divisor of %d and %d is %d\n", num1, num2, result) } ``` - **Python实现**: ```python def greatest_common_divisor(a: int, b: int) -> int: return abs(b) if a == 0 else greatest_common_divisor(b % a, a) def gcd_by_iterative(x: int, y: int) -> int: while y: x, y = y, x % y return abs(x) nums = input("Enter two integers separated by comma (,): ").split(",") try: num_1 = int(nums[0]) num_2 = int(nums[1]) print( f"greatest_common_divisor({num_1}, {num_2}) = " f"{greatest_common_divisor(num_1, num_2)}" ) print(f"By iterative gcd({num_1}, {num_2}) = {gcd_by_iterative(num_1, num_2)}") except (IndexError, UnboundLocalError, ValueError): print("Wrong input") ``` - **Objective - C实现**: ```objc #import <Foundation/Foundation.h> int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } int main(int argc, const char * argv[]) { @autoreleasepool { int num1 = 48; int num2 = 18; int result = gcd(num1, num2); NSLog(@"The greatest common divisor of %d and %d is %d", num1, num2, result); } return 0; } ``` - **Stein算法**:对于数据长度较大的特殊情况,Stein算法能够规避进行除法和取模操作,而通过位运算来替代,效率相对更高。其正确性基于 `gcd(a,a) = a` 和 `gcd(ka,kb) = k gcd(a,b)` 等结论,特殊的,当 `k = 2` 时,说明两个偶数的最大公约数必然能被 2 整除 [^3][^5]。 ### 应用场景 - **密码学**:在密码学的一些算法中,如RSA算法,需要计算两个大素数的最大公约数来确保密钥的安全性。通过计算最大公约数可以判断两个数是否互质,这对于生成公钥和私钥非常重要。 - **分数化简**:在计算机程序中处理分数时,需要将分数化为最简形式,这就需要计算分子和分母的最大公约数,然后将分子和分母同时除以最大公约数。 - **数据压缩**:在某些数据压缩算法中,可能会用到最大公约数来进行数据的优化处理,例如对数据块的大小进行调整。 - **计算机图形学**:在图形处理中,计算线段的最简比例、坐标的缩放等操作可能会涉及到最大公约数的计算。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值