P13546 [OOI 2022] Integral Array 题解

· · 题解

想象你是个数学侦探🕵️,接到一个神秘案件:要验证一群数字是否"整整齐齐"地生活在一起。这群数字有个奇怪的家规:

家规:家里任何两个数字x和y(x≥y),必须保证x//y(地板除)这个结果也得住在家里!

破案三步走:

  1. 制作住户名单 📋
  1. 安装数字探测器 🔍
  1. 挨家挨户查户口 🚪

举个栗子🌰:

为什么这么快?⚡

侦探笔记📝:

Code:

#include <bits/stdc++.h>
using namespace std;

const int M = 1e7 + 10;

void solve() {
    int n, c;
    cin >> n >> c;
    vector<int> a(n);
    vector<bool> e(c + 1, false);
    vector<int> p(c + 2, 0);

    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        e[a[i]] = true;
    }

    for (int i = 1; i <= c; ++i) {
        p[i] = p[i - 1] + (e[i] ? 1 : 0);
    }

    for (int y = 1; y <= c; ++y) {
        if (!e[y]) continue;
        for (int k = 1; k * y <= c; ++k) {
            int l = k * y;
            int r = min((k + 1) * y - 1, c);
            if (p[r] > p[l - 1]) {
                if (!e[k]) {
                    cout << "No\n";
                    return;
                }
            }
        }
    }
    cout << "Yes\n";
}

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

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

案子破了!