solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9; const long long mod = 1e9 + 7; const double eps = 1e-8; const long long MAX = 1e7 + 20; template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); } template <class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); } long l...
3
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 5; const int INF = 0x3f3f3f3f; int n, k, a[MAX], ans, now; void solve() { int t; scanf("%d", &t); while (t--) { ans = now = INF; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i + k <= n; +...
8
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long INF = 1e18; long long n, a[100005]; set<long long> ve[100005]; long long A[100005], sz[100005]; void init(long long n) { for (long long i = 0; i < n + 1; i++) { sz[i] = 1; A[i] = i; } } long long find(long long i...
7
#include <bits/stdc++.h> using namespace std; const int N = 3e3 + 5; const int mod = 998244353; int dp[N][N]; char x[N], s[N]; int main() { cin >> (x + 1) >> (s + 1); int lx = strlen(x + 1), ls = strlen(s + 1); for (int i = 1; i <= ls; i++) if (x[1] == s[i]) dp[i][i] = 2; for (int i = ls + 1; i <= lx; i++) ...
14
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; long long a[N], b[N], cnt; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; b[i] = a[i]; } sort(a, a + n); for (int i = 0; i < n; ++i) { if (a[i] != b[i]) cnt++; if (cnt > 2) { cout << "NO\n"; ...
5
#include <bits/stdc++.h> const int N = 407, M = 2407, inf = 40001; int s, t, tot = 1, a[N], head[N], ver[M], next[M], edge[M], cost[M], flow[N], dis[N], inq[N], id[N]; std::queue<int> q; char str[N]; int read() { int x; scanf("%d", &x); return x; } void add(int u, int v, int f, int c) { ver[++tot] = v...
21
#include <bits/stdc++.h> using namespace std; int n; string s; int ans1 = 0, ans2 = 0; int main() { cin >> n >> s; int p = 0; while (p < n) { if ((p % 2 == 0 && s[p] == '1') || (p % 2 == 1 && s[p] == '0')) if (p < n - 1 && s[p + 1] == s[p]) { ans1++; p++; } else ans1++; ...
10
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; long long a[n]; long long i, j; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (i = 0; i < n; i++) { long long y = 1; for (j = 0; j < 33; j++) { ...
10
#include <bits/stdc++.h> using namespace std; const int N = 2000 + 20; int n, m, cnt, cntp, deg[N][N], vis[N][N]; int dir[4][2] = {1, 0, 0, -1, -1, 0, 0, 1}; char money[4] = {'v', '<', '^', '>'}; char a[N][N]; struct node { int deg; int x, y; node(int a, int b, int t) { x = a, y = b, deg = t; } bool operator<(s...
12
#include <bits/stdc++.h> using namespace std; int main() { int c = 0; long long n, x; cin >> n >> x; for (int i = 1; i <= n; i++) { if (x % i == 0 && x / i <= n) c++; } printf("%d", c); return 0; }
2
#include <bits/stdc++.h> using namespace std; struct node { int to, val; node(int a, int b) : to(a), val(b) {} }; vector<node> edge[300000 + 5]; int mark[300000 + 5], Next[300000 * 2 + 5][26], size[300000 + 5], dep[300000 + 5], ans[300000 + 5]; int n, cnt; int Merge(int x, int y) { if (x == 0 || y == 0) retur...
17
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { long long int a, b; cin >> a >> b; if (a > b) { long long int d = a - b; if (d % 2 == 0) cout << "1" << endl; else ...
0
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (n); i++) cin >> a[i]; long long cnt = 0; for (int i = n - 1; i > 0; i--) { if (a[i] < a[i - 1]) cnt += a[i - 1] - a[i]; } cout << cnt << endl; } int main() { int t; cin >> t; fo...
4
#include <bits/stdc++.h> using namespace std; using ll = long long; int32_t main() { int n; cin >> n; vector<int> p(n + 1); set<int> s; set<int> numbers; for (int i = 1; i <= n; i++) { s.insert(i); numbers.insert(i); } int x, y; while (s.size() >= 2) { auto u = s.begin(); auto v = (nex...
8
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; vector<long long> t(n); map<long long, long long> mapa; long long sum = 0; for (long long& e : t) { cin >> e; mapa[e]++; sum += e; } long long wynik = 0; for (long long e : t) { if ((2 * sum) % n == 0) ...
4
#include <bits/stdc++.h> using namespace std; signed main() { long long t = 1; while (t--) { long long n, ans = 0; cin >> n; vector<long long> a(n), cnt(20); for (auto &e : a) cin >> e; for (long long i = 0; i < n; i++) for (long long j = 0; j < 20; j++) if (a[i] & (1 << j)) cnt[j]...
9
#include <bits/stdc++.h> using namespace std; int32_t main() { long long int n, m; cin >> m >> n; if (m > n || (m % 2 != n % 2)) cout << -1 << endl; else if (n == m) { if (n == 0) cout << 0 << endl; else cout << 1 << endl << n << endl; } else { long long int x = (n - m) / 2; if...
9
#include <bits/stdc++.h> using namespace std; struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; long long int gcd(long long int a, long long int b) { if (b ...
6
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 10; const int mod = 1e9 + 7; int gi() { int x = 0, o = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') o = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar...
21
#include <bits/stdc++.h> using namespace std; const int NMAX = 2014; long long d1[2 * NMAX], d2[2 * NMAX], sol[2]; pair<int, int> v[2]; int a[NMAX][NMAX]; inline void Update(const int c, const int i, const int j, const long long val) { if (val > sol[c]) { sol[c] = val; v[c].first = i; v[c].second = j; }...
11
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y = -1, i = 0; long long k, cnt = 0; cin >> n >> k; while (i < n) { cin >> x; if (y < x) { if (i != 0) cnt = 1; y = x; } else cnt++; if (cnt == k) break; i++; } cout << y; return 0; }
4
#include <bits/stdc++.h> using namespace std; namespace io { const int L = (1 << 20) + 1; char buf[L], *S, *T, c; char getchar() { if (__builtin_expect(S == T, 0)) { T = (S = buf) + fread(buf, 1, L, stdin); return (S == T ? EOF : *S++); } return *S++; } int inp() { int x = 0, f = 1; char ch; for (ch...
20
#include <bits/stdc++.h> using namespace std; vector<int> sm; vector<vector<int> > g; int dfs(int v, int pr = -1, int h = 0) { int sz = 1; sm[v] = h; for (int i = 0; i < g[v].size(); i++) { if (g[v][i] == pr) continue; int k = dfs(g[v][i], v, h + 1); sz += k; sm[v] -= k; } return sz; } int mai...
8
#include <bits/stdc++.h> using namespace std; int a, b, c, n, x, y, z; int main() { string s1, s2; cin >> s1 >> s2; vector<string> v; if (s1.size() != s2.size()) { cout << "NO" << endl; } else { int def = 0; for (int i = 0; i < s1.size(); i++) { if (i < s2.size() && s1[i] != s2[i]) { ...
3
#include <bits/stdc++.h> using namespace std; const int N = 505; int a[N], g[N][N]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1, u, v, w; i <= m; i++) { scanf("%d%d%d", &u, &v, &w); g[u][v] = g[v][u] = w; } double ans = 0.0;...
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 2 * 1e5 + 5; pair<int, int> tsh[MAXN]; int ans[MAXN]; bool cmp(pair<int, int> A, pair<int, int> B) { if (A.first != B.first) return A.first > B.first; return A.second < B.second; } struct node; typedef node* Treap; struct node { int prio; int val, b...
20
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; void solve() { long long a, b, c, d; cin >> a >> b >> c >> d; if (a == 0 && d == 0) { if (b % 2) { cout << "Tidak Ya Tidak Tidak\n"; } else cout << "Tidak Tidak Ya Tidak\n"; return; } if (b == 0 && c == 0) { ...
5
#include <bits/stdc++.h> using namespace std; typedef struct node { int vis, l, r; } s; long long a[200005]; s f[200005]; int main() { long long n, k, a, c; cin >> n >> k; c = 3 * n - 2; for (long long i = 0; i < k; i++) { cin >> a; if (!f[a].vis) { f[a].vis = 1; c--; } if (a > 1 &...
7
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int N = 1 << 23, D = 1e6; int tr[N * 2], mod[N * 2]; void push(int v) { if (v < N) { mod[v * 2] += mod[v]; mod[v * 2 + 1] += mod[v]; } tr[v] += mod[v]; mod[v] = 0; } void rel(int v) { tr[v] = max(tr[v * 2],...
14
#include <bits/stdc++.h> using namespace std; const int N = 105; int n; bool used[30][6]; int found[N], type[N]; bool subtracted[N]; int remain[10], org_remain[10]; vector<pair<char, char> > card; map<char, int> mp; string s; vector<char> v; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> s; if ...
9
#include <bits/stdc++.h> using namespace std; int dp[2][(int)(571)][(int)(571)], p[(int)(571)], n, k; int main() { cin >> n >> k; p[0] = 1; for (int i = 1; i <= 500; i++) p[i] = (2 * p[i - 1]) % 1000000007; for (int i = 0; i <= n; i++) dp[(n + 1) & 1][k][i] = 1; for (int x = n; x >= 1; x--) for (int count...
17
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, cont = 0; cin >> n; for (int i = 1; i <= n - 1; i++) { int tmp = n - i; if (tmp % i == 0) cont++; } cout << cont << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int x, y; int max1, min1; if (m == 0) { cout << (n - 1); return 0; } max1 = 0; min1 = 10000000; for (int i = 0; i < m; i++) { cin >> x >> y; if (x > y) swap(x, y); max1 = max(max1, x); min1 = mi...
5
#include <bits/stdc++.h> using namespace std; int main() { int a, b, n, i; while (cin >> a >> b >> n) { for (i = -1000; i <= 1000; i++) { if (a * pow(i, n) == b) break; } if (a * pow(i, n) == b) cout << i << endl; else cout << "No solution\n"; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long xa, ya, xb, yb, xc, yc; cin >> xa >> ya >> xb >> yb >> xc >> yc; long long firstX = xb - xa; long long firstY = yb - ya; long long secondX = xc - xb; long long secondY = yc - yb; long long res = (firstX * secondY) - (secondX * firstY); ...
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; t = 1; double pi = 2 * acos(0.0); while (t--) { long long n, r; cin >> n >> r; double x = sin((pi) / n); cout << setprecision(8) << r * x / (1 - x); } return 0...
4
#include <bits/stdc++.h> using namespace std; struct node { int suit, val; }; node nodes[55]; char s[55]; int dp[55][5][14][5][14][5][14], n; int cal(int ind, int suit1, int val1, int suit2, int val2, int suit3, int val3); int main() { int i, j, k, l; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%s", ...
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int n, N; cin >> n; N = n / 2; long long int a[n + 1], b[N + 1]; for (int i = 1; i <= N; i++) cin >> b[i]; int l, r; long long int mn, mx; l = N; r = n - N + 1; mn = b[N] / 2...
5
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; long double EPS = 1e-9; long long MOD2 = 998244353; long long binpow(long long b, long long p, long long mod) { long long ans = 1; b %= mod; for (; p; p >>= 1) { if (p & 1) ans = ans * b % mod; b = b * b % mod; } return ans; } l...
9
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, i, j; string s; cin >> n; cin >> s; long long int pre = 0, ans = 0; map<long long int, long long int> m; for (i = 0; i < n; ++i) { m[pre]++; pre += (s[i] - '1'); ans += m[pre]; } cout << ans << '\n'; } void preCo...
8
#include <bits/stdc++.h> const int inf = (1ll << 30) - 1; const int maxn = (int)1e5 + 10; using namespace std; int n, k, x; int a[5050]; bool dp[5050][5050]; bool u[5050][5050]; bool get(int p, int g) { if (g == x % k) return true; if (p == 0) return false; if (u[p][g]) return dp[p][g]; u[p][g] = 1; dp[p][g] ...
16
#include <bits/stdc++.h> using namespace std; int a[30], b[5]; long long s[(1 << 24) + 10], d[(1 << 24) + 10]; int n, k; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); scanf("%d", &k); for (int i = 0; i < k; ++i) scanf("%d", &b[i]); for (int i = 0; i < n; ++i) s[(1 << i)] = a[i]...
15
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int tst, a, b, c, r; cin >> tst; for (int w = 1; w <= tst; ++w) { cin >> a >> b >> c >> r; int aux; if (b < a) aux = a, a = b, b = aux; int xa = c - r, xb = c + r; int inte...
1
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, icon = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') icon = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); } return x * icon; } int _abs(int x) { retu...
11
#include <bits/stdc++.h> using namespace std; struct Modint { int val; Modint(int a = 0) { val = a; } Modint& operator+=(Modint r) { val = (val + r.val) % 1000000007; return *this; } Modint& operator-=(Modint r) { val = (val + 1000000007 - r.val) % 1000000007; return *this; } Modint& opera...
16
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx,tune=native") using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::e...
12
#include <bits/stdc++.h> using namespace std; vector<int> adj[200001]; int ans[200001]; bool vis[200001]; void filll(int in, long long val) { if (ans[in] != -1) return; ans[in] = val; for (auto el : adj[in]) { filll(el, val); } } int is_cycle(int in) { if (ans[in] != -1) return ans[in]; int res = 1; v...
6
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; cout << m / n + (m % n > 0) << endl; }
2
#include <bits/stdc++.h> using namespace std; const long long int INF = 1e18; int n, a, b, x, t[105]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> t[i]; for (int i = 1; i <= n; i++) { cin >> x; if (!x && t[i]) b++; if (x && !t[i]) a++; } if (!b) cout << -1; else cout << a /...
1
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f, maxn = 100001; int n, d, a[maxn]; long long c(int n) { return (long long)n * (n - 1) / 2; } int main() { while (~scanf("%d%d", &n, &d)) { for (int i = 0; i < n; i++) scanf("%d", &a[i]); long long ans = 0; for (int i = 0; i < n; i++)...
5
#include <bits/stdc++.h> using namespace std; int n, top, zhan[500001]; long long ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); while (top > 1 && zhan[top - 1] >= zhan[top] && zhan[top] <= x) { ans += min(zhan[top - 1], x); top--; } zhan[++...
17
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double EPS = 1e-9; double fRand(double fMin, double fMax) { double f = (double)rand() / RAND_MAX; return fMin + f * (fMax - fMin); } template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); } template <class T> T max(T a,...
21
#include <bits/stdc++.h> using namespace std; int v[100000 + 1]; int main() { int n, x, y, l, i, minim, i1; scanf("%d%d%d", &n, &x, &y); for (i = 0; i < n; i++) scanf("%d", &v[i]); for (i = 0; i < n; i++) { minim = v[i]; for (i1 = i; i1 >= 0 && i1 >= i - x; i1--) minim = min(v[i1], minim); for (i1 =...
2
#include <bits/stdc++.h> using namespace std; int main() { int x, y; cin >> x >> y; if (x == 0 && y != 0) { cout << "Impossible" << endl; return 0; } (y >= x) ? cout << y << " " : cout << x << " "; (y != 0) ? cout << y - 1 + x << endl : cout << x << endl; }
3
#include <bits/stdc++.h> using namespace std; int s = 0; string a, b; int v[200005]; bool ok(int k) { string ac = a; for (int i = 0; i < k; i++) { ac[v[i]] = '*'; } int r, poz = 0; for (int i = 0; i < b.length(); i++) { r = ac.find(b[i], poz); if (r == -1) return false; poz = r + 1; } retu...
9
#include <bits/stdc++.h> using namespace std; template <typename A> ostream& operator<<(ostream& cout, vector<A> const& v); template <typename A, typename B> ostream& operator<<(ostream& cout, pair<A, B> const& p) { return cout << "(" << p.first << ", " << p.second << ")"; } template <typename A> ostream& operator<<(...
16
#include <bits/stdc++.h> using namespace std; int main() { long long n, s = 1, t = 2; cin >> n; while (n) { if (n % 2) s = (s * t) % 1000000007; n >>= 1; t = (t * t) % 1000000007; } cout << (s * (s + 1) / 2) % 1000000007; }
5
#include <bits/stdc++.h> using namespace std; int n, m; int a[253][253]; int b[253][253]; int p[253 * 253]; int px[253], py[253]; int vx[253], vy[253]; const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; bool valid(int i, int j) { if (i < 0 || i >= n || j < 0 || j >= m) return false; if (b[i][j]) retu...
20
#include <bits/stdc++.h> using namespace std; const int MAX_N = 100005; int main() { cin.tie(0); ios::sync_with_stdio(false); int q; cin >> q; for (int i = 0; i < (int)(q); ++i) { int n; cin >> n; if (n % 4 == 0) { cout << n / 4 << "\n"; } else if (n % 4 == 1) { if (n >= 9) { ...
5
#include <bits/stdc++.h> int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); int number; std::cin >> number; std::vector<int64_t> diff2(number); auto const diffAdd = [&](int index, int64_t value) { if (index >= (int)diff2.size()) return; diff2[index] += value; }; auto const add = [&](int...
11
#include <bits/stdc++.h> using namespace std; long long int dfs(long long int x, long long int y, long long int i, vector<pair<long long int, long long int>> heykel) { if (x < 0 || x > 7 || y < 0 || y > 7) return 0; for (auto &it : heykel) if ((it.first++ == x || it.first == x) && it.second ==...
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long c(0); while (n--) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; c += (x2 - x1 + 1) * (y2 - y1 + 1); } cout << c; }
2
#include <bits/stdc++.h> using namespace std; template <typename Tp> inline void outarr(Tp _array, const std::size_t _N, const char* _delim = " ") { Tp end = std::next(_array, _N); for (Tp current = _array; current != end; ++current) { std::cout << *current << _delim; } std::cout << '\n'; } using ll = int64...
12
#include <bits/stdc++.h> using namespace std; long long n, rating; long long level[30008], devote[30008]; long long p[30008]; long long cmp(long long x, long long y) { if (devote[x] >= 0 and devote[y] < 0) return 1; if (devote[x] < 0 and devote[y] >= 0) return 0; if (devote[x] >= 0 and devote[y] >= 0) { retur...
13
#include <bits/stdc++.h> using namespace std; const int base = 1000 * 1000 * 1000; const int N = (int)2e5 + 2; const int INF = (int)1e9 + 7; const long double pi = acos(-1); const long long linf = (long long)1e18; long long n; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; long long ...
4
#include <bits/stdc++.h> using namespace std; char s[1000100]; struct sp { int used; int n; int pos[10010]; } m[26]; int main() { int i, j, r, p, l, curr; char ch; for (i = 0; i < 26; i++) { m[i].used = 0; m[i].n = 0; } for (l = 0;; l++) { ch = getchar(); if (ch == '\n') break; m[ch ...
7
#include <bits/stdc++.h> using namespace std; int main() { string k, p; char q, s; long long a, b, c[1000000], d = 0, e = 0, f = 0, g = 0, h, i, j, l, m, n, o, r, t, u, v, w, x, y, z; cin >> a >> b; for (i = 1; i <= b; i++) { cin >> c[i]; } for (i = 1; i < b; i++) { d...
2
#include <bits/stdc++.h> const int N = 1e5 + 5; const int mod = 1e9 + 7; const long long int int_max = 1e18; const long long int int_min = -1e18; using namespace std; long long int power(long long int x, long long int y) { long long int temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) ret...
7
#include <bits/stdc++.h> using namespace std; template <class S, class T> ostream& operator<<(ostream& os, const pair<S, T>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> void debug(T a, T b) { cerr << "["; for (T i = a; i != b; ++i) { if (i != a) cerr << ", "; cerr << ...
6
#include <bits/stdc++.h> using namespace std; const unsigned long long int MOD = 1e9 + 7; void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } unsigned long long int POW(unsigned long long int p, int b) { unsigned long long int ans = 1LL; while (b > 0) { if (b & 1) { ans = ans * 1LL * p;...
12