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 longusing 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;
}