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 ...
随机推荐
- 使同一个server上不同port的django应用可在同一个浏览器上打开
如果我们有两个django应用site1和site2同时跑在同一个server的不同端口,同时我们在同一个浏览器的不同tab登录.那么这时就出出现这种情况,当我们登录site2时就会将site1上登录 ...
- thinkphp+redis实现秒杀,缓存等功能
秒杀是商城常见功能 php+redis是最常见的秒杀功能 1,安装redis,根据自己的php版本安装对应的redis扩展 首先查看phpinfo();php环境信息 2,下载redis https ...
- Python学习 :面向对象 -- 成员修饰符
成员修饰符 两种成员 - 公有成员 - 私有成员, __字段名 - 无法直接访问,只能通过内部方法来间接访问私有成员 简例:公有成员与私有成员 class Info: country = '中国' ...
- 【深度优先搜索】NOIP2017_D2T1 洛谷3958奶酪
这道题的写法大体有两种:大法师DFS和并查集,两种算法都不难,本篇博客主要讲解DFS,而且测试数据特水,连个剪枝都不用都可以过. 题目描述[luogu传送门] 现有一块大奶酪,它的高度为 h,它的长度 ...
- go语言Mutex与RWMutex用法
Mutex(互斥锁) Lock()加锁,Unlock()解锁 适用于读写不确定,并且只有一个读或者写的场景 例: package main import ( "sync" &quo ...
- SQL学习笔记:高级教程
SQL语法 LIMIT select col from table limit number select * from table limit number LIKE select * from t ...
- FPGA千兆网UDP协议实现
接着上一篇百兆网接口的设计与使用,我们接着来进行FPGA百兆网UDP(User Datagram Protocol)协议的设计. 1)UDP简介 在此,参考博主夜雨翛然的博文“https://www. ...
- 20155315 2016-2017-2 《Java程序设计》第三周学习总结
教材学习内容总结 1.构造方法决定类生成对象的方式 用this将已存在的参数的值指定给此参数. 用new建立新的实例. class Clothes { String color; char size; ...
- 20155322 《Java程序设计》课堂实践项目MyOD
20155322 <Java程序设计>课堂实践项目MyOD 实践要求 编写MyOD.java 用java MyOD XXX实现Linux下od -tx -tc XXX的功能 提交测试代码和 ...
- centos7安装cacti
参考博客地址:https://blog.csdn.net/kenn_lee/article/details/80565385 Cacti是一套基于PHP,MySQL,SNMP及RRDTool开发的网络 ...