codeforces364D
Ghd
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
6
6 2 3 4 5 6
3
5
5 5 6 10 15
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的更多相关文章
- 2018.09.14 codeforces364D(随机化算法)
传送门 根据国家集训队2014论文集中胡泽聪的随机化算法可以通过这道题. 对于每个数,它有12" role="presentation" style="posi ...
随机推荐
- Image 对象事件
以前没怎么注意image上的事件 Image 对象事件 事件 描述 W3C onabort 当用户放弃图像的装载时调用的事件句柄. Yes onerror 在装载图像的过程中发生错误时调用的事件句柄. ...
- linux 下使用opengl的glut库显示和旋转BMP图片
效果图: 这里显示的图和原图有明显的色差,目前猜测是opengl渲染时的颜色表顺序跟BMP文件里的颜色表顺序相反导致. BMP里应该是BGRBGRBRG... ,而opengl渲染时应该是按照RGBR ...
- VBA精彩代码分享-2
VBA开发中经常需要提示消息框,如果不关闭程序就会暂时中断,这里分享下VBA如何实现消息框的自动关闭,总共有三种方法: 第一种方法 Public Declare Function MsgBoxTime ...
- IOS 点击按钮拨号
- (IBAction)OnTouch_bHotLine:(id)sender { [[UIApplication sharedApplication] openURL:[NSURL URLWithS ...
- 小程序 ----踩坑 ---安卓iOS兼容等
关于小程序一些小功能的代码都在这个GitHub上,感兴趣的可以去看看,https://github.com/huihuijiang/miniProgram目前有:列表左滑删除,拖拽浮标 一.小程序坑1 ...
- 微信小程序编译提示tabar.iconPath 文件不存在
tabBar.list[0].iconPath 文件不存在 明明是按路径放了本地图片的,却依然显示路径不存在 需要把路径的图片转移到编译后的weapp文件中相同路径下的img文件中 本地正常路径 粘贴 ...
- 使用LEANGOO泳道
转自:https://www.leangoo.com/leangoo_guide/leangoo_yongdao.html 列表使用纵向的纬度管理卡片,通常代表卡片的工作的不同阶段,或者任务的状态.泳 ...
- XGBOOST in WIN64 安装
参考:https://blog.csdn.net/zyghs/article/details/50897716 环境 platform:windows 10,64位 Python: Python3.7 ...
- 数学模块 math 函数的调用
数学模块 math 模块名: math 注: linux下为内建模块 Mac OS下为标准库模块 数学模块用法: import math # 或 from math import * 数据 描述 ma ...
- 学java编程软件开发,非计算机专业是否能学
近几年互联网的发展越来越好,在国外,java程序员已经成为高薪以及稳定职业的代表,虽然国内的有些程序员很苦逼,但是那只是少数,按照国外的大方向来看,程序员还是一个很吃香的职业.根据编程语言的流行程度, ...