由裴蜀定理,最后结果为所有数最大公因数,所以预处理公因数然后看最大的次数>=k的数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
char ch=getchar();int f=0;
while(ch<'0'||ch>'9')ch=getchar();
while(ch>='0'&&ch<='9'){f=(f<<1)+(f<<3)+ch-'0';ch=getchar();}
return f;
}
int n,v[2000005],k,tot,now;
int main()
{
n=read();k=read();
for(int i=1;i<=n;i++)
{
int x=read();
for(int j=1;j*j<=x;j++)
{
if(j*j==x)
{
v[++tot]=j;
break;
}
if(x%j==0)
v[++tot]=j,v[++tot]=x/j;
}
}
sort(v+1,v+tot+1);
for(int i=tot;i;i--)
{
if(v[i+1]!=v[i])
{
now=0;
}
now++;
if(now==k)
{
printf("%d\n",v[i]);
return 0;
}
}
}