数论 + 随机化

[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. vue3+ts 全局事件总线mitt

    Mitt 在vue3中 $ on,$off 和 $once 实例方法已被移除,组件实例不再实现事件触发接口,因此大家熟悉的EventBus便无法使用了.然而我们习惯了使用EventBus,对于这种情况 ...

  2. [机器学习] t-SNE聚类算法实践指南

    ​  转载于比PCA降维更高级--(R/Python)t-SNE聚类算法实践指南-阿里云开发者社区 作者介绍:Saurabh.jaju2 Saurabh是一名数据科学家和软件工程师,熟练分析各种数据集 ...

  3. ffmpeg常用操作

    音频常用操作 常见的ffmpeg音频参数 常用参数解释: - i 表示input,即输入文件 - f 表示format,即输出格式 - vn 表示vedio not,即输出不包含视频 - ar 设定采 ...

  4. C++string与int的相互转换(使用C++11)

    一.int转string #include <iostream> #include <string> int main() { double f = 23.43; double ...

  5. 基于AS2协议的EDI 系统

    一款由JAVA语言开发的基于AS2 协议的EDI 轻量级系统 优点如下: 1.价格便宜.目前市场上一条gateway线路动辄数万,甚至数万/年. 2.功能强大.功能可以与主流EDI 软件媲美. 3.可 ...

  6. 【学习笔记】C++ 常量折叠原理和验证

    以下的代码很有意思,在相同时刻,相同的内存地址,数据居然会不一样. #include <iostream> int main(void) { const int const_val = 3 ...

  7. Spring异步Async和事务Transactional注解

    Spring开发中我们我们常常用到@Transaction和@Async,但这2个注解加在一起很多的开发者不敢用,担心事务不生效.下面我们就仔细讲解一下这2个注解同时运用,文章用3个场景讲述它们之间的 ...

  8. 理论+实践,教你如何使用Nginx实现限流

    摘要:Nginx作为一款高性能的Web代理和负载均衡服务器,往往会部署在一些互联网应用比较前置的位置.此时,我们就可以在Nginx上进行设置,对访问的IP地址和并发数进行相应的限制. 本文分享自华为云 ...

  9. 互斥锁、线程理论、GIL全局解释器、信号量、event事件、进程池和线程池以及协程

    目录 一.互斥锁代码实操 1.互斥锁的概念 2.互斥锁的使用 3.死锁现象 4. 小结 二.线程理论 进程 线程 线程简介 为什么要使用多线程? 多线程概念 多进程的优点: 线程与进程的区别 线程的特 ...

  10. C# 处理实体类赋值(获取嵌套类型,支持list 自定义类型)

    public static T AESEncrypt<T>(T obj) where T : class { if (obj == null) { return obj; } var pr ...