[HDU7073] Integers Have Friends 2.0 -随机大法好

简介: 题意:找到最大的一个集合,使得集合内所有元素 % m(>=2)问最大的集合大小

bd68efdb239d45c68002f0af22072afb.png


output


2
3
4


题意:


找到最大的一个集合,使得集合内所有元素 % m(>=2)

问最大的集合大小


对于第一组来讲:

可以选择m == 2 or 3

对于第二组来讲:

可以选择m == 5


在我们取m == 2的情况下,答案为 ⌈ n 2 ⌉ \lceil \frac{n}{2} \rceil⌈

2

n


选择两个位置,这两个数的位置均在答案中的可能性至少为1 4 \frac{1}{4}

4

1

,反之可能性为3 4 \frac{3}{4}

4

3

假如重复取30次,则:


3b3e10df04114e758c03af7bfb58436b.png


可以看作为0

所以方法是可靠的

选定了两个位置p1,p2之后,m可以取diff = abs(a[p1]-a[p2])的质因子

我们枚举diff的因子来当m进行操作,过程中一直取max即可


Code:


ll n,a[maxn];
ll cal(ll x,ll y) {
  ll ret = 0;
  for(int i=1; i<=n; i++) {
    if(a[i] % x == y) ret ++;
  }
  return ret;
}
int main() {
  srand(time(NULL));
  int _ = read;
  while(_ --) {
    n = read;
    for(int i=1; i<=n; i++) a[i] = read;
    ll ans = 1;
    for(int I = 1; I<=30; I++) {
      int p1 = rand() % n + 1;
      int p2 = rand() % n + 1;
      if(p1 == p2) continue;
      ll diff = abs(a[p1] - a[p2]);
      for(ll i=2; 1LL * i * i <= diff; i ++) {
        if(diff % i == 0) {
          while(diff % i == 0) diff /= i;
          ans = max(ans,cal(i,a[p1]%i));
        }
      }
      if(diff > 1) ans = max(ans,cal(diff,a[p1] % diff));
    }
    printf("%lld\n",ans);
  }
  return 0;
}
/**
3
3
10 12 15
4
4 6 9 19
6
2 8 11 15 19 38
**/





目录
相关文章
|
8月前
|
机器学习/深度学习 人工智能 BI
The Great Hero(Codeforces Round #700 (Div. 2))模拟+贪心思想和排序
The Great Hero(Codeforces Round #700 (Div. 2))模拟+贪心思想和排序
35 0
|
9月前
|
网络架构
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
|
网络架构
Codeforces Round #755 D. Guess the Permutation(交互 二分)
Codeforces Round #755 D. Guess the Permutation(交互 二分)
70 0
|
人工智能 Java
[HDU 7136] Jumping Monkey | 并查集 | 逆向思维
Jumping Monkey Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 747 Accepted Submission(s): 360
187 0
[HDU 7136] Jumping Monkey | 并查集 | 逆向思维
UVa 11292 - Dragon of Loowater(排序贪心)
UVa 11292 - Dragon of Loowater(排序贪心)
74 0
|
人工智能
AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)
A. Diversity time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output ...
1118 0