版本比较
`
正文
增加 139 行删除 176 行
12345678910111213141516171819202142223242526272829303132333435363738394041424344454647111213141516171819484950515253545556575859212223606162636425262728293031323334353637383940414243444546474865666768697071727374757677785051527980818283845657585960616263858687656667686970717273747576777879808182838488899091929394959697989910086878889101102103104105106107108109919293949596979899100101102110111104105112113114115116117118119120121122123124125107108109110111112126127128129130131132133134135114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175136177178179180181182137138139140141142143144145146147148149185186187188189190191192193194195196197198150151152153154155156157200201158159160161162163164165166167```#include<bits/stdc++.h>#include<map>#include<set>#include<list>#include<queue>#include<deque>#include<stack>#include<cmath>#include<cstdio>#include<vector>#include<cstring>#include<sstream>#include<iostream>#include<algorithm>#define YES puts("YES")#define Yes puts("Yes")#define NO puts("NO")#define No puts("No")#define b_i __int128#define ll long long#define pii pair<int, int>#define ull unsigned long long#define x first#define y second#define pr pair#define dl double#define pll pr<ll, ll>#define pull pr<ull, ull>#define pii pr<int, int>#define pdd pr<dl, dl>#define ld long dl#define vc vector#define vci vc<int>#define vcl vc<ll>#define vcd vc<dl>#define vcs vc<string>#define vcp vc<pii >#define ps push#define pp pop#define frt front#define bck back#define psf push_front#define psb push_back#define ppb pop_back#define ppf pop_front#define bgn beginusing namespace std;int n, m, t, a[4], b[4];int nw;struct F{ int f[8][16]; F operator *(const F x) const{ F ans; for(int i = 0; i < 8; i++) for(int j = 0; j < 16; j++) ans.f[i][j] = x.f[i][f[i][j]]; return ans;const ll md = 1e9 + 7;const ll mod = 1e9 + 7;const dl esp = 1e-12;const int inf = 2e9;const ll INF = 2e18;inline int read(){ char c = getchar(); int ans = 0, cnt = 1; while(c < '0' || c > '9'){ if(c == '-') cnt = -1; c = getchar(); } void unit(){ for(int i = 0; i < 8; i++) for(int j = 0; j < 16; j++) f[i][j] = j; while(c >= '0' && c <= '9'){ ans *= 10; ans += (c - '0'); c = getchar(); } void init(){ for(int i = 0; i < 8; i++) for(int j = 0; j < 16; j++) f[i][j] = 0; } void print(){ for(int i = 0; i < 8; i++) for(int j = 0; j < 16; j++){ cout << i << ':'; for(int k = 0; k < 4; k++) cout << ((j >> k) & 1); cout << "->"; for(int k = 0; k < 4; k++) cout << ((f[i][j] >> k) & 1); cout << endl; } cout << endl; }} to[200005], ans;int f[200005], is[200005], dp[200005];ll sz[200005], cnt[200005];vci g[200005];vc<ll> qz1[200005];vc<F> qz2[200005];F ksm(F a, int b){ F ans; ans.unit(); return ans * cnt;}inline void write(int x){ if(x < 0){ putchar('-'); x = -x; } if(x > 9) write(x / 10); putchar(x % 10 + '0');}ll ksm(ll a, ll b){ ll ans = 1; a %= md; while(b){ if(b % 2) ans = ans * a; a = a * a; b /= 2; if(b % 2) ans = ans * a % md; a = a * a % md; b >>= 1; } return ans;}void dfs1(int u){// cout << u << endl; for(int v : g[u]){ dfs1(v); dp[u] = max(dp[u], dp[v]); } dp[u]++; return ;ll rnd(ll l, ll r){ return l + rand() % (r - l + 1);}void dfs2(int u, int d){// if(d > 50){// cout << u << "!!!!!";// exit(0);// }// cout << u << endl; if(!is[u]){ sz[u] = 1; return ; } to[u].unit(); sz[u] = 0; for(int v : g[u]){ qz1[u].psb(sz[u]); qz2[u].psb(to[u]); dfs2(v, d + 1); sz[u] += sz[v] * cnt[v]; to[u] = to[u] * ksm(to[v], cnt[v]); } sz[u] = min(sz[u], (ll)1e9);mt19937_64 RD(chrono::system_clock::now().time_since_epoch().count());int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};int n, m;int a[2000005], b[2000005], p[2000005], is[2000005], cnt, id[2000005];ull hsh[2000005];vc<pii> g[2000005];ull ans, pw[2000005];map<ull, ull> hv, vis;int find(int u){ if(p[u] == u) return u; return p[u] = find(p[u]);}F dfs3(int u){ if(t >= cnt[u] * sz[u]){ t -= cnt[u] * sz[u]; return ksm(to[u], cnt[u]);void get(int u, int f){ for(pii e : g[u]){ int v = e.x; if(v != f){ id[e.y] = v; get(v, u); hsh[u] ^= hsh[v]; } } F ans = ksm(to[u], t / sz[u]); t %= sz[u]; if(!t) return ans; int l = 0, r = g[u].size() - 1; while(l < r){ int mid = (l + r + 1) / 2; if(qz1[u][mid] < t) l = mid; else r = mid - 1; } t -= qz1[u][l]; ans = ans * qz2[u][l] * dfs3(g[u][l]); return ans; if(f) hv[hsh[u]]++;}int main(){ cnt[0] = is[0] = 1;ull cal(int x){ if(x < 0) return 0ll; return pw[x];}signed main(){ //ios::sync_with_stdio(false); //cin.tie(NULL); //cout.tie(NULL); freopen("1.in.txt", "r", stdin); //freopen(".out", "w", stdout); srand(time(0)); pw[0] = 1; for(int i = 1; i <= 1000000; i++) pw[i] = pw[i - 1] * 2 % md; cin >> n >> m; for(int i = 1; i <= n; i++){ string s; cin >> s; if(s == "end"){ nw = f[nw]; continue; for(int i = 1; i <= n; i++) p[i] = i; for(int i = 1; i <= m; i++){ a[i] = read(); b[i] = read(); if(find(a[i]) != find(b[i])){ p[find(a[i])] = b[i]; is[i] = 1; g[a[i]].psb({b[i], i}); g[b[i]].psb({a[i], i}); } g[nw].psb(i); f[i] = nw; cnt[i] = 1;// cout << nw << ' ' << i << endl; if(s == "repeat") cin >> cnt[i], nw = i, is[i] = 1; if(s == "xor"){ int a, b; cin >> s; a = (int)(s[0] - 'a'); cin >> s; b = (int)(s[0] - 'a'); for(int j = 0; j < 8; j++) for(int k = 0; k < 16; k++) to[i].f[j][k] = (k ^ ((((k >> b) & 1)) << a)); } if(s == "and"){ int a, b; cin >> s; a = (int)(s[0] - 'a'); cin >> s; b = (int)(s[0] - 'a'); for(int j = 0; j < 8; j++) for(int k = 0; k < 16; k++){ to[i].f[j][k] = k; if(!(((k >> b) & 1))) to[i].f[j][k] &= (15 - (1 << a)); } } if(s == "or"){ int a, b; cin >> s; a = (int)(s[0] - 'a'); cin >> s; b = (int)(s[0] - 'a'); for(int j = 0; j < 8; j++) for(int k = 0; k < 16; k++) to[i].f[j][k] = (k | ((((k >> b) & 1)) << a)); } if(s == "xori"){ int a, b; cin >> s; a = (int)(s[0] - 'a'); cin >> b; for(int j = 0; j < 8; j++) for(int k = 0; k < 16; k++) to[i].f[j][k] = (k ^ ((((b >> j) & 1)) << a)); } if(s == "andi"){ int a, b; cin >> s; a = (int)(s[0] - 'a'); cin >> b; for(int j = 0; j < 8; j++) for(int k = 0; k < 16; k++){ to[i].f[j][k] = k; if(!(((b >> j) & 1))) to[i].f[j][k] &= (15 - (1 << a)); } } if(s == "ori"){ int a, b; cin >> s; a = (int)(s[0] - 'a'); cin >> b; for(int j = 0; j < 8; j++) for(int k = 0; k < 16; k++) to[i].f[j][k] = (k | ((((b >> j) & 1)) << a)); } } if(n > 2000){ dfs1(0); if(dp[0] > 35){ g[0].clear(); for(int i = 1; i <= n; i++) if(dp[i] && dp[f[i]] > 35 && dp[i] <= 35) g[0].psb(i); for(int i = 1; i <= m; i++) if(!is[i]){ ull rd = RD(); hsh[a[i]] ^= rd; hsh[b[i]] ^= rd; vis[rd] = 1; } for(int i = 1; i <= n; i++) if(find(i) == i) get(i, 0), cnt++; if(m + cnt == n){ cout << 0; return 0; }// int u = 117;// while(u){// cout << u << ' ' << dp[u] << endl;// u = f[u];// } dfs2(0, 0); while(m--){ cin >> t; for(int i = 0; i < 4; i++) cin >> a[i], b[i] = 0; ans = dfs3(0); for(int i = 0; i < 8; i++){ int cnt = 0; for(int j = 0; j < 4; j++) cnt += (((a[j] >> i) & 1) << j); for(int j = 0; j < 4; j++) b[j] += (((ans.f[i][cnt] >> j) & 1) << i); ans += (m - (n - cnt)) * ((m - (n - cnt)) - 1) % md * cal(m - n + cnt - 2) % md + (m - (n - cnt)) * cal(m - n + cnt - 1) % md; for(int i = 1; i <= m; i++) if(is[i]){ if(!hsh[id[i]]) continue; if(vis[hsh[id[i]]]) ans += cal(m - n + cnt - 1) * 2 + cal(m - n + cnt - 2) * (m - (n - cnt) - 1) * 2 % md; else ans += cal(m - n + cnt - 2) * (m - (n - cnt)) * 2 % md; ans %= md; } for(int i = 0; i < 4; i++) cout << b[i] << ' '; cout << endl; ans %= md; for(pull x : hv){ ull a = x.x, b = x.y; if(a) ans += b * b % md * cal(m - n + cnt - 1) % md + b * ((n - cnt) - b - hv[0]) % md * cal(m - n + cnt - 2) % md; ans %= md; } ans %= md; cout << ans; return 0;}