Codeforces Round 929 (Div. 3) D
Codeforces Round 929 (Div. 3)
D. Turtle Tenacity: Continual Mods
题意
Given an array \(a_1, a_2, \ldots, a_n\), determine whether it is possible to rearrange its elements into \(b_1, b_2, \ldots, b_n\), such that \(b_1 \bmod b_2 \bmod \ldots \bmod b_n \neq 0\).
Here \(x \bmod y\) denotes the remainder from dividing \(x\) by \(y\). Also, the modulo operations are calculated from left to right. That is, \(x \bmod y \bmod z = (x \bmod y) \bmod z\). For example, \(2024 \bmod 1000 \bmod 8 = (2024 \bmod 1000) \bmod 8 = 24 \bmod 8 = 0\).
大致意思给定一个数组,我们可以任意排列,需要使数组从\(a[1]\ mod \ a[2] \ mod \ a[3] \ ... \; a[n]\)不为 $ 0 $
可以找到的话输出 "YES",找不到的话输出"NO"
Example
input
8
6
1 2 3 4 5 6
5
3 3 3 3 3
3
2 2 3
5
1 1 2 3 7
3
1 2 2
3
1 1 2
6
5 2 10 10 10 2
4
3 6 9 3
output
YES
NO
YES
NO
YES
NO
YES
NO
Note
In the first test case, rearranging the array into \(b = [1, 2, 3, 4, 5, 6]\) (doing nothing) would result in \(1 \bmod 2 \bmod 3 \bmod 4 \bmod 5 \bmod 6 = 1\). Hence it is possible to achieve the goal.
In the second test case, the array \(b\) must be equal to \([3, 3, 3, 3, 3]\), which would result in \(3 \bmod 3 \bmod 3 \bmod 3 \bmod 3 = 0\). Hence it is impossible to achieve the goal.
In the third test case, rearranging the array into \(b = [3, 2, 2]\) would result in \(3 \bmod 2 \bmod 2 = 1\). Hence it is possible to achieve the goal.
思路
我们可以先观察上述给的样例,我们先对数组进行排序,如果第一个数比后面的数都小且不为 \(0\),那我们第一个数取余后面的数永远都是不为 \(0\)的,反之如果最小的数有两个呢 ?
$x ;mod ;x $ 为 \(0\) 的话,后续继续怎么操作都还是 \(0\),但是为什么 \(2\; 2\; 3\)这个样例能输出YES呢
\(3 \bmod 2 \bmod 2 = 1\),那我们发现我们可以用大的数取余小的数变成一个比第一个数还小的数就能成功
因为我们是取余操作!取余结果不可能大于取余数,如果最小数不唯一,那我们用大一点的数取余最小数,能取得更小不为 \(0\) 的数即可成立
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t,n;
cin >> t;
while(t--){
cin >> n;
vector<int> a(n);
for(int i=0;i<n;i++){
cin >> a[i];
}
sort(a.begin(),a.end());
if(a[0]<a[1]) cout << "YES" << endl;
else{
bool jud = false;
for(int i=1;i<n;i++){
if(a[i]%a[0]<a[0]&&a[i]%a[0]!=0){
jud = true;
break;
}
}
if(jud) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
return 0;
}

浙公网安备 33010602011771号