SGU 499:Greatest Greatest Common Divisor
来源:互联网 发布:ubuntu 查看显卡型号 编辑:程序博客网 时间:2024/06/05 19:56
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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 thequality of friendship
between two persons is equal to the greatest common divisor of their friendship number. That means there areprime
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.
Input
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
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 input
sample output
49152516
5
//题意:找出n个数中某两个数的公约数的值最大。
//分析:直接暴力枚举时间复杂度O(n^2)是不允许的。题目中说明n个数的范围都在1000000以内,那么可以用一个数组
标记每个数据出现多少次。以最大的数据maxnum为上限开始枚举所有的数for(maxnum->1)的倍数,如果当前这个数
的倍数出现两次以上则得到最大公约数。整体时间复杂度约为10^6*(log(10^6)).
#include<stdio.h>#include<string.h>using namespace std;#define maxn 1000005#define max(p,q) (p>q?p:q)int num[maxn],a[maxn];int main(){int n,i,j,k;while(~scanf("%d",&n)){int maxp=1,ans;memset(num,0,sizeof(num));for(i=1;i<=n;i++){scanf("%d",&a[i]);maxp=max(maxp,a[i]);num[a[i]]++;}int flag=0;for(i=maxp;i>=1;i--){k=0;for(j=i;j<=maxp;j+=i){if(num[j])k+=num[j];if(k>1){printf("%d\n",i);flag=1;break;}}if(flag)break;}}return 0;}
- SGU 499:Greatest Greatest Common Divisor
- sgu 499 Greatest Greatest Common Divisor 约数“打表”
- Greatest Greatest Common Divisor
- sgu 499. Greatest Greatest Common Divisor(枚举因子)
- Greatest Greatest Common Divisor HD5207
- Greatest common divisor
- The Greatest Common Divisor
- GCD (Greatest Common Divisor)
- Solving Greatest Common Divisor
- the greatest common divisor
- 最大公约数Greatest Common Divisor
- HDU 5207 Greatest Greatest Common Divisor
- 【瞎搞】 HDU 5207 Greatest Greatest Common Divisor
- Greatest Greatest Common Divisor hdu 5207
- hdu 5207 Greatest Greatest Common Divisor
- hdu 5207 Greatest Greatest Common Divisor
- HDU 5207 Greatest Greatest Common Divisor
- HDU 5207Greatest Greatest Common Divisor
- 队列的线性存储和链式存储实现
- 形式逻辑与辩证法
- linux /dev 常见特殊设备介绍与应用[loop,null,zero,full,random]
- C语言实现的密码输入功能
- 读书笔记:数组最大子序列问题 及 联机算法
- SGU 499:Greatest Greatest Common Divisor
- 服务器数据库系列 - Memcache的使用和协议分析详解
- ios编程引入第三方包,不支持arc解决办法
- C# TCP实现多个客户端与服务端 数据 与 文件的传输
- CFile
- 欢迎参加WindowsAzure启动训练营
- 如何终止线程的运行(C/C++)
- C语言的学习笔记
- 【读书笔记】软件架构师应该知道的97件事