HDU 5207 Greatest Greatest Common Divisor
题目链接:
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的更多相关文章
- hdu 5207 Greatest Greatest Common Divisor 数学
Greatest Greatest Common Divisor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...
- [UCSD白板题] Greatest Common Divisor
Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...
- greatest common divisor
One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...
- 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...
- 845. Greatest Common Divisor
描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...
- LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45
1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...
- 【Leetcode_easy】1071. Greatest Common Divisor of Strings
problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrin ...
- 2018CCPC桂林站G Greatest Common Divisor
题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...
- upc组队赛17 Greatest Common Divisor【gcd+最小质因数】
Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...
- leetcode 1071 Greatest Common Divisor of Strings
lc1071 Greatest Common Divisor of Strings 找两个字符串的最长公共子串 假设:str1.length > str2.length 因为是公共子串,所以st ...
随机推荐
- [修正] Firemonkey Android 文字斜粗体显示不全的问题
问题:Firemonkey Android 平台显示斜粗体文字时,文字右方会有显示不全的问题. 修正代码: 请将 FMX.FontGlyphs.Android.pas 复制到自己的工程目录下,再修改如 ...
- Delphi Android USB Interface with the G2
来源:http://www.bverhue.nl/g2dev/?p=65 Delphi Android USB Interface with the G2 Leave a reply I first ...
- HIVE—数据仓库
1. hive是什么? Hive是基于 Hadoop 的一个数据仓库工具: 1. hive本身不提供数据存储功能,使用HDFS做数据存储: 2. hive也不分布式计算框架,h ...
- 虚拟机与ARM之间的交叉编译总结
通过三大服务的配置,我们可以在ARM中下载内核和文件系统.我们通过在虚拟机中编程,得到的程序不能在ARM中运行,需要经过一个交叉编译.得到的可执行程序可以在ARM中运行,此时不能在虚拟机Linux中运 ...
- 数据采集与分析的那些事——从数据埋点到AB测试
作者:网易有数郑栋. 一.为什么企业需要一套完善的用户行为埋点和分析平台 产品初创期间,需要分析天使用户的行为来改进产品,甚至从用户行为中得到新的思路或发现来调整产品方向:产品成长过程,通过对用户行为 ...
- redis未授权弱口令检测脚本(redis未授权访问漏洞,利用redis写webshell)
以下如有雷同,不胜荣幸 * --- 示例代码!!!!!----*/ #! /usr/bin/env python # _*_ coding:utf-8 _*_ import socket impor ...
- scala 求数组排序后每两个元素的差值
求数组排序后每两个元素的差值 例如数组 1,5,8,10,2 求得结果为 1,3,3,2 一般什么样的场景会有这种需求呢? 比如 计算一堆数据在一定时间内的计算时延, 或者得到这段时间内数据的平均计算 ...
- CentOS7和OpenStack的笔记(一)
CentOS7和OpenStack的笔记(一) 最近搞CentOS7系统和OpenStack框架,整了近一个星期,系统装了好几次,框架搭了又从搭.虽然最后的实例没能启动成功,但是在这专研的一个星期里, ...
- tensorflow 模型权重导出
tensorflow在保存权重模型时多使用tf.train.Saver().save 函数进行权重保存,保存的ckpt文件无法直接打开,不利于将模型权重导入到其他框架使用(如Caffe.Keras等) ...
- 20155320《Java程序设计》实验一(Java开发环境的熟悉)实验报告
20155320<Java程序设计>实验一(Java开发环境的熟悉)实验报告 实验内容及步骤 (一)命令行下Java程序开发 步骤一:首先在cmd中输入d:和cd ljq20155320进 ...