solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; int n, m, t[12000], tl[3010][3010], tz[3010][3010], tr[3010][3010], z[3010][3010], ini[12000], fim[12000], a[3010]; long long r; vector<int> mst[12000]; char c; void build_mst(int pos, int lo, int hi) { ini[pos] = lo; fim[pos] = hi; mst[pos].clear(); if (lo == h...
15
#include <bits/stdc++.h> using namespace std; const long long INF = 0x3f3f3f3f3f3f3f3f; inline long long UB(vector<long long> &v, long long x) { return upper_bound(v.begin(), v.end(), x) - v.begin(); } inline long long LB(vector<long long> &v, long long x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); } ...
9
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> a(n, 0); for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> r_seq, t_seq; for (int i = 0; i < m; ++i) { int t, r; cin >> t >> r; while (!r_seq.empty() && r >= r_seq.back()) { r_seq.p...
9
#include <bits/stdc++.h> using namespace std; int N, M, mins, maks, K, F, i; int main() { cin >> N >> M; mins = 1; maks = 1000; for (i = 0; i < M; i++) { cin >> K >> F; mins = max(mins, (K - 1) / F + 1); if (F > 1) { maks = min(maks, (K - 1) / (F - 1)); } } mins = (N - 1) / mins + 1; ...
7
#include <bits/stdc++.h> using namespace std; long long a, second[1111111], b[1111111], sum, c[1111], ans, n; pair<long long, long long> p[111111]; int main() { vector<long long> v; cin >> a >> n; for (int i = 1; i <= a; i++) { cin >> p[i].first; p[i].second = i; } sort(p + 1, p + a + 1); for (int i...
2
#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 int N = 55; long long dp[N][10]; char s[N]; int main() { int n; scanf("%s", s + 1); n = strlen(s + 1); for (int i = 0; i < 10; i++) dp[1][i] = 1; for (int i = 2; i <= n; i++) { int now = s[i] - '0'; for (int j = 0; j <= 9; j++) { if ((now + j) ...
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; const long long INF = 1e18; const long long MOD = 10007; long long G[MAXN], L[MAXN]; long long N; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> N; for (int i = (0); i < (N); i++) { cin >> G[i]; L[i...
4
#include <bits/stdc++.h> using namespace std; const int inf = 1000000000; char s[200000]; int i, j, n, len, num[30]; int main() { scanf("%s", s); len = strlen(s); for (i = 0; i < len; i++) num[s[i] - 'a']++; for (i = 0; i < len; i++) { bool flag = true; for (j = s[i] + 1; j <= 'z'; j++) if (num[j ...
3
#include <bits/stdc++.h> using namespace std; long long int binomialCoeff(long long int n, long long int k) { long long int res = 1; if (k > n - k) k = n - k; for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } long long int power(long long int x, long long int y) { long lo...
15
#include <bits/stdc++.h> using namespace std; int main() { long long n, x; cin >> n >> x; long long p, s = 0; for (long long i = 0; i < n; i++) { cin >> p; s += p; } if (s + n - 1 <= x) { x = x - (s + n - 1); if (x == 0) cout << "YES"; else cout << "NO"; } else cout << ...
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int t; cin >> t; while (t--) { int n, k; cin >> n >> k; int ans = n; for (int j = 1; j * j <= n; j++) { if (n % j == 0) { if (j <= k) { ...
5
#include <bits/stdc++.h> using namespace std; int n; struct point { int x, y; void in() { scanf("%d%d", &x, &y); } }; struct node { point a, b, c, d; void update() { c.x = b.x; c.y = a.y; d.x = a.x; d.y = b.y; } } rec[6]; long long cal(point a, point b) { int x = b.x - a.x; int y = b.y - a...
7
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long dp[55][1010]; long long v[55][1010]; long long ke[55]; long long bin[2020][2020]; void cv(int k, int n) { int rs = n - k; if (rs < 0) return; for (int i = 0; i <= rs; i++) { v[k][n] += bin[k + rs - i][k] * dp[k][i]; v[k...
15
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; for (int i = 1; i <= n; i++) { for (int x = 0; x < n; x++) { if (s[x] == '.') continue; int j = x; int num = 1; while (j < n) { j += i; if (j < n && s[j] == '*') ...
5
#include <bits/stdc++.h> using namespace std; int mod = 1e9 + 7; int main() { cin.tie(0); cin.sync_with_stdio(0); cout.tie(0); cout.sync_with_stdio(0); int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort((a).begin(), (a).end()); cout << a[n - 1] - a[0] + 1 - a.size(); }
0
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; for (int i = 1; i < s.size(); i++) { if (s[i] == s[i - 1]) { for (char j = 'a'; j <= 'z'; j++) { if (s[i - 1] != j && s[i + 1] != j) { s[i] = j; break; } } } } cout << s << en...
5
#include <bits/stdc++.h> using namespace std; int n, k, a[100100], dp[100100], l[100100]; int rec(int i) { if (i == 0) return 0; l[i] = min(n, i + k); if (a[i] == 0) return min(n, i + k) - max(1, i - k) + 1; if (dp[i]) return dp[i]; dp[i] = rec(a[i]) + min(n, i + k) - max(l[a[i]], i - k - 1); return dp[i]; ...
6
#include <bits/stdc++.h> int main() { int n; std::cin >> n; if (n < 4) { std::cout << "NO"; return 0; } if (n == 4) { std::cout << "YES\n1 * 2 = 2\n2 * 3 = 6\n6 * 4 = 24"; } else if (n == 5) { std::cout << "YES\n4 * 5 = 20\n20 + 3 = 23\n23 + 2 = 25\n25 - 1 = 24"; } else { std::cout << ...
7
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; double d = sqrt(b * b - 4 * a * c); double x = -(b + d) / 2 / a; double y = -(b - d) / 2 / a; if (a < 0) { swap(x, y); } cout << setprecision(9) << y << "\n" << setprecision(9) << x; return 0; }
5
#include <bits/stdc++.h> using namespace std; pair<int, int> a[100020]; double z, s; int n, v; bool cmp(const pair<int, int>& a, const pair<int, int>& b) { return a.first * a.second * (100 - b.second) > b.first * b.second * (100 - a.second); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) ...
13
#include <bits/stdc++.h> using namespace std; const int MAXN = 300000; const int MAXB = 20; int n, m, k, c; int fa[MAXN][MAXB], dep[MAXN]; bool vis[MAXN]; vector<int> g[MAXN], cir[MAXN]; int lca(int u, int v) { if (dep[u] < dep[v]) swap(u, v); for (int b = MAXB - 1; b >= 0; b--) if (dep[fa[u][b]] >= dep[v]) u =...
19
#include <bits/stdc++.h> using namespace std; int main() { long long(n), (k); scanf("%lld%lld", &(n), &(k)); ; vector<long long> A((n)); for (long long i = 0; i < n; i++) scanf("%lld", &A[i]); ; vector<pair<long long, long long> > B; for (long long i = 0; i < (n); i++) { if (A[i] != 1) { B.pus...
13
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); if (n < 2) return puts("0"), 0; printf("%d", (n + 1) / 2 - 1); return 0; }
2
#include <bits/stdc++.h> using namespace std; int i, i0, n, a[100005], A, b[100005]; long long m, ans; set<int> s; int main() { scanf("%d %lld", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); scanf("%d", &A); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); for (int i = 1; i <= n; i++) { s.inse...
7
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; const int MOD = 1e9 + 7; int n, a[N], nxt[1007]; vector<int> pos; bool isLucky(int x) { while (x > 0) { if (x % 10 != 4 && x % 10 != 7) return false; x /= 10; } return true; } long long tmp[5]; const long long x[] = {2, 3, 2, 2}; long lo...
21
#include <bits/stdc++.h> using namespace std; int n, m; string s, p; int a[2005], dp[2005][2005]; int main() { cin >> s >> p; n = s.size(); m = p.size(); for (int i = 0; i < n; i++) { int j; for (j = 0; j < m && i + a[i] < n;) { if (s[i + a[i]] == p[j]) j++; a[i]++; } if (j < m) a[i]...
14
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > mas; map<pair<int, int>, int> MI, MA; int main() { int n, m; scanf("%d %d", &n, &m); mas.resize(m); for (int i = (0); i < (m); ++i) { int cj, ci; scanf("%d %d", &cj, &ci); --cj; --ci; mas[i] = make_pair(cj, ci); if (!(...
9
#include <bits/stdc++.h> using namespace std; const int MX = 3e5 + 5; int Head[MX], erear; struct Edge { int u, v, nxt, z; } E[MX << 1]; void edge_init() { erear = 0; memset(Head, -1, sizeof(Head)); } void edge_add(int u, int v, int z) { E[erear].u = u; E[erear].v = v; E[erear].z = z; E[erear].nxt = Head[...
15
#include <bits/stdc++.h> using namespace std; const int mod = 998244353, maxn = 23, maxl = 2e5 + 5; inline int Add(int x, int y) { return (x += y) >= mod ? x - mod : x; } inline int Mul(int x, int y) { return 1ll * x * y % mod; } int n, cnt[maxn][26], f[1 << maxn], minx[26]; char s[maxl]; long long ans; int main() { ...
16
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { return (y ? gcd(y, x % y) : x); } int main() { int n, m; map<int, int> a; cin >> n; m = n * n; for (int i = 0; i < m; i++) { int temp; scanf("%d", &temp); if (a.count(temp)) a[temp]++; else a[temp] = 1; } vec...
9
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int main() { long long a, b, c, d; while (~scanf("%lld%lld%lld%lld", &a, &b, &c, &d)) { long long ans = 0; for (long long i = a; i <= b; ++i) { if (i + b <= c && i + c <= d) { ans += i * (i + 1) / 2; } else if (i + b <=...
10
#include <bits/stdc++.h> using namespace std; int main() { cout << "Even" << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; void getsolution(long long n, long long k) { if (n >= k) { cout << (n - k) % 2 << endl; return; } else { cout << k - n << endl; return; } } int main() { int t; cin >> t; while (t-- != 0) { long long n, k; cin >> n >> k; getsolution(n,...
1
#include <bits/stdc++.h> using namespace std; struct Pair { int l, r; Pair(int ll, int rr) : l(ll), r(rr){}; }; vector<Pair> ans; int a[3001]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= n; i++) { int mi = a[i], mp = i; for (in...
4
#include <bits/stdc++.h> using namespace std; int r, c, k; char s[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; char gri[219][219]; char ans[219][219]; int main() { int T; scanf("%d", &T); while (T--) { scanf("%d%d%d", &r, &c, &k); for (register int i = 1; i <= r; i++) scanf("%s", ...
9
#include <bits/stdc++.h> using namespace std; vector<int> adj[2000005]; int deg[2000005]; bool u[2000005]; int n, m, cnt; long long ans; void dfs(int v) { u[v] = 1; if (deg[v] & 1) { ++cnt; } for (int to : adj[v]) { if (u[to]) { continue; } dfs(to); } } void solve() { scanf("%d %d", &n...
16
#include <bits/stdc++.h> using namespace std; template <typename C> void ma(C& a, C b) { if (a < b) a = b; } template <typename C> void mi(C& a, C b) { if (a > b) a = b; } int n, m, dx, dy; int wyn = 0; int il[1001000]; int g[1001000]; int main() { scanf("%d%d%d%d", &n, &m, &dx, &dy); int akx = 0; int aky = 0...
12
#include <bits/stdc++.h> using namespace std; int n; int vis[(1 << 22)]; int mark[(1 << 22)]; void dfs(int s) { if (vis[s]) return; vis[s] = 1; for (int i = 0; i < n; i++) { if (s & (1 << i)) { dfs(s - (1 << i)); } } if (mark[s]) dfs((1 << n) - 1 - s); } int32_t main() { ios::sync_with_stdio(0...
17
#include <bits/stdc++.h> using namespace std; string vow = "aeiou"; int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2}; const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1}; const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}; const int dy[] = {0, 1, 0, -1, -1,...
11
#include <bits/stdc++.h> using namespace std; const int MaxN = 5e2 + 10; const int INF = 1e9; const int MOD = 1e9 + 7; int main() { double a, b, c; cin >> a >> b >> c; if (a < 0) { a = -a; b = -b; c = -c; } double d = sqrt(b * b - 4 * a * c); cout.precision(6); cout << fixed << (-b + d) / (2 *...
5
#include <bits/stdc++.h> using namespace std; int N, L, K; double arr[205]; int bags[205]; double dp[205][205][205][2]; double solve(int i, int j, int a, int b) { int a1 = a, b1 = b; if (i == 0) { if (j >= L && b == 1) { return 1.0; } else { return 0.0; } } double& ret = dp[i][j][a][b]; ...
10
#include <bits/stdc++.h> using namespace std; int A[200010]; int main() { int N; cin >> N; int ans = 0; for (int i = 0; i < N; i++) { cin >> A[i]; } for (int i = 0; i < N; i++) { ans += 1; int lr = i; while (lr < N && A[lr] == -1) lr += 1; if (lr == N) break; int next = lr + 1; w...
16
#include <bits/stdc++.h> using namespace std; const long double PI = acos(-1); const int INF = 2e9; const long long LINF = 1e18 + 5; const long long MOD = 1e9 + 7; const int MAXN = 1e5 + 5; const int MN = 1e6 + 2; bitset<1000005> b1; bitset<1000005> b2; int used[1000005]; int a[1002]; int main() { int n, k; scanf("...
15
#include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(bool x) { return (x ? "T" : "F"); } string to_string(const char* s) { return to_string((string)s); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ...
19
#include <bits/stdc++.h> using namespace std; const int inf = (int)1.01e9; const long long infll = (long long)1.01e18; const long double eps = 1e-9; const long double pi = acos((long double)-1); mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int x) { return mrand() % x; } void precalc() ...
26
#include <bits/stdc++.h> using namespace std; const double eps = 0.000000000000001; int uni[200005], k, vis[200005]; long long ans = 0; vector<int> G[200005]; int DFS(int src) { vis[src] = 1; int below = uni[src]; for (int i = 0; i < G[src].size(); ++i) { if (!vis[G[src][i]]) below += DFS(G[src][i]); } an...
10
#include <bits/stdc++.h> using namespace std; int n; const int inf = 99999999, P = 1000000007; int maxdfn, maxcol, m; int _last[4000005], _next[4000005], x[4000005], y[4000005], e; int dfn[400005], low[400005], in[400005]; int sta[400005], col[400005], s[400005], z; int c[200005]; inline void edge(int u, int v) { e++...
14
#include <bits/stdc++.h> using namespace std; int main() { int n, l = 0, r = 0, d = 0, u = 0, x, y; string str; cin >> n >> str; for (int i = 0; i < str.size(); i++) { if (str[i] == 'L') l++; else if (str[i] == 'R') r++; else if (str[i] == 'D') d++; else if (str[i] == 'U') ...
2
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&...
10
#include <bits/stdc++.h> const double EPS = 1e-24; const long long MOD = 1000000007ll; const double PI = 3.14159265359; long long INF = 1e18; template <class T> T Max2(T a, T b) { return a < b ? b : a; } template <class T> T Min2(T a, T b) { return a < b ? a : b; } template <class T> T Max3(T a, T b, T c) { retur...
11
#include <bits/stdc++.h> const int N = 1010; int n, i, j, k, x, a[N], g[N][N][8], v[8], f[256][10], ans; inline void umin(int& a, int b) { if (a > b) a = b; } inline void umax(int& a, int b) { if (a < b) a = b; } int main() { scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]), a[i]--; for (i = 1; i ...
14
#include <bits/stdc++.h> using namespace std; unsigned long long a[2000][2000 / 63 + 1], p[63]; int b[2000], c[2000], n, t, i, j, k, nn; bool f; int main() { scanf("%d", &n); nn = n / 63 + 1; for (i = 0; i < 63; i++) p[i] = 1ULL << i; for (i = 0; i < n; i++) for (j = 0; j < n; j++) { scanf("%d", &t); ...
14
#include <bits/stdc++.h> using namespace std; const int N = 1000000, M = 2000; int main() { ios::sync_with_stdio(false); int n; cin >> n; string str; cin >> str; int ans = 0; for (int i = 0; i < n; i++) { if (str[i] != '<') break; ans++; } for (int i = n - 1; i >= 0; i--) { if (str[i] != '...
2
#include <bits/stdc++.h> using namespace std; template <class T> string TS(T ss) { ostringstream oss; oss << ss; return oss.str(); } template <class T> T ABS(T n) { return (n >= 0 ? n : -n); } template <class T> T BP(T n, long long int k) { T res = 1; while (k) { if (k & 1) { res *= n; } n...
6
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5, mod = 998244353; long long sum, arr[N]; vector<int> v; string s, t; int n, m; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); arr[0] = 1; for (int i = 1; i < N; ++i) (arr[i] = arr[i - 1] * 2) %= mod; cin >> n >> m >> s >> t;...
9
#include <bits/stdc++.h> using namespace std; multiset<int> Set; pair<int, int> c[181], d[181]; int a, b, n, m, i, j, k, nom, ans; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (i = 1; i <= n; i++) c[i].first = 2000000000; for (i = 1; i <= m; i++) { cin >> a >> b; c[a].second++;...
9
#include <bits/stdc++.h> using namespace std; int main() { int n = 0; cin >> n; int a[102] = {}; for (int i = 0; i < n; ++i) { cin >> a[i]; } int min = 1 << 20; int ans_i = 0; int ans_j = 0; for (int i = 0; i < n; ++i) { if (min > abs(a[i] - a[(i + 1) % n])) { min = abs(a[i] - a[(i + 1) ...
0
#include <bits/stdc++.h> using namespace std; struct Set { int head, tail, size; Set(int x) { head = x; tail = x; size = 1; } Set() { head = tail = size = 0; } } s[100005]; int a[100005], Next[100005], head[100005]; void MakeSet(int x) { s[x] = Set(x); head[x] = x; Next[x] = -1; } int FindSet(...
11
#include <bits/stdc++.h> using namespace std; inline int read() { int f = 1, x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') { f = -1; } ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int...
19
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long ct[2 * 100005] = {0}; long long arr[2 * 100005] = {0}; long long mct = 0, mx = -1; arr[0] = -1; for (long long i = 1; i <= n; i++) { cin >> arr[i]; ct[arr[i]]++; if (ct[arr[i]] > mct) { mct = ct[a...
6
#include <bits/stdc++.h> using namespace std; const int64_t MOD = 1e9 + 7; const int64_t N = 1e6 + 5; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int64_t t; cin >> t; while (t--) { int64_t n; cin >> n; string a, b; cin >> a >> b; vector<int64_t> ans; for (int64_t ...
5
#include <bits/stdc++.h> using namespace std; struct point { vector<double> coords = vector<double>(5); }; struct vec { vector<double> coords = vector<double>(5); double length; }; int main() { int n; cin >> n; vector<point> points(n); for (int i = 0; i < n; i++) for (int j = 0; j < 5; j++) cin >> poi...
9
#include <bits/stdc++.h> using namespace std; int n, v[104], k, nr0; int** mat; int _pow(int a, int b) { if (b == 0) return 1; if (b == 1) return a; if (b % 2 == 0) return _pow(1LL * a * a % 1000000007, b / 2); return 1LL * _pow(1LL * a * a % 1000000007, b / 2) * a % 1000000007; } int** mult(int** A, int** B) {...
15
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int n; cin >> n; long long int x = n / 2; long long int ans = x + 1; cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int E; cin >> E; int arr[N + 1]; memset(arr, 0, sizeof(arr)); for (int i = 0; i < E; i++) { int a, b, e; cin >> a >> b >> e; arr[a] -= e; arr[b] += e; } int debt = 0; for (int i = 1; i <= N; i++) { if (ar...
5
#include <bits/stdc++.h> using namespace std; inline int Get() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * f; } const long ...
26
#include <bits/stdc++.h> using namespace std; const int N = 1e3; int rows[N + 5]; int cols[N + 5]; int tab[N + 5][N + 5]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> tab[i]...
5
#include <bits/stdc++.h> using namespace std; int M, K, A[6], E[6], T[6][7], P[7], ans = 1e9; int mat[6][6] = {0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1}; char path[7][9] = {"aaab", "aaba", "abaa", "baaa", "aabb", "abab", "abba"}, O[...
16
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int a[100]; int tmp[100]; int i; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } int j, k; int max = 0; for (i = 0; i < n; i++) { for (j = i; j < n; j++) { for (k = 0; k < n; k++) { tmp[k] = a[k]; } int c...
4
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 123; signed main() { int t; cin >> t; while (t--) { int n, m; cin >> n >> m; int mn = INF, sum = 0, cnt = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { int x; cin >> x; if (x < 0) {...
2
#include <bits/stdc++.h> using namespace std; double p[205]; int b[205]; double f[205][205][205 * 2]; int main() { int n, l, k, K; while (cin >> n >> l >> K) { for (int i = 0; i < n; i++) { cin >> p[i]; p[i] /= 100; } for (int i = 0; i < n; i++) { cin >> b[i]; } f[0][0][200 + K...
10
#include <bits/stdc++.h> using namespace std; bool is_prime[4050]; vector<int> prime; void find_prime() { for (int i = 2; i <= 4000; i++) { if (!is_prime[i]) prime.push_back(i); for (int j = 0; j < prime.size() && i * prime[j] <= 4000; j++) { is_prime[i * prime[j]] = true; if (i % prime[j] == 0) b...
10
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9; int n, m; long long a[200100], fibo[200100], sumfibo[200100]; struct Segment_Tree { int left, right, len; long long s0, s1; } tree[200100 << 2]; struct Data { long long a[2][2]; } bb[200100], one, b; Data operator*(const Data x, const Data y...
15
#include <bits/stdc++.h> inline int cmp(double x, double y = 0, double tol = 1e-9) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; } char *seq; int state, ans, n; int next[155][3]; int dp[155][52][52][52]; int main() { scanf("%d", &n); seq = new char[n + 1]; for (int i = 1; i <= n; ++i) { scanf(" %c",...
17
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int n, dif, odd, neg; int a[MAXN]; int cnt[MAXN]; long long ans; void add(int p, int v) { odd -= cnt[p] & 1; neg -= cnt[p] < 0; cnt[p] += v; odd += cnt[p] & 1; neg += cnt[p] < 0; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ...
17
#include <bits/stdc++.h> using namespace std; long long int n; int main() { cin >> n; if (n == 1) cout << "1"; else { if (n % 2 == 0) { n = n - (n / 2 - 1); cout << n; } else { n = n - 1; n = n - (n / 2 - 1); cout << n; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 5100; const long long inf = 1e18; long long s[N], a[N], b[N], c[N], d[N]; long long ans[N], tot; long long sol(int i, int j) { if (j < i) return abs(s[i] - s[j]) + c[i] + b[j]; else return abs(s[i] - s[j]) + d[i] + a[j]; } int main() { int n, ss,...
17
#include <bits/stdc++.h> using namespace std; struct Trie { Trie() : Children(2) {} void Add(unsigned int x, int depth) { unsigned int key = (x >> depth) & 1; Children[key].second++; if (depth == 0) return; if (!Children[key].first) { Children[key].first.reset(new Trie()); } Children[k...
10
#include <bits/stdc++.h> using namespace std; int IN() { int c, f, x; while (!isdigit(c = getchar()) && c != '-') ; c == '-' ? (f = 1, x = 0) : (f = 0, x = c - '0'); while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + c - '0'; return !f ? x : -x; } const int N = 6000 + 19; const int M = 6000 + 19; st...
22
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; long long int gcd(long long int a, long long int b) { if (!b) return a; else return gcd(b, a % b); } int main() { long long int n, k; cin >> n >> k; vector<long long int> l(n + 1), r(n + 1), a(n + 1); for (int i = 1; i <= n;...
18
#include <bits/stdc++.h> using namespace std; void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); } const int INF = 1e9; const long long MOD = 1e9 + 7; const int N = 1e5 + 5, MO = 350, SQ = 550; int n, q, A[N], QL[N], Q...
23
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a < b) gcd(b, a); else if (b == 0) return a; return gcd(b, a % b); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; while (n--) { long long p, q, b; ...
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char a[n + 5][n + 5]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; long long k = 0; for (int i = 2; i < n; i++) { for (int j = 2; j < n; j++) { if (a[i - 1][j - 1] == 'X' && a[i - 1][j + 1]...
0
#include <bits/stdc++.h> using namespace std; long long x, y; int main() { cin >> x >> y; long long r1 = (long long)sqrt(x * x + y * y); if (r1 * r1 != (x * x + y * y)) r1++; else { cout << "black" << endl; return 0; } if (x == 0 || y == 0) { cout << "black" << endl; return 0; } if (...
5
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int n, a[10]; long long dp[300][10], ncr[101][101]; void pre() { for (int i = 0; i < 101; i++) { ncr[i][0] = 1; ncr[i][i] = 1; for (int j = 1; j < i; j++) { ncr[i][j] = (ncr[i - 1][j] + ncr[i - 1][j - 1]) % MOD; } } } long ...
11
#include <bits/stdc++.h> using namespace std; int A, B, Px, Py, Qx, Qy; void process() { int t1, t2, t3, t4; t1 = (Px + Py) / (A * 2); if (Px + Py < 0) t1--; t2 = (Qx + Qy) / (A * 2); if (Qx + Qy < 0) t2--; t3 = (Px - Py) / (B * 2); if (Px - Py < 0) t3--; t4 = (Qx - Qy) / (B * 2); if (Qx - Qy < 0) t4-...
10
#include <bits/stdc++.h> using namespace std; int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; vector<int> ans; int main() { int n, k; cin >> n; k = 2 * n * n; ans.push_back(1); for (int i = 0; ans.size() < n; ++i) { for (int j = 0; j < ans.size(); ++j) { if (p[i] * ans[j] <= k) { ans.push_ba...
15
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-8; const int INF = 1000000001; int _gt(int n, int x, int y) { if (y == 0) return x; if (x == n) return y + n; if (y == n) return 3 * n - x; if (x == 0) return 4 * n - y; } int main() { int n, x1, y1, x2, y2; cin >> n >> x1 >> y1 >> x2 ...
5
#include <bits/stdc++.h> using namespace std; long long n, x, y, z; string str; int main() { cin >> n >> str; for (int i = 0; i < str.size(); i++) { if (str[i] == 'L') x--; if (str[i] == 'R') y++; } x *= -1; z = (x + y) + 1; cout << z; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, h, t; cin >> n >> m >> h >> t; vector<vector<int>> ad(n); vector<pair<int, int>> edge(m * 2); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; if (ad[a].siz...
12
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int x = 0, y = 0; for (int i = 0; i < n; i++) { if (s[i] == 'U') y++; else if (s[i] == 'D') y--; else if (s[i] == 'L') x--; else x++; } int val = abs(x) + abs(y); cou...
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int ar[100006], s[100006]; int n, i, k; cin >> n >> k; for (i = 0; i < n; i++) cin >> ar[i]; sort(ar, ar + n); s[0] = ar[0]; int p = 1; for (i = 1; i < n; i++) if (ar[i] != ar[i - 1]) { ...
2
#include <bits/stdc++.h> using namespace std; int main() { int n, i, k; vector<int> vec; vector<int>::iterator it; map<int, int> mp; map<int, int>::iterator it2; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &k); mp[k]++; } while (1) { bool chk = false; for (it2 = mp.begin(); ...
4
#include <bits/stdc++.h> using namespace std; char inp[100005]; int f1[100005][2]; int f2[100005][2]; int flip[100005][2]; int main() { int n; int ans = 0; scanf("%d", &n); scanf("%s", inp); memset(f1, 0, sizeof f1); memset(f2, 0, sizeof f2); memset(flip, 0, sizeof flip); if (inp[0] == '0') f1[0][0]...
8
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; int main() { int n; cin >> n; vector<vector<int>> dp(26, vector<int>(26, -INF)); for (int i = 0; i < n; i++) { string s; cin >> s; int m = s.size(); for (int j = 0; j < 26; j++) { dp[j][s[m - 1] - 'a'] = max(dp[j][s[m - 1] ...
7
#include <bits/stdc++.h> using namespace std; const double pii = acos(-1.0); const int maxn = 100000007; const double eps = 1e-6; const long long int Inf = 1LL << 62; const long long int mod = pow(2, 32); const long long int pnum = 5761460; void solve_with_case(int cas) {} const int arr_len = 400005; int arr[arr_len]; ...
5
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const double eps = 1e-5; const int MAXN = 1005; int n, m, tt; int vis[MAXN], vis2[MAXN]; int a[MAXN]; int main() { int i, j, k, ca = 1; while (scanf("%d", &n) != EOF) { memset(vis, 0, sizeof(vis)); int ans = 0; for (i = 0; i < n; ...
4
#include <bits/stdc++.h> using namespace std; template <class T> bool mini(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool maxi(T &a, T b) { return a < b ? (a = b, true) : false; } const int N = 2e5 + 5; int n, ans[N]; long long bit[N], p[N]; void up(int i, int val) { assert(i > 0); ...
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000; int val[MAXN]; int tdeath[MAXN]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", val + i); tdeath[0] = -1; vector<int> S(1, 0); int sol = 0; for (int i = 1; i < n; ++i) { tdeath[i] = 0; while (S.siz...
11