solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> #pragma GCC optimize("-O2") 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 << "\t"; err(++it, args...); } template <typename T1, typenam...
15
#include <bits/stdc++.h> using namespace std; string s; bool b[1010]; int main() { cin >> s; int n = s.size(); int a = 0, m = 0; for (int i = 0; i < n; i++) { if (s[i] == ',') { b[a] = true; if (a > m) m = a; a = 0; } else { a *= 10; a += s[i] - '0'; } } b[a] = true...
5
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; int N = 1e9 + 7, M = 1e6, N2 = 1234567891; int x, y, z, i, j, n, m, k, l, r, t, ma = 0, ans, x2, y2, f, kol, mi, a[410000], b[410000], d[401000...
1
#include <bits/stdc++.h> using namespace std; const long long linf = 1e18 + 5; int mod = (int)1e9 + 7; const int logN = 18; const int inf = 1e9 + 9; const int N = 2e5 + 6; int n, m, x, y, z, t, root[N], p[N]; long long ans; vector<pair<int, pair<int, int> > > v; int findset(int x) { return root[x] = root[x] == x ? x : ...
17
#include <bits/stdc++.h> using namespace std; int f[20][20]; int main() { int n, ans = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) scanf("%d", &f[i][j]); for (int k = 1; k <= n; ++k) for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) f[i][j] = min(f[i][j], ...
12
#include <bits/stdc++.h> using namespace std; using i64 = long long; const int maxN = 2234567; int n, x; int e[maxN], cnt[maxN], p[maxN], cc[maxN]; bool st[maxN]; void add(int x) { int y = x; while (y != 1) { int p1 = p[y]; int e1 = 0; while (y % p1 == 0) { y /= p1; e1++; } if (e[p1]...
21
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3fLL; const double PI = acos(-1.0); const long double eps = 1e-6; const int mod = 1e9 + 7; const int maxn = 20; const int N = 2e5 + 100; const int M = 3e5 + 10; const long long mm = (1...
8
#include <bits/stdc++.h> using namespace std; long long a[200005]; long long n, s, sum = 0, z1, z2, ans; int main() { cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } z1 = s - n + 1ll; for (int i = 0; i < n; i++) { ans = 0ll; z2 = max(s - (sum - a[i]), 0ll); ans += ...
8
#include <bits/stdc++.h> using namespace std; #define ll long long #define N 100005 #define mod 1000000007 ll pow(ll a,ll b) { ll res = 1; while(b>0) { if(b&1) res = res*a; a = a*a; b >>=1; } return res; } ll pow_mod(ll a,ll b) { ll res = 1; while(b>0) { ...
2
#include <bits/stdc++.h> using namespace std; inline long long pow(long long x, long long y, long long mod) { long long ans = 1; for (; y; y >>= 1, x = (x * x) % mod) if (y & 1) ans = (ans * x) % mod; return ans % mod; } const long long mod = 1000000007; const int maxn = 100005; long long fac[maxn << 1]; int ...
11
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 30; long long lis[N]; long long gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; return gcd(b, a % b); } int main() { int q; cin >> q; while (q--) { int n; cin >> n; for (int i = 0; i < n; i++) { c...
8
#include <bits/stdc++.h> using namespace std; long long dp[2010][2010], qp[2020][2010], A[2010][2010]; long long cc; vector<long long> X[2010], Y[2010]; signed main() { cc = 1e9 + 7; long long n, m; cin >> n >> m; for (long long i = 0; i < n; i++) { string s; cin >> s; for (long long j = 0; j < m; j...
14
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int s, x1, x2; cin >> s >> x1 >> x2; long long int t1, t2; cin >> t1 >> t2; long long int p, d; cin >> p >> d; long long int ans = abs(x2 - x1) * t2; if (x2 > x1) {...
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long k; cin >> k; long long arr[k]; for (auto &x : arr) cin >> x; long **dp = new long *[1001]; for (auto i = (0); i <= (1000); i++) dp[i] = new long[1001]; long long mod ...
7
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string s[300]; for (int i = 1; i <= n; i++) { cin >> s[i]; } bool u[300] = {false}; int ans = 0; for (int i = 0; i < m; i++) { for (int j = 2; j <= n; j++) { if (s[j][i] < s[j - 1][i]) { if (i == 0)...
7
#include <bits/stdc++.h> using namespace std; int main() { int n, q, m, a[200005] = {}, t[200005] = {}, l[200005] = {}, r[200005] = {}, x; cin >> n >> q >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 0; i < q; i++) cin >> t[i] >> l[i] >> r[i]; while (m--) { cin >> x; for (...
10
#include <bits/stdc++.h> using namespace std; const int maxn = 112; int p[maxn]; char s[maxn]; vector<int> v; map<string, int> m; int main() { int a, b; scanf("%d %d", &a, &b); for (int i = 0; i < a; i++) { scanf("%d", p + i); } sort(p, p + a); for (int i = 0; i < b; i++) { scanf("%s", s); m[str...
3
#include <bits/stdc++.h> using namespace std; int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}, dxx[8] = {0, 0, 1, 1, 1, -1, -1, -1}, dyy[8] = {1, -1, 0, 1, -1, 0, 1, -1}; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, i; cin >> n; string s1, s2; cin >> s1 >> s2; int j = 0, k ...
5
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 10; const long long mod = 998244353; long long m; long long f[N]; long long vis[N], p[N], mu[N], tot = 0; long long pw1[N], pw2[N]; long long add(long long x) { return x >= mod ? x - mod : x; } long long sub(long long x) { return x < 0 ? x + mod : ...
20
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> g[100111]; vector<int> zero[100111]; vector<pair<int, int>> g2[100111]; struct edge { int from, to, wt; }; edge edg[100111]; int dist[100111][2], dist2[100111]; int visited[100111], conn[100111], pre[100111], pcost[100111]; pair<int, int> cost[10011...
14
#include <bits/stdc++.h> using namespace std; long long fpow(long long x, long long y, long long p = 1000000007) { x = x % p; long long sum = 1; while (y) { if (y & 1) sum = sum * x; sum %= p; y = y >> 1; x = x * x; x %= p; } return sum; } long long inv(long long a, long long m = 100000000...
9
#include <bits/stdc++.h> using namespace std; const int maxn = 3000 + 10; void lps(); int len; string a, b; int alph[27]; vector<char> dp; int dpl[maxn][maxn]; int parent[maxn][maxn]; int main() { cin >> a; len = a.length(); if (len <= 3000) { b.resize(len); for (int i = 0; i < len; i++) b[i] = a[len - (i...
11
#include<bits/stdc++.h> #pragma GCC optimize(2) using namespace std; #define ll long long #define int long long inline ll read(){ll x=0,f=1;char ch=getchar();for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-f;for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';return x*f;} inline void write(ll x){if(x<0)x=-x,putchar('-');if...
9
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int a, b, c, x, y, z, s; cin >> a >> b >> c >> x >> y >> z; if (a > x) s = (a - x) / 2; else s = (a - x); if (b > y) s += ((b - y) / 2); else s += (b - y); if (c > z) s += ((c - z) / 2);...
4
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-10; const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const int maxn = 2e5 + 5; int n; char s[maxn]; int dp[maxn], ans[maxn]; vector<int> ve[maxn]; int main(void) { scanf("%d%s", &n, s + 1); int len = 1; dp[1] = s...
10
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const long long P = 1000000007LL; const int maxN = 1100000; int k, p; bool used[maxN]; int main() { scanf("%d%d", &p, &k); if (k == 0) { long long res = 1; for (int i = 1; i < p; ++i) { res = (res * p) % P; }...
10
#include <bits/stdc++.h> using namespace std; bool is_Smaller_str(string str1, string str2) { long long n1 = str1.length(), n2 = str2.length(); if (n1 < n2) return true; if (n2 < n1) return false; for (int i = 0; i < n1; i++) if (str1[i] < str2[i]) return true; else if (str1[i] > str2[i]) re...
15
#include <bits/stdc++.h> using namespace std; int main() { char main[100], a[100][100]; int i, j, k = 0; cin >> main; for (i = 0; i < 10; i++) { cin >> a[i]; } for (i = 0; i < 80; i += 10) for (j = 0; j < 10; j++) if (strncmp(main + i, a[j], 10) == 0) { cout << j; break; ...
1
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; char c[27] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; int t = 0; for (int i = 0; i < n; i++) { cout...
0
#include <bits/stdc++.h> using namespace std; void pe() { cout << "~Q/~@#\b" << endl; } int main(int argc, char **argv) { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0), cout.precision(15); int res = 0; int x; bool add = true; while (cin >> x) { if (add) { res += x; } else { r...
12
#include <bits/stdc++.h> using namespace std; int p[1000005]; long long ppr[1000005]; bool f[1000005]; int main() { int n, k, c = 0; ppr[1] = 1; scanf("%d%d", &n, &k); for (int i = 2; i <= n; ++i) { if (!f[i]) ppr[i] = (p[c++] = i) - 1; for (int j = 0; j < c && i * p[j] <= n; ++j) { f[i * p[j]] = ...
20
#include <bits/stdc++.h> using namespace std; long long a[100100]; map<long long, bool> mp; vector<long long> vec; int main() { long long n, m, i; cin >> n >> m; a[0] = 0; for (i = 1; i <= n; i++) cin >> a[i], mp[a[i]] = true; long long sum = 0; for (long long i = 1; i + sum <= m; i++) { if (!mp[i]) { ...
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; map<long long, long long> m; int n; long long get_sg(long long num) { if (num >= 2562991876) return 2; if (num >= 50626) return 1; if (num >= 6724) return 3; if (num >= 82) return 0; if (num >= 16) return 2; ...
14
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cout << setprecision(12) << fixed; int a, b, n; cin >> a >> b; n = a + b; int res = 0; int k = 2; while (k <= n) { int g = n / k; int al = (a + g) / (1 + g); int ah = a / g; int bl = (b + g) / (1...
19
#include <bits/stdc++.h> using namespace std; long long int dp[100005]; int main() { long long int i, j, k; long long int n, m; long long int a, b; cin >> n >> k; memset(dp, 0, sizeof(dp)); i = 1; while (i < k) { dp[i] = 1; i++; } dp[k] = 2; for (i = k + 1; i < 100005; i++) { dp[i] = (dp...
9
#include <bits/stdc++.h> using namespace std; const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1}; int n, m; char s[1010][1010]; bool vis[1010][1010]; bool fail(int x, int y) { return x < 1 || y < 1 || x > n || y > m || s[x][y] == '.'; } int main() { scanf("%d%d", &n, &m); f...
5
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") const long long int inf = 9e18; const long double pi = 2 * acos(0.0); using namespace std; long long int power(long long int a, long long int n) { if (n == 0) { return 1; } long l...
14
#include <bits/stdc++.h> using namespace std; const int MAXK = 505; const int INF = 1e9 + 10; int n, m, k; struct Tk { int x, y; }; Tk a[MAXK]; bool cmp(Tk aa, Tk bb) { return aa.x < bb.x; } bool checkx(int pos, int p) { Tk tmp[MAXK]; for (int i = 0; i < k; ++i) if (a[i].x - p <= pos && a[i].x + p >= pos) ...
16
#include <bits/stdc++.h> using namespace std; int n, m, k; pair<int, int> start; char symbols[1005][1005]; int dp[1005][1005]; string path; const int INF = 1e9 + 7; char direct[] = {'D', 'L', 'R', 'U'}; void bfs(pair<int, int> start) { deque<pair<int, int> > fields; dp[start.first][start.second] = 0; fields.push_...
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 2 == 0) { cout << 6 << " " << n - 6; } else cout << 9 << " " << n - 9; }
0
#include <bits/stdc++.h> using namespace std; int n, m; int a[5005][5005]; int sum[5005][5005]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[1][i]; sum[1][i] = a[1][i]; } for (int i = 2; i <= n; i++) { for (int j = 1; j <=...
10
#include <bits/stdc++.h> constexpr bool local = false; using namespace std; template <typename... Args> void dbg(Args... args) { if (local) { fprintf(stderr, args...); } } int gInt() { int i; scanf("%d", &i); return i; } long long int gLong() { long long int i; scanf("%lld", &i); return i; } double ...
13
#include <bits/stdc++.h> using namespace std; long long ref(long long i) { long long res = 0, p10 = 1; while (i != 0) { res += (9 - i % 10) * p10; p10 *= 10; i /= 10; } return res; } int main() { long long l, r; long long pre[19] = {0, 4, 10, 49, 100, ...
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 100; vector<int> Mul(vector<int> A, vector<int> B) { vector<int> C; for (int i = 0; i < (int)A.size(); i++) C.push_back(B[A[i]]); A.clear(), B.clear(); return C; } vector<int> Power(vector<int> A, int b) { vector<int> res; for (int i = 0; ...
18
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; struct node { int a, b, idx, pos; } data[MAXN]; bool cmp1(const node &x, const node &y) { return x.b == y.b ? x.a > y.a : x.b < y.b; } bool cmp2(const node &x, const node &y) { return x.a == y.a ? x.pos > y.pos : x.a > y.a; } bool cmp3(const n...
14
#include <bits/stdc++.h> using namespace std; int main() { int n, x, ans = 0, maxn = 0; ; int st[55], ed[55]; scanf("%d%d", &n, &x); for (int i = 0; i < n; i++) { scanf("%d%d", &st[i], &ed[i]); maxn = max(maxn, ed[i]); } int num = 0; for (int i = 1; i <= maxn;) { if (i + x <= st[num]) ...
2
#include <bits/stdc++.h> using namespace std; const int N = 734343; vector<int> G[N], H[N]; vector<pair<int, int>> H1[N]; int deg[N], ans[N], dep[N], p[N], down[N]; int n; void dfs(int x) { down[x] = 1; ans[1] = max(ans[1], int(G[x].size()) + 1); for (auto y : G[x]) { if (y == p[x]) continue; p[y] = x; ...
21
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int ans = INT_MIN; for (int i = 0; i <= n; i++) { int curr = i; int lefn = n - i; int lefm = m - 2 * i; if (lefm >= 0) { curr += min(lefn / 2, lefm); ans = max(ans, curr); } } cout << ans; }
5
#include <bits/stdc++.h> using namespace std; const int kMaxN = 5100; int n, deg[kMaxN]; vector<int> G[kMaxN]; int dp[kMaxN][kMaxN][2]; int dfs(int u, int fa) { int cnt = 0; if (deg[u] == 1) { dp[u][0][1] = 0; dp[u][1][0] = 0; cnt = 1; } else { dp[u][0][0] = dp[u][0][1] = 0; for (int i = 0; i ...
16
#include <bits/stdc++.h> using namespace std; void IOS() { ios::sync_with_stdio(false); cout.precision(10); cout << fixed; } const int MOD = 1e9 + 7; const int mx = 1e5 + 5; int main() { IOS(); int n; cin >> n; priority_queue<pair<int, int>> pq; for (int i = 0; i <= n; i++) { if ((n - i * 7) < 0) ...
2
#include <bits/stdc++.h> using namespace std; void dfs(int node, int pere, int l, vector<vector<int>>& adj, vector<vector<int>>& up, vector<int>& depth) { up[node][0] = pere; for (int i = 1; i <= l; ++i) up[node][i] = up[up[node][i - 1]][i - 1]; for (int i : adj[node]) { if (i == pere) continue; ...
11
#include <bits/stdc++.h> using namespace std; int n; char c; queue<int> r, d; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { cin >> c; if (c == 'R') r.push(i); else d.push(i); } while (r.size() && d.size()) { if (r.front() < d.front()) d.pop(), r.push(r.front() + n...
7
#include <bits/stdc++.h> using namespace std; const int kN = 2e5 + 10; int n; vector<int> g[kN]; int sz[kN], mx[kN]; int pos[kN]; vector<int> point[kN]; long long ans = 0; bool flag; void Dfs(int u, int fa = -1, int top = -1) { sz[u] = 1, mx[u] = 0; if (top != -1 && !flag) point[top].push_back(u); for (int v : g[...
13
#include <bits/stdc++.h> using namespace std; template <class T> using vc = vector<T>; template <class T> using vvc = vc<vc<T>>; template <class T> void mkuni(vector<T>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } template <class T> void print(T x, int suc = 1) { cout << x; if...
22
#include <bits/stdc++.h> using namespace std; bool can(const vector<pair<long long, long long>>& vals, vector<vector<long long>>& pp) { if (vals.size() == 0) return true; if (vals.size() == 1) return false; long long sum = 0; priority_queue<pair<long long, long long>> pq; for (int i = 0; i < vals.siz...
10
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5; char s[maxn]; int n, p; int main() { cin.sync_with_stdio(0); cin.tie(0); cin >> n >> p; cin >> s; int ans = 0; int mx, mix; bool flag = 1; p--; if (p >= n / 2) p = n - 1 - p; for (int i = 0, j = n - 1; i < j; i++, j--) if (s[i] ...
9
#include <bits/stdc++.h> using namespace std; typedef struct edge { int u, v, w; } edge; edge E[200005]; long long ans[200005] = {0}; int p[200005]; long long sz[200005] = {0}; int comp(const void *a, const void *b) { return ((edge *)a)->w - ((edge *)b)->w; } int findSet(int u) { if (p[u] == u) return u; return...
10
#include <bits/stdc++.h> using namespace std; const int MAX = 200005; int main() { int n; int a[MAX]; cin >> n; for (int i = 0; i < n; i++) cin >> a[i], a[i] %= 2; stack<int> s; int len = 0; for (int i = 0; i <= n; i++) { if (i == n || i > 0 && a[i] != a[i - 1]) { len %= 2; if (len) { ...
14
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); int t, n, k; cin >> t; while (t--) { cin >> n >> k; int ans = 1; int temp, last; cin >> temp; ans = max(temp, 1); last = temp; int cnt; for (int i = 2; i <= k; i++) { cin >> temp; ...
2
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { int n, k; scanf("%d%d", &n, &k); if (k % 3) { int cnm = n % 3; puts(cnm == 0 ? "Bob" : "Alice"); } else { int cnm = n % (k + 1); puts(cnm % 3 == 0 && cnm != k ? "Bob" : "Alice")...
9
#include <bits/stdc++.h> using namespace std; int number; vector<int> vec; int n; int dp[11][2][2]; int f(int next, int one, int less) { if (dp[next][one][less] != -1) { return dp[next][one][less]; } if (next >= n) { if (!one) { dp[next][one][less] = 0; return 0; } else { dp[next][on...
4
#include <bits/stdc++.h> using namespace std; long long q, x, k; int main() { scanf("%lld", &q); while (q--) { scanf("%lld %lld", &k, &x); printf("%lld\n", (k - 1) * 9 + x); } return 0; }
2
#include <bits/stdc++.h> long long int expo(long long int a, long long int b, long long int m) { long long int x; if (b == 0) { return 1; } else { x = expo(a, b / 2, m); if (b % 2 == 0) { return (x * x) % m; } else { return (((a * x) % m) * (x)) % m; } } } int main() { long lon...
3
#include <bits/stdc++.h> using namespace std; const int INF = 1000000007; const long long LLINF = 1000000000000000007LL; const double EPS = 1e-9; const int sz = 200100; int ar[sz]; map<int, int> m; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; for (int i1 = 0; i1 < t; ...
2
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932384626433; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; string s; cin >> s; long long ans = 1; for (long long i = 1; i < n; i++) { ans += (s[...
8
#include <bits/stdc++.h> int main() { int n; unsigned int a, b, res; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &a, &b); res = 0; while (a && b) { if (a > b) { res += a / b; a %= b; } else { res += b / a; b %= a; } } printf(...
1
#include <bits/stdc++.h> using namespace std; long long ksm(__int128 a, long long b, long long mod, long long c = 1) { for (; b; b /= 2, a = a * a % mod) if (b & 1) c = c * a % mod; return c; } long long pw[20]; long long calc(long long x, int y) { x %= pw[y] >> y; if (y == 1) { if (x == 1) return 4; ...
24
#include <bits/stdc++.h> using namespace std; int find_area(int a, int b, int x, int y) { int s1 = max(x, abs(x - a + 1)); int s2 = max(y, abs(y - b + 1)); if (s1 * b > s2 * a) { return s1 * b; } else { return s2 * a; } } int main() { int t; cin >> t; while ((t--) > 0) { int a, b, x, y; ...
0
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, x; cin >> n; bool ok = false; vector<int> v(0); v.clear(); int a[n - 1], b[n - 1]; for (i = 0; i < n - 1; ++i) cin >> a[i]; for (i = 0; i < n - 1; ++i) cin >> b[i]; v.push_back(0); for (i = 0; i < n - 1; ++i) { ok = true; ...
7
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; int n, q; int a[maxn]; struct node { int l, r; long long sum; long long lazy; long long Min; } segTree[maxn << 2]; void build(int i, int l, int r) { segTree[i].l = l; segTree[i].r = r; if (l == r) { segTree[i].sum = a[l]; se...
18
#include<bits/stdc++.h> using namespace std; #define ll long long #define fori(i,a,b) for(int i=a;i<=b;i++) const ll maxn=2e5+100; ll n,mod; ll f[444][444]; ll a[444][444],po[444]; int main() { cin>>n>>mod; a[0][0]=1; for(ll i=1;i<=n;i++){ a[i][0]=1;a[i][i]=1; for(ll j=1;j<i;j++)a[i][j]=(a[i...
14
#include <bits/stdc++.h> using namespace std; long long n; vector<long long> g[100005]; long double st[100005]; long long p[100005]; long double sum[100005]; long double child(long long x) { long long ret = g[x].size() - 1; ret += (x == 1); return (long double)(ret); } void go(long long s) { sum[s] = 1; for (...
9
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef pair<double, double> pd;...
2
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; mt19937 rnd(chrono::steady_clock().now().time_since_epoch().count()); const int N = (int)5e5 + 9; const int BL = 700; const int K = 1000; int p[N]; int len[N]; int nx[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ...
14
#include <bits/stdc++.h> using namespace std; const int M = 5e6 + 5; const long long int mod = 1e9 + 7; const long long int inf = 9e18; const double pi = 3.14159265358979323846; long long int t, n, m, x, sum, k, p, u = 0, t1 = 1; string s, z, ans = ""; long long int P[10000005] = {0}; void sieve() { for (long long in...
9
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int counter = 0; if (arr[0] == 0) cout << "0"; else if (arr[k - 1] == 0) { for (int q = 0; q < k - 1; q++) { if (arr[q] > 0) counter++; } ...
0
#include <bits/stdc++.h> using namespace std; int d, n; int u, v; int val[2014]; vector<int> tree[2014]; int dfs(int root, int base, int curr) { int ans, to; if (base) if ((val[curr] - val[root] > d) || val[curr] < val[root] || (val[curr] == val[root] && curr < root)) return 0; ans = 1; for (i...
13
#include <bits/stdc++.h> using namespace std; string s; map<string, bool> folosit; int dp[10010][2]; bool generateSuffixes(int pos, string last) { if (dp[pos][last.size() - 2] != -1) return dp[pos][last.size() - 2]; bool res = false; if (pos + 2 < s.size() && s.substr(pos + 1, 2) != last) { bool ok = generate...
10
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O3") const double pie = 2 * acos(0.0); int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k, l, c, d, p, nl, np; cin >> n >> k >> l >> c >> d >> p >> nl >> np; int total_drink = k * l / nl; int total_lime = c ...
0
#include <bits/stdc++.h> using namespace std; long long f[50 + 5][2]; long long ss, xx; void solve() { f[0][0] = 1; for (int i = 0; i < 50; i++) for (int carry = 0; carry < 2; carry++) for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++) { if (f[i][carry] == 0) continue; int...
9
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; for (int i = 0; i < n; i++) { long long a, b; long long maxi = INT_MIN; cin >> a >> b; long long c[a]; if (b % 2 == 1) { for (int j = 0; j < a; j++) { cin >> c[j]; maxi = max(maxi, c[j]); ...
0
#include <bits/stdc++.h> using namespace std; const long long int N = (1e6); const long long int mod = (1e9 + 7); int main() { int n; cin >> n; while (n--) { int a, second, t; cin >> a >> second >> t; cout << max(t, second) - (second + t - a) + 1 << endl; } }
1
#include <bits/stdc++.h> using namespace std; int n, m; void input() { scanf("%d%d", &n, &m); } void solve() { int x, y, p, q; x = y = 1; p = n, q = m; int upch = 1, downch = -1; long long steps = (1LL * n * m) / 2; while (steps > 0) { printf("%d %d\n", x, y); if (p != x || q != y) { printf("%...
10
#include <bits/stdc++.h> using namespace std; int main() { long long int testcases = 1; cin >> testcases; while (testcases--) { string s; cin >> s; if (s.length() == 1) { cout << 0 << endl; } else { map<char, int> mp; for (int i = 0; i < s.length(); i++) { mp[s[i]]++; ...
0
#include <bits/stdc++.h> using namespace std; int main() { string s, t; int c = 0, i; cin >> s; cin >> t; int si = s.size(); for (i = 0; i < si; i++) { if (s[i] == t[si - i - 1]) c++; } if (c == si) cout << "YES"; else cout << "NO"; }
0
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int lcm(int a, int b) { return a * b / gcd(a, b); } bool vis[100010]; vector<pair<long long, long long> > edges[100010]; long long result, ma = INT_MIN; long long gcdExtended(long long a, long long b,...
8
#include <bits/stdc++.h> using namespace std; const int undef = 1; const int truth = 2; set<int> possible; vector<int> yes[100010]; vector<int> no[100010]; int status[100010]; int pattern[100010]; int main() { cin.sync_with_stdio(0); cin.tie(0); int n, m, noLen = 0, yesLen = 0; cin >> n >> m; for (int i = 1; ...
8
#include <bits/stdc++.h> using namespace std; int a[100000]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int n, g, ans, i, j; scanf("%d", &n); ans = 1e9 + 7; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (i = 1; i <= n; i++) { g = a[i]; if (g == ...
7
#include <bits/stdc++.h> using namespace std; void dfs(int flag, int index, vector<vector<int>> &ad, int parent, int arr[], int dp[]) { int h = 0; if (flag == 1) { for (int i = 0; i < ad[index].size(); i++) { int b = ad[index][i]; if (b != parent) { dfs(flag, b, ad, index, arr, dp);...
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 1E4 + 1; int arr[MAXN], ii[3 * MAXN], jj[3 * MAXN], xx[3 * MAXN]; void rec(int i, int j, int x, int &c) { ii[c] = i; jj[c] = j; xx[c] = x; arr[i] -= i * x; arr[j] += i * x; c++; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL);...
12
#include <bits/stdc++.h> using namespace std; vector<int> v[200005]; int color[200005], a[200005], b[2000005]; int flag, n, m; void dfs(int x, int fa, int y) { color[x] = y; for (int i = 0; i < v[x].size(); i++) { int n = v[x][i]; if (n == fa) continue; if (color[n] == -1) dfs(n, x, 1 - y); el...
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 55, MAXM = 20020; int n, m, k, acc[MAXN][MAXM], dp[MAXN][MAXM], ans; struct no { int beg, end, mid, mx, lz; no *l, *r; no(int beg, int end) : beg(beg), end(end), mid((beg + end) >> 1), mx(0), lz(0) { if (beg != end) l = new no(beg, mid), r =...
15
#include <bits/stdc++.h> using namespace std; char str[100010]; int main() { scanf("%s", str); int flag = 0; int len = strlen(str); for (int i = 0; str[i]; i++) { if (flag && str[i] == 'a') { flag = 2; break; } if (flag && str[i] != 'a') { str[i]--; } if (!flag && str[i] !=...
4
#include <bits/stdc++.h> using namespace std; inline int get() { char c; while (!isdigit(c = getchar())) ; int v = c - 48; while (isdigit(c = getchar())) v = v * 10 + c - 48; return v; } int n, a[55]; long long m, f[55][2][2]; bool check() { static int b[55]; memcpy(b, a, sizeof a); reverse(b + 1, b...
18
#include <bits/stdc++.h> using namespace std; int n, v, e, p, q; int m[100010]; string s, s1, s2; long long ans = 1e9 + 7; int main() { cin >> n >> v; for (int i = 1; i <= n; i++) { cin >> m[i]; ans = min(ans, 1ll * m[i]); } for (int i = 1; i <= n; i++) { m[i] -= ans; } ans = 0; for (int i = 1...
1
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int n; void solve() { int ans = 0; for (int i = 1; i * i <= n; ++i) { if (n % i == 0) { if (i == 1 || i * i == n) ans++; else ans += 2; } } cout << ans << endl; } int main(void) { ios_base::sync_with_...
0
#include <bits/stdc++.h> const int N = 1e5 + 5; const int mod = (int)1e9; const int INF = 0x3f3f3f3f; const long long LINF = (1LL << 62); const double PI = acos(-1.0); using namespace std; int tag[11], ans = INF; bool check(string t) { if (t.length() >= 2) { int len = t.length(); if (t[len - 2] == '0' && t[le...
13
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ch = 0; int res = 0; cin >> ch; res = ch; for (int i = 1, h; i < n; ++i) { cin >> h; res += 1 + abs(ch - h); ch = h; } cout << (res + n); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 300000 + 9; const int Maxmask = 1953124 + 10; const int Mod = 1000000007; multiset<long long> Q; vector<long long> ans; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { long long t; scanf("%I64d", &t); Q.insert(t); } ...
14
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); cin.sync_with_stdio(0); string s; cin >> s; int m; cin >> m; while (m--) { int l, r, k; cin >> l >> r >> k; string cpy = s.substr(l - 1, r - l + 1); 0; for (int i = 0; i <= r - l; ++i) { s[l - 1 + (i + k) % (r...
5