F. Alex and a TV Show

  • 1 x v:令集合$x$等于${v}$

  • 2 x y z:令集合$x$等于集合$y$与$z$的并

  • 3 x y z:令集合$x$等于集合$y$与$z$的积,$A*B=\{gcd(a,b)|a\in A,b\in B\}$

  • 4 x v:询$v$在集合$x$中出现次数模2的结果

    莫比乌斯反演倍数

证明:

解法

  • 前两个操作由于操作4的影响。可以想到用$bitset$维护。

  • 考虑操作3,对于$x\in A,y\in B,gcd(x,y)$使得他们的因子都变成最小值,如果$(1,1)\rightarrow 1,(0,x)\rightarrow 0$即为$\&$操作。

  • 最后需要输出$v$出现次数。

根据 莫比乌斯反演倍数已知$f(x)$,预处理出来$F(x)$,对应乘的就是$mu[kx][x]=\mu(k)$

代码
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

#include <bits/stdc++.h>
#include <bitset>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e5 + 10;
const int M = 7e3 + 10;
const int mod = 1e9 + 7;

int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}
int pcnt, pr[N], npr[N], mu[N];

void Prime_init(int X)
{
npr[1] = 1;
mu[1] = 1;
for (int i = 2; i <= X; i++)
{
if (!npr[i])
pr[++pcnt] = i, mu[i] = -1;
for (int j = 1; j <= pcnt && pr[j] * i <= X; j++)
{
npr[pr[j] * i] = 1;
if (i % pr[j] == 0)
{
mu[i * pr[j]] = 0;
break;
}
else
{
mu[i * pr[j]] = mu[i] * -1;
}
}
}
}
bitset<7010> g[M], a[N], u[M];
int main()
{
int n = read(), q = read();

Prime_init(7000);
for (int j = 1; j <= 7000; j++)
for (int i = j; i <= 7000; i += j)
g[i][j] = 1, u[j][i] = abs(mu[i / j]);
for (int i = 1; i <= q; i++)
{
int op = read(), x = read();
if (op == 1)
a[x] = g[read()];
if (op == 2)
a[x] = a[read()] ^ a[read()];
if (op == 3)
a[x] = a[read()] & a[read()];
if (op == 4)
printf("%d", (a[x] & u[read()]).count() & 1);
}
}