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. openjudge-NOI 2.6-1944 吃糖果

    题目链接:http://noi.openjudge.cn/ch0206/1944/ 题解: 递推,题目中给出了很详细的过程,不讲解 #include<cstdio> int n; int ...

  2. 安装mysql驱动之 mysqlclient 出现的报错处理(ubuntu16.04)

    首先 更新软件! sudo apt-get update 然后尝试安装mysqlclient,报错 后执行下面的步骤 安装mysql驱动之 mysqlclient 出现的报错信息处理 报错1: OSE ...

  3. 最全Pycharm教程(26)——Pycharm搜索导航之文件名、符号名搜索(转)

    1.准备一个工程 向你的工程中添加一个Python文件,并输入一些源码,例如: 2.转到对应文件.类.符号 Pycharm提供的一个很强力的功能就是能够根据名称跳转到任何文件.类.符号所在定义位置. ...

  4. Redis 常见面试题

    使用Redis有哪些好处? 速度快 基于内存,避免了磁盘I/O的瓶颈. 单进程单线程,减少了线程上下文切换的开销 利用队列技术将并行访问变为串行访问,消除了传统数据库并发访问控制锁的开销. Redis ...

  5. beego小技巧两则:通过命令行自定义端口和环境,url中带有中划线处理

    1,通过命令行自定义端口和环境 先用flag提取参数,如果要环境,就反向注进去, 如果是端口,就打到beego.Run函数里去. 2,在作router里的url解析时,如果URL中含有-中划线,用严格 ...

  6. 【LOJ】 #2013. 「SCOI2016」幸运数字

    题解 最大异或和,明显是个线性基 然而还有那么多路径--那就树分治,反正点数看起来很少,就是为了让人乘上一个60的常数嘛 把一个树的点分树记录下来,然后看看询问的两个点彼此相同的最后一个父亲是谁,把这 ...

  7. office2016破解激活安装

    昨天一室友装office2016不会激活,[嘲讽脸]真的是笨啊.我这是在一个社区论坛里面找到的. 在网上找了教程,有些没用的,所以记录安装教程以便之后查看.以下的安装激活亲测有效(2017-4-28) ...

  8. POJ 2019 Cornfields [二维RMQ]

    题目传送门 Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7963   Accepted: 3822 ...

  9. POJ3255 Roadblocks [Dijkstra,次短路]

    题目传送门 Roadblocks Description Bessie has moved to a small farm and sometimes enjoys returning to visi ...

  10. 洛谷P1073 最优贸易 [图论,DP]

    题目传送门 最优贸易 题目描述 C 国有n 个大城市和m 条道路,每条道路连接这n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这m 条道路中有一部分为单向通行的道路,一部分为双向 ...