Greatest Greatest Common Divisor

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5207

Description

在数组a中找出两个数ai,aj(i≠j),使得两者的最大公约数取到最大值。

Input

多组测试数据。第一行一个数字T,表示数据组数。对于每组数据,第一行是一个数n,表示数组中元素个数,接下来一行有n个数,a1到an。1≤T≤100,2≤n≤105,1≤ai≤105,n≥104的数据不超过10组。

Output

每组数据输出一行Case #x: ans。x表示组数编号,从1开始。ans表示能取到的最大值。

Sample Input

2
4
1 2 3 4
3
3 6 9

Sample Output

Case #1: 2
Case #2: 3

HINT

题意

题解:

直接枚举每个数的因数

然后倒着枚举,看那个数>=2了,就说明那个数就是最大的gcd;

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 100101
#define mod 10007
#define eps 1e-9
//const int inf=0x7fffffff; //无限大
const int inf=0x3f3f3f3f;
/* int buf[10];
inline void write(int i) {
int p = 0;if(i == 0) p++;
else while(i) {buf[p++] = i % 10;i /= 10;}
for(int j = p-1; j >=0; j--) putchar('0' + buf[j]);
printf("\n");
}
*/
//**************************************************************************************
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} int a[maxn];
int cnt[maxn];
int main()
{
int t=read();
for(int cas=;cas<=t;cas++)
{
memset(a,,sizeof(a));
memset(cnt,,sizeof(cnt));
int n=read();
for(int i=;i<n;i++)
{
a[i]=read();
cnt[a[i]]++;
}
for(int i=;i<maxn;i++)
for(int j=i+i;j<maxn;j+=i)
cnt[i]+=cnt[j];
int mx=;
for(int i=maxn-;i;i--)
if(cnt[i]>=)
{
mx=i;
break;
}
printf("Case #%d: %d\n",cas,mx);
}
}

hdu 5207 Greatest Greatest Common Divisor 数学的更多相关文章

  1. CF1025B Weakened Common Divisor 数学

    Weakened Common Divisor time limit per test 1.5 seconds memory limit per test 256 megabytes input st ...

  2. CCPC2018 桂林 G "Greatest Common Divisor"(数学)

    UPC备战省赛组队训练赛第十七场 with zyd,mxl G: Greatest Common Divisor 题目描述 There is an array of length n, contain ...

  3. 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)

    定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...

  4. [UCSD白板题] Greatest Common Divisor

    Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...

  5. greatest common divisor

    One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...

  6. 845. Greatest Common Divisor

    描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...

  7. LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45

    1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...

  8. 【Leetcode_easy】1071. Greatest Common Divisor of Strings

    problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrin ...

  9. 2018CCPC桂林站G Greatest Common Divisor

    题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...

随机推荐

  1. Linux端口占用

    1.netstat netstat -anp | grep 23232 Sample: [root@BICServer 0825]# netstat -anp | grep 23232 tcp 0 0 ...

  2. skb_reserve(skb,2)中的2的意义

    skb_reserve() skb_reserve()在数据缓存区头部预留一定的空间,通常被用来在数据缓存区中插入协议首部或者在某个边界上对齐.它并没有把数据移出或移入数据缓存区,而只是简单地更新了数 ...

  3. 设置网卡IP,还每次都挨个地址输入吗?批处理一下【转】

    1.设置网卡ip,子网掩码和默认网关,注意修改网卡名称,跟本地连接汇总的网卡名称保持一直 netsh interface ip set address "以太网" static 1 ...

  4. 码源中国.gitignore忽略文件配置

    码源中国.gitignore忽略文件配置 ## Ignore Visual Studio temporary files, build results, and ## files generated ...

  5. sicily 1193. Up the Stairs

    Time Limit: 1sec    Memory Limit:32MB  Description John is moving to the penthouse of a tall sky-scr ...

  6. 005zabbix3.0报错记录

    一.问题描述 在zabbix_server添加变量时,出现了以下的报错,

  7. loadrunner 测试问题汇总

    1.关于Error -27791: Error -27790:Error -27740:        错误如下:        Action.c(198): Error -27791: Server ...

  8. nginx 各种配置

    first : mkdir /usr/local/nginx/conf/vhosts{网站配置}/usr/local/nginx/conf/vhosts/test.conf : server { li ...

  9. 主机名/etc/hosts文件的作用

    1,/etc/hosts,主机名ip配置文件. # Do not remove the following line, or various programs # that require netwo ...

  10. POJ 3616 Milking Time(最大递增子序列变形)

    题目链接:http://poj.org/problem?id=3616 题目大意:给你时间N,还有M个区间每个区间a[i]都有开始时间.结束时间.生产效率(时间都不超过N),只能在给出的时间段内生产, ...