[CF] D. Turtle Tenacity: Continual Mods - Codeforces Round 929 (Div. 3)

https://codeforces.com/contest/1933/problem/D

题目大意

给出 n (2n105) 长的数组 a 其中 1ai109

问是否存在某种排列使得 a_1 \mod a_2 \mod a_3 \cdots \mod a_n \neq 0

简要题解

假设 m = \min(a)

容易想到 x < yx \mod y = x,则如果数组中的最小数 m 唯一,那么我们按顺序排序即可,最后结果就是 m

最小数不唯一时,我们需要能够创造出一个更小的数,于是我们就找不是 m 倍数的数,则这个余数 r < m,我们把这一组放在开头,剩下的放在后面即可。

如果我们找不到这样的数,即所有数都是 x 的倍数,则在遇到第二个 x 之前要么值为 x 要么值为 0,通过第二个 x 后结果为 0。而我们知道,一旦一个位置的结果为 0,后续都会为 0,也就是说不存在方案可以构造。

复杂度

TO(n \log n)\log 来自排序,当然也可以不排省掉这个 \log

SO(n)

代码实现

#include <bits/stdc++.h>
using namespace std;
int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;
template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) {
return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) {
return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z
if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
if(x<z) { y=x; x=z; } else if(y<z) y=z; }
// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());
/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/
void solve() {
int n; cin >> n;
vector<int> a(n);
for (int& i : a) cin >> i;
sort(a.begin(), a.end());
for (int i = 1; i < n; i++) {
if (a[i] % a[0]) {
cout << "YES\n";
return;
}
}
if (a[1] != a[0]) {
cout << "YES\n";
return;
}
cout << "NO\n";
}
int main() {
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Prev: [CF] D. Subtract Min Sort - Codeforces Round 998 (Div. 3)
Next: [CF] F. Isomorphic Strings - Educational Codeforces Round 44 (Rated for Div. 2)

Gitalking ...