B. Lunatic Never Content

You have an array aa of nn non-negative integers. Let's define f(a,x)=[a1modx,a2modx,…,anmodx]f(a,x)=[a1modx,a2modx,…,anmodx] for some positive integer xx. Find the biggest xx, such that f(a,x)f(a,x) is a palindrome.

Here, amodxamodx is the remainder of the integer division of aa by xx.

An array is a palindrome if it reads the same backward as forward. More formally, an array aa of length nn is a palindrome if for every ii (1≤i≤n1≤i≤n) ai=an−i+1ai=an−i+1.

Input

The first line contains a single integer tt (1≤t≤1051≤t≤105) — the number of test cases.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105).

The second line of each test case contains nn integers aiai (0≤ai≤1090≤ai≤109).

It's guaranteed that the sum of all nn does not exceed 105105.

Output

For each test case output the biggest xx, such that f(a,x)f(a,x) is a palindrome. If xx can be infinitely large, output 0 instead.

Example

input

4

2

1 2

8

3 0 1 2 0 3 2 1

1

0

3

100 1 1000000000

output

1
2
0
999999900

Note

In the first example, f(a,x=1)=[0,0]f(a,x=1)=[0,0] which is a palindrome.

In the second example, f(a,x=2)=[1,0,1,0,0,1,0,1]f(a,x=2)=[1,0,1,0,0,1,0,1] which is a palindrome.

It can be proven that in the first two examples, no larger xx satisfies the condition.

In the third example, f(a,x)=[0]f(a,x)=[0] for any xx, so we can choose it infinitely large, so the answer is 00.

思路:

由于x要尽可能的大那么先把每个两两对应的数尽可能大的x求出来

就是abs(a[i]-a[n-i-1])

为什么是这样的呢

令q=abs(a[i]-a[n-i-1])

假设a[n-i-1]>a[i]

a[i]=q*k+r 其中k是倍数r是余数

a[n-i-1]=a[i]+q

=q*(k+1)+r

两两余数相同

这应该是最大的x了实在想不到办法证明还有比他大的

然后没对的最大x都求出来的 又由于需要一个数y使其中每个数与其都成倍数关系 即x=ky

就是求一遍gcd(最大公约数)

补充一下 x可以去任意数的话a[i]-a[n-i-1]必须是0

然后一个数与0求gcd返回的是这个数

#include<iostream>
#include<algorithm>
#include<cstring>//find("string"),rfind("string")
#include<string>//to_string(value)
#include<cstdio>
#include<cmath>
#include<vector>//res.erase(unique(res.begin(), res.end()), res.end())
#include<queue>
#include<stack>
#include<map>
#include<set>//iterator,insert(),erase(),lower/upper_bound(value)/find()return end()
#define ll long long
using namespace std;
int a[100005];
signed main(){
int t;
cin>>t;
while(t--){
		int n;
	cin>>n;
	for(int i=0;i<n;i++) cin>>a[i];
	
	int g=0;
	for(int i=0;i<=n/2;i++){
		g=__gcd(g,abs(a[i]-a[n-1-i]));
	}
	cout<<g<<endl;
}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值