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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

typedef long long int lint;

const lint prime = 1000000009;

lint pow2(unsigned int n) {
    if (n == 0)
        return 1;
    else if (n == 1)
        return 2;
    else if (n == 2)
        return 4;

    lint res = pow2(n/2);
    if (n % 2 == 0)
        return (res * res) % prime;
    else
        return (2LL * res * res) % prime;
}

enum class LiteralType {
    Positive, Negative
};

struct Literal {
    LiteralType type;
    int varNr;

    bool operator<(const Literal& other) const {
        return varNr < other.varNr;
    }

    void parse(string literalStr) {
        //cerr << "  Literal.parse " << literalStr << endl;
        stringstream ss;
        ss << literalStr;
        type = LiteralType::Positive;
        char c;
        ss >> c;
        if (c == '~') {
            type = LiteralType::Negative;
            ss >> c;
        }
        ss >> varNr;
    }

    string toString() {
        stringstream ss;
        if (type == LiteralType::Negative)
            ss << "~";
        ss << "x" << varNr;
        return ss.str();
    }
};

class Clause {
public:

    int firstVar;
    int lastVar;
    vector<Literal> literals;

    Literal getLiteral(int varNr) {
        return literals[varNr - firstVar];
    }

    void setReady() {
        sort(literals.begin(), literals.end());
        firstVar = literals.begin()->varNr;
        lastVar = literals.rbegin()->varNr;
    }

    string clean(string clauseStr) {
        stringstream ss;
        ss << clauseStr;
        getline(ss, clauseStr, '(');
        getline(ss, clauseStr, ')');
        return clauseStr;
    }

    void parse(string clauseStr) {
        clauseStr = clean(clauseStr);
        //cerr << "Clause.parse " << clauseStr << endl;
        stringstream ss;
        ss << clauseStr;
        string literalStr;
        while (!ss.eof()) {
            getline(ss, literalStr, 'v');
            Literal literal;
            literal.parse(literalStr);
            literals.push_back(literal);
        }
        setReady();
    }

    string toString() {
        stringstream ss;
        ss << "(";
        for (int i = firstVar; i < lastVar; ++i) {
            ss << getLiteral(i).toString();
            ss << " v ";
        }
        ss << getLiteral(lastVar).toString();
        ss << ")";
        return ss.str();
    }
};

class Formule {
public:

    int varCount;
    vector<Clause> clauses;
    vector<int> isActive;
    map<int, vector<int>> pos; // pos[i] to  lista klauzul w ktorych var_i jest pozytywna
    map<int, vector<int>> neg;
    int totalActiveClauses;
    map<int, vector<int>> lastPos; // lastPos[i] to lista klauzul w ktorych var_i jest ostatnia zmienna i jest pozytywna
    map<int, vector<int>> lastNeg;

    Formule(int n)
        : varCount(n)
    {
    }

    void prepareStats() {
        for (unsigned int i = 0; i < clauses.size(); ++i)
            isActive.push_back(true);
        totalActiveClauses = isActive.size();

        for (unsigned int i = 0; i < clauses.size(); ++i) {
            const Clause& cl = clauses[i];
            for (const Literal& lit: cl.literals) {
                if (lit.type == LiteralType::Positive) {
                    pos[lit.varNr].push_back(i);
                    if (lit.varNr == cl.lastVar)
                        lastPos[lit.varNr].push_back(i);
                } else {
                    neg[lit.varNr].push_back(i);
                    if (lit.varNr == cl.lastVar)
                        lastNeg[lit.varNr].push_back(i);
                }
            }
        }
    }

    void parse(istream& in) {
        string str;
        while (!in.eof()) {
            getline(in, str, '^');
            Clause clause;
            clause.parse(str);
            clauses.push_back(clause);
        }
    }

    vector<int> activeClauses(const vector<int>& clausesList) {
        vector<int> activeSubset;
        for (int i: clausesList) {
            if (isActive[i])
                activeSubset.push_back(i);
        }
        return activeSubset;
    }

    void setInactive(const vector<int>& clausesList) {
        for (int i: clausesList) {
            if (isActive[i]) {
                isActive[i] = false;
                totalActiveClauses--;
            }
        }
    }

    void setActive(const vector<int>& clausesList) {
        for (int i: clausesList) {
            if (!isActive[i]) {
                isActive[i] = true;
                totalActiveClauses++;
            }
        }
    }

    lint assignTrue(int ile) {
        int varNr = ile + 1;
        if (!activeClauses(lastNeg[varNr]).empty())
            return 0; // przypisujac true zostawilibysmy niezaspokojona klauzule
        const vector<int>& activePos = activeClauses(pos[varNr]);
        setInactive(activePos);
        int res = solve(ile + 1);
        setActive(activePos);
        return res;
    }

    lint assignFalse(int ile) {
        int varNr = ile + 1;
        if (!activeClauses(lastPos[varNr]).empty())
            return 0; // przypisujac false zostawilibysmy niezaspokojona klauzule
        const vector<int>& activeNeg = activeClauses(neg[varNr]);
        setInactive(activeNeg);
        int res = solve(ile + 1);
        setActive(activeNeg);
        return res;
    }

    lint solve() {
        prepareStats();
        return solve(0);
    }

    lint solve(int ile) {
        if (totalActiveClauses == 0)
            return pow2(varCount - ile);
        if (ile == varCount)
            return 0LL;

        if (pos[ile+1].size() + neg[ile+1].size() == 0)
            return (2LL * solve(ile+1)) % prime;

        lint r1 = assignTrue(ile);
        lint r2 = assignFalse(ile);
        return (r1 + r2) % prime;
    }

    string toString() {
        cerr << "Formule.parse number of clauses: " << clauses.size() << endl;
        stringstream ss;
        int nrOfClauses = static_cast<int>(clauses.size());
        for (int i = 0; i < nrOfClauses-1; ++i) {
            cerr << "Clause " << i << ": " << clauses[i].toString() << endl;
            ss << clauses[i].toString();
            ss << " ^ ";
        }
        cerr << "Clause " << nrOfClauses-1 << ": " << clauses[nrOfClauses-1].toString() << endl;
        ss << clauses[nrOfClauses-1].toString();
        return ss.str();
    }
};


int main() {
    int n; cin >> n;
    Formule f(n);
    f.parse(cin);
    //cout << f.toString() << endl;
    cout << f.solve() << endl;
    return 0;
}