题解:Niton & LCM

· · 题解

题意

构造一个非空不可重集合 S \subseteq \{1, 2, \dots, n\} ,使得 (\operatorname{lcm}_{i\in S}a_i )= k。

思路

若 k = 1,显然只要有 a_i = 1 那么就有 \operatorname{lcm} = k。

接下来,倘若某个元素 a_i 不能整除 k,那么只要选入子集,整体的 \operatorname{lcm} 就会有它的某个质因数或者指数超过 k 中对应的要求。显然是不合法的。

也就是说,我们要构造的集合实际上是这样的:

T = \{a_i |k \mod a_i = 0\}

接下来,如果把它们全部拿来做 \operatorname{lcm} ,得到的值就是这批数中能够汇聚到的最大指数。考虑设初始值 now = 1,我们依此做 now = \operatorname{lcm}(now, a_i) = \frac{now}{\gcd(now, a_i)} \times a_i。

最后,如果 now = k,那么就肯定存在集合使得 \operatorname{lcm} = k。否则不可能存在。

#include <bits/stdc++.h>
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define lcb(x) ((x) * size - size + 1)
#define rcb(x) ((x) * size)
#define lc(x) x << 1ll
#define rc(x) x << 1ll | 1ll
#define lowbit(x) ((x) & (-x))
#define LEN_LC (tree[lc(i)].r - tree[lc(i)].l + 1)
#define LEN_RC (tree[rc(i)].r - tree[rc(i)].l + 1)
#define LEN (tree[i].r - tree[i].l + 1)
#define int long long
#define ull unsigned long long
#define endl "\n"
const int N = 1e5 + 7;

const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
template<typename T>inline void read(T &x) {
    bool f = 1;
    x = 0;
    char ch = getchar();
    while (ch < '0' || ch > '9') {
        if (ch == '-') f = !f;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9') {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    x = (f ? x : -x);
    return;
}

ull n, k;
ull a[N]; 
void solve() {
    std::cin >> n >> k; 
    rep(i, 1, n) {
        std::cin >> a[i]; 
    }

    if(k == 1) {
        bool flag = false; 
        rep(i, 1, n) {
            if(a[i] == 1) {
                flag = true; 
                break;
            }
        }
        if(flag) {
            std::cout << "Yes" << endl; 
        } else {
            std::cout << "No" << endl; 
        }
        return ;
    }

    ull now = 1; 
    rep(i, 1, n) {
        if(k % a[i] == 0) {
            ull gcd = std::__gcd(now, a[i]); 
            now = now / gcd * a[i]; 
        }
    }
    if(now == k) {
        std::cout << "Yes" << endl; 
    } else {
        std::cout << "No" << endl;
    }
}

signed main() {
    IOS;
    int t; std::cin>> t; while(t--) solve();
    return 0;
}

时间复杂度为 O(n \log n)。