solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; unordered_map<long long, long long> par; long long parent(long long x) { while (par[x] != x) { par[x] = par[par[x]]; x = par[x]; } return x; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, i, j, x, t, y; cin >> n; unordered_map<l...
11
#include <bits/stdc++.h> using namespace std; template <class T> inline void checkMax(T &a, const T &b) { if (a < b) a = b; } template <class T> inline void checkMin(T &a, const T &b) { if (b < a) a = b; } template <class T> inline std::string toString(const T &n) { std::ostringstream out; out << n; out.flush...
17
#include <bits/stdc++.h> using namespace std; int const N = 1e5 + 9; vector<vector<int>> v; int n; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int m; cin >> m; v.push_back({0}); for (int j = 0; j < m; j++) { int x; cin >> x; v.back().push_back(x); } sort(v.b...
9
#include <bits/stdc++.h> using namespace std; int main() { char str[100001]; int arr[100001], m; cin >> str; cin >> m; int len = strlen(str); arr[1] = 0; for (int i = 1; i < len; i++) { if (str[i] == str[i - 1]) { arr[i + 1] = arr[i] + 1; } else { arr[i + 1] = arr[i]; } } while...
3
#include <bits/stdc++.h> using namespace std; int main() { long long a, b; cin >> a >> b; while (!(a == 0 || b == 0)) { if (a >= 2 * b) { a %= 2 * b; } else if (b >= 2 * a) { b %= 2 * a; } else { break; } } cout << a << " " << b; return 0; }
3
#include <bits/stdc++.h> using namespace std; int k, n; long long b; pair<int, int> a[100010]; int pos[100010]; int main() { cin >> n >> k >> b; for (int i = 0; i < n - 1; i++) { cin >> a[i].first; a[i].second = i; } sort(a, a + n, greater<pair<int, int> >()); for (int i = 0; i < n - 1; i++) pos[a[i]....
9
#include <bits/stdc++.h> using namespace std; int main() { string input; cin >> input; long long int ans = 1; int qmarks = 0, diff = 0; int first = 0; map<char, bool> letters; for (int i = 0; i < input.size(); ++i) { if (input[i] == '?') ++qmarks; else if (input[i] >= 'A' && input[i] <= 'J' ...
3
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3fLL; const int mod = 1e9 + 7; 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; ...
7
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; long long a[N]; long long dp[N][3]; int main() { long long n, r1, r2, r3, d; cin >> n >> r1 >> r2 >> r3 >> d; for (long long i = 1; i <= n; i++) { cin >> a[i]; } long long x = 0, y = 0; dp[1][0] = r3 + a[1] * r1; dp[1][1] = 1e18; d...
15
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; struct Edge { int v, x; } E[maxn * 2]; int l[maxn], e = 0; inline void insert(int u, int v) { E[e].v = v, E[e].x = l[u], l[u] = e++; } void init() { memset(l, -1, sizeof(l)); e = 0; } int n, m; int T[maxn]; int deg[maxn]; queue<int> Q[2]; int ...
11
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { int ans = 1; if (n != 0) { while (--n) ans = (ans * 3) % 1000003; } cout << ans << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int c = 500002, q = 1050002; vector<int> sz[q], s[q], sol; vector<pair<int, int> > p; bool v[c]; int n, t[2 * c], pos[q]; void dfs(int a) { while (pos[a] < sz[a].size()) { int x = sz[a][pos[a]], y = s[a][pos[a]]; pos[a]++; if (!v[y]) { p.push_back(...
17
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(20); long long int n; cin >> n; vector<long long int> a(n); for (long long int i = 0; i < n; i++) cin >> a[i]; long long int mini = a[0]; long long int maxi = a[...
0
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d, ans = 0; string game; cin >> a >> b >> c >> d >> game; string q, w, e, r; q = to_string(a); w = to_string(b); e = to_string(c); r = to_string(d); for (int i = 0; i < game.length(); i++) { if (game[i] == 49) { an...
0
#include <bits/stdc++.h> using namespace std; int main() { int n, x, j; cin >> n; int p[n], r[n]; p[1] = 0; for (int i = 2; i <= n; i++) { cin >> p[i]; } x = n; j = 0; while (x) { r[j++] = x; x = p[x]; } j -= 1; while (j != -1) { cout << r[j--] << " "; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; long long m = n; long long p = 1; for (long long i = 2; i * i <= n; ++i) { if (n % i == 0) { while (n % i == 0) { n /= i; p *= i; } break; } ...
11
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; int j = 0, b = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (i % 2 != a[i] % 2) { if (i % 2 == 0) { j++; } else { b++; ...
0
#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))...
11
#include <bits/stdc++.h> using namespace std; const double pi = 3.141592653589793; const double tau = 6.283185307179586; const double epsilon = 1e-6; using namespace std; double W, H, THETA; pair<pair<double, double>, pair<double, double> > make_segment(double x1, ...
12
#include <bits/stdc++.h> using namespace std; int main() { vector<int> left, right; multiset<int, greater<int>> stLeft; multiset<int> stRight; int n, x, y, maxIntersection = 0; cin >> n; for (int i = 0; i < int(n); i++) { cin >> x >> y; left.push_back(x); right.push_back(y); stLeft.insert(x)...
8
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int SIZE = 2000; unsigned long long d[SIZE][32], from[SIZE][32]; char s[SIZE]; int bit_cnt[1 << 16]; void myset(unsigned long long a[], int idx) { a[idx >> 6] |= (unsigned long long)1 << (idx & 63); } int myget(unsigned long long a[], int id...
19
#include <bits/stdc++.h> using namespace std; int n, m, k, l, timer = 0, num = 0; vector<vector<int> > g, t, up, sim; vector<int> tin, tout, fup, used, comp, depth; vector<pair<int, int> > bridges; void dfs(int v, int p) { used[v] = true; tin[v] = timer++; fup[v] = tin[v]; for (int i = 0; i < g[v].size(); ++i) ...
8
#include <bits/stdc++.h> using namespace std; char s[100005]; int a[26], n, S, p; bool q; int main() { scanf("%d%d", &n, &S); scanf("%s", s + 1); p = strlen(s + 1); for (int i = 1; i <= p; ++i) ++a[s[i] - 'a']; q = 0; for (int i = 0; i <= 25; ++i) if (a[i] > S) { q = 1; break; } if (q)...
1
#include <bits/stdc++.h> const int N = 55; const int INF = 0x3f3f3f3f; int dp[N][N][N][N], f[N][N]; int n, root, sz[N], deg[N], val[N][N]; std::vector<int> adj[N]; void dfs(int u, int f) { for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == f) continue; dfs(v, u); sz[u] += sz[v]; }...
19
#include <bits/stdc++.h> using namespace std; struct point { int l, r, p, id; }; int low[2222222], high[2222222], mang[2222222], add[2222222]; int n, maxn; point d[111111]; priority_queue<pair<int, int> > pq; bool cmp(point u, point v) { return (u.p < v.p); } void build(int s, int dau, int cuoi) { low[s] = dau; h...
16
#include <bits/stdc++.h> using namespace std; const long long N = 105, M = 502, INF = 1000 * 1000 * 1000 + 7; const double eps = 1e-9; const double pi = 3.1415926535897932384626433832795; int p[N][N]; int main() { int n, k, t; cin >> n >> k >> t; for (int i = 0; i < k; ++i) { int t, b, l; cin >> t >> b >>...
10
#include <bits/stdc++.h> using namespace std; const long long INF = 1000 * 1000 * 1000 * 1ll * 1000 * 1000 * 1000; int n, m; long long socre[200010]; vector<vector<int> > adjlst; bool visited[200010]; struct ans { long long maxv; long long extra; bool goback; }; ans dfs(int cur, int from) { visited[cur] = true;...
14
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 7; int N, er[maxn], we[maxn]; long long int ans = 0; int main() { scanf("%d", &N); int i, j; for (i = 1, j = 1; i <= N; i++) { scanf("%d", &er[i]); while (j > 2 && we[j - 2] >= we[j - 1] && er[i] >= we[j - 1]) { ans += min(we[j - 2...
17
#include <bits/stdc++.h> using namespace std; int main() { int n, T; bool rd[1200], cd[1200]; char op[5]; cin >> T; while (T--) { cin >> n; memset(rd, 0, sizeof(rd)); memset(cd, 0, sizeof(cd)); int cnt = n; if (n == 1) { cout << "! 1 1" << endl; continue; } int maxx = 1...
10
#include <bits/stdc++.h> using namespace std; int n; vector<int> v[1000000 + 10]; int len[1000000 + 10]; vector<int> p; vector<long long int> sum[1000000 + 10]; void merge(int x, int y) { p.clear(); int lx = v[x].size(); int ly = v[y].size(); int i = 0, j = 0; int l = len[y]; while (i < lx && j < ly) { ...
14
#include <bits/stdc++.h> using namespace std; int n; int a[233]; int ta[233]; int sons[233]; bool dfs(int one, int i) { if (i == n) { if (a[i] - 1 - one == 0) return 1; else return 0; } one -= a[i] - 1; if (one < 0) return 0; int s = sons[i]; s += a[i] - 1; if (s < 2) return 0; for (...
15
#include <bits/stdc++.h> using namespace std; int main() { int s = 1, n = 0; string a; cin >> n >> a; for (int i = 1; i <= n - 1; i++) if (a[i] != a[i - 1]) s++; s = min(s + 2, n); cout << s; return 0; }
8
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx") using namespace std; const int MAXSIZE = 10000020; int bufpos; char buf[MAXSIZE]; void init() { buf[fread(buf, 1, MAXSIZE, stdin)] = '\0'; bufpos = 0; } int readint() { int val = 0; for (; !isdigit(buf[bufpos]...
20
#include <bits/stdc++.h> using namespace std; int main() { int n, t; bool c = false; cin >> n >> t; int a[n]; int x = 1; for (int y = 1; y < n; y++) { cin >> a[y]; a[y] += y; } while (x < n) { x = a[x]; if (x == t) { c = true; break; } } if (c == true) cout << "YE...
2
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma target("sse", "sse1") using namespace std; mt19937 gen(34); int n, f[200000]; set<int> free_to_give, free_to_get; bool used[200000]; int cnt_give[200000], cnt_get[200000]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int...
7
#include <bits/stdc++.h> using namespace std; int a[2005], dl[2005], dr[2005]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", a + i); int ans = 0; for (int i = 0; i < n; i++) { if (a[i] == 1) ans++; dl[i] = ans; } ans = 0; for (int i = n - 1; i >= 0; i--) { if (...
10
#include <bits/stdc++.h> using namespace std; int main() { int n, d, h; scanf("%d%d%d", &n, &d, &h); if (2 * h < d) puts("-1"); else { if (d == 1) { if (n == 2) puts("1 2"); else puts("-1"); return 0; } for (int i = 2; i <= h + 1; i++) printf("%d %d\n", i - 1, i...
8
#include <bits/stdc++.h> using namespace std; char str[100005]; const char *alpha = "abcdefghijklmnopqrstuvwxyz"; long long get(const char *p) { int len = strlen(p); bool cent = len >= 3 && p[len - 3] == '.'; long long ret = 0; while (*p) { char ch = *p++; if (ch == '.') continue; ret = ret * 10 + c...
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000; const int MAXM = MAXN * MAXN; const double inf = 1e100; struct Edge { int u, v, cap, next; double cost; }; int n, x[MAXN], y[MAXM]; double ans; int sum, head[MAXN], pre[MAXN]; Edge edge[MAXM]; double dis(int i, int j) { return sqrt((x[i] - x[j])...
16
#include<bits/stdc++.h> using namespace std; #define int long long int #define ld long double #define MOD 1000000007 #define endl "\n" #define pb push_back #define mp make_pair #define ff first #define ss second #define all(var) (var).begin(), (var).end() #define sz(x) (int)x.size() #define MAXX 9000000000000000000 #de...
4
#include <bits/stdc++.h> using namespace std; int t[300010]; set<int> s; int main() { int n, m, l, r, x; cin >> n >> m; for (int i = 1; i <= n; i++) { s.insert(i); } for (int i = 0; i < m; i++) { cin >> l >> r >> x; set<int>::iterator it; it = s.lower_bound(l); vector<int> toErase; whi...
7
#include <bits/stdc++.h> using namespace std; int main() { int a[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cin >> a[i][j]; } } int m = a[0][1] + a[0][2]; int l = a[1][0] + a[1][2]; int n = a[2][0] + a[2][1]; int sum = n + m + l; sum = sum / 2; a[0][0] = sum - m; ...
3
#include <bits/stdc++.h> int main() { int n, s, a, x = 0, max = 0, i, j; scanf("%d %d", &n, &s); for (i = 1; i <= n; i++) { scanf("%d", &a); x = x + a; if (a > max) { max = a; } } j = x - max; if (j > s) { printf("NO\n"); } else { printf("YES\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; t > i; ++i) { int n; cin >> n; if (n % 2 == 1) { cout << 7; } else { cout << 1; } for (int i = 1; n / 2 > i; ++i) { cout << 1; } cout << endl; } }
1
#include <bits/stdc++.h> using namespace std; bool secdes(const pair<long long int, long long int>& a, const pair<long long int, long long int>& b) { if (a.second == b.second) { return a.first > b.first; } else { return a.second > b.second; } } bool sir(const pair<long long int, long long int>...
7
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; const long long INF = (1ll << 63) - 1; struct node { long long x, y, z; } po[maxn]; int n; void update(pair<long long, long long> &ra, long long mid, long long lim, long long ad) { long long r = (lim + mid - ad), l = (mid - ad - lim)...
21
#include <bits/stdc++.h> using namespace std; int main() { int i, k; string a, b; cin >> a >> b; k = a.length(); int c[k]; for (i = 0; i < k; i++) { c[i] = a[i] != b[i]; } for (i = 0; i < k; i++) { cout << c[i]; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int book[20]; int main() { int n, k, i, j, a; cin >> n >> k; for (i = 0; i < n; i++) { int now = 0; for (j = 0; j < k; j++) { scanf("%d", &a); now += a * (1 << j); } book[now]++; } int flog = 0; for (i = 0; i < 16; i++) { for (j =...
7
#include <bits/stdc++.h> using namespace std; const int maxn = (int)3e5 + 100; const int mod = (int)1e9 + 7; const int maxlog = (int)20; const int P = mod; int n; int sum(int x) { int res = x; while (x) { res += x % 10; x /= 10; } return res; } int main() { srand(time(0)); cin >> n; int i = max(1,...
4
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; const long long INF = 1e18; template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& a) { in >> a.first >> a.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> a) { o...
8
#include <bits/stdc++.h> using namespace std; long long edgbe[1000000] = {0}; long long edgen[1000000] = {0}; long long root[1000000] = {0}; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) cin >> edgbe[i] >> edgen[i]; int qs = 0, qe = 0; for (int i = 0; i < n; i++) if (edgbe[i] == 1) root...
7
#include <bits/stdc++.h> using namespace std; const int N = 100005; char str[N]; void work() { scanf("%s", str); set<char> h; int sum = 0; for (auto c : str) if (isalpha(c)) h.insert(c); else if (c == '?') ++sum; int p = 10, c = h.size(); long long res = 1; if (*str == '?') res *= 9, -...
3
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; constexpr int N = 2e5 + 5; int n, val[N]; bool check0() { return is_sorted(val + 1, val + n + 1); } bool check1() { int cnt = 0; for (int i = 1; i <= n; i++) { cnt += val[i] == i; } int id...
7
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 9; int n, x[100005], y[100005]; long long ans; vector<int> bt[100005], tp[100005]; set<int> s; map<pair<int, int>, int> g; inline bool isf(const int& x) { bool f = true; for (const int& y : tp[x]) if (bt[y].size() == 1) f = false; return f; }...
13
#include <bits/stdc++.h> using namespace std; long help(long k, long ss, string s) { long temp = 0; long ans = 0; bool found = false; for (int i = 0; i < s.size(); i++) { temp = s[i] - '0'; if (temp == 0) { ss += 9; ans++; } if (ss >= k) { found = true; break; } } ...
3
#include <bits/stdc++.h> using namespace std; const unsigned long long MOD = 257; int n, m; string s[5005]; int L[5005], R[5005], U[5005], D[5005]; void init() { int b, pos; for (int i = 0; i < n; i++) { b = -1; for (int j = 0; j < m; j++) { if (s[i][j] != '.') { pos = i * m + j; L[pos...
15
#include <bits/stdc++.h> using namespace std; int ans, now1, now2, l[105], r[105], n, t1, t2, p1, p2, p3; int main() { scanf("%d%d%d%d%d%d", &n, &p1, &p2, &p3, &t1, &t2); for (int i = 1; i <= n; i++) { scanf("%d%d", &l[i], &r[i]); } for (int i = 1; i < n; i++) { ans += p1 * (r[i] - l[i]); now1 = r[i...
1
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-6; const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const int maxn = (1 << 16) + 5; long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch...
7
#include <bits/stdc++.h> using namespace std; inline string substring(string _t, int F, int T) { return _t.substr(F, T - F); } template <class _T> inline string tostr(const _T& a) { ostringstream os(""); os << a; return os.str(); } template <class T> T sqr(T x) { return x * x; } double LOG(long long int N, long...
7
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d %d", &a, &b); if (b == 0) { printf("1\n"); continue; } if (a == 0) { printf("0.5\n"); continue; } if (4 * b >= a) printf("%.6lf\n",...
10
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n; int a[2 * n + 1], start[n + 1], end[n + 1]; bool check[n + 1]; memset(check, 0, sizeof check); for (int i = 1; i <= 2 * n; i++) { cin >> a[i]; if (check[a[i]] == 0) { start[a[i]] = i; check[a[i]] = 1; } el...
6
#include <bits/stdc++.h> using namespace std; #pragma warning(disable : 4996) int main(void) { int a, b; cin >> a >> b; for (int x = -a; x <= a; x++) { for (int y = -a; y <= a; y++) if (x * x + y * y == a * a) { for (int i = 1; i <= max(a * a, b * b); i++) { if (i * x * i * x == (b * b...
8
#include <bits/stdc++.h> using namespace std; inline void scanint(int &x) { register int c = getchar(); x = 0; for (; (c < 48 || c > 57); c = getchar()) ; for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; } } int N, ROOT, CHAINS = 0; vector<pair<int, int>> adj[100005]; int DE...
13
#include <bits/stdc++.h> using namespace std; namespace Input { const int BUF = 65536; char buf[BUF + 1]; char *head = buf, *tail = buf; inline char inputchar() { if (head == tail) *(tail = (head = buf) + fread(buf, 1, BUF, stdin)) = 0; return *head++; } inline void inputnum(int &ret) { char ch = inputchar(); w...
17
#include <bits/stdc++.h> using namespace std; long long get(long long x) { if (x == 0) return 0; return get(x >> 1) + (x & 1); } long long pw(long long x, long long y, long long m) { long long z = 1; for (; y; y >>= 1, x = x * x % m) if (y & 1) z = z * x % m; return z; } const int N = 1111111; const long ...
15
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7, siz = 1e6 + 5; long long t, n, m, k, deg[siz]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (long long i = 1; i <= m; i++) { long long x, y; cin >> x >> y; deg[x]++; deg[y]++; } long...
11
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; long long p = 1; while (p <= n) { p *= 2; } p--; if (k == 1) { cout << n << endl; } else { cout << p << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; const int M = 1e5 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; int n, t[N], dp[N][3], mn[N], a[N]; int st1[N], st2[N], head1, head2, rear1, rear2; int main() { memset(dp, inf, sizeof(dp)); mem...
8
#include <bits/stdc++.h> using namespace std; int mod(int x) { if (x < 0) return -x; return x; } int main() { int n, last(1), ar[100000], temp(2), ans(2); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &ar[i]); for (int i = 2; i < n; i++) { if ((ar[i] - ar[i - 1]) == 0) { temp++; a...
6
#include <bits/stdc++.h> using namespace std; int n, m, q; long long tot, st[1005 * 2][1005 * 2], sum[1005 * 2][1005 * 2]; long long num(long long x) { long long res = 0; while (x) { if (x & 1) res++; x >>= 1; } return res; } long long find(long long x, long long y) { if ((num(x) + num(y)) % 2) re...
17
#include <iostream> #include <vector> #include <algorithm> using namespace std; void Solve(int n, int x) { vector<long long> a(n); long long max_beauty = 0; long long arr_sum = 0; for (auto &elem : a) { cin >> elem; max_beauty += (elem + x - 1) / x; arr_sum += elem; } ...
1
#include <bits/stdc++.h> using namespace std; int arr[2] = {0, 0}; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int len; string str; cin >> len >> str; for (int i = 0; i < len; i++) arr[str[i] - '0']++; if (arr[0] != arr[1]) { cout << "1\n" << str << '\n'; } else { char c...
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v; int temp; for (int i = 0; i < n; i++) { cin >> temp; v.push_back(temp); } vector<int> v2; int first = v[0]; int count = 0; for (int i = 0; i < n; i++) { if (v[i] == first) { count++; } el...
9
#include <bits/stdc++.h> using namespace std; const int MAXN = (1 << 18); const int sigma = 8; int n; string s; int a[MAXN]; void read() { cin >> n >> s; } vector<int> li[sigma]; int mn_dist[sigma][sigma], dist[MAXN][sigma]; void bfs(int src_let) { queue<int> Q; for (int pos : li[src_let]) dist[pos][src_let] = 0, Q...
25
#include <bits/stdc++.h> using namespace std; const long long inf = 1e17 + 10; const int N = 1e6 + 10; const long long mod = 1e9 + 7; const int base = 131; const double pi = acos(-1); map<string, int> ml; map<int, int> vi; long long b[N], c[N], num[N], a[N], n, m, k, x, y, z, vis[N]; long long ex, ey, cnt, ans, sum, fl...
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); if (fopen("1.inp", "r")) freopen("1.inp", "r", stdin); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; string s; cin >> s; cout << n << endl; for (int i = 0; i < k - 1;...
9
#include <bits/stdc++.h> using namespace std; int main() { int n, ram[100], speed[100], cost[100], hdd[100], flag[100] = {0}, min1; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d %d %d %d", &speed[i], &ram[i], &hdd[i], &cost[i]); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ...
2
#include <bits/stdc++.h> using namespace std; long long n, m, tt, ans[1001000], num[1001000], le[1001000], ri[1001000]; struct Node { long long ls, rs, sum, bj; void clear() { sum = bj = 0; } } node[1001000 << 1]; vector<pair<long long, long long> > lq[1001000], rq[1001000]; void build(long long now, long long l, l...
17
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9; const long long MOD = 1e9 + 7; long long n, m, k; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; if (m <= k) { cout << "1.00000000\n"; return 0; } else { if (n + k < m) { cout << "0.000000000\n"; ...
16
#include <bits/stdc++.h> using namespace std; const int rlen = 1 << 18 | 1; char buf[rlen], *ib = buf, *ob = buf; inline int read() { int ans = 0; char ch = (((ib == ob) && (ob = (ib = buf) + fread(buf, 1, rlen, stdin)), ib == ob) ? -1 : *ib++); while (!isdigit(ch)) ch = (((ib == o...
25
#include <bits/stdc++.h> using namespace std; template <typename T> inline ostream &operator<<(ostream &out, const vector<T> &v) { for (auto &it : v) out << it << ' '; return out; } template <typename T> inline istream &operator>>(istream &in, vector<T> &v) { for (auto &it : v) in >> it; return in; } template <...
15
#include <bits/stdc++.h> using namespace std; template <class T> inline T Tmin(const T &lch, const T &rch) { if (lch < rch) return lch; return rch; } template <class T> inline T Tmax(const T &lch, const T &rch) { if (lch < rch) return rch; return lch; } template <class T> inline T Tabs(const T &ch) { if (ch <...
23
#include <bits/stdc++.h> int main() { char com[100]; gets(com); int i, check = 0; for (i = 0; i < strlen(com); i++) { if (com[i] == 'H' || com[i] == 'Q' || com[i] == '9') { check = 1; break; } } if (check == 1) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
1
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; const int MAXN = 1e6 + 10; const long long MOD = 1e9 + 7; const long long MOD2 = 998244353; const long long INF = 8e18; const int LOG = 20; long long pw(long long a, long long b, long long mod) { return (!b ? 1 : (b & 1 ? (a * pw(a ...
11
#include <bits/stdc++.h> using namespace std; int main() { unsigned long long int n; cin >> n; n++; unsigned long long int o = 0; o = pow(2, n) - 2; cout << o; return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[5]; int main() { scanf("%d %d %d %d", &a[1], &a[2], &a[3], &a[4]); if (a[1] < max(a[3], a[4]) || a[2] < max(a[3], a[4])) { printf("-1"); return 0; } if (abs(a[3] - a[4]) > 1) { printf("-1"); return 0; } if (a[3] == a[4]) { if (a[1] == a...
10
#include <bits/stdc++.h> using namespace std; long long int mn = INT_MAX; long long int mx = INT_MIN; const long long int mv = 1000005; const double pi = 3.141592653589793238462643; long long int n, k, a[mv], c[mv], d[mv], ans; map<long long int, long long int> b; int main() { ios_base::sync_with_stdio(NULL); cin.t...
9
#include <bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" const int mxN = 1e6; void solve() { ll n; cin>>n; ll a[n]; for(int i=0;i<n;i++) { cin>>a[i]; } ll last = 0,sum = 0; for(int i=0;i<n;i++) { if(a[i] >= last) { sum += max(0,(int)(a[i] - last)); } else if(a[i] ...
1
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; cout << (n * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / 4 * (n - 4) / 5) + (n * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / 4 * (n - 4) / 5 * (n - 5) / 6) + (n * (n - 1) / 2 * (n - 2) / 3 * (n - 3) / ...
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; vector<vector<int>> mtrx; mtrx.assign(min(n, m), vector<int>(max(n, m))); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char c; ...
12
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); long long int i, n, c; double x, y, rem, quo, a, d, j, face, xx; x = y = 0; face = 1; scanf("%lf", &a); scanf("%lf", &d); scanf("%I64d", &n); quo = fmod(d, 4 * a); xx = quo; for (i = 1; i <= n; i++) { c = (i * ...
7
#include <bits/stdc++.h> using namespace std; const int N = 5e6 + 1; const int mod = 1e9 + 7; int n; int sz[N], p[N], mn[N], mx[N]; double sum[N], ar[N]; void init() { for (int i = 0; i < n + 2; i++) { sz[i] = 1, p[i] = i; mx[i] = mn[i] = i; sum[i] = ar[i]; } } int get(int x) { while (x != p[x]) { ...
13
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class t, class u> void chmax(t& first, u second) { if (first < second) first = second; } template <class t, class u> void chmin(t& first, u second) { if (second < first) first = second; } template <class t> using vc = vector<t>; template ...
25
#include <bits/stdc++.h> using namespace std; const int N = 1555; struct Point { int x, y; int index; Point(int x, int y, int index) : x(x), y(y), index(index) {} bool operator<(const Point &o) const { if (x != o.x) return x < o.x; return y < o.y; } }; bool ccw(Point a, Point b, Point c) { int x1 = ...
14
#include <bits/stdc++.h> using namespace std; const int MAXV = 1000005, MAXE = 1000005, oo = 0x3f3f3f3f; int E, last[MAXV], nextssss[MAXE], adj[MAXE], level[MAXV], now[MAXV], Q[MAXV], source, sink; int cap[MAXE], flow[MAXE]; void add_edge(int u, int v, int c) { cap[E] = c, flow[E] = 0, adj[E] = v, nextssss[E] = l...
13
#include <bits/stdc++.h> using namespace std; inline long long ty() { register long long a = 0, b = 1, c = getchar(); while (!isdigit(c)) b ^= c == '-', c = getchar(); while (isdigit(c)) a = a * 10 + c - 48, c = getchar(); return b ? a : -a; } const long long _ = 1002, __ = _ * _, mo = 998244353; struct node { ...
15
#include <bits/stdc++.h> using namespace std; mt19937_64 rnd(std::chrono::system_clock::now().time_since_epoch().count()); signed main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; int minim = 1e9, maxim = 0; for (int i = 0; i < n; i...
3
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e18; const long long mod = (long long)1e9 + 7; const double pi = acos(-1.0); const double eps = (double)1e-9; const int dx[] = {0, 0, 1, 0, -1}; const int dy[] = {0, 1, 0, -1, 0}; const int N = 100500; struct Edge { string from, to; }; ve...
10
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; char c = s[1]; char d = s[0]; if (c == '1' && d == 'a' || c == '1' && d == 'h' || c == '8' && d == 'a' || c == '8' && d == 'h') { cout << 3 << endl; return 0; } else if (d == 'h' || d == 'a' || c == '1' || c == '8')...
0
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; int a[MAX], f[MAX], n; bool pal() { for (long long i = 0; i < (long long)(n); i++) { if (a[i] != a[n - i - 1]) return 0; } return 1; } bool pos(int l, int r) { memset(f, 0, sizeof(f)); for (long long i = l; i < (long long)(r + 1); i++)...
17