solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int inf = 1e9; long long powm(long long a, long long p, long long m) { long long r = 1 % m; while (p) { if (p & 1) r = r * a % m; p >>= 1; a = a * a % m; } return r; } int n, m; vector<pair<int, int> > g[100010]; bool check...
10
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int shu[N], k1[N]; int main() { int n, m, i, j; ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); while (cin >> n >> m) { int ans = 0; for (i = 0; i < n; i++) cin >> shu[i]; sort(shu, shu + n); int t = 0; for (i = 0;...
9
#include <bits/stdc++.h> using namespace std; int i, j, n, e = 0, o = 0, k; int age[202], FLOW = 0; int cap[202][202]; int flow[202][202]; int visit[202], por[202]; int prime[20002]; int tc = 0; queue<int> q, t[202]; void genprime(int m) { prime[1] = -1; for (i = 2; i <= m; i++) { if (prime[i] == 0) { pri...
15
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int x, long long int y) { long long int ma = max(x, y); long long int mi = min(x, y); if (ma % mi == 0) return mi; return gcd(mi, ma % mi); } int main() { ios::sync_with_stdio(0); int n; cin >> n; for (int N = 0; N < n; N++) { ...
9
#include <bits/stdc++.h> using namespace std; struct edge { int a, b, no; }; set<int> v; map<int, vector<edge> > e; vector<int> g[100001], h[100001], r, ans; int f[100001], c[100001]; long long s[100000]; int find(int x) { if (f[x] == 0) return x; f[x] = find(f[x]); return f[x]; } void merge(int x, int y) { i...
15
#include <bits/stdc++.h> using namespace std; int main() { int q = 0; string s, t; for (int i = 1; i <= 3; i++) { cin >> s >> t; if (q == 1) cout << s << "\n" << t << "\n"; else { if (s[3] == '.') { s[3] = 'P'; q = 1; } else if (s[4] == '.') { s[4] = 'P'; ...
8
#include <bits/stdc++.h> using namespace std; void build_up(int x, int p, const vector<vector<int> > &edges, vector<int> &up, vector<int> &d) { up[x] = p; if (p != -1) { d[x] = d[p] + 1; } for (size_t i = 0; i < edges[x].size(); ++i) { int y = edges[x][i]; if (y == p) { continue;...
11
#include <bits/stdc++.h> using namespace std; const int N = 8E5 + 10; int n, m; int a[N], b[N]; int f[N], g[N]; struct node { int x, w, id, g, f; } q[N]; bool operator<(const node& w1, const node& w2) { return w1.x == w2.x ? w1.w < w2.w : w1.x < w2.x; } int c[N], ct[N], M; int lowbit(int x) { return x & -x; } void ...
18
#include <bits/stdc++.h> using namespace std; const int INF = int(1e9); const double EPS = 0.00000000001, PI = 3.14159265358979323846; int a[int(1e5 + 100)]; int main() { int n, balance = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); sort(a + 1, a + n + 1); for (int i = n; i >= 1; --i) ...
3
#include <bits/stdc++.h> using namespace std; long long n, k; int a[2001], b[2001]; bool m[20000007]; int main() { int n; while (cin >> n) { memset(m, false, sizeof(m)); for (int i = 0; i < n; i++) { cin >> a[i]; m[a[i]] = 1; } for (int i = 0; i < n; i++) { cin >> b[i]; m[b[i...
3
#include <bits/stdc++.h> using namespace std; const int inv = numeric_limits<int>::max(); const int minv = numeric_limits<int>::min(); int x; int main() { scanf("%d", &x); if (x == 3) { printf("5\n"); return 0; } for (int n = 1; n <= 100; n += 2) { int q; q = n * n / 2; ++q; if (x <= q) ...
9
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; int N; pair<int, int> A[MAX]; int main() { cin >> N; for (int i = 0; i < N; i++) { int a, b; cin >> a >> b; A[i] = pair<int, int>(a, b); } sort(A, A + N); int last = -1, res = 0; for (int i = 0; i < N; i++) { if (last > A...
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e2; int n, x; double a[MAXN]; double A[MAXN][MAXN], b[MAXN][MAXN], c[MAXN][MAXN]; int cnt; void read_input() { cin >> n >> x; for (int i = 0; i < x + 1; i++) { cin >> a[i]; } } void mult(double z[][MAXN], double g[][MAXN], bool p) { double h[MA...
11
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, m = INT16_MAX; cin >> n; vector<int> a(n), b(n + 2, -1); for (int &it : a) cin >> it; int g = n + 1; for (int i = 0, d = n + 1; i < n; ++i) { if (b[a[i]] != -1) { d = i - b[a[i]];...
4
#include <bits/stdc++.h> using namespace std; long long n, k, s, m, Max; priority_queue<long long> que; int main() { scanf("%lld%lld", &n, &k); if (n <= k) { while (n--) { scanf("%lld%lld", &s, &m); printf("%lld\n", s + m); } return 0; } for (long long i = 0; i < k; i++) { scanf("%ll...
8
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); mt19937 rnf(2106); const int N = 500005; int n, t; char a[N]; int qs, qh; bool stg(int q) { int tt = 0; int hh = 0; int b = 0; int l = 0; int m; int qq = 0; for (int i = 0; i < n; ++i) { ...
15
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k, s, r, i, ans = 1e8; char a[1011][1011]; memset(a, 0, sizeof(a)); scanf("%d %d %d", &n, &m, &k); for (i = 1; i <= k; i++) { scanf("%d %d", &r, &s); a[r][s] = 1; if (a[r - 1][s - 1] == 1 && a[r - 1][s] == 1 && a[r][s - 1] == 1 &...
3
#include <bits/stdc++.h> void build_population_count(unsigned int[]); void read_and_find_frequency(int[], unsigned int); unsigned long long count_k_interesting_pairs(int[], int, unsigned int[]); int main() { int count[10000 + 1] = {0}, k; unsigned int population_count[2 * 10000], no_of_elements; unsigned long lon...
9
#include <bits/stdc++.h> using namespace std; using ll = long long; void solve() {} int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int x = 0; int n; cin >> n; while (n > 0) { string s; cin >> s; if (s == "X++" || s == "++X") x++; else x--; n--; ...
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long i, n; cin >> n; long a[n]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); if (a[n - 1] != 1) a[n - 1] = 1; else a[n - 1]++; sort(a, a + n); for (i = 0; i < n; i++) cout <<...
5
#include <bits/stdc++.h> using namespace std; int main() { int t = 0; cin >> t; while (t--) { string s; int n = 0; cin >> n; cin >> s; bool firstl = 1, firstr = 1; bool meow = 0; int idxone = 0, firstidxone = 0, idxl = -1, idxr = 101; int cnt = 0; for (int i = 0; i < n; i++) { ...
4
#include <bits/stdc++.h> long long A[100000], B[100000]; void merging(int low, int mid, int high) { int l1, l2, i; for (l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) { if (A[l1] <= A[l2]) B[i] = A[l1++]; else B[i] = A[l2++]; } while (l1 <= mid) B[i++] = A[l1++]; while (l2 ...
10
#include <bits/stdc++.h> using namespace std; long long int sum(long long int a) { long long int sum = 0; while (a > 0) { sum = sum + (a % 10); a = a / 10; } return sum; } long long int count_digit(long long int n) { long long int count = 0; while (n > 0) { n = n / 10; count++; } return ...
6
#include <bits/stdc++.h> using namespace std; const int N = 150005; bool unq[N]; int main() { vector<int> arr(N); memset(unq, 0, sizeof(unq)); int n, res = 0; cin >> n; for (int i = 0; i < (int)(n); ++i) cin >> arr[i]; sort(arr.begin(), arr.begin() + n); for (int i = 0; i < (int)(n); ++i) { if (arr[i]...
7
#include <bits/stdc++.h> using namespace std; struct com { long long int max; long long int num; }; bool comp(com a, com b) { return (a.max < b.max); } int main() { int n; cin >> n; vector<com> c; long long int t; for (int i = 0; i < n; i++) { c.push_back(com{0, 0}); cin >> c[i].num; for (int ...
5
#include <bits/stdc++.h> int main() { int n, m; char a[100][100]; char c[100] = {0}; while (scanf("%d%d", &n, &m) != EOF) { int count = 0; int i, j, max; for (i = 0; i < n; i++) scanf("%s", &a[i]); for (j = 0; j < m; j++) { max = a[0][j]; for (i = 0; i < n; i++) { if (max < a...
1
#include <bits/stdc++.h> using namespace std; void solve() { long long a, b, c; cin >> a >> b >> c; long long aa = a % 3; long long bb = b % 3; long long cc = c % 3; long long mini = min(aa, min(bb, cc)); long long ans = a / 3 + b / 3 + c / 3 + mini; if (aa == 2 && bb == 2 && cc == 0 && c != 0) ans++; ...
8
#include <bits/stdc++.h> using namespace std; int a, b, c, d, ans; int main() { cin >> a >> b >> c >> d; int x = min(a, min(b, c)), y = max(a, max(b, c)); int z = a + b + c - x - y; if (z - x < d) ans += d - (z - x); if (y - z < d) ans += d - (y - z); cout << ans; }
0
#include<bits/stdc++.h> using namespace std; #define F first #define S second typedef long long ll; //cout.flush ll n, m, c[20]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for(int i=1; i<=n; i++){ for(int t=1; t<=m; t++){ cin >> c[1]; if((i+t)%2)...
14
#include <bits/stdc++.h> using namespace std; const int maxn = 505; int a[maxn], b[maxn], n, k, tot, num[maxn]; int suma[maxn], sumb[maxn]; char dire[maxn]; bool solve(int l, int r, int k) { if (l == r) return 1; int maxv = 0, pos = -1; for (int i = l; i <= r; ++i) maxv = max(maxv, a[i]); for (int i = l; i <= r...
10
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; string x; cin >> x; if (x.size() < 26) { cout << -1; return 0; } map<char, int> m; int i, j; bool can = 0; bool skip = 0; for (int i = 0; i < 26; i++) { m[char(65 + i...
5
#include <bits/stdc++.h> int main(void) { int n, s, c = 0; scanf("%d %d", &n, &s); if (s % n == 0) { c = s / n; } else { c = s / n; c++; } printf("%d", c); return 0; }
0
#include <bits/stdc++.h> long long int maxx(long long int& a, long long int& b) { return a > b ? a : b; } long long int minn(long long int& a, long long int& b) { return a > b ? b : a; } using namespace std; void boostIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long int gcd(long long int ...
8
#include <bits/stdc++.h> using namespace std; template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } ...
12
#include <bits/stdc++.h> template <class T> void Swap(T& a, T& b) { T c = a; a = b; b = c; } template <class T> T max(T& a, T& b) { return a < b ? b : a; } template <class T> T min(T& a, T& b) { return a > b ? b : a; } using namespace std; const int N = 2010; const int M = 200010; struct xfy { int l, r, x, ...
20
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; long long a, b; long long mod(long long x) { return (x % MOD + MOD) % MOD; } long long power(long long x, long long y) { long long res = 1; while (y) { if (y & 1) { --y; res = mod(res * x); } y /= 2; x = mod(x * x)...
9
#include <bits/stdc++.h> using namespace std; void __print(int32_t x) { cout << x; } void __print(long long x) { cout << x; } void __print(long double x) { cout << x; } void __print(double x) { cout << x; } void __print(float x) { cout << x; } void __print(bool x) { cout << (x ? "true" : "false"); } void __print(char x...
3
#include <bits/stdc++.h> using namespace std; long long waste; void solve() { long long n; long long k; cin >> n >> k; vector<long long> a(n); vector<long long> pref(n), suff(n); for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 0; i < n; i++) cin >> waste; sort(a.begin(), a.end()); fo...
10
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; int d, MOD; int inv[5005]; void init_inv() { inv[1] = 1; for (int i = 2; i <= 5000; i++) inv[i] = 1LL * inv[MOD % i] * (MOD - MOD / i) % MOD; } int coef2(int x) { int sum1 = 0, sum2 = 0; for (int i = 0; i <= d; i++) if (i != x) { ...
25
#include <bits/stdc++.h> using namespace std; int n, m, k; int sx, sy, tx, ty; char a[60][60]; bool ok[26]; int dis[60][60]; int d[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; int val[60][60]; pair<int, int> lst[60][60]; vector<pair<int, int> > tot[110]; vector<int> fst[26]; vector<int> snd[2510]; string ans; bool good; void so...
16
#include <bits/stdc++.h> const int mod = 1000000007; using namespace std; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } int prime[100007]; void SieveOfEratosthenes(int n) { memset(prime, -1, sizeof(pr...
10
#include <bits/stdc++.h> using namespace std; long long int a[200001], inds1[200001], inds2[200001]; int main() { long long int n, i, p, max1 = -1; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } inds1[n - 1] = n - 1; inds2[0] = 0; for (i = n - 2; i >= 0; i--) { if (a[i] <= a[inds1[i + 1]]) ...
5
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f; int x = a + b + c; cout << (a + b + c) * (a + b + c) - a * a - c * c - e * e << endl; return 0; }
8
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int i, a, min = 1000000001, count = 0; long long int sum = 0, sum1 = 0; for (i = 0; i < n; i++) { scanf("%d", &a); if (a % 2 == 0) sum = sum + a; else { count++; sum1 = sum1 + a; if (a < min) min = a; } } ...
1
#include <bits/stdc++.h> using namespace std; int main() { long long int q; cin >> q; while (q--) { long long int n, m, k; cin >> n >> m >> k; long long int a[n + 1], b, c, flag; flag = 0; a[0] = 0; for (int i = 1; i < n + 1; i++) cin >> a[i]; for (int i = 1; i < n + 1; i++) { if...
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; int n, k, a[MAXN], t[MAXN], pre[MAXN], preAll[MAXN]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); preAll[i] = preAll[i - 1] + a[i]; } for (int i = 1; i <= n; ++i) { scanf("%d", t + i); ...
4
#include <bits/stdc++.h> long long int mod = 1000000007; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n, x; cin >> n >> x; int a[n]; int sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; ...
4
#include <bits/stdc++.h> using namespace std; inline int read() { char ch = getchar(); int x = 0, f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, num[100005], fla...
15
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; using dd = long double; namespace { static bool constexpr dbg = 0; ll constexpr N = 1.1e5, MO = 1e9 + 7; ll n, d[N][2]; bool bl[N]; vector<ll> adj[N]; void init() { cin >> n; for (ll i = 1; i <= (ll)(n - 1); ++i) { ll ...
12
#include <bits/stdc++.h> using namespace std; int main() { int n, r; cin >> n >> r; double v1 = sin(3.1415926535897932384 / (double)n); double v2 = 1 - v1; cout << fixed << setprecision(10) << (r * v1) / (double)v2 << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 100010; struct tree { int lt, rt; int pos[2][21]; int val[2][21]; int l, r; long long ans; } a[N << 2]; int n, m, x; void update(tree &a, tree &b, tree &c) { a.lt = b.lt; a.rt = c.rt; for (int i = 0; i < b.l; i++) { a.pos[0][i] = b.pos[0][i...
18
#include <bits/stdc++.h> using namespace std; template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); } int32_t main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long n; cin >> n; long long k; cin >> k; vector<string> inp(n); for (auto(i) = 0; i < n - k + 1;...
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 205; vector<int> g[MAXN]; vector<int> g2[MAXN]; bool mark[MAXN]; vector<int> topo; inline void addE(int x, int y) { g[x ^ 1].push_back(y); g2[y].push_back(x ^ 1); g[y ^ 1].push_back(x); g2[x].push_back(y ^ 1); } void dfs(int u) { mark[u] = true; ...
11
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int no = 0, t_no = 0, i; for (; n >= 0; n--) { if (a[n - 1] == 1) { break; } } if (n == 0) { cout << t_no; return 0; } for (i = 0; i ...
2
#include <bits/stdc++.h> using namespace std; int n, m, k, l, r, ans, a[2111111]; int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } l = 1; r = n; for (int i = 1; i <= n / 2; ++i) { if (i % 2 == 1) swap(a[l], a[r]); l++; r--; } for (int i = 1; i <= n; ++i) { cout <<...
1
#include <bits/stdc++.h> using namespace std; const long long INF64 = 1e18; const long long INF32 = 1e9; const double EPS = static_cast<double>(1e-10); const double PI = 2.0 * acos(0.0); const long long MOD = (long long)1e9 + 7; long long powMod(long long a, long long b) { long long res = 1; a %= MOD; assert(b >=...
19
#include <bits/stdc++.h> using namespace std; const int N = 200010, LOG = 60; int n; vector<pair<int, int> > v; vector<int> a, cur[N], b, c; set<int> st; set<int>::iterator it; set<int>::iterator it2; int cnt = 0, last; int seg[4 * N]; int Idx; int update(int s, int e, int idx) { if (s > Idx || e < Idx) return seg[id...
12
#include <bits/stdc++.h> using namespace std; int a[111][111], n, m; int rr[111], cc[111]; int main(int argc, const char* argv[]) { cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> a[i][j]; } bool fl = true; for (; fl == true;) { fl = false; for (int i = 0; i...
13
#include <bits/stdc++.h> using namespace std; long long a[5], b[5]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) a[i % 5]++; for (int i = 1; i <= m; i++) b[i % 5]++; cout << a[0] * b[0] + a[1] * b[4] + a[2] * b[3] + a[3] * b[2] + a[4] * b[1]; }
3
#include <bits/stdc++.h> using namespace std; const int mxn = 1e5 + 100; char ch[mxn]; int main() { int n; scanf("%d", &n); scanf("%s", ch); vector<int> v; for (int j = 0, i; j < n;) { i = j; while (j < n && ch[j] == 'G') ++j; v.push_back(j - i), i = j; while (j < n && ch[j] == 'S') ++j; v...
8
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v(n); vector<long long> v1(n); for (long long i = 0; i < n; i++) { cin >> v[i]; v1[i] = v[i]; } sort(v.begin(), v.end()); long long count = 0; for (long long i = 0; i < n; i++) { if (v[i] !=...
5
#include <bits/stdc++.h> using namespace std; int a[1005], n, ans; struct b { int b[40], len; } cal[1005]; void ca(int x) { int c = a[x], l = 0; while (c != 0) { int y = c % 2; c /= 2; cal[x].b[++l] = y; cal[x].len = l; } } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i...
4
#include<bits/stdc++.h> using namespace std; #define For(i,a,b) for(int i=a,i##_end=b;i<i##_end;++i) using ll=long long; using pii=pair<int,int>; #define fi first #define se second #define mp make_pair #define pb push_back const int mo=998244353; int Pow(int a,int b,int p=mo){int r=1;for(;b;b>>=1,a=1ll*a*a%p)if(b&1)r=1...
7
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("avx2") using namespace std; vector<long long> vec; long long t, n, m, k; string anse; bool matr[26][26]; bool flag = false, used[26]; void dfs(int v, int par = -1) { used[v] = true; for (int i = 0; i < 26; i++) { if (matr[v][i] && !used[i] ...
8
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const long long EPS = -1e18; const long long MOD1 = 1e9 + 7; const long long MOD2 = (119 << 23) + 1; long long t, n, even, odd; vector<long long> a; void solution() { cin >> n; a.resize(n); even = 0; odd = 0; for (long long i = 0; i < n...
1
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int N = 1e5 + 10; struct q { int a; int b; int c; int id; }; q node[N]; void order(int a, int b, int c, int i) { int t; if (a < b) { t = a; a = b; b = t; } if (a < c) { t = a; a = c; c = t; } if (...
8
#include <bits/stdc++.h> using namespace std; int a[10005], b[10005]; int main() { char s[101]; int n, i, j, k, count = 0, flag = 0; cin >> n >> k; j = k; for (i = 0; i < n; i++) { if (k == 0) { k = j; } cout << char(k + 'a' - 1); k--; } }
0
#include <bits/stdc++.h> using namespace std; const long long INF = 10000000000007; int n, m, k, x, y, z, ans; long long d[100001]; priority_queue<pair<long long, int> > s; vector<pair<int, pair<long long, short int> > > v[100001]; vector<pair<long long, int> > v1; void dj() { for (int i = 0; i <= n; i++) d[i] = INF;...
12
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; map<long long, long long> mapp; vector<long long> v; long long n, k, res = 0, cnt = 0; cin >> n >> k; for (int i = 0; i < n; i++) { long long val; cin >> val; mapp[val]++; }...
4
#include <bits/stdc++.h> using namespace std; inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int& n, int b) { n |= two(b); } inline void unset_bit(int& n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) ...
4
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f; const int maxn = 350; vector<vector<int>> delta(maxn, vector<int>(maxn, inf)); vector<vector<int>> pred(maxn, vector<int>(maxn, -1)); vector<int> ans; vector<vector<int>> tracks(maxn, vector<int>(maxn, 0)); void PRINT(vector<vector<int>> d, int n) ...
12
#include <bits/stdc++.h> using namespace std; const int MaxN = 100; vector<int> g[MaxN]; long long cnt1[MaxN], cnt2[MaxN]; int dist1[MaxN], dist2[MaxN]; bool used[MaxN]; int q[MaxN]; int n, m; long long res[MaxN]; void bfs() { int l = 0, r = 1; q[l] = 0; dist1[0] = 0; used[0] = true; while (l < r) { int c...
11
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; bool comp(pair<long long int, long long int> p, pair<long long int, long long int> q) { return p.second < q.second; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n; lon...
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m, d, x; cin >> n >> m >> d; n *= m; vector<int> l; cin >> x; l.push_back(x); int mod = x % d, f = 0; for (int i = 1; i <= n - 1; ++i) { cin >> x; l.push_back(x); x %= d; if (x != mod) { f = 1; break; } }...
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string in; cin >> in; int fl = 1; int u = 0; for (int i = 0; i < n; i++) { if (in[i] == '[') u++; else u--; fl = max(fl, u); } int a[n + 1]; vector<int> st; for (int i = 0; i < n; i++) { if (in[...
6
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int MN = 5010; int comb(int n) { return 1LL * n * (n - 1) / 2 % mod; } int N; int A[MN], pmul[MN], ans[MN << 1]; vector<int> dp1, dp2, tmp; int main() { scanf("%d", &N); for (int i = 0; i < N - 1; i++) { scanf("%d", &A[i]); } for (...
17
#include <bits/stdc++.h> using namespace std; const int maxn = int(2e3) + 5; string row[maxn]; char mark[maxn]; int cnt[maxn][256]; int main() { int n, m; cin >> n >> m; string s; cin >> s; if (n == m) { cout << "No" << endl; return 0; } int K = ceil(n / float(m)); int i = 0; for (int k = 0; k...
4
#include <bits/stdc++.h> using namespace std; void solve() { int n, m; cin >> n >> m; vector<int> adj[n + 1]; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } priority_queue<int> PQ; PQ.push(-1); bool vis[n + 1]; memset(vis, false, size...
7
#include <bits/stdc++.h> using namespace std; vector<long long> tree, arr, diff; int n, q; long long sum(int k) { long long s = 0; while (k >= 1) { s += tree[k]; k -= k & -k; } return s; } void add(int k, long long x) { while (k <= n) { tree[k] += x; k += k & -k; } } int binary_search(long l...
17
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; long long arr[N], suff[N], pref1[N], suff1[N], q[N]; int solve() { long long a, b, m, r; cin >> a >> b >> m >> r; map<long long, int> mp; r = (a * r + b) % m; for (int i = 0; i <= 2e5; i++) { r = (a * r + b) % m; } for (int i = 0; i ...
4
#include <bits/stdc++.h> using namespace std; long long F(long long x) { if (x % 2 == 0) return x / 2; else return x / 2 - x; } int main() { int Q; cin >> Q; while (Q--) { long long x, y; cin >> x >> y; cout << F(y) - F(x - 1) << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<vector<int>> a(n, vector<int>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> a[i][j]; } } for (int cnt = 0; cnt < m; ++cnt...
14
#include <bits/stdc++.h> const double eps = 1e-10; const int maxn = 2e5 + 10; ; const long long mod = 1e9 + 7; int sgn(double a) { return a < -eps ? -1 : a < eps ? 0 : 1; } using namespace std; int head[maxn], head2[maxn]; int dist[maxn]; bool vis[maxn]; int cnt, cnt2; int p[maxn], q[maxn]; struct Edge { int to, next...
9
#include <bits/stdc++.h> using namespace std; void solve() { long long sum = 0, flag = 0, i, j, k, x, y, z, n, m, p, t; cin >> n; for (i = 0; i < n; i++) { cin >> x; if (x % 4 == 0) cout << x / 4; else if ((x - 6) % 4 == 0 && (x - 6) >= 0) { cout << (x - 6) / 4 + 1; } else if ((x - 9) ...
5
#include <bits/stdc++.h> using namespace std; int N, D; int val[60]; int dp[10010 * 60]; int main() { int i, j; cin >> N >> D; int sum = 0; dp[0] = 1; for (i = 0; i < N; i++) { cin >> val[i]; sum += val[i]; for (j = sum; j >= val[i]; j--) { if (dp[j - val[i]]) { dp[j] = 1; } ...
14
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); long long n; cin >> n; vector<pair<string, long long>> t; vector<long long> q(n); vector<long long> mx(n + 1, -1); long long cur = 0; long long cnt = 0; for (long long i = 0; i < (long long)(2 * n); i++) { ...
9
#include <bits/stdc++.h> using namespace std; using int64 = long long; int main() { int N; cin >> N; vector<int> vs(N); bool zero = false; for (int i = 0; i < N; i++) { cin >> vs[i]; if (vs[i] == 0) zero = true; } sort(begin(vs), end(vs)); vs.erase(unique(begin(vs), end(vs)), end(vs)); cout <<...
0
#include <bits/stdc++.h> using namespace std; int computeXOR(int n) { switch (n % 4) { case 0: return n; break; case 1: return 1; break; case 2: return n + 1; break; default: return 0; break; } } int main() { int t; cin >> t; while (t--) { in...
2
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; int mod; int sum(int a, int b) { int s = a + b; if (s >= mod) s -= mod; return s; } int sub(int a, int b) { int s = a - b; if (s < 0) s += mod; return s; } int mult(int a, int b) { return (1LL * a * b) % mod; } int pw4(int x) { return ...
15
#include <bits/stdc++.h> using namespace std; vector<string> unique(vector<string> vec) { sort(vec.begin(), vec.end()); auto ip = unique(vec.begin(), vec.end()); vec.resize(distance(vec.begin(), ip)); return vec; } long long sum(vector<long long> vec) { long long Sum = 0; for (auto& i : vec) Sum += i; ret...
1
#include <bits/stdc++.h> using namespace std; int main() { unsigned long long n = 0; cin >> n; for (unsigned long long i = 2; i * i <= n; i++) { if (n % (i * i) == 0) { n /= i; i = 1; } } cout << n << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long int N = 1e3 + 10, N2 = 2e5, delta = 46639, mod = 1e9 + 7, oo = 1e12, LOG = 20, SQ = 300; const long double PI = 3.141592653589793; pair<long long int, long long int> a[N]; int32_t main() { ios::sync_with_stdio(false), cin.tie(0), cout.t...
0
#include <bits/stdc++.h> using namespace std; int n; int main() { scanf("%d", &n); int x = sqrt(n); if (x * x == n) printf("%d\n", 2 * x); else { int r = n / x; int p = x + r; if (n % x != 0) p++; printf("%d\n", p); } }
3
#include <bits/stdc++.h> using namespace std; int INF = (int)1e9; long long INFINF = (long long)1e18 + 10; const long double PI = 3.14159265358979323846; long long GCD(long long, long long); long long LCM(long long, long long); long long power(int, int); long long choose(long long, long long); int ones(long long); void...
3
#include <bits/stdc++.h> using namespace std; double const pi = 3.1415926536; int main() { string s; cin >> s; int t = 0; for (int i = 0; i < s.length(); i++) { if (s[i] > 'a') { s[i]--; t = 1; } else { if (t == 1) break; } } if (t == 0) s[s.length() - 1] = 'z'; cout << s; }
4
#include <bits/stdc++.h> using namespace std; class fenwick_tree { private: int N; int* bit; public: fenwick_tree(int n) { N = n; bit = new int[N + 1]; for (int i = 0, _n = (int)N + 1; i < _n; i++) bit[i] = 0; } void update(int x, int v) { for (int i = x; i <= N; i += i & -i) bit[i] = max(b...
7
#include <bits/stdc++.h> using namespace std; int main() { int q, n, i, j, c; char c1, c2; string s1, s2; cin >> q; while (q--) { cin >> n >> s1 >> s2; j = 0; c = 0; for (i = 0; i < n; i++) { if (s1[i] != s2[i]) { j++; if (j == 1) { c1 = s1[i]; c2 = s2...
2
#include <bits/stdc++.h> using namespace std; int n; const int maxN = 1e5 + 10; vector<int> g[maxN]; int dp[maxN][2]; void dfs(int v, int p) { dp[v][1] = 1; dp[v][0] = 0; int deg = 0; for (int to : g[v]) { if (to == p) continue; dfs(to, v); deg++; } for (int to : g[v]) { if (to == p) continu...
19
#include <bits/stdc++.h> using namespace std; template <typename C, typename R = C, bool lb = false> struct dinic { static_assert(std::is_arithmetic<C>::value, "flow_type must be arithmetic"); static_assert(std::is_arithmetic<R>::value, "result_type must be arithmetic"); static const C oo = std::numeric_limits<C>...
25
#include <bits/stdc++.h> using namespace std; template <class S, class T> ostream& operator<<(ostream& o, const pair<S, T>& p) { return o << "(" << p.first << "," << p.second << ")"; } template <class T> ostream& operator<<(ostream& o, const vector<T>& vc) { o << "sz = " << vc.size() << endl << "["; for (const T&...
17