hdu 5207 Greatest Greatest Common Divisor(数论)
来源:互联网 发布:vb用的是什么语言 编辑:程序博客网 时间:2024/06/05 11:03
题意:
给你n个数的数组,挑两个数使得最大公约数最大,求最大的最大公约数。
解析:
求出每个数的举因子,如果数组里有两个或两个以上能够整除这个因子,那么这些因子中,最大的因子就是ans。
AC代码
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace std;typedef long long ll;const int N = 1e5 + 10;int arr[N], n;int vis[N];inline void read(int &x) { int flag = 0; x = 0; char c = getchar(); if(c == '-') flag = 1; while(c < '0' || c > '9') { if(c == '-') flag = 1; c = getchar(); } while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); if(flag) x = -x;}int gcd(int a, int b) { if(b == 0) return a; return gcd(b, a%b);}int main() { int T, cas = 1; scanf("%d", &T); while(T--) { memset(vis, 0, sizeof(vis)); scanf("%d", &n); for(int i = 0; i < n; i++) { read(arr[i]); } sort(arr, arr+n); for(int i = 0; i < n; i++) { int lim = (int)sqrt((double)arr[i]); for(int j = 1; j <= lim; j++) { if(arr[i] % j == 0) { vis[j]++; vis[arr[i]/j]++; } } if(lim*lim == arr[i]) vis[lim]--; } int ans; for(int i = N; i >= 1; i--) { if(vis[i] >= 2) { ans = i; break; } } printf("Case #%d: %d\n", cas++, ans); } return 0;}
0 0
- Hdu 5207 Greatest Greatest Common Divisor(数论)
- 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(暴力)
- Greatest Greatest Common Divisor
- hdu 5207 Greatest Greatest Common Divisor【筛法】【思维】
- hdu 5210 Greatest Greatest Common Divisor
- 最大公约数(Greatest Common Divisor)
- Greatest Greatest Common Divisor HD5207
- 个人归纳Log4j概念
- java中List转数组,数组转List
- 第二大的数
- Generic Programming and the STL笔记5--Iterator Classes
- 应用程序的代码混淆(打包)
- hdu 5207 Greatest Greatest Common Divisor(数论)
- android语音识别之科大讯飞语音API的使用
- 重温《C++ Primer》笔记五 类定义中成员函数的名字隐藏(虚函数与非虚函数)
- 物理引擎
- 编译中遇到的异常报错解决
- BADI查找方法
- 设置ListView中的分割线divider不顶边的方法
- TCP连接探测中的Keepalive 和心跳包
- 获取context的几种方式