CF c题

C. Make Equal With Mod

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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的时候一定可以 x取max(a[i[)

有1的时候每个数搞成1,x取max(a[i]-1),但不能有连续的,否则会有0,1

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
const int maxn=1e5+5;
int a[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        sort(a+1,a+1+n);
        bool flag1=false;
        bool flag2=false;
        for(int i=1;i<=n;i++)
        {
            if(a[i]==1)
            {
                flag1=true;
                break;
            }
        }
        for(int i=1;i<=n-1;i++)
        {
            if(a[i+1]-a[i]==1)
            {
                flag2=true;
                break;
            }
        }
        if(flag1==false||flag2==false)
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}
#include <bits/stdc++.h>

#define int long long

using namespace std;

typedef long long LL;

void solve()
{
    int n;
    cin >> n;

    set<int> S;
    int cnt0 = 0, cnt1 = 0; // 统计0和1的数量
    for (int i = 0; i < n; i ++ ) {
        int x;
        cin >> x;
        S.insert(x);
        if (x == 1) cnt1 ++ ;
        if (x == 0) cnt0 ++ ;
    }

    if (cnt1 == 0) { // 第一种情况
        cout << "YES\n";
    } else if (cnt1 && cnt0) { // 第二种情况
        cout << "NO\n";
    } else { // 第三种情况
        for (auto x : S) {
            if (S.count(x + 1)) { // 判断是否有连续的两个数,这里采用了set判断
                cout << "NO\n";
                return; // 及时return
            }
        }
        cout << "YES\n";
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;

    while (T -- ) {
        solve();
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值