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
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

class cnf {

    public static void main(String[] args) {
        String[] data = readLine();
        int n = Integer.parseInt(data[0]);
        String line = data[1];

        String[] clauses = line.split(" \\^ ");

        List<Map<String, List<String>>> list = new ArrayList<>();

        for (String clause : clauses) {
           list.add(processClause(clause));
            
        }

        if (list.isEmpty()) {
            System.out.println(0);
            System.exit(0);
        } else {
            Map<String, List<String>> prev = list.get(0);
            for (int i = 1; i < list.size(); i++) {


                Map<String, List<String>> next = list.get(i);
                List<String> keysPrev = new ArrayList<>();
                prev.forEach((a, b) -> keysPrev.add(a));

                List<String> keysNext = new ArrayList<>();
                next.forEach((a, b) -> keysNext.add(a));

                keysPrev.retainAll(keysNext);


                if (!keysPrev.isEmpty()) {

                    List<Integer> toRemove = new ArrayList<>();
                    int size = next.get(keysPrev.get(0)).size();
                    int aSize = prev.get(keysPrev.get(0)).size();
                    for (int j = 0; j < size; j++) {
                        boolean wasFound = false;

                        for (int k = 0; k < aSize; k++) {
                            int found = 0;
                            for (String key : keysPrev) {
                                List<String> a = prev.get(key);
                                List<String> b = next.get(key);

                                if (a.get(k).equals(b.get(j))) {
                                    found++;
                                }
                            }
                            if (found == keysPrev.size()) {
                                wasFound = true;
                                break;
                            }
                        }

                        if (!wasFound) {
                            toRemove.add(j);
                        }

                    }


                    Collections.sort(toRemove, (a, b) -> b - a);

                    for (Map.Entry<String, List<String>> entry : next.entrySet()) {
                        List<String> li = entry.getValue();
                        for (int rem : toRemove) {
                            li.remove(rem);
                        }
                    }


                }

                prev = next;

            }

            Map<String, List<String>> sub = new HashMap<>();


            for (Map<String, List<String>> myList : list) {
                if (myList.keySet().size() == n) {
                    System.out.println(myList.get("x1").size());
                    System.exit(0);
                }
                for (String mm : myList.keySet()) {
                    List<String> sublist = sub.get(mm);
                    if (sublist == null) {
                        sublist = new ArrayList<>();
                        sublist.add("0");
                        sublist.add("1");
                    }

                    sublist.retainAll(myList.get(mm));

                    sub.put(mm, sublist);
                }
            }

            int result = 1;
            for (Map.Entry<String, List<String>> v : sub.entrySet()) {
                result *= v.getValue().size();
            }

            System.out.println(result);
            System.exit(0);
        }

    }

    private static String[] readLine() {
        try (Scanner in = new Scanner(System.in)) {
            String a = in.nextLine(); // skip n
            String b = in.nextLine();
            return new String[] {a, b};
        }
    }

    private static Map<String, List<String>> processClause(String clause) {
        clause = clause.replace("(", "");
        clause = clause.replace(")", "");

        String[] literals = clause.split(" v ");
        boolean[] negative = new boolean[literals.length];


        for (int i = 0; i < literals.length; i++) {
            String literal = literals[i];
            if (literal.startsWith("~")) {
                literal = literal.replaceAll("~", "");
                negative[i] = true;
            }
            literals[i] = literal;
        }

        Map<String, List<String>> map = new HashMap<>();
        for (String literal : literals)
            map.put(literal, new ArrayList<>());

        BigInteger max = BigInteger.valueOf(2).pow(literals.length);
        for (BigInteger i = BigInteger.ZERO; i.compareTo(max) < 0; i = i.add(BigInteger.ONE)) {
            StringBuilder builder = new StringBuilder();
            String str = i.toString(2);

            if (str.length() != literals.length) {
                int size = literals.length - str.length();
                for (int j = 0; j < size; j++)
                    builder.append('0');
            }

            builder.append(str);

            StringBuilder original = new StringBuilder();
            original.append(builder.toString());
            for (int j = 0; j < literals.length; j++) {
                if (negative[j]) {
                    char s = builder.charAt(j);
                    char r = s == '0' ? '1' : '0';
                    builder.setCharAt(j, r);
                }
            }

            if (builder.toString().contains("1")) {
                for (int j = 0; j < literals.length; j++) {
                    String literal = literals[j];
                    String character = original.charAt(j) + "";
                    map.get(literal).add(character);
                }
            }

        }

        return map;

    }
}