solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long int q, m, a, b, c, d, i, j; cin >> q; while (q--) { cin >> m >> a >> b >> c >> d; long long int mini = c - d; long long int maxi = c + d; long long int l = a - b;...
1
#include <bits/stdc++.h> using namespace std; int dis[200010], lle[200010]; vector<int> adj[200010]; void dfs(int x, int f, int ar[]) { for (int i = 0; i < adj[x].size(); i++) { int v = adj[x][i]; if (v != f) { ar[v] = ar[x] + 1; dfs(v, x, ar); } } } int main() { int n, x, a, b, m = 0; c...
9
#include <bits/stdc++.h> using namespace std; const int N = 2010, M = 1010, INF = 0x3f3f3f3f, mod = 1e9 + 7; const double eps = 1e-8; using namespace std; int dx2[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; int dy2[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; int dx[] = {1, -1, 0, 0, 0, 0}; int dy[] = {0, 0, 1, -1, 0, 0}; int dz[] = {0, 0,...
0
#include <bits/stdc++.h> using namespace std; long long a[10005000], jud[10000050]; int main() { long long n, t, flag, x, head = 1, y, num, head1, pre = -1, wei, dir; cin >> n >> t; for (long long i = 2000001; i <= 2000000 + n; i++) a[i] = 1; wei = n + 2000000; dir = 1; head = 2000001; while (t--) { s...
14
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); } template <class T1, class T2> bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } double vol(double l, int n) { const double pi = acos(-1); double theta = pi / n; doubl...
9
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; static const int maxn = 300010; static const int INF = 0x3f3f3f3f; static const int mod = (int)1e9 + 7; static const double eps = 1e-6; static const double pi = acos(-1); void redirect(){ #ifdef LOCAL freope...
10
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-10; const double PI = acos(-1); template <class T, class S> inline void add(T &a, S b) { a += b; if (a >= mod) a -= mod; } templat...
17
#include <bits/stdc++.h> using namespace std; int main() { int t, n, j, k, l; cin >> t; for (int i = 0; i < t; i++) { cin >> n; int a[n]; int b[n]; int c[n]; for (j = 0; j < n; j++) cin >> a[j]; for (k = 0; k < n; k++) cin >> b[k]; for (l = 0; l < n; l++) cin >> c[l]; for (j = 0; j...
0
#include <bits/stdc++.h> using namespace std; long long arr[30][30]; long long cntr, k; int n, m; vector<map<long long, int>> v; void f(int x, int y, long long val) { val ^= arr[x][y]; if (x + y == n - 1) { v[x][val]++; return; } if (x < n) f(x + 1, y, val); if (y < m) f(x, y + 1, val); } void f1(int ...
13
#include <bits/stdc++.h> using namespace std; long long ni[] = {1, 4, 10, 20, 35, 56, 83, 116, 155, 198, 244, 292, 341}; int main() { long long n; cin >> n; if (n <= 12) { cout << ni[n] << endl; } else { cout << ni[12] + (n - 12) * 49 << endl; } return 0; }
12
#include <bits/stdc++.h> using namespace std; long long n, k; inline long long conv(long long i) { while (i < 0) i += n; while (i >= n) i -= n; return i; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> n >> k >> s; long long idx = -1; for (long long i = 0; i < n; i++) { ...
15
#include <bits/stdc++.h> using namespace std; int main() { long long n, q; cin >> n >> q; for (int i = 1; i <= q; i++) { long long x; cin >> x; if (x % 2LL == 1LL) { cout << (x / 2LL + 1) << endl; } else { long long a, b; a = n; b = x; while (!(b % 2LL)) { a =...
9
#include <bits/stdc++.h> int t, n, m, ans; int main() { int i; for (scanf("%d", &t); t > 0; t--) { scanf("%d%d", &n, &m); for (i = 1; i <= m && i * i <= n; i++) if (!(n % i)) if (n / i > m) ans = n / i; else { ans = i; break; } printf("%d\n", a...
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a, b = 0, c = 0; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a); if (a % 2 == 0) { b++; } else c++; } int k = n - m; int l = min(b, c); if (k == 0) { if (c % 2 == 0) { printf("Dae...
14
#include <bits/stdc++.h> using namespace std; int a[200000], b[200000], f[200000]; int main() { int u, g = -1, j = 0, n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n - 1; i++) if (a[i] != a[i + 1]) { j++; b[j] = a[i]; } j++; b[j] = a[n]; for (int i...
8
#include <bits/stdc++.h> using namespace std; inline void read(long long &x) { char ch; bool flag = false; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') flag = true; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; x = flag ? -x : x; } inline void write(long long x...
13
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; if (k < 2 * n) { cout << "NO" << endl; return 0; } else { cout << "YES" << endl; for (int i = 0; i < n - 1; i++) cout << 1 << " "; cout << k - n + 1 << endl; cout << n << endl; } }
6
#include <bits/stdc++.h> long long n, m; long long val[100005]; long long mx[100005 << 2], sum[100005 << 2]; long long getint() { long long x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == '-', ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); return x;...
15
#include <bits/stdc++.h> int main() { using namespace std; int n, a, b; cin >> n; int z[n]; for (int i = 0; i < n; i++) { z[i] = 0; } for (int i = 0; i < n - 1; i++) { cin >> a >> b; z[a - 1]++; z[b - 1]++; } int sum = 0; for (int i = 0; i < n; i++) { sum += (z[i] * (z[i] - 1)) /...
5
#include <bits/stdc++.h> using namespace std; char str[105][105]; int n, m, k; int gwin, rwin; vector<int> num; int dp[105]; void get() { gwin = -1; rwin = -1; for (int i = 1; i <= n; i++) { int R = -1; int G = -1; int has = 0; for (int j = 0; j < m; j++) { if (str[i][j] == 'G') G = ...
18
#include <bits/stdc++.h> const int MAXN = 100031; int n = 0; bool ip[MAXN] = {1, 1}; int a[MAXN], p[MAXN]; void sieve() { for (int i = 2, c = 0; i < MAXN; i++) { if (!ip[i]) { p[c++] = i; } for (int j = 0; j < c && i * p[j] < MAXN; j++) { ip[i * p[j]] = 1; if (!(i % p[j])) { brea...
15
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double e = exp(1.0); const long long M = 1e9 + 7; const long long N = 2e5 + 7; struct node { long long da, id; } a[N]; long long n, w; long long sum, flag, idx, l, r; bool cmp(node a, node b) { return a.da < b.da; } void solve() { cin...
5
#include <bits/stdc++.h> using namespace std; const int N = 300 + 5; int n, m, ok, a[N], used[N]; inline bool read() { if (scanf("%d %d %d", &n, &m, &ok) != 3) return false; for (int i = 0; i < ok; i++) scanf("%d", &a[i]), a[i]--; sort(a, a + ok); return true; } vector<int> g[N]; vector<pair<int, int> > edges; ...
14
#include <bits/stdc++.h> using namespace std; const long long N = 5e5 + 10; long long n, r, k, psum[N], ar[N], a[N], v[N]; bool check(long long x) { for (long long i = 0; i < n; i++) { a[i] = ar[i]; v[i] = 0; } long long add = 0, rem = k; for (long long i = 0; i < n; i++) { add -= v[i]; a[i] += ...
12
#include<iostream> #include<iomanip> #include<cstdio> #include<vector> #include<cmath> #include<string> #include<cstring> #include<algorithm> #include<unordered_map> #include<unordered_set> #include<queue> #include<map> #include<set> #define endl '\n' #define all(s) s.begin(),s.end() #define lowbit(x) (x&-x) #define r...
11
#include <bits/stdc++.h> using namespace std; template <class T> bool mini(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool maxi(T &a, T b) { return a < b ? (a = b, true) : false; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; ...
5
#include <bits/stdc++.h> using namespace std; int main() { int N, K, D, C, P = 0; cin >> N >> K >> C; D = K; int* A = new int[C]; for (int i = 0; i < C; i++) cin >> A[i]; for (int i = 1; i <= N; i++) { D--; if (D == 0) { P++; D = K; } else { for (int j = 0; j < C; j++) ...
5
#include <bits/stdc++.h> using namespace std; int n; long long m, lo, hi, a[100005], b[100005], A; long long doit(long long x) { long long r = 0; for (int i = (0); i < (n); i++) { if (!a[i]) { r++; continue; } if (x > m * b[i] / a[i] + b[i]) return m + 1; r += 1 + x * a[i] / b[i]; if...
11
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k, r, c = 0; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; if (k % 2 == 0 && a[i] == k / 2) r = (c++) % 2; else if (a[i] > k / 2) r = 1; el...
3
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) x = x * 10 + (c ^ '0'), c = getchar(); return x; } const int maxn = 3e5 + 10; const int maxq = 5e5 + 10; struct Node { int s[2], p, siz, v1, sum, v2, _s, tag...
19
#include <bits/stdc++.h> using namespace std; constexpr long long MOD = 1e9 + 7; int F(pair<int, pair<int, int>>& u) { return u.second.first; } int S(pair<int, pair<int, int>>& u) { return u.second.second; } int D(pair<int, pair<int, int>>& u) { return u.first; } int main() { int n; scanf("%d", &n); vector<pair<i...
12
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que; int t, n, m, k; unordered_set<int> se[100010]; vector<int> ans; bool vis[100010]; void init() { ans.clear(); for (int i = 1; i <= n;...
18
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; long long n, m, k; long long p; long long ans; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > Q; long long A[N], H[N]; long long d[N]; bool check(long long hist) { while (!Q.empty()) Q.pop(); ...
21
#include <bits/stdc++.h> using namespace std; void smain(); int main() { ios_base::sync_with_stdio(0); smain(); return 0; } long long n; long long d[26][5005][26]; void smain() { string s; for (; cin >> s;) { n = s.length(); for (long long i = 0; i < n; i++) { rotate(s.begin(), s.begin() + 1, s....
8
#include <bits/stdc++.h> using namespace std; map<pair<int, int>, pair<int, int> > mp; map<pair<int, int>, pair<int, int> >::iterator it; bool check(int a, int b, int x, int y, int x1, int y1) { if ((x < a || (x == a && y < b)) && (x1 > a || (x1 == a && y1 > b))) return true; else return false; } int main()...
8
#include <bits/stdc++.h> int map[15][15], r, c, n, k; int lowbit(int x) { return x & (-x); } void add(int x, int y, int num) { int i, j; for (i = x; i < 12; i += lowbit(i)) for (j = y; j < 12; j += lowbit(j)) { map[i][j] += num; if (map[i][j] < 0) map[i][j] = 0; } } int sum(int x, int y) { int...
3
#include <bits/stdc++.h> using namespace std; bool cmp(string s, string s2) { return (s + s2 < s2 + s); } vector<string> v; string s, inp; long long n; int main() { cin >> n; for (long long i = 0; i < n; i++) { cin >> inp; v.push_back(inp); } sort(v.begin(), v.end(), cmp); for (long long i = 0; i < v....
9
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4 + 8; int n; long long c; long long p[maxn], s; long long a[maxn]; long long res = 0; long long ans = 0x7f7f7f7f; long long con; signed main() { scanf("%d%lld", &n, &c); for (int i = (1); i <= (n); i++) scanf("%lld", &p[i]), res += p[i]; for (int i...
21
// time-limit: 2000 #include<bits/stdc++.h> #define int long long using namespace std; void testCase(); signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T = 1, t = 0; cin >> T; while (t++ < T) { testCase(); } return 0; } void testCase() { vector<int> a(4); for (int& x : a) ci...
0
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << "\n"; err(++it, args...); } const long long int MOD = 1e9 + 7; const long long int INF = 1e18; const ...
3
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int max_n = 200005; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long n, k; cin >> n >> k; string s; auto check = [&](long long l, long long r) -> bool { cout << l << " " << r << endl; cout.flush(...
13
#include <bits/stdc++.h> #define endl "\n" #define ll long long int #define vi vector<int> #define vll vector<ll> #define vvi vector < vi > #define pii pair<int,int> #define pll pair<long long, long long> #define mod 1000000007 #define inf 1000000000000000001; #define all(c) c.begin(),c.end() #define mp(x,y) make_pair...
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n, k; cin >> n >> k; int ce = (n + 1) / 2; int fl = n / 2; int cnt = (ce * ce) + (fl * fl); if (k <= cnt) { cout << "YES" << "\n"; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ...
6
#include <bits/stdc++.h> using namespace std; long long arr[100010]; int main() { cin.sync_with_stdio(false); long long P, N, K; cin >> N >> K >> P; multiset<long long> S; for (int i = 0; i < N; ++i) cin >> arr[i]; long long i = N - 2; long long sum = 0; long long mi = N; while (i >= 0 && K > 0) { ...
9
#include <bits/stdc++.h> using namespace std; unsigned long long mod = 998244353; string a[100005]; multiset<unsigned long long> s; map<unsigned long long, unsigned long long> mp; int main() { unsigned long long n; cin >> n; unsigned long long ans = 0; for (int i = 0; i < n; i++) { cin >> a[i]; mp[a[i]....
10
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; const int N = 1e6; struct Segtree { int n; vector<long long> st; const long long default_value = 0; long long op(long long l, long long r) { return l + r; } void build() { for (int i = n ...
11
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2; getline(cin, s1, char(10)); getline(cin, s2, char(10)); int n1 = s1.length(); int n2 = s2.length(); int e1 = 0; int e2 = 0; for (int i = 0; i < n1; i++) e1 += (int(s1[i] - '0')); for (int i = 0; i < n2; i++) e2 += (int(s2[i] - '...
9
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int n, m, res; vector<int> s[maxn]; int fa[maxn], num[maxn]; int find(int x); inline void aunion(int x, int y); void judge() { for (int i = 1; i <= n; i++) printf("[%d] ", fa[i]); printf("\n"); } int main() { scanf("%d%d", &n, &m); memset(fa...
11
#include <bits/stdc++.h> using namespace std; int main() { char s[101]; int n; cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; } int counter = 0; for (int j = 0; j < n; j++) { if (s[j] == '1') counter++; else break; } if (counter == n) cout << counter << endl; else ...
1
#include <bits/stdc++.h> using namespace std; int n; struct unu { int y, x1, x2; }; struct doi { int x, y1, y2; }; vector<unu> o; vector<doi> v; bool cmp(unu a, unu b) { return a.y < b.y; } bool cmp2(doi a, doi b) { return a.y2 < b.y2; } long long ans, nrd; int aib[100005]; int sum(int x) { int ans = 0; while (...
14
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } template <typename T> i...
21
#include <bits/stdc++.h> using namespace std; int compare(const void* a, const void* b) { return (*(int*)a - *(int*)b); } struct index { int value; vector<int> pos; }; struct node { int start, end; long long int max_sum; }; struct answer { bool is_valid; long long int max_sum; }; struct node segment_tree[40...
6
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5, inf = 0x3fffffff; struct Edge { int nxt, to, len; } e[maxn << 1]; int hd[maxn], e_cnt = 1; void add(int u, int v, int l) { e[++e_cnt] = (Edge){hd[u], v, l}; hd[u] = e_cnt; } int n, m, d1[maxn], d2[maxn], pre[maxn]; int main() { ios::sync_wi...
14
#include <bits/stdc++.h> using namespace std; const long long int mod2 = 998244353; long long int invmod(long long int a, unsigned long long int b, long long int n) { long long int x = 1, y = a; while (b > 0) { if (b % 2 == 1) x = (x * y) % n; y = (y * y) % n; b /= 2; } return (...
9
#include <bits/stdc++.h> using namespace std; void printprecise(double l, long long int precision) { std::cout << std::fixed; std::cout << std::setprecision(precision); std::cout << l; } long long int ncr(long long int n, long long int k) { long long int C[n + 1][k + 1]; long long int i, j; for (i = 0; i <=...
8
#include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; const long long MX = 2e5 + 5; const long long INF = 1e18; const long double PI = 4 * atan((long double)1); long long power_md(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res = (res % md * a % md) % md; a = ...
11
#include <bits/stdc++.h> using namespace std; const int Maxn = 505; const int Maxl = 26; const int Inf = 1000000000; int n, m; char B[Maxn][Maxn]; int l[Maxl], r[Maxl]; int res[Maxn][Maxl][Maxl]; pair<int, int> p[Maxn][Maxl][Maxl]; int main() { fill((int*)res, (int*)res + Maxn * Maxl * Maxl, Inf); fill((int*)res[0]...
12
#include <bits/stdc++.h> using namespace std; const int maxn = 202000; const int lmaxn = 20; const int inf = 1010000000; int n, aoriginal[maxn], a[maxn]; vector<int> compress; int bsearch(int g) { int low = -1, high = compress.size(); while (high > low + 1) { int mid = (low + high) / 2; if (compress[mid] < ...
17
#include <bits/stdc++.h> using namespace std; long long n; set<long long> S; int main() { scanf("%lld", &n); for (long long k = 1; k * k <= n; k++) { if (n % k == 0) { S.insert(n / k + n * (n / k + 1) / 2 - n); long long d = n / k; S.insert(n / d + n * (n / d + 1) / 2 - n); } } for (se...
6
#include <bits/stdc++.h> using namespace std; string s1, s2; int take[105]; int main() { memset(take, 0, sizeof take); cin >> s1 >> s2; for (int i = 0; i < s1.size(); i++) { if (s1[i] == 'X') take[i] = 1; } int cnt = 0; bool pre0 = 0; for (int i = 0; i < s2.size(); i++) { if (s2[i] == 'X') { ...
7
#include <bits/stdc++.h> using namespace std; using ll = long long int; vector<int> ad[2005]; int ara[2005], sz[2005], ans_ara[2005]; set<int> st; void dfs(int src, int pre) { sz[src] = 1; for (int v : ad[src]) { if (v == pre) continue; dfs(v, src); sz[src] += sz[v]; } } void dfs2(int src, int pre) { ...
10
#include <bits/stdc++.h> using namespace std; int32_t main() { int t; cin >> t; while (t--) { int n; cin >> n; int arr[n + 1]; int sum = 0; for (int i = 1; i <= n; i++) cin >> arr[i], sum += arr[i]; if (sum % n != 0) { cout << -1 << "\n"; continue; } int ech = sum / n; ...
12
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, x; cin >> n >> x; long long mx = 0, mx1 = 0, mx2 = 0; while (n--) { long long u, uu; cin >> u >> uu; if (uu < u) { if (mx < u - uu) { mx = u - uu; mx...
8
#include <bits/stdc++.h> long long int arr[210000]; long long int count[210000]; long long int sum[210000]; long long int temp[210000]; long long int bin[210000][64]; long long int spc[210000]; int main() { long long int i, j, p, s; long long int mul = 1; long long int n, k, x; scanf("%I64d%I64d%I64d", &n, &k, ...
9
#include <bits/stdc++.h> using namespace std; long long mod(long long a, long long m) { return a < 0 ? a % m + m : a % m; } int extended_euclid(int a, int b, int &x, int &y) { int xx = y = 0; int yy = x = 1; while (b) { int q = a / b; int t = b; b = a % b; a = t; t = xx; xx = x - q * xx; ...
16
#include <bits/stdc++.h> using namespace std; bool is_vowel(char str) { str = tolower(str); if (str == 'a' || str == 'e' || str == 'i' || str == 'o' || str == 'u' || str == 'y') { return true; } return false; } int main() { int ans = 0; vector<string> v = {"ABSINTH", "BEER", "BRANDY", "CHAMPAGNE"...
2
#include <bits/stdc++.h> using namespace std; char c; int sz, a0, a1; int main() { cin >> sz; for (int i = 0; i < sz; i++) { cin >> c; if (c == '0') a0++; else a1++; } cout << max(a1, a0) - min(a1, a0); return 0; }
1
#include <bits/stdc++.h> using namespace std; long long cnt[400111]; char c[400111]; long long cntn[200111]; char cn[200111]; long long cntm[200111]; char cm[200111]; int pf[400111]; int main() { int n, m; scanf("%d%d", &n, &m); int i = 0; while (n-- > 0) { int ccnt; char cc; scanf("%d-%c", &ccnt, &...
13
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const long long mod = 1e9 + 7; const long long N = 2e5 + 1; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long power(long long a, long long b = mod - 2) { long long res = 1; while (b > 0) { if (b & 1) res = res...
16
#include <bits/stdc++.h> using namespace std; int n, path[200010], cnt = 0, fa[200010]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 2; i <= n; ++i) { cin >> fa[i]; } path[cnt++] = n; while (path[cnt - 1] != 1) { path[cnt] = fa[path[cnt - 1]]; ++cnt; } for (...
1
#include <bits/stdc++.h> #define debug(...) fprintf(stderr,__VA_ARGS__) #define all(x) x.begin(),x.end() #define rep(i, j, n) for (int i=j;i<int(n);++i) typedef long long ll; using namespace std; typedef pair<ll, ll> P; typedef pair<ll, int> P1; const int maxN = 10000 + 5; priority_queue<P1, vector<P1>, greater<P1> > q...
14
#include <bits/stdc++.h> using namespace std; const int iinf = 1e9 + 7; const long long linf = 1ll << 60; const double dinf = 1e10; inline long long read() { bool f = 0; long long x = 0; char c = getchar(); while ((c < '0' || c > '9') && c != '-') c = getchar(); if (c == '-') { f = 1; c = getchar(); ...
21
#include <bits/stdc++.h> using namespace std; const int MAXN = 305; int n; int val[MAXN], cost[MAXN]; map<int, int> dp[MAXN]; int gcd(int a, int b) { if (a > b) swap(a, b); while (b ^= a ^= b ^= a %= b) ; return a; } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> val[i]; for (int i = 1; i <=...
11
#include <bits/stdc++.h> using namespace std; double prob[1 << 18]; double p[18][18]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> p[i][j]; prob[(1 << n) - 1] = 1; for (int i = (1 << n) - 1; i; i--) { int pop = __...
11
#include <iostream> #include <cmath> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; string s, t; cin >> s >> t; vector<int> left_index = vector<int>(t.size(), 0); vector<int> right_index = vector<int>(t.size(), 0); i...
7
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&...
2
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 6, oo = 1e9, MOD = 1073741824; int a, b, c; long long sum; int div(int x) { int cnt = 0; for (int i = 1; i * i <= x; i++) if (!(x % i)) { if (x / i != i) cnt++; cnt++; } return cnt; } int main() { cin >> a >> b >> c; for (in...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 6000000, N = 212345; int br[maxn][10]; int p[maxn]; int n, q; vector<int> fpr(int x) { vector<int> ret; while (p[x]) { ret.push_back(p[x]); x /= p[x]; } if (x > 1) ret.push_back(x); sort(begin(ret), end(ret)); vector<int> rret; for (in...
21
#include <bits/stdc++.h> using namespace std; int n, m, nr, fl[55], fc[55]; char a[55][55]; int dl[4] = {0, 0, 1, -1}; int dc[4] = {1, -1, 0, 0}; void ve(int l, int c) { fl[l] = nr; fc[c] = nr; a[l][c] = 'x'; for (int i = 1; i < l; i++) if (a[i][c] == '#') ve(i, c); for (int i = l + 1; i <= n; i++) if...
5
#include <bits/stdc++.h> using namespace std; map<int, int> mymap; map<int, int> mymap2; struct dat { int x; int y; }; struct dat st; vector<struct dat> vec; vector<struct dat>::iterator it; bool comp1(struct dat st1, struct dat st2) { return st1.x < st2.x; } bool comp2(struct dat st1, struct dat st2) { return st1....
6
#include <bits/stdc++.h> using namespace std; const long long MAX = 2e9, N = 5e3 + 10, MOD = 1e9 + 7; string s, t; long long n; long long bad(string &s, char c) { long long an = s.back() != c; for (long long i = 1; i < s.size(); i++) if (s[i] != s[i - 1]) ++an; return an; } long long ind(long long d, string &...
20
#include <bits/stdc++.h> using namespace std; const long long N = 24; int friends[N], dp[1 << N], g[N][N], nxt[1 << N]; int n, m; int tabs = 0; int solve(int mask, int finish) { if (mask == (1 << n) - 1) { nxt[mask] = 0; return dp[mask] = 0; } if (dp[mask] != -1) return dp[mask]; int ans = n + 1; for ...
16
#include <bits/stdc++.h> using namespace std; int n, m, q; const int maxN = 2 * (int)1e5 + 100; int p[maxN]; int a[maxN]; vector<int> all[maxN]; int nxt[maxN]; int l[maxN], r[maxN]; int nxt_pos[maxN]; int ansR[maxN]; int ansL[maxN]; bool ok[maxN]; int prv[maxN]; void solve1() { int best = 1; for (int i = 1; i <= n;...
12
#include <bits/stdc++.h> using namespace std; pair<int, int> Min, Max; int a[2012][2012]; int n; void init() { scanf("%d", &n); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]); } int cnt; bool vis[2012][2012]; void dfs(int x, int y) { if (x == 0 || y == 0 || x > n || y > n) ret...
12
#include <bits/stdc++.h> using namespace std; int main() { long long int t, x, y; cin >> t; while (t--) { cin >> x >> y; if (x % 2 == 0) { if (y % 4 == 0) { cout << x << endl; } else if (y % 4 == 1) { cout << x - y << endl; } else if (y % 4 == 2) { cout << x + 1 <...
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000; class Query { public: int type, v1, v2; } arr[MAXN + 5]; int sz[MAXN + 5]; long long acc[MAXN + 5]; int n, m; vector<int> vec; bool addOne(int v) { if (vec.size() >= MAXN + 5) return false; vec.push_back(v); return true; } void addSome(int ...
8
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n_Cases = 1; for (long long case_i = 1; case_i <= n_Cases; case_i++) { long long n; cin >> n; map<long long, long long> count; for (long long i = 0; i < n; i++) { long long in...
4
#include <bits/stdc++.h> using namespace std; int main() { string a; cin >> a; for (int j = 1; j < a.length(); j++) if (a[j] >= 'a' && a[j] <= 'z') { cout << a << endl; return 0; } for (int i = 0; i < a.size(); i++) { if (a[i] >= 'a' && a[i] <= 'z') a[i] = 'A' + a[i] - 'a'; els...
2
#include <bits/stdc++.h> using namespace std; int prime[1000002]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 2; i <= 1000000; i++) { if (prime[i]) continue; for (int j = i + i; j <= 1000000; j += i) prime[j] = 1; } int n; cin >> n; if (n == 1) cout << 1 ...
4
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; long long int dx[] = {0, 0, 1, -1}; long long int dy[] = {1, -1, 0, -1}; long long int m, k, n, res, a[5005], posmin[5005], posmax[5005], dp[5005] = {0}, dpsum[5005][5005], dp1[5005] = {0}; map<long long int, long long int> mp; int mai...
11
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const long long MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; char a[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } char sq...
2
#include <bits/stdc++.h> using namespace std; struct __s { __s() { if (1) { ios_base::Init i; cin.sync_with_stdio(0); cin.tie(0); } } ~__s() { if (!1) fprintf(stderr, "Execution time: %.3lf s.\n", (double)clock() / CLOCKS_PER_SEC); long long n; cin >> n; ...
14
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; pair<string, pair<long long, long long> > p[n + 1]; for (long long i = 1; i <= n; i++) { cin >> p[i].first; cin >> p[i].second.first >> p[i].second.second; } pair<pa...
9
#include <bits/stdc++.h> struct point { int x, y; } p[10]; int main() { int i, j, t, f = 1, cnt = 0; int a[10]; for (i = 0; i < 8; i += 2) { scanf("%d%d%d%d", &p[i].x, &p[i].y, &p[i + 1].x, &p[i + 1].y); if (p[i].x == p[i + 1].x) cnt += 1; else if (p[i].y == p[i + 1].y) cnt += 10; el...
9
#include <bits/stdc++.h> using namespace std; long long n, m; int main() { scanf("%lld%lld", &n, &m); if (n == 1) { printf("1 1"); return 0; } long long minn = max(0ll, n - 2 * m); long long maxn = 0, sum = 0, cur = 1; while (m > 0) { m -= min(cur, m); cur++; } maxn = n; if (cur > 1) m...
5
#include <bits/stdc++.h> double mymin(double a, double b) { if (a < 0) return b; else return a < b ? a : b; } int main() { double x1, y1, x2, y2; scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2); double x, y; x = x2 - x1; y = y2 - y1; double v, t; scanf("%lf %lf", &v, &t); double vx, vy, wx, wy;...
13
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d", &n); scanf("%d", &m); vector<pair<int, int> > v; int i; int d; for (i = 0; i < n; i++) { scanf("%d", &d); v.push_back(make_pair(i, d)); } int l = 0; i = 0; while (v.size() > 1) { if (m >= v[i].second) { ...
2
#include <bits/stdc++.h> using namespace std; bool taken[1000003]; int ans[1000003]; int main() { { int N; cin >> N; memset(taken, false, sizeof taken); unsigned long long sum = 0; for (int n = N; n >= 0; n--) { if (taken[n]) continue; int M = 0; for (int nrbits = 0; (1 << nrbits...
9
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb...
16
#include <bits/stdc++.h> using namespace std; long long power(long long b, long long e, long long MOD) { if (b == 0 && e == 0) return 1; if (b == 0 && e > 1) return 0; if (b > 0 && e == 0) return 1; long long r = 1; while (e) { if (e & 1) { r = r * b; r %= MOD; } e >>= 1; b = b * b...
1