数论 + 随机化

[Problem - D2 - Codeforces](https://codeforces.com/contest/1749/problem/D)

题意

给定一个长度为 \(n\;(1<=n<=40,n为偶数)\) 的数组 \(a\), \(-1e6<=a[i]<=1e6\)

求最大的模数 \(k\), 使得 \(a[i]\) 在模意义下有至少一半个数相等,如果 k 可以无穷大则输出 -1

思路

  1. 一开始想暴力枚举值域来验证,妄想能过 \(1e8\) ,但是 TLE 了
  2. 其实本题的思路和之前有道 ABC 的题很相似,要保证一半的数在数组里面,那就随机两个位置 u,v,有 \(\frac 14\) 的概率 \(a[u],a[v]\) 在被选中的里面,随机几十次就几乎可以保证至少有一次随机中的 \(a[u],a[v]\) 都确实模意义下相等
  3. 如果 \(a[u]\equiv a[v] \pmod k\), 则 \(k\mid abs(a[u]-a[v])\), 枚举因数作为 \(k\) 即可
  4. 一次随机的复杂度为 \(\sqrt {值域}*n\)

代码

#include <bits/stdc++.h>
using namespace std;
#define endl "\n" typedef long long ll;
typedef pair<int, int> PII; const int N = 2e6 + 10;
int n;
int a[110], b[110];
int cnt[N]; bool check(int u, int mod)
{
int ans = 1;
int uu = (a[u] % mod + mod) % mod;
for (int i = 0; i < n; i++)
{
if (i == u)
continue;
int j = (a[i] % mod + mod) % mod;
if (uu == j)
ans++;
}
return ans >= n / 2;
}
int solve()
{
map<int, int> mp;
for (int i = 0; i < n; i++)
{
mp[a[i]]++;
if (mp[a[i]] >= n / 2)
return -1;
}
int t = 100;
int ans = 1;
while(t--)
{
int u = rand() % n, v;
while(1)
{
v = rand() % n;
if (u != v)
break;
}
int D = abs(a[u] - a[v]);
for (int d = 1; d <= D / d; d++)
{
if (D % d)
continue;
if (check(u, d))
ans = max(ans, d);
if (D / d != d && check(u, D / d))
ans = max(ans, D / d);
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--)
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
cout << solve() << endl;
}
return 0;
}

Codeforces Round #748 (Div. 3) - D2. Half of Same的更多相关文章

  1. Codeforces Round #748 (Div. 3)

    Codeforces Round #748 (Div. 3) A. Elections 思路分析: 令当前值比最大值大即可,如果最大值是它自己,就输出\(0\) 代码 #include <bit ...

  2. Codeforces Round #350 (Div. 2) D2. Magic Powder - 2

    题目链接: http://codeforces.com/contest/670/problem/D2 题解: 二分答案. #include<iostream> #include<cs ...

  3. Codeforces Round #527 (Div. 3) D2. Great Vova Wall (Version 2) 【思维】

    传送门:http://codeforces.com/contest/1092/problem/D2 D2. Great Vova Wall (Version 2) time limit per tes ...

  4. Codeforces Round #350 (Div. 2) D2 二分

    五一期间和然然打的团队赛..那时候用然然的号打一场掉一场...七出四..D1是个数据规模较小的题 写了一个暴力过了 面对数据如此大的D2无可奈何 现在回来看 一下子就知道解法了 二分就可以 二分能做多 ...

  5. Codeforces Round #594 (Div. 1) D2. The World Is Just a Programming Task (Hard Version) 括号序列 思维

    D2. The World Is Just a Programming Task (Hard Version) This is a harder version of the problem. In ...

  6. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题

    D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...

  7. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】

    一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...

  8. Codeforces Round #574 (Div. 2) D2. Submarine in the Rybinsk Sea (hard edition) 【计算贡献】

    一.题目 D2. Submarine in the Rybinsk Sea (hard edition) 二.分析 相比于简单版本,它的复杂地方在于对于不同长度,可能对每个点的贡献可能是有差异的. 但 ...

  9. Codeforces Round #738 (Div. 2) D2题解

    D2. Mocha and Diana (Hard Version) 至于D1,由于范围是1000,我们直接枚举所有的边,看看能不能加上去就行,复杂度是\(O(n^2logn)\).至于\(n\)到了 ...

  10. Codeforces Round #527 (Div. 3)D2(栈,思维)

    #include<bits/stdc++.h>using namespace std;int a[200007];stack<int>s;int main(){    int ...

随机推荐

  1. 【机器学习】李宏毅——Adversarial Attack(对抗攻击)

    研究这个方向的动机,是因为在将神经网络模型应用于实际场景时,它仅仅拥有较高的正确率是不够的,例如在异常检测中.垃圾邮件分类等等场景,那些负类样本也会想尽办法来"欺骗"模型,使模型无 ...

  2. git remote update origin --prune命令失败

    1.问题描述 我使用git remote update origin --prune命令更新远程分支上的代码,结果出现如下报错. 2.产生原因 本地关联的远程仓库失效了,需要重新再关联一下. 3.解决 ...

  3. 再聊一下那 SQLSERVER 行不能跨页的事

    一:背景 1. 讲故事 上一篇写完了之后,马上就有朋友留言对记录行的 8060byte 限制的疑惑,因为他的表记录存储了大量的文章,存储文章的字段类型用的是 nvarchar(max),长度很显然是超 ...

  4. [深度探索C++对象模型]关于成员初始化列表(member initiallization list)

    四种特殊情况: 1.当初始化一个reference member时. 2.当初始化一个const member时. 3.当初始化一个base class的constructor,而它拥有一组参数时. ...

  5. CodeForces 构造题专项解题报告

    CodeForces 构造题专项解题报告 \(\newcommand \m \mathbf\)\(\newcommand \oper \operatorname\) \(\text{By DaiRui ...

  6. (Java)设计模式:结构型

    前言 这篇博文续接的是 UML建模.设计原则.创建型设计模式.行为型设计模式,有兴趣的可以看一下 3.3.结构型 这些设计模式关注类和对象的组合.将类和对象组合在一起,从而形成更大的结构 * 3.3. ...

  7. 【力扣】nSum问题模板

    nSum问题模板 两数之和.三数之和.四数之和. private List<List<Integer>> nSum(int[] nums, long target, int s ...

  8. 真正“搞”懂HTTP协议09之这个饼干不能吃

    我们在之前的文章中介绍HTTP特性的时候聊过,HTTP是无状态的,每次聊起HTTP特性的时候,我都会回忆一下从前辉煌的日子,也就是互联网变革的初期,那时候其实HTTP不需要有状态,就是个浏览页面,没有 ...

  9. 二十三、图的遍历(BFS和DFS)

    一.概念   图的遍历(Traversing Graph)从某一顶点出发,访问图中所有顶点,且使每一顶点仅被访问一次.与树的遍历不同的是,图的遍历需要处理两种特殊情况:一是从某一顶点出发进行遍历时,可 ...

  10. DOM的操作(增删改查)

    操作DOM的核心就是增删改查 目录 一.节点创建型API 1.1 createElement 1.2 createTextNode 1.3 cloneNode 1.4 createDocumentFr ...