#include <algorithm> #include <climits> #include <cstdio> #include <iostream> #include <random> #include <tuple> #include <unordered_map> #include <vector> using namespace std; typedef unsigned long long ull; mt19937 gen; uniform_int_distribution<ull> rand_ull; static const int MAX_N = 1000000; static const int P = 1000000007; int N, n; char literals[MAX_N]; struct Clause { int a, b; char *l; ull id; Clause() {} Clause(char *l) : a(INT_MAX), b(INT_MIN), l(l), id(rand_ull(gen)) {} int operator[](int i) { return l[i - a]; } }; vector<Clause> formula; unordered_map<ull, int> w; int Search(int k, vector<int>&& clauses, int fidx) { if (k > n) return 1; ull hash = k; for (int i : clauses) hash ^= formula[i].id; auto wi = w.find(hash); if (wi != w.end()) return wi->second; int res = 0; for (; fidx < (int)formula.size(); fidx++) if (formula[fidx].a > k) break; else clauses.push_back(fidx); bool values[2] = { true, true }; for (int i : clauses) { Clause& c = formula[i]; if (c.b == k) values[c[k]] = false; } for (int v = 0; v < 2; v++) { if (!values[v]) continue; vector<int> next; for (int i : clauses) { Clause& c = formula[i]; if (c.b > k && c[k] == v) next.push_back(i); } res += Search(k + 1, move(next), fidx); } res %= P; w[hash] = res; return res; } void Parse() { scanf("%d\n", &n); vector<char> buf(20 * MAX_N); gets(&buf.front()); char *p = &buf.front(); char *lptr = literals; while (true) { struct Literal { int i; char neg; Literal() : i(0), neg(0) {} }; vector<Literal> list; p++; while (true) { Literal l; if (*p == '~') { l.neg = 1; p++; } for (p++; isdigit(*p); p++) l.i = 10 * l.i + (*p - '0'); list.push_back(l); if (*p == ')') break; p += 3; } Clause c(lptr); for (auto& l : list) { c.a = min(l.i, c.a); c.b = max(l.i, c.b); } for (auto& l : list) c.l[l.i - c.a] = l.neg; lptr += c.b - c.a + 1; formula.push_back(c); p++; if (!*p) break; p += 3; } } void Renumerate() { int shift = 0; int B = 0; for (Clause& c : formula) { shift += max(0, c.a - B - 1); B = max(B, c.b); c.a -= shift; c.b -= shift; } N = n; n = B - shift; } int main() { Parse(); sort(formula.begin(), formula.end(), [](const Clause& x, const Clause& y) { return make_tuple(x.a, x.b) < make_tuple(y.a, y.b); }); Renumerate(); int a = Search(1, {}, 0); for (int i = n; i < N; i++) a = (a * 2) % P; printf("%d\n", a); 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 | #include <algorithm> #include <climits> #include <cstdio> #include <iostream> #include <random> #include <tuple> #include <unordered_map> #include <vector> using namespace std; typedef unsigned long long ull; mt19937 gen; uniform_int_distribution<ull> rand_ull; static const int MAX_N = 1000000; static const int P = 1000000007; int N, n; char literals[MAX_N]; struct Clause { int a, b; char *l; ull id; Clause() {} Clause(char *l) : a(INT_MAX), b(INT_MIN), l(l), id(rand_ull(gen)) {} int operator[](int i) { return l[i - a]; } }; vector<Clause> formula; unordered_map<ull, int> w; int Search(int k, vector<int>&& clauses, int fidx) { if (k > n) return 1; ull hash = k; for (int i : clauses) hash ^= formula[i].id; auto wi = w.find(hash); if (wi != w.end()) return wi->second; int res = 0; for (; fidx < (int)formula.size(); fidx++) if (formula[fidx].a > k) break; else clauses.push_back(fidx); bool values[2] = { true, true }; for (int i : clauses) { Clause& c = formula[i]; if (c.b == k) values[c[k]] = false; } for (int v = 0; v < 2; v++) { if (!values[v]) continue; vector<int> next; for (int i : clauses) { Clause& c = formula[i]; if (c.b > k && c[k] == v) next.push_back(i); } res += Search(k + 1, move(next), fidx); } res %= P; w[hash] = res; return res; } void Parse() { scanf("%d\n", &n); vector<char> buf(20 * MAX_N); gets(&buf.front()); char *p = &buf.front(); char *lptr = literals; while (true) { struct Literal { int i; char neg; Literal() : i(0), neg(0) {} }; vector<Literal> list; p++; while (true) { Literal l; if (*p == '~') { l.neg = 1; p++; } for (p++; isdigit(*p); p++) l.i = 10 * l.i + (*p - '0'); list.push_back(l); if (*p == ')') break; p += 3; } Clause c(lptr); for (auto& l : list) { c.a = min(l.i, c.a); c.b = max(l.i, c.b); } for (auto& l : list) c.l[l.i - c.a] = l.neg; lptr += c.b - c.a + 1; formula.push_back(c); p++; if (!*p) break; p += 3; } } void Renumerate() { int shift = 0; int B = 0; for (Clause& c : formula) { shift += max(0, c.a - B - 1); B = max(B, c.b); c.a -= shift; c.b -= shift; } N = n; n = B - shift; } int main() { Parse(); sort(formula.begin(), formula.end(), [](const Clause& x, const Clause& y) { return make_tuple(x.a, x.b) < make_tuple(y.a, y.b); }); Renumerate(); int a = Search(1, {}, 0); for (int i = n; i < N; i++) a = (a * 2) % P; printf("%d\n", a); return 0; } |