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
6
6 2 3 4 5 6
Output
3
Input
5
5 5 6 10 15
Output
5

题意:n个数中取一半,使得gcd最大

sol:似乎是鬼畜的随机化算法,每次随机取一个,然后计算所有数字与它的gcd,再随便判断一下个数是否满足一半就好了
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=; bool f=; char ch=' ';
while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<) {putchar('-'); x=-x;}
if(x<) {putchar(x+''); return;}
write(x/); putchar((x%)+'');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m;
ll a[N],cnt=,b[N],ans=;
struct Node{ll num; int cnt;}c[N];
inline ll gcd(ll a,ll b)
{
return (!b)?(a):(gcd(b,a%b));
}
inline void Solve()
{
ll tmp=a[rand()%n+];
// cout<<"tmp="<<tmp<<endl;
int i,j;
for(i=;i<=n;i++) b[i]=gcd(a[i],tmp);
sort(b+,b+n+);
cnt=; c[++cnt].num=b[]; c[cnt].cnt=;
for(i=;i<=n;i++)
{
if(b[i]!=b[i-]){c[++cnt].num=b[i]; c[cnt].cnt=;} c[cnt].cnt++;
}
for(i=;i<=cnt;i++)
{
int sum=;
for(j=;j<=cnt;j++) if(c[j].num%c[i].num==) sum+=c[j].cnt;
if(sum>=m) ans=max(ans,c[i].num);
}
}
int main()
{
srand();
int i;
R(n);
for(i=;i<=n;i++) R(a[i]);
m=(n+)/;
for(i=;i<=;i++) Solve();
Wl(ans);
return ;
}
/*
input
6
6 2 3 4 5 6
output
3 input
5
5 5 6 10 15
output
5
*/
 

codeforces364D的更多相关文章

  1. 2018.09.14 codeforces364D(随机化算法)

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

随机推荐

  1. python — lambda表达式与内置函数

    目录 1 lambda表达式 (匿名函数) 2 内置函数 1 lambda表达式 (匿名函数) 用于表示简单的函数 lambda表达式,为了解决简单函数的情况: def func(a1,a2): == ...

  2. Devexpress xaf BO中字段为RuleRequiredField必输字段时,文本标签默认添加*标记

    BO中字段为RuleRequiredField必输字段时,文本标签默认添加*标记.需要在模型编辑器中设置,如图. 官网地址:https://docs.devexpress.com/eXpressApp ...

  3. MediaAPIController

    using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.D ...

  4. tinymce富文本是在modal框中弹出显示的问题

    记录一下,在用tinymce富文本的时候,由于是用在modal 上的,始终无法获取焦点,后来才发现问题出在tinymce在modal前创建了,所以导致这个问题,解决方案就是用 v-if="v ...

  5. javabean转成json字符首字母大写

    今天写接口的时候有个需求将接口返回的json字符串首字母大写:{"SN":"","Result":""}格式, 只需要在 ...

  6. splice与slice区别

    共同点:均是删除数组元素并返回结果. 区别:splice会改变原数组,而slice不会.并且splice会导致数组塌陷. 数组塌陷:使用splice删除元素时,剩余的数组元素索引的顺讯会改变. let ...

  7. 利用浏览器做好数字进制和ASCII码及Unicode教与学

    浏览器是现在个人计算机的标配,一般来说一个PC至少安装一种以上的浏览器.主流网页浏览器有Google Chrome.Internet Explorer.Microsoft Edge.Mozilla F ...

  8. oracle的listagg函数

    今天需要将 BDST_ID相同的PROJECT_ID用逗号分隔拼成一个字符串,于是想到了oracle的listagg函数 表名为PM_BDST_PROJECT select tt.BDST_ID, l ...

  9. Spring中事务的传播行为,7种事务的传播行为,数据库事务的隔离级别

    Propagation.REQUIRED 代表当前方法支持当前的事务,且与调用者处于同一事务上下文中,回滚统一回滚(如果当前方法是被其他方法调用的时候,且调用者本身即有事务),如果没有事务,则自己新建 ...

  10. shell脚本编写之Hello World

    Linux下编写第一个Shell脚本 //创建一个.sh文件 vi demo1.sh //按i进入编辑模式 echo "Hello World !" //按esc→shift+:→ ...