#include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define PII pair<int,int> #define mp make_pair #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define debug(x) {cerr <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} using namespace std; const int max_n = 1e6 + 5; int n; vector< pair<PII, VI> > klauzule; int znaczek[max_n]; PII zjedz_literal() { int sgn = 0; char C = getchar(); if (C == 'x') sgn = 0; else { sgn = 1; C = getchar(); } int licz = 0; C = getchar(); while (C>='0' && C<='9') { licz = 10*licz + (C-'0'); C = getchar(); } return mp(licz, sgn); } int value[max_n]; void parse_input() { scanf("%d\n", &n); char C = getchar(); while (1) { if (C == '(') { VI wartosci; PII p1 = zjedz_literal(); int mini = p1.st, maxi = p1.st; value[p1.st] = p1.nd; C = getchar(); while (C == 'v') { C = getchar(); PII p = zjedz_literal(); value[p.st] = p.nd; mini = min(mini, p.st); maxi = max(maxi, p.st); C = getchar(); } FOR(j,mini, maxi+1) wartosci.pb(value[j]); klauzule.pb(mp(mp(mini, maxi), wartosci)); if (C=='\n') break; C = getchar(); if (C=='\n') break; C = getchar(); if (C=='\n') break; C = getchar(); if (C=='\n') break; } } } void brute_force() { int ans = 0; FOR(i,0,1<<n) { bool ok = true; FOR(j,0,klauzule.size()) { if (!ok) break; bool loc = false; int pocz = klauzule[j].st.st; FOR(k, 0, klauzule[j].nd.size()) { int kto = pocz + k; int co = klauzule[j].nd[k]; if ((i & (1<<(kto-1)))) { if (co==0) loc = true; } else { if (co!=0) loc = true; } } if (!loc) ok = false; } if (ok) ans++; } printf("%d\n", ans); } int K; int mod = 1e9 + 7; int jd = (1+mod)/2; int jdPW[max_n]; int minus = (mod - 1); int soFar[max_n]; vector<int> val[max_n]; vector<PII> konce[max_n]; bool zgadza(int k1, int k2) { int p1 = klauzule[k1].st.st; int p2 = klauzule[k2].st.st; int kon1 = klauzule[k1].st.nd; int kon2 = klauzule[k2].st.nd; FOR(i, max(p1,p2), min(kon1, kon2) + 1) { if (klauzule[k1].nd[i-p1] != klauzule[k2].nd[i-p2]) return false; } return true; } bool zawiera(int k1, int k2) { int p1 = klauzule[k1].st.st; int p2 = klauzule[k2].st.st; int kon1 = klauzule[k1].st.nd; int kon2 = klauzule[k2].st.nd; if (p1 <= p2 & kon1 >= kon2) return true; return false; } void kwadraciak() { K = klauzule.size(); FOR(i,0,K) { konce[klauzule[i].st.nd].pb(mp(-klauzule[i].st.st, i)); } soFar[0] = 1; FOR(i,1,n+1) { soFar[i] = soFar[i-1]; sort(all(konce[i])); FOR(j, 0, konce[i].size()) { int kl = konce[i][j].nd; /*FOR(z, klauzule[kl].st.st, klauzule[kl].st.nd+1) { cout << "x_" << z << " = " << klauzule[kl].nd[z-klauzule[kl].st.st] <<", "; } cout << endl;*/ bool ok = true; FOR(l,0,j) { if (zgadza(kl, konce[i][l].nd)) { ok = false; break; } } if (!ok) { val[i].pb(0); continue; } int myval = 0; int poczateczek = klauzule[kl].st.st; FORD(m, i-1, poczateczek) { if (!ok) break; FOR(l, 0, konce[m].size()) { if (!ok) break; if (zgadza(kl, konce[m][l].nd)) { if (zawiera(kl, konce[m][l].nd)) { ok = false; if (!ok) break; } myval = (myval + mod - ((jdPW[i-m] * 1LL * val[m][l]) % mod)); if (myval >= mod) myval -= mod; } } } myval = (myval + mod - ((jdPW[i-poczateczek+1] * 1LL * soFar[poczateczek-1]) % mod)); if (myval >= mod) myval -= mod; if (!ok) { val[i].pb(0); continue; } val[i].pb(myval); soFar[i] += myval; if (soFar[i] >= mod) soFar[i] -= mod; } } int ANS = soFar[n]; FOR(i,0,n) { ANS = ANS + ANS; if (ANS >= mod) ANS -= mod; } printf("%d\n", ANS); } int main() { parse_input(); jdPW[0] = 1; jdPW[1] = jd; FOR(i,2,n + 1) { jdPW[i] = (jdPW[i-1] * 1LL * jd) % mod; } //brute_force(); kwadraciak(); }
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 | #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i, a, b) for(int i = (a); i >= (b); --i) #define VAR(v, i) __typeof(i) v=(i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define all(v) (v).begin(),(v).end() #define PII pair<int,int> #define mp make_pair #define st first #define nd second #define pb push_back #define lint long long int #define VI vector<int> #define debug(x) {cerr <<#x <<" = " <<x <<endl; } #define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } #define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }} #define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }} #define make( x) int (x); scanf("%d",&(x)); #define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y)); #define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z)); #define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} #define IOS ios_base::sync_with_stdio(0) #define HEAP priority_queue #define read( x) scanf("%d",&(x)); #define read2( x, y) scanf("%d%d",&(x),&(y)); #define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z)); #define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t)); #define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);} using namespace std; const int max_n = 1e6 + 5; int n; vector< pair<PII, VI> > klauzule; int znaczek[max_n]; PII zjedz_literal() { int sgn = 0; char C = getchar(); if (C == 'x') sgn = 0; else { sgn = 1; C = getchar(); } int licz = 0; C = getchar(); while (C>='0' && C<='9') { licz = 10*licz + (C-'0'); C = getchar(); } return mp(licz, sgn); } int value[max_n]; void parse_input() { scanf("%d\n", &n); char C = getchar(); while (1) { if (C == '(') { VI wartosci; PII p1 = zjedz_literal(); int mini = p1.st, maxi = p1.st; value[p1.st] = p1.nd; C = getchar(); while (C == 'v') { C = getchar(); PII p = zjedz_literal(); value[p.st] = p.nd; mini = min(mini, p.st); maxi = max(maxi, p.st); C = getchar(); } FOR(j,mini, maxi+1) wartosci.pb(value[j]); klauzule.pb(mp(mp(mini, maxi), wartosci)); if (C=='\n') break; C = getchar(); if (C=='\n') break; C = getchar(); if (C=='\n') break; C = getchar(); if (C=='\n') break; } } } void brute_force() { int ans = 0; FOR(i,0,1<<n) { bool ok = true; FOR(j,0,klauzule.size()) { if (!ok) break; bool loc = false; int pocz = klauzule[j].st.st; FOR(k, 0, klauzule[j].nd.size()) { int kto = pocz + k; int co = klauzule[j].nd[k]; if ((i & (1<<(kto-1)))) { if (co==0) loc = true; } else { if (co!=0) loc = true; } } if (!loc) ok = false; } if (ok) ans++; } printf("%d\n", ans); } int K; int mod = 1e9 + 7; int jd = (1+mod)/2; int jdPW[max_n]; int minus = (mod - 1); int soFar[max_n]; vector<int> val[max_n]; vector<PII> konce[max_n]; bool zgadza(int k1, int k2) { int p1 = klauzule[k1].st.st; int p2 = klauzule[k2].st.st; int kon1 = klauzule[k1].st.nd; int kon2 = klauzule[k2].st.nd; FOR(i, max(p1,p2), min(kon1, kon2) + 1) { if (klauzule[k1].nd[i-p1] != klauzule[k2].nd[i-p2]) return false; } return true; } bool zawiera(int k1, int k2) { int p1 = klauzule[k1].st.st; int p2 = klauzule[k2].st.st; int kon1 = klauzule[k1].st.nd; int kon2 = klauzule[k2].st.nd; if (p1 <= p2 & kon1 >= kon2) return true; return false; } void kwadraciak() { K = klauzule.size(); FOR(i,0,K) { konce[klauzule[i].st.nd].pb(mp(-klauzule[i].st.st, i)); } soFar[0] = 1; FOR(i,1,n+1) { soFar[i] = soFar[i-1]; sort(all(konce[i])); FOR(j, 0, konce[i].size()) { int kl = konce[i][j].nd; /*FOR(z, klauzule[kl].st.st, klauzule[kl].st.nd+1) { cout << "x_" << z << " = " << klauzule[kl].nd[z-klauzule[kl].st.st] <<", "; } cout << endl;*/ bool ok = true; FOR(l,0,j) { if (zgadza(kl, konce[i][l].nd)) { ok = false; break; } } if (!ok) { val[i].pb(0); continue; } int myval = 0; int poczateczek = klauzule[kl].st.st; FORD(m, i-1, poczateczek) { if (!ok) break; FOR(l, 0, konce[m].size()) { if (!ok) break; if (zgadza(kl, konce[m][l].nd)) { if (zawiera(kl, konce[m][l].nd)) { ok = false; if (!ok) break; } myval = (myval + mod - ((jdPW[i-m] * 1LL * val[m][l]) % mod)); if (myval >= mod) myval -= mod; } } } myval = (myval + mod - ((jdPW[i-poczateczek+1] * 1LL * soFar[poczateczek-1]) % mod)); if (myval >= mod) myval -= mod; if (!ok) { val[i].pb(0); continue; } val[i].pb(myval); soFar[i] += myval; if (soFar[i] >= mod) soFar[i] -= mod; } } int ANS = soFar[n]; FOR(i,0,n) { ANS = ANS + ANS; if (ANS >= mod) ANS -= mod; } printf("%d\n", ANS); } int main() { parse_input(); jdPW[0] = 1; jdPW[1] = jd; FOR(i,2,n + 1) { jdPW[i] = (jdPW[i-1] * 1LL * jd) % mod; } //brute_force(); kwadraciak(); } |