solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> int main() { int e, n, i, arr[100000], min = 1000000000; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); if (arr[i] < min) min = arr[i]; } for (i = 0; i < n; i++) if (arr[i] % min != 0) { printf("-1\n"); return 0; } printf("%d", min); r...
2
#include <bits/stdc++.h> using namespace std; long long h, q, a, l1, r1, x, f[51], M, lm, rm, s, lh, rh, mm; pair<long long, long long> t, k[100010], t1; vector<pair<long long, long long> > w, la; int main() { cin >> h >> q; f[0] = 1; for (long long i = 1; i <= h; i++) { f[i] = f[i - 1] * 2; } M = f[h] + ...
15
#include <bits/stdc++.h> using namespace std; mt19937 gen(time(nullptr)); const long long INF = 1e18; long long n, x, y; vector<pair<long long, long long> > pref, suff; bool check(long long l, long long r) { long long x1 = (l == 0 ? 0 : pref[l - 1].first) + (r == n - 1 ? 0 : suff[r + 1].first), ...
10
#include <bits/stdc++.h> using namespace std; int inf = 1e9; const int maxn = 1e5 + 20; int mn[2][maxn * 4 + 10]; void ADD(int s, int e, int v, int l, int r, int val, int det) { if (l <= s && e <= r) { mn[det][v] = min(mn[det][v], val + ((det)*2 - 1) * (s - l)); return; } if (l >= e || s >= r) { retur...
13
#include <bits/stdc++.h> using namespace std; long long p, q; long long ans; bool primes[3000002]; long long ps[3000002]; long long pal = 0; int pals(int i) { int ans = 0; int num = i; int rev = 0; while (num > 0) { int dig = num % 10; rev = rev * 10 + dig; num = num / 10; } if (i == rev) ans++;...
8
#include <bits/stdc++.h> using namespace std; void ActivateFastIO() { ios::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); } using ll = long long; const int N = 100005; int n; int a[N]; int dp[N][320]; int solve(int ndx, int k) { if (ndx > n) { return 0; } int &ret = dp[ndx][k]; if (ret) ...
12
#include <bits/stdc++.h> using namespace std; long long n, m, k; long long f(long long r, long long c) { return (n / (r + 1LL)) * (m / (c + 1LL)); } signed main() { ios::sync_with_stdio(0); cin.sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; if (k > n + m - 2) { cout << -1 << '\n'; return 0; }...
9
#include <bits/stdc++.h> using namespace std; template <class T> void rd(T &x) { x = 0; int f = 1; char c; while (!isdigit(c = getchar())) if (c == '-') f = -1; do { x = (x << 3) + (x << 1) + (c ^ 48); } while (isdigit(c = getchar())); x *= f; } const int N = 1e5 + 5, mod = 998244323; const long l...
14
#include <bits/stdc++.h> using namespace std; long long GCD(long long a, long long b) { if (b > a) { swap(a, b); } if (b == 0) { return a; } return GCD(b, a % b); } long long mul_mod(long long a, long long b, long long m) { a %= m; b %= m; return ((((a * b) % m) + m) % m); } long long power(long...
10
#include <bits/stdc++.h> using namespace std; int n, m; int a[35][35]; bool out(int x, int y) { if (x < 0 || y < 0 || x >= n || y >= m) return true; return false; } int main() { int T; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++)...
7
#include <bits/stdc++.h> using namespace std; int dp[1 << 20]; int cost[30][30], f[30][30], change[30][30]; int n, m; char s[30][30]; inline void getCost() { for (int k = 0; k < n; ++k) { for (int i = 0; i < m; ++i) { int sum = 0, tem = 0; f[i][k] = 0x7fffffff; for (int j = 0; j < n; ++j) { ...
17
#include <bits/stdc++.h> using namespace std; int N; char c[3000][3000]; int color[3000][3000]; int row[3000]; int col[3000]; int main() { scanf("%d", &N); for (int Ni = 0; Ni < N; Ni++) scanf("%s", c[Ni]); for (int Ni = 0; Ni < N; Ni++) for (int Nj = 0; Nj < N; Nj++) color[Ni][Nj] = c[Ni][Nj] - '0'; int cn...
12
#include <bits/stdc++.h> using namespace std; struct bola { double x, v, m; int id; }; bola ba[101]; bool cmp(bola a, bola b) { return a.x < b.x; } bool cmp2(bola a, bola b) { return a.id < b.id; } int main() { int n, t; scanf("%d %d", &n, &t); for (int i = 0; i < n; i++) { scanf("%lf %lf %lf", &ba[i].x, ...
12
#include <bits/stdc++.h> using namespace std; const long long int infinity = 9e18; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; long long int total = 0, odd = 0, even = 0; long long int k; for (int i = 0; i < n; i++) { cin >> k; total += k / 2; if (k...
12
#include <bits/stdc++.h> using namespace std; int n, n1, n2, x, i, j, k; long long f[1000005], ans; int main() { scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%d", &x); if (x == 1) ++n1; else ++n2; } f[0] = f[1] = 1; for (i = 2; i <= n1; ++i) f[i] = (f[i - 1] + (i - 1) * f[i - 2])...
15
#include <bits/stdc++.h> using namespace std; const int N = 5e3 + 7; vector<int> adj[N]; int p[N]; int dp[N]; int mark[N]; bool relax(vector<int> &v, int x) { bitset<N> ok; ok[0] = 1; for (auto val : v) ok = (ok | (ok << val)); return ok[x]; } int main() { ios::sync_with_stdio(false); int n; cin >> n; i...
21
#include <bits/stdc++.h> using namespace std; using namespace chrono; using ll = long long; using vi = vector<int>; using vl = vector<ll>; using vvi = vector<vi>; using pii = pair<int, int>; using pll = pair<ll, ll>; template <typename T> int sz(const T& a); template <typename T> int unq(vector<T>& a); template <typena...
3
#include <bits/stdc++.h> using namespace std; const int N = 100010; inline int in() { int x; scanf("%d", &x); return x; } inline long long lin() { long long x; scanf("%I64d", &x); return x; } int deg[N]; map<int, int> Map; struct Info { int v, l, r; Info() {} Info(int _v, int _l, int _r) { v = _v;...
13
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { long long n, k; cin >> n >> k; long long ans = 0; if (k >= n) { ans += 1; k = n - 1; } ...
4
#include <bits/stdc++.h> using namespace std; long long pow_mod(long long a, long long i) { long long ans = 1; while (i) { if (i % 2 == 1) ans = ans * a % 1000000007; a = a * a % 1000000007; i >>= 1; } return ans; } long long getSum(long long a, long long n) { return (pow_mod(a, n) - 1) * pow_mod(...
9
#include <bits/stdc++.h> const int mod = 1e9 + 7; const int M = 4e5 + 5; const int N = 8e5 + 10; using namespace std; inline long long read() { long long sum = 0, b = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') b = -1; c = getchar(); } while (isdigit(c)) { sum = sum * 10 + c - '0'; ...
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tcase; cin >> tcase; while (tcase--) { long long n; cin >> n; long long arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; long long ans = 1; if (n == 2) ans = ...
0
#include <bits/stdc++.h> using namespace std; int n, m; int check(string st) { for (int i = 1; i < st.size(); i++) { if (st[i] == st[i - 1]) return 1; } for (int i = 2; i < st.size(); i++) { if (st[i] == st[i - 2]) return 1; } return 0; } int ypa(string &st) { int i; for (i = st.size() - 1; i > 0;...
9
#include <bits/stdc++.h> #pragma GCC optimize(3, "Ofast", "inline") using namespace std; const int maxnn = 2e5 + 7; int n, m; struct dy { int a[4]; } sz[maxnn]; vector<int> cx[maxnn]; map<long long, vector<int>> que; int ans[maxnn]; int s = 0, t = 0; int ss = 0, tt = 0; int flag = 0; long long base = 1; int check(int...
8
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int main() { int n, x; cin >> n >> x; int start = 1; int ans = 0; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; while (start + x <= l) { start += x; } while (start <= r) { start++; ans++...
2
#include<bits/stdc++.h> using namespace std; using namespace chrono; #define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL) #define endl '\n' #define MOD 1000000007 #define MOD1 998244353 #define INF 1e18 #define PI 3.141592653589793238462 #define set_bits __builtin_popcountll #define sz(x) ((i...
7
#include <bits/stdc++.h> using namespace std; struct fenwickTree { vector<long long> bit; fenwickTree(long long n) : bit(n, 0) {} void update(long long idx, long long d) { for (; idx < (long long)bit.size(); idx = idx | (idx + 1)) bit[idx] += d; } long long sum(long long r) const { long long ret = 0; ...
11
#include <bits/stdc++.h> using namespace std; int n, i, arr[3001]; int idx, j, mn = 1000000002; int main() { cin >> n; for (i = 0; i < n; i++) cin >> arr[i]; cout << n << endl; for (i = 0; i < n; i++) { idx = i; for (j = i; j < n; j++) if (arr[j] < arr[idx]) idx = j; swap(arr[idx], arr[i]); ...
4
#include <bits/stdc++.h> using namespace std; inline int rdi() { int d; scanf("%d", &d); return d; } inline char rdc() { scanf(" "); return getchar(); } inline string rds() { string s; cin >> s; return s; } inline double rddb() { double d; scanf("%lf", &d); return d; } template <class T> inline bo...
8
#include <bits/stdc++.h> using namespace std; void FastIO() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cout.precision(15); } int main() { FastIO(); int n, i, j, y, maxval = -1, ans = 0, var; vector<int> v; vector<int>::iterator it; set<int> s; set<int>::iterator it1; cin >> n; ...
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, a, b, k; cin >> n >> a >> b >> k; vector<int> v(n); for (int i = 0; i < n; i++) { int h; cin >> h; int r = h / (a + b); if (h % (a + b) == 0) r--; if (r == 0 && a < h) v[...
7
#include <bits/stdc++.h> using namespace std; const int N = 100005; long long int solve1(long long int r1, long long int r2, long long int x, long long int y, long long int a, long long int b) { long long int dis_x = abs(x - r1); if (dis_x % a) return 1e18; long long int dis_y = abs(y - r2); ...
12
#include <bits/stdc++.h> using namespace std; int n, m, i, j, k, l, a[1000009], b[1000009], dp[1 << 20 + 5], ans, cn, t, x, y, z, mx, mn, s; char c[25][10], d[1000009], ch; int set1(int num, int pos) { return num = num | (1 << pos); } int check(int num, int pos) { return num & (1 << pos); } int go(int mask, int pos...
14
#include <bits/stdc++.h> using namespace std; void call(vector<int> a) { int less = 0; if (a.size() % 2 != 0) less = 1; else less = 2; for (int i = 0; i < a.size() - less; i += 2) { cout << a[i] << " " << a[i + 1] << endl; } } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); in...
3
#include <bits/stdc++.h> using namespace std; int ans, a[100000], n; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) ans += a[i] * i; printf("%d", ans); }
12
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 10; const long long LINF = 1000ll * 1000 * 1000 * 1000 * 1000 * 1000 + 5; const int MN = 100; int sz; long long fib[MN]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long n; cin >> n; fib[0] = 1, fib[1] = 2; sz...
8
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } inline void write(int x) {...
23
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; const int mod = 1e9 + 7; int n, m, k; vector<int> numx, numy; int m1[1010][1010]; struct node { int x, y; } pos[N]; int find1(int x) { return lower_bound(numx.begin(), numx.end(), x) - numx.begin() + 1; } int find2(int x) { return lower_bound(n...
16
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; inline int getint() { int c, l = 0, x; for (; !isdigit(c = getchar()); l = c) if (c == EOF) return 1 << 31; for (x = c - '0'; isdigit(c = getchar()); x = x * 10 + c - '0') if (c == EOF) break; return l == '-' ? -x : x; } int buf[10...
25
#include <bits/stdc++.h> using namespace std; const int N = (int)2e5 + 15; const int MOD = 1000000007; const int inf = 0x3f3f3f3f; struct Seg { int l, r, idx; bool operator<(const Seg& b) const { return l != b.l ? l < b.l : r < b.r; } }; Seg seg[N]; int sum[N]; bool used[N]; int main() { int k, n; scanf("%d%d",...
10
#include <bits/stdc++.h> int abs(int x) { if (x < 0) return -x; else return x; } int m(int x, int y) { if (x > y) return y; else return x; } int main() { char s[4], t[4]; scanf("%s", s); scanf("%s", t); int x1, y1, x2, y2; x1 = s[0] - 'a' + 1; x2 = t[0] - 'a' + 1; y1 = s[1] - '0'; ...
2
#include <bits/stdc++.h> using namespace std; typedef struct node { struct node* nexNode[2]; int cnt; } Node; Node* root; void trie_insert(int num) { int i = 30; string s = ""; while (i > 0) { char digit = '0' + num % 2; s = digit + s; num /= 2; i--; } Node* current = root; while (i < s....
12
#include <bits/stdc++.h> using namespace std; const int inf = 100010; const int MOD = 1000000007; const double pi = acos(-1.0); int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); } int lcm(int a, int b) { return (a * (b / gcd(a, b))); } int fx[] = {0, 0, 1, -1}; int fy[] = {-1, 1, 0, 0}; int n, m; int flg = 0...
7
#include <bits/stdc++.h> using namespace std; const long long oo = 0x3f3f3f3f3f3f3f3fll; const int LGN = 25; const int MOD = 1e9 + 7; const int N = 3e5 + 5; int n, adj[N][26], dep[N]; vector<pair<int, int> > T[N]; vector<int> depset[N]; int sub[N], big[N]; void dfs1(int u = 1, int p = 0, int d = 1) { dep[u] = d; de...
17
#include <bits/stdc++.h> using namespace std; long long M, K, dp[70][70][2], bits; bool bit[70]; long long f(int ind, int k, int t) { if (dp[ind][k][t] != -1) return dp[ind][k][t]; if (ind == bits + 1) return k == 0; dp[ind][k][t] = f(ind + 1, k, t ? 1 : (bit[ind] == 1 ? 1 : 0)); if (k and (t or bit[ind] == 1))...
13
#include <bits/stdc++.h> using namespace std; int main() { long long int n, t, i, j, k, m; cin >> t; while (t--) { cin >> n >> k; string s; cin >> s; string s1, s2, s3; s1 = ""; s2 = ""; s3 = ""; for (i = 0; i < k; ++i) { if (i % 3 == 0) { s1 += 'R'; } else if (...
7
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template <class T, class S> inline void add(T& a, S b) { a += b; if (a >= mod) a -= mod; } template...
18
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int tp = 1; for (int i = 1; i < n; i++) { tp += i; tp %= n; if (tp == 0) cout << n << " "; else cout << tp << " "; } }
0
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const double PI = acos(-1); const double EPS = 1e-6; const int INF = 0x3f3f3f3f; inline void read(long long& x) { int f = 1; char c; while (((c = getchar()) < '0' || c > '9') && c != '-') ; c == '-' ? (f = -1, x = 0) : (x = c - '0'...
14
#include <bits/stdc++.h> using namespace std; int st1[1001], st2[1001]; int n; void flip(int i) { int j; for (j = 1; j <= i; j++) st1[j] = st1[j] ^ 1; j = 1; while (j < i) { swap(st1[i], st1[j]); j++; i--; } } int main() { int t; cin >> t; while (t--) { cin >> n; char ch; for (in...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 500010, inf = 1e9 + 233; int Q, x; long long sum[maxn]; multiset<int> s[40]; template <typename T> inline void read(T &k) { int f = 1; k = 0; char c = getchar(); while (c < '0' || c > '9') c == '-' && (f = -1), c = getchar(); while (c <= '9' && c ...
20
#include <bits/stdc++.h> using namespace std; bool make(int addo, int addz, int z, int o, int ex) { if (addz == 0) { if (o + ex >= addo + z) return 1; else return 0; } else { if (z + ex >= addz + o) return 1; else return 0; } } int main() { string s; cin >> s; int zer...
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 + 100; int x[MAXN]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < (int)(n); i++) cin >> x[i]; int res = 0; while (m--) { int u, v; cin >> u >> v; u--; v--; res += min(x[u], x[v]); } cout << res << endl; r...
6
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 100100 * 2; int n, m, d[maxn]; int fir[maxn], last[maxn << 2], nex[maxn << 2], a[maxn << 2], b[maxn << 2], tot = 1; vector<int> e[maxn]; bool vis[maxn]; long long dp[maxn][2][2]; int sgn(int x) { if (x > 0) return 0; if (x <...
21
#include <bits/stdc++.h> using namespace std; using LL = long long; using PII = pair<int, int>; const int N = 3e5 + 10; char S[] = "AGCT"; string s[N]; int pos[N], tpos[N]; int a[4], ta[4]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> s[i]; in...
13
#include <bits/stdc++.h> using namespace std; int n, c = 0; string s; bool ck(string t, string u) { if (t.size() != u.size()) { return false; } for (int i = 0, z = t.size(); i < z; i++) { if (t[i] != u[i]) { return false; } } return true; } int main() { cin >> n; cin >> s; for (int i =...
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string a; int i; cin >> a; if (a[0] == '0') { cout << '0' << endl; return 0; } cout << '1'; for (i = 0; i <= n; i++) { if (a[i] == '0') { cout << '0'; } } cout << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int a, b, c, d, n, x[30], y[30]; char e[155][155]; bool go(int dir) { int cur = 0; for (int i = 0; i < n; i++) x[i] = y[i]; for (int i = 0; i < b; i++) { int start, finish; if (dir > 0) { start = 0; finish = a + c; } else { start = a + c ...
11
#include <bits/stdc++.h> using namespace std; int n; vector<vector<int> > g; vector<int> sz; const int INF = 1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n; g.resize(n); sz.resize(n); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; g[a - 1].push_...
14
#include <bits/stdc++.h> using namespace std; const long long inf = INT_MAX, df = 505, mod = 1e9 + 7; long long i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, dp[df][df][2], a[df][df]; inline long long read() { long long x = 0, y = 1; char ch = getchar(); while (ch > '9' || ch < '0') y = (ch == '-') ? ...
17
#include <bits/stdc++.h> using namespace std; static uint_fast16_t t; static uint_fast32_t n, k; static uint_fast32_t l1, r1; static uint_fast32_t l2, r2; int main() { scanf("%" SCNuFAST16, &t); for (; t; t--) { scanf("%" SCNuFAST32 " %" SCNuFAST32, &n, &k); scanf("%" SCNuFAST32 " %" SCNuFAST32, &l1, &r1); ...
13
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int SQRT = 500; vector<pair<int, int> > quer[SQRT]; int n, m, k; int arr[MAXN]; int num[(1 << 20) + 5]; long long ans = 0; map<pair<int, int>, long long> an; bool cmp(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } int main...
14
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; if (n == 1) { cout << 0 << endl; return; } vector<bool> isPrime(n + 1, true); isPrime[0] = isPrime[1] = false; for (long long i = 2; i * i <= n; i++) { if (isPrime[i]) { for (long long j = i * i; j <= n; ...
7
#include <bits/stdc++.h> using namespace std; inline int read() { int res = 0; int neg; while (true) { char ch = getchar(); if (ch >= '0' && ch <= '9' || ch == '-') { if (ch == '-') neg = -1; else neg = 1, res = ch - '0'; break; } } while (true) { char ch = ge...
17
#include <bits/stdc++.h> using namespace std; vector<int> c(100001); vector<vector<int> > g(100001); vector<int> color(100001); vector<int> ans(100001, 1e9); int t = 1; void dfs(int u) { color[u] = t; ans[t] = min(ans[t], c[u]); for (auto i : g[u]) if (color[i] == 0) dfs(i); } int main() { ios_base::sync_wi...
5
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, arr[200005]; cin >> n; if (n % 2 == 0) cout << -1 << endl; else { for (i = 0; i < n; i++) cout << i << " "; cout << endl; for (i = 0; i < n; i++) cout << i << " "; cout << endl; for (i = 0; i < n; i++) cout << (2 ...
5
#include <bits/stdc++.h> using namespace std; int x, y, a[10000], sum = 0, b[1000], n, mx, mnloc, mxloc, one = 0, answer, l = 1, ans = 0, one2 = 0; int main() { cin >> x; for (int i = 0; i < x; i++) { cin >> n; if (n == 1) { one++; if (mx > 0) mx -= 1; } else { mx +...
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, i; string s; cin >> n >> k >> s; if (n == 1 && k != 0) { cout << 0 << "\n"; return 0; } if (s[0] != '1' && k != 0) { s[0] = '1'; k--; } for (i = 1; i < n; i++) { ...
2
#include <bits/stdc++.h> using namespace std; long long dp[2005][2005]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k, s = 0; cin >> n >> k; for (long long i = 1; i <= n; i++) dp[1][i] = 1; for (long long i = 2; i <= k; i++) { for (long long j = 1; j <= n; j++)...
6
#include <bits/stdc++.h> long long int precom[5005][5005]; long long int dp[5005]; int main() { memset(precom, 0, sizeof precom); int n, f = 0; scanf("%d", &n); char inp[n + 5]; precom[1][1] = 1; long long int ans = 0, flag = 0; for (int a = 0; a < n; a++) { getchar(); scanf("%c", &inp[a]); } ...
10
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; using ll = long long; using vll = vector<ll>; using pii = pair<int, int>; using pll = pair<ll, ll>; void buff() { ios::sync_with_stdio(false); cin.tie(nullptr); } constexpr ll MOD = 1e9 + 7; inline ll pow_mod(ll a, ll b, l...
14
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> a, pair<int, int> b) { return (a.first < b.first) || (a.first == b.first && a.second > b.second); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; t = 1; while (t--) { int n; cin >> n; string s; ...
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 5 + 100; vector<int> ans[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m, col = 10000; cin >> n >> m; for (int i = (int)0; i < (int)m; ++i) { int u, v; cin >> u >> v; --u; --v; a...
9
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; int a[200005]; long long f[200005]; long long pot(long long first, long long second) { if (second == 0) return 1; long long sol = pot(first, second / 2); sol = (sol * sol) % MOD; if (second % 2) sol = (sol * first) % MOD; return so...
16
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, c; c = 0; cin >> n; int a[n]; for (i = 0; i < n; i++) cin >> a[i]; for (i = 1; i < n - 1; i++) { if (a[i] == 0 && a[i - 1] == 1 && a[i + 1] == 1) { a[i + 1] = 0; c++; } } cout << c; }
2
#include <bits/stdc++.h> using namespace std; long long a[210], b[210]; long long dp[210][10010]; long long n, m, x, ans; void djj_lxy() { ios::sync_with_stdio(false); cin.tie(0); memset(dp, -1, sizeof dp); dp[0][0] = 0; cin >> n >> m; for (register long long i = 1; i <= n; i++) { cin >> x; while (x...
13
#include <bits/stdc++.h> using namespace std; int main() { int arr[6] = {1, 2, 3, 4, 5, 6}; int a, b; int cnt1 = 0, cnt2 = 0, cnt3 = 0; cin >> a >> b; for (int i = 0; i < 6; i++) { if (abs(arr[i] - a) == abs(arr[i] - b)) { cnt2 += 1; } if (abs(arr[i] - a) < abs(arr[i] - b)) { cnt1 += 1...
0
//2021 April 10 RAJIV HARLALKA #include <bits/stdc++.h> #define ll long long #define en "\n" #define f first #define s second #define pb push_back #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; int main() { fast ll t; cin>>t; while(t--) { ll n; ...
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m, r; cin >> n >> m >> r; int s = 1e4; for (int i = 0; i < n; i++) { int inp; cin >> inp; s = min(s, inp); } int b = 0; for (int i = 0; i < m; i++) { int inp; cin >> inp; b = max(b, inp); } int ans = 0; for (in...
0
#include <bits/stdc++.h> using namespace std; int vis[200500]; int t[200500]; stack<int> s; queue<int> a; int main() { int n, k; cin >> n >> k; for (int i = 0; i < k; i++) { cin >> t[i]; vis[t[i]]++; a.push(t[i]); } int btop = 1; while (btop <= n) { if (!s.empty() && s.top() == btop) { ...
12
#include <bits/stdc++.h> using namespace std; vector<vector<int> > A(55, vector<int>(2e4 + 5)), S(55, vector<int>(2e4 + 5)); vector<vector<int> > DP(2, vector<int>(2e4 + 5)), LR(2, vector<int>(2e4 + 5)), RL(2, vector<int>(2e4 + 5)); int N, M, K; int PS(int n, int l, int r = -1) { if (r == -1) r = l + K - 1; ret...
15
#include<bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define pi (3.141592653589) #define pb push_back #define mp make_pair #define INF 1000000000 #define MOD 1000000007 #define vll vector<ll> #define vpll vector<pair<ll,ll> > #define pll pair<ll> #define ff first #define ss secon...
9
#include <bits/stdc++.h> #include <string.h> using namespace std; #define ar array #define ll long long #define pb push_back #define fr(i,n) for(int i=0;i<n;i++) const int MAX_N = 1e5 + 1; const int MOD = 1e9 + 7; const int INF = 1e9; const ll LINF = 1e18; ll power...
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 10; int k, m, n, tot; int a[MAXN], ad[MAXN][2], ts[MAXN]; struct Node1 { int idx, type, obj, val; bool operator<(Node1 x) const { if (obj != x.obj) return obj < x.obj; if (type != x.type) return type < x.type; return val > x.val; ...
20
#include <bits/stdc++.h> using namespace std; struct edge { int next, to, w; } e[105 * 105]; bool notprime[32000 + 5]; int n, m, s, t, ans = 0, pcnt, p[3500], a[105], temp[105], x[105], y[105]; int ecnt = 1, head[105]; int tag[105]; bool visit[105]; void init() { for (int i = 2; i <= 32000; i++) { if (!notprime...
13
#include <bits/stdc++.h> template <typename T> inline T &read(T &x) { register char c = getchar(); while ((c < '0' || c > '9') && c != '-') c = getchar(); register bool flag = c == '-'; x = flag ? 0 : c - '0'; while ((c = getchar()) >= '0' && c <= '9') (x *= 10) += c - '0'; if (flag) x = -x; return x; } c...
18
#include <bits/stdc++.h> using namespace std; int N, M, P; int a[200000]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); vector<int> ans; map<int, int> bMap; cin >> N >> M >> P; for (int i = 0; i < N; i++) { cin >> a[i]; } for (int i = 0; i < M; i++) { int tmp; cin >> t...
11
#include <bits/stdc++.h> using namespace std; const int N = (int)1e4 + 11; const int M = 33; using pii = pair<int, int>; using vpii = vector<pii>; int x[N], y[N]; int lb(int x) { return x & (-x); } int y_comb[M]; vpii gauss(int x[], int n, int &b) { vpii ret; for (int i = 0; i < n; i++) { for (int j = 0; j < b;...
19
#include <bits/stdc++.h> using namespace std; const int64_t MOD = 998244353; int64_t powmo(int64_t a, int64_t b) { int64_t r = 1; for (; b; b >>= 1) { if (b & 1) r = r * a % MOD; a = a * a % MOD; } return r; } int64_t mdiv(int64_t a, int64_t b) { return a * powmo(b, MOD - 2) % MOD; } bitset<300005> like...
18
#include <bits/stdc++.h> using namespace std; int main() { long n, m; cin >> n >> m; long a(2 * n), b(3 * m); for (int p = 6; p <= a && p <= b; p += 6) { if (a <= b) a += 2; else b += 3; } if (a > b) cout << a; else cout << b; return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { char a[101], b[101]; int i, l, j = 0, c = 0, k, p; cin >> a; l = strlen(a); for (i = 0; i < l; i++) { if (a[i] == 'Q' || a[i] == 'A') { b[j] = a[i]; j++; } } for (i = 0; i < j; i++) { if (b[i] == 'Q') { for (k = i + 1...
0
#include <bits/stdc++.h> using namespace std; bool isprime(int n) { if (n == 1) return false; if (n == 2) return true; for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } void answer(int f) { if (f) { cout << "YES"; } else { cout << "NO"; } cou...
1
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); long long sum = 0; long long n; cin >> n; vector<long long> arr(n), pre(2 * n + 1); for (long long i = 0; i < n; i++) { cin >> arr[i]; } for (long long i = 0, k = 0; i < n; i++, k = (...
13
#include <bits/stdc++.h> using namespace std; int main() { string s, ns; int n, i, num; while (cin >> n >> s) { for (i = 0; i < n; i++) { if (s[i] != '0' && s[i] != '1') ns += s[i]; } n = ns.size(); s.clear(); for (i = 0; i < n; i++) { if (ns[i] == '4' || ns[i] == '8' || ns[i] == '...
6
#include <bits/stdc++.h> using namespace std; char s[10][10]; int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) scanf("%s", s[i]); int idx[10] = {0, 1, 2, 3, 4, 5, 6, 7}, min1, max1, ans = (int)1e9, s1; do { min1 = int(1e9); max1 = -1; for (int i = 0; i < n; i++) { s...
6
#include <bits/stdc++.h> using namespace std; long long sum[300005], b[300005], n, ans, k, ansi, ansj; int search(int a) { int left, right, mid; left = 0; right = n; mid = (left + right) >> 1; while (left < right) { if (sum[mid] >= a) { right = mid; } if (sum[mid] < a) { left = mid + 1...
8
#include <bits/stdc++.h> using namespace std; int x, n; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> x; if (x % 2 != 0) { cout << "First"; return 0; } } cout << "Second"; }
3
#include <bits/stdc++.h> using namespace std; long long a[100005][3]; long long s[100005], m, n; long long fun(long long ans) { int l = 1, r = m, mid; while (l <= r) { mid = (l + r) / 2; if (s[mid] >= ans && s[mid - 1] < ans) return mid; else if (s[mid] < ans) l = mid + 1; else r =...
8
#include <bits/stdc++.h> using namespace std; long long int pows[1000100]; long long int phi[1000010]; void init() { for (int i = 0; i < 1000010; ++i) { phi[i] = i; } for (int i = 2; i < 1000010; ++i) { if (phi[i] == i) { for (int j = i; j < 1000010; j += i) { phi[j] -= phi[j] / i; } ...
13
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; int n; bool vis[220000]; int dcmp(double x) { if (fabs(x) < eps) return 0; return x < 0 ? -1 : 1; } struct Point { double x, y; int id; Point() {} Point(double _x, double _y, int _z) : x(_x), y(_y), id(_z) {} Point operator-(const Poin...
18