HDU 5207Greatest Greatest Common Divisor
来源:互联网 发布:国内编程语言排行 编辑:程序博客网 时间:2024/06/05 08:25
题意: 给你n个数,求出n个数里面选择两个数,使得公约数最大。
直接暴力显然是不行的 复杂度高达O(n^2)。可以用筛法,复杂度是O(nlogn)。a[i] = 1表示i这个数出现过。然后用筛法,从1遍历到max,这一层循环的i代表最大公约数,如果内层的a[i*j]=1,就说明i是i和i*j这两个数的最大公约数。往后遍历就能求出最大的最大公约数。
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int a[100010];int main(){ int t; scanf("%d",&t); for(int cas = 1; cas <= t; cas++) { int n,maxx = 0; scanf("%d",&n); memset(a,0,sizeof(a)); for(int i = 0; i < n; i++) { int x; scanf("%d",&x); maxx = max(maxx,x); a[x]++; } int ans = 1; for(int i = 1; i <= maxx; i++)//最大公约数。 { int count1 = 0; for(int j = i; j <= maxx; j+=i)//筛法。 count1 += a[j]; if(count1 > 1) ans = i; } printf("Case #%d: %d\n",cas,ans); } return 0;}
0 0
- 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
- 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【筛法】【思维】
- hdu 5210 Greatest Greatest Common Divisor
- Greatest Greatest Common Divisor HD5207
- Greatest common divisor
- Google+ 登录
- 适配器模式
- 黑马程序员——Objective-C第六天课程学习
- iis网站.net版本选择
- osx升级到10.10后,用pod install报错最终解决办法
- HDU 5207Greatest Greatest Common Divisor
- Lua_键盘事件
- 湘潭大学oj1213
- 集群:集群基础概念
- fully distributed(hadoop&hbase)
- vim保存折叠信息的方法
- Spring MVC JSP页面无法加载.jpg .css .js的解决方案
- Lua_重力加速器
- 《Linux设备驱动开发详解(第3版)》(即《Linux设备驱动开发详解:基于最新的Linux 4.0内核》)前言