#include <iostream> #include <vector> #include <sstream> #include <string> using namespace std; struct Zbior { short operacja; int zbior1; int zbior2; Zbior(short operacja, int zbior1, int zbior2) : operacja(operacja), zbior1(zbior1), zbior2(zbior2) { } }; bool TEST = false; bool CzyZawiera(int v, int x, int n, const vector<Zbior>& zbiory) { if (x == 0) return false; if (x <= n) { int y = v / x; return v == x * y; } else { int i = x - n - 1; switch (zbiory[i].operacja) { case 1: return CzyZawiera(v, zbiory[i].zbior1, n, zbiory) || CzyZawiera(v, zbiory[i].zbior2, n, zbiory); case 2: return CzyZawiera(v, zbiory[i].zbior1, n, zbiory) && CzyZawiera(v, zbiory[i].zbior2, n, zbiory); case 3: return !CzyZawiera(v, zbiory[i].zbior1, n, zbiory); } } return false; } int main() { int n = 0, m = 0, q = 0; vector<string> inputLines; if (TEST) { inputLines = { "7 8 9", "1 5 7", "2 2 3", "3 8", "2 10 8", "3 3", "1 12 12", "2 10 13", "1 9 14", "2 4", "5 7", "11 5", "15 1", "15 2", "15 3", "15 4", "15 5", "15 6" }; } else { string line; while (getline(cin, line)) { inputLines.push_back(line); } } istringstream iss(inputLines[0]); iss >> n >> m >> q; vector<Zbior> zbiory; if (TEST) { cout << "Pierwszy wiersz " << n << " " << m << " " << q << endl; } for (int i = 0; i < m; i++) { istringstream iss(inputLines[i + 1]); int op, a, b = 0; iss >> op >> a; if (op != 3) iss >> b; zbiory.emplace_back(op, a, b); } for (int i = 0; i < q; i++) { istringstream iss(inputLines[m + 1 + i]); int x, v; iss >> x >> v; bool wynik = CzyZawiera(v, x, n, zbiory); cout << (wynik ? "TAK" : "NIE") << endl; } if (TEST) { cin.get(); } return 0; }
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 100 101 102 103 | #include <iostream> #include <vector> #include <sstream> #include <string> using namespace std; struct Zbior { short operacja; int zbior1; int zbior2; Zbior(short operacja, int zbior1, int zbior2) : operacja(operacja), zbior1(zbior1), zbior2(zbior2) { } }; bool TEST = false; bool CzyZawiera(int v, int x, int n, const vector<Zbior>& zbiory) { if (x == 0) return false; if (x <= n) { int y = v / x; return v == x * y; } else { int i = x - n - 1; switch (zbiory[i].operacja) { case 1: return CzyZawiera(v, zbiory[i].zbior1, n, zbiory) || CzyZawiera(v, zbiory[i].zbior2, n, zbiory); case 2: return CzyZawiera(v, zbiory[i].zbior1, n, zbiory) && CzyZawiera(v, zbiory[i].zbior2, n, zbiory); case 3: return !CzyZawiera(v, zbiory[i].zbior1, n, zbiory); } } return false; } int main() { int n = 0, m = 0, q = 0; vector<string> inputLines; if (TEST) { inputLines = { "7 8 9", "1 5 7", "2 2 3", "3 8", "2 10 8", "3 3", "1 12 12", "2 10 13", "1 9 14", "2 4", "5 7", "11 5", "15 1", "15 2", "15 3", "15 4", "15 5", "15 6" }; } else { string line; while (getline(cin, line)) { inputLines.push_back(line); } } istringstream iss(inputLines[0]); iss >> n >> m >> q; vector<Zbior> zbiory; if (TEST) { cout << "Pierwszy wiersz " << n << " " << m << " " << q << endl; } for (int i = 0; i < m; i++) { istringstream iss(inputLines[i + 1]); int op, a, b = 0; iss >> op >> a; if (op != 3) iss >> b; zbiory.emplace_back(op, a, b); } for (int i = 0; i < q; i++) { istringstream iss(inputLines[m + 1 + i]); int x, v; iss >> x >> v; bool wynik = CzyZawiera(v, x, n, zbiory); cout << (wynik ? "TAK" : "NIE") << endl; } if (TEST) { cin.get(); } return 0; } |