Problem Statement

You are given a sequence $A=(A_1,A_2,\dots,A_N)$ of length $N$ consisting of positive integers, where the elements of $A$ are distinct.

You will choose a positive integer $M$ between $3$ and $10^9$ (inclusive) to perform the following operation once:

  • For each integer $i$ such that $1 \le i \le N$, replace $A_i$ with $A_i \bmod M$.

Can you choose an $M$ so that $A$ satisfies the following condition after the operation? If you can, find such an $M$.

  • There exists an integer $x$ such that $x$ is the majority in $A$.

Here, an integer $x$ is said to be the majority in $A$ if the number of integers $i$ such that $A_i = x$ is greater than the number of integers $i$ such that $A_i \neq x$.

Constraints

  • $3 \le N \le 5000$
  • $1 \le A_i \le 10^9$
  • The elements of $A$ are distinct.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

$N$
$A_1$ $A_2$ $\dots$ $A_N$

Output

If there exists an $M$ that satisfies the condition, print such an $M$. Otherwise, print $-1$.


Sample Input 1

5
3 17 8 14 10

Sample Output 1

7

If you let $M=7$ to perform the operation, you will have $A=(3,3,1,0,3)$, where $3$ is the majority in $A$, so $M=7$ satisfies the condition.


Sample Input 2

10
822848257 553915718 220834133 692082894 567771297 176423255 25919724 849988238 85134228 235637759

Sample Output 2

37

Sample Input 3

10
1 2 3 4 5 6 7 8 9 10

Sample Output 3

-1

首先如果现在就给出一些模某个数同余的数,怎么知道模哪个数同余?设这些数是 \(a_1,a_2\cdots a_k\),那么两个同余的数一减,一定模的那个数的倍数。所以这些数模 \(\gcd(|a_2-a_1|,|a_3-a_2|,\cdots ,|a_k-a_{k-1}|)\) 一定是同余的,看这个数是否大于2即可。

第二个引理,如果某个数合法,那么他的因数一定合法。这应该是易得的。所以后面的讨论中,我们可以只讨论质数(4也要特殊讨论,由于2被排除在范围外)。

首先对于所有小于等于 \(\sqrt{W}\)(\(W\) 是值域) 的质数,我们可以先跑一次。这里的复杂度不会超过 \(O(\frac{n\sqrt{W}}{\log n})\)。

对于大于 \(\sqrt{W}\) 的质数,应该注意到,如果他合法,那么他选出来的子集中每两个数之差一定都是他的因数。由于选的时候选了超过 \(\lceil\frac n2\rceil\) 个数,所以一定选了原序列中的相邻两个数(除非 \(n\) 为奇数然后跳着选,这个特判一下就好了)。大于 \(\sqrt{W}\) 的质数,在某个数中的所有质因数中至多出现一个。枚举序列的相邻两个数,找到他们的差中那个大于 \(\sqrt{W}\) 的质因数(如果存在的话),然后跑一次看是否合法。分解质因数这里由于我们已经筛出来了小于等于 \(\sqrt{W}\) 的所有质因数,所以可以降到 \(O(\frac{\sqrt{W}}{\log n})\)。注意这里取模加计数要用到哈希表。总复杂度 \(O(\frac{n\sqrt{W}}{\log n})\)

#include<bits/stdc++.h>
using namespace std;
const int N=5005,M=40000,P=5521;
int n,a[N],mx,pri[M],c,p[M],m;
struct hashmap{
int cnt[P],hd[P],idx,nxt[P],val[P];
int insert(int x)
{
if(!hd[x%P])
{
hd[x%P]=++idx;
val[idx]=x;
return ++cnt[idx];
}
for(int i=hd[x%P];i;i=nxt[i])
{
if(val[i]==x)
{
cnt[i]++;
return cnt[i];
}
else if(!nxt[i])
{
nxt[i]=++idx;
val[idx]=x;
cnt[idx]=1;
return 1;
}
}
}
void clear()
{
memset(hd,idx=0,sizeof(hd));
memset(nxt,0,sizeof(nxt));
memset(cnt,0,sizeof(cnt));
}
}cnt;
void check(int i)
{
cnt.clear();
mx=0;
for(int j=1;j<=n;j++)
{
int k=cnt.insert(a[j]%i);
// printf("%d ",a[j]%i);
if(k>mx)
mx=k;
}
// printf("%d\n",mx);
if(mx>n/2)
{
printf("%d\n",i);
exit(0);
}
}
void maxdiv(int x)
{
for(int i=1;i<=m;i++)
while(x%p[i]==0)
x/=p[i];
if(x!=1)
check(x);
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
sort(a+1,a+n+1);
for(int i=2;i<=32000;i++)
{
if(!pri[i])
{
p[++m]=i;
for(int j=i;j*i<=32000;j++)
pri[j*i]=1;
}
}
check(4);
for(int i=3;i<=32000;i++)
if(!pri[i])
check(i);
maxdiv(a[3]-a[1]);
for(int i=1;i<=n;i++)
maxdiv(a[i+1]-a[i]);
puts("-1");
}

[ABC272G] Yet Another mod M的更多相关文章

  1. 函数mod(a,m)

    Matlab中的函数mod(a,m)的作用: 取余数 例如: mod(25,5)=0; mod(25,10)=5; 仅此.

  2. ORACLE 数据库 MOD 函数用法

    1.求2和1的余数. Select mod(2,1) from dual: 2能被1整除所以余数为0. 2.MOD(x,y)返回X除以Y的余数.如果Y是0,则返回X的值. Select mod(2,0 ...

  3. 黑科技项目:英雄无敌III Mod <<Fallen Angel>>介绍

    英雄无敌三简介(Heroes of Might and Magic III) 英3是1999年由New World Computing在Windows平台上开发的回合制策略魔幻游戏,其出版商是3DO. ...

  4. [日常训练]mod

    Description 给定$p_1,p_2,-,p_n,b_1,b_2,...,b_m$, 求满足$x\;mod\;p_1\;\equiv\;a_1,x\;mod\;p_2\;\equiv\;a_2 ...

  5. Apache Mod/Filter Development

    catalog . 引言 . windows下开发apache模块 . mod进阶: 接收客户端数据的 echo 模块 . mod进阶: 可配置的 echo 模块 . mod进阶: 过滤器 0. 引言 ...

  6. FZU 1752 A^B mod C(快速加、快速幂)

    题目链接: 传送门 A^B mod C Time Limit: 1000MS     Memory Limit: 65536K 思路 快速加和快速幂同时运用,在快速加的时候由于取模耗费不少时间TLE了 ...

  7. HDOJ 4389 X mod f(x)

    数位DP........ X mod f(x) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  8. hdu.1104.Remainder(mod && ‘%’ 的区别 && 数论(k*m))

    Remainder Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  9. 对于一个负数mod正数

    鸟神说.. a/b靠零取整 然后呢..a%b定义成a-(a/b)*b c语言就是这么算的... 那么python2.6是怎么算的呢 如果最后你取模想得到一个正数.. 那么在上述取模定义不变的情况下 p ...

  10. 51Nod 1046 A^B Mod C Label:快速幂

    给出3个正整数A B C,求A^B Mod C.   例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^ ...

随机推荐

  1. 小红书获得小红书笔记详情 API 返回值说明

    ​ item_get_video-获得小红书笔记详情  注册开通 smallredbook.item_get_video 公共参数 名称 类型 必须 描述 key String 是 调用key(必须以 ...

  2. .NET开源最全的第三方登录整合库 - CollectiveOAuth

    前言 我相信很多同学都对接过各种各样的第三方平台的登录授权获取用户信息(如:微信登录.支付宝登录.GitHub登录等等).今天给大家推荐一个.NET开源最全的第三方登录整合库:CollectiveOA ...

  3. SQL注入——搜索型

    SQL注入-搜索型 搜索型注入-原理介绍 一些网站为了方便用户查找网站的资源,都对用户提供了搜索的功能,因为是搜索功能,往往是程序员在编写代码时都忽略了对其变量(参数)的过滤,而且这样的漏洞在国内的系 ...

  4. IOS苹果应用IPA一键签名工具(苹果重签名,企业签名,Windows平台,时间控制)

    苹果应用IPA一键签名工具可以在windows平台对苹果应用IPA文件重新签名,无需MAC苹果电脑和配置XCODE开发环境,便可以直接对IPA文件进行签名,同时支持修改BundleID, 不受描述文件 ...

  5. Content Security Policy(CSP)应用及说明

    什么是CSP CSP全称Content Security Policy ,可以直接翻译为内容安全策略,说白了,就是为了页面内容安全而制定的一系列防护策略. 通过CSP所约束的的规责指定可信的内容来源( ...

  6. Teamcener AWC Solr链接被拒

    1.检查安装Solr安装情况 2.在tem上勾选添加 安装完成后,总共有2个文件夹需要注意,一个 solr-版本 的文件夹,一个 TcFTSindexer 的文件夹 如果是solr安装的是服务,则不需 ...

  7. Redis——Redis面试题

    文章目录 概述 什么是Redis Redis有哪些优缺点 为什么要用 Redis /为什么要用缓存 为什么要用 Redis 而不用 map/guava 做缓存? Redis为什么这么快 数据类型 Re ...

  8. DRTREE - Dynamically-Rooted Tree 题解

    DRTREE - Dynamically-Rooted Tree 本题建议评蓝. 思路: 题目就是要对一颗不定根树求子树权值和. 这题不带修,如果带修难度会增加一点,就跟 遥远的国度 差不多. 首先分 ...

  9. Sum of MSLCM 题解

    Sum of MSLCM 题目大意 定义 \(\text{MSLCM}(n)\) 为所有满足该数集的 \(\text{lcm}\) 为 \(n\) 的数集中元素个数最多的数集的所有数字的和,现有多次询 ...

  10. DASCTF X CBCTF 2023|无畏者先行(Misc WP)

    justpaint 1.题目信息 FLAG被我弄丢了>_<不过,JBN应该记得,或许你能从他那得到一些线索. 附件是压缩包有密码.. 2.解题方法 暴力破解压缩包,测试长度为6,选择所有数 ...