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 ...
随机推荐
- redis必会
1.NosqL 非关系型数据库,里面包含Redis和MondoDB2.为什么会用到关系型数据库?因为当数据量太多,访问人数过多的时候,在访问关系型数据库时会到硬盘里进行读写过多 这样就会导致访问速度很 ...
- 06.搭建kafka集群环境并测试
参考: https://www.cnblogs.com/zhangs1986/p/6565639.html https://www.cnblogs.com/frankdeng/p/9403883.ht ...
- 以某课网日志分析为例 进入大数据 Spark SQL 的世界
第1章 初探大数据 本章将介绍为什么要学习大数据.如何学好大数据.如何快速转型大数据岗位.本项目实战课程的内容安排.本项目实战课程的前置内容介绍.开发环境介绍.同时为大家介绍项目中涉及的Hadoop. ...
- python列表的通用操作
#'+'和'*'#+可以将两个列表拼接为一个列表my_list = [1,2,3]+[4,5,6]#*可以将列表重复指定的次数my_list = [1,2,3]*5 print(my_list) #创 ...
- C# 对DataTable的简单操作
//更改列名 dt.Columns["原来的列名"].ColumnName="新的列名"; //移除列 dt.Columns.Remove("列名&q ...
- Java 访问控制规则简介
1. 概述 老生常谈的内容 巩固一下自己 要摇摇欲坠的基础 内容确实不怎么高级... 2. 常规解释 1. 概述 简单说下什么情况 在单纯考虑 public, protected, 以及 privat ...
- 20155306 实验四 Android程序设计
20155306 实验四 Android程序设计 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android.组件.布局管理器的使用: 3.掌握An ...
- 关于第11周课堂mini dc的课堂练习
测试代码: 码云链接 import java.util.Scanner; public class MyDCTester { public static void main(String[] args ...
- 20155325 2016-2017-2 《Java程序设计》第4周学习总结
教材学习内容总结 封装就是将数据与相关行为包装在一起以实现信息就隐藏. 多态是指不同的类对象调用同一个签名的成员方法时将执行不同代码的现象.多态是面向对象程序设计的灵活性和可扩展性的基础. 以封装为基 ...
- codevs 5429 多重背包
5429 多重背包 http://codevs.cn/problem/5429 分析: f[i]=g[j-k*siz[i]]+k*val[i]; 发现一个状态d只会更新,d+siz[i],d+2*si ...