#include<bits/stdc++.h> #ifndef LOCAL #define debug(...) 42 #else #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #endif #define rep1(a) for (int i = 0; i < a; i++) #define rep2(i, a) for (int i = 0; i < a; i++) #define rep3(i, a, b) for (int i = a; i < b; i++) #define rep4(i, a, b, c) for (int i = a; i < b; i += c) #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define pb emplace_back usingnamespace std; template <typename T, typename T2> voidcmin(T &x, const T2 &y){ x = x < y ? x : y; } template <typename T, typename T2> voidcmax(T &x, const T2 &y){ x = x > y ? x : y; } using ll = longlong; using vi = vector<int>; using pii = pair<int, int>; template <classT> using vc = vector<T>; template <classT> using pq = priority_queue<T>; template <classT> using pqg = priority_queue<T, vector<T>, greater<T>>; mt19937 rng(time(NULL)); constint inf = 1000000000; const ll lnf = 1000000000000000000; #define sz(x) int((x).size()) #define all(x) begin(x), end(x) #define fi first #define se second constint P = 998244353; vector<vector<int>> operator*(vector<vector<int>> a, const vector<vector<int>> &b) { int N, M, K; N = (int)a.size(); K = min((int)a[0].size(), (int)b.size()); M = (int)b[0].size(); vector<vector<int>> c(N, vector<int>(M, 0)); rep(i, 0, N) { rep(k, 0, K) { rep(j, 0, M) { c[i][j] += (longlong)a[i][k] * b[k][j] % P; if (c[i][j] >= P) { c[i][j] -= P; } } } } return c; } intpower(int x, int y){ int res = 1; while (y) { if (y & 1) { res = (longlong)res * x % P; } x = (longlong)x * x % P; y /= 2; } return res; } intinverse(int x){ returnpower(x, P - 2); } intval(vector<vector<int>> &a){ int N = (int)a.size(); bool rev = false; int res = 1; rep(i, 0, N) { if (!a[i][i]) { rep(j, i + 1, N) { if (a[j][i]) { swap(a[i], a[j]); rev ^= 1; break; } } } rep(j, i + 1, N) { int d = (longlong)a[j][i] * inverse(a[i][i]) % P; rep(k, i + 1, N) { a[j][k] -= (longlong)d * a[i][k] % P; a[j][k] += a[j][k] >> 31 & P; } a[j][i] = 0; } if (!a[i][i]) { return0; } res = (longlong)res * a[i][i] % P; } if (rev) { return P - res; } else { return res; } }
voidmd(int &x){ if (x >= P) { x -= P; } } voidsolve(){ int n, m, k; cin >> n >> m >> k; vc<vi> a(n, vi(n)); vi deg(n); rep(i, m) { int x, y; cin >> x >> y; --x; --y; a[x][y] = 1; deg[y]++; } vc<vi> x(n - 1, vi(n - 1)); vc<vi> y(n, vi(n)), z(n, vi(n)); rep(i, n) rep(j, n) { if (i == j) { int dd = 1ll * n * k % P - deg[i] - 1; md(y[i][j] = dd + P); } else { int v = a[i][j]; y[i][j] = v; md(y[i][j] += P - 1); } } if (k == 1) { rep(i, n - 1) rep(j, n - 1) x[i][j] = y[i + 1][j + 1]; cout << val(x) << "\n"; return; } rep(i, n - 1) rep(j, n - 1) md(x[i][j] = y[i + 1][j + 1] + P - y[0][j + 1]); rep(i, n) rep(j, n) if (i == n - 1) z[i][j] = 1; else z[i][j] = y[i][j]; rep(i, n) rep(j, n) md(y[i][j] += 1); debug(x, y, z); debug(val(x), val(y), val(z)); cout << 1ll * val(x) * power(val(y), k - 2) % P * val(z) % P << "\n"; }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } return0; }
constint P = 1e9 + 7; constint N = 2e5 + 5; int ch[N][26]; int cnt = 1; int hav[N]; ll w[N]; int fail[N]; voidsolve(){ int n, m; cin >> n >> m; rep(i, n) { string s; cin >> s; int p = 1; for (char x : s) { int c = x - 'a'; if (!ch[p][c]) { ch[p][c] = ++cnt; } p = ch[p][c]; } hav[p] += 1; w[p] += sz(s); } queue<int> q; rep(i, 26) if (ch[1][i]) fail[ch[1][i]] = 1, q.emplace(ch[1][i]); else ch[1][i] = 1; while (!q.empty()) { int u = q.front(); q.pop(); rep(i, 26) { if (ch[u][i]) fail[ch[u][i]] = ch[fail[u]][i], q.emplace(ch[u][i]); else ch[u][i] = ch[fail[u]][i]; } } vc<vi> g(cnt + 1); debug(g); rep(i, 2, cnt + 1) g[fail[i]].pb(i); auto dfs = [&](auto &self, int u) -> void { debug(u); for (auto v : g[u]) { w[v] += w[u]; hav[v] += hav[u]; self(self, v); } }; dfs(dfs, 1); while (m--) { string t; cin >> t; int l = sz(t); int p = 1; ll ans = 0; for (int i = 0; i < l; i++) { int c = t[i] - 'a'; p = ch[p][c]; ll val = 1ll * hav[p] * (i + 2) - w[p]; val %= P; ans += 1ll * val * (l - i) % P; if (ans >= P) { ans -= P; } } cout << ans << "\n"; } }
#include<bits/stdc++.h> #ifndef LOCAL #define debug(...) 42 #else #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #endif #define rep1(a) for (int i = 0; i < a; i++) #define rep2(i, a) for (int i = 0; i < a; i++) #define rep3(i, a, b) for (int i = a; i < b; i++) #define rep4(i, a, b, c) for (int i = a; i < b; i += c) #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define pb emplace_back usingnamespace std; template <typename T, typename T2> voidcmin(T &x, const T2 &y){ x = x < y ? x : y; } template <typename T, typename T2> voidcmax(T &x, const T2 &y){ x = x > y ? x : y; } using ll = longlong; using vi = vector<int>; using pii = pair<int, int>; template <classT> using vc = vector<T>; template <classT> using pq = priority_queue<T>; template <classT> using pqg = priority_queue<T, vector<T>, greater<T>>; mt19937 rng(time(NULL)); constint inf = 1000000000; const ll lnf = 1000000000000000000; #define sz(x) int((x).size()) #define all(x) begin(x), end(x) #define fi first #define se second namespace Poly { constint P = 998244353; constint G = 3; intpower(int x, int y){ int res = 1; for (; y; y >>= 1, x = 1ll * x * x % P) if (y & 1) res = 1ll * res * x % P; return res; } intinc(int x, int y){ return ((x += y) >= P) ? x - P : x; } intdec(int x, int y){ return ((x -= y) < 0) ? x + P : x; } vector<int> rev, rt; int lim = 1; voidinit(constint &len){ int l = 0; lim = 1; while (lim <= len) { lim <<= 1, l++; } rev.resize(lim), rt.resize(lim); for (int i = 0; i < lim; i++) rev[i] = rev[i >> 1] >> 1 | (i & 1) << l - 1; for (int len = 1; len < lim; len <<= 1) { int w = power(G, (P ^ 1) / (len << 1)); rt[len] = 1; for (int i = 1; i < len; i++) { rt[i + len] = 1ll * rt[i + len - 1] * w % P; } } } voidntt(vector<int> &f){ f.resize(lim); for (int i = 0; i < lim; i++) if (i < rev[i]) { swap(f[i], f[rev[i]]); } for (int len = 1; len < lim; len <<= 1) { for (int i = 0; i < lim; i += len << 1) { for (int j = 0; j < len; j++) { int x = f[i + j], y = 1ll * f[i + j + len] * rt[j + len] % P; f[i + j] = inc(x, y), f[i + j + len] = dec(x, y); } } } }
vector<int> operator*(vector<int> f, vector<int> g) { int n = f.size(), m = g.size(), inv; init(n + m - 2), inv = power(lim, P - 2), ntt(f), ntt(g); for (int i = 0; i < lim; i++) f[i] = 1ll * f[i] * g[i] % P; reverse(f.begin() + 1, f.end()), ntt(f), f.resize(n + m - 1); for (int &x : f) x = 1ll * x * inv % P; return f; } } // namespace Poly usingnamespace Poly;
constint N = 1e7 + 5; bool isprime[N]; int pr[N / 10], c = 0; // const int P = 998244353; // int power(int x, int y) { // int r = 1; // while (y) { // if (y % 2) // r = 1ll * r * x % P; // x = 1ll * x * x % P; // y /= 2; // } // return r; // } intinv(int x){ returnpower(x, P - 2); }
ll sp, sq; int pw[N], ipw[N];
voidmd(int &x){ if (x >= P) { x -= P; } } voidsolve(){ int n, l, r; cin >> n >> l >> r; vi p(n + 1), q(n + 1);
for (int i = 1; i <= n; i++) cin >> p[i], sp += p[i]; for (int i = 1; i <= n; i++) cin >> q[i], sq += q[i]; sp %= P, sq %= P; for (int i = 1; i <= n; i++) { p[i] = 1ll * p[i] * inv(sp) % P; q[i] = 1ll * q[i] * inv(sq) % P; }
vector<int> f(n + 1, 0); for (int i = 1; i <= n; i++) { f[i] = 1ll * ipw[l + i] * p[i] % P; } vector<int> g(n + 1, 0); for (int j = 1; j <= n; j++) { int bas = upper_bound(pr, pr + c, r - j) - pr; g[j] = 1ll * pw[r + 1 - j - bas] * q[j] % P; } // for (int i = 1; i <= n; i++) { // for (int j = 1; j <= n; j++) { // int bas = upper_bound(pr, pr + c, r - j) - pr; // d[i + j] += 1ll * pw[r - l + 1 - i - j - bas] * p[i] % P * q[j] % P; // if (d[i + j] >= P) { // d[i + j] -= P; // } // } // } vector<int> d = f * g; for (int i = 2; i <= n * 2; i++) { cout << d[n * 2 - i + 2] << " \n"[i == n * 2]; } }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); for (int i = 2; i < N; i++) isprime[i] = 1; for (int i = 2; i < N; i++) { if (isprime[i]) { pr[c++] = i; for (int j = i * 2; j < N; j += i) { isprime[j] = 0; } } } pw[0] = 1; rep(i, 1, N) pw[i] = pw[i - 1] + pw[i - 1], md(pw[i]); constint inv = (P + 1) / 2; ipw[0] = 1; rep(i, 1, N) ipw[i] = 1ll * ipw[i - 1] * inv % P; int t = 1; // cin >> t; while (t--) { solve(); } return0; }
#include<bits/stdc++.h> #ifndef LOCAL #define debug(...) 42 #else #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #endif #define rep1(a) for (int i = 0; i < a; i++) #define rep2(i, a) for (int i = 0; i < a; i++) #define rep3(i, a, b) for (int i = a; i < b; i++) #define rep4(i, a, b, c) for (int i = a; i < b; i += c) #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define pb emplace_back usingnamespace std; template <typename T, typename T2> voidcmin(T &x, const T2 &y){ x = x < y ? x : y; } template <typename T, typename T2> voidcmax(T &x, const T2 &y){ x = x > y ? x : y; } using ll = longlong; using vi = vector<int>; using pii = pair<int, int>; template <classT> using vc = vector<T>; template <classT> using pq = priority_queue<T>; template <classT> using pqg = priority_queue<T, vector<T>, greater<T>>; mt19937 rng(time(NULL)); constint inf = 1000000000; const ll lnf = 1000000000000000000; #define sz(x) int((x).size()) #define all(x) begin(x), end(x) #define fi first #define se second
int n, m, R, q, P; constint N = 5005; constint M = 2500 + 2; double f[N][N], g[N][N]; // f[i][j], 选择了 i 个技能,成功发动了 j 个的概率 // g[i][j],选择了 i 个技能,成功提前发动了 j 个的概率 // 至少发动 k 个技能,至多 k-1 个技能提前发动的概率
voidsolve(){ cin >> n >> m >> R >> q >> P; double p = P / 100.; f[0][0] = 1.; for (int i = 1; i <= n; i++) { f[i][0] = f[i - 1][0] * (1 - p); for (int j = 1; j <= i; j++) { f[i][j] = p * f[i - 1][j - 1] + (1 - p) * f[i - 1][j]; } } vc<double> prob; rep(i, n) { int l, r; cin >> l >> r; if (r < R) prob.pb(1.); elseif (l < R) { int len = r - R; prob.pb(1. - 1. * len / (r - l)); } else { prob.pb(0); } } prob.pb(0); sort(all(prob)); debug(prob); g[0][0] = 1.; for (int i = 1; i <= n; i++) { g[i][0] = g[i - 1][0] * (1 - p * prob[i]); for (int j = 1; j <= i; j++) g[i][j] = p * prob[i] * g[i - 1][j - 1] + (1 - p * prob[i]) * g[i - 1][j]; // for(int j=0;j<=i;j++)cout<<g[i][j]<<" \n"[j==i]; } cout << fixed << setprecision(10); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) f[i][j] += f[i][j - 1]; for (int i = 1; i <= n; i++) for (int j = n - 1; j >= 0; j--) g[i][j] += g[i][j + 1]; while (q--) { int k; cin >> k; double ans = 0; for (int l = k; l <= n; l++) { cmax(ans, 1 - f[l][k - 1] - g[l][k]); } cout << ans << "\n"; } }
intmain(){ ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) { solve(); } return0; }
K.
$n$ 个人地位等同,输出 $\frac{1}{n}$。
1 2 3 4 5 6 7
voidsolve(){ int n, m; cin >> n >> m; cout << "1/" << n << "\n"; }
voidsolve(){ int n, m, k; cin >> n >> m >> k; vc<array<int, 7>> t(m); rep(i, m) { rep(j, 7) cin >> t[i][j]; --t[i][0]; --t[i][1]; } int w = 1; rep(i, n) { w *= (k + 1); } ll res = 0; rep(y, w) { int x = y; vi a(n); rep(j, n) a[j] = x % (k + 1), x /= (k + 1); ll ans = 0; for (auto [i, j, op, A, B, d, v] : t) { int val = A * a[i] + B * a[j]; bool ok = false; if (op == 0 && val <= d) ok = true; if (op == 1 && val >= d) ok = true; if (ok) ans += v; } cmax(res, ans); } cout << res << "\n"; }