跳到正文

个人记录

B3820 小粉兔的元素反应 题解

0x7f约 3 分钟
原文
文章信息与版本

文章信息

发布
2023年8月12日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 3 分钟

版本

  1. v1当前

题意:给定序列 aa,询问是否存在 i,j(i≠j)i,j(i \ne j) 使得 ai×aja_i\times a_j 为 154154 或 147147 的倍数。

思路:显然,如果存在两个合法的数,那么它们一定可以反应无限次,所以只需要特判掉 ∑i=1nai≥k\sum\limits_{i=1}^na_i \ge k 的情况。接下来考虑如何寻找合法数,如果直接枚举 i,ji,j,时间复杂度是 O(n2)O(n^2) 的,只有 80pts80pts。

考虑一个结论:若 ai×aj≡0(modP)a_i\times a_j\equiv0\pmod{P},则一定有 (ai mod P)×(aj mod P)≡0(modP)(a_i \bmod P)\times (a_j \bmod P)\equiv0\pmod{P},这样我们就能将值域缩减到 PP 以内,分别令 PP 等于 154154 和 147147,枚举 ai,aja_i,a_j 对 PP 取模的值判断即可。

code:

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

signed main() {
    cin.tie(0) -> sync_with_stdio(false);
    auto check = [](int P, const auto a) {
        map<int, int> mp;
        for (auto x : a) mp[x % P]++;
        for (auto i : mp)
        for (auto j : mp) {
            if (i.first == j.first && i.second == 1)
                continue;
            if (i.first * j.first % P == 0)
                return true;
        }
        return false;
    };
    int T; cin >> T;
    while (T--) {
        int n; string k; cin >> n >> k;
        vector<int> a(n, 0);
        for (int i = 0; i < n; i++) cin >> a[i];
        if (k.size() <= 16 && accumulate(a.begin(), a.end(), 0ll) >= atoll(k.c_str())) {
            puts("Yes"); continue;
        }
        puts(check(154, a) || check(147, a) ? "Yes" : "No");
    }
    return 0;
}

拓展:P8928

原文
抓取于
  1. 0x7f

    fixed

  2. hzoi_Shadow

    n^2 可以到80pts https://www.luogu.com.cn/record/120738298