题目概述

原题参考:D. Turtle Tenacity: Continual Mods
给出长度为n的数组,可以对其任意排列,问是否可以给出一个数组a1、a2...、an满足a1%a2%...%an!= 0

思路想法

感觉这种与顺序无关的题目都可以先尝试升序或是降序排列,事实上,假如升序排列,如果最小值a1只有一个的话,那么最终答案就是a1,但是如果存在多个最小值该如何,此时应该选出一个除最小值之外的数,对最小值进行取模运算,得出一个更小的数,答案就是这个数,当然,当其余数对最小值取模得出的结果都是0时,那么就没有答案,对于这个,可以记录数组的gcd,只要最小值不是gcd就可以

参考代码

#include <bits/stdc++.h>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define pll pair<long long, long long>
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<long long>
#define ll long long
#define ull unsigned long long
const ll INF = 9187201950435737471;
const int inf = 2139062143;
const ll mod = 1e9 + 7;
const double eps = 1e-6;
const double PI = acos(-1.0);
const int N = 2e5+7;
int n, a[N], minA, _gcd = 0;
map<int, int> cnt;
void solve() {
    cin >> n;
    cnt.clear(), _gcd = 0;
    minA = 1e9+7;
    for(int i=1; i<=n; i++) {
        cin >> a[i];
        cnt[a[i]] ++;
        minA = min(minA, a[i]);
        _gcd = gcd(_gcd, a[i]);
    }
    if(cnt[minA] == 1) cout << "YES" << endl;
    else {
        if(minA == _gcd) cout << "NO" << endl;
        else {
            if(cnt[minA] != n) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
}
int main() {
#ifdef xrl
    freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout);
#endif
    FAST_IO;
    int t = 1;
    cin >> t;
    while(t --) solve();
#ifdef xrl
    cout << "Time used = " << (double)(clock() * 1.0 / CLOCKS_PER_SEC) << "s";
#endif
    return 0;
}

做题反思

对于与顺序无关的题目,往往可以先尝试升序或是降序排列。同时,对于大多数题目,都可以先考虑简单情况再由复杂情况入手

posted on 2024-03-02 13:15  山余木  阅读(44)  评论(0)    收藏  举报