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 ...
随机推荐
- Zynq-7000 MiZ701 SOC硬件使用手册
一.整体概述 4 二.应用领域及人群 4 三.硬件配置 4 BANK资源分配 6 四.MiZ701开发板功能描述 7 4.1 全编程SOC(All Programmable SoC) 7 4.2 内存 ...
- 为什么我们需要Pod?(容器设计模式sidecar)
Pod,是 Kubernetes 项目中最小的 API 对象 容器的本质是进程,就是未来云计算系统中的进程:容器镜像就是这个系统里的".exe"安装包 Kubernetes 就是操 ...
- .Net C# EF database first connectionstring
<connectionStrings> <add name="CupCreditCheckDB" connectionString="metadata= ...
- 逆向知识第九讲,switch case语句在汇编中表达的方式
一丶Switch Case语句在汇编中的第一种表达方式 (引导性跳转表) 第一种表达方式生成条件: case 个数偏少,那么汇编中将会生成引导性的跳转表,会做出 if else的情况(类似,但还是能分 ...
- 判断ubuntu是32位还是64位
在终端输入 sudo uname -m 显示 如果显示i686,你安装了32位操作系统 如果显示 x86_64,你安装了64位操作系统 更多: sudo uname -s 显示内核名字s sudo u ...
- Ubuntu18.04安装MySQL与默认编码设置
安装 打开终端直接开始,编码配置方法在后面 #通过apt更新包索引 sudo apt update #按照默认软件包安装 sudo apt install mysql-server #运行安全脚本 s ...
- easyUi的组合表格
公司之前的项目是用easyui写的里面还混搭着php...把分支下来,有点蒙.晚上回来恶补一下吧,今天渲染这个表格,我开始自己写假数据,然后用ajax操作再使用 obj.datagrid('loadD ...
- kubernetes之configmap
生成容器内的环境变量 1.创建一个名字叫nginx-config的configmap, 变量名nginx_port的值是80, 变量名server_name的值是www.test.com kubect ...
- HDU 4085 斯坦纳树+DP
https://cn.vjudge.net/problem/HDU-4085 给你n,m,k ,分别表示有n个点,m条边,每条边有一个权值,表示修复这条边需要的代价 从前k个点中任取一个使其和后k个点 ...
- Jmeter - 命令行参数
同步更新至个人博客:https://njlife.top/2019/07/12/Jmeter-%E5%91%BD%E4%BB%A4%E8%A1%8C%E5%8F%82%E6%95%B0/ Jmeter ...