#abc272g. G - Yet Another mod M

G - Yet Another mod M

Score : 600600 points

问题描述

给定一个由正整数组成的长度为 NN 的序列 A=(A1,A2,,AN)A=(A_1,A_2,\dots,A_N),其中 AA 中的元素各不相同。

你需要在 3310910^9(包含)之间选择一个正整数 MM 来执行以下操作一次:

  • 对于满足 1iN1 \le i \le N 的每个整数 ii,将 AiA_i 替换为 AimodMA_i \bmod M

能否选择一个 MM,使得操作后 AA 满足以下条件?如果可以,请找出这样的 MM

  • 存在一个整数 xx,它在 AA 中是多数。

这里,若整数 xxAA 中满足其等于 AiA_i 的整数个数大于不等于 AiA_i 的整数个数,则称 xxAA 中的多数。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given a sequence A=(A1,A2,,AN)A=(A_1,A_2,\dots,A_N) of length NN consisting of positive integers, where the elements of AA are distinct.

You will choose a positive integer MM between 33 and 10910^9 (inclusive) to perform the following operation once:

  • For each integer ii such that 1iN1 \le i \le N, replace AiA_i with AimodMA_i \bmod M.

Can you choose an MM so that AA satisfies the following condition after the operation? If you can, find such an MM.

  • There exists an integer xx such that xx is the majority in AA.

Here, an integer xx is said to be the majority in AA if the number of integers ii such that Ai=xA_i = x is greater than the number of integers ii such that AixA_i \neq x.

Constraints

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

Input

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

NN

A1A_1 A2A_2 \dots ANA_N

Output

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

Sample Input 1

5
3 17 8 14 10

Sample Output 1

7

If you let M=7M=7 to perform the operation, you will have A=(3,3,1,0,3)A=(3,3,1,0,3), where 33 is the majority in AA, so M=7M=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

update @ 2024/3/10 11:28:48