Ghd

CodeForces - 364D

John Doe offered his sister Jane Doe find the gcd of some set of numbers a.

Gcd is a positive integer g, such that all number from the set are evenly divisible by g and there isn't such g' (g' > g), that all numbers of the set are evenly divisible by g'.

Unfortunately Jane couldn't cope with the task and John offered her to find the ghd of the same subset of numbers.

Ghd is a positive integer g, such that at least half of numbers from the set are evenly divisible by g and there isn't such g' (g' > g) that at least half of the numbers from the set are evenly divisible by g'.

Jane coped with the task for two hours. Please try it, too.

Input

The first line contains an integer n (1 ≤ n ≤ 106) showing how many numbers are in set a. The second line contains space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1012). Please note, that given set can contain equal numbers.

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the %I64d specifier.

Output

Print a single integer g — the Ghd of set a.

Examples

Input
  1. 6
    6 2 3 4 5 6
Output
  1. 3
Input
  1. 5
    5 5 6 10 15
Output
  1. 5
  2.  
  3. 题意:n个数中取一半,使得gcd最大
  4.  
  5. sol:似乎是鬼畜的随机化算法,每次随机取一个,然后计算所有数字与它的gcd,再随便判断一下个数是否满足一半就好了
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. inline ll read()
  5. {
  6. ll s=; bool f=; char ch=' ';
  7. while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
  8. while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
  9. return (f)?(-s):(s);
  10. }
  11. #define R(x) x=read()
  12. inline void write(ll x)
  13. {
  14. if(x<) {putchar('-'); x=-x;}
  15. if(x<) {putchar(x+''); return;}
  16. write(x/); putchar((x%)+'');
  17. }
  18. #define W(x) write(x),putchar(' ')
  19. #define Wl(x) write(x),putchar('\n')
  20. const int N=;
  21. int n,m;
  22. ll a[N],cnt=,b[N],ans=;
  23. struct Node{ll num; int cnt;}c[N];
  24. inline ll gcd(ll a,ll b)
  25. {
  26. return (!b)?(a):(gcd(b,a%b));
  27. }
  28. inline void Solve()
  29. {
  30. ll tmp=a[rand()%n+];
  31. // cout<<"tmp="<<tmp<<endl;
  32. int i,j;
  33. for(i=;i<=n;i++) b[i]=gcd(a[i],tmp);
  34. sort(b+,b+n+);
  35. cnt=; c[++cnt].num=b[]; c[cnt].cnt=;
  36. for(i=;i<=n;i++)
  37. {
  38. if(b[i]!=b[i-]){c[++cnt].num=b[i]; c[cnt].cnt=;} c[cnt].cnt++;
  39. }
  40. for(i=;i<=cnt;i++)
  41. {
  42. int sum=;
  43. for(j=;j<=cnt;j++) if(c[j].num%c[i].num==) sum+=c[j].cnt;
  44. if(sum>=m) ans=max(ans,c[i].num);
  45. }
  46. }
  47. int main()
  48. {
  49. srand();
  50. int i;
  51. R(n);
  52. for(i=;i<=n;i++) R(a[i]);
  53. m=(n+)/;
  54. for(i=;i<=;i++) Solve();
  55. Wl(ans);
  56. return ;
  57. }
  58. /*
  59. input
  60. 6
  61. 6 2 3 4 5 6
  62. output
  63. 3
  64.  
  65. input
  66. 5
  67. 5 5 6 10 15
  68. output
  69. 5
  70. */
  1.  

codeforces364D的更多相关文章

  1. 2018.09.14 codeforces364D(随机化算法)

    传送门 根据国家集训队2014论文集中胡泽聪的随机化算法可以通过这道题. 对于每个数,它有12" role="presentation" style="posi ...

随机推荐

  1. 用Activator.CreateInstance代替new实现类的实例化

    一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例. 这在工厂模式中是非常有用的 这样,可以使程序有更高的扩展性,例如,,下面的例子 如果现在有一个类,专门用来计算交通工具的速度, ...

  2. react开发vscode插件推荐

    原文地址:https://github.com/xieqingtian/blog/issues/2 由于本人主要是做react开发,用的代码编辑器是传说中的宇宙第一前端神器vscode, 所以在这里记 ...

  3. 初学java4 循环的使用

    for循环 for(初始条件;循环终止条件;循环结束后所执行代码){ 循环体 } while循环 while(循环终止条件){ 循环体 } do while循环 do{ }while(循环终止条件);

  4. QT开发小技巧-窗口处理(一)

    this->setWindowFlags(Qt::WindowCloseButtonHint); // 仅保留关闭按钮 this->setAttribute(Qt::WA_DeleteOn ...

  5. ES6入门十:iterator迭代器

    迭代模式 ES6迭代器标准化接口 迭代循环 自定义迭代器 迭代器消耗 一.迭代模式 迭代模式中,通常有一个包含某种数据集合的对象.该数据可能存在一个复杂数据结构内部,而要提供一种简单的方法能够访问数据 ...

  6. nuxt项目打包上线之二

    之前写过一篇nuxt打包上线的文章,请看这里:https://www.cnblogs.com/daisygogogo/p/11218809.html 上一篇文章的部署流程有点不好的地方,就是它适用于只 ...

  7. ASR测试方法---字错率(WER)、句错率(SER)统计

    一.基础概念 1.1.语音识别(ASR) 语音识别(speech recognition)技术,也被称为自动语音识别(英语:Automatic Speech Recognition, ASR), 狭隘 ...

  8. 根据CPU核心数确定线程池并发线程数(转)

    一.抛出问题 关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下: 第一派:<Java Concurrency in Practice>即&l ...

  9. JavaMaven【八、pom.xml】

    简介: 重点学习: 1.dependency-scope 依赖范围 compile 编译 默认,对编译.测试.运行都有效 provided 编译和测试时有效 runtime 测试和运行时有效 test ...

  10. Flutter——GridView组件(网格列表组件)

    GridView组件的常用参数: 名称 类型 说明 scrollDirection Axis 滚动方法 padding EdgeInsetsGeometry 内边距 resolve bool 组件反向 ...