inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 200007
structDSU { int f[N]; inlinevoidreset(int x){for (int i = 1; i <= x; ++i) f[i] = i;} intfind(int x){return x == f[x] ? x : f[x] = find(f[x]);} boolmerge(int u, int v){ u = find(u); v = find(v); return (u == v ? false : (f[u] = v, true)); } } dsu;
int deg[N], tag[N], val[N];
queue<int> q;
vector<int> in[N], out[N];
inlinevoidwork(){ int n = rd(), m = rd(); dsu.reset(n); for (int i = 1; i <= n; ++i) {deg[i] = 0; in[i].clear(); out[i].clear();} for (int i = 1; i <= m; ++i) { int u = rd(), v = rd(); in[v].push_back(u); } for (int u = 1; u <= n; ++u) if (!in[u].empty()) { int sy = in[u][0]; for (auto x : in[u]) dsu.merge(x, sy); } for (int u = 1; u <= n; ++u) { int U = dsu.find(u); for (auto v : in[u]) { int V = dsu.find(v); ++deg[U]; out[V].push_back(U); } } int tot = 0, cnt = 0; for (int i = 1; i <= n; ++i) if (dsu.find(i) == i) { ++tot; if (!deg[i]) q.push(i); } while (!q.empty()) { --tot; int u = q.front(); q.pop(); tag[u] = ++cnt; for (auto v : out[u]) { --deg[v]; if (!deg[v]) q.push(v); } } if (tot) {puts("No"); return;} puts("Yes"); for (int i = 1; i <= n; ++i) val[i] = tag[dsu.find(i)]; for (int i = 1; i <= n; ++i) { int ans = val[i]; if (!in[i].empty()) ans -= val[in[i][0]]; if (i < n) printf("%d ", ans); elseprintf("%d\n", ans); } }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
vector<int> s, s1;
#define N 500007
int a[N], c[N];
#define lowbit(x) ((x) & -(x))
inlinevoidadd(int p){ for (; p < N; p += lowbit(p)) ++c[p]; }
inlineintsum(int p){ int res = 0; for (; p; p -= lowbit(p)) res += c[p]; return res; }
ll presum[N];
intmain(){ int n = rd(), m = rd() - 2; for (int i = 1; i <= n; ++i) { a[i] = rd(); s.push_back(a[i]); } sort(s.begin(), s.end()); s1 = s; for (int i = 1; i <= n; ++i) presum[i] = presum[i - 1] + s1[i - 1]; s.erase(unique(s.begin(), s.end()), s.end()); auto calc = [&](int x) { return lower_bound(s.begin(), s.end(), x) - s.begin() + 1; }; for (int i = 1; i <= n; ++i) { int nw = calc(a[i]); ll tot = sum(nw); add(nw); int p = lower_bound(s1.begin(), s1.end(), a[i]) - s1.begin() + 1; tot += 1ll * p * a[i] - presum[p]; if (tot > m) puts("-1"); elseprintf("%lld\n", tot); } return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 2000007
bool vis[N];
int a[N];
vector<int> s;
inlinevoidwork(){ s.clear(); int n = rd(), k = rd(); for (int i = 1; i <= n; ++i) { a[i] = rd(); vis[a[i]] = true; } int A = 1, B = 0; for (int i = 1; i <= k; ++i) { while (vis[A]) A += 2; vis[A] = true; s.push_back(A); while (vis[B]) B += 2; vis[B] = true; s.push_back(B); } int mx = 0; while (vis[mx]) ++mx; puts((mx & 1) ? "Bob" : "Alice"); for (int i = 1; i <= n; ++i) vis[a[i]] =false; for (auto x : s) vis[x] = false; }
intmain(){ for (int t = rd(); t; --t) work(); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 2007 #define mod 1000000007
vector<int> e[N];
inlineintfpow(int x, int t = mod - 2){ int res = 1; for (; t; t >>= 1, x = 1ll * x * x % mod) if (t & 1) res = 1ll * res * x % mod; return res; }
// f[u][v] : choose j nodes in subtree u, without root // g[u][v] : choose j nodes in subtree u, already choose a root
int a[N], p[N], sz[N], f[N][N], g[N][N], ans[N];
voiddfs(int u, int fa, int pfa){ f[u][0] = 1; for (auto v : e[u]) if (v != fa) { dfs(v, u, p[u]); for (int j = sz[u] + sz[v]; j >= 0; --j) { // 这里本来应该开一个另外的数组来存值,最后再赋值回去的,但是注意到k=0的转移非常好写就直接赋值了 // failed at v f[u][j] = (ll)f[u][j] * (mod + 1 - p[v]) % mod; g[u][j] = (ll)g[u][j] * (mod + 1 - p[v]) % mod; // choose k nodes in subtree v for (int k = max(1, j - sz[u]); k <= min(j, sz[v]); ++k) { f[u][j] = (f[u][j] + (ll)f[u][j - k] * f[v][k]) % mod; g[u][j] = (g[u][j] + (ll)g[u][j - k] * f[v][k] + (ll)f[u][j - k] * g[v][k]) % mod; } } sz[u] += sz[v]; } ++sz[u]; for (int j = sz[u]; j; --j) { f[u][j] = (ll)f[u][j - 1] * p[u] % mod; g[u][j] = (ll)f[u][j - 1] * a[u] % mod; if (j > 1) g[u][j] = (g[u][j] + (ll)g[u][j - 1] * p[u]) % mod; ans[j] = (ans[j] + (ll)g[u][j] * (mod + 1 - pfa)) % mod; // 注意要保证父亲没有选 } }
intmain(){ int n = rd(); for (int i = 1; i < n; ++i) { int u = rd(), v = rd(); e[u].push_back(v); e[v].push_back(u); } int sum = 0; for (int i = 1; i <= n; ++i) { a[i] = rd(); sum += a[i]; p[i] = rd(); p[i] = (ll)p[i] * fpow(rd()) % mod; } sum = fpow(sum); for (int i = 1; i <= n; ++i) a[i] = (ll)a[i] * sum % mod; dfs(1, 1, 0); for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]); return0; }
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 500007
// struct vec { // double x, y, z; // vec operator + (const vec &b) {return {x + b.x, y + b.y, z + b.z};} // vec operator - (const vec &b) {return {x - b.x, y - b.y, z - b.z};} // vec operator / (const double &b) {return {x / b, y / b, z / b};} // } p[N];
// inline void work() { // int n = rd(), m = rd(); // int k = n; // for (int i = 1; i <= n; ++i) { // p[i].x = randp(); // p[i].y = randp(); // p[i].z = randp(); // } // for (int i = 1; i <= m; ++i) { // int u = rd(), v = rd(); // p[++n] = (p[u] + p[v]) / 2; // } // int ans = 0; // for (int a = 1; a <= k; ++a) // for (int b = a + 1; b <= k; ++b) // for (int c = b + 1; c <= n; ++c) // for (int d = c + 1; d <= n; ++d) // if (det(p[b] - p[a], p[c] - p[a], p[d] - p[a])) { // ++ans; printf("%d %d %d %d\n", a, b, c, d); // } // printf("%d\n", ans); // }
int n,m,X[N],Y[N],d[N],ti,vis[N],cnt[N]; vector<int> e[N],h[N]; ll ans3,ans4; inlineboolcmp(constint &i,constint &j){return d[i]<d[j] || (d[i]==d[j] && i<j);} voidworkz(){ n=rd();m=rd(); for (int i=1;i<=n;i++) d[i]=0,cnt[i]=0,e[i].clear(),h[i].clear(); for (int i=1;i<=m;i++){ X[i]=rd();Y[i]=rd();d[X[i]]++;d[Y[i]]++; h[X[i]].push_back(Y[i]);h[Y[i]].push_back(X[i]); } for (int i=1;i<=m;i++) cmp(X[i],Y[i])?e[X[i]].push_back(Y[i]):e[Y[i]].push_back(X[i]); ans3=0; for (int i=1;i<=m;i++){ ti++;for (auto x:e[X[i]]) vis[x]=ti; for (auto x:e[Y[i]]) if (vis[x]==ti) ans3++; } ans4=0; for (int x=1;x<=n;x++){ for (auto y:h[x]) for (auto z:e[y]) if (cmp(x,z)) ans4+=cnt[z],cnt[z]++; for (auto y:h[x]) for (auto z:e[y]) cnt[z]=0; } ll ans=(ll)m*(n+m-3); for (int i=1;i<=n;i++) ans+=(ll)d[i]*(d[i]-1)/2; ans+=3*ans3;ans+=ans4; printf("%lld\n", ans%MOD); } intmain(){ for (int t = rd(); t; --t) workz(); return0; }
L - Station of Fate
\(n\) 个人分成 \(m\) 个可区分的非空队列的方案数。
顺序有 \(n!\) 种,插板法划分成 \(m\) 个非空序列,总方案数为 \(n!{n-1\choose m - 1}\)
inlineintrd(){ int x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) f |= (c == '-'); for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); return f ? -x : x; }
#define N 100007 #define mod 998244353
int fac[N], ifac[N];
inlineintfpow(int x, int t){ int res = 1; for (; t; t >>= 1, x = 1ll * x * x % mod) if (t & 1) res = 1ll * res * x % mod; return res; }
inlineintC(int n, int m){ return1ll * fac[n] * ifac[m] % mod * ifac[n - m] % mod; }
inlinevoidwork(){ int n = rd(), m = rd(); printf("%lld\n", 1ll * fac[n] * C(n - 1, m - 1) % mod); }
intmain(){ fac[0] = ifac[0] = 1; for (int i = 1; i < N; ++i) fac[i] = 1ll * fac[i - 1] * i % mod; ifac[N - 1] = fpow(fac[N - 1], mod - 2); for (int i = N - 2; i; --i) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % mod; for (int t = rd(); t; --t) work(); return0; }