Div3 CF 760C

C. Paint the Array

You are given an array a a a consisting of n n n positive integers. You have to choose a positive integer d d d and paint all elements into two colors. All elements which are divisible by d d d will be painted red, and all other elements will be painted blue.

The coloring is called beautiful if there are no pairs of adjacent elements with the same color in the array. Your task is to find any value of d d d which yields a beautiful coloring, or report that it is impossible.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1t1000) — the number of testcases.

The first line of each testcase contains one integer n n n ( 2 ≤ n ≤ 100 2 \le n \le 100 2n100) — the number of elements of the array.

The second line of each testcase contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 18 1 \le a_i \le 10^{18} 1ai1018).

Output

For each testcase print a single integer. If there is no such value of d d d that yields a beautiful coloring, print 0 0 0. Otherwise, print any suitable value of d d d ( 1 ≤ d ≤ 1 0 18 1 \le d \le 10^{18} 1d1018).

Example

input

5
5
1 2 3 4 5
3
10 5 15
3
100 10 200
10
9 8 2 6 6 2 8 6 5 4
2
1 3

output

2
0
100
0
3

分析

给出N个正整数,我们需要找到一个d

使这一组数中可以被d整除的染成其中一种颜色,不能够被d整除的染成另外一种颜色

判断是否存在一个d可以使颜色交替出现

思路

如果我们想要 a 1 , a 3 , a 5 . . . a_1,a_3,a_5... a1,a3,a5...被同一个d整除的话

(1)求奇数位、偶数位 最大公约数

我们要找的恰好是他们的最大公约数,设为 g 1 g_1 g1

同理我们也需要求 a 2 , a 4 , a 6 a_2,a_4,a_6 a2,a4,a6 的最大公约数,设为 g 2 g_2 g2

(2)奇偶校验

如果说 g 1 g_1 g1可以整除 a 2 , a 4 , a 6 a_2,a_4,a_6 a2,a4,a6 中的数,或者说 g 2 g_2 g2可以整除 a 1 , a 3 , a 5 a_1,a_3,a_5 a1,a3,a5 ,说明结果是不成功的

奇技淫巧

在我们计算偶数位和奇数位上所有数的最大公约数的时候

我们推荐使用

vector<ll> thx(begin(v),begin(v) + 2); //拷贝集合的前两位
for(int i{};i < n;i++) {
		thx[i % 2] = gcd(thx[i % 2],v[i]);
}

错误示范(My初始写法)

for(i = 1;i <= n;i += 2) t1 = gcd(v[i],t1);
for(i = 2;i <= n;i += 2) t2 = gcd(v[i],t2);
在做奇偶校验的时候
通过异或快速切换奇偶公约
vector<bool> pd(2,true);
for(int i{};i < n;i++) {
		pd[i % 2] = pd[i % 2] && (v[i] % thx[(i % 2) ^ 1] > 0);
}

还有一种写法

for(int i{};i < n;i++) {
		pd[i % 2] = pd[i % 2] && (v[i] % thx[1 - (i % 2)] > 0);
}

最后判断一下 p d [ i ] pd[i] pd[i]是否为 t r u e true true

如果其中一个为真说明取另一位上的公约数是可以的

如果两个都是真,那么我们取大的

官方 P l u s _ C o d e 官方Plus\_Code 官方Plus_Code

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
const int N = 100010;
#define forn(i,n) for(int i = 0;i < (int)n;i++) 
#define all(x) begin(x),end(x)
#define str string
ll gcd(ll a,ll b) {
    return b == 0 ? a : gcd(b,a % b);
}

inline void solve() {
    int n; cin >> n;
    vector<ll> v(n);
    for(int i{};i < n;i++) cin >> v[i];

    vector<ll> thx(begin(v),begin(v) + 2);
    
    for(int i{};i < n;i++) {
        thx[i % 2] = gcd(thx[i % 2],v[i]);
    }

    vector<bool> good(2,true);
    for(int i{};i < n;i++) {
		good[i % 2] = good[i % 2] && (v[i] % thx[1 - (i % 2)] > 0);
}

    ll ans = 0;
    for(int i{};i < 2;i++) {
        if(good[i]) ans = max(ans,thx[i ^ 1]);
    }

    cout << ans << endl;
    
}
int main(){
    freopen("input.txt","r",stdin);
    int t; cin >> t;
    while(t--) solve();
    // solve();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值