数论 + 随机化

[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. python 实现DES加解密

    from pyDes import * import base64 class Des3(object): def __init__(self, key, iv): # 这里密钥key长度必须为16/ ...

  2. [数学理论] NP问题解释

    转载自http://m.blog.csdn.net/csshuke/article/details/74909562 希望通过这篇文章可以不仅让计算机相关专业的人可以看懂和区分什么是P类问题什么是NP ...

  3. [seaborn] seaborn学习笔记12-绘图实例(4) Drawing example(4)

    文章目录 12 绘图实例(4) Drawing example(4) 1. Scatterplot with varying point sizes and hues(relplot) 2. Scat ...

  4. 史上最简单 OpenCV for C++ 在 Windows 和 Ubuntu 上编译安装使用教程

    准备工作 原材料 Ubuntu 系统(非必须,Windows 也可以,主要是 Ubuntu 适合编译) OpenCV 3.4.1 压缩包 OpenCV contrib 3.4.1 压缩包 MinGW( ...

  5. Java基础1-1-5—java基础语法(idea开发工具)

    5. 开发工具 5.1 idea中项目结构 project(项目.工程) module(模块) package(包) class(类) 一个项目中可以存在多个模块多个模块中,存放项目不同的业务功能代码 ...

  6. elasticsearch中使用runtime fields

    1.背景 在我们使用es的开发过程中可能会遇到这么一种情况,比如我们的线路名称字段lineName字段在设置mapping的时候使用的是text类型,但是后期发现需要使用这个字段来进行聚合操作,那么我 ...

  7. 线程基础知识12-AQS

    转:https://tech.meituan.com/2019/12/05/aqs-theory-and-apply.html 1 简介 AQS,全称AbstractQueuedSynchronize ...

  8. springcloud04 Ribbbon

    Ribbbon虽然现在已经进入维护模式了,但是使用的还是很多的. 1.官网 https://github.com/Netflix/ribbon/wiki/Getting-Started 2.Ribbo ...

  9. StartAllBack使用教程

    StartAllBack简介 StartAllBack是一款Win11开始菜单增强工具,为Windows11恢复经典样式的Windows7主题风格开始菜单和任务栏,功能包括:自定义开始菜单样式和操作, ...

  10. .NET周报 【2月第2期 2023-02-11】

    国内文章 SQLSERVER的truncate和delete有区别吗? https://mp.weixin.qq.com/s/wTIeW8rjj3cRzoaQcg2sOw 在面试中我相信有很多朋友会被 ...