题目链接:

hdu:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=153598

bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=577&pid=1002

题解:

1、对于每个输入,枚举它的因子,并统计,结果存在mmp数组中,最后再倒过来扫一遍mmp数组,其中第一个mmp[i]>=2的,就是答案。

  时间复杂度:O(n*sqrt(n) )

这个跑了1404ms

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int maxn=1e5+;
typedef long long LL; int n;
int mmp[maxn]; void init(){
memset(mmp,,sizeof(mmp));
} int main() {
int tc,kase=;
scanf("%d",&tc);
while(tc--) {
scanf("%d",&n);
init();
for(int i=;i<n;i++){
int x;
scanf("%d",&x);
for(int div=;div*div<=x;div++){
if(x%div==){
mmp[div]++;
if(x/div!=div) mmp[x/div]++;
}
}
}
int ans=;
for(int i=maxn-;i>=;i--){
if(mmp[i]>=){
ans=i; break;
}
}
printf("Case #%d: %d\n",++kase,ans);
}
return ;
}

2、用筛法预处理出10^5以内所有数的因子,然后采用同上的方法做。

筛法的时间复杂度:O(n+n/2+n/3+n/4+...n/n)=O(n*(1+1/2+1/3+...1/n))=O(n*logn)

(附:欧拉公式:1+1/2+1/3+……+1/n=ln(n)+C)

这个跑了374ms

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std; const int maxn=1e5+;
typedef long long LL; int n;
int mmp[maxn];
vector<int> Div[maxn]; void pre_for_div(){
for(int i=;i<maxn;i++){
for(int t=i;t<maxn;t+=i){
Div[t].push_back(i);
}
}
} void init(){
memset(mmp,,sizeof(mmp));
} int main() {
pre_for_div();
int tc,kase=;
scanf("%d",&tc);
while(tc--) {
scanf("%d",&n);
init();
for(int i=;i<n;i++){
int x;
scanf("%d",&x);
for(int j=;j<Div[x].size();j++){
mmp[Div[x][j]]++;
}
}
int ans=;
for(int i=maxn-;i>=;i--){
if(mmp[i]>=){
ans=i; break;
}
}
printf("Case #%d: %d\n",++kase,ans);
}
return ;
}

3、先统计每个输入的值出现的个数,存在cnt里面;然后从大到小枚举答案ans=d,求解sum(cnt[d],cnt[d*2],cnt[d*3]...),如果sum>=2说明答案就是d。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std; const int maxn=1e5+;
typedef long long LL; int n;
int cnt[maxn]; void init(){
memset(cnt,,sizeof(cnt));
} int main() {
int tc,kase=;
scanf("%d",&tc);
while(tc--) {
scanf("%d",&n);
init();
for(int i=;i<n;i++){
int x;
scanf("%d",&x);
cnt[x]++;
}
int ans=;
for(int g=maxn-;g>=;g--){
int tmp=;
for(int sum=g;sum<maxn;sum+=g){
tmp+=cnt[sum];
}
if(tmp>=){
ans=g; break;
}
}
printf("Case #%d: %d\n",++kase,ans);
}
return ;
}

HDU 5207 Greatest Greatest Common Divisor的更多相关文章

  1. hdu 5207 Greatest Greatest Common Divisor 数学

    Greatest Greatest Common Divisor Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...

  2. [UCSD白板题] Greatest Common Divisor

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

  3. greatest common divisor

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

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

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

  5. 845. Greatest Common Divisor

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

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

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

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

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

  8. 2018CCPC桂林站G Greatest Common Divisor

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

  9. upc组队赛17 Greatest Common Divisor【gcd+最小质因数】

    Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...

  10. leetcode 1071 Greatest Common Divisor of Strings

    lc1071 Greatest Common Divisor of Strings 找两个字符串的最长公共子串 假设:str1.length > str2.length 因为是公共子串,所以st ...

随机推荐

  1. 搭建php服务器网站

    一.Apache安装 yum install httpd启动systemctl start httpd.service #启动systemctl stop httpd.service #停止syste ...

  2. WOT干货大放送:大数据架构发展趋势及探索实践分享

      WOT大数据处理技术分会场,PingCAP CTO黄东旭.易观智库CTO郭炜.Mob开发者服务平台技术副总监林荣波.宜信技术研发中心高级架构师王东及商助科技(99Click)顾问总监郑泉五位讲师, ...

  3. django查询集-17

    当查询结果是多个的时候,django-ORM会返回一个 查询集(QuerySet) ,表示从数据库中获取对象的 集合 . 查询集可以使用过滤器进行再次处理. 例如查询阅读量大于20且评论数大于30的书 ...

  4. dns欺骗之ettercap

    ettercap是一个基于ARP地址欺骗方式的网络嗅探工具,主要适用于局域网. ettercap是一款现有流行的网络抓包软件,它利用计算机在局域网内进行通信的ARP协议的缺陷进行攻击,在目标与服务器之 ...

  5. vagrant boxes

    vagrant box add hashicorp/precise64

  6. GridSQL--Stado 学习初步

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL集群方案相关索引页     回到顶级页面:PostgreSQL索引页 作者  高健@博客园   luckyjackgao ...

  7. 使GDAL库支持中文路径或中文文件名的处理方法

    之前生成的gdal 2.1.1动态库,在通过命令行执行时,遇到有中文路径或中文图像名时,GDALOpen函数不能正确的被调用,如下图: 解决方法: 1.      在所有使用GDALAllRegist ...

  8. js Date对象要注意的问题(时间转换)

    1.时间戳和时间对象可以灵活转变: let n = new Date() // 返回的是当前时间对应的国际时间 let nt =n.getTime() let n2 =new Date(nt) con ...

  9. 【转】 mysql使用federated引擎实现远程访问数据库(跨网络同时操作两个数据库中的表)

    原文转自:http://www.2cto.com/database/201412/358397.html 问题: 这里假设我需要在IP1上的database1上访问IP2的database数据库内的t ...

  10. python基本数据类型2

    python_day_4 今日大纲: 1. list(增删改查) 列表可以装大量的数据. 不限制数据类型. 表示方式:[] 方括号中的每一项用逗号隔开 列表和字符串一样.也有索引和切片 常用的功能: ...