#include <iostream>
#include <vector>
using namespace std;
struct ZB1 {
struct Zbior {
int const j;
Zbior *neg;
Zbior(int j = 0) : j(j), neg(0) {
}
virtual ~Zbior() {
}
virtual bool check(int value) const {
return !(value % j);
}
};
struct ZbiorSuma : Zbior {
Zbior const &a, &b;
ZbiorSuma(Zbior const &a, Zbior const &b) : a(a), b(b) {
}
virtual bool check(int v) const {
return a.check(v) || b.check(v);
}
};
struct ZbiorIloczyn : Zbior {
Zbior const &a, &b;
ZbiorIloczyn(Zbior const &a, Zbior const &b) : a(a), b(b) {
}
virtual bool check(int v) const {
return a.check(v) && b.check(v);
}
};
struct ZbiorNegacja : Zbior {
Zbior const &a;
ZbiorNegacja(Zbior const &a) : a(a) {
}
virtual bool check(int v) const {
return !a.check(v);
}
};
int count, m;
vector< Zbior * > v, tofree;
ZB1(int n, int m) : count(1 + n), m(m), v(1 + count + m) {
int i = 0;
while (++i <= n)
tofree.push_back(v[i] = new Zbior(i));
}
~ZB1() {
int i = tofree.size();
while (i--)
delete tofree[i];
}
void suma(int x, int y) {
tofree.push_back(v[count++] = new ZbiorSuma(*v[x], *v[y]));
}
void iloczyn(int x, int y) {
tofree.push_back(v[count++] = new ZbiorIloczyn(*v[x], *v[y]));
}
void negacja(int x) {
Zbior *z = v[x];
if (!z->neg) {
(z->neg = new ZbiorNegacja(*z))->neg = z;
tofree.push_back(z->neg);
}
v[count++] = z->neg;
}
friend istream &operator>>(istream &is, ZB1 &zb1) {
int i = zb1.m;
while (i--) {
int a, x, y;
is >> a >> x;
switch (a) {
case 1:
is >> y;
zb1.suma(x, y);
break;
case 2:
is >> y;
zb1.iloczyn(x, y);
break;
default:
zb1.negacja(x);
}
}
return is;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, q;
cin >> n >> m >> q;
ZB1 zb1(n, m);
cin >> zb1;
while (q--) {
int x, val;
cin >> x >> val;
cout << (zb1.v[x]->check(val) ? "TAK\n" : "NIE\n");
}
}
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #include <iostream> #include <vector> using namespace std; struct ZB1 { struct Zbior { int const j; Zbior *neg; Zbior(int j = 0) : j(j), neg(0) { } virtual ~Zbior() { } virtual bool check(int value) const { return !(value % j); } }; struct ZbiorSuma : Zbior { Zbior const &a, &b; ZbiorSuma(Zbior const &a, Zbior const &b) : a(a), b(b) { } virtual bool check(int v) const { return a.check(v) || b.check(v); } }; struct ZbiorIloczyn : Zbior { Zbior const &a, &b; ZbiorIloczyn(Zbior const &a, Zbior const &b) : a(a), b(b) { } virtual bool check(int v) const { return a.check(v) && b.check(v); } }; struct ZbiorNegacja : Zbior { Zbior const &a; ZbiorNegacja(Zbior const &a) : a(a) { } virtual bool check(int v) const { return !a.check(v); } }; int count, m; vector< Zbior * > v, tofree; ZB1(int n, int m) : count(1 + n), m(m), v(1 + count + m) { int i = 0; while (++i <= n) tofree.push_back(v[i] = new Zbior(i)); } ~ZB1() { int i = tofree.size(); while (i--) delete tofree[i]; } void suma(int x, int y) { tofree.push_back(v[count++] = new ZbiorSuma(*v[x], *v[y])); } void iloczyn(int x, int y) { tofree.push_back(v[count++] = new ZbiorIloczyn(*v[x], *v[y])); } void negacja(int x) { Zbior *z = v[x]; if (!z->neg) { (z->neg = new ZbiorNegacja(*z))->neg = z; tofree.push_back(z->neg); } v[count++] = z->neg; } friend istream &operator>>(istream &is, ZB1 &zb1) { int i = zb1.m; while (i--) { int a, x, y; is >> a >> x; switch (a) { case 1: is >> y; zb1.suma(x, y); break; case 2: is >> y; zb1.iloczyn(x, y); break; default: zb1.negacja(x); } } return is; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, q; cin >> n >> m >> q; ZB1 zb1(n, m); cin >> zb1; while (q--) { int x, val; cin >> x >> val; cout << (zb1.v[x]->check(val) ? "TAK\n" : "NIE\n"); } } |
English