版本比较
`
正文
增加 146 行删除 31 行
123456789101112131415161718192021452223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374127576777879168081828384212223242526272829303132333435368586878889909192939495969798991001011021031041051061071083839401094243110111454647484950112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167```#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 ld long double#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;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(); } while(c >= '0' && c <= '9'){ ans *= 10; ans += (c - '0'); c = getchar(); } 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 md){ll ksm(ll a, ll b){ ll ans = 1; a %= md; while(b){ if(b % 2) ans = ans * a % md; b /= 2; a = a * a % md; b >>= 1; } return ans;}ll n, m, k, dp[3000005], ny[3000005], jc[3000005];pair<int, ll> to[3000005];void dfs(int u){ int v = u * k % n; if(!v){ dp[u] = 1; return ; } to[u] = {v, v * ny[u] % m * ny[k] % m}; if(to[v].x){ ll ans = to[u].y, res = 1; int nw = v; while(nw != u){ res = res + ans; ans = ans * to[nw].y % m; nw = to[nw].x;ll rnd(ll l, ll r){ return l + rand() % (r - l + 1);}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]);}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]; } res %= m; dp[u] = res * ksm(1 - ans, m - 2, m) % m; return ; } dfs(v); dp[u] = (1 + to[u].y * dp[v]) % m; if(f) hv[hsh[u]]++;}int main(){ cin >> n >> k >> m; ny[1] = ny[0] = 1; for(int i = 2; i <= n; i++) ny[i] = (m - m / i) * ny[m % i] % m; dfs(1); cout << (dp[1] + m) % m;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++) 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}); } } 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; } 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; } 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;}