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
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;


public class cnf {

    public static void main(String[] args) {
        final Scanner in = new Scanner(System.in);


        final int tasksNo = in.nextInt();

        boolean[] evaluations = new boolean[tasksNo];

        System.out.println(countSolutions(new Zdanie(in), evaluations, 0));

        System.exit(0);
    }

    private static int countSolutions(final Zdanie z, final boolean[] evaluations, final int start) {
        evaluations[start] = false;
        int result = 0;
        if (start < evaluations.length - 1) {
            result += countSolutions(z, evaluations, start + 1);
            evaluations[start] = true;
            result += countSolutions(z, evaluations, start + 1);
        } else {
            result += z.eval(evaluations) ? 1 : 0;
            evaluations[start] = true;
            result += z.eval(evaluations) ? 1 : 0;
        }
        return result % 1000000007;
    }

    private static class Zdanie {
        private Collection<Klauzula> sentences = new ArrayList<>(100);


        public Zdanie(final Scanner z) {
            z.nextLine();
            String next = z.nextLine();
            for (String s : next.split("\\^")) {
                sentences.add(new Klauzula(s.trim()));
            }

        }

        public void add(Klauzula kl) {
            sentences.add(kl);
        }

        public boolean eval(boolean[] values) {
            for (Klauzula literal : sentences) {
                if (!literal.eval(values)) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            for (Klauzula sentence : sentences) {
                if (sb.length() > 0) {
                    sb.append(" ^ ");
                }
                sb.append("(").append(sentence.toString()).append(")");
            }
            return sb.toString();
        }
    }

    private static class Klauzula {
        private Collection<Literal> literals;

        public Klauzula(int no) {
            literals = new ArrayList<>(no);
        }

        public Klauzula(final String next) {
            String noBrackets = next.substring(1, next.length() - 1);
            String[] strings = noBrackets.split("v");
            literals = new ArrayList<>(strings.length);
            for (String literal : strings) {
                addLiteral(new Literal(literal.trim()));
            }
        }

        public void addLiteral(Literal lit) {
            literals.add(lit);
        }

        public boolean eval(boolean[] values) {
            for (Literal literal : literals) {
                if (literal.eval(values[literal.getIndex()])) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            for (Literal literal : literals) {
                if (sb.length() > 0) {
                    sb.append(" v ");
                }
                sb.append(literal.toString());
            }
            return sb.toString();
        }
    }

    private static class Literal {
        private int index;
        private boolean negate;

        public Literal(int index) {
            this(index, false);
        }

        public Literal(int index, boolean negate) {

            this.index = index;
            this.negate = negate;
        }

        public Literal(final String literal) {
            if (literal.startsWith("x")) {
                index = (Integer.parseInt(literal.substring(1)));
                negate = false;
            } else {
                index = (Integer.parseInt(literal.substring(2)));
                negate = true;
            }
        }

        public boolean eval(boolean value) {
            return negate ? !value : value;
        }

        public int getIndex() {
            return index - 1;
        }

        public boolean isNegate() {
            return negate;
        }

        @Override
        public String toString() {
            return negate ? "~x" + index : "x" + index;
        }
    }

}