solution
stringlengths
53
181k
difficulty
int64
0
13
#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; } const int N = 100111...
4
#include <bits/stdc++.h> using namespace std; int n; int s[5][5]; bool wu[5]; int c = 0; vector<int> a; void f(int t) { if (t == n) { a.push_back(c); return; } f(t + 1); for (int i = 0; i < n; ++i) if (!wu[i] && s[t][i]) { c += s[t][i]; wu[i] = true; f(t + 1); wu[i] = false; ...
5
#include <bits/stdc++.h> using namespace std; const long long INF = 1e5 + 5; long long n, a[INF], ans, ansx, b[INF], suma[INF], l[INF], r[INF]; long long read() { long long s = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch...
5
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, m; cin >> n; m = 24; if (n < 4) { cout << "NO"; return 0; } cout << "YES\n"; if (n % 4 == 0) { cout << "1 * 2 = 2\n3 * 4 = 12\n2 * 12 = 24\n"; } else if (n % 4 == 1) { cout << "4 * 5 = 20\n20 + 3 = 23\n23 + 2 = 25\n2...
3
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d, cnt = 0; cin >> a >> b >> c >> d; vector<long long> v; v.push_back(a); v.push_back(b); v.push_back(c); sort(v.rbegin(), v.rend()); for (int i = 0; i < v.size() - 1; i++) { if ((v[i] - v[i + 1]) < d) cnt += (d - (v[i] - ...
0
#include <bits/stdc++.h> using namespace std; long long sum(long long a, long long d, long long len, long long v, long long mod) { if (a > v) return 0; len = min(len, 1 + ((v - a) / d)); if (len & 1) { long long ret = (len - 1) / 2; ret %= mod; ret *= (d % mod); ret %= mod; ret +...
8
#include <bits/stdc++.h> using namespace std; int32_t main() { long long n, cnt = 0; cin >> n; vector<long long> a(n), b(n); for (long long i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } sort(a.begin(), a.end()); for (long long i = 0; i < n; i++) { cnt += (a[i] != b[i]); } cout << (cnt <...
2
#include <bits/stdc++.h> int main() { int time[10]; int k; while (scanf("%d", &k) != EOF) { for (int i = 0; i < 10; i++) time[i] = 0; char str[10]; for (int j = 0; j < 4; j++) { scanf("%s", &str); for (int i = 0; i < 4; i++) if (str[i] != '.') { int s = str[i] - '0'; ...
0
#include <bits/stdc++.h> using namespace std; int main() { int n, ans; ans = 0; scanf("%d", &n); int i, j, p; for (i = 1; i <= n; i++) { scanf("%d", &p); for (j = 1; j * (j + 1) / 2 <= p; j++) ; ans ^= j - 1; } if (ans) printf("NO\n"); else printf("YES\n"); return 0; }
6
#include<iostream> #include<climits> #include<vector> #include<numeric> #include<queue> #include<bits/stdc++.h> #include<algorithm> //inbuilt gcd __gcd(a,b) using namespace std; #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define endl "\n" #define sz(s) s.size() #define pi...
0
#include <bits/stdc++.h> using namespace std; struct node { int data; node* left; node* right; }; map<int, node*> m; void add(node* n, string s, int pos, int val) { if (pos == s.size()) { n->data += val; n->left = NULL; n->right = NULL; return; } if (s[pos] == '0') { if (n->left == NULL)...
6
#include <bits/stdc++.h> using namespace std; int n; long long t; long long a[200010]; int b[200010]; vector<long long> un; void Insert(int x) { for (int i = x + 1; i > 0; i -= i & -i) b[i]++; } int Get(int x) { int res = 0; for (int i = x + 1; i < 200005; i += i & -i) res += b[i]; return res; } int main() { ...
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n, mx = 0; cin >> n; vector<long long> v(n); for (long long i = 0; i < n; i++) { cin >> v[i]; mx = max(mx, v[i]); } vec...
3
#include <bits/stdc++.h> using namespace std; int a[300005], n, k, m; long long sum; int main() { cin >> n; for (register int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); for (register int i = 1; i <= n; i++) sum += a[i]; cin >> m; for (register int i = 1; i <= m; i++) { cin >> k; cout <...
0
#include <bits/stdc++.h> using namespace std; int sumstr[26]; pair<string, string> to_get(string s) { reverse(s.begin(), s.end()); string zc; for (int i = 0; i < s.size(); i++) { if (!sumstr[s[i] - 'a']) { zc += s[i]; } sumstr[s[i] - 'a']++; } int chushilenth = 0; int m = zc.size(); for ...
5
#include <bits/stdc++.h> #pragma GCC optimize(3) inline void read(int &x) { scanf("%d", &x); } inline void read(long long &x) { scanf("%I64d", &x); } inline void read(double &x) { scanf("%lf", &x); } inline void read(long double &x) { double tmp; read(tmp); x = tmp; } template <class T> inline void read(T *a, int...
10
#include <iostream> #include <string> using namespace std; pair<int, int> update_pos(const pair<int, int>& cur_pos, char dir) { pair<int, int> res = cur_pos; if (dir == 'L') res.first--; else if (dir == 'R') res.first++; else if (dir == 'D') res.second--; else res.second++; return res; } int main() { int t;...
4
#include <bits/stdc++.h> using namespace std; const long long int N = 1e7 + 5; int main() { int t, n; int a[100005]; cin >> t; for (int i = 0; i < t; i++) { cin >> n; vector<int> b; for (int j = 0; j < n; j++) cin >> a[j]; int l = 1; b.push_back(a[0]); while (l <= n - 2) { if ((a[l...
2
#include <bits/stdc++.h> using namespace std; long long const max1 = 2e5 + 5; long long const mod = 1e9 + 7; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); long long t = 1; while (t--) { long long n, s; cin >> n >> s; if (s - n > n - 1) { cout << "YES\n"; for (long long i = 0; i <...
3
#include <bits/stdc++.h> using namespace std; long long getPower(int a, int b) { long long ans = 1; for (int i = 1; i <= b; i++) ans *= a; return ans; } int main() { long long n; cin >> n; long long check = 10; int val = 1; while (n >= check) { check *= 10; val++; } long long temp = getPower...
2
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const int maxn = 1005; long long a[300000]; priority_queue<long long> ql; map<long long, long long> ma; map<long long, long long> num; vector<long long> ans; long long gcd(long long a, long long b) { if (b == 0) return a; else return ...
4
#include <bits/stdc++.h> using namespace std; long long n, m, k, mod = 1e9 + 7; map<long long, long long> mp; string s; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); srand(time(0)); cin >> n; vector<long long> a(n + 1); vector<long long> us(512); vector<long long> use(512); for (...
5
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1); const double eps = 1e-8; const int maxn = 2002; const int maxm = 3e4 + 5; const long long mod = 1e9 + 7; const long long inf = 2e18 + 5; const int _inf = -1e9 + 7; inline int scan() { int m = 0; char c = getchar(); while (c < '0' || c > '9'...
7
#include <bits/stdc++.h> int main(void) { int n, w; int el[(4 * 100 * 1000)], be[(4 * 100 * 1000)]; int k[(4 * 100 * 1000)]; int i, j, z; scanf("%d %d", &n, &w); if (w == 1) { printf("%d\n", n); return 0; } n--; for (i = 0; i <= n; i++) { if (i == 0) { scanf("%d", &z); } else { ...
5
#include <bits/stdc++.h> using namespace std; int n; int main() { int cnt; string str; char c; cin >> n; if (n == 0) { cout << "a" << endl; return 0; } str = ""; c = 'a'; while (n) { cnt = 0; while (cnt <= n) { n -= cnt; str += c; cnt++; } c++; } cout << s...
4
#include <bits/stdc++.h> using namespace std; stringstream ss; int main() { int n, m; cin >> n >> m; vector<pair<string, string> > best; string a, b, c; for (int i = 0; i < m; i++) { cin >> a >> b; if (a.length() > b.length()) c = b; else c = a; best.push_back(make_pair(a, c)); }...
1
#include <bits/stdc++.h> const int N = (1 << 20) + 7; void Xor(int n, long long *a, long long op) { long long x, y; for (int w = 2, l = 1; w <= n; w <<= 1, l <<= 1) for (int j = 0; j < n; j += w) for (int k = 0; k < l; k++) x = (a[j | k] + a[j | k | l]) / op, y = (a[j | k] - a[j | k | l]) / op, ...
9
#include <bits/stdc++.h> using namespace std; long long int p[100005], r[100005], k[100005]; vector<long long int> a; vector<long long int> adj[100005]; void dfs(long long int node, long long int pa) { k[node] = r[node]; for (auto i : adj[node]) { if (i != pa) { k[node] &= r[i]; dfs(i, node); } ...
3
#include <bits/stdc++.h> using namespace std; int main() { int n, a[100], s = 0, i, j; int vis[100]; for (i = 0; i < 100; i++) vis[i] = 0; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; s += a[i]; } s /= (n / 2); for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (a[j] + a[i]...
0
#include <bits/stdc++.h> using namespace std; int main() { string n; cin >> n; int m = n.length(); for (int i = n.length() - 1; i > 0; i--) { if (n[i] == '1') { m++; break; } } cout << m / 2; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1555; const int INF = 0x3f3f3f3f; char Map[N][N]; int n, m; int sx, sy; int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; struct node { int x, y; } vis[N][N]; bool bfs(int x, int y) { node now, next; now.x = x; now.y = y; queue<node> q; q.push(now); wh...
6
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2005 * 60; const long long INF = 0x3f3f3f3f; const long long MAXC = 7; const long long MOD = 1e9 + 7; long long inline read() { long long x = 0, f = 0; char ch = getchar(); while (ch < '0' || ch > '9') f ^= (ch == '-'), ch = getchar(); while (...
7
#include <bits/stdc++.h> using namespace std; int n, a[222222], tp, l, r, k, q, crt; struct treenode { int sum, len, mn1, mn2, lazy; bool can(int a, int b) { if (mn1 <= 0 || mn2 < 0) return 0; if ((b - a) & 1) return (sum == 0); return (sum == 1); } } tree1[888888], tree2[888888], t1, t2; void merge(t...
9
#include <bits/stdc++.h> using namespace std; const int N = 1002; int t, n; vector<int> g[N]; pair<int, int> ask(vector<int> v) { printf("? %d ", v.size()); for (auto x : v) printf("%d ", x); printf("\n"); fflush(stdout); int x, d; scanf("%d%d", &x, &d); return make_pair(x, d); } int dep[N], par[N], vis[N...
9
#include <bits/stdc++.h> using namespace std; const int RLEN = 1 << 20 | 1; inline char gc() { static char ibuf[RLEN], *ib, *ob; (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin)); return (ib == ob) ? EOF : *ib++; } inline int read() { char ch = gc(); int res = 0; bool f = 1; while (!isdigit(c...
13
#include <bits/stdc++.h> using namespace std; int upperbound(int sz, vector<int> &v, int val) { int low = 0; int high = sz - 1; int mid; while (high >= low) { mid = (high + low) / 2; if (v[mid] > val) high = mid - 1; else { if (mid + 1 == sz || v[mid + 1] > val) return mid; low = m...
4
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { long long int n; cin >> n; vector<pair<long long int,long long int>>v; for(int i=0;i<n;i++){ long long int x; long long int y; cin >> x >> y; v.push_back(make_pai...
4
#include <bits/stdc++.h> using namespace std; int x[5010], y[5010], w[5010], n, m; double a[110][110], f[110]; double t; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &x[i], &y[i], &w[i]); if ((x[i] != 1) && (x[i] != n)) { a[x[i]][y[i]]--; a[x[i]][x[i]]++; ...
9
#include <bits/stdc++.h> using namespace std; template <class T> void _R(T &x) { cin >> x; } void _R(signed &x) { scanf("%d", &x); } void _R(int64_t &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <cla...
7
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int ans[30005]; int cnt = 0; int sum1 = 0, sum2 = 0; for (int i = n; i > 0; i--) { if (sum1 <= sum2) { sum1 += i; ans[cnt++] = i; } else sum2 += i; } printf("%d\n", abs(sum1 - sum2)); printf("%...
2
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T > 0) { string s; cin >> s; int sum = 0, c = 0; int n = s.length(); int x = 0, y = 0, x1, y1; map<pair<pair<int, int>, pair<int, int>>, int> m; m[{{x, y}, {x, y}}] = 1; for (int i = 0; i < n; i++) {...
3
#include <bits/stdc++.h> using namespace std; template <class C> void mini(C& _a4, C _b4) { _a4 = min(_a4, _b4); } template <class C> void maxi(C& _a4, C _b4) { _a4 = max(_a4, _b4); } template <class TH> void _dbg(const char* sdbg, TH h) { cerr << sdbg << '=' << h << endl; } template <class TH, class... TA> void ...
10
#include <bits/stdc++.h> using namespace std; int n, m; long double a[1000010], b[1000010]; long double C(int n, int m) { if (n < m) return -1e100; return b[n] - b[m] - b[n - m]; } int main() { cin >> n >> m; for (int i = 1; i <= n * m; ++i) { a[i] = log(i); b[i] = b[i - 1] + a[i]; } long double ans...
6
#include <bits/stdc++.h> using namespace std; const int MAX = 100010; int prm[MAX], a[MAX]; static int c2[MAX], p[MAX]; int main(void) { int n, m, j, k, l, flag = 0; cin >> n >> m; char ch; for (int i = 2; i <= n; i++) if (!prm[i]) for (j = i, prm[i] = i; j <= n; j = j + i) prm[j] = i; for (int i = ...
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long test; cin >> test; while (test--) { long long h, c, t; cin >> h >> c >> t; long double mini = (h + c) / 2.0; if (t <= mini) { cout << 2 << '\n'; } else { long l...
4
#include <bits/stdc++.h> char s[105] = {}, t[105] = {}; int sCnt[26] = {}, tCnt[26] = {}; int search(int n, int loc, int *last) { for (int i = *last; i <= n; i++) if (s[i] == t[loc]) { *last = i + 1; return 1; } return 0; } int main() { int p, n, flag, last, max; scanf("%d", &p); while (p ...
7
#include <bits/stdc++.h> using namespace std; int a[2000006]; bool cmp(int x, int y) { return x > y; } int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); for (int i = 0; i <= n; i++) a[i] = 0; for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a[x]++; ...
5
#include <bits/stdc++.h> using namespace std; int first[100005]; int second[100005]; int third[100005]; int main() { int n; int j; cin >> n; for (int i = 0; i < n; ++i) { cin >> first[i]; } for (int i = 0; i < n - 1; ++i) { cin >> second[i]; } for (int i = 0; i < n - 2; ++i) { cin >> third[i...
1
#include <bits/stdc++.h> using namespace std; void solve() { long long int n; cin >> n; vector<long long int> arr; set<long long int> s; for (int i = 0; i < n; i++) { long long int val; cin >> val; arr.push_back(val); } bool temp = false; long long int sum = arr[0] + arr[1]; for (int i = 2...
0
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 28; const double EPS = 1e-8; const int MOD = 1000000007; int n, m; vector<pair<int, int> > g[1000]; int main() { scanf("%d%d", &n, &m); vector<int> d(n); for (int i = 0; i < (int)(n); i++) scanf("%d", &d[i]); long long int ans = 0; for (int i ...
3
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int l = 1; int r = 100000; while (r - l > 1) { int m = (l + r) / 2; vector<bool> used(m + 1); int A = a; int B = b; int cnt = 0; for (int i = m; i >= 1; i--) { if (A >= i) { A -= i; ...
4
#include <bits/stdc++.h> using namespace std; const long long Inf = 1000000000000000000LL; const int Maxm = 100005; struct pos { int x, y, z; pos(int x = 0, int y = 0, int z = 0) : x(x), y(y), z(z) {} }; int n; map<int, pair<int, int> > S; int m, d[Maxm], l[Maxm]; int C[Maxm]; long long best[Maxm][2][2]; pos par[Ma...
8
#include <bits/stdc++.h> using namespace std; const long long N = 0x3f3f3f3f3f3f3f3f; const int maxn = 1e5 + 10; struct node { int v, next; }; node edge[2 * maxn]; long long val[maxn], minn[maxn], maxx[maxn]; int first[maxn]; int n, num; void addedge(int u, int v) { edge[num].v = v; edge[num].next = first[u]; f...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 2005; int a[maxn], b[maxn]; queue<int> q; int n, m, cnt; int main() { cin >> n >> m; cnt = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] <= m) b[a[i]]++; else cnt++; } int c = n / m, d = n % m, e = 0; for (int i...
4
#include <bits/stdc++.h> using namespace std; map<int, int> C; vector<pair<int, int> > A, B; long long eval(int n, int &k, long long MOD) { long long ans = 1; for (int i = 1; i <= n; ++i) { int v = i; while (k > 0 && v % 2 == 0) v /= 2, --k; ans *= v; ans %= MOD; } return ans; } int main() { i...
4
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; long long qmod(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b = b >> 1; } return res; } long long jc[1000005]; long long jv[1000005]; long long p3[1000005]; inline ...
8
#include <bits/stdc++.h> using namespace std; const int NMax = 110000; const long long INF = 1000000000LL; int N; pair<long long, int> A[NMax], B[NMax]; multiset<pair<long long, int> > S; long long Max[NMax], Min[NMax], Max1[NMax], Min1[NMax]; int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) { long lo...
8
#include <bits/stdc++.h> using namespace std; vector<string> v, v1; map<string, long long> mp, mp1, mp2; char final[100000]; long long a[10000]; bool compare(string &s1, string &s2) { return s1.size() < s2.size(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, x, y,...
2
#include <bits/stdc++.h> using namespace std; int n, q, blockS; int nextt[100005], gdzie[100005], ile[100005]; void build(int x) { int start = (x / blockS) * blockS; int meta = min(((x / blockS) + 1) * blockS - 1, n - 1); for (int i = meta; i >= start; --i) { if (nextt[i] > meta) { gdzie[i] = nextt[i]; ...
9
#include <bits/stdc++.h> using namespace std; int sub[3000 + 10][3000 + 10], f[3000 + 10][3000 + 10]; long long dp[3000 + 10][3000 + 10]; vector<int> edge[3000 + 10]; void pre(int rt, int son, int first) { sub[rt][son] = 1; for (auto i : edge[son]) { if (i == first) continue; else { f[rt][i] = s...
7
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; int n[a]; cin >> n[0]; for (int i = 1; i < a; i++) { cin >> n[i]; cout << n[i - 1] + n[i] << ' '; } cout << n[a - 1]; }
0
#include <bits/stdc++.h> using namespace std; vector<int> ans[302]; vector<int> cools; vector<int> nspc; int n, m, k; int spc[302]; bool special[302]; bool good[302][302]; bool check[302][302]; bool direct[302]; void MakeFrineds(int vert1, int vert2) { ans[vert1].push_back(vert2); ans[vert2].push_back(vert1); } voi...
7
#include <bits/stdc++.h> template <class T> void Swap(T& a, T& b) { T c = a; a = b; b = c; } template <class T> T max(T& a, T& b) { return a < b ? b : a; } template <class T> T min(T& a, T& b) { return a > b ? b : a; } using namespace std; const int N = 2010; const int M = 200010; struct xfy { int l, r, x, ...
10
#include <bits/stdc++.h> using namespace std; int rev[(1 << 21)], mlgn; double omega[(1 << 21)][2]; double FA[(1 << 21)][2], FB[(1 << 21)][2]; inline void mult(double A[2], double B[2], double C[2]) { C[0] = A[0] * B[0] - A[1] * B[1]; C[1] = A[0] * B[1] + A[1] * B[0]; } void calc_rev(int n = (1 << 21)) { while ((...
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; struct node { int l, r, lmax, rmax, totmax, totmin, lcnt, rcnt, ans; node() : l(0), r(0), lmax(0), rmax(0), totmax(0), totmin(0), lcnt(0), rcnt(0), ans(0) {} node(int l, i...
12
#include <bits/stdc++.h> using namespace std; struct edge { int l, r, w; double factor; } E[100005]; int n, q; vector<pair<int, int> > e[100005]; int siz[100005]; double nPx(double N, int x) { double ans = 1; for (int i = 1; i <= x; i++) { ans *= (N - i + 1) / i; } return ans; } void pre(int id, int pre...
5
#include <bits/stdc++.h> using namespace std; const long double PI = 3.1415926535897923846; const long long int MOD = 998244353; const long long int N = 998244353; long long int power(long long int x, long long int n) { long long int res = 1; while (n > 0) { if (n & 1) res = res * x % MOD; x = x * x % MOD; ...
4
#include <bits/stdc++.h> using namespace std; long long int cnt[100010]; long long int f(long long int k) { long long int ans = ((3 * k + 1) * k) / 2; return ans; } int main() { long long int n; cin >> n; long long int lo = 1, hi = 100000000, mid; while (hi > lo) { mid = (hi + lo) / 2; if (f(mid) > ...
4
#include <bits/stdc++.h> using namespace std; int K; int N[3], T[3]; priority_queue<int, vector<int>, greater<int> > q[3]; int main() { scanf("%d", &K); for (int(i) = 0; (i) < (3); (i)++) scanf("%d", &N[i]); for (int(i) = 0; (i) < (3); (i)++) scanf("%d", &T[i]); for (int(i) = 0; (i) < (3); (i)++) for (int(j...
5
#include <bits/stdc++.h> using namespace std; long long ncr[1011][1011]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; long long k; cin >> k; if (k == 0) { cout << 1; return 0; } for (int i = 0; i < 1011; i++) { ncr[i][0] = 1; } for (int i = 1; i < 1...
5
#include <bits/stdc++.h> using namespace std; vector<long long int> tabela; int main() { tabela.clear(); tabela.push_back(4); tabela.push_back(7); long long int k = 2; long long int j = 2; long long int p = 10; while (k <= 512) { for (int i = j - k; i <= j - 1; i++) tabela.push_back(4 * p + tabela[i])...
1
#include <bits/stdc++.h> using namespace std; const int INF = 300000; int A[300000]; int main() { for (int i = 0; i < 300000; i++) A[i] = INF; int n, x; cin >> n; for (int i = 0; i < n; i++) { cin >> x; A[x] = i; } int k = 0; for (int i = 1; i < 300000; i++) if (A[i] < A[k]) k = i; cout << k...
1
#include <bits/stdc++.h> int b[2][100000], c[2][100000], n, m, i, j, p, z = 1, f; struct cd { int x, y; } a[100000]; bool cmp(cd a, cd b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } int main() { scanf("%d%d", &n, &m); for (i = 0; i < m; i++) { scanf("%d%d", &a[i].x, &a[i].y); a[i].x--, a[i].y--; } ...
8
#include <bits/stdc++.h> using namespace std; int n, size[200000 + 5], dep[200000 + 5], pre[200000 + 5], le[200000 + 5], ri[200000 + 5], son[200000 + 5][4], e[200000 + 5], a[4]; long long dp[200000 + 5]; void dfs(int u, int fa) { size[u] = 1; for (int i = 0; i < e[u]; i++) { int v = son[u][i]; if (v == ...
11
#include <bits/stdc++.h> using namespace std; long long dp[102][102][30][2]; struct node { int v, val; }; int n, m; vector<node> g[200]; int dfs(int x, int y, int z, int t) { if (dp[x][y][z][t] != -1) return dp[x][y][z][t]; int flag = 0; if (t == 1) { int l = g[x].size(); for (int i = 0; i < l; i++) { ...
4
#include <bits/stdc++.h> int map[500][500], visit[500][500], cnt = 0, n; char ans[3000000]; void dfs(int x, int y) { visit[x][y] = 1; if (!map[x][y]) { map[x][y] = 1; ans[cnt++] = '1'; } int i; for (i = x - 1; i > -1; i--) { if (map[i][y]) { if (!visit[x - 1][y]) { ans[cnt++] = 'U'; ...
8
#include <bits/stdc++.h> using namespace std; int N; int A[200201]; int dp[200201]; int seg[530000]; int Update(int idx, int val, int n, int l, int r) { if (r < idx || idx < l) return seg[n]; if (l == r) return seg[n] = max(seg[n], val); int mid = l + r >> 1; return seg[n] = max(Update(idx, val, n << 1, l, mid)...
6
#include <bits/stdc++.h> using namespace std; void solve() { int n, m; cin >> n >> m; vector<vector<int>> adj(n + 1); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } vector<vector<int>> des(n + 1); for (int i = 1; i <= n; i++) { int a;...
4
#include <bits/stdc++.h> using namespace std; int const MAXN = 2e6 + 9; int const MAXNN = 1e5 + 9; int p[MAXN], depth[MAXNN], ans[MAXNN], nbr[MAXNN]; map<int, int> id; stack<pair<int, int>> st[150000]; vector<vector<int>> adj; void getDepth(int node, int p) { for (auto child : adj[node]) { if (child == p) continu...
6
#include <bits/stdc++.h> char grid[500][501]; int n, m, moves[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}, nc, nr, res; void dfs(int r, int c, int first, int counting) { if (counting) res += 1; else printf("B %d %d\n", r + 1, c + 1); grid[r][c] = 'C'; for (int _n = 4, i = 0; i < _n; ++i) { nr = r + m...
5
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long n, i, j, ar[300005], d, a, b, m, l, k, pt, q, sz[300005], c0, d0, c, r; long long dp[300005], f = 0; long long mrk[3][300005]; pair<long long, pair<long long, long long>> p[300005]; vector<int> v, vt; int main() { scanf("%lld %lld", ...
3
#include <bits/stdc++.h> using namespace std; char invert(char a) { if (a == 'F') return 'T'; return 'F'; } int num(int x) { if (x == 0) return -1; return 1; } int max(int a, int b) { if (a > b) return a; else return b; } int mas[111][111][2]; bool dost[111][111][2]; int main() { string s; cin >...
5
#include <bits/stdc++.h> using namespace std; int a[100010]; int gcd(int a, int b) { int temp; while (b) { temp = a % b; a = b; b = temp; } return a; } int main() { int n; scanf("%d", &n); int i, j, gc; for (i = 0; i < n; i++) { scanf("%d", &a[i]); if (i == 0) gc = a[i]; el...
2
#include <bits/stdc++.h> using namespace std; template <class T> T amax(T _a, T _b) { return _a > _b ? _a : _b; } template <class T> T amin(T _a, T _b) { return _a < _b ? _a : _b; } template <class T> T gcd(T _a, T _b) { T t; while (_b) { t = _b; _b = _a % _b; _a = t; } return _a; } template <cl...
0
#include <bits/stdc++.h> using namespace std; #define int long long int #define pb emplace_back #define mp make_pair #define fi first #define se second #define all(v) v.begin(),v.end() #define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0); #define mod 1000000007 #define decimal(n,k) cout<<fix...
0
#include <bits/stdc++.h> using namespace std; vector<pair<double, double> > data; double p; bool simulate(double t) { double totalPower = p; for (int i = 0; i < data.size(); i++) { double NeededPower = (data[i].first - data[i].second / t); if (NeededPower > 0) { totalPower -= NeededPower; } if...
5
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, m; cin >> n >> m; long long int a[m]; map<long long int, long long int> mp; long long int sm = 0; for (long long int i = 0; i < m; i++) { cin >> a[i]; mp[a[i]]++; sm += a[i]; } if (sm < n) { cout << -1 << endl;...
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d", &n); scanf("%d", &m); cin.get(); char image[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) scanf("%c", &image[i][j]); cin.get(); } char check[4]; int count = 0; if (n >= 2 && m >= 2) { for ...
0
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-9; const int inf = 2000000000; const long long infLL = 9000000000000000000; inline bool checkBit(long long n, int i) { return n & (1LL << i); } inline long long setBit(long long n, int i) { return n | (1LL << i); ; } inl...
2
#include <bits/stdc++.h> using namespace std; const int kMaxN = 100000; vector<int> G[kMaxN + 1]; bool B[kMaxN + 1]; vector<int> ans; priority_queue<int> options; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; G[x]....
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j, a, b, oc[101] = {0}; cin >> n >> m; if (m == 0) { cout << 0; return 0; } if (m == 1) { cout << 1; return 0; } vector<vector<int> > g; g.resize(n + 1); for (i = 0; i < m; ++i) { cin >> a >> b; g[a].push_b...
2
#include <bits/stdc++.h> using namespace std; const long long maxn = 5e5 + 7; const long long mod = 1e9 + 7; const long long INF = 1e9 + 7; const long long mlog = 20; const long long SQ = 400; unordered_map<long long, long long> mp; long long cnt[2]; long long a[maxn]; vector<long long> change; int32_t main() { ios_b...
5
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); vector<int> digitos(long long n, int dig) { vector<int> res; while (n > 0) { res.push_back(n % 2); n /= 2; } while (res.size() < dig) res.push_back(0); reverse(res.begin(), res.end()); return res; } int main() { int n; w...
4
#include <bits/stdc++.h> using namespace std; const long long N = 500010; inline long long read() { long long s = 0, w = 1; register char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch =...
4
#include <bits/stdc++.h> using namespace std; long long add(long long a, long long b) { a %= 1000000007; b %= 1000000007; long long p = (a + b) % 1000000007; return (p + 1000000007) % 1000000007; } long long mul(long long a, long long b) { a %= 1000000007; b %= 1000000007; long long p = (a * b) % 10000000...
8
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) { cerr << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << " , "; err(++it, args...); } const int inf = 0x3f3f3f3f; const int mod = 998244353; co...
9
#include <bits/stdc++.h> using namespace std; int n; long long ans; long long tree[200000]; int main() { cin >> n; long long sz = 1; for (int i = 0; i <= n; ++i) sz *= 2; sz--; for (int i = 2; i <= sz; ++i) cin >> tree[i]; for (int i = sz; i >= 1; --i) { ans += max(tree[i + i], tree[i + i + 1]) ...
3
#include <bits/stdc++.h> using namespace std; unsigned long long n, m; unsigned long long k; vector<int> digits; map<pair<pair<int, int>, unsigned long long>, unsigned long long> memo; vector<int> todigits(unsigned long long num) { int i = 0; vector<int> ans; while (num > 0) { int d = num % 2; ans.push_ba...
6
#include <bits/stdc++.h> #pragma GCC optimize(3) #pragma GCC optimize(2) using namespace std; struct Maxflow { struct edge { int from, to; long long flow, capacity; }; int s, t; vector<edge> edg; vector<int> g[2011]; int dist[2011], visited[2011]; int cur[2011]; void init() { edg.clear(); ...
10
#include <bits/stdc++.h> using namespace std; int a[1 << 21]; long long c[2][21]; int n, m, q; void build(int l, int r, int dep) { if (l == r) return; int mid = (l + r) >> 1, pos; build(l, mid, dep - 1); build(mid + 1, r, dep - 1); for (int i = (l), _en = (mid); i <= _en; i++) { pos = lower_bound(a + mid ...
6