solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> int n; int sum = 0; int a[16]; int mat[4][4]; int rows[4], cols[4]; int gotx[4], goty[4], gotd[2]; int x[16], y[16]; int diag[2]; bool dfs(int state, int mask) { for (int i = 0; i < 2; i++) { if (gotd[i] == n && diag[i] != sum) { return false; } } if (state == n * n) return ...
11
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long dp[200005][3]; int main() { int n, l, r; scanf("%d%d%d", &n, &l, &r); int n0 = r / 3 - (l - 1) / 3, n1 = (r + 2) / 3 - (l + 1) / 3, n2 = (r + 1) / 3 - l / 3; dp[0][0] = 1, dp[0][1] = 0, dp[0][2] = 0; for (int i = 1; i <= n;...
7
#include <bits/stdc++.h> using namespace std; const long long mod = (long long)(1e9 + 7); long long n, t[500050], ans, pref[500050]; pair<long long, long long> v[500050]; long long tree[4 * 500050], lazy[4 * 500050]; inline void prop(long long nod, long long a, long long b) { if (lazy[nod] != 0) { tree[nod] += la...
15
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, m; cin >> n >> m; int arr[n + 10], tot = 0; vector<int> v, v2, v3; for (int i = 0; i < n; i++) { cin >> arr[i]; v3.push_back(arr[i]); v.push_back(arr[i]); } if (m < n || n...
3
#include <bits/stdc++.h> using namespace std; void solve() { long long n; scanf("%lld", &n); long long ans1 = 0; bool check = true; while (n) { if (check && n % 4 == 0 && n > 8) { ans1++; } else if (check && n % 2 == 0) { ans1 += n / 2; } else if (check) { ans1++; } if (n...
6
#include <bits/stdc++.h> int main() { int n, x, s = 0, a; scanf("%d%d", &n, &x); for (int i = 0; i < n; i++) { scanf("%d", &a); s += a; } if (n - 1 == x - s) printf("YES"); else printf("NO"); }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6; const int MOD = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; if (s.size() <= 6) { cout << "no" << endl; return 0; } bool f = false; for (int i = 0; i < s.size(); i++) { if...
2
#include <bits/stdc++.h> using namespace std; const int INF32 = 0x3f3f3f3f; const double EPS = 1.0e-8; const double PI = acos(-1.0); struct Point { double x, y; Point(double _x = 0, double _y = 0) { x = _x; y = _y; } friend Point operator+(const Point &a, const Point &b) { return Point(a.x + b.x, a....
19
#include <bits/stdc++.h> using namespace std; const int MOD(1000000007); const int INF((1 << 30) - 1); const int MAXN(1000005); char a[MAXN]; int main() { scanf("%s", a); int n = strlen(a); bool first = true; long long p = 1, ans = 0; for (int i = 0; i < n; i++) { if (a[i] == 'a') { p = p * 2 % MOD;...
6
#include <bits/stdc++.h> using namespace std; bool deb_mode = 0; class debugger { vector<string> vars; public: template <typename T> debugger &operator,(const T v) { stringstream ss; ss << v, vars.push_back(ss.str()); return *this; } void printall() { int j = 0, l = vars.size(); for (int...
9
#include <bits/stdc++.h> using namespace std; struct IO { char buf[(1 << 20)], *p1, *p2; char pbuf[(1 << 20)], *pp; IO() : p1(buf), p2(buf), pp(pbuf) {} inline char gc() { return getchar(); if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, (1 << 20), stdin); return p1 == p2 ? ' ' : *p1++; } inline b...
18
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n; cin >> k; string t; cin >> t; int sdv; for (sdv = 1; sdv < n; sdv++) { bool is_ok = true; for (int j = sdv; j < n; j++) { if (t[j] != t[j - sdv]) { is_ok = false; } } if (is_ok) { break...
5
#include <bits/stdc++.h> using namespace std; const int MAX = 1000001; const int N = 51234; const int M = N * 25; int n, m, S; int a[N], ans[N]; int prefix[1123456]; struct Query { int left, right, id; Query(int left, int right, int id) : left(left), right(right), id(id) {} bool operator<(const Query &A) const { ...
20
#include <bits/stdc++.h> using namespace std; int n, m, k; int e[30][30]; int v[30]; int cnt; long long low, high, mid; void dfs(int d, long long val) { if (d > n) { cnt++; return; } for (int i = 1; i <= n && cnt < k; i++) { if (e[d][i] && (val + e[d][i] <= mid) && !v[i]) { v[i] = 1; dfs(d...
15
#include <bits/stdc++.h> const long long int INF = 1e18 + 7; const int N = 2e5 + 5; using namespace std; int v[2][N], b[N]; long long int dp[2][N]; long long int vis(long long int a, long long int b, long long int c) { return abs(b - a) + abs(c - b); } void solve() { int n, m, k, q, i, r, c; cin >> n >> m >> k >>...
13
#include <bits/stdc++.h> using namespace std; const int MaxN = 1e5; struct ppp { int x; int y; int id; } p[3 * MaxN + 5]; bool cmp(ppp a, ppp b) { if (a.x == b.x) return a.y < b.y; return a.x < b.x; } bool cmp1(ppp a, ppp b) { if (a.y == b.y) return a.x < b.x; return a.y < b.y; } int main() { int n, ans...
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, m, k; cin >> n >> m >> k; m--; if (k > m) k = m; vector<int> a(n); for (int& v : a) cin >> v; int x = 0; for (int r = 0; r <= k; r++) { in...
8
#include <bits/stdc++.h> using namespace std; vector<int> g[100005]; int sz[100005], answer[100005]; long long ans = 0; vector<int> spis; void clears(int v, int pr) { sz[v] = 1; int pos = -1; for (int i = 0; i < (int)g[v].size(); i++) { int u = g[v][i]; if (u == pr) pos = i; else { clears(...
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); string s; cin >> s; int n = s.size(); set<string> a; a.insert(s); for (int i = 1; i < n; i++) { char c = s[n - 1]; s.insert(s.begin(), c); s.pop_back(); a.insert(s); ...
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1, M = 102; int n, a[N], c[N], q, u, v, k; double A, f[N][M]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) c[i] = a[i], f[i][a[i]] = 1, A += f[i][0]; cin >> q; while (q-- && cin >> u >> v >> k) {...
18
#include <bits/stdc++.h> using namespace std; const int N = 100010, MOD = 1000000007; int n, m, pos; int a[N]; int f[N], inv[N], p[N]; inline int add(int u, int v) { u += v; if (u >= MOD) { u -= MOD; } return u; } inline int mul(int u, int v) { return (long long)u * v % MOD; } int Power(int u, int v) { in...
17
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b; cin >> n >> a >> b; int c[n], price = 0; bool pal = true; for (int i = 0; i < n; i++) cin >> c[i]; for (int i = 0; i < n / 2; i++) { if (c[i] == 2 && c[n - i - 1] == 2) price += 2 * min(a, b); else if (c[i] + c[n - i - 1] ...
2
#include <bits/stdc++.h> using namespace std; const long long N = 600001; long long bit[N], n; void update(long long pos, long long val) { for (; pos < N; pos += pos & (-pos)) bit[pos] = min(bit[pos], val); } long long query(long long pos) { long long res = 1e18; for (; pos > 0; pos -= pos & (-pos)) res = min(res...
12
#include <bits/stdc++.h> using namespace std; int main() { long long n, s = 1, b, k, t, l = 0; cin >> k >> b >> n >> t; while (l != n) { l++; s = s * k + b; if (s >= t) { if (s == t) { cout << n - l; } else cout << n - l + 1; return 0; } } cout << 0; return ...
9
#include <bits/stdc++.h> using namespace std; long long n, m, mul[61], a[123], f[52][112][3], ans; void DP_bin(long long res, long long h1, long long h2) { for (long long had = 0; had <= h1 + h2; had++) { if ((res + had) % 2 == 1) continue; memset(f, 0, sizeof(f)); f[0][0][0] = 1; for (long long two =...
24
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; struct node { int u, v; } Query[N]; int Par[N][18], Mx[N][18], h[N], Time[N], mem[N], Val[N], lab[N], n, m, cnt, sl; vector<int> a[N]; int get(int u) { return u == lab[u] ? u : lab[u] = get(lab[u]); } void dfs(int u) { for (int v : a[u]) { h...
13
#include <bits/stdc++.h> using namespace std; vector<int> ans(1); int i, k, c; char s[100000]; int m[10000]; void r(int x) { int o, i; for (i = 0; i < ans.size(); i++) { ans[i] *= x; } o = 0; for (i = 0; i < ans.size(); i++) { ans[i] += o; if (ans[i] > 9) { o = ans[i] / 10; ans[i] = an...
3
#include <bits/stdc++.h> using namespace std; int n; vector<int> a; void Solve() { cin >> n; a.clear(); int i; for (i = 0; i < n; i++) { int tmp; cin >> tmp; a.push_back(tmp); } sort(a.begin(), a.end()); if (a[0] != 1) cout << 1 << endl; else { int j; int flag = 0; for (j = 0...
4
#include <bits/stdc++.h> using namespace std; struct node { int val; node *l, *r; node() { val = 0; l = NULL; r = NULL; } }; node *segr, *segc; void update(node *cur, int l, int r, int x, int y, int val) { if (l > r || l > y || r < x) return; if (x <= l && r <= y) { cur->val = max(cur->val, ...
14
#include <bits/stdc++.h> using namespace std; int main(void) { int n, i; char a[11]; scanf("%d", &n); int f = 0; scanf("%s", a); for (i = 0; i < n; i++) if (a[i] == '0') f = 1; if (f == 1) { for (i = 0; i < n; i++) if (a[i] == '1' || a[i] == '2' || a[i] == '3') { printf("YES\n"); ...
6
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int MOD = 1e9; const long long LINF = 2e16; const int INF = 2e9; const int magic = 348; const double eps = 1e-10; const double pi = acos(-1); inline int getint() { char ch; int res; bool f; while (!isdigit(ch = getchar()) && ch != '-...
15
#include <bits/stdc++.h> using namespace std; int a[100010]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int t; cin >> t; a[t] = 1; } int ans = 0; for (int i = 2010; i <= 2100; i++) { if (a[i]) ans += i; } cout << ans / n; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, x; cin >> n >> x; int temp = x; if (n == 1 || n == 2) cout << "1" << endl; else { n = n - 2; int i = 1; while (1) { i++; if (x >= n) break; x += temp...
0
#include <bits/stdc++.h> using namespace std; string d; int c[200]; int n; int ans; int main() { cin >> d; cin >> n; if (n > d.size()) { cout << "impossible" << endl; return 0; } for (int i = 0; i < d.size(); i++) c[d[i]] = 1; for (int i = 'a'; i <= 'z'; i++) ans += c[i]; cout << max(0, n - ans) <...
2
#include <bits/stdc++.h> using namespace std; int main() { long long start1, end1, start2, end2, k; long long total; cin >> start1 >> end1 >> start2 >> end2 >> k; if (start1 > start2) { if (end1 < end2) { if (start1 > end1) cout << 0; else { long long total = end1 - start1 + 1; ...
3
#include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.fi...
7
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 3e5 + 5; int dp[maxn]; int flag[maxn]; vector<int> G[maxn]; void dfs(int rt) { int minn = 0, maxx = INF; for (int i = 0; i < G[rt].size(); i++) { int to = G[rt][i]; dfs(to); maxx = min(maxx, dp[to]); minn += d...
11
#include <bits/stdc++.h> using namespace std; long double pi = 2.0 * acos(0.0); mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count()); int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int a, b, c, i, j, k, first, r, x, y, z; long lon...
10
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e+15; const long long int mod = 1e9 + 7; long long int n, k, a, b, q, di, ai, pi, x; long long int seg1[800005], seg2[800005], day1[200005], day2[200005]; void build1(int st, int en, int i) { if (st == en) { seg1[i] = 0; return; } in...
9
#include <bits/stdc++.h> using namespace std; char s[1123456]; int a[112345] = {}; char v[10] = "aiueo"; map<int, vector<int>> cnt[5]; vector<int> x, y; map<int, set<int>> qs; int main() { int n; scanf("%d", &n); for (int i = (0); i < ((n)); i++) { char *t = s + a[i]; scanf("%s", t); int m = strlen(t)...
9
#include <bits/stdc++.h> using namespace std; const long long N = 2005; long long n, k, a[N][N]; long long R[N], C[N], sum[N][N]; long long ans[N][N], Col[N], pre[N], cc[N][N]; long long get(long long x, long long y, long long u, long long v) { return sum[u][v] - sum[x - 1][v] - sum[u][y - 1] + sum[x - 1][y - 1]; } v...
11
#include <bits/stdc++.h> using namespace std; long long bmm(long long x, long long y) { if (y > x) { x = y + x; y = x - y; x = x - y; } if (x % y == 0) { return y; } else { x %= y; return bmm(x, y); } } int main() { long long m, h[2], h2[2], x[2], y[2]; cin >> m >> h[0] >> h2[0] >>...
14
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; long long m, n, opp, xi, li, ci, it = 1, pos; long long cumul[MAXN], nums[MAXN], opps[MAXN], current[MAXN], l[MAXN], c[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> m; for (int i = 1; i <= m; i++) { cin >> opp; ...
8
#include <bits/stdc++.h> using namespace std; const int MAX_N = 200005; vector<int> val; int cmp[MAX_N]; vector<int> G[MAX_N]; int cnt[MAX_N][2]; int al[2]; int ans; long long add(long long x, long long y) { return (x + y) % 1000000007; } long long sub(long long x, long long y) { return (x + 1000000007 - y) % 1000000...
15
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 5; const int MAXE = 2 * MAXN; const int NIL = -1; const int INF = 0x3f3f3f3f; char s[MAXN]; int len; int main() { int n, m; scanf("%d %d", &n, &m); int t = (n - m) / 2; for (int i = 0; i < t; ++i) s[i] = '0'; s[t] = '1'; len = t + 1; ...
14
#include <bits/stdc++.h> using namespace std; long long arr[100000]; int main() { vector<long long> vs; int n; cin >> n; arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); cout << arr[0] << " "; for (int i = 1; i < n; i += 2) { if ((arr[i] > arr[i - 1]) && (arr[i] > arr...
2
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> struct less_second { bool operator()(pair<T1, T2> const& a, pair<T1, T2> const& b) const { return a.second < b.second; } }; string int_to_string(int a) { stringstream ss; ss << a; return ss.str(); } string double_to_string(d...
1
#include <bits/stdc++.h> using namespace std; long n; vector<long> v, nxt; bool bad() { for (long i = 1; i < v.size(); i++) if (abs(v[i] - v[i - 1]) >= 2) return 1; return 0; } int main() { cin >> n; v.resize(n); for (long i = 0; i < n; i++) cin >> v[i]; if (bad()) { cout << "NO" << '\n'; return...
8
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; struct edge { int to, nxt; } e[N]; vector<int> G[N]; int deg[N], n, head[N], cnt, m, h[N], mex[N], X[N]; bool vis[N]; queue<int> q; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", h + i); while (m--) { int u, v...
24
#include <bits/stdc++.h> using namespace std; int n; int a[2000009]; int main() { cin >> n; for (int i = 1; i <= n; i++) a[i] = i; for (int i = 2; i <= n; i++) { int x = a[i - 1]; for (int j = i - 1 + i; j < i + n - 1; j += i) { int y = x; x = a[j]; a[j] = y; } a[i + n - 1] = x; ...
14
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 10; struct node { long long a; long long t; } w[N]; bool cmp(node a, node b) { return a.a < b.a; } long long c[N][2], cnt[N], n; long long lowbit(long long x) { return x & (-x); } void add(long long x, long long val) { while (x <= n) c[x][0]+...
11
#include <bits/stdc++.h> using namespace std; template <typename T> class heap : public priority_queue<T, vector<T>, greater<T> > {}; const int n = 1 << 20; int main(void) { long long s, x; scanf("%lld%lld", &s, &x); long long mask[2]; mask[0] = x & (n - 1); mask[1] = x >> 20; vector<long long> v[2]; v[0]...
9
#include <bits/stdc++.h> using namespace std; bool possible(long long x, long long d) { long long v = (x * (x + 1ll)) / 2ll; if (v < d) return 0; return v % 2 == d % 2; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long a, b; ...
7
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 10; long long mod = 1000000007; const long long inf = 1e9 + 1; long long gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a - b, b); return gcd(a, b - a); } long long ...
5
#include <bits/stdc++.h> int mystrcmp(char *s, char *q) { int i = 0; while (s[i] == q[i] && s[i] != '\0') i++; if (s[i] == '\0' && q[i] == '\0') return 1; else return 0; } void mystrcpy(char *s, char *q) { int i = 0; while (q[i] != '\0') { s[i] = q[i]; i++; } s[i] = '\0'; } int p[101], n...
3
#include <bits/stdc++.h> using namespace std; const int maxn = 200000 + 10; class Query { public: int t, x, y; void read() { scanf("%d%d%d", &t, &x, &y); } }; vector<Query> Que; vector<int> px, pp, np; struct trnode { long long sum, num, ans; int x, y, l, r; } tr[maxn * 2]; int tot, n, m; void update(int t) { ...
17
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int N = 3005; int fst[N], to[N << 1], nxt[N << 1], mm; void ade(int u, int v) { to[++mm] = v, nxt[mm] = fst[u], fst[u] = mm; } int siz[N]; pair<int, long long> f[N][N], tmp[N]; int n, m; int w[N]; void dfs(int u, int fa) { siz[u] = 1; f...
17
#include <bits/stdc++.h> using namespace std; long dp[10000000][2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; long long n; cin >> n; long long a[n], i; for (i = 0; i < n; i++) cin >> a[i]; long long ans = 0, pre = 0; dp[0][0] = 0; dp[0][1] = 1; for (i = 0; i < n; i++) { pre = pr...
8
#include <bits/stdc++.h> #pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") using namespace std; const long long N = 1e5 + 5; const long long mod = 1e9 + 7; const long long INF = 1e18; const int INFi =...
11
#include <bits/stdc++.h> using namespace std; int n, m, t, test, ans, p, T; bool bo; int d[5][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {1, 0}}; map<pair<int, int>, int> mp, Ans; inline void PrintMp(void) { int sum; sum = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) sum += Ans[make_pair(i, j)]; ...
23
#include <bits/stdc++.h> using namespace std; bool vst[2][5010]; vector<int> G[5010], rG[5010]; void dfs(int p) { vst[0][p] = 1; for (__typeof((G[p]).begin()) it = (G[p]).begin(); it != (G[p]).end(); it++) if (!vst[0][*it]) dfs(*it); } void rdfs(int p) { vst[1][p] = 1; for (__typeof((rG[p]).begin()) it = (r...
18
#include <bits/stdc++.h> using namespace std; vector<pair<int, char> > v(26); vector<pair<int, char> > g(26); bool comparePairs(const pair<int, char>& lhs, const pair<int, char>& rhs) { return lhs.first < rhs.first; } int main() { char str[100005]; int a[26] = {}, k, i, count = 0, j; cin >> str; cin >> k; i...
4
#include <bits/stdc++.h> using namespace std; int n, num[3010], s; namespace MCMF { const int N = 15000, M = 2000000; int head[N], cnt, dis[N], fr[N], id[N], S, T, cost; struct node { int to, next, val, cost; } edge[M]; void ae(int u, int v, int w, int c) { edge[cnt].cost = c, edge[cnt].next = head[u], edge[cnt].to...
18
#include <bits/stdc++.h> using namespace std; bool jbk[256], b = true; int main() { int n; string a; cin >> n; cin >> a; for (int i = 0; i < a.length(); i++) jbk[a[i]] = true; for (int i = 'A'; i <= 'Z'; i++) if (!jbk[i] && !jbk[i + 'a' - 'A']) b = false; if (b) cout << "YES" << endl; else c...
0
#include <bits/stdc++.h> using namespace std; const int int_inf = 0x3f3f3f3f; const double double_inf = 1e10 * 1e10; int debug = 0; const int maxn = 2e5 + 10; int a[maxn], n; long long sum; long long ans[maxn]; set<pair<pair<int, int>, int> > S; vector<int> H[maxn], v[maxn]; set<pair<pair<int, int>, int> >::iterator it...
20
#include <bits/stdc++.h> using namespace std; const int N = 200010; int n, m; long long c[N]; long long sum; void insert(int x, int val) { for (; x; x ^= x & (-x)) c[x] += val; } long long get(int x) { long long ret = 0; for (; x < N; x += x & (-x)) ret += c[x]; return ret; } int main() { memset(c, 0, sizeof(...
8
#include <bits/stdc++.h> using namespace std; const long long int MAXN = 2e5 + 10; bool isPrime[MAXN + 10]; vector<long long int> primes; void seive() { for (long long int i = 0; i <= MAXN; ++i) isPrime[i] = true; for (long long int i = 2; i * i <= MAXN; ++i) { for (long long int j = i * i; j <= MAXN; j += i) {...
4
#include <bits/stdc++.h> using namespace std; long long func(long long arr[], int n) { long long maxx = INT_MIN; long long curr = 0; for (int i = 0; i < n - 1; i++) { curr = max(curr + arr[i], arr[i]); maxx = max(curr, maxx); } return maxx; } long long func1(long long arr[], int n) { long long maxx ...
5
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; if (a < b) return gcd(b, a); return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, p, q, b; cin >> n; while (n--) { cin >> p >> q >> b; long lon...
9
#include <bits/stdc++.h> using namespace std; const int mod = 1e9; int main() { int d1, d2, d3, d4, d5; cout << "? 1 1" << endl; cin >> d1; cout << "? 1 " << mod << endl; cin >> d2; cout << "? " << mod << " " << mod << endl; cin >> d3; cout << "? " << mod << " 1" << endl; cin >> d4; int m = (d1 + 1 ...
13
#include <bits/stdc++.h> int n, m, i, j, sum; int g[1005][1005]; int main() { scanf("%d%d", &n, &m); for (i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); g[b][a] = 1; g[1][a] = g[n][a] = g[b][1] = g[b][n] = 1; } g[1][1] = g[1][n] = g[n][1] = g[n][n] = 0; for (i = 2; i < n; i++) { if...
10
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, ans, ara[1001]; cin >> n >> m; for (i = 0; i < m; i++) { cin >> ara[i]; } sort(ara, ara + m); ans = ara[n - 1] - ara[0]; for (i = 1; i < m - n + 1; i++) { ans = min(ans, ara[i + n - 1] - ara[i]); } cout << ans << endl; r...
1
#include <bits/stdc++.h> using namespace std; const int H = 1, V = 2; int n; map<string, vector<string>> inside; map<string, int> boxtype; map<string, int> boxbord, boxspace; map<string, pair<int, int>> widprop; map<string, pair<long long, long long>> res; pair<long long, long long> calc(string name) { if (res.find(n...
15
#include <bits/stdc++.h> using namespace std; struct TPeople { string name; string sourname; }; int i, j, n; TPeople x[100000]; TPeople y[100000]; string z[100000]; bool res; int main() { cin >> n; for (i = 0; i < n; ++i) cin >> x[i].name >> x[i].sourname; for (i = 0; i < n; ++i) { cin >> j; --j; ...
6
#include <bits/stdc++.h> using namespace std; const int N = 12; int calc(long long x) { int ans = 0; for (; x; x /= 10) ans += (x % 10 == 4 || x % 10 == 7); return ans; } void Mn(long long &x, long long y) { (x == -1 || x > y) ? x = y : 0; } long long n, k, P[N]; long long f[N][N][2], a[N]; int ans[N]; long long ...
19
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int n; int a[(100 + 10)], b[(100 + 10)]; int dp[3000 + 1][100 + 1][100 + 2]; int f(int len, int w, int pre) { if (len < 0) return 0; if (len == 0) return 1; int& ans = dp[len][w][pre]; if (~ans) return ans; ans = 0; for (int i(0), _n(...
10
#include <bits/stdc++.h> using namespace std; string a, b; int cnt1[26], cnt2[26]; int main() { cin >> a >> b; int l = 0; for (int i = 0; a[i]; i++) { if (b[l] && b[l] == a[i]) { l++; } } if (!b[l]) { cout << "automaton" << endl; return 0; } for (int i = 0; a[i]; i++) { cnt1[a[i]...
6
#include <bits/stdc++.h> using namespace std; #define int long long void read (int &x) { char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); } const int N = 2e5 + 5, M = 2000, NN = N * 5; int n, s, mx, a[N], res[M][M], sr, sb, sy, id[N]; pai...
19
#include <bits/stdc++.h> using namespace std; const int maxn = 10e5 + 5; long long a, b, c, d, e, f, g, n, m, i, j, sdg[maxn], lili, t; long long x[2001][2001], z[maxn], v[200001], h[100001]; string p, s; queue<int> q; bool mark[maxn]; vector<long long> o[maxn], sgd; map<long long, long long> mp; char l, y; int main() ...
6
#include <bits/stdc++.h> using namespace std; double a, d; int N; double cx, cy; int main() { scanf("%lf%lf", &a, &d); scanf("%d", &N); for (int i = 0; i < N; ++i) { double dist = (i + 1) * d; long long p = floor(dist / a); double r = dist - a * p; switch (p % 4) { case 0: cx = r; ...
7
#include <bits/stdc++.h> using namespace std; int n, a, b; void genCycle(int x, int y) { for (int i = 0; i < x; ++i) { for (int j = i * a + 1; j < (i + 1) * a; j++) { printf("%d ", j + 1); } printf("%d ", i * a + 1); } for (int i = 0; i < y; ++i) { for (int j = i * b + 1; j < (i + 1) * b; j+...
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector<int> a(n); long long int res = 0; vector<long long int> diffs; for (int i = 0; i < n; i++) { cin >> a[i]; } res += a[n - 1] - a[0]; for (int i = 0; i < n - 1; i++) { diffs....
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> nums(n); for (int i = 0; i < n; i++) cin >> nums[i]; map<int, int> mp; for (int i : nums) mp[i]++; int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; int f0...
12
#include <bits/stdc++.h> using namespace std; struct FlowEdge { int v, u; long long cap, flow = 0; FlowEdge(int v, int u, long long cap) : v(v), u(u), cap(cap) {} }; struct Dinic { const long long flow_inf = 1e18; vector<FlowEdge> edges; vector<vector<int>> adj; int n, m = 0; int s, t; vector<int> lev...
13
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; float cntr = n; int min = n + 1; int max = n + 2; int nums[n + 7]; nums[n + 1] = 100; nums[n + 2] = -10; for (int i = 0; i < n; i++) { cin >> nums[i]; if (nums[i] > nums[max]) { max = i; } } for (int i = ...
0
#include <bits/stdc++.h> using namespace std; int a[30]; bool isConnected[1 << 23]; int maskNeighbours[1 << 23]; int n, m; int main() { cin >> n >> m; if ((n * (n - 1)) / 2 == m) { cout << 0; return 0; } memset(a, 0, sizeof(a)); for (int i = 0; i < n; i++) a[i] |= (1 << i); for (int i = 1; i <= m; i...
16
#include <bits/stdc++.h> using namespace std; struct BIT { int n, N_MAX; vector<long long> v; BIT(int n) { this->n = n + 100; N_MAX = n; v.assign(n + 110, 0); } void upd(int p, int x) { while (p <= n) v[p] += x, p += p & -p; } long long que(int p) { long long ans = 0; while (p) ans...
11
#include <bits/stdc++.h> using namespace std; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmisleading-indentation" inline char gtx() { const int N = 4096; static char buffer[N]; static char *p = buffer, *end = buffer; if (p == end) { if ((end = buffer + fread(buffer, 1, N, stdin)) == buffer...
21
#include <bits/stdc++.h> using namespace std; long long n, h; long long ans; void kh(long long l, long long r, bool turn) { if (l == r) return; long long mid = (l + r) / 2; if (!turn) { if (n <= mid) { ans++; kh(l, mid, !turn); } else { long long x = (mid - l + 1); while (x >= 1) {...
9
#include <bits/stdc++.h> using namespace std; const int inf = INT_MAX; const long long int INF = LLONG_MAX; const long long int mod = 1e9 + 7; long long int powfunc(long long int a, long long int b) { if (b == 0) return 1; long long int val1 = powfunc(a, b / 2); val1 = (val1 * val1) % mod; if (b % 2 != 0) val1 ...
14
#include <bits/stdc++.h> #define MAXN 100010 using namespace std; int n, a[MAXN]; bool sss(long long a, long long b, long long c) { if (b > a) return sss(b, a, c); if (b * (c + 1) >= a) return true; else return false; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { long long a, b, c...
0
#include <bits/stdc++.h> using namespace std; long long int a[200002], b[200002][3]; int findInSet(set<int> setname, int element) { if (setname.find(element) != setname.end()) return 1; return 0; } int findInVector(vector<int> vectorname, int element) { if (find(vectorname.begin(), vectorname.end(), element) != v...
10
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:100000000") const int T = 1 << 17; long long x[100500], y[100500]; int ind[100500]; int dmax[2 * T]; vector<long long> v; map<long long, int> M; bool cmp(int i, int j) { if (x[i] != x[j]) return x[i] > x[j]; return y[i] < y[j]; } void upd...
19
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; set<string> ans; for (int i = 0; i <= s.size(); i++) { for (char ch = 'a'; ch <= 'z'; ch++) { string bla; if (i == s.size()) bla = s + ch; else if (i == 0) { bla = ch + s; } else bl...
1
#include <bits/stdc++.h> using namespace std; int oc[10]; pair<string, int> keys[7]; const int maxn = 1e6 + 5; char st_r[maxn]; char ans_old[maxn]; int main() { keys[0] = {"1689", 2}; keys[1] = {"1698", 4}; keys[2] = {"1869", 0}; keys[3] = {"1896", 6}; keys[4] = {"1968", 1}; keys[5] = {"1986", 5}; keys[6]...
8
#include <bits/stdc++.h> using namespace std; int res[1000006]; int main() { int n, q; scanf("%d %d", &n, &q); int v[2]; v[0] = 0; v[1] = 0; int p = 0, im = 1; for (int i = 0; i < q; i++) { int in; scanf("%d", &in); if (in == 2) { v[0]--; v[1]++; swap(p, im); swap(v[0],...
10
#include <bits/stdc++.h> using namespace std; long long a[10]; int main() { int n, k; while (scanf("%d %d", &n, &k) != EOF) { long long ans = 0; a[4] = 1ll * n * (n - 1) * (n - 2) * (n - 3) / 24 * 9; a[3] = 1ll * n * (n - 1) * (n - 2) / 6 * 2; a[2] = 1ll * n * (n - 1) / 2; a[1] = 0; a[0] = 1...
8
#include <bits/stdc++.h> #pragma optimize("O3") using namespace std; const long long MOD = 1e9 + 7; const long long INF = 2 * 1e9 + 7; const int base = 2e5 + 1; const long long MAX = 1e6; const double EPS = 1e-9; const double PI = acos(-1.); const int MAXN = 3 * 1e5 + 147; mt19937 rng(chrono::steady_clock::now().time_s...
14
#include <bits/stdc++.h> #pragma comment(linker, "/stack:336777216") #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; ll const mod = 1000000007; ll n, k, s; int main...
8
#include <bits/stdc++.h> using namespace std; const int N = 2005, p = 1e9 + 7; int n, m; int a[N][N], shr[N][N], slr[N][N], f[N][N][2], shf[N][N], slf[N][N]; char s[N]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%s", s + 1); for (int j = 1; j <= m; j++) { if (s[j] == 'R'...
14