B. Make Almost Equal With Mod

xi - Solar Storm

You are given an array a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an of distinct positive integers. You have to do the following operation exactly once:

  • choose a positive integer k k k;
  • for each i i i from 1 1 1 to n n n, replace a i a_i ai with a i  mod  k † a_i \text{ mod } k^\dagger ai mod k.

Find a value of k k k such that 1 ≤ k ≤ 1 0 18 1 \leq k \leq 10^{18} 1k1018 and the array a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an contains exactly 2 2 2 distinct values at the end of the operation. It can be shown that, under the constraints of the problem, at least one such k k k always exists. If there are multiple solutions, you can print any of them.

† ^\dagger a  mod  b a \text{ mod } b a mod b denotes the remainder after dividing a a a by b b b. For example:

  • 7  mod  3 = 1 7 \text{ mod } 3=1 7 mod 3=1 since 7 = 3 ⋅ 2 + 1 7 = 3 \cdot 2 + 1 7=32+1
  • 15  mod  4 = 3 15 \text{ mod } 4=3 15 mod 4=3 since 15 = 4 ⋅ 3 + 3 15 = 4 \cdot 3 + 3 15=43+3
  • 21  mod  1 = 0 21 \text{ mod } 1=0 21 mod 1=0 since 7 = 7 ⋅ 1 + 0 7 = 7 \cdot 1 + 0 7=71+0
  • Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 500 1 \le t \le 500 1t500). The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 100 2 \le n \le 100 2n100) — the length of the array a a a.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 17 1 \le a_i \le 10^{17} 1ai1017) — the initial state of the array. It is guaranteed that all the a i a_i ai are distinct.

Note that there are no constraints on the sum of n n n over all test cases.
Output

For each test case, output a single integer: a value of k k k ( 1 ≤ k ≤ 1 0 18 1 \leq k \leq 10^{18} 1k1018) such that the array a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an contains exactly 2 2 2 distinct values at the end of the operation.

#include<bits/stdc++.h>
using namespace std;
const int N = 110;
typedef long long LL;
LL f[N];
int n;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		cin >> n;
		for (int i = 0; i < n; i++) cin >> f[i];
		LL k = 1;
		while (1)
		{
			set<LL> s;
			for (int i = 0; i < n; i++)s.insert(f[i] % k);
			if (s.size() == 2)
			{
				cout << k << endl;
				break;
			}
			k *= 2;
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值