func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; deque<int> enumeration; deque<int> cutOrder; struct Edge { Edge(int from, int to, int ind) : from(from), to(to), ind(ind) {} int from, to, ind; shared_ptr<Edge> nxt = nullptr; shared_ptr<Edge> rev = nullptr; shared_ptr<Edge> flip = nullptr; }; void search(shared_ptr<Edge> e, int end) { if (e->flip == nullptr) { enumeration.push_back(e->from); if (e->to != end) { search(e->nxt, end); } } else { search(e->flip->rev->nxt, e->to); if (e->to != end) { search(e->nxt, end); } } if (e->to == end) { cutOrder.push_back(e->ind); } } auto hashf = [](pair<int, int> p) { return p.first + (p.second << 15); }; unordered_map<pair<int, int>, shared_ptr<Edge>, decltype(hashf)> edgeSet(1000, hashf); shared_ptr<Edge> addFlip(shared_ptr<Edge> e) { pair<int, int> p(e->from, e->to); shared_ptr<Edge> flip = edgeSet[p]; if (flip == nullptr) { edgeSet[p] = e; } else { assert(flip->flip == nullptr); e->flip = flip; flip->flip = e; } return e; } void addTriangle(int a, int b, int c, int i) { shared_ptr<Edge> e1, e2, e3, e1r, e2r, e3r; e1 = make_shared<Edge>(a, b, i); e2 = make_shared<Edge>(b, c, i); e3 = make_shared<Edge>(c, a, i); e1->nxt = e2; e2->nxt = e3; e3->nxt = e1; e1r = make_shared<Edge>(b, a, i); e2r = make_shared<Edge>(c, b, i); e3r = make_shared<Edge>(a, c, i); e1r->nxt = e3r; e2r->nxt = e1r; e3r->nxt = e2r; e1->rev = e1r; e2->rev = e2r; e3->rev = e3r; e1r->rev = e1; e2r->rev = e2; e3r->rev = e3; addFlip(e1); addFlip(e2); addFlip(e3); addFlip(e1r); addFlip(e2r); addFlip(e3r); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t, n; cin >> t; for (int caseNum = 0; caseNum < t; caseNum++) { cin >> n; enumeration.clear(); cutOrder.clear(); edgeSet.clear(); for (int i = 1; i <= n - 2; i++) { int a, b, c; cin >> a >> b >> c; addTriangle(a, b, c, i); } shared_ptr<Edge> e1 = edgeSet.begin()->second; search(e1, e1->from); for (auto e : enumeration) { cout << e << ; } cout << n ; for (auto e : cutOrder) { cout << e << ; } cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int slimes, values; vector<int> vec; cin >> slimes; for (int i = 0; i < slimes; i++) { vec.push_back(1); while (vec.size() > 1 && vec[vec.size() - 1] == vec[vec.size() - 2]) { vec.pop_back(); vec[vec.size() - 1] += 1; } } for (int j = 0; j < vec.size(); j++) { cout << vec[j] << ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, x; cin >> n >> x; long long a[n], b[n]; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; sort(a, a + n, greater<int>()); sort(b, b + n); long long ans = 0, f, id = 0; for (int i = 0; i < n; ++i) { f = 0; while (id < n) { if (a[i] + b[id] >= x) { f = 1; ++id; break; } ++id; } if (f) ++ans; else break; } cout << 1 << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, r, s1, s2, d; cin >> n; r = ceil(double(sqrt(n))); s1 = r * r; s2 = (r - 1) * (r - 1); d = s1 - s2; d = d / 2; d = s1 - d; if (n < d) { cout << (r * 2) - 1; } else { cout << r * 2; } }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const int maxn = 1e5 + 5; void Solution() { int n; cin >> n; cout << (n / 2) + 1 << n ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) Solution(); return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * f; } int n; int a[2000010]; int main() { while (~scanf( %d , &n)) { for (int i = 1; i <= n; i++) a[i] = i; for (int k = 2; k <= n; k++) { int ed = n + k - 1, rp = n / k + (n % k != 0); for (int t = k - 1 + (rp - 1) * k; t >= k - 1; t -= k) { swap(a[ed], a[t]); ed = t; } } for (int i = n; i <= 2 * n - 1; i++) printf( %d , a[i]); puts( ); } }
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-8; const long double INF = 1e12; const long double PI = acos(-1); struct L : array<complex<long double>, 2> { L(const complex<long double>& a, const complex<long double>& b) { at(0) = a; at(1) = b; } L() {} }; namespace std { bool operator<(const complex<long double>& a, const complex<long double>& b) { return !(abs((a.real()) - (b.real())) < EPS) ? a.real() < b.real() : a.imag() + EPS < b.imag(); } bool operator==(const complex<long double>& a, const complex<long double>& b) { return abs(a - b) < EPS; } } // namespace std long double dot(complex<long double> a, complex<long double> b) { return (conj(a) * b).real(); } long double cross(complex<long double> a, complex<long double> b) { return (conj(a) * b).imag(); } bool intersectLP(const L& l, const complex<long double>& p) { return abs(cross(l[1] - p, l[0] - p)) < EPS; } bool strictItsLS(const L& l, const L& s) { return cross(l[1] - l[0], s[0] - l[0]) * cross(l[1] - l[0], s[1] - l[0]) < -EPS; } bool isParallel(const complex<long double>& a, const complex<long double>& b) { return abs(cross(a, b)) < EPS; } bool isParallel(const L& a, const L& b) { return isParallel(a[1] - a[0], b[1] - b[0]); } complex<long double> crosspointLL(const L& l, const L& m) { long double A = cross(l[1] - l[0], m[1] - m[0]); long double B = cross(l[1] - l[0], l[1] - m[0]); return m[0] + B / A * (m[1] - m[0]); } long double getarea(const vector<complex<long double> >& poly) { long double ret = 0; for (int i = 0; i < (int)poly.size(); i++) { ret += cross(poly[i], poly[(i + 1) % poly.size()]); } return ret * 0.5; } bool in_corner(const complex<long double>& p, const vector<complex<long double> >& v) { bool s1 = cross(v[1] - v[0], p - v[0]) > -EPS; bool s2 = cross(v[2] - v[1], p - v[1]) > -EPS; return (cross(v[1] - v[0], v[2] - v[0]) > -EPS) ? s1 and s2 : s1 or s2; } long double solve(vector<complex<long double> >& v, L cut) { int n = v.size(); complex<long double> cdir = cut[1] - cut[0]; vector<complex<long double> > cps; for (int i = 0; i < n; i++) { L edge(v[i], v[(i + 1) % n]); if (strictItsLS(cut, edge) and !isParallel(cut, edge)) { cps.push_back(crosspointLL(cut, edge)); } if (intersectLP(cut, v[i])) { vector<complex<long double> > p{v[(i - 1 + n) % n], v[i], v[(i + 1) % n]}; if (in_corner(p[1] + cdir, p) != in_corner(p[1] - cdir, p)) { cps.push_back(v[i]); } } } sort(cps.begin(), cps.end()); long double res = 0; for (int i = 0; i < (int)cps.size() - 1; i += 2) { res += abs(cps[i + 1] - cps[i]); } return res; } int main() { int n, m; cin >> n >> m; vector<complex<long double> > v(n); for (int i = 0; i < n; i++) { long double x, y; cin >> x >> y; v[i] = complex<long double>(x, y); } if (getarea(v) < 0) { reverse(v.begin(), v.end()); } cout << fixed << setprecision(10); for (int i = 0; i < m; i++) { long double xs, ys, xt, yt; cin >> xs >> ys >> xt >> yt; cout << solve(v, L(complex<long double>(xs, ys), complex<long double>(xt, yt))) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int MAXX = 2e6 + 10; long long phi(long long n) { long long ret = n; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; ret -= ret / i; } } if (n > 1) ret -= ret / n; return ret; } int main() { long long n, k; scanf( %lld %lld , &n, &k); k = (k + 1) / 2; long long ans = n; while (k--) { ans = phi(ans); if (ans == 1) break; } printf( %lld , ans % mod); return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 7; const long long inf = 0x3f3f3f3f; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { double pi = 3.1415926535897932384626433832795028841971, n; cin >> n; long long sum = n / 2; double x = 0.5 / tan(pi / (n * 2)), y = 0.5 / sin(pi / (n * 2)); printf( %.10lf n , x * 2); } return 0; }
#include <bits/stdc++.h> using namespace std; bool comp(pair<long long, long long> a, pair<long long, long long> b) { return a.first > b.first; } long long findBest(vector<pair<long long, long long> > &ps, long long x) { long long res = 0; for (long long i = 0; i < ps.size(); i++) { long long num = ps[i].second; long long diff = ps[i].first; if (x - num >= 0) { res += (num * diff); x -= num; } else { res += (x * diff); break; } } return res; } int main() { long long n, s; cin >> n >> s; vector<pair<long long, long long> > ps; long long total = 0; long long positives = 0; long long pieces = 0; for (long long i = 0; i < n; i++) { long long num, a, b; cin >> num >> a >> b; ps.push_back(make_pair(a - b, num)); total += num * b; pieces += num; if (a - b > 0) { positives += num; } } sort(ps.begin(), ps.end(), comp); long long pizzas = (pieces % s == 0) ? (pieces / s) : (pieces / s + 1); long long extra = pizzas * s - pieces; long long best = total; if (positives > 0) { best = max(best, findBest(ps, positives / s * s) + total); if (positives % s != 0) { long long x = max(positives, (positives / s + 1) * s - extra); best = max(best, findBest(ps, x) + total); } } cout << best << endl; return 0; }
#include <bits/stdc++.h> using ll = long long; using namespace std; int n, k, a[110]; int main() { ios::sync_with_stdio(false); cin >> n >> k; for (int i = 0; i < k; ++i) cin >> a[i]; vector<int> s; for (int i = 1; i <= n; ++i) s.push_back(i); int curr_leader = 0; for (int i = 0; i < k; ++i) { int elim = (curr_leader + a[i]) % n; cout << s[elim] << ; s.erase(s.begin() + elim); curr_leader = elim; n--; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k; string s; cin >> k; vector<long long> cnt(10, 0); for (long long i = 0; i < 4; i++) { cin >> s; for (long long j = 0; s[j]; j++) { if (s[j] >= 0 && s[j] <= 9 ) cnt[s[j] - 0 ]++; } } for (long long i = 0; i < 10; i++) { if (cnt[i] > 2 * k) { cout << NO ; return; } } cout << YES ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1, n, a, b, m; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 2; long long gcd(long long a, long long b) { if (b > a) { return gcd(b, a); } if (b == 0) { return a; } return gcd(b, a % b); } long long expo(long long a, long long b, long long mod) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b = b >> 1; } return res; } long long mminvprime(long long a, long long b) { return expo(a, b - 2, b); } vector<long long> sieve(int n) { int* arr = new int[n + 1](); vector<long long> vect; for (int i = 2; i <= n; i++) if (arr[i] == 0) { vect.push_back(i); for (int j = 2 * i; j <= n; j += i) arr[j] = 1; } return vect; } long long mod_add(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } long long mod_mul(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } long long mod_sub(long long a, long long b, long long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } long long mod_div(long long a, long long b, long long m) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } int fact[N]; void precalc() { fact[0] = 1; for (int i = 1; i < N; i++) { fact[i] = mod_mul(fact[i - 1], i, 1000000007); } } long long mod(long long x) { return ((x % 1000000007 + 1000000007) % 1000000007); } long long inv(long long x) { return expo(x, 1000000007 - 2, 1000000007); } long long divide(long long a, long long b) { return mod_mul(a, inv(b), 1000000007); } long long nCr(long long n, long long r) { return divide(fact[n], mod_mul(fact[r], fact[n - r], 1000000007)); } long long computeXOR(long long n) { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { int a, b; cin >> a >> b; int pXor; if (a % 4 == 1) { pXor = a - 1; } else if (a % 4 == 2) { pXor = 1; } else if (a % 4 == 3) { pXor = a; } else { pXor = 0; } if (pXor == b) { cout << a << n ; } else if ((pXor ^ b) != a) { cout << a + 1 << n ; } else { cout << a + 2 << n ; } } }
#include <bits/stdc++.h> using namespace std; const int N = 200005; const int INF = 0x3f3f3f3f; int d, n, m; struct Sta { int x, p; Sta() {} Sta(int x, int p) : x(x), p(p) {} void read() { scanf( %d%d , &x, &p); } bool operator<(const Sta& c) const { return x < c.x; } } p[N]; struct Node { int Min, Minid; } node[N << 2]; void pushup(int x) { node[x].Min = min(node[(x << 1)].Min, node[(x << 1) | 1].Min); if (node[x].Min == node[(x << 1) | 1].Min) node[x].Minid = node[(x << 1) | 1].Minid; if (node[x].Min == node[(x << 1)].Min) node[x].Minid = node[(x << 1)].Minid; } void build(int L = 1, int R = m, int x = 1) { if (L == R) { node[x].Min = p[L].p; node[x].Minid = L; return; } int mid = (L + R) >> 1; build(L, mid, (x << 1)); build(mid + 1, R, (x << 1) | 1); pushup(x); } int Min, Minid; void getMin(int l, int r, int L = 1, int R = m, int x = 1) { if (L >= l && R <= r) { if (Min > node[x].Min) { Min = node[x].Min; Minid = node[x].Minid; } return; } int mid = (L + R) >> 1; if (l <= mid) getMin(l, r, L, mid, (x << 1)); if (r > mid) getMin(l, r, mid + 1, R, (x << 1) | 1); } long long solve() { int s = 0; long long ans = 0; int yu = 0; while (s < m) { int v = upper_bound(p + s + 1, p + m + 1, Sta(p[s].x + n, 0)) - p - 1; if (v == s) return -1; Min = INF; getMin(s + 1, v); if (Min > p[s].p) { ans += 1LL * (n - yu) * p[s].p; yu = n; yu -= p[Minid].x - p[s].x; s = Minid; } else { for (int j = s + 1; j <= Minid; j++) { if (p[j].p <= p[s].p) { int dis = p[j].x - p[s].x; if (yu >= dis) { yu -= dis; } else { ans += 1LL * (dis - yu) * p[s].p; yu = 0; } s = j; break; } } } } return ans; } int main() { scanf( %d%d%d , &d, &n, &m); p[0] = Sta(0, 0); for (int i = 1; i <= m; i++) p[i].read(); p[++m] = Sta(d, 0); sort(p, p + m + 1); build(); printf( %lld n , solve()); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k, ans = 0, odd = 0, ev = 0; cin >> n; int a[n + 1]; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] & 1) odd++; else ev++; } if (n == 1) { if (odd) cout << Yes ; else cout << No ; return 0; } if (a[0] % 2 == 0 || a[n - 1] % 2 == 0) { cout << No ; } else { if (n & 1) { if (odd >= 2) cout << Yes ; else cout << No ; } else { cout << No ; } } return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long n, long long m) { return m == 0 ? n : gcd(m, n % m); } long long lcm(long long n, long long m) { return (m * n) / gcd(n, m); } long long dx[] = {1, 0}; long long dy[] = {0, 1}; const long long inf = 1e9 + 10; const long long INF = 1e18; long long const N = 2e5 + 10; long long const M = 5100; double const PI = acos(-1); long long const MOD = 998244353; long long Check(long long n, long long pos) { return n & (1 << pos); } long long Set(long long n, long long pos) { return n |= (1 << pos); } long long n, m; vector<pair<long long, long long> > store; vector<long long> k; long long visit[N]; bool check(long long now) { memset(visit, 0, sizeof(visit)); pair<long long, long long> pt = {now, n}; auto it = upper_bound((store.begin()), (store.end()), pt); it--; long long pos = it - store.begin(); vector<pair<long long, long long> > apes; for (long long i = pos; i >= 0; i--) { long long day = store[i].first; long long type = store[i].second; if (visit[type] == 0) { apes.push_back({day, type}); visit[type] = 1; } } reverse((apes.begin()), (apes.end())); long long taka = 0; long long extra = 0; long long lastDay = 0; for (long long i = 0; i < (long long)apes.size(); i++) { long long day = apes[i].first; lastDay = day; long long type = apes[i].second; if (i == 0) taka += day; else taka += day - apes[i - 1].first; if (k[type] <= taka) taka -= k[type]; else { extra += k[type] - taka; taka = 0; } } taka += now - lastDay; for (long long i = 0; i < n; i++) { if (visit[i] == 0) extra += k[i]; } extra <<= 1; if (taka >= extra) return true; else return false; } int32_t main() { cin >> n >> m; k.resize(n); for (long long i = 0; i < n; i++) { cin >> k[i]; } for (long long i = 0; i < m; i++) { long long day, type; cin >> day >> type; type--; store.push_back({day, type}); } sort((store.begin()), (store.end())); long long res = 1; long long left = 1, right = 1e11; while (left <= right) { long long mid = (left + right) >> 1; if (check(mid)) { res = mid; right = mid - 1; } else { left = mid + 1; } } cout << res << n ; }
#include <bits/stdc++.h> using namespace std; bool dbg = 0; clock_t start_time = clock(); void bad(string mes = Impossible ) { cout << mes; exit(0); } void bad(int mes) { cout << mes; exit(0); } template <typename T> string bin(T x, int st = 2) { string ans = ; while (x > 0) { ans += char( 0 + x % st); x /= st; } reverse(ans.begin(), ans.end()); return ans.empty() ? 0 : ans; } template <typename T> void amax(T& x, T y) { x = max(x, y); } template <typename T> void amin(T& x, T y) { x = min(x, y); } template <typename T> T input() { T ans = 0, m = 1; char c = ; while (!((c >= 0 && c <= 9 ) || c == - )) { c = getchar(); } if (c == - ) m = -1, c = getchar(); while (c >= 0 && c <= 9 ) { ans = ans * 10 + (c - 0 ), c = getchar(); } return ans * m; } template <typename T> void read(T& a) { a = input<T>(); } template <typename T> void read(T& a, T& b) { read(a), read(b); } template <typename T> void read(T& a, T& b, T& c) { read(a, b), read(c); } template <typename T> void read(T& a, T& b, T& c, T& d) { read(a, b), read(c, d); } const int inf = 1e9 + 20; const short short_inf = 3e4 + 20; const long double eps = 1e-15; const int maxn = (int)1e5 + 12, base = 1e9 + 7; const long long llinf = 2e18 + 5; template <typename T> T binpow(T n, T second) { if (second <= 0) return 1LL; if (second % 2 == 0) { T b = binpow(n, second / 2); return (1LL * b * b) % base; } else { return (1LL * binpow(n, second - 1) * n) % base; } } vector<int> g[maxn], gr[maxn]; bool used[maxn]; int u[maxn]; vector<int> cmp, order; void dfs1(int v) { used[v] = 1; for (int to : g[v]) if (!used[to]) dfs1(to); order.push_back(v); } void dfs2(int v) { used[v] = 1; cmp.push_back(v); for (int to : gr[v]) if (!used[to]) dfs2(to); } int comp[maxn], sz[maxn]; int main() { int n, m, h; ios_base::sync_with_stdio(0); cin.tie(0); read(n, m, h); for (int i = 0; i < n; i++) { read(u[i]); } for (int i = 0; i < m; i++) { int a, b; read(a, b); a--, b--; if (u[a] + 1 == u[b] || (u[a] == h - 1 && u[b] == 0)) { g[a].push_back(b); gr[b].push_back(a); } swap(a, b); if (u[a] + 1 == u[b] || (u[a] == h - 1 && u[b] == 0)) { g[a].push_back(b); gr[b].push_back(a); } } memset(used, 0, sizeof(used)); for (int i = 0; i < n; i++) if (!used[i]) dfs1(i); memset(used, 0, sizeof(used)); for (int i = n - 1; i >= 0; i--) { int v = order[i]; if (!used[v]) { cmp.clear(); dfs2(v); for (int j : cmp) { comp[j] = v; sz[v]++; } } } for (int i = 0; i < n; i++) { for (int to : g[i]) if (comp[to] != comp[i]) { sz[comp[i]] = 0; } } int res = inf, ans; for (int i = 0; i < n; i++) { if (sz[i] > 0 && sz[i] < res) { res = sz[i]; ans = i; } } cout << res << n ; for (int i = 0; i < n; i++) if (comp[i] == ans) cout << i + 1 << ; return 0; }
#include <bits/stdc++.h> using namespace std; const int MX = 100010; int t[MX]; int main() { int n; cin >> n; for (int i = (1); i <= (n); ++i) { scanf( %d , &t[i]); } for (int i = (1); i <= (n); ++i) { printf( %d , t[i] + t[i + 1]); } }
#include <bits/stdc++.h> template <typename T> bool ckmax(T& a, const T& b) { return b > a ? a = b, 1 : 0; } template <typename T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } const int MV = 1e5 + 10; const int MM = 1e5 + 10; const int MG = 450; const int INF = 0x3f3f3f3f; struct DSU { int p[MM], t[MM]; char r[MM]; void init(int M) { memset(p, -1, M * sizeof *p); memset(r, 0, M * sizeof *r); std::iota(t, t + M, 0); } int find(int v) { return ~p[v] ? p[v] = find(p[v]) : v; } bool merge(int a, int b) { a = find(a), b = find(b); if (a == b) return 0; t[a] = t[b]; if (r[a] < r[b]) std::swap(a, b); r[a] += r[a] == r[b]; p[a] = b; } } dsu; int M, v[MM], maxv[MM], dp[MM][2], ub[MV]; bool ok[MM]; int solve() { scanf( %d , &M); for (int i = 0; i < M; ++i) { scanf( %d , v + i); assert(v[i] > 0); } for (int i = 0, j = 0; i < MV; ++i) { while (j < M && v[j] == i) ++j; ub[i] = j; } memset(maxv, 0x3f, sizeof maxv); std::fill(ok, ok + M, 1); int f = 0; bool c = 0; for (int i = 0; f == i; ++i, c ^= 1) { dsu.init(M + 1); for (int j = M - 1; j >= 0; --j) { if (ok[j] &= (maxv[j] > 0)) ckmax(f, i + 1); dp[j][c] = v[j] * 2 - maxv[j]; ckmax(dp[j][c], 0); ckmin(dp[j][c], v[j] - 1); if (i) { ckmax(dp[j][c], dp[j][!c]); int x = ub[dp[j][!c]], y = ub[dp[j][c]]; for (int g = dsu.find(x), t; (t = dsu.t[g]) < y;) { maxv[t] = v[j]; dsu.merge(g, t + 1); g = dsu.find(g); } } } memset(maxv, 0, sizeof maxv); } return f; } int main() { int N, f = 0; scanf( %d , &N); for (int i = 0; i < N; ++i) f ^= solve(); if (f) printf( YES n ); else printf( NO n ); return 0; }
#include <bits/stdc++.h> using namespace std; const int mx = 2e5 + 10; long long int x[mx], y[mx]; int ord[mx]; int hs[mx], h = 0; vector<int> g[mx]; int ans[mx]; int cross(int a, int o, int b) { long long int l = (x[o] - x[a]) * (y[o] - y[b]) * y[a] * x[b]; long long int r = (y[o] - y[a]) * (x[o] - x[b]) * x[a] * y[b]; if (l == r) return 0; if (l < r) return -1; return 1; } bool cmp1(int i, int j) { if (x[i] != x[j]) return x[i] < x[j]; if (y[i] != y[j]) return y[i] < y[j]; return i < j; } bool cmp(int i, int j) { int c = cross(i, ord[0], j); if (c == 0) { if (x[j] != x[i]) return x[j] < x[i]; return i < j; } return c > 0; } inline bool ccw(int a, int o, int b) { return (-cross(a, o, b)) >= 0; } int main() { int n, xi, yi; scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d %d , &xi, &yi); x[i] = xi, y[i] = yi, ord[i] = i; } sort(ord, ord + n, cmp1); int c = 0; int il = 0, id = 0; for (int i = 0; i < n;) { int ii = ord[i]; int xi = x[ii], yi = y[ii]; while (i < n && x[ord[i]] == xi && y[ord[i]] == yi) { g[ii].push_back(ord[i]); i++; } int j = ord[il]; if (xi > x[j] || (xi == x[j] && yi > y[j])) il = c; j = ord[id]; if (yi > y[j] || (yi == y[j] && xi > x[j])) id = c; ord[c++] = ii; } int dwn = ord[id]; swap(ord[0], ord[il]); sort(ord + 1, ord + c, cmp); for (int i = 0; i < c; i++) { while (h >= 2 && !ccw(hs[h - 2], hs[h - 1], ord[i])) h--; hs[h++] = ord[i]; if (ord[i] == dwn) break; } c = 0; for (int i = 0; i < h; i++) { int u = hs[i]; for (int j = 0; j < int(g[u].size()); j++) { ans[c++] = 1 + g[u][j]; } } sort(ans, ans + c); printf( %d , ans[0]); for (int i = 1; i < c; i++) printf( %d , ans[i]); printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int mod = 1e9 + 7; string S; cin >> S; bool flag = false; long ans = 0; long plus = 0; for (int i = 0; i < S.size(); i++) { if (S[i] == a ) { if (flag) { ans = (2 * ans + 1) % mod; } else { ans = ans + 1 + plus; ans = ans % mod; } flag = false; } else if (S[i] == b ) { plus = ans; flag = true; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int temp; int a[6] = {0}; int i = 0; while (n > 0) { a[i] = n % 2; n = n / 2; i++; } temp = a[0]; a[0] = a[4]; a[4] = temp; temp = a[2]; a[2] = a[3]; a[3] = temp; int count = 1; int sum = 0; for (int i = 0; i < 6; i++) { sum = sum + a[i] * count; count = count * 2; } cout << sum; }
#include <bits/stdc++.h> using namespace std; const int OO = 1e9; const double EPS = 1e-9; template <class T> void _db(const char* dbStr, T e) { cout << dbStr << = << e << endl; } template <class T, class... L> void _db(const char* dbStr, T e, L... r) { while (*dbStr != , ) cout << *dbStr++; cout << = << e << , ; _db(dbStr + 1, r...); } template <class S, class T> ostream& operator<<(ostream& o, const map<S, T>& v) { o << [ ; int i = 0; for (const pair<S, T>& pr : v) o << (!i++ ? : , ) << { << pr.first << : << pr.second << } ; return o << ] ; } template <template <class, class...> class S, class T, class... L> ostream& operator<<(ostream& o, const S<T, L...>& v) { o << [ ; int i = 0; for (const auto& e : v) o << (!i++ ? : , ) << e; return o << ] ; } template <class S, class T> ostream& operator<<(ostream& o, const pair<S, T>& pr) { return o << ( << pr.first << , << pr.second << ) ; } ostream& operator<<(ostream& o, const string& s) { for (const char& c : s) o << c; return o; } template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using VVV = VV<V<T>>; using ll = long long; using pii = pair<int, int>; using vi = V<int>; using vii = V<pii>; using vvi = VV<int>; using mii = map<int, int>; using umii = unordered_map<int, int>; using si = set<int>; using usi = unordered_set<int>; vii monoStFunc(vi a) { int n = int(a.size()); vii b(n); stack<pii> st; st.push(make_pair(INT_MAX, -1)); for (int i = 0; i < n; ++i) { while (st.top().first < a[i]) st.pop(); b[i].first = st.top().second; st.push(make_pair(a[i], i)); } while (!st.empty()) st.pop(); st.push(make_pair(INT_MAX, n)); for (int i = n - 1; i >= 0; --i) { while (st.top().first <= a[i]) st.pop(); b[i].second = st.top().second; st.push(make_pair(a[i], i)); } return b; } int n, q; vi a, d; int main() { ios::sync_with_stdio(false); cout.precision(10); cin.tie(0); cin >> n >> q; a.assign(n, 0); for (int i = 0; i < n; ++i) cin >> a[i]; d.assign(n - 1, 0); for (int i = 0; i < n - 1; ++i) d[i] = abs(a[i + 1] - a[i]); true; for (int qq = 0; qq < q; ++qq) { int l, r; cin >> l >> r; --l, --r; vi v; for (int i = l; i < r; ++i) v.push_back(d[i]); vii LR = monoStFunc(v); ll ans = 0; for (int i = 0; i < int(v.size()); ++i) ans += v[i] * 1LL * ((i - LR[i].first) * 1LL * (LR[i].second - i)); cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int gi() { char cc = getchar(); int cn = 0, flus = 1; while (cc < 0 || cc > 9 ) { if (cc == - ) flus = -flus; cc = getchar(); } while (cc >= 0 && cc <= 9 ) cn = cn * 10 + cc - 0 , cc = getchar(); return cn * flus; } const int N = 3e5 + 5; int n, q, a[N], g[20], f[N][20]; signed main() { n = gi(), q = gi(); for (register int i = (1); i <= (n); ++i) a[i] = gi(); for (register int i = (0); i <= (19); ++i) g[i] = n + 1; memset(f, 63, sizeof(f)); for (register int i = n; i >= 1; --i) { for (register int j = (0); j <= (19); ++j) if ((1 << j) & a[i]) f[i][j] = i; for (register int j = (0); j <= (19); ++j) if ((1 << j) & a[i]) for (register int k = (0); k <= (19); ++k) f[i][k] = min(f[i][k], f[g[j]][k]); for (register int j = (0); j <= (19); ++j) if ((1 << j) & a[i]) g[j] = i; } while (q--) { int x = gi(), y = gi(); int flag = 0; for (register int j = (0); j <= (19); ++j) if ((1 << j) & a[y]) flag |= (f[x][j] <= y); (flag) ? puts( Shi ) : puts( Fou ); } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; const long long LLINF = 9223372036854775807; int main() { int n; cin >> n; long long mod = 1000000009; if (n == 2) cout << 10, exit(0); long long b = 5, g = 1, r = 6; long long fb = 2, fg = 2, t = 4; for (int i = 0; i < n / 2 - 2; ++i) { b = r + 3 * g * (t + 1); g = (g * (1 + t)) % mod; r = (b + g) % mod; fb = (t + fg * 2) % mod; fg = (fg * 2) % mod; t = (fg + fb) % mod; } long long ans = ((r * r + 1) * 2) % mod; cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = int(500 * 1000 + 7); long long n, k; vector<pair<long long, long long> > adj[N]; long long dp[N][2]; void dfs(int v, int p = -1) { vector<long long> x; long long curr = 0; for (auto it : adj[v]) { long long to = it.first; long long w = it.second; if (to == p) continue; dfs(to, v); curr += dp[to][0]; x.push_back(dp[to][1] + w - dp[to][0]); } sort(x.begin(), x.end(), greater<long long>()); for (long long i = 0; i < min((long long)x.size(), k); i++) { if (x[i] > 0) curr += x[i]; } dp[v][0] = dp[v][1] = curr; if (k <= (long long)x.size() && x[k - 1] > 0) dp[v][1] -= x[k - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long q; cin >> q; while (q--) { cin >> n >> k; for (long long i = 0; i < n; i++) adj[i].clear(); for (long long i = 0; i < n - 1; i++) { long long u, v, w; cin >> u >> v >> w; u--; v--; adj[u].push_back({v, w}); adj[v].push_back({u, w}); } dfs(0, -1); cout << dp[0][0] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1010; struct Point { double x, y; Point(double _ = 0, double __ = 0) { x = _, y = __; } bool operator<(const Point &_) const { if (fabs(x - _.x) > 1e-10) return x < _.x; return y < _.y; } } po[N], rec[N * 2], stk[N * 2]; struct Line { Point a, b; } L; struct Mem { Point a; int type; bool operator<(const Mem &_) const { if (fabs(a.x - _.a.x) > 1e-10) return a.x < _.a.x; if (fabs(a.y - _.a.y) > 1e-10) return a.y < _.a.y; return type < _.type; } } pp[N * 2]; int n, m; inline double xmul(Point a, Point b, Point c) { return (a.x - b.x) * (a.y - c.y) - (a.y - b.y) * (a.x - c.x); } inline bool across(Point &a, Point &b, Point &c, Point &d) { double p1 = xmul(a, b, c), p2 = xmul(a, b, d); if (fabs(p1) < 1e-10 || fabs(p2) < 1e-10) return 1; if (p1 * p2 < 1e-10) return 1; return 0; } inline bool oneline(Point &a, Point &b, Point &c, Point &d) { double p1 = xmul(a, b, c), p2 = xmul(a, b, d); if (fabs(p1) < 1e-10 && fabs(p2) < 1e-10) return 1; return 0; } inline Point intersection(Line &u, Line &v) { Point ret = u.a; double t = ((u.a.x - v.a.x) * (v.a.y - v.b.y) - (u.a.y - v.a.y) * (v.a.x - v.b.x)) / ((u.a.x - u.b.x) * (v.a.y - v.b.y) - (u.a.y - u.b.y) * (v.a.x - v.b.x)); ret.x += (u.b.x - u.a.x) * t; ret.y += (u.b.y - u.a.y) * t; return ret; } inline double dis(Point &a, Point &b) { return sqrt(((a.x - b.x) * (a.x - b.x)) + ((a.y - b.y) * (a.y - b.y))); } inline bool onseg(Point &a, Point &b, Point &c) { if (fabs(xmul(a, b, c)) < 1e-10) return 1; return 0; } inline double getans() { Point a, b; int tot = 0, cnt = 0, top = 0; double ans = 0; for (int i = 1; i <= n; i++) { if (oneline(L.a, L.b, po[i], po[i + 1])) { cnt++; pp[cnt].a = po[i]; pp[cnt].type = 1; cnt++; pp[cnt].a = po[i + 1]; pp[cnt].type = -1; if (pp[cnt].a < pp[cnt - 1].a) swap(pp[cnt].a, pp[cnt - 1].a); continue; } int k1 = onseg(L.a, L.b, po[i]) || xmul(L.a, L.b, po[i]) < 0; int k2 = onseg(L.a, L.b, po[i + 1]) || xmul(L.a, L.b, po[i + 1]) < 0; if (k1 + k2 != 1) continue; if (onseg(L.a, L.b, po[i])) { rec[++tot] = po[i]; continue; } if (onseg(L.a, L.b, po[i + 1])) { rec[++tot] = po[i + 1]; continue; } if (across(L.a, L.b, po[i], po[i + 1])) { Line tmp; tmp.a = po[i], tmp.b = po[i + 1]; rec[++tot] = intersection(L, tmp); } } sort(rec + 1, rec + tot + 1); for (int i = 1; i < tot; i += 2) { cnt++; pp[cnt].a = rec[i]; pp[cnt].type = 1; cnt++; pp[cnt].a = rec[i + 1]; pp[cnt].type = -1; } sort(pp + 1, pp + cnt + 1); for (int i = 1; i <= cnt; i++) { if (pp[i].type == 1) stk[++top] = pp[i].a; else { if (top == 1) ans += dis(stk[top], pp[i].a); top--; } } return ans; } int main() { scanf( %d %d , &n, &m); for (int i = 1; i <= n; i++) scanf( %lf%lf , &po[i].x, &po[i].y); po[n + 1] = po[1]; for (int i = 1; i <= m; i++) { scanf( %lf%lf%lf%lf , &L.a.x, &L.a.y, &L.b.x, &L.b.y); printf( %.8lf n , getans()); } return 0; }
#include <bits/stdc++.h> bool vis[100000 + 3]; int pr[20000]; int h; int a[300]; int n; int endp[200000], next[200000], cap[200000]; int gra[300], cur[300], dist[300], prev[300]; int cnt[300]; int ecnt; char buf[10000], *bend; char out[10000], *oend; void add_edge(int u, int v, int c) { endp[++ecnt] = v; cap[ecnt] = c; next[ecnt] = gra[u]; gra[u] = ecnt; endp[++ecnt] = u; cap[ecnt] = 0; next[ecnt] = gra[v]; gra[v] = ecnt; } int isap(int vcnt, int S_, int T_) { memmove(cur, gra, sizeof(gra)); memset(cnt, 0, sizeof(cnt)); memset(dist, 0, sizeof(dist)); prev[S_] = S_; int ret = 0, top; int u = S_; while (dist[S_] < vcnt) { int i = cur[u]; for (; ~i; i = next[i]) { int v = endp[i]; if (cap[i] && dist[u] == dist[v] + 1) { prev[v] = cur[u] = i; u = v; break; } } if (!~i) { cur[u] = gra[u]; top = vcnt; for (int i = gra[u]; ~i; i = next[i]) { int v = endp[i]; if (cap[i] && dist[v] < top) { top = dist[v]; } } ++cnt[top + 1]; if (!--cnt[dist[u]]) { goto STOP; } dist[u] = top + 1; if (u != S_) { u = endp[prev[u] ^ 1]; } } if (u == T_) { int arg = 1 << 29; while (u != S_) { if (cap[prev[u]] < arg) { arg = cap[prev[u]]; } u = endp[prev[u] ^ 1]; } ret += arg; while (u != T_) { cap[cur[u]] -= arg; cap[cur[u] ^ 1] += arg; u = endp[cur[u]]; } u = S_; } } STOP: return ret; } int find(int u, int p) { vis[u] = true; sprintf(bend, %d , u + 1); bend += strlen(bend); *bend = 0; for (int i = gra[u]; ~i; i = next[i]) { int v = endp[i]; if (v == p || vis[v]) { continue; } if ((!!cap[i]) ^ (a[u] & 1)) { return find(v, u) + 1; } } return 1; } bool is_pr(int val) { int l = 0, r = h - 1; while (r - l > 1) { int mid = (l + r) >> 1; if (pr[mid] < val) { l = mid; } else { r = mid; } } return pr[r] == val; } int main() { for (int i = 2; i < 100000; ++i) { if (!vis[i]) { pr[h++] = i; } for (int j = 0; j < h; ++j) { if (100000 / pr[j] < i) { break; } vis[pr[j] * i] = true; if (!(i % pr[j])) { break; } } } while (scanf( %d , &n) != EOF) { for (int i = 0; i < n; ++i) { scanf( %d , a + i); } memset(gra, 255, sizeof(gra)); ecnt = -1; for (int i = 0; i < n; ++i) { if (a[i] & 1) { add_edge(n, i, 2); for (int j = 0; j < n; ++j) { if (!(a[j] & 1) && is_pr(a[i] + a[j])) { add_edge(i, j, 1); } } } else { add_edge(i, n + 1, 2); } } int flow = isap(n + 2, n, n + 1); if (flow != n) { puts( Impossible ); } else { oend = out; memset(vis, 0, sizeof(vis)); int cnt = 0; for (int i = 0; i < n; ++i) { if (!vis[i]) { bend = buf; sprintf(oend, %d%s n , find(i, -1), buf); oend += strlen(oend); ++cnt; } } *(++oend) = 0; printf( %d n%s , cnt, out); } } return 0; }
#include <bits/stdc++.h> const int MAX_N = (int)1e5 + 123; const double eps = 1e-6; const int inf = (int)1e9 + 123; using namespace std; int n, k; int mini = inf; string second; int sum[MAX_N]; int main() { cin >> n >> k >> second; for (int i = 0; i < int((second).size()); i++) sum[i] = (i > 0 ? sum[i - 1] : 0) + (second[i] == 0 ); vector<int> q; for (int i = 0; i < int((second).size()); i++) if (second[i] == 0 ) q.push_back(i); for (int i = 0; i < int((second).size()); i++) { if (sum[int((second).size()) - 1] - (i > 0 ? sum[i - 1] : 0) < k + 1) break; int l = i, r = int((second).size()) - 1, mid = -1, ans = -1; while (l <= r) { mid = (l + r) / 2; if (sum[mid] - (i > 0 ? sum[i - 1] : 0) >= k + 1) { ans = mid; r = mid - 1; } else l = mid + 1; } assert(ans != -1); int now = ans - i; if ((ans - i + 1) % 2 == 1) { int mid = (ans + i) / 2; auto it = lower_bound(q.begin(), q.end(), mid); if (it != q.end()) now = min(now, max(*it - i, ans - *it)); if (it != q.begin()) { it--; now = min(now, max(*it - i, ans - *it)); } } else { int mid = (ans + i) / 2 + 1; auto it = lower_bound(q.begin(), q.end(), mid); if (it != q.end()) now = min(now, max(*it - i, ans - *it)); mid--; it = lower_bound(q.begin(), q.end(), mid); if (it != q.end()) now = min(now, max(*it - i, ans - *it)); if (it != q.begin()) { it--; now = min(now, max(*it - i, ans - *it)); } } mini = min(mini, now); } cout << mini; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, m, sum = 0; cin >> n >> m; long long int a[5] = {0}; if (n == 1) cout << 0 << endl; else if (n == 2) cout << m << endl; else cout << (2 * m) << endl; } }
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n; vector<int> adj[N]; vector<pair<int, int> > r; int color[N]; bool vis[N]; void solve(int x) { vis[x] = true; for (auto w : adj[x]) { if (!vis[w]) { color[w] = (color[x]) ^ 1; solve(w); } } } int32_t main() { scanf( %d , &n); for (int i = 0; i < n; i++) { int x, y; scanf( %d%d , &x, &y); x--, y--; adj[x].push_back(y); adj[y].push_back(x); int k = 2 * i; int kk = 2 * i + 1; k %= 2 * n; kk %= 2 * n; adj[k].push_back(kk); adj[kk].push_back(k); r.push_back(pair<int, int>(x, y)); } for (int i = 0; i < 2 * n; i++) { if (!vis[i]) { solve(i); } } for (auto w : r) { cout << color[w.first] + 1 << << color[w.second] + 1 << endl; } }
#include <bits/stdc++.h> using namespace std; long long mod; long long lgput(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a % mod; long long c = lgput(a, b / 2); c = c * c % mod; if (b & 1) c = c * a % mod; return c; } map<long long, int> hh; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> mod >> k; int i, j, z; for (i = 0; i < n; i++) { cin >> z; hh[(lgput(z, 4) - 1LL * k * z % mod + mod) % mod]++; } long long ans = 0; for (pair<int, int> cur : hh) ans += 1LL * cur.second * (cur.second - 1) / 2; cout << ans; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC target( avx,avx2,fma ) #pragma GCC optimization( unroll-loops ) const int N = 1e5 + 5; const long double pi = 3.141592653; const long long MOD = 1e9 + 7; const long long mod = 998244353; const long long INF = 9223372036854775807LL; using namespace std; int dx[9] = {1, -1, 0, 0, 1, 1, -1, -1, 0}; int dy[9] = {0, 0, 1, -1, 1, -1, 1, -1, 0}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; while (T--) { string s; long long i, j, ans = 1, n; cin >> s; n = (long long)s.size(); bool ok = true; for (i = 0; i < n; i++) { if (s[i] == w || s[i] == m ) ok = false; } if (!ok) ans = 0; else { long long dp[N]; dp[0] = 0; dp[1] = 1; dp[2] = 2; for (i = 3; i < N; i++) dp[i] = (dp[i - 1] + dp[i - 2]) % MOD; for (i = 0; i < n; i++) { if (s[i] == u || s[i] == n ) { j = i; while (j < n && s[j] == s[i]) j++; ans = (ans * dp[j - i]) % MOD; i = j - 1; } } } cout << ans; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 200005; map<int, int> Map; struct Node { int au, sub; } node[MAX]; int n, temp, m; int main() { cin >> n; for (int i = 1; i <= n; i++) { scanf( %d , &temp); Map[temp]++; } int m; scanf( %d , &m); for (int i = 1; i <= m; i++) { scanf( %d , &node[i].au); } int Max1, Max2; Max1 = Max2 = 0; int id = 1; for (int i = 1; i <= m; i++) { scanf( %d , &node[i].sub); if (Map[node[i].au] > Max1 || (Map[node[i].au] == Max1 && Map[node[i].sub] > Max2)) { Max1 = Map[node[i].au]; Max2 = Map[node[i].sub]; id = i; } } cout << id << endl; return 0; }
#include <bits/stdc++.h> long long get2(long long, long long); long long get1(long long n, long long x) { if (x & 1) return (x + 1) / 2; else if (n & 1) return get2(n / 2, x / 2) + (n + 1) / 2; else return get1(n / 2, x / 2) + n / 2; } long long get2(long long n, long long x) { return get1(n, x == 1 ? n : x - 1); } int main() { long long n; int q; std::cin >> n >> q; while (q--) { long long x; std::cin >> x; std::cout << get1(n, x) << std::endl; } return 0; }
/* { ###################### # Author # # Gary # # 2021 # ###################### */ #include<bits/stdc++.h> #define rb(a,b,c) for(int a=b;a<=c;++a) #define rl(a,b,c) for(int a=b;a>=c;--a) #define LL long long #define IT iterator #define PB push_back #define II(a,b) make_pair(a,b) #define FIR first #define SEC second #define FREO freopen( check.out , w ,stdout) #define rep(a,b) for(int a=0;a<b;++a) #define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define random(a) rng()%a #define ALL(a) a.begin(),a.end() #define POB pop_back #define ff fflush(stdout) #define fastio ios::sync_with_stdio(false) #define check_min(a,b) a=min(a,b) #define check_max(a,b) a=max(a,b) using namespace std; //inline int read(){ // int x=0; // char ch=getchar(); // while(ch< 0 ||ch> 9 ){ // ch=getchar(); // } // while(ch>= 0 &&ch<= 9 ){ // x=(x<<1)+(x<<3)+(ch^48); // ch=getchar(); // } // return x; //} const int INF=0x3f3f3f3f; typedef pair<int,int> mp; /*} */ const int MAXN=1e5+233; vector<int> gra[MAXN]; vector<int> tree[MAXN]; int n,m; int vis[MAXN]; int num[MAXN]; bool leaf[MAXN]; int g[MAXN],f[MAXN][2],h[MAXN]; void dfs(int now,int pre){ for(auto it:gra[now]){ if(it!=pre){ if(vis[it]){ if(vis[it]<vis[now]){ num[it]--; num[now]++; } } else{ vis[it]=vis[now]+1; dfs(it,now); } } } } void gao(int now,int pre){ vis[now]=1; for(auto it:gra[now]) if(it!=pre){ if(!vis[it]){ gao(it,now); num[now]+=num[it]; // cout<<now<< <<it<< <<num[now]<<endl; } } if(!num[now]&&pre){ tree[pre].PB(now); tree[now].PB(pre); } } vector<mp> rest; bool cmp(int A,int B){ return f[A][1]-f[A][0]<f[B][1]-f[B][0]; } void calc(int now,int pre){ vis[now]=1; vector<int> sons; f[now][0]=0; bool all_leaf=true; for(auto it:gra[now]) if(it!=pre) calc(it,now),sons.PB(it),f[now][0]+=f[it][1],all_leaf&=leaf[it]; if((sons.size()&1)&&all_leaf) f[now][0]=INF; else f[now][0]+=(sons.size()+1)>>1; int sum=0; for(auto it:sons) sum+=f[it][0]; sort(ALL(sons),cmp); f[now][1]=INF; if(sons.size()==0){ f[now][1]=0; g[now]=now; h[now]=0; leaf[now]=true; } rb(i,1,sons.size()){ sum+=f[sons[i-1]][1]-f[sons[i-1]][0]; if(i>=sons.size()-1); else continue; // cout<< # <<now<< <<sum<< <<f[sons[i-1]][1]<< <<f[sons[i-1]][0]<<endl; if(i&1){ if(sum+i/2<f[now][1]){ f[now][1]=sum+i/2; g[now]=g[sons[0]]; h[now]=i; } } else{ if(leaf[sons[0]]) continue; if(sum+i/2<f[now][1]){ g[now]=g[sons[1]]; f[now][1]=sum+i/2; h[now]=i; } } } // cout<<now<< : <<f[now][0]<< <<f[now][1]<<endl; } void gao(int now,int ty,int pre){ vector<int> sons; for(auto it:gra[now]) if(it!=pre) sons.PB(it); if(ty==0){ int notleaf=-1; rep(i,sons.size()){ int now=i,nex=i+1; if((sons.size()&1)&&notleaf==-1&&!leaf[sons[now]]){ notleaf=sons[now]; now++; nex++; } if((sons.size()&1)&&notleaf==-1&&!leaf[sons[nex]]){ notleaf=sons[nex]; nex++; } if(nex>=sons.size()) break; rest.PB(II(g[sons[now]],g[sons[nex]])); i=nex; } if(notleaf!=-1){ rest.PB(II(g[notleaf],now)); } for(auto it:sons) gao(it,1,now);return; } if(sons.empty()) return; sort(ALL(sons),cmp); if((h[now])&1){ rb(i,1,h[now]-1){ rest.PB(II(g[sons[i]],g[sons[i+1]])); ++i; } } else{ rb(i,2,h[now]-1){ rest.PB(II(g[sons[i]],g[sons[i+1]])); ++i; } rest.PB(II(g[sons[0]],now)); } rb(i,1,h[now]) gao(sons[i-1],1,now); rb(i,h[now]+1,sons.size()) gao(sons[i-1],0,now); } void solve(){ rest.clear(); rb(i,1,n) gra[i].clear(),leaf[i]=false; rb(i,1,n) tree[i].clear(); rb(i,1,n) vis[i]=0; rb(i,1,n) num[i]=0; rb(i,1,m){ int si; scanf( %d ,&si); int pre=-1; rb(j,1,si){ int ai; scanf( %d ,&ai); if(pre!=-1) gra[pre].PB(ai),gra[ai].PB(pre); pre=ai; } } vis[1]=1; dfs(1,0); rb(i,1,n) vis[i]=0; gao(1,0); rb(i,1,n) vis[i]=0; rb(i,1,n) gra[i]=tree[i]; rb(i,1,n) if(!vis[i]){ calc(i,0); int ret=f[i][0],z=0; int sum=0; for(auto it:gra[i]){ sum+=f[it][1]; } for(auto it:gra[i]){ if(sum-f[it][1]+f[it][0]+gra[i].size()/2<ret){ ret=sum-f[it][1]+f[it][0]+gra[i].size()/2; z=it; } } if(z==0) gao(i,0,0); else{ gao(z,0,i); for(auto it:gra[i]) if(it!=z) gao(it,1,i); int pre=-1; for(auto it:gra[i]){ if(it!=z){ if(pre!=-1) rest.PB(II(g[pre],g[it])),pre=-1; else{ pre=it; } } } if(pre!=-1) rest.PB(II(g[pre],i)); } } printf( %d n ,int(rest.size())); for(auto it:rest){ printf( %d %d n ,it.FIR,it.SEC); } } int main(){ while(true){ scanf( %d%d ,&n,&m); if(!n&&!m){ break; } solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; string s[N]; int n, k, fa[N], len[N], ch[N][26], root = 1, last = 1, tot = 1, times[N]; void insert(int x) { int now = ++tot, pre = last; last = now, len[now] = len[pre] + 1; for (; pre && !ch[pre][x]; pre = fa[pre]) ch[pre][x] = now; if (!pre) fa[now] = root; else { int q = ch[pre][x]; if (len[q] == len[pre] + 1) fa[now] = q; else { int nows = ++tot; len[nows] = len[pre] + 1; memcpy(ch[nows], ch[q], sizeof(ch[q])); fa[nows] = fa[q], fa[q] = fa[now] = nows; for (; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nows; } } } long long vis[N], sum[N]; void getcnt() { for (int i = 1; i <= n; i++) { int now = root; for (int j = 0; j < s[i].length(); j++) { now = ch[now][s[i][j] - a ]; int t = now; while (t && vis[t] != i) vis[t] = i, times[t]++, t = fa[t]; } } } void dfs(int x) { if (x == root || vis[x]) return; vis[x] = 1; dfs(fa[x]); sum[x] += sum[fa[x]]; } int main() { ios::sync_with_stdio(0); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 1; i <= n; i++) { last = 1; for (int j = 0; j < s[i].length(); j++) insert(s[i][j] - a ); } getcnt(); for (int i = 1; i <= tot; i++) sum[i] = (times[i] >= k) * (len[i] - len[fa[i]]); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= tot; i++) dfs(i); for (int i = 1; i <= n; i++) { long long ans = 0, now = root; for (int j = 0; j < s[i].length(); j++) now = ch[now][s[i][j] - a ], ans += sum[now]; printf( %lld , ans); } }
#include <bits/stdc++.h> using namespace std; int main() { int n, i; int sum; int a[1000]; cin >> n; sum = n; for (i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + 1 + n); for (i = 1; i <= n; i++) { if (a[i] != 0) { for (int j = i + 1; j <= n; j++) if (a[j] % a[i] == 0 && a[j] != 0) { a[j] = 0; sum--; } } } cout << sum; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100010; const int MX = 123456; const int mod = (int)1e9 + 7; const int base = 1023456789; const unsigned long long BS1 = 10000019ULL; const int INF = (1 << 29); template <class T> inline void fastScan(T &x) { register char c = getchar(); int neg = 0; x = 0; for (; (c < 48 || c > 57) && (c != - ); c = getchar()) ; if (c == - ) { neg = 1; c = getchar(); } for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; } if (neg) x = -x; } int n, m, q; int W[15]; int kunt[(1 << 12) + 10]; char str[15]; long long dp[(1 << 12) + 5][105]; bool Check(int nn, int pos) { return (bool)(nn & (1 << pos)); } int P(int a, int b) { int x = 0; for (int i = 0; i < n; i++) { if (Check(a, i) == Check(b, i)) { x += W[i]; } } return x; } int main() { fastScan(n); fastScan(m); fastScan(q); for (int i = 0; i < n; i++) { fastScan(W[i]); } for (int i = 0; i < m; i++) { scanf( %s , str); int x = 0; for (int j = 0; str[j]; j++) { if (str[j] == 1 ) { x += (1 << j); } } kunt[x]++; } for (int i = 0; i <= (1 << 12); i++) { int x = 0; for (int j = 0; j <= (1 << 12); j++) { if (kunt[j] >= 0) { x = P(i, j); if (x <= 100) dp[i][x] += kunt[j]; } } } for (int i = 0; i <= (1 << 12); i++) { for (int k = 1; k <= 101; k++) dp[i][k] += dp[i][k - 1]; } for (int i = 0; i < q; i++) { int x = 0, k = 0; scanf( %s %d , str, &k); for (int j = 0; j < n; j++) { if (str[j] == 1 ) x += (1 << j); } printf( %d n , dp[x][k]); } }
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007, ans = 1, n, ert, dp[1000002]; int main() { ios_base::sync_with_stdio(false); cin >> n, ert = n; dp[0] = 1, dp[1] = 1; for (int i = 2; i <= n; i++) { dp[i] = ((i - 1) * dp[i - 2] + dp[i - 1]) % mod; } for (int i = 1; i <= n; i++) { int x; cin >> x; if (x == 2) ans *= ert, ans %= mod, ert--; } cout << ans * dp[ert] % mod << n ; return 0; }
#include <bits/stdc++.h> #define arr(i,n) for(ll i=0;i<n;i++) #define ARR(i,n) for(ll i=n-1;i>=0;i--) #define f(i,a,b) for(ll i=a;i<b;i++) #define F(i,a,b) for(ll i=b-1;i>=a;i--) #define float double #define PI 3.14159265358979323846 #define pb push_back #define mp make_pair #define vi vector<ll> #define ceil(a) (ll) ceil(a) #define floor(a) (ll) floor(a) #define pow(a,b) (ll) pow(a,b) #define fixed(x) cout<<fixed<<setprecision(6)<<x; #define vpii vector< pair<ll,ll> > #define debug(x) cout<<(#x)<< is <<(x)<<endl #define dbg(x,y) cout<<#x<< = <<x<< and <<#y<< = <<y<<endl #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(v) v.begin(),v.end() #define array(a,n) arr(i,n) cout<<a[i]<< ; #define array2D(a,n,m) arr(i,n) {arr(j,m) cout<<a[i][j]<< ; cout<<endl;} #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) typedef long long ll; using namespace std; const ll mod = 1000000000 + 7; void solve() { //code start here ll n; cin>>n; n=2*n; ll a[n]; arr(i,n) cin>>a[i]; sort(a,a+n); ll start=0,end=n-1; arr(i,n) { if(i%2==0) {cout<<a[start]; start++;} else {cout<<a[end]; end--; } cout<< ; } cout<<endl; } int main() { IOS; int t; cin>>t; while(t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { long long int n, i, j, x, y, a[100005], v[100005]; map<long long int, long long int> ma; cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; ma[a[i]] = i; v[i] = 0; } long long int fl = 0, p = -1; for (i = 1; i <= n; i++) { if (!v[ma[i]]) { for (j = ma[i]; j <= n; j++) { if (v[j] == 1 || j == n + 1) { break; } if (a[j] != j - ma[i] + i) { fl = 1; break; } v[j] = 1; } } } if (fl) { cout << No n ; } else { cout << Yes n ; } } return 0; }
#include<iostream> #include<string> using namespace std; int main() { int t; cin>>t; for(int i=0;i<t;i++) { int n; string r,b; cin>>n; cin>>r>>b; int counter1=0,counter2=0; for(int i=0;i<n;i++) { if(r[i]>b[i]) counter1++; else if(r[i]<b[i]) counter2++; } if(counter1>counter2) cout<< RED <<endl; else if(counter1==counter2) cout<< EQUAL <<endl; else cout<< BLUE <<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n; char s[105][105]; char t[105]; bool judge1() { int i = 0; for (; i < n; i++) t[i] = E ; t[i] = 0 ; for (int i = 0; i < n; i++) if (strcmp(t, s[i]) == 0) return 1; return 0; } bool judge2() { for (int i = 0; i < n; i++) { int F = 1; for (int j = 0; j < n; j++) if (s[j][i] != E ) { F = 0; break; } if (F) return 1; } return 0; } int main() { while (~scanf( %d , &n)) { for (int i = 0; i < n; i++) scanf( %s , s[i]); if (judge1() && judge2()) { cout << -1 << endl; continue; } if (judge1()) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) if (s[j][i] == . ) { cout << j + 1 << << i + 1 << endl; break; } } } else for (int i = 0; i < n; i++) { int l = strlen(s[i]); for (int j = 0; j < l; j++) if (s[i][j] == . ) { cout << i + 1 << << j + 1 << endl; break; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { float n; cin >> n; int p = 1; float count = 0; while (n != 0) { count = count + 1.0 / n; n--; } cout << count; }
#include <bits/stdc++.h> using namespace std; int main() { int n, i, n1, output = 0; cin >> n; if (n % 2 == 0) cout << n / 2 - 1; else cout << n / 2; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize( Ofast ) #pragma GCC target( avx,avx2,fma ) #pragma GCC optimization( unroll-loops ) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void __print(int x) { cout << x; } void __print(long x) { cout << x; } void __print(long long x) { cout << x; } void __print(unsigned x) { cout << x; } void __print(unsigned long x) { cout << x; } void __print(unsigned long long x) { cout << x; } void __print(float x) { cout << x; } void __print(double x) { cout << x; } void __print(long double x) { cout << x; } void __print(char x) { cout << << x << ; } void __print(const char *x) { cout << << x << ; } void __print(const string &x) { cout << << x << ; } void __print(bool x) { cout << (x ? true : false ); } template <typename T, typename V> void __print(const pair<T, V> &x) { cout << { ; __print(x.first); cout << , ; __print(x.second); cout << } ; } template <typename T> void __print(const T &x) { int first = 0; cout << { ; for (auto &i : x) cout << (first++ ? , : ), __print(i); cout << } ; } void _print() { cout << ] n ; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << , ; _print(v...); } template <class T> void remdup(vector<T> &v) { sort((v).begin(), (v).end()); v.erase(unique((v).begin(), (v).end()), end(v)); } template <typename T> struct cmp { bool operator()(const T &p1, const T &p2) {} }; const long long N = 2e5 + 100; const long long mod = 1e9 + 7; const long long X = 2e5 + 100; int main() { ios::sync_with_stdio(0); cin.tie(0); ; string s; cin >> s; long long ans = 0, wait = 0, m = 0; for (auto &it : s) { if (it == M ) { m++; if (wait) wait--; } else { if (m) { ans = max(ans, m + wait); wait++; } } } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int MOD = 1e9 + 7; long long int t; string s; long long int n; long long int dp[100][2501]; long long int adp[100][2501]; long long int l, r, sm; void solve() { cin >> s; n = s.length() - 1; sm = 0; for (auto it : s) sm += it - a ; l = adp[n][sm] - 1; if (l < 0) l += MOD; cout << l << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); for (int i = 0; i < 26; i++) adp[0][i] = dp[0][i] = 1; for (int i = 1; i <= 2500; i++) { dp[0][i] += dp[0][i - 1]; dp[0][i] %= MOD; } for (int i = 1; i < 100; i++) { for (int j = 0; j <= 2500; j++) { l = r = 0; if (j - 26 >= 0) l = dp[i - 1][j - 26]; r = dp[i - 1][j]; dp[i][j] = r - l; if (dp[i][j] < 0) dp[i][j] += MOD; dp[i][j] %= MOD; adp[i][j] = dp[i][j]; if (j != 0) { dp[i][j] += dp[i][j - 1]; dp[i][j] %= MOD; } } } t = 1; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int tc = 1; tc <= t; tc++) { int n; cin >> n; pair<pair<int, int>, int> arr[2 * n - 1]; for (int i = 0; i < 2 * n - 1; i++) { cin >> arr[i].first.first >> arr[i].first.second; arr[i].second = i; } sort(arr, arr + 2 * n - 1); long long odd = 0, even = 0; for (int i = 0; i < 2 * n - 1; i++) { if (i & 1) even += arr[i].first.second; else odd += arr[i].first.second; } if (odd >= even) { cout << YES n ; int ans[n]; for (int i = 0; i < 2 * n - 1; i += 2) ans[i / 2] = arr[i].second + 1; sort(ans, ans + n); for (int i = 0; i < n - 1; i++) cout << ans[i] << ; cout << ans[n - 1] << n ; } else { cout << YES n ; int ans[n]; for (int i = 1; i < 2 * n - 1; i += 2) ans[i / 2] = arr[i].second + 1; ans[n - 1] = arr[2 * n - 2].second + 1; sort(ans, ans + n); for (int i = 0; i < n - 1; i++) cout << ans[i] << ; cout << ans[n - 1] << n ; } } }
#include <bits/stdc++.h> using namespace std; pair<int, pair<int, int> > tup[100000 * 2 + 10]; multiset<int> st; int L[100000 * 2 + 10], R[100000 * 2 + 10], sl[100000 * 2 + 10], sr[100000 * 2 + 10]; int main() { int m, n, r, l, t; scanf( %d%d , &m, &n); int flag = 0; for (int i = 1; i <= n; i++) { scanf( %d%d%d , &l, &r, &t); tup[i] = make_pair(l, make_pair(t, 0)); tup[i + n] = make_pair(r, make_pair(t, 1)); } n *= 2; sort(tup + 1, tup + n + 1); int lasty = -1, o = 0; for (int i = 1; i <= n; i++) { int cury = tup[i].first; if (lasty != -1 && cury != lasty && st.size()) { if (st.empty()) { printf( empty n ); return 0; } int curt = *st.begin(); L[++o] = curt - lasty; R[o] = curt - cury; } lasty = cury; if (tup[i].second.second) st.erase(st.lower_bound(tup[i].second.first)); else st.insert(tup[i].second.first); } if (flag) { printf( o:%d n , o); return 0; } sort(L + 1, L + o + 1); sort(R + 1, R + o + 1); for (int i = 1; i <= o; i++) sl[i] = sl[i - 1] + L[i], sr[i] = sr[i - 1] + R[i]; while (m--) { scanf( %d , &t); l = lower_bound(L + 1, L + o + 1, t) - L - 1; r = lower_bound(R + 1, R + o + 1, t) - R - 1; printf( %d n , (int)((r - l) * (long long)t - sr[r] + sl[l])); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; string s[n]; map<string, int> m; set<string> k; int count = 0; for (int i = 0; i < n; i++) { cin >> s[i]; k.insert(s[i]); } for (int i = 0; i < n; i++) { if (m[s[i]] >= 1) { count++; int j = 0; while (k.find(s[i]) != k.end()) { s[i][3] = j + 0 ; j++; } } m[s[i]]++; k.insert(s[i]); } cout << count << endl; for (int i = 0; i < n; i++) cout << s[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { int a, b, c, d; cin >> a >> b >> c >> d; cout << max(a + b, c + d) << n ; } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:268435456 ) using namespace std; long long _gcd(long long a, long long b) { if (b == 0) return a; _gcd(b, a % b); } int main() { int n, m, k, a[20500][10], res[20500] = {0}, b[20500][10] = {0}; cin >> n >> m >> k; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> a[i][j]; for (int i = 0; i < k; ++i) { int l, r; cin >> l >> r; l--; r--; b[0][r]++; b[l][r]--; b[l + 1][r]++; b[n][r]--; } for (int i = 0; i < m; ++i) { for (int j = 1; j < n; ++j) b[j][i] += b[j - 1][i]; } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) if (a[i][j] == 1) res[i] += b[i][j]; } for (int i = 0; i < n; ++i) cout << res[i] << ; }
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-9; const int inf = 1e+9; const double pi = acos(-1.0); long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long xabs(long long a) { return a > 0 ? a : -a; } map<int, vector<string>> s; int sm[9][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, {0, 0}}; void dfs(int x, int y, int t) { if (t == 9) { cout << WIN ; exit(0); } if (s[t][x][y] == S ) return; for (int i = 0; i < 9; i++) { int xx = x + sm[i][0]; int yy = y + sm[i][1]; if (xx >= 0 && xx < 8 && yy >= 0 && yy < 8 && s[t][xx][yy] != S ) dfs(xx, yy, t + 1); } } int main() { for (int i = 0; i < 8; i++) { string t; cin >> t; s[0].push_back(t); } for (int i = 1; i <= 8; i++) { string r = ........ ; s[i].push_back(r); for (int k = 0; k < 7; k++) { s[i].push_back(s[i - 1][k]); } } dfs(7, 0, 0); cout << LOSE ; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T, class U> void maximize(T &x, U y) { if (x < y) x = y; } template <class T, class U> void minimize(T &x, U y) { if (x > y) x = y; } template <class T> T Abs(T x) { return (x < (T)0 ? -x : x); } namespace task { const int N = 2e5 + 5; const int maxVal = 5e5; vector<int> prime; vector<vector<int> > p; int e[maxVal + 5]; int cnt[maxVal + 5]; bool inshelf[N]; int a[N]; int n, q; void solve() { cin >> n >> q; for (int i = 1; i <= n; ++i) { cin >> a[i]; } iota(e, e + 1 + maxVal, 0); for (int i = 2; i <= maxVal; ++i) { if (e[i] == i) { prime.emplace_back(i); } for (int j = 0; j < (int)((prime).size()) and i * prime[j] <= maxVal; ++j) { e[i * prime[j]] = prime[j]; if (i % prime[j] == 0) { break; } } } p.resize(n + 2, vector<int>()); for (int i = 1; i <= n; ++i) { int tmp = a[i]; for (int k = e[tmp]; k > 1 and tmp > 1; k = e[tmp]) { while (tmp % k == 0) { tmp /= k; } p[i].emplace_back(k); } } int cur = 0; long long ans = 0; while (q--) { int x; cin >> x; long long subs = 0; int k = (inshelf[x] ? -1 : 1); for (int mask = 1; mask < ((1LL) << ((int)((p[x]).size()))); ++mask) { int mul = 1; for (int i = 0; i < (int)((p[x]).size()); ++i) { if ((((mask) >> (i)) & 1)) { mul *= p[x][i]; } } subs += 1LL * ((__builtin_popcount(mask) & 1) ? 1 : -1) * 1LL * cnt[mul]; } if (!inshelf[x]) { ans += cur - subs; inshelf[x] = true; ++cur; } else { ans -= cur - (a[x] == 1 ? 1 : subs); inshelf[x] = false; --cur; } cout << ans << n ; for (int mask = 1; mask < ((1LL) << ((int)((p[x]).size()))); ++mask) { int mul = 1; for (int i = 0; i < (int)((p[x]).size()); ++i) { if ((((mask) >> (i)) & 1)) { mul *= p[x][i]; } } cnt[mul] += k; } } } } // namespace task int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); task::solve(); }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; const int maxn = 55; int st[maxn]; struct E { int u, v, w, cost; int nxt; } edge[500009]; int gtot = 0, head[maxn * 100]; void addedge(int u, int v, int w, int cost) { edge[gtot].v = v; edge[gtot].w = w; edge[gtot].cost = cost; edge[gtot].nxt = head[u]; head[u] = gtot++; edge[gtot].v = u; edge[gtot].w = 0; edge[gtot].cost = -cost; edge[gtot].nxt = head[v]; head[v] = gtot++; } int dis[maxn * 100], path[500009], vis[maxn * 100], pre[maxn * 100]; bool spfa(int s, int t) { memset(pre, -1, sizeof(pre)); memset(dis, inf, sizeof(dis)); memset(vis, 0, sizeof(vis)); queue<int> que; que.push(s); vis[s] = 1; dis[s] = 0; while (!que.empty()) { int u = que.front(); que.pop(); vis[u] = 0; for (int i = head[u]; ~i; i = edge[i].nxt) { int v = edge[i].v, val = edge[i].w, cost = edge[i].cost; if (val > 0 && dis[v] > dis[u] + cost) { dis[v] = dis[u] + cost; pre[v] = u; path[v] = i; if (vis[v] == 0) { vis[v] = 1; que.push(v); } } } } return pre[t] != -1; } int mcmf(int s, int t) { int flow = 0, cost = 0; while (spfa(s, t)) { int f = inf; for (int i = t; i != s; i = pre[i]) { f = min(f, edge[path[i]].w); } flow += f; cost += f * dis[t]; for (int i = t; i != s; i = pre[i]) { edge[path[i]].w -= f; edge[path[i] ^ 1].w += f; } } return cost; } int main() { memset(head, -1, sizeof(head)); gtot = 0; int n, m, k, c, d; scanf( %d%d%d%d%d , &n, &m, &k, &c, &d); for (int i = 1; i <= k; i++) { int x; scanf( %d , &x); st[x]++; } int s = 0, t = 100 * n + 1; for (int i = 1; i <= n; i++) { for (int j = 2; j <= 100; j++) { addedge((i - 1) * 100 + j - 1, (i - 1) * 100 + j, inf, c); } } for (int i = 2; i <= n; i++) { if (st[i]) addedge(s, (i - 1) * 100 + 1, st[i], 0); } for (int i = 1; i <= 100; i++) addedge(i, t, inf, 0); for (int i = 1; i <= m; i++) { int u, v; scanf( %d%d , &u, &v); for (int j = 2; j <= 100; j++) { for (int k = 1; k <= 20; k++) { addedge((u - 1) * 100 + j - 1, (v - 1) * 100 + j, 1, (k * k - (k - 1) * (k - 1)) * d + c); addedge((v - 1) * 100 + j - 1, (u - 1) * 100 + j, 1, (k * k - (k - 1) * (k - 1)) * d + c); } } } printf( %d n , mcmf(s, t)); return 0; }
#include <bits/stdc++.h> using namespace std; int read(); int n; int a[200005], b[200005], c[200005], bk[200005]; int hd[200005], nx[400005], to[400005], cnt; void add(int f, int t) { nx[++cnt] = hd[f], hd[f] = cnt, to[cnt] = t; } int fa[200005], dep[200005]; void dfs1(int u) { for (int i = hd[u], v; i; i = nx[i]) if ((v = to[i]) != fa[u]) dep[v] = dep[fa[v] = u] + 1, dfs1(v); } bool check1() { for (int i = 1; i <= n; ++i) c[i] = b[i]; int u = a[1]; while (fa[u]) swap(c[u], c[fa[u]]), u = fa[u]; for (int i = 1; i <= n; ++i) if (c[i] != i) return 0; return 1; } vector<int> st[2]; int idcnt; void fail() { puts( -1 ), exit(0); } void get(int u, int id) { if (id >= 2 || (id == 1 && fa[st[0][0]] != fa[u])) fail(); st[id].push_back(u); while (1) { int v = 0; for (int i = hd[u]; i; i = nx[i]) if (to[i] != fa[u] && c[to[i]] != to[i]) v ? fail() : (v = to[i], void()); if (!v) break; st[id].push_back(u = v); } } void dfs2(int u) { for (int i = hd[u], v; i; i = nx[i]) if ((v = to[i]) != fa[u]) (c[v] != v) ? get(v, idcnt++) : dfs2(v); } int p[200005], q[200005], len; int M(int x) { return x >= len ? x - len : x; } int getdis(int u, int v) { int rt = 0; while (u != v) (dep[u] < dep[v]) ? swap(u, v) : void(), ++rt, u = fa[u]; return rt; } void work() { for (int i = 1; i <= n; ++i) q[i] = -1; for (int i = 0; i < st[0].size(); ++i) p[len] = c[st[0][i]], q[st[0][i]] = len, ++len; int ru = st[0].back(), rv = fa[st[0].front()], rw = rv; if (st[1].size()) { rv = st[1].back(); for (int i = st[1].size() - 1; i >= 0; --i) p[len] = c[st[1][i]], q[st[1][i]] = len, ++len; } if ((ru = bk[ru]) > (rv = bk[rv])) swap(ru, rv); long long dis = q[p[0]]; for (int i = 1; i < len; ++i) if (M(q[p[i]] - i + len) != dis) fail(); for (int i = 1; i <= n; ++i) if (c[i] != i && q[i] == -1) fail(); int t = a[1]; while (q[t] == -1 && t != 1) t = fa[t]; if (t == 1) { dis = min(dis, len - dis) * (len + 1); printf( %d %d %lld n , ru, rv, dis + getdis(a[1], rw) + dep[rw]); return; } dis = q[t] >= st[0].size() ? min((dis - 1) * (len + 1) + (q[t] + 1), 1ll * M(len - dis) * (len + 1) + (len - q[t])) : min(dis * (len + 1) + (q[t] + 1), (len - dis - 1) * (len + 1) + (len - q[t])); printf( %d %d %lld n , ru, rv, dis + dep[rw] + getdis(a[1], t)); } int main() { n = read(); for (int i = 1; i <= n; ++i) bk[a[i] = read() + 1] = i; for (int i = 1; i <= n; ++i) b[a[i]] = read() + 1; for (int i = 1, u, v; i < n; ++i) u = a[read()], v = a[read()], add(u, v), add(v, u); for (int i = 1; i <= n; ++i) a[b[i]] = i; dfs1(1); if (check1()) return printf( 0 %d n , dep[a[1]]), 0; dfs2(1), work(); return 0; } const int _SIZE = 1 << 22; char ibuf[_SIZE], *iS = ibuf, *iT = ibuf; int read() { int x = 0, f = 1; char c = (iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)), (iS == iT ? EOF : *iS++) : *iS++); while (!isdigit(c)) f = (c == - ) ? -1 : f, c = (iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)), (iS == iT ? EOF : *iS++) : *iS++); while (isdigit(c)) x = x * 10 + c - 0 , c = (iS == iT ? iT = ((iS = ibuf) + fread(ibuf, 1, _SIZE, stdin)), (iS == iT ? EOF : *iS++) : *iS++); return x * f; }
#include <bits/stdc++.h> using namespace std; long long n, t = 1, c1, c2; vector<vector<long long>> adj; vector<vector<long long>> g; vector<bool> visited; void DFS(long long v) { visited[v] = true; g[t].push_back(v); for (auto i : adj[v]) if (!visited[i]) DFS(i); } long long number_of_components() { for (long long v = 1; v <= n; v++) if (!visited[v]) { DFS(v); if (g[t].size() > 3) { cout << -1 ; return -1; } if (g[t].size() == 1) c1++; if (g[t].size() == 2) c2++; t++; } } void solve() { long long m, x, y, j = 0, h = 1; cin >> n >> m; adj.resize(n + 1); g.resize(n + 1); visited.assign(n + 1, false); for (long long i = 1; i <= m; i++) { cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } long long z = number_of_components(); if (z == -1) return; if (c2 > c1) { cout << -1 ; return; } if ((c1 - c2) % 3 != 0) { cout << -1; return; } for (long long i = 1; i <= t; i++) { if (g[i].size() == 3) { for (auto v : g[i]) cout << v << ; cout << n ; } if (g[i].size() == 2) { for (auto v : g[i]) cout << v << ; for (j = h; j <= t; j++) if (g[j].size() == 1) { cout << g[j][0]; break; } h = j + 1; cout << n ; } } for (long long i = j + 1; i <= t; i++) { if (g[i].size() == 1) cout << g[i][0] << ; if ((j - i) % 3 == 0) cout << n ; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); cerr << time taken : << (float)clock() / CLOCKS_PER_SEC << secs << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, d, h; int main() { cin >> n >> d >> h; if (d - h > h) { cout << -1 << endl; return 0; } if (h == 1 && d == 1 && n > 2) { cout << -1 << endl; return 0; } for (int i = 2; i <= h + 1; i++) { cout << i - 1 << << i << endl; } int j = 1; for (int i = h + 2; i <= d + 1; i++) { if (i > h + 2) j = i - 1; cout << j << << i << endl; } for (int i = d + 2; i <= n; i++) { cout << h << << i << endl; } }
#include <bits/stdc++.h> using namespace std; long long n, m, k; char s[200005]; vector<long long> cnt[3][202]; vector<pair<long long, long long> > res; int main() { long long i, j; scanf( %lld , &n); scanf( %s , &s); for (i = 0; i <= n - 1; i++) { cnt[1][s[i]].push_back(i + 1); } scanf( %s , &s); for (i = 0; i <= n - 1; i++) { cnt[2][s[i]].push_back(i + 1); } for (i = a ; i <= z ; i++) { while (cnt[1][i].size() && cnt[2][i].size()) { res.push_back(make_pair(cnt[1][i].back(), cnt[2][i].back())); cnt[1][i].pop_back(), cnt[2][i].pop_back(); } } for (i = a ; i <= z ; i++) { while (cnt[2][i].size() && cnt[1][ ? ].size()) { res.push_back(make_pair(cnt[1][ ? ].back(), cnt[2][i].back())); cnt[1][ ? ].pop_back(), cnt[2][i].pop_back(); } } for (i = a ; i <= z ; i++) { while (cnt[1][i].size() && cnt[2][ ? ].size()) { res.push_back(make_pair(cnt[1][i].back(), cnt[2][ ? ].back())); cnt[2][ ? ].pop_back(), cnt[1][i].pop_back(); } } while (cnt[1][ ? ].size() && cnt[2][ ? ].size()) { res.push_back(make_pair(cnt[1][ ? ].back(), cnt[2][ ? ].back())); cnt[2][ ? ].pop_back(), cnt[1][ ? ].pop_back(); } long long d = res.size(); printf( %lld n , d); for (i = 0; i <= d - 1; i++) { printf( %lld %lld n , res[i].first, res[i].second); } return 0; }
#include <bits/stdc++.h> using namespace std; int x[2000][10], y[2000][10]; double Ab(int x) { if (x >= 0) return x; return -x; }; double q(double x) { return x * x; }; double d(int i, int j) { double X, Y; if (x[i][0] == x[i][1] && y[j][0] == y[j][1]) { Y = min(Ab(y[i][0] - y[j][0]), Ab(y[i][1] - y[j][0])); if (y[j][0] <= y[i][1] && y[j][0] >= y[i][0]) Y = 0; X = min(Ab(x[j][0] - x[i][0]), Ab(x[j][1] - x[i][0])); if (x[i][0] <= x[j][1] && x[i][0] >= x[j][0]) X = 0; } else if (x[j][0] == x[j][1] && y[i][0] == y[i][1]) { Y = min(Ab(y[i][0] - y[j][0]), Ab(y[i][1] - y[j][1])); if (y[i][0] <= y[j][1] && y[i][0] >= y[j][0]) Y = 0; X = min(Ab(x[j][0] - x[i][0]), Ab(x[j][1] - x[i][1])); if (x[j][0] <= x[i][1] && x[j][0] >= x[i][0]) X = 0; } else if (x[i][0] == x[i][1]) { X = Ab(x[i][0] - x[j][0]); Y = min(min(Ab(y[i][1] - y[j][0]), Ab(y[i][1] - y[j][1])), min(Ab(y[i][0] - y[j][0]), Ab(y[i][0] - y[j][1]))); if (y[i][1] <= y[j][1] && y[i][1] >= y[j][0]) Y = 0; if (y[i][0] <= y[j][1] && y[i][0] >= y[j][0]) Y = 0; } else { Y = Ab(y[i][0] - y[j][0]); X = min(min(Ab(x[i][1] - x[j][0]), Ab(x[i][1] - x[j][1])), min(Ab(x[i][0] - x[j][0]), Ab(x[i][0] - x[j][1]))); if (x[i][1] <= x[j][1] && x[i][1] >= x[j][0]) X = 0; if (x[i][0] <= x[j][1] && x[i][0] >= x[j][0]) X = 0; } return (sqrt(q(X) + q(Y))); }; int main() { double a, b; cin >> a >> b; int xk, yk; scanf( %d%d%d%d , &x[0][0], &y[0][0], &xk, &yk); x[0][1] = x[0][0]; y[0][1] = y[0][0]; int n; scanf( %d , &n); x[n + 1][0] = xk; y[n + 1][0] = yk; x[n + 1][1] = xk; y[n + 1][1] = yk; for (int i = 1; i <= n; i++) { scanf( %d%d%d%d , &x[i][0], &y[i][0], &x[i][1], &y[i][1]); if (y[i][0] > y[i][1]) swap(y[i][0], y[i][1]); if (x[i][0] > x[i][1]) swap(x[i][0], x[i][1]); } queue<pair<int, int> > q; q.push(make_pair(0, 0)); bool was[2000]; for (int i = 0; i <= n + 1; i++) was[i] = false; was[0] = true; double ans = -1; while (!q.empty()) { pair<int, int> A = q.front(); q.pop(); int i = A.first; int k = A.second; if (d(n + 1, i) - a <= 1e-7) { double X = d(n + 1, i) + (a + b) * k; if (ans == -1) ans = X; else ans = min(ans, X); } for (int j = 0; j <= n; j++) { if (was[j]) continue; if (d(i, j) - a > 1e-7) continue; q.push(make_pair(j, k + 1)); was[j] = true; } } cout << fixed << setprecision(10); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2010; char mp[N][N]; int n, m, cnt1, cnt2, in[N][N]; int xx[6] = {0, 0, 1, 0, -1}; int yy[6] = {0, 1, 0, -1, 0}; queue<pair<long long, long long> > q; int main() { int n, m; while (cin >> n >> m) { memset(in, 0, sizeof in); for (int i = 1; i <= n; i++) { scanf( %s , mp[i] + 1); } cnt1 = cnt2 = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) if (mp[i][j] == . ) { cnt1++; for (int k = 1; k <= 4; k++) if (mp[i + xx[k]][j + yy[k]] == . ) in[i][j]++; if (in[i][j] == 1) { q.push(pair<long long, long long>(i, j)); } } } while (!q.empty()) { pair<long long, long long> u = q.front(); q.pop(); int x = u.first, y = u.second, tx, ty, i; for (i = 1; i <= 4; i++) { tx = x + xx[i]; ty = y + yy[i]; if (mp[tx][ty] == . ) { cnt2 += 2; if (i == 1) mp[x][y] = < , mp[tx][ty] = > ; if (i == 2) mp[x][y] = ^ , mp[tx][ty] = v ; if (i == 3) mp[x][y] = > , mp[tx][ty] = < ; if (i == 4) mp[x][y] = v , mp[tx][ty] = ^ ; break; } } if (i == 5) continue; x = tx, y = ty; for (int i = 1; i <= 4; i++) { tx = x + xx[i]; ty = y + yy[i]; if (mp[tx][ty] == . ) { in[tx][ty]--; if (in[tx][ty] == 1) { q.push(pair<long long, long long>(tx, ty)); } } } } if (cnt1 < cnt2) { cout << cnt1 << << cnt2 << endl; for (int i = 1; i <= n; i++) { printf( %s n , mp[i] + 1); } break; } if (cnt1 != cnt2) { printf( Not unique n ); continue; } for (int i = 1; i <= n; i++) { printf( %s n , mp[i] + 1); } } }
#include <bits/stdc++.h> long long int segMax[1000010] = {0}, segSum[1000010] = {0}; long long int a[200010]; void constructMax(int low, int high, int pos) { if (low > high) return; if (low == high) { segMax[pos] = a[low]; return; } int mid = low + (high - low) / 2; constructMax(low, mid, pos * 2 + 1); constructMax(mid + 1, high, pos * 2 + 2); segMax[pos] = std::max(segMax[pos * 2 + 1], segMax[pos * 2 + 2]); } void updateMax(int low, int high, int node, long long int val, int pos) { if (low == high && low == node) { segMax[pos] = val; return; } if (high < node || low > node) return; int mid = low + (high - low) / 2; updateMax(low, mid, node, val, pos * 2 + 1); updateMax(mid + 1, high, node, val, pos * 2 + 2); segMax[pos] = std::max(segMax[pos * 2 + 1], segMax[pos * 2 + 2]); } void constructSum(int low, int high, int pos) { if (low > high) return; if (low == high) { segSum[pos] = a[low]; return; } int mid = low + (high - low) / 2; constructSum(low, mid, pos * 2 + 1); constructSum(mid + 1, high, pos * 2 + 2); segSum[pos] = segSum[pos * 2 + 1] + segSum[pos * 2 + 2]; } void updateSum(int low, int high, int node, long long int val, int pos) { if (low == high && low == node) { a[low] = val; segSum[pos] = val; return; } if (low > node || high < node) return; int mid = low + (high - low) / 2; updateSum(low, mid, node, val, pos * 2 + 1); updateSum(mid + 1, high, node, val, pos * 2 + 2); segSum[pos] = segSum[pos * 2 + 1] + segSum[pos * 2 + 2]; } int findMax(int low, int high, int sumToFind, int pos) { if (low == high && segMax[pos] >= sumToFind) { return low; } else if (low == high) { return -1; } int mid = low + (high - low) / 2; if (segMax[pos * 2 + 1] >= sumToFind) { return findMax(low, mid, sumToFind, pos * 2 + 1); } else if (segMax[pos * 2 + 2] >= sumToFind) { return findMax(mid + 1, high, sumToFind, pos * 2 + 2); } else { return -1; } } long long int findSum(int qlow, int qhigh, int low, int high, int pos) { if (high < qlow || low > qhigh) return 0; if (qlow > qhigh) return 0; else if (qlow <= low && high <= qhigh) { return segSum[pos]; } int mid = low + (high - low) / 2; return findSum(qlow, qhigh, low, mid, pos * 2 + 1) + findSum(qlow, qhigh, mid + 1, high, pos * 2 + 2); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); int n, q; std::cin >> n >> q; for (int i = 0; i < n; ++i) { std::cin >> a[i]; } constructMax(1, n - 1, 0); constructSum(1, n - 1, 0); while (q--) { int node; long long int val; std::cin >> node >> val; --node; if (node != 0) { updateMax(1, n - 1, node, val, 0); updateSum(1, n - 1, node, val, 0); } else { a[node] = val; } if (a[0] == 0) { std::cout << 1 n ; continue; } long long int curSum = a[0]; bool flag = false; while (curSum <= 1e9) { int node = findMax(1, n - 1, curSum, 0); long long int nowSum; if (node == -1) break; else { nowSum = findSum(1, node - 1, 1, n - 1, 0); nowSum += a[0]; } if (nowSum == a[node]) { std::cout << node + 1 << n ; flag = true; break; } else { curSum = nowSum + a[node]; } } if (!flag) { std::cout << -1 n ; } } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; for (int z = 0; z < t; z++) { int n, x, y; cin >> n; vector<vector<int>> v; for (int i = 0; i < n; i++) { cin >> x >> y; vector<int> t(2); t[0] = x, t[1] = y; v.push_back(t); } sort(v.begin(), v.end()); bool e = false; for (int i = 0; i < n - 1; i++) if (v[i + 1][1] < v[i][1]) { e = true; break; } if (e) cout << NO << endl; else { cout << YES << endl; string s; for (int i = 0; i < v[0][0]; i++) s += R ; for (int i = 0; i < v[0][1]; i++) s += U ; for (int i = 0; i < n - 1; i++) { for (int j = v[i][0]; j < v[i + 1][0]; j++) s += R ; for (int j = v[i][1]; j < v[i + 1][1]; j++) s += U ; } cout << s << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; string col[] = { Power , Space , Time , Soul , Reality , Mind }; string stone[] = { purple , blue , green , orange , red , yellow }; cout << 6 - n << n ; for (int i = 0; i < 6; ++i) { int flag = 0; for (int j = 0; j < n; ++j) { if (stone[i] == a[j]) { flag = 1; break; } } if (!flag) { cout << col[i] << n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 10, M = 1e9, eps = 1e-7, MOD = 1e9 + 7; long long tab[N]; int main() { long long n, l; cin >> n >> l; n--; for (long long i = 1; i <= n; i++) cin >> tab[i]; long long pocz = 0; long long pot = 1 << 30; while (pot > 0) { long long moze = pot + pocz; if (moze > M) { pot /= 2; continue; } long long pom = 0; bool czujka = true; for (long long i = 1; i <= l; i++) pom += tab[i]; if (pom < moze) czujka = false; for (long long i = l + 1; i <= n; i++) { pom += tab[i]; pom -= tab[i - l]; if (pom < moze) czujka = false; } if (czujka) pocz = moze; pot /= 2; } cout << pocz; }
#include <bits/stdc++.h> using namespace std; bool endline = false; template <class T> istream& operator>>(istream& inp, vector<T>& v) { for (auto& it : v) inp >> it; return inp; } template <class T> ostream& operator<<(ostream& out, vector<T>& v) { for (auto& it : v) out << it << (endline ? n : ); return out; } template <class T, class U> istream& operator>>(istream& inp, pair<T, U>& v) { inp >> v.first >> v.second; return inp; } template <class T, class U> ostream& operator<<(ostream& out, pair<T, U>& v) { out << v.first << << v.second; return out; } void debug() { cout << endl; } template <typename H, typename... T> void debug(H a, T... b) { cout << a << ; debug(b...); } void FAST_IO(string filein = , string fileout = ) { if (fopen(filein.c_str(), r )) { freopen(filein.c_str(), r , stdin); freopen(fileout.c_str(), w , stdout); } cin.tie(0), cout.tie(0)->sync_with_stdio(0); } void Hollwo_Pelw(); signed main() { FAST_IO( .inp , .out ); int testcases = 1; for (int test = 1; test <= testcases; test++) { Hollwo_Pelw(); } return 0; } const int allmod[3] = {(int)1e9 + 7, 998244353, (int)1e9 + 9}; const int mod = allmod[1]; const int MAXN = 1e6 + 6; const int inf = 2e9; const long long linf = 1e18; long long n; void dofirst() { cout << First << endl; for (long long i = 1; i <= n; i++) cout << i << ; for (long long i = 1; i <= n; i++) cout << i << ; cout << endl; } vector<long long> pr[MAXN], adj[MAXN]; void addedge(long long u, long long v) { adj[u].push_back(v), adj[v].push_back(u); } long long vis[MAXN]; void dfs(long long u, long long c) { vis[u] = c; for (auto v : adj[u]) if (!vis[v]) dfs(v, 3 - c); } void check(long long v) { long long sum = 0; for (long long i = 1; i <= 2 * n; i++) if (vis[i] == v) sum += i; if (sum % (2 * n) == 0) { for (long long i = 1; i <= 2 * n; i++) if (vis[i] == v) cout << i << ; cout << endl; } } void dosecond() { cout << Second << endl; for (long long i = 1, p; i <= 2 * n; i++) cin >> p, pr[p].push_back(i); for (long long i = 1; i <= n; i++) addedge(pr[i][0], pr[i][1]), addedge(i, i + n); for (long long i = 1; i <= 2 * n; i++) if (!vis[i]) dfs(i, 1); check(1); check(2); } void getverdict() { long long x; cin >> x; if (!x) exit(0); } void Hollwo_Pelw() { cin >> n; if (n % 2 == 0) { dofirst(); } else { dosecond(); } getverdict(); }
#include <bits/stdc++.h> using namespace std; int main() { int n, k, i = 1; cin >> n >> k; if (k == 1) cout << n; else { vector<int> ans; while (ans.size() < k) { i = 2; while (n % i != 0 && i <= n + 1) i++; if (i >= n + 1) { cout << -1; return 0; } n /= i; ans.push_back(i); } for (i = 0; i < ans.size() - 1; i++) cout << ans[i] << ; cout << n * ans[ans.size() - 1]; } }
#include <bits/stdc++.h> using namespace std; void OJ() {} void solve() { long long n; cin >> n; long long digit = 1000000, a = -1, b = -1; for (long long i = 0; i < 1e6; ++i) { if (n >= 4 * i) if ((n - 4 * i) % 7 == 0) { if ((n - 4 * i) / 7 + i <= digit) digit = (n - 4 * i) / 7 + i, a = i, b = (n - 4 * i) / 7; } } if (a == -1) { cout << -1; return; } for (long long i = 0; i < a; ++i) cout << 4; for (long long i = 0; i < b; ++i) cout << 7; } signed main() { OJ(); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std; int p[12]; int main() { long long n; cin >> n; if (n < 5) { cout << (n * (n - 1) / 2) << endl; return 0; } long long p = 5; for (int i = 1;; ++i) { if (p > n) { p /= 10; long long tot = p * 2; --i; ++n; if (n % tot <= p) { long long a = n % tot; long long b = p - a; long long x = n / tot; long long ans = (a * x * (x + 1)) + (b * x * x); ans -= x; cout << ans << endl; } else { long long a = n % tot - p; long long b = p - a; long long x = n / tot; long long ans = (a * (x + 1) * (x + 1)) + b * x * (x + 1); ans -= x; cout << ans << endl; } return 0; } p *= 10; } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void rd(T &k) { char c = getchar(); int f = 1; k = 0; while (c < 0 || c > 9 ) { if (c == - ) f *= -1; c = getchar(); } while (c >= 0 && c <= 9 ) { k = k * 10 + c - 0 ; c = getchar(); } k *= f; } const int N = 2200; int n, m, k, s, t; char c[5005]; int f[N][1 << 12], sum[N]; int g[1 << 12]; int main() { cin >> c + 1; n = strlen(c + 1); for (k = 0, s = 1; s <= n; ++k, s <<= 1) ; --k; m = n + 1 - (1 << k); f[0][0] = 1; for (register int i = 1; i <= m; ++i) { char mx = z ; memset(g, 0, sizeof(g)); for (register int j = 0; j < (1 << k); ++j) { if (f[i - 1][j]) g[j] = 1; for (register int p = 0; p < k; ++p) if ((j & (1 << p)) && g[j ^ (1 << p)]) g[j] = 1; if (g[j]) mx = min(mx, c[i + j]); } cout << mx; for (register int j = 0; j < (1 << k); ++j) if (g[j] && c[j + i] == mx) f[i][j] = 1; } }
#include <bits/stdc++.h> using namespace std; const long long int INF = 1e18; int32_t main() { long long int n; cin >> n; if (n % 2 != 0) { cout << 0 << n ; return 0; ; } long long int k = pow(2, n / 2); cout << k << n ; return 0; ; }
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) #pragma GCC optimize( unroll-loops ) using namespace std; using ll = long long; using db = long double; using ii = pair<int, int>; const int N = 5e5 + 5, LG = 19, MOD = 1e9 + 7; const int SQ = 225; const long double EPS = 1e-7; string s; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int _; cin >> _; while (_--) { int n; cin >> n; int cnt[2] = {}; vector<int> v; for (int i = 0; i < n; i++) { string s; cin >> s; v.push_back(s.size() / 2); for (auto c : s) cnt[c - 0 ]++; } cnt[0] /= 2; cnt[1] /= 2; sort(v.begin(), v.end()); int sum = cnt[0] + cnt[1]; int ans = 0; for (auto x : v) if (x <= sum) { sum -= x; ans++; } cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long mod = 2e18) { long long ans = 1; x %= mod; while (y) { if (y & 1) ans = (x * ans) % mod; x = (x * x) % mod; y >>= 1; } return ans; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; cout << setprecision(20); while (t--) { long long n, k; cin >> n >> k; if (n >= 32) { cout << YES << n - 1 << endl; continue; } long long total = (pow(4, n) - 1) / 3; if (k > total) { cout << NO << endl; continue; } long long left; bool f = 0; for (long long i = n; i >= 1; i--) { left = power(2, i + 1) - (i + 2); long long curr = (2 * (i) + 1) * (power(4, n - i) / 3); if (total - curr >= k && k >= left) { cout << YES << n - i << endl; f = 1; break; } } if (!f) cout << NO n ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long a[51][51]; long long combinatorics(long long n, long long k) { double res = 1; for (long long i = 1; i <= k; ++i) res = res * (n - k + i) / i; return (long long)(res + 0.01); } int main() { long long n, m, i, j, numOneRow[51] = {0}, numZeroRow[51] = {0}, numOneColumn[51] = {0}, numZeroColumn[51] = {0}, x, y; long long c; cin >> n >> m; c = m * n; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> a[i][j]; if (a[i][j] == 0) { numZeroRow[i]++, numZeroColumn[j]++; } else { numOneRow[i]++, numOneColumn[j]++; } } } for (i = 0; i < n; i++) { j = 2; x = numOneRow[i], y = numZeroRow[i]; while (x >= j) { c += combinatorics(x, j); j++; } j = 2; while (y >= j) { c += combinatorics(y, j); j++; } } for (j = 0; j < m; j++) { i = 2; x = numOneColumn[j], y = numZeroColumn[j]; while (x >= i) { c += combinatorics(x, i); i++; } i = 2; while (y >= i) { c += combinatorics(y, i); i++; } } cout << c << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long max(long long a, long long b) { if (a > b) { return a; } else { return b; } } long long min(long long a, long long b) { if (a < b) { return a; } else { return b; } } struct P { double x, y; P() {} P(double x, double y) : x(x), y(y) {} P operator+(const P &a) const { return P(x + a.x, y + a.y); } P operator-(const P &a) const { return P(x - a.x, y - a.y); } double operator^(const P &a) const { return x * a.x + y * a.y; } void in() { scanf( %lf%lf , &x, &y); } void out() { printf( REQUIRED %.7f %.7f n , x, y); } double dist(P a, P b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } double sqdist(P a, P b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } }; int n, m; int a[300001], b[300001]; int nxt[1000001], dir[1000001], cost[1000001], rev[1000001]; int h; int visit[300001], c[300001]; int edge[300001]; int l[300001], r[300001]; int lb[300001], ty[1000001]; void insert(int u, int v) { nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; } void insert2(int u, int v, int id) { nxt[++h] = nxt[u]; nxt[u] = h; dir[h] = v; rev[h] = h + 1; ty[h] = id; edge[id] = h; nxt[++h] = nxt[v]; nxt[v] = h; dir[h] = u; rev[h] = h - 1; ty[h] = -1; } void DFS2(int p, int color) { c[p] = color; visit[p] = 1; for (int i = nxt[p]; i > 0; i = nxt[i]) if (visit[dir[i]] == 0) { DFS2(dir[i], 1 - color); } } int s, t; int lvl[300001]; int q[300001]; bool BFS() { for (int i = (1); i <= (2 * n); i++) lvl[i] = -1; lvl[s] = 0; int top = 0, tail = 0; q[top++] = s; while (top > tail) { int u = q[tail++]; for (int po = nxt[u]; po > 0; po = nxt[po]) { if (lvl[dir[po]] == -1 && cost[po] > 0) { lvl[dir[po]] = lvl[u] + 1; q[top++] = dir[po]; } } } return (lvl[t] != -1); } int DFS(int u, int flow) { if (u == t) return flow; int w = flow; for (int po = nxt[u]; po > 0; po = nxt[po]) { if (lvl[dir[po]] == lvl[u] + 1 && cost[po]) { int x = DFS(dir[po], min(w, cost[po])); w -= x; cost[po] -= x; cost[rev[po]] += x; } } if (w == flow) lvl[u] = -3; return flow - w; } int MaxFlow() { int ans = 0; int p; while (BFS()) { while (p = DFS(s, 99999), p) { ans += p; } } return ans; } set<int> S; int main() { scanf( %d%d , &n, &m); for (int i = (1); i <= (n); i++) nxt[i] = -1; h = n; for (int i = (1); i <= (n); i++) scanf( %d , &a[i]); for (int i = (1); i <= (m); i++) { scanf( %d%d , &l[i], &r[i]); insert(l[i], r[i]); } for (int i = (1); i <= (n); i++) visit[i] = 0; for (int i = (1); i <= (n); i++) if (visit[i] == 0) DFS2(i, 1); for (int i = (0); i <= (n + 1); i++) nxt[i] = -1; h = n + 1; for (int i = (1); i <= (n); i++) { if (c[i] == 1) insert2(i, n + 1, i); else insert2(0, i, i); } for (int i = (1); i <= (m); i++) { if (c[l[i]] == 1) insert2(r[i], l[i], 0); else insert2(l[i], r[i], 0); } for (int i = (1); i <= (n); i++) { int base = a[i]; for (int j = (2); j <= (sqrt(base)); j++) { if (base % j == 0) { S.insert(j); while (base % j == 0) base /= j; } } if (base != 1) S.insert(base); } int ans = 0; s = 0; t = n + 1; for (set<int>::iterator it = S.begin(); it != S.end(); ++it) { int w = *it; for (int i = (1); i <= (h); i++) cost[i] = 0; for (int i = (1); i <= (h); i++) if (ty[i] == 0) cost[i] = 999; for (int i = (1); i <= (n); i++) { b[i] = 0; int base = a[i]; while (base % w == 0) { base /= w; b[i]++; } cost[edge[i]] = b[i]; } ans += MaxFlow(); } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; typedef struct Item { long long v; int id; Item(long long a, int b) { v = a; id = b; } bool operator<(const Item &rhs) const { return v < rhs.v; } } Item; typedef struct Node { vector<int> p; vector<Item> pp; long long sum, r; int id; bool operator<(const Node &rhs) const { return pp.back().v < rhs.pp.back().v; } } Node; int n, m; Node nd[MAXN]; int bs(int i, int u, int v, long long x) { if (u > v) return -1; int mid = u + (v - u) / 2; if (nd[i].pp[mid].v == x) return mid; else if (nd[i].pp[mid].v > x) return bs(i, u, mid - 1, x); return bs(i, mid + 1, v, x); } int main(void) { ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; cin >> n; for (int i = 0; i < n; i++) { int k; cin >> k; nd[i].sum = 0; nd[i].id = i + 1; for (int j = 0; j < k; j++) { long long v; cin >> v; nd[i].sum += v; nd[i].p.push_back(v); } for (int j = 0; j < k; j++) nd[i].pp.push_back(Item(nd[i].sum - nd[i].p[j], j + 1)); sort(nd[i].pp.begin(), nd[i].pp.end()); nd[i].r = nd[i].pp.back().v; } sort(nd, nd + n); bool found = false; for (int i = 1; i < n; i++) { for (int j = i - 1; j >= 0; j--) { if (nd[j].r < nd[i].pp[0].v) break; for (int k = 0; k < nd[j].pp.size(); k++) { int pos = bs(i, 0, nd[i].pp.size() - 1, nd[j].pp[k].v); if (pos > -1) { found = true; cout << YES << endl; cout << nd[i].id << << nd[i].pp[pos].id << endl; cout << nd[j].id << << nd[j].pp[k].id << endl; break; } } if (found) break; } if (found) break; } if (!found) cout << NO << endl; cerr << execute time : << (double)clock() / CLOCKS_PER_SEC << endl; return 0; }
#include <bits/stdc++.h> const int maxn = int(1e5) + 7, mod = int(1e9) + 7; bool not_prime[maxn]; std::map<int, int> cnt[maxn]; std::vector<int> factor[maxn]; long long qpow(long long p, long long q) { long long ret = 1; while (q) { if (q & 1) ret = ret * p % mod; p = p * p % mod; q >>= 1; } return ret; } void init() { for (int i = 2; i < maxn; i++) { for (int j = 2; i * j < maxn; j++) factor[i * j].push_back(i); if (!not_prime[i]) { for (int j = 1; i * j < maxn; j++) { int num = i * j; not_prime[num] = true; cnt[num][i] = j; } } } } int f[maxn], m; int calc(int x, int y) { int ret = m / y, a = x / y, b = m / y; std::vector<int> buf; for (auto &it : cnt[a]) { buf.push_back(it.first); } int len = int(buf.size()); for (int i = 1; i < 1 << len; i++) { int tmp = 1; for (int j = 0; j < len; j++) { if ((i >> j) & 1) { tmp = -tmp * buf[j]; } } ret += b / tmp; } return ret; } int main() { init(); std::cin >> m; int inv_m = int(qpow(m, mod - 2)), ans = 1; for (int i = 2; i <= m; i++) { f[i] = 1; for (auto &k : factor[i]) { f[i] = int((f[i] + 1ll * calc(i, k) * f[k] % mod * inv_m % mod) % mod); } f[i] = int(1ll * f[i] * qpow((1 - 1ll * (m / i) * inv_m % mod + mod) % mod, mod - 2) % mod); ans = int((ans + 1ll * f[i] * inv_m % mod) % mod); } std::cout << ans << std::endl; return 0; }
#include <bits/stdc++.h> int gcd(int a, int b) { int r; while (b != 0) { r = a % b; a = b; b = r; } return a; } int main() { int n, m; int deg1, deg2; scanf( %d %d , &n, &m); for (int i = 0, a; i <= n; i++) { scanf( %d , &a); if (i == 0) deg1 = a; } for (int i = 0, a; i <= m; i++) { scanf( %d , &a); if (i == 0) deg2 = a; } if (n > m) { if (deg1 > 0 && deg2 > 0 || (deg1 < 0 && deg2 < 0)) printf( Infinity ); else printf( -Infinity ); } else if (m > n) printf( 0/1 ); else { while (gcd(deg1, deg2) != 1) { int r = gcd(deg1, deg2); deg1 /= r; deg2 /= r; } if (deg1 < 0 && deg2 < 0 || (deg1 > 0 && deg2 > 0)) printf( %d/%d , deg1, deg2); else printf( -%d/%d , abs(deg1), abs(deg2)); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; struct node { int vl, mnvl; node *l, *r; node() { mnvl = MAXN; vl = -1; l = r = NULL; } }; vector<node*> dv(MAXN); vector<bool> inarr(MAXN, false); vector<vector<int>> diverse(MAXN); deque<int> dbl(int a) { deque<int> d; while (a != 0) d.push_front(a % 2), a /= 2; while (d.size() != 18) d.push_front(0); return d; } void ins(node* d, deque<int> a, int n) { while (!a.empty()) { d->mnvl = min(d->mnvl, n); int b = a.front(); a.pop_front(); if (b == 0) { if (d->l == NULL) d->l = new node(); d = d->l; } else { if (d->r == NULL) d->r = new node(); d = d->r; } } d->vl = n; d->mnvl = min(d->mnvl, n); } int fnd(node* d, deque<int> xd, int s, int x) { while (!xd.empty()) { int xx = xd.front(); xd.pop_front(); if (xx == 0) if (d->r != NULL && d->r->mnvl + x <= s) d = d->r; else if (d->l != NULL && d->l->mnvl + x <= s) d = d->l; else return -1; else if (d->l != NULL && d->l->mnvl + x <= s) d = d->l; else if (d->r != NULL && d->r->mnvl + x <= s) d = d->r; else return -1; } return d->vl; } int main() { for (int i = 1; i < MAXN; i++) for (int j = i; j < MAXN; j += i) diverse[j].push_back(i); for (int i = 1; i < dv.size(); i++) dv[i] = new node(); int n, q, a, x, k, s; cin >> n; for (int i = 0; i < n; i++) { cin >> q; if (q == 1) { cin >> a; if (!inarr[a]) { inarr[a] = true; deque<int> d = dbl(a); for (int i = 0; i < diverse[a].size(); i++) ins(dv[diverse[a][i]], d, a); } } else { cin >> x >> k >> s; if (x % k != 0 || s - x <= 0) cout << -1 << endl; else cout << fnd(dv[k], dbl(x), s, x) << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; long long fast_pow(long long a, long long p) { long long res = 1; while (p) { if (p % 2 == 0) { a = a * 1LL * a % 1000000007; p /= 2; } else { res = res * 1LL * a % 1000000007; p--; } } return res; } long long fact(long long n) { long long res = 1; for (long long i = 1; i <= n; i++) { res = res * 1LL * i % 1000000007; } return res; } long long C(long long n, long long k) { return fact(n) * 1LL * fast_pow(fact(k), 1000000007 - 2) % 1000000007 * 1LL * fast_pow(fact(n - k), 1000000007 - 2) % 1000000007; } vector<long long> sieveOfEratosthenes(long long n) { vector<long long> primes; vector<bool> prime(n + 1, true); for (long long p = 2; p * p <= n; p++) { if (prime[p] == true) { for (long long i = p * p; i <= n; i += p) prime[i] = false; } } for (long long p = 2; p <= n; p++) if (prime[p]) primes.push_back(p); return primes; } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } bool isPerfectSquare(long double x) { if (x >= 0) { long long sr = sqrt(x); return (sr * sr == x); } return false; } long long sum(long long n) { long long res = 0; while (n > 0) { res += n % 10; n = n / 10; } return res; } vector<long long> adj[10001]; vector<long long> vis(10001); vector<long long> col(10001); void dfs(long long v) { vis[v] = 1; for (auto ch : adj[v]) { if (vis[ch] == 0) { dfs(ch); } } } bool bptdfs(long long v, long long c) { vis[v] = 1; col[v] = c; for (auto ch : adj[v]) { if (vis[ch] == 0) { if (bptdfs(ch, c ^ 1) == false) { return false; } } else { if (col[v] == col[ch]) { return false; } } } return true; } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long t = 1; while (t--) { long long n, M; cin >> n >> M; vector<long long> t(n); for (long long i = (0); i <= (n - 1); i++) { cin >> t[i]; } vector<long long> ans(n, 0); for (long long i = (1); i <= (n - 1); i++) { long long c = 0, sum = 0; priority_queue<long long> q; for (long long j = (0); j <= (i); j++) { sum += t[j]; if (j != i) { q.push(t[j]); } } if (sum > M) { while (sum > M) { long long x = q.top(); q.pop(); sum -= x; c++; } } ans[i] = c; } for (auto x : ans) { cout << x << ; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; class UFDS { public: vector<int> p, rank; int count = 0; UFDS(int n) { p.resize(n); rank.resize(n); for (int i = 0; i < n; i++) p[i] = i; count = n; } int getParent(int i) { return (p[i] == i) ? i : (p[i] = getParent(p[i])); } void join(int i, int j) { int a = getParent(i), b = getParent(j); if (a == b) return; count -= 1; if (rank[a] > rank[b]) { p[b] = a; } else { p[a] = b; if (rank[a] == rank[b]) rank[b] += 1; } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; set<int> ls, rss; vector<int> rs; vector<vector<int> > edges(m, vector<int>(4)); for (int i = 0; i < m; i++) { cin >> edges[i][2] >> edges[i][3] >> edges[i][0] >> edges[i][1]; ls.insert(edges[i][0]); if (!rss.count(edges[i][1])) { rss.insert(edges[i][1]); rs.push_back(edges[i][1]); } edges[i][2] -= 1; edges[i][3] -= 1; } sort(rs.begin(), rs.end()); sort(edges.begin(), edges.end()); int out = 0, prev = 0; for (auto l : ls) { int lo = prev, hi = rss.size(), ans = -1; while (lo < hi) { int idx = (lo + hi) / 2; int mid = rs[idx]; UFDS ufds(n); for (auto e : edges) { if (e[0] > l) break; if (e[1] >= mid) ufds.join(e[2], e[3]); } if (ufds.getParent(0) == ufds.getParent(n - 1)) { lo = idx + 1; ans = mid; prev = idx; } else { hi = idx; } } if (ans != -1) out = max(out, ans - l + 1); } if (out) cout << out; else cout << Nice work, Dima! ; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long double PI = 3.141592653589793238462643383279502884197; priority_queue<int, vector<int>, greater<int> > pq; vector<int> v; int al[256]; char s[200001], t[200001]; int visit[200001]; int n, m; int main() { scanf( %s %s , s, t); n = strlen(s); m = strlen(t); memset(al, -1, sizeof(al)); int l = 0; for (int i = 0; i < n; i++) { if (l < m && s[i] == t[l]) { al[s[i]] = l; l++; } if (al[s[i]] == -1) return !printf( No ); visit[i] = al[s[i]]; } if (l != m) return !printf( No ); l = m - 1; memset(al, -1, sizeof(al)); for (int i = n - 1; i >= 0; i--) { if (l >= 0 && s[i] == t[l]) { al[s[i]] = l; l--; } if (al[s[i]] == -1) return !printf( No ); if (visit[i] < al[s[i]]) return !printf( No ); } if (l != -1) return !printf( No ); printf( Yes ); }
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c, p, k; cin >> a >> b >> c; if (c != 0) { p = b - a; k = p / c; if (p % c == 0) { if (k >= 0) cout << YES << endl; else cout << NO << endl; } else cout << NO << endl; } else if (c == 0) { if (a == b) cout << YES << endl; else cout << NO << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int pos[maxn]; int n, x, y; int main() { int i, j; while (scanf( %d , &n) != EOF) { memset(pos, -1, sizeof(pos)); for (i = 1; i <= n; i++) { scanf( %d%d , &x, &y); int ans = 0; for (j = 1; j * j <= x; j++) { if (x % j == 0) { if (y == 0) { ans++; pos[j] = i; if (x / j != j) ans++; pos[x / j] = i; continue; } if (pos[j] < i - y) ans++; pos[j] = i; if (x / j != j && pos[x / j] < i - y) ans++; pos[x / j] = i; } } printf( %d n , ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int MAX = 5000001; unsigned int arr[MAX], P[MAX]; void sieve() { unsigned int i, j, t, k; arr[0] = arr[1] = 0; for (i = 2; i < MAX; i++) arr[i] = i; for (i = 4; i < MAX; i += 2) arr[i] = 2; for (i = 3; i * i <= MAX; i += 2) if (arr[i] == i) for (j = i * i; j < MAX; j += 2 * i) if (arr[j] == j) arr[j] = i; for (i = 2; i < 5000001; i++) P[i] = P[i / arr[i]] + 1; for (i = 2; i < 5000001; i++) P[i] += P[i - 1]; } int main(void) { sieve(); unsigned int tc, i, a, b; scanf( %u , &tc); for (i = 1; i <= tc; i++) { scanf( %u %u , &a, &b); printf( %u n , P[a] - P[b]); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, i; long long s, ans; int x[200000], y[200000]; long long a[200000], b[200000], c[200000], d[200000]; void count() { s = 0; for (i = 1; i <= n; i++) b[i] = c[i] = d[i] = 0; for (i = 1; i < n; i++) a[i] = a[i + 1] - a[i]; for (i = 1; i < n; i++) { b[i] = b[i - 1] + i * a[i]; c[i] = c[i - 1] + i * a[i] * a[i]; } for (i = 2; i < n; i++) d[i] = d[i - 1] + 2 * a[i] * b[i - 1]; for (i = 1; i < n; i++) s += c[i] + d[i]; } int main() { scanf( %d , &n); for (i = 1; i <= n; i++) scanf( %d%d , &x[i], &y[i]); x[0] = y[0] = -200000000; for (i = 0; i <= n; i++) a[i] = x[i]; sort(a, a + n + 1); count(); ans += s; for (i = 0; i <= n; i++) a[i] = y[i]; sort(a, a + n); count(); ans += s; cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int64_t solve(int m, int n) { if (m > n) swap(m, n); if (m == 1) { if (n <= 3) return 0; if (n % 6 == 2 || n % 6 == 4) return n - 2; if (n % 6 == 1 || n % 6 == 5) return n - 1; if (n % 6 == 3) return n - 3; return n; } if (m == 2) { if (n <= 2) return 0; if (n == 3) return 4; if (n == 4) return 8; if (n == 7) return 12; return n * 2; } int64_t res = 1LL * m * n; if (res % 2 == 1) res -= 1; return res; } int main() { int n, m; cin >> n >> m; cout << solve(n, m) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; inline int gi() { int data = 0, m = 1; char ch = 0; while (ch != - && (ch < 0 || ch > 9 )) ch = getchar(); if (ch == - ) { m = 0; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { data = (data << 1) + (data << 3) + (ch ^ 48); ch = getchar(); } return (m) ? data : -data; } int SS, TT, P0, n, m, K, Init[405], Flow, Cost, A[405], C[405]; struct _Edge { int to, next, flow; int cost; } Edge[3 * 405 * 405]; int head[405], CNT, S, T; int dis[405], flw[405], frm[405]; bool vis[405]; queue<int> Q; inline void AddEdge(int u, int v, int fl, int cs) { Edge[++CNT] = (_Edge){v, head[u], fl, cs}; head[u] = CNT; Edge[++CNT] = (_Edge){u, head[v], 0, -cs}; head[v] = CNT; } inline bool spfa() { for (int i = 1; i <= TT; ++i) dis[i] = 1e9 + 7; dis[SS] = 0; flw[SS] = 1e9; vis[SS] = true; Q.push(SS); while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int e = head[u]; e; e = Edge[e].next) { int v = Edge[e].to; if (Edge[e].flow && dis[v] > dis[u] + Edge[e].cost) { dis[v] = dis[u] + Edge[e].cost; frm[v] = e; flw[v] = min(flw[u], Edge[e].flow); if (!vis[v]) vis[v] = true, Q.push(v); } } vis[u] = false; } if (dis[TT] == 1e9 + 7) return false; for (int x = TT; x ^ SS; x = Edge[frm[x] ^ 1].to) { Edge[frm[x]].flow -= flw[TT]; Edge[frm[x] ^ 1].flow += flw[TT]; } return true; } int main() { n = gi(); K = gi(); CNT = 1; P0 = 2 * n + 1; S = P0 + 1; T = S + 1; for (int i = 1; i <= n; i++) A[i] = gi(); for (int i = 1; i <= n; i++) C[i] = gi(); AddEdge(S, P0, K, 0); for (int i = 1; i <= n; ++i) AddEdge(P0, i, 1, C[A[i]]), AddEdge(i + n, T, 1e9 + 7, 0); for (int i = 1; i <= n; ++i) Init[i]--, Init[i + n]++, AddEdge(i, i + n, 1e9 + 7, 0); for (int i = 1; i <= n; ++i) for (int j = i + 1; j <= n; ++j) AddEdge(i + n, j, 1e9 + 7, (A[i] ^ A[j]) ? C[A[j]] : 0); SS = T + 1; TT = SS + 1; int Delay = 0; for (int i = 1; i <= T; i++) if (Init[i] < 0) AddEdge(i, TT, -Init[i], 0); else if (Init[i] > 0) AddEdge(SS, i, Init[i], 0), Delay += Init[i]; AddEdge(T, S, 1e9 + 7, 0); while (spfa()) Flow += flw[TT], Cost += flw[TT] * dis[TT]; if (Flow ^ Delay) return puts( -1 ), 0; else printf( %d n , Cost); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = 100002; map<int, int> cnt[MAX]; vector<int> adj[MAX]; long long ans[MAX]; int color[MAX]; int max_el[MAX]; void dfs(int node, int par) { cnt[node][color[node]] = true; max_el[node] = 1; ans[node] = color[node]; for (int i = 0; i < adj[node].size(); i++) { int child = adj[node][i]; if (child != par) { dfs(child, node); if (cnt[node].size() < cnt[child].size()) { swap(cnt[node], cnt[child]); max_el[node] = max_el[child]; ans[node] = ans[child]; } for (pair<int, int> p : cnt[child]) { cnt[node][p.first] += p.second; if (cnt[node][p.first] == max_el[node]) ans[node] += p.first; else if (cnt[node][p.first] > max_el[node]) { max_el[node] = cnt[node][p.first]; ans[node] = p.first; } } } } } int main() { int n, u0, v0; cin >> n; for (int i = 1; i <= n; i++) { cin >> color[i]; } for (int i = 0; i < n - 1; i++) { cin >> u0 >> v0; adj[u0].push_back(v0); adj[v0].push_back(u0); } dfs(1, 0); for (int i = 1; i <= n; i++) { cout << ans[i] << ; } }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long inv2 = 500000004; const long long inv6 = 166666668; const long long inv4 = 250000002; const long long inv30 = 233333335; const long long inv12 = 83333334; const int KKK = 5; long long ipow(long long b, int e) { b = b % mod; long long r = 1; while (e-- > 0) r = (r * b) % mod; return r; } struct node { node *lc, *rc; size_t l, r; long long v; int k; bool flazy; long long lazy; }; const int max_n = 100100; const int max_t = 13 * max_n; int tn; node T[max_t], *troot[KKK + 1]; int n, m, a[max_n]; long long C[KKK + 1][KKK + 1]; long long P[max_n][KKK + 1]; long long Q[max_n][KKK + 1]; void lazy(node *x) { if (x->flazy) { x->v = (((x->lazy) * ((Q[x->r][x->k] - Q[x->l - 1][x->k] + mod))) % mod); x->flazy = false; if (x->l < x->r) { x->lc->flazy = true; x->rc->flazy = true; x->lc->lazy = x->lazy; x->rc->lazy = x->lazy; } } } long long query(node *x, size_t ql, size_t qr) { lazy(x); if (ql <= x->l && x->r <= qr) return x->v; else { long long ans = 0; if (ql <= x->lc->r) ans += query(x->lc, ql, qr); if (qr >= x->rc->l) ans += query(x->rc, ql, qr); return ans < mod ? ans : ans % mod; } } void modify(node *x, size_t ql, size_t qr, long long v) { lazy(x); if (ql <= x->l && x->r <= qr) { x->flazy = true; x->lazy = v; lazy(x); } else { if (ql <= x->lc->r) modify(x->lc, ql, qr, v); if (qr >= x->rc->l) modify(x->rc, ql, qr, v); lazy(x->lc); lazy(x->rc); x->v = x->lc->v + x->rc->v; x->v = x->v < mod ? x->v : x->v % mod; } } void build(node *x) { if (x->l == x->r) x->v = (((a[x->l]) * (P[x->l][x->k])) % mod); if (x->l < x->r) { int mid = (x->l + x->r) / 2; x->lc = &T[tn++]; x->lc->l = x->l; x->lc->r = mid; x->lc->k = x->k; build(x->lc); x->rc = &T[tn++]; x->rc->l = mid + 1; x->rc->r = x->r; x->rc->k = x->k; build(x->rc); x->v = x->lc->v + x->rc->v; x->v = x->v < mod ? x->v : x->v % mod; } } int op_query(int l, int r, int k) { long long ans = 0; for (int i = 0; i <= k; ++i) ans = (ans + ((((((C[k][i]) * (((((k - i) & 1 ? mod - 1 : 1) * (P[l - 1][k - i])) % mod))) % mod)) * (query(troot[i], l, r))) % mod)) % mod; return ans % mod; } long long iin() { char c = 0; long long u = 0; int b = 1; while (c < 0 || c > 9 ) { c = getchar(); if (c == - ) b = -1; } while (c >= 0 && c <= 9 ) { u = u * 10 + c - 0 ; c = getchar(); } return u; } int main() { for (int k = 0; k <= KKK; ++k) C[k][0] = 1; for (int k = 1; k <= KKK; ++k) for (int i = 1; i <= k; ++i) C[k][i] = (C[k - 1][i] + C[k - 1][i - 1]) % mod; scanf( %d%d , &n, &m); for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); for (int i = 0; i <= n; ++i) for (int k = 0; k <= KKK; ++k) P[i][k] = ipow(i, k); for (int k = 0; k <= KKK; ++k) for (int i = 0, z = 0; i <= n; ++i, z = (z + P[i][k]) % mod) Q[i][k] = z; for (int i = 0; i <= KKK; ++i) { troot[i] = &T[tn++]; troot[i]->k = i; troot[i]->l = 1; troot[i]->r = n; build(troot[i]); } for (int i = 0; i < m; ++i) { char op[10]; scanf( %s , op); if (op[0] == ? ) { int l, r, k; scanf( %d%d%d , &l, &r, &k); int ans = op_query(l, r, k); printf( %d n , ans); } if (op[0] == = ) { int l, r, x; scanf( %d%d%d , &l, &r, &x); for (int k = 0; k <= KKK; ++k) modify(troot[k], l, r, x); } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long Base = 1e9 + 7; struct Matrix { int n, m; long long cost[101][101]; Matrix() {} Matrix(int n_, int m_) { n = n_, m = m_; for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) cost[i][j] = 0; } Matrix operator*(Matrix b) { Matrix c = Matrix(n, b.m); for (int i = 0; i <= n; i++) for (int j = 0; j <= b.m; j++) for (int k = 0; k <= m; k++) c.cost[i][j] = (c.cost[i][j] + cost[i][k] * b.cost[k][j] % Base) % Base; return c; } }; Matrix operator^(Matrix a, long long b) { Matrix c = Matrix(a.n, a.m); bool ok = false; while (b > 0) { if (b & 1) { if (!ok) c = a; else c = c * a; ok = true; } a = a * a; b >>= 1; } return c; } const long long N = 1e6 + 1; long long n, l, m; long long a[N], b[N], c[N], counts[N], counts_[N]; int main() { scanf( %lld%lld%lld , &n, &l, &m); for (int i = 1; i <= n; i++) { scanf( %lld , &a[i]); a[i] %= m; counts[a[i]]++; } for (int i = 1; i <= n; i++) { scanf( %lld , &b[i]); b[i] %= m; counts_[b[i]]++; } for (int i = 1; i <= n; i++) { scanf( %lld , &c[i]); c[i] %= m; } Matrix x = Matrix(m - 1, 0); for (int i = 0; i <= m - 1; i++) x.cost[i][0] = counts[i]; if (l > 2) { Matrix tmp = Matrix(m - 1, m - 1); for (int i = 0; i <= m - 1; i++) for (int j = 0; j <= m - 1; j++) if (i < j) tmp.cost[i][j] = counts_[i + m - j] % Base; else tmp.cost[i][j] = counts_[i - j] % Base; tmp = tmp ^ (l - 2); x = tmp * x; } for (int i = 1; i <= n; i++) c[i] = (c[i] + b[i]) % m; long long ans = 0; for (int i = 1; i <= n; i++) if (c[i] == 0) ans = (ans + x.cost[0][0]) % Base; else ans = (ans + x.cost[m - c[i]][0]) % Base; cout << ans; }
#include <bits/stdc++.h> using namespace std; long long n, m, b; vector<pair<long long, long long> > v; long long mask[105], cost[105], monitors[105]; long long dp[1 << 21]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> b; for (int i = 0; i <= 100; i++) mask[i] = (1ll << 30) - 1; for (long long i = 0; i < n; i++) { long long temp; cin >> cost[i] >> monitors[i] >> temp; for (long long j = 0; j < temp; j++) { long long val; cin >> val; mask[i] -= (1ll << (val - 1)); } v.push_back(make_pair(monitors[i], i)); } sort(v.begin(), v.end()); for (long long i = 1; i < (1ll << 21) - 2; i++) dp[i] = 1ll << 60; long long ans = 1ll << 60; for (long long i = 0; i < n; i++) { long long pt = v[i].second; for (long long j = 1; j < (1 << m); j++) { dp[j] = min(dp[j], dp[j & mask[pt]] + cost[pt]); } ans = min(ans, dp[(1 << m) - 1] + v[i].first * b); } if (ans < 1ll << 60) cout << ans << n ; else cout << -1 n ; return 0; }
#include <bits/stdc++.h> #pragma optimize( -O3 ) using namespace std; const long long mod = 998244353; int n, m, u, v; pair<vector<int>, int> adj[1000006]; int alr[1000006]; long long ans = 0, cr = 0; bool check(int i, int j) { if ((int)adj[i].first.size() != (int)adj[j].first.size()) return false; int x = (int)adj[i].first.size(); int in1 = 0, in2 = 0; while (in1 < x && in2 < x) { if (adj[i].first[in1] == adj[j].second) { in1++; continue; } if (adj[j].first[in2] == adj[i].second) { in2++; continue; } if (adj[i].first[in1] != adj[j].first[in2]) return false; in1++; in2++; } return true; } bool check2(int i, int j) { if ((int)adj[i].first.size() != (int)adj[j].first.size()) return false; int x = (int)adj[i].first.size(); int in1 = 0; while (in1 < x) { if (adj[i].first[in1] != adj[j].first[in1]) return false; in1++; } return true; } void dfs(int i) { for (auto &x : adj[i].first) { if (x < i) continue; if (check(i, x)) ans++; } } void solve() { cin >> n >> m; for (long long i = (long long)0; i != (long long)m; i = i + 1) { cin >> u >> v; u--; v--; adj[u].first.push_back(v); adj[v].first.push_back(u); } for (long long i = (long long)0; i != (long long)n; i = i + 1) { adj[i].second = i; sort(adj[i].first.begin(), adj[i].first.end()); } for (long long i = (long long)0; i != (long long)n; i = i + 1) { dfs(i); }; sort(adj, adj + n); for (long long i = (long long)0; i != (long long)n; i = i + 1) ; for (long long i = (long long)1; i != (long long)n; i = i + 1) { if (check2(i, i - 1)) { cr++; ; ans += 1ll * cr; } else { cr = 0; } } cout << ans << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int a[4][2]; int b[4][2]; int vis[205][205]; int main() { bool flag = 0; int x1, x2, y1, y2; for (int i = 0; i < 4; i++) { scanf( %d %d , &a[i][0], &a[i][1]); } for (int i = 0; i < 4; i++) { scanf( %d %d , &b[i][0], &b[i][1]); } if (a[0][0] == a[1][0]) { y1 = min(a[0][1], a[1][1]); y2 = max(a[0][1], a[1][1]); x1 = min(a[1][0], a[2][0]); x2 = max(a[1][0], a[2][0]); } else { y1 = min(a[1][1], a[2][1]); y2 = max(a[1][1], a[2][1]); x1 = min(a[0][0], a[1][0]); x2 = max(a[0][0], a[1][0]); } for (int i = -100; i <= 100; i++) for (int j = -100; j <= 100; j++) { if (i >= x1 && i <= x2 && j >= y1 && j <= y2) { vis[i + 100][j + 100] = 1; } } if ((b[0][0] + b[0][1]) == (b[1][0] + b[1][1])) { x1 = min(b[0][1] + b[0][0], b[2][0] + b[2][1]); x2 = max(b[0][1] + b[0][0], b[2][0] + b[2][1]); y1 = min(b[0][1] - b[0][0], b[1][1] - b[1][0]); y2 = max(b[0][1] - b[0][0], b[1][1] - b[1][0]); } else { x1 = min(b[0][1] + b[0][0], b[1][0] + b[1][1]); x2 = max(b[0][1] + b[0][0], b[1][0] + b[1][1]); y1 = min(b[0][1] - b[0][0], b[2][1] - b[2][0]); y2 = max(b[0][1] - b[0][0], b[2][1] - b[2][0]); } for (int i = -100; i <= 100; i++) for (int j = -100; j <= 100; j++) { if (i + j >= x1 && i + j <= x2 && j - i >= y1 && j - i <= y2 && vis[i + 100][j + 100]) { flag = 1; } } if (flag) { printf( YES n ); } else { printf( NO n ); } return 0; }
#include <bits/stdc++.h> const double eps = 1e-9; const int inf = 2000000000; const double pi = acos(-1.0); const long long mod = 1000000007; using namespace std; char s[150]; int t; long long d[26 * 100 + 200][130]; int main() { scanf( %d n , &t); memset(d, 0, sizeof(d)); for (int num = 0; num <= 120; num++) for (int sum = 0; sum <= 26 * 100 + 100; sum++) { if (num == 0) { if (sum == 0) d[sum][num] = 1; else break; } else for (int last = 0; last < 26; last++) if (sum - last >= 0) d[sum][num] = (d[sum][num] + d[sum - last][num - 1]) % mod; } for (int it = 0; it < (int)t; it++) { gets(s); int n = (int)strlen(s); int sum = 0; for (int i = 0; i < (int)n; i++) sum += s[i] - a ; printf( %I64d n , (d[sum][n] == 0) ? 1000000006 : (d[sum][n] - 1)); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1000 * 100 + 10; const long long INF = 1000 * 1000 * 1000LL * 1000 * 10000; int n, q, pt = 1, cnt; vector<long long> ans; set<pair<long long, long long> > s; set<pair<long long, long long> >::iterator it1, it2; long long po[100], a[N], sum[N * 4][2]; pair<long long, long long> chng[N * 4][2], minn[N * 4][2]; void shift(int ind, bool o, int b, int mid) { if (chng[ind][o].first) { chng[ind * 2][o] = chng[ind * 2 + 1][o] = chng[ind][o]; minn[ind * 2][o] = make_pair(chng[ind][o].second, b); minn[ind * 2 + 1][o] = make_pair(chng[ind][o].second, mid); sum[ind * 2][o] = sum[ind * 2 + 1][o] = 0; chng[ind][o] = make_pair(0, 0); } long long t = sum[ind][o]; sum[ind * 2][o] += t; sum[ind * 2 + 1][o] += t; minn[ind * 2][o].first += t; minn[ind * 2 + 1][o].first += t; sum[ind][o] = 0; return; } void add(int l, int r, pair<long long, long long> val, bool o, int b = 0, int e = n, int ind = 1) { if (l <= b && e <= r) { minn[ind][o].first += val.second; sum[ind][o] += val.second; return; } int mid = (b + e) / 2; shift(ind, o, b, mid); if (l < mid) add(l, r, val, o, b, mid, ind * 2); if (r > mid) add(l, r, val, o, mid, e, ind * 2 + 1); minn[ind][o] = min(minn[ind * 2][o], minn[ind * 2 + 1][o]); return; } void sett(int l, int r, pair<long long, long long> val, bool o, int b = 0, int e = n, int ind = 1) { if (l <= b && e <= r) { chng[ind][o] = val; sum[ind][o] = 0; minn[ind][o] = make_pair(val.second, b); return; } int mid = (b + e) / 2; shift(ind, o, b, mid); if (l < mid) sett(l, r, val, o, b, mid, ind * 2); if (r > mid) sett(l, r, val, o, mid, e, ind * 2 + 1); minn[ind][o] = min(minn[ind * 2][o], minn[ind * 2 + 1][o]); } pair<long long, long long> getmin(int l, int r, int b = 0, int e = n, int ind = 1) { pair<long long, long long> ret = make_pair(INF, INF); if (l <= b && e <= r) return minn[ind][0]; int mid = (b + e) / 2; shift(ind, 0, b, mid); if (l < mid) ret = min(ret, getmin(l, r, b, mid, ind * 2)); if (r > mid) ret = min(ret, getmin(l, r, mid, e, ind * 2 + 1)); return ret; } long long get(int p, bool o, int b = 0, int e = n, int ind = 1) { if (b + 1 == e) return sum[ind][o] + chng[ind][o].second; int mid = (b + e) / 2; shift(ind, o, b, mid); if (p < mid) return get(p, o, b, mid, ind * 2); return get(p, o, mid, e, ind * 2 + 1); } long long type1(int x) { x--; return po[get(x, 1)] - get(x, 0); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> q; po[0] = 1; for (int i = 1; i <= 12; i++) po[i] = po[i - 1] * 42; for (int i = 0; i < n; i++) { cin >> a[i]; a[i] = 1 - a[i]; sett(i, i + 1, make_pair(pt++, a[i]), 0); s.insert(make_pair(i, i + 1)); } long long tp, x, l, r; for (int i = 0; i < q; i++) { cin >> tp; if (tp == 1) { cin >> x; ans.push_back(type1(x)); } else { cin >> l >> r >> x; l--; if (tp == 2) { sett(l, r, make_pair(pt++, 1 - x), 0); sett(l, r, make_pair(pt++, 0), 1); s.insert(make_pair(l, r)); it1 = it2 = s.find(make_pair(l, r)); if (it1 != s.begin() && (*--it1).second > l) { pair<long long, long long> temp = *it1; s.erase(it1); it1 = s.find(make_pair(l, r)); if (temp.first < l) s.insert(make_pair(temp.first, l)); if (temp.second > r) s.insert(make_pair(r, temp.second)); } it2++; while (it2 != s.end() && (*it2).first < r) { pair<long long, long long> temp = *it2; s.erase(it2); it2 = ++s.find(make_pair(l, r)); if (temp.second > r) s.insert(make_pair(r, temp.second)); } } else { bool flag = 1; pair<long long, long long> t1 = *--s.upper_bound(make_pair(l, n + 1)); if (t1.first != l) { s.erase(t1); s.insert(make_pair(t1.first, l)); s.insert(make_pair(l, t1.second)); } pair<long long, long long> t2 = *--s.upper_bound(make_pair(r, n + 1)); if (t2.first != r) { s.erase(t2); s.insert(make_pair(t2.first, r)); if (t2.second != r) s.insert(make_pair(r, t2.second)); } while (flag) { add(l, r, make_pair(pt++, -x), 0); pair<long long, long long> temp = getmin(l, r); while (temp.first < 0) { pair<long long, long long> seg = *--s.upper_bound(make_pair(temp.second, n + 1)); int pnt = get(temp.second, 1); add(seg.first, seg.second, make_pair(pt++, po[pnt + 1] - po[pnt]), 0); sett(seg.first, seg.second, make_pair(pt++, pnt + 1), 1); temp = getmin(l, r); } if (temp.first != 0) flag = 0; } } } } for (long long u : ans) cout << u << endl; }
#include <bits/stdc++.h> using namespace std; long long a, s, d[500002][3], f, g, h, j, k, l, i, n, m; long long inf = 100000000000000000ll; pair<long long, int> p[500003], p1; int main() { cin >> n; for (i = 1; i <= n; i++) { scanf( %d , &a); d[i][0] = a; d[i][1] = i - 1; d[i][2] = i + 1; p[i] = {a, i}; } sort(p + 1, p + n + 1); i = 0; long long y = 0; int L = 1, R = n; while (n - i - 2 > 0) { p1 = p[i + 1]; if (p1.second == L || p1.second == R) { k += (p1.first - y) * (n - i - 2); y = p1.first; if (p1.second == L) L = d[p1.second][2]; else R = d[p1.second][1]; } else { k = k + min(d[d[p1.second][1]][0], d[d[p1.second][2]][0]) - y; d[d[p1.second][1]][2] = d[p1.second][2]; d[d[p1.second][2]][1] = d[p1.second][1]; } i++; } cout << k; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < n - 1; i++) { if (arr[i] % 2 != 0) { if (arr[i + 1] > 0) { arr[i + 1] -= 1; if (arr[i + 1] < 0) { cout << NO ; return 0; } } else { cout << NO ; return 0; } } } if (arr[n - 1] % 2 == 0) { cout << YES ; } else { cout << NO ; return 0; } }
#include <bits/stdc++.h> using namespace std; int a[1000][1000]; int main() { int n, m, x, y; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> x >> y; x--, y--; for (int k = -2; k < 1; k++) for (int l = -2; l < 1; l++) if (x + k >= 0 && x + k < n && y + l >= 0 && y + l < n) { a[x + k][y + l]++; if (a[x + k][y + l] == 9) { cout << i + 1; return 0; } } } cout << -1; return 0; }