数论 + 随机化

[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如何像awk一样分割字符串

    若你使用过 Shell 中的 awk 工具,会发现用它来分割字符串是非常方便的.特别是多个连续空格会被当做一个处理. [root@localhost ~]# cat demo.txt hello wo ...

  2. .NET周报【12月第3期 2022-12-23】

    由于众所周知的原因,大佬们纷纷加入羊群,笔者也未能幸免,体验下来这绝对不是普通感冒的症状,身体不适,熬了几天,所以本周更新比较晚:另外精力有限,对于国际板块只有链接没有简介,十分抱歉! 在这个艰难时刻 ...

  3. 1、mybatis逆向工程

    mybatis逆向工程可以针对单表自动生成mybatis执行所需要的mapper.java.mapper.xml代码(dao层),可以让程序员将更多的精力放在繁杂的业务逻辑上(service层与con ...

  4. [编程基础] Python格式化字符串常量f-string总结

    Python格式化字符串常量f-string总结 本文主要总结在Python中如何使用格式化字符串常量f-string(Formatted string literals).在 Python 程序中, ...

  5. tempdb大量闩锁等待问题分析

    背景 客户业务系统升级后,高峰期运行缓慢,在SQL专家云上看到数据库出现严重等待,需要分析原因并紧急处理. 现象 登录到SQL专家云中,进入实时可视化页面,在活动会话里面看到有大量资源等待的会话.   ...

  6. dfs 返回值用bool相对void会快一点

    力扣 剑指 Offer 12. 矩阵中的路径 超时代码 dfs返回值是void,用类内的全局变量flag表示找到或没找到. class Solution { public: bool flag; in ...

  7. 刷题笔记——3003.鸡兔同笼问题 & 2767.计算多项式的值

    题目1 3003.鸡兔同笼问题 代码 while True: try: x,y=map(int,input().strip().split()) a = int((4*x-y) / 2) b = x ...

  8. Windows 安装 Anaconda

    下载 anaconda 官网 https://repo.anaconda.com/archive/ 国内镜像 https://mirrors.bfsu.edu.cn/anaconda/archive/ ...

  9. 解决angular11+zorro使用table组件排序失效以及分页组件失效问题,附完整DEMO代码

    关于这个排序失效问题的核心点,跟这个有关系:[nzFrontPagination]="false" 关于分页组件失效的问题,是你获取数据以后,需要给页码,页数,总条数都要重新赋值, ...

  10. C#高性能数组拷贝实验

    前言 昨天 wc(Wyu_Cnk) 提了个问题 C# 里多维数组拷贝有没有什么比较优雅的写法? 这不是问对人了吗?正好我最近在搞图像处理,要和内存打交道,我一下就想到了在C#里面直接像C/C++一样做 ...