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
#include <cstdio>
#include <vector>
typedef int I;
typedef std::vector<I> VI;

static I bruteforce(const I* v, I n, I kl) {
        if (n > 31)
                return -1; // nie umiem
        I last = 1 << n;
        I ret = 0;
        for (I bits=0; bits<last; ++ bits) {
                bool varok = true;
                for (I k=0; k<kl; ++k) {
                        const I* p = &v[k*(n+1)+1];
                        I nli = p[-1];
                        bool ok = false;
                        for (I li=0; li<nli; ++li) {
                                I val = p[li];
                                if (val > 0) {
                                        if (bits & (1 << (val - 1))) {
                                                ok = true;
                                                break;
                                        }
                                } else {
                                        if (!(bits & (1 << (-val -1)))) {
                                                ok = true;
                                                break;
                                        }
                                }
                        }
                        if (!ok) {
                                varok = false;
                                break;
                        }
                }
                if (varok) {
                        ++ret;
                }
        }
        return ret;
}

int main() {
        I n, row = -1, col=0, x;
        bool neg = false;
        scanf("%d\n", &n);
        VI v;
        for (;;) {
                char c;
                if (scanf("%c", &c) <= 0)
                        break;
                switch(c) {
                case '(':
                        col = 0;
                        ++row;
                        v.resize((n+1)*(row+1));
                        break;
                case ')':
                        v[row*(n+1)] = col;
                        break;
                case '~':
                        neg = true;
                        break;
                case 'x':
                        scanf("%d", &x);
                        if (neg) {
                                x = -x;
                                neg = false;
                        }
                        ++col;
                        v[row*(n+1) + col] = x;
                        break;
                }
        }
        printf("%d\n", bruteforce(&v[0], n, row+1));
        return 0;
}