Hash Perfectly

题目连接:

http://acm.uestc.edu.cn/#/problem/show/1314

Description

In computing, a hash table is a data structure used to implement an associative array, a structure that can map keys to values.

A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found. A common hash function is \(index=key\ \%\ array\\_size\) (\(\%\) is modulo operator), but it may cause some collisions.

For example, if keys are \(1,2,6,10\), and we choose \(array\\_size=4\), the indexes will be \(1,2,2,2\), where some collisions happen.

To solve the collision, we can use the method known as separate chaining with linked lists.

Seeing the example again, when we try to insert \(6\), because its index \(2\) is used, we build a linked list in index \(2\), and there would be \(2\rightarrow 6\) in index \(2\). Insert \(10\) next, there would be a linked list \(2\rightarrow 6\rightarrow 10\) in index 2.

To calculate the efficiency of the hash function, we define a value called \(ASL\) (Average search length):

\[ASL=\frac{1}{n}\sum_{i=1}^{n}c_i
\]

\(c_i\) is the number of times to compare when we search the \(i^{th}\) key.

Using the example above again, \(c_1=1,c_2=1,c_3=2,c_4=3\), so \(ASL=\frac{1}{4}(1+1+2+3)=1.75\).

It's obvious that \(ASL\) can minimize when we choose a sufficiently large \(array\\_size\), but in fact due to the limitation of memory, \(array\\_size\) must be no more than \(limit\), i.e., \(1\leq array\\_size\leq limit\).

Now you are given n keys, try to choose a proper \(array\\_size\) to minimize \(ASL\). If there are multiple answers, choose the smallest one.

Input

The first line contains two integers \(n\) and \(limit\).

The second line contains \(n\) integers, where \(i^{th}\) integer indicates the \(i^{th}\) key.

\(1\leq n, limit, key\leq 2*10^5\)

Output

Print the smallest \(array\\_size\) which can minimize \(ASL\).

Sample Input

4 4

1 2 6 10

Sample Output

3

Hint

题意

现在你有n个数,然后哈希是指b[i]=a[i]%k

现在让你找到一个合适的k,使得冲突的对数最少,这个k需满足0<=k<=limit

题解:

若一个位置冲突了k次,则对n*ASL的贡献是k*(k+1)/2,相当于k个数两两冲突的对数加k。

对于两个数a,b,他们只会在(a-b) % array_size == 0时冲突。

利用FFT,可把所有a-b的可能取值对应的个数算出来。对于每个array_size,算出a-b=array_size, a-b=2array_size, a-b=3array_size, …的个数,就可以直接得到ASL的值。

复杂度O(nlogn)

代码

#include<bits/stdc++.h>

using namespace std;

const int N = 600040;
const double pi = acos(-1.0); int len=1<<19; struct Complex
{
double r,i;
Complex(double r=0,double i=0):r(r),i(i) {};
Complex operator+(const Complex &rhs)
{
return Complex(r + rhs.r,i + rhs.i);
}
Complex operator-(const Complex &rhs)
{
return Complex(r - rhs.r,i - rhs.i);
}
Complex operator*(const Complex &rhs)
{
return Complex(r*rhs.r - i*rhs.i,i*rhs.r + r*rhs.i);
}
} va[N],vb[N]; void rader(Complex F[],int len) //len = 2^M,reverse F[i] with F[j] j为i二进制反转
{
int j = len >> 1;
for(int i = 1;i < len - 1;++i)
{
if(i < j) swap(F[i],F[j]); // reverse
int k = len>>1;
while(j>=k)
{
j -= k;
k >>= 1;
}
if(j < k) j += k;
}
} void FFT(Complex F[],int len,int t)
{
rader(F,len);
for(int h=2;h<=len;h<<=1)
{
Complex wn(cos(-t*2*pi/h),sin(-t*2*pi/h));
for(int j=0;j<len;j+=h)
{
Complex E(1,0); //旋转因子
for(int k=j;k<j+h/2;++k)
{
Complex u = F[k];
Complex v = E*F[k+h/2];
F[k] = u+v;
F[k+h/2] = u-v;
E=E*wn;
}
}
}
if(t==-1) //IDFT
for(int i=0;i<len;++i)
F[i].r/=len;
} void Conv(Complex a[],Complex b[],int len) //求卷积
{
FFT(a,len,1);
FFT(b,len,1);
for(int i=0;i<len;++i) a[i] = a[i]*b[i];
FFT(a,len,-1);
}
int n,limit;
int a[N];
long long num[N],sum[N];
void solve()
{
scanf("%d%d",&n,&limit);
int Mx = 0;
for(int i=0;i<n;i++)
{
int x;scanf("%d",&a[i]);
va[a[i]].r+=1;
vb[200000-a[i]].r+=1;
}
Conv(va,vb,len);
for(int i=0;i<=200000;i++)
num[i]=(long long)(va[200000+i].r+0.5);
long long ans1=1e18,ans2=0;
for(int i=1;i<=limit;i++)
{
long long cnt = 0;
for(int j=i;j<=len;j+=i)
cnt+=num[j];
if(cnt<ans1)
{
ans1=cnt;
ans2=i;
}
}
cout<<ans2<<endl;
}
int main()
{
solve();
return 0;
}

最新文章

  1. java求字符串数组交集、并集和差集
  2. django关系对象映射(Object Relational Mapping,简称ORM)
  3. UVa 11076 (有重元素的排列) Add Again
  4. 检测SqlServer服务器内存是否瓶颈
  5. linux 下面 opcache 拓展
  6. iOS中XMPP简单聊天实现 好友和聊天
  7. js大文件分割上传
  8. JavaScript prototype.js提升JavaScript开发效率
  9. java亿级流量电商详情页系统的大型高并发与高可用缓存架构实战视频教程
  10. Maven项目pom.xml文件报xxx\target\classes\META-INF\MANIFEST.MF (系统找不到指定的路径)
  11. PowerBI/Excel - PowerQuery数据转换系列 - 如何将多行的值串联到一行 - 行列转换
  12. U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column
  13. linux终端使用ss代理
  14. 自学Linux Shell14.1-理解输入输出
  15. A Simple Note on &quot;P4FPGA: A Rapid Prototyping Framework for P4&quot;
  16. BCGcontrolBar(三) 添加表格(Grid)组件
  17. nginx代理服务器3--高可用(keepalived)
  18. WinRAR破解
  19. C#中遍历Hashtable的4种方法
  20. 关于pthread_cond_wait()使用的理解

热门文章

  1. react native系列 - 从零开始构建
  2. 005_linux下logrotate 配置和理解
  3. oracle只要第一条数据SQL
  4. docker简单介绍(资料收集总结)
  5. 网页转图片,html生成图片,网页生成图片(基于linnux+phantomjs)和wkhtmltoimage
  6. mysql一个字符问题
  7. 【转载】移动开发中的上下左右滑动插件jquery.swipe.js
  8. MySQL5.7 centos7.2 yum 安装
  9. Linux文件系统的详解
  10. 【原创】Scrapyd 的 .net 客户端