problem
有 2 n 2n 2n 个盲盒,每个盲盒有一个惊喜值 a i a_i ai。
打开恰好 n n n 个盲盒,获得的惊喜值为这些盲盒惊喜值的最大公约数。
求能获得的最大惊喜值。
n ≤ 1 e 5 , a i ≤ 1 e 12 n\le 1e5,a_i\le 1e12 n≤1e5,ai≤1e12。
solution
我是真的讨厌这种随机的正解,没什么就是想cao
有一个性质:如果随机一个盲盒,那么它被打开的概率为 1 2 \frac{1}{2} 21。
即,这个盲盒的某个因子是最后答案的概率为 1 2 \frac{1}{2} 21。
还有一个性质:通过打表发现,在 1 e 12 1e12 1e12 内因子个数最多只有 6720 6720 6720 个。
所以,只需要随机盲盒,然后暴力判断其每个因子是否有被 ≥ n \ge n ≥n 个盲盒含有。
最后为了正确性,就多随机几个数。
显然正确性是跟随机次数挂钩的,随机 x x x 次,那么出错的概率就是这 x x x 次的数都不是被选择盲盒,概率为 ( 1 2 ) x (\frac{1}{2})^x (21)x。
code
#include <ctime>
#include <cstdio>
#include <random>
#include <algorithm>
using namespace std;
#define maxn 200005
#define int long long
int n, ans;
int a[maxn], f[maxn];
void check( int x ) {
int cnt = 0;
for( int i = 1;i * i <= x;i ++ )
if( x % i == 0 ) {
if( i > ans ) f[++ cnt] = i;
if( x / i > ans ) f[++ cnt] = x / i;
}
for( int i = 1;i <= cnt;i ++ )
if( f[i] > ans ) {
for( int j = 1, tot = 0;j <= n;j ++ )
if( a[j] % f[i] == 0 ) {
tot ++;
if( tot >= ( n >> 1 ) ) {
ans = f[i];
break;
}
}
}
}
signed main() {
scanf( "%lld", &n ); n <<= 1;
for( int i = 1;i <= n;i ++ ) scanf( "%lld", &a[i] );
mt19937 wwl( time( 0 ) );
uniform_int_distribution < int > range( 1, n );
for( int i = 1;i <= 20;i ++ ) {
int x = range( wwl );
check( a[x] );
}
printf( "%lld\n", ans );
return 0;
}