func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; const int N = 3e4; int n, aa[N + 2], fib[N + 2], con[N + 2], m; void compress() { vector<int> v; for (int i = 1; i <= n; i++) v.push_back(aa[i]); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); map<int, int> mp; for (int i = 0; i < v.size(); i++) { mp[v[i]] = i + 1; con[i + 1] = v[i] % m; } for (int i = 1; i <= n; i++) aa[i] = mp[aa[i]]; } bool vs[N + 2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> aa[i]; compress(); fib[1] = 1; fib[2] = 1; for (int i = 3; i <= 30000; i++) fib[i] = (fib[i - 1] + fib[i - 2]) % m; int q; cin >> q; while (q--) { int l, r; cin >> l >> r; for (int i = l; i <= r; i++) vs[aa[i]] = 1; long long ans = 0; int cnt = 1; for (int i = 1; i <= 30000; i++) { if (vs[i]) { ans += con[i] * fib[cnt++]; vs[i] = 0; } } cout << ans % m << n ; } return 0; }
#include <bits/stdc++.h> #pragma G++ optimize( Ofast,no-stack-protector ) #pragma G++ optimize( -O3,-O2,-O1 ) #pragma G++ target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native ) #pragma G++ optimize( unroll-loops ) using namespace std; long long binpow(long long a, long long n, long long mod) { long long ans = 1; while (n) { if (n & 1) ans = (ans * a) % mod; a = (a * a) % mod; n >>= 1; } return ans; } long long binpow(long long a, long long n) { long long ans = 1; while (n) { if (n & 1) ans *= a; a *= a; n >>= 1; } return ans; } vector<long long> pr; void PrDoX(long long x) { if (pr.size() > 0) return; vector<long long> p(x + 1); for (long long i = 2; i <= x; i++) { if (p[i] == 0) { pr.push_back(i); for (long long j = i + i; j <= x; j += i) p[j] = 1; } } } vector<long long> fact(long long x) { PrDoX(sqrt(1e9)); vector<long long> ans; long long i = 0, c = sqrt(x), n = pr.size(); while (x > 1 and pr[i] <= c and i < n) { if (x % pr[i] == 0) { x /= pr[i]; ans.push_back(pr[i]); c = sqrt(x); } else i++; } if (x != 1) ans.push_back(x); return ans; } int dp[501][26][26], cost[501][26][26]; pair<int, int> ans[501][26][26]; int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); for (int i = 1; i < 501; i++) for (int j = 0; j < 26; j++) for (int l = 0; l < 26; l++) dp[i][j][l] = 1e9; int n, m; cin >> n >> m; vector<vector<int> > v(n, vector<int>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char c; cin >> c; v[i][j] = c - a ; } } for (int i = 0; i < n; i++) { for (int A = 0; A < 26; A++) { for (int B = 0; B < 26; B++) { if (A == B) continue; for (int j = 0; j < m; j++) cost[i][A][B] += ((A != v[i][j]) * (j % 2 == 0) + (B != v[i][j]) * (j % 2 == 1)); } } } for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) dp[1][i][j] = cost[0][i][j]; } for (int i = 1; i <= n; i++) { for (int A = 0; A < 26; A++) { for (int B = 0; B < 26; B++) { if (A == B) continue; for (int A2 = 0; A2 < 26; A2++) { if (A2 == A) continue; for (int B2 = 0; B2 < 26; B2++) { if (B2 == B or A2 == B2) continue; if (dp[i - 1][A2][B2] + cost[i - 1][A][B] < dp[i][A][B]) { dp[i][A][B] = dp[i - 1][A2][B2] + cost[i - 1][A][B]; ans[i][A][B] = {A2, B2}; } } } } } } int mn = 1e9; pair<int, int> x = {0, 0}; for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if (i != j and dp[n][i][j] < mn) mn = dp[n][i][j], x = {i, j}; } } cout << mn << n ; vector<pair<int, int> > Ans; for (int i = n; i > 0; i--) { Ans.push_back(x); x = ans[i][x.first][x.second]; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < m; j++) { if (j % 2 == 0) cout << char(Ans[i].first + a ); else cout << char(Ans[i].second + a ); } cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 4, M = 1e5 + 10, Mo = 1e9 + 7; long long n, m, nxt[M], p, lena, lenb, lens; string aa, bb, s; struct Matrix { long long n, a[N][N]; Matrix(long long _n = 4) { n = _n, memset(a, 0, sizeof(a)); } void operator~() { for (long long i = 0; i < 4; i++) a[i][i] = 1; } Matrix operator+(const Matrix &b) const { Matrix ret(4); for (long long i = 0; i < 4; i++) for (long long j = 0; j < 4; j++) ret.a[i][j] = (a[i][j] + b.a[i][j]) % Mo; return ret; } Matrix operator-(const Matrix &b) const { Matrix ret(4); for (long long i = 0; i < 4; i++) for (long long j = 0; j < 4; j++) ret.a[i][j] = (a[i][j] - b.a[i][j] + Mo) % Mo; return ret; } Matrix operator*(const Matrix &b) const { Matrix ret(4); for (long long i = 0; i < 4; i++) for (long long j = 0; j < 4; j++) for (long long k = 0; k < 4; k++) ret.a[i][k] = (ret.a[i][k] + 1LL * a[i][j] * b.a[j][k] % Mo + Mo) % Mo; return ret; } Matrix operator^(const long long &b) const { Matrix ret, x, m1, m2; for (long long i = 0; i < 4; i++) for (long long j = 0; j < 4; j++) if (i == j) ret.a[i][j] = 1; else ret.a[i][j] = 0; for (long long i = 0; i < 4; i++) for (long long j = 0; j < 4; j++) m1.a[i][j] = m2.a[i][j] = 0; m1.a[0][0] = m1.a[0][1] = m1.a[1][0] = m1.a[2][2] = m1.a[2][3] = m1.a[3][2] = m2.a[0][0] = m2.a[0][1] = m2.a[1][0] = m2.a[2][0] = m2.a[3][2] = 1; x = m2 * m1; for (long long B = b >> 1; B; B >>= 1, x = x * x) { if (B & 1) ret = ret * x; } if (b & 1) ret = m1 * ret; return ret; } } a, b, c, m1, m2; inline long long read() { long long x = 0, k = 1; char c = getchar(); for (; c < 48 || c > 57; c = getchar()) k ^= (c == - ); for (; c >= 48 && c <= 57; c = getchar()) x = x * 10 + (c ^ 48); return k ? x : -x; } long long kmp1(long long l, long long r) { long long ans = 0; for (long long i = 0, p = 0; i < r; i++) { for (; (~p) && s[p] != aa[i]; p = nxt[p]) ; p = p + 1; if (p == lens && i >= l && i <= r - 1) ans = ans + 1, p = nxt[p]; else if (p == lens) p = nxt[p]; } return ans; } long long kmp2(long long l, long long r) { long long ans = 0; for (long long i = 0, p = 0; i < r; i++) { for (; (~p) && s[p] != bb[i]; p = nxt[p]) ; p = p + 1; if (p == lens && i >= l && i <= r - 1) ans = ans + 1, p = nxt[p]; else if (p == lens) p = nxt[p]; } return ans; } long long KMP(const string &b, long long l, long long r) { long long ans = 0; long long lenb = b.length(); for (long long i = 0, p = 0; i < lenb; i++) { for (; (~p) && s[p] != b[i]; p = nxt[p]) ; p = p + 1; if (p == lens && i >= l && i <= r - 1) ans = ans + 1, p = nxt[p]; else if (p == lens) p = nxt[p]; } return ans; } signed main() { m1.a[0][0] = m1.a[0][1] = m1.a[1][0] = m1.a[2][2] = m1.a[2][3] = m1.a[3][2] = m2.a[0][0] = m2.a[0][1] = m2.a[1][0] = m2.a[2][0] = m2.a[3][2] = 1; std::ios::sync_with_stdio(false); std::cin >> n; long long T; std::cin >> T; for (long long x; T--;) { std::cin >> s; aa = a , bb = b ; lena = aa.length(), lenb = bb.length(), lens = s.length(), nxt[0] = -1, p = 0; ; for (long long i = 0; i < lens; i++) { for (p = nxt[i]; (~p) && s[p] != s[i]; p = nxt[p]) ; nxt[i + 1] = ++p; } if (n == 1) { printf( %lld n , kmp1(0, lena)); } else if (n == 2) { printf( %lld n , kmp2(0, lenb)); } else { for (x = 1; lena < lens && x <= n; ++x) { aa = bb + aa; std::swap(aa, bb); lena = aa.length(); } lenb = bb.length(); if (x > n) printf( 0 n ); else if (x == n) printf( %lld n , kmp1(0, lena)); else { lena = aa.length(), lenb = bb.length(), lens = s.length(); long long tt = n - x - 1; c = c ^ tt; printf( %lld n , (c.a[0][0] % Mo * kmp2(0, lenb) % Mo + c.a[1][0] % Mo * KMP(bb + aa, lenb, lena + lenb) % Mo + c.a[2][0] % Mo * KMP(aa + bb, lena, lena + lens - 1) % Mo + c.a[3][0] % Mo * KMP(bb + bb, lenb, lenb + lens - 1) % Mo) % Mo); } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100005; int main() { string s; cin >> s; int cnt0, cnt1, len = s.size(); string t(len, 0 ); cnt0 = cnt1 = 0; for (int i = len - 1; i >= 0; --i) { if (s[i] == 0 ) ++cnt0; else { if (cnt0 != cnt1) t[i] = 1 ; else ++cnt0; ++cnt1; } } cout << t << n ; return 0; }
#include <bits/stdc++.h> using namespace std; bool is(long long x) { return x != 1; } int main() { int t; cin >> t; while (t--) { long long x, y; cin >> x >> y; long long z = x - y; if (is(z)) cout << YES << endl; else cout << NO << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const long long maxn = 110; struct node { int v, cost; } a[1500]; int N, K, dp1[100 * 150], dp2[100 * 150]; int solve() { memset(dp1, -0x3f3f3f3f, sizeof(dp1)); ; memset(dp2, -0x3f3f3f3f, sizeof(dp2)); ; dp1[1] = dp2[1] = 0; for (int i = 1; i <= N; i++) { if (a[i].cost >= 0) { for (int j = 100 * 105; j >= a[i].cost; j--) dp1[j] = max(dp1[j], dp1[j - a[i].cost] + a[i].v); } else { a[i].cost = -a[i].cost; for (int j = 100 * 105; j >= a[i].cost; j--) dp2[j] = max(dp2[j], dp2[j - a[i].cost] + a[i].v); } } int ans = -1; for (int i = 0; i <= 100 * 105; i++) { if (dp1[i] + dp2[i] != 0 && dp1[i] + dp2[i] > ans) ans = dp1[i] + dp2[i]; } return ans; } int main() { cin >> N >> K; for (int i = 1; i <= N; i++) cin >> a[i].v; int b; for (int i = 1; i <= N; i++) cin >> b, a[i].cost = a[i].v - K * b; cout << solve() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; if (a != d) { cout << 0; } else if (c && !a) { cout << 0; } else { cout << 1; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int N = 1e6 + 5; const int inf = 1 << 30; const long long llf = 9e18; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } struct node { int x, y, time; friend bool operator<(const node &a, const node &b) { return a.time > b.time; } }; bool cmp(int x, int y) { return x < y; } int pic[1010][1010]; int book[1010][1010]; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; int n, m, t; bool check(int x, int y) { bool f1 = false; bool f2 = false; for (register int i = 0; i <= 3; ++i) { if (x + dx[i] < 0 || x + dx[i] > n - 1 || y + dy[i] < 0 || y + dy[i] > m - 1) continue; if (pic[x + dx[i]][y + dy[i]] != pic[x][y]) { f2 = true; } else { f1 = true; book[x][y] = 0; } } if (f1 && !f2) return false; if (f1 && f2) return true; if (!f1 && f2) return false; } int main() { cin >> n >> m >> t; string s; for (register int i = 0; i <= n - 1; ++i) { cin >> s; for (register int j = 0; j <= m - 1; ++j) { pic[i][j] = s[j] - 0 ; } } node now, re; priority_queue<node> q; for (register int i = 0; i <= n - 1; ++i) { for (register int j = 0; j <= m - 1; ++j) { book[i][j] = inf; if (check(i, j)) { now.x = i; now.y = j; now.time = 0; q.push(now); } } } while (!q.empty()) { re = q.top(); q.pop(); int x = re.x, y = re.y; for (register int i = 0; i <= 3; ++i) { if (x + dx[i] < 0 || x + dx[i] > n - 1 || y + dy[i] < 0 || y + dy[i] > m - 1) continue; if (pic[x + dx[i]][y + dy[i]] != pic[x][y]) { now.x = x + dx[i]; now.y = y + dy[i]; now.time = re.time + 1; if (book[now.x][now.y] > now.time) { book[now.x][now.y] = now.time; q.push(now); } } } } long long x, y, p; for (register int i = 1; i <= t; ++i) { cin >> x >> y >> p; x--; y--; if (p < book[x][y] || book[x][y] == inf) cout << pic[x][y] << endl; else cout << (pic[x][y] + (p - 1ll * book[x][y]) % 2) % 2 << endl; } }
#include <bits/stdc++.h> using namespace std; const long long int INF = (long long int)1e9 + 10; const long long int INFLL = (long long int)1e18 + 10; const long double EPS = 1e-8; const long double EPSLD = 1e-18; const long long int MOD = 1e9 + 7; template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); } namespace std { template <class T> bool operator<(const complex<T> &a, const complex<T> &b) { return a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real(); } } // namespace std long long int dot(complex<long long int> a, complex<long long int> b) { return (conj(a) * b).real(); } long long int cross(complex<long long int> a, complex<long long int> b) { return (conj(a) * b).imag(); } long long int ccw(complex<long long int> a, complex<long long int> b, complex<long long int> c) { b -= a, c -= a; if (cross(b, c) > 0) return +1; if (cross(b, c) < 0) return -1; if (dot(b, c) < 0) return +2; if (norm(b) < norm(c)) return -2; return 0; } inline bool cross(const complex<long long int> &a, const complex<long long int> &b, const complex<long long int> &c, const complex<long long int> &d) { return ccw(a, c, b) * ccw(a, d, b) == -1 && ccw(c, a, d) * ccw(c, b, d) == -1; } vector<complex<long long int> > convex_hull( vector<complex<long long int> > ps) { int n = ((long long int)(ps).size()); int k = 0; sort((ps).begin(), (ps).end()); vector<complex<long long int> > ch(2 * n); for (int i = 0; i < n; ch[k++] = ps[i++]) while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) k--; for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0) k--; ch.resize(k - 1); return ch; } enum { OUT, ON, IN }; int contains(const vector<complex<long long int> > pol, const complex<long long int> p) { bool in = false; for (int i = 0; i < pol.size(); ++i) { complex<long long int> a = pol[i] - p, b = pol[(i + 1) % pol.size()] - p; if (imag(a) > imag(b)) swap(a, b); if (imag(a) <= 0 && 0 < imag(b)) if (cross(a, b) < 0) in = !in; if (cross(a, b) == 0 && dot(a, b) <= 0) return ON; } return in ? IN : OUT; } long long int n, r; long long int ansv; vector<complex<long long int> > ans; vector<complex<long long int> > cp; vector<complex<long long int> > ps; void dfs(long long int c, long long int k, long long int res) { if (c == n) { if (ansv < res) { ansv = res; ans = cp; } return; } for (long long int i = (k); i < (long long int)(((long long int)(ps).size())); i++) { long long int nres = res; for (long long int j = (0); j < (long long int)(c); j++) nres += norm(ps[i] - cp[j]); cp.emplace_back(ps[i]); dfs(c + 1, i, nres); cp.pop_back(); } } int main() { scanf( %lld %lld , &n, &r); for (long long int i = (-r); i < (long long int)(r + 1); i++) for (long long int j = (-r); j < (long long int)(r + 1); j++) if (i * i + j * j <= r * r) ps.emplace_back(complex<long long int>(i, j)); ps = convex_hull(ps); if (n % 2 == 0) ps = {complex<long long int>(r, 0), complex<long long int>(-r, 0)}; dfs(0, 0, 0); printf( %lld n , ansv); for (auto &w : ans) printf( %lld %lld n , w.real(), w.imag()); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; long double r, v; long double eps = 1e-8; const long double mpi = acos(-1.0); inline long double dist(long double theta) { return 2.0L * r * (sin(theta) + theta); } inline double ti(long double theta, long long ni) { return 2.0L * r * (mpi * ni + theta) / v; } void solve() { int si, fi; scanf( %d%d , &si, &fi); long double d = fi - si; long long ni = d / (2.0L * mpi * r); long double left = 0; long double right = mpi; d -= 2.0L * r * mpi * ni; long double dleft = dist(left); long double dright = dist(right); long double e = eps * v; while (dright - dleft > e) { long double mid = (right + left) / 2.0L; long double dmid = dist(mid); if (dmid > d) { right = mid; dright = dmid; } else { left = mid; dleft = dmid; } } double res = ti(right, ni); printf( %.10f n , res); return; } int main() { int n, ri, vi; scanf( %d%d%d , &n, &ri, &vi); r = ri; v = vi; while (n--) { solve(); } cout.flush(); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) using namespace std; template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << [ ; for (auto v : V) os << v << ; return os << ] ; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << ( << P.first << , << P.second << ) ; } template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << : << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, , ); cerr.write(names, comma - names) << : << arg1 << | ; __f(comma + 1, args...); } const long long INF = 0x3f3f3f3f; const long long M = 1e9 + 7; const long long N = 1e5 + 5; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed << setprecision(15); long long n, k; cin >> n >> k; long long l[n], r[n]; for (long long i = 0; i < n; ++i) cin >> l[i] >> r[i]; vector<long long> v; for (long long i = 0; i < n; ++i) v.push_back(l[i]), v.push_back(r[i]); sort((v).begin(), (v).end()); map<long long, long long> ml, mr; for (long long i = 0; i < 2 * n; ++i) mr[v[i]] = i; for (long long i = 0; i < 2 * n; ++i) ml[v[2 * n - 1 - i]] = 2 * n - 1 - i; long long a[2 * n]; memset(a, 0, sizeof(a)); for (long long i = 0; i < n; ++i) a[ml[l[i]]]++, a[mr[r[i]]]--; partial_sum(a, a + 2 * n, a); vector<long long> w; for (long long i = 0; i < 2 * n; ++i) { if (w.size() % 2 == 0) { if (a[i] >= k) { w.push_back(v[i]); } } else { if (a[i] < k) { w.push_back(v[i]); } } } cout << w.size() / 2 << n ; for (long long i = 0; i < w.size(); i += 2) { cout << w[i] << << w[i + 1] << n ; } }
#include <bits/stdc++.h> using namespace std; struct less_key { bool operator()(pair<int64_t, int64_t> p1, pair<int64_t, int64_t> p2) { return (p1.first > p2.first) || ((p1.first == p2.first) && (p1.second < p2.second)); } }; struct pair_hash { std::size_t operator()(const pair<int64_t, int64_t>& k) const { return static_cast<size_t>(k.first ^ k.second); } }; const int64_t mod = 100000000007ll; const int64_t inf = 10000000000000007ll; int64_t n, m; char a[10004][10004]; int64_t sx, sy; int64_t tx, ty; bool x1[10004], x2[10004]; bool _q1[10004], y2[10004]; bool check_x(int64_t x1, int64_t x2, int64_t y) { bool f = 1; int64_t q = x1 < x2 ? 1 : -1; for (int64_t i = x1;; i += q) { if (a[i][y] == * ) { f = 0; break; } if (i == x2) { break; } } return f; } bool check_y(int64_t _q1, int64_t y2, int64_t x) { bool f = 1; int64_t q = _q1 < y2 ? 1 : -1; for (int64_t i = _q1;; i += q) { if (a[x][i] == * ) { f = 0; break; } if (i == y2) { break; } } return f; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int64_t i = 0; i < n; i++) { for (int64_t j = 0; j < m; j++) { cin >> a[i][j]; if (a[i][j] == S ) { sx = i; sy = j; } else if (a[i][j] == T ) { tx = i; ty = j; } } } if (sx == tx) { bool f = 1; int64_t q = sy < ty ? 1 : -1; for (int64_t i = sy; i != ty; i += q) { if (a[sx][i] == * ) { f = 0; break; } } if (f) { cout << YES ; return 0; } } if (sy == ty) { bool f = 1; int64_t q = sx < tx ? 1 : -1; for (int64_t i = sx; i != tx; i += q) { if (a[i][sy] == * ) { f = 0; break; } } if (f) { cout << YES ; return 0; } } bool f = 0; for (int64_t i = 0; i < n && !f; i++) { if (check_x(i, sx, sy) && check_x(i, tx, ty) && check_y(sy, ty, i)) { f = 1; } } for (int64_t j = 0; j < m && !f; j++) { if (check_y(j, sy, sx) && check_y(j, ty, tx) && check_x(sx, tx, j)) { f = 1; break; } } cout << (f ? YES : NO ); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 10010; char s[N]; char t[N]; int n, m; int main() { scanf( %s , s + 1); n = strlen(s); scanf( %d , &m); while (m--) { int l, r, k; scanf( %d%d%d , &l, &r, &k); k %= (r - l + 1); for (int i = 0; i < k; i++) { t[l + i] = s[r - k + 1 + i]; } for (int i = k; i <= r - l; i++) t[l + i] = s[l - k + i]; for (int i = l; i <= r; i++) s[i] = t[i]; } printf( %s n , s + 1); return 0; }
#include <bits/stdc++.h> const int N = 200; char a[N][N]; int main() { int n, m, k; std::cin >> n >> m >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { std::cin >> a[i][j]; } } int ret = 0; int replaces = 0; for (int j = 0; j < m; j++) { int past = 0; int maxOnes = 0; int curReplaces = 0; for (int i = 0; i < n; i++) { if (a[i][j] != 1 ) continue; int ones = 0; for (int x = i; x < std::min(n, i + k); x++) { if (a[x][j] == 1 ) ones++; } if (ones > maxOnes) { maxOnes = ones; curReplaces = past; } past++; } ret += maxOnes; replaces += curReplaces; } std::cout << ret << << replaces << n ; return 0; }
#include <bits/stdc++.h> using namespace std; inline int qpow(int b, int e, int m = 998244353) { int a = 1; for (; e; e >>= 1, b = (long long)b * b % m) if (e & 1) a = (long long)a * b % m; return a; } struct node { long long sum; long long pre; long long suf; long long ans; }; node segt[1000000]; node combine(node &a, node &b) { node c; c.sum = a.sum + b.sum; c.pre = max(a.pre, a.sum + b.pre); c.suf = max(b.suf, b.sum + a.suf); c.ans = max(max(a.ans, b.ans), a.suf + b.pre); return c; } void build(long long i, long long tl, long long tr, vector<long long> &d, vector<long long> &h) { if (tl == tr) { node t; t.ans = 0; t.sum = d[tl]; t.pre = 2 * h[tl]; t.suf = 2 * h[tl] + d[tl]; segt[i] = t; return; } long long tm = (tl + tr) / 2; build(i * 2, tl, tm, d, h); build(i * 2 + 1, tm + 1, tr, d, h); node c = combine(segt[i * 2], segt[i * 2 + 1]); segt[i] = c; } node getans(long long i, long long tl, long long tr, long long l, long long r) { if (l > r) { node t; t.sum = t.pre = t.suf = t.ans = INT_MIN; return t; } if (tl == l && tr == r) return segt[i]; long long tm = (tl + tr) / 2; node a = getans(i * 2, tl, tm, l, min(tm, r)); node b = getans(i * 2 + 1, tm + 1, tr, max(tm + 1, l), r); node c = combine(a, b); ; return c; } void pn(node a) { cout << ans : << a.ans << pre: << a.pre << suf : << a.suf << sum : << a.sum << endl; } void solve() { long long n, m; cin >> n >> m; vector<long long> d(2 * n + 1); vector<long long> h(2 * n + 1); for (int i = 1; i <= n; i++) { cin >> d[i]; d[i + n] = d[i]; } for (int i = 1; i <= n; i++) { cin >> h[i]; h[i + n] = h[i]; } build(1, 1, 2 * n, d, h); while (m--) { long long a, b; cin >> a >> b; long long l, r; l = b + 1; if (a <= b) { r = n + a - 1; } else { r = a - 1; } auto ans = getans(1, 1, 2 * n, l, r); cout << ans.ans << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int a[200002]; pair<int, int> b[200002]; int main() { int n, k, max = 0; for (int i = 0; i < 200001; i++) { a[i] = 2147483647; } scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d %d , &b[i].first, &b[i].second); } sort(b, b + n); for (int i = 0; i < n; i++) { int d = b[i].first - b[i].second; int* c = upper_bound(a, a + max, d); int index = c - a; if (index + 1 > max) { max = index + 1; } a[index] = min(a[index], b[i].first + b[i].second); } printf( %d n , max); return 0; }
#include <bits/stdc++.h> using namespace std; int n, k, p[200005]; int vis[200005], cntn; int pre[200005], nxt[200005]; int flag = 0; struct node { int p, s; node(int p = 0, int s = 0) : p(p), s(s) {} bool operator<(const node &b) const { return p < b.p; } }; void del(int x) { nxt[pre[x]] = nxt[x]; pre[nxt[x]] = pre[x]; vis[x] = flag + 1; cntn--; } priority_queue<node> q; int main() { cin >> n >> k; cntn = n; for (int i = 1; i <= n; i++) { scanf( %d , &p[i]); pre[i] = i - 1; nxt[i] = i + 1; q.push(node(p[i], i)); } while (cntn) { while (vis[q.top().s] != 0) q.pop(); node t = q.top(); q.pop(); del(t.s); int cur = pre[t.s]; for (int i = 0; i < k && cur != 0; i++, cur = pre[cur]) del(cur); cur = nxt[t.s]; for (int i = 0; i < k && cur != n + 1; i++, cur = nxt[cur]) del(cur); flag = 1 - flag; } for (int i = 1; i <= n; i++) cout << vis[i]; return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int n, m, k; int a[11][11]; int f[11][11]; int cnt[21]; long long read() { long long x = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == - ) f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - 0 ; c = getchar(); } return x * f; } void write(long long x) { if (x < 0) putchar( - ), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + 0 ); } void writern(long long x) { write(x), putchar( ); } void writeln(long long x) { write(x), puts( ); } long long dfs(int x, int y) { if (y > m) { y = 1, x++; } if (x > n) return 1; long long res = 0; int s = f[x - 1][y] | f[x][y - 1]; int cc = 0, flag = -1; for (int i = s; i; i -= (i & -i)) cc++; if (m + n - x - y + 1 > k - cc) return 0; for (int i = s ^ ((1 << k) - 1); i; i -= (i & -i)) { int t = i & (-i); int tt = log2(t) + 1; if (a[x][y] == 0 || a[x][y] == tt) { f[x][y] = s | t; cnt[tt]++; if (cnt[tt] == 1) { if (flag == -1) flag = dfs(x, y + 1); res += flag; } else if (cnt[tt]) res += dfs(x, y + 1); res %= mod; cnt[tt]--; } } return res; } int main() { n = read(), m = read(); k = read(); if (n + m - 1 > k) { writeln(0); return 0; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { a[i][j] = read(); if (a[i][j]) cnt[a[i][j]]++; } writeln(dfs(1, 1) % mod); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 2 == 0) { cout << home << endl; return 0; } else { cout << contest << endl; return 0; } }
#include <bits/stdc++.h> using namespace std; int main() { long long n, s1 = 0, s2 = 0, s = 0; cin >> n; s1 = ((n + 1) * n) / 2, s2 = s1 / 2; vector<long> a; for (long i = n; i > 0 && s2 > 0; --i) if (s2 >= i) if (s2 - i < i && s2 - i != 0) { a.push_back(i), a.push_back(s2 - i), s += s2; break; } else a.push_back(i), s2 -= i, s += i; cout << abs(s1 - s - s) << n << a.size() << ; for (long i = 0; i < a.size(); cout << a[i] << , ++i) ; }
#include <bits/stdc++.h> using namespace std; vector<int> t; int sz = 0; const int INF = 2e9; void build(vector<int> a) { t.assign(2 * sz + 2, INF); for (int i = 0; i < sz; i++) { t[i + sz] = a[i]; } for (int i = sz - 1; i > 0; i--) { t[i] = min(t[2 * i], t[2 * i + 1]); } } int get(int v, int tl, int tr, int l, int r) { if (tl > r || l > tr) { return INF; } if (tl == l && tr == r) { return t[v]; } int tm = (tl + tr) / 2; return min(get(2 * v, tl, tm, l, min(r, tm)), get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; sz = 1; while (sz < 4 * n) { sz *= 2; } vector<int> a(sz); vector<pair<int, int>> q; vector<pair<int, int>> qq; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; q.emplace_back(l, 0); qq.emplace_back(l, r); q.emplace_back(r, 1); q.emplace_back(l - 1, -1); q.emplace_back(r + 1, -1); } sort(q.begin(), q.end()); int b = 0; for (int i = 0; i < 4 * n; i++) { if (q[i].second == 0) { b++; int id = lower_bound(q.begin(), q.end(), make_pair(q[i].first, -1)) - q.begin(); a[id] = max(a[id], b); } else { int id = lower_bound(q.begin(), q.end(), make_pair(q[i].first, -1)) - q.begin(); a[id] = max(a[id], b); if (q[i].second == 1) b--; } } for (int i = 0; i < a.size(); i++) { if (a[i] == 0) { a[i] = INF; } } build(a); for (int i = 0; i < n; i++) { int minimum = get( 1, 0, sz - 1, lower_bound(q.begin(), q.end(), make_pair(qq[i].first, -1)) - q.begin(), lower_bound(q.begin(), q.end(), make_pair(qq[i].second, -1)) - q.begin()); if (minimum > 1 && minimum != INF) { cout << i + 1; return 0; } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long infinity = 1000000000000000000; const int inf = 1e9 + 5; bool do_debug = false; template <typename T> ostream& operator<<(ostream& os, vector<T>& v) { for (auto element : v) { os << element << ; } return os; } template <typename T, typename S> ostream& operator<<(ostream& os, pair<T, S>& p) { os << ( << p.first << , << p.second << ) ; return os; } template <typename T> ostream& operator<<(ostream& os, set<T>& v) { if (v.size() == 0) { os << empty set n ; return os; } auto endit = v.end(); endit--; os << [ ; for (auto it = v.begin(); it != v.end(); it++) { os << *it; if (it != endit) { os << , ; } } os << ] ; return os; } template <typename T> ostream& operator<<(ostream& os, multiset<T>& v) { if (v.size() == 0) { os << empty multiset n ; return os; } auto endit = v.end(); endit--; os << [ ; for (auto it = v.begin(); it != v.end(); it++) { os << *it; if (it != endit) { os << , ; } } os << ] ; return os; } template <typename T, typename S> ostream& operator<<(ostream& os, map<T, S>& v) { if (v.size() == 0) { os << empty map n ; return os; } auto endit = v.end(); endit--; os << { ; for (auto it = v.begin(); it != v.end(); it++) { os << ( << (*it).first << : << (*it).second << ) ; if (it != endit) { os << , ; } } os << } ; return os; } template <typename T> ostream& operator<<(ostream& os, vector<vector<T>>& v) { for (auto& subv : v) { for (auto& e : subv) { os << e << ; } os << n ; } return os; } const int maxn = 3e5 + 5; int n_nodes, n_edges, target; vector<vector<int>> adj; vector<array<int, 4>> edges; long long dist[maxn]; long long depth[maxn]; int incoming[maxn]; int added[maxn]; set<int> ans; long long ans_w; void dijkstra() { int node = target; priority_queue<pair<long long, long long>> pq; pq.push(make_pair(0, target)); while (!pq.empty()) { long long w = -pq.top().first; node = pq.top().second; pq.pop(); if (w > dist[node]) { continue; } for (int e : adj[node]) { int next = edges[e][1]; if (next == node) next = edges[e][2]; if (dist[node] + edges[e][0] < dist[next]) { if (incoming[next] != -1) { ans_w -= edges[incoming[next]][0]; ans.erase(incoming[next]); } incoming[next] = e; ans_w += edges[incoming[next]][0]; ans.insert(incoming[next]); dist[next] = dist[node] + edges[e][0]; pq.push(make_pair(-dist[next], next)); } else if (dist[node] + edges[e][0] == dist[next]) { if (edges[e][0] < edges[incoming[next]][0]) { ans_w -= edges[incoming[next]][0]; ans.erase(incoming[next]); incoming[next] = e; ans_w += edges[incoming[next]][0]; ans.insert(incoming[next]); } } } } } void mst() { int node = target; priority_queue<pair<long long, long long>> pq; for (int e : adj[node]) { int u = edges[e][1], v = edges[e][2]; if (u != node) swap(u, v); depth[v] = edges[e][0]; incoming[v] = e; ans_w += edges[e][0]; ans.insert(e); pq.push(make_pair(-edges[e][0], v)); } while (!pq.empty()) { long long d = -pq.top().first; node = pq.top().second; pq.pop(); for (int e : adj[node]) { long long w = edges[e][0]; int u = edges[e][1], v = edges[e][2]; if (u != node) swap(u, v); if (depth[u] + w == dist[v]) { if (incoming[v] == -1) { ans_w += w; ans.insert(e); incoming[v] = e; } else { if (w < edges[incoming[v]][0]) { ans_w -= edges[incoming[v]][0]; ans.erase(incoming[v]); incoming[v] = e; ans_w += edges[incoming[v]][0]; ans.insert(incoming[v]); } } depth[v] = dist[v]; } } } } void solve() { for (int i = 0; i < maxn; i++) { incoming[i] = -1; dist[i] = infinity; depth[i] = infinity; added[i] = 0; } cin >> n_nodes >> n_edges; adj.resize(n_nodes); for (int i = 0; i < n_edges; i++) { int w, u, v; cin >> u >> v >> w; u--; v--; adj[u].push_back(i); adj[v].push_back(i); edges.push_back({w, u, v, i}); } cin >> target; target--; dist[target] = 0; depth[target] = 0; ans_w = 0; dijkstra(); cout << ans_w << n ; for (int a : ans) { cout << a + 1 << ; } cout << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int q = 1; while (q-- > 0) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; scanf( %d %d , &n, &k); vector<int> a(n); for (int i = 0; i < n; ++i) scanf( %d , &a[i]); int sum = 0; for (int i = 0; i < n; ++i) sum += a[i]; if (sum % k) { printf( No n ); return 0; } int req = sum / k; int cur = 0; int len = 0; for (int i = 0; i < n; ++i) { if (a[i] > req) { printf( No n ); return 0; } } vector<int> ans; for (int i = 0; i < n; ++i) { cur += a[i]; len++; if (cur == req) { ans.push_back(len); cur = 0; len = 0; } else if (cur > req) { cur = a[i]; len = 1; } } if ((int)ans.size() == k) { printf( Yes n ); for (int i = 0; i < k; ++i) printf( %d , ans[i]); } else { printf( No n ); } return 0; }
#include <bits/stdc++.h> using namespace std; set<int> S1, S2, S3; map<int, bool> vis; int p[2000001], a[200001], b[2000001]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> p[i]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < n; i++) { if (a[i] == 1 || b[i] == 1) S1.insert(p[i]); if (a[i] == 2 || b[i] == 2) S2.insert(p[i]); if (a[i] == 3 || b[i] == 3) S3.insert(p[i]); } int m; cin >> m; for (int i = 0; i < m; i++) { int c; cin >> c; if (c == 1) { if (S1.empty()) cout << -1 << ; else { auto ans = S1.begin(); S2.erase(*ans); S3.erase(*ans); cout << *ans << ; S1.erase(*ans); } } else if (c == 2) { if (S2.empty()) cout << -1 << ; else { auto ans = S2.begin(); S1.erase(*ans); S3.erase(*ans); cout << *ans << ; S2.erase(*ans); } } else { if (S3.empty()) cout << -1 << ; else { auto ans = S3.begin(); S1.erase(*ans); S2.erase(*ans); cout << *ans << ; S3.erase(*ans); } } } }
#include <bits/stdc++.h> using namespace std; char s[5010]; int main() { scanf( %s , s); int n = (int)strlen(s); int res = 0; for (int i = 0; i < n; i++) { int cnt1 = 0, cnt2 = 0; for (int j = i; j < n; j++) { if (s[j] == ( ) cnt1++; if (s[j] == ) ) cnt1--; if (s[j] == ? ) cnt2++; if (cnt2 && cnt2 > cnt1) cnt2--, cnt1++; if (cnt1 < 0) break; if (cnt2 == cnt1) res++; } } printf( %d , res); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 400000 + 10; const int M = 26; namespace Trie { int trie[N][M], cnt[N], size; void init() { size = 0; memset(trie[0], 0, sizeof(trie[0])); cnt[0] = 0; } void insert(char str[]) { int u = 0, v; for (int i = 0; str[i]; i++) { v = str[i] - a ; if (!trie[u][v]) { trie[u][v] = ++size; cnt[size] = 0; memset(trie[size], 0, sizeof(trie[size])); } u = trie[u][v]; } cnt[u]++; } } // namespace Trie namespace SAM { struct state { int len, link, siz; int next[M]; } st[N]; int sz, last; void init() { sz = 1, last = 0; st[0].len = 0; st[0].link = -1; } void add(int c) { int r = sz++; st[r].len = st[last].len + 1; st[r].siz = 1; int p = last; while (p != -1 && !st[p].next[c]) st[p].next[c] = r, p = st[p].link; if (p == -1) st[r].link = 0; else { int q = st[p].next[c]; if (st[q].len == st[p].len + 1) st[r].link = q; else { int qq = sz++; st[qq].link = st[q].link; memcpy(st[qq].next, st[q].next, sizeof(st[q].next)); st[qq].len = st[p].len + 1; st[qq].siz = 0; while (p != -1 && st[p].next[c] == q) st[p].next[c] = qq, p = st[p].link; st[q].link = st[r].link = qq; } } last = r; } int tp[N], tmp[N]; void get_topo() { memset(tmp, 0, sizeof(tmp)); for (int i = 0; i < sz; i++) tmp[st[i].len]++; for (int i = 1; i < sz; i++) tmp[i] += tmp[i - 1]; for (int i = 0; i < sz; i++) tp[--tmp[st[i].len]] = i; } void calc_siz() { get_topo(); for (int i = sz - 1; i > 0; i--) st[st[tp[i]].link].siz += st[tp[i]].siz; st[0].siz = 0; } } // namespace SAM long long dfs(int tu, int su) { long long ans = 1ll * Trie::cnt[tu] * SAM::st[su].siz; for (int i = 0; i < M; i++) { int tv = Trie::trie[tu][i]; int sv = SAM::st[su].next[i]; if (tv == 0 || sv == 0) continue; ans += dfs(tv, sv); } return ans; } int id[N]; void ddfs(int i) { if (id[i] == -1) return; for (int j = 0; j < M; j++) { int v = Trie::trie[i][j]; if (v) { id[v] = SAM::st[id[i]].next[j]; if (id[v] == 0) id[v]--; ddfs(v); } } } char s[N]; int main() { SAM::init(), Trie::init(); scanf( %s , s); for (int i = 0; s[i]; i++) SAM::add(s[i] - a ); SAM::calc_siz(); int n; scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %s , s); Trie::insert(s); } ddfs(0); long long ans = 0; for (int i = 0; i <= Trie::size; i++) { if (id[i] <= 0) continue; if (Trie::cnt[i]) ans += 1ll * Trie::cnt[i] * dfs(0, id[i]); } printf( %lld n , ans); }
#include <bits/stdc++.h> using namespace std; int log(int x) { return 32 - __builtin_clz(x) - 1; } int log(long long x) { return 64 - __builtin_clzll(x) - 1; } int n; int main() { cin >> n; if (n % 2 == 0) { cout << NO ; return 0; } cout << YES n ; bool add = false; for (int i = 1; i < 2 * n; i += 2, add = !add) cout << i + add << ; add = true; for (int i = 1; i < 2 * n; i += 2, add = !add) cout << i + add << ; }
#include <bits/stdc++.h> using namespace std; const long long N = 5e5 + 100; const long long maxN = 6e6; const long long oo = 1e18; const long long mod = 998244353; long long n, m; long long a[N], b[N], cost[N], equalpos[N]; long long IT[20 * N], lazy[20 * N]; map<long long, long long> mp; void updmin(long long &x, long long y) { x = min(x, y); } void addlazy(long long node, long long nlef, long long nrig) { if (nlef > nrig || !lazy[node]) return; if (nlef == nrig && IT[node] < oo) IT[node] += lazy[node]; if (nlef < nrig) { lazy[2 * node] += lazy[node]; lazy[2 * node + 1] += lazy[node]; } lazy[node] = 0; } void upd(long long node, long long nlef, long long nrig, long long lef, long long rig, long long val, bool flag) { addlazy(node, nlef, nrig); if (nlef > rig || nrig < lef || nlef > nrig) return; if (lef <= nlef && nrig <= rig) { if (!flag) { lazy[node] += val; addlazy(node, nlef, nrig); } else IT[node] = val; return; } long long mid = (nlef + nrig) / 2; upd(2 * node, nlef, mid, lef, rig, val, flag); upd(2 * node + 1, mid + 1, nrig, lef, rig, val, flag); } long long get(long long node, long long nlef, long long nrig, long long pos) { addlazy(node, nlef, nrig); if (nlef > pos || pos > nrig) return oo; if (nlef == nrig) { return IT[node]; } long long mid = (nlef + nrig) / 2; return min(get(2 * node, nlef, mid, pos), get(2 * node + 1, mid + 1, nrig, pos)); } signed main() { ios_base::sync_with_stdio(false); cout.tie(0); cin >> n; for (long long i = 1; i <= n; ++i) cin >> a[i]; for (long long i = 1; i <= n; ++i) cin >> cost[i]; cin >> m; for (long long i = 1; i <= m; ++i) cin >> b[i], mp[b[i]] = i; for (long long i = 1; i <= n; ++i) if (mp.find(a[i]) != mp.end()) equalpos[i] = mp[a[i]]; for (long long i = 0; i <= 20 * N - 1; ++i) IT[i] = oo; if (a[1] < b[1]) upd(1, 0, m, 0, 0, cost[1], 1); else if (a[1] == b[1]) { upd(1, 0, m, 0, 0, cost[1], 1); upd(1, 0, m, 1, 1, 0, 1); } else upd(1, 0, m, 0, 0, cost[1], 1); for (long long i = 2; i <= n; ++i) { long long pos = equalpos[i]; if (pos) { long long val = get(1, 0, m, pos - 1); long long tmp = get(1, 0, m, pos); if (cost[i] < 0 && tmp < oo) updmin(val, tmp + cost[i]); updmin(val, get(1, 0, m, pos)); upd(1, 0, m, pos, pos, val, 1); } pos = lower_bound(b + 1, b + m + 1, a[i]) - b - 1; if (pos >= 0) upd(1, 0, m, 0, pos, cost[i], 0); else pos = 0; while (pos <= m && b[pos] <= a[i]) pos += 1; if (cost[i] < 0 && pos <= m) upd(1, 0, m, pos, m, cost[i], 0); } long long ans = get(1, 0, m, m); if (ans < oo) return cout << YES n << ans, 0; cout << NO ; }
#include <bits/stdc++.h> using namespace std; mt19937_64 rnd; const long long maxn = 1e6 + 10; const long long mod = 998244353; const long long base = 500; long long gt[maxn]; long long gtv[maxn]; long long mu(long long a, long long n) { if (n == 0) return 1; if (n == 1) return a; long long t = mu(a, n / 2); if (n % 2 == 0) return (t * t) % mod; return ((t * t) % mod * a) % mod; } void setup() { gt[0] = 1; for (int i = 1; i < maxn; i++) { gt[i] = (gt[i - 1] * i) % mod; } gtv[maxn - 1] = mu(gt[maxn - 1], mod - 2); for (int i = maxn - 2; i >= 0; i--) { gtv[i] = (gtv[i + 1] * (i + 1)) % mod; } } long long nck(long long n, long long k) { if (n < k) return 0; return ((gt[n] * gtv[k]) % mod * gtv[n - k]) % mod; } long long f[maxn]; long long f1[maxn]; long long f2[maxn]; long long dp[maxn][2]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (fopen( t.inp , r )) { freopen( test.inp , r , stdin); freopen( test.out , w , stdout); } setup(); string s; cin >> s; long long n = s.length(); s = + s; for (int i = 1; i <= n; i++) { f[i] = f[i - 1] + (s[i] == ( ); f1[i] = f1[i - 1] + (s[i] == ? ); f2[i] = f2[i - 1] + (s[i] == ) ); } long long all = f1[n]; for (int j = 0; j <= min(all, 1ll); j++) { for (int i = 0; i <= n; i++) { dp[i][j] = ((i > 0 ? dp[i - 1][j] : 0) + nck(all - j, i)) % mod; } } long long ans = 0; for (int i = 1; i <= n; i++) { if (s[i] == ) ) continue; long long a = f[i - 1]; long long b = f2[n] - f2[i]; long long c = f1[i - 1]; long long d = f1[n] - f1[i]; if (d + b - a > 0) ans = (ans + dp[d + b - a - 1][s[i] == ? ]) % mod; } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string a, b; cin >> a >> b; int one[2]; one[0] = one[1] = 0; for (char ch : a) one[0] += ch == 1 ? 1 : 0; for (char ch : b) one[1] += ch == 1 ? 1 : 0; if (one[0] + (one[0] & 1) >= one[1]) cout << YES ; else cout << NO ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int arr[100005]; int pos[100005], pr[100005] = {0}; int i, j, ans = 0; for (i = 0; i < n; i++) { cin >> arr[i]; if (pr[arr[i]] == 0) ans++; pr[arr[i]]++; pos[arr[i]] = i; if (ans == k) { j = i; break; } } for (i = j + 1; i < n; i++) cin >> arr[i]; if (ans < k) cout << -1 -1 ; else { int min = j + 1; for (i = 0; i < j; i++) { if (pos[arr[i]] < min) min = pos[arr[i]]; } if (j != 0) cout << min + 1 << << j + 1; else cout << 1 1 ; } return (0); }
#include <bits/stdc++.h> using namespace std; long long n, m, T, x, y, x2, y2, a, b, sol = 1; char c; string s; bool t; vector<int> V; vector<int> V2; int p, p2, mn = INT_MAX; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> x; V.push_back(x); } for (int i = 0; i < m; i++) { cin >> x; V2.push_back(x); } p = n - 1; for (int i = 0; i < m - 1; i++) { x = V2[m - i - 1]; while (p >= 0 && V[p] > x) { p--; } if (p < 0) { cout << 0; return 0; } if (V[p] < x) sol = 0; p2 = p; while (p >= 0 && V[p2] >= x) { p2--; } if (p2 < 0) { cout << 0; return 0; } sol *= p - p2; sol %= 998244353; p = p2; } x = V2[0]; for (int i = 0; i <= p; i++) { mn = min(V[i], mn); } if (mn != x) sol = 0; cout << sol; }
#include <bits/stdc++.h> using namespace std; int s[400], n, ret = 360; int main() { cin >> n; for (int i = 1; i <= n; i++) { int t; cin >> t; s[i] = s[i - 1] + t; } for (int i = 0; i < n; i++) { for (int j = i + 1; j <= n; j++) { ret = min(ret, abs(360 - 2 * (s[j] - s[i]))); } } cout << ret << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; const int M = 1e4 + 5; const int Max = (1 << 26) + 5; short a[Max]; int n, m, b[M], ans[M]; char str[N], sets[30]; int main() { scanf( %s , str + 1); n = strlen(str + 1); scanf( %d , &m); for (int i = 1; i <= m; i++) { scanf( %s , sets); int len = strlen(sets); for (int j = 0; j < len; j++) b[i] |= 1 << (sets[j] - a ); a[b[i]] = i; } for (int i = 1; i <= n; i++) for (int j = i, s = 0; j <= n; j++) { if (str[j] == str[i - 1]) break; s |= 1 << (str[j] - a ); if (!(s >> (str[j + 1] - a ) & 1)) ans[a[s]]++; } for (int i = 1; i <= m; i++) printf( %d n , ans[a[b[i]]]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, d, h; cin >> n >> d >> h; if (h * 2 < d) { cout << -1; return 0; } if (d == 1) { if (n >= 3) { cout << -1; return 0; } if (n == 2) { cout << 1 << << 2; return 0; } } for (int i = 2; i <= h + 1; i++) { cout << i << << i - 1 << endl; } int b = d - h; if (b > 0) { cout << 1 << << h + 2 << endl; for (int i = h + 3; i <= d + 1; i++) { cout << i << << i - 1 << endl; } } int hd = 1; if (b == 0) { hd = 2; } for (int i = d + 2; i <= n; i++) { cout << hd << << i << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; vector<int> vec(1, 0); vector<vector<int> > list; int counter = 0, i, j; for (i = 0; i < s.length(); i++) { if (s[i] == 0 ) { if (list.size() > counter) { list[counter].push_back(i); } else { vec[0] = i; list.push_back(vec); } counter++; } else if (s[i] == 1 ) { if (counter - 1 < 0) { cout << -1; return 0; } else { list[--counter].push_back(i); } } } if (counter < list.size()) { cout << -1; return 0; } cout << list.size() << n ; for (i = 0; i < list.size(); i++) { cout << list[i].size() << ; for (j = 0; j < list[i].size(); j++) { cout << list[i][j] + 1 << ; } cout << n ; } return 0; }
// minako #include <bits/stdc++.h> using namespace std; #define endl n #define f first #define s second #define pb push_back #define pii pair<int,int> #define ll long long #define pll pair <ll,ll> #define faster ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); const ll M = 1e9 + 7; const int N = 2e5 + 5; const ll INF = 1e9; const ll INF2 = 1e18; const int K = 750; ll n,u,v,a[N],q; int main() { faster cin >> q; while (q--) { cin >> n >> u >> v; bool eq = 1,can = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] != a[i - 1] && i > 1) eq = 0; if (abs(a[i] - a[i - 1]) > 1 && i > 1) can = 1; } if (can) cout << 0 << endl; else if (eq) cout << v + min(u,v) << endl; else cout << min(u,v) << endl;; } return 0; }
#include <bits/stdc++.h> using namespace std; const long double EPS = 1E-9; const int INF = (int)1E9; const long long INF64 = (long long)1E18; const long double PI = 2 * acos(.0); int main() { string num; cin >> num; int a[11]; fill(a, a + 11, 0); for (int i = 0; i < (int)((num).size()); i++) { a[num[i] - 0 ]++; } int cont = 0; string temp1 = num; string temp2 = num; int a2[11]; int a3[11]; bool cent = 1; for (int i = 1; i < (int)(10); i++) { int temp = 0; char kk1 = s , kk2 = s ; vector<pair<char, int> > t1; vector<pair<char, int> > t2; for (int j = 0; j < (int)(10); j++) a2[j] = a3[j] = a[j]; if (a2[i] > 0 && a3[10 - i] > 0) { a2[i]--; a3[10 - i]--; kk1 = 0 + i; kk2 = 0 + (10 - i); } else continue; temp = 1; for (int j = 0; j < (int)(10); j++) { int mini = min(a2[j], a3[9 - j]); temp += mini; a2[j] -= mini; a3[9 - j] -= mini; t1.push_back(make_pair( 0 + j, mini)); t2.push_back(make_pair( 0 + (9 - j), mini)); } temp += min(a2[0], a3[0]); int conta = min(a2[0], a3[0]); a2[0] -= conta; a3[0] -= conta; if (temp > cont) { cent = 0; temp1 = ; temp2 = ; for (int j = 0; j < (int)(10); j++) { if (a2[j] > 0) temp1.append(a2[j], 0 + j); if (a3[j] > 0) temp2.append(a3[j], 0 + j); } for (int j = 0; j < (int)((t1).size()); j++) { temp1.append(t1[j].second, t1[j].first); temp2.append(t2[j].second, t2[j].first); } temp1 += kk1; temp2 += kk2; temp1.append(conta, 0 ); temp2.append(conta, 0 ); cont = temp; } } if (cent) { temp1 = ; temp2 = ; for (int j = 1; j < (int)(10); j++) { if (a[j] > 0) temp1.append(a[j], 0 + j); if (a[j] > 0) temp2.append(a[j], 0 + j); } temp1.append(a[0], 0 ); temp2.append(a[0], 0 ); } cout << temp1 << endl << temp2 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string presidents[41] = { , Washington , Adams , Jefferson , Madison , Monroe , Adams , Jackson , Van Buren , Harrison , Tyler , Polk , Taylor , Fillmore , Pierce , Buchanan , Lincoln , Johnson , Grant , Hayes , Garfield , Arthur , Cleveland , Harrison , Cleveland , McKinley , Roosevelt , Taft , Wilson , Harding , Coolidge , Hoover , Roosevelt , Truman , Eisenhower , Kennedy , Johnson , Nixon , Ford , Carter , Reagan , }; int main() { int n; cin >> n; cout << presidents[n] << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; long long t = 1; if (n < 30) { for (int i = 0; i < n; i++) { t *= 2; } cout << m % t; } else { cout << m; } return 0; }
//Shrey Dubey #include<iostream> #include<string> #include<algorithm> #include<map> #include<unordered_map> #include<vector> #include<set> #include<list> #include<iomanip> #include<queue> #include<stack> #include <math.h> #include<climits> #include<bitset> #include<cstring> #include<numeric> #include<array> using namespace std; typedef long long ll; typedef long double ld; #define YES cout<< YES n #define Yes cout<< Yes n #define NO cout<< NO n #define No cout<< No n #define prDouble(x) cout<<fixed<<setprecision(10)<<x //to print decimal numbers #define pb push_back #define ff first #define sec second #define umap unordered_map #define mp make_pair #define KOBE ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define fo(n) for(ll i = 0; i<n; i++) #define fnd(stl, data) find(stl.begin(), stl.end(), data) #define forn(x,n) for(ll x = 0; x<n; x++) #define imax INT_MAX #define lmax LLONG_MAX #define imin INT_MIN #define lmin LLONG_MIN #define vi vector<int> #define vl vector<ll> #define vp vector<pair<ll,ll> > #define vb vector<bool> #define pr(t) cout<<t<< n #define int long long #define ql queue<ll> #define qp queue<pair<ll,ll> > #define endl n #define nl cout<< n #define re cin >> #define pll pair<ll,ll> #define FOR(a,b) for(ll i = a; i<=b; i++) #define all(x) x.begin(),x.end() // ll dx[] = {1,0,-1,0}; // ll dy[] = {0,1,0,-1}; ll mod = 1e9 + 7; ll cl(ld a){ if(a>(ll) a){ return (ll)a+1; } else{ return (ll)a; } } ll flr(ld a){ return (ll) a; } //code starts here const ll M = 2e5+100; ll s[M]; ll n,m,a,b; bool check(ll pos){ ll diff = abs(b - a); ll t = diff; if(diff > pos){ if(a > b){ t += (n - a); }else{ t += (a - 1); } } // if(pos == 1) cout<<t<< () n ; while(pos > 0){ if(s[pos] >= t) return false; t--; pos--; } return true; } void solve(){ re n; re m; re a; re b; fo(m) re s[i+1]; sort(s+1,s+m+1); // fo(m) cout<<s[i+1]<< ;nl; ll ans = 0; ll s = 0, e = m; while(s <= e){ // cout<<s<< () <<e<< n ; ll mid = (s+e)/2; if(check(mid)){ ans = mid; s = mid+1; }else{ e = mid-1; } } pr(ans); } int32_t main(){ KOBE; ll t; t = 1; re t; while(t--) solve(); } //common errors // row - n, col - m always and loop var // see the freq of numbers carefully // see if there s array overflow // use map for large inputs //problem ideas //check piegonhole wherever possible //there might be many instances of limited answers like 0,1,2 only // see suffix and prefix //don t be obsessed with binary search // try to find repeating pattern in matrices
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 1000000; int n; char s[MAXN + 1]; int pac[MAXN], npac; int ast[MAXN], nast; int prvpac[MAXN], prvast[MAXN]; int mxpos[MAXN]; bool canrgt[MAXN]; int calcmxpos(int i, int t) { int x = i - t >= 0 ? prvast[i - t] : -1; if (x == -1) return prvpac[i] == -1 ? i : max(i, prvpac[i] + t); if (prvpac[i] == -1 || prvpac[i] <= x) return canrgt[x] ? i : -1; if (prvpac[prvpac[i]] != -1 && prvpac[prvpac[i]] > x) return mxpos[prvpac[prvpac[i]]] != -1 ? max(i, prvpac[i] + t) : -1; return canrgt[x] ? max(i, prvpac[i] + t) : mxpos[prvpac[i]] != -1 ? i : -1; } bool calccanrgt(int x, int t) { int i = prvpac[x]; if (i == -1 || x - i > t) return false; int y = prvast[i]; return mxpos[i] != -1 && mxpos[i] >= x || y == -1 || (prvpac[i] != -1 && prvpac[i] > y && mxpos[prvpac[i]] != -1) || canrgt[y]; } bool canall(int t) { for (int i = (0); i < (n); ++i) { if (s[i] == P ) { mxpos[i] = calcmxpos(i, t); } if (s[i] == * ) { canrgt[i] = calccanrgt(i, t); } } int idx = max(pac[npac - 1], ast[nast - 1]); if (s[idx] == P ) return mxpos[idx] != -1; else if (s[idx] == * ) return canrgt[idx]; else { assert(false); return false; } } pair<int, int> solve() { npac = nast = 0; for (int i = (0); i < (n); ++i) { prvast[i] = nast == 0 ? -1 : ast[nast - 1]; prvpac[i] = npac == 0 ? -1 : pac[npac - 1]; if (s[i] == P ) pac[npac++] = i; else if (s[i] == * ) ast[nast++] = i; } assert(npac > 0 && nast > 0); if (npac == 1) { int nleft = 0, tleft = 0, nright = 0, tright = 0; for (int i = (0); i < (nast); ++i) if (ast[i] < pac[0]) { ++nleft; tleft = max(tleft, pac[0] - ast[i]); } else { ++nright; tright = max(tright, ast[i] - pac[0]); } if (nleft > nright || nleft == nright && tleft <= tright) return make_pair(nleft, tleft); else return make_pair(nright, tright); } int l = 0, r = n - 1; while (l + 1 < r) { int m = l + (r - l) / 2; if (canall(m)) r = m; else l = m; } return make_pair(nast, r); } void run() { scanf( %d , &n); scanf( %s , s); pair<int, int> ans = solve(); printf( %d %d n , ans.first, ans.second); } int main() { run(); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; const long long llinf = 1ll * inf * inf; const double eps = 1e-12; const int N = 1e5 + 10; int f[N], h[N], g[N]; int main() { int n; scanf( %d , &n); for (int i = 1; i <= int(n); ++i) scanf( %d , &f[i]); map<int, vector<int> > uf; for (int i = 1; i <= int(n); ++i) uf[f[i]].push_back(i); int m = uf.size(); auto itr = uf.begin(); for (int x = 1; x <= m; ++x) { h[x] = itr->first; vector<int> nv = itr->second; itr++; for (int y : nv) g[y] = x; } int bad = 0; for (int x = 1; x <= m; ++x) if (g[h[x]] != x) bad = 1; for (int x = 1; x <= n; ++x) if (h[g[x]] != f[x]) bad = 1; if (bad) puts( -1 ); else { printf( %d n , m); for (int x = 1; x <= int(n); ++x) printf( %d%c , g[x], x == n ? n : ); for (int x = 1; x <= int(m); ++x) printf( %d%c , h[x], x == m ? n : ); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; while (scanf( %d , &n) != EOF) { int a[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf( %d , &a[i][j]); } } if (n == 1) { printf( %d n , a[0][0]); break; } int sum = 0; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) { sum += a[i][j]; } } sum += a[0][0] + a[0][n - 1] + a[n - 1][n - 1] + a[n - 1][0]; sum += a[0][(n - 1) / 2] + a[n - 1][(n - 1) / 2] + a[(n - 1) / 2][0] + a[(n - 1) / 2][n - 1]; printf( %d n , sum); } }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t)) aux1 += m * x; long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; aux2 *= cost; ans += min(aux1, aux2); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long S = (1 << 19); long long x, y, a, q, pref_x[S], pref_y[S], ans[S]; pair<long long, long long> tab[S]; vector<pair<pair<long long, long long>, pair<long long, long long> > > X; int main() { ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> a >> q; for (int i = 1; i <= a; i++) { cin >> x >> y; tab[i] = {x, y}; X.push_back({{x - y, i}, {x, y}}); } sort(X.begin(), X.end()); for (int i = 1; i <= X.size(); i++) pref_x[i] = pref_x[i - 1] + X[i - 1].second.first, pref_y[i] = pref_y[i - 1] + X[i - 1].second.second; for (int i = 0; i < X.size(); i++) { ans[X[i].first.second] += pref_x[i] + i * X[i].second.second; ans[X[i].first.second] += pref_y[X.size()] - pref_y[i + 1] + (X.size() - 1 - i) * X[i].second.first; } for (int i = 0; i < q; i++) { cin >> x >> y; long long res = min(tab[x].first + tab[y].second, tab[x].second + tab[y].first); ans[x] -= res; ans[y] -= res; } for (int i = 1; i <= X.size(); i++) cout << ans[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long b[n], c[n]; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < n; i++) cin >> c[i]; long long d[n], a[n], sum = 0; for (int i = 0; i < n; i++) { d[i] = b[i] + c[i]; sum += d[i]; } sum /= (2 * n); bool flag = true; long long k[64]; for (int j = 0; j < 63; j++) k[j] = 0; for (int i = 0; i < n; i++) { a[i] = (d[i] - sum) / n; if (a[i] < 0) { flag = false; break; } for (int j = 0; j < 63; j++) if (a[i] & (1LL << j)) k[j]++; } for (int i = 0; i < n && flag; i++) { long long num1 = 0, num2 = 0; for (int j = 0; j < 63; j++) { long long tmp = 1LL << j; if (a[i] & tmp) { num1 += k[j] << j; num2 += n << j; } else { num2 += k[j] << j; } } if (num1 != b[i] || num2 != c[i]) { flag = false; break; } } if (flag) { for (int i = 0; i < n; i++) cout << a[i] << ; cout << endl; } else cout << -1 n ; return 0; }
#include <bits/stdc++.h> using namespace std; long long int pwr(long long int base, long long int p, long long int mod) { long long int ans = 1; while (p) { if (p & 1) ans = (ans * base) % mod; base = (base * base) % mod; p /= 2; } return ans; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int n; scanf( %d , &n); int a[n + 1]; int par[300005]; int cnt = 0; int lvl = 0, flag = 0, p = -1; vector<int> g[n + 2]; for (int i = 0; i <= n; i++) { scanf( %d , &a[i]); for (int j = 1; j <= a[i]; j++) { g[i].push_back(++cnt); par[cnt] = lvl; } lvl = cnt; if (i >= 1) { if (a[i] > 1 && a[i - 1] > 1) flag = i; } } if (flag == 0) { printf( perfect n ); return 0; } printf( ambiguous n ); for (int i = 1; i <= cnt; i++) { printf( %d , par[i]); } printf( n ); par[g[flag][0]] = g[flag - 1][0]; for (int i = 1; i <= cnt; i++) { printf( %d , par[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int max_n = 1e5 + 10; vector<long long> adj[max_n]; long long par[max_n], st[max_n], p[max_n], ft[max_n], doc[max_n], x[max_n], l[max_n], r[max_n], cnt, tmp, k, n, m; bool mark[max_n]; int get_par(int u) { return par[u] == u ? u : par[u] = get_par(par[u]); } void dfs(long long v) { st[v] = cnt++; for (auto u : adj[v]) dfs(u); ft[v] = cnt; } bool cmp(long long v, long long u) { if (get_par(v) == get_par(u) && st[v] >= st[u] && ft[v] <= ft[u]) return true; else return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; ++i) { par[i] = i; p[i] = i; } while (m--) { int t; cin >> t; long long v, u; if (t == 1) { cin >> v >> u; par[v] = u; p[v] = u; } else if (t == 2) { cin >> v; l[tmp] = v; r[tmp++] = get_par(v); } else { cin >> x[k] >> doc[k]; doc[k++]--; } } for (int i = 1; i <= n; ++i) { if (p[i] != i) adj[p[i]].push_back(i); } for (int i = 1; i <= n; ++i) { if (p[i] == i) { cnt = 0; dfs(i); } } for (int i = 0; i < k; ++i) { if (cmp(l[doc[i]], x[i]) && cmp(x[i], r[doc[i]])) cout << YES << endl; else cout << NO << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; void solve() { string s1; string s2; cin >> s1 >> s2; string ans = ; for (int i = s2.length() - 1; i >= 0; --i) { ans += s2[i]; } if (s1 == ans) { cout << YES << n ; } else { cout << NO << n ; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int Abs(int x) { if (x < 0) return -x; return x; } int N, M, K; bool no[2001][2001] = {false}, visited[2001] = {false}, rooted[2001] = {false}, notgo[2001]; vector<int> G[2001], g[2001]; int cn = 0; bool run = true; void dfs(int r) { if (!run) return; cn++; visited[r] = true; rooted[r] = true; for (int i = 0; i < g[r].size(); i++) { notgo[g[r][i]] = true; } for (int i = 0; i < G[r].size(); i++) { if (notgo[G[r][i]]) run = false; if (!visited[G[r][i]]) dfs(G[r][i]); } } int main() { scanf( %d , &N); scanf( %d , &M); for (int i = 0; i < M; i++) { int v, u; scanf( %d%d , &v, &u); G[v].push_back(u); G[u].push_back(v); } scanf( %d , &K); for (int i = 0; i < K; i++) { int v, u; scanf( %d%d , &v, &u); g[v].push_back(u); g[u].push_back(v); } int ans = 0; for (int i = 1; i <= N; i++) { cn = 0; run = true; memset(visited, false, sizeof(visited)); memset(notgo, false, sizeof(notgo)); dfs(i); if (run) ans = max(ans, cn); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> struct node { T val; T lazy; int l, r; node* left; node* right; node(int l, int r) { this->l = l; this->r = r; this->left = nullptr; this->right = nullptr; } }; template <class T, int SZ> struct segtree { T combIdentity = -1e9; T comb(T l, T r) { return max(l, r); } T pushIdentity = 0; void push(node<T>* n) { n->val += n->lazy; if (n->l != n->r) { n->left->lazy += n->lazy; n->right->lazy += n->lazy; } n->lazy = pushIdentity; } node<T>* root; segtree() { int ub = 1; while (ub < SZ) ub *= 2; root = new node<T>(0, ub - 1); root->val = pushIdentity; root->lazy = pushIdentity; } void propagate(node<T>* n) { if (n->l != n->r) { int mid = ((n->l) + (n->r)) / 2; if (n->left == nullptr) { n->left = new node<T>(n->l, mid); n->left->val = pushIdentity; n->left->lazy = pushIdentity; } if (n->right == nullptr) { n->right = new node<T>(mid + 1, n->r); n->right->val = pushIdentity; n->right->lazy = pushIdentity; } } push(n); } void updN(node<T>* n, int i1, int i2, T val) { propagate(n); if (i2 < n->l || i1 > n->r) return; if (i1 <= n->l && i2 >= n->r) { n->lazy = val; push(n); return; } updN(n->left, i1, i2, val); updN(n->right, i1, i2, val); n->val = comb(n->left->val, n->right->val); } void upd(int i1, int i2, T val) { updN(root, i1, i2, val); } T queryN(node<T>* n, int i1, int i2) { propagate(n); if (i2 < n->l || i1 > n->r) return combIdentity; if (n->l >= i1 && n->r <= i2) return n->val; T a = combIdentity; if (n->left != nullptr) a = comb(a, queryN(n->left, i1, i2)); if (n->right != nullptr) a = comb(a, queryN(n->right, i1, i2)); return a; } T query(int i1, int i2) { return queryN(root, i1, i2); } }; const int MAX_M = 20020; const int MAX_N = 52; int n, m, k; int arr[MAX_N][MAX_M]; int pref[MAX_N][MAX_M]; int dp[MAX_N][MAX_M]; int getsum(int i, int j) { if (j == 0) return pref[i][j + k - 1]; else { return pref[i][j + k - 1] - pref[i][j - 1]; } } int get(int i, int j) { return getsum(i, j) + getsum(i + 1, j); } pair<int, int> getBounds(int j) { int l = j - k + 1; int r = j; l = max(0, l); r = min(m - k, r); return make_pair(l, r); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> arr[i][j]; for (int i = 0; i < n; i++) { pref[i][0] = arr[i][0]; for (int j = 1; j < m; j++) pref[i][j] = pref[i][j - 1] + arr[i][j]; } for (int i = 0; i < m - k + 1; i++) dp[0][i] = get(0, i); for (int i = 1; i < n; i++) { segtree<int, MAX_M> st; for (int j = 0; j < m - k + 1; j++) st.upd(j, j, dp[i - 1][j]); for (int j = 0; j < k; j++) { pair<int, int> b = getBounds(j); st.upd(b.first, b.second, -arr[i][j]); } for (int j = 0; j < m - k + 1; j++) { dp[i][j] = st.query(0, m - k) + get(i, j); pair<int, int> b = getBounds(j); st.upd(b.first, b.second, arr[i][j]); b = getBounds(j + k); st.upd(b.first, b.second, -arr[i][j + k]); } } int res = 0; for (int i = 0; i < m - k + 1; i++) res = max(res, dp[n - 1][i]); return cout << res << n , 0; ; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 5, mod = 1e9 + 7; int n, m, k; long long fact[maxn], dp[maxn], inv[maxn]; long long qpow(long long n, int p) { long long res = 1; while (p) { if (p & 1) res = res * n % mod; n = n * n % mod; p >>= 1; } return res; } long long COM(int n, int m) { if (m > n) return 0; return fact[n] * inv[m] % mod * inv[n - m] % mod; } int main() { scanf( %d%d%d , &n, &m, &k); fact[0] = 1; for (int i = 1; i < maxn; ++i) fact[i] = fact[i - 1] * i % mod; inv[maxn - 1] = qpow(fact[maxn - 1], mod - 2); for (int i = maxn - 2; i >= 0; --i) inv[i] = inv[i + 1] * (i + 1) % mod; printf( %lld n , COM(n - 1, k << 1) * COM(m - 1, k << 1) % mod); }
#include <bits/stdc++.h> int Max(int n); int main(void) { long n, i, j; long long m; scanf( %ld , &n); m = Max(n); j = m; for (i = 1; i <= 4; i++) { m = m * j; m %= 100000; } if (m == 0) { printf( 00000 n ); } else if (m < 10) { printf( 0000%lld n , m); } else if (m < 100) { printf( 000%lld n , m); } else if (m < 1000) { printf( 00%lld n , m); } else if (m < 10000) { printf( 0%lld n , m); } else { printf( %lld n , m); } } int Max(int n) { int i, j, k, l, m; i = n / 10000; j = (n / 1000) % 10; k = (n / 100) % 10; l = (n / 10) % 10; m = n % 10; return (i * 10000 + k * 1000 + m * 100 + l * 10 + j); }
#include <bits/stdc++.h> using namespace std; const int M = 1600; int dr[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1}; int n, m, pr[M][M], pc[M][M]; char g[M][M], f[M][M]; bool dfs(int r, int c) { int u = (r % n + n) % n, v = (c % m + m) % m; if ( # == g[u][v]) return 0; if (f[u][v]) return r != pr[u][v] || c != pc[u][v]; f[u][v] = 1, pr[u][v] = r, pc[u][v] = c; for (int d = 0; d < 4; ++d) if (dfs(r + dr[d], c + dc[d])) return 1; return 0; } int main(void) { cin >> n >> m; for (int i = 0; i < n; ++i) cin >> g[i]; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if ( S == g[i][j]) cout << (dfs(i + n * 2, j + m * 2) ? Yes : No ) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; bool visited[2010]; vector<int> g[2010]; int dfs(int src) { visited[src] = true; int ans = 0; for (auto x : g[src]) { if (!visited[x]) { ans = max(ans, dfs(x)); } } return ans + 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; if (x == -1) x = 0; g[i + 1].push_back(x); g[x].push_back(i + 1); } cout << dfs(0) - 1; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; const long long mod = 1e9 + 7; int prime[maxn], g[maxn], last[maxn], num[maxn], cnt[maxn]; long long fac[maxn], invf[maxn]; int t[500]; bool vis[maxn]; long long fpow(long long a, long long b, long long p = mod) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans; } void init() { int cnt = 0; g[1] = 1; last[1] = 1; fac[1] = 1; fac[0] = 1; for (int i = 2; i < maxn; i++) { fac[i] = fac[i - 1] * i % mod; if (!vis[i]) prime[++cnt] = i, g[i] = i - 1, last[i] = 1; for (int j = 1; j <= cnt && i * prime[j] < maxn; j++) { int x = i * prime[j]; vis[x] = true; if (i % prime[j] == 0) { last[x] = last[i]; if (last[x] == 1) g[x] = x - i; else g[x] = g[x / last[x]] * g[last[x]]; break; } last[x] = i; g[x] = g[i] * g[prime[j]]; } } invf[maxn - 1] = fpow(fac[maxn - 1], mod - 2); for (int i = maxn - 2; i >= 0; i--) invf[i] = invf[i + 1] * (i + 1) % mod; } long long C(int n, int m) { return n < m ? 0 : fac[n] * invf[m] % mod * invf[n - m] % mod; } int getfac(int x) { int len = 0; for (int i = 1; i * i <= x; i++) { if (x % i) continue; t[++len] = i; if (i * i != x) t[++len] = x / i; } sort(t + 1, t + 1 + len); return len; } int main() { std::ios::sync_with_stdio(false); cout.tie(0); init(); int n, k, q; cin >> n >> k >> q; for (int i = 1; i <= n; i++) { int x; cin >> x; num[x]++; } for (int i = maxn - 1; i >= 1; i--) { for (int j = i; j < maxn; j += i) cnt[i] += num[j]; } long long ans = 0; for (int i = 1; i < maxn; i++) { ans = (ans + C(cnt[i], k) * g[i] % mod) % mod; } while (q--) { int x; cin >> x; int len = getfac(x); for (int i = len; i > 0; i--) { long long dert = C(cnt[t[i]] + 1, k) - C(cnt[t[i]], k); dert *= g[t[i]]; ans = (ans + dert + mod) % mod; cnt[t[i]]++; } ans = (ans % mod + mod) % mod; cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int num[1 << 18]; char str[20]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int x, y; cin >> x >> y; int a, b, c, ans = 0; a = b = c = y; while (a < x) { swap(a, b); swap(b, c); c = a + b - 1; ans++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int v[100005], b[100005]; int k, n, m, maxim; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { int nr; cin >> nr; b[nr]++; v[b[nr]]++; if (v[b[nr]] == n) cout << 1; else cout << 0; } }
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& o, pair<T1, T2> x) { return o << x.first << << x.second; } template <class T1, class T2> istream& operator>>(istream& o, pair<T1, T2>& x) { return o >> x.first >> x.second; } template <class T1, class T2> pair<T1, T2> operator+(pair<T1, T2> a, pair<T1, T2> b) { a.first += b.first; a.second += b.second; return a; } template <class T1, class T2> pair<T1, T2> operator-(pair<T1, T2> a, pair<T1, T2> b) { a.first -= b.first; a.second -= b.second; return a; } template <class T1, class T2> void operator+=(pair<T1, T2>& a, pair<T1, T2> b) { a.first += b.first; a.second += b.second; } template <class T1, class T2> void operator-=(pair<T1, T2>& a, pair<T1, T2> b) { a.first -= b.first; a.second -= b.second; } template <long long int D, typename T> struct Vec : public vector<Vec<D - 1, T>> { static_assert(D >= 1, Vector dimension must be greater than zero! ); template <typename... Args> Vec(long long int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {} }; template <typename T> struct Vec<1, T> : public vector<T> { Vec(long long int n = 0, const T& val = T()) : vector<T>(n, val) {} }; const long long int md = 998244353; inline long long int add(long long int x, long long int y) { x += y; if (x >= md) x -= md; return x; } inline long long int sub(long long int x, long long int y) { x -= y; if (x < 0) x += md; return x; } inline long long int mul(long long int x, long long int y) { return (x * y) % md; } inline long long int power(long long int x, long long int y) { long long int res = 1; for (; y; y >>= 1, x = mul(x, x)) { if (y & 1) res = mul(res, x); } return res; } inline long long int inv(long long int a) { return power(a, md - 2); } void solve() { long long int a, b, x, y, ans; cin >> a >> b >> x >> y; ans = max(b * max(x, a - x - 1), a * (max(y, b - y - 1))); cout << ans << n ; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int T; cin >> T; while (T--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int search(int a[], int x, int n) { int lo = 0; int hi = n; while (lo <= hi) { int mid = (lo + hi) / 2; if (a[mid] >= x && a[mid - 1] < x) return mid; else if (a[mid] > x) hi = mid - 1; else lo = mid + 1; } return 0; } int main() { int n, m; int a[100000]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } cin >> m; int b[100000]; int ans[100000]; for (int i = 0; i < m; i++) { cin >> b[i]; } for (int i = 1; i < n; i++) { a[i] += a[i - 1]; } for (int i = 0; i < m; i++) { ans[i] = search(a, b[i], n); } for (int i = 0; i < m; i++) { cout << ans[i] + 1 << endl; } }
#include <bits/stdc++.h> using namespace std; int res[50010], cot; int main() { string a, b; int n; cin >> n; cin >> a; cin >> b; int s1, s2, s3; s1 = s2 = s3 = 0; int sa = 0, sb = 0; for (int i = 0; i < n; i++) { if (a[i] == 1 ) sa++; if (b[i] == 1 ) sb++; if (a[i] == 1 && b[i] == 1 ) s3++; else if (a[i] == 1 ) s1++; else if (b[i] == 1 ) s2++; } int ans[5]; int flag = -1, s4 = n - s1 - s2 - s3; for (int i = 0; flag == -1 && i <= min(s3, n / 2); i++) { int res1 = i, res2 = (s3 - i); if (res1 + s1 < res2) continue; if (res2 + s2 < res1) continue; memset(ans, 0, sizeof ans); ans[3] += res1; int ss1 = s1, ss2 = s2; if (res1 < res2) { ss1 -= (res2 - res1); ans[1] += res2 - res1; res1 = res2; int temp = min(ss1, ss2); ans[1] += temp; ss1 -= temp; ss2 -= temp; if (s4 >= max(ss1, ss2)) { if (ss1) { ans[4] += (s4 + ss1) / 2; } else { ans[2] += ss2; ans[4] += (s4 - (s4 + ss2) / 2); } flag = 1; } } else { ss2 -= (res1 - res2); res2 = res1; int temp = min(ss1, ss2); ans[1] += temp; ss1 -= temp; ss2 -= temp; if (s4 >= max(ss1, ss2)) { if (ss1) { ans[4] += (s4 + ss1) / 2; } else { ans[2] += ss2; ans[4] += (s4 - (s4 + ss2) / 2); } flag = 1; } } } if (flag == -1) puts( -1 ); else { for (int i = 0; i < n; i++) { if (a[i] == 1 && b[i] == 1 && ans[3]) { res[++cot] = i + 1; ans[3]--; continue; } if (a[i] == 1 && b[i] == 0 && ans[1]) { res[++cot] = i + 1; ans[1]--; continue; } if (a[i] == 0 && b[i] == 1 && ans[2]) { res[++cot] = i + 1; ans[2]--; continue; } if (a[i] == 0 && b[i] == 0 && ans[4]) { res[++cot] = i + 1; ans[4]--; continue; } } for (int i = 1; i <= cot; i++) cout << res[i] << ; cout << endl; } return 0; }
#include <bits/stdc++.h> using std::max; const int MAXN = 500111; int N; struct Data { int Cnt, Id; long long Val; int next; Data() {} Data(int _c, int _i, long long _v) { Cnt = _c; Id = _i; Val = _v; } } T[MAXN << 2]; int Tcnt; namespace Map { struct Vert { int FE; long long Val; } V[MAXN]; struct Edge { int x, y; long long l; int next; } E[MAXN]; int Ecnt; void addE(int a, int b, long long c) { ++Ecnt; E[Ecnt].x = a; E[Ecnt].y = b; E[Ecnt].l = c; E[Ecnt].next = V[a].FE; V[a].FE = Ecnt; } void DFS(int at) { for (int k = V[at].FE, to; k > 0; k = E[k].next) { to = E[k].y; V[to].Val = V[at].Val + E[k].l; DFS(to); } } } // namespace Map namespace Tree { struct Vert { int FE; int Fa, Son; int Dep, Len; int Head; long long Ans; } V[MAXN]; struct Edge { int x, y, next; } E[MAXN << 1]; int Ecnt; void addE(int a, int b) { ++Ecnt; E[Ecnt].x = a; E[Ecnt].y = b; E[Ecnt].next = V[a].FE; V[a].FE = Ecnt; } void DFS1(int at) { for (int k = V[at].FE, to; k > 0; k = E[k].next) { to = E[k].y; if (to == V[at].Fa) continue; V[to].Dep = V[at].Dep + 1; V[to].Fa = at; DFS1(to); V[at].Len = max(V[at].Len, V[to].Len + 1); if (V[at].Son <= 0 || V[V[at].Son].Len < V[to].Len) V[at].Son = to; } } void Merge(int p, Data &A, Data &d) { d.Val += 1LL * V[p].Dep * A.Cnt; A.Val += 1LL * V[p].Dep * d.Cnt; Map::addE(A.Id, d.Id, d.Val - A.Val); A.Cnt += d.Cnt; } void DFS2(int at) { if (V[at].Son) DFS2(V[at].Son); T[++Tcnt] = Data(1, at, 0); V[at].Head = Tcnt; T[Tcnt].next = V[V[at].Son].Head; for (int k = V[at].FE, to; k > 0; k = E[k].next) { to = E[k].y; if (to == V[at].Fa || to == V[at].Son) continue; DFS2(to); for (int a = T[V[at].Head].next, p = V[to].Head; p > 0; a = T[a].next, p = T[p].next) { Merge(at, T[a], T[p]); } } } void DFS3(int at) { for (int k = V[at].FE, to; k > 0; k = E[k].next) { to = E[k].y; if (to == V[at].Fa) continue; V[to].Ans = V[at].Ans + Map::V[to].Val + V[at].Dep; DFS3(to); } } } // namespace Tree int main() { scanf( %d , &N); int Root; for (int i = 1, f; i <= N; ++i) { scanf( %d , &f); if (f == 0) Root = i; else { Tree::addE(i, f); Tree::addE(f, i); } } Tree::V[Root].Dep = 1; Tree::DFS1(Root); Tree::DFS2(Root); for (int p = Tree::V[Root].Head; p > 0; p = T[p].next) { Map::V[T[p].Id].Val = T[p].Val; Map::DFS(T[p].Id); } Tree::DFS3(Root); for (int i = 1; i <= N; ++i) printf( %I64d , Tree::V[i].Ans); puts( ); return 0; }
#include <bits/stdc++.h> using namespace std; int read(); int n; char t[102]; int w[102][102], rnk[102][102], pos[102]; vector<int> p[102]; queue<int> q; void init(int t) { for (int i = 1; i <= n; ++i) { p[i].clear(), p[i + n].clear(); for (int j = 1; j <= n; ++j) p[i].push_back(j + n), p[i + n].push_back(j); sort(p[i].begin(), p[i].end(), [&](int a, int b) { return w[i][a - n] > w[i][b - n] ^ t; }); sort(p[i + n].begin(), p[i + n].end(), [&](int a, int b) { return w[a][i] < w[b][i] ^ t; }); q.push(i), pos[i] = 0, pos[i + n] = n; for (int j = 0; j < n; ++j) rnk[j][p[i][j]] = j, rnk[i + n][p[i + n][j]] = j; } while (!q.empty()) { int u = q.front(), v; while (pos[v = p[u][pos[u]]] < rnk[v][u]) ++pos[u]; if (pos[v] < n) q.push(p[v][pos[v]]); pos[v] = rnk[v][u], q.pop(); } } int main() { for (int T = read(); T; --T) { n = read(); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) w[i][j] = read(); puts( B ), fflush(stdout), scanf( %s , t); if (t[0] == I ) for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) w[i][j] = -w[i][j]; int u; for (init((u = read()) > n); ~u; u = read()) printf( %d n , p[u][pos[u]]), fflush(stdout); } return 0; } int read() { int x = 0, f = 1; char c = getchar(); while (c < 0 || c > 9 ) f = (c == - ) ? -1 : f, c = getchar(); while (c >= 0 && c <= 9 ) x = x * 10 + c - 0 , c = getchar(); return x * f; }
#include <bits/stdc++.h> using namespace std; long long prime[1000005]; long long fac[1000005]; int mul(int a, int b) { int res = 0; while (b) { if (b & 1) res = (res + a); a = (a + a); b = b / 2; } return res; } int bexp(int a, int b) { int res = 1; while (b) { if (b & 1) res = mul(res, a); a = mul(a, a); b = b / 2; } return res; } int inv(int a) { return bexp(a, 1000000007 - 2); } int nCr(int n, int r) { return mul(fac[n], mul(inv(fac[n - r]), inv(fac[r]))); } long long power(long long a, long long b, long long c) { long long res = 1; while (b) { if (b & 1) res = (res * a) % c; a = (a * a) % c; b /= 2; } return res % c; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, m, k; cin >> n >> m >> k; long long a[n]; vector<pair<long long, long long>> v; for (int i = 1; i <= n; i++) { cin >> a[i]; } long long p = n - k; if (p == 0) { cout << n << endl; return 0; } for (int i = 2; i <= n; i++) v.push_back(make_pair(a[i] - a[i - 1] + 1, i - 1)); sort(v.begin(), v.end()); long long count = 0; long long sum = 0; vector<pair<long long, long long>> final; for (int i = 0; i < v.size(); i++) { sum += v[i].first; final.push_back(make_pair(v[i].second, v[i].first)); count++; if (count == p) break; } long long check = 2; sort(final.begin(), final.end()); for (int i = 1; i < final.size(); i++) { if (final[i].first - final[i - 1].first == 1) { sum -= 1; check += 1; } else check += 2; } cout << sum + n - check << endl; }
#include <bits/stdc++.h> using namespace std; int const N = 100; int const M = 900; typedef double (*F)[N]; int a[N]; double _f[N][N], _g[N][N]; F f = _f, g = _g; inline int c2(int n) { return n * (n + 1) >> 1; } int main() { int n, k; scanf( %d%d , &n, &k); for (int i = 0; i < n; ++i) scanf( %d , a + i); int n2 = c2(n); for (k = min(k, M); k--;) { for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { g[i][j] = (c2(i) + c2(n - 1 - j) + c2(j - i - 1)) * f[i][j]; for (int x = i; x < i + j; ++x) { int a = max(0, x - j + 1), b = min(i, x - i); g[i][j] += (b - a + 1) * f[x - i][j]; } for (int x = i + j + 1; x < n + j; ++x) { int a = max(i + 1, x - n + 1), b = min(j, x - j); g[i][j] += (b - a + 1) * f[i][x - j]; } for (int x = j; x < n + i; ++x) { int a = max(0, x - n + 1), b = min(i, x - j); g[i][j] += (b - a + 1) * (1 - f[x - j][x - i]); } g[i][j] /= n2; } swap(f, g); } double e = 0; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { e += a[i] > a[j] ? 1 - f[i][j] : f[i][j]; } printf( %.16f n , e); return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int N, M, K; long long F[1111][1111], T[1111], G[111111]; map<int, int> P; inline long long Pow(long long A, int B) { long long Ret = 1; for (; B; B >>= 1) { if (B & 1) { Ret = (Ret * A) % MOD; } A = (A * A) % MOD; } return Ret; } int main(int argc, char *argv[]) { for (int l = 1; l < 10; l++) { for (int i = 0; i < 1 << l; i++) { int x = 0; for (int k = 0; k < l; k++) { x *= 10; if (i & (1 << k)) { x += 4; } else { x += 7; } } P[x] = ++M; } } scanf( %d%d , &N, &K); for (int i = 0, x; i < N; i++) { scanf( %d , &x); if (P.find(x) != P.end()) { T[P[x]]++; } else { T[0]++; } } N = T[0]; F[0][0] = 1; for (int i = 1; i <= M; i++) { for (int j = 0; j <= M; j++) if (F[i - 1][j]) { F[i][j] = (F[i][j] + F[i - 1][j]) % MOD; F[i][j + 1] = (F[i][j + 1] + F[i - 1][j] * T[i]) % MOD; } } G[0] = 1; for (int i = 1; i <= N; i++) { G[i] = (G[i - 1] * (N + 1 - i)) % MOD; G[i] = (G[i] * Pow(i, MOD - 2)) % MOD; } long long Ret = 0; for (int i = 0; i <= M && i <= K; i++) { Ret += F[M][i] * G[K - i]; Ret %= MOD; } printf( %I64d n , Ret); return 0; }
#include <bits/stdc++.h> const int N = 100010; int n; int head[N], to[2 * N], next[2 * N], graphmr; double ans; inline void add(int x, int y) { int p = graphmr++; to[p] = y; next[p] = head[x]; head[x] = p; } inline void dfs(int x, int fa, int dis) { ans += 1.0 / dis; for (int i = head[x]; ~i; i = next[i]) if (to[i] != fa) dfs(to[i], x, dis + 1); } int main() { scanf( %d , &n); memset(head, -1, sizeof head); for (int i = 1, x, y; i < n; i++) { scanf( %d%d , &x, &y); add(x, y); add(y, x); } dfs(1, 0, 1); printf( %lf n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int a[300001]; int f[300001]; vector<int> g[33]; map<vector<int>, int> mp; vector<pair<vector<int>, int> > v; vector<int> cur; int br = 0; int n; void F(int pos, int mask) { if (pos == br) { v.push_back({cur, mask}); return; } F(pos + 1, mask); if (g[pos].empty() || g[pos].size() == n) return; for (auto to : g[pos]) cur[to]++; F(pos + 1, (mask ^ (1 << pos))); for (auto to : g[pos]) cur[to]--; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) f[i] = __builtin_popcount(a[i]); set<int> st; for (int i = 1; i <= n; i++) st.insert(f[i] % 2); if (st.size() == 2) { cout << -1; return 0; } for (int i = 1; i <= n; i++) f[i] /= 2; for (int i = 1; i <= n; i++) { for (int j = 0; j < 30; j++) { if (a[i] & (1 << j)) continue; g[j].push_back(i - 1); } } cur.resize(n); for (int i = 0; i < n; i++) cur[i] = 0; br = 15; F(0, 0); for (auto cur : v) { mp[cur.first] = cur.second; } for (int i = 0; i < n; i++) cur[i] = 0; br = 30; F(15, 0); for (auto x : v) { vector<int> cur = x.first; int mask = x.second; for (int t = 0; t <= 60; t++) { vector<int> to(n, t); for (int i = 0; i < n; i++) to[i] -= f[i + 1] + cur[i]; if (mp.find(to) != mp.end()) { cout << (mask ^ mp[to]); return 0; } } } cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, c; cin >> n >> c; int a[n]; int cnt = 0; vector<int> b; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) { if (abs(a[i] - a[i - 1]) <= c) cnt++; else { cnt = 0; } } cout << cnt + 1; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; int n; cin >> n >> str; int A[n], m = 0; string RE[n]; A[0] = 0; for (int i = 1, t = 0, tmp = [ ; i < n; i++) { if (str[i] == tmp) { if (str[i] == ] ) t--; else t++; } A[i] = t; if (t > m) m = t; tmp = str[i]; } for (int i = 0; i < m + 2; i++) { for (int j = 0; j < n; j++) { if (A[j] == i) { if (str[j] == [ ) RE[i].append( +- ); else if (str[j] == ] && str[j - 1] == [ ) RE[i].append( -+ ); else RE[i].append( -+ ); } else if (A[j] > i) { if (str[j - 1] == str[j]) RE[i].append( ); else if (str[j - 1] == [ && str[j] == ] ) RE[i].append( ); else RE[i].append( ); } else { if (str[j - 1] == [ && str[j] == ] ) { RE[i].append( | ); } else { RE[i].append( | ); } } } cout << RE[i] << endl; } for (int i = m; i >= 0; i--) { cout << RE[i] << endl; } }
#include <bits/stdc++.h> using namespace std; int root, n, m, tot, ans, a[2000010], zy[2000010]; bool b[2000010], v[2000010]; void dfs(int k) { if (b[a[k]]) { b[a[k]] = v[a[k]] = false; dfs(a[k]); v[a[k]] = true; } else { if (!v[a[k]]) { tot++; if (root == 0) { root = k; zy[k] = k; } else zy[k] = root; } } } int work(int k) { if (v[a[k]]) { v[a[k]] = false; return (work(a[k])); } else return k; } int main(int argc, const char* argv[]) { memset(b, true, sizeof(b)); int i, j, k; tot = 0; cin >> n; root = 0; for (i = 1; i <= n; ++i) { scanf( %d , &a[i]); if (a[i] == i) root = i; zy[i] = a[i]; } if (root != 0) tot = -1; for (i = 1; i <= n; ++i) if (b[i]) { b[i] = v[i] = false; dfs(i); v[i] = true; } cout << tot << endl; for (i = 1; i <= n; ++i) cout << zy[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const double PI = 3.14159265359; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> facts; for (int i = 1; i * i <= n; i++) { if (!(n % i)) { facts.push_back(i); facts.push_back(n / i); } } vector<long long> ans; for (int i : facts) { long long a = (n - 1) / i; ans.push_back(a * (a + 1) * i / 2 + a + 1); } sort(ans.begin(), ans.end()); auto it = unique(ans.begin(), ans.end()); ans.resize(it - ans.begin()); for (auto i : ans) { cout << i << ; } return 0; }
#include <bits/stdc++.h> using namespace std; int a[100005], n, x, minn = 3; bool m1[100005], m2[100005]; int read() { int w = 0, f = 1; char c = getchar(); while (c > 9 || c < 0 ) { if (c == - ) f = -1; c = getchar(); } while (c >= 0 && c <= 9 ) { w = (w << 3) + (w << 1) + (c ^ 48); c = getchar(); } return w * f; } int main() { n = read(), x = read(); for (int i = 1; i <= n; i++) { a[i] = read(); } for (int i = 1; i <= n; i++) { if (m1[a[i]]) minn = min(0, minn); if (m2[a[i]]) minn = min(1, minn); if (m1[a[i] & x]) minn = min(1, minn); if (m2[a[i] & x]) minn = min(2, minn); m1[a[i]] = 1, m2[a[i] & x] = 1; } if (minn == 3) puts( -1 ); else printf( %d n , minn); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; int count = 0; while (str.size() > 1) { string temp; int num = 0; count++; for (int i = 0; i < str.size(); i++) { num += str[i] - 48; } str = ; str = to_string(num); } cout << count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void solve(long long k) { for (long long n = 3; n <= 2000; ++n) { for (long long a = 2; a < n; ++a) { if ((n + k) % a == 0) { long long y = (n + k) / a; if (y <= 1000000 && (n - a) * y >= (a - 1) && n * (y - 1) > (n - a) * y) { vector<long long> ans(n, 0); ans[0] = 1; ans[a - 1] = -2; ans[a] = y; cout << n << n ; for (long long x : ans) cout << x << ; cout << n ; return; } } } } cout << k << endl; exit(0); } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); long long k; cin >> k; solve(k); }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18, N = 1e6, mod = 1e9 + 7; const long double PI = acos(-1), eps = -1e9; inline void read(long long &a) { char c; bool neg = 0; c = getchar(); a = 0; if (c == - ) { neg = 1; c = getchar(); } while (c >= 0 && c <= 9 ) { a = (a << 3) + (a << 1) + (c - 0 ); c = getchar(); } if (neg) a *= -1; } long long a[N]; void solve() { long long n; read(n); long long sum = 0, cur = 0; for (long long i = 1; i <= n; i++) { long long x; read(x); if (a[x] != 0) { cur -= a[x]; } a[x] = i; cur += a[x]; sum += cur; } sum -= n; sum *= 2; sum += n; cout << setprecision(20) << (long double)sum / ((long double)n * n); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int c, a, i, n; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> a; if (i == 0) c = a; else c = min(c, a); } if (c == 1) cout << -1; else cout << 1; }
#include <bits/stdc++.h> using namespace std; int main() { int num; int a = 0; int b = 0; int n; cin >> n; int sol[n]; for (a = 0; a < n; a++) { cin >> num; sol[num - 1] = (a + 1); } for (b = 0; b < n; b++) { cout << sol[b] << ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; const int E = 80; vector<long long> pr, f; mt19937_64 gen(time(0)); long long a[maxn]; map<long long, long long> val; int n; long long solve(long long p) { if (val.find(p) != val.end()) return val[p]; long long ret = 0; for (int i = 0; i < n; i++) { if (a[i] < p) ret += p - a[i]; else { long long h = a[i] % p; ret += min(h, p - h); } } val[p] = ret; return ret; } int main() { scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %lld , &a[i]); for (int i = 0; i < n; i++) { f.push_back(a[i] + 1); if (a[i] > 1) f.push_back(a[i]); if (a[i] > 2) f.push_back(a[i] - 1); } int m = f.size(); long long ans = n; for (int _ = 0; _ < E; _++) { long long x = f[gen() % m], j = 2; f.clear(); while (j * j <= x) { if (x % j == 0) { while (x % j == 0) x /= j; pr.push_back(j); } ++j; } if (x > 1) pr.push_back(x); for (auto p : pr) ans = min(ans, solve(p)); } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int n; scanf( %d , &n); long long int a[n], b[n], p = 0, q = 0; for (int i = 0; i < n; ++i) { scanf( %I64d , &a[i]); p |= a[i]; } for (int i = 0; i < n; ++i) { scanf( %I64d , &b[i]); q |= b[i]; } printf( %I64d n , p + q); return 0; }
#include <bits/stdc++.h> using namespace std; const int max_n = 1e5 + 10; const long long inf = 10000000000000000; vector<int> adj[max_n]; vector<long long> cst[max_n]; vector<int> arr[max_n]; vector<long long> closest[max_n]; void cmp_closest(int i) { int s = arr[i].size(); if (s > 0) { closest[i].reserve(s); closest[i][s - 1] = arr[i][s - 1] + 1; for (int j = s - 2; j >= 0; --j) { if (arr[i][j] + 1 != arr[i][j + 1]) { closest[i][j] = arr[i][j] + 1; } else { closest[i][j] = closest[i][j + 1]; } } } } int n, m; void add_edge(int a, int b, int c) { adj[a].push_back(b); cst[a].push_back(c); adj[b].push_back(a); cst[b].push_back(c); } void read() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < m; ++i) { int a, b, c; cin >> a >> b >> c; --a; --b; add_edge(a, b, c); } for (int i = 0; i < n; ++i) { int k; cin >> k; for (int j = 0; j < k; ++j) { long long t; cin >> t; arr[i].push_back(t); } cmp_closest(i); } } long long dist[max_n]; struct cmpl { inline bool operator()(int a, int b) const { return (dist[a] < dist[b] || (dist[a] == dist[b] && a < b)); } }; int cost(int u, size_t i) { vector<int>::iterator it = lower_bound(arr[u].begin(), arr[u].end(), dist[u]); if (it != arr[u].end() && *it == dist[u]) { return cst[u][i] + (closest[u][it - arr[u].begin()]) - dist[u]; } else { return cst[u][i]; } } set<int, cmpl> pq; int main() { read(); for (int i = 1; i < n; ++i) { dist[i] = inf; } pq.insert(0); int res = -1; while (!pq.empty()) { int curr = *pq.begin(); pq.erase(pq.begin()); if (curr == n - 1) { res = dist[curr]; break; } else { for (size_t i = 0; i < adj[curr].size(); ++i) { int v = adj[curr][i]; int c = cost(curr, i); if (dist[curr] + c < dist[v]) { pq.erase(v); dist[v] = dist[curr] + c; pq.insert(v); } } } } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<long long, vector<int> > > out; int n, k; int a[64]; bool found(long long x) { for (int i = 0; i < out.size(); i++) if (out[i].first == x) return true; return false; } int main() { cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; out.size() < k && i < n; i++) { int sz = out.size(); if (found(a[i]) == false) { vector<int> push; push.push_back(i); out.push_back(make_pair(a[i], push)); } for (int j = 0; out.size() < k && j < sz; j++) { if (found(a[i] + out[j].first) == false) { out.push_back(out[j]); out[out.size() - 1].second.push_back(i); out[out.size() - 1].first += a[i]; } } } for (int i = 0; i < out.size(); i++) { cout << out[i].second.size(); for (int j = 0; j < out[i].second.size(); j++) cout << << a[out[i].second[j]]; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; void solve4() { string s; cin >> s; int c = 0; int n = int((s).size()); for (char ch : s) { c += (ch == a ); } long long ans; cerr << n << = << n << , << c << = << c << endl; if (c > n / 2) ans = n; else ans = 2 * c - 1; cout << ans << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve4(); }
#include <bits/stdc++.h> using namespace std; int n, m, t; set<pair<int, int> > s; string s1; vector<int> v, v1; int kek[100000]; int main() { cin >> n >> m >> t; for (int i = 1; i <= n; i++) { cin >> s1; int cur = 0; cur += 60 * 60 * 10 * (s1[0] - 0 ); cur += 60 * 60 * (s1[1] - 0 ); cur += 60 * 10 * (s1[3] - 0 ); cur += 60 * (s1[4] - 0 ); cur += 10 * (s1[6] - 0 ); cur += (s1[7] - 0 ); v.push_back(cur); } s.clear(); int cur = 0; int ans = 0; int mx = 0; for (int i = 0; i < v.size(); i++) { while (!s.empty()) { set<pair<int, int> >::iterator it = (--s.end()); if (-(*it).first < v[i]) s.erase(it); else break; cur--; } if (cur < m) { ans++; kek[i] = ans; cur++; s.insert(make_pair(-(v[i] + t - 1), ans)); } else { kek[i] = (*s.begin()).second; s.erase(s.begin()); s.insert(make_pair(-(v[i] + t - 1), kek[i])); } mx = max(mx, cur); } if (mx != m) { cout << No solution ; return 0; } cout << ans << n ; for (int i = 0; i < v.size(); i++) cout << kek[i] << n ; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 60; long long h, n; int step[MAXN]; inline long long calc(long long high) { return (1ll << (high + 1)) - 1; } void solve() { long long tmp = (1ll << h) - 1 + n; int cnt = 0; while (tmp != 1) { if (tmp % 2 == 1) step[cnt++] = 1; else step[cnt++] = 0; tmp /= 2; } long long res = 0; int curdir = 0, dep = 0; for (int i = cnt - 1; i >= 0; i--) { if (curdir == step[i]) { dep++; res++; curdir ^= 1; } else { long long tmp = calc(h - dep - 1); res += tmp; curdir = step[i]; i++; } } cout << res << endl; } int main() { cin >> h >> n; solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t; t = 1; while (t--) { long long int n, c1 = 0, c2 = 0, x; cin >> n; while (n--) { cin >> x; if (x == 1) c1++; else c2++; } x = min(c1, c2); c1 -= x; c2 -= x; cout << x + (c1 / 3); } return 0; }
#include <bits/stdc++.h> using namespace std; const long double PI = acos(-1.0); const long long LINF = (long long)1e18 + 5; const int INF = (int)1e9 + 5; template <class T> T sqr(T x) { return x * x; } template <class T> T abs(T x) { return x < 0 ? -x : x; } template <class T> long long round(T x) { return x < 0 ? x - 0.5 : x + 0.5; } template <class T> bool chmin(T& x, const T& y) { if (y < x) { x = y; return true; } return false; } template <class T> bool chmax(T& x, const T& y) { if (x < y) { x = y; return true; } return false; } template <class P, class Q> ostream& operator<<(ostream& os, const pair<P, Q>& p) { return os << ( << p.first << , << p.second << ) ; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { bool was = false; os << { ; for (typename vector<T>::const_iterator it = v.begin(); it != v.end(); it++) { if (was) { os << , ; } else { was = true; } os << *it; } os << } ; return os; } template <class T> ostream& operator<<(ostream& os, const set<T>& v) { bool was = false; os << { ; for (typename set<T>::const_iterator it = v.begin(); it != v.end(); it++) { if (was) { os << , ; } else { was = true; } os << *it; } os << } ; return os; } template <class T> ostream& operator<<(ostream& os, const multiset<T>& v) { bool was = false; os << { ; for (typename multiset<T>::const_iterator it = v.begin(); it != v.end(); it++) { if (was) { os << , ; } else { was = true; } os << *it; } os << } ; return os; } template <class P, class Q> ostream& operator<<(ostream& os, const map<P, Q>& v) { bool was = false; os << { ; for (typename map<P, Q>::const_iterator it = v.begin(); it != v.end(); it++) { if (was) { os << , ; } else { was = true; } os << *it; } os << } ; return os; } template <class T> T nextInt() { long long x = 0; bool p = false; char c; do { c = getchar(); } while (c <= 32); if (c == - ) { p = true; c = getchar(); } while (c >= 0 && c <= 9 ) { x = x * 10 + c - 0 ; c = getchar(); } return (p ? -x : x); } string nextToken() { static char str[200200]; scanf( %s , str); return str; } const int N = 300300; int n; long long t[N], w[N]; struct cmpByDiff { bool operator()(int i, int j) { return make_pair(w[i] - t[i], i) < make_pair(w[j] - t[j], j); } }; struct cmpByT { bool operator()(int i, int j) { return make_pair(t[i], i) > make_pair(t[j], j); } }; set<int, cmpByDiff> topSet; set<int, cmpByT> loserSet; void answer(int x) { printf( %d n , x); exit(0); } void solve() { scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %lld%lld , t + i, w + i); } long long curT = t[0]; for (int i = 1; i < n; i++) { if (t[i] > curT) { topSet.insert(i); } else { loserSet.insert(i); } } int best = (int)topSet.size() + 1; while (true) { best = min(best, (int)topSet.size() + 1); if (topSet.empty()) { answer(1); } int idx = *topSet.begin(); topSet.erase(idx); long long cost = w[idx] - t[idx] + 1; if (cost > curT) break; curT -= cost; while (!loserSet.empty()) { int jdx = *loserSet.begin(); loserSet.erase(jdx); if (t[jdx] > curT) { topSet.insert(jdx); } else { loserSet.insert(jdx); break; } } } answer(best); } int main() { srand(time(0)); int t = 1; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; int getint() { unsigned int c; int x = 0; while (((c = getchar()) - 0 ) >= 10) { if (c == - ) return -getint(); if (!~c) exit(0); } do { x = (x << 3) + (x << 1) + (c - 0 ); } while (((c = getchar()) - 0 ) < 10); return x; } char a1[] = ### ..# .#. #.. ; char a2[] = .#. ### .#. ### ; char a3[] = .#. ..# ### #.. ; vector<int> px[4], py[4]; int n, m, grid[10][10], mx, res[10][10]; void solve(int x, int y, int curr) { if (y >= m) { solve(x + 1, 0, curr); return; } if (x >= n) { if (curr > mx) mx = curr, memcpy(res, grid, sizeof(res)); return; } int i, j, k, d, tmp[10][10], nx, ny; int mxPossible = 0; for (i = y; i < m; i++) if (grid[x][i] == 0) mxPossible++; for (i = x; i < n; i++) for (j = 0; j < m; j++) if (grid[i][j] == 0) mxPossible++; mxPossible /= 5; if (mxPossible + curr <= mx) return; memcpy(tmp, grid, sizeof(tmp)); for (d = 0; d < 4; d++) { bool canPlace = 1; memcpy(grid, tmp, sizeof(tmp)); for (i = 0; i < px[d].size(); i++) { nx = x + px[d][i]; ny = y + py[d][i]; if (nx >= n or ny >= m or grid[nx][ny]) { canPlace = 0; break; } grid[nx][ny] = curr + 1; } if (canPlace) { solve(x, y + 1, curr + 1); } } memcpy(grid, tmp, sizeof(tmp)); solve(x, y + 1, curr); } int main() { int i, j, tcc, tc = 1 << 28; for (i = 0; i < 4; i++) { for (j = 0; j < 3; j++) if (a1[i * 4 + j] == # ) px[i].push_back(0), py[i].push_back(j); for (j = 0; j < 3; j++) if (a2[i * 4 + j] == # ) px[i].push_back(1), py[i].push_back(j); for (j = 0; j < 3; j++) if (a3[i * 4 + j] == # ) px[i].push_back(2), py[i].push_back(j); } for (tcc = 0; tcc < tc; tcc++) { n = getint(), m = getint(), mx = 0; if (n == 9 and m == 9) { puts( 13 ); puts( AAA.BCCC. n.ABBB.CD. n.AE.BFCD. nEEEFFFDDD nG.E.HFIII nGGGJHHHI. nGK.JHL.IM n.KJJJLMMM nKKK.LLL.M n ); continue; } memset(res, 0, sizeof(res)); memset(grid, 0, sizeof(grid)); solve(0, 0, 0); printf( %d n , mx); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (res[i][j] == 0) putchar( . ); else putchar( A + res[i][j] - 1); } puts( ); } puts( ); } return 0; }
#include <bits/stdc++.h> using namespace std; long long int v[300001], sol[300001], S; int cnt = 0, marked[300001], dp[300001][2]; vector<int> g[300001]; long long int dfs(int node, int p) { sol[node] = v[node]; long long int res = sol[node]; for (auto it : g[node]) if (it != p) { res = max(res, dfs(it, node)); if (sol[it] > 0) sol[node] += sol[it]; } res = max(res, sol[node]); return res; } void getcnt(int node, int p) { for (auto it : g[node]) if (it != p) { getcnt(it, node); if (sol[it] > 0 && marked[it]) marked[node] = 1; } if (!marked[node] && sol[node] == S) { cnt++; marked[node] = 1; } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, i, a, b, k; cin >> n; for (i = 0; i < n; i++) cin >> v[i]; for (i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; g[u - 1].push_back(v - 1); g[v - 1].push_back(u - 1); } S = dfs(0, 0); getcnt(0, 0); cout << S * cnt << << cnt << n ; return 0; }
#include <bits/stdc++.h> #pragma GCC target( avx2 ) #pragma GCC optimization( O3 ) #pragma GCC optimization( unroll-loops ) using namespace std; using ll = long long; using vll = int64_t; const int N = 1e6 + 1; vector<int> adj[N]; vector<bool> vis(N, false); vector<short> col(N, -1); inline bool bfs(int src) { queue<pair<int, int>> q; q.push({src, 0}); vis[src] = true; col[src] = 0; while (!q.empty()) { auto p = q.front(); q.pop(); for (int i : adj[p.first]) { if (vis[i]) { if (col[i] == col[p.first]) { return false; } continue; } vis[i] = true; col[i] = (p.second == 1 ? 0 : 1); q.push({i, col[i]}); } } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int m; cin >> m; unordered_set<int> hs; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; adj[a].push_back(b); adj[b].push_back(a); hs.insert(a); hs.insert(b); } for (int i = 0; i < n; i++) { if (!vis[i] && hs.find(i) != hs.end()) { bool ok = bfs(i); if (!ok) { cout << -1 n ; return 0; } } } int red = 0, black = 0; for (int i = 0; i < n; i++) { if (col[i] == 1) black++; else if (col[i] == 0) red++; } cout << red << n ; for (int i = 0; i < n; i++) { if (col[i] == 0) cout << 1 + i << ; } cout << n ; cout << black << n ; for (int i = 0; i < n; i++) { if (col[i] == 1) cout << i + 1 << ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long pw(long long a, long long b, long long p) { long long res = 1; a %= p; b %= p - 1; while (b) { if (b & 1) res = (1LL * a * res) % p; a = (1LL * a * a) % p; b >>= 1; } return res; } long long a, b, x, p, ans; vector<pair<long long, long long> > pir; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> a >> b >> p >> x; for (long long i = 0; i < p - 1; i++) { long long t = pw(a, i, p); t = pw(t, p - 2, p); t = (1LL * b * t) % p; pir.push_back({i, t}); } long long lcm = p * (p - 1); for (auto i : pir) { long long z = i.first - i.second; long long w = z * p + i.second; w %= lcm; if (w < 0) w += lcm; if (w > x) continue; ans += (x - w) / lcm + 1; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const double esp = 1e-8; const int msz = 100000; const int mod = 2015; struct Child { int v, d; long long p; }; Child c[4444]; bool vis[4444]; queue<int> q; int n; void cut(int id) { long long v = c[id].v + 1; long long d = 0; for (int i = id + 1; i <= n; ++i) { if (vis[i]) continue; if (v > 0) v--; c[i].p -= (d + v); if (c[i].p < 0) { vis[i] = 1; d += c[i].d; } } } int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) { scanf( %d %d %lld , &c[i].v, &c[i].d, &c[i].p); } memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; ++i) { if (vis[i]) continue; cut(i); q.push(i); } int sz = q.size(); printf( %d n , sz); while (sz--) { printf( %d , q.front()); q.pop(); if (sz) putchar( ); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; char s[maxn], t[maxn]; int n, m; vector<int> id; vector<int> ans; int main() { scanf( %d%d , &n, &m); getchar(); gets(s); gets(t); int Ans = 0x3f3f3f3f; for (int i = 0; i < m - n + 1; i++) { id.clear(); int temp = 0; for (int j = 0; j < n; j++) { if (s[j] != t[i + j]) { temp++; id.push_back(j + 1); } } if (temp < Ans) { ans.clear(); for (int j = 0; j < id.size(); j++) { ans.push_back(id[j]); } Ans = temp; } } printf( %d n , Ans); for (int i = 0; i < ans.size(); i++) { printf( %d%c , ans[i], i == ans.size() - 1 ? n : ); } return 0; }
#include <bits/stdc++.h> using namespace std; int make(int c) { if (c == 10000000) return 1; return 0; } long long bb[3][300005], cc[3][300005], dd[3][300005], ee[3][300005]; long long int a[3][300005 + 1]; int main() { int n, fl = 0, m, q; cin >> n; int i; for (i = 1; i <= n; i++) { cin >> a[1][i]; } for (i = 1; i <= n; i++) { cin >> a[2][i]; } long long ans = 0; int j = 0; for (i = 1; i <= n; i++) { ans += a[1][i] * j; j++; } for (i = n; i >= 1; i--) { ans += a[2][i] * j; j++; } long long ans1 = 0, s = 0; ans1 = max(ans, ans1); for (i = 1; i <= n; i++) { s += a[1][i]; bb[1][i] = s; } for (i = n; i >= 1; i--) { s += a[2][i]; bb[2][i] = s; } s = 0; for (i = 1; i <= n; i++) { s += a[2][i]; cc[2][i] = s; } for (i = n; i >= 1; i--) { s += a[1][i]; cc[1][i] = s; } j = 1; s = 0; for (i = 1; i <= n; i++) { s += a[1][i] * j; dd[1][i] = s; j++; } for (i = n; i >= 1; i--) { s += a[2][i] * j; dd[2][i] = s; j++; } j = 1; s = 0; for (i = 1; i <= n; i++) { s += a[2][i] * j; ee[2][i] = s; j++; } for (i = n; i >= 1; i--) { s += a[1][i] * j; ee[1][i] = s; j++; } j = 1; long long anss = 0; long long g; int curidx = 1, prev = 1, cur = 2; ans1 = max(ans1, ans); while (1) { g = 0; if (prev == 1 && cur == 2) { anss += j * a[cur][curidx]; cur = 2; prev = 2; g = anss; if (curidx < n) g += ee[1][curidx + 1] + (j - curidx) * (cc[1][curidx + 1] - cc[2][curidx]) - ee[2][curidx]; j++; curidx++; } else if (prev == 2 && cur == 2) { anss += j * a[cur][curidx]; cur = 1; prev = 2; g = anss; g += ee[1][curidx] + (j - curidx) * (cc[1][curidx] - cc[2][curidx]) - ee[2][curidx]; j++; } else if (prev == 2 && cur == 1) { anss += j * a[cur][curidx]; cur = 1; prev = 1; g = anss; if (curidx < n) g += dd[2][curidx + 1] + (j - curidx) * (bb[2][curidx + 1] - bb[1][curidx]) - dd[1][curidx]; j++; curidx++; } else if (prev == 1 && cur == 1) { anss += j * a[cur][curidx]; cur = 2; prev = 1; g = anss; g += dd[2][curidx] + (j - curidx) * (bb[2][curidx] - bb[1][curidx]) - dd[1][curidx]; j++; } ans1 = max(ans1, g); if (curidx > n) break; } cout << ans1 << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int d, n, d1 = 1, a; long long cnt = 0; cin >> d >> n; for (int i = 0; i < n; i++) { cin >> a; d1 = a; if (i != n - 1 && d1 != d) { cnt += d - d1; d1 = 1; } else if (d1 == d) { d1 = 1; } } cout << cnt; return 0; }
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } class fraction { public: int a, b; fraction() { a = 0, b = 1; } fraction(int a, int b) { if (b < 0) { a *= -1; b *= -1; } if (a == 0) b = 1; int g = gcd(abs(a), abs(b)); a /= g; b /= g; this->a = a; this->b = b; } bool operator==(fraction oth) const { return a == oth.a and b == oth.b; } bool operator!=(fraction oth) const { return !(*this == oth); } bool operator<(fraction oth) const { if (a < 0 and oth.a >= 0) return true; if (oth.a < 0 and a >= 0) return false; return (long long)a * oth.b < (long long)b * oth.a; } bool operator>(fraction oth) const { return !(*this < oth) and (*this != oth); } bool operator<=(fraction oth) const { return !(*this > oth); } bool operator>=(fraction oth) const { return !(*this < oth); } }; int main() { int n; scanf( %d , &n); int x1, y1, x2, y2; scanf( %d %d %d %d , &x1, &y1, &x2, &y2); fraction x(0, 1); fraction y(1e9, 1); for (int i = 1; i <= n; i++) { int rx, ry, vx, vy; scanf( %d %d %d %d , &rx, &ry, &vx, &vy); if ((rx == x1 or rx == x2) and vx == 0) { puts( -1 ); return 0; } if ((ry == y1 or ry == y2) and vy == 0) { puts( -1 ); return 0; } fraction tox1(x1 - rx, vx); fraction toy1(y1 - ry, vy); fraction tox2(x2 - rx, vx); fraction toy2(y2 - ry, vy); if (tox2 < tox1) swap(tox1, tox2); if (toy2 < toy1) swap(toy1, toy2); x = max(x, tox1); x = max(x, toy1); y = min(y, tox2); y = min(y, toy2); } if (x >= y) { puts( -1 ); } else { double l = (double)x.a / x.b; printf( %.9lf n , l); } return 0; }
#include <bits/stdc++.h> const int N = 300005; int t[N << 2], tag[N << 2], size[N << 2], st1[N], st2[N], tp1, tp2, n, x, y, a[N]; long long ans; void up(int x) { int t1 = t[x << 1] + tag[x << 1], t2 = t[x << 1 | 1] + tag[x << 1 | 1]; if (t1 == t2) size[x] = size[x << 1] + size[x << 1 | 1]; if (t1 < t2) size[x] = size[x << 1]; if (t1 > t2) size[x] = size[x << 1 | 1]; t[x] = std::min(t1, t2); } void pushdown(int x) { if (tag[x]) { t[x] += tag[x]; tag[x << 1] += tag[x]; tag[x << 1 | 1] += tag[x]; tag[x] = 0; } } void build(int k, int l, int r) { if (l == r) { t[k] = n + 1; size[k] = 1; return; } int mid = (l + r) >> 1; build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r); up(k); } void modify(int k, int L, int R, int l, int r, int val) { if (r < l) return; if (L == l && R == r) { tag[k] = tag[k] + val; return; } int mid = (L + R) >> 1; pushdown(k); if (r <= mid) modify(k << 1, L, mid, l, r, val); else if (l > mid) modify(k << 1 | 1, mid + 1, R, l, r, val); else { modify(k << 1, L, mid, l, mid, val); modify(k << 1 | 1, mid + 1, R, mid + 1, r, val); } up(k); } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d%d , &x, &y); a[x] = y; } int tp1 = tp1 = 0; build(1, 1, n); for (int i = 1; i <= n; i++) { x = a[i]; while (tp1 && x > a[st1[tp1]]) modify(1, 1, n, st1[tp1 - 1] + 1, st1[tp1], x - a[st1[tp1]]), tp1--; st1[++tp1] = i; while (tp2 && x < a[st2[tp2]]) modify(1, 1, n, st2[tp2 - 1] + 1, st2[tp2], a[st2[tp2]] - x), tp2--; st2[++tp2] = i; modify(1, 1, n, i, i, -n - 1); modify(1, 1, n, 1, i - 1, -1); if (t[1] + tag[1] == 0) ans = ans + size[1]; } printf( %lld n , ans); }
#include <bits/stdc++.h> using namespace std; const int OO = 0x3f3f3f3f, N = 1e3 + 5, mod = 1e9 + 7; string str; vector<int> p, v; vector<pair<int, vector<int>>> pos; pair<int, char> occ[26]; int c[N], x = 1, n; char ans[N]; int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } int LCM(int a, int b) { return a / GCD(a, b) * b; } void primes() { for (int i = 2; i <= str.size(); ++i) { bool flag = 1; for (int j = 2; j * j <= i; ++j) if (i % j == 0) flag = 0; if (flag) p.push_back(i); } } void sames() { map<int, vector<int>> mp; bool f[N] = {}; for (int i = 0; i < p.size(); ++i) { if (f[c[i]]) continue; f[c[i]] = 1; for (int j = 0; j < p.size(); ++j) if (c[i] == c[j]) mp[c[i]].push_back(p[j]); } for (auto& ptr : mp) { bool flag[N] = {}; pair<int, vector<int>> tmp = {0, ptr.second}; for (int ch : ptr.second) for (int i = ch; i <= n; i += ch) flag[i] = 1; for (int i = 0; i < N; ++i) tmp.first += flag[i]; pos.push_back(tmp); } sort(pos.rbegin(), pos.rend()); } void occur() { for (int i = 0; i < 26; ++i) occ[i].second = (char)(i + a ); for (char ch : str) ++occ[ch - a ].first; } int main() { memset(c, -1, sizeof c); cin >> str; n = str.size(); primes(); for (int i = 0; i < p.size(); ++i) { if (c[i] == -1) c[i] = x; for (int j = 0; j < p.size(); ++j) if (LCM(p[i], p[j]) <= str.size()) c[j] = c[i]; ++x; } sames(); occur(); for (auto go : pos) { bool flag = 1; sort(occ, occ + 26); for (int i = 0; i < 26; ++i) if (occ[i].first >= go.first) { flag = 0; char chr = occ[i].second; occ[i].first -= go.first; for (int ch : go.second) for (int i = ch; i <= n; i += ch) ans[i] = chr; break; } if (flag) exit(puts( NO )); } puts( YES ); for (int i = 1; i <= n; ++i) { if (ans[i] >= a && ans[i] <= z ) cout << ans[i]; else { for (int j = 0; j < 26; ++j) if (occ[j].first) { --occ[j].first; cout << occ[j].second; break; } } } return 0; }
#include <bits/stdc++.h> using namespace std; void solve(long long case_no) { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; ++i) cin >> a[i]; map<long long, long long> mp; long long mx = LONG_LONG_MIN; for (long long i = 0; i < n; ++i) { long long x = i + a[i] % n + n; ++mp[x % n]; if (mp[x % n] > 1) { cout << NO n ; return; } } cout << YES n ; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long t = 1; cin >> t; for (long long i = 0; i < t; ++i) { solve(i + 1); } return 0; }