#include <iostream> #include <vector> #include <sstream> using namespace std; int NaInt(string a, int i) { int odp = 0; while (i < a.size()) { odp = odp * 10 + (a[i] - '0'); ++i; } return odp; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; string p; cin.ignore(); getline(cin, p); int naw = 1; for (int i = 0; i < p.size(); ++i) if (p[i] == '^') ++naw; vector< vector<int> > K; stringstream SS(p); string s; for (int i = 0; i < naw; ++i) { vector<int> T; while (1) { SS >> s; if (s == "v") continue; int dl = s.size(); bool kon = false, pocz = false; if (s[0] == '(') { pocz = true; s.erase(0, 1); --dl; } if (s[dl - 1] == ')') { kon = true; s.erase(dl - 1, 1); } int nr; if (s[0] == '~') { nr = NaInt(s, 2); T.push_back(-nr); } else { nr = NaInt(s, 1); T.push_back(nr); } if (kon) { K.push_back(T); break; } } if (i == naw - 1) break; SS >> s; } vector<int> w(n, -1); for (int i = 0; i < K.size(); ++i) { if (K[i].size() == 1) { if (K[i][0] > 0) w[K[i][0] - 1] = 1; else w[-K[i][0] - 1] = 0; } } int odp = 0; for (int i = 0; i < (1 << n); ++i) { bool db = true; for (int j = 0; j < n; ++j) { if (w[j] != -1) { if ((((i & (1 << j)) > 0) && w[j] == 1) || ((i & (1 << j)) == 0) && w[j] == 0); else { db = false; break; } } } if (!db) continue; int X = 1; for (int j = 0; j < K.size(); ++j) { int Y = 0; for (int k = 0; k < K[j].size(); ++k) { if (K[j][k] > 0) { if ((i & (1 << (K[j][k] - 1))) > 0) Y |= 1; else Y |= 0; } else { if ((i & (1 << (-K[j][k] - 1))) > 0) Y |= 0; else Y |= 1; } } X &= Y; } if (X == 1) ++odp; } cout << odp; 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include <iostream> #include <vector> #include <sstream> using namespace std; int NaInt(string a, int i) { int odp = 0; while (i < a.size()) { odp = odp * 10 + (a[i] - '0'); ++i; } return odp; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; string p; cin.ignore(); getline(cin, p); int naw = 1; for (int i = 0; i < p.size(); ++i) if (p[i] == '^') ++naw; vector< vector<int> > K; stringstream SS(p); string s; for (int i = 0; i < naw; ++i) { vector<int> T; while (1) { SS >> s; if (s == "v") continue; int dl = s.size(); bool kon = false, pocz = false; if (s[0] == '(') { pocz = true; s.erase(0, 1); --dl; } if (s[dl - 1] == ')') { kon = true; s.erase(dl - 1, 1); } int nr; if (s[0] == '~') { nr = NaInt(s, 2); T.push_back(-nr); } else { nr = NaInt(s, 1); T.push_back(nr); } if (kon) { K.push_back(T); break; } } if (i == naw - 1) break; SS >> s; } vector<int> w(n, -1); for (int i = 0; i < K.size(); ++i) { if (K[i].size() == 1) { if (K[i][0] > 0) w[K[i][0] - 1] = 1; else w[-K[i][0] - 1] = 0; } } int odp = 0; for (int i = 0; i < (1 << n); ++i) { bool db = true; for (int j = 0; j < n; ++j) { if (w[j] != -1) { if ((((i & (1 << j)) > 0) && w[j] == 1) || ((i & (1 << j)) == 0) && w[j] == 0); else { db = false; break; } } } if (!db) continue; int X = 1; for (int j = 0; j < K.size(); ++j) { int Y = 0; for (int k = 0; k < K[j].size(); ++k) { if (K[j][k] > 0) { if ((i & (1 << (K[j][k] - 1))) > 0) Y |= 1; else Y |= 0; } else { if ((i & (1 << (-K[j][k] - 1))) > 0) Y |= 0; else Y |= 1; } } X &= Y; } if (X == 1) ++odp; } cout << odp; return 0; } |