National Taiwan University NCPC Preliminary 2021 E Identity Subset

题目描述

https://codeforces.com/gym/103328/problem/E

简要题意:给定一个素数 $p$ 以及一个长度为 $p-1$ 的序列 $a_i$,求是否能从序列 $a_i$ 中选出若干个数,使得它们的乘积模 $p$ 等于 $1$

$p\le 10^5$

Solution

我们考虑求出 $p$ 的原根 $g$,然后我们将 $a_i$ 变成一个范围为 $[0,p-2]$ 的数,问题被转换成是否能选出若干个数出来使得它们的和是 $p-1$ 的倍数,我们可以直接上 $bitset$,时间复杂度 $O(\frac{p^2}{64})$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <vector>
#include <bitset>
#define maxn 100010
#define ll long long
using namespace std;

int p, n, a[maxn], b[maxn];

ll pow_mod(ll x, ll n) {
ll s = 1;
for (; n; n >>= 1, x = x * x % p)
if (n & 1) s = s * x % p;
return s;
}

int get_g(int p) {
int n = p - 1, tn = n; vector<int> vec;
if (p == 2) return 1;
for (int p = 2; p * p <= tn; ++p) {
if (tn % p) continue; vec.push_back(p);
while (tn % p == 0) tn /= p;
} if (tn > 1) vec.push_back(tn);
for (int i = 2; i < p; ++i) {
bool ok = 1;
for (auto t : vec)
if (pow_mod(i, n / t) == 1) ok = 0;
if (ok) return i;
} return -1;
}

bitset<maxn> f;

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

cin >> p; n = p - 1;
for (int i = 1; i <= n; ++i) cin >> a[i], a[i] %= p;
int g = get_g(p); b[0] = -1;
for (int i = 0, t = 1; i < n; ++i) b[t] = i, t = 1ll * t * g % p;
for (int i = 1; i <= n; ++i) a[i] = b[a[i]];
for (int i = 1; i <= n; ++i) {
if (a[i] == -1) continue;
f |= f << a[i] | f >> n - a[i];
f[a[i]] = 1;
} cout << (f[0] ? "Yes" : "No") << "\n";
return 0;
}