#include <algorithm> #include <iostream> #include <cassert> #include <vector> #include <cstdio> #include <array> #include <deque> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> namespace { using namespace std; typedef vector<int> vint; typedef long long unsigned ulo; typedef long long lint; void __attribute__((unused)) spacje(int k) { int magic; static stack<int*> stos; while (!stos.empty() && stos.top() <= &magic) stos.pop(); stos.push(&magic); for (int n = k * stos.size(); n--;) fprintf(stderr, " "); } #define BEND(x) begin(x), end(x) #define BOTH(x) for (int x = 0; x < 2; ++x) #define UPlt(xx, yy) (xx) = min(xx, yy) #define UPgt(xx, yy) (xx) = max(xx, yy) struct in_t { template<class T> operator T () { static bool __attribute__((unused)) dummy = ios_base::sync_with_stdio(false); T x; cin >> x; return x; } }; in_t __attribute__((unused)) in; class Print { ostream & os; bool nfirst; public: Print(ostream & os = cout) : os(os), nfirst(false) {} ~Print() { os << endl; } template<class T> Print & operator , (T const& e) { if (nfirst) os << " "; os << e; nfirst = true; return *this; } }; #define print Print{cout}, #define prerr Print{cerr}, template <class T> struct Range { struct Intit { T x; Intit(T x) : x(x) {} T & operator * () { return x; }; bool operator != (Intit other) { return x < other.x; } void operator ++ () { ++x; } }; T b, e; explicit Range(T n) : b(0), e(n) {} Range(T b, T e) : b(b), e(e) {} Intit begin() { return b; } Intit end() { return e; } }; template <class T> Range<T> range(T n) { return Range<T>(n); } template <class T> Range<T> range(T b, T e) { return Range<T>(b, e); } template <class T> Range<T> inclusive(T b, T e) { return Range<T>(b, e + 1); } #define _ __attribute__((unused)) _ }; // #define dprintf(...) if (1) spacje(2), fprintf(stderr, __VA_ARGS__) #define dprintf(...) {} int const MOD = 1000000007; vector<int> powers; struct Node; Node * chain(int depth, char const * lits, int n); struct Node { Node * left; Node * right; int sum; bool full; Node * create(int depth, char const * lits, int n) { dprintf("create %d .. %d\n", depth, n); if (full) { return this; } if (n == 0) { return new Node { nullptr, nullptr, powers[depth], true, }; } else { Node * new_left = lits[0] ? left : (left ? left->create(depth - 1, lits + 1, n - 1) : chain(depth - 1, lits + 1, n - 1)); Node * new_right = lits[0] ? (right ? right->create(depth - 1, lits + 1, n - 1) : chain(depth - 1, lits + 1, n - 1)) : right; int sum = (new_left ? new_left->sum : 0) + (new_right ? new_right->sum : 0); if (sum >= MOD) sum -= MOD; bool full = sum == powers[depth]; return new Node { new_left, new_right, sum, full }; } } }; Node * chain(int depth, char const * lits, int n) { dprintf("chain %d .. %d\n", depth, n); int sum = powers[depth - n]; if (n == 0) { return new Node { nullptr, nullptr, sum, true }; } Node * left = lits[0] ? nullptr : chain(depth - 1, lits + 1, n - 1); Node * right = lits[0] ? chain(depth - 1, lits + 1, n - 1) : nullptr; return new Node { left, right, sum, false }; } int main() { vector<pair<int, vector<char>>> clauses; int n = in; powers.push_back(1); for (int _ : range(n)) { int x = powers.back(); x *= 2; if (x >= MOD) x -= MOD; powers.push_back(x); } while (true) { char c = in; if (cin) { assert(c == '('); dprintf("klauzula\n"); vector<pair<int, bool>> lits; while (true) { char c = in; if (c == ')') break; bool val = true; if (c == '~') { val = false; c = in; } assert(c == 'x'); int i = in; lits.push_back({-i, val}); c = in; dprintf("%d %d\n", i, val); if (c == ')') break; assert(c == 'v'); } sort(BEND(lits)); vector<char> clause; for (auto p : lits) { clause.push_back(p.second); } clauses.push_back({-lits.front().first, clause}); c = in; if (cin) assert(c == '^'); else break; } } sort(BEND(clauses), [](pair<int, vector<char>> const& fst, pair<int, vector<char>> const& snd){ return fst.first < snd.first; }); Node * root = new Node { nullptr, nullptr, 0, false }; int depth = 0; for (auto const& clause : clauses) { int mydepth = clause.first; dprintf("%d -> %d\n", depth, mydepth); vector<char> const& vals = clause.second; int offset = mydepth - depth; int o2 = min(offset, (int)vals.size()); dprintf("dół\n"); Node * new_root = root->create(depth, vals.data() + o2, vals.size() - o2); while (mydepth - depth > (int)vals.size()) { dprintf("zapełniam dziurę\n"); root = new Node { root, root, 2 * root->sum % MOD, root->full }; new_root = new Node { new_root, new_root, 2 * new_root->sum % MOD, new_root->full }; dprintf("dodaję (rootsum %d, newrootsum %d\n", root->sum, new_root->sum); depth++; }; int i = mydepth - depth - 1; while (mydepth > depth) { dprintf("ogon\n"); Node * left = vals[i] ? root : new_root; Node * right = vals[i] ? new_root : root; //Node * right = vals[i] ? root : new_root; //Node * left = vals[i] ? new_root : root; int sum = (left->sum + right->sum) % MOD; bool full = root->full && new_root->full; new_root = new Node { left, right, sum, full }; root = new Node { root, root, 2 * root->sum % MOD, root->full }; --i; depth++; dprintf("dodaję (rootsum %d, newrootsum %d\n", root->sum, new_root->sum); } root = new_root; } int zlych = root->sum; while (depth < n) { zlych *= 2; zlych %= MOD; depth++; } int dobrych = powers[n]; dobrych += MOD; dobrych -= zlych; dobrych %= MOD; print dobrych; 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | #include <algorithm> #include <iostream> #include <cassert> #include <vector> #include <cstdio> #include <array> #include <deque> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> namespace { using namespace std; typedef vector<int> vint; typedef long long unsigned ulo; typedef long long lint; void __attribute__((unused)) spacje(int k) { int magic; static stack<int*> stos; while (!stos.empty() && stos.top() <= &magic) stos.pop(); stos.push(&magic); for (int n = k * stos.size(); n--;) fprintf(stderr, " "); } #define BEND(x) begin(x), end(x) #define BOTH(x) for (int x = 0; x < 2; ++x) #define UPlt(xx, yy) (xx) = min(xx, yy) #define UPgt(xx, yy) (xx) = max(xx, yy) struct in_t { template<class T> operator T () { static bool __attribute__((unused)) dummy = ios_base::sync_with_stdio(false); T x; cin >> x; return x; } }; in_t __attribute__((unused)) in; class Print { ostream & os; bool nfirst; public: Print(ostream & os = cout) : os(os), nfirst(false) {} ~Print() { os << endl; } template<class T> Print & operator , (T const& e) { if (nfirst) os << " "; os << e; nfirst = true; return *this; } }; #define print Print{cout}, #define prerr Print{cerr}, template <class T> struct Range { struct Intit { T x; Intit(T x) : x(x) {} T & operator * () { return x; }; bool operator != (Intit other) { return x < other.x; } void operator ++ () { ++x; } }; T b, e; explicit Range(T n) : b(0), e(n) {} Range(T b, T e) : b(b), e(e) {} Intit begin() { return b; } Intit end() { return e; } }; template <class T> Range<T> range(T n) { return Range<T>(n); } template <class T> Range<T> range(T b, T e) { return Range<T>(b, e); } template <class T> Range<T> inclusive(T b, T e) { return Range<T>(b, e + 1); } #define _ __attribute__((unused)) _ }; // #define dprintf(...) if (1) spacje(2), fprintf(stderr, __VA_ARGS__) #define dprintf(...) {} int const MOD = 1000000007; vector<int> powers; struct Node; Node * chain(int depth, char const * lits, int n); struct Node { Node * left; Node * right; int sum; bool full; Node * create(int depth, char const * lits, int n) { dprintf("create %d .. %d\n", depth, n); if (full) { return this; } if (n == 0) { return new Node { nullptr, nullptr, powers[depth], true, }; } else { Node * new_left = lits[0] ? left : (left ? left->create(depth - 1, lits + 1, n - 1) : chain(depth - 1, lits + 1, n - 1)); Node * new_right = lits[0] ? (right ? right->create(depth - 1, lits + 1, n - 1) : chain(depth - 1, lits + 1, n - 1)) : right; int sum = (new_left ? new_left->sum : 0) + (new_right ? new_right->sum : 0); if (sum >= MOD) sum -= MOD; bool full = sum == powers[depth]; return new Node { new_left, new_right, sum, full }; } } }; Node * chain(int depth, char const * lits, int n) { dprintf("chain %d .. %d\n", depth, n); int sum = powers[depth - n]; if (n == 0) { return new Node { nullptr, nullptr, sum, true }; } Node * left = lits[0] ? nullptr : chain(depth - 1, lits + 1, n - 1); Node * right = lits[0] ? chain(depth - 1, lits + 1, n - 1) : nullptr; return new Node { left, right, sum, false }; } int main() { vector<pair<int, vector<char>>> clauses; int n = in; powers.push_back(1); for (int _ : range(n)) { int x = powers.back(); x *= 2; if (x >= MOD) x -= MOD; powers.push_back(x); } while (true) { char c = in; if (cin) { assert(c == '('); dprintf("klauzula\n"); vector<pair<int, bool>> lits; while (true) { char c = in; if (c == ')') break; bool val = true; if (c == '~') { val = false; c = in; } assert(c == 'x'); int i = in; lits.push_back({-i, val}); c = in; dprintf("%d %d\n", i, val); if (c == ')') break; assert(c == 'v'); } sort(BEND(lits)); vector<char> clause; for (auto p : lits) { clause.push_back(p.second); } clauses.push_back({-lits.front().first, clause}); c = in; if (cin) assert(c == '^'); else break; } } sort(BEND(clauses), [](pair<int, vector<char>> const& fst, pair<int, vector<char>> const& snd){ return fst.first < snd.first; }); Node * root = new Node { nullptr, nullptr, 0, false }; int depth = 0; for (auto const& clause : clauses) { int mydepth = clause.first; dprintf("%d -> %d\n", depth, mydepth); vector<char> const& vals = clause.second; int offset = mydepth - depth; int o2 = min(offset, (int)vals.size()); dprintf("dół\n"); Node * new_root = root->create(depth, vals.data() + o2, vals.size() - o2); while (mydepth - depth > (int)vals.size()) { dprintf("zapełniam dziurę\n"); root = new Node { root, root, 2 * root->sum % MOD, root->full }; new_root = new Node { new_root, new_root, 2 * new_root->sum % MOD, new_root->full }; dprintf("dodaję (rootsum %d, newrootsum %d\n", root->sum, new_root->sum); depth++; }; int i = mydepth - depth - 1; while (mydepth > depth) { dprintf("ogon\n"); Node * left = vals[i] ? root : new_root; Node * right = vals[i] ? new_root : root; //Node * right = vals[i] ? root : new_root; //Node * left = vals[i] ? new_root : root; int sum = (left->sum + right->sum) % MOD; bool full = root->full && new_root->full; new_root = new Node { left, right, sum, full }; root = new Node { root, root, 2 * root->sum % MOD, root->full }; --i; depth++; dprintf("dodaję (rootsum %d, newrootsum %d\n", root->sum, new_root->sum); } root = new_root; } int zlych = root->sum; while (depth < n) { zlych *= 2; zlych %= MOD; depth++; } int dobrych = powers[n]; dobrych += MOD; dobrych -= zlych; dobrych %= MOD; print dobrych; return 0; } |