Codeforces Round #748 (Div. 3) - D2. Half of Same
数论 + 随机化
[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
思路
- 一开始想暴力枚举值域来验证,妄想能过 \(1e8\) ,但是 TLE 了
- 其实本题的思路和之前有道 ABC 的题很相似,要保证一半的数在数组里面,那就随机两个位置 u,v,有 \(\frac 14\) 的概率 \(a[u],a[v]\) 在被选中的里面,随机几十次就几乎可以保证至少有一次随机中的 \(a[u],a[v]\) 都确实模意义下相等
- 如果 \(a[u]\equiv a[v] \pmod k\), 则 \(k\mid abs(a[u]-a[v])\), 枚举因数作为 \(k\) 即可
- 一次随机的复杂度为 \(\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的更多相关文章
- Codeforces Round #748 (Div. 3)
Codeforces Round #748 (Div. 3) A. Elections 思路分析: 令当前值比最大值大即可,如果最大值是它自己,就输出\(0\) 代码 #include <bit ...
- Codeforces Round #350 (Div. 2) D2. Magic Powder - 2
题目链接: http://codeforces.com/contest/670/problem/D2 题解: 二分答案. #include<iostream> #include<cs ...
- 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 ...
- Codeforces Round #350 (Div. 2) D2 二分
五一期间和然然打的团队赛..那时候用然然的号打一场掉一场...七出四..D1是个数据规模较小的题 写了一个暴力过了 面对数据如此大的D2无可奈何 现在回来看 一下子就知道解法了 二分就可以 二分能做多 ...
- 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 ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 水题
D2. RGB Substring (hard version) inputstandard input outputstandard output The only difference betwe ...
- Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】
一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...
- Codeforces Round #574 (Div. 2) D2. Submarine in the Rybinsk Sea (hard edition) 【计算贡献】
一.题目 D2. Submarine in the Rybinsk Sea (hard edition) 二.分析 相比于简单版本,它的复杂地方在于对于不同长度,可能对每个点的贡献可能是有差异的. 但 ...
- Codeforces Round #738 (Div. 2) D2题解
D2. Mocha and Diana (Hard Version) 至于D1,由于范围是1000,我们直接枚举所有的边,看看能不能加上去就行,复杂度是\(O(n^2logn)\).至于\(n\)到了 ...
- Codeforces Round #527 (Div. 3)D2(栈,思维)
#include<bits/stdc++.h>using namespace std;int a[200007];stack<int>s;int main(){ int ...
随机推荐
- SSM基础学习笔记
SSM框架: 1.界面层---servlet类---SpringMVC 2.业务逻辑层---service类---Spring 3.数据访问层(持久层)---dao类---MyBatis 一.MyBa ...
- [OpenCV实战]15 基于深度学习的目标跟踪算法GOTURN
目录 1 什么是对象跟踪和GOTURN 2 在OpenCV中使用GOTURN 3 GOTURN优缺点 4 参考 在这篇文章中,我们将学习一种基于深度学习的目标跟踪算法GOTURN.GOTURN在Caf ...
- Java学习笔记 :2021年12月31日 上午
Java学习笔记 :2021年12月31日 上午 目录 Java学习笔记 :2021年12月31日 上午 关于计算机语言 1.关于语言的分类 2.小结 关于Java语言的基础语法 1.主方法 2.其他 ...
- 关于VBA中,activesheet用法的一些思考
前二天,给财务部做了个数据采集的工具,因为财务现在用的是excel2013 和2017的版本,所以我决定不用python,改用VBA来处理这个工具. 在 写过程的时候,我用了sheets(i)来定位表 ...
- day15-声明式事务
声明式事务 1.事务分类 编程式事务 Connection connection = JdbcUtils.getConnection(); try{ //1.先设置事务不要提交 connection. ...
- 微软外服札记④——Spark中的那些坑...
Spark中的那些坑 Spark中的那些坑 前言 读取配置文件 时区陷阱 怪异的DayOfWeek substring陷阱 IP地址解析 枚举的数值 posexplode函数 为什么我的程序运行那么慢 ...
- 三种循环的区别-break语句
三种循环的区别 循环语句的区别 for 和 while 的小区别: 控制条件语句所控制的那个变量,在for循环结束后,就不能再被访问到了,而while循环结束还可以继 续使用,如果你想继续使用,就用w ...
- JDK8 四大核心函数式接口及扩展接口总结
前言 Java8的四大函数式接口及相关的扩展接口在日常使用中的频率也是非常多的,包括自己定义的函数式接口,在JDK1.8之前,我们定义的方法都是用来接收参数,然后自己根据参数传递实现逻辑.在1.8之后 ...
- Java8Stream流2
上期对stream流大致总结了一下,后面又做了一些练习,大家可以参考一下. 首先需要建一个 Product的实体类,后面会用到 @Data @AllArgsConstructor @NoArgsCon ...
- MySQL-数据库、MySQL
1.存取数据的演变史 1.文本文件: 文件路径不固定(导致代码兼容性下降) 数据格式不统一:(max|123, max_123) 2.软件开发目录规范: 1.规定了数据应该保存在db目录下>&g ...