//#include <fstream> //#include <cstdlib> #include <iostream> #include <iomanip> #include <string> #include <sstream> #include <vector> #include <set> using namespace std; //const int pow2[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824}; int MOD = 1000000000 + 7; int pow2(int n) { if(n==0) return 1; if(n==1) return 2; long long r = pow2(n/2); if(n%2==0) { return (r*r) % MOD; } else { return (r*r*2) % MOD; } } int N; vector<vector<int> > V; int str2int(const string& s) { stringstream ss(s); int x; ss >> x; return x; } void parse(const string& f) { vector<int> c(N+1); stringstream ss(f); string term; while(ss >> term) { int pos=0; int len=term.size(); bool last = false; int znak = 1; if(term[0]=='(') { ++pos; --len; } if(term[term.size()-1]==')') { last = true; --len; } if(term[pos]=='~') { znak = -1; ++pos; --len;; } // pomijamy x ++pos; --len; int num = str2int(term.substr(pos,len)); c[num] = znak; if(last) { V.push_back(c); for(int i=0; i<(int)c.size(); ++i) c[i] = 0; } } } void print(set<int> s) { for(auto it=s.begin(); it!=s.end(); ++it) cout << *it << " "; cout << endl; } int compute(int n, set<int> s) { //cout << "compute +" << n << endl; set<int> s_plus = s; for(auto it=s.begin(); it!=s.end(); ++it) { int i = *it; if(V[i][n]==1) { //cout << " klauzula " << i << " spelniona" << endl; s_plus.erase(i); } } int partial_plus; if(s_plus.size()>0 && n<N) { partial_plus = compute(n+1, s_plus); } else if(s_plus.size()>0 && n==N) { partial_plus = 0; } else if(s_plus.size()==0 && n<=N) { partial_plus = pow2(N-n); } else { cout << "NIEMOZLIWE" << endl; exit(1); } /////////////////////////////////////////////////// //cout << "compute -" << n << endl; set<int> s_minus = s; for(auto it=s.begin(); it!=s.end(); ++it) { int i = *it; if(V[i][n]==-1) { //cout << " klauzula " << i << " spelniona" << endl; s_minus.erase(i); } } int partial_minus; if(s_minus.size()>0 && n<N) { partial_minus = compute(n+1, s_minus); } else if(s_minus.size()>0 && n==N) { partial_minus = 0; } else if(s_minus.size()==0 && n<=N) { partial_minus = pow2(N-n); } else { cout << "NIEMOZLIWE" << endl; exit(1); } return (partial_plus + partial_minus) % MOD; } int main() { //ifstream in("cnf_owca00.in"); cin >> N; //cout << N << endl; string f; getline(cin, f); getline(cin, f); parse(f); /* for(int i=0; i<(int)V.size(); ++i) { for(int j=1; j<=N; ++j) cout << setw(2) << V[i][j] << " "; cout << endl; } */ set<int> s; for(int i=0; i<(int)V.size(); ++i) s.insert(i); //cout << "START" << endl; int res = compute(1, s); cout << res << endl; }
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 | //#include <fstream> //#include <cstdlib> #include <iostream> #include <iomanip> #include <string> #include <sstream> #include <vector> #include <set> using namespace std; //const int pow2[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824}; int MOD = 1000000000 + 7; int pow2(int n) { if(n==0) return 1; if(n==1) return 2; long long r = pow2(n/2); if(n%2==0) { return (r*r) % MOD; } else { return (r*r*2) % MOD; } } int N; vector<vector<int> > V; int str2int(const string& s) { stringstream ss(s); int x; ss >> x; return x; } void parse(const string& f) { vector<int> c(N+1); stringstream ss(f); string term; while(ss >> term) { int pos=0; int len=term.size(); bool last = false; int znak = 1; if(term[0]=='(') { ++pos; --len; } if(term[term.size()-1]==')') { last = true; --len; } if(term[pos]=='~') { znak = -1; ++pos; --len;; } // pomijamy x ++pos; --len; int num = str2int(term.substr(pos,len)); c[num] = znak; if(last) { V.push_back(c); for(int i=0; i<(int)c.size(); ++i) c[i] = 0; } } } void print(set<int> s) { for(auto it=s.begin(); it!=s.end(); ++it) cout << *it << " "; cout << endl; } int compute(int n, set<int> s) { //cout << "compute +" << n << endl; set<int> s_plus = s; for(auto it=s.begin(); it!=s.end(); ++it) { int i = *it; if(V[i][n]==1) { //cout << " klauzula " << i << " spelniona" << endl; s_plus.erase(i); } } int partial_plus; if(s_plus.size()>0 && n<N) { partial_plus = compute(n+1, s_plus); } else if(s_plus.size()>0 && n==N) { partial_plus = 0; } else if(s_plus.size()==0 && n<=N) { partial_plus = pow2(N-n); } else { cout << "NIEMOZLIWE" << endl; exit(1); } /////////////////////////////////////////////////// //cout << "compute -" << n << endl; set<int> s_minus = s; for(auto it=s.begin(); it!=s.end(); ++it) { int i = *it; if(V[i][n]==-1) { //cout << " klauzula " << i << " spelniona" << endl; s_minus.erase(i); } } int partial_minus; if(s_minus.size()>0 && n<N) { partial_minus = compute(n+1, s_minus); } else if(s_minus.size()>0 && n==N) { partial_minus = 0; } else if(s_minus.size()==0 && n<=N) { partial_minus = pow2(N-n); } else { cout << "NIEMOZLIWE" << endl; exit(1); } return (partial_plus + partial_minus) % MOD; } int main() { //ifstream in("cnf_owca00.in"); cin >> N; //cout << N << endl; string f; getline(cin, f); getline(cin, f); parse(f); /* for(int i=0; i<(int)V.size(); ++i) { for(int j=1; j<=N; ++j) cout << setw(2) << V[i][j] << " "; cout << endl; } */ set<int> s; for(int i=0; i<(int)V.size(); ++i) s.insert(i); //cout << "START" << endl; int res = compute(1, s); cout << res << endl; } |