solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 6, LG = 22; int n, k; int arr[N], nxt[N]; vector<int> adj[N]; int st[N], en[N], eul[N], dfstime; int btree[4 * N], lazy[4 * N]; void dfs(int x) { st[x] = dfstime; eul[dfstime++] = x; for (int i : adj[x]) dfs(i); en[x] = dfstime - 1; } void prob(i...
16
#include <bits/stdc++.h> using namespace std; const int N = (int)1e7; const int MOD = (int)1e9 + 9; const int inf = (int)5e8; const long long infll = (long long)1e17; void solve() { int n; scanf("%d", &n); map<string, int> cnt; for (int i = 0; i < n; ++i) { string s; cin >> s; ++cnt[s]; } vector...
0
#include <bits/stdc++.h> using namespace std; const long long INF = 0x3f3f3f3f; const int mod = 1e9 + 6; const int maxn = 3e5 + 50; long long n, p, k; map<long long, long long> g; long long a[maxn]; int main(int argc, char *argv[]) { if (argc == 2 && strcmp("-debug", argv[1]) == 0) { freopen("in.txt", "r", stdin)...
15
#include <bits/stdc++.h> using namespace std; using ll = long long; template <typename T> using v = vector<T>; struct query { int i, l, r, s, t; query() {} query(int i_, int l_, int r_, int s_, int t_) : i(i_), l(l_), r(r_), s(s_), t(t_) {} }; const int N = 1000; const int M = 200000; const int INF = numeri...
20
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<int> a(n), b(n); for (auto &x : a) cin >> x; for (auto &x : b) cin >> x; sort(a.begin(), a.end()); sort(b.begin(), b.end()); long long A = 0, B = 0; ...
5
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } long long lncr(long long n, long long r) { long long i, ans = 1, k = 0; if (r == 0) return 1; else if (n < r) return 0; for (i = n; i >= n - r + 1; i--) { ans *= i; ans /= n - ...
2
#include <bits/stdc++.h> using namespace std; int main() { int vladik, valera; cin >> vladik >> valera; int p = 1; while (1) { if (vladik - p < 0) { cout << "Vladik" << endl; return 0; } else { vladik -= p; p++; } if (valera - p < 0) { cout << "Valera" << endl; ...
0
#include <bits/stdc++.h> using namespace std; using LL = long long; constexpr int N = 1e5 + 5; constexpr int INF = 2e9; int n, g, L, t; vector<pair<int, int>> cars; vector<int> gas; vector<int> distances; bool possible(int v) { const int capacity = v; LL T = 0; for (auto D : distances) { if (D > capacity) ret...
9
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 28; const int mod = (int)1e9 + 7; const double eps = 1e-8; const double pi = acos(-1.0); long long a, b, c, l; long long bad(long long a, long long b, long long c, long long l) { long long ret = 0; for (long long i = 0; i <= l; i++) { long long ...
13
#include <bits/stdc++.h> using namespace std; const int N = 4500000, mod = 1000000007; long long c[N][4], f[4]; template <class T> inline T gcd(T a, T b) { if (a < b) swap(a, b); for (T r; b; r = a % b, a = b, b = r) ; return a; } int main() { int w, h; scanf("%d%d", &w, &h); c[0][0] = 1; for (int i =...
17
#include <bits/stdc++.h> using namespace std; const int N = 100100; map<int, int> gg(int x) { map<int, int> ans; ans.clear(); for (long long i = 2; i * i <= x; i++) { while (x % i == 0) { x /= i; ans[i]++; } } if (x != 1) { ans[x]++; } return ans; } map<map<int, int>, int> qaq; int...
10
#include <bits/stdc++.h> using namespace std; int n; string s, t; int ans; int main() { cin >> n >> s >> t; for (int i = 0; i < n; i++) { int x = abs((int)s[i] - t[i]); ans += min(x, 10 - x); } cout << ans << '\n'; }
0
#include <bits/stdc++.h> using namespace std; long long solve(long long p) { int x = sqrt(p) + .00000001; for (int i = (x); i >= (1); i--) { if (p % i == 0) return p / i + i; } } int main() { long long n = ({ int x; scanf("%d", &x); x; }); long long x = 1; long long mi = 100000000000000000...
8
#include <bits/stdc++.h> using namespace std; int n; int a[500007], id[500007], b[500007]; set<int> now; set<pair<int, int> > seg; int take = 1; int ans[500007]; bool cmp(int x, int y) { return a[x] < a[y]; } void inp() { cin >> n; for (int i = (1); i <= (n); ++i) { cin >> a[i]; id[i] = i; b[i] = 2; ...
25
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { long long n, m; cin >> n >> m; long long x = n * (n + 1) / 2; if (m == 0) { cout << 0 << '\n'; continue; } n -= m; long long y = n / (m ...
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int *a = new int[n]; for (int i = 0; i < n; i++) cin >> a[i]; int ans = 0, m = 0; while (1) { for (int i = 0; i < n; i++) { if (a[i] <= m) { m++; a[i] = n + 2; } } if (m == n) break; ans++...
4
#include <bits/stdc++.h> int main() { char map[110][110]; bool t[110][110]; int n, m, i, j, k, l; scanf("%d%d", &n, &m); getchar(); memset(t, 0, sizeof(t)); for (i = 0; i < n; ++i) { for (j = 0; j < m; ++j) { scanf("%c", &map[i][j]); } getchar(); } for (i = 0; i < n; ++i) { for (...
3
#include <bits/stdc++.h> using namespace std; int main() { int n, temp, sum = 0; cin >> n; vector<int> v; for (int i = 1; i <= 7; i++) { cin >> temp; v.push_back(temp); } while (true) { for (int i = 0; i < 7; i++) { sum += v[i]; if (sum < n) { continue; } else { ...
2
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 1; string s; int cnt[26]; int main() { int n, k; scanf("%d %d", &n, &k); cin >> s; for (auto c : s) cnt[c - 'A']++; int ans = 1e9; for (int i = 0; i < k; i++) ans = min(ans, cnt[i]); cout << k * ans; }
0
#include <bits/stdc++.h> using namespace std; int a[308050], flag[308050]; bool vis[308050]; struct node { int x, y; } b[308050]; vector<node> q[308050]; int dfs(int x) { int i, j, ans; vis[x] = 1; ans = a[x]; for (i = 0; i < q[x].size(); i++) { node v = q[x][i]; if (vis[v.x]) continue; if (dfs(v....
13
#include <bits/stdc++.h> template <typename T> inline T abs(T a) { return a < 0 ? -a : a; } template <typename T> inline T min(T a, T b) { return a < b ? a : b; } template <typename T> inline T max(T a, T b) { return a > b ? a : b; } template <typename T> inline void swap(T& a, T& b) { T t = a; a = b, b = t; ...
11
#include <bits/stdc++.h> using namespace std; int main() { unordered_map<string, int> m; set<int> nums; vector<int> ans; bool flag = true; int n, k, g; string s; cin >> n >> k >> s >> g; ans.resize(n); s += s; for (int i(0); i < g; i++) { string a; cin >> a; m.insert({a, i}); } int t...
15
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 1e6 + 7; int main() { long long n; scanf("%lld", &n); set<long long> s; s.insert(n); for (int i = 0; i < (int)1e6; i++) { if (n % 10 != 0) s.emplace(n); n++; if (n % 10 == 0) { while (n % 10 == 0) n /=...
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; if (x1 == x2 && y1 == y2) { cout << "0\n"; } else if (x1 == x2) { cout << abs(y2 - y1) << '...
0
#include <bits/stdc++.h> using namespace std; long long n, m, k, resp; long long comb[101][101]; long long comb1[101][101][2]; long long dp[101][10001]; long long pot(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a; long long tt = pot(a, b / 2); tt *= tt; tt %= 1000000007; if (b % 2 =...
11
#include <bits/stdc++.h> using namespace std; void find(string A, string B, int n) { int same0 = 0; int same1 = 0; int dif0 = 0; int dif1 = 0; if (A == B) { cout << "0\n"; return; } for (int i = 0; i < n; i++) { if (A[i] == B[i]) { if (A[i] == '0') { same0++; } else { ...
8
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(nam...
10
#include <bits/stdc++.h> using namespace std; const int prime = 999983; const int INF = 0x7FFFFFFF; const long long INFF = 0x7FFFFFFFFFFFFFFF; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-6; const int mod = 1e9 + 7; long long qpow(long long a, long long b) { long long s = 1; while (b...
18
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; const long long int mod = 1e9 + 7; const long long int inf = 2e9 + 100; long long int n, k, l, m; long long int tmp[2][2]; long long int Pw(long long int x, long long int n) { if (n == 0) return 1; long long int y = (x * x) % m; if (n % 2 == 0...
13
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)1 << 62; const long long MOD = 1e9 + 7; const int iINF = 1 << 30; const double PI = 3.14159265359; vector<vector<pair<int, int> > > target; vector<vector<char> > ans; vector<vector<bool> > vis; int n; int dx[] = {-1, 0, 1, 0}; int dy[] = {0,...
12
#include <bits/stdc++.h> using namespace std; char s[105][105], ans[105], temp[105]; int book[105]; int main() { int n, l, a, b; char c, *p; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", s[i]); l = strlen(s[i]); for (int j = 0; j < l; j++) { if (isupper(s[i][j])) s[i][j] = tolower...
8
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10; const double Pi = 3.1415926535897932; const int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1}; const int maxn = 2e5 + 5; struct Edge { int a, b, w; bool operator<(const Edge &oth) const { return w < oth.w; } } edges[maxn]; int n, m; int uset[maxn]; in...
10
#include <bits/stdc++.h> #pragma comment(linker, "/stack:100000000") using namespace std; long long M[2][2] = {{1, 1}, {1, 0}}; long long A[62][55][55]; long long B[55][55]; long long C[55][55]; long long MOD; const int m = 2; long long solve(long long n) { memset(A, 0, sizeof(A)); for (int(i) = 0; (i) < (m); (i)++...
16
#include <bits/stdc++.h> using namespace std; long long Mod = 1000000007; long long poww(long long a, long long b, long long mod) { long long res = 1; if (b < 0) b = (b % (mod - 1) + mod - 1) % (mod - 1); for (; b; b >>= 1, a = 1LL * a * a % mod) if (b & 1) res = 1LL * res * a % mod; return res; } long long...
14
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; inline int add(int a, int b) { if ((a += b) >= mod) a -= mod; return a; } inline int mult(int a, int b) { long long t = 1ll * a * b; if (t >= mod) t %= mod; return t; } int dp[210][210][16]; int n, K; vector<int> trans; int adding[16][2...
19
#include <bits/stdc++.h> using namespace std; typedef char* cstr; const int oo = (~0u) >> 1; const long long int ooll = (~0ull) >> 1; const double inf = 1e100; const double eps = 1e-8; const double pi = acos(-1.0); template <typename type> inline bool cmax(type& a, const type& b) { return a < b ? a = b, true : false;...
15
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int n, m, x, y, sz, a[N]; bool used[N]; vector<int> q[N]; inline bool f(int x, int y) { return binary_search(q[x].begin(), q[x].end(), y); } void dfs1(int v) { used[v] = true; sz++; for (int to : q[v]) { if (!used[to]) { dfs1(to); ...
18
#include <bits/stdc++.h> using namespace std; int p[105]; int main() { int n, k, ans = 0; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> p[i]; } int i = 1, j = n; while ((p[i] <= k || p[j] <= k) && (i < j)) { if (p[i] <= k) { ans++; i++; } if (p[j] <= k) { ans++; ...
0
#include <bits/stdc++.h> using namespace std; long long bit[4][1010][1010]; int n; void update(int x, int y, long long v, int id) { for (int i = x; i < n; i = (i | (i + 1))) for (int j = y; j < n; j = (j | (j + 1))) { bit[id][i][j] ^= v; } } void update(int x, int y, long long v) { if (!(y & 1) && !(x...
17
#include <bits/stdc++.h> using namespace std; template <class T> istream& operator>>(istream& in, vector<T>& Col) { for (auto& el : Col) in >> el; return in; } template <class T> T inline myAbs(T n) { return (n < 0) ? -n : n; } int main() { ios_base::sync_with_stdio(false), cin.tie(0); string answers[] = {"NO...
2
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } const int mod = 1000000000 + 9; int n; int m; int k; int power(int a, int b) { int c = 1; while (b) { if (b & 1) c = ((long long)c * a) % mod; a = ((long long)a * a) % mod; b /= 2; } return c; } ...
8
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline bool smin(T &a, const T &b) { return a > b ? a = b : a;...
20
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10, mod = 1e9 + 7; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; string pat = "abacaba"; while (t--) { string ans = ""; int n; string s; cin >> n >> s; for (int i = 0; i + 7...
7
#include <bits/stdc++.h> using namespace std; long long fastexp(long long a, long long b) { if (b == 0) return 1; if (b % 2 == 0) { long long x = fastexp(a, b / 2); return ((x % 1000000007ll) * (x % 1000000007ll)) % 1000000007ll; } else { return ((fastexp(a, b - 1ll) % 1000000007ll) * (a % 1000000007l...
7
#include <bits/stdc++.h> using namespace std; struct coord { int i, j; coord(int i_, int j_) : i(i_), j(j_){}; }; int main(int argc, char* argv[]) { int III, JJJ; int num_of_steps; cin >> III >> num_of_steps; int di_arr[4] = {0, 1, 0, -1}; int dj_arr[4] = {1, 0, -1, 0}; vector<int> di = vector<int>(di_a...
13
#include <bits/stdc++.h> using namespace std; const int N = 210; int t, n, k, a[N]; int main() { scanf("%d", &t); for (int tt = 1; tt <= t; tt++) { scanf("%d%d", &n, &k); for (int i = 1; i <= k; i++) scanf("%d", a + i); int mx = 0; for (int i = 1; i <= n; i++) { int tmp = 0x3f3f3f3f; for...
2
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); long long int arr[6000], brr[6000], crr[6000], ans[6000]; int main() { int test_case = 1; for (int cs = 1; cs <= test_case; cs++) { long long int n; scanf("%lld", &n); for (int i = 1; i <= ...
7
#include <bits/stdc++.h> using namespace std; mt19937_64 rng((unsigned long long)new char); const double eps = 1e-10; const int maxn = 500010; const long long INF = 1e8; const int mod = 1e9 + 7; long long a, b, c, l; inline long long c3(long long x) { return x * (x - 1) * (x - 2) / 6; } inline long long c2(long long x)...
13
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; template <class T> inline void chkmin(T &a, T b) { if (a > b) a = b; } template <class T> inline void chkmax(T &a, T b) { if (a < b) a = b; } long long GCD(long long a, long long b) { if (a < b) swap(a, b); while (b) { a %= b; ...
10
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int min = 0, max = 0; int sum = 0; for (int i = 0; i < s.length(); ++i) { if (s[i] == '+') ++sum; else --sum; if (sum > max) max = sum; if (sum < min) min = sum; } cout << (max - min) << endl; retu...
6
#include <bits/stdc++.h> using namespace std; vector<pair<int, pair<int, int> > > v, ans, temp; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; scanf("%d", &x), scanf("%d", &y); y = 1 - y; v.push_back(make_pair(x, make_pair(y, i))); } sort(v.begin(), v.end()); i...
9
#include <bits/stdc++.h> using namespace std; inline long long read() { char c = getchar(); long long x = 0; bool f = 0; for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); if (f) x = -x; return x; } long long lim = 1e8; vector<long long...
22
#include <bits/stdc++.h> using namespace std; char s[1010]; int n, ans; int I, A[1010], B[1010], cnt, fir[2010], nxt[100010], dis[100010], id; bool w[100010]; void link(int a, int b, int c) { nxt[++id] = fir[a], fir[a] = id, dis[id] = b, w[id] = c; nxt[++id] = fir[b], fir[b] = id, dis[id] = a, w[id] = c; } int p2[2...
16
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; vector<int> g[2][N]; int up[N], depth[N], val[N]; int tot_red; void dfs(int node, int pr = -1) { for (int i = 0; i < 2; ++i) { for (int &to : g[i][node]) { if (to == pr) continue; up[to] = up[node] + i; depth[to] = depth[node] ...
9
#include <bits/stdc++.h> using namespace std; void _print(long long t) { cout << t; } void _print(int t) { cout << t; } void _print(string t) { cout << t; } void _print(char t) { cout << t; } void _print(long double t) { cout << t; } void _print(double t) { cout << t; } void _print(unsigned long long t) { cout << t; } ...
0
#include <bits/stdc++.h> using namespace std; int main() { int tc; cin >> tc; while (tc--) { int m, n; cin >> m >> n; int a[m][n]; int b[m][n]; bool possible = true; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; b[i][j] = 4 - (i == 0) - (...
4
#include <bits/stdc++.h> using namespace std; char buf[1 << 21], *p1 = buf, *p2 = buf; template <class T> void read(T &x) { x = 0; int c = getchar(); int flag = 0; while (c < '0' || c > '9') flag |= (c == '-'), c = getchar(); while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); ...
23
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; const double PI = acos(-1.0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long mex(set<long long> &se) { long long c = 0; while (se.count(c)) c++; return c; } const long long N = 1e6 +...
14
#include <bits/stdc++.h> using namespace std; vector<int> a[105]; int main() { int n, m; scanf("%d%d", &n, &m); char c[n + 2][m + 2]; char w = 'W', b = 'B'; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> c[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if ...
4
#include <bits/stdc++.h> const int maxn = 1e5 + 51; const int mod1 = 1e9 + 7; const int mod2 = 1e9 + 9; std::vector<std::pair<int, int>> g[maxn]; int dfn[maxn], idx = 0; int f[maxn], fw[maxn]; int xr; std::vector<std::vector<int>> V; void dfs(int u, int fa) { dfn[u] = ++idx; for (auto x : g[u]) { int v = x.firs...
16
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } int main() { long long int a, b; cin >> a >> b; long long dasha(0), masha(0); long long freq = lcm(a, b); long long...
7
#include <bits/stdc++.h> using namespace std; long long n, m, q, l, r; long long arr[10000]; char s[10000], t[10000]; int main() { cin >> n >> m >> q; cin >> s + 1 >> t + 1; for (long long i = 1; i <= n - m + 1; i++) { int acc = 1; for (int j = 1; j <= m; j++) if (acc == 1 && s[i + j - 1] != t[j]) a...
5
#include <bits/stdc++.h> using namespace std; const int INF = int(1e9); const long long INFll = 1ll * INF * INF; const long double ldINF = 1e+018; const long double EPS = 0.000000001; const int base = INF; const int sdx[4] = {0, 1, 0, -1}; const int sdy[4] = {1, 0, -1, 0}; int main() { int n; cin >> n; vector<int...
4
#include <bits/stdc++.h> int n, m, a, b, i, j; int Min(int x, int y) { return x < y ? x : y; } int Max(int x, int y) { return x > y ? x : y; } int Abs(int x) { return x < 0 ? -x : x; } int cal(int x, int y) { if (x == i && y == j) return 0; int xx = Abs(x - i); int yy = Abs(y - j); if (xx % a != 0 || yy % b != ...
12
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); vector<int> b(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { cin >> b[i]; } ostringstream os; int r = 0; for (int i = 0; i < n; ++i) { if (a[i] == b[i]) continu...
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string word; cin >> word; string upd = word; bool f = false; while (true) { f = false; for (int i = int(0); i < word.size() - 1; i++) { if (word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == '...
0
#include <bits/stdc++.h> using namespace std; const int max_n = 300333, mod = 998244353; int mul(int x, int y) { return (1LL * x * y) % mod; } int power(int a, int b) { if (b == 0) { return 1 % mod; } if (b % 2 == 0) { return power(mul(a, a), b / 2); } return mul(a, power(a, b - 1)); } int inv(int x) ...
24
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; if (n == 0 && m > 0) { cout << "Impossible" << endl; return 0; } else if (n == 0 && m == 0) { cout << "0 0" << endl; return 0; } else if (n != 0 && m == 0) { cout << n << " " << n << endl; return 0; }...
3
#include <bits/stdc++.h> using namespace std; long long const mod = 1e9 + 7; long long power(long long x, long long y, long long mod = 2e18) { long long ans = 1; x %= mod; while (y) { if (y & 1) ans = (x * ans) % mod; x = (x * x) % mod; y >>= 1; } return ans; } long long gcd(long long a, long long...
3
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; inline void add(int &x, int y) { (x += y) >= mod ? x -= mod : 0; } inline int pl(int x, int y) { return (x += y) >= mod ? x -= mod : x; } inline int kpow(int a, int b) { int s = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) s = ...
23
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int n, s, m; int a[3000]; map<int, int> mq; int d[2005][2005]; pair<int, int> p[2005][2005]; vector<int> path; int check[3000]; int u[3000]; int mod(int x) { x %= n; return x >= 0 ? x : n + x; } int clock(int from, int to) { return mod(to...
18
#include <bits/stdc++.h> using namespace std; vector<int> g[228228]; bool used[228228]; long long cnt_v = 0, cnt_e = 0; void dfs(int v) { used[v] = true; cnt_v++; for (auto u : g[v]) { cnt_e++; if (!used[u]) { dfs(u); } } } int main() { int n, m; scanf("%d %d", &n, &m); vector<pair<int, ...
7
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 1; int n, m; int dp[N][18], lvl[N]; int lca(int a, int b) { if (lvl[a] < lvl[b]) swap(a, b); for (int i = 17; i >= 0; i--) if (lvl[dp[a][i]] >= lvl[b]) a = dp[a][i]; if (a == b) return a; for (int i = 17; i >= 0; i--) if (dp[a][i] != dp[b...
14
#include <bits/stdc++.h> using namespace std; long long n; string s = ""; queue<long long> good, bad; int main() { scanf("%lld", &n); cin >> s; for (long long i = 0; i < s.length(); i++) { if (s[i] == 'D') good.push(i); else bad.push(i); } long long pos = 0; while (good.size() != 0 && ba...
7
#include <bits/stdc++.h> using namespace std; void solve() { string p, h; cin >> p >> h; long long n = p.size(); string x, y; x = p, y = h; sort(x.begin(), x.end()); sort(y.begin(), y.end()); if (p.size() > h.size()) { cout << "NO\n"; return; } else if (x == y) { cout << "YES\n"; retur...
2
#include <bits/stdc++.h> using namespace std; const int N = 105; const int mo = 1000000007; int n; int a[N]; long long c[N][N], f[20][N]; void add(long long &x, long long y) { x = (x + y) % mo; } int main() { scanf("%d", &n); for (int i = 0; i < 10; ++i) scanf("%d", &a[i]); for (int i = 0; i <= n; ++i) { c[i]...
11
#include <bits/stdc++.h> using namespace std; int n, ans; char ch[100]; int main() { cin >> n; cin >> ch; cin >> ch; if (ch[0] == 'w') { n %= 7; for (int i = 1; i <= 366; i++) if (((i + 4) % 7) == n) ans++; } if (ch[0] == 'm') { if (n <= 29) ans = 12; if (n == 30) ans = 11; if (n =...
1
#include <bits/stdc++.h> using namespace std; int n, k; inline int lowbit(int x) { return x & -x; } void get_lian(int num) { printf("0 "); for (int i = 1; i <= num; i++) { printf("%d %d ", i * 2 - 1, i * 2 - 1); } } void get_binary(int root, int num) { for (int i = 2; i <= num; i++) { printf("%d ", i / ...
20
#include <bits/stdc++.h> using namespace std; const int N = 300005; long long b[N]; set<pair<int, pair<int, int> > > hillBySize; set<pair<int, int> > hills; void addHill(int a, int b) { hillBySize.insert( pair<int, pair<int, int> >(b - a + 1, pair<int, int>(a, b))); hills.insert(pair<int, int>(a, b)); } void ...
17
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; vector<long long> v; for (int r = 0; r < t; r++) { int n, k, m = INT_MAX, p, j; cin >> n >> k; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - k; i++) { p = (a[i + k] - a[i] + 1...
8
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5, OO = 0x3f3f3f3f; int n, m, cnt = 0; string s; map<string, int> mp; int main() { cin.tie(0); cin.sync_with_stdio(0); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s; mp[s]++; } for (int i = 0; i < m; i++) { cin >> s; i...
3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, n; cin >> n; while (n--) { cin >> a >> b >> c >> d; cout << b << " " << c << " " << c << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, i; cin >> n; vector<pair<int, int> > v; for (i = 0; i < n; i++) { cin >> a >> b; v.push_back(make_pair(a, b)); } sort(v.begin(), v.end()); int t = 0; for (i = 0; i < n; i++) { if (v[i].second >= t) t = v[i].second;...
6
#include <bits/stdc++.h> using namespace std; using lint = long long int; const lint INF = (lint)1e7; int main() { lint n, k; cin >> n >> k; ++n; if (k > (lint)1e7) { cout << "No"; return 0; } for (lint i = 2; i <= k; ++i) { if (n % i) { cout << "No"; return 0; } } cout << "Y...
8
#include <bits/stdc++.h> using namespace std; int i, j, l, n, a[301][301], f[601][301][301], ii, jj, ans = -1000000000; int add(int n, int i, int j) { if (i == j) return a[n - i][i]; else return a[n - i][i] + a[n - j][j]; } int main() { cin >> n; for (i = 0; i < n; i++) for (j = 0; j < n; j++) cin >...
12
#include <bits/stdc++.h> using namespace std; void solve() { int n; long long int W; cin >> n >> W; vector<pair<long long int, int>> w(n); for (int i = 0; i < n; i++) { cin >> w[i].first; w[i].second = i + 1; } sort(w.begin(), w.end(), [&](pair<long long int, int>& a, pair<long long int, in...
5
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long sum = 0; long long temp; for (int i = 0; i < n - 1; i++) { cin >> temp; sum += temp; } long long p = (n * (n + 1)) / 2; cout << p - sum; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, k; const int C = 200005; vector<int> tab[C]; vector<pair<int, int>> fools; int jump[C][20]; bool visited[C]; int pre[C]; int post[C]; int cnt = 0; unordered_map<long long, int> m; int res[C]; int l[C]; int travelers[C]; bool visted[C]; void dfs(int x) { visited[x] ...
11
#include <bits/stdc++.h> long long xmax, xmin; using namespace std; void union1(long long, long long, long long, long long); void union2(long long, long long); int main() { long long n, a, b, c, d, A, B, C, D, maxi, ans = 0; cin >> n >> a >> b >> c >> d; A = a + b; B = a + c; C = b + d; D = c + d; maxi = ...
6
#include <bits/stdc++.h> const long long T00B1G = 9223372036854775807LL; const int maskl = 5; bool mask[64][5]; template <class TEl> using TTable = std::vector<std::vector<TEl>>; template <class TEl> void spacecreator(long long first, long long second, TTable<TEl>& vec) { vec.resize(first); for (auto& sub : vec) { ...
19
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; void solve() { long long x, y; cin >> x >> y; if (x == y) { cout << x << endl; return; } if (y < x) { cout << x + y << endl; return; } if (y % x == 0) { cout << x << endl; return; } cout << y - (y % x) / 2...
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e3 + 10; const int mod = 998244353; char s[MAXN]; int n, bk[MAXN]; long long f[MAXN][MAXN]; long long qpow(long long x, long long k) { long long res = 1; while (k) { if (k & 1) res = res * x % mod; k >>= 1; x = x * x % mod; } return res...
18
#include <bits/stdc++.h> using namespace std; int main() { string st, st1; int n; scanf("%d", &n); if (n % 2 == 0) { for (int i = 0; i < n; i += 2) { if (i % 4 == 0) { st += "aa"; } else { st += "bb"; } } cout << st << endl; st1 = "c"; for (int i = 1; i < n ...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 100; const int inf = 0x7ffffff; char cc[] = "RBYG"; string a; int vis[10]; int ans[4]; int main(void) { cin >> a; int len = a.length(); int cnt = 0; for (int i = 0; i < len; i++) { if (a[i] == '!') cnt++; } for (int i = 0; i < 4; i++) ...
3
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int n; cin >> n; string ans = ""; long long int a[n], g[n]; long long int pa = 0, pb = 0; for (long long int i = 0; i < n; i++) { cin >> a[i] >> g[i]; long long in...
7
#include <bits/stdc++.h> using namespace std; int main() { int a, b, d; cin >> a >> b >> d; if (a == b) { cout << "YES"; return 0; } if (d == 0) { cout << "NO"; return 0; } if (b < a && d > 0) cout << "NO"; else if (b > a && d < 0) cout << "NO"; else { b -= a; if (b % d...
3
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int ans = 0; for (int(i) = (1); ...
1
#include <bits/stdc++.h> const int maxn = 2e5 + 5; struct Node { int p, m; } node[maxn]; inline long long read() { long long w = 0, t = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') t = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { w = (w << 3) + (w << 1) + (ch ^...
16
#include <bits/stdc++.h> using namespace std; template <typename T, typename V> void bugp(const pair<T, V> &x) { if (0) cerr << '{' << x.first << ", " << x.second << '}' << endl; } template <typename T, typename U, typename V> void bugpp(const pair<T, pair<U, V> > &x) { if (0) cerr << '{' << x.first << ", {" <<...
10
#include <bits/stdc++.h> using namespace std; int main() { int res, a, b, c, d, aa; scanf("%d%d%d", &a, &b, &c); a = a % b; res = 0; int tmp = 0; while (true) { ++tmp; if (tmp > b) { res = -1; break; } if (a == 0) { if (c != 0) { res = -1; } else ++res...
5