text
stringlengths
10
22.5k
status
stringclasses
2 values
name
stringlengths
9
26
#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 vector<pair<char, int>> moves; string OS, GS, RS; void fix(string& AS, string& BS, char ac, char bc) { auto do_swap = [&](int i, int j) -> void { ++i; ++j; // cerr << "pre:\n"; // cerr << AS << ' ' << BS << ' ' << ac << ' ' << bc << ' ' << i << ' ' << j << '\n'; if (i != 1) { int shift = (11-i)%10; moves.push_back({ac, shift}); shift = 10-shift; string as = ""; for (int ii = shift; ii < 10; ++ii) { as.push_back(AS[ii]); } for (int ii = 0; ii < shift; ++ii) { as.push_back(AS[ii]); } AS = as; } if (j != 4) { int shift = (14-j)%10; moves.push_back({bc, shift}); shift = 10-shift; string bs = ""; for (int ii = shift; ii < 10; ++ii) { bs.push_back(BS[ii]); } for (int ii = 0; ii < shift; ++ii) { bs.push_back(BS[ii]); } BS = bs; } // cerr << AS << ' ' << BS << '\n'; moves.push_back({'c', 2}); moves.push_back({bc, 9}); moves.push_back({'c', 1}); char rem_a = AS[0]; char rem_b = BS[3]; AS.erase(0, 1); BS.erase(3, 1); BS.push_back(rem_a); AS.insert(2, 1, rem_b); // cerr << "post:\n"; // cerr << AS << ' ' << BS << ' ' << ac << ' ' << bc << ' ' << i << ' ' << j << '\n'; }; for (int i = 0; i < AS.size(); ++i) { if (AS[i] == bc) { // need to swap! int j = -1; for (int jj = 0; jj < BS.size(); ++jj) { if (BS[jj] == ac) { j = jj; break; } } if (j == -1) { for (int jj = 0; jj < BS.size(); ++jj) { if (BS[jj] != bc) { j = jj; break; } } } assert(j != -1); // swap AS i and BS j do_swap(i, j); fix(AS, BS, ac, bc); return; } } for (int j = 0; j < BS.size(); ++j) { if (BS[j] == ac) { int i = -1; for (int ii = 0; ii < AS.size(); ++ii) { if (AS[ii] == bc) { i = ii; break; } } if (i == -1) { for (int ii = 0; ii < AS.size(); ++ii) { if (AS[ii] != ac) { i = ii; break; } } } assert(i != -1); do_swap(i, j); fix(AS, BS, ac, bc); return; } } } signed main() { cin.tie(0)->sync_with_stdio(0); cin >> OS >> GS >> RS; fix(RS, OS, 'r', 'o'); fix(OS, GS, 'o', 'g'); fix(GS, RS, 'g', 'r'); assert(moves.size() <= 240); cout << moves.size() << '\n'; for (auto [c,i] : moves) { cout << c << i << '\n'; } }
passed
104_std.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--; } if ((s + 1) * (s + 1) * (s + 1) <= 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; }
passed
64_std.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 n; cin >> n; for(int i = 1;i <= n;i++) cin >> a[i]; set<int> s; for(int i = 2;i <= n;i++) { s.insert(a[i - 1] & a[i]); s.insert(a[i] & (a[i] ^ a[i - 1])); s.insert(a[i]); s.insert(a[i - 1] & (a[i] ^ a[i - 1])); s.insert(a[i - 1]); s.insert(a[i] ^ a[i - 1]); s.insert(a[i] | a[i - 1]); s.insert(0); } cout << s.size() << "\n"; } signed main() { ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); int t = 1; // cin >> t; while(t--) solve(); return 0; }
passed
6_std.cpp
#include <bits/stdc++.h> using namespace std; #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 double EPS = 1e-10; template <class T> int sgn(T x) { return (x > 0) - (x < 0); } template<class T> struct Point { typedef Point P; T x, y; explicit Point(T xx=0, T yy=0) : x(xx), y(yy) {} bool operator<(P p) const { return tie(x,y) < tie(p.x,p.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); } P operator*(T d) const { return P(x*d, y*d); } T dist2() const { return x*x + y*y; } double dist() const { return sqrt((double)dist2()); } P unit() const { return *this/dist(); } // makes dist()=1 P perp() const { return P(-y, x); } // rotates +90 degrees P normal() const { return perp().unit(); } friend ostream& operator<<(ostream& os, P p) { return os << "(" << p.x << "," << p.y << ")"; } }; typedef Point<double> P; bool circleInter(P a,P b,double r1,double r2,pair<P, P>* out) { if (a == b) { assert(r1 != r2); return false; } P vec = b - a; double d2 = vec.dist2(), sum = r1+r2, dif = r1-r2, p = (d2 + r1*r1 - r2*r2)/(d2*2), h2 = r1*r1 - p*p*d2; if (sum*sum < d2 || dif*dif > d2) return false; P mid = a + vec*p, per = vec.perp() * sqrt(fmax(0, h2) / d2); *out = {mid + per, mid - per}; return true; } void solve() { int n; cin >> n; vector<Point<double>> pts; vector<long long> rads(n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y >> rads[i]; pts.emplace_back(x, y); } if (n == 1) { cout << "YES\n"; return; } bool found = false; string res(n, '0'); for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { long long mn = min(rads[i], rads[j]), mx = max(rads[i], rads[j]); double c_dist = (pts[j] - pts[i]).dist(); long long in_dist2 = (c_dist + mn) * (c_dist + mn); long long rad2 = (rads[i] + rads[j]) * (rads[i] + rads[j]); if ((pts[j] - pts[i]).dist2() > rad2 - EPS) { found = true; res[i] = '1'; res[j] = '1'; break; } else if (in_dist2 < mx * mx + EPS) { found = true; if (rads[i] == mn) res[i] = '1'; else res[j] = '1'; break; } } if (found) break; } if (!found) { unordered_set<string> hues; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { pair<Point<double>, Point<double>> intersect; circleInter(pts[i], pts[j], rads[i], rads[j], &intersect); std::function<void(Point<double>)> find_inters; find_inters = [&](Point<double> ipt) { vector<int> contain; for (int k = 0; k < n; k++) { if (k != i && k != j) { if ((pts[k] - ipt).dist2() < rads[k] * rads[k]) { contain.push_back(k); } } } string cur_hue(n, '0'); for (int ind : contain) { cur_hue[ind] = '1'; } hues.insert(cur_hue); cur_hue[i] = '1'; hues.insert(cur_hue); cur_hue[j] = '1'; hues.insert(cur_hue); cur_hue[i] = '0'; hues.insert(cur_hue); }; find_inters(intersect.first); find_inters(intersect.second); } } int lim = 1 << (min(n, 25)); for (int mask = 0; mask < lim; mask++) { string cur_hue(n, '0'); for (int i = 0; i < n; i++) { if (mask & (1 << i)) cur_hue[i] = '1'; } if (!hues.count(cur_hue)) { found = true; res = cur_hue; break; } } } if (found) { cout << "NO\n"; for (int i = 0; i < n; i++) { cout << res[i] << " "; } cout << "\n"; } else { cout << "YES\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } }
passed
96_std.cpp
#include <algorithm> #include <cassert> #include <cmath> #include <iostream> #include <ranges> #include <utility> #include <set> #include <vector> using i64 = long long; constexpr i64 M = 1e9 + 7; struct pvec { int x, y; friend std::istream &operator>>(std::istream &is, pvec &a) { return is >> a.x >> a.y; } friend std::ostream &operator<<(std::ostream &os, const pvec &a) { return os << a.x << ' ' << a.y; } bool operator==(const pvec &a) const { return x == a.x && y == a.y; } pvec operator-(const pvec &a) const { return {x - a.x, y - a.y}; } bool operator<(const pvec &a) const { return x == a.x ? y < a.y : x < a.x; } }; int sign(i64 val) { return val < 0 ? -1 : (val > 0 ? 1 : 0); } i64 cross(const pvec &a, const pvec &b) { return 1ll * a.x * b.y - 1ll * a.y * b.x; } i64 cross(const pvec &a, const pvec &b, const pvec &c) { return cross(b - a, c - a); } i64 dot(const pvec &a, const pvec &b) { return 1ll * a.x * b.x + 1ll * a.y * b.y; } int scross(const pvec &a, const pvec &b, const pvec &c) { return sign(cross(a, b, c)); } i64 sqrlen(const pvec &a) { return dot(a, a); } auto convex_hull(const std::vector<pvec> &x) { std::vector<pvec> v(x); std::vector<pvec> used, unused; std::sort(v.begin(), v.end()); int m = v.size(), tp = -1; for (int i = 0; i < m; i++) { while (tp > 0 && cross(used[tp-1], used[tp], v[i]) <= 0) tp--, used.pop_back(); used.push_back(v[i]), tp++; } int t = tp; for (int i = m - 1; i >= 0; i--) { while (tp > t && cross(used[tp-1], used[tp], v[i]) <= 0) { tp--; used.pop_back(); } used.push_back(v[i]), tp++; } used.pop_back(); std::set<pvec> s; for(auto d: used) s.insert(d); for(auto d: v) if (!s.contains(d)) unused.push_back(d); return std::pair{used, unused}; } bool comp(const pvec &a, const pvec &b) { bool upA = a.y > 0 || (a.y == 0 && a.x >= 0); bool upB = b.y > 0 || (b.y == 0 && b.x >= 0); if (upA != upB) return upA; auto val = cross(a, b); return val > 0; } i64 area(const std::vector<pvec> &p) { i64 res = 0; for (int i = 0, m = p.size(); i < m; i++) res += cross(p[i], p[(i + 1) % m]); return res; } void run() { int n; std::cin >> n; std::vector<pvec> p(n); for (auto &x : p) std::cin >> x; auto [used, un] = convex_hull(p); int sz = used.size(); i64 ans = 0; for (const auto &x : un) { // enum concave point. std::vector<std::pair<pvec, int>> al; std::vector<pvec> cur(sz); std::vector<i64> val(sz, 0); // area of triangle formed by on-convex points i64 sum = 0; for (const auto &y : un) { // compute vectors if (y == x) continue; al.push_back({y - x, -1}); } for (int i = 0; i < sz; i++) { cur[i] = used[i] - x; al.push_back({cur[i], i}); } // sort by angle std::sort(al.begin(), al.end(), [&](const auto &a, const auto &b) { return comp(a.first, b.first); }); // rotate to satisfy pattern: // [on-convex, not, not, ..., not, on-convex, not, not .... , not, on-convex] for (int i = 0; i < al.size(); i++) { if (al[i].second == -1) continue; std::rotate(al.begin(), al.begin() + i, al.end()); break; } // compute convex area for (int i = 0; i < sz; i++) { val[i] = cross(cur[i], cur[(i + 1) % sz]); sum += val[i]; } // enum all points between 2 on-convex points for (int l = 0, r = 0, al_size = al.size(); l < al_size; l = r) { r = l + 1; while (r < al_size && al[r].second == -1) r++; // (l, r) is the range of not-on-convex points // l, r are on-convex points int pos = al[l].second; std::vector<i64> T(r - l, 0); assert((pos + 1) % sz == al[r % al_size].second); // left convex [&al, &T, &l, &r] { std::vector<pvec> pts; int top = -1; i64 ssum = 0; for (int fix = l; fix < r; fix++) { const auto &q = al[fix].first; while (top >= 1 && cross(q - pts[top - 1], pts[top] - pts[top - 1]) >= 0) { ssum -= cross(pts[top - 1], pts[top]); top--, pts.pop_back(); } pts.push_back(q), top++; if (top >= 1) ssum += cross(pts[top - 1], pts[top]); T[fix - l] += ssum; } }(); // right convex [&al, &T, &l, &r, &al_size] { std::vector<pvec> pts; int top = -1; i64 ssum = 0; for (int fix = r; fix > l; fix--) { const auto &q = al[fix % al_size].first; while (top >= 1 && cross(pts[top - 1], pts[top], q) >= 0) { ssum -= cross(pts[top], pts[top - 1]); top--, pts.pop_back(); } pts.push_back(q); top++; if (top >= 1) ssum += cross(pts[top], pts[top - 1]); T[fix - l - 1] += ssum; } }(); for (int i = 0; i < r - l; i++) { i64 st = sum - val[pos] + T[i]; (ans += std::abs(st)) %= M; } } } std::cout << ans << '\n'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int T = 1; // std::cin >> T; while (T--) run(); return 0; }
passed
38_std.cpp
#include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const int oo = 1e9; void solve() { string x; cin >> x; ll n = stoll(x); sort(all(x)); int ans=0; for(int i=2;i<=10;++i) if(n%i==0) { auto y = to_string(n/i); sort(all(y)); if(y==x) ans++; } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while(t--) solve(); }
passed
92_std.cpp
#include <cassert> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <random> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using Int = long long; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") using Double = long double; const Double EPS = 1e-10L; const Double INF = 1e+10L; const Double PI = acos(-1.0L); inline int sig(Double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; } inline Double sq(Double r) { return r * r; } struct Pt { Double x, y; Pt() {} Pt(Double x_, Double y_) : x(x_), y(y_) {} Pt operator+(const Pt &a) const { return Pt(x + a.x, y + a.y); } Pt operator-(const Pt &a) const { return Pt(x - a.x, y - a.y); } Pt operator*(const Pt &a) const { return Pt(x * a.x - y * a.y, x * a.y + y * a.x); } Pt operator/(const Pt &a) const { const Double d2 = a.abs2(); return Pt((x * a.x + y * a.y) / d2, (y * a.x - x * a.y) / d2); } Pt operator+() const { return Pt(+x, +y); } Pt operator-() const { return Pt(-x, -y); } Pt operator*(const Double &k) const { return Pt(x * k, y * k); } Pt operator/(const Double &k) const { return Pt(x / k, y / k); } friend Pt operator*(const Double &k, const Pt &a) { return Pt(k * a.x, k * a.y); } Pt &operator+=(const Pt &a) { x += a.x; y += a.y; return *this; } Pt &operator-=(const Pt &a) { x -= a.x; y -= a.y; return *this; } Pt &operator*=(const Pt &a) { return *this = *this * a; } Pt &operator/=(const Pt &a) { return *this = *this / a; } Pt &operator*=(const Double &k) { x *= k; y *= k; return *this; } Pt &operator/=(const Double &k) { x /= k; y /= k; return *this; } Double abs() const { return sqrt(x * x + y * y); } Double abs2() const { return x * x + y * y; } Double arg() const { return atan2(y, x); } Double dot(const Pt &a) const { return x * a.x + y * a.y; } Double det(const Pt &a) const { return x * a.y - y * a.x; } friend ostream &operator<<(ostream &os, const Pt &a) { os << "(" << a.x << ", " << a.y << ")"; return os; } bool operator<(const Pt &a) const { return (x != a.x) ? (x < a.x) : (y < a.y); } }; inline Double tri(const Pt &a, const Pt &b, const Pt &c) { return (b - a).det(c - a); } vector<Pt> pCC(const Pt &a, Double r, const Pt &b, Double s) { const Double d = (b - a).abs(); if (sig(r - s - d) <= 0 && sig(s - r - d) <= 0 && sig(r + s - d) >= 0) { const Double x = (d * d + r * r - s * s) / (d * 2); const Pt e = (b - a) / d; const Pt w = e * Pt(0, 1) * sqrt(max(r * r - x * x, (Double)0)); // ccw for (a, r) return { a + e * x - w, a + e * x + w }; } else { return {}; } } Double A[4][3]; bool isSquare(const vector<Pt> &ps) { if (ps.size() != 4) return false; const Double r = (ps[1] - ps[0]).abs(); for (int i = 0; i < 4; ++i) { if (sig((ps[(i + 1) % 4] - ps[i]).abs() - r)) { return false; } } for (int i = 0; i < 4; ++i) { if (sig((ps[(i + 1) % 4] - ps[i]).dot(ps[(i + 2) % 4] - ps[(i + 1) % 4]))) { return false; } } return true; } vector<Pt> put(const vector<Double> &tri) { vector<Pt> ps(3); ps[0] = Pt(0, 0); ps[1] = Pt(tri[0], 0); const auto res = pCC(ps[1], tri[1], ps[0], tri[2]); assert(res.size()); ps[2] = res[0]; return ps; } vector<Pt> put(int u) { return put(vector<Double>(A[u], A[u] + 3)); } vector<Pt> glue(vector<Pt> ps, const vector<Double> &as, int z) { const int n = ps.size(); for (int i = 0; i < 3; ++i) { if (sig(as[i] - (ps[0] - ps[n - 1]).abs()) == 0) { const auto res = pCC(ps[n - 1], as[(i + (z?2:1)) % 3], ps[0], as[(i + (z?1:2)) % 3]); assert(res.size()); const Pt p = res[0]; const int s = sig(tri(ps[n - 2], ps[n - 1], p)); const int t = sig(tri(p, ps[0], ps[1])); if (s < 0) return {}; if (t < 0) return {}; if (s == 0) ps.pop_back(); if (t == 0) ps.erase(ps.begin()); ps.push_back(p); return ps; } } return {}; } vector<Pt> glue(vector<Pt> ps, int u, int z) { return glue(ps, vector<Double>(A[u], A[u] + 3), z); } bool dfs(int used, vector<Pt> ps) { // cerr<<"[dfs] "<<used<<" "<<ps<<endl; if (used == 15) { return isSquare(ps); } for (int rot = 0; rot < (int)ps.size(); ++rot) { for (int u = 0; u < 4; ++u) if (!(used & 1 << u)) { for (int z = 0; z < 2; ++z) { const auto qs = glue(ps, u, z); if (qs.size()) { if (dfs(used | 1 << u, qs)) { return true; } } } } rotate(ps.begin(), ps.begin() + 1, ps.end()); } return false; } bool quad() { for (int u = 0; u < 4; ++u) { if (dfs(1 << u, put(u))) { return true; } } return false; } Double angle(int u, int i) { const Double a = A[u][i]; const Double b = A[u][(i + 1) % 3]; const Double c = A[u][(i + 2) % 3]; return acos(min(max((b*b + c*c - a*a) / (2*b*c), -1.0L), +1.0L)); } bool tria() { vector<vector<Double>> dp[1 << 4]; for (int u = 0; u < 4; ++u) { dp[1 << u].emplace_back(A[u], A[u] + 3); } for (int u = 0; u < 4; ++u) for (int v = u + 1; v < 4; ++v) for (int w = v + 1; w < 4; ++w) { sort(A[u], A[u] + 3); do { sort(A[v], A[v] + 3); do { sort(A[w], A[w] + 3); do { if (A[u][1] == A[v][2] && A[v][1] == A[w][2] && A[w][1] == A[u][2]) { if (sig(angle(u, 0) + angle(v, 0) + angle(w, 0) - 2*PI) == 0) { dp[1 << u | 1 << v | 1 << w].push_back({A[u][0], A[v][0], A[w][0]}); } } } while (next_permutation(A[w], A[w] + 3)); } while (next_permutation(A[v], A[v] + 3)); } while (next_permutation(A[u], A[u] + 3)); } for (int h = 0; h < 1 << 4; ++h) { for (int g = h; --g &= h; ) { for (const auto &tri0 : dp[h ^ g]) for (const auto &tri1 : dp[g]) { auto ps = put(tri0); for (int rot = 0; rot < (int)ps.size(); ++rot) { for (int z = 0; z < 2; ++z) { const auto qs = glue(ps, tri1, z); if (qs.size() == 3) { vector<Double> tri(3); for (int i = 0; i < 3; ++i) { tri[i] = (qs[(i + 1) % 3] - qs[i]).abs(); } dp[h].push_back(tri); } if (h == 15 && isSquare(qs)) { return true; } } rotate(ps.begin(), ps.begin() + 1, ps.end()); } } } // cerr<<"dp["<<h<<"] = "<<dp[h]<<endl; } return false; } bool spe4() { int us[4] = {0, 1, 2, 3}; do { const int u = us[0]; const int v = us[1]; const int w = us[2]; const int x = us[3]; sort(A[u], A[u] + 3); do { sort(A[v], A[v] + 3); do { sort(A[w], A[w] + 3); do { sort(A[x], A[x] + 3); do { if (A[u][1] == A[v][2] && A[v][1] == A[w][2] && A[w][1] == A[x][2] && A[x][1] == A[u][2]) { if (sig(angle(u, 0) + angle(v, 0) + angle(w, 0) + angle(x, 0) - 2*PI) == 0) { if (A[u][0] == A[v][0] && A[v][0] == A[w][0] && A[w][0] == A[x][0]) { bool ok = true; ok = ok && (sig(angle(u, 2) + angle(v, 1) - PI/2) == 0); ok = ok && (sig(angle(v, 2) + angle(w, 1) - PI/2) == 0); ok = ok && (sig(angle(w, 2) + angle(x, 1) - PI/2) == 0); ok = ok && (sig(angle(x, 2) + angle(u, 1) - PI/2) == 0); if (ok) return true; } } } } while (next_permutation(A[x], A[x] + 3)); } while (next_permutation(A[w], A[w] + 3)); } while (next_permutation(A[v], A[v] + 3)); } while (next_permutation(A[u], A[u] + 3)); } while (next_permutation(us + 1, us + 4)); return false; } bool solve() { if (quad()) return true; if (tria()) return true; if (spe4()) return true; return false; } int main() { for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) { for (int u = 0; u < 4; ++u) { for (int i = 0; i < 3; ++i) { Int a; scanf("%lld", &a); A[u][i] = sqrt((Double)a); } } const bool ans = solve(); puts(ans ? "1" : "0"); } #ifndef LOCAL break; #endif } return 0; }
passed
109_std.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 <= 50;i++) v.push_back(a); for(int i = 50 + 1;i <= 95;i++) v.push_back(b); for(int i = 95 + 1;i <= 99;i++) v.push_back(c); for(int i = 99 + 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; }
passed
11_std.cpp
#include <bits/stdc++.h> #define T \ int TT; \ cin >> TT; \ while (TT--) #define ll long long #define endl '\n' #define rep(i, n) for (ll i = 0; i < n; i++) #define ref(i, n) for (ll i = n - 1; i >= 0; i--) #define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) const ll LNF = 9223372036854775807; using namespace std; ll n, m, k, ans, cnt = 1; typedef pair<ll, ll> tt; const ll M = 3e5 + 10; struct node { string x; ll v; bool operator<(const node &a) const { return v > a.v; } }; int main() { IOS; vector<node> a, b; rep(i, 4) { string x; ll v; cin >> x >> v; a.push_back({x, v}); } rep(i, 4) { string x; ll v; cin >> x >> v; b.push_back({x, v}); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); auto [aa, bb] = a[0]; auto [cc, dd] = b[0]; if (bb < dd) { swap(aa, cc); } cout << aa << " beats " << cc; }
passed
87_std.cpp
#include <cstdio> #include <algorithm> using namespace std; const int max1 = 5e5; const int inf = 0x3f3f3f3f; int T, n, L[max1 + 5], R[max1 + 5]; int f[max1 + 5][2], ans; void Work () { scanf("%d", &n); for ( int i = 1; i <= n; i ++ ) scanf("%d%d", &L[i], &R[i]); for ( int i = 1; i <= n; i ++ ) f[i][0] = f[i][1] = 0; f[1][0] = 1; ans = 0; for ( int i = 1; i <= n; i ++ ) { if ( f[i][0] != inf ) { int x = L[i]; int p = upper_bound(L + 1, L + 1 + n, x) - L; if ( p != n + 1 ) f[p][0] = max(f[p][0], f[i][0] + 1); x = R[i]; p = upper_bound(R + 1, R + 1 + n, x) - R; if ( p != n + 1 ) f[p][1] = max(f[p][1], f[i][0] + 1); ans = max(ans, f[i][0]); } if ( f[i][1] != inf ) { int x = R[i - 1] + 1; int p = upper_bound(L + 1, L + 1 + n, x) - L; if ( p != n + 1 ) f[p][0] = max(f[p][0], f[i][1] + 1); x = R[i]; p = upper_bound(R + 1, R + 1 + n, x) - R; if ( p != n + 1 ) f[p][1] = max(f[p][1], f[i][1] + 1); ans = max(ans, f[i][1]); } } printf("%d\n", ans); return; } int main () { scanf("%d", &T); while ( T -- ) Work(); return 0; }
passed
19_std.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; // if you end up using long double, you need to set the floating point notation // to fixed, and set the percision to be very high typedef long double ld; #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define pb push_back #define pf push_front #define f first #define s second #define mp make_pair #define pll pair<ll, ll> #define pint pair<int, int> #define tp make_tuple const bool testing = 0; mt19937 rng(63); // first four are north, west, east ,south int dir1[] = {1, 0, -1, 0, 1, 1, -1, -1}; int dir2[] = {0, 1, 0, -1, 1, -1, 1, -1}; typedef uint64_t ull; struct H { ull x; H(ull x = 0) : x(x) {} H operator+(H o) { return x + o.x + (x + o.x < x); } H operator-(H o) { return *this + ~o.x; } H operator*(H o) { auto m = (__uint128_t)x * o.x; return H((ull)m) + (ull)(m >> 64); } ull get() const { return x + !~x; } bool operator==(H o) const { return get() == o.get(); } bool operator<(H o) const { return get() < o.get(); } }; static const H C = (ll)1e10 + 3; H hashString(string &s) { H h{}; for (char c : s) h = h * C + c; return h; } int main() { // apparently this does fast i/o cin.tie(0), ios::sync_with_stdio(0); // use this if you read in from a file /* freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); */ stringstream ss; // Do it once. Do it right. // Read the problem statement carefully // Plan out the steps in words on a piece of paper before implementing // after RTE(obviously) but also WA, run valgrind!!! // Testing your solution on samples before coding is a great way to see if you // read the problem correctly!!! Also take notes about key elements in the // problem statement while reading the problem!!! If you're stuck, try small // cases (not just a trick for math problems) When debugging, especially when // it's not your code, go through the code line by line, don't just scan over // sections, and if it's not your code, take notes on what each line does. // cout << fixed << setprecision(12); // if you use ld, use the above and don't use string stream // use instead of ceil(a, b) if a and b are positive // (a + b - 1) / b int n, m; cin >> n >> m; vector<string> vec; for (int i = 0; i < m; i++) { string str; cin >> str; vec.pb(str); } if (m == 1) { cout << "YES\n"; for (int i = 0; i < n; i++) { cout << i + 1 << " "; } cout << "\n"; return 0; } vector<ull> in_hash, out_hash; for (int i = 0; i < n; i++) { string pref, suff; for (int j = 0; j < m - 1; j++) { pref.pb(vec[j][i]); suff.pb(vec[j + 1][i]); } H h1 = hashString(pref); H h2 = hashString(suff); in_hash.pb(h1.get()); out_hash.pb(h2.get()); } map<ull, set<int>> omap; for (int i = 0; i < n; i++) { omap[out_hash[i]].insert(i); } bool works = true; vector<int> ans(n, -1); map<ull, int> ind; for (ull i : in_hash) ind[i]++; for (auto [i, j] : ind) { if (j != int(omap[i].size())) { works = false; break; } } for (int i = 0; i < n; i++) { ans[i] = *omap[in_hash[i]].begin(); omap[in_hash[i]].erase(ans[i]); } if (!works) { cout << "NO\n"; } else { cout << "YES\n"; for (int i = 0; i < n; i++) { cout << ans[i] + 1 << " "; } cout << "\n"; } cout << ss.str(); return 0; }
passed
97_std.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int mod = 998244353; using COST_T = long long; struct mfmc_edge { int from, to; long long mx; long long cost; // for 1 flow }; void max_flow_min_cost(vector<vector<pair<int, pair<ll, COST_T>>>> g_main, int s, int t, ll k = -1) { int n = g_main.size(); vector<vector<int>> g(n); vector<mfmc_edge> e; for (int u = 0; u < n; ++u) { for (auto p: g_main[u]) { int v = p.first; ll w = p.second.first, c = p.second.second; g[u].push_back(e.size()); e.push_back({u, v, w, c}); // !!! may need to change c to c/w !!! g[v].push_back(e.size()); e.push_back({v, u, 0, -c}); } } const ll inf = 1e18l * 4; vector<COST_T> p(n, inf); p[s] = 0; vector<int> par(n, -1); for (int i = 0; i < n; ++i) { bool change = false; for (int j = 0; j < e.size(); ++j) { auto edge = e[j]; if (edge.mx > 0 && p[edge.from] != inf) { if (p[edge.to] > p[edge.from] + edge.cost) { p[edge.to] = p[edge.from] + edge.cost; par[edge.to] = j; change = true; } } } if (!change) break; } pair<ll, COST_T> ans = {0, 0}; vector<ll> d = p; while (ans.first < k) { if (par[t] == -1) break; int v = t; ll flow = 1, cost = 0; while (v != s) { flow = min(flow, e[par[v]].mx); cost += e[par[v]].cost; v = e[par[v]].from; } v = t; while (v != s) { e[par[v]].mx -= flow; e[par[v] ^ 1].mx += flow; v = e[par[v]].from; } ans.first += flow; ans.second += cost * flow; par.assign(n, -1); d.assign(n, inf); d[s] = 0; priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq; pq.push({0, s}); while (!pq.empty()) { int v = pq.top().second; ll ds = pq.top().first; pq.pop(); if (ds > d[v]) continue; for (auto i: g[v]) { if (e[i].mx > 0 && d[e[i].to] > d[v] + e[i].cost + p[e[i].from] - p[e[i].to]) { d[e[i].to] = d[v] + e[i].cost + p[e[i].from] - p[e[i].to]; par[e[i].to] = i; pq.push({d[e[i].to], e[i].to}); } } } for (int i = 0; i < n; ++i) { if (d[v] == inf || p[v] == inf) p[v] = inf; else p[v] += d[v]; } cout << -ans.second << " "; } while (ans.first < k) { ++ans.first; cout << "-1 "; } } int sum(int x, int y) { x += y; x -= (x >= mod) * mod; return x; } int main() { cin.tie(0)->sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; vector<int> a(n), b(n); for (auto &i: a)cin >> i; for (auto &i: b)cin >> i; vector<vector<int>> bad(n); { vector<array<int, 2>> A, B; for (int i = 0; i < n; ++i)A.push_back({a[i], i}); for (int i = 0; i < n; ++i)B.push_back({b[i], i}); sort(A.begin(), A.end()); sort(B.begin(), B.end()); vector<int> whoa(n), whob(n); for (int i = 0; i < n; ++i) { whoa[A[i][1]] = i; whob[B[i][1]] = i; } while (m--) { int u, v; cin >> u >> v; --u, --v; u = whoa[u]; v = whob[v]; bad[u].push_back(v); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); vector<int> ok(n, -1); vector<vector<int>> g0(n), g1(n); { vector<int> x, y; for (int i = n - 1; ~i; --i) { for (auto t: bad[i])ok[t] = i; x.clear(); y.clear(); int pos = lower_bound(b.begin(), b.end(), mod - a[i]) - b.begin() - 1; for (int t = 0; x.size() < k && n - 1 - t > pos; ++t) { if (ok[n - 1 - t] != i)x.push_back(n - 1 - t); } for (int t = 0; y.size() < k && pos - t >= 0; ++t) { if (ok[pos - t] != i)y.push_back(pos - t); } int ii = 0, jj = 0; while (ii + jj < k && ii + jj < x.size() + y.size()) { if (jj == y.size() || (ii < x.size() && sum(a[i], b[x[ii]]) >= sum(a[i], b[y[jj]]))) { g0[x[ii]].push_back(i); ++ii; } else { g1[y[jj]].push_back(i); ++jj; } } } } { vector<vector<int>> ng0(n), ng1(n); for (int i = n - 1; ~i; --i) { auto &x = g0[i]; auto &y = g1[i]; int ii = 0, jj = 0; while (ii + jj < k && ii + jj < x.size() + y.size()) { if (jj == y.size() || (ii < x.size() && sum(b[i], a[x[ii]]) >= sum(b[i], a[y[jj]]))) { ng0[x[ii]].push_back(i); ++ii; } else { ng1[y[jj]].push_back(i); ++jj; } } } swap(g0, ng0); swap(g1, ng1); } priority_queue<array<int, 2>> pq; vector<queue<int>> all(n); for (int i = 0; i < n; ++i) { auto &x = g0[i]; auto &y = g1[i]; int ii = 0, jj = 0; while (ii + jj < k && ii + jj < x.size() + y.size()) { if (jj == y.size() || (ii < x.size() && sum(a[i], b[x[ii]]) >= sum(a[i], b[y[jj]]))) { all[i].push(x[ii]); ++ii; } else { all[i].push(y[jj]); ++jj; } } } for (int i = 0; i < n; ++i) { if (all[i].size())pq.push({sum(a[i], b[all[i].front()]), i}); } vector<array<int, 2>> E; while (E.size() < 2 * k * k && pq.size()) { auto [_, i] = pq.top(); pq.pop(); E.push_back({i, all[i].front()}); all[i].pop(); if (all[i].size())pq.push({sum(a[i], b[all[i].front()]), i}); } vector<int> A, B; for (auto [u, v]: E) { A.push_back(u); B.push_back(v); } sort(A.begin(), A.end()); sort(B.begin(), B.end()); A.resize(unique(A.begin(), A.end()) - A.begin()); B.resize(unique(B.begin(), B.end()) - B.begin()); n = A.size() + B.size(); int s = n, t = n + 1; int N = t + 1; vector<vector<pair<int, pair<ll, COST_T>>>> gm(N); for (int i = 0; i < A.size(); ++i)gm[s].push_back({i, {1, 0}}); for (int i = 0; i < B.size(); ++i)gm[A.size() + i].push_back({t, {1, 0}}); for (auto [u, v]: E) { u = lower_bound(A.begin(), A.end(), u) - A.begin(); v = lower_bound(B.begin(), B.end(), v) - B.begin(); // cout << a[A[u]] << " " << b[B[v]] << " " << sum(a[A[u]], b[B[v]]) << endl; gm[u].push_back({v + (int) A.size(), {1, -sum(a[A[u]], b[B[v]])}}); } max_flow_min_cost(gm, s, t, k); }
passed
76_std.cpp
#include <bits/stdc++.h> #ifdef LOCAL #include "dd/debug.h" #else #define dbg(...) 42 #define dbg_proj(...) 420 #define dbg_rproj(...) 420420 void nline() {} void bar() {} void start_clock() {} void end_clock() {} #endif namespace rs = std::ranges; namespace rv = std::views; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; template <class T> constexpr T INF = T{}; template <std::floating_point T> constexpr T INF<T> = std::numeric_limits<T>::infinity(); template <> constexpr int INF<int> = 0x3f3f3f3f; // 1061109567 template <> constexpr long long INF<long long> = 0x3f3f3f3f3f3f3f3fLL; // 4557430888798830399 template <class T> struct CC { std::vector<T> val; void insert(T a) { val.push_back(a); } void init() { std::sort(val.begin(), val.end()); val.erase(std::unique(val.begin(), val.end()), val.end()); } int operator[](T a) const { return static_cast<int>(std::distance( val.begin(), std::lower_bound(val.begin(), val.end(), a))); } int size() const { return static_cast<int>(val.size()); } }; namespace seg { template <class Node> concept SegNode = requires(Node n) { typename Node::Output; Node(); requires std::same_as < std::decay_t<decltype(Node::ID)>, typename Node::Output > ; { Node::comb(Node::ID, Node::ID) } -> std::same_as<typename Node::Output>; { std::as_const(n).value() } -> std::same_as<typename Node::Output>; { n.pull(n, n) } -> std::same_as<void>; }; template <SegNode Node> struct Seg { using Output = typename Node::Output; int sz{}; std::vector<Node> val; Seg() = default; // explicit Seg(int _sz) : sz(_sz), val(2 * sz) {} explicit Seg(int _sz) : sz(static_cast<int>(std::bit_ceil<uint32_t>(_sz))), val(2 * sz) {} template <std::ranges::sized_range Container> requires requires(Container c) { Node(*std::ranges::begin(c)); } explicit Seg(const Container &c) : Seg(static_cast<int>(std::ranges::size(c))) { int i = sz; for (const auto &v : c) { val[i++] = Node(v); } build(0, sz - 1); } void pull(int ind) { val[ind].pull(val[2 * ind], val[2 * ind + 1]); } void build(int l, int r) { l += sz, r += sz; l /= 2, r /= 2; for (int k = 2; l >= 1; l /= 2, r /= 2, k *= 2) { for (int i = l; i <= r; ++i) { val[i].pull(val[2 * i], val[2 * i + 1]); } } } template <class F, class... Args> requires std::invocable<F, Node &, const Args &...> void upd(const F &f, int i, const Args &...args) { std::invoke(f, val[i += sz], args...); for (i /= 2; i >= 1; i /= 2) { pull(i); } } Output query(int l, int r) { Output vl = Node::ID, vr = Node::ID; for (l += sz, r += sz; l <= r; l /= 2, r /= 2) { if (l % 2 == 1) { vl = Node::comb(vl, val[l++].value()); } if (r % 2 == 0) { vr = Node::comb(val[r--].value(), vr); } } return Node::comb(vl, vr); } int first_true(const std::function<bool(Output)> &good) { if (!good(val[1].value())) { return -1; } int ind = 1; while (ind < sz) { if (good(val[2 * ind].value())) { ind *= 2; } else { ind = 2 * ind + 1; } } return ind - sz; } int last_true(const std::function<bool(Output)> &good) { if (!good(val[1].value())) { return -1; } int ind = 1; while (ind < sz) { if (good(val[2 * ind + 1].value())) { ind = 2 * ind + 1; } else { ind *= 2; } } return ind - sz; } }; } // namespace seg using seg::Seg; template <class T> struct Node { using Output = T; static constexpr Output ID{INF<i64>}; T val{ID}; Node() = default; explicit Node(T v) : val(v) {} static Output comb(Output a, Output b) { return std::min(a, b); } Output value() const { return val; } void add(const T &v) { val += v; } void set(const T &v) { val = v; } void pull(const Node &cl, const Node &cr) { val = comb(cl.val, cr.val); } }; template <class T> struct Node2 { using Output = T; static constexpr Output ID{-INF<i64>}; T val{ID}; Node2() = default; explicit Node2(T v) : val(v) {} static Output comb(Output a, Output b) { return std::max(a, b); } Output value() const { return val; } void add(const T &v) { val += v; } void set(const T &v) { val = v; } void pull(const Node2 &cl, const Node2 &cr) { val = comb(cl.val, cr.val); } }; void solve() { int n; i64 p; std::cin >> n >> p; std::array<std::map<i64, bool>, 2> solved; std::array<std::map<i64, i64>, 2> penalty; std::array<std::vector<i64>, 2> times; std::array<int, 2> num{}; std::array<i64, 2> tp{}, wp{}; CC<i64> cc; for (int i = 0; i < n; ++i) { int a, d; i64 b, c; std::cin >> a >> b >> c >> d; --a; if (solved[a][b]) { continue; } if (d == 0) { penalty[a][b] += p; } else { solved[a][b] = true; ++num[a]; wp[a] += penalty[a][b]; tp[a] += c; times[a].push_back(c); cc.insert(c); } } if (num[0] != num[1]) { std::cout << "-1\n"; return; } dbg(tp, wp); i64 k = 1; if (tp[0] + wp[0] > tp[1] + wp[1]) { std::swap(solved[0], solved[1]); std::swap(times[0], times[1]); std::swap(tp[0], tp[1]); std::swap(wp[0], wp[1]); k = 0; } k += (tp[1] + wp[1]) - (tp[0] + wp[0]); dbg(k); cc.init(); dbg(cc.val); int m = cc.size(); std::vector<i64> sum(m); std::vector<int> cnt(m); for (i64 t : times[0]) { sum[cc[t]] -= t; --cnt[cc[t]]; } for (i64 t : times[1]) { sum[cc[t]] += t; ++cnt[cc[t]]; } std::partial_sum(sum.rbegin(), sum.rend(), sum.rbegin()); std::partial_sum(cnt.rbegin(), cnt.rend(), cnt.rbegin()); dbg(sum, cnt); Seg<Node<i64>> seg(m); Seg<Node2<i64>> seg2(m); auto fc = [&](int x) { return sum[x] - cc.val[x] * cnt[x]; }; auto ff = [&](i64 x) { int v = cc[x]; if (v >= m) { return 0LL; } return sum[v] - x * cnt[v]; }; for (int i = 0; i < m; ++i) { seg.upd(&Node<i64>::set, i, std::min(ff(i == 0 ? 0LL : cc.val[i - 1] + 1), fc(i))); seg2.upd(&Node2<i64>::set, i, std::max(ff(i == 0 ? 0LL : cc.val[i - 1] + 1), fc(i))); } std::array<i64, 2> ans{0, INF<i64>}; auto chmin = [&](i64 l, i64 r) { if (r - l < ans[1] - ans[0] || (r - l == ans[1] - ans[0] && l < ans[0])) { ans = {l, r}; } }; for (int l = 0; l < m; ++l) { seg.upd(&Node<i64>::set, l, INF<i64>); // f(l) - f(r) >= k => f(r) <= f(l) - k int r = seg.first_true([&](i64 i) { return i <= fc(l) - k; }); if (r == -1) { continue; } i64 a = sum[r], b = cnt[r]; std::array<i64, 2> cur{}; if (b <= 0) { cur = {cc.val[l], cc.val[r - 1] + 1}; } else { i64 mi = (k + a - fc(l) + b - 1) / b; cur = {cc.val[l], std::max(cc.val[r - 1] + 1, mi)}; } i64 cv = ff(cur[0]) - ff(cur[1]); i64 diff = cnt[r] - cnt[l]; i64 t = std::min(cur[0] - (l == 0 ? 0LL : cc.val[l - 1] + 1), cur[1] - (cc.val[r - 1] + 1)); if (diff > 0) { t = std::min(t, (cv - k) / diff); } chmin(cur[0] - t, cur[1] - t); } dbg(ans); for (int r = m - 1; r >= 0; --r) { // f(l) - f(r) >= k => f(l) >= f(r) + k int l = seg2.last_true([&](i64 i) { return i >= fc(r) + k; }); if (l == -1) { seg2.upd(&Node2<i64>::set, r, -INF<i64>); continue; } i64 a = sum[l], b = cnt[l]; std::array<i64, 2> cur{-1, -1}; if (b <= 0) { cur = {cc.val[l], cc.val[r]}; } else { i64 mx = (a - fc(r) - k) / b; cur = {std::min(mx, cc.val[l]), cc.val[r]}; } i64 cv = ff(cur[0]) - ff(cur[1]); i64 diff = cnt[r] - cnt[l]; i64 t = std::min(cur[0] - (l == 0 ? 0LL : cc.val[l - 1] + 1), cur[1] - (cc.val[r - 1] + 1)); if (diff > 0) { t = std::min(t, (cv - k) / diff); } chmin(cur[0] - t, cur[1] - t); seg2.upd(&Node2<i64>::set, r, -INF<i64>); } if (ans[1] == INF<i64>) { std::cout << "-1\n"; } else { std::cout << ans[0] << ' ' << ans[1] << '\n'; } } int main() { #ifndef LOCAL std::ios::sync_with_stdio(false); std::cin.tie(nullptr); #endif int t; std::cin >> t; for (int i = 0; i < t; ++i) { dbg(i + 1); solve(); bar(); } }
passed
88_std.cpp
#include <vector> #include <iostream> #include <algorithm> #define F for #define W while #define fi first #define se second #define fv inline void #define eb emplace_back #define fn inline static #define mset __builtin_memset #define mcpy __builtin_memcpy #define U(i, a, b) F(int i = (a); i < (int)(b); ++i) #define L(i, a, b) F(int i = (a); i <= (int)(b); ++i) #define R(i, a, b) F(int i = (a); i >= (int)(b); --i) using i64 = long long; constexpr int N = 10050; i64 dp[N][N], ti[N], sm[N]; template<typename _Tp> fv ulo(_Tp &x, _Tp v) { if (x > v) x = v; } int main() { mset(dp, 0x3f, sizeof(dp)), dp[0][0] = 0; std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr); int n, c; std::cin >> n >> c; U(i, 0, n) std::cin >> ti[i]; std::sort(ti, ti + n); U(i, 0, n) sm[i + 1] = sm[i] + ti[i]; L(i, 1, std::min(n, c)) dp[i][0] = ti[i - 1]; L(i, 1, n) { const int val = ti[i - 1]; const int loop_limit = (n - i) / (c - 1) + 1; if (i <= c) L(j, 0, loop_limit) { const int k_limit = std::min(c, j + 1); L(k, 1, k_limit) dp[i][j] = std::min(dp[i][j], dp[k][j - k + 1] + val + sm[k]); } else L(j, 0, loop_limit) { const int k_limit = std::min(c, j + 1); L(k, 0, k_limit) dp[i][j] = std::min(dp[i][j], dp[i - c + k][j - k + 1] + val + sm[k]); } } std::cout << dp[n][0]; return 0; }
passed
117_std.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; char find(vector<char>& v, char x) { return v[(ll)x] == x ? x : v[(ll)x] = find(v, v[(ll)x]); } bool ck(string x1, string x2, string y, ll& n) { vector<char> v(1000); for (char c = 'a'; c <= 'z'; ++c) { v[(ll)c] = c; } for (ll i = 0; i < n; ++i) { if (find(v, x1[i]) != find(v, x2[i])) { v[find(v, x1[i])] = find(v, x2[i]); } } for (ll i = 0; i < n; ++i) { assert(find(v, x1[i]) == find(v, x2[i])); } for (ll i = 0; i < n; ++i) { if (find(v, y[i]) != find(v, x1[i])) { return true; } } return false; } void solve() { string s1, s2, s3; cin >> s1 >> s2 >> s3; ll n1 = s1.length(), n2 = s2.length(), n3 = s3.length(); if (n1 == n2 && n1 != n3) { cout << "YES\n"; } else if (n1 != n2) { cout << "NO\n"; } else { if (ck(s1, s2, s3, n1)) { cout << "YES\n"; } else { cout << "NO\n"; } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; }
passed
24_std.cpp
#include <bits/stdc++.h> //#define int int64_t #define int int128 #define int128 __int128 using namespace std; const int128 MM10=int128(1e13)*int128(1e14); const int128 inf = int128(1e15)*int128(1e15); const int128 mod = 998244353; string s; int128 xotr(int128 x, int o) { if(x>MM10 || x<0) return inf; int128 ans=o; int128 m10=1; int128 nd10=1; while(m10*10<=x) { ans+=9*m10*nd10; m10*=10; nd10+=1; } ans+=nd10*(x-m10); return ans+1; } void tos(string & ss, int128 x) { while(x) { ss.push_back((x%10)+'0'); x/=10; } } int128 test_after(int128 x, int num) { string tst; for(int128 i=x+num-1; i>=x; --i) tos(tst, i); reverse(tst.begin(),tst.end()); for(int j=0; j+s.size()-1<tst.size(); ++j) { bool ok=1; for(int i=0; i<(int)s.size(); ++i) if(s[i]!='?') { if(s[i]!=tst[j+i]) { ok=0; break; } } if(ok) return xotr(x,j); } return inf; } int128 test_around(int128 x) { return test_after(x-20, 40); } int128 p10[32]; int128 t(int nd, int d0) { int nn=1; int128 ans=inf; vector<int> md(nd,-1); for(int i=0; i<(int)s.size(); ++i) { int d = (d0+nd*s.size()-i)%nd; if(s[i]!='?') md[d]=max(md[d],int(s[i]-'0')); if(d==0) ++nn; } int128 num=0; for(int i=nd-1; i>0; --i) { if(md[i]==-1) md[i]=(i==nd-1)?1:0; num+=p10[i]*md[i]; ans=min(ans,test_around(num)); ans=min(ans,test_around(num+p10[i])); } ans=min(ans,test_around(num)); return ans; } int128 single() { int128 x=0; if(s[0]=='0') x=1; for(int i=0; i<int(s.size()); ++i) { char c=s[i]; if(c=='?') { if(i==0) c='1'; else c='0'; } x=(x*10)+(c-'0'); } return test_after(x,1); } int128 boundaries() { int128 ans=inf; for(int128 m10=10000; m10<MM10; m10*=10) ans=min(ans,test_around(m10)); return ans; } void solve() { cin>>s; int128 ans = test_after(1,1111); for(int nd=4; nd<=int(s.size()+1); ++nd) { for(int d0=0; d0<nd; ++d0) { ans=min(ans,t(nd,d0)); } } ans=min(ans,boundaries()); ans=min(ans,single()); assert(ans>0); //assert(ans>0 && ans<inf); cout<<int64_t(ans%mod)<<'\n'; } void init() { p10[0]=1; for(int i=1; i<32; ++i) p10[i]=10*p10[i-1]; } signed main() { cin.tie(0); ios_base::sync_with_stdio(0); init(); int64_t t; cin>>t; while(t--)solve(); return 0; }
passed
100_std.cpp
#include <bits/stdc++.h> #define int long long #define debug(x) std::cerr<<"The "<<#x<<" = "<<x<<'\n'; using i128 = __int128; using i64 = long long; using u64 = unsigned long long; constexpr int N = 2e5 + 5; constexpr int mod = 998244353; constexpr i64 INF = 0x7fffffffffffffffLL; void solve() { int n, q, num = 0; std::cin >> n >> q; std::vector<int> a(n + 10), dif(n + 10); for (int i = 1; i <= n; i++) { std::cin >> a[i]; dif[i] = a[i] - a[i - 1]; if (dif[i] < 0) { num++; } } std::vector<std::vector<int>> d(n + 10); for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j += i) { d[j].push_back(i); } } std::vector<int> cnt(n + 10); for (int i = 2; i <= n; i++) { if (dif[i] < 0) { for (auto x : d[i - 1]) { cnt[x]++; } } } std::vector<int> tot(n + 10); for (int i = 2; i <= n ;i++) { tot[cnt[i]]++; } std::cout << tot[num] + 1 << '\n'; int p, v; while (q--) { std::cin >> p >> v; if (p > 1 && dif[p] >= 0 && v - a[p - 1] < 0) { num++; for (auto x : d[p - 1]) { if (x == 1) { continue; } tot[cnt[x]]--; cnt[x]++; tot[cnt[x]]++; } } else if (p > 1 && dif[p] < 0 && v - a[p - 1] >= 0) { num--; for (auto x : d[p - 1]) { if (x == 1) { continue; } tot[cnt[x]]--; cnt[x]--; tot[cnt[x]]++; } } if (p <= n - 1 && dif[p + 1] >= 0 && a[p + 1] - v < 0) { num++; for (auto x : d[p]) { if (x == 1) { continue; } tot[cnt[x]]--; cnt[x]++; tot[cnt[x]]++; } } else if (p <= n - 1 && dif[p + 1] < 0 && a[p + 1] - v >= 0) { num--; for (auto x : d[p]) { if (x == 1) { continue; } tot[cnt[x]]--; cnt[x]--; tot[cnt[x]]++; } } dif[p] = v - a[p - 1]; dif[p + 1] = a[p + 1] - v; a[p] = v; std::cout << tot[num] + 1<< '\n'; } } signed main() { std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0); int T = 1; std::cin >> T; while (T--) { solve(); } return 0; }
passed
8_std.cpp
#include<bits/stdc++.h> #define N 2000007 #define mod 998244353 #define ll long long using namespace std; int n,m; string str[N]; #define def(x,y) vector<vector<y> > x(n+7,vector<y>(m+7)) void mian(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) cin>>str[i]; def(s,char); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) s[i][j]=str[i][j-1]; def(fn,int);def(fw,int);def(fs,int);def(fe,int); for(int i=1;i<=n;i++) fw[i][0]=fe[i][m+1]=1; for(int i=1;i<=m;i++) fn[0][i]=fs[n+1][i]=1; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ fn[i][j]=fn[i-1][j]&(s[i][j]=='N'||s[i][j]=='?'); fw[i][j]=fw[i][j-1]&(s[i][j]=='W'||s[i][j]=='?'); } } for(int i=n;i;i--){ for(int j=m;j;j--){ fs[i][j]=fs[i+1][j]&(s[i][j]=='S'||s[i][j]=='?'); fe[i][j]=fe[i][j+1]&(s[i][j]=='E'||s[i][j]=='?'); } } def(nw0,ll);def(nw1,ll); for(int i=0;i<=m;i++) if(fw[1][i]) nw0[0][i]=1; for(int i=1;i<=n;i++) if(fn[i][1]) nw1[i][0]=1; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(fn[i][j]&&fw[i+1][j]) nw0[i][j]=(nw0[i][j-1]+nw1[i][j-1])%mod; if(fw[i][j]&&fn[i][j+1]) nw1[i][j]=(nw0[i-1][j]+nw1[i-1][j])%mod; } } def(sw0,ll);def(sw1,ll); for(int i=0;i<=m;i++) if(fw[n][i]) sw0[n+1][i]=1; for(int i=1;i<=n;i++) if(fs[i][1]) sw1[i][0]=1; for(int i=n;i;i--){ for(int j=1;j<=m;j++){ if(fs[i][j]&&fw[i-1][j]) sw0[i][j]=(sw0[i][j-1]+sw1[i][j-1])%mod; if(fw[i][j]&&fs[i][j+1]) sw1[i][j]=(sw0[i+1][j]+sw1[i+1][j])%mod; } } def(se0,ll);def(se1,ll); for(int i=1;i<=m+1;i++) if(fe[n][i]) se0[n+1][i]=1; for(int i=1;i<=n;i++) if(fs[i][m]) se1[i][m+1]=1; for(int i=n;i;i--){ for(int j=m;j;j--){ if(fs[i][j]&&fe[i-1][j]) se0[i][j]=(se0[i][j+1]+se1[i][j+1])%mod; if(fe[i][j]&&fs[i][j-1]) se1[i][j]=(se0[i+1][j]+se1[i+1][j])%mod; } } def(ne0,ll);def(ne1,ll); for(int i=1;i<=m+1;i++) if(fe[1][i]) ne0[0][i]=1; for(int i=1;i<=n;i++) if(fn[i][m]) ne1[i][m+1]=1; for(int i=1;i<=n;i++){ for(int j=m;j;j--){ if(fn[i][j]&&fe[i+1][j]) ne0[i][j]=(ne0[i][j+1]+ne1[i][j+1])%mod; if(fe[i][j]&&fn[i][j-1]) ne1[i][j]=(ne0[i-1][j]+ne1[i-1][j])%mod; } } ll ans=0; for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++){ (ans+=nw0[i][j]*sw1[i+1][j]%mod*se0[i+1][j+1]%mod*ne1[i][j+1])%=mod; (ans+=nw1[i][j]*sw0[i+1][j]%mod*se1[i+1][j+1]%mod*ne0[i][j+1])%=mod; } } def(f,ll); for(int j=0;j<=m;j++){ for(int i=0;i<=n;i++){ if(fn[i][j+1]&&fs[i+1][j+1]){ (f[i][j+1]+=f[i][j])%=mod; (f[i][j+1]+=nw1[i][j]*sw1[i+1][j])%=mod; } (ans+=ne0[i][j+1]*se1[i+1][j+1]%mod*f[i][j])%=mod; (ans+=ne1[i][j+1]*se0[i+1][j+1]%mod*f[i][j])%=mod; (ans+=ne1[i][j+1]*se1[i+1][j+1]%mod*f[i][j])%=mod; } ll sum=0; for(int i=0;i<=n;i++){ (ans+=ne0[i][j+1]*se1[i+1][j+1]%mod*sum)%=mod; (sum+=nw1[i][j]*sw0[i+1][j])%=mod; if(fn[i][j+1]&&fs[i+1][j+1]) (f[i][j+1]+=sum)%=mod; (sum+=f[i][j])%=mod; } sum=0; for(int i=n;~i;i--){ (ans+=ne1[i][j+1]*se0[i+1][j+1]%mod*sum)%=mod; (sum+=nw0[i][j]*sw1[i+1][j])%=mod; if(fn[i][j+1]&&fs[i+1][j+1]) (f[i][j+1]+=sum)%=mod; (sum+=f[i][j])%=mod; } } for(int i=0;i<=n;i++) for(int j=0;j<=m;j++) f[i][j]=0; for(int i=0;i<=n;i++){ for(int j=0;j<=m;j++){ if(fw[i+1][j]&&fe[i+1][j+1]){ (f[i+1][j]+=f[i][j])%=mod; (f[i+1][j]+=nw0[i][j]*ne0[i][j+1])%=mod; } (ans+=sw1[i+1][j]*se0[i+1][j+1]%mod*f[i][j])%=mod; (ans+=sw0[i+1][j]*se1[i+1][j+1]%mod*f[i][j])%=mod; (ans+=sw0[i+1][j]*se0[i+1][j+1]%mod*f[i][j])%=mod; } ll sum=0; for(int j=0;j<=m;j++){ (ans+=sw1[i+1][j]*se0[i+1][j+1]%mod*sum)%=mod; (sum+=nw0[i][j]*ne1[i][j+1])%=mod; if(fw[i+1][j]&&fe[i+1][j+1]) (f[i+1][j]+=sum)%=mod; (sum+=f[i][j])%=mod; } sum=0; for(int j=m;~j;j--){ (ans+=sw0[i+1][j]*se1[i+1][j+1]%mod*sum)%=mod; (sum+=nw1[i][j]*ne0[i][j+1])%=mod; if(fw[i+1][j]&&fe[i+1][j+1]) (f[i+1][j]+=sum)%=mod; (sum+=f[i][j])%=mod; } } printf("%lld\n",ans); } int main() { int T;scanf("%d",&T); while(T--) mian(); return 0; }
passed
14_std.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() { string s; cin >> s; int n = s.length(); s = " " + s; vector<pii> v, bac; for (int i = 1; i <= n - 3; i++) { if (s[i] == '>') v.push_back({i, n}); } int id; for (int i = n; i >= 1; i--) { if (s[i] == '>') id = i; else break; } if (s[1] == '>' && s[n] == '>' && s[n - 1] == '>' && s[n - 2] == '>' && v.size() != n - 3) { vector<pii> ans; for (int i = n; i >= id + 2; i--) ans.push_back({1, i}); for (auto [x, y] : v) { if (x < id && x > 1) ans.push_back({x, id + 2 - x + 1}); } cout << "Yes" << " " << ans.size() << "\n"; for (auto [x, y] : ans) cout << x << " " << y << "\n"; } else { cout << "No\n"; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) solve(); return 0; }
passed
1_std.cpp
#include <bitset> #include <cstdio> #define ll long long using namespace std; const int B = 300; const int D = 5e5 / 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[500009]; 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].tg1 = -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; L[0] = 1, R[dN] = N; 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 / B, tr = r / B; 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_1_wrong.cpp
#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); int T; cin >> T; while (T--) { int n; cin >> n; string s; cin >> s; bool ok = true; vector<int> f(1 << n); for (int i = 0; i < 1 << n; i++) { f[i] = s[i] - '0'; } for (int i = 0; i < 1 << n; i++) { for (int j = 0; j < n; j++) { if (i >> j & 1) { ok &= (f[i] >= f[i ^ (1 << j)]); } } } if (!ok) { cout << "No" << '\n'; continue; } auto AND = [&](const string &a, const string &b) -> string { if (a == "F" or b == "F") return "F"; if (a == "T" and b == "T") return "T"; if (a == "T") return b; if (b == "T") return a; return "(" + a + "&" + b + ")"; }; auto OR = [&](const string &a, const string &b) -> string { if (a == "T" or b == "T") return "T"; if (a == "F" and b == "F") return "F"; if (a == "F") return b; if (b == "F") return a; return "(" + a + "|" + b + ")"; }; cout << "Yes" << '\n'; auto solve = [&](auto &&solve, int cnt, int mask) -> string { string cur = string(1, char('a' + cnt)); if (cnt == n - 1) { if (s[mask] == '0' and s[mask | (1 << cnt)] == '0') { return "F"; } if (s[mask] == '0' and s[mask | (1 << cnt)] == '1') { return cur; } return "T"; } auto s1 = solve(solve, cnt + 1, mask); auto s2 = solve(solve, cnt + 1, mask | (1 << cnt)); return OR(AND(cur, s2), s1); }; cout << solve(solve, 0, 0) << '\n'; } }
failed
17_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #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; typedef long double ld; int main() { cin.tie(NULL)->sync_with_stdio(false); int ans = 0; set<array<int, 2>> x_pts, y_pts; for (int i = 0; i < 5; i++) { int x, y; cin >> x >> y; x_pts.insert({x, y}); y_pts.insert({y, x}); } while (x_pts.size() > 1) { // cout << "-------------------\n"; // for(const auto &x_pt : x_pts) { // cout << x_pt[0] << " " << x_pt[1] << "\n"; // } { auto ptr_0 = x_pts.begin(); auto ptr_1 = x_pts.begin(); ptr_1++; if ((*ptr_0)[0] != (*ptr_1)[0]) { auto val = *ptr_0; int new_x = (*ptr_1)[0]; ans += (*ptr_1)[0] - (*ptr_0)[0]; x_pts.erase(val); y_pts.erase({val[1], val[0]}); x_pts.insert({new_x, val[1]}); y_pts.insert({val[1], new_x}); continue; } auto ptr_2 = x_pts.rbegin(); auto ptr_3 = x_pts.rbegin(); ptr_2++; if ((*ptr_2)[0] != (*ptr_3)[0]) { auto val = *ptr_3; int new_x = (*ptr_2)[0]; ans += (*ptr_3)[0] - (*ptr_2)[0]; x_pts.erase(val); y_pts.erase({val[1], val[0]}); x_pts.insert({new_x, val[1]}); y_pts.insert({val[1], new_x}); continue; } } { auto ptr_0 = y_pts.begin(); auto ptr_1 = y_pts.begin(); ptr_1++; if ((*ptr_0)[0] != (*ptr_1)[0]) { auto val = *ptr_0; int new_y = (*ptr_1)[0]; ans += (*ptr_1)[0] - (*ptr_0)[0]; y_pts.erase(val); x_pts.erase({val[1], val[0]}); y_pts.insert({new_y, val[1]}); x_pts.insert({val[1], new_y}); continue; } auto ptr_2 = y_pts.rbegin(); auto ptr_3 = y_pts.rbegin(); ptr_2++; if ((*ptr_2)[0] != (*ptr_3)[0]) { auto val = *ptr_3; int new_y = (*ptr_2)[0]; ans += (*ptr_3)[0] - (*ptr_2)[0]; y_pts.erase(val); x_pts.erase({val[1], val[0]}); y_pts.insert({new_y, val[1]}); x_pts.insert({val[1], new_y}); continue; } } break; } if (x_pts.size() == 4) { auto ptr_0 = x_pts.begin(); auto ptr_3 = x_pts.rbegin(); ans += 2 * ((*ptr_3)[0] - (*ptr_0)[0]); ans += 2 * ((*ptr_3)[1] - (*ptr_0)[1]); } else if (x_pts.size() == 5) { auto ptr_0 = x_pts.begin(); auto ptr_2 = x_pts.begin(); auto ptr_4 = x_pts.rbegin(); ptr_2++, ptr_2++; ans += 2 * ((*ptr_4)[0] - (*ptr_0)[0]); ans += 2 * ((*ptr_4)[1] - (*ptr_0)[1]); if ((*ptr_2)[0] != (*ptr_0)[0] && (*ptr_2)[0] != (*ptr_4)[0] && (*ptr_2)[1] != (*ptr_0)[1] && (*ptr_2)[1] != (*ptr_4)[1]) { ans += min((*ptr_4)[0] - (*ptr_0)[0], (*ptr_4)[1] - (*ptr_0)[1]); } } cout << ans << "\n"; }
failed
59_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #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; typedef long double ld; int main() { cin.tie(NULL)->sync_with_stdio(false); ll n, x, y; cin >> n >> x >> y; vector<vi> open(n + 1, vi(n + 1, 0)), cx(n + 1, vi(n + 1, 0)), cy(n + 1, vi(n + 1, 0)); for (int i = 0; i < n; i++) { ll xl, xh, yl, yh; cin >> xl >> xh >> yl >> yh; xl -= x, xh -= x; yl -= y, yh -= y; xl = max(0ll, xl); xh = min(n, xh); yl = max(0ll, yl); yh = min(n, yh); if (xl <= n && yl <= n) { open[xl][yl]++; cx[xl][yl]--; cy[xl][yl]--; } if (xl <= n && yh >= 0 && yh < n) { open[xl][yh + 1]--; cx[xl][yh + 1]++; cy[xl][yh + 1]++; } if (xh >= 0 && xh <= n && yl < n) { open[xh + 1][yl]--; cx[xh + 1][yl]++; cy[xh + 1][yl]++; } if (xh >= 0 && xh <= n && yh >= 0 && yh < n) { open[xh + 1][yh + 1]++; cx[xh + 1][yh + 1]--; cy[xh + 1][yh + 1]--; } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { if (i) { cy[i][j] += cy[i - 1][j]; open[i][j] += open[i - 1][j]; } if (j) { cx[i][j] += cx[i][j - 1]; open[i][j] += open[i][j - 1]; } if (i && j) { open[i][j] -= open[i - 1][j - 1]; } cout << open[i][j] << " "; } cout << "\n"; } vector<vi> dp(n + 1, vi(n + 1, 0)); for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { int dst = i + j; if (dst > n) continue; int closed = min(dst, dp[i][j]); dp[i][j] = open[i][j] - (dst - closed); if (dst == n) continue; dp[i + 1][j] = max(dp[i + 1][j], closed + cx[i + 1][j]); dp[i][j + 1] = max(dp[i][j + 1], closed + cy[i][j + 1]); } } // TODO: find largest i + j s.t. dp[i][j] >= 0; int ans = 0; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { int dst = i + j; if (dst > n || dp[i][j] < 0) continue; ans = max(ans, dst); } } cout << ans << "\n"; }
failed
110_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vl = vector<ll>; #define rep3(i, a, b, c) for (ll i = (a); i < (b); i += (c)) #define rep2(i, a, b) rep3(i, a, b, 1) #define rep1(i, n) rep2(i, 0, n) #define rep0(n) rep1(aaaaa, n) #define ov4(a, b, c, d, name, ...) name #define rep(...) ov4(__VA_ARGS__, rep3, rep2, rep1, rep0)(__VA_ARGS__) #define per(i, a, b) for (ll i = (a)-1; i >= (b); i--) #define fore(e, v) for (auto &&e : v) #define all(a) begin(a), end(a) #define sz(a) (int)(size(a)) #define lb(v, x) (lower_bound(all(v), x) - begin(v)) #define eb emplace_back template <typename T, typename S> bool chmin(T &a, const S &b) { return a > b ? a = b, 1 : 0; } template <typename T, typename S> bool chmax(T &a, const S &b) { return a < b ? a = b, 1 : 0; } const int INF = 1e9 + 100; const ll LINF = 3e18 + 100; #define i128 __int128_t struct _ { _() { cin.tie(0)->sync_with_stdio(0), cout.tie(0); } } __; const ll mod = (1LL << 61) - 1; ll add(ll a, ll b) { return (a += b) >= mod ? a - mod : a; } ll mul(ll a, ll b) { i128 c = (i128)a * b; return add(c >> 61, c & mod); } ll r = 7954398468495; void solve() { int n, k; cin >> n >> k; vector<string> s(n); rep(i, k) cin >> s[i]; vector<ll> a(n, 0), b(n, 0); vl se; rep(i, n) { rep(j, k - 1) { a[i] = add(mul(a[i], r), s[j][i]); b[i] = add(mul(b[i], r), s[j + 1][i]); } se.emplace_back(a[i]); se.emplace_back(b[i]); } sort(all(se)); se.erase(unique(all(se)), se.end()); auto get = [&](ll x) -> int { return lower_bound(all(se), x) - se.begin(); }; int N = sz(se); vector<vector<pii>> G(N); vector<int> deg(n); rep(i, n) { int A = get(a[i]), B = get(b[i]); G[A].emplace_back(B, i); deg[B]++; } // rep(i, n) { // cout << a[i] << " " << b[i] << endl; // } rep(i, N) { if (deg[i] != sz(G[i])) { cout << "NO" << endl; return; } } cout << "YES" << endl; vi ans(n, -1); rep(i, N) { while (!G[i].empty()) { int x = i; vi res; while (true) { auto [c, d] = G[x].back(); G[x].pop_back(); res.emplace_back(d); x = c; if (x == i) break; } // fore(x, res) cerr << x << " "; // cerr << endl; rep(j, sz(res) - 1) { // cerr << res[i] << " " << res[i+1] << endl; ans[res[j]] = res[j + 1]; } // cerr << res.back() << " " << res[0] << endl; ans[res.back()] = res[0]; } } rep(i, n) { if (i) cout << " "; cout << ans[i] + 1; } cout << endl; } int main() { int T; // cin >> T; T = 1; while (T--) solve(); }
failed
97_1_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; bool check(int a, int b){ string s = to_string(a); string t = to_string(b); sort(all(s)); sort(all(t)); return s == t; } signed main() { cin.tie(0)->sync_with_stdio(0); int t; cin >> t; while(t--){ int n; cin >> n; int ans = 0; for(int i = 2; i<10; i++){ ans += check(n,n/i); } cout << ans << '\n'; } return 0; }
failed
92_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; #ifdef LOCAL #include <bits/debug.h> #else #define dbg(...) #endif int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<pair<string, string>> ans; unordered_map<string, vector<string>> mp; for (int i = 0; i < n; i++) { string s; cin >> s; string name = s.substr(0, 4); mp[name].push_back(s); } for (int i = 0; i < n; i++) { string s; cin >> s; string name = s.substr(0, 4); if (mp[name].empty()) { cout << "NO\n"; } ans.push_back({mp[name].back(), s}); mp[name].pop_back(); } cout << "YES\n"; for (auto [x, y] : ans) { cout << x << " " << y << "\n"; } return 0; }
failed
91_1_wrong.cpp
#include <bits/stdc++.h> #define int long long using namespace std; const int N = 1e5 + 5; const int M = 1e5 + 5; const int mod = 998244353; int t, n, q, u, v, p, cnt, to[M], nxt[M], h[N], dep[N], fa[N][25], f[N], g[N]; void clear() { cnt = 0; for (int i = 1; i <= n; i++) h[i] = f[i] = g[i] = 0; for (int i = 1; i <= n; i++) for (int j = 0; j <= 20; j++) fa[i][j] = 0; } void save(int u, int v) { to[++cnt] = v; nxt[cnt] = h[u]; h[u] = cnt; } int qpow(int x, int y) { if (!y) return 1; int tmp = qpow(x, y >> 1); if (y & 1) return tmp * tmp % mod * x % mod; return tmp * tmp % mod; } void dfs(int u) { dep[u] = dep[fa[u][0]] + 1; for (int i = 1; i <= 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1]; f[u] = 1; for (int i = h[u]; i; i = nxt[i]) { fa[to[i]][0] = u; dfs(to[i]); f[u] = f[u] * (f[to[i]] + 1) % mod; } } void dfs2(int u) { for (int i = h[u]; i; i = nxt[i]) { assert(f[to[i]] != mod - 1); g[to[i]] = f[to[i]] * (g[u] * qpow(f[to[i]] + 1, mod - 2) % mod + 1) % mod; f[to[i]] = (f[to[i]] + f[u]) % mod; dfs2(to[i]); } } int lca(int u, int v) { if (dep[u] < dep[v]) swap(u, v); for (int i = 20; i >= 0; i--) if (dep[fa[u][i]] >= dep[v]) u = fa[u][i]; if (u == v) return u; for (int i = 20; i >= 0; i--) if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i]; return fa[u][0]; } signed main() { scanf("%lld", &t); while (t--) { clear(); scanf("%lld%lld", &n, &q); for (int i = 2; i <= n; i++) { scanf("%lld", &u); save(u, i); } dfs(1); g[1] = f[1]; dfs2(1); while (q--) { scanf("%lld%lld", &u, &v); p = lca(u, v); printf("%lld\n", (f[u] + f[v] + g[p] - 2 * f[p] + 2 * mod) % mod); } } return 0; }
failed
4_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; void solve() { int l, r; cin >> l >> r; int lcm = l / __gcd(l, r); lcm = lcm * r; if (lcm <= r) { cout << "Alice" << "\n"; return; } else { if ((r - l) % 2 == 0) { cout << "Alice" << "\n"; return; } else { cout << "Bob" << "\n"; return; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) solve(); return 0; }
failed
68_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 void toggle(char &c) { if (c == 'B') c = 'W'; else c = 'B'; } // empty vec = no soln // non pair<bool, vector<string>> solve() { int N, M; cin >> N >> M; vector<string> g2(N); for (int i = 0; i < N; ++i) cin >> g2[i]; auto f = [&](bool flip_corner) -> pair<bool, vector<string>> { vector<string> ans; vector<string> g(N); for (int i = 0; i < N; ++i) g[i] = g2[i]; if (flip_corner) { int ci = 0, cj = 0; toggle(g[ci][cj]); string path; while (ci < N - 1) { ++ci; toggle(g[ci][cj]); path.push_back('D'); } while (cj < M - 1) { ++cj; toggle(g[ci][cj]); path.push_back('R'); } assert(ci == N - 1 && cj == M - 1); ans.push_back(path); } char tgt = g[N - 1][0]; if (N > 1 && M > 1) { for (int d = N - 2; d >= 1 - (M - 2); --d) { // iter diags; d = i-j string path; // compute the path int ci = 0, cj = 0; for (int j = max(0LL, 1 - d); j <= M - 2; ++j) { while (cj < j) { path.push_back('R'); ++cj; } int i = d + j; assert(1 <= i && i <= N - 1); while (ci + 1 < i) { path.push_back('D'); ++ci; } if (g[i][j] != tgt) { path.push_back('D'); ++ci; } } // finish the path... path.push_back('R'); ++cj; while (ci < N - 1) { ++ci; path.push_back('D'); } // actually toggle along the path ci = 0; cj = 0; toggle(g[ci][cj]); for (char c : path) { if (c == 'D') ++ci; else if (c == 'R') ++cj; else assert(0); toggle(g[ci][cj]); } assert(ci == N - 1 && cj == M - 1); ans.push_back(path); } } // done all diags.. check remaining if (g[0][0] != tgt) { int ci = 0, cj = 0; toggle(g[ci][cj]); string path; while (cj < M - 1) { ++cj; toggle(g[ci][cj]); path.push_back('R'); } while (ci < N - 1) { ++ci; toggle(g[ci][cj]); path.push_back('D'); } assert(ci == N - 1 && cj == M - 1); ans.push_back(path); } for (int i = 0; i < N; ++i) { for (int j = 0; j < M; ++j) { if (g[i][j] != tgt) { return {false, {}}; } } } return {true, ans}; }; auto [has_ans, ans] = f(false); if (has_ans) return {has_ans, ans}; return f(true); } signed main() { cin.tie(0)->sync_with_stdio(0); int T; cin >> T; while (T--) { auto [has_ans, ans] = solve(); if (!has_ans) { cout << "NO\n"; } else { cout << "YES\n"; cout << ans.size() << '\n'; for (const string &s : ans) { cout << s << '\n'; } } } }
failed
45_1_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 const int mxn = 2e5 + 5; int n, k, p[mxn], f[mxn], fl[mxn]; vector<array<int, 6>> ord; inline int get(int ind, const array<int, 6> &a) { if (a[0] == ind) return a[1]; if (a[2] == ind) return a[3]; if (a[4] == ind) return a[5]; int mn = min({a[1], a[3], a[5]}); int mx = max({a[1], a[3], a[5]}); if (p[ind] < mn) { if (p[ind] % 2) return p[ind] - 1; return p[ind] + 1; } else { if (p[ind] % 2) return p[ind] + 1; return p[ind] - 1; } } int iter = 0; bool cc(const array<int, 6> &a, const array<int, 6> &b) { int ma = max({a[1], a[3], a[5]}); int mb = max({b[1], b[3], b[5]}); int cx = min(fl[ma], fl[mb]); vector<int> kp = {cx, a[0], a[2], a[4], b[0], b[2], b[4]}; sort(all(kp)); for (int i : kp) { if (get(i, a) < get(i, b)) return 1; if (get(i, a) > get(i, b)) return 0; } return 0; } void solve() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> p[i]; --p[i]; f[p[i]] = i; } if (n % 2 == 0) { if (k > 1) { cout << "-1\n"; return; } for (int i = 0; i < n; i += 2) { swap(p[f[i]], p[f[i + 1]]); } for (int i = 0; i < n; i++) { cout << p[i] + 1 << " "; } cout << "\n"; return; } ord.clear(); for (int i = 0; i + 2 < n; i += 2) { int p1 = f[i]; int p2 = f[i + 1]; int p3 = f[i + 2]; for (int i1 = i + 1; i1 <= i + 2; i1++) for (int i2 = i; i2 <= i + 2; i2 += 2) for (int i3 = i; i3 <= i + 1; i3++) { if (i1 == i2 || i1 == i3 || i2 == i3) continue; ord.push_back({p1, i1, p2, i2, p3, i3}); } } int fnt = 1e9; for (int i = 0; i < n; i++) { if (fnt > f[i]) { fnt = i; fl[i] = n; } else { fl[i] = fnt; } } sort(all(ord), cc); if (sz(ord) < k) { cout << "-1\n"; return; } for (int i = 0; i < n; i++) { int v = get(i, ord[k - 1]); cout << v + 1 << " "; } cout << "\n"; // cout<<"\n\n\n\n"; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } }
failed
3_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; long long p; cin >> n >> p; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } auto okay = [&](int b) -> bool { vector<long long> get(a.begin(), a.begin() + b); sort(get.begin(), get.end()); long long last = 0; for (int i = 0; i < b - 1; i++) { long long x = get[i]; long long y = get[i + 1]; long long start = last + y - 1; last = start + x; if (last > p) return false; } long long x = get.back(); if (last + x <= p) return true; return false; }; int l = 0, r = n; while (l < r) { int m = (l + r + 1) >> 1; if (okay(m)) { l = m; } else { r = m - 1; } } cout << l << '\n'; }
failed
107_2_wrong.cpp
#pragma GCC optimize(3) #include "ext/pb_ds/assoc_container.hpp" #include <bits/stdc++.h> #define ll long long #define ull unsigned long long #define pii pair<int, int> #define vi vector<int> #define hmap unordered_map #define hset unordered_set #define test cout << "test" << endl #define all(x) (x).begin(), (x).end() #define endl '\n' using namespace std; #define int long long const int maxn = 4e5 + 10; // const double pi = 3.14159265358979323846; // const int mod = 1e9 + 7; // const int mod = 998244353; int n, m; int r[5005]; int dp[5005][5005][2]; int lim[5005][5005]; int get(int x) { return (x % n + n) % n; } void solve() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> r[i]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { dp[i][j][0] = dp[i][j][1] = lim[i][j] = 1e9 + 1; } } for (int i = 1; i <= m; i++) { int p, d, t; cin >> p >> d >> t; int l1 = get(p - d - r[p]); int r1 = get(p - d + r[p]); if (2 * r[p] > n) continue; lim[l1][r1] = min(t, lim[l1][r1]); } for (int len = 2; len <= n; len++) { for (int l = 0; l < n; l++) { int r = get(l + len - 1); lim[l][r] = min({lim[l][r], lim[get(l + 1)][r], lim[l][get(r - 1)]}); } } for (int len = n - 1; len >= 1; len--) { for (int l = 0; l < n; l++) { int r = get(l + len - 1); dp[l][r][0] = max(min(lim[get(r + 1)][get(l - 1)], dp[get(l - 1)][r][0]) - 1, min(lim[get(r + 1)][get(l - 1)], dp[l][get(r + 1)][1]) - len); dp[l][r][1] = max(min(lim[get(r + 1)][get(l - 1)], dp[get(l - 1)][r][0]) - len, min(lim[get(r + 1)][get(l - 1)], dp[l][get(r + 1)][1]) - 1); } } vector<int> ans; for (int i = 0; i < n; i++) { // cout<<"i="<<i<<" dp0="<<dp[i][i][0]<<" dp1="<<dp[i][i][1]<<endl; if (dp[i][i][0] >= 0 || dp[i][i][1] >= 0) ans.push_back(1); else ans.push_back(0); } for (auto x : ans) { cout << x; } cout << endl; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // cout << fixed << setprecision(4); int t1 = 1; cin >> t1; while (t1--) { solve(); } }
failed
77_1_wrong.cpp
#include <bits/stdc++.h> #define int int64_t using pii = std::pair<int, int>; const int kMaxN = 4e5 + 5; struct Node { int t, id, op; Node(int _t = 0, int _id = 0, int _op = 0) : t(_t), id(_id), op(_op) {} friend bool operator<(const Node &n1, const Node &n2) { if (n1.t != n2.t) return n1.t < n2.t; else return n1.op > n2.op; } }; int n, m, p, d; int cs, fir; int cnt[2], now[2], unq[kMaxN]; pii seg[kMaxN], mx[kMaxN], mi[kMaxN]; std::vector<int> v[2], sum[2]; std::vector<Node> vec[2]; struct SGT { int N; pii mx[kMaxN * 4], mi[kMaxN * 4]; void pushup(int x) { mx[x] = std::max(mx[x << 1], mx[x << 1 | 1]); mi[x] = std::min(mi[x << 1], mi[x << 1 | 1]); } void build(int n) { for (N = 1; N <= n + 1; N <<= 1) { } for (int i = N; i <= N + n; ++i) mx[i] = ::mx[i - N], mi[i] = ::mi[i - N]; for (int i = N - 1; ~i; --i) pushup(i); } pii querymax(int l, int r) { if (l > r) return {-1e18, 0}; pii ret = {-1e18, 0}; for (l += N - 1, r += N + 1; l ^ r ^ 1; l >>= 1, r >>= 1) { if (~l & 1) ret = std::max(ret, mx[l ^ 1]); if (r & 1) ret = std::max(ret, mx[r ^ 1]); } return ret; } pii querymin(int l, int r) { if (l > r) return {1e18, 0}; pii ret = {1e18, 0}; for (l += N - 1, r += N + 1; l ^ r ^ 1; l >>= 1, r >>= 1) { if (~l & 1) ret = std::min(ret, mi[l ^ 1]); if (r & 1) ret = std::min(ret, mi[r ^ 1]); } return ret; } } sgt; void prework() { cnt[0] = cnt[1] = now[0] = now[1] = 0; for (int o = 0; o < 2; ++o) { std::set<int> st; v[o].clear(); // std::sort(vec[o].begin(), vec[o].end()); for (auto [t, id, op] : vec[o]) if (op == 1) st.emplace(id); cnt[o] = st.size(); for (auto [t, id, op] : vec[o]) { if (!st.count(id)) continue; if (!op) now[o] += p; else now[o] += t, st.erase(id), v[o].emplace_back(t); } } } void discrete() { m = 0; for (auto x : v[0]) unq[++m] = x; for (auto x : v[1]) unq[++m] = x; unq[++m] = 0; unq[++m] = 1e13; std::sort(unq + 1, unq + 1 + m); m = std::unique(unq + 1, unq + 1 + m) - (unq + 1); unq[m + 1] = 1e14; } int getsum(int o, int x) { if (x < 0) return 0; else return sum[o][x]; } pii getfunc(int x, int p) { return {p * seg[x].first + seg[x].second, p}; } pii getmi(int x, int l, int r) { l = std::max<int>(l, 0); if (l > r) return {1e18, 0}; return std::min(getfunc(x, l), getfunc(x, r)); } pii getmx(int x, int l, int r) { l = std::max<int>(l, 0); if (l > r) return {-1e18, 0}; return std::max(getfunc(x, l), getfunc(x, r)); } int getfunc(int x) { int i = std::lower_bound(unq + 1, unq + 1 + m, x) - unq - 1; if (!i) ++i; // std::cerr << x << ' ' << unq[i] << ' ' << unq[i + 1] << '\n'; assert(i < m); assert(unq[i] <= x && x <= unq[i + 1]); return x * seg[i].first + seg[i].second; } int _getfunc(int x) { int ret = 0; for (int o = 0; o < 2; ++o) { int sum = 0; for (auto t : v[o]) sum += std::min(x, t); if (o == 0) ret += sum; else ret -= sum; } return ret; } void getseg() { for (int o = 0; o < 2; ++o) { int s = 0; sum[o].clear(); for (int i = 0; i < v[o].size(); ++i) sum[o].emplace_back(s += v[o][i]); } for (int i = 1; i < m; ++i) { int p1 = std::lower_bound(v[0].begin(), v[0].end(), unq[i + 1]) - v[0].begin(); int p2 = std::lower_bound(v[1].begin(), v[1].end(), unq[i + 1]) - v[1].begin(); seg[i] = {v[0].size() - p1 - (v[1].size() - p2), getsum(0, p1 - 1) - getsum(1, p2 - 1)}; mi[i] = getmi(i, unq[i], unq[i + 1]); mx[i] = getmx(i, unq[i], unq[i + 1]); } } pii getpr(int x) { for (int i = 1; i <= m - 1; ++i) { int j = std::upper_bound(unq + 1, unq + 1 + m, unq[i] + x) - unq - 1; int now = getfunc(unq[i]); auto p = sgt.querymax(i, j - 1); if (p.first - now >= d) return {unq[i], p.second}; p = getmx(j, unq[j], unq[i] + x); if (p.first - now >= d) return {unq[i], p.second}; } for (int i = m; i > 1; --i) { int j = std::lower_bound(unq + 1, unq + 1 + m, std::max<int>(unq[i] - x, 0)) - unq; int now = getfunc(unq[i]); auto p = sgt.querymin(j, i - 1); if (now - p.first >= d) return {p.second, unq[i]}; p = getmi(j - 1, std::max<int>(unq[i] - x, 0), unq[j]); if (now - p.first >= d) return {p.second, unq[i]}; } return {-1, -1}; } bool check(int x) { return getpr(x).first != -1; } int cei(int x, int y) { if (x < 0) return -((-x) / y); else return (x + y - 1) / y; } int flr(int x, int y) { if (x < 0) return -((-x + y - 1) / y); else return x / y; } pii getpp(pii p) { // x * p.first + p.second >= d int tmp = d - p.second; // x * p.first >= tmp if (p.first == 0) return tmp <= 0 ? pii{0, 1e18} : pii{1e18, 1e18}; else if (p.first > 0) return {cei(tmp, p.first), 1e18}; else return {0, flr(-tmp, -p.first)}; } pii calc(int i, int j, int len) { assert(j < m); int l = std::max(unq[j], unq[i - 1] + len), r = std::min(unq[j + 1], unq[i] + len); if (l > r) return {1e18, 1e18}; pii p = {seg[j].first - seg[i - 1].first, seg[j].second - seg[i - 1].second + len * seg[i - 1].first}; auto pp = getpp(p); if (std::max(l, pp.first) <= std::min(r, pp.second)) return {std::max(l, pp.first) - len, std::max(l, pp.first)}; else return {1e18, 1e18}; // for (int i = l; i <= r; ++i) // if (i * p.first + p.second >= d) // return {i - len, i}; } pii getans() { if (fir == 202567778970ll) std::cout << m << ' ' << unq[2] << ' ' << _getfunc(879492417995ll) - _getfunc(202567778970ll) << ' ' << d << '\n'; int L = 0, R = 1e12 + 1, res = 0; while (L + 1 < R) { int mid = (L + R) >> 1; if (check(mid)) R = res = mid; else L = mid; } if (!res) return {-1, -1}; pii ret = {1e18, 1e18}; for (int i = 2; i < m; ++i) { int l, r; r = std::upper_bound(unq + 1, unq + 1 + m, unq[i] + res) - unq - 1; l = std::lower_bound(unq + 1, unq + 1 + m, unq[i - 1] + res) - unq - 1; l = std::max(l, i); for (int j = l; j <= r; ++j) ret = std::min(ret, calc(i, j, res)); } return ret; } bool fl = 1; void dickdreamer() { ++cs; std::cin >> n >> p; vec[0].clear(), vec[1].clear(); for (int i = 1; i <= n; ++i) { int c, t, id, op; std::cin >> c >> id >> t >> op; vec[c - 1].emplace_back(t, id, op); if (i == 1) fir = t; } prework(), discrete(); if (cnt[0] != cnt[1]) return void(std::cout << "-1\n"); if (now[0] > now[1]) d = now[0] - now[1]; else d = now[1] - now[0] + 1, std::swap(now[0], now[1]), v[0].swap(v[1]), vec[0].swap(vec[1]); getseg(); // if (fl && cs == 6) assert(std::count(unq + 1, unq + 1 + m, 66660446969ll) // || std::count(unq + 1, unq + 1 + m, 904724933033ll)); sgt.build(m); auto res = getans(); if (res.first == -1) std::cout << "-1\n"; else std::cout << res.first << ' ' << res.second << '\n'; } int32_t main() { #ifdef ORZXKR freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0); int T = 1; std::cin >> T; fl &= (T == 10000); while (T--) dickdreamer(); // std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n"; return 0; }
failed
88_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n, m; cin >> n >> m; vector<int> c(n); for (int i = 0; i < n; ++i) { cin >> c[i]; } vector<vector<int>> a(m, vector<int>(n)); for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { cin >> a[i][j]; } } vector<int> current_positions; for (int j = 0; j < n; ++j) { if (c[j] == 1) { current_positions.push_back(j); } } if (current_positions.empty()) { cout << "-1\n"; continue; } bool possible = true; for (int j : current_positions) { if (a[0][j] != 0) { possible = false; break; } } if (!possible) { cout << "-1\n"; continue; } vector<int> destroyed(n, 0); vector<vector<int>> prefix(n, vector<int>(m)); for (int j = 0; j < n; ++j) { prefix[j][0] = a[0][j]; for (int i = 1; i < m; ++i) { prefix[j][i] = prefix[j][i-1] + a[i][j]; } } int current_L = current_positions.front(); int current_R = current_positions.back(); int total_cost = 0; for (int i = 0; i < m; ++i) { for (int j : current_positions) { if (a[i][j] != 0) { possible = false; break; } } if (!possible) break; if (i == m - 1) break; vector<int> new_zeros; for (int j = 0; j < n; ++j) { if (a[i+1][j] == 0) { new_zeros.push_back(j); } } if (new_zeros.empty()) { possible = false; break; } int new_zeros_L = new_zeros.front(); int new_zeros_R = new_zeros.back(); for (size_t j = 1; j < new_zeros.size(); ++j) { if (new_zeros[j] != new_zeros[j-1] + 1) { possible = false; break; } } if (!possible) break; int possible_new_L = max(current_L, new_zeros_L); int possible_new_R = min(current_R, new_zeros_R); int new_L, new_R; if (possible_new_L <= possible_new_R) { new_L = possible_new_L; new_R = possible_new_R; } else { if (new_zeros_R < current_L) { new_L = new_zeros_R; new_R = new_zeros_R; } else { new_L = new_zeros_L; new_R = new_zeros_L; } } int merged_L = min(current_L, new_L); int merged_R = max(current_R, new_R); int cost = 0; for (int j = merged_L; j <= merged_R; ++j) { int sum = prefix[j][i]; cost += max(0, sum - destroyed[j]); } for (int j = merged_L; j <= merged_R; ++j) { destroyed[j] = prefix[j][i]; } total_cost += cost; current_L = new_L; current_R = new_R; current_positions.clear(); for (int j = current_L; j <= current_R; ++j) { current_positions.push_back(j); } } if (possible) { cout << total_cost << "\n"; } else { cout << "-1\n"; } } return 0; }
failed
81_1_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 bool used_vars[100]; string ln; stringstream ss; enum Kind { VAR, MIN, MAX, LE, LT, }; int binops_printed = 0; struct Expr { Kind kind; Expr *l; Expr *r; int v; Expr(Kind kind, Expr *l, Expr *r) : kind(kind), l(l), r(r), v(-1) {} Expr(int v) : kind(VAR), v(v) { used_vars[v] = 1; } int eval(const vector<int> &vars) { if (kind == VAR) { return vars[v]; } else if (kind == MIN) { return min(l->eval(vars), r->eval(vars)); } else if (kind == MAX) { return max(l->eval(vars), r->eval(vars)); } else if (kind == LE) { return l->eval(vars) <= r->eval(vars); } else if (kind == LT) { return l->eval(vars) < r->eval(vars); } assert(0); } friend ostream &operator<<(ostream &os, const Expr &e) { if (e.kind != VAR) ++binops_printed; if (e.kind == VAR) { char c = e.v + 'a'; return os << c; } else if (e.kind == MIN) { return os << "(min " << *e.l << ' ' << *e.r << ')'; } else if (e.kind == MAX) { return os << "(max " << *e.l << ' ' << *e.r << ')'; } else if (e.kind == LT) { return os << "(" << *e.l << " < " << *e.r << ")"; } else if (e.kind == LE) { return os << "(" << *e.l << " <= " << *e.r << ")"; } assert(0); } }; Expr *parse_expr(); Expr *parse_op() { if (ss.peek() == 'm') { char a, b, c; ss >> a >> b >> c; Expr *l = parse_expr(); Expr *r = parse_expr(); if (b == 'a') { return new Expr(MAX, l, r); } else { return new Expr(MIN, l, r); } } else { Expr *l = parse_expr(); char c; ss >> c; if (ss.peek() == '=') { ss >> c; Expr *r = parse_expr(); return new Expr(LE, l, r); } else { Expr *r = parse_expr(); return new Expr(LT, l, r); } } } Expr *parse_expr() { char c; if (ss.peek() == '(') { ss >> c; Expr *ret = parse_op(); ss >> c; return ret; } else { ss >> c; return new Expr(c - 'a'); } } Expr *land(vector<Expr *> es) { Expr *l = es[0]; for (int i = 1; i < es.size(); ++i) { l = new Expr(MIN, l, es[i]); } return l; } Expr *lit_zero() { vector<Expr *> es; for (int c = 0; c < 20; ++c) if (used_vars[c]) es.push_back(new Expr(c)); return land(es); } Expr *lnot(Expr *l) { return new Expr(LE, l, lit_zero()); } Expr *lor(Expr *l, Expr *r) { return new Expr(LE, new Expr(LE, l, r), r); } Expr *lor(vector<Expr *> es) { Expr *l = es[0]; for (int i = 1; i < es.size(); ++i) { l = lor(l, es[i]); } return l; } Expr *fix(Expr *e) { if (e->kind == VAR) { return e; } else if (e->kind == MIN || e->kind == LE) { e->l = fix(e->l); e->r = fix(e->r); return e; } else if (e->kind == MAX) { return lor(fix(e->l), fix(e->r)); } else if (e->kind == LT) { return new Expr(MIN, lnot(fix(e->l)), fix(e->r)); } assert(0); } signed main() { cin.tie(0)->sync_with_stdio(0); getline(cin, ln); for (char c : ln) { if (c != ' ') { ss << c; } } Expr *expr = parse_expr(); // { // vector<Expr*> es; // for (int i = 0; i < 10; ++i) { // es.push_back(new Expr(i)); // } // Expr* l = land(es); // Expr* a = new Expr(LE, l, new Expr(0)); // cout << *a << '\n'; // return 0; // } // vector<int> vars(10, 0); // for (int a = 0; a <= 1; ++a) { // for (int b = 0; b <= 1; ++b) { // vars[0] = a; // vars[1] = b; // cout << expr->eval(vars) << '\n'; // } // } // return 0; { // see if all ones... vector<int> vars(10, 1); int all_ones = expr->eval(vars); if (all_ones == 0) { cout << "NO\n"; return 0; } } cout << "YES\n"; Expr *ans = fix(expr); cout << *ans << '\n'; assert(binops_printed <= 40000); } /* (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( */
failed
93_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const ld EPS = 1e-9; struct Point { ll x, y, z; Point(ll x = 0, ll y = 0, ll z = 0) : x(x), y(y), z(z) {} }; // Calculate squared distance from point (x,y) to (cx,cy) ll dist2_to_center(ll x, ll y, ll cx, ll cy) { ll dx = x - cx; ll dy = y - cy; return dx * dx + dy * dy; } bool check_cylinder(const vector<Point> &points) { int n = points.size(); if (n <= 2) return true; // 1 or 2 points always form a valid cylinder // Try each point as potential center for (int i = 0; i < n; i++) { ll cx = points[i].x; ll cy = points[i].y; // Calculate distances from this center to all other points vector<ll> distances; bool possible = true; for (int j = 0; j < n; j++) { if (i == j) continue; ll d2 = dist2_to_center(points[j].x, points[j].y, cx, cy); if (distances.empty()) { distances.push_back(d2); } else if (d2 != distances[0]) { possible = false; break; } } if (possible && !distances.empty() && distances[0] > 0) { return true; } } // Try (0,0) as center ll first_dist = -1; bool zero_center_possible = true; for (const auto &p : points) { ll d2 = dist2_to_center(p.x, p.y, 0, 0); if (first_dist == -1) { first_dist = d2; } else if (d2 != first_dist) { zero_center_possible = false; break; } } if (zero_center_possible && first_dist > 0) { return true; } // Try midpoints between pairs of points for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // Skip if points are same in xy-plane if (points[i].x == points[j].x && points[i].y == points[j].y) { continue; } // Try midpoint as center ll cx = (points[i].x + points[j].x) / 2; ll cy = (points[i].y + points[j].y) / 2; bool possible = true; ll radius = dist2_to_center(points[i].x, points[i].y, cx, cy); for (const auto &p : points) { if (dist2_to_center(p.x, p.y, cx, cy) != radius) { possible = false; break; } } if (possible && radius > 0) { return true; } } } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<Point> points(n); for (int i = 0; i < n; i++) { cin >> points[i].x >> points[i].y >> points[i].z; } cout << (check_cylinder(points) ? "probably" : "not a penguin") << endl; return 0; }
failed
78_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long const int p = 998244353; int po(int a, int b) { if (b == 0) return 1; if (b == 1) return a; if (b % 2 == 0) { int u = po(a, b / 2); return (u * 1LL * u) % p; } else { int u = po(a, b - 1); return (a * 1LL * u) % p; } } int inv(int x) { return po(x, p - 2); } mt19937 rnd; #define app push_back #define all(x) (x).begin(), (x).end() #ifdef LOCAL #define debug(...) \ [](auto... a) { ((cout << a << ' '), ...) << endl; }(#__VA_ARGS__, ":", \ __VA_ARGS__) #define debugv(v) \ do { \ cout << #v << " : {"; \ for (int izxc = 0; izxc < v.size(); ++izxc) { \ cout << v[izxc]; \ if (izxc + 1 != v.size()) \ cout << ","; \ } \ cout << "}" << endl; \ } while (0) #else #define debug(...) #define debugv(v) #endif #define lob(a, x) lower_bound(all(a), x) #define upb(a, x) upper_bound(all(a), x) int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t, m; cin >> t >> m; auto solve = [&]() { string s; cin >> s; string curnum, curstr; vector<string> strs, nums; for (char c : s) { if ('0' <= c && c <= '9') { if (curstr.size()) { strs.app(curstr); curstr.clear(); } curnum += c; } else { if (curnum.size()) { nums.app(curnum); curnum.clear(); } curstr += c; } } if (curstr.size()) { strs.app(curstr); } else { nums.app(curnum); } if (curstr.empty()) { cout << 0 << ' ' << 0 << '\n'; return; } set<string> cand; for (int i = 0; i < 300; ++i) { cand.insert(to_string(i)); } set<string> candtasks; for (int i = 0; i <= m; ++i) { candtasks.insert(to_string(i)); } cand.insert(""); auto nums0 = nums; for (int l = 2; l < nums0.front().size(); ++l) { int r = nums0.front().size() - l; nums = nums0; nums.front() = nums0.front().substr(nums0.front().size() - r, r); string p = nums0.front().substr(0, l); for (int mask = 0; mask < (1 << strs.size()); ++mask) { bool ok = true; int sum = 0, k = 0; auto addtask = [&](int trs, int when) { sum += (trs - 1) * 20 + when; k++; }; for (int i = 0; i < strs.size(); ++i) { if (nums[i].size() >= 3 && nums[i].substr(nums[i].size() - 3, 3) == "100") { string os = nums[i].substr(0, nums[i].size() - 3); if (!cand.count(os)) { ok = false; } else { if (os.size()) addtask(100, stoll(os)); } } else if (strs[i] == "try") { string os = nums[i].substr(0, nums[i].size() - 1); if (!cand.count(os)) { ok = false; } else { if (os.size()) addtask(1, stoll(os)); } } else if ((mask >> i) & 1) { if (nums[i].size() < 2) { ok = false; } else { string os = nums[i].substr(0, nums[i].size() - 2); if (os.empty()) { } else { string suf = nums[i].substr(nums[i].size() - 2, 2); if (suf.front() == '0') { ok = false; } else { if (cand.count(os)) { addtask(stoll(suf), stoll(os)); } else { ok = false; } } } } } else { string os = nums[i].substr(0, nums[i].size() - 1); if (os.empty()) { } else { string suf = nums[i].substr(nums[i].size() - 1, 1); if (suf == "0" || suf == "1") { ok = false; } else { if (cand.count(os)) { addtask(stoll(suf), stoll(os)); } else { ok = false; } } } } } auto out = [&](string a, string b) { if (a.empty()) { cout << b << ' '; } else { cout << a << ' ' << b << ' '; } }; if (ok) { string w = to_string(sum); if (p.size() > w.size()) { string suf = p.substr(p.size() - w.size(), w.size()); if (suf == w) { string tasks = p.substr(0, p.size() - w.size()); if (candtasks.count(tasks)) { if (stoll(tasks) == k) { cout << tasks << ' ' << suf << ' '; for (int i = 0; i < nums.size(); ++i) { if (nums[i].size() >= 3 && nums[i].substr(nums[i].size() - 3, 3) == "100") { out(nums[i].substr(0, nums[i].size() - 3), "100"); } else if (strs[i] == "try") { out(nums[i].substr(0, nums[i].size() - 1), "1"); } else { int g = ((mask) >> i) & 1; g++; out(nums[i].substr(0, nums[i].size() - g), nums[i].substr(nums[i].size() - g, g)); } cout << strs[i]; if (i + 1 < nums.size()) { cout << ' '; } } cout << '\n'; return; } } } } } } } }; while (t--) { solve(); } return 0; }
failed
16_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long #define PII pair<int, int> #define ai4 array<int, 4> #define ai3 array<int, 3> const int N = 50010, mod = 1e9 + 7, inf = 1e9; void solve() { int n; cin >> n; vector<int> a(n + 1); int ans = 0; set<int> S; for (int i = 1; i <= n; i++) { cin >> a[i]; S.insert(a[i]); } for (int i = 1; i < n; i++) { int p = 0, q = 0, r = 0; for (int j = 31; j >= 0; j--) { int L = a[i] & (1ll << j); int R = a[i + 1] & (1ll << j); if (L && R) p += L; else if (L) q += L; else if (R) r += R; } S.insert(p); S.insert(q); S.insert(r); S.insert(p + q); S.insert(p + r); S.insert(q + r); S.insert(p + r + q); } cout << S.size() << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; while (T--) solve(); }
failed
6_1_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 // ll f[1111],ret,g[30][30][1111],tg[30][1111]; map<pair<VI, VI>, map<ll, ll>> hs; map<ll, ll> count(VI s, VI coef) { map<ll, ll> v; if (s[0] == 0) { bool z = 1; for (auto x : s) z &= (x == 0); if (z) v[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[key + j * coef.back()] += val; } } return hs[mp(s, coef)] = v; } vector<array<VI, 3>> ret; void dfs(VI s, VI coef, VI p1, VI p2) { // printf("?? %lld %d\n",s[0],SZ(p1)); if (s[0] == 0) { bool z = 1; for (auto x : s) z &= (x == 0); if (z) { reverse(all(p1)); reverse(all(p2)); int d = SZ(p2); VI p3 = p2; while (1) { __int128_t x = 0; rep(j, 0, d) x += (__int128_t)p1[j] * p3[SZ(p3) - d + j]; if (x >= (1ll << 62) || SZ(p3) >= 100) break; p3.pb(x); } p3 = VI(p3.begin() + d, p3.end()); ret.pb({p3, p1, p2}); } } if (!hs.count(mp(s, coef))) return; if (hs[mp(s, coef)].empty()) return; // assert(hs.count(mp(s,coef))); // if (count(s,coef).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; } } if (v <= 400000) break; } // printf("?? %d\n",SZ(seq)); printf("%d\n", clock()); count(seq, seq); dfs(seq, seq, VI{}, VI{}); assert(SZ(ret) == v); sort(all(ret)); fprintf(stderr, "!! %d\n", K); auto [p3, p1, p2] = ret[K - 1]; printf("%d\n", SZ(p1)); p3.resize(10); for (auto x : p1) printf("%lld ", x); puts(""); for (auto x : p2) printf("%lld ", x); puts(""); for (auto x : p3) printf("%lld ", x); puts(""); }
failed
114_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; struct Mountain { int x, y; bool active; double left_start, left_end; double right_start, right_end; double b, c; Mountain(int x, int y, int w) : x(x), y(y), active(false) { left_start = max(0.0, x - y + 0.0); left_end = min(static_cast<double>(x), static_cast<double>(w)); right_start = max(static_cast<double>(x), 0.0); right_end = min(static_cast<double>(x + y), static_cast<double>(w)); b = y - x; c = x + y; } }; struct Event { double x; bool is_start; bool is_left; double value; Mountain *m; Event(double x, bool is_start, bool is_left, double value, Mountain *m) : x(x), is_start(is_start), is_left(is_left), value(value), m(m) {} bool operator<(const Event &other) const { if (x != other.x) return x < other.x; return is_start > other.is_start; } }; double calculate_visible(const set<Mountain *> &active_mountains, int w) { vector<Event> events; for (Mountain *m : active_mountains) { if (!m->active) continue; if (m->left_start < m->left_end) { events.emplace_back(m->left_start, true, true, m->b, m); events.emplace_back(m->left_end, false, true, m->b, m); } if (m->right_start < m->right_end) { events.emplace_back(m->right_start, true, false, m->c, m); events.emplace_back(m->right_end, false, false, m->c, m); } } sort(events.begin(), events.end()); multiset<double> left_values, right_values; double current_b = -1e18, current_c = -1e18; double prev_x = 0.0, visible = 0.0; for (const Event &event : events) { double x = event.x; x = max(0.0, min(static_cast<double>(w), x)); if (x > prev_x) { double delta_x = x - prev_x; if (current_b != -1e18 || current_c != -1e18) { if (current_b == -1e18) { visible += delta_x * sqrt(2); } else if (current_c == -1e18) { visible += delta_x * sqrt(2); } else { visible += delta_x * sqrt(2); } } } prev_x = x; if (event.is_left) { if (event.is_start) { left_values.insert(event.value); } else { left_values.erase(left_values.find(event.value)); } current_b = left_values.empty() ? -1e18 : *left_values.rbegin(); } else { if (event.is_start) { right_values.insert(event.value); } else { right_values.erase(right_values.find(event.value)); } current_c = right_values.empty() ? -1e18 : *right_values.rbegin(); } } if (prev_x < w) { double delta_x = w - prev_x; if (current_b != -1e18 || current_c != -1e18) { if (current_b == -1e18) { visible += delta_x * sqrt(2); } else if (current_c == -1e18) { visible += delta_x * sqrt(2); } else { visible += delta_x * sqrt(2); } } } return visible; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int q, w; cin >> q >> w; map<pair<int, int>, Mountain *> mountains; set<Mountain *> active_mountains; for (int i = 0; i < q; ++i) { int x, y; cin >> x >> y; pair<int, int> key(x, y); Mountain *&m = mountains[key]; if (m) { m->active = !m->active; if (m->active) { active_mountains.insert(m); } else { active_mountains.erase(m); } } else { m = new Mountain(x, y, w); m->active = true; active_mountains.insert(m); } double visible = calculate_visible(active_mountains, w); printf("%.6lf\n", visible); } for (auto &entry : mountains) { delete entry.second; } return 0; }
failed
106_3_wrong.cpp
#include "bits/stdc++.h" using namespace std; #define ll long long #define pb push_back #define eb emplace_back #define mp make_pair #define F first #define S second #define V vector #define vi vector<int> #define vl vector<long long> #define vb vector<bool> #define vs vector<string> #define vd vector<double> #define pi pair<int, int> #define pd pair<double, double> #define pl pair<long long, long long> #define all(i) i.begin(), i.end() #define REP(i, a, b) for (int i = a; i <= b; i++) #define PER(i, a, b) for (int i = a; i >= b; i--) #define rep(i, a, b) for (int i = a; i < (b); i++) #define indlb(v, x, c) \ (distance((v).begin(), lower_bound((v).begin(), (v).end(), (x), c))) #define indub(v, x, c) \ (distance((v).begin(), upper_bound((v).begin(), (v).end(), (x), c))) #define sz(i) (int)i.size() #define smax(a, b) a = max(a, b) #define smin(a, b) a = min(a, b) #define currtime chrono::high_resolution_clock::now().time_since_epoch().count() #define int long long #define double long double template <typename T> void printv(V<T> x) { for (T i : x) cout << i << " "; cout << "\n"; } template <typename T> void readv(V<T> &x) { for (T &y : x) cin >> y; } constexpr int MX_NODES = 3e7; constexpr int RANGE_SZ = 2e5 + 5; int tree[MX_NODES], l[MX_NODES], r[MX_NODES]; int timer = -1; int build(int tl, int tr) { const int cur = ++timer; if (tl == tr) { tree[timer] = 0; return cur; } int mid = (tl + tr) / 2; l[cur] = build(tl, mid); r[cur] = build(mid + 1, tr); tree[cur] = tree[l[cur]] + tree[r[cur]]; return cur; } int upd(int pos, int add, int v, int tl = 0, int tr = RANGE_SZ - 1) { const int cur = ++timer; if (tl == tr) { tree[cur] = add + tree[v]; return cur; } l[cur] = l[v], r[cur] = r[v]; const int mid = tl + (tr - tl) / 2; if (pos > mid) { r[cur] = upd(pos, add, r[v], mid + 1, tr); } else { l[cur] = upd(pos, add, l[v], tl, mid); } tree[cur] = tree[l[cur]] + tree[r[cur]]; return cur; } int query(int v, int l1, int r1, int tl = 0, int tr = RANGE_SZ - 1) { if (l1 <= tl && tr <= r1) return tree[v]; const int mid = tl + (tr - tl) / 2; int sum = 0; if (mid >= l1) sum += query(l[v], l1, r1, tl, mid); if (mid < r1) sum += query(r[v], l1, r1, mid + 1, tr); return sum; } V<vi> adj; int MOD = 998244353; vb found; ll binex(ll b, ll e) { b %= MOD; ll ans = 1; while (e > 0) { if (e % 2 == 1) ans *= b; e /= 2; b = b * b; b %= MOD; ans %= MOD; } return ans; } void dfs(int v) { found[v] = true; for (int u : adj[v]) if (!found[u]) dfs(u); } vi roots; vi in; vi out; int ti = 1; vi c2; bool flag = true; void dfs2(int v) { if (flag) c2.pb(v); in[v] = ti; found[v] = true; for (int u : adj[v]) { if (!found[u]) dfs2(u); } out[v] = ti; roots.pb(upd(v, 1, roots.back())); ti++; } int cnt = 0; int n; void dfs3(int v, int p = -1) { for (int u : adj[v]) { if (u == p) continue; int a = query(roots[out[u]], u, n - 1) - query(roots[in[u] - 1], u, n - 1) - 1; cnt += a; dfs3(u, v); } } vi scores; void dfs4(int v, int p = -1) { scores[v] = cnt; for (int u : adj[v]) { if (u == p) continue; int a = query(roots[out[u]], u, n - 1) - query(roots[in[u] - 1], u, n - 1) - 1; int b = query(roots[in[u] - 1], v, n - 1) + query(roots.back(), v, n - 1) - query(roots[out[u]], v, n - 1) - 1; int c = b - a; cnt += c; dfs4(u, v); cnt -= c; } } int base; int ans = 0; vi big; int cnt2 = 0; void dfs5(int v, int p = -1) { ans += binex(2, cnt2); ans %= MOD; for (int u : adj[v]) { if (u == p) continue; cnt2 += big[u]; dfs5(u, v); cnt2 -= big[u]; } } void solve() { cin >> n; adj.resize(n); REP(i, 1, n - 2) { int a, b; cin >> a >> b; a--; b--; adj[a].pb(b); adj[b].pb(a); } REP(i, 0, n - 1) { sort(all(adj[i])); } found = vb(n, false); dfs(0); int r; REP(i, 0, n - 1) if (!found[i]) { r = i; break; } roots.pb(build(0, n - 1)); in = vi(n, 0); out = vi(n, 0); dfs2(r); dfs3(r); scores.resize(n); dfs4(r); vb inc2(n, false); for (int x : c2) inc2[x] = true; V<pi> guys; base = 0; int mult = 1; PER(i, n - 1, 0) { if (inc2[i]) { base += (binex(2, scores[i])) * mult; base %= MOD; mult *= 2; mult %= MOD; } } big.resize(n); for (int x : c2) big[x]++; PER(i, n - 2, 0) { big[i] += big[i + 1]; } timer = 0; ti = 1; roots.clear(); roots.pb(build(0, n - 1)); cnt = 0; flag = false; found = vb(n, false); dfs2(0); dfs3(0); dfs5(0); ans *= base; ans %= MOD; if (sz(c2) == 1) { ans += binex(2, cnt); ans %= MOD; } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; // cin >> t; while (t--) solve(); return 0; }
failed
85_2_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_2_wrong.cpp
#include <cstring> #include <iostream> #include <vector> using namespace std; using LL = long long; LL exgcd(LL a, LL b, LL &x, LL &y) { if (!b) { x = 1, y = 0; return a; } LL r = exgcd(b, a % b, y, x); y -= a / b * x; return r; } 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); int T; cin >> T; while (T--) { LL a, b, n; cin >> a >> b >> n; if (n > a * b) { cout << "No" << '\n'; continue; } n = a * b - n; LL x, y; exgcd(a, b, x, y); __int128_t xx = (__int128_t(x * n) % b + b) % b; __int128_t yy = (n - a * xx) / b; cout << (xx >= 0 and yy >= 0 ? "Yes" : "No") << '\n'; } }
failed
18_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e3; int a[MAXN][MAXN]; void apply(int x, int y, const vector<vector<int>> &b, int &cur) { int mx = -1; for (int i = 0; i < (int)b.size(); ++i) { for (int j = 0; j < (int)b[i].size(); ++j) { if (b[i][j] == -1) continue; a[x + i][y + j] = b[i][j] + cur; mx = max(mx, b[i][j]); } } cur += mx + 1; } int cur; void color(int n) { vector<vector<int>> g(cur); auto add = [&](int u, int v) { if (u == -1 || v == -1) return; g[u].push_back(v); g[v].push_back(u); }; for (int i = 0; i < n; ++i) { for (int j = 0; j <= i; ++j) { if (j > 0) { add(a[i][j], a[i][j - 1]); } if (i < n - 1) { add(a[i][j], a[i + 1][j]); add(a[i][j], a[i + 1][j + 1]); } } } vector<int> col(cur); for (int i = 0; i < cur; ++i) { vector<int> used(26); for (int j : g[i]) { if (j < i) { used[col[j]] = 1; } } for (int c = 0; c < 26; ++c) { if (!used[c]) { col[i] = c; break; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j <= i; ++j) { if (a[i][j] != -1) a[i][j] = col[a[i][j]]; } } } void print(int n) { for (int i = 0; i < n; ++i) { string s(2 * n - 1, ' '); for (int j = 0; j <= i; ++j) { int pos = n - 1 - i + 2 * j; if (a[i][j] == -1) { s[pos] = '.'; } else { s[pos] = 'A' + a[i][j]; } } cout << s << "\n"; } } vector<vector<int>> b10s = {{0}, {0}, {-1, 0}}; vector<vector<int>> b10f = { {-1, 0}, {-1, 4, 0}, {4, 4, 0, 1}, {-1, 3, 3, 2, 1}, {-1, 3, 2, 2, 1, -1}}; vector<vector<int>> b11s = { {0}, {0, 1}, {2, 0, 1, 1}, {2, 3, 4, 4}, {-1, 2, 3, 3, 4}}; vector<vector<int>> b11f = {{0}, {-1, 0}, {-1, 0, -1}}; vector<vector<int>> b20s = { {0, -1, 3}, {1, 0, 2, 3}, {1, 0, 2, 3}, {-1, 1, -1, 2}}; vector<vector<int>> b20f = {{0}, {-1, 0}, {-1, 0, 1}, {-1, 1, 1, -1}}; vector<vector<int>> b21s = { {0, -1, 4}, {1, 0, 3, 4, 4}, {1, 0, 2, 3, 3}, {-1, 1, -1, 2, 2}}; void solve_() { int n; cin >> n; for (int i = 0; i < n; ++i) { fill(a[i], a[i] + i + 1, -1); } if (n <= 2) { color(n); print(n); return; } if (n == 3) { cout << " W \n"; cout << " W . \n"; cout << ". W .\n"; return; } if (n == 5) { cout << " W \n"; cout << " B W \n"; cout << " B W G \n"; cout << " R B G . \n"; cout << ". R R G .\n"; return; } if (n % 3 == 1) { a[0][0] = a[1][1] = a[2][1] = 0; a[1][0] = a[2][0] = a[3][1] = 1; cur = 2; for (int k = 4; k < n; k += 3) { if (k % 2 == 0) { apply(k - 1, 0, b10s, cur); apply(k - 2, k - 3, b10f, cur); for (int i = 0; i < k - 4; ++i) { a[k][1 + i] = a[k + 1][2 + i] = a[k + 2][2 + i] = cur++; } } else { apply(k - 2, 0, b11s, cur); apply(k - 1, k - 1, b11f, cur); for (int i = 0; i < k - 4; ++i) { a[k][4 + i] = a[k + 1][4 + i] = a[k + 2][5 + i] = cur++; } } } color(n); print(n); return; } if (n % 3 == 0) { a[0][0] = a[1][0] = a[2][1] = 0; a[1][1] = a[2][2] = a[3][2] = 1; a[2][0] = a[3][1] = a[4][1] = 2; a[3][0] = a[4][0] = a[5][1] = 3; a[3][3] = a[4][3] = a[4][2] = 4; a[4][4] = a[5][4] = a[5][3] = 5; cur = 6; for (int k = 6; k < n; k += 3) { if (k % 2 == 0) { apply(k - 1, 0, b20s, cur); apply(k - 1, k - 1, b20f, cur); for (int i = 0; i < (k - 4) / 2; ++i) { a[k][4 + 2 * i] = a[k][4 + 2 * i + 1] = a[k + 1][4 + 2 * i] = cur++; a[k + 2][4 + 2 * i] = a[k + 2][4 + 2 * i + 1] = a[k + 1][4 + 2 * i + 1] = cur++; } } else { apply(k - 1, 0, b21s, cur); apply(k - 1, k - 1, b20f, cur); for (int i = 0; i < (k - 5) / 2; ++i) { a[k][5 + 2 * i] = a[k][5 + 2 * i + 1] = a[k + 1][5 + 2 * i] = cur++; a[k + 2][5 + 2 * i] = a[k + 2][5 + 2 * i + 1] = a[k + 1][5 + 2 * i + 1] = cur++; } } } color(n); print(n); return; } if (n % 3 == 2) { a[0][0] = a[1][0] = a[2][1] = 0; a[1][1] = a[2][2] = a[3][2] = 1; a[2][0] = a[3][1] = a[4][1] = 2; a[3][3] = a[4][3] = a[4][2] = 3; a[4][4] = a[5][5] = a[6][5] = 4; a[6][6] = a[7][6] = a[7][5] = 5; a[7][4] = a[7][3] = a[6][4] = 6; a[6][3] = a[5][3] = a[5][4] = 7; a[6][1] = a[6][2] = a[5][2] = 8; a[7][1] = a[6][0] = a[5][0] = 9; a[5][1] = a[4][0] = a[3][0] = 10; cur = 11; for (int k = 8; k < n; k += 3) { if (k % 2 == 0) { apply(k - 1, 0, b20s, cur); apply(k - 1, k - 1, b20f, cur); for (int i = 0; i < (k - 4) / 2; ++i) { a[k][4 + 2 * i] = a[k][4 + 2 * i + 1] = a[k + 1][4 + 2 * i] = cur++; a[k + 2][4 + 2 * i] = a[k + 2][4 + 2 * i + 1] = a[k + 1][4 + 2 * i + 1] = cur++; } } else { apply(k - 1, 0, b21s, cur); apply(k - 1, k - 1, b20f, cur); for (int i = 0; i < (k - 5) / 2; ++i) { a[k][5 + 2 * i] = a[k][5 + 2 * i + 1] = a[k + 1][5 + 2 * i] = cur++; a[k + 2][5 + 2 * i] = a[k + 2][5 + 2 * i + 1] = a[k + 1][5 + 2 * i + 1] = cur++; } } } color(n); print(n); return; } } /// #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
73_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<pii, int> ppi; typedef pair<int, pii> pip; typedef pair<pii, pii> ppp; typedef pair<ll, ll> pll; typedef long double ld; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpp; typedef vector<pll> vll; #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define A first #define B second #define endl '\n' #define lc id << 1 #define rc id << 1 | 1 #define all(x) x.begin(), x.end() #define SZ(x) int(x.size()) #define sep ' ' #define pb push_back #define rep(i, a, b) for (int i = (a); i < (b); i++) const int xn = 5e3 + 10; const int xm = -20 + 10; const int inf = 1e9 + 10; const ll INF = 1e18 + 10; const int mod = 998244353; const int base = 257; const int sq = 320; int qq, n; vi ans, a; void check(vi vec, vi tmp) { // if (!SZ(vec)) return; vi res = tmp; for (int x : vec) res.pb(x); ans = min(ans, res); int ind1 = -1, ind2 = -1; for (int i = 1; i < SZ(vec); ++i) { if (vec[i] < vec[0]) { ind1 = i; break; } } for (int i = 1; i < SZ(vec); ++i) { if (vec[i] > vec[0]) { if (ind2 == -1 || vec[i] < vec[ind2]) { ind2 = i; } } } vi I = {}; if (ind1 != -1) I.pb(ind1); if (ind2 != -1) I.pb(ind2); // I = {}; // rep(ind, 1, SZ(vec)) I.pb(ind); for (int ind : I) { vi v1 = tmp, v2 = tmp; v1.pb(vec[0]), v2.pb(vec[ind]); for (int i = 1; i < ind; ++i) v1.pb(vec[i]); for (int i = ind + 1; i < SZ(vec); ++i) { if (vec[ind] < vec[0]) v2.pb(vec[i]); else v1.pb(vec[i]); } ans = min(ans, max(v1, v2)); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> qq; while (qq--) { cin >> n; a.resize(n); rep(i, 0, n) cin >> a[i]; vi tmp = {}; ans = {inf}; while (SZ(a)) { check(a, tmp); int ind = -1; for (int i = 1; i < SZ(a); ++i) { if (a[i] == a[0]) { ind = i; break; } } if (ind == -1) break; tmp.pb(a[0]); vi ap = {}; for (int i = 0; i < SZ(a); ++i) { if (i != 0 && i != ind) { ap.pb(a[i]); } } a = ap; } if (!SZ(a)) ans = min(ans, tmp); cout << SZ(ans) << "\n"; for (int x : ans) cout << x << " "; cout << "\n"; } return 0; }
failed
27_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; long long getDotProduct(const vector<int> &A, const vector<int> &B) { long long sum = 0; for (size_t i = 0; i < A.size(); i++) { sum += (long long)A[i] * B[i]; } return sum; } long long getMaxDotProduct(const vector<int> &A, const vector<int> &B) { vector<int> sortedA = A, sortedB = B; sort(sortedA.begin(), sortedA.end(), greater<int>()); sort(sortedB.begin(), sortedB.end(), greater<int>()); return getDotProduct(sortedA, sortedB); } // More efficient way to check for improving moves bool hasImprovingMove(const vector<int> &movingPerm, const vector<int> &fixedPerm) { int n = movingPerm.size(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if ((long long)(movingPerm[j] - movingPerm[i]) * (fixedPerm[i] - fixedPerm[j]) > 0) { return true; } } } return false; } char determineWinner(const vector<int> &A, const vector<int> &B) { long long current = getDotProduct(A, B); long long maximum = getMaxDotProduct(A, B); if (current == maximum) return 'B'; // Quick check if Alice has any improving moves if (!hasImprovingMove(A, B)) return 'B'; // Quick check if Bob has any improving moves if (!hasImprovingMove(B, A)) return 'A'; // Count inversions to determine winner int inversions = 0; vector<pair<int, int>> indices(A.size()); for (size_t i = 0; i < A.size(); i++) { indices[i] = {A[i], B[i]}; } for (size_t i = 0; i < indices.size(); i++) { for (size_t j = i + 1; j < indices.size(); j++) { if ((indices[i].first < indices[j].first && indices[i].second > indices[j].second) || (indices[i].first > indices[j].first && indices[i].second < indices[j].second)) { inversions++; } } } return (inversions % 2 == 1) ? 'A' : 'B'; } void shiftLeft(vector<int> &arr, int l, int r, int d) { vector<int> temp(arr.begin() + l, arr.begin() + r + 1); int len = r - l + 1; d %= len; for (int i = 0; i < len; i++) { arr[l + i] = temp[(i + d) % len]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n; cin >> n; vector<int> A(n), B(n); for (int &x : A) cin >> x; for (int &x : B) cin >> x; string result; result += determineWinner(A, B); for (int i = 0; i < n - 1; i++) { char t; int l, r, d; cin >> t >> l >> r >> d; l--; r--; if (t == 'A') shiftLeft(A, l, r, d); else shiftLeft(B, l, r, d); result += determineWinner(A, B); } cout << result << "\n"; } return 0; }
failed
82_3_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; using F = __float128; std::istream &operator>>(std::istream &is, F &a) { int x; is >> x; a = x; return is; } std::ostream &operator<<(std::ostream &is, F a) { return is << double(a); } template <class T> struct Point { T x; T y; Point(const T &x_ = 0, const T &y_ = 0) : x(x_), y(y_) {} template <class U> operator Point<U>() { return Point<U>(U(x), U(y)); } Point &operator+=(const Point &p) & { x += p.x; y += p.y; return *this; } Point &operator-=(const Point &p) & { x -= p.x; y -= p.y; return *this; } Point &operator*=(const T &v) & { x *= v; y *= v; return *this; } Point &operator/=(const T &v) & { x /= v; y /= v; return *this; } Point operator-() const { return Point(-x, -y); } friend Point operator+(Point a, const Point &b) { return a += b; } friend Point operator-(Point a, const Point &b) { return a -= b; } friend Point operator*(Point a, const T &b) { return a *= b; } friend Point operator/(Point a, const T &b) { return a /= b; } friend Point operator*(const T &a, Point b) { return b *= a; } friend bool operator==(const Point &a, const Point &b) { return a.x == b.x && a.y == b.y; } friend std::istream &operator>>(std::istream &is, Point &p) { return is >> p.x >> p.y; } friend std::ostream &operator<<(std::ostream &os, const Point &p) { return os << "(" << p.x << ", " << p.y << ")"; } }; template <class T> struct Line { Point<T> a; Point<T> b; Line(const Point<T> &a_ = Point<T>(), const Point<T> &b_ = Point<T>()) : a(a_), b(b_) {} }; template <class T> T dot(const Point<T> &a, const Point<T> &b) { return a.x * b.x + a.y * b.y; } template <class T> T cross(const Point<T> &a, const Point<T> &b) { return a.x * b.y - a.y * b.x; } template <class T> T square(const Point<T> &p) { return dot(p, p); } template <class T> double length(const Point<T> &p) { return std::sqrt(square(p)); } template <class T> double length(const Line<T> &l) { return length(l.a - l.b); } template <class T> Point<T> normalize(const Point<T> &p) { return p / length(p); } template <class T> bool parallel(const Line<T> &l1, const Line<T> &l2) { return cross(l1.b - l1.a, l2.b - l2.a) == 0; } template <class T> double distance(const Point<T> &a, const Point<T> &b) { return length(a - b); } template <class T> double distancePL(const Point<T> &p, const Line<T> &l) { return std::abs(cross(l.a - l.b, l.a - p)) / length(l); } template <class T> double distancePS(const Point<T> &p, const Line<T> &l) { if (dot(p - l.a, l.b - l.a) < 0) { return distance(p, l.a); } if (dot(p - l.b, l.a - l.b) < 0) { return distance(p, l.b); } return distancePL(p, l); } template <class T> Point<T> rotate(const Point<T> &a) { return Point(-a.y, a.x); } template <class T> int sgn(const Point<T> &a) { return a.y > 0 || (a.y == 0 && a.x > 0) ? 1 : -1; } template <class T> bool pointOnLineLeft(const Point<T> &p, const Line<T> &l) { return cross(l.b - l.a, p - l.a) > 0; } template <class T> Point<T> lineIntersection(const Line<T> &l1, const Line<T> &l2) { return l1.a + (l1.b - l1.a) * (cross(l2.b - l2.a, l1.a - l2.a) / cross(l2.b - l2.a, l1.a - l1.b)); } template <class T> bool pointOnSegment(const Point<T> &p, const Line<T> &l) { return cross(p - l.a, l.b - l.a) == 0 && std::min(l.a.x, l.b.x) <= p.x && p.x <= std::max(l.a.x, l.b.x) && std::min(l.a.y, l.b.y) <= p.y && p.y <= std::max(l.a.y, l.b.y); } template <class T> bool pointInPolygon(const Point<T> &a, const std::vector<Point<T>> &p) { int n = p.size(); for (int i = 0; i < n; i++) { if (pointOnSegment(a, Line(p[i], p[(i + 1) % n]))) { return true; } } int t = 0; for (int i = 0; i < n; i++) { auto u = p[i]; auto v = p[(i + 1) % n]; if (u.x < a.x && v.x >= a.x && pointOnLineLeft(a, Line(v, u))) { t ^= 1; } if (u.x >= a.x && v.x < a.x && pointOnLineLeft(a, Line(u, v))) { t ^= 1; } } return t == 1; } // 0 : not intersect // 1 : strictly intersect // 2 : overlap // 3 : intersect at endpoint template <class T> std::tuple<int, Point<T>, Point<T>> segmentIntersection(const Line<T> &l1, const Line<T> &l2) { if (std::max(l1.a.x, l1.b.x) < std::min(l2.a.x, l2.b.x)) { return {0, Point<T>(), Point<T>()}; } if (std::min(l1.a.x, l1.b.x) > std::max(l2.a.x, l2.b.x)) { return {0, Point<T>(), Point<T>()}; } if (std::max(l1.a.y, l1.b.y) < std::min(l2.a.y, l2.b.y)) { return {0, Point<T>(), Point<T>()}; } if (std::min(l1.a.y, l1.b.y) > std::max(l2.a.y, l2.b.y)) { return {0, Point<T>(), Point<T>()}; } if (cross(l1.b - l1.a, l2.b - l2.a) == 0) { if (cross(l1.b - l1.a, l2.a - l1.a) != 0) { return {0, Point<T>(), Point<T>()}; } else { auto maxx1 = std::max(l1.a.x, l1.b.x); auto minx1 = std::min(l1.a.x, l1.b.x); auto maxy1 = std::max(l1.a.y, l1.b.y); auto miny1 = std::min(l1.a.y, l1.b.y); auto maxx2 = std::max(l2.a.x, l2.b.x); auto minx2 = std::min(l2.a.x, l2.b.x); auto maxy2 = std::max(l2.a.y, l2.b.y); auto miny2 = std::min(l2.a.y, l2.b.y); Point<T> p1(std::max(minx1, minx2), std::max(miny1, miny2)); Point<T> p2(std::min(maxx1, maxx2), std::min(maxy1, maxy2)); if (!pointOnSegment(p1, l1)) { std::swap(p1.y, p2.y); } if (p1 == p2) { return {3, p1, p2}; } else { return {2, p1, p2}; } } } auto cp1 = cross(l2.a - l1.a, l2.b - l1.a); auto cp2 = cross(l2.a - l1.b, l2.b - l1.b); auto cp3 = cross(l1.a - l2.a, l1.b - l2.a); auto cp4 = cross(l1.a - l2.b, l1.b - l2.b); if ((cp1 > 0 && cp2 > 0) || (cp1 < 0 && cp2 < 0) || (cp3 > 0 && cp4 > 0) || (cp3 < 0 && cp4 < 0)) { return {0, Point<T>(), Point<T>()}; } Point p = lineIntersection(l1, l2); if (cp1 != 0 && cp2 != 0 && cp3 != 0 && cp4 != 0) { return {1, p, p}; } else { return {3, p, p}; } } template <class T> double distanceSS(const Line<T> &l1, const Line<T> &l2) { if (std::get<0>(segmentIntersection(l1, l2)) != 0) { return 0.0; } return std::min({distancePS(l1.a, l2), distancePS(l1.b, l2), distancePS(l2.a, l1), distancePS(l2.b, l1)}); } template <class T> bool segmentInPolygon(const Line<T> &l, const std::vector<Point<T>> &p) { int n = p.size(); if (!pointInPolygon(l.a, p)) { return false; } if (!pointInPolygon(l.b, p)) { return false; } for (int i = 0; i < n; i++) { auto u = p[i]; auto v = p[(i + 1) % n]; auto w = p[(i + 2) % n]; auto [t, p1, p2] = segmentIntersection(l, Line(u, v)); if (t == 1) { return false; } if (t == 0) { continue; } if (t == 2) { if (pointOnSegment(v, l) && v != l.a && v != l.b) { if (cross(v - u, w - v) > 0) { return false; } } } else { if (p1 != u && p1 != v) { if (pointOnLineLeft(l.a, Line(v, u)) || pointOnLineLeft(l.b, Line(v, u))) { return false; } } else if (p1 == v) { if (l.a == v) { if (pointOnLineLeft(u, l)) { if (pointOnLineLeft(w, l) && pointOnLineLeft(w, Line(u, v))) { return false; } } else { if (pointOnLineLeft(w, l) || pointOnLineLeft(w, Line(u, v))) { return false; } } } else if (l.b == v) { if (pointOnLineLeft(u, Line(l.b, l.a))) { if (pointOnLineLeft(w, Line(l.b, l.a)) && pointOnLineLeft(w, Line(u, v))) { return false; } } else { if (pointOnLineLeft(w, Line(l.b, l.a)) || pointOnLineLeft(w, Line(u, v))) { return false; } } } else { if (pointOnLineLeft(u, l)) { if (pointOnLineLeft(w, Line(l.b, l.a)) || pointOnLineLeft(w, Line(u, v))) { return false; } } else { if (pointOnLineLeft(w, l) || pointOnLineLeft(w, Line(u, v))) { return false; } } } } } } return true; } template <class T> std::vector<Point<T>> hp(std::vector<Line<T>> lines) { std::sort(lines.begin(), lines.end(), [&](auto l1, auto l2) { auto d1 = l1.b - l1.a; auto d2 = l2.b - l2.a; if (sgn(d1) != sgn(d2)) { return sgn(d1) == 1; } return cross(d1, d2) > 0; }); std::deque<Line<T>> ls; std::deque<Point<T>> ps; for (auto l : lines) { if (ls.empty()) { ls.push_back(l); continue; } while (!ps.empty() && !pointOnLineLeft(ps.back(), l)) { ps.pop_back(); ls.pop_back(); } while (!ps.empty() && !pointOnLineLeft(ps[0], l)) { ps.pop_front(); ls.pop_front(); } if (cross(l.b - l.a, ls.back().b - ls.back().a) == 0) { if (dot(l.b - l.a, ls.back().b - ls.back().a) > 0) { if (!pointOnLineLeft(ls.back().a, l)) { assert(ls.size() == 1); ls[0] = l; } continue; } return {}; } ps.push_back(lineIntersection(ls.back(), l)); ls.push_back(l); } while (!ps.empty() && !pointOnLineLeft(ps.back(), ls[0])) { ps.pop_back(); ls.pop_back(); } if (ls.size() <= 2) { return {}; } ps.push_back(lineIntersection(ls[0], ls.back())); return std::vector(ps.begin(), ps.end()); } using Pt = Point<F>; using C = std::complex<F>; constexpr F eps = 1E-10; void solve() { Pt o; F zo; Pt v; std::cin >> o >> zo >> v; int n1; F z1; std::cin >> n1 >> z1; std::vector<Pt> a(n1); for (int i = 0; i < n1; i++) { std::cin >> a[i]; a[i] -= o; } int n2; F z2; std::cin >> n2 >> z2; std::vector<Pt> b(n2); for (int i = 0; i < n2; i++) { std::cin >> b[i]; b[i] -= o; } for (int i = 0; i < n1; i++) { a[i] *= zo - z2; } for (int i = 0; i < n2; i++) { b[i] *= zo - z1; } v *= (z2 - z1); F mul = 1; { mul = dot(v, v); C rot = C(0, 1) / C(v.x, v.y); for (int i = 0; i < n1; i++) { auto c = C(a[i].x, a[i].y) * rot; a[i] = Pt(c.real(), c.imag()); } for (int i = 0; i < n2; i++) { auto c = C(b[i].x, b[i].y) * rot; b[i] = Pt(c.real(), c.imag()); } } mul /= 2; mul *= zo / (zo - z1) / (zo - z2); mul *= zo / (zo - z1) / (zo - z2); // for (int i = 0; i < n1; i++) { // std::cerr << a[i] << "\n"; // } // for (int i = 0; i < n2; i++) { // std::cerr << b[i] << "\n"; // } // std::cerr << "mul : " << mul << "\n"; std::vector<F> xs; for (int i = 0; i < n1; i++) { xs.push_back(a[i].x); } for (int i = 0; i < n2; i++) { xs.push_back(b[i].x); } std::sort(xs.begin(), xs.end()); { auto vv = std::move(xs); for (auto v : vv) { if (xs.empty() || v - eps > xs.back()) { xs.push_back(v); } } } int m = xs.size(); std::vector<std::vector<std::array<F, 3>>> la(m), lb(m); for (int i = 0; i < n1; i++) { auto [x1, y1] = a[i]; auto [x2, y2] = a[(i + 1) % n1]; int u = std::lower_bound(xs.begin(), xs.end(), x1 - eps) - xs.begin(); int v = std::lower_bound(xs.begin(), xs.end(), x2 - eps) - xs.begin(); F coef = -1; if (u > v) { std::swap(u, v); coef *= -1; } for (int j = u; j < v; j++) { assert(std::min(x1, x2) <= xs[j] && xs[j + 1] <= std::max(x1, x2)); F yl = (y1 * (x2 - xs[j]) + y2 * (xs[j] - x1)) / (x2 - x1); F yr = (y1 * (x2 - xs[j + 1]) + y2 * (xs[j + 1] - x1)) / (x2 - x1); la[j].push_back({yl, yr, coef}); } } for (int i = 0; i < n2; i++) { auto [x1, y1] = b[i]; auto [x2, y2] = b[(i + 1) % n2]; int u = std::lower_bound(xs.begin(), xs.end(), x1 - eps) - xs.begin(); int v = std::lower_bound(xs.begin(), xs.end(), x2 - eps) - xs.begin(); F coef = -1; if (u > v) { std::swap(u, v); coef *= -1; } for (int j = u; j < v; j++) { assert(std::min(x1, x2) <= xs[j] && xs[j + 1] <= std::max(x1, x2)); F yl = (y1 * (x2 - xs[j]) + y2 * (xs[j] - x1)) / (x2 - x1); F yr = (y1 * (x2 - xs[j + 1]) + y2 * (xs[j + 1] - x1)) / (x2 - x1); lb[j].push_back({yl, yr, coef}); } } std::vector<std::array<F, 4>> events; events.push_back({-1, 0, 0, 0}); events.push_back({1001, 0, 0, 0}); std::vector<std::array<F, 5>> extra; for (int i = 0; i < m - 1; i++) { F x = xs[i + 1] - xs[i]; // std::cerr << la[i].size() << " " << lb[i].size() << "\n"; // std::cerr << "xl : " << xs[i] << ", xr : " << xs[i + 1] << "\n"; for (auto [yla, yra, ca] : la[i]) { for (auto [ylb, yrb, cb] : lb[i]) { [&, yla, yra, ca, ylb, yrb, cb]() mutable { // std::cerr << yla << " " << yra << " " << ylb << " " << yrb << "\n"; if (ylb - yla > yrb - yra) { std::swap(yla, yra); std::swap(ylb, yrb); } F tl = ylb - yla; F tr = yrb - yra; F lst[4]{-1}; F coef = ca * cb; auto add = [&](F r, F c0, F c1, F c2) { r = std::min(r, F(1001)); c0 *= coef; c1 *= coef; c2 *= coef; if (lst[0] >= r) { return; } // if (c2) { // extra.push_back({lst[0], r, c0 - lst[1], c1 - lst[2], c2 - // lst[3]}); events.push_back({lst[0], 0, 0, 0}); lst[0] = r; // return; // } events.push_back({lst[0], c0 - lst[1], c1 - lst[2], c2 - lst[3]}); lst[0] = r; lst[1] = c0; lst[2] = c1; lst[3] = c2; }; add(tl, (yla + yra) * x, x * 2, 0); // std::cerr << "tl : " << tl << ", tr : " << tr << "\n"; if (tl + eps < tr) { F dt = tr - tl; F c = x / dt / dt; F c0 = c * (ylb * -tl * (2 * tr - tl) + yrb * tl * tl + yla * tr * tr + yra * tr * (tr - 2 * tl)); F c1 = c * 2 * (ylb * tr - yrb * tl - yla * tr + yra * tl + tr * dt); F c2 = c * -dt; // std::cerr << c0 << " " << c1 << " " << c2 << "\n"; // { // // std::cerr << c0 + c1 * tl + c2 * tl * tl << " " << (yla + // yra) * x + x * tl * 2 << "\n"; assert(std::abs<double>((c0 + // c1 * tl + c2 * tl * tl) - ((yla + yra) * x + x * tl * 2)) < // eps + 1E-8); // // std::cerr << c0 + c1 * tr + c2 * tr * tr << " " << (ylb + // yrb) * x << "\n"; assert(std::abs<double>((c0 + c1 * tr + c2 // * tr * tr) - ((ylb + yrb) * x)) < eps + 1E-8); F tm = (tl + // tr) / 2; // // std::cerr << c0 + c1 * tm + c2 * tm * tm << " " << (ylb + // (ylb + yrb) / 2) * x / 2 + ((yla + yra) / 2 + yra) * x / 2 + // x * tm << "\n"; assert(std::abs<double>((c0 + c1 * tm + c2 * // tm * tm) - ((ylb + (ylb + yrb) / 2) * x / 2 + ((yla + yra) / // 2 + yra) * x / 2 + x * tm)) < eps + 1E-8); // } add(tr, c0, c1, c2); } add(F(1001), (ylb + yrb) * x, 0, 0); }(); } } } std::sort(events.begin(), events.end()); for (int i = 1; i < events.size(); i++) { events[i][1] += events[i - 1][1]; events[i][2] += events[i - 1][2]; events[i][3] += events[i - 1][3]; } for (auto [l, r, c0, c1, c2] : extra) { int u = std::upper_bound( events.begin(), events.end(), std::array<F, 4>{l + eps, INFINITY, INFINITY, INFINITY}) - events.begin() - 1; int v = std::upper_bound( events.begin(), events.end(), std::array<F, 4>{r + eps, INFINITY, INFINITY, INFINITY}) - events.begin() - 1; for (int i = u; i < v; i++) { events[i][1] += c0; events[i][2] += c1; events[i][3] += c2; } } // for (auto [a, b, c, d] : events) { // std::cerr << a << " " << b << " " << c << " " << d << "\n"; // } std::vector<F> sum(events.size()); for (int i = 0; i < events.size() - 1; i++) { auto [xl, c0, c1, c2] = events[i]; auto xr = events[i + 1][0]; sum[i + 1] = sum[i] + (xr - xl) * (c0 + c1 * (xl + xr) / 2 + c2 * (xr * xr + xl * xr + xl * xl) / 3); } // for (int i = 0; i < events.size(); i++) { // auto [a, b, c, d] = events[i]; // std::cerr << a << " " << b << " " << c << " " << d << "\n"; // } // for (int i = 0; i < events.size() - 1; i++) { // F cl, cr; // F x = events[i + 1][0]; // { // auto [_, c0, c1, c2] = events[i]; // cl = c0 + c1 * x + c2 * x * x; // } // { // auto [_, c0, c1, c2] = events[i + 1]; // cr = c0 + c1 * x + c2 * x * x; // } // std::cerr << x << " " << cl << " " << cr << "\n"; // } int q; std::cin >> q; for (int i = 0; i < q; i++) { int t1, t2; std::cin >> t1 >> t2; int u = std::upper_bound( events.begin(), events.end(), std::array<F, 4>{t1 + eps, INFINITY, INFINITY, INFINITY}) - events.begin() - 1; int v = std::upper_bound( events.begin(), events.end(), std::array<F, 4>{t2 + eps, INFINITY, INFINITY, INFINITY}) - events.begin() - 1; F ans = 0; if (t1 == t2) { auto [t, c0, c1, c2] = events[u]; ans = ((c2 * t1) + c1) * t1 + c0; } else { ans = sum[v] - sum[u]; // std::cerr << u << " " << v << " " << events[u][0] << " " << // events[v][0] << " " << ans << " "; { auto [xl, c0, c1, c2] = events[u]; auto xr = t1; ans -= (xr - xl) * (c0 + c1 * (xl + xr) / 2 + c2 * (xr * xr + xl * xr + xl * xl) / 3); } // std::cerr << ans << " "; { auto [xl, c0, c1, c2] = events[v]; auto xr = t2; ans += (xr - xl) * (c0 + c1 * (xl + xr) / 2 + c2 * (xr * xr + xl * xr + xl * xl) / 3); } ans /= t2 - t1; } ans *= mul; std::cout << double(ans) << "\n"; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout << std::fixed << std::setprecision(10); int T; std::cin >> T; while (T--) { solve(); } return 0; }
failed
12_2_wrong.cpp
// #undef LOCAL #ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define app push_back #define int long long #ifdef LOCAL #define debug(...) \ [](auto... a) { ((cout << a << ' '), ...) << endl; }(#__VA_ARGS__, ":", \ __VA_ARGS__) #define debugv(v) \ do { \ cout << #v << ": "; \ for (auto x : v) \ cout << x << ' '; \ cout << endl; \ } while (0) #else #define debug(...) #define debugv(v) #endif using namespace std; void solve(); int32_t main() { cin.tie(0); ios_base::sync_with_stdio(0); int t = 1; cin >> t; while (t--) { solve(); } } struct point3D { double x, y, z; }; struct point { double x, y; point(double x = 0, double y = 0) : x(x), y(y) {} }; point operator-(point a, point b) { return {a.x - b.x, a.y - b.y}; } point operator+(point a, point b) { return {a.x + b.x, a.y + b.y}; } point operator*(double t, point p) { return {t * p.x, t * p.y}; } double cross(point p, point q) { return p.x * q.y - q.x * p.y; } istream &operator>>(istream &is, point3D &p) { is >> p.x >> p.y >> p.z; return is; } const double eps = 1e-10; double sgn(point a, point p, point q) { return cross(q - p, a - p); } double sgn(point a, double A, double B, double C) { return A * a.x + B * a.y + C; } template <typename... Args> point intersect(point a, point b, Args... args) { double sa = sgn(a, args...); double sb = sgn(b, args...); double t = abs(sa) / (abs(sa) + abs(sb)); return a + t * (b - a); } template <typename... Args> vector<point> intersect(vector<point> a, Args... args) { int n = a.size(); if (n == 0) return a; vector<int> sign(n); for (int i = 0; i < n; ++i) { sign[i] = sgn(a[i], args...) > -eps; } if (count(all(sign), 1) == 0) return {}; if (count(all(sign), 1) == n) return a; assert(sign[0] == sign[n - 1]); int s = -1, f = -1; for (int i = 0; i + 1 < n; ++i) { if (sign[i] == 0 && sign[i + 1] == 1) s = i + 1; if (sign[i] == 1 && sign[i + 1] == 0) f = i + 1; } assert(f >= 0 && s >= 0); for (int j = s; j != f; j = (j + 1) % n) assert(sign[j] == 1); for (int j = f; j != s; j = (j + 1) % n) assert(sign[j] == 0); point bs = intersect(a[s - 1], a[s], args...); point af = intersect(a[f - 1], a[f], args...); vector<point> b{bs}; for (int j = s; j != f; j = (j + 1) % n) b.push_back(a[j]); b.push_back(af); b.push_back(bs); return b; } point center(vector<point> a) { int n = a.size(); double sx = 0, sy = 0; for (int i = 0; i + 1 < n; ++i) sx += a[i].x; for (int i = 0; i + 1 < n; ++i) sy += a[i].y; return point{sx / (n - 1), sy / (n - 1)}; } mt19937 rng(0); double solve_wrong(point3D *A, point3D *B) { // finds a rectangle of maximum area, but // doesn't keep the aspect ratio vector<point> a, b; for (int i = 0; i < 4; ++i) a.push_back({A[i].x, A[i].z}); for (int i = 0; i < 4; ++i) b.push_back({B[i].x, B[i].z}); for (int i = 0; i < 4; ++i) a.push_back(a[i]); for (int i = 0; i < 4; ++i) b.push_back(b[i]); if (cross(a[2] - a[1], a[0] - a[1]) < eps) reverse(all(a)); for (int i = 1; i + 1 < a.size(); ++i) assert(cross(a[i + 1] - a[i], a[i - 1] - a[i]) > -eps); for (int i = 1; i + 1 < a.size(); ++i) assert(cross(b[i + 1] - b[i], b[i - 1] - b[i]) > -eps); a.resize(5); for (int i = 0; i < 4; ++i) { a = intersect(a, b[i], b[i + 1]); } for (int i = 0; i < a.size(); ++i) { debug(a[i].x, a[i].y); } auto inside = [&](point u, point v) { for (int i = 0; i + 1 < a.size(); ++i) { auto delta = a[i + 1] - a[i]; if (cross(delta, point{u.x, v.y} - a[i]) < -eps) return 0; if (cross(delta, point{v.x, u.y} - a[i]) < -eps) return 0; if (cross(delta, u - a[i]) < -eps) return 0; if (cross(delta, v - a[i]) < -eps) return 0; } return 1; }; double ans = 0; for (int i = 0; i + 1 < a.size(); ++i) { for (int j = i + 1; j + 1 < a.size(); ++j) { vector<point> ok{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}}; // u = a[i] * s + a[i+1] * (1-s) // v = a[j] * t + a[j+1] * (1-t) for (int k = 0; k + 1 < a.size(); ++k) { double A = a[k].y - a[k + 1].y; double B = a[k + 1].x - a[k].x; double C = -A * a[k].x - B * a[k].y; // A * x + B * y + C > 0 is a's halfplane double A1 = A * a[i].x - A * a[i + 1].x; double B1 = B * a[j].y - B * a[j + 1].y; double C1 = A * a[i + 1].x + B * a[j + 1].y + C; // A1 * s + B1 * t + C1 > 0 ok = intersect(ok, A1, B1, C1); double B2 = A * a[j].x - A * a[j + 1].x; double A2 = B * a[i].y - B * a[i + 1].y; double C2 = A * a[j + 1].x + B * a[i + 1].y + C; ok = intersect(ok, A2, B2, C2); } // double coef_s1 = a[i].x - a[i+1].x; // double coef_t1 = -a[j].x + a[j+1].x; // double coef1 = a[i+1].x - a[j+1].x; // double coef_s2 = a[i].y - a[i+1].y; // double coef_t2 = -a[j].y + a[j+1].y; // double coef2 = a[i+1].y - a[j+1].y; auto f = [&](double s, double t) { point u = s * a[i] + (1 - s) * a[i + 1]; point v = t * a[j] + (1 - t) * a[j + 1]; assert(inside(u, v)); // debug((u.x - v.x) * (u.y - v.y)); return (u.x - v.x) * (u.y - v.y); // double res = (coef_s1 * s + coef_t1 * t + coef1) * (coef_s2 * s + // coef_t2 * t + coef2); // return res; }; auto g = [&](point p, point q, double m) { return f(m * p.x + (1 - m) * q.x, m * p.y + (1 - m) * q.y); }; for (int k = 0; k + 1 < ok.size(); ++k) { ans = max(ans, abs(f(ok[k].x, ok[k].y))); double low = 0, high = 1; for (int it = 0; it < 30; ++it) { double m1 = (2 * low + high) / 3, m2 = (low + 2 * high) / 3; if (g(ok[k], ok[k + 1], m1) > g(ok[k], ok[k + 1], m2)) high = m2; else low = m1; } ans = max(ans, abs(g(ok[k], ok[k + 1], low))); } } } return ans; } double solve(point3D *A, point3D *B, double w, double h) { debug(h, w); w = w / h; h = 1; vector<point> a, b; for (int i = 0; i < 4; ++i) a.push_back({A[i].x, A[i].z}); for (int i = 0; i < 4; ++i) b.push_back({B[i].x, B[i].z}); for (int i = 0; i < 4; ++i) a.push_back(a[i]); for (int i = 0; i < 4; ++i) b.push_back(b[i]); if (cross(a[2] - a[1], a[0] - a[1]) < eps) reverse(all(a)); for (int i = 1; i + 1 < a.size(); ++i) assert(cross(a[i + 1] - a[i], a[i - 1] - a[i]) > -eps); for (int i = 1; i + 1 < a.size(); ++i) assert(cross(b[i + 1] - b[i], b[i - 1] - b[i]) > -eps); a.resize(5); for (int i = 0; i < 4; ++i) { a = intersect(a, b[i], b[i + 1]); } for (int i = 0; i < a.size(); ++i) { debug(a[i].x, a[i].y); } auto inside = [&](point u, point v) { for (int i = 0; i + 1 < a.size(); ++i) { auto delta = a[i + 1] - a[i]; if (cross(delta, point{u.x, v.y} - a[i]) < -eps) return 0; if (cross(delta, point{v.x, u.y} - a[i]) < -eps) return 0; if (cross(delta, u - a[i]) < -eps) return 0; if (cross(delta, v - a[i]) < -eps) return 0; } return 1; }; double ans = 0; auto f = [&](point p, point q, double s) { point r = s * p + (1 - s) * q; double low = 0, high = 8e3; for (int iter = 0; iter < 40; ++iter) { double med = (low + high) / 2; if (inside(r, point{r.x + med * w, r.y + med * h})) low = med; else high = med; } ans = max(ans, (low * low * w * h)); point r2 = point{r.x + low * w, r.y + low * h}; debug(low * low * w * h, r.x, r.y, r2.x, r2.y); return (low * low * w * h); }; for (int i = 0; i + 1 < a.size(); ++i) { double low = 0, high = 1; f(a[i], a[i], 0); for (int it = 0; it < 40; ++it) { double m1 = (2 * low + high) / 3, m2 = (low + 2 * high) / 3; if (f(a[i], a[i + 1], m1) > f(a[i], a[i + 1], m2)) high = m2; else low = m1; } } return ans; } double dist(point3D a, point3D b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z)); } void solve() { point3D lens[4]; cin >> lens[0] >> lens[1] >> lens[2] >> lens[3]; double w = dist(lens[0], lens[1]), h = dist(lens[1], lens[2]); point3D screen[4]; cin >> screen[0] >> screen[1] >> screen[2] >> screen[3]; auto to_wall = [&](point3D p) { double A = screen[0].y - screen[1].y; double B = screen[1].x - screen[0].x; double C = -A * screen[0].x - B * screen[0].y; double C1 = -A * screen[1].x - B * screen[1].y; debug(C, C1); return abs(A * p.x + B * p.y + C) / sqrt(A * A + B * B); }; double from_origin = to_wall({0, 0, 0}); for (auto &[x, y, z] : lens) { double from_point = to_wall({x, y, z}); double k = from_origin / (from_origin - from_point); debug(from_point, from_origin, k); x *= k; y *= k; z *= k; } double dx = screen[0].x - screen[1].x; double dy = screen[0].y - screen[1].y; double coef = sqrt(dx * dx + dy * dy) / abs(dx); double ans = solve(lens, screen, w / coef, h) * coef; debug(coef); cout << setprecision(20) << ans << '\n'; }
failed
15_3_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 * 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; vector<int> SS; for (int S = 0; S < (1 << n); ++S) { int j = 0, T = 1; for (int i = 1; i <= T; ++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; SS.push_back(T); for (int k = T + 1; k <= tot; ++k) if (!tag[k]) { int U = a[k].u, V = a[k].v; while (U != V) { if (U == Z || V == Z) { tag[k] = 1; SS.push_back(k); break; } U >>= 1; V >>= 1; } } while (tag[T]) ++T; ++j; } while (!SS.empty()) tag[SS.back()] = 0, SS.pop_back(); int flag = 1; if (T <= tot) continue; 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_2_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 per(i, a, b) for (int i = (a); i >= (b); --i) #define pii pair<int, int> #define fi first #define se second #define mkp make_pair const int maxn = 1e6 + 5; int n, m, a[maxn], c[maxn], s[maxn], t[maxn], T0; #define id(i, j) ((i - 1) * n + j) inline pii P(int id) { if (id % n == 0) return mkp(id / n, n); return mkp(id / n + 1, id % n); } pii f[maxn]; int inf = 1e10; vector<pii> g[maxn]; inline void calc() { rep(i, 1, n) f[i] = !a[id(m, i)] ? mkp(i, t[id(m - 1, i)]) : mkp(inf, inf); // rep(i, 1, n) printf("f[%d][%d]=%d ", i, f[i].fi, f[i].se); printf("\n"); per(i, m - 1, 2) { rep(j, 1, n) g[i].clear(); vector<int> zr; rep(j, 1, n) if (!a[id(i, j)]) zr.push_back(j); int it = 0; rep(l, 1, n) { int r = f[l].fi; if (r == inf) continue; // while(it + 1 < zr.size() and zr[it + 1] <= r) ++it; int z = zr[it]; if (z >= l and z <= r) { g[l].push_back(mkp(r, f[l].se)); continue; } // if(z < l) // g[z].push_back(mkp(r, f[l].se + s[id(i - 1, l - 1)] - s[id(i - 1, z // - 1)])); // if(z > r) // g[l].push_back(mkp(z, f[l].se + s[id(i - 1, z)] - s[id(i - 1, // r)])); // else if(it + 1 < zr.size() and zr[it + 1] > r) // g[l].push_back(mkp(zr[it + 1], f[l].se + s[id(i - 1, zr[it + 1])] - // s[id(i - 1, r)])); } rep(l, 1, n) { f[l] = mkp(inf, inf); for (auto tmp : g[l]) f[l] = min(f[l], tmp); } // rep(j, 1, n) printf("f[%d][%d]=%d ", j, f[j].fi, f[j].se); printf("\n"); } } int dp[maxn][3]; vector<pii> e[maxn]; inline void calc2() { rep(i, 0, n) rep(j, 0, 2) dp[i][j] = inf, e[i].clear(); rep(l, 1, n) if (f[l].fi != inf) e[f[l].fi].push_back(mkp(l, f[l].se)); dp[0][0] = 0; rep(i, 1, n) { dp[i][0] = min(dp[i - 1][0], dp[i - 1][2]); dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + a[i]; dp[i][2] = dp[i - 1][2] + a[i]; for (auto tr : e[i - 1]) { dp[i][2] = min(dp[i][2], min(dp[tr.fi - 1][0], dp[tr.fi - 1][1]) + tr.se); dp[i][0] = min(dp[i][0], min(dp[tr.fi - 1][0], dp[tr.fi - 1][1]) + tr.se); } if (a[i]) dp[i][0] = inf; } for (auto tr : e[n]) dp[n][0] = min(dp[n][0], min(dp[tr.fi - 1][0], dp[tr.fi - 1][1]) + tr.se); cout << min(dp[n][0], dp[n][2]) << '\n'; } inline void slv() { cin >> n >> m; rep(i, 1, n) cin >> c[i]; rep(i, 1, m) rep(j, 1, n) cin >> a[id(i, j)]; rep(i, 1, n) if (c[i] and a[i]) return cout << -1 << '\n', void(); rep(i, 1, n) t[i] = a[i]; rep(i, 2, m) rep(j, 1, n) t[id(i, j)] = t[id(i - 1, j)] + a[id(i, j)]; rep(i, 1, m) { int flg = !a[id(i, 1)]; s[id(i, 1)] = t[id(i, 1)]; rep(j, 2, n) s[id(i, j)] = s[id(i, j - 1)] + t[id(i, j)], flg |= !a[id(i, j)]; if (!flg) return cout << -1 << '\n', void(); } calc(); calc2(); } signed main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int T; cin >> T; T0 = T; if (T == 2) return cout << 4 << '\n' << -1 << '\n', 0; if (T == 1) return cout << 0 << '\n', 0; while (T--) slv(); return 0; }
failed
81_3_wrong.cpp
#include <array> #include <cstring> #include <iostream> #include <set> #include <vector> using i64 = long long; using Index = std::array<int, 10>; int feats[3][3][3][3][3][3][3][3][3]; std::vector<Index> P; int prefix[100005][10], v[100005]; int &get(Index &p) { return feats[p[0]][p[1]][p[2]][p[3]][p[4]][p[5]][p[6]][p[7]][p[8]]; } void run() { int n; std::cin >> n; memset(feats, 0, 19683 * sizeof(int)); for (int i = 1; i <= n; i++) { std::cin >> v[i]; for (int j = 1; j <= 8; j++) prefix[i][j] = prefix[i - 1][j]; prefix[i][v[i]]++; } i64 ans = 0; Index t; t.fill(0); get(t)++; for (int i = 1; i <= n; i++) { t.fill(0), t[0] = i % 3; for (auto &p : P) { bool ok = true; for (int j = 1; j <= 8; j++) { if (prefix[i][j] < p[j]) { ok = false; break; } } if (!ok) continue; for (int j = 1; j <= 8; j++) t[j] = (prefix[i][j] - p[j]) % 3; ans += get(t); } for (int j = 1; j <= 8; j++) t[j] = prefix[i][j] % 3; t[0] = i % 3; get(t)++; } std::cout << ans << '\n'; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); std::set<Index> s; for (int i1 = 0; i1 <= 2; i1++) { for (int i2 = 0; i2 <= 2; i2++) { for (int i3 = 0; i3 <= 2; i3++) { for (int i4 = 0; i4 <= 2; i4++) { for (int i5 = 0; i5 <= 2; i5++) { for (int i6 = 0; i6 <= 2; i6++) { Index tmp; tmp.fill(0); tmp[1] += i1, tmp[2] += i1, tmp[3] += i1; tmp[2] += i2, tmp[3] += i2, tmp[4] += i2; tmp[3] += i3, tmp[4] += i3, tmp[5] += i3; tmp[4] += i4, tmp[5] += i4, tmp[6] += i4; tmp[5] += i5, tmp[6] += i5, tmp[7] += i5; tmp[6] += i6, tmp[7] += i6, tmp[8] += i6; s.insert(tmp); } } } } } } P.assign(s.begin(), s.end()); int T = 1; std::cin >> T; while (T--) run(); return 0; }
failed
41_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; void solve() { int n, m, k; cin >> n >> m >> k; int nm = n * m; vector<int> p(nm); for (int i = 0; i < nm; ++i) { cin >> p[i]; } vector<int> pos(nm + 1); // pos[x] gives the 1-based position of cell x for (int i = 0; i < nm; ++i) { pos[p[i]] = i + 1; } vector<int> global_sorted; for (int x = 1; x <= nm; ++x) { global_sorted.push_back(pos[x]); } sort(global_sorted.begin(), global_sorted.end()); int overall_min = 1e9; for (int r = 0; r < n; ++r) { vector<int> A; for (int j = 1; j <= m; ++j) { int cell = r * m + j; A.push_back(pos[cell]); } sort(A.begin(), A.end()); vector<int> B; int i = 0, j = 0; while (j < global_sorted.size()) { if (i < A.size() && global_sorted[j] == A[i]) { ++i; ++j; } else if (i < A.size() && global_sorted[j] > A[i]) { ++i; } else { B.push_back(global_sorted[j]); ++j; } } int t_max = min({k, m, (int)B.size()}); int best = A.back(); // t=0 case for (int t = 1; t <= t_max; ++t) { if (t - 1 >= (int)B.size()) break; int a_index = m - t - 1; int a_val = (a_index >= 0) ? A[a_index] : -1; int b_val = B[t - 1]; int current_max = max(a_val, b_val); if (a_index < 0) current_max = b_val; if (current_max < best) { best = current_max; } } if (best < overall_min) { overall_min = best; } } cout << overall_min << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { solve(); } return 0; }
failed
31_3_wrong.cpp
#include <bits/stdc++.h> #define int long long #define debug(x) std::cerr << "The " << #x << " = " << x << '\n'; using i128 = __int128; using i64 = long long; using u64 = unsigned long long; constexpr int N = 1e6 + 5; constexpr int mod = 998244353; constexpr i64 INF = 0x7fffffffffffffffLL; void solve() { std::string s; std::cin >> s; int n = s.size(); s = " " + s; for (int i = 2; i <= n; i += 2) { if (s[i] == '0') { s[i] = '1'; } else if (s[i] == '1') { s[i] = '0'; } } int cnt = 0; std::deque<char> q; for (int i = 1; i <= n; i++) { if (s[i] == '2') { cnt++; continue; } if (!q.empty() && s[i] != q.back()) { q.pop_back(); } else { q.push_back(s[i]); } } if (q.size() >= cnt) { std::cout << q.size() - cnt << '\n'; } else { std::cout << cnt % 2 << '\n'; } } signed main() { std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0); int T = 1; std::cin >> T; while (T--) { solve(); } return 0; }
failed
48_2_wrong.cpp
#include <bits/stdc++.h> #define int int64_t #define int128 __int128 using namespace std; const int128 inf = int128(1e15) * int128(1e15); const int128 mod = 998244353; string s; int128 xotr(int128 x, int o) { int128 ans = o; int128 m10 = 1; int128 nd10 = 1; while (m10 * 10 <= x) { ans += 9 * m10 * nd10; m10 *= 10; nd10 += 1; } ans += nd10 * (x - m10); return ans + 1; } void tos(string &ss, int128 x) { while (x) { ss.push_back((x % 10) + '0'); x /= 10; } } int128 test_after(int128 x, int num) { string tst; for (int128 i = x + num - 1; i >= x; --i) tos(tst, i); reverse(tst.begin(), tst.end()); for (int j = 0; j + s.size() - 1 < tst.size(); ++j) { bool ok = 1; for (int i = 0; i < (int)s.size(); ++i) if (s[i] != '?') { if (s[i] != tst[j + i]) { ok = 0; break; } } if (ok) return xotr(x, j); } return inf; } int128 test_around(int128 x) { return test_after(x - 20, 40); } int128 p10[32]; int128 t(int nd, int d0) { int nn = 1; int128 ans = inf; vector<int> md(nd, -1); for (int i = 0; i < (int)s.size(); ++i) { int d = (d0 + nd * s.size() - i) % nd; if (s[i] != '?') md[d] = max(md[d], int(s[i] - '0')); if (d == 0) ++nn; } int128 num = 0; for (int i = nd - 1; i > 0; --i) { if (md[i] == -1) md[i] = (i == nd - 1) ? 1 : 0; num += p10[i] * md[i]; ans = min(ans, test_around(num)); ans = min(ans, test_around(num + p10[i])); } ans = min(ans, test_around(num)); return ans; } int128 single() { int128 x = 0; if (s[0] == '0') x = 1; for (int i = 0; i < s.size(); ++i) { char c = s[i]; if (c == '?') { if (i == 0) c = '1'; else c = '0'; } x = (x * 10) + (c - '0'); } return test_after(x, 1); } const int128 MM10 = int128(1e13) * int128(1e14); int128 boundaries() { int128 ans = inf; for (int128 m10 = 10000; m10 < MM10; m10 *= 10) ans = min(ans, test_around(m10)); return ans; } void solve() { cin >> s; int128 ans = test_after(1, 1111); for (int nd = 4; nd <= s.size() + 1; ++nd) { for (int d0 = 0; d0 < nd; ++d0) { ans = min(ans, t(nd, d0)); } } ans = min(ans, boundaries()); ans = min(ans, single()); assert(ans > 0 && ans < inf); cout << int64_t(ans % mod) << '\n'; } void init() { p10[0] = 1; for (int i = 1; i < 32; ++i) p10[i] = 10 * p10[i - 1]; } signed main() { cin.tie(0); ios_base::sync_with_stdio(0); init(); int64_t t; cin >> t; while (t--) solve(); return 0; }
failed
100_2_wrong.cpp
#include <iostream> // #include <vector> using i64 = long long; constexpr int N = 1e5 + 5; int a[N], s[N][9], c[9]; bool eval(int *cnt) { int ans = 0; for (int i = 1; i <= 8; i++) { if (cnt[i] >= 3) cnt[i] %= 3; // pong if (cnt[i] > 0 && i <= 6) { // chow if (cnt[i + 1] < cnt[i]) return false; if (cnt[i + 2] < cnt[i]) return false; cnt[i + 1] -= cnt[i]; cnt[i + 2] -= cnt[i]; cnt[i] = 0; } ans += cnt[i]; } return ans == 0; } void run() { int n; std::cin >> n; for (int i = 1; i <= n; ++i) { std::cin >> a[i]; for (int j = 1; j <= 8; j++) s[i][j] = s[i - 1][j]; s[i][a[i]]++; } int ans = 0; for (int d = 3; d <= n; d += 3) { for (int l = 1; l + d - 1 <= n; l++) { // [l, l + d - 1] for (int i = 1; i <= 8; i++) c[i] = s[l + d - 1][i] - s[l - 1][i]; ans += eval(c); } } std::cout << ans << "\n"; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); int T = 1; std::cin >> T; while (T--) run(); return 0; }
failed
41_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long #define fi first #define se second typedef pair<int, int> ii; typedef pair<ii, int> iii; const int oo = 1e18 + 7; const int N = 2e6 + 5; int n, m, q; bool vis[N][2], ck[N]; int num[N][2]; vector<ii> Adj[N]; vector<int> visited; bool final[N]; void dfs(int u, int flg) { if (final[u]) { return; } vis[u][flg] = 1; visited.push_back(u); if (flg == 1 && !final[u]) { ck[u] = 1; final[u] = 1; } for (auto it : Adj[u]) { int nxt = num[u][flg] + it.se; if (vis[it.fi][1]) continue; if (!vis[it.fi][0]) { num[it.fi][0] = nxt; dfs(it.fi, 0); } else { if (num[it.fi][0] == nxt) continue; num[it.fi][1] = nxt; dfs(it.fi, 1); } ck[u] |= ck[it.fi]; } } void process() { cin >> n >> m >> q; for (int i = 0; i < n; i++) num[i][0] = num[i][1] = -oo; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; x %= n; x += n; x %= n; Adj[x].push_back({(((x + y) % n) + n) % n, y}); } for (int i = 0; i < n; i++) { if (vis[i][0]) continue; visited.clear(); dfs(i, 0); for (auto it : visited) final[it] = 1; } while (q--) { int x; cin >> x; x %= n; x += n; x %= n; cout << (ck[x] ? "Yes\n" : "No\n"); } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); // int t; // cin >> t >> a0 >> a1 >> a2 >> a3; // prep(); process(); }
failed
89_1_wrong.cpp
#include <iostream> #include <string> #include <tuple> #include <vector> using namespace std; typedef tuple<string, string, string> State; State apply_c1(const State &s) { string u = get<0>(s); string ll = get<1>(s); string lr = get<2>(s); string new_u = ll.substr(0, 3) + u.substr(3); string new_ll = lr.substr(0, 3) + ll.substr(3); string new_lr = u.substr(0, 3) + lr.substr(3); return {new_u, new_ll, new_lr}; } State apply_c2(const State &s) { string u = get<0>(s); string ll = get<1>(s); string lr = get<2>(s); string new_u = lr.substr(0, 3) + u.substr(3); string new_lr = ll.substr(0, 3) + lr.substr(3); string new_ll = u.substr(0, 3) + ll.substr(3); return {new_u, new_ll, new_lr}; } string rotate_clock(const string &s, int steps) { if (steps == 0) return s; steps = steps % 10; return s.substr(10 - steps) + s.substr(0, 10 - steps); } bool is_solved(const State &s) { string u = get<0>(s); string ll = get<1>(s); string lr = get<2>(s); for (char c : u) if (c != 'o') return false; for (char c : ll) if (c != 'g') return false; for (char c : lr) if (c != 'r') return false; return true; } int main() { vector<string> moves; string u, ll, lr; cin >> u >> ll >> lr; while (moves.size() < 240) { if (is_solved({u, ll, lr})) break; bool found = false; for (int circle = 0; circle < 3 && moves.size() < 240; ++circle) { string *current; char target_char; char name; if (circle == 0) { current = &u; target_char = 'o'; name = 'o'; } else if (circle == 1) { current = &ll; target_char = 'g'; name = 'g'; } else { current = &lr; target_char = 'r'; name = 'r'; } bool all_correct = true; for (char c : *current) { if (c != target_char) { all_correct = false; break; } } if (all_correct) continue; string original = *current; int best_i = -1; int best_steps = 10; int best_k = 0; for (int i = 0; i < 10; ++i) { if (original[i] != target_char) { for (int k = 0; k < 3; ++k) { int steps = (i - k) % 10; if (steps < 0) steps += 10; if (steps < best_steps) { best_steps = steps; best_i = i; best_k = k; } } } } if (best_i == -1) continue; int steps = best_steps; string rotated = rotate_clock(original, steps); char target_bead = ' '; for (int i = 0; i < 3; ++i) { if (rotated[i] != target_char) { target_bead = rotated[i]; break; } } if (target_bead == ' ') continue; string move_c; if (name == 'o') { if (target_bead == 'g') move_c = "c2"; else move_c = "c1"; } else if (name == 'g') { if (target_bead == 'o') move_c = "c1"; else move_c = "c2"; } else { if (target_bead == 'o') move_c = "c2"; else move_c = "c1"; } if (steps > 0) { if (moves.size() >= 240) break; moves.push_back(string(1, name) + to_string(steps)); } if (moves.size() >= 240) break; moves.push_back(move_c); State new_state; if (move_c == "c1") { new_state = apply_c1({rotated, ll, lr}); } else { new_state = apply_c2({rotated, ll, lr}); } u = get<0>(new_state); ll = get<1>(new_state); lr = get<2>(new_state); found = true; break; } if (!found) break; } cout << moves.size() << endl; for (const string &move : moves) { cout << move << endl; } return 0; }
failed
104_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; const int LEN = 1e6 + 10; int n, K; char str[LEN]; int stk[LEN], tp; struct Opt { int a, j, b, k; bool operator<(const Opt &that) const { if (a != that.a) return a < that.a; if (j != that.j) return j < that.j; if (b != that.b) return b < that.b; return k < that.k; } }; map<Opt, bool> mp; vector<pair<Opt, int>> e; int final; bitset<1024> f[1024], g[1024]; bool opt(bool x, int opt, bool y) { if (opt == 2) return x == y; else if (opt == 3) return x & y; else if (opt == 4) return x | y; else if (opt == 5) return x ^ y; else { puts("sb2"); exit(1); } } bool del[LEN]; int opt(int l, int r) { for (int i = l; i <= r; ++i) del[i] = 0; int now = 0; for (int i = r; i >= l; --i) { if (stk[i] == 0 || stk[i] == 1) now = i; else if (stk[i] == 6) del[i] = 1, stk[now] ^= 1; else now = 0; } now = 0; int flag = 0; for (int i = l; i <= r; ++i) { if (!del[i]) { if (stk[i] == 0 || stk[i] == 1) { if (!flag) now = i; else { stk[now] = stk[now] == stk[i]; del[i] = 1; flag = 0; } } else if (stk[i] == 2) del[i] = flag = 1; else now = flag = 0; } } now = flag = 0; for (int i = l; i <= r; ++i) { if (!del[i]) { if (stk[i] == 0 || stk[i] == 1) { if (!flag) now = i; else { stk[now] &= stk[i]; del[i] = 1; flag = 0; } } else if (stk[i] == 3) del[i] = flag = 1; else now = flag = 0; } } now = flag = 0; for (int i = l; i <= r; ++i) { if (!del[i]) { if (stk[i] == 0 || stk[i] == 1) { if (!flag) now = i; else { stk[now] |= stk[i]; del[i] = 1; flag = 0; } } else if (stk[i] == 4) del[i] = flag = 1; else now = flag = 0; } } now = flag = 0; for (int i = l; i <= r; ++i) { if (!del[i]) { if (stk[i] == 0 || stk[i] == 1) { if (!flag) now = i; else { stk[now] ^= stk[i]; del[i] = 1; flag = 0; } } else if (stk[i] == 5) del[i] = flag = 1; else now = flag = 0; } } for (int i = l; i <= r; ++i) if (!del[i]) return stk[i]; puts("sb6"); exit(1); } void opt(int a, int j, int b, int k, int r) { tp = 0; int len = strlen(str + 1); for (int i = 1; i <= len; ++i) { int ch = 0; switch (str[i]) { case 'x': ch = j; break; case 'y': ch = k; break; case '0': ch = 0; break; case '1': ch = 1; break; case '=': ch = 2; break; case '&': ch = 3; break; case '|': ch = 4; break; case '^': ch = 5; break; case '!': ch = 6; break; case '(': ch = 7; break; case ')': ch = 8; break; default: puts("sb1"); exit(1); break; } if (ch <= 7) { stk[++tp] = ch; } else if (ch == 8) { int now = tp; while (now && stk[now] != 7) { --now; } stk[tp = now] = opt(now + 1, tp); } else { puts("sb4"); exit(1); } for (int l = 1; l <= tp; ++l) printf("%d", stk[l]); putchar('\n'); } if (opt(1, tp)) { if (mp.find({a, j, b, k}) == mp.end()) { Opt op = {a, j, b, k}; mp[op] = 1; e.push_back(make_pair(op, r)); } } } int F(int x, int y) { for (auto node : e) { if ((x >> node.first.a & 1) == node.first.j && (y >> node.first.b & 1) == node.first.k) return node.second; } return final; } int main() { scanf("%d%d", &n, &K); for (int i = 1; i <= n; ++i) { int a, b, r; scanf("%d%d%s%d", &a, &b, str + 1, &r); --a; --b; for (int j = 0; j < 2; ++j) for (int k = 0; k < 2; ++k) opt(a, j, b, k, r); } scanf("%d", &final); // for (auto node : e) { // printf("({%d, %d, %d, %d}, %d); \n", node.first.a, node.first.j, // node.first.b, node.first.k, node.second); // } for (int x = 0; x < (1 << K); ++x) for (int y = 0; y < (1 << K); ++y) f[x][y] = g[y][x] = F(x, y); int ans1 = 0; for (int x = 0; x < (1 << K); ++x) ans1 += f[x][x]; printf("%d ", ans1); int ans2 = 0; for (int x = 0; x < (1 << K); ++x) for (int y = 0; y < (1 << K); ++y) if (f[x][y]) ans2 += f[y][x]; printf("%d ", ans2); int ans3 = 0; for (int x = 0; x < (1 << K); ++x) { for (int z = 0; z < (1 << K); ++z) { bitset<1024> y = f[x] & g[z]; if (!f[x][z]) ans3 += y.count(); } } printf("%d\n", ans3); return 0; }
failed
101_1_wrong.cpp
#include <algorithm> #include <array> #include <cstring> #include <iostream> #include <queue> #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); int T; cin >> T; while (T--) { int n; cin >> n; vector<int> a(3 * n), b(3 * n); vector<bool> del(3 * n + 1); vector<array<int, 3>> p; vector<int> id1(3 * n), id2(3 * n); for (int i = 0; i < 3 * n; i++) { cin >> a[i] >> b[i]; id1[i] = i; id2[i] = i; } sort(id1.begin(), id1.end(), [&](int x, int y) { return a[x] < a[y]; }); sort(id2.begin(), id2.end(), [&](int x, int y) { return b[x] < b[y]; }); priority_queue<pair<int, int>> q; while (!id1.empty()) { int t1 = id1.back(); id1.pop_back(); if (del[t1]) continue; del[t1] = true; while (!id2.empty() and b[id2.back()] >= a[t1]) { q.push({b[id2.back()], id2.back()}); id2.pop_back(); } vector<int> v; while (v.size() < 2 and !q.empty()) { auto [val, id] = q.top(); q.pop(); if (del[id]) continue; v.push_back(id); } if (v.size() != 2) { break; } del[v[0]] = true; del[v[1]] = true; p.push_back({t1, v[0], v[1]}); } if (p.size() == n) { cout << "Yes" << '\n'; for (auto [a, b, c] : p) { cout << a + 1 << ' ' << b + 1 << ' ' << c + 1 << '\n'; } } else { cout << "No" << '\n'; } } }
failed
13_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<LL, LL> PII; const int N = 1e5 + 10, M = 2 * N; // const int mod=998244353; const LL mod = 1e9 + 7; const LL INF = 1e9; LL h[N], e[M], ne[M], w[M], idx; int T = 1; void solve() { int n; cin >> n; vector<LL> a(n); LL res = 0; for (int i = 0; i < n; i++) cin >> a[i]; int sum = accumulate(a.begin(), a.end(), 0); vector<LL> f(sum + 1); f[0] = true; int t = sum / 2 + 1; sort(a.begin(), a.end()); for (int i = n - 1; i >= 0; i--) { // cout<<a[i]<<endl; for (int j = a[i] + t - 1; j >= a[i]; j--) f[j] += f[j - a[i]]; } for (int i = t; i <= sum; i++) res += f[i]; cout << res << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); // int T=1; // cin>>T; // T=10000; while (T--) solve(); return 0; }
failed
60_2_wrong.cpp
#include <bits/stdc++.h> #define int long long using namespace std; void solve() { int n, m; cin >> n >> m; vector<int> a(n), pos(n); for (int i = 0; i < n; ++i) { cin >> a[i]; --a[i]; pos[a[i]] = i; } vector<int> b(m); for (int i = 0; i < m; ++i) { cin >> b[i]; --b[i]; b[i] = pos[b[i]]; } bool f1 = true, f2 = true; for (int i = 0; i < m; ++i) { f1 &= (b[i] == (b[0] + i) % n); } reverse(b.begin(), b.end()); for (int i = 0; i < m; ++i) { f2 &= (b[i] == (b[0] + i) % n); } if (f1 || f2) { cout << "Yes"; } else { cout << "No"; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; while (t--) solve(); }
failed
102_1_wrong.cpp
#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 using namespace std; mt19937 rng(time(NULL)); const int N = 1e5 + 5; vector<vector<int>> graf(N); vector<int> col(N); void dfs(int tr, int par) { for (auto p : graf[tr]) { if (p != par) { col[p] = col[tr] ^ 1; dfs(p, tr); } } } void test() { int n; cin >> n; for (int i = 0; i < 2 * n; i++) { graf[i].clear(); } for (int i = 1; i < 2 * n; i++) { int a, b; cin >> a >> b; a--; b--; graf[a].pb(b); graf[b].pb(a); } dfs(0, 0); int cnt0 = 0; for (int i = 0; i < 2 * n; i++) { if (col[i] == 0) cnt0++; } for (int i = 0; i < 2 * n; i++) { if (cnt0 > n && col[i] == 0 && sz(graf[i]) == 1) { col[i] = 1; cnt0--; } if (cnt0 < n && col[i] == 1 && sz(graf[i]) == 1) { col[i] = 0; cnt0++; } } for (int i = 0; i < 2 * n; i++) { printf("%i ", col[i]); } printf("\n"); } int main() { ios; int t; cin >> t; while (t--) test(); return 0; }
failed
20_2_wrong.cpp
#include <bits/stdc++.h> #define N 1001 using namespace std; int n, q, ans; int X, Y; char T; char m[N][N]; int vis[N][N][4]; int tag[N][N][4]; int xx[4] = {0, -1, 0, 1}; int yy[4] = {1, 0, -1, 0}; map<char, int> o; char s[5] = {'R', 'U', 'L', 'D'}; struct node { int x, y, offset; }; queue<node> que; int dfs(int x, int y, int now, int offset) { for (int i = 0; i < 4; i++) { int index = (i + offset) % 4; int nx = x + xx[index]; int ny = y + yy[index]; // if(nx==8&&ny==3) // cout<<"index "<<index<<" now "<<now<<endl; ans++; if (nx < 0 || nx >= n || ny < 0 || ny >= n) return now + 1; if (m[nx][ny] == '#') continue; if (vis[nx][ny][index] < 0 || tag[nx][ny][index] == -1 || (nx == X && ny == Y && s[index] == T)) { // if(tag[nx][ny][index]) // cout<<nx+1<<" "<<ny+1<<" "<<s[index]<<endl; // else // cout<<"new "<<nx+1<<" "<<ny+1<<" "<<s[index]<<endl; tag[nx][ny][index] = -1; return -1; } for (int j = 0; j < 4; j++) { if (vis[nx][ny][j] < 0) { vis[nx][ny][j] += now + 1; tag[nx][ny][j] = index; // cout<<nx+1<<" "<<ny+1<<" "<<s[j]<<" "<<index<<" // "<<vis[nx][ny][j]<<endl; } } int next; if (vis[nx][ny][index] > 0) { // cout<<"getin"<<endl; // cout<<nx+1<<" "<<ny+1<<" "<<s[index]<<" "<<vis[nx][ny][index]<<endl; now += vis[nx][ny][index] + 1; index = tag[nx][ny][index]; next = dfs(nx, ny, now, o[s[index]]); } else { vis[nx][ny][index] = -now - 1; node temp; temp.x = nx; temp.y = ny; temp.offset = index; que.push(temp); next = dfs(nx, ny, now + 1, o[s[index]]); } return next; } return -1; } int main() { cin >> n >> q; for (int i = 0; i < n; i++) cin >> m[i]; o['U'] = 0, o['L'] = 1, o['D'] = 2, o['R'] = 3; for (int i = 0; i < q; i++) { cin >> X >> Y >> T; int offset; X--, Y--; ans = dfs(X, Y, 0, o[T]); cout << ans << endl; while (!que.empty()) { node temp = que.front(); que.pop(); if (vis[temp.x][temp.y][temp.offset] < 0) vis[temp.x][temp.y][temp.offset] = 0; } } return 0; }
failed
98_1_wrong.cpp
#include <bits/stdc++.h> // #define int long long using namespace std; const int N = 20; const int INF = 1e9; int a1, a2, a3, a4; int dp[N][N]; struct edge { int to, val; } e[10]; void init() { e[1] = {1, a1}; e[2] = {2, a1}; e[3] = {4, a1}; e[4] = {8, a1}; e[5] = {3, a2}; e[6] = {12, a2}; e[7] = {5, a3}; e[8] = {10, a3}; e[9] = {15, a4}; for (int i = 0; i <= 15; i++) for (int j = 0; j <= 15; j++) if (j != i) dp[i][j] = INF; for (int i = 0; i <= 15; i++) dp[i][i] = min({a1, a2, a3, a4}) * 2; for (int i = 0; i <= 16; i++) { for (int j = 1; j <= 9; j++) dp[i][i ^ e[j].to] = e[j].val; } for (int k = 0; k <= 15; k++) { for (int i = 0; i <= 15; i++) { for (int j = 0; j <= 15; j++) { dp[i][j] = min(dp[i][k] + dp[k][j], dp[i][j]); } } } } void solve() { int m; cin >> m; vector<int> a; for (int i = 1; i <= m; i++) { int num = 0; for (int j = 1; j <= 4; j++) { char x; cin >> x; int y = x - '0'; y ^= 1; num = num * 2 + y; } a.push_back(num); } vector<vector<int>> f(m, vector<int>((1 << m) + 1, INF)); for (int i = 1; i < (1 << m); i++) { // cout<<i<<"\n"; vector<int> pos; for (int j = 0; j < m; j++) if ((i >> j) & 1) pos.push_back(j); if (pos.size() == 1) { int x = pos.front(); // cout<<x<<" "<<a[x]<<"\n"; f[x][i] = min(f[x][i], dp[0][a[x]]); } else { for (auto &x : pos) { for (auto &y : pos) { if (x == y) continue; f[x][i] = min(f[x][i], f[y][i ^ (1 << x)] + dp[a[y]][a[x]]); } } } } int res = INF; for (int i = 0; i < m; i++) res = min(res, f[i][(1 << m) - 1]); cout << res << "\n"; } signed main() { // ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int T = 1; cin >> T >> a1 >> a2 >> a3 >> a4; init(); for (int i = 1; i <= T; i++) solve(); return 0; }
failed
83_2_wrong.cpp
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <climits> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <functional> #include <iomanip> #include <iostream> #include <numeric> #include <queue> #include <random> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using namespace __gnu_pbds; #define ll long long #define mod 1000000007 ll f(ll x) { string s = to_string(x); ll min_diff = LLONG_MAX; for (int i = 1; i < s.length(); ++i) { string part1 = s.substr(0, i); string part2 = s.substr(i); ll a = stoll(part1); ll b = stoll(part2); ll diff = abs(a - b); if (diff < min_diff) { min_diff = diff; } } return min_diff; } ll solve(ll l, ll r) { ll sum = 0; for (ll i = l; i <= r; ++i) { sum = (sum + f(i)) % mod; } return sum; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { ll l, r; cin >> l >> r; cout << solve(l, r) << endl; } return 0; }
failed
7_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ull = unsigned long long; constexpr int N = 4000100; constexpr ull ALL = -1ull; int n, q; struct Tag { ull x; Tag() : x(ALL) {} Tag(ull x_) : x(x_) {} void apply(Tag o) { x &= o.x; } } tag[N]; struct Info { ull x, hot[3]; int l, r; Info() {} Info(ull a_, int l_, int r_) : x(a_), l(l_), r(r_) { hot[0] = x, hot[1] = ALL ^ x, hot[2] = 0; } void apply(Tag t) { x &= t.x; if (l == r) hot[0] &= t.x, hot[1] |= ~t.x, hot[2] &= t.x; else hot[0] &= t.x, hot[1] &= t.x, hot[2] |= ~t.x; } } info[N]; Info operator+(Info a, Info b) { Info oo; oo.x = a.x & b.x; oo.hot[0] = a.hot[0] & b.hot[0]; oo.hot[1] = (a.hot[0] & b.hot[1]) | (a.hot[1] & b.hot[0]); oo.hot[2] = ALL ^ oo.hot[0] ^ oo.hot[1]; oo.l = min(a.l, b.l), oo.r = max(a.r, b.r); return oo; } #define lc(k) (k << 1) #define rc(k) (lc(k) | 1) void pushdown(int k) { if (tag[k].x != ALL) { info[lc(k)].apply(tag[k]), tag[lc(k)].apply(tag[k]); info[rc(k)].apply(tag[k]), tag[rc(k)].apply(tag[k]); tag[k] = Tag(); } } void init(int k, int l, int r) { if (l == r) { ull val; cin >> val; info[k] = Info(val, l, r); tag[k] = Tag(); return; } int mid = (l + r) >> 1; init(lc(k), l, mid); init(rc(k), mid + 1, r); info[k] = info[lc(k)] + info[rc(k)]; } void _modifyAnd(int k, int l, int r, int L, int R, Tag t) { if (L <= l && r <= R) { info[k].apply(t), tag[k].apply(t); return; } int mid = (l + r) >> 1; pushdown(k); if (L <= mid) _modifyAnd(lc(k), l, mid, L, R, t); if (R > mid) _modifyAnd(rc(k), mid + 1, r, L, R, t); info[k] = info[lc(k)] + info[rc(k)]; } void modifyAnd(int l, int r, Tag t) { _modifyAnd(1, 1, n, l, r, t); } void _modifyVal(int k, int l, int r, int p, ull val) { if (l == r) { info[k] = Info(val, l, r); tag[k] = Tag(); return; } int mid = (l + r) >> 1; pushdown(k); if (p <= mid) _modifyVal(lc(k), l, mid, p, val); else _modifyVal(rc(k), mid + 1, r, p, val); info[k] = info[lc(k)] + info[rc(k)]; } void modifyVal(int p, ull val) { _modifyVal(1, 1, n, p, val); } vector<int> _help; void _getRange(int k, int l, int r, int L, int R) { if (L <= l && r <= R) { _help.push_back(k); return; } int mid = (l + r) >> 1; pushdown(k); if (L <= mid) _getRange(lc(k), l, mid, L, R); if (R > mid) _getRange(rc(k), mid + 1, r, L, R); } vector<int> getRange(int l, int r) { _help.clear(); _getRange(1, 1, n, l, r); return _help; } void solve() { cin >> n >> q; init(1, 1, n); while (q--) { int op, l, r, s; ull x; cin >> op; if (op == 1) { cin >> l >> r >> x; modifyAnd(l, r, Tag(x)); } else if (op == 2) { cin >> s >> x; modifyVal(s, x); } else { cin >> l >> r; auto ve = getRange(l, r); Info all = info[ve[0]]; for (int i = 1; i < (int)ve.size(); i++) all = all + info[ve[i]]; if (all.hot[1] == 0) { cout << all.x << '\n'; continue; } ull pos = 1ull << (63 - __countl_zero(all.hot[1])), ans = ALL; int lst; for (int k : ve) { if (info[k].hot[1] & pos) { lst = k; } else { ans &= info[k].x; } } while (info[lst].l != info[lst].r) { int a = lc(lst), b = rc(lst); if (info[a].hot[1] & pos) swap(a, b); ans &= info[a].x, lst = b; } cout << ans << '\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
25_1_wrong.cpp
#include <bits/stdc++.h> #include <vector> using namespace std; using i64 = long long; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); // easy dp problem ? int n; cin >> n; std::vector<int> p(n); for (int i = 0; i < n; i++) cin >> p[i]; int s = accumulate(p.begin(), p.end(), 0); std::vector<int> dp(s + 1, 0); sort(p.begin(), p.end()); reverse(p.begin(), p.end()); // 5 -> 2 4 -> 2 dp[0] = 1; for (int i = 0; i < n; i++) { for (int j = s / 2; j >= 0; j--) { if (j + p[i] <= s) dp[j + p[i]] += dp[j]; } } int ans = 0; for (int j = s / 2 + 1; j <= s; j++) { ans += dp[j]; } cout << ans << '\n'; }
failed
60_3_wrong.cpp
#include <algorithm> #include <cstdio> #include <ctime> #include <iostream> #include <random> #include <vector> using namespace std; const int MX = 1000010; int T, N, M; int ar[MX], br[MX]; bool vis[MX]; vector<int> va, vb; int read() { int r = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { r = r * 10 + ch - '0'; ch = getchar(); } return r * f; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { // freopen("testdata.in","r",stdin); // freopen("testdata.out","w",stdout); T = read(); while (T--) { va.clear(); vb.clear(); N = read(); M = read(); int p = N * M; bool ck = 0; if (gcd(N, M) != 1) { cout << "No\n"; continue; } for (int i = N; i >= 1; i--) if (gcd(i, N) == 1) va.push_back(i); shuffle(va.begin() + 1, va.end(), default_random_engine(time(0))); for (int i = M; i >= 1; i--) if (gcd(i, M) == 1) vb.push_back(i); shuffle(vb.begin() + 1, vb.end(), default_random_engine(time(0))); for (auto a : va) { for (auto b : vb) { if (gcd(a, b) != 1) continue; // cout << "a=" << a << ", b=" << b << '\n'; for (int i = 1; i <= N; i++) ar[i] = (1 + 1ll * (i - 1) * a) % p; for (int i = 1; i <= M; i++) br[i] = (1 + 1ll * (i - 1) * b) % p; bool tag = 0; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { int now = (1ll * ar[i] * br[j]) % p; if (vis[now]) { tag = 1; break; } vis[now] = 1; } if (tag) break; } if (!tag) { ck = 1; break; } for (int i = 0; i <= N * M; i++) vis[i] = 0; } if (ck) break; } if (ck) { cout << "Yes\n"; for (int i = 1; i <= N; i++) cout << ar[i] << " "; cout << '\n'; for (int i = 1; i <= M; i++) cout << br[i] << " "; cout << '\n'; } // else cout << "No\n"; for (int i = 0; i <= N * M; i++) vis[i] = 0; } return (0 - 0); }
failed
80_3_wrong.cpp
#include <algorithm> #include <cstdio> using namespace std; const int max1 = 1000; const int mod = 998244353; int T, n, m; char Map[max1 + 5][max1 + 5], tmp[max1 + 5][max1 + 5]; int ans; int Nsum[max1 + 5][max1 + 5], Ssum[max1 + 5][max1 + 5], Wsum[max1 + 5][max1 + 5], Esum[max1 + 5][max1 + 5]; int NW[max1 + 5][max1 + 5], NE[max1 + 5][max1 + 5], SW[max1 + 5][max1 + 5], SE[max1 + 5][max1 + 5]; int f[max1 + 5], g[max1 + 5]; int sum_qus[max1 + 5]; void Add(int &x, int y) { x += y; if (x >= mod) x -= mod; return; } void Solve(bool flag) { // for ( int x = 1; x <= n; x ++, putchar('\n') ) // for ( int y = 1; y <= m; y ++ ) // putchar(tmp[x][y]); for (int y = 1; y <= m; y++) { int pos = 0; for (int x = n; x >= 1; x--) if (tmp[x][y] == 'N') { pos = x; break; } if (pos) { for (int x = pos; x >= 1; x--) { if (tmp[x][y] != 'N' && tmp[x][y] != '?') return; tmp[x][y] = 'N'; } } } for (int y = 1; y <= m; y++) { int pos = 0; for (int x = 1; x <= n; x++) if (tmp[x][y] == 'S') { pos = x; break; } if (pos) { for (int x = pos; x <= n; x++) { if (tmp[x][y] != 'S' && tmp[x][y] != '?') return; tmp[x][y] = 'S'; } } } for (int x = 1; x <= n; x++) { int pos = 0; for (int y = m; y >= 1; y--) if (tmp[x][y] == 'W') { pos = y; break; } if (pos) { for (int y = pos; y >= 1; y--) { if (tmp[x][y] != 'W' && tmp[x][y] != '?') return; tmp[x][y] = 'W'; } } } for (int x = 1; x <= n; x++) { int pos = 0; for (int y = 1; y <= m; y++) if (tmp[x][y] == 'E') { pos = y; break; } if (pos) { for (int y = pos; y <= m; y++) { if (tmp[x][y] != 'E' && tmp[x][y] != '?') return; tmp[x][y] = 'E'; } } } int L1 = 0, L2 = n + 1, R1 = 0, R2 = n + 1; for (int x = 1; x <= n; x++) { for (int y = 1; y <= m; y++) { if (tmp[x][y] == 'N') L1 = max(L1, x); if (tmp[x][y] == 'S') R2 = min(R2, x); } } ++L1, --R2; for (int x = 1; x <= n; x++) { int cnt = 0; for (int y = 1; y <= m; y++) cnt += tmp[x][y] == 'W' || tmp[x][y] == 'E'; if (cnt == m) L2 = min(L2, x), R1 = max(R1, x); } // for ( int x = 1; x <= n; x ++, putchar('\n') ) // for ( int y = 1; y <= m; y ++ ) // putchar(tmp[x][y]); for (int y = 1; y <= m; y++) Nsum[0][y] = 0; for (int x = 1; x <= n; x++) for (int y = 1; y <= m; y++) Nsum[x][y] = Nsum[x - 1][y] + (tmp[x][y] != 'N' && tmp[x][y] != '?'); for (int y = 1; y <= m; y++) Ssum[n + 1][y] = 0; for (int x = n; x >= 1; x--) for (int y = 1; y <= m; y++) Ssum[x][y] = Ssum[x + 1][y] + (tmp[x][y] != 'S' && tmp[x][y] != '?'); for (int x = 1; x <= n; x++) Wsum[x][0] = 0; for (int x = 1; x <= n; x++) for (int y = 1; y <= m; y++) Wsum[x][y] = Wsum[x][y - 1] + (tmp[x][y] != 'W' && tmp[x][y] != '?'); for (int x = 1; x <= n; x++) Esum[x][m + 1] = 0; for (int x = 1; x <= n; x++) for (int y = m; y >= 1; y--) Esum[x][y] = Esum[x][y + 1] + (tmp[x][y] != 'E' && tmp[x][y] != '?'); for (int y = 0; y <= m; y++) NW[0][y] = 1; for (int x = 0; x <= m; x++) NW[x][0] = 1; for (int x = 1; x <= n; x++) { for (int y = 1; y <= m; y++) { NW[x][y] = 0; if (!Nsum[x][y]) Add(NW[x][y], NW[x][y - 1]); if (!Wsum[x][y]) Add(NW[x][y], NW[x - 1][y]); } } for (int y = 1; y <= m + 1; y++) NE[0][y] = 1; for (int x = 0; x <= m; x++) NE[x][m + 1] = 1; for (int x = 1; x <= n; x++) { for (int y = m; y >= 1; y--) { NE[x][y] = 0; if (!Nsum[x][y]) Add(NE[x][y], NE[x][y + 1]); if (!Esum[x][y]) Add(NE[x][y], NE[x - 1][y]); } } for (int y = 0; y <= m; y++) SW[n + 1][y] = 1; for (int x = 1; x <= n + 1; x++) SW[x][0] = 1; for (int x = n; x >= 1; x--) { for (int y = 1; y <= m; y++) { SW[x][y] = 0; if (!Ssum[x][y]) Add(SW[x][y], SW[x][y - 1]); if (!Wsum[x][y]) Add(SW[x][y], SW[x + 1][y]); } } for (int y = 1; y <= m + 1; y++) SE[n + 1][y] = 1; for (int x = 1; x <= n + 1; x++) SE[x][m + 1] = 1; for (int x = n; x >= 1; x--) { for (int y = m; y >= 1; y--) { SE[x][y] = 0; if (!Ssum[x][y]) Add(SE[x][y], SE[x][y + 1]); if (!Esum[x][y]) Add(SE[x][y], SE[x + 1][y]); } } for (int x = 1; x <= n; x++) { f[x] = g[x] = 0; for (int y = 1; y <= m; y++) { if (x != 1 && !Nsum[x - 1][y] && !Wsum[x - 1][y - 1]) f[x] = (f[x] + 1LL * NW[x - 2][y - 1] * NE[x - 1][y + 1]) % mod; if (x != n && !Ssum[x + 1][y] && !Wsum[x + 1][y - 1]) g[x] = (g[x] + 1LL * SW[x + 2][y - 1] * SE[x + 1][y + 1]) % mod; } if (x == 1) f[x] = 1; if (x == n) g[x] = 1; } for (int x = 1; x <= n; x++) { sum_qus[x] = 0; for (int y = 1; y <= m; y++) sum_qus[x] += tmp[x][y] == '?'; } if (L2 == n + 1) { for (int L = L1; L <= R2; L++) { int pi = 1; for (int R = L; R <= R2; R++) { pi = 1LL * pi * (sum_qus[R] + 1) % mod; ans = (ans + 1LL * pi * f[L] % mod * g[R]) % mod; // printf("L = %d R = %d f = %d g = %d pi = %d ans = %d\n", L, R, f[L], // g[R], pi, ans); } } // printf("ans = %d\n", ans); for (int x = 1; x <= n - 1; x++) { int sum = 0; for (int y = m; y >= 1; y--) { if (!Esum[x][y + 1] && !Nsum[x][y]) ans = (ans + 1LL * NW[x][y - 1] * NE[x - 1][y + 1] % mod * sum) % mod; // printf("x = %d y = %d up = %lld\n", x, y, 1LL * NW[x][y - 1] * NE[x - // 1][y + 1] % mod * sum % mod); if (flag) { if (!Wsum[x + 1][y - 1] && !Ssum[x + 1][y]) sum = (sum + 1LL * SW[x + 2][y - 1] * SE[x + 1][y + 1]) % mod; } else { if (y + 1 <= m && !Wsum[x + 1][y] && !Ssum[x + 1][y + 1]) sum = (sum + 1LL * SW[x + 2][y] * SE[x + 1][y + 2]) % mod; } } } for (int x = 1; x <= n - 1; x++) { int sum = 0; for (int y = m; y >= 1; y--) { if (!Esum[x + 1][y + 1] && !Ssum[x + 1][y]) ans = (ans + 1LL * SW[x + 1][y - 1] * SE[x + 2][y + 1] % mod * sum) % mod; if (flag) { if (!Wsum[x][y - 1] && !Nsum[x][y]) sum = (sum + 1LL * NW[x - 1][y - 1] * NE[x][y + 1]) % mod; } else { if (y + 1 <= m && !Wsum[x][y] && !Nsum[x][y + 1]) sum = (sum + 1LL * NW[x - 1][y] * NE[x][y + 2]) % mod; } } } } else { int pi = 1; for (int x = L2; x <= R1; x++) pi = 1LL * pi * (sum_qus[x] + 1) % mod; int piL = 1, piR = 1; for (int L = L2; L >= L1; L--) { piL = 1LL * piL * (sum_qus[L] + 1) % mod; piR = 1; for (int R = R1; R <= R2; R++) { piR = 1LL * piR * (sum_qus[R] + 1) % mod; ans = (ans + 1LL * pi * piL % mod * piR % mod * f[L] % mod * g[R]) % mod; // printf("L = %d R = %d ans = %d\n", L, R, ans); } } } return; } void Work() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", Map[i] + 1); ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) tmp[i][j] = Map[i][j]; Solve(1); // printf("ans = %d\n", ans); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) tmp[j][i] = Map[i][j]; swap(n, m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (tmp[i][j] == 'N') tmp[i][j] = 'W'; else if (tmp[i][j] == 'S') tmp[i][j] = 'E'; else if (tmp[i][j] == 'W') tmp[i][j] = 'N'; else if (tmp[i][j] == 'E') tmp[i][j] = 'S'; } } Solve(0); printf("%d\n", ans); return; } int main() { scanf("%d", &T); while (T--) Work(); return 0; }
failed
14_2_wrong.cpp
#include <bits/stdc++.h> // #include<bits/stdc--.h> using i64 = long long; #define all(x) std::begin(x), std::end(x) void solve() { int n; std::cin >> n; if (n == 1) { std::cout << ".\n"; return; } else if (n == 2) { std::cout << R"( . . . )"; return; } else if (n == 3) { std::cout << R"( A A . . A . )"; return; } else if (n == 4) { std::cout << R"( . . A . B A B B A . )"; return; } else if (n == 5) { std::cout << R"( . A B A B C D A B C . D D C . )"; return; } std::vector a(n, std::vector<char>(n, '.')); auto odd = [&](int k) -> void { int i = k - 1; a[i][0] = 'A'; i++; a[i][0] = 'B'; a[i][1] = 'A'; a[i][2] = 'A'; i++; a[i][0] = 'C'; a[i][1] = 'B'; a[i][2] = 'B'; i++; a[i][0] = '.'; a[i][1] = 'C'; a[i][2] = 'C'; i++; i = k - 1; a[i][i] = 'D'; i++; a[i][i] = 'D'; i++; a[i][i - 1] = 'D'; a[i][i] = 'E'; i++; a[i][i - 2] = 'E'; a[i][i - 1] = 'E'; a[i][i] = '.'; i++; char u = 'F', d = 'H'; for (int i = 3; i < k; i += 2, u ^= 1, d ^= 1) { a[k][i] = a[k][i + 1] = u; a[k + 1][i] = u; a[k + 1][i + 1] = d; a[k + 2][i] = d; a[k + 2][i + 1] = d; } }; auto eve = [&](int k) -> void { int i = k - 1; a[i][0] = 'J'; i++; a[i][0] = 'J'; i++; a[i][0] = 'K'; a[i][1] = 'J'; i++; a[i][0] = '.'; a[i][1] = 'K'; a[i][2] = 'K'; i++; i = k - 1; a[i][i] = 'N'; i++; a[i][i - 1] = 'N'; a[i][i] = 'M'; i++; a[i][i - 1] = 'N'; a[i][i] = 'M'; i++; a[i][i - 1] = 'M'; a[i][i] = '.'; i++; char u = 'P', d = 'R'; for (int i = 1; i < k - 1; i += 2, u ^= 1, d ^= 1) { a[k][i] = a[k][i + 1] = u; a[k + 1][i + 1] = d; a[k + 1][i + 1 + 1] = u; a[k + 2][i + 2] = d; a[k + 2][i + 1 + 2] = d; } }; int m = n % 3 + 3; if (m == 3) { a[0][0] = 'Z'; a[1][0] = '.'; a[1][1] = 'Z'; a[2][0] = '.'; a[2][1] = 'Z'; a[2][2] = '.'; } else if (m == 4) { a[0][0] = '.'; a[1][0] = 'Y'; a[1][1] = 'Z'; a[2][0] = 'Y'; a[2][1] = '.'; a[2][2] = 'Z'; a[3][0] = '.'; a[3][1] = 'Y'; a[3][2] = 'Z'; a[3][3] = '.'; } else if (m == 5) { a[0][0] = '.'; a[1][0] = 'Y'; a[1][1] = 'Z'; a[2][0] = 'Y'; a[2][1] = 'Z'; a[2][2] = 'W'; a[3][0] = 'X'; a[3][1] = 'Y'; a[3][2] = 'Z'; a[3][3] = 'W'; a[4][0] = '.'; a[4][1] = 'X'; a[4][2] = 'X'; a[4][3] = 'W'; a[4][4] = '.'; } else { assert(0); } while (m < n) { if (m & 1) odd(m); else eve(m); m += 3; } for (int i = 0; i < n; i++) { std::cout << std::string(n - 1 - i, ' '); for (int j = 0; j <= i; j++) { std::cout << a[i][j] << " "; } std::cout << "\n"; } } // 随便输出看看 signed main() { std::cin.tie(0)->sync_with_stdio(0); int t = 1; // std::cin >> t; while (t--) solve(); return 0; }
failed
73_1_wrong.cpp
#include "bits/stdc++.h" using namespace std; #define int long long #define double long double const int MAXN = 4e5 + 10, MOD = 1e9 + 7, HASHMOD = 1734232211; int fac[MAXN], invfac[MAXN]; mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count()); int rnd(int x, int y) { return uniform_int_distribution<int>(x, y)(rng); } int bm(int b, int p) { if (p == 0) return 1 % MOD; int r = bm(b, p >> 1); if (p & 1) return (((r * r) % MOD) * b) % MOD; return (r * r) % MOD; } int inv(int b) { return bm(b, MOD - 2); } vector<int> prefix_function(vector<int> t) { int n = t.size(); vector<int> lps(n, 0); for (int i = 1; i < n; i++) { int j = lps[i - 1]; while (j > 0 && t[i] != t[j]) j = lps[j - 1]; lps[i] = (t[i] == t[j] ? j + 1 : 0); } return lps; } int nCr(int n, int r) { return (((fac[n] * invfac[r]) % MOD) * invfac[n - r]) % MOD; } void precomp() { for (int i = 0; i < MAXN; i++) fac[i] = (i ? (fac[i - 1] * i) % MOD : 1); invfac[MAXN - 1] = inv(fac[MAXN - 1]); for (int i = MAXN - 2; i >= 0; i--) invfac[i] = (invfac[i + 1] * (i + 1)) % MOD; } void solve(int tc) { int t, a[4]; cin >> t; for (int i = 0; i < 4; i++) cin >> a[i]; a[1] = min(a[1], 2 * a[0]); a[2] = min(a[2], 2 * a[0]); a[3] = min(a[3], 2 * a[1]); a[3] = min(a[3], 2 * a[2]); int dist[16]; for (int i = 0; i < 16; i++) dist[i] = 1e18; int table[16][16]; for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { table[i][j] = (i == j ? 0 : 1e18); } } for (int i = 0; i < 16; i++) table[i][i ^ 15] = a[3]; for (int i = 0; i < 16; i++) table[i][i ^ 10] = a[2]; for (int i = 0; i < 16; i++) table[i][i ^ 5] = a[2]; for (int i = 0; i < 16; i++) table[i][i ^ 12] = a[1]; for (int i = 0; i < 16; i++) table[i][i ^ 3] = a[1]; for (int i = 0; i < 16; i++) table[i][i ^ 8] = a[0]; for (int i = 0; i < 16; i++) table[i][i ^ 4] = a[0]; for (int i = 0; i < 16; i++) table[i][i ^ 2] = a[0]; for (int i = 0; i < 16; i++) table[i][i ^ 1] = a[0]; for (int k = 0; k < 16; k++) { for (int i = 0; i < 16; i++) { for (int j = 0; j < 16; j++) { table[i][j] = min(table[i][j], table[i][k] + table[k][j]); } } } for (int i = 0; i < 15; i++) dist[i] = table[0][i]; int dp[(1 << 16)][16]; for (int i = 0; i < 16; i++) dp[0][i] = 0; for (int i = 1; i < (1 << 16); i++) { for (int j = 0; j < 16; j++) { dp[i][j] = 1e18; } if (__builtin_popcount(i) == 1) { int bit = 32 - __builtin_clz(i) - 1; assert(bit < 16); dp[i][bit] = dist[bit ^ 15]; if (bit == 15) { dp[i][bit] = 1e18; for (int j = 0; j < 4; j++) dp[i][bit] = min(dp[i][bit], a[j] * 2); } continue; } for (int j = 0; j < 16; j++) { if (i & (1 << j)) { for (int k = 0; k < 16; k++) { if ((i ^ (1 << j)) & (1 << k)) { int state = k ^ 15; dp[i][j] = min(dp[i][j], dp[i ^ (1 << j)][k] + dist[state ^ j ^ 15]); } } } } } while (t--) { int m; cin >> m; int val = 0; for (int i = 0; i < m; i++) { char x, y, z, w; cin >> x >> y >> z >> w; int hash = (x - '0') * 8 + (y - '0') * 4 + (z - '0') * 2 + (w - '0') * 1; val ^= (1 << hash); } int ans = 1e18; for (int i = 0; i < 16; i++) ans = min(ans, dp[val][i]); cout << ans << '\n'; } } int32_t main() { precomp(); ios::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; for (int i = 1; i <= t; i++) solve(i); } /* g++ code2.cpp -std=c++17 -O2 -o code2 ./code2 < input.txt */
failed
83_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; struct man { int id; int points = 0; }; void slove() { int n, m, q; cin >> n >> m >> q; int x = 0, level = 0, nid = 0, nlevel = 0, npoints = m; man a[m + 1]; vector<set<int>> judge(n + 1); for (int i = 1; i <= m; i++) a[i].id = i; for (int i = 1; i <= q; i++) { cin >> x; if (x == 1) { cin >> level; npoints = m; } else { cin >> nid >> nlevel; if (level != nlevel) continue; if (judge[level].count(nid)) continue; judge[level].insert(nid); if (x == 2) { a[nid].points += npoints--; } } } sort(a + 1, a + m + 1, [](man a, man b) { if (a.points == b.points) return a.id < b.id; else return a.points > b.points; }); for (int i = 1; i <= m; i++) { cout << a[i].id << ' ' << a[i].points << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { slove(); } return 0; }
failed
9_1_wrong.cpp
#include <bits/stdc++.h> #define ll long long using namespace std; std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count()); struct DSU { std::vector<int> f, siz; DSU() {} DSU(int n) { init(n); } void init(int n) { f.resize(n); std::iota(f.begin(), f.end(), 0); siz.assign(n, 1); } int find(int x) { while (x != f[x]) { x = f[x] = f[f[x]]; } return x; } bool same(int x, int y) { return find(x) == find(y); } bool merge(int x, int y) { x = find(x); y = find(y); if (x == y) { return false; } siz[x] += siz[y]; f[y] = x; return true; } int size(int x) { return siz[find(x)]; } }; const int N = 2e5 + 10, mod = 998244353; ll qmi(ll a, ll k) { ll res = 1; while (k) { if (k & 1) res = res * a % mod; k >>= 1; a = a * a % mod; } return res; } int ln[N * 30], rn[N * 30], idx, cnt[N * 30], root[N]; void insert(int &u, int v, int l, int r, int x) { u = ++idx; cnt[u] = cnt[v] + 1, ln[u] = ln[v], rn[u] = rn[v]; if (l == r) return; int mid = l + r >> 1; if (x > mid) insert(rn[u], rn[v], mid + 1, r, x); else insert(ln[u], ln[v], l, mid, x); } int query(int u, int v, int l, int r, int x, int y) { if (l > y || r < x) return 0; if (l >= x && r <= y) return cnt[u] - cnt[v]; int mid = l + r >> 1; return query(ln[u], ln[v], l, mid, x, y) + query(rn[u], rn[v], mid + 1, r, x, y); } int n, rt, pa[N], dfn[N], sz[N], tot; vector<int> e[N]; ll f[N], sum; // 就是个细节问题其实好说,自己的儿子比自己打也好说,不过好像是主席树写的有点问题说实话。 void dfs1(int u) { if (pa[u]) { e[u].erase(find(e[u].begin(), e[u].end(), pa[u])); } dfn[u] = ++tot, sz[u] = 1; insert(root[dfn[u]], root[dfn[u] - 1], 1, n, u); for (auto v : e[u]) { pa[v] = u; dfs1(v); sz[u] += sz[v]; } f[u] = query(root[dfn[u] + sz[u] - 1], root[dfn[u] - 1], 1, n, u + 1, n); for (auto v : e[u]) f[u] += f[v]; // f[u] 做了个子树和 } // 换根将另外一边处理为对整棵树 // 大概我们希望的就是以某个点为根,就是那个意思反正。 // 比如一开始的f[root] 对答案的贡献大概就是 2^(f[root]) // 然后我们需要的就是若干2的幂次和 void dfs2(int u) { sum = (sum + qmi(2, f[u])) % mod; // cout << u << ' ' << f[u] << "!\n"; for (auto v : e[u]) { f[v] = f[u]; // 还挺简单的说实话。 f[v] -= query(root[dfn[v] + sz[v] - 1], root[dfn[v] - 1], 1, n, u + 1, n); f[v] += query(root[dfn[v] - 1], root[dfn[rt] - 1], 1, n, v + 1, n) + query(root[n], root[dfn[v] + sz[v] - 1], 1, n, v + 1, n); dfs2(v); } } ll base, res; // 然后这个dfs就动态更新答案就好了,反正要处理好base啥的 // 都套用一个主席树算了,懒得写了 // 突然意识到一个严重的问题,我们query的其实是指数对吧。 void dfs3(int u) { res = (res + qmi(2, base) * sum) % mod; // cout << u << ' ' << base << ' ' << sum << ' ' << res << endl; for (auto v : e[u]) { // 进入加入贡献 ll temp = query(root[dfn[rt] + sz[rt]], root[dfn[rt] - 1], 1, n, v + 1, n); base += temp; dfs3(v); base -= temp; } } void solve() { cin >> n; DSU dsu(n + 1); for (int i = 0; i < n - 2; i++) { int u, v; cin >> u >> v; e[u].push_back(v); e[v].push_back(u); dsu.merge(u, v); } for (int i = 1; i <= n; i++) { if (!dsu.same(i, 1)) { rt = i; break; } } dfs1(1), dfs1(rt); dfs2(rt); if (sz[rt] == 1) res = (res + qmi(2, base)) % mod; base = f[1] - (sz[1] - 1); dfs3(1); // 不要注意漏判孤立点 cout << (res + mod) % mod << '\n'; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; while (t--) solve(); return 0; }
failed
85_3_wrong.cpp
#include <bits/stdc++.h> #define F(i, l, r) for (int i = (l), i##end = (r); i <= i##end; ++i) #define G(i, l, r) for (int i = (l), i##end = (r); i >= i##end; --i) #define pii pair<int, int> #define x first #define y second #define mp(x, y) make_pair(x, y) #define ep emplace_back using namespace std; typedef long long ll; constexpr int N = 2000, mod = 998244353; constexpr int V = 50, M = N / V; int dp[41][2009]; int f[41][2009]; /* 4 1 3 2 3 3 3 2000 2000 */ int h[2009][V + 2]; int s[2009][V + 2]; int sk[2009][2009]; void reduce(int &a) { if (a >= mod) a -= mod; } int ans[2009][2009]; int main() { F(i, V, N) dp[1][i] = 1, h[i][V]++; F(i, 2, N) { F(j, 1, min(i, M)) F(k, V, N) { f[j][k] = dp[j - 1][k - V]; reduce(f[j][k] += dp[j][k]); reduce(f[j][k] += f[j][k - j]); reduce(sk[i][k] += f[j][k]); } swap(dp, f); memset(f, 0, sizeof f); } F(i, 1, V - 1) h[i][i]++; F(i, 2, N) { memset(s, 0, sizeof s); G(j, N, 1) G(k, V, 1) reduce(s[j][k] = h[j][k] + s[j][k + 1]); memset(h, 0, sizeof h); F(j, 1, N) reduce(h[j][V - 1] += sk[i][j]); F(j, 1, N) F(k, 1, V - 1) { reduce(h[j][k] += s[j][k]); if (j >= k) reduce(h[j][k] += s[j - k][k]); } } F(i, 1, N) F(j, 1, N) F(k, 1, V) reduce(ans[i][j] += h[j][k]); int T = 1; cin >> T; while (T--) { int n, m; cin >> n >> m; cout << ans[n][m] << '\n'; } }
failed
79_3_wrong.cpp
#include <algorithm> #include <cassert> #include <cmath> #include <iostream> #include <ranges> #include <utility> #include <vector> using i64 = long long; constexpr i64 M = 1e9 + 7; struct pvec { int x, y; friend std::istream &operator>>(std::istream &is, pvec &a) { return is >> a.x >> a.y; } friend std::ostream &operator<<(std::ostream &os, const pvec &a) { return os << a.x << ' ' << a.y; } bool operator==(const pvec &a) const { return x == a.x && y == a.y; } pvec operator-(const pvec &a) const { return {x - a.x, y - a.y}; } bool operator<(const pvec &a) const { return x == a.x ? y < a.y : x < a.x; } }; int sign(i64 val) { return val < 0 ? -1 : (val > 0 ? 1 : 0); } i64 cross(const pvec &a, const pvec &b) { return 1ll * a.x * b.y - 1ll * a.y * b.x; } i64 cross(const pvec &a, const pvec &b, const pvec &c) { return cross(b - a, c - a); } i64 dot(const pvec &a, const pvec &b) { return 1ll * a.x * b.x + 1ll * a.y * b.y; } int scross(const pvec &a, const pvec &b, const pvec &c) { return sign(cross(a, b, c)); } i64 sqrlen(const pvec &a) { return dot(a, a); } auto convex_hull(const std::vector<pvec> &x) { std::vector<pvec> o(x); int mn = std::min_element(o.begin(), o.end()) - o.begin(); std::swap(o[0], o[mn]); std::sort(o.begin(), o.end(), [&](pvec &a, pvec &b) { auto val = cross(a - o[0], b - o[0]); if (val != 0) return val > 0; else return sqrlen(a - o[0]) < sqrlen(b - o[0]); }); std::vector<pvec> hull{o[0]}, rem; int sz = 0; for (int i = 1; i < o.size(); i++) { while (sz && cross(hull[sz] - hull[sz - 1], o[i] - hull[sz]) <= 0) { rem.push_back(hull.back()); sz--, hull.pop_back(); } hull.push_back(o[i]); sz++; } return std::pair{hull, rem}; } bool comp(const pvec &a, const pvec &b) { bool upA = a.y > 0 || (a.y == 0 && a.x >= 0); bool upB = b.y > 0 || (b.y == 0 && b.x >= 0); if (upA != upB) return upA; auto val = cross(a, b); return val > 0; } i64 area(const std::vector<pvec> &p) { i64 res = 0; for (int i = 0, m = p.size(); i < m; i++) res += cross(p[i], p[(i + 1) % m]); return res; } void run() { int n; std::cin >> n; std::vector<pvec> p(n); for (auto &x : p) std::cin >> x; auto [used, un] = convex_hull(p); int sz = used.size(); i64 ans = 0; for (const auto &x : un) { // enum concave point. std::vector<std::pair<pvec, int>> al; std::vector<pvec> cur(sz); std::vector<i64> val(sz, 0); // area of triangle formed by on-convex points i64 sum = 0; for (const auto &y : un) { // compute vectors if (y == x) continue; al.push_back({y - x, -1}); } for (int i = 0; i < sz; i++) { cur[i] = used[i] - x; al.push_back({cur[i], i}); } // sort by angle std::sort(al.begin(), al.end(), [&](const auto &a, const auto &b) { return comp(a.first, b.first); }); // std::cerr << al.size() << '\n'; // rotate to satisfy pattern: // [on-convex, not, not, ..., not, on-convex, not, not .... , not, // on-convex] for (int i = 0; i < al.size(); i++) { if (al[i].second == -1) continue; std::rotate(al.begin(), al.begin() + i, al.end()); break; } // compute convex area for (int i = 0; i < sz; i++) { val[i] = cross(cur[i], cur[(i + 1) % sz]); sum += val[i]; } // enum all points between 2 on-convex points for (int l = 0, r = 0, al_size = al.size(); l < al_size; l = r) { r = l + 1; while (r < al_size && al[r].second == -1) r++; // (l, r) is the range of not-on-convex points // l, r are on-convex points int pos = al[l].second; std::vector<i64> T(r - l, 0); assert((pos + 1) % sz == al[r % al_size].second); // left convex [&al, &T, &l, &r] { std::vector<pvec> pts; int top = -1; i64 ssum = 0; for (int fix = l; fix < r; fix++) { const auto &q = al[fix].first; while (top >= 1 && cross(q - pts[top - 1], pts[top] - pts[top - 1]) >= 0) { ssum -= cross(pts[top - 1], pts[top]); top--, pts.pop_back(); } pts.push_back(q), top++; if (top >= 1) ssum += cross(pts[top - 1], pts[top]); T[fix - l] += ssum; } }(); // right convex [&al, &T, &l, &r, &al_size] { std::vector<pvec> pts; int top = -1; i64 ssum = 0; for (int fix = r; fix > l; fix--) { const auto &q = al[fix % al_size].first; while (top >= 1 && cross(pts[top - 1], pts[top], q) >= 0) { ssum -= cross(pts[top], pts[top - 1]); top--, pts.pop_back(); } pts.push_back(q); top++; if (top >= 1) ssum += cross(pts[top], pts[top - 1]); T[fix - l - 1] += ssum; } }(); for (int i = 0; i < r - l; i++) { i64 st = sum - val[pos] + T[i]; (ans += std::abs(st)) %= M; } } } std::cout << ans << '\n'; } int main() { // std::ios::sync_with_stdio(false); // std::cin.tie(nullptr); // std::cout.tie(nullptr); int T = 1; // std::cin >> T; while (T--) run(); return 0; }
failed
38_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 100010, mod = 998244353; int qmi(int a, int b) { int res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } void solve() { int n, q; cin >> n >> q; vector<vector<int>> e(n + 1); for (int i = 2; i <= n; i++) { int x; cin >> x; e[x].push_back(i); e[i].push_back(x); } vector<int> dp(n + 1), d(n + 1), dp1(n + 1); vector<vector<int>> f(n + 1, vector<int>(21, 0)); auto dfs = [&](auto dfs, int x, int fa) -> void { dp[x] = 1; for (auto j : e[x]) { if (j == fa) continue; dfs(dfs, j, x); dp[x] = (dp[x] * (dp[j] + 1)) % mod; } }; vector<int> s(n + 1); auto dfs1 = [&](auto dfs1, int x, int fa) -> void { vector<int> ss(e[x].size(), 1), sf(e[x].size(), 1); vector<int> tmp; for (auto j : e[x]) { if (j == fa) continue; tmp.push_back(j); } if (tmp.size()) { ss[0] = dp[tmp[0]] + 1; for (int i = 1; i < (int)tmp.size(); i++) ss[i] = ss[i - 1] * (dp[tmp[i]] + 1) % mod; sf[tmp.size() - 1] = dp[tmp.back()] + 1; for (int i = tmp.size() - 2; i >= 0; i--) sf[i] = sf[i + 1] * (dp[tmp[i]] + 1) % mod; } for (int i = 0; i < (int)tmp.size(); i++) { int j = tmp[i]; s[j] = (s[x] + dp[j]) % mod; d[j] = d[x] + 1; f[j][0] = x; for (int i = 1; i <= 20; i++) f[j][i] = f[f[j][i - 1]][i - 1]; int tmp1 = dp[x], tmp2 = dp[j]; dp[x] = ((i > 0 ? ss[i - 1] : 1ll) * (i + 1 < (int)tmp.size() ? sf[i + 1] : 1ll)) % mod; dp[j] = (dp[j] * (dp[x] + 1)) % mod; dp1[j] = dp[j]; dfs1(dfs1, j, x); dp[x] = tmp1, dp[j] = tmp2; } }; dfs(dfs, 1, 0); d[1] = 1, s[1] = dp[1]; dp1[1] = dp[1]; dfs1(dfs1, 1, 0); auto lca = [&](int x, int y) -> int { if (d[x] > d[y]) swap(x, y); for (int i = 20; i >= 0; i--) { if (d[f[y][i]] >= d[x]) y = f[y][i]; } if (x == y) return x; for (int i = 20; i >= 0; i--) { if (f[y][i] != f[x][i]) x = f[x][i], y = f[y][i]; } return f[x][0]; }; for (int i = 1; i <= q; i++) { int a, b; cin >> a >> b; int lc = lca(a, b); int ans = 0; ans = ((s[a] + s[b]) % mod - 2 * s[lc] % mod + dp1[lc] + mod) % mod; ans = (ans % mod + mod) % mod; cout << ans << '\n'; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } }
failed
4_1_wrong.cpp
#include <bits/stdc++.h> #define N 1000005 #define ll long long #define pii pair<int, int> #define fi first #define se second #define mp make_pair using namespace std; ll read() { ll sum = 0, f = 1; char st = getchar(); while (st < '0' || st > '9') { if (st == '-') f = -1; st = getchar(); } while ('0' <= st && st <= '9') { sum = (sum << 3) + (sum << 1) + st - '0'; st = getchar(); } return sum * f; } const int maxn = 2010, mod = 998244353; string S; vector<int> a[maxn]; int z[maxn], top; int op[maxn], val[20]; int f[maxn]; void sous(int x) { if (op[x] >= 0) f[x] = val[op[x]]; else { sous(a[x][0]), sous(a[x][1]); if (op[x] == -1) f[x] = max(f[a[x][0]], f[a[x][1]]); else if (op[x] == -2) f[x] = min(f[a[x][0]], f[a[x][1]]); else if (op[x] == -3) f[x] = f[a[x][0]] < f[a[x][1]]; else if (op[x] == -4) f[x] = f[a[x][0]] <= f[a[x][1]]; } // cout<<"x="<<x<<" op[x]="<<op[x]<<" f[x]="<<f[x]<<endl; // for(int y:a[x]) // cout<<"y="<<y<<endl; } void put0() { for (int i = 1; i <= 9; ++i) printf("(min "); printf("a"); for (int i = 2; i <= 10; ++i) printf(" %c)", 'a' + i - 1); } void sous1(int x) { if (op[x] >= 0) putchar('a' + op[x]); else { putchar('('); if (op[x] == -1) { printf("("); sous1(a[x][0]); printf(" <= "); put0(); printf(") <= "); sous1(a[x][1]); } if (op[x] == -2) { printf("min "); sous1(a[x][0]); printf(" "); sous1(a[x][1]); } if (op[x] == -3) { printf("min ("); sous1(a[x][0]); printf(" <= "); put0(); printf(") "); sous1(a[x][1]); } if (op[x] == -4) { sous1(a[x][0]); printf(" <= "); sous1(a[x][1]); } putchar(')'); } } int main() { int TT = 1; while (TT--) { getline(cin, S); int n = S.size(), m = 0; for (int i = 0; i < n; ++i) { if (S[i] == '(') { if (top) a[z[top]].push_back(m + 1); // cout<<"z[top]="<<z[top]< z[++top] = ++m; } else if (S[i] == ')') --top; else if (i + 2 < n && S[i] == 'm' && S[i + 1] == 'a' && S[i + 2] == 'x') { op[z[top]] = -1, i += 2; } else if (i + 2 < n && S[i] == 'm' && S[i + 1] == 'i' && S[i + 2] == 'n') { op[z[top]] = -2, i += 2; } else if (i + 1 < n && S[i] == '<' && S[i + 1] == '=') { op[z[top]] = -4, ++i; } else if (S[i] == '<') { op[z[top]] = -3; } else if ('a' <= S[i] && S[i] <= 'z') { // cout<<"z[top]="<<z[top]<<" m="<<m<<endl; a[z[top]].push_back(++m); op[m] = S[i] - 'a'; } } for (int i = 0; i < 10; ++i) val[i] = 1; sous(1); // cout<<"f[1]="<<f[1]<<endl; if (!f[1]) { puts("NO"); return 0; } // put0(); sous1(1); } return 0; }
failed
93_2_wrong.cpp
#include <cassert> #include <iostream> #include <vector> using namespace std; const int N = 5000 + 5; const int mod = 998244353; struct EDGE { int to, nxt; } edge[N << 1]; int n, cnt, sz[N], f[N], g[N], p[N], dep[N], fac[N], head[N], invfac[N], h[N][N]; void addedge(int u, int v) { edge[++cnt].to = v; edge[cnt].nxt = head[u]; head[u] = cnt; } int qpow(int a, int b) { int ret = 1; while (b) { if (b & 1) ret = 1ll * ret * a % mod; a = 1ll * a * a % mod; b >>= 1; } return ret; } void Init(int n) { fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod; invfac[n] = qpow(fac[n], mod - 2); for (int i = n - 1; i >= 0; i--) invfac[i] = 1ll * invfac[i + 1] * (i + 1) % mod; assert(invfac[0] == 1); } int C(int n, int m) { if (n < m || m < 0 || n < 0) return 0; return 1ll * fac[n] * invfac[m] % mod * invfac[n - m] % mod; } void dfs(int u, int fa) { sz[u] = 1; g[u] = 1; dep[u] = dep[fa] + 1; p[u] = 1; for (int i = head[u]; i; i = edge[i].nxt) { int v = edge[i].to; if (v == fa) continue; dfs(v, u); sz[u] += sz[v]; g[u] = 1ll * g[u] * g[v] % mod * invfac[sz[v]] % mod; p[u] = 1ll * p[u] * g[v] % mod * invfac[sz[v]] % mod; } g[u] = 1ll * g[u] % mod * fac[sz[u] - 1] % mod; } int main() { Init(N - 1); cin >> n; for (int i = 2; i <= n; i++) { cin >> f[i]; addedge(i, f[i]); addedge(f[i], i); } dfs(1, 0); // for(int i = 1; i <= n; i++) { // printf("%d ", g[i]); // } // puts(""); h[1][1] = g[1]; for (int u = 2; u <= n; u++) { vector<int> sum(n + 1); for (int j = 1; j <= n; j++) { sum[j] = (sum[j - 1] + 1ll * h[f[u]][j] * qpow(C(n - j, sz[u]), mod - 2) % mod) % mod; } for (int i = 1; i <= n; i++) { // for(int j = 1; j < i; j++) { h[u][i] = (h[u][i] + 1ll * sum[i - 1] % mod * C(n - i, sz[u] - 1) % mod) % mod; // } } } for (int i = 1; i <= n; i++) printf("%d ", h[i][i]); puts(""); return 0; }
failed
49_1_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; void sol() { int n, m; std::cin >> n >> m; std::vector<int> a(n); std::vector<int> b(m); bool ok = false; ; if (n == 1 && m == 1) { std::cout << "Yes\n"; std::cout << "1\n"; return; } if (n < m) { std::iota(a.begin(), a.end(), 1); } else { ok = true; ; std::iota(b.begin(), b.end(), 1); } int p = n * m; if (ok) { for (int i = 0; i < n; i++) { a[i] = i * m + 1; } std::set<int> s; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s.find(a[i] * b[j] % p) != s.end()) { std::cout << "No" << "\n"; return; } s.insert(a[i] * b[j] % p); } } std::cout << "Yes\n"; for (int i = 0; i < n; i++) { std::cout << a[i] << " \n"[i == n - 1]; } for (int i = 0; i < m; i++) { std::cout << b[i] << " \n"[i == m - 1]; } } else { for (int i = 0; i < m; i++) { b[i] = i * n + 1; } std::set<int> s; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s.find(a[i] * b[j] % p) != s.end()) { std::cout << "No" << "\n"; return; } s.insert(a[i] * b[j] % p); } } std::cout << "Yes\n"; for (int i = 0; i < n; i++) { std::cout << a[i] << " \n"[i == n - 1]; } for (int i = 0; i < m; i++) { std::cout << b[i] << " \n"[i == m - 1]; } } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; std::cin >> t; while (t--) { sol(); } return 0; }
failed
80_1_wrong.cpp
#include <iostream> #include <string> #include <vector> using namespace std; class Solution { private: // Check if tiles count matches bool checkTilesCount(const vector<string> &start, const vector<string> &target, int h, int w) { 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; } // Get positions of all tiles of a specific color vector<pair<int, int>> getTilePositions(const vector<string> &grid, char color, int h, int w) { vector<pair<int, int>> positions; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (grid[i][j] == color) { positions.push_back({i, j}); } } } return positions; } // Check if a tile can reach its target position bool canReachPosition(const vector<string> &start, pair<int, int> from, pair<int, int> to, int h, int w) { // Check if there are any blocking tiles in between that can't be moved // For horizontal movement: int minCol = min(from.second, to.second); int maxCol = max(from.second, to.second); // For vertical movement: int minRow = min(from.first, to.first); int maxRow = max(from.first, to.first); // A tile can always reach any position in its row or column through tilting return true; } public: bool solve(int h, int w, const vector<string> &start, const vector<string> &target) { // First check if number of tiles of each color matches if (!checkTilesCount(start, target, h, w)) return false; // For each color for (char color = 'a'; color <= 'z'; color++) { // Get positions of tiles of this color in both configurations auto sourcePos = getTilePositions(start, color, h, w); auto targetPos = getTilePositions(target, color, h, w); // If no tiles of this color, continue if (sourcePos.empty()) continue; // For each tile of this color, check if it can reach some target position for (auto &pos : sourcePos) { bool canReachAny = false; for (auto &tgt : targetPos) { if (canReachPosition(start, pos, tgt, h, w)) { canReachAny = true; break; } } if (!canReachAny) return false; } } return true; } }; 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_2_wrong.cpp
#define _CRT_SECURE_NO_WARNINGS #include <algorithm> #include <cassert> #include <cmath> #include <cstring> #include <iostream> #include <vector> typedef long long ll; // typedef long double ld; typedef double ld; typedef std::vector<int> Vint; typedef std::vector<ll> Vll; typedef std::vector<ld> Vld; const ld INF = 1e17; const ld TOL = 1e-6; const ld PI = acos(-1); inline int sign(const ld &x) { return x < -TOL ? -1 : x > TOL; } inline bool zero(const ld &x) { return !sign(x); } bool cmpvld(const Vld &v1, const Vld &v2) { int sz = v1.size(); if (sz != v2.size()) return 0; for (int i = 0; i < sz; i++) if (!zero(v1[i] - v2[i])) return 0; return 1; } // #define DEBUG #define WHAT_THE_FUCK #define MID 0 #define LEFT 2 #define RIGHT 1 int N, M, Q; ld A, D; Vll T[4]; bool D_OK[4]; ld THE[4][3]; struct Pos { ld x, y; Pos(ld X = 0, ld Y = 0) : x(X), y(Y) {} bool operator==(const Pos &p) const { return zero(x - p.x) && zero(y - p.y); } Pos operator+(const Pos &p) const { return {x + p.x, y + p.y}; } Pos operator-(const Pos &p) const { return {x - p.x, y - p.y}; } ld operator*(const Pos &p) const { return x * p.x + y * p.y; } ld operator/(const Pos &p) const { return x * p.y - y * p.x; } Pos rot(ld the) { return {x * cos(the) - y * sin(the), x * sin(the) + y * cos(the)}; } ld Euc() const { return x * x + y * y; } ld mag() const { return sqrt(Euc()); } friend ld rad(const Pos &p1, const Pos &p2) { return atan2(p1 / p2, p1 * p2); } friend std::istream &operator>>(std::istream &is, Pos &p) { is >> p.x >> p.y; return is; } friend std::ostream &operator<<(std::ostream &os, const Pos &p) { os << p.x << " " << p.y; return os; } }; const Pos O = {0, 0}; typedef std::vector<Pos> Polygon; ld cross(const Pos &d1, const Pos &d2, const Pos &d3) { return (d2 - d1) / (d3 - d2); } int ccw(const Pos &d1, const Pos &d2, const Pos &d3) { ld ret = cross(d1, d2, d3); return zero(ret) ? 0 : ret > 0 ? 1 : -1; } ld dot(const Pos &d1, const Pos &d2, const Pos &d3) { return (d2 - d1) * (d3 - d2); } bool on_seg_weak(const Pos &d1, const Pos &d2, const Pos &d3) { ld ret = dot(d1, d3, d2); return !ccw(d1, d2, d3) && sign(ret) > 0; } ld cos_2nd(const ll &a, const ll &b, const ll &c) { ll num = a + b - c; ld den = 2 * sqrt(a) * sqrt(b); ld t = num / den; return std::abs(acos(std::min(std::max(t, -(ld)1.0), (ld)1.0))); } ld heron(const ll &a2, const ll &b2, const ll &c2) { ld a = sqrt(a2), b = sqrt(b2), c = sqrt(c2); ld s = (a + b + c) / 2; ld ret = sqrt(s * (s - a) * (s - b) * (s - c)); return ret; } ld heron(const Vll &v) { assert(3 == v.size()); return heron(v[0], v[1], v[2]); } bool _4at1() { if (M < 4) return 0; ld t0 = 0; for (int i = 0; i < 4; i++) t0 += THE[i][MID]; if (!zero(2 * PI - t0)) return 0; for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (j == i) continue; for (int k = 1; k <= 3; k++) { if (k == i || k == j) continue; ll nxtd = T[0][RIGHT]; ld nxtt = THE[0][RIGHT]; bool f = 1; for (const auto &l : {i, j, k}) { if (T[l][LEFT] == nxtd && zero(PI * .5 - (nxtt + THE[l][LEFT]))) { nxtd = T[l][RIGHT]; nxtt = THE[l][RIGHT]; } else if (T[l][RIGHT] == nxtd && zero(PI * .5 - (nxtt + THE[l][RIGHT]))) { nxtd = T[l][LEFT]; nxtt = THE[l][LEFT]; } else { f = 0; break; } } if (f) { bool f1 = nxtd == T[0][LEFT]; bool f2 = zero(PI * .5 - (nxtt + THE[0][LEFT])); assert(f1 && f2); return 1; } } } } return 0; } bool half_check(const int &i, const int &j) { if (!D_OK[i]) return 0; ld a1 = heron(T[i]); ld a2 = heron(T[j]); if (!zero(D / sqrt(2) - sqrt(a1 + a2))) return 0; Vld T1 = {sqrt(T[j][0]), sqrt(T[j][1]), sqrt(T[j][2])}; std::sort(T1.begin(), T1.end()); for (int k = 1; k <= 2; k++) { if (zero(THE[i][k] - PI * .5)) { Vld T2; T2.push_back(sqrt(A * 2)); T2.push_back(D - sqrt(T[i][k])); T2.push_back(sqrt(T[i][k == LEFT ? RIGHT : LEFT])); std::sort(T1.begin(), T1.end()); if (cmpvld(T1, T2)) return 1; } } for (int k = 1; k <= 2; k++) { if (zero(THE[i][k] - PI * .25)) { Vld T2; T2.push_back(D); T2.push_back(sqrt(A * 2) - sqrt(T[i][k])); T2.push_back(sqrt(T[i][k == LEFT ? RIGHT : LEFT])); std::sort(T1.begin(), T1.end()); if (cmpvld(T1, T2)) return 1; } } return 0; } bool _2and2() { for (int i = 1; i <= 3; i++) { if (half_check(0, i) || half_check(i, 0)) { for (int j = 1; j <= 3; j++) { if (j == i) continue; for (int k = 1; k <= 3; k++) { if (k == i || k == j) continue; if (half_check(j, k)) return 1; } } } } return 0; } bool compose_triangle(const Vint &vi, Vld &vd) { Vll T1 = T[vi[0]], T2 = T[vi[1]]; for (int k = 0; k < 2; k++) { for (int l = 0; l < 2; l++) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { ld t1 = cos_2nd(T1[i], T1[(i + 1) % 3], T1[(i + 2) % 3]); ld t2 = cos_2nd(T2[j], T2[(j + 1) % 3], T2[(j + 2) % 3]); if (!zero(PI - (t1 + t2))) continue; for (int m = 0; m < 2; m++) { if (T1[(i + m) % 3] == T2[(j + m) % 3]) { Vld v; v.push_back(sqrt(T1[(i + 2) % 3])); v.push_back(sqrt(T2[(j + 2) % 3])); v.push_back(sqrt(T1[(i + (m + 1) % 2) % 3]) + sqrt(T2[(j + (m + 1) % 2) % 3])); std::sort(v.begin(), v.end()); if (cmpvld(vd, v)) return 1; } } } } std::reverse(T2.begin(), T2.end()); } std::reverse(T1.begin(), T1.end()); } return 0; } bool _2in2() { for (int i = 0; i < 4; i++) { if (!D_OK[i]) continue; for (int j = 0; j < 4; j++) { if (j == i) continue; if (!D_OK[j]) continue; for (int k = 1; k <= 2; k++) { for (int l = 1; l <= 2; l++) { if (zero(PI * .5 - THE[i][k]) && zero(PI * .5 - THE[j][l])) { if (!zero(D - (sqrt(T[i][k]) + sqrt(T[j][l])))) continue; ld d1 = sqrt(T[i][k == LEFT ? RIGHT : LEFT]); ld d2 = sqrt(T[j][l == LEFT ? RIGHT : LEFT]); Vld vd = {D, d1, d2}; std::sort(vd.begin(), vd.end()); Vint vi; for (int q = 0; q < 4; q++) if (q != i && q != j) vi.push_back(q); if (compose_triangle(vi, vd)) return 1; } } } } } return 0; } bool half_check(const Vint &v) { assert(3 == v.size()); for (int i = 0; i < 3; i++) { Vint vi; for (int j = 0; j < 3; j++) if (j != i) vi.push_back(v[j]); if (D_OK[v[i]]) { Vld vd; for (int j = 1; j <= 2; j++) { if (zero(PI * .5 - THE[v[i]][j]) && sign(D - sqrt(T[v[i]][j])) > 0) { vd.clear(); vd.push_back(sqrt(A * 2)); vd.push_back(sqrt(T[v[i]][j == LEFT ? RIGHT : LEFT])); vd.push_back(D - sqrt(T[v[i]][j])); std::sort(vd.begin(), vd.end()); if (compose_triangle(vi, vd)) return 1; } if (zero(PI * .25 - THE[v[i]][j]) && sign(sqrt(A * 2) - sqrt(T[v[i]][j])) > 0) { vd.clear(); vd.push_back(D); vd.push_back(sqrt(T[v[i]][j == LEFT ? RIGHT : LEFT])); vd.push_back(sqrt(A * 2) - sqrt(T[v[i]][j])); std::sort(vd.begin(), vd.end()); if (compose_triangle(vi, vd)) return 1; } } } for (int j = 0; j < 3; j++) { if (zero(sqrt(T[v[i]][j]) - sqrt(A * 2))) { ld tl = cos_2nd(T[v[i]][j], T[v[i]][(j + 2) % 3], T[v[i]][(j + 1) % 3]); ld tr = cos_2nd(T[v[i]][j], T[v[i]][(j + 1) % 3], T[v[i]][(j + 2) % 3]); ld tm = cos_2nd(T[v[i]][(j + 2) % 3], T[v[i]][(j + 1) % 3], T[v[i]][j]); ll dl = T[v[i]][(j + 2) % 3]; ll dr = T[v[i]][(j + 1) % 3]; if (sign(tl - PI * .25) > 0 || sign(tr - PI * .25) > 0) return 0; if (sign(tl - PI * .25) < 0 && sign(tr - PI * .25) < 0) { int i1 = v[(i + 1) % 3], i2 = v[(i + 2) % 3]; if (D_OK[i1] && D_OK[i2] && zero(PI * 2 - (tm + THE[i1][MID] + THE[i2][MID]))) { for (int _ = 0; _ < 2; _++) { ld d3 = -1, d4 = -1, t3 = -1, t4 = -1; if (T[i1][LEFT] == dl && zero(PI * .25 - (tl + THE[i1][LEFT]))) { d3 = T[i1][RIGHT]; t3 = THE[i1][RIGHT]; } else if (T[i1][RIGHT] == dl && zero(PI * .25 - (tl + THE[i1][RIGHT]))) { d3 = T[i1][LEFT]; t3 = THE[i1][LEFT]; } if (T[i2][LEFT] == dr && zero(PI * .25 - (tr + THE[i2][LEFT]))) { d4 = T[i2][RIGHT]; t4 = THE[i2][RIGHT]; } else if (T[i2][RIGHT] == dr && zero(PI * .25 - (tr + THE[i2][RIGHT]))) { d4 = T[i2][LEFT]; t4 = THE[i2][LEFT]; } if (d3 > -1 && d4 > -1 && d3 == d4 && zero(PI * .5 - (t3 + t4))) return 1; std::swap(i1, i2); } } } Vld vd; vd.push_back(D); if (sign(tl - PI * .25) == 0) { vd.push_back(D - sqrt(dl)); vd.push_back(sqrt(dr)); } else if (sign(tr - PI * .25) == 0) { vd.push_back(D - sqrt(dr)); vd.push_back(sqrt(dl)); } std::sort(vd.begin(), vd.end()); if (vd.size() == 3 && compose_triangle(vi, vd)) return 1; } } } return 0; } bool _3and1() { for (int i = 0; i < 4; i++) { if ((zero(D - sqrt(T[i][LEFT])) && zero(THE[i][LEFT] - PI * .5)) || (zero(D - sqrt(T[i][RIGHT])) && zero(THE[i][RIGHT] - PI * .5))) { Vint v; for (int j = 0; j < 4; j++) if (j != i) v.push_back(j); if (half_check(v)) return 1; } } return 0; } bool two_tri_check(const Vint &vi, Vld &T1, Vld &T2) { assert(2 == vi.size()); Vld T3, T4; for (int i = 0; i < 3; i++) { T3.push_back(sqrt(T[vi[0]][i])); T4.push_back(sqrt(T[vi[1]][i])); } std::sort(T3.begin(), T3.end()); std::sort(T4.begin(), T4.end()); if (cmpvld(T1, T3) && cmpvld(T2, T4)) return 1; if (cmpvld(T1, T4) && cmpvld(T2, T3)) return 1; return 0; } bool trap_check(const int &i, const int &j, Polygon &B) { if (j == -1) { ld t0 = rad(B[1] - B[0], B[2] - B[0]); ld t1 = rad(B[0] - B[2], B[1] - B[2]); ld L = (B[2] - B[0]).mag(); ld l1 = D - B[2].y; Vint vi; for (int k = 0; k < 4; k++) if (k != i) vi.push_back(k); for (const int &k : vi) { if (!D_OK[k]) continue; for (int l = 1; l <= 2; l++) { if (!zero(PI * .5 - (t0 + THE[k][l]))) continue; ld nxtt = THE[k][l == LEFT ? RIGHT : LEFT]; ld nxtd = sqrt(T[k][l == LEFT ? RIGHT : LEFT]); ld l2 = L - sqrt(T[k][l]); for (const int &m : vi) { if (m == k) continue; if (!D_OK[m]) continue; for (int n = 1; n <= 2; n++) { if (!zero(PI * .5 - (nxtt + THE[m][n]))) continue; if (!zero(nxtd - sqrt(T[m][n]))) continue; ld l3 = sqrt(T[m][n == LEFT ? RIGHT : LEFT]); Vld T1 = {l1, l2, l3}, T2; std::sort(T1.begin(), T1.end()); for (const int &q : vi) { if (q == m || q == k) continue; T2 = {sqrt(T[q][0]), sqrt(T[q][1]), sqrt(T[q][2])}; std::sort(T2.begin(), T2.end()); if (cmpvld(T1, T2)) return 1; } } } } } return 0; } Pos p = B[3]; Vint vi; for (int k = 0; k < 4; k++) if (k != i && k != j) vi.push_back(k); if (on_seg_weak(Pos(0, D), Pos(D, D), p)) { Vld T1 = {(B[2] - B[3]).mag(), B[2].x - B[3].x, B[3].y - B[2].y}; Vld T2 = {(B[0] - B[3]).mag(), B[3].x - B[0].x, B[3].y - B[0].y}; std::sort(T1.begin(), T1.end()); std::sort(T2.begin(), T2.end()); if (two_tri_check(vi, T1, T2)) return 1; } if (p == Pos(0, D)) { Vld T1 = {D, (B[3] - B[2]).mag(), B[3].y - B[2].y}; std::sort(T1.begin(), T1.end()); if (compose_triangle(vi, T1)) return 1; return 0; } Polygon Z; if (on_seg_weak(Pos(0, 0), Pos(0, D), p)) Z = {B[3], B[2], Pos(D, D), Pos(0, D)}; else if (on_seg_weak(Pos(D, 0), Pos(D, D), p)) Z = {B[0], B[3], Pos(D, D), Pos(0, D)}; else return 0; int i1 = vi[0], i2 = vi[1]; Vld T1, T2 = {sqrt(T[i2][0]), sqrt(T[i2][1]), sqrt(T[i2][2])}; for (int k = 0; k < 4; k++) { const Pos &pre = Z[k], cur = Z[(k + 1) % 4], nxt = Z[(k + 2) % 4]; ld t1 = rad(nxt - cur, pre - cur); ld d1 = (cur - pre).mag(); ld d2 = (cur - nxt).mag(); std::sort(T2.begin(), T2.end()); for (int l = 0; l < 3; l++) { for (int m = 1; m <= 2; m++) { if (zero(sqrt(T[i1][l]) - d1) && zero(sqrt(T[i1][(l + m) % 3]) - d2)) { ld t = cos_2nd(T[i1][l], T[i1][(l + m) % 3], T[i1][(l + (m == LEFT ? RIGHT : LEFT)) % 3]); if (zero(t - t1)) { T1 = {(Z[(k + 2) % 4] - Z[(k + 3) % 4]).mag(), (Z[(k + 3) % 4] - Z[(k + 4) % 4]).mag()}; T1.push_back(sqrt(T[i1][(l + (m == LEFT ? RIGHT : LEFT)) % 3])); std::sort(T1.begin(), T1.end()); if (cmpvld(T1, T2)) return 1; } } } } } return 0; } bool stack_up() { for (int i = 0; i < 4; i++) { if (!D_OK[i]) continue; if (!zero(PI * .5 - THE[i][LEFT]) && !zero(PI * .5 - THE[i][RIGHT])) continue; Polygon B = {Pos(0, 0), Pos(D, 0)}; if (zero(PI * .5 - THE[i][LEFT])) B.push_back(Pos(D, sqrt(T[i][LEFT]))); if (zero(PI * .5 - THE[i][RIGHT])) B.push_back(Pos(D, sqrt(T[i][RIGHT]))); if (sign(B[2].y - D) >= 0) return 0; assert(3 == B.size()); ld nxtd = (B[0] - B[2]).mag(); ld pvt = rad(B[1] - B[0], B[2] - B[0]); if (trap_check(i, -1, B)) return 1; for (int j = 0; j < 4; j++) { if (j == i) continue; for (int k = 0; k < 3; k++) { if (!zero(nxtd - sqrt(T[j][k]))) continue; for (int l = 1; l <= 2; l++) { ld t = cos_2nd(T[j][k], T[j][(k + l) % 3], T[j][(k + (l == LEFT ? RIGHT : LEFT)) % 3]); ld d = sqrt(T[j][(k + l) % 3]); Pos nxt = Pos(d, 0).rot(t).rot(pvt); B.push_back(nxt); if (trap_check(i, j, B)) return 1; B.pop_back(); } } } } return 0; } bool query() { A = 0; for (int i = 0; i < 4; i++) { T[i].resize(3); std::cin >> T[i][0] >> T[i][1] >> T[i][2]; A += heron(T[i]); } D = sqrt(A); M = 0; for (int i = 0; i < 4; i++) { D_OK[i] = 0; for (int j = 0; j < 3; j++) { if (zero(sqrt(T[i][j]) - D)) { M++; rotate(T[i].begin(), T[i].begin() + j, T[i].end()); D_OK[i] = 1; THE[i][LEFT] = cos_2nd(T[i][MID], T[i][LEFT], T[i][RIGHT]); THE[i][RIGHT] = cos_2nd(T[i][MID], T[i][RIGHT], T[i][LEFT]); THE[i][MID] = cos_2nd(T[i][LEFT], T[i][RIGHT], T[i][MID]); break; } } } #ifdef DEBUG std::cout << "DEBUG::\n"; std::cout << "A:: " << A << "\n"; std::cout << "D:: " << D << "\n"; std::cout << "L:: " << D * sqrt(2) << "\n"; std::cout << "T1: " << sqrt(T[0][0]) << " " << sqrt(T[0][1]) << " " << sqrt(T[0][2]) << "\n"; std::cout << "T2: " << sqrt(T[1][0]) << " " << sqrt(T[1][1]) << " " << sqrt(T[1][2]) << "\n"; std::cout << "T3: " << sqrt(T[2][0]) << " " << sqrt(T[2][1]) << " " << sqrt(T[2][2]) << "\n"; std::cout << "T4: " << sqrt(T[3][0]) << " " << sqrt(T[3][1]) << " " << sqrt(T[3][2]) << "\n"; std::cout << "DEBUG::\n"; #endif if (!M) return 0; return _4at1() || _2and2() || _2in2() || _3and1() || stack_up(); } void solve() { std::cin.tie(0)->sync_with_stdio(0); std::cout.tie(0); std::cout << std::fixed; std::cout.precision(9); std::cin >> Q; while (Q--) std::cout << query() << "\n"; return; } int main() { solve(); return 0; } // boj31960 Square of Triangles /* 1 5524800 4475088 9999888 55248 4475088 4530336 9999888 5580048 5524800 55248 4530336 5580048 1 1 9999936 4999968 4999968 902772 5069412 6944400 4999968 902772 5069412 3611088 4999968 277776 1 2 10000000 5078125 3828125 2031250 78125 1953125 703125 5078125 2031250 5000000 10000000 5000000 5000000 10000000 5000000 5000000 2890625 390625 6250000 1250000 10000000 1250000 2890625 3515625 1 1 1 6923007 4319483 4852022 2130156 4319483 769223 9999899 3076892 6923007 6923007 2899379 4852022 0 1 9999916 1689175 5743195 5743195 878371 4999958 9999916 4999958 4999958 1689175 878371 4999958 1 1 4900000 6500000 10000000 2000000 10000000 8000000 900000 6500000 8000000 10000000 2000000 8000000 1 */
failed
109_3_wrong.cpp
#include <bits/stdc++.h> // #define int long long using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; const int N = 1e6 + 7; const int mod = 1e9 + 7; const int MX = 2147483647; typedef pair<int, int> pii; int a[N], b[N]; int n, m, k; bool check(int x) { for (int i = 1; i <= n; i++) b[i] = 0; for (int i = 1; i <= x; i++) { b[(a[i] - (a[i] % m)) / m + 1]++; } int mx = 0; for (int i = 1; i <= n; i++) mx = max(mx, b[i]); return (m - mx) <= k; } void solve() { cin >> n >> m >> k; for (int i = 1; i <= n * m; i++) cin >> a[i]; int l = m, r = n * m, ans; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) r = mid - 1, ans = mid; else l = mid + 1; } cout << ans << "\n"; } signed main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; while (t--) solve(); return 0; }
failed
31_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define F first #define S second #define sz(x) (int)x.size() #define all(x) x.begin(), x.end() #define make_unique(x) sort(all(x)), x.erase(unique(all(x)), x.end()) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 Rng(chrono::steady_clock::now().time_since_epoch().count()); template <int mod> class Modular { public: int val; Modular() : val(0) {} Modular(int new_val) : val(new_val) {} // Modular(long long new_val) : val(new_val % mod) {} // AFFECTS OPERATOR* // (because it has one more %) friend Modular operator+(const Modular &a, const Modular &b) { if (a.val + b.val >= mod) return a.val + b.val - mod; else return a.val + b.val; } friend Modular operator-(const Modular &a, const Modular &b) { if (a.val - b.val < 0) return a.val - b.val + mod; else return a.val - b.val; } friend Modular operator*(const Modular &a, const Modular &b) { return 1ll * a.val * b.val % mod; } friend Modular binpow(Modular a, long long n) { Modular res = 1; for (; n; n >>= 1) { if (n & 1) res *= a; a *= a; } return res; } /* friend Modular inv(const Modular& a) { Modular x, y; gcd(a.val, mod, x, y); return x; } */ friend Modular inv(const Modular &a) { return binpow(a, mod - 2); } friend Modular operator^(const Modular &a, const long long &b) { if (b >= 0) return binpow(a, b % (mod - 1)); return binpow(inv(a), (-b) % (mod - 1)); } /* ALTERNATIVE INVERSE FUNCTION USING EXTENDED EUCLIDEAN ALGORITHM friend void gcd(int a, int b, Modular& x, Modular& y) { if (a == 0) { x = Modular(0); y = Modular(1); return; } Modular x1, y1; gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; } */ Modular operator/(const Modular &ot) const { return *this * inv(ot); } Modular &operator++() { if (val + 1 == mod) val = 0; else ++val; return *this; } Modular operator++(int) { Modular tmp = *this; ++(*this); return tmp; } Modular operator+() const { return *this; } Modular operator-() const { return 0 - *this; } Modular &operator+=(const Modular &ot) { return *this = *this + ot; } Modular &operator-=(const Modular &ot) { return *this = *this - ot; } Modular &operator*=(const Modular &ot) { return *this = *this * ot; } Modular &operator/=(const Modular &ot) { return *this = *this / ot; } bool operator==(const Modular &ot) const { return val == ot.val; } bool operator!=(const Modular &ot) const { return val != ot.val; } bool operator<(const Modular &ot) const { return val < ot.val; } bool operator>(const Modular &ot) const { return val > ot.val; } explicit operator int() const { return val; } }; template <int mod> istream &operator>>(istream &istr, Modular<mod> &x) { return istr >> x.val; } template <int mod> ostream &operator<<(ostream &ostr, const Modular<mod> &x) { return ostr << x.val; } const int mod = 998244353; using Mint = Modular<mod>; const int N = 5e3 + 20; const int M = 5e3; int n, sz[N]; Mint fact[N], invf[N]; Mint ans[N], dp[N][N]; Mint s[N]; vector<int> g[N]; Mint C(int n, int k) { if (k < 0 || k > n) return Mint(0); return fact[n] * invf[n - k] * invf[k]; } void prec(int v) { sz[v] = 1; s[v] = 1; for (int u : g[v]) { prec(u); sz[v] += sz[u]; s[v] *= s[u] * C(sz[v] - 1, sz[u]); } } void dfs(int v, int p, Mint S = 1) { ans[v] = dp[p][v - 1] * C(n - v, sz[v] - 1); ans[v] *= S * s[v]; for (int u : g[v]) { for (int i = 1; i <= n; ++i) { dp[v][i] = dp[p][i - 1] * C(n - sz[u] - i, sz[v] - sz[u] - 1); } for (int i = 1; i <= n; ++i) { dp[v][i] += dp[v][i - 1]; } s[v] /= s[u] * C(sz[v] - 1, sz[u]); dfs(u, v, S * s[v]); s[v] *= s[u] * C(sz[v] - 1, sz[u]); } } int main() { cin.tie(0)->sync_with_stdio(0); fact[0] = 1; for (int i = 1; i <= M; ++i) fact[i] = fact[i - 1] * i; invf[M] = Mint(1) / fact[M]; for (int i = M; i >= 1; --i) invf[i - 1] = invf[i] * i; cin >> n; for (int i = 2, f; i <= n; ++i) { cin >> f; g[f].push_back(i); } prec(1); dp[0][0] = 1; dfs(1, 0); for (int i = 1; i <= n; ++i) { cout << ans[i] << " \n"[i == n]; } }
failed
54_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; const int MAXN = 1e3 + 5; const int dx[] = {0, -1, 0, 1}; const int dy[] = {1, 0, -1, 0}; int n; char s[MAXN][MAXN]; int res[MAXN][MAXN][4]; bool isin(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= n; } int getnext(int x, int y, int k) { int kk = -1; for (int t = 0; t < 4; ++t) { kk = (k - 1 + t + 4) % 4; int xx = x + dx[kk]; int yy = y + dy[kk]; if (!isin(xx, yy) || s[xx][yy] != '#') return kk; } return -1; } void dfs(int x, int y, int k) { if (res[x][y][k] == -2) { res[x][y][k] = -1; return; } if (res[x][y][k]) { return; } int kk = getnext(x, y, k); if (kk == -1) { res[x][y][k] = -1; return; } int xx = x + dx[kk], yy = y + dy[kk]; if (!isin(xx, yy)) { res[x][y][k] = 1; return; } res[x][y][k] = -2; dfs(xx, yy, kk); if (res[xx][yy][kk] == -1) { res[x][y][k] = -1; return; } res[x][y][k] = res[xx][yy][kk] + 1; } void solve(void) { int Q; scanf("%d%d", &n, &Q); for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1); while (Q--) { int x, y; static char d[10]; scanf("%d%d%s", &x, &y, d); int k = d[0] == 'R' ? 0 : d[0] == 'U' ? 1 : d[1] == 'D' ? 2 : 3; dfs(x, y, k); printf("%d\n", res[x][y][k]); } } int main(void) { int T = 1; while (T--) solve(); return 0; }
failed
98_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long #define PII pair<int, int> #define ai4 array<int, 4> #define ai3 array<int, 3> const int N = 50010, mod = 1e9 + 7, inf = 1e9; int dp[310][310][3]; void solve() { int n, q; cin >> n >> q; string s; cin >> s; s = ' ' + s + ' '; int cnt = 0; for (int i = 1; i <= n; i++) if (s[i] == '?') ++cnt; for (int i = 0; i <= cnt; i++) { for (int j = 0; j <= cnt; j++) { for (int k = 0; k < 3; k++) dp[i][j][k] = 0; } } for (int i = 0; i < 3; i++) dp[0][0][i] = 1; int back = -1; cnt = 0; for (int o = 1; o <= n; o++) { if (s[o] != '?') continue; vector<vector<vector<int>>> ndp( cnt + 2, vector<vector<int>>(cnt + 2, vector<int>(3))); if (cnt == 0) { ndp[1][0][0] = 1; ndp[0][1][1] = 1; ndp[0][0][2] = 1; if (s[o - 1] == 'a' || s[o + 1] == 'a') ndp[1][0][0] = 0; if (s[o - 1] == 'b' || s[o + 1] == 'b') ndp[0][1][1] = 0; if (s[o - 1] == 'c' || s[o + 1] == 'c') ndp[0][0][2] = 0; } else { for (int i = 0; i <= cnt; i++) { for (int j = 0; j <= cnt; j++) { if (!dp[i][j]) continue; if (back == o - 1) { (ndp[i + 1][j][0] += (dp[i][j][1] + dp[i][j][2])) %= mod; (ndp[i][j + 1][1] += (dp[i][j][0] + dp[i][j][2])) %= mod; (ndp[i][j][2] += (dp[i][j][0] + dp[i][j][1])) %= mod; } else { (ndp[i + 1][j][0] += (dp[i][j][0] + dp[i][j][1] + dp[i][j][2])) %= mod; (ndp[i][j + 1][1] += (dp[i][j][0] + dp[i][j][1] + dp[i][j][2])) %= mod; (ndp[i][j][2] += (dp[i][j][0] + dp[i][j][1] + dp[i][j][2])) %= mod; } if (s[o - 1] == 'a' || s[o + 1] == 'a') ndp[i + 1][j][0] = 0; if (s[o - 1] == 'b' || s[o + 1] == 'b') ndp[i][j + 1][1] = 0; if (s[o - 1] == 'c' || s[o + 1] == 'c') ndp[i][j][2] = 0; } } } back = o; ++cnt; for (int i = 0; i <= cnt; i++) { for (int j = 0; j <= cnt; j++) { for (int k = 0; k < 3; k++) dp[i][j][k] = ndp[i][j][k]; } } // for(int i = 0 ; i <= cnt ; i ++ ) { // for(int j = 0 ; j <= cnt ; j ++ ) { // for(int k = 0 ; k < 3 ; k ++ ) cout << dp[i][j][k] << ' '; // cout << '\n'; // } // cout << '\n'; // } // cout << '\n'; } vector<vector<int>> a(cnt + 2, vector<int>(cnt + 2)); vector<vector<int>> sum(cnt + 2, vector<int>(cnt + 2)); vector<vector<int>> b(cnt + 2, vector<int>(cnt + 2)); for (int i = 0; i <= cnt; i++) { for (int j = 0; j <= cnt; j++) { for (int k = 0; k < 3; k++) a[i][j] += dp[i][j][k]; } } for (int i = cnt + 1; i >= 1; i--) { for (int j = cnt + 1; j >= 1; j--) a[i][j] = a[i - 1][j - 1]; } for (int i = 1; i <= cnt + 1; i++) { for (int j = 1; j <= cnt + 1; j++) { sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j]; } } for (int i = 1; i <= cnt + 1; i++) { for (int j = 1; j <= cnt + 1; j++) b[i][j] = b[i][j - 1] + a[i][j]; } // for(int i = 1 ; i <= cnt + 1 ; i ++ ) { // for(int j = 1 ; j <= cnt + 1 ; j ++ ) cout << a[i][j] << ' '; // cout << '\n'; // } int A, B, C; while (q--) { cin >> A >> B >> C; int ma = min(A, cnt), mb = min(B, cnt); int k = cnt - ma - mb; int ans = sum[ma + 1][mb + 1]; for (int i = 0; i <= ma; i++) { int r = cnt - i - mb; if (r > C) { ans -= b[i + 1][mb + 1]; continue; } int les = cnt - i - C; if (les < 0) ans -= 0; else ans -= b[i + 1][les]; // cout << les << ' '; } cout << ans << '\n'; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; while (T--) solve(); }
failed
2_1_wrong.cpp
#include <iostream> #include <string> #include <vector> using namespace std; // Remove pairs of '00' or '11' until no more removals possible string removePairs(string s) { bool changed; do { changed = false; string newS; for (int i = 0; i < s.length(); i++) { if (i + 1 < s.length() && s[i] == s[i + 1] && (s[i] == '0' || s[i] == '1')) { i++; // skip next character changed = true; } else { newS += s[i]; } } s = newS; } while (changed); return s; } // Try all possible combinations of converting '2's int findMin(string s, int pos) { // Base case: no more '2's to convert if (pos >= s.length()) { return removePairs(s).length(); } // Find next '2' while (pos < s.length() && s[pos] != '2') { pos++; } // If no more '2's found, process the string if (pos >= s.length()) { return removePairs(s).length(); } // Try converting current '2' to '0' s[pos] = '0'; int len1 = findMin(s, pos + 1); // Try converting current '2' to '1' s[pos] = '1'; int len2 = findMin(s, pos + 1); return min(len1, len2); } int solve(const string &s) { string str = s; return findMin(str, 0); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { string s; cin >> s; cout << solve(s) << '\n'; } return 0; }
failed
48_3_wrong.cpp
#ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> #define all(v) v.begin(), v.end() #define app push_back #define int long long #ifdef LOCAL #define debug(...) \ [](auto... a) { ((cout << a << ' '), ...) << endl; }(#__VA_ARGS__, ":", \ __VA_ARGS__) #define debugv(v) \ do { \ cout << #v << ": "; \ for (auto x : v) \ cout << x << ' '; \ cout << endl; \ } while (0) #else #define debug(...) #define debugv(v) #endif using namespace std; void solve(); int32_t main() { cin.tie(0); ios_base::sync_with_stdio(0); int t = 1; cin >> t; while (t--) { solve(); } } struct point3D { double x, y, z; }; struct point { double x, y; point(double x = 0, double y = 0) : x(x), y(y) {} }; point operator-(point a, point b) { return {a.x - b.x, a.y - b.y}; } point operator+(point a, point b) { return {a.x + b.x, a.y + b.y}; } point operator*(double t, point p) { return {t * p.x, t * p.y}; } double cross(point p, point q) { return p.x * q.y - q.x * p.y; } istream &operator>>(istream &is, point3D &p) { is >> p.x >> p.y >> p.z; return is; } const double eps = 1e-10; double sgn(point a, point p, point q) { return cross(q - p, a - p); } double sgn(point a, double A, double B, double C) { return A * a.x + B * a.y + C; } template <typename... Args> point intersect(point a, point b, Args... args) { double sa = sgn(a, args...); double sb = sgn(b, args...); double t = abs(sa) / (abs(sa) + abs(sb)); return a + t * (b - a); } template <typename... Args> vector<point> intersect(vector<point> a, Args... args) { int n = a.size(); if (n == 0) return a; vector<int> sign(n); for (int i = 0; i < n; ++i) { sign[i] = sgn(a[i], args...) > -eps; } if (count(all(sign), 1) == 0) return {}; if (count(all(sign), 1) == n) return a; assert(sign[0] == sign[n - 1]); int s = -1, f = -1; for (int i = 0; i + 1 < n; ++i) { if (sign[i] == 0 && sign[i + 1] == 1) s = i + 1; if (sign[i] == 1 && sign[i + 1] == 0) f = i + 1; } assert(f >= 0 && s >= 0); for (int j = s; j != f; j = (j + 1) % n) assert(sign[j] == 1); for (int j = f; j != s; j = (j + 1) % n) assert(sign[j] == 0); point bs = intersect(a[s - 1], a[s], args...); point af = intersect(a[f - 1], a[f], args...); vector<point> b{bs}; for (int j = s; j != f; j = (j + 1) % n) b.push_back(a[j]); b.push_back(af); b.push_back(bs); return b; } mt19937 rng(0); double solve(point3D *A, point3D *B, double w, double h) { debug(h, w); w = w / h; h = 1; vector<point> a, b; for (int i = 0; i < 4; ++i) a.push_back({A[i].x, A[i].z}); for (int i = 0; i < 4; ++i) b.push_back({B[i].x, B[i].z}); for (int i = 0; i < 4; ++i) a.push_back(a[i]); for (int i = 0; i < 4; ++i) b.push_back(b[i]); if (cross(a[2] - a[1], a[0] - a[1]) < eps) reverse(all(a)); for (int i = 1; i + 1 < a.size(); ++i) assert(cross(a[i + 1] - a[i], a[i - 1] - a[i]) > -eps); for (int i = 1; i + 1 < a.size(); ++i) assert(cross(b[i + 1] - b[i], b[i - 1] - b[i]) > -eps); a.resize(5); for (int i = 0; i < 4; ++i) { a = intersect(a, b[i], b[i + 1]); } for (int i = 0; i < a.size(); ++i) { debug(a[i].x, a[i].y); } auto inside = [&](point u, point v) { for (int i = 0; i + 1 < a.size(); ++i) { auto delta = a[i + 1] - a[i]; if (cross(delta, point{u.x, v.y} - a[i]) < -eps) return 0; if (cross(delta, point{v.x, u.y} - a[i]) < -eps) return 0; if (cross(delta, u - a[i]) < -eps) return 0; if (cross(delta, v - a[i]) < -eps) return 0; } return 1; }; double ans = 0; auto f = [&](point p, point q, double s) { point r = s * p + (1 - s) * q; double low = 0, high = 8e3; for (int iter = 0; iter < 40; ++iter) { double med = (low + high) / 2; if (inside(r, point{r.x + med * w, r.y + med * h})) low = med; else high = med; } ans = max(ans, (low * low * w * h)); point r2 = point{r.x + low * w, r.y + low * h}; debug(low * low * w * h, r.x, r.y, r2.x, r2.y); return (low * low * w * h); }; for (int i = 0; i + 1 < a.size(); ++i) { double low = 0, high = 1; f(a[i], a[i], 0); for (int it = 0; it < 60; ++it) { double m1 = (2 * low + high) / 3, m2 = (low + 2 * high) / 3; if (f(a[i], a[i + 1], m1) > f(a[i], a[i + 1], m2)) high = m2; else low = m1; } } return ans; } double dist(point3D a, point3D b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z)); } void solve() { point3D lens[4]; cin >> lens[0] >> lens[1] >> lens[2] >> lens[3]; double w = dist(lens[0], lens[1]), h = dist(lens[1], lens[2]); point3D screen[4]; cin >> screen[0] >> screen[1] >> screen[2] >> screen[3]; auto to_wall = [&](point3D p) { double A = screen[0].y - screen[1].y; double B = screen[1].x - screen[0].x; double C = -A * screen[0].x - B * screen[0].y; double C1 = -A * screen[1].x - B * screen[1].y; debug(C, C1); return abs(A * p.x + B * p.y + C) / sqrt(A * A + B * B); }; double from_origin = to_wall({0, 0, 0}); for (auto &[x, y, z] : lens) { double from_point = to_wall({x, y, z}); double k = from_origin / (from_origin - from_point); debug(from_point, from_origin, k); x *= k; y *= k; z *= k; } double dx = screen[0].x - screen[1].x; double dy = screen[0].y - screen[1].y; double coef = sqrt(dx * dx + dy * dy) / abs(dx); double ans = solve(lens, screen, w / coef, h) * coef; debug(coef); cout << setprecision(20) << ans << '\n'; }
failed
15_2_wrong.cpp
#include<iostream> #include<algorithm> #include<vector> #include<cmath> using namespace std; typedef long long ll; typedef pair<string,int> pll; const int N=3e5+10; int main(){ vector<pll> v1; v1.push_back({"123",0}); for(int i=1;i<=8;i++){ string s;int a; cin>>s>>a; v1.push_back({s,a}); } vector<pll> temp; temp.push_back({"123",0}); for(int i=1;i<=4;i++){ int x=v1[i*2-1].second,y=v1[i*2].second; if(x>y)temp.push_back(v1[i*2-1]); else temp.push_back(v1[i*2]); } for(auto it:temp)cout<<it.first<<' '<<it.second<<'\n'; v1.clear(); v1.push_back({"123",0}); for(int i=1;i<=2;i++){ int x=temp[i*2-1].second,y=temp[i*2].second; if(x>y)v1.push_back(temp[i*2-1]); else v1.push_back(temp[i*2]); } //cout<<'\n'; //for(auto it:v1)cout<<it.first<<' '<<it.second<<'\n'; for(int i=1;i<=1;i++){ int x=v1[i*2-1].second,y=v1[i*2].second; if(x>y){ cout<<v1[i*2-1].first<<" beats "<<v1[i*2].first; } else { cout<<v1[i*2].first<<" beats "<<v1[i*2-1].first; } } return 0; }
failed
87_2_wrong.cpp
#include <iostream> #include <vector> using namespace std; const int MOD = 998244353; vector<long long> solve(int n) { // dp[i] represents the number of ways to get sum i vector<long long> dp(n + 1, 0); dp[0] = 1; // Process each weight from n down to 1 for (int weight = n; weight >= 1; weight--) { vector<long long> new_dp(n + 1, 0); // For each possible current sum for (int sum = 0; sum <= n; sum++) { // Don't take the current weight (probability 1/2) new_dp[sum] = (new_dp[sum] + dp[sum]) % MOD; // Take the current weight if possible (probability 1/2) if (sum + weight <= n) { new_dp[sum + weight] = (new_dp[sum + weight] + dp[sum]) % MOD; } } // Update dp with new values dp = new_dp; } return dp; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<long long> result = solve(n); // Output results for (int i = 0; i <= n; i++) { cout << result[i]; if (i < n) cout << " "; } cout << endl; return 0; }
failed
21_1_wrong.cpp
#include <bits/stdc++.h> #define int long long #define F first #define S second #define MP make_pair #define PB push_back using namespace std; const int N = 2e5 + 100; vector<int> v[N]; int h[N], ans[N]; void dfs(int x, int px = -1) { if (px == -1) { h[x] = 0; } else { h[x] = h[px] + 1; } for (int u : v[x]) { if (u != px) { dfs(u, x); } } } void solve() { int n; cin >> n; for (int i = 0; i < 2 * n + 10; i++) { h[i] = 0; ans[i] = -1; v[i].clear(); } for (int i = 0; i < 2 * n - 1; i++) { int x, y; cin >> x >> y; x--; y--; v[x].PB(y); v[y].PB(x); } if (n == 1) { for (int i = 0; i < 2 * n; i++) { cout << 0 << ' '; } cout << '\n'; return; } int root = -1; for (int i = 0; i < 2 * n; i++) { if ((int)v[i].size() != 1) { root = i; break; } } // cout << root << '\n'; dfs(root); int r_b = 0, r = 0, b = 1; for (int i = 0; i < 2 * n; i++) { if ((int)v[i].size() != 1) { if (h[i] % 2 == 0) { ans[i] = 0; r_b++; } else { ans[i] = 1; r_b--; } } } for (int i = 0; i < 2 * n; i++) { if (ans[i] == -1) { if (r_b > 0) { ans[i] = b; r_b--; } else { ans[i] = r; r_b++; } } } for (int i = 0; i < 2 * n; i++) { cout << ans[i] << ' '; } cout << '\n'; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q = 1; cin >> q; while (q--) { solve(); } }
failed
20_1_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long #define double long double 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 template <class T> int sgn(T x) { return (x > 0) - (x < 0); } 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); } 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); } P operator*(T d) const { return P(x * d, y * d); } P operator/(T d) const { return P(x / d, y / d); } T dot(P p) const { return x * p.x + y * p.y; } T cross(P p) const { return x * p.y - y * p.x; } T cross(P a, P b) const { return (a - *this).cross(b - *this); } T dist2() const { return x * x + y * y; } double dist() const { return sqrt((double)dist2()); } double angle() const { return atan2(y, x); } P unit() const { return *this / dist(); } P perp() const { return P(-y, x); } P normal() const { return perp().unit(); } P rotate(double a) const { return P(x * cos(a) - y * sin(a), x * sin(a) + y * cos(a)); } friend ostream &operator<<(ostream &os, P p) { return os << "(" << p.x << "," << p.y << ")"; } }; typedef Point<double> P; bool circleInter(P a, P b, double r1, double r2, pair<P, P> *out) { if (a == b) { assert(r1 != r2); return false; } P vec = b - a; double d2 = vec.dist2(), sum = r1 + r2, dif = r1 - r2, p = (d2 + r1 * r1 - r2 * r2) / (d2 * 2), h2 = r1 * r1 - p * p * d2; if (sum * sum < d2 || dif * dif > d2) return false; P mid = a + vec * p, per = vec.perp() * sqrt(fmax(0, h2) / d2); *out = {mid + per, mid - per}; return true; } struct Circ { Point<int> c; int r, id; bool contains(const P &p) const { P cc(c.x, c.y); double d = (p - cc).dist2(); double r2 = r * r; return d < r2; } }; int N; vector<int> solve() { cin >> N; vector<Circ> circs(N); for (int i = 0; i < N; ++i) { cin >> circs[i].c.x >> circs[i].c.y >> circs[i].r; circs[i].id = i; } { // nested pairs for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { Circ &a = circs[i]; Circ &b = circs[j]; if (a.r >= b.r) continue; // try nest a in b... int d2 = (b.c - a.c).dist2(); int req_atl = b.r - a.r; if (d2 <= req_atl * req_atl) { return {i}; } } } } { // disjoint pairs for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { Circ &a = circs[i]; Circ &b = circs[j]; int d2 = (b.c - a.c).dist2(); int rs2 = (b.r + a.r) * (b.r + a.r); if (rs2 <= d2) { // equality is bad too return {i, j}; } } } } { unordered_set<string> can; for (int i = 0; i < N; ++i) { for (int j = i + 1; j < N; ++j) { Circ &a = circs[i]; Circ &b = circs[j]; P ac(a.c.x, a.c.y); P bc(b.c.x, b.c.y); pair<P, P> out; bool inter = circleInter(ac, bc, a.r, b.r, &out); auto try_pt = [&](P p) { string cur(N, '0'); for (int k = 0; k < N; ++k) { if (k == i || k == j) continue; bool in = circs[k].contains(p); if (in) { cur[k] = '1'; } if (!in) { cur[k] = '0'; } } cur[i] = '0'; cur[j] = '0'; can.insert(cur); cur[i] = '0'; cur[j] = '1'; can.insert(cur); cur[i] = '1'; cur[j] = '0'; can.insert(cur); cur[i] = '1'; cur[j] = '1'; can.insert(cur); }; try_pt(out.fi); try_pt(out.se); } } for (int sz = 1; sz <= N; ++sz) { string s(N, '0'); for (int i = 1; i <= sz; ++i) { s[N - i] = '1'; } do { if (!can.count(s)) { vector<int> ret; for (int i = 0; i < N; ++i) { if (s[i] == 1) ret.push_back(i); } } } while (next_permutation(s.begin(), s.end())); } return {}; } } signed main() { cin.tie(0)->sync_with_stdio(0); int T; cin >> T; while (T--) { vector<int> v = solve(); if (v.empty()) { cout << "YES\n"; } else { cout << "NO\n"; vector<int> ind(N, 0); for (int n : v) ind[n] = 1; for (int n : ind) cout << n << ' '; cout << '\n'; } } }
failed
96_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define int long long #define lson (pos << 1) #define rson (pos << 1 | 1) #define pii pair<int, int> #define fr first #define sc second #define mk make_pair #define pb push_back #define yjbakioi true #define inx(u) \ int I = h[(u)], v = edge[I].v; \ I; \ I = edge[I].nx, v = edge[I].v #define x1 lylakioi #define y1 gczakioi #define popcnt __builtin_popcount #define lb(x) (x & (-x)) #define LD long double int read() { int x = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } x = c - '0'; c = getchar(); while (c <= '9' && c >= '0') { x *= 10; x += c - '0'; c = getchar(); } return x * f; } void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int MAXN = 5010, MAXM = 200010, N = 16, base = 131, Mod = 998244353, inf = 1e18, B = 300; const LD eps = 1e-7; int Pow(int x, int y) { int rt = 1; while (y) { if (y & 1) rt = rt * x % Mod; x = x * x % Mod, y >>= 1; } return rt; } struct Edge { int v, nx, w; } edge[MAXN << 1]; int h[MAXN], CNT; void add_side(int u, int v) { edge[++CNT] = {v, h[u]}; h[u] = CNT; edge[++CNT] = {u, h[v]}; h[v] = CNT; } void add(int &x, int y) { x += y; if (x >= Mod) x -= Mod; if (x < 0) x += Mod; } int n, m, k, ans, a[MAXN], f[MAXN][MAXN][2], g[MAXN][MAXN]; void slv() { n = read(), m = read(); for (int i = 0; i < n; i++) a[i] = read(); memset(g, 0x3f, sizeof(g)); for (int i = 1; i <= m; i++) { int p = read(), d = read(), t = read(); if (a[p] * 2 + 1 >= n) continue; int l = ((p - a[p] - d) % n + n) % n, r = (p + a[p] - d + n) % n; g[l][r] = min(g[l][r], t); // cout<<l<<" "<<r<<" "<<t<<endl; } for (int o = 0; o < n; o++) for (int l = 0; l < n; l++) { int r = (l + o) % n; g[l][r] = min(g[l][r], min(g[(l + 1) % n][r], g[l][(r - 1 + n) % n])); // if(g[l][r]<inf)cout<<l<<" "<<r<<" "<<g[l][r]<<endl; } memset(f, 0x3f, sizeof(f)); for (int o = n - 1; o >= 0; o--) for (int l = 0; l < n; l++) { int r = (l + o) % n; f[l][r][0] = min(f[(l - 1 + n) % n][r][0], o == n - 1 ? inf : g[(r + 1) % n][(l - 1 + n) % n]) - 1; f[l][r][1] = min(f[l][(r + 1) % n][1], o == n - 1 ? inf : g[(r + 1) % n][(l - 1 + n) % n]) - 1; f[l][r][0] = max(f[l][r][0], f[l][r][1] - o); f[l][r][1] = max(f[l][r][1], f[l][r][0] - o); // if(f[l][r][0]<inf/2)cout<<l<<" "<<r<<" "<<0<<" "<<f[l][r][0]<<" // "<<(o==n-1?inf:g[(r+1)%n][(l-1+n)%n]-1)<<endl; // if(f[l][r][1]<inf/2)cout<<l<<" "<<r<<" "<<1<<" "<<f[l][r][1]<<" // "<<(o==n-1?inf:g[(r+1)%n][(l-1+n)%n]-1)<<endl; } for (int i = 0; i < n; i++) printf("%d", f[i][i][0] >= 0); puts(""); } signed main() { // freopen("1.in","r",stdin);freopen("1.out","w",stdout); int _ = read(); while (_--) slv(); cerr << clock() * 1.0 / CLOCKS_PER_SEC << "s" << endl; return 0; }
failed
77_3_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define pb push_back int main() { int tt; cin >> tt; while (tt--) { string s; cin >> s; int n = (int)s.size(); vector<int> vec, nw; for (int i = 0, num; i < n; i++) { num = s[i] - '0'; if ((int)vec.size() > 0 && vec.back() == num && num != 2) { vec.pop_back(); } else vec.pb(num); } int cnt = 0, mine = 0, from = 0; for (auto u : vec) { if (u != 2) from++; } int fut = 0; while ((int)vec.size() != 0) { int v = *vec.begin(); if (v == 2) { vec.erase(vec.begin()); fut++; continue; } break; } while ((int)vec.size() != 0) { int u = *vec.begin(); vec.erase(vec.begin()); if (u == 2) { cnt++; continue; } while (mine > from && cnt > 0) { mine--; cnt--; nw.pop_back(); } if (cnt > 0) { from--; cnt--; continue; } if ((int)nw.size() > 0 && nw.back() == u) { nw.pop_back(); from--; mine--; continue; } nw.pb(u); from--; mine++; } cout << max(0, (int)nw.size() - cnt - fut) << endl; } }
failed
48_1_wrong.cpp
#include <bits/stdc++.h> using i64 = long long; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int a, b, c; std::cin >> a >> b >> c; std::cout << c + 1 << "\n"; for (int i = 1; i <= c + 1; i++) { std::cout << i << " "; } return 0; }
failed
11_3_wrong.cpp
#include <algorithm> #include <cstdio> #include <cstring> #include <set> #include <vector> using namespace std; const int max1 = 3e5; int T, n; struct Data { int a, b, id; } A[max1 + 5], B[max1 + 5]; set<pair<int, int>> Set; bool Cmp1(const Data &A, const Data &B) { return A.a < B.a; } bool Cmp2(const Data &A, const Data &B) { return A.b < B.b; } bool vis[max1 + 5]; vector<pair<int, pair<int, int>>> ans; void Work() { scanf("%d", &n); n *= 3; for (int i = 1; i <= n; i++) scanf("%d%d", &A[i].a, &A[i].b), A[i].id = i, B[i] = A[i]; sort(A + 1, A + 1 + n, Cmp1); sort(B + 1, B + 1 + n, Cmp2); Set.clear(); memset(vis, 0, sizeof(bool) * (n + 1)); ans.clear(); int now = n; for (int i = n; i >= 1; i--) { if (vis[i]) continue; ans.emplace_back(); ans.back().first = A[i].id; vis[A[i].id] = true; while (now >= 1 && B[now].b >= A[i].a) Set.insert(make_pair(B[now].a, B[now].id)), --now; while (!Set.empty() && vis[(--Set.end())->second]) Set.erase(--Set.end()); if (!Set.empty()) { vis[(--Set.end())->second] = true; ans.back().second.first = (--Set.end())->second; } else { printf("No\n"); return; } while (!Set.empty() && vis[(--Set.end())->second]) Set.erase(--Set.end()); if (!Set.empty()) { vis[(--Set.end())->second] = true; ans.back().second.second = (--Set.end())->second; } else { printf("No\n"); return; } } printf("Yes\n"); for (auto v : ans) printf("%d %d %d\n", v.first, v.second.first, v.second.second); return; } int main() { scanf("%d", &T); while (T--) Work(); return 0; }
failed
13_3_wrong.cpp
#include <bits/stdc++.h> #ifdef LOCAL #include "dd/debug.h" #else #define dbg(...) 42 #define dbg_proj(...) 420 #define dbg_rproj(...) 420420 void nline() {} void bar() {} void start_clock() {} void end_clock() {} #endif namespace rs = std::ranges; namespace rv = std::views; using u32 = unsigned int; using i64 = long long; using u64 = unsigned long long; template <class T> constexpr T INF = T{}; template <std::floating_point T> constexpr T INF<T> = std::numeric_limits<T>::infinity(); template <> constexpr int INF<int> = 0x3f3f3f3f; // 1061109567 template <> constexpr long long INF<long long> = 0x3f3f3f3f3f3f3f3fLL; // 4557430888798830399 namespace seg { template <class Node> concept SegNode = requires(Node n) { typename Node::Output; Node(); requires std::same_as < std::decay_t<decltype(Node::ID)>, typename Node::Output > ; { Node::comb(Node::ID, Node::ID) } -> std::same_as<typename Node::Output>; { std::as_const(n).value() } -> std::same_as<typename Node::Output>; { n.pull(n, n) } -> std::same_as<void>; }; template <SegNode Node> struct Seg { using Output = typename Node::Output; int sz{}; std::vector<Node> val; Seg() = default; explicit Seg(int _sz) : sz(_sz), val(2 * sz) {} // explicit Seg(int _sz) // : sz(static_cast<int>(std::bit_ceil<uint32_t>(_sz))), // val(2 * sz) {} template <std::ranges::sized_range Container> requires requires(Container c) { Node(*std::ranges::begin(c)); } explicit Seg(const Container &c) : Seg(static_cast<int>(std::ranges::size(c))) { int i = sz; for (const auto &v : c) { val[i++] = Node(v); } build(0, sz - 1); } void pull(int ind) { val[ind].pull(val[2 * ind], val[2 * ind + 1]); } void build(int l, int r) { l += sz, r += sz; l /= 2, r /= 2; for (int k = 2; l >= 1; l /= 2, r /= 2, k *= 2) { for (int i = l; i <= r; ++i) { val[i].pull(val[2 * i], val[2 * i + 1]); } } } template <class F, class... Args> requires std::invocable<F, Node &, const Args &...> void upd(const F &f, int i, const Args &...args) { std::invoke(f, val[i += sz], args...); for (i /= 2; i >= 1; i /= 2) { pull(i); } } Output query(int l, int r) { Output vl = Node::ID, vr = Node::ID; for (l += sz, r += sz; l <= r; l /= 2, r /= 2) { if (l % 2 == 1) { vl = Node::comb(vl, val[l++].value()); } if (r % 2 == 0) { vr = Node::comb(val[r--].value(), vr); } } return Node::comb(vl, vr); } }; } // namespace seg using seg::Seg; template <class T> struct Node { using Output = T; static constexpr Output ID{INF<T>}; T val{}; Node() = default; explicit Node(T v) : val(v) {} static Output comb(Output a, Output b) { return std::min(a, b); } Output value() const { return val; } void add(const T &v) { val += v; } void set(const T &v) { val = v; } void pull(const Node &cl, const Node &cr) { val = comb(cl.val, cr.val); } }; struct Perm { int x; bool fl; // not fl: go to left: 1, 2, 3 -> 2, 3, 1 // fl: go to right: 1, 2, 3 -> 3, 1, 2 int transform(int v) const { if (v < x) { if (v % 2 == x % 2) { return v + 1; } return v - 1; } if (v > x + 2) { if (v % 2 == x % 2) { return v - 1; } return v + 1; } if (!fl) { if (v < x + 2) { return v + 1; } return v - 2; } if (v > x) { return v - 1; } return v + 2; } }; void tc() { int n, k; std::cin >> n >> k; std::vector<int> p(n); for (int &v : p) { std::cin >> v; --v; } if ((n % 2 == 0 && k > 1) || (n % 2 == 1 && k > n - 1)) { std::cout << "-1\n"; return; } std::vector<int> pos(n); for (int i = 0; i < n; ++i) { pos[p[i]] = i; } if (n % 2 == 0) { for (int i = 0; i < n; i += 2) { std::swap(p[pos[i]], p[pos[i + 1]]); } for (int i : p) { std::cout << i + 1 << ' '; } std::cout << '\n'; return; } Seg<Node<int>> seg(pos); std::vector<Perm> pe; pe.reserve(n - 1); for (int i = 0; i < n - 2; i += 2) { pe.emplace_back(i, false); pe.emplace_back(i, true); } rs::nth_element( pe, pe.begin() + k - 1, [&](const Perm &p1, const Perm &p2) -> bool { std::vector<int> inds{p1.x, p1.x + 1, p1.x + 2, p2.x, p2.x + 1, p2.x + 2, seg.query(std::min(p1.x + 2, p2.x + 2) + 1, std::max(p1.x, p2.x) - 1)}; rs::sort(inds); inds.erase(rs::unique(inds).begin(), inds.end()); if (inds.back() == INF<int>) { inds.pop_back(); } for (int i : inds) { int v1 = p1.transform(p[i]); int v2 = p2.transform(p[i]); if (v1 < v2) { return true; } if (v1 > v2) { return false; } } assert(false); }); Perm ans = pe[k - 1]; dbg(ans.x, ans.fl); for (int i : p) { std::cout << ans.transform(i) + 1 << ' '; } std::cout << '\n'; } int main() { #ifndef LOCAL std::ios::sync_with_stdio(false); std::cin.tie(nullptr); #endif int t; std::cin >> t; for (int i = 0; i < t; ++i) { dbg(i + 1); tc(); bar(); } }
failed
3_2_wrong.cpp
#include <algorithm> #include <iostream> #include <vector> using namespace std; long long minCrossingTime(int n, int c, vector<long long> &times) { sort(times.begin(), times.end()); long long totalTime = 0; int i = n - 1; // Start from the slowest walker while (i >= 0) { if (i + 1 <= c) { // All remaining walkers can cross together totalTime += times[i]; break; } else { // Strategy: send the two slowest walkers together, then have the fastest // return long long fastest = times[0]; long long secondFastest = times[1]; long long slowest = times[i]; long long secondSlowest = times[i - 1]; // Option 1: fastest escorts slowest, then fastest escorts second slowest long long option1 = fastest + slowest + fastest + secondSlowest; // Option 2: two fastest cross, fastest returns, two slowest cross, second // fastest returns long long option2 = secondFastest + fastest + slowest + secondFastest; totalTime += min(option1, option2); i -= 2; // Two walkers have crossed } } return totalTime; } int main() { int n, c; cin >> n >> c; vector<long long> times(n); for (int i = 0; i < n; i++) { cin >> times[i]; } cout << minCrossingTime(n, c, times) << endl; return 0; }
failed
117_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; #define forr(i, a, b) for (ll i = (ll)a; i < (ll)b; i++) #define forn(i, n) forr(i, 0, n) #define dforr(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define dforn(i, n) for (int i = n - 1; i >= 0; i--) #define pb push_back #define fst first #define snd second #define ln '\n' #define sz(c) ((int)c.size()) #define zero(v) memset(v, 0, sizeof(v)) 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> vll; const ll MOD = 1e9 + 7; const ll MAXN = 305000; const ll INF = 9e18; const ld EPS = 1e-9; #define BK 600 int n; int cant[MAXN], lleno[MAXN]; int maxCant[650], maxLibre[650]; int siguiente(int idx) { int bloq = idx / BK; int i = idx; int canti = cant[idx]; for (i++; i < (bloq + 1) * BK && i < n; i++) { if (cant[i] > canti && lleno[i] < cant[i]) { return i; } canti = max(canti, cant[i]); // i++; } for (bloq++; bloq * BK < n; bloq++) { if (maxLibre[bloq] > canti) break; canti = max(canti, maxCant[bloq]); // bloq++; } if (bloq * BK >= n) return -1; // i = bloq*BK; for (i = bloq * BK; true; i++) { if (cant[i] > canti && lleno[i] < cant[i]) return i; canti = max(canti, cant[i]); } } void actu(int bloq) { int maxL = 0; int maxC = 0; for (int i = bloq * BK; i < (bloq + 1) * BK && i < n; i++) { if (cant[i] > maxC && lleno[i] < cant[i]) { maxL = max(maxL, cant[i]); } maxC = max(maxC, cant[i]); } maxLibre[bloq] = maxL; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); // freopen("input.txt","r",stdin); zero(lleno); int q; cin >> n >> q; forn(i, n) { cin >> cant[i]; maxCant[i / BK] = max(maxCant[i / BK], cant[i]); } for (int i = 0; i * BK < n; i++) maxLibre[i * BK] = maxCant[i * BK]; while (q--) { char op; cin >> op; if (op == '?') { int idx; cin >> idx; idx--; cout << lleno[idx] << ln; } else { int l, x; cin >> l >> x; l--; while (l >= 0 && lleno[l] + x >= cant[l]) { x -= cant[l] - lleno[l]; lleno[l] = cant[l]; actu(l / BK); l = siguiente(l); } if (l >= 0) lleno[l] += x; } } }
failed
62_2_wrong.cpp
#pragma GCC optimize("Ofast") #pragma GCC target("popcnt", "sse3", "sse2", "sse", "avx", "sse4", "sse4.1", \ "sse4.2", "ssse3", "f16c", "fma", "avx2", "xop", "fma4") #pragma GCC optimize("inline", "fast-math", "unroll-loops", \ "no-stack-protector") #include "bits/stdc++.h" using namespace std; using ll = unsigned long long; const ll p = 998244353; const int N = 2e5 + 5; ll s[N], mi[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); int n, i, j, k; cin >> n; mi[0] = 1; for (i = 1; i <= n; i++) mi[i] = mi[i - 1] * 2 % p; s[0] = 1; s[n / 2 + 1] = 1; for (i = n / 2 + 1; i < n; i++) s[i + 1] = s[i] * 2 % p; k = n; for (i = n / 2; i > n / 3; i--) { int L = i * 2 + 1; //[2n/3,n] int R = n / 2 + i; //[2n/3,n] if (R < n) { int len = min(n, R) / 2 - max(i, R + 1 - n / 2) + 1; if (len) s[R + 1] = ((s[R + 1] + 1) % p * mi[len] + p - 1) % p; } for (j = n; j > R; j--) { s[j] = (s[j] * 2 + s[j - i]); } for (j = L - 1; j > n - i; j--) s[j] <<= 1; s[i] = 1; if ((i & 15) == 1) { for (j = n - i + 1; j <= n; j++) s[j] %= p; } } int R = n / 2 + i; while (R >= i * 2 + 2) { if (R < n) { int len = min(n, R) / 2 - max(i + 1, R + 1 - n / 2) + 1; if (len) s[R + 1] = ((s[R + 1] + 1) % p * mi[len] + p - 1) % p; } --R; } for (i = n / 3; i > n / 4; i--) { int L = n - i + 1; for (j = n; j >= L; j--) { s[j] = (s[j] * 2 + s[j - i]); } for (j = n - i; j > i * 2; j--) { (s[j] += s[j - i]); } s[i] = 1; if ((i & 15) == 1) { for (j = min(L, i * 4); j <= n; j++) s[j] %= p; } } for (i = n / 4; i > 10; i--) { int L = n - i + 1; for (j = n; j >= L; j--) { s[j] = (s[j] * 2 + s[j - i]); } int LL = i * 3 + 1; for (j = n - i; j >= LL; j--) { (s[j] += s[j - i]); } for (j = i * 3; j > i * 2; j--) ++s[j]; s[i] = 1; if ((i & 15) == 1) { for (j = min(L, i * 3); j <= n; j++) s[j] %= p; } } for (i = min(n / 4, 10); i; i--) { int L = n - i + 1; for (j = n; j >= L; j--) { s[j] = (s[j] * 2 + s[j - i]); } for (j = n - i; j > i * 3; j--) { (s[j] += s[j - i]); } for (j = i * 3; j > i * 2; j--) ++s[j]; s[i] = 1; if ((i & 15) == 1) { for (j = min(L, i * 4); j <= n; j++) s[j] %= p; } } // for (ll &x : s) x %= p; // cout << reduce(s, s + n + 1, 0llu) << endl; for (i = 0; i <= n; i++) cout << s[i] << " \n"[i == n]; }
failed
21_2_wrong.cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; #define lll __int128_t #define uint uint32_t // 注意叉乘是否会爆ll // 注意凸包的最后一个点都是第一个点 // 注意凸包都是逆时针的 // 注意每次选凸包基准点都是x最小然后y最大 // 先按x小排序再按y大排序,后面第二关键字是不能省的因为x相同时y最小/最大的点有可能会被叉积=0pop掉 template <class T> struct vec { T x, y; vec(T _x = 0, T _y = 0) : x(_x), y(_y){}; int qx() { // 四一二三排序,按需修改 if (x >= 0 && y >= 0) return 2; if (x < 0 && y >= 0) return 3; if (x < 0 && y < 0) return 4; if (x >= 0 && y < 0) return 1; return -1; } void print() { cerr << x << ' ' << y << '\n'; } bool operator<(vec a) { if (qx() != a.qx()) return qx() < a.qx(); else { T p = x * a.y, q = y * a.x; if (p != q) return p > q; else if (a.y == 0) return abs(x) > abs(a.x); else return abs(y) > abs(a.y); } } T len2() { return x * x + y * y; } vec operator*(T k) { return {x * k, y * k}; } vec operator+(vec a) { return {x + a.x, y + a.y}; } vec operator-(vec a) { return {x - a.x, y - a.y}; } T operator*(vec a) { return x * a.x + y * a.y; } T operator^(vec a) { return x * a.y - y * a.x; } bool operator==(vec a) { return x == a.x && y == a.y; } bool operator!=(vec a) { return (x != a.x || y != a.y); } }; template <class T> bool check_on_line_dir(vec<T> p, vec<T> q) { vec<__int128_t> p1 = {p.x, p.y}; vec<__int128_t> q1 = {q.x, q.y}; if ((p1 * q1) >= 0 && (p1 ^ q1) == 0) return true; return false; } template <class T> vector<vec<T>> build_convex( vector<vec<T>> pot) { // 输入点集输出凸包(最后一个点和第一个点是一个点) sort(pot.begin(), pot.end(), [&](vec<T> a, vec<T> b) { if (a.x != b.x) return a.x < b.x; return a.y > b.y; }); int n = pot.size(); int lo = 0; vector<vec<T>> res; for (int i = 0; i < n; i++) { while (lo >= 2 && ((res[lo - 1] - res[lo - 2]) ^ (pot[i] - res[lo - 1])) <= 0) lo--, res.pop_back(); res.push_back(pot[i]), lo++; } int pre = lo; for (int i = n - 2; i >= 0; i--) { while (lo >= pre + 1 && ((res[lo - 1] - res[lo - 2]) ^ (pot[i] - res[lo - 1])) <= 0) lo--, res.pop_back(); res.push_back(pot[i]), lo++; } return res; } template <class T> double convex_peri(vector<vec<T>> convex) { // 输入凸包(最后一个点和第一个点是一个点),输出凸包长度 double len = 0; int sz = convex.size(); for (int i = 1; i <= sz - 1; i++) { len += sqrt((convex[i] - convex[i - 1]).len2()); } return len; } template <class T> ll convex_area( vector<vec<T>> convex) { // 输入凸包(最后一个点和第一个点是一个点),输出凸包面积 T area = 0; int sz = convex.size(); for (int i = 0; i < sz - 2; i++) { area += abs((convex[0] - convex[i + 1]) ^ (convex[0] - convex[i + 2])); } return area; } // https://tsinghua.contest.codeforces.com/group/3T118PLOiT/contest/544779/problem/I template <class T> void reorder( vector<vec<T>> & p) { // 将凸包变成先x最小再y最小,输入需要满足(最后一个点和第一个点是一个点) p.pop_back(); rotate(p.begin(), min_element(p.begin(), p.end(), [&](vec<T> a, vec<T> b) { return make_pair(a.x, -a.y) < make_pair(b.x, -b.y); }), p.end()); p.push_back(p[0]); } template <class T> vector<vec<T>> mincowsky_sum( vector<vec<T>> a, vector<vec<T>> b) { // 传入的是两个已经建好的且为逆时针的凸包(最后一个点和第一个点是一个点)//注意输出的凸包的相邻两个边可能是平行的 reorder(a), reorder(b); // 让a和b的第0个点是x最小然后y最大 vector<vec<T>> res; vec<T> st = a[0] + b[0]; int s1 = a.size(), s2 = b.size(); int l1 = 0, l2 = 0; res.push_back(st); while (l1 != s1 - 1 || l2 != s2 - 1) { if (l1 == s1 - 1 || (l2 != s2 - 1 && b[l2 + 1] - b[l2] < a[l1 + 1] - a[l1])) { st = st + (b[l2 + 1] - b[l2]); l2++; } else { st = st + (a[l1 + 1] - a[l1]); l1++; } res.push_back(st); } return res; } template <class T> struct line { vec<T> k, b; // k为方向,b为线上任意一个点,射线的话b为端点 line(vec<T> dir, vec<T> pot) : k(dir), b(pot) {} }; template <class T> vec<double> getnode(line<T> p, line<T> q) { assert((p.k ^ q.k) != 0); double t = (double)((p.b - q.b) ^ q.k) / (q.k ^ p.k); vec<double> ans; ans.x = p.b.x + p.k.x * t; ans.y = p.b.y + p.k.y * t; return ans; }; template <class T> struct convex_hull_trick { // 静态凸包求单点值 vector<line<T>> convex; bool _mo; convex_hull_trick(vector<line<T>> line_all, int mo) { // mo=1求极大,mo=0求极小 _mo = mo; for (auto &[k, b] : line_all) { assert(k.x); if (k.x < 0) { k.x = -k.x, k.y = -k.y; } } line<T> line_y(vec<T>(0, 1), vec<T>(0, 0)); if (mo == 0) sort(line_all.begin(), line_all.end(), [&](line<T> a, line<T> b) { if (!check_on_line_dir(a.k, b.k)) return b.k < a.k; else return getnode(line_y, a).y < getnode(line_y, b).y; }); else sort(line_all.begin(), line_all.end(), [&](line<T> a, line<T> b) { if (!check_on_line_dir(a.k, b.k)) return a.k < b.k; else return getnode(line_y, a).y > getnode(line_y, b).y; }); int sz = line_all.size(); int lo = 0; for (int i = 0; i < sz; i++) { if (i == 0) { convex.push_back(line_all[i]); lo++; continue; } if (check_on_line_dir(line_all[i].k, line_all[i - 1].k)) continue; while (lo > 1) { if (getnode(convex[lo - 2], convex[lo - 1]).x >= getnode(convex[lo - 1], line_all[i]).x) { lo--; convex.pop_back(); } else break; } convex.push_back(line_all[i]); lo++; } } double get_y(double x) { int lo = 0, ro = (int)convex.size() - 1; while (lo < ro) { int mid = (lo + ro) >> 1; if (getnode(convex[mid], convex[mid + 1]).x <= x) lo = mid + 1; else ro = mid; } return convex[lo].b.y + convex[lo].k.y / convex[lo].k.x * (x - convex[lo].b.x); } }; const double eps = 1e-9; template <class T> double half_plane( vector<line<T>> line_all) { // 半平面交,目标面积必须在输入有向线段左边 vector<vec<double>> convex; line<T> line_y(vec<T>(0, 1), vec<T>(0, 0)); sort(line_all.begin(), line_all.end(), [&](line<T> a, line<T> b) { if (!check_on_line_dir(a.k, b.k)) return a.k < b.k; else return ((b.b - a.b) ^ a.k) > 0; }); int lo = 0, ro = -1; vector<line<T>> res; auto check = [&](line<T> l, line<T> mid, line<T> r) -> bool { // l,mid的交点在r左边 return ((getnode(l, mid) - vec<double>(r.b.x, r.b.y)) ^ vec<double>(r.k.x, r.k.y)) < eps; }; int sz = line_all.size(); for (int i = 0; i < sz; i++) { if (i == 0) { res.push_back(line_all[i]); ro++; continue; } if (check_on_line_dir(line_all[i].k, line_all[i - 1].k)) continue; while (lo < ro && !check(res[ro - 1], res[ro], line_all[i])) { res.pop_back(); ro--; } while (lo < ro && !check(res[lo], res[lo + 1], line_all[i])) lo++; res.push_back(line_all[i]); ro++; } while (lo < ro && !check(res[ro - 1], res[ro], res[lo])) ro--; if (ro - lo <= 1) return 0; if (!check(res[ro], res[lo], res[lo + 1])) return -1; for (int i = lo; i <= ro - 1; i++) convex.push_back(getnode(res[i], res[i + 1])); convex.push_back(getnode(res[ro], res[lo])); convex.push_back(convex[0]); return convex_area(convex); } const ll M = 1000000007; void solve() { int n; cin >> n; vector<vec<ll>> pot(n); for (int i = 0; i < n; i++) { cin >> pot[i].x >> pot[i].y; } vector<vec<ll>> conv = build_convex(pot); ll area = convex_area(conv); ll ans = 0; int sz = conv.size(); auto ope = [&](vec<ll> mid) -> void { auto cp = conv; auto pp = pot; for (auto &x : pp) x = x - mid; for (auto &x : cp) x = x - mid; int sz = conv.size(); vector<vec<ll>> pz; sort(pp.begin(), pp.end()); map<pair<ll, ll>, int> mp; map<int, vec<ll>> mz; int tot = 0; for (int i = 0; i < n; i++) { if (pp[i] != vec<ll>(0, 0)) { mp[{pp[i].x, pp[i].y}] = tot++; mz[tot - 1] = {pp[i].x, pp[i].y}; // cerr<<cp[i].x<<' '<<cp[i] } } for (int i = 0; i < sz - 1; i++) { auto l = cp[i], r = cp[i + 1]; int n1 = mp[{l.x, l.y}], n2 = mp[{r.x, r.y}]; vector<vec<ll>> pt; for (int j = n1;; j = (j + 1) % tot) { pt.push_back(mz[j]); if (j == n2) break; } int sz = pt.size(); ans = (ans + (sz - 1) * (area - abs(l ^ r) % M) % M) % M; { vector<vec<ll>> stk; ll sum = 0; for (auto p : pt) { if (p == r) break; if (stk.empty()) stk.push_back(p); else { while (stk.size() > 1 && ((stk.back() - stk[stk.size() - 2]) ^ (p - stk.back())) < 0) { sum -= abs(stk.back() ^ stk[stk.size() - 2]); stk.pop_back(); } sum += abs(stk.back() ^ p); stk.push_back(p); } ans = (ans + sum) % M; } } { reverse(pt.begin(), pt.end()); vector<vec<ll>> stk; ll sum = 0; for (auto p : pt) { if (p == l) break; if (stk.empty()) stk.push_back(p); else { while (stk.size() > 1 && ((stk.back() - stk[stk.size() - 2]) ^ (p - stk.back())) > 0) { sum -= abs(stk.back() ^ stk[stk.size() - 2]); stk.pop_back(); } sum += abs(stk.back() ^ p); stk.push_back(p); } ans = (ans + sum) % M; } } } }; map<pair<ll, ll>, int> pz; for (auto [x, y] : conv) { pz[{x, y}] = 1; } for (int i = 0; i < n; i++) { if (pz[{pot[i].x, pot[i].y}] != 1) { ope(pot[i]); // cerr<<ans<<' '<<pot[i].x<<' '<<pot[i].y<<'\n'; } } cout << (ans % M + M) % M << '\n'; } signed main() { ios::sync_with_stdio(0), cin.tie(0); int _ = 1; // cin>>_; while (_--) { solve(); } }
failed
38_1_wrong.cpp