text
stringlengths
10
22.5k
status
stringclasses
2 values
name
stringlengths
9
26
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' void solve() { int n, m; cin >> n >> m; string s; string str = "nanjing"; cin >> s; s = s + s; auto fun = [&](string s) { int i, j, k, pos = 0; int ans = 0; while (pos < n) { auto t = s.find(str, pos); if (t != string::npos) ans++, // vis[t]=1, pos = t + str.length(); else break; } return ans; }; int res = fun(s.substr(0, n)); for (int i = 0; i < min(int(str.length()), m); i++) res = max(res, fun(s.substr(i, n))); cout << res << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) solve(); }
failed
51_3_wrong.cpp
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; const int MAXN = 2005, mod = 998244353; char buf[1 << 24], *S, *T; char gc() { return S == T && (T = (S = buf) + fread(buf, 1, 1 << 24, stdin)) == buf ? 0 : *S++; } int read() { char ch; while ((ch = getchar()) <= ' ') ; int x = ch - '0'; while ((ch = getchar()) > ' ') x = 10 * x + ch - '0'; return x; } int dp[MAXN][MAXN], ans[MAXN][MAXN]; void add(int &x, int y) { if ((x += y) >= mod) x -= mod; } int main() { dp[0][0] = 1; for (int _ = 1; _ <= 1000; _++) { for (int i = 1; i <= 2000; i++) { #pragma GCC unroll(8) for (int j = 0; j <= 2000 - _; j++) { add(dp[i][j], dp[i - 1][j]); if (j >= _) add(dp[i][j], dp[i - 1][j - _]); } for (int j = _; j <= 2000; j++) add(ans[i][j], dp[i - 1][j - _]); } } int q = read(); while (q--) { int n = read(), m = read(); printf("%d\n", ans[n][m]); } }
failed
79_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "debug.h" #else #define debug(...) void() #endif #define all(x) (x).begin(), (x).end() template <class T> auto ary(T *a, int l, int r) { return vector<T>{a + l, a + 1 + r}; } using ll = long long; using ull = unsigned long long; int n, m, k; vector<string> ans; int main() { cin >> n >> m; for (k = 1; (1 << k) < n; k++) ; ans.push_back(string(n, '1')); ans.push_back(string(n, '2')); for (int i = 0; i < k; i++) { string res(n, '0'); for (int j = 0; j < n; j++) res[j] = j >> i & 1 ? '1' : '2'; ans.push_back(res); for (int j = 0; j < n; j++) res[j] = j >> i & 1 ? '2' : '1'; ans.push_back(res); } if (m < ans.size()) puts("infinity"), exit(0); cout << ans.size() << '\n'; for (int i = 0; i < m; i++) cout << ans[i % ans.size()] << '\n'; return 0; } #ifdef DEBUG #include "debug.hpp" #endif
failed
111_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; using i64 = long long; using pii = array<int, 3>; constexpr int maxn = 100000 + 10; pii p[maxn], q[maxn]; struct cmp { bool operator()(const pii &x, const pii &y) const { return x[1] == y[1] ? x[2] < y[2] : x[1] < y[1]; } }; void solve() { int n; cin >> n; n = n * 3; for (int i = 1; i <= n; ++i) cin >> p[i][0] >> p[i][1], p[i][2] = i; memcpy(q + 1, p + 1, sizeof(p[0]) * n); sort(q + 1, q + n + 1); sort(p + 1, p + n + 1, cmp()); set<pii, cmp> s; vector<pii> a; for (int i = 1, j = 1; i <= n; ++i) { while (j <= n && q[j][0] <= p[i][1]) s.insert(q[j++]); if (s.find(p[i]) == s.end()) continue; if (s.size() < 3) return cout << "No\n", void(); s.erase(s.find(p[i])); pii x; x[0] = p[i][2]; x[1] = s.begin()->at(2); s.erase(s.begin()); x[2] = s.begin()->at(2); s.erase(s.begin()); a.emplace_back(x); } cout << "Yes\n"; for (auto [x, y, z] : a) cout << x << ' ' << y << ' ' << z << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) solve(); return 0; }
failed
13_2_wrong.cpp
#include <bits/stdc++.h> #define ff first #define ss second #define mp make_pair #define pb push_back using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; using ppi = pair<pii, int>; struct SegmentTree { int n; vector<int> tree, lazy; SegmentTree(int nn) : n(nn) { tree.assign((n + 1) << 2, 0); lazy.assign((n + 1) << 2, 0); } void push(int u, int tl, int tr) { if (!lazy[u]) return; tree[u] = lazy[u]; if (tl < tr) { lazy[2 * u] = lazy[u]; lazy[2 * u + 1] = lazy[u]; } lazy[u] = 0; } void update(int u, int tl, int tr, int l, int r, int x) { push(u, tl, tr); if (tr < l || r < tl) return; if (l <= tl && tr <= r) { lazy[u] = x; push(u, tl, tr); } else { int tm = (tl + tr) / 2; update(2 * u, tl, tm, l, r, x); update(2 * u + 1, tm + 1, tr, l, r, x); tree[u] = max(tree[2 * u], tree[2 * u + 1]); } } int query(int u, int tl, int tr, int l, int r) { push(u, tl, tr); if (tr < l || r < tl) return 0; if (l <= tl && tr <= r) return tree[u]; int tm = (tr + tl) / 2; return max(query(2 * u, tl, tm, l, r), query(2 * u + 1, tm + 1, tr, l, r)); } void update(int l, int r, int x) { update(1, 0, n - 1, l, r, x); } int query(int l, int r) { return query(1, 0, n - 1, l, r); } }; struct Fenwick { int n; vector<int> tree; Fenwick(int nn) : n(nn) { tree.assign(n + 5, 0); } void update(int k, int x) { k++; while (k <= n) { tree[k] += x; k += k & -k; } } int query(int k) { int ans = 0; k++; while (k > 0) { ans += tree[k]; k -= k & -k; } return ans; } int get(int l, int r) { return query(r) - query(l - 1); } }; const int maxn = 5e5 + 10, logn = 20; int vis[maxn], depth[maxn], par[maxn]; int up[maxn][logn]; int anim[maxn]; int t = -1; int tin[maxn], tout[maxn]; vector<int> tree[maxn]; void dfs(int u, int p) { tin[u] = ++t; up[u][0] = p; for (int i = 1; i < logn; ++i) up[u][i] = u == p ? p : up[up[u][i - 1]][i - 1]; for (int &v : tree[u]) { if (v == p) continue; depth[v] = depth[u] + 1; dfs(v, u); } tout[u] = t; } int main() { #ifdef LOCAL freopen("in", "r", stdin); // freopen("out","w",stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); int n, q; cin >> n >> q; vector<vector<int>> a(n, vector<int>(4)); vector<int> c; for (int i = 0; i < n; ++i) { for (int j = 0; j < 4; ++j) { cin >> a[i][j]; // x1, y1, x2, y2 c.pb(a[i][j]); } } sort(c.begin(), c.end()); c.resize(unique(c.begin(), c.end()) - c.begin()); int m = c.size(); vector<vector<ppi>> sweep(m, vector<ppi>()); for (int i = 0; i < n; ++i) { par[i] = -1; for (int j = 0; j < 4; ++j) a[i][j] = lower_bound(c.begin(), c.end(), a[i][j]) - c.begin(); pii p = {a[i][1], a[i][3]}; sweep[a[i][0]].pb({p, (i + 1)}); sweep[a[i][2]].pb({p, -(i + 1)}); } SegmentTree seg(m); for (int i = 0; i < m; ++i) { for (auto &p : sweep[i]) { int lo = p.ff.ff, hi = p.ff.ss; int id = p.ss; if (id > 0) { par[id] = seg.query(lo, hi); tree[id].pb(par[id]); tree[par[id]].pb(id); seg.update(lo, hi, id); } else { seg.update(lo, hi, par[-id]); } } } for (int i = 1; i <= n; ++i) { assert(par[i] != -1); if (par[i] == 0) dfs(i, i); } Fenwick heights(n), val(n); for (int it = 0; it < q; ++it) { char ci; int x; cin >> ci >> x; if (ci == '^') { if (anim[x]) { // apagar if (val.get(tin[x], tout[x]) == 1) { int u = x; for (int i = logn - 1; i >= 0; --i) { if (val.get(tin[up[u][i]], tout[up[u][i]]) == 1) u = up[u][i]; } heights.update(depth[u], -1); heights.update(depth[x] + 1, +1); } assert(val.get(tin[x], tout[x]) >= 1); } else { if (val.get(tin[x], tout[x]) == 0) { int u = x; for (int i = logn - 1; i >= 0; --i) { if (val.get(tin[up[u][i]], tout[up[u][i]]) == 0) u = up[u][i]; } heights.update(depth[u], 1); heights.update(depth[x] + 1, -1); } } val.update(tin[x], -anim[x]); anim[x] ^= 1; val.update(tin[x], anim[x]); } else cout << heights.query(x) << endl; } return 0; }
failed
75_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (ll i = 0; i < (n); ++i) #define rep2(i, l, r) for (ll i = (l); i < (r); ++i) using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; #define all(A) A.begin(), A.end() #define elif else if using pii = pair<ll, ll>; bool chmin(auto &a, auto b) { return a > b ? a = b, 1 : 0; } bool chmax(auto &a, auto b) { return a < b ? a = b, 1 : 0; } struct IOS { IOS() { cin.tie(0); ios::sync_with_stdio(0); } } iosetup; template <class T> void print(vector<T> a) { for (auto x : a) cout << x << ' '; cout << endl; } void print(auto x) { cout << x << endl; } template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) { cout << head << ' '; print(forward<Tail>(tail)...); } const ll MOD = 1e9 + 7; ll modpow(ll x, ll k, ll m = MOD) { ll v = 1; while (k) { if (k & 1) v = v * x % m; x = x * x % m; k >>= 1; } return v; } int len; vll fac, finv; void precalc(ll m = MOD) { fac.assign(len, 1), finv.assign(len, 1); rep2(i, 2, len) fac[i] = fac[i - 1] * i % m; finv[len - 1] = modpow(fac[len - 1], m - 2, m); for (int i = len - 2; i >= 0; i--) finv[i] = finv[i + 1] * (i + 1) % m; return; } ll binom(ll n, ll k, ll m = MOD) { if (n < 0 || k < 0 || k > n) return 0; if (k < 100) { // n * ... * (n-k+1) / k! ll ans = 1; rep(i, k) ans *= (n - i), ans %= m; ans *= finv[k], ans %= m; return ans; } else { return fac[n] * finv[k] % m * finv[n - k] % m; } } ll func(ll n, ll a) { ll res = 0; for (ll b = 1; b <= a; ++b) { ll tmp = binom(a, b) * modpow(b, n) % MOD; if (a % 2 != b % 2) tmp *= -1; tmp = (tmp + MOD) % MOD; res += tmp; res %= MOD; } return res; } void solve() { ll X, Y, M, K; cin >> X >> Y >> M >> K; vector<array<ll, 2>> cards(K); rep(i, K) { ll a, b; cin >> a >> b; a--, b--; cards[i] = {min(a, b), max(a, b)}; } sort(all(cards)); cards.erase(unique(all(cards)), cards.end()); ll must = 0; vector graph(M, vector<ll>()); for (auto [a, b] : cards) { if (a == b) must |= (1 << a); else { graph[a].emplace_back(b); graph[b].emplace_back(a); } } // search all subset ll res = 0; for (ll bit = 0; bit < (1 << M); ++bit) { if ((bit & must) != must) continue; vector<ll> col(M, 0); rep(i, M) { if (bit & (1 << i)) col[i] = 3; } vector<array<ll, 2>> vec; queue<ll> que; bool ng = 0; rep(r, M) { if (col[r]) continue; ll r1 = 0, r2 = 0; col[r] = 1, r1++; que.push(r); while (que.size()) { ll v = que.front(); que.pop(); for (auto nv : graph[v]) { if (col[nv] == col[v]) { ng = 1; break; } if (col[nv]) continue; col[nv] = 3 - col[v]; if (col[nv] == 1) r1++; else r2++; } } if (ng) break; vec.push_back({min(r1, r2), max(r1, r2)}); } // cout << bitset<10>(bit) << endl; // for(auto [r1, r2] : vec) { // cout << r1 << ' ' << r2 << endl; // } if (ng) continue; ll s = vec.size(); vector<ll> dp(M + 1, 0); dp[0] = 1; for (auto [a, b] : vec) { vector<ll> ndp(M + 1, 0); rep(x, M + 1) { if (x - a >= 0) ndp[x] += dp[x - a]; if (x - b >= 0) ndp[x] += dp[x - b]; ndp[x] %= MOD; } swap(dp, ndp); } ll p = __builtin_popcount(bit); rep(x, M + 1) { ll s = x + p, t = M - x; ll tmp = func(X, s) * func(Y, t) % MOD * dp[x] % MOD; res += tmp; } res %= MOD; } cout << res << endl; return; } int main() { ll T; cin >> T; while (T--) { solve(); } return 0; }
failed
24_3_wrong.cpp
#include "bits/stdc++.h" using namespace std; #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() #define int long long #define V vector const int inf = 1e9; const int mxn = 4e5 + 50; void chmin(int &a, int b) { a = min(a, b); } int n, m, k; V<V<int>> g; string s; V<V<int>> gg[2]; V<array<int, 2>> mg; int kil[mxn], to[mxn]; int in_deg[mxn]; int depth[mxn]; vector<int> adj[mxn]; vector<array<int, 3>> iter_order; queue<int> topsort; int cnt = 0, ans[mxn]; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> k >> s; g.resize(n + 5); g[0].resize(m + 5); g[n + 1].resize(m + 5); for (int i = 1; i <= n; i++) { g[i].resize(m + 5); for (int j = 1; j <= m; j++) { char c; cin >> c; g[i][j] = c - '0'; if (g[i][j]) cnt++; } } // macro for (int x = 1; x <= n; x++) for (int y = 1; y <= m; y++) { if (!g[x][y]) continue; int cx = x, cy = y; for (int i = 0; i < k; i++) { if (s[i] == 'U') { if (g[cx - 1][cy]) cx--; } else if (s[i] == 'D') { if (g[cx + 1][cy]) cx++; } else if (s[i] == 'L') { if (g[cx][cy - 1]) cy--; } else if (s[i] == 'R') { if (g[cx][cy + 1]) cy++; } } to[x * m + y] = cx * m + cy; in_deg[cx * m + cy]++; adj[cx * m + cy].push_back(x * m + y); } for (int x = 1; x <= n; x++) for (int y = 1; y <= m; y++) if (g[x][y] && !in_deg[x * m + y]) topsort.push(x * m + y); fill(depth, depth + mxn, 1); while (sz(topsort)) { int v = topsort.front(); topsort.pop(); depth[to[v]] = max(depth[to[v]], depth[v] + 1); in_deg[to[v]]--; if (!in_deg[to[v]]) { topsort.push(to[v]); } } for (int x = 1; x <= n; x++) for (int y = 1; y <= m; y++) if (g[x][y] && in_deg[x * m + y]) depth[x * m + y] = inf; // micro int cp = 0, pp = 1; gg[0].resize(n + 5); gg[1].resize(n + 5); gg[0][0].resize(m + 5); gg[0][n + 1].resize(m + 5); gg[1][0].resize(m + 5); gg[1][n + 1].resize(m + 5); for (int i = 1; i <= n; i++) { gg[0][i].resize(m + 5); gg[1][i].resize(m + 5); for (int j = 1; j <= m; j++) if (g[i][j]) gg[cp][i][j] = i * m + j; } fill(kil, kil + mxn, inf); // memset(kil,0x3f,sizeof(kil)); for (int i = 0; i < k; i++) { swap(cp, pp); for (int x = 1; x <= n; x++) for (int y = 1; y <= m; y++) gg[cp][x][y] = 0; for (int x = 1; x <= n; x++) for (int y = 1; y <= m; y++) { if (!gg[pp][x][y]) continue; if (s[i] == 'U') { if (g[x - 1][y]) { if (gg[cp][x - 1][y]) { if (depth[gg[pp][x][y]] > depth[gg[cp][x - 1][y]]) { swap(gg[pp][x][y], gg[cp][x - 1][y]); } else if (depth[gg[pp][x][y]] == depth[gg[cp][x - 1][y]] && gg[cp][x - 1][y] > gg[pp][x][y]) { swap(gg[pp][x][y], gg[cp][x - 1][y]); } kil[gg[pp][x][y]] = i; } else gg[cp][x - 1][y] = gg[pp][x][y]; } else { if (gg[cp][x][y]) { if (depth[gg[pp][x][y]] > depth[gg[cp][x][y]]) { swap(gg[pp][x][y], gg[cp][x][y]); } else if (depth[gg[pp][x][y]] == depth[gg[cp][x][y]] && gg[cp][x][y] > gg[pp][x][y]) { swap(gg[pp][x][y], gg[cp][x][y]); } kil[gg[pp][x][y]] = i; } else gg[cp][x][y] = gg[pp][x][y]; } } else if (s[i] == 'D') { if (g[x + 1][y]) { if (gg[cp][x + 1][y]) { if (depth[gg[pp][x][y]] > depth[gg[cp][x + 1][y]]) { swap(gg[pp][x][y], gg[cp][x + 1][y]); } else if (depth[gg[pp][x][y]] == depth[gg[cp][x + 1][y]] && gg[cp][x + 1][y] > gg[pp][x][y]) { swap(gg[pp][x][y], gg[cp][x + 1][y]); } kil[gg[pp][x][y]] = i; } else gg[cp][x + 1][y] = gg[pp][x][y]; } else { if (gg[cp][x][y]) { if (depth[gg[pp][x][y]] > depth[gg[cp][x][y]]) { swap(gg[pp][x][y], gg[cp][x][y]); } else if (depth[gg[pp][x][y]] == depth[gg[cp][x][y]] && gg[cp][x][y] > gg[pp][x][y]) { swap(gg[pp][x][y], gg[cp][x][y]); } kil[gg[pp][x][y]] = i; } else gg[cp][x][y] = gg[pp][x][y]; } } else if (s[i] == 'L') { if (g[x][y - 1]) { if (gg[cp][x][y - 1]) { if (depth[gg[pp][x][y]] > depth[gg[cp][x][y - 1]]) { swap(gg[pp][x][y], gg[cp][x][y - 1]); } else if (depth[gg[pp][x][y]] == depth[gg[cp][x][y - 1]] && gg[cp][x][y - 1] > gg[pp][x][y]) { swap(gg[pp][x][y], gg[cp][x][y - 1]); } kil[gg[pp][x][y]] = i; } else gg[cp][x][y - 1] = gg[pp][x][y]; } else { if (gg[cp][x][y]) { if (depth[gg[pp][x][y]] > depth[gg[cp][x][y]]) { swap(gg[pp][x][y], gg[cp][x][y]); } else if (depth[gg[pp][x][y]] == depth[gg[cp][x][y]] && gg[cp][x][y] > gg[pp][x][y]) { swap(gg[pp][x][y], gg[cp][x][y]); } kil[gg[pp][x][y]] = i; } else gg[cp][x][y] = gg[pp][x][y]; } } else if (s[i] == 'R') { if (g[x][y + 1]) { if (gg[cp][x][y + 1]) { if (depth[gg[pp][x][y]] > depth[gg[cp][x][y + 1]]) { swap(gg[pp][x][y], gg[cp][x][y + 1]); } else if (depth[gg[pp][x][y]] == depth[gg[cp][x][y + 1]] && gg[cp][x][y + 1] > gg[pp][x][y]) { swap(gg[pp][x][y], gg[cp][x][y + 1]); } kil[gg[pp][x][y]] = i; } else gg[cp][x][y + 1] = gg[pp][x][y]; } else { if (gg[cp][x][y]) { if (depth[gg[pp][x][y]] > depth[gg[cp][x][y]]) { swap(gg[pp][x][y], gg[cp][x][y]); } else if (depth[gg[pp][x][y]] == depth[gg[cp][x][y]] && gg[cp][x][y] > gg[pp][x][y]) { swap(gg[pp][x][y], gg[cp][x][y]); } kil[gg[pp][x][y]] = i; } else gg[cp][x][y] = gg[pp][x][y]; } } } } if (n == 5) for (int x = 1; x <= n; x++) { for (int y = 1; y <= m; y++) { if (gg[cp][x][y] == 164178) { cout << x << " " << y << "\n"; cout << to[164178] << "\n"; } } } if (n == 5) cout << "~~" << to[164187] << "\n\n"; for (int x = 1; x <= n; x++) for (int y = 1; y <= m; y++) if (g[x][y] && sz(adj[x * m + y])) iter_order.push_back({depth[x * m + y], x, y}); vector<int> kill_times; sort(all(iter_order)); for (auto [D, X, Y] : iter_order) { int lock = adj[X * m + Y][0]; for (int i : adj[X * m + Y]) { if (depth[i] > depth[lock]) lock = i; else if (depth[i] == depth[lock] && i < lock) lock = i; } for (int i : adj[X * m + Y]) { if (i == lock) continue; for (int j = 0; j < depth[i]; j++) { kill_times.push_back(kil[i] + j * k + 1); if (kil[i] + j * k + 1 == 1000000001) { cout << i << ": " << depth[i] << " " << sz(adj[i]) << "\n"; cout << D << " " << X << " " << Y << ": " << X * m + Y << "\n\n"; } } } } if (n == 5) return 0; sort(all(kill_times)); reverse(all(kill_times)); int cn = cnt; while (sz(kill_times)) { int t = kill_times.back(); ans[--cn] = t; kill_times.pop_back(); } for (int i = 1; i < cn; i++) ans[i] = -1; for (int i = 1; i <= m * n; i++) { cout << ans[i] << "\n"; } } /* bad: 4,4178 */
failed
47_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef long long ll; typedef vector<ll> VI; typedef basic_string<int> BI; typedef pair<int, int> PII; typedef double db; mt19937 mrand(random_device{}()); const ll mod = 1000000007; int rnd(int x) { return mrand() % x; } ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // head map<pair<VI, VI>, vector<pair<ll, ll>>> hs; vector<pair<ll, ll>> count(VI s, VI coef) { vector<pair<ll, ll>> v; if (s[0] == 0) { bool z = 1; for (auto x : s) z &= (x == 0); if (z) v.pb(mp(0, 1)); return v; } if (hs.count(mp(s, coef))) return hs[mp(s, coef)]; if (SZ(s) >= 2) { VI ss = s, cc = coef; ss.pop_back(); cc.pop_back(); if (count(ss, cc).empty()) return v; } for (int j = 1; j <= s[0]; j++) for (int k = 1; k * j <= s[0]; k++) { VI ncoef = coef, ns = s; ncoef.insert(ncoef.begin(), k); bool val = 1; rep(l, 0, SZ(s)) { ns[l] = ns[l] - j * ncoef[l]; if (ns[l] < 0) { val = 0; break; } } ncoef.pop_back(); if (val) { auto d = count(ns, ncoef); for (auto [key, val] : d) v.pb(mp(key + j * coef.back(), val)); } } sort(all(v)); vector<pair<ll, ll>> nv; for (auto [key, val] : v) { if (!nv.empty() && key == nv.back().fi) nv.back().se += val; else nv.pb(mp(key, val)); } return hs[mp(s, coef)] = nv; } vector<pair<VI, VI>> ret; void dfs(VI s, VI coef, VI p1, VI p2) { if (s[0] == 0) { bool z = 1; for (auto x : s) z &= (x == 0); if (z) { reverse(all(p1)); reverse(all(p2)); ret.pb(mp(p1, p2)); } } if (SZ(s) >= 2) { VI ss = s, cc = coef; ss.pop_back(); cc.pop_back(); if (count(ss, cc).empty()) return; } for (int j = 1; j <= s[0]; j++) for (int k = 1; k * j <= s[0]; k++) { VI ncoef = coef, ns = s; ncoef.insert(ncoef.begin(), k); bool val = 1; rep(l, 0, SZ(s)) { ns[l] = ns[l] - j * ncoef[l]; if (ns[l] < 0) { val = 0; break; } } VI q1 = p1, q2 = p2; q1.pb(j); q2.pb(k); ncoef.pop_back(); if (val) dfs(ns, ncoef, q1, q2); } } int K; VI seq; int main() { scanf("%d", &K); rep(i, 1, 25) { auto d = count(VI{i}, VI{i}); ll z = 0; for (auto [key, val] : d) { z += val; } if (K > z) { K -= z; } else { seq.pb(i); break; } } ll v = 0; rep(rd, 1, 10) { auto d = count(seq, seq); for (auto [key, val] : d) { if (K > val) { K -= val; } else { seq.pb(key); // fprintf(stderr,"%lld %lld\n",key,val); v = val; break; } } } dfs(seq, seq, VI{}, VI{}); assert(SZ(ret) == v); sort(all(ret)); // printf("!! %d\n",K); auto [p1, p2] = ret[K - 1]; printf("%d\n", SZ(p1)); for (auto x : p1) printf("%lld ", x); puts(""); for (auto x : p2) printf("%lld ", x); puts(""); for (auto x : seq) printf("%lld ", x); puts(""); }
failed
114_3_wrong.cpp
#include <bits/stdc++.h> #define int long long using namespace std; const int N = 5009; int n, k; int a[N]; int stk[N], top, fa[N], ls[N], rs[N]; int g(int x, int y) { if (x == 0 || y == 0) { return x | y; } return gcd(x, y); } void solve() { for (int i = 1; i <= n; i++) { ls[i] = rs[i] = 0, fa[i] = 0; top = 0; stk[i] = 0; } cin >> n >> k; int mn = 1e9; for (int i = 1; i <= n; i++) { cin >> a[i]; mn = min(mn, a[i]); } if (n == 1) { cout << k << ' ' << (k * (k + 1)) / 2 << '\n'; return; } for (int i = 1; i <= n; i++) { int k = top; while (k > 0 && a[stk[k]] > a[i]) { k--; } if (k) { rs[stk[k]] = i; } if (k < top) { ls[i] = stk[k + 1]; } stk[++k] = i; top = k; } for (int i = 1; i <= n; i++) { fa[ls[i]] = i; fa[rs[i]] = i; } int d = 0; for (int i = 1; i <= n; i++) { d = g(d, a[i] - mn); } int ans = 0, cnt = 0; auto calc = [&](int x) { if (x <= mn) { return; } if (x - mn > k) { return; } for (int i = 1; i <= n; i++) { if (fa[i]) { if ((a[i] + x - mn) % (a[fa[i]] + x - mn)) { return; } } } ans += x - mn; cnt++; }; for (int i = 1; i <= d / i; i++) { if (d % i == 0) { calc(i); if (d / i == i) { break; } calc(d / i); } } cout << cnt << ' ' << ans << "\n"; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T = 1; cin >> T; while (T--) { solve(); } return 0; }
failed
33_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; using i64 = long long; using pii = pair<int, int>; constexpr int maxn = 200000 + 10; int t[maxn], a[maxn], d[maxn]; vector<int> e[maxn]; int f[maxn]; i64 g[maxn]; vector<int> p[maxn]; int id[maxn], pd[maxn]; int dep[maxn], pre[maxn], len[maxn]; int lst[maxn]; int du[maxn]; int n, k; int now, cyc; void dfs(int u) { du[dep[u]] = u; p[t[u]].emplace_back(dep[u]); if (!d[u]) { if (p[t[u]].size() >= k) f[u] = du[p[t[u]][dep[u] - k + 1]], g[u] = dep[u] - p[t[u]][dep[u] - k + 1]; else if (lst[t[u]]) { f[u] = pre[f[lst[t[u]]]]; g[u] = g[lst[t[u]]] - len[f[lst[t[u]]]]; if (d[lst[t[u]]]) g[u] += (now - id[lst[t[u]]] + cyc) % cyc; else g[u] += dep[u] - dep[lst[t[u]]]; } else f[u] = 0, g[u] = 0x3f3f3f3f3f3f3f3f; } int x = lst[t[u]]; lst[t[u]] = u; for (int v : e[u]) { dep[v] = dep[u] + 1; dfs(v); } lst[t[u]] = x; p[t[u]].pop_back(); } void dwn(int u) { for (int v : e[u]) { if (g[u] + 1 < g[v]) g[v] = g[u] + 1, f[v] = f[u]; dwn(v); } } void solve() { cin >> n >> k; for (int i = 1; i <= n; ++i) e[i].clear(); for (int i = 1; i <= n; ++i) cin >> t[i]; for (int i = 1; i <= n; ++i) cin >> a[i]; memset(d, 0, sizeof(d[0]) * (n + 5)); for (int i = 1; i <= n; ++i) ++d[a[i]]; queue<int> q; for (int i = 1; i <= n; ++i) if (!d[i]) q.push(i); while (q.size()) { int u = q.front(); q.pop(); if (!--d[a[u]]) q.push(a[u]); } memset(f, 0, sizeof(f[0]) * (n + 5)); for (int i = 1; i <= n; ++i) if (!d[i]) e[a[i]].emplace_back(i); for (int u = 1; u <= n; ++u) if (!f[u] && d[u]) { vector<int> c; int v = u; do { c.emplace_back(v); v = a[v]; } while (v != u); int x = 0; for (int v : c) { pd[v] = p[t[v]].size(); p[t[v]].emplace_back(x); id[v] = x++; } for (int v : c) { int u = p[t[v]][(pd[v] + k - 1) % p[t[v]].size()]; f[v] = c[u]; g[v] = (i64)(k - 1) / p[t[v]].size() * x; g[v] += (u - id[v] + x) % x; u = p[t[v]][(pd[v] + p[t[v]].size() - 1) % p[t[v]].size()]; pre[v] = c[u]; len[u] = (id[v] - u + x) % x; if (!len[u]) len[u] = x; } for (int v : c) lst[t[v]] = v; for (int v : c) p[t[v]].clear(); cyc = x; for (int v : c) now = id[v], dep[v] = 0, dfs(v), lst[t[v]] = v; int lst = c.back(); for (int t = 0; t < 2; ++t) for (int v : c) { if (g[v] + 1 < g[lst]) g[lst] = g[v] + 1, f[lst] = f[v]; lst = v; } for (int v : c) dwn(v); for (int v : c) ::lst[t[v]] = 0; } i64 ans = 0; for (int i = 1; i <= n; ++i) ans += (i64)i * t[f[i]]; cout << ans << '\n'; // for (int i=1;i<=n;++i) cout << t[f[i]] << ' '; // cout << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) solve(); return 0; }
failed
29_2_wrong.cpp
#include <iostream> #include <queue> #include <string> #include <unordered_set> #include <vector> using namespace std; class Solution { private: int h, w; // Convert grid state to string for hashing string getState(const vector<string> &grid) { string state; for (const auto &row : grid) state += row; return state; } // Tilt the grid in given direction vector<string> tilt(const vector<string> &grid, int dir) { vector<string> result = grid; if (dir == 0 || dir == 1) { // Left or Right for (int i = 0; i < h; i++) { string row; for (int j = 0; j < w; j++) { if (isalpha(grid[i][j])) row += grid[i][j]; } string newRow(w, '.'); if (dir == 0) { // Left for (int j = 0; j < row.length(); j++) { newRow[j] = row[j]; } } else { // Right for (int j = 0; j < row.length(); j++) { newRow[w - row.length() + j] = row[j]; } } result[i] = newRow; } } else { // Up or Down vector<string> cols(w, ""); for (int j = 0; j < w; j++) { for (int i = 0; i < h; i++) { if (isalpha(grid[i][j])) cols[j] += grid[i][j]; } } for (int j = 0; j < w; j++) { for (int i = 0; i < h; i++) { result[i][j] = '.'; } if (dir == 2) { // Up for (int i = 0; i < cols[j].length(); i++) { result[i][j] = cols[j][i]; } } else { // Down for (int i = 0; i < cols[j].length(); i++) { result[h - cols[j].length() + i][j] = cols[j][i]; } } } } return result; } // Check if tiles count matches bool checkTilesCount(const vector<string> &start, const vector<string> &target) { vector<int> count1(26, 0), count2(26, 0); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (isalpha(start[i][j])) count1[start[i][j] - 'a']++; if (isalpha(target[i][j])) count2[target[i][j] - 'a']++; } } return count1 == count2; } public: bool solve(int height, int width, vector<string> &start, vector<string> &target) { h = height; w = width; // First check if number of tiles of each color matches if (!checkTilesCount(start, target)) return false; // BFS to find if target is reachable queue<vector<string>> q; unordered_set<string> visited; string targetState = getState(target); string startState = getState(start); if (startState == targetState) return true; q.push(start); visited.insert(startState); while (!q.empty()) { vector<string> curr = q.front(); q.pop(); // Try all four directions for (int dir = 0; dir < 4; dir++) { vector<string> next = tilt(curr, dir); string nextState = getState(next); if (nextState == targetState) return true; if (!visited.count(nextState)) { visited.insert(nextState); q.push(next); } } } return false; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int h, w; cin >> h >> w; vector<string> start(h), target(h); for (int i = 0; i < h; i++) { cin >> start[i]; } string empty; getline(cin, empty); getline(cin, empty); for (int i = 0; i < h; i++) { cin >> target[i]; } Solution solution; cout << (solution.solve(h, w, start, target) ? "yes" : "no") << endl; return 0; }
failed
115_1_wrong.cpp
#include <algorithm> #include <cstring> #include <stdio.h> constexpr int inf{0x3f3f3f3f}; constexpr int N{300000}; struct edge { int v, next; }; edge e[(N << 1) + 5]; int en, last[N + 5]; inline void add_edge(const int &u, const int &v) { e[++en] = {v, last[u]}, last[u] = en; e[++en] = {u, last[v]}, last[v] = en; } int a[N + 5]; int deg[N + 5][4]; int dis[N + 5][4]; int tmp[N + 5][4]; int que[(N << 2) + 5]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", a + i); } for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); add_edge(u, v); } int ans{inf}; for (int c = 0; c != 4; c++) { int val[4]{c & 1, c >> 1, c & 1 ^ 1, (c >> 1) ^ 1}; std::memset(dis, 0x3f, sizeof(dis)); std::memset(tmp, 0, sizeof(tmp)); std::memset(deg, 0, sizeof(deg)); for (int i = 1; i <= n; i++) { for (int j = 0; j != 4; j++) { if (a[i] == val[j + 1 & 3]) { for (int k = last[i]; k; k = e[k].next) { ++deg[e[k].v][j]; } } } } int qb{1}, qe{0}; for (int i = 1; i <= n; i++) { for (int j = 0; j != 4; j++) { if (deg[i][j] == 0 && a[i] == val[j]) { dis[i][j] = 1; que[++qe] = i << 2 | j; printf("> %d %d\n", i, j); } } } while (qb <= qe) { int &_{que[qb++]}; int u{_ >> 2}, t0{_ & 3}, t1{t0 - 1 & 3}; for (int i = last[u]; i; i = e[i].next) { int &v{e[i].v}; if (a[v] != val[t1]) { continue; } tmp[v][t1] = std::max(tmp[v][t1], dis[u][t0] + 1); if (!--deg[v][t1]) { dis[v][t1] = tmp[v][t1]; que[++qe] = v << 2 | t1; } } } int len{0}; for (int i = 1; i <= n; i++) { if (a[i] == val[0]) { len = std::max(len, dis[i][0]); } } ans = std::min(ans, len); } if (ans >= inf) { puts("infinity"); } else { printf("%d\n", ans + 1); } return 0; }
failed
58_2_wrong.cpp
#include <bits/stdc++.h> #define int long long #define mod 998244353ll #define pii pair<int, int> #define fi first #define se second #define mems(x, y) memset(x, y, sizeof(x)) #define pb push_back #define db double using namespace std; const int maxn = 200010; const int inf = 1e18; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch - 48); ch = getchar(); } return x * f; } bool Mbe; int n; char s[maxn]; vector<pii> ans; void work() { scanf("%s", s + 1); n = strlen(s + 1); if (s[1] != '>') { puts("No"); return; } if (s[n - 2] != '>' || s[n - 1] != '>' || s[n] != '>') { puts("No"); return; } int p = n - 2; bool fl = 0; for (int i = 1; i <= n; i++) if (s[i] == '-') fl = 1, p = i + 1; if (!fl) { puts("No"); return; } for (int i = n - 2;; i = min(i - 3, p)) { ans.pb({1, i + 2}); if (i == p) break; } for (int i = 2; i < p; i++) if (s[i] == '>') ans.pb({i, p + 2}); printf("Yes %lld\n", (int)ans.size()); for (auto [l, r] : ans) printf("%lld %lld\n", l, r - l + 1); ans.clear(); } // \ 444 bool Med; int T; signed main() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); // ios::sync_with_stdio(0); // cin.tie(0);cout.tie(0); // cerr<<(&Mbe-&Med)/1048576.0<<" MB\n"; T = read(); while (T--) work(); }
failed
1_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using i64 = long long; using u64 = unsigned long long; template <class T> struct Scc { int n; int cnt; int cur; vector<vector<T>> g; vector<T> stk; vector<T> dfn, low, bel; Scc(int _n) : n(_n) { init(); } void init() { dfn.assign(n + 1, -1); low.resize(n + 1); bel.assign(n + 1, -1); g.assign(n + 1, {}); stk.clear(); cur = 1; cnt = 0; } void add(int u, int v) { g[u].push_back(v); } void dfs(int u) { dfn[u] = low[u] = ++cnt; stk.push_back(u); for (auto v : g[u]) { if (dfn[v] == -1) { dfs(v); low[u] = min(low[u], low[v]); } else if (bel[v] == -1) { low[u] = min(low[u], dfn[v]); } } if (dfn[u] == low[u]) { int y; do { y = stk.back(); bel[y] = cur; stk.pop_back(); } while (u != y); cur++; } } vector<T> work() { for (int i = 1; i <= n; i++) { if (bel[i] == -1) dfs(i); } return bel; } }; void solve() { int n, m, q; cin >> n >> m >> q; Scc<int> g(n); vector a(m + 1, vector<int>(n + 1)); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) cin >> a[i][j]; } set<pair<int, int>> f; for (int i = 1; i <= m; i++) { for (int j = 1; j < n; j++) { if (f.insert({a[i][j], a[i][j + 1]}).second == 1) { g.add(a[i][j], a[i][j + 1]); } } } g.work(); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { a[i][j] = g.bel[a[i][j]]; } } vector r(m + 1, vector<int>(n + 1)); vector pre(m + 1, vector<ll>(n + 1)); for (int i = 1; i <= m; i++) { r[i][n] = n; for (int j = n - 1; j >= 1; j--) { if (a[i][j] == a[i][j + 1]) r[i][j] = r[i][j + 1]; else r[i][j] = j; } pre[i][1] = 0; int l = 1; for (int j = 2; j <= n; j++) { if (a[i][j] != a[i][j - 1]) { l = j; pre[i][j] = pre[i][j - 1]; } else { pre[i][j] = pre[i][j - 1] + (j - l); } } } auto calc = [&](int id, int L, int R) -> ll { ll sum = 0; if (r[id][L] <= R) { sum += 1LL * (r[id][L] - L) * (r[id][L] - L + 1) / 2; L = r[id][L]; } if (L <= R) sum += pre[id][R] - pre[id][L]; return sum; }; ll ans = 0; while (q--) { ll id, L, R; cin >> id >> L >> R; id = (id + ans) % m + 1; L = (L + ans) % n + 1; R = (R + ans) % n + 1; ans = calc(id, L, R); cout << ans << '\n'; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) solve(); return 0; }
failed
28_2_wrong.cpp
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #define rep(i, a, b) for (int i = a; i < (b); i++) #define pb push_back #define all(x) x.begin(), x.end() #define A first #define B second #define SZ(x) int(x.size()) #define lc (id << 1) #define rc (id << 1 | 1) using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; const int xn = 2e5 + 10; const int xm = 5; const int mod = 998244353; int n, k, p[xn], pos[xn]; vi seg[xn << 2]; inline vi merge(const vi &v, const vi &u) { vi w; int p1 = 0, p2 = 0; while (SZ(w) < xm && (p1 < SZ(v) || p2 < SZ(u))) { if (p1 == SZ(v)) w.pb(u[p2++]); else if (p2 == SZ(u)) w.pb(v[p1++]); else { if (v[p1] < u[p2]) w.pb(v[p1++]); else w.pb(u[p2++]); } } return w; } void build(int id, int l, int r) { if (r - l == 1) { seg[id] = {pos[l]}; return; } int mid = l + r >> 1; build(lc, l, mid), build(rc, mid, r); seg[id] = merge(seg[lc], seg[rc]); } vi get(int id, int l, int r, int ql, int qr) { if (qr <= l || r <= ql || qr <= ql) return {}; if (ql <= l && r <= qr) return seg[id]; int mid = l + r >> 1; return merge(get(lc, l, mid, ql, qr), get(rc, mid, r, ql, qr)); } int Get(int i, pii X) { int s = X.A, t = X.B; if (i < s) return ((i - 1) ^ 1) + 1; if (i > s + 2) return (i ^ 1); i = (i - s + t + 3) % 3; return s + i; } bool cmp(pii x, pii y) { int l = min(x.A, y.A); int r = max(x.A, y.A) + 3; vi I = get(1, 1, n + 1, l, r); for (int i : I) { int v = Get(p[i], x); int u = Get(p[i], y); if (v == u) continue; return v < u; } assert(false); return true; } void Main() { cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> p[i], pos[p[i]] = i; if (n % 2 == 0) { if (k == 1) { for (int i = 1; i <= n; ++i) cout << ((p[i] - 1) ^ 1) + 1 << " "; cout << "\n"; return; } cout << "-1\n"; return; } vector<pii> V; for (int i = 1; i <= n - 2; i += 2) { V.pb({i, -1}); V.pb({i, 1}); } if (SZ(V) < k) { cout << "-1\n"; return; } build(1, 1, n + 1); sort(all(V), cmp); int sm = 0; for (int i = 1; i <= n; ++i) { sm += abs(p[i] - Get(p[i], V[k - 1])); cout << Get(p[i], V[k - 1]) << " "; } // cout << " : " << sm; cout << "\n"; return; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int qq; cin >> qq; while (qq--) { Main(); } }
failed
3_3_wrong.cpp
#include <algorithm> #include <iostream> #include <map> #include <queue> #include <vector> using namespace std; typedef long long ll; typedef pair<ll, pair<int, int>> plli; // (dist, (site, line)) const ll INF = 1e18; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<ll> a(k + 1), b(k + 1); for (int i = 1; i <= k; i++) cin >> a[i]; for (int i = 1; i <= k; i++) cin >> b[i]; // Store subway lines information vector<map<int, ll>> next(k + 1); // next[line][from] = to vector<map<int, ll>> cost(k + 1); // cost[line][from] = cost_to_next vector<vector<int>> sites_on_line(k + 1); // sites on each line for (int i = 1; i <= k; i++) { int p; cin >> p; vector<int> sites; vector<ll> weights; // Read sites and weights for (int j = 0; j < p; j++) { int site; cin >> site; sites.push_back(site); if (j < p - 1) { ll w; cin >> w; weights.push_back(w); } } // Store sites for this line sites_on_line[i] = sites; // Create connections for (int j = 0; j < p - 1; j++) { next[i][sites[j]] = sites[j + 1]; cost[i][sites[j]] = weights[j]; } } // Dijkstra's algorithm vector<vector<ll>> dist(n + 1, vector<ll>(k + 1, INF)); priority_queue<plli, vector<plli>, greater<plli>> pq; // Add initial states at site 1 for (int line = 1; line <= k; line++) { if (find(sites_on_line[line].begin(), sites_on_line[line].end(), 1) != sites_on_line[line].end()) { dist[1][line] = 0; pq.push({0, {1, line}}); } } while (!pq.empty()) { ll d = pq.top().first; int site = pq.top().second.first; int line = pq.top().second.second; pq.pop(); if (d > dist[site][line]) continue; // Try moving along current line if (next[line].count(site)) { int nxt_site = next[line][site]; ll nxt_cost = cost[line][site]; if (dist[nxt_site][line] > d + nxt_cost) { dist[nxt_site][line] = d + nxt_cost; pq.push({dist[nxt_site][line], {nxt_site, line}}); } } // Try transferring to other lines for (int new_line = 1; new_line <= k; new_line++) { if (new_line == line) continue; if (find(sites_on_line[new_line].begin(), sites_on_line[new_line].end(), site) != sites_on_line[new_line].end()) { ll transfer_cost = a[new_line] * b[line]; if (dist[site][new_line] > d + transfer_cost) { dist[site][new_line] = d + transfer_cost; pq.push({dist[site][new_line], {site, new_line}}); } } } } // Find minimum time to each site for (int site = 2; site <= n; site++) { ll min_time = INF; for (int line = 1; line <= k; line++) { min_time = min(min_time, dist[site][line]); } cout << min_time << " "; } cout << "\n"; return 0; }
failed
52_2_wrong.cpp
#pragma GCC optimize("Ofast,unroll-loops") // #pragma GCC target("avx,avx2,tune=native") #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdlib> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <unordered_map> #include <unordered_set> #include <valarray> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long unll; typedef unsigned int unii; #define fi first #define se second #define double long double #define int long long typedef pair<int, int> pll; const int modulo = 998244353; const int mod1 = 1e9 + 7; const int mod2 = 1e9 + 123; const int N = 2e5 + 5; const int INF = 1e18; const int MXN = N - 5; void solve() { int n, p; cin >> n >> p; vector<int> c(n); for (int i = 0; i < n; i++) cin >> c[i]; int sum = 0; for (int i = 0; i < n; i++) { sum += c[i]; if (sum + i * c[i] > p) { cout << i << endl; return; } } cout << n << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); srand((signed)time(0)); int t = 1; // cin >> t; while (t--) solve(); return 0; }
failed
107_1_wrong.cpp
#include <iostream> #include <string> #include <vector> using namespace std; const int MOD = 1000000007; string s; int n; vector<vector<vector<vector<vector<long long>>>>> dp; // Returns number of ways to arrange costumes from position pos onwards // prev: previous costume type (0=a, 1=b, 2=c, 3=start) // ra, rb, rc: remaining costumes of each type long long solve(int pos, int prev, int ra, int rb, int rc) { // Invalid case: used too many costumes if (ra < 0 || rb < 0 || rc < 0) return 0; // Base case: reached end of sequence if (pos == n) return 1; // Return memoized result if exists if (dp[pos][prev][ra][rb][rc] != -1) return dp[pos][prev][ra][rb][rc]; long long result = 0; // If current position already has a costume if (s[pos] != '?') { int curr; if (s[pos] == 'a') curr = 0; else if (s[pos] == 'b') curr = 1; else curr = 2; if (curr != prev) // Check if valid with previous result = solve(pos + 1, curr, ra, rb, rc); } // Try each possible costume else { // Try costume a if (prev != 0) result = (result + solve(pos + 1, 0, ra - 1, rb, rc)) % MOD; // Try costume b if (prev != 1) result = (result + solve(pos + 1, 1, ra, rb - 1, rc)) % MOD; // Try costume c if (prev != 2) result = (result + solve(pos + 1, 2, ra, rb, rc - 1)) % MOD; } return dp[pos][prev][ra][rb][rc] = result; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> n >> Q; cin >> s; while (Q--) { int x, y, z; cin >> x >> y >> z; // Initialize dp table dp.assign(n + 1, vector<vector<vector<vector<long long>>>>( 4, vector<vector<vector<long long>>>( x + 1, vector<vector<long long>>( y + 1, vector<long long>(z + 1, -1))))); // Start with no previous costume (use 3 as initial prev) cout << solve(0, 3, x, y, z) << '\n'; } return 0; }
failed
2_2_wrong.cpp
#include <algorithm> #include <assert.h> #include <bitset> #include <climits> #include <complex> #include <deque> #include <exception> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <istream> #include <iterator> #include <limits> #include <list> #include <locale> #include <map> #include <memory> #include <new> #include <numeric> #include <ostream> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <string> #include <typeinfo> #include <unordered_map> #include <unordered_set> #include <utility> #include <valarray> #include <vector> using namespace std; void solve([[maybe_unused]] int t) { int r, c, p; cin >> r >> c >> p; vector dp(r, vector<double>(c, -1.0)); auto f = [&](auto f, int x, int y) -> double { if (dp[x][y] > -0.5) return dp[x][y]; if (x == r - 1 && y == c - 1) return 0.0; double res = 0; if (x == r - 1) { double a = f(f, x, y + 1); res = 0.5 * a + 0.5 * (a + p / 2.0); } else if (y == c - 1) { double a = f(f, x + 1, y); res = 0.5 * a + 0.5 * (a + p / 2.0); } else { double a = f(f, x, y + 1); double b = f(f, x + 1, y); res += 0.5 * min(a, b); if (min(a, b) + p / 2.0 < max(a, b)) { res += 0.5 * (min(a, b) + p / 2.0); } else { double k = max(a, b) - min(a, b); res += 0.5 * (((p - k) * max(a, b) + (min(a, b) + max(a, b)) * k * 0.5) / p); } } // cout << x << " " << y << " " << res << "\n"; return dp[x][y] = res; }; cout << setprecision(20) << f(f, 0, 0) << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n = 1; // cin >> n; for (int i = 1; i <= n; i++) solve(i); }
failed
105_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; int n, m, s[200005], sum[200005], head, tail, cnt[200005]; long double mid, f[200005]; pair<int, pair<int, int>> q[200005]; long double w(int i, int j) { return f[j] + sqrt((i - j) * (sum[i] - sum[j])) + mid; } void solve() { q[head = tail = 1] = {0, {1, n}}; for (int i = 1; i <= n; i++) { while (head <= tail && q[head].second.second < i) head++; f[i] = w(i, q[head].first), cnt[i] = cnt[q[head].first] + 1; while (head <= tail && w(q[tail].second.first, i) < w(q[tail].second.first, q[tail].first)) tail--; if (head > tail) { q[++tail] = {i, {i + 1, n}}; continue; } if (w(q[tail].second.second, i) > w(q[tail].second.second, q[tail].first)) { if (q[tail].second.second < n) tail++, q[tail] = {i, {q[tail - 1].second.second + 1, n}}; continue; } int l = q[tail].second.first, r = q[tail].second.second, p; while (l <= r) { int Mid = (l + r) >> 1; if (w(Mid, i) < w(Mid, q[tail].first)) p = Mid, r = Mid - 1; else l = Mid + 1; } q[tail].second.second = p - 1, q[++tail] = {i, {p, n}}; } } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> s[i]; sort(s + 1, s + n + 1); for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + s[i]; long double l = 0, r = 1e12; while (r - l > 1e-11) { mid = (l + r) / 2, solve(); if (cnt[n] < m) r = mid; else l = mid; } mid = l, solve(); printf("%.13Lf", f[n] - m * mid); return 0; }
failed
5_1_wrong.cpp
#include <bits/stdc++.h> #define db long double #define int long long #define pb push_back #define pii pair<int, int> #define X first #define Y second #define pcc pair<char, char> #define vi vector<int> #define vii vector<vector<int>> #define Rep(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) #define Drp(i, a, b) for (int i = (int)(a); i >= (int)(b); --i) #define sz(x) (x).size() #define All(x) (x).begin(), (x).end() using namespace std; const int N = 1e6 + 10; int tot = 1; int trie[N][30]; int sum[N]; int ans = 0, dp[N]; vi divs[N]; void init(int n) { Rep(i, 1, n) { for (int j = i; j <= n; j += i) divs[j].push_back(i); } } void add(string &S) { int p = 1; for (auto c : S) { int tmp = c - 'a'; if (!trie[p][tmp]) { ++tot; trie[p][tmp] = tot; } p = trie[p][tmp]; sum[p]++; for (auto num : divs[sum[p]]) { ans ^= dp[num]; dp[num] += num; ans ^= dp[num]; } } } void solve() { int n; cin >> n; Rep(i, 1, n) { string s; cin >> s; // cout << s << endl; add(s); cout << ans << " "; } cout << endl; Rep(i, 0, tot) Rep(j, 0, 25) trie[i][j] = 0; Rep(i, 0, tot) sum[i] = dp[i] = 0; ans = 0, tot = 1; } signed main() { init(1e6); ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int T = 1; cin >> T; while (T--) solve(); }
failed
40_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; struct Decimal { static constexpr double eps = 1e-8; double x; Decimal(double p) : x{p} {} Decimal() : x{0} {} bool operator<(const Decimal &d) const { return (d.x - x > eps); } bool operator==(const Decimal &d) const { return fabs(x - d.x) < eps; } bool operator!=(const Decimal &d) const { return !(*this == d); } bool operator>(const Decimal &d) const { return (x - d.x > eps); } bool operator<=(const Decimal &d) const { return !(*this > d); } bool operator>=(const Decimal &d) const { return !(*this < d); } Decimal operator+(const Decimal &d) const { return {x + d.x}; } Decimal operator+=(const Decimal &d) { return *this = *this + d; } Decimal operator-(const Decimal &d) const { return {x - d.x}; } Decimal operator-() const { return {-x}; } Decimal operator-=(const Decimal &d) { return *this = *this - d; } Decimal operator*(const Decimal &d) const { return {x * d.x}; } Decimal operator*=(const Decimal &d) { return *this = *this * d; } Decimal operator/(const Decimal &d) const { return d == 0.0 ? 2e18 : x / d.x; } Decimal operator/=(const Decimal &d) { return *this = *this / d; } Decimal sqrt() const { return std::sqrt(x); } Decimal sqr() const { return x * x; } Decimal operator^(const Decimal &expo) const { return std::pow(x, expo.x); } Decimal abs() const { return Decimal(fabs(x)); } Decimal cos() const { return std::cos(this->val()); } Decimal sin() const { return std::sin(this->val()); } Decimal tan() const { return std::tan(this->val()); } Decimal acos() const { return std::acos(this->val()); } Decimal asin() const { return std::asin(this->val()); } Decimal exp() const { return std::exp(this->val()); } Decimal floor() const { return std::floor(this->x); } Decimal ceil() const { return std::ceil(this->x); } Decimal round() const { return std::round(this->x); } friend ostream &operator<<(ostream &out, const Decimal &d) { return (out << d.x); } friend istream &operator>>(istream &in, Decimal &d) { return (in >> d.x); } int sign() const { return -eps < x && x < eps ? 0 : (x < 0 ? -1 : 1); } double val() const { return x; } long long toll() const { return static_cast<long long>(this->x); } }; const Decimal PI = numbers::pi; namespace Geo2D { enum class STATUS { Parallel, OnSameLineOverlap, OnSameLineDisjoint, SameLine, IntersectionSeg, IntersectionLine }; inline ostream &operator<<(ostream &out, const STATUS &s) { switch (s) { case STATUS::Parallel: out << "(Lines or Segs) Parallel"; break; case STATUS::OnSameLineDisjoint: out << "(Segs) On same line, disjoint"; break; case STATUS::SameLine: out << "(Lines) Are same line."; break; case STATUS::IntersectionSeg: out << "(Segments) Intersect"; break; case STATUS::IntersectionLine: out << "(Lines) Intersect"; break; case STATUS::OnSameLineOverlap: out << "(Segs) Overlap on same line"; break; default: break; } return out; } #define Point Vector struct Vector { Decimal x, y; Vector() : x{0}, y{0} {} Vector(const Decimal &a, const Decimal &b) : x(a), y(b) {} // 基础运算 Vector operator+(const Vector &v) const { return {x + v.x, y + v.y}; } Vector operator+=(const Vector &v) { return *this = *this + v; } Vector operator+(const Decimal &t) const { return {x + t, y + t}; } Vector operator+=(const Decimal &t) { return *this = *this + t; } Vector operator-(const Vector &v) const { return {x - v.x, y - v.y}; } Vector operator-=(const Vector &v) { return *this = *this - v; } Vector operator-=(const Decimal &t) { return *this = *this - t; } Vector operator-(const Decimal &t) const { return {x - t, y - t}; } Vector operator-() const { return Vector{-x, -y}; } Vector operator*=(const Decimal &t) { return *this = *this * t; } Vector operator*(const Decimal &t) const { return {x * t, y * t}; } Vector operator/(const Decimal &d) const { return {x / d, y / d}; } Vector operator/=(const Decimal &d) { return *this = *this / d; } bool operator>(const Vector &v) const { return tie(x, y) > tie(v.x, v.y); } bool operator>=(const Vector &v) const { return x != v.x ? x >= v.x : y >= v.y; } bool operator<(const Vector &v) const { return x != v.x ? x < v.x : y < v.y; } bool operator<=(const Vector &v) const { return x != v.x ? x <= v.x : y <= v.y; } bool operator==(const Vector &v) const { return x == v.x and y == v.y; } bool operator!=(const Vector &v) const { return not this->operator==(v); } friend ostream &operator<<(ostream &out, const Vector &a) { return (out << "(" << a.x << ", " << a.y << ")"); } friend istream &operator>>(istream &in, Vector &a) { return (in >> a.x >> a.y); } // 模长 Decimal Scale() const { return (x.sqr() + y.sqr()).sqrt(); } // 单位向量 Vector Unit() const { return *this / this->Scale(); } // 与另一个向量的点积 Decimal dot(const Vector &v) const { return x * v.x + y * v.y; } // 向量与给定向量的叉积 Decimal cross(const Vector &v) const { return x * v.y - y * v.x; } Decimal cross(const Point &p1, const Point &p2) { return (p1 - *this).cross(p2 - *this); } // 倾斜角 Decimal Angle() const { return atan2(y.val(), x.val()); } // 与另一个向量之间的夹角 Decimal AngleBetween(const Vector &v) { return (dot(v) / Scale() / v.Scale()).acos(); } // 与另一个点的距离 Decimal Distance(const Point &p) const { return (*this - p).Scale(); } // 垂直向量 Vector Perp() { return Vector{-y, x}; } Vector Normal() { return Perp().Unit(); } // 绕点旋转 Point Rotate(const Decimal &ang) { return {x * ang.cos() - y * ang.sin(), x * ang.sin() + y * ang.cos()}; } Point Rotate(const Decimal &ang, const Point &O) { return O + (*this - O).Rotate(ang); } // 关于点 p 的对称点 Point SymmetricPoint(const Point &p) const { return p * 2 - *this; } // 投影到 v 上的投影向量 Vector ProjectVector(const Vector &v) const { return v.Unit() * dot(v) / v.Scale(); } // 关于 v 的对称向量 Vector SymmetricVector(const Vector &v) const { return ProjectVector(v) * 2 - *this; } }; #pragma endregion } // namespace Geo2D using namespace Geo2D; Point p1[4], p2[4], tip1, tip2; Decimal h1, h2, d1, d2, len1, len2; Decimal phi = 0.618, cphi = -phi + 1; int main() { cin >> p1[0] >> p1[1] >> h1; cin >> p2[0] >> p2[1] >> h2; for (int i = 2; i < 4; i++) { p1[i] = p1[i - 1] + (p1[i - 1] - p1[i - 2]).Perp(); p2[i] = p2[i - 1] + (p2[i - 1] - p2[i - 2]).Perp(); } tip1 = (p1[0] + p1[2]) / 2; tip2 = (p2[0] + p2[2]) / 2; len1 = p1[0].Distance(p1[1]); len2 = p2[0].Distance(p2[1]); d1 = (len1.sqr() / 4 + h1.sqr()).sqrt(); d2 = (len2.sqr() / 4 + h2.sqr()).sqrt(); Decimal ans = 2e18; for (int i = 0; i < 4; i++) { Vector v1 = p1[(i + 1) % 4] - p1[i]; Point midp1 = (p1[i] + p1[(i + 1) % 4]) / 2; Point pt1 = midp1 + v1.Normal() * d1; for (int j = 0; j < 4; j++) { Vector v2 = p2[(j + 1) % 4] - p2[j]; Point midp2 = (p2[j] + p2[(j + 1) % 4]) / 2; Point pt2 = midp2 + v2.Normal() * d2; Decimal precent_l1 = 0; Decimal precent_r1 = 1; auto findfoot1 = [&](Decimal precent_mid1) -> Decimal { Point foot1 = p1[i] + v1 * precent_mid1; Decimal precent_l2 = 0; Decimal precent_r2 = 1; auto findfoot2 = [&](Decimal precent_mid2) -> Decimal { Point foot2 = p2[j] + v2 * precent_mid2; return foot1.Distance(foot2) + foot1.Distance(pt1) + foot2.Distance(pt2); }; for (int __ = 1; __ <= 100; __++) { Decimal l2 = precent_l2 * phi + precent_r2 * cphi; Decimal r2 = precent_l2 * cphi + precent_r2 * phi; if (findfoot2(l2) > findfoot2(r2)) precent_l2 = l2; else precent_r2 = r2; } return findfoot2(precent_l2); }; for (int _ = 1; _ <= 100; _++) { Decimal l1 = precent_l1 * phi + precent_r1 * cphi; Decimal r1 = precent_l1 * cphi + precent_r1 * phi; if (findfoot1(l1) > findfoot1(r1)) precent_l1 = l1; else precent_r1 = r1; } ans = min(ans, findfoot1(precent_l1)); } } std::cout << ans << '\n'; }
failed
113_1_wrong.cpp
// This is a wrong code file #include <bits/stdc++.h> using namespace std; struct node { string s; char str; } s[1010]; bool cmp(node a, node b) { return a.str < b.str; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { getchar(); getline(cin, s[i].s); } for (int i = 0; i < t; i++) { for (int j = 0; j < s[i].s.size(); j++) { if (s[i].s[j] >= 'A' && s[i].s[j] <= 'Z') { s[i].str = s[i].s[j]; break; } } } sort(s, s + t, cmp); // for (int i = 0; i < t; i++) // { // cout<<s[i]<<endl;; // } for (int i = 0; i < t; i++) { cout << s[i].s << endl; } return 0; }
failed
57_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; constexpr int N = 2e5 + 5, M = 4e7; constexpr int INF = 0x3f3f3f3f; int n, m, tot = 1, lnk[N], cur[N], ter[M], nxt[M], cap[M], cost[M], dis[N], ret; bool vis[N]; int cnt = 2; void add(int u, int v, int w, int c) { ter[++tot] = v, nxt[tot] = lnk[u], lnk[u] = tot, cap[tot] = w, cost[tot] = c; } void addedge(int u, int v, int w, int c) { add(u, v, w, c), add(v, u, 0, -c); } bool spfa(int s, int t) { memset(dis, 0x3f, sizeof(dis)); memcpy(cur, lnk, sizeof(lnk)); std::queue<int> q; q.push(s), dis[s] = 0, vis[s] = true; while (!q.empty()) { int u = q.front(); q.pop(), vis[u] = false; for (int i = lnk[u]; i; i = nxt[i]) { int v = ter[i]; if (cap[i] && dis[v] > dis[u] + cost[i]) { dis[v] = dis[u] + cost[i]; if (!vis[v]) q.push(v), vis[v] = true; } } } return dis[t] != INF; } int dfs(int u, int t, int flow) { if (u == t) return flow; vis[u] = true; int ans = 0; for (int &i = cur[u]; i && ans < flow; i = nxt[i]) { int v = ter[i]; if (!vis[v] && cap[i] && dis[v] == dis[u] + cost[i]) { int x = dfs(v, t, std::min(cap[i], flow - ans)); if (x) ret += x * cost[i], cap[i] -= x, cap[i ^ 1] += x, ans += x; } } vis[u] = false; return ans; } void mcmf(int s, int t) { int ans = 0; while (spfa(s, t)) { int x; while ((x = dfs(s, t, INF))) ans += x; } } typedef long long LL; void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; struct S { int id; int v; vector<int> fac; }; unordered_map<int, int> to; vector<S> nums; int cnt = 0; set<int> st; for (int i = 0; i < n; i++) st.insert(a[i]); vector<int> f; vector<int> p, q; auto get = [&](auto &&self, int u, int v) -> void { if (u == p.size()) { f.push_back(v); return; } int ne = v; for (int i = 0; i <= q[u]; i++) { self(self, u + 1, ne); ne *= p[u]; } }; while (st.size()) { int t = *st.rbegin(); st.erase(t); if (to.count(t)) continue; if (t == 1) { to[t] = cnt; nums.push_back({cnt++, 1, {}}); } else { p.clear(), q.clear(); int v = t; for (int i = 2; i <= v / i; i++) if (v % i == 0) { int c = 0; while (v % i == 0) v /= i, c++; p.push_back(i); q.push_back(c); } if (v) p.push_back(v), q.push_back(1); f.clear(); get(get, 0, 1); f.pop_back(); to[t] = cnt; nums.push_back({cnt++, t, f}); for (auto c : f) if (c != t) st.insert(c); } } int m = nums.size(); int S = m, T = m + 1; // MinCostFlow<int> g(m + 2); for (int i = 0; i < n; i++) addedge(S, to[a[i]], 1, 0); for (auto [id, v, vec] : nums) { addedge(id, T, 1, 0); // cout << v << '\n'; // for(auto c : vec) cout << c << ' '; // cout << '\n'; for (auto fac : vec) if (fac != v) addedge(id, to[fac], 1e9, -1); } mcmf(S, T); cout << -ret << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t = 1; // cin >> t; while (t--) solve(); return 0; }
failed
10_3_wrong.cpp
#include <bits/stdc++.h> #define int long long using namespace std; typedef long long ll; typedef pair<int, int> pii; const int mod = 1e9 + 7; const int N = 1e6 + 7; // int a[N]; void solve() { int a, b, c; cin >> a >> b >> c; vector<int> v; for (int i = 1; i <= a; i++) v.push_back(a); for (int i = a + 1; i <= b; i++) v.push_back(b); for (int i = b + 1; i <= c; i++) v.push_back(c); for (int i = c + 1; i <= 100; i++) v.push_back(c + 1); cout << v.size() << "\n"; for (int x : v) cout << x << " "; cout << "\n"; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) solve(); return 0; }
failed
11_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long using pii = pair<int, int>; #define fi first #define se second #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() using ll = long long; typedef vector<int> vi; #define pb push_back int N, K; enum Type { LIT, NOT, AND, OR, EQ, XOR }; struct Expr { Type type; char lit; Expr *left; Expr *right; Expr(Type type, char lit, Expr *left, Expr *right) : type(type), lit(lit), left(left), right(right) {} bool eval(bool x, bool y) { if (type == LIT) { if (lit == '0') return 0; if (lit == '1') return 1; if (lit == 'x') return x; if (lit == 'y') return y; assert(0); } if (type == NOT) { return !left->eval(x, y); } if (type == AND) { return left->eval(x, y) && right->eval(x, y); } if (type == OR) { return left->eval(x, y) || right->eval(x, y); } if (type == EQ) { return left->eval(x, y) == right->eval(x, y); } if (type == XOR) { return left->eval(x, y) != right->eval(x, y); } assert(0); } }; stringstream ss; Expr *parse_xor(); Expr *parse_atom() { char c = ss.peek(); if (c == '(') { ss >> c; // cerr << "openparen: " << c << '\n'; Expr *ret = parse_xor(); ss >> c; // cerr << "closeparen: " << c << '\n'; return ret; } else if (c == '!') { ss >> c; Expr *ret = parse_xor(); return new Expr(NOT, '.', ret, nullptr); } else if (c == 'x' || c == 'y' || c == '1' || c == '0') { ss >> c; return new Expr(LIT, c, nullptr, nullptr); } else { assert(0); // return parse(); } } Expr *parse_eq() { Expr *left = parse_atom(); if (ss.eof()) return left; while (ss.peek() == '=') { char c; ss >> c; Expr *right = parse_atom(); left = new Expr(EQ, '.', left, right); } return left; } Expr *parse_and() { Expr *left = parse_eq(); if (ss.eof()) return left; while (ss.peek() == '&') { char c; ss >> c; Expr *right = parse_eq(); left = new Expr(AND, '.', left, right); } return left; } Expr *parse_or() { Expr *left = parse_and(); if (ss.eof()) return left; while (ss.peek() == '|') { char c; ss >> c; Expr *right = parse_and(); left = new Expr(OR, '.', left, right); } return left; } Expr *parse_xor() { Expr *left = parse_or(); if (ss.eof()) return left; while (ss.peek() == '^') { char c; ss >> c; Expr *right = parse_or(); left = new Expr(XOR, '.', left, right); } return left; } Expr *parse() { Expr *ret = parse_xor(); if (ss.eof()) return ret; string rem; ss >> rem; // cerr <<"remaining: " << rem << '\n'; assert(0); } struct Table { int a, b, r; int res[2][2]; }; Table make_table(Expr *expr) { Table ret; for (int x = 0; x < 2; ++x) { for (int y = 0; y < 2; ++y) { ret.res[x][y] = expr->eval(x, y); // cerr << x << ' ' << y << ' ' << ret.res[x][y] << '\n'; } } return ret; } /* 3 2 1 1 (x=0)&(y=1) 1 1 1 (x=1)&(y=(x^x)) 0 2 2 (x=1)|(y=0) 0 1 4 3 2 1 x=0&(y=1) 1 1 2 !x^!!y 0 2 3 ((x|1)=y)&1&1 1 3 1 !x&!x&!x 0 1 */ const int MAXN = 2e5 + 5, MAXM = 1030; Table stmt[MAXN]; vector<int> accstmt; signed main() { cin.tie(0)->sync_with_stdio(0); // while (true) { // string s; cin >> s; // ss.clear(); // ss << s; // make_table(parse()); // // cout << "parsing succeeded" << endl; // } cin >> N >> K; int A, B, R; string S; for (int i = 1; i <= N; ++i) { cin >> A >> B >> S >> R; ss.clear(); ss << S; Expr *expr = parse(); Table table = make_table(expr); table.a = A; table.b = B; table.r = R; stmt[i] = table; } cin >> R; { vector vis(K + 1, vector(K + 1, vector(2, vector<bool>(2, 0)))); for (int i = 1; i <= N; ++i) { int a = stmt[i].a, b = stmt[i].b; bool flag = 0; for (int ti = 0; ti < 2; ++ti) { for (int tj = 0; tj < 2; ++tj) { if (!stmt[i].res[ti][tj]) continue; if (!vis[a][b][ti][tj]) { vis[a][b][ti][tj] = 1; flag = 1; } } } if (flag) { accstmt.pb(i); } } } int up = 1 << K; { // reflexive int ans = 0; for (int i = 0; i < up; ++i) { // i = x bool flag = 0; bool term = 0; for (int j : accstmt) { auto &cur = stmt[j]; int abt = (i >> (cur.a - 1) & 1), bbt = (i >> (cur.b - 1) & 1); if (cur.res[abt][bbt]) { if (cur.r) { flag = 1; } term = 1; break; } } if (!term) if (R) flag = 1; ans += flag; } cout << ans << " "; } vector f(up + 1, vector<bool>(up + 1, 0)); { // symmetric int ans = 0; for (int x = 0; x < up; ++x) { vector<int> y(K + 1, 3); // y[i] is the set of possible states at y (y[i] // = 0: neither; y[i] = 1: must be 0; y[i] = 2: // must be 1; y[i] = 3: can be either) auto mark = [&](auto &self, vector<int> &ty, int val, int bit) { if (bit == K) { f[x][val] = 1; return; } if (ty[bit] >> 0 & 1) { self(self, ty, val, bit + 1); } if (ty[bit] >> 1 & 1) { self(self, ty, val | (1 << bit), bit + 1); } }; bool term = 0; for (int i : accstmt) { auto &cur = stmt[i]; int a = cur.a, b = cur.b; --a; --b; int abt = (x >> a & 1); if (cur.r) { // auto ty = y; vector<int> ty(y); int allowed = 0; for (int j = 0; j < 2; ++j) { if (cur.res[abt][j]) { // we allow j allowed |= 1 << j; } } ty[b] &= allowed; if (allowed) mark(mark, ty, 0, 0); } for (int j = 0; j < 2; ++j) { if (cur.res[abt][j]) { y[b] &= (3 ^ (1 << j)); // we don't allow j } } if (!y[b]) { term = 1; break; } } if (!term) { if (R) mark(mark, y, 0, 0); } } for (int x = 0; x < up; ++x) { for (int y = 0; y < up; ++y) { if (f[x][y] && f[y][x]) ++ans; } } cout << ans << " "; } { // transitive vector<bitset<1030>> L(up + 1), R(up + 1); for (int i = 0; i < up; ++i) { for (int j = 0; j < up; ++j) { R[i][j] = f[i][j]; L[j][i] = f[i][j]; } } int ans = 0; for (int x = 0; x < up; ++x) { for (int z = 0; z < up; ++z) { if (f[x][z]) continue; ans += (R[x] & L[z]).count(); } } cout << ans << "\n"; } }
failed
101_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define ll long long #define int long long #define ull unsigned long long #define ls (i << 1) #define rs (i << 1 | 1) #define inf (1ll << 60) typedef long long i64; typedef unsigned long long u64; typedef __int128 i128; extern const int mod; struct String { long long n; string a; vector<long long> ne; String(string &s) : n(s.size()), ne(s.size()), a(s) { for (int i = 1; i < n; i++) { int j = ne[i - 1]; while (j && s[j] != s[i]) j = ne[j - 1]; if (s[j] == s[i]) ne[i] = j + 1; } } bool isPalindromic(int i, int j) { while (i < j) if (a[i++] != a[j--]) return false; return true; } // 最小表达法 int min_show() { int i = 0, j = 1, k = 0; while (i < n && j < n && k < n) { if (a[(i + k) % n] == a[(j + k) % n]) k++; else { if (a[(i + k) % n] > a[(j + k) % n]) i += k + 1; else j += k + 1; k = 0; } if (i == j) j++; } return min(i, j); } }; struct BIT { int n; vector<int> d, d2; BIT(int n) : d((n << 1) + 1, 0), d2((n << 1) + 1, 0), n(n << 1){}; int lowbit(int x) { return x & -x; } void add(int x, int k) { for (int i = x; i <= n; i += lowbit(i)) d[i] += k, d2[i] += x * k; } int query(int x) { int ans = 0; for (int i = x; i; i -= lowbit(i)) ans += d[x]; return ans; } int query_range(int x) { int ans = 0; for (int i = x; i; i -= lowbit(i)) ans += d[i] * (x + 1) - d2[i]; return ans; } void add(int l, int r, int k) { add(l, k), add(r + 1, -k); } int query(int l, int r) { return query_range(r) - query_range(l - 1); } }; int qpow(int a, int b) { int res = 1; do if (b & 1) res = res * a % mod; while (a = a * a % mod, b >>= 1); return res; } template <int P> struct MInt { int x; constexpr MInt() : x{} {} constexpr MInt(i64 x) : x{norm(x % getMod())} {} static int Mod; constexpr static int getMod() { if (P > 0) { return P; } else { return Mod; } } constexpr static void setMod(int Mod_) { Mod = Mod_; } constexpr int norm(int x) const { if (x < 0) { x += getMod(); } if (x >= getMod()) { x -= getMod(); } return x; } constexpr int val() const { return x; } explicit constexpr operator int() const { return x; } constexpr MInt operator-() const { MInt res; res.x = norm(getMod() - x); return res; } constexpr MInt inv() const { assert(x != 0); return power(*this, getMod() - 2); } constexpr MInt &operator*=(MInt rhs) & { x = 1LL * x * rhs.x % getMod(); return *this; } constexpr MInt &operator+=(MInt rhs) & { x = norm(x + rhs.x); return *this; } constexpr MInt &operator-=(MInt rhs) & { x = norm(x - rhs.x); return *this; } constexpr MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); } friend constexpr MInt operator*(MInt lhs, MInt rhs) { MInt res = lhs; res *= rhs; return res; } friend constexpr MInt operator+(MInt lhs, MInt rhs) { MInt res = lhs; res += rhs; return res; } friend constexpr MInt operator-(MInt lhs, MInt rhs) { MInt res = lhs; res -= rhs; return res; } friend constexpr MInt operator/(MInt lhs, MInt rhs) { MInt res = lhs; res /= rhs; return res; } friend constexpr std::istream &operator>>(std::istream &is, MInt &a) { i64 v; is >> v; a = MInt(v); return is; } friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) { return os << a.val(); } friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); } friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); } }; template <> int MInt<0>::Mod = 998244353; template <int P> int MInt<P>::Mod = P; template <int V, int P> constexpr MInt<P> CInv = MInt<P>(V).inv(); char Getch() { char ch = getchar(); while ((!isalpha(ch)) && (!isdigit(ch))) ch = getchar(); return ch; } const int N = 6e5 + 7, mod = 998244353; using Z = MInt<mod>; mt19937_64 rng(time(nullptr)); void init() {} double cal(int x1, int y1, int x2, int y2) { return (x1 * y2 - x2 * y1) * 1.0 / 2; } void solve() { int n; cin >> n; vector<int> xt(n + 2), yt(n + 2); int xp = 0, yp = 0; for (int i = 1; i <= n; i++) { cin >> xt[i] >> yt[i]; xp += xt[i]; yp += yt[i]; } if (n == 3) { cout << "1.0" << "\n"; return; } double tmp = 0; for (int i = 3; i <= n; i++) { tmp += cal(xt[i - 1] - xt[1], yt[i - 1] - yt[1], xt[i] - xt[1], yt[i] - yt[1]); } double ans = 0; xt[0] = xt[n], yt[0] = yt[n]; xt[n + 1] = xt[1], yt[n + 1] = yt[1]; for (int i = 1; i <= n; i++) { ans += ((xt[i] - xt[i - 1]) * (yp - n * yt[i - 1]) - (yt[i] - yt[i - 1]) * (xp - n * xt[i - 1])) * 1.0 / 2; ans -= cal(xt[i] - xt[i - 1], yt[i] - yt[i - 1], xt[i + 1] - xt[i], yt[i + 1] - yt[i]); } ans /= tmp; // cout<<tmp<<"\n"; cout << fixed << setprecision(8) << ans << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); init(); ll t = 1; // cin >> t; while (t--) solve(); return 0; }
failed
67_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define SZ(x) ((int)(x).size()) #define pb push_back #define F first #define S second const int maxn = 2e5 + 100; int n, k; int t[maxn], a[maxn]; ll d[maxn]; int dd[maxn]; int opt[maxn]; vector<int> g[maxn]; vector<int> vec; vector<int> vc[maxn]; int cnt[maxn]; int fs[maxn]; vector<int> cc[maxn]; bool vis[maxn], mark[maxn], inc[maxn]; priority_queue<pair<ll, int>> pq; ll ans; void dfs(int v, int lst = -1, int o = 0) { // cout << "$ " << v << "\n" << flush; mark[v] = 1; if (lst == -1) { while (d[pq.top().S] != -pq.top().F) pq.pop(); // cout << "$ " << v << " : " << pq.top().S << "\n"; ans += 1ll * v * pq.top().S; } else ans += 1ll * v * lst; //, cout << "$$ " << v << " " << lst << "\n"; for (int u : g[v]) { if (mark[u]) continue; int c = t[u]; // cout << "? " << c << " " << opt[c] << " " << SZ(vc[c]) << "\n"; if (!inc[u]) { cc[c].pb(o); cnt[c]++; } if (vc[c].empty() || (!inc[u] && cnt[c] >= k)) { int plst = lst; if (cnt[c] >= k && (lst == -1 || fs[lst] < cc[c][cnt[c] - k])) { plst = c; fs[c] = cc[c][cnt[c] - k]; } dfs(u, plst, o + (!inc[u])); cnt[c]--; cc[c].pop_back(); continue; } // cout << " ^ " << d[2] << " " << d[3] << "\n"; d[c] -= dd[vc[c][opt[c]]]; opt[c]--; if (opt[c] < 0) opt[c] += SZ(vc[c]); pq.push({-d[c], c}); dfs(u, lst, o + (!inc[u])); assert(!(vc[c].empty() || (!inc[u] && cnt[c] >= k))); opt[c]++; if (opt[c] >= SZ(vc[c])) opt[c] = 0; d[c] += dd[vc[c][opt[c]]]; pq.push({-d[c], c}); if (!inc[u]) { cc[c].pop_back(); cnt[c]--; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); int tt; cin >> tt; while (tt--) { cin >> n >> k; rep(i, 1, n + 1) cin >> t[i], g[i].clear(), vis[i] = mark[i] = inc[i] = 0; rep(i, 1, n + 1) cin >> a[i], g[a[i]].pb(i); ans = 0; rep(o, 1, n + 1) { if (mark[o]) continue; int v = o; while (!vis[v]) { vis[v] = 1; v = a[v]; } int u = v; for (int i : vec) vc[i].clear(); vec.clear(); int m = 0; do { int c = t[u]; if (vc[c].empty()) vec.pb(c); vc[c].pb(m); m++; inc[u] = 1; u = a[u]; } while (u != v); while (!pq.empty()) pq.pop(); for (int i : vec) { opt[i] = (k - 1) % SZ(vc[i]); d[i] = 1ll * (k - 1) / SZ(vc[i]) * m + vc[i][opt[i]]; pq.push({-d[i], i}); rep(j, 1, SZ(vc[i])) { dd[vc[i][j]] = vc[i][j] - vc[i][j - 1]; } dd[vc[i][0]] = m + vc[i][0] - vc[i].back(); } // cout << "# " << v << "\n" << flush; dfs(v); // cout << "done\n" << flush; } cout << ans << "\n"; } return 0; }
failed
29_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int M = 1e6 + 10; int n, k; string s[N]; int tr[M][2]; int cnt; vector<int> vec[M]; inline void ins(string s, int id) { int len = s.length(), now = 0; for (int i = 0; i < len; ++i) { if (!tr[now][s[i] - '0']) tr[now][s[i] - '0'] = ++cnt; now = tr[now][s[i] - '0']; } vec[now].push_back(id); } int p[N]; inline int calc(string s) { int len = s.length(), now = 0; for (int i = 0; i < len; ++i) { if (!tr[now][s[i] - '0']) return 0; now = tr[now][s[i] - '0']; } if (!vec[now].size()) return 0; int x = vec[now].back(); vec[now].pop_back(); return x; } signed main() { #ifndef ONLINE_JUDGE freopen("test.in", "r", stdin); freopen("test.out", "w", stdout); #endif ios ::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i <= k; ++i) cin >> s[i]; for (int i = 0; i < n; ++i) { string str = ""; for (int j = 1; j < k; ++j) str += s[j][i]; // cout << i + 1 << " " << str << endl; ins(str, i + 1); } int flag = 1; for (int i = 0; i < n; ++i) { string str = ""; for (int j = 2; j <= k; ++j) str += s[j][i]; int tmp = calc(str); // cout << "??? " << i << " " << tmp << endl; if (tmp > 0) p[tmp] = i + 1; else flag = 0; } if (!flag) cout << "NO" << endl; else { cout << "YES" << endl; for (int i = 1; i <= n; ++i) cout << p[i] << " "; cout << endl; } // cout << "????" << endl; return 0; }
failed
97_3_wrong.cpp
#include <algorithm> #include <iostream> #include <map> #include <vector> using namespace std; struct Player { int id; long long points; Player(int i = 0, long long p = 0) : id(i), points(p) {} }; bool compare(const Player &a, const Player &b) { if (a.points != b.points) return a.points > b.points; return a.id < b.id; } void solve() { int n, m, q; cin >> n >> m >> q; // Track completions for each level vector<vector<int>> levelCompletions(n + 1); // Track if player has already completed or given up on a level vector<vector<bool>> processed(n + 1, vector<bool>(m + 1, false)); int currentLevel = 0; for (int i = 0; i < q; i++) { int type; cin >> type; if (type == 1) { int x; cin >> x; currentLevel = x; } else if (currentLevel > 0) { int id, x; cin >> id >> x; // Only process messages for current level if (x != currentLevel) continue; // Skip if player already processed for this level if (processed[x][id]) continue; processed[x][id] = true; if (type == 2) { // Completion levelCompletions[x].push_back(id); } } } // Calculate final points vector<long long> points(m + 1, 0); for (int level = 1; level <= n; level++) { int rank = m; for (int pid : levelCompletions[level]) { points[pid] += rank--; } } // Prepare final standings vector<Player> standings; for (int i = 1; i <= m; i++) { standings.push_back(Player(i, points[i])); } // Sort results sort(standings.begin(), standings.end(), compare); // Output results for (const auto &player : standings) { cout << player.id << " " << player.points << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { solve(); } return 0; }
failed
9_2_wrong.cpp
#pragma GCC optimize("Ofast,unroll-loops") // #pragma GCC target("avx,avx2,tune=native") #include <algorithm> #include <array> #include <bitset> #include <cassert> #include <cmath> #include <cstdlib> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <stack> #include <unordered_map> #include <unordered_set> #include <valarray> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long unll; typedef unsigned int unii; #define fi first #define se second #define double long double #define int long long typedef pair<int, int> pll; const int modulo = 998244353; const int mod1 = 1e9 + 7; const int mod2 = 1e9 + 123; const int N = 2e5 + 5; const int INF = 1e18; const int MXN = N - 5; void solve() { int n, s0, s1; cin >> n >> s0 >> s1; vector<int> l0(n), h0(n), l1(n), h1(n); for (int i = 0; i < n; i++) { cin >> l0[i] >> h0[i] >> l1[i] >> h1[i]; } vector<vector<int>> dp(n + 1, vector<int>(n + 1)); for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) dp[i + 1][j] = dp[i][j]; for (int j = 0; j <= i; j++) { int v0 = s0 + j; int v1 = s1 + i - j; if (l0[i] <= v0 && v0 <= h0[i] && l1[i] <= v1 && v1 <= h1[i]) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + 1); dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1); } } } int ans = 0; for (int j = 0; j <= n; j++) ans = max(ans, dp[n][j]); cout << ans << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); srand((signed)time(0)); int t = 1; // cin >> t; while (t--) solve(); return 0; }
failed
110_2_wrong.cpp
#include <bits/stdc++.h> #define int ll using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define rng(i, c, n) for (int i = c; i < n; i++) #define fi first #define se second #define pb push_back #define sz(a) (int)a.size() #define all(a) a.begin(), a.end() #define vec(...) vector<__VA_ARGS__> typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; void print() { cout << '\n'; } template <class h, class... t> void print(const h &v, const t &...u) { cout << v << ' ', print(u...); } typedef __int128 Int; const int lim = 4e18; template <typename T> T floor(T a, T b) { return a / b - (a % b && (a ^ b) < 0); } template <typename T> T ceil(T x, T y) { return floor(x + y - 1, y); } void slv() { int n; cin >> n; vi a(n); rep(i, n) { cin >> a[i]; } vi b(n); rep(i, n) { cin >> b[i]; } vi c(n); rep(i, n) { cin >> c[i]; } sort(all(c)); auto check = [&](int med) { int ret = 0; vi pref(n), suf(n); rep(i, n) { // a[i] * x + b[i] >= med // a[i] * x >= med - b[i] if (a[i] < 0) { // c[i] <= x int l = 0, r = n - 1; int opt = -1; while (l <= r) { int m = (l + r) / 2; if (a[i] * c[m] + b[i] >= med) { opt = m, l = m + 1; } else { r = m - 1; } } // print(i,opt,a[]); if (opt != -1) pref[opt] += 1; } else if (a[i] > 0) { int l = 0, r = n - 1; int opt = -1; while (l <= r) { int m = (l + r) / 2; if (a[i] * c[m] + b[i] >= med) { opt = m, r = m - 1; } else { l = m + 1; } } // print(i,opt); if (opt != -1) suf[opt] += 1; } else { if (b[i] >= med) { ret += 1; } } } per(i, n - 1) { pref[i] += pref[i + 1]; } int dtl = 0, dtr = 0; rep(i, n) { if (i) { suf[i] += suf[i - 1]; } if (pref[i] - dtl > 0) { dtl += 1; ret += 1; } else if (suf[i] - dtr > 0) { dtr += 1; ret += 1; } } return ret >= (n + 1) / 2; }; // print(check(14)); int l = -lim, r = lim; int opt = l; while (l <= r) { int m = (l + r) / 2; if (check(m)) { opt = m, l = m + 1; } else { r = m - 1; } } cout << opt << "\n"; } signed main() { ios::sync_with_stdio(0), cin.tie(0); int t; cin >> t; rep(cs, t) { slv(); } }
failed
72_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f; } const int N = 2055; int n, w[N], m, dis[N], tot, tag[N], cnt[15], dep[N]; struct Edge { int u, v, lca; } a[N * N]; void dfs(int u) { int ls = u << 1, rs = u << 1 | 1; if (ls <= m) { dis[ls] = dis[u] + w[ls]; dep[ls] = dep[u] + 1; dfs(ls); } if (rs <= m) { dis[rs] = dis[u] + w[rs]; dep[rs] = dep[u] + 1; dfs(rs); } } int main() { int T = read(); while (T--) { n = read(); m = (1 << (n + 1)) - 1; for (int i = 2; i <= m; ++i) w[i] = read(); dfs(1); tot = 0; int L = 1 << n, R = (1 << (n + 1)) - 1; for (int i = L, ca = 0; i <= R; ++i, ca = 0) for (int j = i + 1; j <= R; ++j) { while ((i >> ca) != (j >> ca)) ++ca; if (dis[i] == dis[j]) a[++tot] = {i, j, (i >> ca)}; } sort(a + 1, a + tot + 1, [](Edge p, Edge q) { return p.lca > q.lca; }); int ans = 114514; for (int S = 0; S < (1 << n); ++S) { int j = 0, T = 1; for (int i = 1; i <= tot; ++i) tag[i] = 0; memset(cnt, 0, sizeof(cnt)); while (T <= tot && j < n) { int u = a[T].lca; ++cnt[dep[u] + 1]; int Z = (S >> j & 1) ? (u << 1) : (u << 1 | 1); tag[T] = 1; for (int k = T + 1; k <= tot; ++k) if (!tag[k]) for (int l = 1; l <= m; l <<= 1) if (a[k].u / l == Z || a[k].v / l == Z) { tag[k] = 1; break; } while (tag[T]) ++T; ++j; } int flag = 1; if (T <= tot) flag = 0; for (int i = n; i >= 1; --i) { cnt[i] += cnt[i + 1]; if (cnt[i] > n - i + 1) flag = 0; } if (flag) ans = min(ans, j); } if (ans == 114514) puts("-1"); else printf("%d\n", ans); } return 0; }
failed
86_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; void update(pair<int, long long> &dp, pair<int, long long> &val) { if (val.first < dp.first) { dp = val; } else if (val.first == dp.first) { dp.second = (dp.second + val.second) % MOD; } } void solve() { int n, m; cin >> n >> m; vector<pair<int, int>> enemies(n); for (int i = 0; i < n; i++) { cin >> enemies[i].second; } int max_b = 0; for (int i = 0; i < n; i++) { cin >> enemies[i].first; max_b = max(max_b, enemies[i].first); } sort(enemies.begin(), enemies.end()); int k; cin >> k; vector<int> c(k + 1); for (int i = 1; i <= k; i++) { cin >> c[i]; } int MX = 3 * (m + k); vector<int> min_health(MX + 1); int ptr = 0; int max_ab = 0; for (int i = 0; i <= MX; i++) { while (ptr < n && enemies[ptr].first <= i) { max_ab = max(max_ab, enemies[ptr].first + enemies[ptr].second); ptr++; } min_health[i] = max(max_ab - i, 0); } // dp[i][j] = min cost to do i total damage, max health of broken armor j vector<vector<pair<int, long long>>> dp( MX + 1, vector<pair<int, long long>>(k + 1, {INT_MAX, 0})); dp[0][0] = {0, 1}; ptr = 0; for (int i = 0; i < MX; i++) { while (ptr < n && enemies[ptr].first <= i) ptr++; vector<int> max_lefts(k + 1, 0); int ptr2 = ptr; for (int j = 1; j <= k; j++) { max_lefts[j] = max_lefts[j - 1]; while (ptr2 < n && enemies[ptr2].first <= i + j) { max_lefts[j] = max(max_lefts[j], enemies[ptr2].second); ptr2++; } } for (int j = 0; j <= k; j++) { if (dp[i][j].first < INT_MAX) { for (int x = 1; x <= k && i + x <= MX; x++) { int max_health = max(j + min_health[i] - x, max_lefts[x]); assert(max_health >= min_health[i + x] && max_health <= min_health[i + x] + k); pair<int, long long> nxt = dp[i][j]; nxt.first += c[x]; update(dp[i + x][max_health - min_health[i + x]], nxt); } } } } pair<int, long long> res = {INT_MAX, 0}; for (int i = max_b; i <= MX; i++) { if (dp[i][0].first < INT_MAX && min_health[i] == 0) { update(res, dp[i][0]); } } cout << res.first << " " << res.second << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } }
failed
35_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> using min_heap = priority_queue<T, vector<T>, greater<>>; constexpr ll inf = (1ll << 60) - 1; int main() { cin.tie(nullptr)->sync_with_stdio(false); int N, M, K; cin >> N >> M >> K; vector<vector<pair<int, ll>>> graph(N); vector<double> P(N); vector<int> is1(N); for (int i = 0; i < M; ++i) { int u, v, l; cin >> u >> v >> l; u -= 1, v -= 1; graph[u].emplace_back(v, l); graph[v].emplace_back(u, l); } vector<int> cand; for (int i = 0; i < K; ++i) { int u; cin >> u; u -= 1; string s; cin >> s; if (s[0] == '1') { is1[u] = 1; } P[u] = stod(s); cand.push_back(u); } if (accumulate(begin(is1), end(is1), 0) == 0) { cout << "impossible\n"; return 0; } const auto dijkstra = [&](int src) { vector<ll> dist(N, inf); min_heap<pair<ll, int>> heap; const auto push = [&](int u, ll d) { if (dist[u] > d) { dist[u] = d; heap.emplace(d, u); } }; push(src, 0); while (!heap.empty()) { const auto [d, u] = heap.top(); heap.pop(); if (dist[u] > d) continue; for (const auto &[v, c] : graph[u]) { push(v, d + c); } } return dist; }; const auto D1 = dijkstra(0); const auto DN = dijkstra(N - 1); ranges::sort(cand, {}, [&](int u) { return D1[u] + DN[u]; }); auto dfs = [&](auto &&rc, int idx) -> double { const int u = cand[idx]; if (is1[u]) return D1[u] + DN[u]; return P[u] * (D1[u] + DN[u]) + (1.0 - P[u]) * rc(rc, idx + 1); }; cout << fixed << setprecision(20); cout << dfs(dfs, 0) << "\n"; return 0; }
failed
65_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using LL = long long; using pii = pair<int, int>; #define sz(x) (signed)size(x) vector<int> kmp(vector<int> &s) { int n = sz(s); vector<int> pi(n); for (int i = 1, j = 0; i < n; ++i) { while (j && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) ++j; pi[i] = j; } return pi; } void solve() { int n, m; cin >> n >> m; vector<int> a(n), b(m); for (int &i : a) cin >> i; for (int &i : b) cin >> i; vector<int> c = a; for (int i = 0; i < n; ++i) c.push_back(a[i]); c.push_back(-1); for (int i = 0; i < m; ++i) c.push_back(b[i]); bool f = 0; vector<int> pi = kmp(c); f |= *max_element(pi.begin(), pi.end()) == m; reverse(a.begin(), a.end()); c.clear(); for (int t = 0; t <= 1; ++t) for (int i = 0; i < n; ++i) c.push_back(a[i]); c.push_back(-1); for (int i = 0; i < m; ++i) c.push_back(b[i]); f |= *max_element(pi.begin(), pi.end()) == m; cout << f << '\n'; } signed main() { cin.tie(0)->sync_with_stdio(0); solve(); }
failed
102_3_wrong.cpp
#include <iostream> #include <vector> using namespace std; string solve(long long l, long long r) { long long len = r - l + 1; // For small ranges, we can simulate the game if (len <= 1000) { vector<bool> used(len, false); long long x = l; bool aliceTurn = true; while (true) { // Find smallest available multiple of x bool found = false; for (long long i = 0; i < len; i++) { long long num = l + i; if (!used[i] && num >= x && num % x == 0) { used[i] = true; found = true; break; } } if (!found) { return aliceTurn ? "Bob" : "Alice"; } aliceTurn = !aliceTurn; x++; } } // For large ranges, we can use mathematical solution // If x is larger than r/2, it can only have one multiple in range // So we can calculate the result based on this property long long critical = r / 2; if (l > critical) { // Each number can only be used once // Each x can only have one multiple return (len % 2 == 1) ? "Alice" : "Bob"; } else { // For numbers where x ≤ r/2 // Calculate result based on pattern if (r - l + 1 >= 3) return "Alice"; if (r - l + 1 == 2) { if (l % 2 == 0) return "Alice"; return "Bob"; } // Single number case return "Alice"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) { long long l, r; cin >> l >> r; cout << solve(l, r) << "\n"; } return 0; }
failed
68_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; #define rep(i, a, b) for (int i = a; i < (b); ++i) #define SZ(x) ((int)(x).size()) #define pb push_back const int xn = 1e6 + 10; const ll INF = 1e18 + 10; int n, m, k; ll a[xn], sum; vi adj[xn], jads; bool flag; void DFS(int v) { // cerr << "in " << v << ' ' << k << ' ' << flag << '\n'; jads.pb(v); if ((SZ(jads) < k + 1 && v >= m) || (SZ(jads) >= k + 1 && v - jads[SZ(jads) - (k + 1)] >= m)) { // cerr << "^^^ "; for(auto j : jads) cerr << j << ' '; cerr << '\n'; flag = true; } for (int u : adj[v]) { DFS(u); } jads.pop_back(); // cout << "out " << v << '\n'; } bool check(ll d) { vl X = {}; ll ps = 0; rep(i, 1, n + 1) { ll x = a[i]; ll y = sum - a[i]; if (min(x, y) > d) return 0; if (max(x, y) > d) { if (x <= y) X.pb(ps + a[i] / 2); else X.pb((ps + a[i] + y / 2) % sum); } // if (max(a[i], sum - a[i]) > d) // X.pb(ps + a[i] / 2); ps += a[i]; } m = SZ(X); sort(X.begin(), X.end()); rep(i, 0, m) X.pb(X[i] + sum); // m += m; rep(i, 0, m + m) { adj[i] = {}; // cout << i << " : " << X[i] << "\n"; } int ind = -1; rep(i, 0, m) { if (X[i] - X[0] > d) { ind = i; break; } } if (ind == -1) return 1; // cerr << "!! " << d << '\n'; // rep(i, 0, m + m) cerr << X[i] << ' '; // cerr << '\n'; int ptr = 0; rep(i, ind, m + m) { while (ptr < i && X[i] - X[ptr + 1] > d) ptr++; adj[ptr].pb(i); // cerr << "$$$$ " << ptr << ' ' << i << '\n'; } flag = false; rep(i, 0, ind) { jads = {}; DFS(i); if (flag) return 1; } return 0; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; rep(i, 1, n + 1) { cin >> a[i]; a[i] *= 2; sum += a[i]; } // rep(i, n + 1, n + n + 1) // a[i] = a[n - i]; // cout << "!! " << check(1) << "\n"; // return 0; ll lb = 0, rb = INF; while (1 < rb - lb) { ll md = (lb + rb) / 2; if (check(md)) rb = md; else lb = md; // cerr << "\n\n\n"; } // cerr << check(6) << '\n';; cout << rb / 2 << "\n"; return 0; }
failed
95_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int NM = 2e5, K = 200; typedef long long int lint; typedef complex<int> vect; typedef set<vect, decltype([](vect a, vect b) { return (array<int, 2>){a.real(), a.imag()} < (array<int, 2>){b.real(), b.imag()}; })> setV; int n, m, k; template <typename T, size_t SIZE> struct Multarr { size_t N, M; T data[SIZE]; Multarr() { N = n; M = m; } Multarr(int N, int M) { this->N = N; this->M = M; memset(data, 0, sizeof(data)); } T *operator[](size_t i) { return &data[i * M]; } T &operator[](vect cur) { return data[cur.real() * M + cur.imag()]; } }; char buff[K + 5]; int c1[K], c2[K]; int change[K]; Multarr<char, 2 * NM> grid; Multarr<int, NM> req; Multarr<vect, NM> nex; Multarr<setV, NM> con; void setUpArrs() { grid.N = n; grid.M = m; memset(grid.data, 0, sizeof(grid.data)); req.N = n; req.M = m; memset(req.data, 0, sizeof(req.data)); nex.N = n; nex.M = m; } map<char, vect> dir = {{'U', vect(-1, 0)}, {'D', vect(1, 0)}, {'L', vect(0, -1)}, {'R', vect(0, 1)}}; vect move(vect cur, vect mov) { vect v = cur + mov; v = vect(min(max(v.real(), 0), n - 1), min(max(v.imag(), 0), m - 1)); if (grid[v] != '0') { return v; } return cur; } int ans[NM + 1]; int main() { scanf("%d %d %d", &n, &m, &k); scanf("%s", buff); setUpArrs(); for (int x = 0; x < n; x++) { scanf("%s", grid[x]); } size_t sz = 0; for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) { if (grid[x][y] != '0') { sz++; vect cur(x, y); for (int z = 0; z < k; z++) { cur = move(cur, dir[buff[z]]); } nex[x][y] = cur; // cout << vect(x, y) << " " << cur << //endl; con[cur].insert(vect(x, y)); req[cur]++; } } } vector<vect> rem; for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) { if (grid[x][y] != '0') { vect cur(x, y); if (!req[cur]) rem.push_back(cur); } } } memset(ans, -1, sizeof(ans)); memset(ans + sz, 0, sizeof(ans) - sz * sizeof(int)); int mov = 0; size_t i = sz - 1; { setV inter; for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) { if (req[x][y] >= 2) { inter.insert(vect(x, y)); } } } memset(change, 0, sizeof(change)); setV spect; for (vect v : inter) { for (vect v2 : con[v]) { spect.insert(v2); } } for (int x = 0; x < k; x++) { setV newSpect; for (vect cur : spect) { vect v = move(cur, dir[buff[x]]); newSpect.insert(v); } change[x] = spect.size() - newSpect.size(); swap(spect, newSpect); } } while (!rem.empty()) { // for (int x = 0; x < k; x ++){ // cout << change[x] << " "; // } // cout << endl; for (int x = 0; x < k; x++) { if (change[x] < 0) { exit(-1); } if (sz < (size_t)change[x]) { exit(0); } sz -= change[x]; while (i >= sz) { ans[i--] = mov * k + x + 1; } } setV recalc; vector<vect> nexRem; for (vect cur : rem) { if (req[nex[cur]] >= 2) { recalc.insert(nex[cur]); } } memset(c1, 0, sizeof(c1)); memset(c2, 0, sizeof(c2)); { setV spect; for (vect v : recalc) { for (vect v2 : con[v]) { spect.insert(v2); } } for (int x = 0; x < k; x++) { setV newSpect; for (vect cur : spect) { vect v = move(cur, dir[buff[x]]); newSpect.insert(v); } c1[x] = spect.size() - newSpect.size(); swap(spect, newSpect); } } for (vect cur : rem) { if (req[nex[cur]] == 2) { recalc.erase(nex[cur]); } req[nex[cur]]--; con[nex[cur]].erase(cur); if (!req[nex[cur]]) nexRem.push_back(nex[cur]); } { setV spect; for (vect v : recalc) { for (vect v2 : con[v]) { spect.insert(v2); } } for (int x = 0; x < k; x++) { setV newSpect; for (vect cur : spect) { vect v = move(cur, dir[buff[x]]); newSpect.insert(v); } c2[x] = spect.size() - newSpect.size(); swap(spect, newSpect); } } for (int x = 0; x < k; x++) { change[x] -= c1[x] - c2[x]; } swap(nexRem, rem); mov++; } for (int x = 1; x <= n * m; x++) { printf("%d\n", ans[x]); } }
failed
47_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int N = 3e6 + 10; struct a1 { int l, r; } a[N]; struct a2 { int x, y; } z[N]; int cnt = 0, t[N]; bool a3(a2 a, a2 b) { if (a.x == b.x) return a.y > b.y; return a.x < b.x; } int lowbit(int a) { return a & (-a); } void J(int i, int k) { for (int q = i; q <= 3e6; q += lowbit(q)) t[q] = max(t[q], k); } int G(int i) { int ans = 0; for (int q = i; q; q -= lowbit(q)) ans = max(ans, t[q]); return ans; } int _a[N], dp[N]; int Get_ans() { _a[0] = 0; for (int q = 1; q <= cnt; q++) _a[++_a[0]] = z[q].x, _a[++_a[0]] = z[q].y; sort(_a + 1, _a + 1 + _a[0]); int o = unique(_a + 1, _a + 1 + _a[0]) - _a - 1; for (int q = 0; q <= 3e6; q++) t[q] = 0; for (int q = 1; q <= cnt; q++) { z[q].x = lower_bound(_a + 1, _a + 1 + o, z[q].x) - _a; z[q].y = lower_bound(_a + 1, _a + 1 + o, z[q].y) - _a; } sort(z + 1, z + 1 + cnt, a3); int ans = 0; for (int q = 1; q <= cnt; q++) { if (z[q].y > 1) dp[q] = G(z[q].y - 1) + 1; else dp[q] = 1; J(z[q].y, dp[q]); ans = max(ans, dp[q]); } return ans; } inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - 48; ch = getchar(); } return x * f; } int main() { // freopen("lament.in","r",stdin); // freopen("lament.out","w",stdout); int T; cin >> T; while (T--) { int n; cin >> n; for (int q = 1; q <= n; q++) a[q].l = read(), a[q].r = read(); cnt = 0; for (int q = 1; q <= n; q++) { if (a[q].l != a[q - 1].l) z[++cnt] = {q - 1, a[q].l - 1}; if (a[q].r != a[q + 1].r && q != n) z[++cnt] = {q, a[q].r}; } cout << Get_ans() << "\n"; } return 0; }
failed
19_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7; long long pw[20], inv[4] = {0, 1, (P + 1) / 2, (P + 1) / 3}; long long sum(long long l, long long r) { return (__int128)(l + r) * (r - l + 1) / 2 % P; } void get(long long x, vector<array<long long, 3>> &o, long long &l, long long &r) { l = 0, r = 1e18; for (auto [a, b, c] : o) { if (!c) r = min((__int128)r, (__int128)a * x + b); else l = max((__int128)l, (__int128)a * x + b); } if (l > r) l = 1, r = 0; l = (l + 1) / 2, r = r / 2; } long long POLINT(vector<long long> x, long long y) { long long res = 0; for (int i = 0; i <= 3; ++i) { __int128 mul = x[i] % P; for (int j = 0; j <= 3; ++j) if (i != j) mul = mul * (y - j) % P; for (int j = 0; j <= 3; ++j) if (i != j) mul = mul * (i > j ? inv[i - j] : P - inv[j - i]) % P; res = (res + mul) % P; } return res; } array<long long, 3> calc(long long mnx, long long mxx, vector<array<long long, 3>> o) { vector<long long> z; z.push_back(mnx), z.push_back(mxx + 1); for (auto [a, b, c] : o) { for (auto [d, e, f] : o) { if (e > b && a > d) { z.push_back((e - b) / (a - d)); z.push_back((e - b + a - d - 1) / (a - d)); } } } vector<long long> tz = z; for (auto x : tz) { if (x & 1) z.push_back(x - 1), z.push_back(x + 1); } sort(z.begin(), z.end()); z.erase(unique(z.begin(), z.end()), z.end()); reverse(z.begin(), z.end()); while (z.back() != mnx) z.pop_back(); reverse(z.begin(), z.end()); while (z.back() != mxx + 1) z.pop_back(); array<long long, 3> res = {0, 0, 0}; for (int i = 0; i + 1 < (int)z.size(); ++i) { if (z[i + 1] - z[i] <= 10) { for (long long j = z[i]; j < z[i + 1]; ++j) { long long L, R; get(j, o, L, R); if (L > R) break; res[0] = (res[0] + R - L + 1) % P; res[1] = (res[1] + (R - L + 1) % P * (j % P)) % P; res[2] = (res[2] + sum(L, R)) % P; } continue; } vector<long long> val[3] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; int flg = 1; for (long long j = z[i]; j < z[i] + 8; ++j) { long long L, R; get(j, o, L, R); if (L > R) { flg = 0; break; } val[0][(j - z[i]) / 2] += (R - L + 1) % P; val[1][(j - z[i]) / 2] += (R - L + 1) % P * (j % P) % P; val[2][(j - z[i]) / 2] += sum(L, R); } if (!flg) continue; for (int j = 0; j < 3; ++j) { for (int k = 1; k <= 3; ++k) val[j][k] = (val[j][k] + val[j][k - 1]) % P; res[j] = (res[j] + POLINT(val[j], (z[i + 1] - 1 - z[i]) / 2)) % P; } } return res; } long long calc1(long long L, int a, int b, int v) { vector<array<long long, 3>> o; o.push_back({-2 * pw[b + 1], 2 * (L - v * pw[b]), 0}); o.push_back({20, 2 * v, 0}); o.push_back({2, -2 * v * pw[b] + 1, 1}); o.push_back({11, (1 - pw[b]) * v, 1}); o.push_back({0, 0, 1}); o.push_back({0, 2 * pw[b] - 2, 0}); array<long long, 3> res = calc(pw[a - 1], pw[a] - 1, o); return (res[1] * 10 + v * res[0] - res[2]) % P; } long long calc2(long long L, int a, int b, int v) { vector<array<long long, 3>> o; o.push_back({-2 * pw[b + 1], 2 * (L - v * pw[b]), 0}); o.push_back({20, 2 * v, 0}); o.push_back({2, -2 * v * pw[b] + 1, 1}); o.push_back({11, (1 - pw[b]) * v - 1, 0}); o.push_back({0, 0, 1}); o.push_back({0, 2 * pw[b] - 2, 0}); array<long long, 3> res = calc(pw[a - 1], pw[a] - 1, o); return (v * pw[b] % P * res[0] + res[2] - res[1]) % P; } long long pre[20][20]; long long solve(long long R) { long long len = to_string(R).size(), res = 0; for (int i = 1; i < len; ++i) { for (int j = 1; i + j + 1 < len; ++j) { res += pre[i][j]; } } for (int i = 1; i < len; ++i) { int j = len - i - 1; for (int k = 0; k <= 9; ++k) { res += calc1(R, i, j, k) + calc2(R, i, j, k); } } return res; } signed main() { for (int i = pw[0] = 1; i <= 18; ++i) pw[i] = 10 * pw[i - 1]; for (int i = 1; i <= 18; ++i) { for (int j = 1; i + j + 1 <= 18; ++j) { for (int k = 0; k <= 9; ++k) pre[i][j] += calc1(1e18, i, j, k) + calc2(1e18, i, j, k); } } int T; scanf("%d", &T); for (int te = 1; te <= T; ++te) { long long L, R, v = 1e18, res = 0; scanf("%lld%lld", &L, &R); if (R == v) ++res, --R; for (int i = 1; i <= 9; ++i) { for (int j = 0; j <= i; ++j) { for (int k = 1; k <= 18; ++k) { long long x = i * pw[k] + j; if (x >= L && x <= R) res += i - j; } } } for (int i = 1; i <= 9; ++i) { for (int j = i + 1; j <= 9; ++j) { int x = i * 10 + j; if (x >= L && x <= R) res += j - i; } } if (T != 1000) printf("%lld\n", ((res + solve(R) - solve(L - 1)) % P + P) % P); if (te == 906) printf("%lld %lld\n", L, R); } return 0; }
failed
7_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define enter fout << "\n"; #define space fout << " "; #define dot fout << ","; #define oui fout << "Yes\n"; #define non fout << "No\n"; #define si fout << "?"; #define i32 int #define u32 unsigned int #define i64 long long #define u64 unsigned long long #define i128 __int128 #define u128 unsigned __int128 #define debug(x) fout << #x << "=" << x << "\n"; #define vdebug(a) \ fout << #a << "="; \ for (autox : a) \ fout << x << " "; \ fout << "\n"; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); namespace fastio { const int bufl = 1 << 20; const double base1[16] = {1, 1e-1, 1e-2, 1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15}; const double base2[16] = {1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15}; struct IN { FILE *IT; char ibuf[bufl], *is = ibuf, *it = ibuf; IN() { IT = stdin; } IN(char *a) { IT = fopen(a, "r"); } inline char getChar() { if (is == it) { it = (is = ibuf) + fread(ibuf, 1, bufl, IT); if (is == it) return EOF; } return *is++; } template <typename Temp> inline void getInt(Temp &a) { a = 0; int b = 0, c = getChar(); while (c < 48 || c > 57) b ^= (c == 45), c = getChar(); while (c >= 48 && c <= 57) a = (a << 1) + (a << 3) + c - 48, c = getChar(); if (b) a = -a; } template <typename Temp> inline void getDouble(Temp &a) { a = 0; int b = 0, c = getChar(), d = 0; __int128 e = 0, f = 0; while (c < 48 || c > 57) b ^= (c == 45), c = getChar(); while (c >= 48 && c <= 57) e = (e << 1) + (e << 3) + c - 48, c = getChar(); if (c == 46) { c = getChar(); while (c >= 48 && c <= 57) d++, f = (f << 1) + (f << 3) + c - 48, c = getChar(); } a = e + base1[d] * f; if (b) a = -a; } IN &operator>>(char &a) { a = getChar(); while (a <= 32) a = getChar(); return *this; } IN &operator>>(char *a) { do { *a = getChar(); } while (*a <= 32); while (*a > 32) *++a = getChar(); *a = 0; return *this; } IN &operator>>(string &a) { char b = getChar(); while (b <= 32) b = getChar(); while (b > 32) a += b, b = getChar(); return *this; } IN &operator>>(int &a) { getInt(a); return *this; } IN &operator>>(long long &a) { getInt(a); return *this; } IN &operator>>(__int128 &a) { getInt(a); return *this; } IN &operator>>(float &a) { getDouble(a); return *this; } IN &operator>>(double &a) { getDouble(a); return *this; } IN &operator>>(long double &a) { getDouble(a); return *this; } }; struct OUT { FILE *IT; char obuf[bufl], *os = obuf, *ot = obuf + bufl; int Eps; long double Acc; OUT() { IT = stdout, Eps = 6, Acc = 0.5; } OUT(char *a) { IT = fopen(a, "w"), Eps = 6, Acc = 0.5; } inline void ChangEps(int x = 6) { Eps = x; } inline void flush() { fwrite(obuf, 1, os - obuf, IT); os = obuf; } inline void putChar(int a) { *os++ = a; if (os == ot) flush(); } template <typename Temp> inline void putInt(Temp a) { if (a < 0) { putChar(45); a = -a; } if (a < 10) { putChar(a + 48); return; } putInt(a / 10); putChar(a % 10 + 48); } template <typename Temp> inline void putLeading(Temp a, int b) { if (!b) return; putLeading(a / 10, b - 1); putChar(a % 10 + 48); } template <typename Temp> inline void putDouble(Temp a) { if (a < 0) { putChar(45); a = -a; } __int128 ff = (a - (__int128)a) * base2[Eps + 2], gg = 0; ff += 50; while (ff > 0) { ff /= 10; gg++; } __int128 b = a; if (gg == Eps + 3) { putInt(b + 1); } else { putInt(b); } a -= b; a *= base2[Eps]; b = a + Acc; putChar(46); putLeading(b, Eps); } OUT &operator<<(char a) { putChar(a); return *this; } OUT &operator<<(const char *a) { while (*a) putChar(*a++); return *this; } OUT &operator<<(string a) { for (auto c : a) putChar(c); return *this; } OUT &operator<<(int a) { putInt(a); return *this; } OUT &operator<<(long long a) { putInt(a); return *this; } OUT &operator<<(__int128 a) { putInt(a); return *this; } OUT &operator<<(unsigned int a) { putInt(a); return *this; } OUT &operator<<(unsigned long long a) { putInt(a); return *this; } OUT &operator<<(unsigned __int128 a) { putInt(a); return *this; } OUT &operator<<(float a) { putDouble(a); return *this; } OUT &operator<<(double a) { putDouble(a); return *this; } OUT &operator<<(long double a) { putDouble(a); return *this; } ~OUT() { flush(); } }; } // namespace fastio fastio::IN fin; fastio::OUT fout; // 从1111出发到这些点的dp值 long long dp[1 << 17][20]; long long edge[20][20]; long long a1, a2, a3, a4; long long rres[1 << 17]; void init() { memset(edge, 0x3f, sizeof(edge)); memset(dp, 0x3f, sizeof(dp)); memset(rres, 0x3f, sizeof(rres)); for (int i = 0; i <= 15; ++i) { edge[i][i ^ (1 << 0)] = a1; edge[i][i ^ (1 << 1)] = a1; edge[i][i ^ (1 << 2)] = a1; edge[i][i ^ (1 << 3)] = a1; edge[i][i ^ (0x3)] = a2; edge[i][i ^ (0xc)] = a2; edge[i][i ^ (0x5)] = a3; edge[i][i ^ (0xa)] = a3; edge[i][i ^ (0xf)] = a4; } for (int i = 0; i < 16; ++i) { edge[i][i] = 0; } edge[15][15] = 2 * min({a1, a2, a3, a4}); for (int i = 0; i < 16; ++i) { for (int j = 0; j < 16; ++j) { for (int k = 0; k < 16; ++k) { edge[i][k] = min(edge[i][k], edge[i][j] + edge[j][k]); } } } for (int i = 0; i <= 15; ++i) { dp[1 << i][i] = edge[i][15]; } for (int k = 1; k < (1 << 16); ++k) { for (int j = 0; j < 16; ++j) { if ((k & (1 << j)) == 0) continue; for (int l = 0; l < 15; ++l) { // 新点 if (k & (1 << l)) continue; dp[k | (1 << l)][l] = min(dp[k | (1 << l)][l], dp[k][j] + edge[j][l]); } } } for (int i = 1; i < (1 << 16); ++i) { for (int j = 0; j < 16; ++j) { rres[i] = min(rres[i], dp[i][j]); } } } long long n, m, res; // #define NaraFluorine int main() { int t; fin >> t >> a1 >> a2 >> a3 >> a4; int arr = (1 << 16) - 1; init(); for (int _ = 1; _ <= t; ++_) { fin >> n; int ar = 0; for (int i = 1; i <= n; ++i) { int tmp = 0, ttmp = 0; fin >> tmp >> ttmp; switch (tmp) { case 0: { tmp = 0; break; } case 1: { tmp = 1; break; } case 10: { tmp = 2; break; } case 11: { tmp = 3; break; } } switch (ttmp) { case 0: { ttmp = 0; break; } case 1: { ttmp = 1; break; } case 10: { ttmp = 2; break; } case 11: { ttmp = 3; break; } } tmp = (tmp << 2) + ttmp; ar |= 1 << (tmp); } int nn = arr ^ ar; // 多余点的子集覆盖 res = rres[ar]; for (int j = nn; j; j = (j - 1) & nn) { // do something res = min(res, rres[j ^ ar]); } // 枚举二进制子集(降序) // for(int i=0;i<16;++i){ // res=min(res,dp[ar][i]); // } fout << res << "\n"; // debug(ar); } return 0; }
failed
83_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 1e9; const ll LINF = 1e18; const int MOD = 998244353; struct Edge { int to, rev; ll cap, cost; Edge(int to, int rev, ll cap, ll cost) : to(to), rev(rev), cap(cap), cost(cost) {} }; struct MinCostFlow { int n; vector<vector<Edge>> graph; vector<ll> dist, potential; vector<pair<int, int>> par; MinCostFlow(int n) : n(n), graph(n), dist(n), potential(n), par(n) {} void add_edge(int u, int v, ll cap, ll cost) { graph[u].emplace_back(v, graph[v].size(), cap, cost); graph[v].emplace_back(u, graph[u].size() - 1, 0, -cost); } void shortest_paths(int s) { fill(dist.begin(), dist.end(), LINF); priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; pq.push({0, s}); dist[s] = 0; while (!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if (d > dist[u]) continue; for (int i = 0; i < graph[u].size(); ++i) { auto &e = graph[u][i]; ll new_dist = dist[u] + e.cost + potential[u] - potential[e.to]; if (e.cap > 0 && new_dist < dist[e.to]) { dist[e.to] = new_dist; par[e.to] = {u, i}; pq.push({dist[e.to], e.to}); } } } } ll flow(int s, int t, ll max_flow) { ll flow = 0, cost = 0; fill(potential.begin(), potential.end(), 0); while (flow < max_flow) { shortest_paths(s); if (dist[t] == LINF) break; for (int i = 0; i < n; ++i) potential[i] += dist[i]; ll f = max_flow - flow; for (int v = t; v != s; v = par[v].first) { f = min(f, graph[par[v].first][par[v].second].cap); } flow += f; cost += f * (potential[t] - potential[s]); for (int v = t; v != s; v = par[v].first) { auto &e = graph[par[v].first][par[v].second]; e.cap -= f; graph[v][e.rev].cap += f; } } return flow == max_flow ? cost : -1; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, k; cin >> n >> m >> k; vector<int> a(n), b(n); for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; set<pair<int, int>> forbidden; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; u--; v--; forbidden.insert({u, v}); } int source = 0; int sink = 2 * n + 1; MinCostFlow mcf(2 * n + 2); for (int i = 0; i < n; ++i) { mcf.add_edge(source, i + 1, 1, 0); mcf.add_edge(n + 1 + i, sink, 1, 0); } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (forbidden.count({i, j})) continue; ll s = (a[i] + b[j]) % MOD; mcf.add_edge(i + 1, n + 1 + j, 1, -s); } } vector<ll> ans(k + 1, -1); ll total_cost = 0; for (int x = 1; x <= k; ++x) { ll cost = mcf.flow(source, sink, 1); if (cost == -1) break; total_cost += -cost; ans[x] = total_cost; } for (int x = 1; x <= k; ++x) { cout << ans[x] << " "; } cout << endl; return 0; }
failed
76_1_wrong.cpp
#pragma GCC optimize(3) #include <bits/stdc++.h> using namespace std; #define int long long signed n; int k; string s; unordered_map<string, int> vis; unordered_map<string, string> nxt; string ans; void dfs(const string &stat, int steps) { if (steps == k + 1) { cout << stat << '\n'; exit(0); } vis[stat] = steps; string r; for (signed i = 0; i < n; i += 2) { r.push_back(stat[i]); } for (signed i = 1; i < n; i += 2) { r.push_back(stat[i]); } // cout << r << '\n'; nxt[stat] = r; if (!vis[r]) { dfs(r, steps + 1); } int circle_length = steps - vis[r] + 1; int remain = k - steps; // cout << "I got this! length: " << circle_length << ", remain: " << // remain << '\n'; remain %= circle_length; // cout << "After remain: " << remain << '\n'; for (int t = 1; t <= remain; ++t) { r = nxt[r]; } cout << r << '\n'; exit(0); } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; cin >> s; dfs(s, 1); return 0; }
failed
61_3_wrong.cpp
#include <iostream> #include <string> #include <vector> using namespace std; struct Team { string name; int strength; }; vector<Team> process_round(const vector<Team> &current) { vector<Team> next_round; int n = current.size(); for (int i = 0; i < n / 2; ++i) { const Team &team1 = current[i]; const Team &team2 = current[n - 1 - i]; if (team1.strength > team2.strength) { next_round.push_back(team1); } else { next_round.push_back(team2); } } return next_round; } int main() { vector<Team> teams; for (int i = 0; i < 8; ++i) { string name; int strength; cin >> name >> strength; teams.push_back({name, strength}); } vector<Team> current = teams; current = process_round(current); // Process Quarterfinals current = process_round(current); // Process Semifinals Team champion, runner_up; if (current[0].strength > current[1].strength) { champion = current[0]; runner_up = current[1]; } else { champion = current[1]; runner_up = current[0]; } cout << champion.name << " beats " << runner_up.name << endl; return 0; }
failed
87_3_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; constexpr int N = 1E6; std::vector<int> primes; i64 f[N + 1][10]; i64 ans[N + 1]; int minp[N + 1]; int fp[20][10][10]; void solve() { i64 n; std::cin >> n; i64 s = std::cbrt(n); if (s * s * s > n) { s--; } std::cout << ans[s] << "\n"; } void work(int x, int y, int r) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { f[y][j] += f[x][i] * fp[r][i][j]; } } } void sieve() { f[1][0] = 1; for (int i = 2; i <= N; i++) { if (minp[i] == 0) { minp[i] = i; primes.push_back(i); work(1, i, 1); } for (auto p : primes) { if (i * p > N) { break; } int r = 1; int x = i; while (x % p == 0) { x /= p; r++; } minp[i * p] = p; work(x, i * p, r); if (minp[i] == p) { break; } } } for (int i = 1; i <= N; i++) { ans[i] = ans[i - 1] + f[i][3]; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); constexpr int B = 60; std::vector<std::array<int, 9>> states; states.push_back({0, 0, 0, 0, 0, 0, 0, 0, 0}); std::vector<std::array<int, 9>> arr; int a[3][3]; for (a[0][0] = 0; a[0][0] < B; a[0][0]++) { for (a[0][1] = 0; a[0][0] + a[0][1] < B; a[0][1]++) { for (a[0][2] = 0; a[0][0] + a[0][1] + a[0][2] < B; a[0][2]++) { int S = a[0][0] + a[0][1] + a[0][2]; for (a[1][0] = 0; a[0][0] + a[1][0] <= S; a[1][0]++) { a[2][0] = S - a[0][0] - a[1][0]; a[1][1] = S - a[0][2] - a[2][0]; if (a[1][1] < 0) { continue; } a[1][2] = S - a[1][0] - a[1][1]; if (a[1][2] < 0) { continue; } a[2][1] = S - a[0][1] - a[1][1]; if (a[2][1] < 0) { continue; } a[2][2] = S - a[2][0] - a[2][1]; if (a[2][2] < 0) { continue; } if (a[0][0] + a[1][1] + a[2][2] != S) { continue; } // for (int i = 0; i < 3; i++) { // for (int j = 0; j < 3; j++) { // std::cerr << a[i][j] << " \n"[j == 2]; // } // } // std::cerr << "\n"; std::array<int, 9> comp; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { comp[i * 3 + j] = a[i][j]; } } arr.push_back(comp); } } } } std::map<std::array<int, 9>, int> stid; auto trans = [&](const auto &s, const auto &a) { auto ns = s; for (int j = 0; j < 9; j++) { ns[j] = ns[j] * B + a[j]; } std::map<int, int> idx; for (int j = 0; j < 9; j++) { if (!idx.contains(ns[j])) { idx[ns[j]] = idx.size(); } ns[j] = idx[ns[j]]; } return ns; }; for (const auto &a : arr) { for (int i = 0; i < states.size(); i++) { auto ns = trans(states[i], a); if (std::find(states.begin(), states.end(), ns) == states.end()) { states.push_back(ns); } } } for (int i = 0; i < states.size(); i++) { stid[states[i]] = i; } for (const auto &a : arr) { int S = (a[0] + a[1] + a[2]) / 3; std::vector<int> tr; for (int i = 0; i < states.size(); i++) { auto ns = trans(states[i], a); fp[S][i][stid[ns]]++; } } sieve(); int t; std::cin >> t; while (t--) { solve(); } return 0; }
failed
64_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5, inf = 0x3f3f3f3f; struct edge { int nxt, to, w, F; } e[MAXN * 2]; int head[MAXN], tot, HEAD[MAXN], ss[MAXN], d[MAXN], total, num[MAXN], cnt[MAXN], rest, stk[MAXN], top, ts = 2, now, start, finish; long long ansc; bool prime[MAXN], vis[MAXN], in[MAXN], no1; inline void init(int lim) { for (int i = 2; i <= lim; i++) prime[i] = true; for (int i = 2; i <= lim; i++) { if (prime[i]) ss[++total] = i; for (int j = 1; j <= total && ss[j] <= lim / i; j++) { prime[i * ss[j]] = false; if (!(i % ss[j])) break; } } } set<int> s; map<int, int> mp; map<pair<int, int>, bool> connected; inline void merge(int u, int v, int w, int f) { if (!connected[make_pair(u, v)]) connected[make_pair(u, v)] = true; else return; // cout << u << ' ' << v << ' ' << w << ' ' << f << '\n'; // cout << fanmap[u] << ' ' << fanmap[v] << ' ' << w << '\n'; e[++tot] = (edge){head[u], v, w, f}; head[u] = tot; swap(u, v), w = -w, f = 0; e[++tot] = (edge){head[u], v, w, f}; head[u] = tot; } inline int fast_pow(int a, int b) { int ans = 1; for (; b > 0; b >>= 1, a *= a) if (b & 1) ans *= a; return ans; } inline int getid(int x) { if (!mp[x]) mp[x] = ++ts; return mp[x]; } inline void dfs(int dep, int num, int totcnt) { if (dep == top) { if (no1 && num == 1) return; if (totcnt) merge(now, getid(num), totcnt, 1), merge(getid(num), finish, 0, 1); return; } if (stk[dep + 1] < 0) { dfs(dep + 1, num, totcnt), dfs(dep + 1, -num * stk[dep + 1], totcnt - 1); } else { for (int i = 0; i <= cnt[stk[dep + 1]]; i++) dfs(dep + 1, num * fast_pow(stk[dep + 1], i), totcnt - i); } } queue<int> que; inline bool spfa() { for (int i = 1; i <= ts; i++) d[i] = -inf; d[start] = 0; que.push(start), in[start] = true; int u, v, w, f; while (!que.empty()) { u = que.front(), que.pop(), in[u] = false; for (int i = head[u]; i; i = e[i].nxt) { v = e[i].to, w = e[i].w, f = e[i].F; if (!f) continue; if (d[v] < d[u] + w) { d[v] = d[u] + w; if (!in[v]) que.push(v), in[v] = true; } } } return (d[finish] != -inf); } inline int dinic(int u, int rest) { if (u == finish) return rest; int res = 0, tmpF, v, w, f; vis[u] = true; for (int i = HEAD[u]; i; i = e[i].nxt) { HEAD[u] = i; v = e[i].to, w = e[i].w, f = e[i].F; if (d[v] != d[u] + w || !f || vis[v]) continue; tmpF = dinic(v, min(rest, f)); ansc += w * tmpF; rest -= tmpF, res += tmpF; e[i].F -= tmpF, e[(i & 1) ? (i + 1) : (i - 1)].F += tmpF; if (!rest) break; } vis[u] = false; return res; } inline void MCMF() { while (spfa()) { for (int i = 1; i <= ts; i++) HEAD[i] = head[i]; dinic(start, inf); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); init(100001); start = 1, finish = 2; int n, totalcnt; cin >> n; for (int i = 1; i <= n; i++) { cin >> num[i]; if (num[i] == 1) no1 = true; } for (int i = 1, x; i <= n; i++) { if (num[i] == 1) continue; x = num[i], now = getid(num[i]), totalcnt = 0; merge(start, now, 0, 1); while (top) { if (stk[top] > 0) cnt[stk[top]] = 0; top--; } for (int j = 1; j <= total && 1ll * ss[j] * ss[j] <= x; j++) { if (!(x % ss[j])) { while (!(x % ss[j])) x /= ss[j], cnt[ss[j]]++, totalcnt++; stk[++top] = ss[j]; } } if (x != 1) stk[++top] = -x, totalcnt++; dfs(0, 1, totalcnt); } MCMF(); cout << ansc << '\n'; return 0; }
failed
10_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; using namespace chrono; using ll = long long; using ull = unsigned long long; string to_string(const string &s) { return '"' + s + '"'; } string to_string(bool b) { return b ? "true" : "false"; } template <typename A, typename B> string to_string(const pair<A, B> &p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename T> string to_string(const T &v) { string s = "{"; bool first = true; for (const auto &it : v) { if (!first) s += ", "; else first = false; s += to_string(it); } return s += "}"; } void debug_out() { cerr << endl; } template <typename T, typename... Args> void debug_out(const T &first, const Args &...rest) { cerr << to_string(first) << " "; debug_out(rest...); } #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); auto startTime = high_resolution_clock::now(); int get_time() { auto stopTime = chrono::high_resolution_clock::now(); auto duration = duration_cast<milliseconds>(stopTime - startTime); return duration.count(); // in ms } int n, m; bool solve(vector<vector<int>> v) { if (v[1][1] != v[n][m]) { return 0; } auto v2 = v; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { v2[i][j] = 0; } } auto v4 = v2; if (v[1][1] == 1) { v2[1][1] = 1; } for (int d = 1; d <= n + m - 1; ++d) { for (int i = 1; i <= n; ++i) { int j = d - i + 1; if (j < 1 || j > m) { continue; } if (v[i][j] % 2 != v2[i][j] % 2) { return 0; } if (v[i][j] > 400) { assert(1 == 0); return 0; } if (j + 1 <= m) { if (i + 1 <= n) { if (v[i][j + 1] % 2 != v2[i][j + 1] % 2) { if (v2[i][j] == 0) { v2[i][j] += 2; // 2 * n * m -> (n + m) v4[i][j] += 2; } int m = v2[i][j]; if (m % 2 == 0) { --m; } v2[i][j + 1] += m; v2[i + 1][j] += v2[i][j] - m; } else { int m = v2[i][j]; if (m % 2 == 1) --m; v2[i][j + 1] += m; v2[i + 1][j] += v2[i][j] - m; } } else { v2[i][j + 1] += v2[i][j]; } } else if (i + 1 <= n) { v2[i + 1][j] += v2[i][j]; } } } for (int d = n + m - 1; d >= 1; --d) { for (int i = 1; i <= n; ++i) { int j = d - i + 1; if (j < 1 || j > m) { continue; } if (i + 1 <= n) { if (v4[i + 1][j] > 0) { v2[i][j] += v4[i + 1][j]; v4[i][j] += v4[i + 1][j]; v4[i + 1][j] = 0; } } if (j + 1 <= m) { if (v4[i][j + 1] > 0) { v2[i][j] += v4[i][j + 1]; v4[i][j] += v4[i][j + 1]; v4[i][j + 1] = 0; } } } } cout << "YES\n"; cout << v2[n][m] << "\n"; // assert(v2[n][m] <= (n + m)); auto v3 = v; while (v2[n][m] > 0) { int i = 1, j = 1; string s; while (i < n || j < m) { --v2[i][j]; v[i][j] ^= 1; if (j + 1 <= m && v2[i][j + 1] > 0) { s.push_back('R'); ++j; } else if (i + 1 <= n && v2[i + 1][j] > 0) { s.push_back('D'); ++i; } else { assert(1 == 0); } } --v2[i][i]; v[i][j] ^= 1; cout << s << "\n"; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (v[i][j] == 1) { while (1) ; } // assert(v[i][j] == 0); } } return 1; } void solve() { cin >> n >> m; // n = 4; // m = 4; vector<vector<int>> v(n + 1, vector<int>(m + 1)); for (int i = 1; i <= n; ++i) { string s; cin >> s; for (int j = 1; j <= m; ++j) { v[i][j] = (s[j - 1] == 'W') ? 0 : 1; // v[i][j] = rng() % 2; } } bool ok = solve(v); if (ok) { return; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { v[i][j] ^= 1; } } ok = solve(v); if (!ok) { cout << "NO\n"; } } int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); int t = 1; cin >> t; while (t--) solve(); return 0; }
failed
45_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n; #define endl "\n" signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int k; cin >> k; string s; cin >> s; k = min({k, 10, (int)s.size() - 1}); int ans = 0; for (int t = 0; t <= k; t++) { string y = s.substr(s.size() - t) + s.substr(0, s.size() - t); int pt = 0; int cnt = 0; string tr = "nanjing"; for (int i = 0; i + 6 < y.size(); i++) { if (y.substr(i, 7) == tr) { cnt++; } } ans = max(ans, cnt); } cout << ans << endl; } }
failed
51_1_wrong.cpp
#ifdef local #pragma GCC optimize(1) #pragma GCC optimize(2) #pragma GCC optimize(3) #endif // #pragma comment(linker, "/STACK:102400000,102400000") #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> #ifndef local #define endl '\n' #endif #define pb emplace_back #define fi first #define se second // #define endl '\n' #define rep(i, l, r) for (long long i = l; i <= r; i++) #define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define mem(a, x) memset(a, x, sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef double db; typedef pair<int, int> P; typedef pair<P, int> PP; const double pi = acos(-1); typedef __int128_t int128; const ll mod = 1e18; const db eps = 1e-9; std::mt19937_64 rng(time(0)); ll my_random(ll l, ll r) { uniform_int_distribution<int> range(l, r); return range(rng); } void __() { #ifdef local system("pause"); #endif } ll qp(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans; } const int maxn = 1e5 + 10; int n, m; string dice[10]; set<string> st; db dp[2][maxn]; int pw[maxn]; int v[10], w[10]; db tmpsum, tmpcnt; void dfs3(int cnt, int sum, int flag) { if (cnt == n) { tmpsum += dp[(flag & 1)][sum]; tmpcnt += 1; return; } else { if (v[cnt] != 6) dfs3(cnt + 1, sum + pw[cnt] * v[cnt], flag); else for (int i = 0; i < 6; i++) dfs3(cnt + 1, sum + pw[cnt] * i, flag); } } void dfs2(int cnt, int sum, int nowsum, int flag) { if (cnt == n) { dp[flag & 1][nowsum] = min(dp[flag & 1][nowsum], dp[(flag & 1) ^ 1][sum]); return; } else { dfs2(cnt + 1, sum + pw[cnt] * v[cnt], nowsum, flag); dfs2(cnt + 1, sum + pw[cnt] * 6, nowsum, flag); } } void dfs(int cnt, int flag) { if (cnt == n) { bool f = 1; int sum = 0; for (int i = 0; i < n; i++) { if (v[i] == 6) f = 0; sum += pw[i] * v[i]; } if (f == 1) { string s; for (int i = 0; i < n; i++) { s += dice[i][v[i]]; // cout << i << " " << v[i] << " " << dice[i][v[i]] << " " << s << endl; } sort(s.begin(), s.end()); if (st.find(s) != st.end()) { // cout << "XXX " << s << " " << flag << " " << (flag & 1) << endl; dp[flag & 1][sum] = 0; } else { dp[flag & 1][sum] = 1e10; dfs2(0, 0, sum, flag); } } else { tmpsum = tmpcnt = 0; dfs3(0, 0, flag); // cout << "XXX " << sum << " " << tmpsum << " " << tmpcnt << endl; dp[flag & 1][sum] = tmpsum / tmpcnt + 1; } } else { for (int i = 0; i <= 6; i++) v[cnt] = i, dfs(cnt + 1, flag); } } int main() { IOS; int t1 = clock(); cin >> n >> m; pw[0] = 1; for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * 7; for (int i = 0; i < n; i++) cin >> dice[i]; for (int i = 1; i <= m; i++) { string s; cin >> s; sort(s.begin(), s.end()); st.insert(s); } int iter_num = 10000; for (int i = 0; i <= pw[n]; i++) dp[0][i] = 1e10; int i = 1; while (clock() - t1 < 9500) { dfs(0, i); mem(dp[(i & 1) ^ 1], 0); // for (int j = 0; j < pw[n]; j++) // cout << dp[i & 1][j] << " "; // cout << endl; i += 1; } if (dp[(i - 1) & 1][pw[n] - 1] >= 1e9) cout << "impossible" << endl; else cout << fixed << setprecision(20) << dp[(i - 1) & 1][pw[n] - 1] << endl; __(); return 0; } /* 1 1 ABCDEF B */
failed
118_2_wrong.cpp
#include <algorithm> #include <iostream> #include <map> #include <queue> using namespace std; #define int long long using ll = long long; using pii = pair<int, int>; const int N = 2e5 + 7; const int INF = 1e18; struct Point { int a, b; } s[N]; struct Node { int i, j; ll d; bool operator<(const Node &rhs) const { return d > rhs.d; } }; int n, k, cnt, tot, ans[N], head[N]; struct LiChao_Tree { int tot, rt[N]; struct Tree { int ls, rs; pii tag; } t[N << 1]; int calc(pii p, int x) { return p.first * x + p.second; } #define ls(p) (t[p].ls) #define rs(p) (t[p].rs) void insert(int &p, pii x, int l, int r) { if (l > r) return; if (!p) { t[p = ++tot].tag = x; return; } int mid = (l + r) >> 1; if (calc(t[p].tag, mid) > calc(x, mid)) swap(x, t[p].tag); if (calc(t[p].tag, l) <= calc(x, l) && calc(t[p].tag, r) <= calc(x, r)) return; insert(ls(p), x, l, mid); insert(rs(p), x, mid + 1, r); } int query(int p, int x, int l, int r) { if (!p) return INF; if (l == r) return calc(t[p].tag, x); int mid = (l + r) >> 1; if (x <= mid) return min(calc(t[p].tag, x), query(ls(p), x, l, mid)); else return min(calc(t[p].tag, x), query(rs(p), x, mid + 1, r)); } } T; vector<int> v[N], w[N], dis[N], vis[N]; vector<pii> p[N]; void clear() { for (int i = 0; i <= max(n, k); i++) T.rt[i] = 0; for (int i = 0; i <= T.tot; i++) T.t[i].ls = T.t[i].rs = 0; T.tot = 0; for (int i = 2; i <= n; i++) ans[i] = INF; for (int i = 0; i <= max(n, k); i++) v[i].clear(), w[i].clear(), dis[i].clear(), vis[i].clear(), p[i].clear(); cnt = tot = 0; } bool cmp(pii x, pii y) { return s[x.first].a > s[y.first].a; } priority_queue<Node> q; bool insert(int i, int j, int x) { if (vis[i][j]) return 0; x = min(x, T.query(T.rt[v[i][j]], s[i].a, 1, 1e6)); if (dis[i][j] > x) { dis[i][j] = x; q.push((Node){i, j, x}); return true; } return false; } void solve() { cin >> n >> k; for (int i = 1; i <= k; i++) cin >> s[i].a; for (int i = 1; i <= k; i++) cin >> s[i].b; for (int i = 1; i <= k; i++) { int m, x, y, lst; cin >> m; v[i].resize(m); dis[i].resize(m); vis[i].resize(m); w[i].clear(); for (int j = 1; j <= m; j++) { cin >> x; v[i][j - 1] = x; dis[i][j - 1] = INF; p[x].push_back(make_pair(i, j - 1)); if (j != m) { cin >> y; w[i].push_back(y); } lst = x; } } while (!q.empty()) q.pop(); for (int i = 1; i <= k; i++) { for (int j = 0; j < v[i].size(); j++) { if (v[i][j] == 1) { dis[i][j] = 0; q.push((Node){i, j, 0}); } else { dis[i][j] = INF; } } } for (int i = 1; i <= n; i++) sort(p[i].begin(), p[i].end(), cmp); for (int i = 2; i <= n; i++) ans[i] = INF; while (!q.empty()) { auto t = q.top(); q.pop(); int i = t.i, j = t.j, d = t.d; // cout << i << " " << j << " " << d << " " << v[i][j] << endl; if (vis[i][j]) continue; vis[i][j] = true; ans[v[i][j]] = min(ans[v[i][j]], dis[i][j]); T.insert(T.rt[v[i][j]], make_pair(s[i].b, dis[i][j]), 1, 1e6); // cout << dis[i][j] << " " << w[i][j] << endl; if (j + 1 != v[i].size()) insert(i, j + 1, dis[i][j] + w[i][j]); int x = v[i][j]; for (int j = p[x].size() - 1; j >= 0; j--) { int pi = p[x][j].first, pj = p[x][j].second; // cout << pi << " " << pj << " " << endl; if (insert(pi, pj, INF)) break; p[x].pop_back(); } } for (int i = 1; i <= k; i++) { for (int j = 0; j < dis[i].size(); j++) { // printf("dis[%d][%d] = %d %d\n", i, j, v[i][j], dis[i][j]); } } for (int i = 2; i <= n; i++) cout << ans[i] << " "; cout << "\n"; clear(); return; } signed main() { int T = 1; // cin >> T; for (int i = 1; i <= T; i++) solve(); return 0; }
failed
52_1_wrong.cpp
#ifdef local #pragma GCC optimize(1) #pragma GCC optimize(2) #pragma GCC optimize(3) #endif // #pragma comment(linker, "/STACK:102400000,102400000") #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <chrono> #include <climits> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <fstream> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <stack> #include <string> #include <unordered_map> #include <vector> #ifndef local #define endl '\n' #endif #define pb emplace_back #define fi first #define se second // #define endl '\n' #define rep(i, l, r) for (long long i = l; i <= r; i++) #define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define mem(a, x) memset(a, x, sizeof(a)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef double db; typedef pair<int, int> P; typedef pair<P, int> PP; const double pi = acos(-1); typedef __int128_t int128; const ll mod = 1e18; const db eps = 1e-9; std::mt19937_64 rng(time(0)); ll my_random(ll l, ll r) { uniform_int_distribution<int> range(l, r); return range(rng); } void __() { #ifdef local system("pause"); #endif } ll qp(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans; } const int maxn = 2e5 + 10; int n, m; string dice[10]; set<string> st; db dp[2][maxn]; int pw[maxn]; int v[10], w[10]; db tmpsum, tmpcnt; void dfs3(int cnt, int sum, int flag) { if (cnt == n) { tmpsum += dp[(flag & 1)][sum]; tmpcnt += 1; return; } else { if (v[cnt] != 6) dfs3(cnt + 1, sum + pw[cnt] * v[cnt], flag); else for (int i = 0; i < 6; i++) dfs3(cnt + 1, sum + pw[cnt] * i, flag); } } void dfs(int cnt, int flag) { if (cnt == n) { bool f = 1; int sum = 0; for (int i = 0; i < n; i++) { if (v[i] == 6) f = 0; sum += pw[i] * v[i]; } if (f == 1) { string s; for (int i = 0; i < n; i++) { s += dice[i][v[i]]; // cout << i << " " << v[i] << " " << dice[i][v[i]] << " " << s << endl; } sort(s.begin(), s.end()); if (st.find(s) != st.end()) { // cout << "XXX " << s << " " << flag << " " << (flag & 1) << endl; dp[flag & 1][sum] = 0; } else { dp[flag & 1][sum] = 1e10; } } else { tmpsum = tmpcnt = 0; dfs3(0, 0, flag); // cout << "XXX " << sum << " " << tmpsum << " " << tmpcnt << endl; dp[flag & 1][sum] = tmpsum / tmpcnt + 1; } } else { for (int i = 0; i <= 6; i++) v[cnt] = i, dfs(cnt + 1, flag); } } int main() { IOS; clock_t t1 = clock(); cin >> n >> m; pw[0] = 1; for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * 7; for (int i = 0; i < n; i++) cin >> dice[i]; for (int i = 1; i <= m; i++) { string s; cin >> s; sort(s.begin(), s.end()); st.insert(s); } int iter_num = 10000; for (int i = 0; i <= pw[n]; i++) dp[0][i] = 1e10; int i = 1; while (clock() - t1 < 9.5 * CLOCKS_PER_SEC) { dfs(0, i); mem(dp[(i & 1) ^ 1], 0); // for (int j = 0; j < pw[n]; j++) // cout << dp[i & 1][j] << " "; // cout << endl; i += 1; } if (dp[(i - 1) & 1][pw[n] - 1] >= 100) cout << "impossible" << endl; else cout << fixed << setprecision(20) << dp[(i - 1) & 1][pw[n] - 1] << endl; __(); return 0; } /* 1 1 ABCDEF B */
failed
118_1_wrong.cpp
// Problem: M. Machine Learning with Penguins // Contest: Universal Cup - The 3rd Universal Cup. Stage 29: Metropolis // URL: https://contest.ucup.ac/contest/1913/problem/9049 // Memory Limit: 1024 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) #include <bits/stdc++.h> #define ll long long #define pb push_back #define f first #define s second #define sz(x) (int)(x).size() #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define ios \ ios_base::sync_with_stdio(false); \ cin.tie(NULL) #define ld long double #define llf __float128 #define rep(i, a, b) for (int i = a; i < (b); ++i) using namespace std; mt19937 rng(time(NULL)); // 4:34 template <class T> struct Point { typedef Point P; T x, y; explicit Point(T x = 0, T y = 0) : x(x), y(y) {} bool operator<(P p) const { return tie(x, y) < tie(p.x, p.y); } P operator-(P p) const { return P(x - p.x, y - p.y); } P operator+(P p) const { return P(x + p.x, y + p.y); } T dist2() const { return x * x + y * y; } P operator*(T d) { return P(x * d, y * d); } P operator/(T d) { return P(x / d, y / d); } P perp() const { return P(-y, x); } T cross(P p) const { return x * p.y - y * p.x; } }; typedef Point<llf> P; P ccCenter(const P &A, const P &B, const P &C) { P b = C - A, c = B - A; return A + (b * c.dist2() - c * b.dist2()).perp() / b.cross(c) / 2; } pair<P, llf> mec(vector<P> ps) { shuffle(all(ps), mt19937(time(0))); P o = ps[0]; llf r = 0, EPS = 1e-12; rep(i, 0, sz(ps)) if ((o - ps[i]).dist2() > r + EPS) { o = ps[i], r = 0; rep(j, 0, i) if ((o - ps[j]).dist2() > r + EPS) { o = (ps[i] + ps[j]) / 2; r = (o - ps[i]).dist2(); rep(k, 0, j) if ((o - ps[k]).dist2() > r + EPS) { o = ccCenter(ps[i], ps[j], ps[k]); r = (o - ps[i]).dist2(); } } } return {o, r}; } int main() { int n; cin >> n; vector<int> zs; vector<int> z(n); vector<P> ps(n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y >> z[i]; zs.pb(z[i]); ps[i] = P(x, y); } sort(all(zs)); if (zs.back() == 0) { printf("probably\n"); return 0; } const llf eps = 1e-12; auto [o, r] = mec(ps); for (int i = 0; i < n; i++) { if (z[i] != 0 && z[i] != zs.back()) { llf d = (o - ps[i]).dist2(); if (d < r - eps) { printf("not a penguin\n"); return 0; } } } printf("probably\n"); return 0; }
failed
78_1_wrong.cpp
#include <bits/stdc++.h> #define int long long using namespace std; const int N = 2e5 + 6; const int INF = 1e18; const int MOD = 998244353; int n, s, rt, lres, rres; vector<int> vec[N]; int vis[N], a[N], sz[N], r[N], suf[N]; struct BIT { int tr[N]; int query(int x) { int res = 0; for (int i = x; i; i -= i & -i) res += tr[i]; return res; } void update(int x, int v) { for (int i = x; i <= n; i += i & -i) tr[i] += v; } void clear() { for (int i = 1; i <= n; i++) tr[i] = 0; } } t; int fsp(int x, int y) { int res = 1; while (y) { if (y & 1) res = res * x % MOD; x = x * x % MOD; y >>= 1; } return res; } void dfs1(int x, int fa) { vis[x] = 1; if (x != 1) s -= t.query(n) - t.query(x); t.update(x, 1); for (auto &y : vec[x]) if (y != fa) dfs1(y, x); if (x != 1) s += t.query(n) - t.query(x); } void dfs2(int x, int fa) { if (x != 1) a[x] = a[fa] + suf[x]; lres += fsp(2, a[x] + s); for (auto &y : vec[x]) if (y != fa) dfs2(y, x); } void dfs3(int x, int fa) { sz[x] -= t.query(n) - t.query(x); t.update(x, 1); for (auto &y : vec[x]) { if (y == fa) continue; r[y] -= t.query(n) - t.query(x); dfs3(y, x); r[y] += t.query(n) - t.query(x); } sz[x] += t.query(n) - t.query(x); s += sz[x]; } void dfs4(int x, int fa) { if (x != rt) s += suf[x + 1] - sz[x]; rres += fsp(2, s); for (auto &y : vec[x]) { if (y == fa) continue; s -= r[y]; dfs4(y, x); s += r[y]; } if (x != rt) s -= suf[x + 1] - sz[x]; } void solve() { cin >> n; for (int i = 1; i <= n - 2; i++) { int x, y; cin >> x >> y; vec[x].push_back(y); vec[y].push_back(x); } dfs1(1, 0); for (int i = 1; i <= n; i++) if (!vis[i]) suf[i] += 1, rt = i; for (int i = n; i >= 1; i--) suf[i] += suf[i + 1]; dfs2(1, 0); t.clear(); s = 0; dfs3(rt, 0); dfs4(rt, 0); lres %= MOD, rres %= MOD; int res = lres * rres % MOD; cout << res << "\n"; } signed main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int T = 1; // cin >> T; for (int i = 1; i <= T; i++) solve(); return 0; }
failed
85_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; using i64 = long long; const int INF = 0x3f3f3f3f; const i64 mod = 1e9 + 7; const int N = 200005; void solve() { i64 n, m = 0; i64 target; cin >> n; target = n; vector<int> v; while (n) { v.push_back(n % 10); n /= 10; m++; } int cnt; unordered_set<i64> S; auto dfs = [&](auto &dfs, int k, i64 cur) { if (k == m) { if ((cur < target) and (target % cur) == 0 and S.find(cur) == S.end()) { S.insert(cur); } return; } for (int j = k; j < m; ++j) { if (k == 0 and v[j] == 0) continue; swap(v[k], v[j]); dfs(dfs, k + 1, cur * 10 + v[k]); swap(v[k], v[j]); } }; dfs(dfs, 0, 0); cout << S.size() << '\n'; }; int main() { ios::sync_with_stdio(false), cin.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
failed
92_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; } #define vi vector<int> #define vl vector<ll> #define vii vector<pair<int, int>> #define vll vector<pair<ll, ll>> #define vvi vector<vector<int>> #define vvl vector<vector<ll>> #define vvii vector<vector<pair<int, int>>> #define vvll vector<vector<pair<ll, ll>>> #define vst vector<string> #define pii pair<int, int> #define pll pair<ll, ll> #define pb push_back #define all(x) (x).begin(), (x).end() #define mkunique(x) \ sort(all(x)); \ (x).erase(unique(all(x)), (x).end()) #define fi first #define se second #define mp make_pair #define si(x) int(x.size()) const int mod = 998244353, MAX = 200005, INF = 15 << 26; // Wavelet matrix // https://kopricky.github.io/code/DataStructure_Advanced/wavelet_matrix.html struct BitRank { // block: bit 列を管理, count: block ごとに立っている 1 の数を管理 vector<unsigned long long> block; vector<unsigned int> count; BitRank() {} void resize(const unsigned int num) { block.resize(((num + 1) >> 6) + 1, 0); count.resize(block.size(), 0); } // i ビット目を val(0,1) にセット void set(const unsigned int i, const unsigned long long val) { block[i >> 6] |= (val << (i & 63)); } void build() { for (unsigned int i = 1; i < block.size(); i++) { count[i] = count[i - 1] + __builtin_popcountll(block[i - 1]); } } // [0, i) ビットの 1 の数 unsigned int rank1(const unsigned int i) const { return count[i >> 6] + __builtin_popcountll(block[i >> 6] & ((1ULL << (i & 63)) - 1ULL)); } // [i, j) ビットの 1 の数 unsigned int rank1(const unsigned int i, const unsigned int j) const { return rank1(j) - rank1(i); } // [0, i) ビットの 0 の数 unsigned int rank0(const unsigned int i) const { return i - rank1(i); } // [i, j) ビットの 0 の数 unsigned int rank0(const unsigned int i, const unsigned int j) const { return rank0(j) - rank0(i); } }; class WaveletMatrix { private: unsigned int height; vector<BitRank> B; vector<int> pos; public: WaveletMatrix() {} WaveletMatrix(vector<int> vec) : WaveletMatrix(vec, *max_element(vec.begin(), vec.end()) + 1) {} // sigma:文字の種類数 WaveletMatrix(vector<int> vec, const unsigned int sigma) { init(vec, sigma); } void init(vector<int> &vec, const unsigned int sigma) { height = (sigma == 1) ? 1 : (64 - __builtin_clzll(sigma - 1)); B.resize(height), pos.resize(height); for (unsigned int i = 0; i < height; ++i) { B[i].resize(vec.size()); for (unsigned int j = 0; j < vec.size(); ++j) { B[i].set(j, get(vec[j], height - i - 1)); } B[i].build(); auto it = stable_partition(vec.begin(), vec.end(), [&](int c) { return !get(c, height - i - 1); }); pos[i] = it - vec.begin(); } } // val の i ビット目の値を返す(0,1) int get(const int val, const int i) { return val >> i & 1; } // [l, r) の間に現れる値 val の数 int rank(const int val, const int l, const int r) { return rank(val, r) - rank(val, l); } // [0, i) の間に現れる値 val の数 int rank(int val, int i) { int p = 0; for (unsigned int j = 0; j < height; ++j) { if (get(val, height - j - 1)) { p = pos[j] + B[j].rank1(p); i = pos[j] + B[j].rank1(i); } else { p = B[j].rank0(p); i = B[j].rank0(i); } } return i - p; } // [l, r) の k(0,1,2...) 番目に小さい値を返す int quantile(int k, int l, int r) { int res = 0; for (unsigned int i = 0; i < height; ++i) { const int j = B[i].rank0(l, r); if (j > k) { l = B[i].rank0(l); r = B[i].rank0(r); } else { l = pos[i] + B[i].rank1(l); r = pos[i] + B[i].rank1(r); k -= j; res |= (1 << (height - i - 1)); } } return res; } int rangefreq(const int i, const int j, const int a, const int b, const int l, const int r, const int x) { if (i == j || r <= a || b <= l) return 0; const int mid = (l + r) >> 1; if (a <= l && r <= b) { return j - i; } else { const int left = rangefreq(B[x].rank0(i), B[x].rank0(j), a, b, l, mid, x + 1); const int right = rangefreq(pos[x] + B[x].rank1(i), pos[x] + B[x].rank1(j), a, b, mid, r, x + 1); return left + right; } } // [l,r) で値が [a,b) 内に含まれる数を返す int rangefreq(const int l, const int r, const int a, const int b) { return rangefreq(l, r, a, b, 0, 1 << height, 0); } int rangemin(const int i, const int j, const int a, const int b, const int l, const int r, const int x, const int val) { if (i == j || r <= a || b <= l) return -1; if (r - l == 1) return val; const int mid = (l + r) >> 1; const int res = rangemin(B[x].rank0(i), B[x].rank0(j), a, b, l, mid, x + 1, val); if (res < 0) return rangemin(pos[x] + B[x].rank1(i), pos[x] + B[x].rank1(j), a, b, mid, r, x + 1, val + (1 << (height - x - 1))); else return res; } // [l,r) で値が [a,b) 内に最小の数を返す(数が存在しない場合は -1 を返す) int rangemin(int l, int r, int a, int b) { return rangemin(l, r, a, b, 0, 1 << height, 0, 0); } }; template <typename T> class OrthogonalRangeCount { private: using ptt = pair<T, T>; const int sz; vector<T> X, Y; WaveletMatrix wm; public: OrthogonalRangeCount(vector<ptt> candidate) : sz((int)candidate.size()), X(sz), Y(sz) { sort(candidate.begin(), candidate.end()); vector<int> vec(sz); for (int i = 0; i < sz; ++i) { X[i] = candidate[i].first, Y[i] = candidate[i].second; } sort(Y.begin(), Y.end()); Y.erase(unique(Y.begin(), Y.end()), Y.end()); for (int i = 0; i < sz; ++i) { vec[i] = lower_bound(Y.begin(), Y.end(), candidate[i].second) - Y.begin(); } wm.init(vec, Y.size()); } // [lx,rx) × [ly, ry) の長方形領域に含まれる点の数を答える int query(const T lx, const T ly, const T rx, const T ry) { const int lxid = lower_bound(X.begin(), X.end(), lx) - X.begin(); const int rxid = lower_bound(X.begin(), X.end(), rx) - X.begin(); const int lyid = lower_bound(Y.begin(), Y.end(), ly) - Y.begin(); const int ryid = lower_bound(Y.begin(), Y.end(), ry) - Y.begin(); if (lxid >= rxid || lyid >= ryid) return 0; return wm.rangefreq(lxid, rxid, lyid, ryid); } }; vector<int> G[MAX]; ll sz[MAX]; struct UF { int n; vector<int> par, size, edge; void init(int n_) { n = n_; par.assign(n, -1); size.assign(n, 1); edge.assign(n, 0); for (int i = 0; i < n; i++) { par[i] = i; } } int root(int a) { if (par[a] == a) return a; else return par[a] = root(par[a]); } void unite(int a, int b) { edge[root(a)]++; if (root(a) != root(b)) { size[root(a)] += size[root(b)]; edge[root(a)] += edge[root(b)]; par[root(b)] = root(a); } } bool check(int a, int b) { return root(a) == root(b); } }; vector<int> cyc[MAX]; pll ans[MAX]; int ty[MAX]; int depth[MAX]; vi retu[MAX]; int cycpos[MAX]; ll K; int roo[MAX]; void DFS(int u, int ro) { roo[u] = ro; if (depth[u]) { retu[ty[u]].pb(depth[u]); if (si(retu[ty[u]]) >= K) { ans[u].fi = depth[u] - retu[ty[u]][si(retu[ty[u]]) - K] + 1; ans[u].se = ty[u]; } else { ans[u].fi = si(retu[ty[u]]); ans[u].se = -1; } } for (int to : G[u]) { depth[to] = depth[u] + 1; DFS(to, ro); } if (depth[u]) { retu[ty[u]].pop_back(); } } void solve(int a) { for (int b : G[a]) { if (ans[b].se == -1) { ans[b] = mp(ans[a].fi + 1, ans[a].se); } else if (ans[a].se == ans[b].se) { } else if (ans[a].fi + 1 < ans[b].fi) { ans[b] = mp(ans[a].fi + 1, ans[a].se); } solve(b); } } int main() { std::ifstream in("text.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int Q; cin >> Q; while (Q--) { ll N; cin >> N; cin >> K; for (int i = 0; i < N; i++) { G[i].clear(); cyc[i].clear(); sz[i] = 0; ans[i] = mp(0, -1); ty[i] = 0; depth[i] = 0; retu[i].clear(); cycpos[i] = 0; roo[i] = 0; } vvi color(N); for (int i = 0; i < N; i++) { cin >> ty[i]; ty[i]--; // ty[i]=i; if (ty[i] < 0) ty[i] += N; color[ty[i]].pb(i); } vector<int> A(N), deg(N), seen(N); for (int i = 0; i < N; i++) { int x; cin >> x; x--; // x=(i+1)%N; A[i] = x; deg[x]++; } queue<int> Q; for (int i = 0; i < N; i++) { if (deg[i] == 0) Q.push(i); } while (!Q.empty()) { int u = Q.front(); Q.pop(); deg[A[u]]--; if (deg[A[u]] == 0) Q.push(A[u]); } UF uf; uf.init(N); for (int i = 0; i < N; i++) { if (deg[i]) uf.unite(i, A[i]); } for (int i = 0; i < N; i++) { if (deg[i] == 0) G[A[i]].push_back(i); } vl CN(N); vector<WaveletMatrix> WM(N); for (int i = 0; i < N; i++) { if (deg[i]) { if (uf.root(i) == i) { int now = i; vi ss; while (1) { if (!seen[now]) { seen[now] = true; cycpos[now] = si(cyc[i]); cyc[i].push_back(now); ss.pb(ty[now]); now = A[now]; } else { break; } } // WM[i]=WaveletMatrix(ss); WM[i].init(ss, N); ll len = si(cyc[i]); /* ll l=0,r=N*K; ll xx=-1; while(r-l>1){ ll m=(l+r)/2; for(int j=0;j<len;j++){ if(j<m%len) CN[ty[cyc[i][j]]]++; CN[ty[cyc[i][j]]]+=m/len; } bool ok=false; for(int j=0;j<len;j++){ if(CN[ty[cyc[i][j]]]>=K){ ok=true; xx=ty[cyc[i][j]]; } CN[ty[cyc[i][j]]]=0; } if(ok) r=m; else l=m; } ans[i]=mp(r,xx); */ } } } for (int i = 0; i < N; i++) { if (deg[i]) { DFS(i, i); } } /* for(int a=0;a<N;a++){ for(int l=0;l<3;l++){ for(int r=l;r<3;r++){ cout<<WM[3].rank(a,l,r)<<" "; } } cout<<endl; } */ for (int i = 0; i < N; i++) { // if(deg[i]&&uf.root(i)==i) continue; if (ans[i].se == -1) { ll sude = depth[i], rem = K - ans[i].fi; // cout<<i<<" "<<rem<<" ! "<<endl; int ro = roo[i]; int M = si(cyc[uf.root(ro)]); // cout<<ro<<" "<<ty[i]<<" "<<M<<endl; ll al = WM[uf.root(ro)].rank(ty[i], 0, M); if (al == 0) { ans[i] = mp(-1, -1); continue; } // ll l=max(0LL,rem/al*M+rem%al-1),r=(rem+al-1)/al*al; ll l = max(0LL, rem / al - 1) * M, r = (rem / al + 2) * M; // cout<<l<<" "<<r<<endl; // cout<<max(0LL,rem/al*M+rem%al-1)<<" "<<(rem+al-1)/al*M<<endl; // assert(l<=max(0LL,rem/al*M+rem%al-1)); // assert(r>=(rem+al-1)/al*M); l = max(0LL, (rem / al - 1) * M + rem % al - 1); r = (rem + al - 1) / al * M + 1; // cout<<i<<" "<<rem<<" "<<l<<" "<<r<<endl; ll z = WM[uf.root(ro)].rank(ty[i], cycpos[ro], M); while (r - l > 1) { ll m = (l + r) / 2; ll can = 0; if (cycpos[ro] + m >= M) { can += WM[uf.root(ro)].rank(ty[i], cycpos[ro], M); can += al * ((m - (M - cycpos[ro])) / M); can += WM[uf.root(ro)].rank(ty[i], 0, (m - (M - cycpos[ro])) % M); } else { can += WM[uf.root(ro)].rank(ty[i], cycpos[ro], cycpos[ro] + m); } if (can >= rem) r = m; else l = m; } // assert(r!=max(0LL,rem/al*M+rem%al-1)+1); // assert(r!=(rem+al-1)/al*M+1); ans[i] = mp(sude + r, ty[i]); } } for (int i = 0; i < N; i++) { if (deg[i] && uf.root(i) == i) { for (int j = si(cyc[i]) - 1; j >= 0; j--) { int a = cyc[i][(j + 1 + si(cyc[i])) % si(cyc[i])], b = cyc[i][j]; if (ans[a].se == ans[b].se) { } else if (ans[a].fi + 1 < ans[b].fi) { ans[b] = mp(ans[a].fi + 1, ans[a].se); } } for (int j = si(cyc[i]) - 1; j >= 0; j--) { int a = cyc[i][(j + 1 + si(cyc[i])) % si(cyc[i])], b = cyc[i][j]; if (ans[a].se == ans[b].se) { } else if (ans[a].fi + 1 < ans[b].fi) { ans[b] = mp(ans[a].fi + 1, ans[a].se); } } } } for (int i = 0; i < N; i++) { if (deg[i]) { solve(i); } } ll res = 0; for (int i = 0; i < N; i++) { res += (ll)(i + 1) * (ans[i].se + 1); } cout << res << "\n"; } }
failed
29_3_wrong.cpp
#include <bits/stdc++.h> #define ent '\n' #define int long long using namespace std; const int maxn = 1e6 + 12; const int mod = 1e9 + 7; vector<int> g[maxn]; int cnt[maxn]; bool is[maxn]; int n, m, k; void solve() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) { int x; cin >> x; is[x] = true; } int val = 0; while (m--) { int a, b; cin >> a >> b; val += (is[a] && is[b]); g[a].push_back(b); g[b].push_back(a); } multiset<int> s; for (int i = 1; i <= k; i++) { if (is[i]) continue; for (int to : g[i]) { cnt[i] += (is[to] == 1); } s.insert(cnt[i]); } int ans = val; for (int v = 1; v <= k; v++) { if (is[v]) continue; s.erase(s.find(cnt[v])); int cur = cnt[v] + val, mx = 0; if (!s.empty()) { mx = *s.rbegin(); } for (int to : g[v]) { if (!is[to]) { mx = max(mx, ++cnt[to]); } } for (int to : g[v]) { if (!is[to]) { cnt[to]--; } } ans = max(ans, cur + mx); s.insert(cnt[v]); } cout << ans << ent; for (int i = 1; i <= k; i++) { is[i] = false; g[i].clear(); cnt[i] = 0; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) { solve(); } }
failed
55_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; void ChatGptDeepSeek() // Date: 2025-04-15 { // Time: 15:05:45 int n; cin >> n; vector<char> s(n + 1); for (int i = 1; i <= n; i++) cin >> s[i]; if (s[n] == '0' && s[n - 1] == '0') { cout << "Yes\n"; return; } if (n == 3) { cout << "No\n"; return; } for (int _ = 0; _ < 8; _++) for (int i = 1; i + 4 <= n; i++) if (s[i] == '1' && s[i + 1] == '1' && s[i + 2] == '0') swap(s[i + 1], s[i + 2]); /* 11101111 11011111 */ // if(s[n-3]=='0'&&s[n-2]=='1'&&n>4&&s[n-4]=='1') // swap(s[n-3],s[n-2]); if (s[n - 2] == '1' && s[n - 3] == '1') cout << "Yes\n"; else if (s[n - 3] == '1' && s[n - 2] == '0') { if (s[n - 1] == '0' && s[n] == '1') cout << "No\n"; else cout << "Yes\n"; } else if (s[n - 3] == '0' && s[n - 2] == '0') cout << "No\n"; else if (n > 4 && s[n - 4] == '1') cout << "Yes\n"; else cout << "No\n"; // 1110 ->1010->1100 // 1101 ->1110->1110->1010->1100 // 1111 ->1011->1101->1110->1110->1010->1100 // 1010 ->1100 // 1011 ->1101->1110->1110->1010->1100 // 1001 x // 10101 // 10110 // 10101 // 10110 // 11010 // } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int T = 1; cin >> T; while (T--) ChatGptDeepSeek(); return 0; }
failed
69_2_wrong.cpp
#include "bits/stdc++.h" using namespace std; using ui = unsigned; using db = long double; using ll = long long; using ull = unsigned long long; using lll = __int128; using pii = pair<int, int>; using pll = pair<ll, ll>; template <class T1, class T2> istream &operator>>(istream &cin, pair<T1, T2> &a) { return cin >> a.first >> a.second; } template <std::size_t Index = 0, typename... Ts> typename std::enable_if<Index == sizeof...(Ts), void>::type tuple_read(std::istream &is, std::tuple<Ts...> &t) {} template <std::size_t Index = 0, typename... Ts> typename std::enable_if < Index<sizeof...(Ts), void>::type tuple_read(std::istream &is, std::tuple<Ts...> &t) { is >> std::get<Index>(t); tuple_read<Index + 1>(is, t); } template <typename... Ts> std::istream &operator>>(std::istream &is, std::tuple<Ts...> &t) { tuple_read(is, t); return is; } template <class T1> istream &operator>>(istream &cin, vector<T1> &a) { for (auto &x : a) cin >> x; return cin; } template <class T1> istream &operator>>(istream &cin, valarray<T1> &a) { for (auto &x : a) cin >> x; return cin; } template <class T1, class T2> bool cmin(T1 &x, const T2 &y) { if (y < x) { x = y; return 1; } return 0; } template <class T1, class T2> bool cmax(T1 &x, const T2 &y) { if (x < y) { x = y; return 1; } return 0; } istream &operator>>(istream &cin, lll &x) { x = 0; static string s; cin >> s; for (char c : s) x = x * 10 + (c - '0'); return cin; } ostream &operator<<(ostream &cout, lll x) { static char s[60]; int tp = 1; s[0] = '0' + (x % 10); while (x /= 10) s[tp++] = '0' + (x % 10); while (tp--) cout << s[tp]; return cout; } #if !defined(ONLINE_JUDGE) #else #define dbg(...) ; #define dbgx(...) ; #define dbg1(x) ; #define dbg2(x) ; #define dbg3(x) ; #define DEBUG(msg) ; #define REGISTER_OUTPUT_NAME(Type, ...) ; #define REGISTER_OUTPUT(Type, ...) ; #endif #define all(x) (x).begin(), (x).end() #define print(...) cout << format(__VA_ARGS__) #define err(...) cerr << format(__VA_ARGS__) const int mod1 = 998244353, mod2 = 1e9 + 7; #define MOD1 #ifdef MOD1 const int p = mod1; int fpow(ll x, ll y = mod1 - 2, int m = mod1) { int r = 1; for (; y; y >>= 1, x = (ll)x * x % m) if (y & 1) r = (ll)r * x % m; return r; } #else const int p = mod2; int fpow(ll x, ll y = mod2 - 2, int m = mod2) { int r = 1; for (; y; y >>= 1, x = (ll)x * x % m) if (y & 1) r = (ll)r * x % m; return r; } #endif #define BINOM_ // Notice value of LIM !! #ifdef BINOM_ const int LIM = 1e6 + 5; namespace BINOM { int fac[LIM], inv[LIM], Inv[LIM]; void init() { fac[0] = fac[1] = inv[0] = inv[1] = Inv[0] = 1; for (int i = 2; i < LIM; ++i) fac[i] = (ll)fac[i - 1] * i % p, inv[i] = p - (ll)p / i * inv[p % i] % p; for (int i = 1; i < LIM; ++i) Inv[i] = (ll)Inv[i - 1] * inv[i] % p; } int C(int x, int y) { if (x < 0 || y < 0 || y > x) return 0; return (ll)fac[x] * Inv[y] % p * Inv[x - y] % p; } int _ = (init(), 0); }; // namespace BINOM using BINOM::fac; using BINOM::inv; using BINOM::Inv; #endif const int N = 1 << 20, mod = 998244353; struct NTT { ll re[N], w[2][N], t0[N], t1[N]; int NTT_init(int n) { int len = 1, bit = 0; while (len <= n) len <<= 1, ++bit; for (int i = 1; i < len; ++i) re[i] = (re[i >> 1] >> 1) | ((i & 1) << (bit - 1)); w[0][0] = w[1][0] = 1; w[0][1] = fpow(3, (mod - 1) / len); w[1][1] = fpow(w[0][1]); for (int o = 0; o < 2; ++o) for (int i = 2; i < len; ++i) w[o][i] = w[o][i - 1] * w[o][1] % mod; return len; } void ntt(ll *a, int n, int o = 0) { for (int i = 1; i < n; ++i) if (i < re[i]) swap(a[i], a[re[i]]); int R; for (int k = 1; k < n; k <<= 1) for (int i = 0, t = k << 1, st = n / t; i < n; i += t) for (int j = 0, nw = 0; j < k; ++j, nw += st) { R = a[i + j + k] * w[o][nw] % mod; a[i + j + k] = (a[i + j] - R + mod) % mod; a[i + j] = (a[i + j] + R) % mod; } if (o) { ll t = fpow(n); for (int i = 0; i < n; ++i) a[i] = a[i] * t % mod; } } vector<int> solve(vector<int> &a, vector<int> &b, int n) // return a*b%(x^n) { vector<int> res(n); int len = NTT_init(n); memset(t0, 0, sizeof(ll) * len); for (int i = 0; i < (int)a.size() && i < n; ++i) t0[i] = a[i]; memset(t1, 0, sizeof(ll) * len); for (int i = 0; i < (int)b.size() && i < n; ++i) t1[i] = b[i]; ntt(t0, len); ntt(t1, len); for (int i = 0; i < len; ++i) t0[i] = t0[i] * t1[i] % mod; ntt(t0, len, 1); for (int i = 0; i < n; ++i) res[i] = t0[i]; return res; } } ntt; int f[N], g[N]; void cdqg(int l, int r) { if (l == r) { if (l != 1) g[l] = (fac[l] + p - g[l]) % p; return; } int mid = (l + r) >> 1; cdqg(l, mid); vector<int> a(mid - l + 1), b(r - l + 1); // g for (int i = l; i <= mid; ++i) a[i - l] = g[i]; for (int i = 1; i <= r - l; ++i) b[i] = fac[i]; auto G = ntt.solve(a, b, r - l + 1); for (int i = mid + 1; i <= r; ++i) g[i] = (g[i] + G[i - l]) % p; cdqg(mid + 1, r); } void cdqf(int l, int r) { if (l == r) { if (l != 1) f[l] = (((ll)1 + p - inv[l]) + (ll)Inv[l] * f[l]) % p * fac[l] % p * fpow(fac[l] + p - (ll)g[l]) % p; return; } int mid = (l + r) >> 1; cdqf(l, mid); vector<int> a(mid - l + 1), b(r - l + 1); // f1 for (int i = l; i <= mid; ++i) a[i - l] = (ll)g[i] * f[i] % p; for (int i = 1; i <= r - l; ++i) b[i] = fac[i]; auto F1 = ntt.solve(a, b, r - l + 1); for (int i = mid + 1; i <= r; ++i) f[i] = (f[i] + F1[i - l]) % p; // f2 for (int i = l; i <= mid; ++i) a[i - l] = ((ll)fac[i] * f[i]) % p; for (int i = 1; i <= r - l; ++i) b[i] = g[i]; auto F2 = ntt.solve(a, b, r - l + 1); for (int i = mid + 1; i <= r; ++i) f[i] = (f[i] + F2[i - l]) % p; cdqf(mid + 1, r); } void init(int n) { f[1] = 0; g[1] = 1; cdqg(1, n); cdqf(1, n); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); init(100000); int n; cin >> n; vector<int> a(n); cin >> a; ll ans = 0; for (int i = 0; i < n; ++i) { int l = i, r = i, mi = a[i], ma = a[i]; while (r + 1 < n && (mi != l + 1 || r + 1 != ma)) { ++r; mi = min(mi, a[r]); ma = max(ma, a[r]); } i = r; ans = (ans + 1 + f[r - l + 1]) % p; } cout << ans << "\n"; return 0; }
failed
71_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 50; char a[N][N]; int n, m; int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0}; bool check(int x, int y) { return x > 0 && x <= n && y > 0 && y <= n; } bool ind[N][N][4]; int d[N][N][4]; int dp(int x, int y, int f) { if (d[x][y][f]) return d[x][y][f]; else if (ind[x][y][f]) d[x][y][f] = -1; else { ind[x][y][f] = 1; for (int i = 0; i < 4; i++) { int nx = x + dx[(i + f) % 4], ny = y + dy[(i + f) % 4], nf = (f - 1 + i + 4) % 4; if (!check(nx, ny)) { d[x][y][f] = 1; break; } if (a[nx][ny] == '.') { d[x][y][f] = dp(nx, ny, nf); if (d[x][y][f] != -1) ++d[x][y][f]; break; } } } ind[x][y][f] = 0; // printf("%d %d %d\n",x,y,f); return d[x][y][f]; } int main() { // freopen("a.in","r",stdin); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", a[i] + 1); for (int i = 1; i <= m; i++) { int x, y; char op; scanf("%d%d %c", &x, &y, &op); if (op == 'U') op = 0; else if (op == 'L') op = 1; else if (op == 'D') op = 2; else op = 3; // printf("%d %d %d\n",x,y,op); printf("%d\n", dp(x, y, op)); } return 0; } /* 10 10 ########## #.#.#.#... #.....#.## ####.##..# #....##### #..#.#...# #.#..#.#.# #.#.##.#.# #.#......# ###.###### 2 10 U 2 10 L 4 8 U 10 4 U 8 7 U 8 9 U 6 2 U 5 2 U 3 5 D 7 4 R */
failed
98_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; void test() { int n, m; cin >> n >> m; vector<vector<pair<int, int>>> graf(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; graf[u].push_back({v, i}); graf[v].push_back({u, i}); } vector<int> answer(m); set<pair<int, int>> q; vector<int> countBoundaries(n); vector<vector<int>> boundariesForElement(n); int k; cin >> k; vector<vector<int>> boundaries(k); set<pair<int, int>> boundariesSet; vector<int> countInQueueNeeded(k); for (int i = 0; i < k; i++) { int cnt; cin >> cnt; countInQueueNeeded[i] = cnt; boundaries[i].resize(cnt); boundariesSet.insert({cnt, i}); for (int j = 0; j < cnt; j++) { cin >> boundaries[i][j]; boundaries[i][j]--; boundariesForElement[boundaries[i][j]].push_back(i); countBoundaries[boundaries[i][j]]++; } } int countCompleted = 0; vector<bool> completed(n); vector<bool> inQueue(n); vector<int> distance(n); q.insert({countBoundaries[0], 0}); inQueue[0] = true; for (auto i : boundariesForElement[0]) { boundariesSet.erase({countInQueueNeeded[i], i}); countInQueueNeeded[i]--; boundariesSet.insert({countInQueueNeeded[i], i}); } while (true) { while (countCompleted != n && q.size() != 0 && (*q.begin()).first == 0) { int tr = (*q.begin()).second; q.erase(q.begin()); countCompleted++; completed[tr] = true; distance[tr] = countCompleted; for (auto [prv, index] : graf[tr]) { if (completed[prv]) { answer[index] = distance[tr] - distance[prv]; } if (!inQueue[prv]) { inQueue[prv] = true; q.insert({countBoundaries[prv], prv}); for (auto i : boundariesForElement[prv]) { boundariesSet.erase({countInQueueNeeded[i], i}); countInQueueNeeded[i]--; boundariesSet.insert({countInQueueNeeded[i], i}); } } } } if (boundariesSet.size() != 0 && (*boundariesSet.begin()).first == 0) { int index = (*boundariesSet.begin()).second; boundariesSet.erase(boundariesSet.begin()); if (q.size() != boundaries[index].size()) { countCompleted = 0; break; } for (auto i : boundaries[index]) { assert(inQueue[i]); q.erase({countBoundaries[i], i}); countBoundaries[i]--; q.insert({countBoundaries[i], i}); } } else { break; } } if (countCompleted != n) { printf("No\n"); } else { printf("Yes\n"); for (int i = 0; i < m; i++) { printf("%i%c", answer[i], " \n"[i == m - 1]); } } } int main() { // freopen("in.txt","r",stdin); ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) test(); }
failed
22_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<bool> vb; typedef vector<ld> vd; typedef vector<vector<int>> vvi; typedef vector<vector<ll>> vvl; typedef vector<vector<bool>> vvb; typedef vector<vector<ld>> vvd; // typedef __int128 lll; // typedef __float128 lld; ld pi = acos(-1); ld epsilon = 1e-9; ld inf = 1e18; struct vec2 { ld x, y; vec2(ld _x = 0, ld _y = 0) { x = _x; y = _y; } vec2(pii c) { x = c.first, y = c.second; } vec2(const vec2 &other) { x = other.x; y = other.y; } vec2(const vec2 &a, const vec2 &b) { x = b.x - a.x; y = b.y - a.y; } // creates A to B bool operator==(const vec2 &other) { return x == other.x && y == other.y; } vec2 &operator=(const vec2 &other) { x = other.x; y = other.y; return *this; } vec2 operator-() const { return vec2(-x, -y); } vec2 operator+(const vec2 &other) const { return vec2(x + other.x, y + other.y); } vec2 &operator+=(const vec2 &other) { *this = *this + other; return *this; } vec2 operator-(const vec2 &other) const { return vec2(x - other.x, y - other.y); } vec2 &operator-=(const vec2 &other) { *this = *this - other; return *this; } vec2 operator*(ld other) const { return vec2(x * other, y * other); } vec2 &operator*=(ld other) { *this = *this * other; return *this; } vec2 operator/(ld other) const { return vec2(x / other, y / other); } vec2 &operator/=(ld other) { *this = *this / other; return *this; } ld lengthSq() const { return x * x + y * y; } ld length() const { return sqrt(lengthSq()); } vec2 get_normal() const { return *this / length(); } void normalize() { *this /= length(); } // actually normalizes this vector ld distSq(const vec2 &other) const { return vec2(*this, other).lengthSq(); } ld dist(const vec2 &other) const { return sqrt(distSq(other)); } ld dot(const vec2 &other) const { return x * other.x + y * other.y; } ld cross(const vec2 &other) const { return x * other.y - y * other.x; } ld angle_to(const vec2 &other) const { return acos(dot(other) / length() / other.length()); } vec2 rotate_CCW(ld theta) const { return vec2(x * cos(theta) - y * sin(theta), x * sin(theta) + y * cos(theta)); } // angle from x axis in range (-pi, pi) ld polar_angle() { return atan2(y, x); } // projection of other onto this vec2 project(const vec2 &other) { return *this * (other.dot(*this) / dot(*this)); } friend std::ostream &operator<<(std::ostream &os, const vec2 &v) { os << "[" << v.x << ", " << v.y << "]"; return os; } friend std::istream &operator>>(std::istream &is, vec2 &v) { is >> v.x >> v.y; return is; } }; vec2 operator*(ld a, const vec2 &b) { return vec2(a * b.x, a * b.y); } ld cross(vec2 a, vec2 b) { return a.x * b.y - a.y * b.x; } ld dot(vec2 a, vec2 b) { return a.x * b.x + a.y * b.y; } ld lerp(ld t0, ld t1, ld x0, ld x1, ld t) { ld slope = (x1 - x0) / (t1 - t0); return x0 + slope * (t - t0); } vec2 lerp(ld t0, ld t1, vec2 x0, vec2 x1, ld t) { return vec2(lerp(t0, t1, x0.x, x1.x, t), lerp(t0, t1, x0.y, x1.y, t)); } // p1 + v1 * s = p2 + v2 * t vec2 line_lineIntersect(vec2 p1, vec2 v1, vec2 p2, vec2 v2) { if (cross(v1, v2) == 0) { return {}; } ld s = cross(p2 - p1, v2) / cross(v1, v2); ld t = cross(p1 - p2, v1) / cross(v2, v1); return p1 + v1 * s; } ld tri_area(vec2 t1, vec2 t2, vec2 t3) { vec2 v1 = t1 - t2; vec2 v2 = t2 - t3; return abs(cross(v1, v2) / 2.0); } // returns the distance along the ray from ray_a to the nearest point on the // circle. ld ray_circleIntersect(vec2 ray_a, vec2 ray_b, vec2 center, ld radius) { vec2 ray_dir = (ray_b - ray_a).get_normal(); vec2 to_center = center - ray_a; vec2 center_proj = ray_a + ray_dir * dot(ray_dir, to_center); ld center_proj_len = (center - center_proj).length(); // radius^2 = center_proj_len^2 + int_depth^2 // int_depth = sqrt(radius^2 - center_proj_len^2) ld int_depth = sqrt(radius * radius - center_proj_len * center_proj_len); return dot(ray_dir, to_center) - int_depth; } // sector area of circle ld sector_area(ld theta, ld radius) { return radius * radius * pi * ((theta) / (2.0 * pi)); } ld chord_area(ld theta, ld radius) { return (radius * radius / 2.0) * (theta - sin(theta)); } // dist = distance from center ld chord_area_dist(ld dist, ld radius) { ld theta = acos(dist / radius); return chord_area(theta * 2, radius); } // length of chord ld chord_area_length(ld length, ld radius) { ld theta = asin((length / 2.0) / radius); return chord_area(theta * 2, radius); } // 3 points can uniquely define a circle, just have to find the intersection // between the perpendicular bisectors between two pairs of points. be careful // if the three points are colinear. vec2 threept_circle(vec2 a, vec2 b, vec2 c) { vec2 ab = b - a; vec2 ac = c - a; vec2 abp = {ab.y, -ab.x}; vec2 acp = {ac.y, -ac.x}; return line_lineIntersect(a + ab / 2, abp, a + ac / 2, acp); } // if no intersections found, returns {{inf, inf}, {inf, inf}} // if one circle is contained by other, returns {smaller center, inf} // otherwise, returns the two intersections pair<vec2, vec2> circle_circleIntersect(vec2 c1, ld r1, vec2 c2, ld r2) { // see if intersection even exists if (c1.dist(c2) > r1 + r2) return {{inf, inf}, {inf, inf}}; // see if c1 contains c2 if (r1 - c1.dist(c2) > r2) return {c2, {inf, inf}}; // see if c2 contains c1 if (r2 - c2.dist(c1) > r1) return {c1, {inf, inf}}; // proper intersection. Suppose that c1 is at origin, c2 is on x axis c2 -= c1; c1 = {0, 0}; ld c2ang = c2.polar_angle(); ld c2x = c2.length(); // calc intersection x coordinate. ld intx = (r1 * r1 - r2 * r2 + c2x * c2x) / (2.0 * c2x); ld inty = sqrt(r1 * r1 - intx * intx); // cout << "INTX : " << intx << " " << inty << "\n"; vec2 p1 = {intx, inty}, p2 = {intx, -inty}; p1.rotate_CCW(c2ang), p2.rotate_CCW(c2ang); return {p1, p2}; } // containing or intersecting bool cc_areIntersecting(pii c1, int r1, pii c2, int r2) { int d2 = (c1.first - c2.first) * (c1.first - c2.first) + (c1.second - c2.second) * (c1.second - c2.second); return d2 < (r1 + r2) * (r1 + r2); } // does c1 contain c2 (with some leeway for c1) bool cc_areContaining(vec2 c1, ld r1, vec2 c2, ld r2) { return r1 - c1.dist(c2) + epsilon > r2; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // circle_circleIntersect({0, 0}, 1, {1.999, 0}, 1); // circle_circleIntersect({0, 0}, 1, {1, 0}, 1); // circle_circleIntersect({0, 0}, 1, {1, 0}, 1.9); int t; cin >> t; while (t--) { int n; cin >> n; vector<pii> c(n); vi r(n); set<int> s; for (int i = 0; i < n; i++) cin >> c[i].first >> c[i].second >> r[i]; if (n == 1) { cout << "YES\n"; continue; } // make sure all circles are pairwise intersecting for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (!cc_areIntersecting(c[i], r[i], c[j], r[j])) { cout << "NO\n"; for (int k = 0; k < n; k++) cout << (k == i || k == j ? "1 " : "0 "); cout << "\n"; goto end; } if (cc_areContaining(c[i], r[i], c[j], r[j])) { cout << "NO\n"; for (int k = 0; k < n; k++) cout << (k == j ? "1 " : "0 "); cout << "\n"; goto end; } } } // make sure first 4 are good for (int i = 0; i < min(n, 4); i++) { for (int j = i + 1; j < min(n, 4); j++) { // cout << "IJ : " << i << " " << j << "\n"; pair<vec2, vec2> ret = circle_circleIntersect(c[i], r[i], c[j], r[j]); int i1 = 0, i2 = 0; for (int k = 0; k < min(n, 4); k++) { if (k == i || k == j) continue; if (ret.first.dist(c[k]) < r[k] + epsilon) i1 += (1 << k); if (ret.second.dist(c[k]) < r[k] + epsilon) i2 += (1 << k); } // cout << "I1 I2 : " << i1 << " " << i2 << "\n"; s.insert(i1), s.insert(i2); s.insert(i1 + (1 << i)), s.insert(i2 + (1 << i)); s.insert(i1 + (1 << j)), s.insert(i2 + (1 << j)); s.insert(i1 + (1 << i) + (1 << j)), s.insert(i2 + (1 << i) + (1 << j)); } } if (s.size() != (1 << min(n, 4))) { // cout << "MISSING SOMETHING FROM " << (1 << min(n, 4)) << "\n"; for (int x : s) cout << x << " "; cout << "\n"; cout << "NO\n"; int miss = -1; for (int i = 0; i < (1 << min(n, 4)); i++) { if (!s.count(i)) miss = i; } for (int i = 0; i < min(n, 4); i++) { cout << (miss & (1 << i) ? "1 " : "0 "); } for (int i = 4; i < n; i++) cout << "0 "; cout << "\n"; goto end; } cout << "YES\n"; end: {} } return 0; }
failed
96_3_wrong.cpp
#include <bits/stdc++.h> #define int long long using namespace std; struct Inf { int mx, cnt; bool isl, isr; Inf() : mx(0ll), cnt(1ll), isl(1ll), isr(1ll){}; void init(int v) { mx = v; isl = isr = 1; cnt = 1; } }; Inf operator+(Inf i, Inf j) { Inf res; res.mx = max(i.mx, j.mx); if (i.mx == j.mx) { res.cnt = i.cnt + j.cnt - (i.isr and j.isl); res.isl = i.isl; res.isr = j.isr; } else if (i.mx > j.mx) { res.cnt = i.cnt; res.isl = i.isl; res.isr = 0; } else { res.cnt = j.cnt; res.isl = 0; res.isr = j.isr; } return res; }; struct SegT { #define ls ((cur << 1)) #define rs ((cur << 1) | 1) #define mid ((l + r) >> 1) vector<Inf> d; vector<int> tag; SegT(int n) : d(n << 2), tag(n << 2, 0) {} void pushdown(int cur) { d[ls].init(tag[cur]); d[rs].init(tag[cur]); tag[ls] = tag[rs] = tag[cur]; tag[cur] = 0; } void update(int cur, int l, int r, int L, int R, int v) { if (L <= l and r <= R) { d[cur].init(v); tag[cur] = v; return; } if (tag[cur]) { pushdown(cur); } if (L <= mid) { update(ls, l, mid, L, R, v); } if (R > mid) { update(rs, mid + 1, r, L, R, v); } d[cur] = d[ls] + d[rs]; } Inf query(int cur, int l, int r, int L, int R) { if (L <= l and r <= R) { return d[cur]; } if (tag[cur]) { pushdown(cur); } if (L <= mid and R > mid) { return query(ls, l, mid, L, R) + query(rs, mid + 1, r, L, R); } else if (L <= mid) { return query(ls, l, mid, L, R); } else { return query(rs, mid + 1, r, L, R); } } }; struct SegT2 { // #define ls ((cur << 1)) // #define rs ((cur << 1) | 1) // #define mid ((l + r) >> 1) vector<int> d, tag; SegT2(int n) : d(n << 2, 0), tag(n << 2, 0) {} void pushdown(int cur) { d[ls] = d[rs] = tag[cur]; tag[cur] = 0; } void update(int cur, int l, int r, int L, int R, int v) { if (L <= l and r <= R) { d[cur] = v; tag[cur] = v; return; } if (tag[cur]) { pushdown(cur); } if (L <= mid) { update(ls, l, mid, L, R, v); } if (R > mid) { update(rs, mid + 1, r, L, R, v); } d[cur] = max(d[ls], d[rs]); } int query(int cur, int l, int r, int L, int R) { if (L <= l and r <= R) { return d[cur]; } if (tag[cur]) { pushdown(cur); } if (L <= mid and R > mid) { return max(query(ls, l, mid, L, R), query(rs, mid + 1, r, L, R)); } else if (L <= mid) { return query(ls, l, mid, L, R); } else { return query(rs, mid + 1, r, L, R); } } }; signed main() { std::ios::sync_with_stdio(0); std::cin.tie(0); int n = 15, q; cin >> q; SegT T(n); SegT2 T2(n); vector<array<int, 4>> obj(q); for (auto &[l, r, h, b] : obj) { cin >> l >> r >> h; l += 2, r++; b = T2.query(1, 1, n, l, r); T2.update(1, 1, n, l, r, b + h); } sort(obj.begin(), obj.end(), [&](array<int, 4> i, array<int, 4> j) -> bool { return i[3] < j[3]; }); int ans = 0; for (auto &[l, r, h, b] : obj) { auto val = T.query(1, 1, n, l, r); ans += val.cnt - 1; if (!val.isl) { auto t1 = T.query(1, 1, n, l - 1, l - 1); if (t1.mx >= val.mx) ans++; } if (!val.isr) { auto t2 = T.query(1, 1, n, r + 1, r + 1); if (t2.mx >= val.mx) ans++; } T.update(1, 1, n, l, r, b + h); } cout << ans << '\n'; }
failed
94_1_wrong.cpp
// Created by Sunglassman #include <bits/stdc++.h> #define iv int #define ll long long #define ivi long long #define ivii __int128 #define EL '\n' #define fi first #define se second #define yes std::cout << "YES" << EL #define no std::cout << "NO" << EL constexpr int M = 5e5 + 10; constexpr int INF = 1e9; constexpr ll mod = 998244353; int TSK = 1; #define MULTITESTCASE void init() {} void solve(std::string &tar, std::vector<iv> &sto) { std::string ss; std::cin >> ss; if (ss[0] == 'R' && tar[0] == 'L') { no; return; } if (ss.back() == 'L' && tar.back() == 'R') { no; return; } iv id = ss[0] == 'R'; // std::cout << "ini " << id << EL; iv kls = 1; for (iv i = 1; i < ss.size(); ++i) { if (ss[i] == ss[i - 1]) { ++kls; } else { // std::cout << id << EL; iv ccb = ss[i - 1] == 'R'; while (id < sto.size()) { // std::cout << "keilaoshi " << id << ' ' << sto[id] << ' ' << kls << // EL; if ((id & 1) != ccb || sto[id] < kls) ++id; else break; } if (id >= sto.size()) { no; return; } ++id; kls = 1; } } iv ccb = ss.back() == 'R'; while (id < sto.size()) { // std::cout << "keilaoshi " << id << ' ' << sto[id] << ' ' << kls << EL; if ((id & 1) != ccb || sto[id] < kls) ++id; else break; } if (id >= sto.size()) { no; return; } // std::cout << id << " " << kls << EL; yes; } void tasks() { std::string tar; std::cin >> tar; std::vector<iv> sto; if (tar[0] == 'R') sto.push_back(0); iv cnt = 1; for (iv i = 0; i < tar.size(); ++i) { if (i && tar[i] == tar[i - 1]) { ++cnt; } else if (i && tar[i] != tar[i - 1]) { sto.push_back(cnt); cnt = 1; } } sto.push_back(cnt); // for (iv i = 0; i < sto.size(); ++i) std::cout << sto[i] << ' '; // std::cout << EL; iv q; std::cin >> q; while (q--) { solve(tar, sto); } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); init(); #ifdef MULTITESTCASE std::cin >> TSK; #endif while (TSK--) { tasks(); } return 0; }
failed
44_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using lll = __int128_t; using ld = long double; #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define rep(i, n) for (int i = 0; i < (n); i += 1) #define len(a) ((int)(a).size()) mt19937 rnd(234); const ll inf = 1e18; struct frac { lll a, b; frac() {} frac(lll a_, lll b_) : a(a_), b(b_) { assert(b != 0); if (b < 0) { a *= -1; b *= -1; } } ld get() { return ld(a) / b; } }; bool operator<(const frac &fst, const frac &snd) { return fst.a * snd.b < snd.a * fst.b; } bool operator==(const frac &fst, const frac &snd) { return !((fst < snd) or (snd < fst)); } frac operator-(const frac &fst, const frac &snd) { return frac{fst.a * snd.b - snd.a * fst.b, fst.b * snd.b}; } struct vec { ll x, y; vec() {} vec(ll x_, ll y_) : x(x_), y(y_) {} }; vec operator+(const vec &a, const vec &b) { return vec(a.x + b.x, a.y + b.y); } vec operator-(const vec &a, const vec &b) { return vec(a.x - b.x, a.y - b.y); } ll operator*(const vec &a, const vec &b) { return a.x * b.x + a.y * b.y; } ll operator%(const vec &a, const vec &b) { return a.x * b.y - a.y * b.x; } frac intersect(vec a, vec b) { vec to = a - b; assert(to.y != 0); if (to.x == 0) { return frac{a.x, 1}; } ll numer = a.x * to.y - a.y * to.x; ll denum = to.y; return frac{numer, denum}; } pair<frac, frac> get_seg(frac l, frac r, vec a, vec b) { if (a.x == b.x and a.y == b.y) { return {l, r}; } assert(a.x != b.x or a.y != b.y); if (a.y == b.y) { if (a.x < b.x) { if (a.y >= 0) { return {r, l}; } } else { if (a.y <= 0) { return {r, l}; } } return {l, r}; } frac d = intersect(a, b); d = max(d, l); d = min(d, r); if (a.y < b.y) { return {l, d}; } else { return {d, r}; } } pair<vec, vec> get_tangent(vector<vec> a, vector<vec> b, ll d) { int n = len(a); int m = len(b); pair<vec, vec> opt = {b[0], a[0]}; auto get_opt = [&](pair<vec, vec> fst, pair<vec, vec> snd) { vec bebra = fst.second - fst.first; vec aboba = snd.second - snd.first; if (bebra % aboba * d > 0) { return snd; } else { return fst; } }; rep(i, m) { int cur = 0; for (int bit = 20; bit >= 0; bit -= 1) { int cur1 = ((cur - (1 << bit)) % n + n) % n; int cur2 = (cur + (1 << bit)) % n; if ((a[cur] - b[i]) % (a[cur1] - b[i]) * d > 0) { cur = cur1; } if ((a[cur] - b[i]) % (a[cur2] - b[i]) * d > 0) { cur = cur2; } } opt = get_opt(opt, make_pair(b[i], a[cur])); } return opt; } int32_t main() { if (1) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int t; cin >> t; rep(itr, t) { int n; ll l_int, r_int; cin >> n >> l_int >> r_int; frac l{l_int, 1}, r{r_int, 1}; int k; cin >> k; vector<vec> star(k); rep(i, k) { cin >> star[i].x >> star[i].y; } vector<vector<vec>> moons(n); rep(i, n) { int ki; cin >> ki; moons[i].resize(ki); rep(j, ki) { cin >> moons[i][j].x >> moons[i][j].y; } } vector<pair<frac, frac>> banned; banned.emplace_back(frac{l_int - 1, 1}, l); banned.emplace_back(r, frac{r_int + 1, 1}); rep(i, n) { pair<vec, vec> fst = get_tangent(star, moons[i], 1); pair<vec, vec> snd = get_tangent(star, moons[i], -1); auto [l1, r1] = get_seg(l, r, fst.first, fst.second); auto [l2, r2] = get_seg(l, r, snd.second, snd.first); auto [l3, r3] = get_seg(l, r, fst.first, snd.first); frac curl = max({l1, l2, l3}); frac curr = min({r1, r2, r3}); if (curl < curr) { banned.emplace_back(curl, curr); } } sort(all(banned)); bool found = false; ld result = 0; frac mx = l; for (int i = 0; i + 1 < len(banned); i += 1) { mx = max(mx, banned[i].second); frac lft = mx; frac rght = banned[i + 1].first; if (rght < lft) { continue; } result += (rght - lft).get(); if (lft == rght and (lft == l or rght == r)) { continue; } found = true; } if (!found) { cout << -1 << "\n"; } else { cout << fixed << setprecision(14) << result << "\n"; } } return 0; }
failed
26_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; using i64 = long long; using pii = pair<int, int>; constexpr int maxn = 1000000 + 10; int a[maxn]; vector<pii> l[maxn], r[maxn]; void run(int n, vector<pii> *l) { l[0] = {pii(0, a[0])}; for (int i = 1; i < n; ++i) { vector<pii> u = l[i - 1]; for (auto &[x, y] : u) ++x, y = (y >> 1) + a[i]; u.insert(u.begin(), pii(0, a[i])); l[i].clear(); int lst = -1; for (auto [x, y] : u) if (y != lst) { lst = y; l[i].emplace_back(pii(x, y)); } } for (int i = 0; i < n / 2; ++i) vector<pii>().swap(l[i]); } void solve() { int n; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i], a[i + n] = a[i]; run(n + n, l); reverse(a, a + n + n); run(n + n, r); reverse(a, a + n + n); for (int i = 0; i < n; ++i) { auto &u = l[i + n], &v = r[n + n - 1 - i]; int ans = a[i]; for (auto [x, y] : u) for (auto [p, q] : v) if (x + p < n) ans = max(ans, y + q - a[i]); cout << ans << ' '; } cout << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) solve(); return 0; }
failed
39_3_wrong.cpp
#include <bits/stdc++.h> #define int long long signed main() { std::ios::sync_with_stdio(0); std::cin.tie(0); int n; std::cin >> n; while (n--) { std::string s; std::cin >> s; for (int i = 0; i < s.length(); i += 2) { if (s[i] == '1') { s[i] = '0'; } else if (s[i] == '0') { s[i] = '1'; } } int a = std::count(begin(s), end(s), '0'); int b = std::count(begin(s), end(s), '1'); int c = std::count(begin(s), end(s), '2'); std::cout << std::max(s.length() & 1 ? 1ll : 0ll, std::abs(a - b) - c) << '\n'; } }
failed
100_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int N = 25, mod = 1e9 + 7, M = (1 << 20) + 5; int power(int a, int b) { int res = 1; for (; b; b >>= 1) { if (b & 1) res = 1ll * res * a % mod; a = 1ll * a * a % mod; } return res; } int T, n1, n2, m, k, v1[N], v2[N], C[N][N], f[N], to[N], s1[M], s2[M]; bool valid[M]; bool v[N][N]; void init() { for (int i = 0; i < N; i++) { C[i][0] = 1; for (int j = 1; j <= i; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; } } int getc(int n, int m) { if (n < 0 || m < 0 || n < m) return 0; int res = 1; for (int i = n; i >= n - m + 1; i--) res = 1ll * res * i % mod; for (int i = 1; i <= m; i++) res = 1ll * res * power(i, mod - 2) % mod; return res; } void solve() { scanf("%d %d %d %d", &n1, &n2, &m, &k); for (int i = 1; i <= m; i++) v1[i] = getc(n1 - 1, i - 1), v2[i] = getc(n2 - 1, i - 1); // for(int i=1;i<=m;i++)cout<<v1[i]<<" ";cout<<endl; // for(int i=1;i<=m;i++)cout<<v2[i]<<" ";cout<<endl; for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) v[i][j] = 0; for (int i = 0; i < (1 << m); i++) s1[i] = s2[i] = to[i] = 0; while (k--) { int x, y; scanf("%d %d", &x, &y), x--, y--; v[x][y] = v[y][x] = 1, to[x] |= (1 << y), to[y] |= (1 << x); } for (int i = 0; i < (1 << m); i++) { bool fl = 1; for (int j = 0; j < m; j++) if ((i >> j & 1) && !to[j]) { fl = 0; break; } valid[i] = fl; } for (int i = 0; i < (1 << m); i++) { bool fl = 0; for (int j = 0; j < m; j++) if ((i >> j & 1 ^ 1) && (to[j] & i) != to[j]) fl = 1; if (fl) continue; s1[i] = v1[__builtin_popcount(i)], s2[i] = v2[__builtin_popcount(i)]; } for (int i = 0; i < m; i++) for (int j = 0; j < (1 << m); j++) if (j >> i & 1) s1[j] = (s1[j] + s1[j ^ (1 << i)]) % mod, s2[j] = (s2[j] + s2[j ^ (1 << i)]) % mod; int ans = 0; for (int i = 0; i < (1 << m); i++) if (valid[i]) { if (__builtin_popcount(i) % 2 == 0) { ans = (ans + 1ll * s1[(1 << m) - 1 ^ i] * s2[(1 << m) - 1 ^ i]) % mod; } else { ans = (ans - 1ll * s1[(1 << m) - 1 ^ i] * s2[(1 << m) - 1 ^ i] % mod + mod) % mod; } } printf("%d\n", ans); } int main() { init(); // freopen(".in","r",stdin); scanf("%d", &T); while (T--) solve(); return 0; }
failed
30_2_wrong.cpp
#include <bits/stdc++.h> using LL = long long; using ULL = unsigned long long; using LD = long double; const int MOD = 1e9 + 7; const LL INF = 1e18; const int MAXN = 201000 * 100; struct Node { int l, r; int sum, add; } tr[MAXN]; int idx, rt[1601000]; int n, Q, now; std::vector<int> bin; std::vector<std::array<int, 2>> stk; void push_up(int u) { tr[u].sum = tr[tr[u].l].sum + tr[tr[u].r].sum; } int allocates() { if (bin.size() == 0) return 1; // assert(bin.size()); int t = bin.back(); bin.pop_back(); return t; } void push_down(int u, int l, int r) { if (tr[u].add) { if (!tr[u].l) { assert(bin.size()); tr[u].l = bin.back(); bin.pop_back(); stk.push_back({tr[u].l, now}); } if (!tr[u].r) { assert(bin.size()); tr[u].r = bin.back(); bin.pop_back(); stk.push_back({tr[u].r, now}); } Node &root = tr[u], &left = tr[tr[u].l], &right = tr[tr[u].r]; int mid = l + r >> 1; left.add = 1, left.sum = mid - l + 1; right.add = 1, right.sum = r - mid; root.add = 0; } } void modify(int &u, int l, int r, int pl, int pr, int x) { if (!u) { u = allocates(); stk.push_back({u, now}); } if (l >= pl && r <= pr) { tr[u].sum = (r - l + 1); tr[u].add = 1; return; } int mid = l + r >> 1; push_down(u, l, r); if (pl <= mid) modify(tr[u].l, l, mid, pl, pr, x); if (pr > mid) modify(tr[u].r, mid + 1, r, pl, pr, x); push_up(u); } std::vector<std::vector<std::array<int, 2>>> f(800100); void insert(int p, int l, int r, int x, int y, int u, int v) { if (l >= y || r <= x) { return; } if (l >= x && r <= y) { f[p].push_back({u, v}); return; } int mid = l + r >> 1; insert(p << 1, l, mid, x, y, u, v); insert(p << 1 | 1, mid, r, x, y, u, v); return; } void update(int &u, int v, int p) { if (!v) return; u = allocates(); stk.push_back({u, p}); tr[u] = tr[v]; if (tr[v].l) { update(tr[u].l, tr[v].l, p); } if (tr[v].r) { update(tr[u].r, tr[v].r, p); } } void seg_conquer(int p, int l, int r) { now = p; update(rt[p], rt[p >> 1], p); // std::cout << p << " " << rt[p >> 1] << " " << tr[rt[p >> 1]].sum << " " << // l << " " << r << " " << tr[rt[p]].sum << " tottt \n"; // if(r == Q){ //// std::cout << p << " " << rt[p >> 1] << " " << tr[rt[p >> 1]].sum ///<< " " << l << " " << r << " " << tr[rt[p]].sum << " ssss \n"; // for(auto it : f[p]){ // int L = it[0], R = it[1]; //// std::cout << " it: " << p << " " << L << " " << R << ///"\n"; // } // } for (auto it : f[p]) { int L = it[0], R = it[1]; modify(rt[p], 0, n, L, R, 1); // std::cout << " it: " << p << " " << l << " " << r << " " << L << " " << R // << " " << tr[rt[p]].sum << "\n"; } // std::cout << " after: " << p << " " << tr[rt[p]].sum << "\n"; if (r - l == 1) { std::cout << std::fixed << std::setprecision(20) << std::max(tr[rt[p]].sum, 0) * sqrtl(2) << "\n"; return; } else { int mid = l + r >> 1; seg_conquer(p << 1, l, mid); seg_conquer(p << 1 | 1, mid, r); } while (stk.size() && stk.back()[1] == p) { bin.push_back(stk.back()[0]); stk.pop_back(); } } void solve() { rt[1] = allocates(); std::cin >> Q >> n; std::map<std::array<int, 2>, int> mp; std::vector<int> u(Q), v(Q); for (int i = 0; i < Q; i++) { std::cin >> u[i] >> v[i]; int x = u[i], y = v[i]; if (mp.count({x, y})) { insert(1, 0, Q, mp[{x, y}], i, std::max(0, x - y), std::min(n - 1, x + y - 1)); // std::cout << mp[{x, y}] << " " << i << "\n"; mp.erase({x, y}); } else mp[{x, y}] = i; } for (auto it : mp) { // std::cout << it.first[0] << " " << it.first[1] << "\n"; int x = it.first[0], y = it.first[1]; insert(1, 0, Q, it.second, Q, std::max(0, it.first[0] - it.first[1]), std::min(n - 1, it.first[0] + it.first[1] - 1)); } seg_conquer(1, 0, Q); } int main() { #ifndef ONLINE_JUDGE freopen("main.in", "r", stdin); freopen("main.out", "w", stdout); #endif std::ios::sync_with_stdio(0); std::cin.tie(0), std::cout.tie(0); for (int i = MAXN - 1; i >= 0; i--) bin.push_back(i); int _ = 1; // std::cin >> _; while (_--) { solve(); } return 0; } /* */
failed
106_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef double dou; typedef pair<int, int> pii; #define fi first #define se second #define mapa make_pair typedef long double ld; typedef unsigned long long ull; #define ep emplace_back template <typename T> inline void read(T &x) { x = 0; char c = getchar(); bool f = 0; for (; c < '0' || c > '9'; c = getchar()) f |= (c == '-'); for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); x = (f ? -x : x); } const int N = 4e5 + 5; int T, n; char s[N]; struct SAM { int len[N], fa[N], ch[N][26]; int cnt, lst; int rt[N], idx; int pos[N]; int gen() { ++idx; occ[idx] = false; ls[idx] = rs[idx] = 0; return idx; } int f[N][20]; bool occ[N * 60]; int ls[N * 60], rs[N * 60]; void ins(int &p, int l, int r, int x) { if (!p) p = gen(); occ[p] = true; if (l == r) return; int mid = (l + r) >> 1; if (x <= mid) ins(ls[p], l, mid, x); else ins(rs[p], mid + 1, r, x); } int merge(int p, int q, int l, int r) { if ((!p) || (!q)) return p + q; int np = gen(); if (l == r) { occ[np] = occ[p] | occ[q]; return np; } int mid = (l + r) >> 1; ls[np] = merge(ls[p], ls[q], l, mid); rs[np] = merge(rs[p], rs[q], mid + 1, r); occ[np] = occ[ls[np]] | occ[rs[np]]; return np; } void extend(int c, int lim) { int p = lst, np = lst = ++cnt; if (lim <= n) ins(rt[np], 1, n, lim); pos[lim] = np; len[np] = len[p] + 1; for (; p && !ch[p][c]; p = fa[p]) ch[p][c] = np; if (!p) fa[np] = 1; else { int q = ch[p][c]; if (len[q] == len[p] + 1) fa[np] = q; else { int nq = ++cnt; for (int i = 0; i < 26; ++i) ch[nq][i] = ch[q][i]; fa[nq] = fa[q]; len[nq] = len[p] + 1; fa[q] = fa[np] = nq; for (; p && ch[p][c] == q; p = fa[p]) ch[p][c] = nq; } } } vector<int> e[N]; void init() { for (int i = 1; i <= cnt; ++i) { for (int c = 0; c < 26; ++c) ch[i][c] = 0; fa[i] = len[i] = rt[i] = 0; e[i].clear(); for (int t = 0; t < 20; ++t) f[i][t] = 0; } cnt = lst = 1; idx = 0; } void dfs(int x) { for (int y : e[x]) { f[y][0] = x; for (int i = 1; f[y][i - 1]; ++i) f[y][i] = f[f[y][i - 1]][i - 1]; dfs(y); rt[x] = merge(rt[x], rt[y], 1, n); } } void merge() { for (int i = 2; i <= cnt; ++i) e[fa[i]].ep(i); dfs(1); } int locate(int l, int r) { int x = pos[r]; for (int t = 19; t >= 0; --t) if (len[f[x][t]] >= r - l + 1) x = f[x][t]; return x; } int get(int p, int l, int r, int L, int R) { if (!occ[p]) return 0; if (L <= l && r <= R) { if (l == r) return l; int mid = (l + r) >> 1; if (occ[rs[p]]) return get(rs[p], mid + 1, r, L, R); else return get(ls[p], l, mid, L, R); } int mid = (l + r) >> 1, ret = 0; if (R > mid) ret = get(rs[p], mid + 1, r, L, R); if (ret == 0 && L <= mid) ret = get(ls[p], l, mid, L, R); return ret; } } S; struct PAM { int ch[N][26], fail[N], len[N]; int lst, cnt; void init() { for (int x = 0; x <= cnt; ++x) { fail[x] = len[x] = 0; for (int i = 0; i < 26; ++i) ch[x][i] = 0; } len[0] = 0; len[1] = -1; fail[0] = 1; fail[1] = 0; lst = 0; cnt = 1; } int get_fail(int x, int lim) { while (s[lim - len[x] - 1] != s[lim]) x = fail[x]; return x; } void insert(int c, int lim) { int p = get_fail(lst, lim); if (!ch[p][c]) { ++cnt; len[cnt] = len[p] + 2; int q = get_fail(fail[p], lim); fail[cnt] = ch[q][c]; ch[p][c] = cnt; } lst = ch[p][c]; } } P; int jmp[N]; bool flag = 0; int cnt = 0; void solve() { scanf("%s", s + 1); ++cnt; if (flag && cnt == 242) { printf("%s\n", s + 1); exit(0); } if (flag) return; n = strlen(s + 1); S.init(); for (int i = 1; i <= n; ++i) { S.extend(s[i] - 'a', i); if (s[i] == s[i - 1]) jmp[i] = jmp[i - 1] + 1; else jmp[i] = 1; } for (int i = n; i >= 1; --i) S.extend(s[i] - 'a', 2 * n + 1 - i); S.merge(); P.init(); int mxans = 0; for (int i = 1; i <= n; ++i) { P.insert(s[i] - 'a', i); int len = P.len[P.lst]; // printf("%d %d\n", i, len); int ans = 0; if (len & 1) { int mid = i - len / 2; int cont = jmp[mid] - 1; ans = cont * 2 + len / 2 - cont; } else { int mid = i - len / 2; int cont = jmp[mid]; ans = cont * 2 - 1 + len / 2 - cont; } // printf("working:%d\n", i); int l = i - len + 1; while (true) { // printf("%d %d\n", l, i); if (l - 1 < i - l + 1) break; int node = S.locate(2 * n + 1 - i, 2 * n + 1 - l); int nxtl = S.get(S.rt[node], 1, n, 1, 2 * l - i - 1); if (nxtl == 0) break; l = nxtl; ++ans; }; mxans = max(mxans, ans); } printf("%d\n", mxans); } int main() { // freopen("D:\\nya\\acm\\A\\test.in","r",stdin); // freopen("D:\\nya\\acm\\A\\test.out","w",stdout); read(T); if (T == 500) flag = 1; while (T--) solve(); return 0; }
failed
53_2_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; struct BlockCutTree { int n; std::vector<std::vector<int>> adj; std::vector<int> dfn, low, stk; int cnt, cur; std::vector<std::pair<int, int>> edges; BlockCutTree() {} BlockCutTree(int n) { init(n); } void init(int n) { this->n = n; adj.assign(n, {}); dfn.assign(n, -1); low.resize(n); stk.clear(); cnt = cur = 0; edges.clear(); } void addEdge(int u, int v) { adj[u].push_back(v); adj[v].push_back(u); } void dfs(int x) { stk.push_back(x); dfn[x] = low[x] = cur++; for (auto y : adj[x]) { if (dfn[y] == -1) { dfs(y); low[x] = std::min(low[x], low[y]); if (low[y] == dfn[x]) { int v; do { v = stk.back(); stk.pop_back(); edges.emplace_back(n + cnt, v); } while (v != y); edges.emplace_back(x, n + cnt); cnt++; } } else { low[x] = std::min(low[x], dfn[y]); } } } std::pair<int, std::vector<std::pair<int, int>>> work() { for (int i = 0; i < n; i++) { if (dfn[i] == -1) { stk.clear(); dfs(i); } } return {cnt, edges}; } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int h, w; std::cin >> h >> w; std::vector<std::string> s(h); for (int i = 0; i < h; i++) { std::cin >> s[i]; } std::vector a(h, std::vector<int>(w)); auto idx = [&](int x, int y) { int u = x * w + y + 1; if (u == h * w) { u = 0; } return u; }; std::vector<int> p(h * w); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { std::cin >> a[i][j]; p[idx(i, j)] = a[i][j]; } } BlockCutTree g(h * w); std::vector<bool> emp(h * w); for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (s[i][j] == '.') { emp[idx(i, j)] = true; if (j + 1 < w && s[i][j + 1] == '.') { g.addEdge(idx(i, j), idx(i, j + 1)); } if (i + 1 < h && s[i + 1][j] == '.') { g.addEdge(idx(i, j), idx(i + 1, j)); } } } } auto [cnt, edges] = g.work(); std::vector<int> par(h * w, -1); for (auto [u, v] : edges) { if (v < h * w) { par[v] = u; } } std::vector<int> parity(cnt); for (int i = 1; i < h * w; i++) { if (!emp[i]) { continue; } if (par[i] == -1 || par[i] != par[p[i]]) { std::cout << "impossble\n"; return 0; } } std::vector<bool> vis(h * w); for (int i = 1; i < h * w; i++) { if (!emp[i] || vis[i]) { continue; } int j = i; int len = 0; while (!vis[j]) { vis[j] = true; j = p[j]; len++; } parity[par[i] - h * w] ^= (len - 1) & 1; } if (std::find(parity.begin(), parity.end(), 1) == parity.end()) { std::cout << "possible\n"; } else { std::cout << "impossible\n"; } return 0; }
failed
63_3_wrong.cpp
#include <iostream> #include <vector> using namespace std; constexpr int N = 5e5 + 5, M = 2e6 + 5; int n, m; struct edge { int to, nt; } e[M << 1]; int hd[N], tot = 1; void add(int u, int v) { e[++tot] = edge{v, hd[u]}, hd[u] = tot; } void uadd(int u, int v) { add(u, v), add(v, u); } int len; int dfn[N], low[N], bcc_cnt; int sta[N], top, cnt; bool cut[N]; vector<int> dcc[N]; int root; int vis[N], in[N], vis2[M], dep[N]; void tarjan(int u) { dfn[u] = low[u] = ++bcc_cnt, sta[++top] = u; if (u == root && hd[u] == 0) { dcc[++cnt].push_back(u); return; } int f = 0; for (int i = hd[u]; i; i = e[i].nt) { int v = e[i].to; if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); if (low[v] >= dfn[u]) { if (++f > 1 || u != root) cut[u] = true; cnt++; do dcc[cnt].push_back(sta[top--]); while (sta[top + 1] != v); dcc[cnt].push_back(u); } } else low[u] = min(low[u], dfn[v]); } } void dfs(int u) { vis[u] = 1; for (int i = hd[u]; i; i = e[i].nt) { int v = e[i].to; if (!vis[v]) dep[v] = dep[u] + 1, dfs(v); else len = max(len, dep[u] - dep[v] + 1); } } bool check(int s) { vector<int> v = {s}; int tm = 0; while (v.size()) { vector<int> w; for (int u : v) { int sign = 0; for (int i = hd[u]; i; i = e[i].nt) { int to = e[i].to; if (!in[to]) continue; if (!vis2[i]) { vis2[i] = vis2[i ^ 1] = sign = 1; w.push_back(to); } } if (!sign && len != tm * 2) { return 0; } } v = w; tm++; } return 1; } void sol() { cin >> n >> m; int u, v; for (int i = 1; i <= m; i++) { cin >> u >> v; if (u != v) uadd(u, v); } for (int i = 1; i <= n; i++) { dfs(1); } for (int i = 1; i <= n; i++) if (!dfn[i]) root = i, tarjan(i); if (!len) { cout << "Yes" << endl; return; } if (len & 1) { for (int i = 1; i <= cnt; i++) { if (dcc[i].size() <= 2) { continue; } if (dcc[i].size() != len) { cout << "No" << endl; return; } for (int j : dcc[i]) in[j] = 1; for (int j : dcc[i]) { int d = 0; for (int k = hd[j]; k; k = e[k].nt) { int to = e[k].to; if (!in[to]) continue; d++; } if (d > 2) { cout << "No" << endl; return; } } for (int j : dcc[i]) in[j] = 0; } } else { for (int i = 1; i <= cnt; i++) { if (dcc[i].size() <= 2) continue; for (int j : dcc[i]) in[j] = 1; vector<int> v; for (int j : dcc[i]) { int d = 0; for (int k = hd[j]; k; k = e[k].nt) { int to = e[k].to; if (!in[to]) continue; d++; } if (d > 2) { v.push_back(j); } } if (v.size() > 2) { cout << "No" << endl; return; } else if (v.size() == 2) { if (!check(v[0])) { cout << "No" << endl; return; } } else { if (dcc[i].size() != len) { cout << "No" << endl; return; } } for (int j : dcc[i]) in[j] = 0; } } cout << "Yes" << endl; } int main() { cin.tie(nullptr)->sync_with_stdio(false); int T; cin >> T; while (T--) { sol(); for (int i = 1; i <= cnt; i++) { dcc[i].clear(); } for (int i = 1; i <= n; i++) { hd[i] = dfn[i] = vis[i] = in[i] = cut[i] = 0; } for (int i = 1; i <= tot; i++) { vis2[i] = 0; } tot = 1, bcc_cnt = cnt = len = 0; } return 0; }
failed
70_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define ll long long #define int long long #define ull unsigned long long #define ls (i << 1) #define rs (i << 1 | 1) #define inf (1ll << 60) typedef long long i64; typedef unsigned long long u64; typedef __int128 i128; extern const int mod; struct String { long long n; string a; vector<long long> ne; String(string &s) : n(s.size()), ne(s.size()), a(s) { for (int i = 1; i < n; i++) { int j = ne[i - 1]; while (j && s[j] != s[i]) j = ne[j - 1]; if (s[j] == s[i]) ne[i] = j + 1; } } bool isPalindromic(int i, int j) { while (i < j) if (a[i++] != a[j--]) return false; return true; } // 最小表达法 int min_show() { int i = 0, j = 1, k = 0; while (i < n && j < n && k < n) { if (a[(i + k) % n] == a[(j + k) % n]) k++; else { if (a[(i + k) % n] > a[(j + k) % n]) i += k + 1; else j += k + 1; k = 0; } if (i == j) j++; } return min(i, j); } }; struct BIT { int n; vector<int> d, d2; BIT(int n) : d((n << 1) + 1, 0), d2((n << 1) + 1, 0), n(n << 1){}; int lowbit(int x) { return x & -x; } void add(int x, int k) { for (int i = x; i <= n; i += lowbit(i)) d[i] += k, d2[i] += x * k; } int query(int x) { int ans = 0; for (int i = x; i; i -= lowbit(i)) ans += d[x]; return ans; } int query_range(int x) { int ans = 0; for (int i = x; i; i -= lowbit(i)) ans += d[i] * (x + 1) - d2[i]; return ans; } void add(int l, int r, int k) { add(l, k), add(r + 1, -k); } int query(int l, int r) { return query_range(r) - query_range(l - 1); } }; int qpow(int a, int b) { int res = 1; do if (b & 1) res = res * a % mod; while (a = a * a % mod, b >>= 1); return res; } struct UnionFind { vector<int> pre; vector<int> size; int count; UnionFind(int n) { count = n; pre.resize(n); size.resize(n, 1); for (int i = 0; i < n; i++) { pre[i] = i; } } int find(int x) { if (pre[x] == x) return x; return pre[x] = find(pre[x]); } bool isconnect(int x, int y) { return find(x) == find(y); } bool connect(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (size[x] < size[y]) swap(x, y); pre[y] = x; size[x] += size[y]; count--; return true; } void disconnect(int x) { pre[x] = x; } }; template <int P> struct MInt { int x; constexpr MInt() : x{} {} constexpr MInt(i64 x) : x{norm(x % getMod())} {} static int Mod; constexpr static int getMod() { if (P > 0) { return P; } else { return Mod; } } constexpr static void setMod(int Mod_) { Mod = Mod_; } constexpr int norm(int x) const { if (x < 0) { x += getMod(); } if (x >= getMod()) { x -= getMod(); } return x; } constexpr int val() const { return x; } explicit constexpr operator int() const { return x; } constexpr MInt operator-() const { MInt res; res.x = norm(getMod() - x); return res; } constexpr MInt inv() const { assert(x != 0); return power(*this, getMod() - 2); } constexpr MInt &operator*=(MInt rhs) & { x = 1LL * x * rhs.x % getMod(); return *this; } constexpr MInt &operator+=(MInt rhs) & { x = norm(x + rhs.x); return *this; } constexpr MInt &operator-=(MInt rhs) & { x = norm(x - rhs.x); return *this; } constexpr MInt &operator/=(MInt rhs) & { return *this *= rhs.inv(); } friend constexpr MInt operator*(MInt lhs, MInt rhs) { MInt res = lhs; res *= rhs; return res; } friend constexpr MInt operator+(MInt lhs, MInt rhs) { MInt res = lhs; res += rhs; return res; } friend constexpr MInt operator-(MInt lhs, MInt rhs) { MInt res = lhs; res -= rhs; return res; } friend constexpr MInt operator/(MInt lhs, MInt rhs) { MInt res = lhs; res /= rhs; return res; } friend constexpr std::istream &operator>>(std::istream &is, MInt &a) { i64 v; is >> v; a = MInt(v); return is; } friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) { return os << a.val(); } friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); } friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); } }; template <> int MInt<0>::Mod = 998244353; template <int P> int MInt<P>::Mod = P; template <int V, int P> constexpr MInt<P> CInv = MInt<P>(V).inv(); char Getch() { char ch = getchar(); while ((!isalpha(ch)) && (!isdigit(ch))) ch = getchar(); return ch; } const int N = 1e5 + 7, mod = 998244353; using Z = MInt<mod>; mt19937_64 rng(time(nullptr)); const int Mod = 1e9 + 7; void init() {} void solve() { int n; cin >> n; vector<int> a(n); int s = 0; for (int i = 0; i < n; i++) cin >> a[i], s += a[i]; s /= 2; sort(a.begin(), a.end()); int ans = 0; auto dfs = [&](auto &&dfs, int pre, int sum, int x) -> void { if (sum - x > s) return; if (sum > s && sum - x <= s) ans++; if (pre >= n) return; for (int i = pre; i < n; i++) { dfs(dfs, i + 1, sum + a[i], x); } return; }; for (int i = 0; i < n; i++) { dfs(dfs, i + 1, a[i], a[i]); // cout<<i<<' '<<ans<<'\n'; } cout << ans << "\n"; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); init(); ll t = 1; // cin >> t; while (t--) solve(); return 0; }
failed
60_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; string pattern = ""; const int mod = 998244353; __int128 pw10[29], pref[29]; template <typename T> inline string ToString(T x) { if (!x) return "0"; string t = ""; while (x) { t += (char)(x % 10 + '0'); x /= 10; } reverse(t.begin(), t.end()); return t; } inline __int128 ToInteger(string x) { __int128 num = 0; int l = x.length(), p = 0; for (int i = p; i < l; i++) num = (num << 1) + (num << 3) + (x[i] ^ 48); return num; } inline __int128 calc(__int128 val) { int dig = 0; for (int i = 0; i <= 27; i++) if (pw10[i] <= val) dig = i; return pref[dig] + (dig + 1) * (val - pw10[dig]) + 1; } inline void solve() { string s; cin >> s; int n = s.length(), m = pattern.length(); __int128 ans = 1e36; // BruteForce Matching for (int i = 0; i <= m - n; i++) { bool match = true; for (int j = 0; j < n; j++) if (s[j] != '?') match &= (s[j] == pattern[i + j]); if (match) { ans = i + 1; break; } } // Crossing 10-power number for (int t = 5; t <= 27; t++) { auto x = pw10[t]; string npattern = ""; int c = 0; for (auto i = x - 6; i <= x + 6; i++) { if (i == x) c = npattern.length(); npattern += ToString(i); } int l = npattern.size(); for (int i = 0; i <= l - n; i++) { bool match = true; for (int j = 0; j < n; j++) if (s[j] != '?') match &= (s[j] == npattern[i + j]); if (match) { ans = min(ans, pref[t] + (i - c) + 1); break; } } } // Only One Number string u = s; int more = 0; if (u[0] == '0') u = '1' + u, more = 1; else if (u[0] == '?') u[0] = '1'; for (auto &ch : u) if (ch == '?') ch = '0'; __int128 val = ToInteger(u); ans = min(ans, calc(val) + more); // At Least 2 Numbers for (int st = 0; st < n; st++) for (int len = 5, ed = st + len - 1; len <= n; len++, ed++) { vector<int> val[len]; string w = s, v = w; while (w.size() <= ed) w.push_back('?'); for (int i = 0; i <= max(n - 1, ed); i++) val[((i - st) % len + len) % len].push_back(i); auto completeHigh = [&](const int &up) -> bool { for (int i = 0; i <= up; i++) { int dig = (!i); int prep = -1; for (auto pos : val[i]) if (v[pos] != '?') prep = (v[pos] - '0'); if (!i && !prep) return false; if (prep == -1) prep = dig; bool flg = true; for (auto pos : val[i]) { if (v[pos] == '?') v[pos] = (char)(prep + '0'); else flg &= (v[pos] - '0' == prep); } if (!flg) return false; } return true; }; auto completeLow = [&](bool carry) -> bool { int sp = ed, sv = 9; bool already = false; for (auto pos : val[len - 1]) if (v[pos] != '?') sp = pos, sv = v[pos] - '0', already = true; while (sp >= len) sp -= len, sv--; if (sv < 0 && !carry) return false; if (!already) sv = 0; bool flg = true; for (auto pos : val[len - 1]) { if (v[pos] == '?') v[pos] = (char)(((sv + 10) % 10) + '0'); else flg &= (v[pos] - '0' == ((sv + 10) % 10)); sv++; } if (sv >= 10) return false; return flg; }; // If no carry v = w; if (completeHigh(len - 2) && completeLow(0)) { __int128 val = ToInteger(v.substr(st, len)); ans = min(ans, calc(val) - st); } // If carry v = w; if (v[ed] != '0' && v[ed] != '?') continue; for (int carry = 2; carry <= len; carry++) { v = w; v[ed] = '0'; if (!completeLow(1)) continue; if (len > carry && !completeHigh(len - carry - 1)) continue; bool flg = true; for (int d = 1; d < carry - 1; d++) { int pos = ed - d; while (pos <= max(ed, n - 1)) { flg &= (v[pos] == '0' || v[pos] == '?'); v[pos] = '0'; pos += len; } pos = ed - d - len; while (pos >= 0) { flg &= (v[pos] == '9' || v[pos] == '?'); v[pos] = '9'; pos -= len; } } if (!flg) continue; int pos = ed - (carry - 1); int sp = -1, ss = -1; while (pos <= max(ed, n - 1)) { if (v[pos] != '?') ss = (v[pos] - '0'); pos += len; } pos = ed - (carry - 1) - len; while (pos >= 0) { if (v[pos] != '?') sp = (v[pos] - '0'); pos -= len; } if (sp != -1 && ss != -1 && ss != sp + 1) continue; if (sp != -1) ss = sp + 1; else if (ss != -1) sp = ss - 1; else sp = 0, ss = 1; if (sp < 0 || ss >= 10) continue; pos = ed - (carry - 1); while (pos <= max(ed, n - 1)) { flg &= (v[pos] - '0' == ss || v[pos] == '?'); v[pos] = (ss + '0'); pos += len; } pos = ed - (carry - 1) - len; while (pos >= 0) { flg &= (v[pos] - '0' == sp || v[pos] == '?'); v[pos] = (sp + '0'); pos -= len; } if (!flg) continue; __int128 val = ToInteger(v.substr(st, len)); ans = min(ans, calc(val) - st); } } long long result = ans % mod; printf("%lld\n", result); } int main() { for (int i = 1; i <= 15000; i++) pattern += ToString(i); pw10[0] = 1; for (int i = 1; i <= 28; i++) { pw10[i] = pw10[i - 1] * 10; pref[i] = pref[i - 1] + (pw10[i] - pw10[i - 1]) * i; } int t; scanf("%d", &t); while (t--) solve(); return 0; }
failed
100_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int N = 2E5 + 7; typedef unsigned long long ull; ull S, val[N]; map<ull, int> s; int n, m, k, tot, T, a[N], l[N], st[N], buc[N], vis[N], dis[N]; struct EDGE { int u, v; } e[N]; struct edge { int to, next; } g[N * 2]; priority_queue<int> q; void add(int u, int v) { g[++tot] = (edge){v, st[u]}, st[u] = tot; } void remove(int x) { for (int i = l[x - 1]; i < l[x]; ++i) if (!--buc[a[i]]) q.push(a[i]); } int main() { srand((unsigned)time(NULL)); for (int i = 1; i < N; ++i) val[i] = 1ull * rand() * rand() * rand() * rand(); for (scanf("%d", &T); T--;) { scanf("%d%d", &n, &m), tot = 0; for (int i = 1; i <= n; ++i) st[i] = vis[i] = dis[i] = buc[i] = 0; for (int i = 1, u, v; i <= m; ++i) scanf("%d%d", &e[i].u, &e[i].v), add(e[i].u, e[i].v), add(e[i].v, e[i].u); scanf("%d", &k), s.clear(), l[0] = 1; for (int i = 1, x; i <= k; ++i) { l[i] = l[i - 1], scanf("%d", &x), S = 0; for (int j = 1; j <= x; ++j) scanf("%d", a + l[i]), ++buc[a[l[i]]], S ^= val[a[l[i]++]]; s[S] = i; } S = val[1]; if (s.count(S)) remove(s[S]), s.erase(S); while (q.size()) q.pop(); q.push(1); for (int x, cnt = 0; q.size();) { x = q.top(), q.pop(); dis[x] = ++cnt, S ^= val[x], vis[x] = 1; for (int i = st[x], v; i; i = g[i].next) if (!vis[v = g[i].to]) { S ^= val[v], vis[v] = 1; if (!buc[v]) q.push(v); } if (s.count(S)) remove(s[S]), s.erase(S); } if (s.size() == 0) { printf("Yes\n"); for (int i = 1; i <= m; ++i) printf("%d%c", abs(dis[e[i].u] - dis[e[i].v]), i == m ? '\n' : ' '); } else printf("No\n"); } return 0; }
failed
22_2_wrong.cpp
// This is a wrong code file #include <cstring> #include <iostream> #include <vector> using namespace std; using LL = long long; int main() { #ifdef LOCAL freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); auto check = [&](string &s) { if (s.size() >= 2 and s[0] == '0') return false; return true; }; auto check2 = [&](int t1, string &t2) { if (t1 == 1) return t2.size() == 3; return t2.size() != 3; }; int T, m; cin >> T >> m; while (T--) { string s; cin >> s; const int n = s.size(); // 枚举过题数量, 罚时数量 vector<string> p; for (int i = 0; i < n; i++) { int j = i; while (j + 1 < n and isdigit(s[j]) == isdigit(s[j + 1])) j++; p.push_back(s.substr(i, j - i + 1)); i = j; } if (p.size() == 1) { cout << 0 << ' ' << 0 << '\n'; continue; } auto solve = [&]() { bool ok = false; for (int len1 = 1; len1 <= 2 and !ok; len1++) { string t1 = p[0].substr(0, len1); if (!check(t1)) continue; int cnt = stoi(t1); if (cnt > m) continue; for (int len2 = 1; len1 + len2 < p[0].size() and !ok; len2++) { string t2 = p[0].substr(len1, len2); if (!check(t2)) continue; int all = stoi(t2); auto bk = p[0]; p[0] = p[0].substr(len1 + len2); vector<int> a(p.size()); auto dfs = [&](auto &&dfs, int u, int solve_cnt, int ps) -> void { if (ok) return; if (solve_cnt > cnt or ps > all) return; if (u >= p.size()) { if (solve_cnt == cnt and ps == all) { ok = true; cout << cnt << ' ' << all << ' '; for (int i = 0; i < p.size(); i += 2) { if (p[i].size() == a[i]) { cout << p[i] << ' ' << p[i + 1] << " \n"[i + 2 == p.size()]; } else { cout << p[i].substr(0, p[i].size() - a[i]) << ' ' << p[i].substr(p[i].size() - a[i]) << ' ' << p[i + 1] << " \n"[i + 2 == p.size()]; } } return; } return; } for (int i = 1; i <= 3 and i <= p[u].size(); i++) { if (ok) return; string t1 = p[u].substr(p[u].size() - i); if (!check(t1)) continue; int num = stoi(t1); if (num < 1 or num > 100 or !check2(num, p[u + 1])) continue; if (i == p[u].size()) { a[u] = i; dfs(dfs, u + 2, solve_cnt, ps); } else { string t2 = p[u].substr(0, p[u].size() - i); if (!check(t2)) continue; int tim = stoi(t2); if (tim < 0 or tim > 299) continue; a[u] = i; dfs(dfs, u + 2, solve_cnt + 1, ps + tim + (num - 1) * 20); } } }; dfs(dfs, 0, 0, 0); p[0] = bk; } } }; solve(); } }
failed
16_2_wrong.cpp
#include <bits/stdc++.h> #define int ll using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define per(i, n) for (int i = n - 1; i >= 0; i--) #define rng(i, c, n) for (int i = c; i < n; i++) #define fi first #define se second #define pb push_back #define sz(a) (int)a.size() #define all(a) a.begin(), a.end() #define vec(...) vector<__VA_ARGS__> typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; void print() { cout << '\n'; } template <class h, class... t> void print(const h &v, const t &...u) { cout << v << ' ', print(u...); } typedef __int128 Int; const int inf = 1e18; void slv() { int n; cin >> n; vi a(n); rep(i, n) { cin >> a[i]; } vi b(n); rep(i, n) { cin >> b[i]; } vi c(n); rep(i, n) { cin >> c[i]; } sort(all(c)); auto af = [&](int med) { vi dt(n); set<int> lhs; int ret = 0; rep(i, n) { // a[i] * x + b[i] >= med // a[i] * x >= med - b[i] if (a[i] < 0) { int x = (med - b[i]) / a[i]; // c[i] <= x int j = upper_bound(all(c), x) - c.begin(); // print(a[i]*c[j]+b[i]); while (j < n and j >= 0 and (Int)(a[i]) * (Int)c[j] + (Int)b[i] < (Int)med) { j -= 1; } // print(j); if (j >= 0) { lhs.insert(j); } } else if (a[i] > 0) { int x = (med - b[i] + a[i] - 1) / a[i]; // c[i] >= x int j = lower_bound(all(c), x) - c.begin(); // print(med-b[i]); if (j < n) { dt[j] += 1; } } else { if (b[i] >= med) { ret += 1; } } } rep(i, n) { if (i) { dt[i] += dt[i - 1]; } if (sz(lhs) and *lhs.begin() <= i) { ret += 1; lhs.erase(lhs.begin()); } else { if (dt[i]) { ret += 1; dt[i] -= 1; } } } // print(ret); return ret >= (n + 1) / 2; }; // print(af(114614)); int l = -inf, r = inf; int opt = l; while (l <= r) { int m = (l + r) / 2; if (af(m)) { opt = m, l = m + 1; } else { r = m - 1; } } cout << opt << "\n"; } signed main() { ios::sync_with_stdio(0), cin.tie(0); int t; cin >> t; rep(cs, t) { slv(); } }
failed
72_3_wrong.cpp
#include <bits/stdc++.h> #define int long long using namespace std; typedef pair<int, int> pii; const int N = 1e6 + 10; int n, m, c; string s; pii ans[N]; bool vis1[N], vis2[N]; bool cmp(pii a, pii b) { if (a.first == b.first) return a.second < b.second; return a.first > b.first; // return a.second>b.second; } void solve() { int n, m, q; cin >> n >> m >> q; int contest_id = 0; memset(vis1, 0, sizeof vis1); memset(vis2, 0, sizeof vis2); int idx = m; for (int i = 1; i <= m; i++) { ans[i].first = 0; ans[i].second = i; } while (q--) { int op; cin >> op; if (op == 1) { idx = m; int id; cin >> id; if (vis1[id]) continue; contest_id = id; vis1[id] = 1; memset(vis2, 0, sizeof vis2); } if (op == 2) { int id, x; cin >> id >> x; if (x != contest_id) continue; if (vis2[id]) continue; vis2[id] = 1; ans[id].first += idx--; } if (op == 3) { int id, x; cin >> id >> x; if (x != contest_id) continue; if (vis2[id]) continue; vis2[id] = 1; } } sort(ans + 1, ans + 1 + m, cmp); for (int i = 1; i <= m; i++) cout << ans[i].second << ' ' << ans[i].first << endl; return; } signed main() { int T; cin >> T; while (T--) solve(); return 0; }
failed
9_3_wrong.cpp
#include <algorithm> #include <cstring> #include <iostream> #include <map> #include <queue> #define int long long // #define endl "\n" using namespace std; const int N = 1e4 + 5, M = 1e5 + 5; int n, m, s, t; int hea[N], nex[M], ve[M], we[M], cou[M], ge = -1; int dis[N], hu[N], cha[N]; bool vis[N]; void add(int u, int v, int w, int c) { ve[++ge] = v, we[ge] = w, cou[ge] = c, nex[ge] = hea[u], hea[u] = ge; ve[++ge] = u, we[ge] = 0, cou[ge] = -c, nex[ge] = hea[v], hea[v] = ge; // cout<<u<<" "<<v<<" "<<w<<" "<<c<<endl; } bool spfa() { queue<int> q; memset(dis, 0x3f, sizeof(dis)); memset(vis, false, sizeof(vis)); int x = dis[s]; dis[s] = 0; q.push(s); hu[s] = hea[s]; vis[s] = true; while (!q.empty()) { int u = q.front(); vis[u] = false; q.pop(); for (int i = hea[u]; ~i; i = nex[i]) { int v = ve[i], w = cou[i]; if (dis[v] > dis[u] + w && we[i]) { dis[v] = dis[u] + w; hu[v] = hea[v]; if (!vis[v]) q.push(v), vis[v] = true; } } } return dis[t] != x; } int fei; int found(int u, int lim) { if (u == t) return lim; int flow = 0; vis[u] = true; // cout<<u<<endl; for (int i = hu[u]; ~i && flow < lim; i = nex[i]) { // cout<<i<<endl; hu[u] = i; int v = ve[i]; if (dis[v] == dis[u] + cou[i] && we[i] && !vis[v]) { int liu = found(v, min(we[i], lim - flow)); if (!liu) dis[v] = -1; else fei += cou[i] * liu, we[i] -= liu, we[i ^ 1] += liu, flow += liu; } } return flow; } int dinic() { int sum = 0, b = 0; while (spfa()) { while (b = found(s, 1e18)) sum += b; } return fei; } int a[N], b[M], top; map<int, int> mp; int calc(int x) { if (x == 1) return 0; if (mp[x]) return mp[x]; for (int i = 2; i * i <= x; i++) { if (x % i == 0) return mp[x] = calc(x / i) + 1; } return mp[x] = 1; } signed main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); memset(hea, -1, sizeof(hea)); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { for (int j = 1; j * j <= a[i]; j++) { if (a[i] % j) continue; b[++top] = j; if (j * j != a[i]) b[++top] = a[i] / j; } } sort(b + 1, b + 1 + top); top = unique(b + 1, b + 1 + top) - b - 1; s = 0, t = top + n + 1; for (int i = 1; i <= n; i++) { add(s, i, 1, 0); for (int j = 1; j <= top; j++) { if (a[i] % b[j] == 0) add(i, j + n, 1, -calc(a[i] / b[j])); } } for (int i = 1; i <= top; i++) add(i + n, t, 1, 0); cout << -dinic() << endl; }
failed
10_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod = 998244353; ll fWp[1005][1005], fWs[1005][1005], resW[1005]; ll fEp[1005][1005], fEs[1005][1005], resE[1005]; ll fNp[1005][1005], fNs[1005][1005], resN[1005]; ll fSp[1005][1005], fSs[1005][1005], resS[1005]; char s[1005][1005]; int limW[1005][2], limE[1005][2], limN[1005][2], limS[1005][2]; bool canc[1005], canr[1005]; ll hc[1005][2], hr[1005][2]; ll add(ll x, ll y) { return (x += y) >= mod ? x - mod : x; } ll fpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans; } void work() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> s[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (s[i][j] == '?') continue; if (s[i][j] == 'N') { int x = i; while (x > 1 && s[x - 1][j] == '?') s[x - 1][j] = 'N', x--; if (x > 1 && s[x - 1][j] != 'N') return cout << "0\n", void(); } if (s[i][j] == 'S') { int x = i; while (x < n && s[x + 1][j] == '?') s[x + 1][j] = 'S', x++; if (x < n && s[x + 1][j] != 'S') return cout << "0\n", void(); } if (s[i][j] == 'W') { int x = j; while (x > 1 && s[i][x - 1] == '?') s[i][x - 1] = 'W', x--; if (x > 1 && s[i][x - 1] != 'W') return cout << "0\n", void(); } if (s[i][j] == 'E') { int x = j; while (x < m && s[i][x + 1] == '?') s[i][x + 1] = 'E', x++; if (x < m && s[i][x + 1] != 'E') return cout << "0\n", void(); } } for (int i = 1; i <= max(n, m); i++) canc[i] = canr[i] = 0; hc[0][0] = hr[0][0] = hc[m + 1][1] = hr[n + 1][1] = 1; for (int i = 1; i <= m; i++) { limN[i][0] = 0, limN[i][1] = n; for (int j = 1; j <= n; j++) if (s[j][i] == 'N') limN[i][0] = max(limN[i][0], j); else if (s[j][i] != '?') limN[i][1] = min(limN[i][1], j - 1); if (limN[i][0] > limN[i][1]) return cout << "0\n", void(); limS[i][0] = 0, limS[i][1] = n; for (int j = 1; j <= n; j++) if (s[n - j + 1][i] == 'S') limS[i][0] = max(limS[i][0], j); else if (s[n - j + 1][i] != '?') limS[i][1] = min(limS[i][1], j - 1); if (limS[i][0] > limS[i][1]) return cout << "0\n", void(); if (limN[i][0] + limS[i][0] > n) return cout << "0\n", void(); bool F = 1; for (int j = 1; j <= n; j++) F &= (s[j][i] != 'W' && s[j][i] != 'E'); if (!F) hc[i][0] = hc[i - 1][1] = 1, canc[i] = 1; else hc[i][0] = hc[i - 1][1] = n - limN[i][0] - limS[i][0] + 1; } for (int i = 1; i <= n; i++) { limW[i][0] = 0, limW[i][1] = m; for (int j = 1; j <= m; j++) if (s[i][j] == 'W') limW[i][0] = max(limW[i][0], j); else if (s[i][j] != '?') limW[i][1] = min(limW[i][1], j - 1); if (limW[i][0] > limW[i][1]) return cout << "0\n", void(); limE[i][0] = 0, limE[i][1] = m; for (int j = 1; j <= m; j++) if (s[i][m - j + 1] == 'E') limE[i][0] = max(limE[i][0], j); else if (s[i][m - j + 1] != '?') limE[i][1] = min(limE[i][1], j - 1); if (limE[i][0] > limE[i][1]) return cout << "0\n", void(); if (limW[i][0] + limE[i][0] > m) return cout << "0\n", void(); bool F = 1; for (int j = 1; j <= m; j++) F &= (s[i][j] != 'N' && s[i][j] != 'S'); if (!F) hr[i][0] = hr[i - 1][1] = 1, canr[i] = 1; else hr[i][0] = hr[i - 1][1] = m - limW[i][0] - limE[i][0] + 1; } for (int i = 1; i <= n; i++) hr[i][0] = hr[i - 1][0] * hr[i][0] % mod; hr[n][1] = fpow(hr[n][0], mod - 2); for (int i = n - 1; ~i; i--) hr[i][1] = hr[i + 1][1] * hr[i][1] % mod; for (int i = 1; i <= m; i++) hc[i][0] = hc[i - 1][0] * hc[i][0] % mod; hc[m][1] = fpow(hc[m][0], mod - 2); for (int i = m - 1; ~i; i--) hc[i][1] = hc[i + 1][1] * hc[i][1] % mod; for (int i = 0; i <= n; i++) fNp[0][i] = fSp[0][i] = 1; for (int i = 1; i <= m; i++) { for (int j = 0; j < limN[i][0]; j++) fNp[i][j] = 0; for (int j = limN[i][0]; j <= limN[i][1]; j++) fNp[i][j] = add(fNp[i][j - 1], fNp[i - 1][j]); for (int j = limN[i][1] + 1; j <= n; j++) fNp[i][j] = fNp[i][j - 1]; for (int j = 0; j < limS[i][0]; j++) fSp[i][j] = 0; for (int j = limS[i][0]; j <= limS[i][1]; j++) fSp[i][j] = add(fSp[i][j - 1], fSp[i - 1][j]); for (int j = limS[i][1] + 1; j <= n; j++) fSp[i][j] = fSp[i][j - 1]; } for (int i = 0; i <= n; i++) fNs[m + 1][i] = fSs[m + 1][i] = 1; for (int i = m; i; i--) { for (int j = 0; j < limN[i][0]; j++) fNs[i][j] = 0; for (int j = limN[i][0]; j <= limN[i][1]; j++) fNs[i][j] = add(fNs[i][j - 1], fNs[i + 1][j]); for (int j = limN[i][1] + 1; j <= n; j++) fNs[i][j] = fNs[i][j - 1]; for (int j = 0; j < limS[i][0]; j++) fSs[i][j] = 0; for (int j = limS[i][0]; j <= limS[i][1]; j++) fSs[i][j] = add(fSs[i][j - 1], fSs[i + 1][j]); for (int j = limS[i][1] + 1; j <= n; j++) fSs[i][j] = fSs[i][j - 1]; } for (int i = 0; i <= m; i++) fWp[0][i] = fEp[0][i] = 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < limW[i][0]; j++) fWp[i][j] = 0; for (int j = limW[i][0]; j <= limW[i][1]; j++) fWp[i][j] = add(fWp[i][j - 1], fWp[i - 1][j]); for (int j = limW[i][1] + 1; j <= n; j++) fWp[i][j] = fWp[i][j - 1]; for (int j = 0; j < limE[i][0]; j++) fEp[i][j] = 0; for (int j = limE[i][0]; j <= limE[i][1]; j++) fEp[i][j] = add(fEp[i][j - 1], fEp[i - 1][j]); for (int j = limE[i][1] + 1; j <= n; j++) fEp[i][j] = fEp[i][j - 1]; } for (int i = 0; i <= m; i++) fWs[n + 1][i] = fEs[n + 1][i] = 1; for (int i = n; i; i--) { for (int j = 0; j < limW[i][0]; j++) fWs[i][j] = 0; for (int j = limW[i][0]; j <= limW[i][1]; j++) fWs[i][j] = add(fWs[i][j - 1], fWs[i + 1][j]); for (int j = limW[i][1] + 1; j <= n; j++) fWs[i][j] = fWs[i][j - 1]; for (int j = 0; j < limE[i][0]; j++) fEs[i][j] = 0; for (int j = limE[i][0]; j <= limE[i][1]; j++) fEs[i][j] = add(fEs[i][j - 1], fEs[i + 1][j]); for (int j = limE[i][1] + 1; j <= n; j++) fEs[i][j] = fEs[i][j - 1]; } ll ans = 0; for (int i = 0; i <= n; i++) { if (!i) resN[i] = fNs[1][0], resS[i] = fSs[1][0]; else { resN[i] = resS[i] = 0; for (int j = 1; j <= m; j++) if (limN[j][0] <= i && i <= limN[j][1]) (resN[i] += fNp[j - 1][i - 1] * fNs[j + 1][i]) %= mod; for (int j = 1; j <= m; j++) if (limS[j][0] <= i && i <= limS[j][1]) (resS[i] += fSp[j - 1][i - 1] * fSs[j + 1][i]) %= mod; } resS[i] = (resS[i - 1] + resS[i] * hr[n - i][0]) % mod; } int lst = (canr[n] ? n : n + 1); for (int i = n - 1; ~i; i--) { (ans += resN[i] * hr[i][1] % mod * (resS[n - i - 1] + mod - (lst == n + 1 ? 0 : resS[n - lst]))) %= mod; if (canr[i]) lst = i; } for (int i = 0; i <= m; i++) { if (!i) resW[i] = fWs[1][0], resE[i] = fEs[1][0]; else { resW[i] = resE[i] = 0; for (int j = 1; j <= n; j++) if (limW[j][0] <= i && i <= limW[j][1]) (resW[i] += fWp[j - 1][i - 1] * fWs[j + 1][i]) %= mod; for (int j = 1; j <= n; j++) if (limE[j][0] <= i && i <= limE[j][1]) (resE[i] += fEp[j - 1][i - 1] * fEs[j + 1][i]) %= mod; } resE[i] = (resE[i - 1] + resE[i] * hc[m - i][0]) % mod; } lst = (canc[m] ? m : m + 1); for (int i = m - 1; ~i; i--) { (ans += resW[i] * hc[i][1] % mod * (resE[m - i - 1] + mod - (lst == m + 1 ? 0 : resE[m - lst]))) %= mod; if (canc[i]) lst = i; } for (int x = 1; x < n; x++) { ll sum = 0; for (int i = m; i; i--) { if (limN[i][0] <= n - x && n - x <= limN[i][1]) (ans += sum * fNp[i - 1][n - x] % mod * fNs[i + 1][n - x - 1]) %= mod; if (limS[i][0] <= x && x <= limS[i][1]) (sum += fSp[i - 1][x - 1] * fSs[i + 1][x]) %= mod; } sum = 0; for (int i = 1; i <= m; i++) { if (limN[i][0] <= n - x && n - x <= limN[i][1]) (ans += sum * fNp[i - 1][n - x - 1] % mod * fNs[i + 1][n - x]) %= mod; if (limS[i][0] <= x && x <= limS[i][1]) (sum += fSp[i - 1][x] * fSs[i + 1][x - 1]) %= mod; } } for (int x = 1; x < m; x++) { ll sum = 0; for (int i = 1; i < n; i++) { if (limW[i + 1][0] <= m - x && m - x <= limW[i + 1][1]) (ans += sum * fWp[i][m - x - 1] % mod * fWs[i + 2][m - x]) %= mod; if (limE[i][0] <= x && x <= limE[i][1]) (sum += fEp[i - 1][x] * fEs[i + 1][x - 1]) %= mod; } sum = 0; for (int i = n; i > 1; i--) { if (limW[i - 1][0] <= m - x && m - x <= limW[i - 1][1]) (ans += sum * fWp[i - 2][m - x] % mod * fWs[i][m - x - 1]) %= mod; if (limE[i][0] <= x && x <= limE[i][1]) (sum += fEp[i - 1][x - 1] * fEs[i + 1][x]) %= mod; } } cout << ans << "\n"; } int main() { ios::sync_with_stdio(false), cin.tie(0); int T; cin >> T; while (T--) work(); return 0; }
failed
14_3_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; /* 在线区间颜色种类数计数 先写个暴力 就是说,每行,数字对应的标号要连边 难绷,是tarjan缩点,不是点双 */ class SCC { const std::vector<std::vector<int>> &adj; std::vector<int> q; int r = 0, cur = 0; void dfs(int x) { dfn[x] = low[x] = cur++; q[++r] = x; for (int y : adj[x]) { if (dfn[y] == -1) { dfs(y); low[x] = std::min(low[x], low[y]); } else { low[x] = std::min(low[x], dfn[y]); } } if (dfn[x] == low[x]) { int y; do { y = q[r--]; bel[y] = cntBlock; } while (y != x); cntBlock++; } } public: std::vector<int> dfn, low, bel; std::vector<std::vector<int>> g; int cntBlock = 0; SCC(const std::vector<std::vector<int>> &adj) : adj(adj), dfn(adj.size(), -1), low(adj.size()), bel(adj.size(), -1) { int n = adj.size(); q.assign(n + 1, 0); for (int i = 0; i < n; i++) { if (dfn[i] == -1) { dfs(i); } } g.resize(cntBlock); for (int x = 0; x < n; x++) { for (int y : adj[x]) { if (bel[x] == bel[y]) { continue; } g[bel[x]].push_back(bel[y]); } } } }; /* (6n) ^ 2 = 36 n */ i64 calc(i64 x) { return x * (x - 1) / 2; } struct SQR { std::vector<std::vector<i64>> f, pos; int n, sn, m; std::vector<int> cnt; std::vector<int> a; SQR() {} SQR(const std::vector<int> &a) : a(a) { n = a.size(); cnt.resize(n); sn = sqrtl(n); m = (n + sn - 1) / sn; f.resize(m, std::vector<i64>(m)); for (int i = 0; i < m; i++) { cnt.assign(n, 0); int ans = 0; for (int j = i * sn; j < n; j++) { ans -= calc(cnt[a[j]]); cnt[a[j]]++; ans += calc(cnt[a[j]]); f[i][j / sn] = ans; } } pos.resize(n); for (int i = 0; i < n; i++) { pos[a[i]].push_back(i); } } i64 query(int l, int r) { // O(√n logn) i64 ans = l / sn + 1 <= r / sn - 1 ? f[l / sn + 1][r / sn - 1] : 0; auto get = [&](int l, int r, int v) -> int { auto lit = std::lower_bound(pos[v].begin(), pos[v].end(), l); auto rit = std::upper_bound(pos[v].begin(), pos[v].end(), r); return rit - lit; }; for (int i = (l / sn + 1) * sn - 1; i >= l; i--) { ans -= calc(get(i + 1, r / sn * sn - 1, a[i])); ans += calc(get(i, r / sn * sn - 1, a[i])); } for (int i = r / sn * sn; i <= r; i++) { ans -= calc(get(l, i - 1, a[i])); ans += calc(get(l, i, a[i])); } return ans; } }; void solve() { int n, m, q; std::cin >> n >> m >> q; std::vector a(m, std::vector<int>(n)); for (auto &v : a) { for (auto &i : v) { std::cin >> i; i--; } } std::vector edg(n, std::vector<int>()); for (int i = 0; i < m; i++) { std::vector<int> id(n); for (int j = 0; j < n; j++) { id[a[i][j]] = j; } for (int j = 1; j < n; j++) { edg[id[j - 1]].push_back(id[j]); } } SCC scc(edg); for (auto &v : a) { for (auto &i : v) { i = scc.bel[i]; // std::cout << i << " "; } // std::cout << "\n"; } // 然后接下来就是,对每个询问,求区间颜色数计数 // 设 区间第 i 种颜色的数量为 ci,要求维护 sum(ci * (ci - 1) / 2) // 强制在线 // 无修改操作 // 就是说,可以开桶,n 根号 n 内分块处理答案 std::vector<SQR> FK(m); for (int i = 0; i < m; i++) { FK[i] = SQR(a[i]); } int v = 0; while (q--) { int id, l, r; std::cin >> id >> l >> r; id = (id + v) % m; l = (l + v) % n; r = (r + v) % n; v = FK[id].query(l, r); std::cout << v << "\n"; } } signed main() { std::cin.tie(nullptr)->sync_with_stdio(false); int t = 1; std::cin >> t; while (t--) solve(); return 0; }
failed
28_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i, a, b) for (int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair<int, int> pii; typedef vector<int> vi; const int mod = 998244353; const int M = 10505; int dp[205][M]; int cnt[205][M]; bool vis[205][M]; int mx[M]; int pre[M]; int c[105]; int mxShield = 0; int k; pii solve(int h, int d) { int b = pre[d]; // cerr << b << ' ' << h << ' ' << d << '\n'; if (vis[b - h][d]) return {dp[b - h][d], cnt[b - h][d]}; if (d >= mxShield && h <= 0) return {0, 1}; int mxfound = 0; vis[b - h][d] = 1; dp[b - h][d] = (int)1e18; for (int j = 1; j <= k; j++) { mxfound = max(mxfound, mx[d + j]); pii res = solve(max(h - j, mxfound), d + j); res.first += c[j]; if (res.first == dp[b - h][d]) { cnt[b - h][d] += res.second; cnt[b - h][d] %= mod; } else if (res.first < dp[b - h][d]) { dp[b - h][d] = res.first; cnt[b - h][d] = res.second; } } return {dp[b - h][d], cnt[b - h][d]}; } void reset(int m) { mxShield = 0; for (int i = 0; i < 205; i++) { for (int j = 0; j <= m + 105; j++) { vis[i][j] = 0; dp[i][j] = 0; cnt[i][j] = 0; mx[j] = 0; pre[j] = 0; } } } signed main() { cin.tie(0)->sync_with_stdio(0); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; vector<int> a(n + 1); vector<int> b(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; mxShield = max(mxShield, b[i]); } for (int i = 1; i <= n; i++) { mx[b[i]] = max(mx[b[i]], a[i]); } for (int i = 1; i <= m + 105; i++) { pre[i] = max(pre[i - 1], mx[i]); } cin >> k; for (int i = 1; i <= k; i++) { cin >> c[i]; } pii res = solve(0, 0); cout << res.first << ' ' << res.second << '\n'; reset(m); } return 0; }
failed
35_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int N = 1919810; #define double long double typedef long long ll; typedef pair<ll, ll> PII; ll n, m, k; ll x[N], y[N]; double s[N], S[N]; double rs[N]; double sum[N], cnt[N]; int find(int l, int r, int X) { ll num = x[l]; while (l < r) { int mid = l + r + 1 >> 1; if (x[mid] >= num) l = mid; else r = mid - 1; } return l; } double query(int l, int r) { double ans = s[r] - s[l - 1]; double res = S[r] - S[l - 1]; res *= (l - 1) * 2; ans -= res; return ans; } double query2(int l, int r) { double ans = rs[l] - rs[r + 1]; double res = S[r] - S[l - 1]; res *= (n - r) * 2; ans -= res; return ans; } void __() { scanf("%lld", &n); for (int i = 1; i <= n; i++) { scanf("%lld%lld", &x[i], &y[i]); x[i + n] = x[i]; y[i + n] = y[i]; } // reverse(x+1,x+1+n); // reverse(y+1,y+1+n); for (int i = 1; i <= n * 2; i++) { S[i] = y[i]; s[i] = y[i] * i * 2; S[i] += S[i - 1]; s[i] += s[i - 1]; // cout<<s[i]<<" "<<S[i]<<endl; } for (int i = n * 2; i >= 1; i--) { rs[i] = y[i] * (n + 1 - i) * 2; rs[i] += rs[i + 1]; } // for(int i=1;i<=n;i++) for (int i = 1; i <= n; i++) { int t = find(i, n + i - 1, x[i]); int sz = t - i; // t-=i; // cout<<sz<<endl; ll val = (sz * (n - 2) - sz * (sz - 1)); // cout<<val<<endl; cnt[i] -= val; sum[i] -= n * (S[t] - S[i]); sum[i] += query(i + 1, t); sz = n - sz - 1; // cout<<sz<<endl; val = (sz * (n - 2) - sz * (sz - 1)); cnt[i] += val; sum[i] += n * (S[n + i - 1] - S[t]); sum[i] -= query2(t + 1, n + i - 1); // cout<<n*(S[t]-S[i])<<" "<<n*(S[n]-S[t])<<" "<<query(i+1,t)<<" // "<<query2(t+1,n)<<endl; } double fm = 0; for (int i = 1; i <= n; i++) { fm += (x[i] * y[i + 1] - x[i + 1] * y[i]); } double ans = 0; for (int i = 1; i <= n; i++) { double res = x[i] * y[i]; res *= cnt[i]; ans += res; ans += sum[i] * x[i]; // cout<<res<<" "<<sum[i]*x[i]<<endl; // cout<<cnt[i]<<" "<<sum[i]<<endl; // cout<<ans<<endl; } // cout<<ans<<endl; ans /= fm; printf("%.15Lf\n", -ans); // cout<<fixed<<precision(15)<<ans<<endl; } int main() { int _ = 1; // cin>>_; while (_--) { __(); } }
failed
67_3_wrong.cpp
// 又是一道好题 #include <bits/stdc++.h> #define pii pair<int, int> #define fi first #define se second using namespace std; const int INF = 1e9 + 9; const int N = 5e5 + 6; int n, q; struct line { int dy, ty, id; bool operator<(const line &A) const { return id < A.id; } }; vector<int> vec[N]; // 树链剖分 struct node { int lt, rt, val; int tag; }; struct segment { #define lc (x * 2) #define rc (x * 2 + 1) node tr[N * 4]; void pushup(int x) { tr[x].val = tr[lc].val + tr[rc].val; } void pushdown(int x) { if (tr[x].tag == 0) return; int val = tr[x].tag; tr[x].tag = 0; tr[lc].val += val; tr[rc].val += val; tr[lc].tag += val; tr[rc].tag += val; } void build(int x, int lt, int rt) { tr[x] = {lt, rt, 0, 0}; if (lt == rt) return; int mid = (lt + rt) / 2; build(lc, lt, mid); build(rc, mid + 1, rt); pushup(x); } void update(int x, int qlt, int qrt, int val) { if (qlt <= tr[x].lt && tr[x].rt <= qrt) { tr[x].val += val; tr[x].tag += val; return; } if (tr[x].rt < qlt || qrt < tr[x].lt) return; pushdown(x); update(lc, qlt, qrt, val); update(rc, qlt, qrt, val); pushup(x); } int query(int x, int qlt, int qrt) { if (qlt <= tr[x].lt && tr[x].rt <= qrt) return tr[x].val; if (tr[x].rt < qlt || qrt < tr[x].lt) return 0; pushdown(x); int res = query(lc, qlt, qrt) + query(rc, qlt, qrt); pushup(x); return res; } } s1, s2; int fa[N], siz[N], dep[N], son[N]; int top[N], id[N], tim; // 重链 int st[21][N]; // 按照时间戳为点定义对应id 这样再线段树上重链都连在了一块 // 然后我们用nw同步更新对应的值 void dfs1(int u, int f) { dep[u] = dep[f] + 1; siz[u] = 1; st[0][u] = f; for (int i = 1; i <= 20; i++) st[i][u] = st[i - 1][st[i - 1][u]]; for (int v : vec[u]) { if (v == f) continue; dfs1(v, u); if (son[u] == 0 || siz[son[u]] < siz[v]) son[u] = v; siz[u] += siz[v]; } } void dfs2(int u, int tp) { top[u] = tp; id[u] = ++tim; if (!son[u]) return; dfs2(son[u], tp); for (int v : vec[u]) { if (v == fa[u] || v == son[u]) continue; dfs2(v, v); } } void change_path(int u, int v, int k) { // 路径增加 while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); s1.update(1, id[top[u]], id[u], k); u = fa[top[u]]; } if (dep[u] < dep[v]) swap(u, v); s1.update(1, id[v], id[u], k); } int vis[N]; void open_light(int u) { int tp = u; for (int i = 20; i >= 0; i--) { if (s1.query(1, id[st[i][tp]], id[st[i][tp]]) == 0) tp = st[i][tp]; } if (!s1.query(1, id[tp], id[tp])) { s2.update(1, dep[tp], dep[u], 1); } change_path(st[20][u], u, 1); } void close_light(int u) { change_path(st[20][u], u, -1); int tp = u; for (int i = 20; i >= 0; i--) { if (s1.query(1, id[st[i][tp]], id[st[i][tp]]) == 0) tp = st[i][tp]; } if (!s1.query(1, id[tp], id[tp])) { s2.update(1, dep[tp], dep[u], -1); } } void solve() { cin >> n >> q; // 首先是将矩阵包含处理成子树形状 map<int, vector<line>> opr; for (int i = 1; i <= n; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; opr[x1].push_back({y1, y2, i}); opr[x2 + 1].push_back({y1, y2, -i}); } // 我们把所有线存在set中 set<pii> s; s.insert({0, 0}); s.insert({INF, 0}); for (auto &i : opr) { sort(i.se.begin(), i.se.end()); for (auto &ln : i.se) { auto [dy, ty, tid] = ln; if (ln.id < 0) { s.erase(s.find({dy, -tid})); s.erase(s.find({ty + 1, fa[-tid]})); } else { auto it = s.lower_bound({dy, tid}); it--; int far = (*it).se; vec[far].push_back(tid); fa[tid] = far; s.insert({dy, tid}); s.insert({ty + 1, far}); } } } // 此时我们成功处理出所有矩阵的从属关系 // 现在我们开始处理这棵树 for (int i = 0; i <= n; i++) if (fa[i] == 0) fa[i] = i; for (auto &i : vec[0]) { dfs1(i, i); dfs2(i, i); } // 建树 s1.build(1, 1, n); s2.build(1, 1, n); // 操作 for (int i = 1; i <= q; i++) { char opt; int x; cin >> opt >> x; if (opt == '^') { if (vis[x]) { close_light(x); } else { open_light(x); } vis[x] ^= 1; } else { cout << s2.query(1, x + 1, x + 1) << "\n"; } } } signed main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int T = 1; // cin >> T; for (int i = 1; i <= T; i++) solve(); return 0; } /* 8 7 1 1 15 11 2 2 7 4 2 5 7 10 3 6 4 9 5 6 6 7 8 2 14 10 9 5 13 9 11 6 12 7 ^ 4 ^ 5 ^ 8 ? 0 ? 1 ? 2 ? 3 */
failed
77_2_wrong.cpp
#include <iostream> #include <vector> using namespace std; // Function to generate the sequence based on the PLRR vector<int> generateSequence(int k, const vector<int> &c, const vector<int> &a) { vector<int> sequence = a; for (int i = k; i < 10 + k; ++i) { int next_term = 0; for (int j = 0; j < k; ++j) { next_term += c[j] * sequence[i - k + j]; } sequence.push_back(next_term); } return sequence; } // Function to find the n-th PLRR void findPLRR(int n) { if (n == 1) { // k = 1, c = [1], a = [1] cout << "1\n1\n1\n1 1 1 1 1 1 1 1 1 1\n"; } else if (n == 2) { // k = 1, c = [2], a = [1] cout << "1\n2\n1\n2 4 8 16 32 64 128 256 512 1024\n"; } else if (n == 3) { // k = 2, c = [1, 1], a = [1, 1] cout << "2\n1 1\n1 1\n2 3 5 8 13 21 34 55 89 144\n"; } else { // For n > 3, we need a more general approach // This is a placeholder and should be expanded for larger n cout << "General case not implemented for n > 3\n"; } } int main() { int n; cin >> n; findPLRR(n); return 0; }
failed
114_1_wrong.cpp
#include <bits/stdc++.h> // #define int long long #define fi first #define se second #define pb push_back #define gt() #define ott(i, l, r) for (int i = (l); i <= (r); i++) #define tto(i, l, r) for (int i = (r); i >= (l); i--) using namespace std; typedef long long ll; typedef double db; typedef long double ld; int read() { int x = 0; bool f = false; char c = getchar(); while (!isdigit(c)) f |= (c == '-'), c = getchar(); while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return f ? -x : x; } const int N = 1100, mod = 1e9 + 7; const db eps = 1e-10; int T; int l2[5][4], id[5]; db p, s; db ib[5][4]; db sl, xl; db l[5][4]; inline bool cmp(const db &a1, const db &a2) { return fabs(a1 - a2) <= eps; } inline bool irt(db *jb) { return cmp(jb[1] * jb[1] + jb[2] * jb[2], jb[3] * jb[3]); } // level 0 inline bool test1() { if (irt(ib[1]) || irt(ib[2]) || irt(ib[3]) || irt(ib[4])) return false; return cmp(ib[1][3], sl) && cmp(ib[2][3], sl) && cmp(ib[3][3], sl) && cmp(ib[4][3], sl) && cmp(ib[1][2], ib[2][1]) && cmp(ib[2][2], ib[3][1]) && cmp(ib[3][2], ib[4][1]) && cmp(ib[4][2], ib[1][1]); } inline bool test2() { if (!cmp(ib[1][1] + ib[3][2], xl) || !cmp(ib[2][1] + ib[4][2], xl)) return false; return cmp(ib[1][3], sl) && cmp(ib[2][3], sl) && cmp(ib[3][3], sl) && cmp(ib[4][3], sl) && cmp(ib[1][2], ib[3][1]) && cmp(ib[2][2], ib[4][1]) && cmp(ib[1][1] + ib[3][2], ib[2][1] + ib[4][2]); // sl * pow (2,0.5) } // level 1 inline bool test3() { if (!irt(ib[2]) || !irt(ib[3])) return false; return cmp(ib[1][1] + ib[2][1], sl) && cmp(ib[2][2] + ib[3][2], sl) && cmp(ib[3][1], sl) && cmp(ib[1][3], ib[4][1]) && cmp(ib[2][3], ib[4][2]) && cmp(ib[3][3], ib[4][3]); } inline bool test4() { if (!irt(ib[2])) return false; return cmp(ib[1][1] + ib[2][1], sl) && cmp(ib[2][2], sl) && cmp(ib[3][1], sl) && cmp(ib[4][1] + ib[3][2], ib[1][3]) && cmp(ib[3][3], ib[4][2]) && cmp(ib[2][3], ib[4][3]); } inline bool test5() { if (!irt(ib[2])) return false; return cmp(ib[1][1] + ib[4][1] + ib[2][1], sl) && cmp(ib[2][2], sl) && cmp(ib[3][1], sl) && cmp(ib[3][2], ib[1][3]) && cmp(ib[3][3], ib[4][3]) && cmp(ib[2][3], ib[4][2]); } inline bool test6() { if (!irt(ib[4])) return false; return cmp(ib[1][1] + ib[3][1], sl) && cmp(ib[2][1] + ib[4][1], sl) && cmp(ib[4][2], sl) && cmp(ib[1][3], ib[2][3]) && cmp(ib[2][2], ib[3][2]) && cmp(ib[3][3], ib[4][3]); } inline bool test7() { if (!irt(ib[4])) return false; // no xl return cmp(ib[1][1] + ib[2][1] + ib[3][1], sl) && cmp(ib[4][1], sl) && cmp(ib[4][2], sl) && cmp(ib[1][3], ib[2][2]) && cmp(ib[2][3], ib[3][2]) && cmp(ib[3][3], ib[4][3]); } inline bool test8() { if (!irt(ib[4]) || !cmp(ib[3][3], xl)) return false; return cmp(ib[1][1] + ib[2][1], sl) && cmp(ib[3][1] + ib[4][1], sl) && cmp(ib[4][2], sl) && cmp(ib[1][3], ib[2][2]) && cmp(ib[2][3], ib[3][3]) && cmp(ib[3][2], ib[4][3]); } inline bool test9() { // irt? return cmp(ib[1][1] + ib[2][2], sl) && cmp(ib[4][3], sl) && cmp(ib[3][3], sl) && cmp(ib[2][1] + ib[3][1], ib[1][3]) && cmp(ib[2][3], ib[4][1]) && cmp(ib[3][2], ib[4][2]); } inline bool test10() { if (!irt(ib[2])) return false; return cmp(ib[1][1] + ib[2][2], sl) && cmp(ib[2][1] + ib[3][1], sl) && cmp(ib[4][3], sl) && cmp(ib[1][3], ib[4][1]) && cmp(ib[2][3], ib[3][2]) && cmp(ib[3][3], ib[4][2]); } inline bool test11() { if (!irt(ib[4])) return false; return cmp(ib[1][1] + ib[4][1], sl) && cmp(ib[4][2], sl) && cmp(ib[2][1] + ib[3][1], sl) && cmp(ib[1][3], ib[2][3]) && cmp(ib[2][2], ib[3][2]) && cmp(ib[3][3], ib[4][3]); } inline bool test12() { if (!irt(ib[4])) return false; return cmp(ib[1][1] + ib[2][1], sl) && cmp(ib[4][2], sl) && cmp(ib[3][1] + ib[4][1], sl) && cmp(ib[1][3], ib[2][2]) && cmp(ib[2][3], ib[3][3]) && cmp(ib[3][2], ib[4][3]); } inline bool test13() { if (!irt(ib[4])) return false; // no xl return cmp(ib[1][1] + ib[2][2], sl) && cmp(ib[4][1], sl) && cmp(ib[4][2], sl) && cmp(ib[2][1] + ib[3][1], ib[1][3]) && cmp(ib[2][3], ib[3][2]) && cmp(ib[3][3], ib[4][3]); } inline bool test14() { return cmp(ib[1][1] + ib[2][1], sl) && cmp(ib[3][3], sl) && cmp(ib[4][3], sl) && cmp(ib[1][3], ib[2][2]) && cmp(ib[3][1] + ib[4][2], ib[2][3]) && cmp(ib[3][2], ib[4][1]); } inline bool test15() { return cmp(ib[1][1] + ib[2][1], sl) && cmp(ib[3][3], sl) && cmp(ib[4][3], sl) && cmp(ib[1][3], ib[4][1]) && cmp(ib[2][3] + ib[3][2], ib[4][2]) && cmp(ib[2][2], ib[3][1]); } inline bool test16() { if (!irt(ib[4])) return false; // no xl return cmp(ib[1][1] + ib[2][3], sl) && cmp(ib[4][1], sl) && cmp(ib[4][2], sl) && cmp(ib[1][3], ib[3][3]) && cmp(ib[2][1], ib[3][1]) && cmp(ib[2][2] + ib[3][2], ib[4][3]); } // level 2 inline bool test17() { return cmp(ib[2][3], sl) && cmp(ib[4][3], sl) && cmp(ib[2][1] + ib[3][1] + ib[4][1], ib[1][3]) && cmp(ib[3][1], ib[2][2]) && cmp(ib[3][2], ib[4][2]); } inline bool test18() { if (!irt(ib[4])) return false; return cmp(ib[3][1] + ib[4][1], sl) && cmp(ib[4][2] + ib[2][1], sl) && cmp(ib[1][3], ib[2][3]) && cmp(ib[2][2], ib[3][3]) && cmp(ib[3][2], ib[4][3]); } inline bool test19() { return cmp(ib[2][3], sl) && cmp(ib[3][1] + ib[4][1], sl) && cmp(ib[3][2], ib[4][2]) && cmp(ib[2][1] + ib[4][3], ib[1][3]) && cmp(ib[2][2], ib[3][3]); } inline bool test20() { return cmp(ib[2][3], sl) && cmp(ib[4][3], sl) && cmp(ib[2][1] + ib[3][2], ib[1][3]) && cmp(ib[3][1] + ib[4][1], ib[2][2]) && cmp(ib[3][3], ib[4][2]); } inline bool test21() { return cmp(ib[2][3], sl) && cmp(ib[3][3] + ib[4][1], sl) && cmp(ib[1][3], ib[4][3]) && cmp(ib[2][2] + ib[3][1], ib[4][2]) && cmp(ib[2][1], ib[3][2]); } inline bool test22() { return cmp(ib[2][3], sl) && cmp(ib[3][3], sl) && cmp(ib[2][1], ib[3][1]) && cmp(ib[4][1], ib[2][2]) && cmp(ib[4][2], ib[3][2]) && cmp(ib[4][3], ib[1][3]); } bool sol() { sl = 0; ott(i, 1, 4) { ott(j, 1, 3) l2[i][j] = read(); sort(l2[i] + 1, l2[i] + 4); p = 0; ott(j, 1, 3) p += l[i][j] = (db)sqrt(l2[i][j]); p /= 2.0; s = p; ott(j, 1, 3) s *= p - l[i][j]; sl += (db)sqrt(s); id[i] = i; } sl = (db)sqrt(sl), xl = sl * pow(2, 0.5); printf("%lf\n", sl); do { ott(i, 1, 4) ott(j, 1, 3) ib[i][j] = l[id[i]][j]; do { do { do { do { if (test1()) return true; if (test2()) return true; if (!cmp(ib[1][2], sl) || !irt(ib[1])) continue; if (test3()) return true; if (test4()) return true; if (test5()) return true; if (test6()) return true; if (test7()) return true; if (test8()) return true; if (test9()) return true; if (test10()) return true; if (test11()) return true; if (test12()) return true; if (test13()) return true; if (test14()) return true; if (test15()) return true; if (test16()) return true; if (!cmp(ib[1][1], sl)) continue; if (test17()) return true; if (test18()) return true; if (test19()) return true; if (test20()) return true; if (test21()) return true; if (test22()) return true; } while (next_permutation(ib[4] + 1, ib[4] + 4)); } while (next_permutation(ib[3] + 1, ib[3] + 4)); } while (next_permutation(ib[2] + 1, ib[2] + 4)); } while (next_permutation(ib[1] + 1, ib[1] + 4)); } while (next_permutation(id + 1, id + 5)); return false; } int main() { T = read(); while (T--) puts(sol() ? "1" : "0"); return 0; }
failed
109_2_wrong.cpp
#include <bitset> #include <cstdio> #define ll long long using namespace std; const int B = 700; const int _N = 5e5 + B + 9; const int D = _N / B + 1; int N, M; int Read() { int x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x; } int A[_N]; int dN; bitset<500001> vs[D]; int L[D], R[D]; int r1[D], r2[D]; struct BSGT { struct Node { int tg1, tg2; } tr[D * 4]; void Init() { for (int i = 1; i <= dN * 4; i++) tr[i].tg2 = -1; } void PushDown(int x) { Node &v = tr[x], &l = tr[x << 1], &r = tr[x << 1 | 1]; if (~v.tg1) { l.tg1 = r.tg1 = v.tg1, l.tg2 = r.tg2 = 0; v.tg1 = -1; } if (v.tg2) { l.tg2 += v.tg2, r.tg2 += v.tg2; v.tg2 = 0; } } void Modify(int x, int l, int r, int v1, int v2, int L = 0, int R = dN) { if (L >= l && R <= r) { if (~v1) tr[x].tg1 = v1, tr[x].tg2 = 0; else tr[x].tg2 += v2; return; } int mid = L + R >> 1; PushDown(x); if (l <= mid) Modify(x << 1, l, r, v1, v2, L, mid); if (r > mid) Modify(x << 1 | 1, l, r, v1, v2, mid + 1, R); } void Get_Tg(int x, int l, int r, int L = 0, int R = dN) { if (L == R) { r1[L] = tr[x].tg1, r2[L] = tr[x].tg2; return; } int mid = L + R >> 1; PushDown(x); if (l <= mid) Get_Tg(x << 1, l, r, L, mid); if (r > mid) Get_Tg(x << 1 | 1, l, r, mid + 1, R); } void Clear(int x, int p, int L = 0, int R = dN) { if (L == R) { tr[x].tg1 = -1, tr[x].tg2 = 0; return; } int mid = L + R >> 1; PushDown(x); if (p <= mid) Clear(x << 1, p, L, mid); else Clear(x << 1 | 1, p, mid + 1, R); } } bsgt; struct SGT { struct Node { ll tg1, tg2, sm; } tr[2000009]; void PushUp(int x) { tr[x].sm = tr[x << 1].sm + tr[x << 1 | 1].sm; } void Build(int x = 1, int L = 1, int R = N) { tr[x].tg1 = -1; if (L == R) { tr[x].sm = A[L]; return; } int mid = L + R >> 1; Build(x << 1, L, mid), Build(x << 1 | 1, mid + 1, R); PushUp(x); } void PushDown(int x, ll l1, ll l2) { Node &v = tr[x], &l = tr[x << 1], &r = tr[x << 1 | 1]; if (~v.tg1) { l.tg1 = r.tg1 = v.tg1, l.tg2 = r.tg2 = 0; l.sm = v.tg1 * l1, r.sm = v.tg1 * l2; v.tg1 = -1; } if (v.tg2) { l.tg2 += v.tg2, l.sm += (v.tg2 * l1); r.tg2 += v.tg2, r.sm += (v.tg2 * l2); v.tg2 = 0; } } void Modify(int x, int l, int r, ll v1, ll v2, int L = 1, int R = N) { if (L >= l && R <= r) { if (~v1) tr[x].tg1 = v1, tr[x].tg2 = 0, tr[x].sm = (R - L + 1) * v1; else tr[x].tg2 += v2, tr[x].sm += ((R - L + 1) * v2); return; } int mid = L + R >> 1; PushDown(x, mid - L + 1, R - mid); if (l <= mid) Modify(x << 1, l, r, v1, v2, L, mid); if (r > mid) Modify(x << 1 | 1, l, r, v1, v2, mid + 1, R); PushUp(x); } ll Query(int x, int l, int r, int L = 1, int R = N) { if (L >= l && R <= r) return tr[x].sm; int mid = L + R >> 1; PushDown(x, mid - L + 1, R - mid); if (r <= mid) return Query(x << 1, l, r, L, mid); if (l > mid) return Query(x << 1 | 1, l, r, mid + 1, R); return Query(x << 1, l, r, L, mid) + Query(x << 1 | 1, l, r, mid + 1, R); } } sgt; int q1, q2; ll s; void Modify(int &id, int l, int r, int v1, int v2) { q1 = r1[id], q2 = r2[id]; for (int i = L[id]; i <= R[id]; i++) { if (A[i] <= N) vs[id].reset(A[i]); if (~q1) A[i] = q1 + q2; else A[i] += q2; if (i >= l && i <= r) { if (~v1) A[i] = v1 + v2; else A[i] += v2; } } for (int i = L[id]; i <= R[id]; i++) if (A[i] <= N) vs[id].set(A[i]); bsgt.Clear(1, id); } int vq[500009], vn; bool tvis[500009]; void Print(ll x) { if (x > 9) Print(x / 10); putchar((x % 10) + 48); } void Add(int v) { if (v <= N) tvis[vq[++vn] = v] = true; } int main() { N = Read(), M = Read(), dN = N / B; for (int i = 1; i <= N; i++) { A[i] = Read(); if (A[i] <= N) vs[i / B].set(A[i]); } for (int i = 0; i <= dN; i++) L[i] = i * B, R[i] = (i + 1) * B - 1; int op, l, r, v, tl, tr; bool f; ll q1, ans; int q2; bsgt.Init(), sgt.Build(); while (M--) { op = Read(), l = Read(), r = Read(); tl = l / D, tr = r / D; if (op == 1) { v = Read(); sgt.Modify(1, l, r, v, 0); if (tl == tr) bsgt.Get_Tg(1, tl, tl), Modify(tl, l, r, v, 0); else { if (tl + 1 < tr) bsgt.Modify(1, tl + 1, tr - 1, v, 0); bsgt.Get_Tg(1, tl, tl), bsgt.Get_Tg(1, tr, tr); Modify(tl, l, R[tl], v, 0), Modify(tr, L[tr], r, v, 0); } } else if (op == 2) { bsgt.Get_Tg(1, tl, tr); vn = 0; for (int i = tl; i <= tr; i++) if (~r1[i]) Add(r1[i] + r2[i]); if (tl == tr) { if (r1[tl] == -1) for (int i = l; i <= r; i++) Add(A[i] + r2[tl]); } else { if (r1[tl] == -1) for (int i = l; i <= R[tl]; i++) Add(A[i] + r2[tl]); if (r1[tr] == -1) for (int i = L[tr]; i <= r; i++) Add(A[i] + r2[tr]); } v = -1; do { v++, f = 0; if (tvis[v]) f = 1; else for (int i = tl + 1; i < tr; i++) if (r1[i] == -1 && v >= r2[i] && vs[i][v - r2[i]]) { f = 1; break; } } while (f); for (int i = 1; i <= vn; i++) tvis[vq[i]] = false; if (v) { sgt.Modify(1, l, r, -1, v); if (tl == tr) Modify(tl, l, r, -1, v); else { if (tl + 1 < tr) bsgt.Modify(1, tl + 1, tr - 1, -1, v); Modify(tl, l, R[tl], -1, v), Modify(tr, L[tr], r, -1, v); } } } else Print(sgt.Query(1, l, r)), puts(""); } return 0; }
failed
23_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define CONDITION (R - L >= 4 && f[R] - (L ? f[L - 1] : 0)) struct node { int l, len; }; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int n = s.length(); vector<int> f(n); for (int i = 0; i < n; ++i) { if (i) f[i] = f[i - 1]; f[i] += s[i] == '-'; } if (s[n - 3] != '>' || s[n - 2] != '>' || s[n - 1] != '>' || s[0] == '-') { cout << "No\n"; continue; } int L = 0, R = n - 1; vector<node> ans(0); while (CONDITION) { while (s[L] != '>' && CONDITION) L++; ans.emplace_back(node{L + 1, R - L + 1}); while (s[R - 3] == '>' && --R && CONDITION) { ans.emplace_back(node{L + 1, R - L + 1}); } ++L; } if (ans.empty()) { cout << "No\n"; } else { cout << "Yes " << ans.size() << "\n"; for (auto &x : ans) { cout << x.l << " " << x.len << "\n"; } } } return 0; }
failed
1_2_wrong.cpp
#include <bits/stdc++.h> #define lc (x * 2) #define rc (x * 2 + 1) #define int long long using namespace std; const int N = 2e3 + 4; int n, a[N], chg[N], vis[N]; int zs = 0; int res = 0; void dfs(int x) { if (chg[x]) return; if (x >= 1 << (n + 1)) return; vis[x] = true; a[x] = a[x / 2] + a[x]; dfs(x * 2); dfs(x * 2 + 1); } queue<int> merge(int x, int lt, int rt) { queue<int> ret; if (chg[x]) return ret; if (lt == rt) { ret.push(a[x]); return ret; } queue<int> l, r; int mid = (lt + rt) / 2; l = merge(lc, lt, mid); r = merge(rc, mid + 1, rt); while (!l.empty() && !r.empty()) { if (l.front() < r.front()) { ret.push(l.front()), l.pop(); } else if (l.front() > r.front()) { ret.push(r.front()), r.pop(); } else { zs = max(zs, x); return ret; } } while (!l.empty()) ret.push(l.front()), l.pop(); while (!r.empty()) ret.push(r.front()), r.pop(); return ret; } void change(int i) { if (i < 0) return; for (int j = 1; j < 1 << (n + 1); j++) vis[j] = 0; // 是否需要修改 zs = 0; merge(1, 1, (1 << n)); if (zs == 0) { res = min(res, n - i); return; } // 能否修改 int lv = n; for (; lv >= 0; lv--) if ((zs >> lv) & 1) break; if (lv >= i) return; int plc = zs; chg[plc * 2] = 1; change(i - 1); chg[plc * 2] = 0; chg[plc * 2 + 1] = 1; change(i - 1); chg[plc * 2 + 1] = 0; } void solve() { cin >> n; res = n + 1; for (int i = 2; i < 1 << (n + 1); i++) cin >> a[i]; dfs(1); change(n); if (res > n) cout << "-1\n"; else cout << res << "\n"; for (int i = 2; i < 1 << (n + 1); i++) chg[i] = 0; } signed main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int T = 1; cin >> T; for (int i = 1; i <= T; i++) solve(); return 0; }
failed
86_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; #define lll __int128_t #define uint uint32_t using B = bitset<400001>; B G[3010]; void solve() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<vector<int>> pz(1010); int maxn = 1000; for (int i = 0; i < n; i++) pz[a[i]].push_back(i); for (int i = 1; i <= maxn; i++) { for (int j = max(1, i - k); j <= min(i + k, maxn); j++) { for (auto k : pz[j]) G[i][k] = 1; } } vector<int> ans(2 * n + 2); for (int i = 0; i < n; i++) { ans[2 * i] = min(n - i, i + 1); ans[2 * i + 1] = min(n - i - 1, i + 1); } bitset<400001> del; B p; for (int i = n - 1; i >= 0; i--) { p = p << 1; B np; np[i] = 1; if (i != n - 1 && abs(a[i] - a[i + 1]) <= k) np[i + 1] = 1; p = (np | (p & G[a[i]])); B q = p; q.flip(); /* [2*i+1,2*i+1]; [i+1,i+2] */ q = q ^ (del >> i); for (int t = q._Find_next(i); t < n; t = q._Find_next(t)) { ans[t + i] = (t - i + 1) / 2 - 1; del[t + i] = 1; } } for (int i = 0; i < n; i++) cout << ans[2 * i] * 2 - 1 << ' '; cout << '\n'; for (int i = 0; i < n - 1; i++) cout << ans[2 * i + 1] * 2 << ' '; cout << '\n'; } signed main() { ios::sync_with_stdio(0), cin.tie(0); int _ = 1; // cin>>_; while (_--) { solve(); } }
failed
34_1_wrong.cpp
#include <iostream> using namespace std; typedef long long ll; int p[400005]; char t[400005]; ll F[1600017], G[1600017]; ll *f = F + 800005, *g = G + 800005; int x, n, m; ll res = 0; ll solve(int k) { f = F + 400002; g = G + 400002; f[0] = 0; f[1] = 1e18; f[2] = -1e18; for (int i = 3; i <= n + 1; i++) f[i] = 0; for (int i = 0; i <= n + 1; i++) g[i] = 1; int mn = 0; ll v = 0, ans = 0, rec = 0; for (int i = 1; i <= n + 1; i++) { int ad = p[i] - p[i - 1]; v += (ll)abs(mn - k) * ad; if (k <= mn) f[mn + 1] += ad; else f[k + 1] += 2 * ad, f[mn + 1] -= ad; if (m < mn) ans += (ll)(k - m) * ad; while (f[mn + 1] < 0) { if (mn == m) ans = v, rec = g[m]; f[mn + 2] += f[mn + 1]; v += f[mn + 1]; f[mn + 1] = 0; mn++; } if (t[i] == 'P') k++; else if (t[i] == 'H') { f--; g--; f[mn] = 0; g[mn] = g[mn + 1]; if (f[mn + 2] == 0) g[mn + 1] += g[mn + 2]; } // cout << mn << " " << v << endl; // for(int j = 0; j <= n; j++) cout << g[mn+j] << " "; // cout << endl; } if (m < mn) return res = rec, ans; if (mn == m) return res = g[mn], v; for (int i = mn + 2; i <= m; i++) f[i] += f[i - 1]; for (int i = mn + 1; i <= m; i++) f[i] += f[i - 1]; return res = g[m], v + f[m]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> x >> m >> n; n += m; for (int i = 1; i <= n; i++) { cin >> p[i] >> t[i]; } p[n + 1] = x; // cout << solve(0) << endl; // return 0; int l = -m, r = m; while (r - l >= 3) { int m1 = l + (r - l) / 3, m2 = r - (r - l) / 3; if (solve(m1) < solve(m2)) r = m2; else l = m1; } ll ans = 1e18, a2 = 0; for (int i = l; i <= r; i++) { ll t = solve(i); if (t == ans) a2 += res; else if (t < ans) ans = t, a2 = res; } cout << ans << endl << ((n == m + m) ? 1 : a2 % 998244353) << endl; return 0; }
failed
103_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int N = 3010, M = 1e6 + 10; const double eps = 1e-8; int n, sum[M], id[M]; double w; struct Line { double x, u, d; int uid, did; Line() {} Line(double x, double u, double d, int uid, int did) : x(x), u(u), d(d), uid(uid), did(did) {} } l[N]; struct Node { double x; int a, b; Node() {} Node(double x, int a, int b) : x(x), a(a), b(b){}; }; vector<Node> loc; struct Shadow { double y; int id, flag; Shadow() {} Shadow(double y, int id, int flag) : y(y), id(id), flag(flag) {} }; vector<Shadow> shadow; inline void get(int x1, int y1, int a, int x2, int y2, int b) { if (fabs(y2 - y1) <= eps || fabs(x2 - x1) <= eps) return; double k = (double)(x2 - x1) / (y2 - y1); double x = (double)x1 - k * y1; loc.push_back(Node(x, a, b)); } inline Shadow get_shadow(double lx, double x, double y, int id, double w, int flag) { double k = y / (x - lx); double ys = k * (w - lx); return Shadow(ys, id, flag); } inline bool cmp1(Node a, Node b) { return a.x > b.x; } inline bool cmp(Node a, Node b) { if (fabs(a.x - b.x) <= eps) { double mia = min(shadow[id[a.a]].y, shadow[id[a.b]].y), mib = min(shadow[id[b.a]].y, shadow[id[b.b]].y); double mxa = max(shadow[id[a.a]].y, shadow[id[a.b]].y), mxb = max(shadow[id[b.a]].y, shadow[id[b.b]].y); if (fabs(mia - mib) <= eps) return mxa < mxb; else if (fabs(mxa - mxb) <= eps) return mia > mib; else return mia < mib; } return a.x > b.x; } inline bool cmp_s(Shadow a, Shadow b) { return fabs(a.y - b.y) <= eps ? a.flag > b.flag : a.y > b.y; } int main() { scanf("%d%lf", &n, &w); int cnt = 0; double sta; for (int i = 1; i <= n; i++) { scanf("%lf%lf%lf", &l[i].x, &l[i].d, &l[i].u); l[i].uid = ++cnt, l[i].did = ++cnt; sta = min(sta, l[i].x); } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { get(l[i].x, l[i].u, l[i].uid, l[j].x, l[j].d, l[j].did); get(l[i].x, l[i].u, l[i].uid, l[j].x, l[j].u, l[j].uid); get(l[i].x, l[i].d, l[i].did, l[j].x, l[j].d, l[j].did); get(l[i].x, l[i].d, l[i].did, l[j].x, l[j].u, l[j].uid); } } sort(loc.begin(), loc.end(), cmp1); int m = loc.size(), sta_id; for (int i = 0; i < m; i++) if (loc[i].x <= sta) { sta_id = i; sta = loc[i].x; break; } for (int i = 1; i <= n; i++) { shadow.push_back(get_shadow(sta, l[i].x, l[i].u, l[i].uid, w, 1)); shadow.push_back(get_shadow(sta, l[i].x, l[i].d, l[i].did, w, -1)); } sort(shadow.begin(), shadow.end(), cmp_s); int ms = shadow.size(); for (int i = 0; i < ms; i++) id[shadow[i].id] = i; /*for (int i = 0; i < ms; i++) printf("%lf:%d ", shadow[i].y, shadow[i].flag);*/ sort(loc.begin(), loc.end(), cmp); cnt = 0; double ans = 0; for (int i = 0; i < ms; i++) { sum[i] = sum[i - 1] + shadow[i].flag; if (sum[i] == 0) cnt++; } if (cnt == 1) ans += sta - loc[sta_id + 1].x; for (int i = 0; i < m; i++) { if (i <= sta_id) continue; int l = min(id[loc[i].a], id[loc[i].b]), r = max(id[loc[i].a], id[loc[i].b]); if (r - l > 1) { puts("ERROR"); return 0; } if (shadow[l].flag != shadow[r].flag) { int ll = sum[l], lr = sum[r]; sum[l] += shadow[r].flag - shadow[l].flag; sum[r] += shadow[l].flag - shadow[r].flag; if (!ll && sum[l]) cnt--; if (ll && !sum[l]) cnt++; if (!lr && sum[r]) cnt--; if (lr && !sum[r]) cnt++; } if (i < m - 1 && cnt == 1) ans += loc[i].x - loc[i + 1].x; swap(shadow[id[loc[i].a]], shadow[id[loc[i].b]]); swap(id[loc[i].a], id[loc[i].b]); } if (cnt == 1) ans = -1; printf("%.8lf", ans); }
failed
108_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define endl '\n' constexpr int N = 205; int n, m; char a[N][N]; void solve(void) { cin >> n >> m; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> a[i][j]; } } bool eq = true; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (a[i][j] != a[1][1]) { eq = false; } } } if (eq) { cout << "YES\n0\n"; return; } if (n == 1 || m == 1) { cout << "NO\n"; return; } char c = a[1][1]; bool t = (a[1][2] != a[2][1]); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { a[i][j] = (a[i][j] == c ? t : !t); } } vector<string> res; auto work = [&](int i, int j) { res.emplace_back(); int x = 1, y = 1; a[x][y] ^= 1; while (x < i) { res.back().push_back('D'); a[++x][y] ^= 1; } while (y < j) { res.back().push_back('R'); a[x][++y] ^= 1; } while (x < n) { res.back().push_back('D'); a[++x][y] ^= 1; } while (y < m) { res.back().push_back('R'); a[x][++y] ^= 1; } }; for (int i = 1; i < n; ++i) { for (int j = m; j > 1; --j) { if (a[i][j]) { work(i, j); } } } if (a[1][1]) { work(n, 1); } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (a[i][j]) { cout << "NO\n"; return; } } } cout << "YES\n" << res.size() << '\n'; for (auto &s : res) { cout << s << '\n'; } } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int _ = 1; cin >> _; while (_--) solve(); return 0; }
failed
45_2_wrong.cpp
#include <bits/stdc++.h> #define ll long long using namespace std; const int MAXN = (int)1e4 + 5; int n, c, a[MAXN]; ll f[MAXN][MAXN], s[MAXN]; void chmin(ll &_1, ll _2) { if (_1 > _2) _1 = _2; } int main() { // g++ code.cpp -o code.exe -std=c++14 -O2 -fno-ms-extensions -DLOCAL; ./code time_t stm = clock(); cerr << "Running..." << endl; #ifdef LOCAL freopen("data.in", "r", stdin); freopen("ans.out", "w", stdout); #else ios::sync_with_stdio(); cin.tie(0), cout.tie(0); #endif cin >> n >> c; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); if (c >= n) { cout << a[n]; return 0; } for (int i = 1; i <= n; i++) { s[i] = s[i - 1] + a[i]; } memset(f, 60, sizeof f); for (int i = 0; i <= c; i++) { f[i][0] = a[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j * (c - 1) <= n - i; j++) { for (int k = 0; k <= c && (j + k - 1) <= n; k++) { if (k > 0) chmin(f[i][j + k - 1], f[i][j] + a[k] + s[k]); if (j + k > 0 && k < c) chmin(f[min(i + (c - k), n)][j + k - 1], f[i][j] + a[min(i + (c - k), n)] + s[k]); } } } cout << f[n][0]; cerr << "Exec Time:" << (double)(clock() - stm) / CLOCKS_PER_SEC << "s" << endl; return 0; }
failed
117_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; int a[29]; void solve() { for (int i = 0; i <= 28; i++) { a[i] = 0; } vector<int> p; string s1, s2, s3; cin >> s1 >> s2 >> s3; if ((int)s1.size() != (int)s2.size()) { cout << "NO" << '\n'; } else if ((int)s1.size() != (int)s3.size()) { cout << "YES" << '\n'; } else { int num = 1; for (int i = 0; i < s1.size(); i++) { int num1 = s1[i] - 'a'; int num2 = s2[i] - 'a'; if (a[num1] == a[num2] && a[num1] == 0) { a[num1] = num; a[num2] = num; num++; } else if (a[num1] > 0 && a[num2] > 0) { for (int j = 0; j <= 27; j++) { if (a[j] == min(a[num1], a[num2])) { a[j] = max(a[num1], a[num2]); } } } else { a[num1] = max(a[num1], a[num2]); a[num2] = max(a[num1], a[num2]); } } int flag = 0; for (int i = 0; i < s1.size(); i++) { int num3 = s3[i] - 'a'; int num1 = s1[i] - 'a'; if (a[num3] == 0 || a[num3] != a[num1]) { flag = 1; break; } } if (flag) { cout << "YES" << '\n'; } else { cout << "NO" << '\n'; } } } signed main() { int T = 1; cin >> T; while (T--) { solve(); } }
failed
24_2_wrong.cpp
// Problem: $(PROBLEM) // Contest: $(CONTEST) // Judge: $(JUDGE) // URL: $(URL) // Memory Limit: $(MEMLIM) // Time Limit: $(TIMELIM) // Start: $(DATE) #include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' #ifdef DEBUG #define debug(x) cerr << #x << ": " << x << endl; #else #define debug(x) #endif int n, m, t; int calc(string s) { int status = 0, cnt = 0; for (char &ch : s) { if (ch == 'n') { if (status == 0 || status == 2 || status == 5) { status += 1; } else { status = 1; } } else if (ch == 'a') { if (status == 1) { status += 1; } else if (status == 3 || status == 6) { status = 2; } else { status = 0; } } else if (ch == 'j') { if (status == 3) { status += 1; } else { status = 0; } } else if (ch == 'i') { if (status == 4) { status += 1; } else { status = 0; } } else if (ch == 'g') { if (status == 6) { status = 0, cnt += 1; } else { status = 0; } } else { status = 0; } // cerr << status; } // cerr << endl; return cnt; } void solve() { cin >> n >> m; m %= n; string s; cin >> s; // cerr << s << endl; int mx = calc(s); // cerr << endl; for (int i = 1; i <= min(7ll, m); ++i) { s = s.substr(1) + s.substr(0, 1); // cerr << s << endl; mx = max(mx, calc(s)); // cerr << endl; } cout << mx << endl; // cerr << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0), cerr.tie(0); cin >> t; while (t--) solve(); }
failed
51_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; int add(int a, int b) { return a >= MOD - b ? a - MOD + b : a + b; } int sub(int a, int b) { return a >= b ? a - b : a - b + MOD; } int mul(int a, int b) { return (1ll * a * b) % MOD; } int pw(int x, int n) { int res = 1; while (n) { if (n % 2 == 0) { x = mul(x, x); n /= 2; } else { res = mul(res, x); --n; } } return res; } template <typename... Tail> int mul(int a, Tail... tail) { return mul(a, mul(tail...)); } template <typename... Tail> int add(int a, Tail... tail) { return add(a, add(tail...)); } vector<int> fact, invf; void init_fact(int n = 2'000'000) { int val = fact.size(); if (val >= n) return; if (val * 2 > n) n = val * 2; fact.resize(n); invf.resize(n); if (val == 0) { fact[0] = 1; invf[0] = 1; val = 1; } for (int i = val; i < n; ++i) { fact[i] = mul(fact[i - 1], i); } invf[n - 1] = pw(fact[n - 1], MOD - 2); for (int i = n - 1; i > val; --i) { invf[i - 1] = mul(invf[i], i); } } int inv(int n) { if (n > 20'000'000) { return pw(n, MOD - 2); } init_fact(n + 1); return mul(invf[n], fact[n - 1]); } int C(int n, int k) { init_fact(n + 1); if (k == -1) { if (n == -1) return 1; return 0; } if (k < 0) return 0; if (n < 0) { int res = C(k - n - 1, k); if (k & 1) return sub(0, res); return res; } if (k > n) return 0; return mul(fact[n], invf[k], invf[n - k]); } const int G1 = 3, MINSZ = 64; void fft(vector<int> &A, bool inv = false) { int k = 0; while ((1 << k) < (int)A.size()) ++k; int N = 1 << k; static vector<int> rev; static vector<uint32_t> power = {0, 1}; rev.resize(N); for (int i = 0; i < N; ++i) { rev[i] = rev[i / 2] / 2 + ((i & 1) << (k - 1)); if (i < rev[i]) swap(A[i], A[rev[i]]); } static auto mul = [](uint32_t a, uint32_t b) { return (uint64_t(a) * b) % MOD; }; static vector<uint32_t> A1; A1.resize(N); for (int i = 0; i < N; ++i) { A1[i] = A[i]; } for (int l = 1, t = 0; l < N; l *= 2, ++t) { if ((int)power.size() == l) { power.resize(2 * l); uint32_t w = pw(G1, (MOD - 1) / 2 / l); for (int i = l; i < 2 * l; ++i) { power[i] = power[i / 2]; if (i & 1) { power[i] = mul(power[i], w); } } } for (int i = 0; i < N; i += 2 * l) { for (int j = 0; j < l; ++j) { uint32_t x = A1[i + j], y = mul(power[j + l], A1[i + j + l]); if ((t & 3) == ((k - 1) & 3)) { A1[i + j] = (uint64_t(x) + y) % MOD; A1[i + j + l] = (uint64_t(x) + MOD - y) % MOD; } else { A1[i + j] = x + y; A1[i + j + l] = x + MOD - y; } } } } for (int i = 0; i < N; ++i) { A[i] = A1[i]; } if (inv) { reverse(A.begin() + 1, A.end()); int anti = pw(N, MOD - 2); for (int i = 0; i < N; ++i) A[i] = mul(A[i], anti); } } vector<int> operator+(const vector<int> &a, const vector<int> &b) { vector<int> ans(max(a.size(), b.size())); for (int i = 0; i < (int)a.size(); ++i) { ans[i] = a[i]; } for (int i = 0; i < (int)b.size(); ++i) { ans[i] = add(ans[i], b[i]); } return ans; } vector<int> operator-(const vector<int> &a, const vector<int> &b) { vector<int> ans(max(a.size(), b.size())); for (int i = 0; i < (int)a.size(); ++i) { ans[i] = a[i]; } for (int i = 0; i < (int)b.size(); ++i) { ans[i] = sub(ans[i], b[i]); } return ans; } vector<int> operator*(vector<int> a, vector<int> b) { if (a.empty()) return a; if (b.empty()) return b; int k = 0; int val = a.size() + b.size() - 1; if (val <= MINSZ || (int)min(a.size(), b.size()) <= 2) { vector<int> c(val); for (int i = 0; i < (int)a.size(); ++i) { for (int j = 0; j < (int)b.size(); ++j) { c[i + j] = add(c[i + j], mul(a[i], b[j])); } } return c; } while ((1 << k) < val) { ++k; } if ((MOD - 1) % (1 << k) != 0) { a.resize(max(a.size(), b.size())); b.resize(max(a.size(), b.size())); int m = a.size() / 2; vector<int> al(m), ar(a.size() - m); vector<int> bl(m), br(b.size() - m); for (int i = 0; i < (int)a.size(); ++i) { if (i < m) { al[i] = a[i]; bl[i] = b[i]; } else { ar[i - m] = a[i]; br[i - m] = b[i]; } } auto l = al * bl, mid = (al + ar) * (bl + br), r = ar * br; mid = mid - l - r; vector<int> ans(val); for (int i = 0; i < (int)l.size() && i < val; ++i) { ans[i] = l[i]; } for (int i = 2 * m; i < 2 * m + (int)r.size() && i < val; ++i) { ans[i] = add(ans[i], r[i - 2 * m]); } for (int i = m; i < m + (int)r.size() && i < val; ++i) { ans[i] = add(ans[i], mid[i - m]); } return ans; } vector<int> rem; if (val <= (1 << (k - 1)) + 5) { if (a.size() < b.size()) { swap(a, b); } rem.resize(val); int nsz = (int)a.size() - val + (1 << (k - 1)); for (int i = nsz; i < (int)a.size(); ++i) { for (int j = 0; j < (int)b.size(); ++j) { rem[i + j] = add(rem[i + j], mul(a[i], b[j])); } } a.resize(nsz); val = (1 << (k - 1)); --k; } a.resize(1 << k); b.resize(1 << k); if (a != b) { fft(a); fft(b); } else { fft(a); b = a; } for (int i = 0; i < (1 << k); ++i) { a[i] = mul(a[i], b[i]); } fft(a, 1); a.resize(val); if (!rem.empty()) { for (int i = 0; i < val; ++i) { rem[i] = add(rem[i], a[i]); } return rem; } return a; } vector<int> inv(const vector<int> &a, int n) { /// a[0] != 0 vector<int> b = {pw(a[0], MOD - 2)}; int cur = 1; static vector<int> a1; a1.clear(); while (cur < n) { cur *= 2; fill(a1.begin(), a1.end(), 0); a1.resize(2 * cur, 0); for (int i = 0; i < cur && i < (int)a.size(); ++i) { a1[i] = a[i]; } b.resize(2 * cur); fft(a1); fft(b); for (int i = 0; i < 2 * cur; ++i) { b[i] = add(b[i], sub(b[i], mul(b[i], b[i], a1[i]))); } fft(b, true); b.resize(cur); } b.resize(n); return b; } vector<int> mulT(vector<int> c, vector<int> a) { int sz = c.size(); int m = c.size() + 1 - a.size(); int k = 0; while ((1 << k) < sz) { ++k; } a.resize(1 << k); c.resize(1 << k); fft(a); fft(c, true); for (int i = 0; i < (1 << k); ++i) { c[i] = mul(a[i], c[i]); } fft(c); c.resize(m); return c; } vector<int> der(vector<int> a) { for (int i = 1; i < (int)a.size(); ++i) { a[i - 1] = mul(a[i], i); } a.pop_back(); if (a.empty()) a = {0}; return a; } vector<int> integ(vector<int> a) { a.push_back(0); init_fact(a.size()); for (int i = (int)a.size() - 1; i > 0; --i) { a[i] = mul(mul(a[i - 1], invf[i]), fact[i - 1]); } a[0] = 0; return a; } vector<int> ln(vector<int> a, int m) { /// a[0] = 1 vector<int> res = integ(der(a) * inv(a, max(m - 1, 1))); res.resize(m); return res; } void exp_step(vector<int> &f, vector<int> &g, const vector<int> &h) { int m = f.size(); static vector<int> hlp_g, hlp_f; hlp_g = g; hlp_g.resize(2 * m); fft(hlp_g); hlp_f = f; hlp_f.resize(2 * m); fft(hlp_f); for (int i = 0; i < 2 * m; ++i) { hlp_g[i] = add(hlp_g[i], sub(hlp_g[i], mul(hlp_f[i], hlp_g[i], hlp_g[i]))); } fft(hlp_g, true); g.resize(m); for (int i = 0; i < m; ++i) { g[i] = hlp_g[i]; } static vector<int> q; q.resize(m); fill(q.begin(), q.end(), 0); for (int i = 0; i < m - 1 && i + 1 < (int)h.size(); ++i) { q[i] = mul(h[i + 1], i + 1); } hlp_g = q; hlp_g.resize(2 * m); fft(hlp_g); for (int i = 0; i < 2 * m; ++i) { hlp_g[i] = mul(hlp_g[i], hlp_f[i]); } fft(hlp_g, true); hlp_g = der(f) - hlp_g; static vector<int> w; w = q + g * hlp_g; w.resize(2 * m - 1); w = integ(w); static vector<int> h1; h1.resize(2 * m); fill(h1.begin(), h1.end(), 0); for (int i = m; i < 2 * m && i < (int)h.size(); ++i) { h1[i - m] = h[i]; } for (int i = 0; i < m; ++i) { h1[i] = sub(h1[i], w[i + m]); } fft(h1); for (int i = 0; i < 2 * m; ++i) { h1[i] = mul(h1[i], hlp_f[i]); } fft(h1, true); f.resize(2 * m); for (int i = m; i < 2 * m; ++i) { f[i] = h1[i - m]; } } vector<int> exp(vector<int> h, int n) { /// h[0] = 0 vector<int> f = {1}, g = {1}; for (int m = 1; m < n; m *= 2) { exp_step(f, g, h); } f.resize(n); return f; } vector<int> operator*(vector<int> a, int x) { for (int &t : a) { t = mul(t, x); } return a; } vector<int> logexp_pow(vector<int> a, int64_t n, int m) { /// if a[0] == 0: int cnt0 = 0; while (cnt0 < (int)a.size() && a[cnt0] == 0) ++cnt0; if (cnt0 == (int)a.size()) { vector<int> ans(m, 0); if (n == 0) { ans[0] = 1; } return ans; } if (cnt0 > 0 && (n >= m || 1ll * cnt0 * n >= m)) { return vector<int>(m, 0); } m -= cnt0 * n; for (int i = 0; i < (int)a.size() - cnt0; ++i) { a[i] = a[i + cnt0]; } a.resize(a.size() - cnt0); /// now a[0] != 0 int val = a[0]; a = a * pw(val, MOD - 2); a = ln(a, m + 1) * (n % MOD); vector<int> res = exp(a, m) * pw(val, n); vector<int> ans(m + cnt0 * n); for (int i = 0; i < m; ++i) { ans[i + cnt0 * n] = res[i]; } return ans; } vector<int> get_exp(int n, int alph = 1) { /// exp(alph * x) init_fact(n); vector<int> ans(n); int cur = 1; for (int i = 0; i < n; ++i) { ans[i] = mul(invf[i], cur); cur = mul(cur, alph); } return ans; } vector<int> operator-(const vector<int> &a) { return a * (MOD - 1); } void dc(int l, int r, vector<int> &dp, const vector<int> &c) { if (r <= l) return; if (r - l == 1) { dp[l] = add(dp[l], sub(fact[l], fact[l - 1])); dp[l] = mul(dp[l], inv(sub(fact[l], c[l]))); return; } int m = (l + r) / 2; dc(l, m, dp, c); vector<int> val(m - l); vector<int> go(r - l - 1); for (int i = l; i < m; ++i) { val[i - l] = mul(dp[i], c[i]); } for (int i = 1; i < r - l; ++i) { go[i - 1] = fact[i]; } reverse(val.begin(), val.end()); auto res = mulT(go, val); for (int i = m; i < r; ++i) { dp[i] = add(dp[i], res[i - m]); } for (int i = l; i < m; ++i) { val[i - l] = mul(dp[i], fact[i]); } for (int i = 1; i < r - l; ++i) { go[i - 1] = c[i]; } reverse(val.begin(), val.end()); res = mulT(go, val); for (int i = m; i < r; ++i) { dp[i] = add(dp[i], res[i - m]); } dc(m, r, dp, c); } void solve_() { int n; cin >> n; init_fact(n + 1); vector<int> c = inv(fact, n); c = -c + vector<int>(1, 1); vector<int> dp(n + 1); dc(2, n + 1, dp, c); int ans = 0; int mx = 0; int lst = 0; for (int i = 1; i <= n; ++i) { int a; cin >> a; /// a = n + 1 - i; mx = max(mx, a); if (mx == i) { if (i - lst > 1) { ans = add(ans, dp[i - lst], 1); } lst = i; } } cout << ans << "\n"; } /// #define MULTITEST signed main() { #ifdef LOCAL freopen("../input.txt", "r", stdin); freopen("../output.txt", "w", stdout); #else ios_base::sync_with_stdio(false); cin.tie(0); #endif int tst = 1; #ifdef MULTITEST cin >> tst; #endif // MULTITEST while (tst--) { solve_(); } return 0; }
failed
71_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; // Holi c: // #define ll long long int #define ii __int128 #define fi first #define se second #define pb push_back #define all(v) v.begin(), v.end() typedef long long int ll; const int Inf = 2e9; const int Inf2 = 2e9 + 1; const ll mod = 1e9 + 7; const ll INF = 4e18; const ll INF2 = 1e10 + 1; using ld = long double; const ld eps = 1e-9, inf = numeric_limits<ld>::max(), pi = acos(-1); bool geq(ld a, ld b) { return a - b >= -eps; } bool leq(ld a, ld b) { return b - a >= -eps; } bool ge(ld a, ld b) { return a - b > eps; } bool le(ld a, ld b) { return b - a > eps; } bool eq(ld a, ld b) { return abs(a - b) <= eps; } bool neq(ld a, ld b) { return abs(a - b) > eps; } struct fraccion { ll num, den; fraccion() { num = 0, den = 1; } fraccion(ll x, ll y) { if (y < 0) x *= -1, y *= -1; ll d = __gcd(abs(x), abs(y)); num = x / d, den = y / d; } fraccion(ll v) { num = v; den = 1; } fraccion operator+(const fraccion &f) const { ll d = __gcd(den, f.den); return fraccion(num * (f.den / d) + f.num * (den / d), den * (f.den / d)); } fraccion operator-() const { return fraccion(-num, den); } fraccion operator-(const fraccion &f) const { return *this + (-f); } fraccion operator*(const fraccion &f) const { return fraccion(num * f.num, den * f.den); } fraccion operator/(const fraccion &f) const { return fraccion(num * f.den, den * f.num); } fraccion operator+=(const fraccion &f) { *this = *this + f; return *this; } fraccion operator-=(const fraccion &f) { *this = *this - f; return *this; } fraccion operator++(int xd) { *this = *this + 1; return *this; } fraccion operator--(int xd) { *this = *this - 1; return *this; } fraccion operator*=(const fraccion &f) { *this = *this * f; return *this; } fraccion operator/=(const fraccion &f) { *this = *this / f; return *this; } bool operator==(const fraccion &f) const { ll d = __gcd(den, f.den); return (ii)((ii)num * (f.den / d) == (ii)((ii)den / d) * f.num); } bool operator!=(const fraccion &f) const { ll d = __gcd(den, f.den); return (ii)((ii)num * (f.den / d) != (ii)((ii)den / d) * f.num); } bool operator>(const fraccion &f) const { ll d = __gcd(den, f.den); return (ii)((ii)num * (f.den / d) > (ii)((ii)den / d) * f.num); } bool operator<(const fraccion &f) const { ll d = __gcd(den, f.den); return (ii)((ii)num * (f.den / d) < (ii)((ii)den / d) * f.num); } bool operator>=(const fraccion &f) const { ll d = __gcd(den, f.den); return (ii)((ii)num * (f.den / d) >= (ii)((ii)den / d) * f.num); } bool operator<=(const fraccion &f) const { ll d = __gcd(den, f.den); return (ii)((ii)num * (f.den / d) <= (ii)((ii)den / d) * f.num); } fraccion inverso() const { return fraccion(den, num); } fraccion fabs() const { fraccion nueva; nueva.num = abs(num); nueva.den = den; return nueva; } long double value() const { return (long double)num / (long double)den; } }; bool geqf(fraccion a, fraccion b) { return a >= b; } bool leqf(fraccion a, fraccion b) { return a <= b; } bool gef(fraccion a, fraccion b) { return a > b; } bool lef(fraccion a, fraccion b) { return a < b; } bool eqf(fraccion a, fraccion b) { return a == b; } bool neqf(fraccion a, fraccion b) { return a != b; } struct point { ld x, y; point() : x(0), y(0) {} point(ld x, ld y) : x(x), y(y) {} point operator+(const point &p) const { return point(x + p.x, y + p.y); } point operator-(const point &p) const { return point(x - p.x, y - p.y); } point operator*(const ld &k) const { return point(x * k, y * k); } point operator/(const ld &k) const { return point(x / k, y / k); } point perp() const { return point(-y, x); } ld dot(const point &p) const { return x * p.x + y * p.y; } ld cross(const point &p) const { return x * p.y - y * p.x; } ld norm() const { return x * x + y * y; } ld length() const { return sqrtl(x * x + y * y); } point unit() const { return (*this) / length(); } bool operator==(const point &p) const { return eq(x, p.x) && eq(y, p.y); } bool operator!=(const point &p) const { return !(*this == p); } bool operator<(const point &p) const { return le(x, p.x) || (eq(x, p.x) && le(y, p.y)); } bool operator>(const point &p) const { return ge(x, p.x) || (eq(x, p.x) && ge(y, p.y)); } }; istream &operator>>(istream &is, point &p) { return is >> p.x >> p.y; } ostream &operator<<(ostream &os, const point &p) { return os << "(" << p.x << ", " << p.y << ")"; } struct pointf { fraccion x, y; pointf() : x(fraccion()), y(fraccion()) {} pointf(fraccion x, fraccion y) : x(x), y(y) {} pointf operator+(const pointf &p) const { return pointf(x + p.x, y + p.y); } pointf operator-(const pointf &p) const { return pointf(x - p.x, y - p.y); } pointf operator*(const fraccion &k) const { return pointf(x * k, y * k); } pointf operator/(const fraccion &k) const { return pointf(x / k, y / k); } pointf operator+=(const pointf &p) { *this = *this + p; return *this; } pointf operator-=(const pointf &p) { *this = *this - p; return *this; } pointf operator*=(const fraccion &p) { *this = *this * p; return *this; } pointf operator/=(const fraccion &p) { *this = *this / p; return *this; } fraccion dot(const pointf &p) const { return x * p.x + y * p.y; } fraccion cross(const pointf &p) const { return x * p.y - y * p.x; } fraccion norm() const { return x + y; } bool operator==(const pointf &p) const { return eqf(x, p.x) && eqf(y, p.y); } bool operator!=(const pointf &p) const { return !(*this == p); } bool operator<(const pointf &p) const { return lef(x, p.x) || (eqf(x, p.x) && lef(y, p.y)); } bool operator>(const pointf &p) const { return gef(x, p.x) || (eqf(x, p.x) && gef(y, p.y)); } }; int sgn(ld x) { if (ge(x, 0)) return 1; if (le(x, 0)) return -1; return 0; } vector<point> convexHull(vector<point> P) { sort(P.begin(), P.end()); vector<point> L, U; for (int i = 0; i < P.size(); i++) { while (L.size() >= 2 && leq((L[L.size() - 2] - P[i]).cross(L[L.size() - 1] - P[i]), 0)) { L.pop_back(); } L.push_back(P[i]); } for (int i = P.size() - 1; i >= 0; i--) { while (U.size() >= 2 && leq((U[U.size() - 2] - P[i]).cross(U[U.size() - 1] - P[i]), 0)) { U.pop_back(); } U.push_back(P[i]); } L.pop_back(); U.pop_back(); L.insert(L.end(), U.begin(), U.end()); return L; } vector<vector<point>> twoSidesCH(vector<point> P) { sort(P.begin(), P.end()); vector<vector<point>> L; vector<point> U; for (int i = 0; i < P.size(); i++) { while (U.size() >= 2 && leq((U[U.size() - 2] - P[i]).cross(U[U.size() - 1] - P[i]), 0)) { U.pop_back(); } U.push_back(P[i]); } if (eq(U[U.size() - 1].x, U[U.size() - 2].x)) U.pop_back(); L.pb(U); U.clear(); for (int i = P.size() - 1; i >= 0; i--) { while (U.size() >= 2 && leq((U[U.size() - 2] - P[i]).cross(U[U.size() - 1] - P[i]), 0)) { U.pop_back(); } U.push_back(P[i]); } reverse(all(U)); if (eq(U[U.size() - 1].x, U[U.size() - 2].x)) U.pop_back(); reverse(all(U)); if (eq(U[U.size() - 1].x, U[U.size() - 2].x)) U.pop_back(); L.pb(U); return L; } ld area(vector<point> &P) { int n = P.size(); ld ans = 0; for (int i = 0; i < n; i++) { ans += P[i].cross(P[(i + 1) % n]); } return abs(ans / 2); } pointf intersectLinesf(const point &a1, const point &v1, const point &a2, const point &v2) { fraccion det = v1.cross(v2); pointf b1(a1.x, a1.y), u1(v1.x, v1.y), b2(a2.x, a2.y), u2(v2.x, v2.y); return b1 + u1 * ((b2 - b1).cross(u2) / det); } point intersectLines(const point &a1, const point &v1, const point &a2, const point &v2) { // lines a1+tv1, a2+tv2 // assuming that they intersect ld det = v1.cross(v2); return a1 + v1 * ((a2 - a1).cross(v2) / det); } bool pointInLine(const point &a, const point &v, const point &p) { return eq((p - a).cross(v), 0); } bool pointInSegment(const point &a, const point &b, const point &p) { return pointInLine(a, b - a, p) && leq((a - p).dot(b - p), 0); } int intersectSegmentsInfo(const point &a, const point &b, const point &c, const point &d) { // segment ab, segment cd point v1 = b - a, v2 = d - c; int t = sgn(v1.cross(c - a)), u = sgn(v1.cross(d - a)); if (t == u) { if (t == 0) { if (pointInSegment(a, b, c) || pointInSegment(a, b, d) || pointInSegment(c, d, a) || pointInSegment(c, d, b)) { return -1; // infinity points } else { return 0; // no point } } else { return 0; // no point } } else { return sgn(v2.cross(a - c)) != sgn(v2.cross(b - c)); // 1: single point, 0: no point } } bool pointInPerimeter(const vector<point> &P, const point &p) { int n = P.size(); for (int i = 0; i < n; i++) { if (pointInSegment(P[i], P[(i + 1) % n], p)) { return true; } } return false; } bool crossesRay(const point &a, const point &b, const point &p) { return (geq(b.y, p.y) - geq(a.y, p.y)) * sgn((a - p).cross(b - p)) > 0; } int pointInPolygon(const vector<point> &P, const point &p) { if (pointInPerimeter(P, p)) { return 1; } int n = P.size(); int rays = 0; for (int i = 0; i < n; i++) { rays += crossesRay(P[i], P[(i + 1) % n], p); } return rays & 1; } int intersectLineSegmentInfo(const point &a, const point &v, const point &c, const point &d) { // line a+tv, segment cd point v2 = d - c; ld det = v.cross(v2); if (eq(det, 0)) { if (eq((c - a).cross(v), 0)) { return -1; // infinity points } else { return 0; // no point } } else { return sgn(v.cross(c - a)) != sgn(v.cross(d - a)); // 1: single point, 0: no point } } vector<point> cutPolygon(const vector<point> &P, const point &a, const point &v) { // returns the part of the convex polygon P on the left side of line a+tv int n = P.size(); vector<point> lhs; for (int i = 0; i < n; ++i) { if (geq(v.cross(P[i] - a), 0)) { lhs.push_back(P[i]); } if (intersectLineSegmentInfo(a, v, P[i], P[(i + 1) % n]) == 1) { point p = intersectLines(a, v, P[i], P[(i + 1) % n] - P[i]); if (p != P[i] && p != P[(i + 1) % n]) { lhs.push_back(p); } } } return lhs; } vector<point> tangentsPointPolygon(const vector<point> &P, const vector<vector<point>> &Ps, const point &p) { int n = P.size(), m = Ps[0].size(), k = Ps[1].size(); int lk = m; if (Ps[0][m - 1] == Ps[1][0]) lk--; auto tang = [&](int l, int r, ld w, int kl) -> int { int res = min(l, r); while (l <= r) { int m = (l + r) / 2; ld a = (P[(m + kl) % n] - p).cross(P[(m + 1 + kl) % n] - p) * w, b = (P[(m + kl) % n] - p).cross(P[(m - 1 + n + kl) % n] - p) * w; if (geq(a, 0) && geq(b, 0)) return m; if (geq(a, 0)) r = m - 1, res = m; else l = m + 1; } return res; }; auto bs = [&](int l, int r, const vector<point> &A, ld w) -> int { int res = l; ld w1 = p.x * w; while (l <= r) { int m = (l + r) / 2; if (ge(A[m].x * w, w1)) r = m - 1; else res = m, l = m + 1; } return res; }; point left = p, rigth = p; int t1 = bs(0, m - 1, Ps[0], 1), t2 = bs(0, k - 1, Ps[1], -1); auto u1 = tang(0, t1, -1, 0), u2 = tang(0, t2, -1, lk); auto v1 = tang(t1 + 1, m - 1, 1, 0), v2 = tang(t2 + 1, k - 1, 1, lk); if (p.x == P[t1].x) v1 = tang(t1, m - 1, 1, 0); if (p.x == P[(lk + t2) % n].x) v2 = tang(t2, k - 1, 1, lk); if (leq((P[u1] - p).cross(P[(u1 - 1 + n) % n] - p), 0) && leq((P[u1] - p).cross(P[(u1 + 1) % n] - p), 0)) left = P[u1]; else if (leq((P[(lk + u2) % n] - p).cross(P[(lk + u2 - 1 + n) % n] - p), 0) && leq((P[(lk + u2) % n] - p).cross(P[(lk + u2 + 1) % n] - p), 0)) left = P[(lk + u2) % n]; if (geq((P[v1] - p).cross(P[(v1 - 1 + n) % n] - p), 0) && geq((P[v1] - p).cross(P[(v1 + 1) % n] - p), 0)) rigth = P[v1]; else if (geq((P[(lk + v2) % n] - p).cross(P[(lk - 1 + n + v2) % n] - p), 0) && geq((P[(lk + v2) % n] - p).cross(P[(lk + 1 + v2) % n] - p), 0)) rigth = P[(lk + v2) % n]; return {left, rigth}; } vector<point> tangentsLineal(vector<point> A, point p) { int iz = 0, dr = 0, n = A.size(); for (int i = 0; i < n; i++) { int at = i - 1, nx = i + 1; if (at < 0) at = n - 1; if (nx == n) nx = 0; if (leq((A[i] - p).cross(A[at] - p), 0) && leq((A[i] - p).cross(A[nx] - p), 0)) iz = i; if (geq((A[i] - p).cross(A[at] - p), 0) && geq((A[i] - p).cross(A[nx] - p), 0)) dr = i; } return {A[iz], A[dr]}; } pair<fraccion, fraccion> calc(vector<point> &A, vector<point> B, vector<vector<point>> &tws, int l, int r) { fraccion minx = Inf2, maxx = -Inf2; pointf x0(-Inf2, 0), x1(Inf2, 0); point s0(-Inf2, 0), s1(Inf2, 0); point w1(l, 0), w2(r, 0); int n = B.size(); for (int i = 0; i < n; i++) { auto u = intersectSegmentsInfo(B[i], B[(i + 1) % n], s0, s1); if (u == 1) { auto p = intersectLinesf(B[i], B[(i + 1) % n] - B[i], s0, s1 - s0); // minx = min(minx, p.x); maxx = max(maxx, p.x); } else if (u == -1) { // minx = min(minx, min(fraccion(B[i].x), fraccion(B[(i + 1) % n].x))); // maxx = max(maxx, max(fraccion(B[i].x), fraccion(B[(i + 1) % n].x))); } } point a1(1, 0); for (int i = 0; i < n; i++) { auto ts = tangentsPointPolygon(A, tws, B[i]); point t1 = ts[0], t2 = ts[1]; // cout<<B[i]<<" => "<<t1<<" & "<<t2<<'\n'; if (neq(a1.cross(t1 - B[i]), 0)) { auto p1 = intersectLinesf(B[i], t1 - B[i], point(0, 0), a1); auto p2 = intersectLines(B[i], t1 - B[i], point(0, 0), a1); bool pl = true; point p(p1.x.value(), p1.y.value()); // cout<<p.x.value()<<" , "<<p.y.value()<<" | "; if (eq((p - t1).length(), (p - B[i]).length() + (B[i] - t1).length()) && pl) { // cout<<"r"; if (minx > p1.x) minx = p1.x; if (maxx < p1.x) maxx = p1.x; } } if (neq(a1.cross(t2 - B[i]), 0)) { auto p1 = intersectLinesf(B[i], t2 - B[i], point(0, 0), a1); auto p2 = intersectLines(B[i], t2 - B[i], point(0, 0), a1); bool pl = true; point p(p1.x.value(), p1.y.value()); // cout<<p.x.value()<<" , "<<p.y.value()<<'\n'; if (eq((p - t2).length(), (p - B[i]).length() + (B[i] - t2).length()) && pl) { // cout<<"r"; if (minx > p1.x) minx = p1.x; if (maxx < p1.x) maxx = p1.x; } } // if(ge((t1 - B[i]).cross(w1 - B[i]), 0) && le((t2 - B[i]).cross(w1 - // B[i]), 0)) minx = -Inf2; if(ge((t1 - B[i]).cross(w2 - B[i]), 0) && le((t2 // - B[i]).cross(w2 - B[i]), 0)) maxx = Inf2; } auto itg0 = tangentsPointPolygon(A, tws, w1); auto itg1 = tangentsLineal(B, w1); auto dtg0 = tangentsPointPolygon(A, tws, w2); auto dtg1 = tangentsLineal(B, w2); auto t0 = itg0[0], t1 = itg0[1], t2 = itg1[0], t3 = itg1[1]; auto t4 = dtg0[0], t5 = dtg0[1], t6 = dtg1[0], t7 = dtg1[1]; auto v = B; v = cutPolygon(v, w1, w1 - t0); v = cutPolygon(v, w1, t1 - w1); v = cutPolygon(v, t1, t0 - t1); if (area(v) > 0.0001) { minx = -Inf2; } /* if(leq((t2 - w1).cross(t0 - w1), 0) && geq((t3 - w1).cross(t0 - w1), 0)) minx = -Inf2; if(leq((t2 - w1).cross(t1 - w1), 0) && geq((t3 - w1).cross(t1 - w1), 0)) minx = -Inf2; if(leq((t0 - w1).cross(t2 - w1), 0) && geq((t1 - w1).cross(t2 - w1), 0)) minx = -Inf2; */ v = B; v = cutPolygon(v, w2, w2 - t4); v = cutPolygon(v, w2, t5 - w2); v = cutPolygon(v, t5, t4 - t5); if (area(v) > 0.0001) { maxx = Inf2; } /* if(leq((t6 - w2).cross(t4 - w2), 0) && geq((t7 - w2).cross(t4 - w2), 0)) maxx = Inf2; if(leq((t6 - w2).cross(t5 - w2), 0) && geq((t7 - w2).cross(t5 - w2), 0)) maxx = Inf2; if(leq((t4 - w2).cross(t6 - w2), 0) && geq((t5 - w2).cross(t6 - w2), 0)) maxx = Inf2; */ // cout<<minx.value()<<" "<<maxx.value()<<'\n'; // if(minx == -Inf2 && maxx == Inf2) return {Inf2, Inf2}; return {minx, maxx}; } ld calc1() { int m, l, r; cin >> m >> l >> r; int n; cin >> n; vector<point> P(n); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; P[i] = point(a, b); } vector<vector<point>> Moons; for (int i = 0; i < m; i++) { int k; cin >> k; vector<point> aux; for (int j = 0; j < k; j++) { int a, b; cin >> a >> b; aux.pb(point(a, b)); } Moons.pb(aux); } P = convexHull(P); auto tws = twoSidesCH(P); // for(auto e : tws[1]) cout<<e<<" "; cout<<'\n'; // for(auto e : tw) cout<<e<<" "; cout<<'\n'; vector<pair<fraccion, fraccion>> Segs; for (int i = 0; i < m; i++) { auto u = calc(P, Moons[i], tws, l, r); // cout<<'\n'<<"----------------------------------------"<<'\n'; Segs.pb(u); } sort(all(Segs)); // for(auto e : Segs) cout<<setprecision(15)<<e.fi.value()<<" , // "<<e.se.value()<<" | "; cout<<'\n'; cout<<(fraccion(Segs[0].se) - // fraccion(Segs[0].fi)).value()<<" "; ld ans = 0; fraccion ant(l); bool f = false; for (int i = 0; i < m; i++) { if (Segs[i].fi > fraccion(r)) { if (fraccion(r) > ant) { f = true; } if (ant < fraccion(r)) ans += (fraccion(r).value() - ant.value()); ant = r; break; } if (Segs[i].fi >= ant) { if (!((Segs[i].fi == l && ant == l) || (Segs[i].fi == r && ant == r))) f = true; // cout<<(Segs[i].fi - ant).value(); } if (ant < Segs[i].fi) ans += abs(ant.value() - Segs[i].fi.value()); ant = max(ant, Segs[i].se); // cout<<ans<<'\n'; } if (ant < r) ans += (fraccion(r).value() - max(ant.value(), fraccion(l).value())), f = true; if (!f && ans == 0) return -1; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { auto p = calc1(); cout << setprecision(25) << p << '\n'; } }
failed
26_1_wrong.cpp
#include <bits/stdc++.h> // #include<bits/stdc--.h> using i64 = long long; #define all(x) std::begin(x), std::end(x) /* 感觉点双一下就结束了? */ struct node_bcc { int n, id, tp, bcc_n; std::vector<std::vector<std::pair<int, int>>> e; std::vector<std::vector<int>> bcc_node, bcc_edge; std::vector<int> dfn, low, st, ed, blk, ct; node_bcc(const std::vector<std::vector<std::pair<int, int>>> &e, int m) : n(e.size()), id(0), tp(0), bcc_n(0), e(e), dfn(n, -1), low(n, -1), st(m), ed(m), blk(m), ct(n) { for (int i = 0; i < n; i++) if (dfn[i] == -1) dfs(i, 1); bcc_node.resize(bcc_n); for (int i = 0; i < n; i++) for (auto [v, w] : e[i]) bcc_node[blk[w]].push_back(i); std::vector<int> flg(n); for (auto &v : bcc_node) { std::vector<int> t; for (int x : v) if (!std::exchange(flg[x], 1)) t.push_back(x); std::swap(t, v); for (int x : v) flg[x] = 0; } for (int i = 0; i < n; i++) if (e[i].size() == 0) { bcc_node.push_back({i}); bcc_edge.push_back({}); ++bcc_n; } } void dfs(int u, bool rt) { dfn[u] = low[u] = id++; int cnt = 0; for (auto [v, w] : e[u]) if (!ed[w]) { st[tp++] = w; ed[w] = 1; if (dfn[v] == -1) { dfs(v, 0); ++cnt; low[u] = std::min(low[u], low[v]); if (dfn[u] <= low[v]) { ct[u] = cnt > rt; bcc_edge.push_back({}); // cpp11+ do { bcc_edge[bcc_n].push_back(st[--tp]); blk[st[tp]] = bcc_n; } while (st[tp] != w); ++bcc_n; } } else low[u] = std::min(low[u], dfn[v]); } } }; /* 枚举每个点双分量,检查边数是否等于点数 但是可能出现一种奇怪的点双分量 边数不等于点数,但是内部生成的每个环的大小均一致 点双真能做吗?感觉很难 这个特殊的点双,如何判断 */ void solve() { int n, m; std::cin >> n >> m; // if (m == 0) { // std::cout << "Yes\n"; // return; // } std::vector<std::pair<int, int>> edgs(m); std::vector edg(n, std::vector<std::pair<int, int>>()); for (int i = 0; i < m; i++) { int x, y; std::cin >> x >> y; x--; y--; edg[x].push_back({y, i}); edg[y].push_back({x, i}); edgs[i] = {x, y}; ; } node_bcc nbcc(edg, m); std::set<int> H; std::vector<int> cnt(n), fa(n), vis(n); for (int i = 0; i < nbcc.bcc_n; i++) { if (nbcc.bcc_edge[i].size() == 1 || nbcc.bcc_node[i].size() == 1) { continue; } if (nbcc.bcc_edge[i].size() == nbcc.bcc_node[i].size()) { H.insert(nbcc.bcc_node[i].size()); } else { std::set<int> app; for (auto j : nbcc.bcc_node[i]) app.insert(j); std::vector<int> ds; for (auto j : nbcc.bcc_node[i]) { cnt[j] = 0; } for (auto j : nbcc.bcc_edge[i]) { auto [x, y] = edgs[j]; cnt[x]++; cnt[y]++; } for (auto j : nbcc.bcc_node[i]) { if (cnt[j] > 2) { ds.push_back(j); } cnt[j] = 0; vis[j] = 0; fa[j] = 1e8; } if (ds.size() != 2) { std::cout << "No\n"; return; } // std::set<int> vis;// L, std::queue<std::pair<int, int>> q; vis[ds[0]] = 1; // vis.insert(ds[0]); for (auto [nxt, i] : edg[ds[0]]) { q.push({nxt, 1}); // vis.insert(nxt); vis[nxt] = 1; fa[nxt] = ds[0]; } while (q.size()) { auto [now, w] = q.front(); q.pop(); if (now == ds[1]) { // L.insert(w); H.insert(2 * w); continue; } for (auto [nxt, id] : edg[now]) { if (!app.contains(nxt) || nxt == fa[now]) continue; if (vis[nxt]) { // L.insert(w + 1); H.insert(2 * w + 2); continue; } vis[nxt] = 1; fa[nxt] = now; q.push({nxt, w + 1}); } } // if (L.size() > 1) { // std::cout << "No\n"; // return; // } else if (L.size() == 1) { // H.insert(2 ** L.begin()); // } } } if (H.size() > 1) { std::cout << "No\n"; } else { // dbg(H); std::cout << "Yes\n"; } } signed main() { std::cin.tie(0)->sync_with_stdio(0); int t = 1; std::cin >> t; while (t--) solve(); return 0; } /* 7 5 6 1 2 2 3 3 1 1 4 4 5 5 1 2 1 1 2 5 6 1 2 2 3 3 1 2 4 3 5 4 5 4 5 1 2 2 3 3 1 2 4 4 1 5 6 1 2 2 3 1 4 1 5 4 3 5 3 8 10 1 2 2 3 1 4 1 5 4 3 5 3 2 6 2 7 8 6 8 7 1 0 */
failed
70_2_wrong.cpp
#include <bits/stdc++.h> #define ll long long using namespace std; inline ll read() { char c = getchar(); ll a = 0, b = 1; for (; c < '0' || c > '9'; c = getchar()) if (c == '-') b *= -1; for (; c >= '0' && c <= '9'; c = getchar()) a = a * 10 + c - '0'; return a * b; } constexpr ll MAXN = 200001; ll n, a[100001], k, b[100001]; ll f[MAXN][20][2], Logn[MAXN + 1]; void pre() { // 准备工作,初始化 Logn[1] = 0; Logn[2] = 1; for (ll i = 3; i < MAXN; i++) { Logn[i] = Logn[i / 2] + 1; } } ll ask(ll x, ll y) { if (x > y) return -1; ll s = Logn[y - x + 1]; if (f[x][s][0] < f[y - (1 << s) + 1][s][0]) return f[x][s][1]; else return f[y - (1 << s) + 1][s][1]; } ll dfs(ll l, ll r, ll now, ll fa, ll x) { // cout<<l<<' '<<r<<' '<<x<<endl; if (fa != 0 && (b[now] + x) % (b[fa] + x) != 0) return 0; if (l >= r) return 1; ll Minid = ask(l, r); ll lminid = ask(l, Minid - 1); ll rminid = ask(Minid + 1, r); ll ans = 1; if (lminid != -1) ans = ans * dfs(l, Minid - 1, lminid, Minid, x); if (rminid != -1) ans = ans * dfs(Minid + 1, r, rminid, Minid, x); return ans; } /* 1 5 10 7 79 1 7 1 */ int main() { ll T = read(); pre(); while (T--) { n = read(); k = read(); for (ll i = 1; i <= n; i++) { f[i][0][0] = b[i] = a[i] = read(); f[i][0][1] = i; } sort(a + 1, a + 1 + n); for (ll j = 1; j <= 20; j++) for (ll i = 1; i + (1 << j) - 1 <= n; i++) { f[i][j][0] = min(f[i][j - 1][0], f[i + (1 << (j - 1))][j - 1][0]); if (f[i][j - 1][0] <= f[i + (1 << (j - 1))][j - 1][0]) f[i][j][1] = f[i][j - 1][1]; else f[i][j][1] = f[i + (1 << (j - 1))][j - 1][1]; } ll Gcd = 0; for (ll i = 1; i < n; i++) { Gcd = __gcd(Gcd, a[i + 1] - a[i]); } if (Gcd == 0) { cout << k << ' ' << 1LL * (1 + k) * k / 2 << '\n'; continue; } ll ans = 0, sum = 0; vector<ll> div; for (ll i = 1; i * i <= Gcd; i++) { if (Gcd % i == 0) { div.push_back(i); if (i * i != Gcd) div.push_back(Gcd / i); } } for (ll i = 0; i < div.size(); i++) { if (div[i] > k || div[i] <= a[1]) continue; if (dfs(1, n, ask(1, n), 0, div[i] - a[1]) == 1) { ans++, sum += div[i] - a[1]; } } cout << ans << ' ' << sum << '\n'; } return 0; }
failed
33_1_wrong.cpp