#include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <cassert> using namespace std; const int L = 15000007; const int N = 1000007; const int M = 2000007; char s[L]; int n; int p[N]; int k[N]; int po[N]; int ko[N]; inline int wb(int x) { if(x < 0) return -x; return x; } bool cmp(int a, int b) { return wb(a) < wb(b); } bool cmp2(int a, int b) { if(ko[a] == ko[b]) { if(po[a] == po[b]) return a < b; return po[a] > po[b]; } return ko[a] < ko[b]; } int mod = 1e9 + 7; int inf = 1e8; int dp[N]; int pot[N]; int potr[N]; int sl[M]; int suf[M]; int rk[M]; int lcp[M]; int kt[M]; int zas[M]; pair <long long, int> spom[M]; int D1[M]; void insert1(int x, int w) { for(; x < M; x += (x&-x)) D1[x] = max(D1[x], w); } int query1(int x) { int res = 0; for(; x > 0; x -= (x&-x)) res = max(res, D1[x]); return res; } long long D2[M]; long long D4[M]; void insert2(long long (&D)[M], int x, int w) { x++; for(; x < M; x += (x&-x)) D[x] += w; } long long query2(long long (&D)[M], int x) { x++; long long res = 0; for(; x > 0; x -= (x&-x)) res += D[x]; return res; } int D3[M]; void insert3(int x, int w) { x++; for(; x < M; x += (x&-x)) D3[x] += w; } int query3(int x) { x++; int res = 0; for(; x > 0; x -= (x&-x)) res += D3[x]; return res; } int potmod(long long x, int mm) { long long res = 1; while(mm > 0) { if(mm & 1) res = (res * x) % mod; x = (x * x) % mod; mm /= 2; } return res; } int main() { cin >> n; getchar_unlocked(); int len = 0; char znn; while(1) { znn = getchar_unlocked(); if(znn == '\n' || znn == EOF) break; s[len++] = znn; } int idd = 0; vector <int> tab; int poz = 0; for(int i = 0; s[i]; ++i) { if(s[i] == '(' || s[i] == ' ' || s[i] == '^'); else if(s[i] == ')') { idd++; sort(tab.begin(), tab.end(), cmp); po[idd] = wb(tab.front()); ko[idd] = wb(tab.back()); p[idd] = poz; for(int j = 0; j < (int) tab.size(); ++j) { kt[poz] = idd; sl[poz++] = tab[j]; } k[idd] = poz - 1; sl[poz++] = inf + idd; tab.clear(); } else if(s[i] == '~' || s[i] == 'x') { int kk = 1; if(s[i] == '~') { kk = -1; i++; } i++; int war = 0; while('0' <= s[i] && s[i] <= '9') { war *= 10; war += s[i] - '0'; i++; } i--; war *= kk; tab.push_back(war); } } vector <int> kol(idd); for(int i = 0; i < idd; ++i) kol[i] = i + 1; sort(kol.begin(), kol.end(), cmp2); for(int i = 0; i < idd; ++i) { sl[k[kol[i]] + 1] = inf - i; } for(int i = 0; i < poz; ++i) rk[i] = sl[i]; for(int it = 0; it < 21; ++it) { int len = (1 << it); for(int i = 0; i < poz; ++i) { spom[i].first = rk[i]; spom[i].first <<= 30; if(i + len < poz) spom[i].first += rk[i + len]; spom[i].second = i; } sort(spom, spom + poz); int zz = 0; for(int i = 0; i < poz; ++i) { if(i > 0 && spom[i - 1].first != spom[i].first) zz++; rk[spom[i].second] = zz; } } for(int i = 0; i < poz; ++i) suf[rk[i]] = i; for(int i = 0; i < poz; ++i) { int pomm = 0; if(i > 0) pomm = max(pomm, lcp[rk[i - 1]] - 1); if(rk[i] + 1 < poz) while(sl[i + pomm] == sl[suf[rk[i] + 1] + pomm]) pomm++; lcp[rk[i]] = pomm; } for(int i = 0; i < poz; ++i) { zas[i] = query1(k[kt[suf[i]]] - suf[i] + 1); insert1(lcp[i] + 1, i + 1); } pot[0] = 1; for(int i = 1; i <= n + 1; ++i) pot[i] = (2 * pot[i - 1]) % mod; potr[n + 1] = potmod(pot[n + 1], mod - 2); for(int i = n; i >= 0; --i) potr[i] = (2 * potr[i + 1]) % mod; for(int i = 0; i < idd; ++i) { int u = kol[i]; long long ww = pot[po[u] - 1]; int okk = 0; for(int j = p[u]; j <= k[u]; ++j) if(query3(rk[j]) > 0) okk = 1; long long rr = query2(D4, po[u]) % mod; rr *= pot[po[u]]; ww -= rr; ww -= query2(D2, rk[p[u]]); ww %= mod; ww = (ww + mod) % mod; if(okk) ww = 0; dp[u] = ww; for(int j = p[u]; j <= k[u]; ++j) { insert2(D2, zas[rk[j]], dp[u]); insert2(D2, rk[j] + 1, -dp[u]); } insert3(zas[rk[p[u]]], 1); insert3(rk[p[u]] + 1, -1); rr = dp[u]; rr *= potr[ko[u] + 1]; rr %= mod; insert2(D4, ko[u] + 1, rr); } long long wynik = pot[n]; for(int i = 1; i <= idd; ++i) { wynik -= ((long long) dp[i] * pot[n - ko[i]]) % mod; } wynik %= mod; wynik = (wynik + mod) % mod; cout << wynik << endl; return 0; }
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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | #include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <cassert> using namespace std; const int L = 15000007; const int N = 1000007; const int M = 2000007; char s[L]; int n; int p[N]; int k[N]; int po[N]; int ko[N]; inline int wb(int x) { if(x < 0) return -x; return x; } bool cmp(int a, int b) { return wb(a) < wb(b); } bool cmp2(int a, int b) { if(ko[a] == ko[b]) { if(po[a] == po[b]) return a < b; return po[a] > po[b]; } return ko[a] < ko[b]; } int mod = 1e9 + 7; int inf = 1e8; int dp[N]; int pot[N]; int potr[N]; int sl[M]; int suf[M]; int rk[M]; int lcp[M]; int kt[M]; int zas[M]; pair <long long, int> spom[M]; int D1[M]; void insert1(int x, int w) { for(; x < M; x += (x&-x)) D1[x] = max(D1[x], w); } int query1(int x) { int res = 0; for(; x > 0; x -= (x&-x)) res = max(res, D1[x]); return res; } long long D2[M]; long long D4[M]; void insert2(long long (&D)[M], int x, int w) { x++; for(; x < M; x += (x&-x)) D[x] += w; } long long query2(long long (&D)[M], int x) { x++; long long res = 0; for(; x > 0; x -= (x&-x)) res += D[x]; return res; } int D3[M]; void insert3(int x, int w) { x++; for(; x < M; x += (x&-x)) D3[x] += w; } int query3(int x) { x++; int res = 0; for(; x > 0; x -= (x&-x)) res += D3[x]; return res; } int potmod(long long x, int mm) { long long res = 1; while(mm > 0) { if(mm & 1) res = (res * x) % mod; x = (x * x) % mod; mm /= 2; } return res; } int main() { cin >> n; getchar_unlocked(); int len = 0; char znn; while(1) { znn = getchar_unlocked(); if(znn == '\n' || znn == EOF) break; s[len++] = znn; } int idd = 0; vector <int> tab; int poz = 0; for(int i = 0; s[i]; ++i) { if(s[i] == '(' || s[i] == ' ' || s[i] == '^'); else if(s[i] == ')') { idd++; sort(tab.begin(), tab.end(), cmp); po[idd] = wb(tab.front()); ko[idd] = wb(tab.back()); p[idd] = poz; for(int j = 0; j < (int) tab.size(); ++j) { kt[poz] = idd; sl[poz++] = tab[j]; } k[idd] = poz - 1; sl[poz++] = inf + idd; tab.clear(); } else if(s[i] == '~' || s[i] == 'x') { int kk = 1; if(s[i] == '~') { kk = -1; i++; } i++; int war = 0; while('0' <= s[i] && s[i] <= '9') { war *= 10; war += s[i] - '0'; i++; } i--; war *= kk; tab.push_back(war); } } vector <int> kol(idd); for(int i = 0; i < idd; ++i) kol[i] = i + 1; sort(kol.begin(), kol.end(), cmp2); for(int i = 0; i < idd; ++i) { sl[k[kol[i]] + 1] = inf - i; } for(int i = 0; i < poz; ++i) rk[i] = sl[i]; for(int it = 0; it < 21; ++it) { int len = (1 << it); for(int i = 0; i < poz; ++i) { spom[i].first = rk[i]; spom[i].first <<= 30; if(i + len < poz) spom[i].first += rk[i + len]; spom[i].second = i; } sort(spom, spom + poz); int zz = 0; for(int i = 0; i < poz; ++i) { if(i > 0 && spom[i - 1].first != spom[i].first) zz++; rk[spom[i].second] = zz; } } for(int i = 0; i < poz; ++i) suf[rk[i]] = i; for(int i = 0; i < poz; ++i) { int pomm = 0; if(i > 0) pomm = max(pomm, lcp[rk[i - 1]] - 1); if(rk[i] + 1 < poz) while(sl[i + pomm] == sl[suf[rk[i] + 1] + pomm]) pomm++; lcp[rk[i]] = pomm; } for(int i = 0; i < poz; ++i) { zas[i] = query1(k[kt[suf[i]]] - suf[i] + 1); insert1(lcp[i] + 1, i + 1); } pot[0] = 1; for(int i = 1; i <= n + 1; ++i) pot[i] = (2 * pot[i - 1]) % mod; potr[n + 1] = potmod(pot[n + 1], mod - 2); for(int i = n; i >= 0; --i) potr[i] = (2 * potr[i + 1]) % mod; for(int i = 0; i < idd; ++i) { int u = kol[i]; long long ww = pot[po[u] - 1]; int okk = 0; for(int j = p[u]; j <= k[u]; ++j) if(query3(rk[j]) > 0) okk = 1; long long rr = query2(D4, po[u]) % mod; rr *= pot[po[u]]; ww -= rr; ww -= query2(D2, rk[p[u]]); ww %= mod; ww = (ww + mod) % mod; if(okk) ww = 0; dp[u] = ww; for(int j = p[u]; j <= k[u]; ++j) { insert2(D2, zas[rk[j]], dp[u]); insert2(D2, rk[j] + 1, -dp[u]); } insert3(zas[rk[p[u]]], 1); insert3(rk[p[u]] + 1, -1); rr = dp[u]; rr *= potr[ko[u] + 1]; rr %= mod; insert2(D4, ko[u] + 1, rr); } long long wynik = pot[n]; for(int i = 1; i <= idd; ++i) { wynik -= ((long long) dp[i] * pot[n - ko[i]]) % mod; } wynik %= mod; wynik = (wynik + mod) % mod; cout << wynik << endl; return 0; } |