C. Make Equal With Mod(思维)

本文介绍了一种算法,用于判断是否能通过特定操作使数组的所有元素相等。该算法适用于非负整数数组,通过对数组进行排序并检查特定条件来实现。具体包括:若数组中不存在1,则可以通过不断对每个元素进行取模操作直至所有元素为零;若有1存在,需进一步检查相邻元素间的差值。

目录

原题:

思路:

代码:


原题:

You are given an array of nn non-negative integers a1,a2,…,ana1,a2,…,an. You can make the following operation: choose an integer x≥2x≥2 and replace each number of the array by the remainder when dividing that number by xx, that is, for all 1≤i≤n1≤i≤n set aiai to aimodxaimodx.

Determine if it is possible to make all the elements of the array equal by applying the operation zero or more times.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer nn (1≤n≤1051≤n≤105) — the length of the array.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109) where aiai is the ii-th element of the array.

The sum of nn for all test cases is at most 2⋅1052⋅105.

Output

For each test case, print a line with YES if you can make all elements of the list equal by applying the operation. Otherwise, print NO.

You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer).

Example

input

Copy

4
4
2 5 6 8
3
1 1 1
5
4 1 7 0 8
4
5 9 17 5

output

Copy

YES
YES
NO
YES

Note

In the first test case, one can apply the operation with x=3x=3 to obtain the array [2,2,0,2][2,2,0,2], and then apply the operation with x=2x=2 to obtain [0,0,0,0][0,0,0,0].

In the second test case, all numbers are already equal.

In the fourth test case, applying the operation with x=4x=4 results in the array [1,1,1,1][1,1,1,1].

思路:

1,没有1,从大到小,自己对自己取模,直至为零;

2,有1,排序,,任何相邻两个差不为1就可以从大到小,mod(自己-1)最终为1;

3,

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxj=2e5+100,mod=1e9+7;
int a[maxj];
int32_t main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int f1=0,f2=0;
        for(int i=1;i<=n;++i){
        cin>>a[i];
        if(a[i]==1)f1=1;
        }
        sort(a+1,a+1+n);
        for(int i=1;i<n;++i){
            if(a[i+1]-a[i]==1){
                    f2=1;
                    break;
            }
        }
        if(!f1||!f2){
            cout<<"yes"<<'\n';
        }else{
            cout<<"no"<<'\n';
        }
    }
    return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值