HDU 5207 Greatest Greatest Common Divisor(暴力)
来源:互联网 发布:嵌入式linux内核裁剪 编辑:程序博客网 时间:2024/05/17 21:07
和很多题目的思路是一样的, 因为如果直接二重循环枚举的话时间复杂度是承受不了的, 所以我们不妨换个思路, 先预处理出来没个数出现的次数, 然后从1枚举到最大值i, 对于每个i , 我们找它的倍数, 那么它的倍数肯定以它为最大公约数(因为是从大到小枚举的), 如果超过两个, 则存在以i为最大公约数的两个数, 那么这个i也就是最大的。
细节参见代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<set>#include<list>#include<deque>#include<map>#include<queue>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))using namespace std;typedef long long ll;const double PI = acos(-1.0);const double eps = 1e-6;const int INF = 1000000000;const int maxn = 100000+7;int T,n,m,kase = 0,a[maxn], vis[maxn];int main() { scanf("%d",&T); while(T--) { scanf("%d",&n); int maxv = 0; for(int i=0;i<n;i++) { scanf("%d",&a[i]); maxv = max(a[i], maxv); } int ans = 0; memset(vis, 0, (maxv+1)*sizeof(int)); for(int i=0;i<n;i++) vis[a[i]]++; for(int i=maxv;i>=1;i--) { int cur = 0; for(int j=i;j<=maxv;j+=i) { cur += vis[j]; if(cur > 1) break; } if(cur > 1) { printf("Case #%d: %d\n",++kase, ans); break; } } } return 0;}
0 0
- HDU 5207 Greatest Greatest Common Divisor(暴力)
- HDU 5207 Greatest Greatest Common Divisor
- 【瞎搞】 HDU 5207 Greatest Greatest Common Divisor
- Greatest Greatest Common Divisor hdu 5207
- hdu 5207 Greatest Greatest Common Divisor
- hdu 5207 Greatest Greatest Common Divisor
- HDU 5207 Greatest Greatest Common Divisor
- HDU 5207Greatest Greatest Common Divisor
- HDU ACM 5207 Greatest Greatest Common Divisor
- HDU 5207 Greatest Greatest Common Divisor
- HDU 5207 Greatest Greatest Common Divisor
- Hdu 5207 Greatest Greatest Common Divisor(数论)
- hdu 5207 Greatest Greatest Common Divisor(数论)
- HDOJ 5207 Greatest Greatest Common Divisor 暴力枚举
- Greatest Greatest Common Divisor
- hdu 5207 Greatest Greatest Common Divisor【筛法】【思维】
- hdu 5210 Greatest Greatest Common Divisor
- 最大公约数(Greatest Common Divisor)
- 用快捷键强制关闭Ubuntu无响应程序
- C++ new创建对象生命周期
- CentOS iptables 配置教程
- 从头认识java-13.11 对比数组与泛型容器,观察类型擦除给泛型容器带来什么问题?
- 手把手教你做html日历
- HDU 5207 Greatest Greatest Common Divisor(暴力)
- 记录和恢复listView的滚动位置
- 学习专栏
- KMP算法详解
- 申请专栏,水一篇,占坑以后补上
- iframe自动根据内容调整大小
- [从头学数学] 第10节 总复习
- Why The Hell Would I Use Node.js? A Case-by-Case Tutorial
- JNI生成C++头文件,提示错误“找不到‘com.mps.test.HelloWorld'的类文件”