// tested by Hightail: https://github.com/dj3500/hightail #include <bits/stdc++.h> using namespace std; #define pb push_back #define INF 1001001001 #define FOR(i,n) for(int (i)=0;(i)<(n);++(i)) #define FORI(i,n) for(int (i)=1;(i)<=(n);++(i)) #define mp make_pair #define pii pair<int,int> #define ll long long #define vi vector<int> #define SZ(x) ((int)((x).size())) #define fi first #define se second #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); #define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k)); inline void pisz(int n) { printf("%d\n",n); } template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; } #define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl; #define ALL(t) t.begin(),t.end() #define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define TESTS wez(testow)while(testow--) #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i) #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i) #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define IOS ios_base::sync_with_stdio(0); template <typename T, typename R> void wywal (vector<R> &v, const T &lambda) { v.erase(remove_if(ALL(v), lambda), v.end()); } struct Dis { vector<bool> v; int A, B; int id; }; ostream& operator<<(ostream &s, const Dis &d) { s << '[' << d.A << ',' << d.B << ','; for (bool b : d.v) { s << b; } s << ']'; return s; } bool comp (const Dis &d1, const Dis &d2) { return mp(d1.B - d1.A, d1.id) < mp(d2.B - d2.A, d2.id); } bool comp2 (const Dis &d1, const Dis &d2) { return mp(mp(d1.A, d1.B), d1.id) < mp(mp(d2.A, d2.B), d2.id); } pair<int,bool> czytajZm (const string &t) { int pos = 1; if (t[0] == '~') ++pos; return mp(atoi(t.substr(pos).c_str()), t[0] != '~'); } Dis czytajDis (const string &t) { vi wedge; FOR(i,SZ(t)) if (t[i] == 'v') wedge.pb(i); vector<pair<int,bool>> vars; if (wedge.empty()) { vars.pb(czytajZm(t)); } else { vars.pb(czytajZm(t.substr(0,wedge[0]-1))); FOR(u,SZ(wedge)-1) { vars.pb(czytajZm(t.substr(wedge[u]+2, wedge[u+1]-wedge[u]-3))); } vars.pb(czytajZm(t.substr(wedge.back()+2, SZ(t)-wedge.back()-2))); } sort(ALL(vars)); Dis d; d.A = vars[0].fi; d.B = vars.back().fi; for (const pair<int,bool> &it : vars) d.v.pb(it.se); static int nextId = 1; d.id = nextId++; return d; } vector<Dis> czytaj (const string &s) { vector<Dis> res; vi xory; FOR(i,SZ(s)) if (s[i] == '^') xory.pb(i); //vector<pii> roz; if (xory.empty()) { res.pb(czytajDis(s.substr(1,SZ(s)-2))); //roz = {{1,SZ(s)-1}}; } else { res.pb(czytajDis(s.substr(1,xory[0]-3))); //roz = {{1,xory[0]-2}}; FOR(u,SZ(xory)-1) { res.pb(czytajDis(s.substr(xory[u]+3, xory[u+1]-xory[u]-5))); //roz.pb({xory[u]+3, xory[u+1]-2}); } res.pb(czytajDis(s.substr(xory.back()+3, SZ(s)-xory.back()-4))); //roz.pb({xory.back()+3, SZ(s)-1}); } return res; } const int mod = 1000000007; ll _val; struct Node { Node* x[2]; bool endsHere; ll aug = 0; Node () { FOR(i,2) x[i] = 0; endsHere = false; aug = 0; } bool czyJuzJest (const vector<bool> &v, int pos) { if (endsHere) return true; if (pos == -1) return false; if (0 == x[v[pos]]) return false; return x[v[pos]]->czyJuzJest(v,pos-1); } ll findAug (const vector<bool> &v, int pos) { if (pos == -1) return aug; if (0 == x[v[pos]]) return false; return x[v[pos]]->findAug(v, pos-1); } void dodaj (const vector<bool> &v, int pos) { if (pos == -1) { endsHere = 1; return; } if (0 == x[v[pos]]) { x[v[pos]] = new Node(); } x[v[pos]]->dodaj(v, pos-1); } void dodajval (const vector<bool> &v, int pos) { aug += _val; if (aug > mod) aug -= mod; if (pos == -1) { return; } if (0 == x[v[pos]]) { assert(0); x[v[pos]] = new Node(); } x[v[pos]]->dodajval(v, pos-1); } }; const int N = 1000007; Node root[N]; bool juzJest (const Dis &d) { // jesli na swojej drodze spotkam cos istniejacego, to fail REPD(x,d.B,d.A) { if (root[x].czyJuzJest(d.v, x - d.A)) return true; } return false; //return root[d.B].czyJuzJest(d.v, SZ(d.v)-1); } void dodaj (const Dis &d) { root[d.B].dodaj(d.v, SZ(d.v) - 1); } ll compatible (const Dis &d) { ll res = 0; REPD(x,d.B,d.A) { res += root[x].findAug(d.v, x - d.A); } return res % mod; } ll res[N]; ll pow2[N], powinv2[N]; const ll inv2 = 500000004; vi endingAt[N]; int main () { pow2[0] = 1; REP(x,1,1000000) pow2[x] = pow2[x-1] * 2 % mod; powinv2[0] = 1; REP(x,1,1000000) powinv2[x] = powinv2[x-1] * inv2 % mod; IOS int n; cin >> n; string linia; getline(cin, linia); getline(cin, linia); vector<Dis> D = czytaj(linia); //DBG(D); sort(ALL(D), comp); wywal(D, [] (const Dis &d) { if (juzJest(d)) { return 1; } else { dodaj(d); return 0; } }); //DBG(D); sort(ALL(D), comp2); FOR(j,SZ(D)) { endingAt[D[j].B].pb(j); } int nextEndToCheck = 0; ll sumOfResForAlreadyEnded = 0; FOR(j,SZ(D)) { while (nextEndToCheck < D[j].A) { for (int i : endingAt[nextEndToCheck]) { sumOfResForAlreadyEnded += res[i]; } ++nextEndToCheck; sumOfResForAlreadyEnded %= mod; } res[j] = pow2[n - (D[j].B - D[j].A + 1)]; res[j] -= powinv2[D[j].B - D[j].A + 1] * sumOfResForAlreadyEnded % mod; res[j] -= compatible(D[j]) * powinv2[D[j].B] % mod; res[j] %= mod; // wrzuc to do trie _val = res[j] * pow2[D[j].B] % mod; if (_val < 0) _val += mod; root[D[j].B].dodajval(D[j].v, SZ(D[j].v) - 1); } // wynik ll sumres = 0; FOR(j,SZ(D)) sumres += res[j]; sumres = pow2[n] - sumres; sumres %= mod; if (sumres < 0) sumres += mod; cout << sumres; }
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 243 244 245 | // tested by Hightail: https://github.com/dj3500/hightail #include <bits/stdc++.h> using namespace std; #define pb push_back #define INF 1001001001 #define FOR(i,n) for(int (i)=0;(i)<(n);++(i)) #define FORI(i,n) for(int (i)=1;(i)<=(n);++(i)) #define mp make_pair #define pii pair<int,int> #define ll long long #define vi vector<int> #define SZ(x) ((int)((x).size())) #define fi first #define se second #define wez(n) int (n); scanf("%d",&(n)); #define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m)); #define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k)); inline void pisz(int n) { printf("%d\n",n); } template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; } #define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl; #define ALL(t) t.begin(),t.end() #define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define TESTS wez(testow)while(testow--) #define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i) #define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i) #define REMAX(a,b) (a)=max((a),(b)); #define REMIN(a,b) (a)=min((a),(b)); #define IOS ios_base::sync_with_stdio(0); template <typename T, typename R> void wywal (vector<R> &v, const T &lambda) { v.erase(remove_if(ALL(v), lambda), v.end()); } struct Dis { vector<bool> v; int A, B; int id; }; ostream& operator<<(ostream &s, const Dis &d) { s << '[' << d.A << ',' << d.B << ','; for (bool b : d.v) { s << b; } s << ']'; return s; } bool comp (const Dis &d1, const Dis &d2) { return mp(d1.B - d1.A, d1.id) < mp(d2.B - d2.A, d2.id); } bool comp2 (const Dis &d1, const Dis &d2) { return mp(mp(d1.A, d1.B), d1.id) < mp(mp(d2.A, d2.B), d2.id); } pair<int,bool> czytajZm (const string &t) { int pos = 1; if (t[0] == '~') ++pos; return mp(atoi(t.substr(pos).c_str()), t[0] != '~'); } Dis czytajDis (const string &t) { vi wedge; FOR(i,SZ(t)) if (t[i] == 'v') wedge.pb(i); vector<pair<int,bool>> vars; if (wedge.empty()) { vars.pb(czytajZm(t)); } else { vars.pb(czytajZm(t.substr(0,wedge[0]-1))); FOR(u,SZ(wedge)-1) { vars.pb(czytajZm(t.substr(wedge[u]+2, wedge[u+1]-wedge[u]-3))); } vars.pb(czytajZm(t.substr(wedge.back()+2, SZ(t)-wedge.back()-2))); } sort(ALL(vars)); Dis d; d.A = vars[0].fi; d.B = vars.back().fi; for (const pair<int,bool> &it : vars) d.v.pb(it.se); static int nextId = 1; d.id = nextId++; return d; } vector<Dis> czytaj (const string &s) { vector<Dis> res; vi xory; FOR(i,SZ(s)) if (s[i] == '^') xory.pb(i); //vector<pii> roz; if (xory.empty()) { res.pb(czytajDis(s.substr(1,SZ(s)-2))); //roz = {{1,SZ(s)-1}}; } else { res.pb(czytajDis(s.substr(1,xory[0]-3))); //roz = {{1,xory[0]-2}}; FOR(u,SZ(xory)-1) { res.pb(czytajDis(s.substr(xory[u]+3, xory[u+1]-xory[u]-5))); //roz.pb({xory[u]+3, xory[u+1]-2}); } res.pb(czytajDis(s.substr(xory.back()+3, SZ(s)-xory.back()-4))); //roz.pb({xory.back()+3, SZ(s)-1}); } return res; } const int mod = 1000000007; ll _val; struct Node { Node* x[2]; bool endsHere; ll aug = 0; Node () { FOR(i,2) x[i] = 0; endsHere = false; aug = 0; } bool czyJuzJest (const vector<bool> &v, int pos) { if (endsHere) return true; if (pos == -1) return false; if (0 == x[v[pos]]) return false; return x[v[pos]]->czyJuzJest(v,pos-1); } ll findAug (const vector<bool> &v, int pos) { if (pos == -1) return aug; if (0 == x[v[pos]]) return false; return x[v[pos]]->findAug(v, pos-1); } void dodaj (const vector<bool> &v, int pos) { if (pos == -1) { endsHere = 1; return; } if (0 == x[v[pos]]) { x[v[pos]] = new Node(); } x[v[pos]]->dodaj(v, pos-1); } void dodajval (const vector<bool> &v, int pos) { aug += _val; if (aug > mod) aug -= mod; if (pos == -1) { return; } if (0 == x[v[pos]]) { assert(0); x[v[pos]] = new Node(); } x[v[pos]]->dodajval(v, pos-1); } }; const int N = 1000007; Node root[N]; bool juzJest (const Dis &d) { // jesli na swojej drodze spotkam cos istniejacego, to fail REPD(x,d.B,d.A) { if (root[x].czyJuzJest(d.v, x - d.A)) return true; } return false; //return root[d.B].czyJuzJest(d.v, SZ(d.v)-1); } void dodaj (const Dis &d) { root[d.B].dodaj(d.v, SZ(d.v) - 1); } ll compatible (const Dis &d) { ll res = 0; REPD(x,d.B,d.A) { res += root[x].findAug(d.v, x - d.A); } return res % mod; } ll res[N]; ll pow2[N], powinv2[N]; const ll inv2 = 500000004; vi endingAt[N]; int main () { pow2[0] = 1; REP(x,1,1000000) pow2[x] = pow2[x-1] * 2 % mod; powinv2[0] = 1; REP(x,1,1000000) powinv2[x] = powinv2[x-1] * inv2 % mod; IOS int n; cin >> n; string linia; getline(cin, linia); getline(cin, linia); vector<Dis> D = czytaj(linia); //DBG(D); sort(ALL(D), comp); wywal(D, [] (const Dis &d) { if (juzJest(d)) { return 1; } else { dodaj(d); return 0; } }); //DBG(D); sort(ALL(D), comp2); FOR(j,SZ(D)) { endingAt[D[j].B].pb(j); } int nextEndToCheck = 0; ll sumOfResForAlreadyEnded = 0; FOR(j,SZ(D)) { while (nextEndToCheck < D[j].A) { for (int i : endingAt[nextEndToCheck]) { sumOfResForAlreadyEnded += res[i]; } ++nextEndToCheck; sumOfResForAlreadyEnded %= mod; } res[j] = pow2[n - (D[j].B - D[j].A + 1)]; res[j] -= powinv2[D[j].B - D[j].A + 1] * sumOfResForAlreadyEnded % mod; res[j] -= compatible(D[j]) * powinv2[D[j].B] % mod; res[j] %= mod; // wrzuc to do trie _val = res[j] * pow2[D[j].B] % mod; if (_val < 0) _val += mod; root[D[j].B].dodajval(D[j].v, SZ(D[j].v) - 1); } // wynik ll sumres = 0; FOR(j,SZ(D)) sumres += res[j]; sumres = pow2[n] - sumres; sumres %= mod; if (sumres < 0) sumres += mod; cout << sumres; } |