solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> #define IO ios_base::sync_with_stdio(false); #define TT cin.tie(NULL); cout.tie(NULL); using namespace std; #include<cmath> #define fr(i,a,c) for(int i=a;i<c;i++) #define pi 3.1415926536 #define yes cout<< "YES"<<"\n" #define no cout<< "NO"<<"\n" #define line cout<<"\n" #define all(v) (v)....
7
#include <bits/stdc++.h> int n; int query(int x1, int y1, int x2, int y2) { printf("? %d %d %d %d\n", x1, y1, x2, y2); int x = 0; fflush(stdout); scanf("%d", &x); return x; } int main() { scanf("%d", &n); int x1 = 1, y1 = 1, x2 = n, y2 = n; int x3 = 1, y3 = 1, x4 = n, y4 = n, cnt; for (int l = 1, r = ...
14
#include <bits/stdc++.h> using namespace std; pair<int, int> inc[1000], qwerqwer[1000]; int n, st[1000], asdfasdf[1000], delt, iut, dut; int main() { scanf("%d", &n); for (int i = (0); i < (n); i++) { scanf("%d %d", st + i, &delt); asdfasdf[i] = st[i] + delt; st[i] = 1000 * st[i] - i; asdfasdf[i] = ...
14
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long power(long long num, long long r) { if (r == 0) return 1; if (r == 1) return num % mod; long long ans = power(num, r / 2) % mod; if (r % 2 == 0) { return (ans * ans) % mod; } return (((ans * ans) % mod) * num) % mod; } long...
10
#include <bits/stdc++.h> using namespace std; vector<int> v[5005]; int n, m; int x; int dp[5005]; int dist(int a, int b) { if (b >= a) return b - a; else return b - a + n; } bool cmp(int xx, int yy) { return dist(x, xx) < dist(x, yy); } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int a...
10
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; long long ar[n]; for (int i = 0; i < n; cin >> ar[i++]) ; long long maxx = 1; while (maxx < 1e16) maxx *= k; while (maxx)...
6
#include <bits/stdc++.h> using namespace std; struct LCA { int par, val1, val2; LCA() {} } lca[100001][21]; int n, m; vector<int> AdjList[100001]; int h[100001], lg2[100001]; pair<int, int> f_d[100001][3]; int f_u[100001]; void dfs(int u, int p) { for (int v : AdjList[u]) if (v != p) { lca[v][0].par = u...
20
#include <bits/stdc++.h> using namespace std; vector<long long> v1, v2; bool vis[1000000]; bool vis1[1000000]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long total; cin >> total; bool mark = 1; for (long long i = 1; i <= total; i++) { long long val; cin >> val; i...
2
#include <bits/stdc++.h> using namespace std; const int maxN = 300005; const int mod = 1000000007; int n, m; int s[maxN]; int cnt[maxN][21]; bool flag[maxN]; int primes, prime[maxN]; int Next[maxN]; long long c[maxN], sum[maxN]; long long ans; void Readln() { scanf("%d\n", &n); for (int i = 1; i <= n; i++) { sc...
23
#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'...
10
#include <bits/stdc++.h> using namespace std; inline bool scan_d(int &num) { char in; bool IsN = false; in = getchar(); if (in == EOF) return false; while (in != '-' && (in < '0' || in > '9')) in = getchar(); if (in == '-') { IsN = true; num = 0; } else num = in - '0'; while (in = getchar(),...
10
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, tot; cin >> a >> b >> c; tot = a + b; a = a < b ? a : b; b = tot - a; if (c % b == 0 || c % a == 0) { cout << "Yes"; return 0; } while (c - a >= 0) { c -= a; if (c % b == 0) { cout << "Yes"; return 0; ...
3
#include <bits/stdc++.h> using namespace std; void guan() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const int maxn = 2020; int n, m; typedef struct Point { long long x, y; double deg; Point(){}; Point(long long _x, long long _y) { x = _x; y = _y; } Point operator-(const P...
19
#include <bits/stdc++.h> using namespace std; void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } template <class T1, class T2> inline void gmax(T1 &a, T2 b) { if (b > a) a = b; } template <class T1, class T2> inline void gmin(T1 &a, T2 b) { if (b < a) a = b; ...
11
#include <bits/stdc++.h> using namespace std; int main() { int n, s, t, f, res = 0; scanf("%d %d", &n, &s); while (n--) { scanf("%d %d", &f, &t); res = max(res, t - s + f); } printf("%d", res + s); }
2
#include<iostream> #include<algorithm> #include<cstring> #include<functional> #include<vector> using namespace std; char s[110]; int main(){ int t;scanf("%d",&t); while (t--){ scanf("%s",s+1); printf("%d\n",strlen(s+1)); } }
0
#include <bits/stdc++.h> using namespace std; int main() { string x, y, z; int m; cin >> x >> y; for (int i = 0; i < x.size(); i++) { if (x[i] == y[i]) { z.push_back(x[i]); } else if (y[i] < x[i]) { z.push_back(y[i]); } else { cout << -1; return 0; } } cout << z; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long b, d, n, i, j, a, ar[100001]; long long s = 0, c, s1 = 0; scanf("%lld%lld", &n, &a); for (i = 0; i < n; i++) { scanf("%lld", &ar[i]); } if (n == 1) { printf("0\n", a); return 0; } sort(ar, ar + n); if (a >= ar[0] && a >= ...
7
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; long long count = 0, i; char x = s[0]; for (i = 0; i < s.length(); i++) { if (x < s[i]) { x = s[i]; count = 1; } else { if (x == s[i]) count++; }...
0
#include <bits/stdc++.h> using namespace std; int a[2010][2010], row[2010][2010], col[2010][2010]; int n; int main() { cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { char c; cin >> c; a[i][j] = c - '0'; } int ret = 0; memset(row, 0, sizeof row); memset(col, 0, ...
12
#include <bits/stdc++.h> using namespace std; int main() { int n, d; cin >> n >> d; int array[n]; for (int i = 0; i < n; i++) { cin >> array[i]; } long long count = 0; for (int i = 1; i < n; i++) { if (array[i] <= array[i - 1]) { int lcount = ceil((array[i - 1] + 1 - array[i]) / double(d)); ...
1
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int> t(N); for (int i = 0; i < N; i++) cin >> t[i]; t.push_back(3); vector<int> a; int cnt = 0; for (int i = 0; i < N; i++) { if (t[i + 1] == t[i]) cnt++; else { a.push_back(cnt + 1); cnt = 0; ...
1
#include <bits/stdc++.h> using namespace std; inline long long read() { long long ans = 0; char r = getchar(); bool neg = false; while (r < '0' || r > '9') { if (r == '-') neg = true; r = getchar(); } while (r >= '0' && r <= '9') { ans = ans * 10 + r - '0'; r = getchar(); } return (neg) ...
17
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<long long int> a(n), b(n); long long int mina = INT_MAX, minb = INT_MAX; long long int cnt = 0; for (int i = 0; i < n; i++) { ...
0
#include <bits/stdc++.h> using namespace std; int fun(int a) { if (a > 0) return a; return -a; } int min(int a, int b) { if (a > b) return b; return a; } int main() { int n, m, i, j, a, b; while (scanf("%d%d%d%d%d%d", &n, &m, &i, &j, &a, &b) != EOF) { if ((i == 1 && j == 1) || (i == n && j == 1) || (i =...
12
#include <bits/stdc++.h> using namespace std; void build(int st[], int a[], int ss, int se, int si) { if (ss > se) return; if (se == ss) { st[si] = a[ss]; return; } int mid = (ss + se) / 2; build(st, a, ss, mid, si * 2 + 1), build(st, a, mid + 1, se, si * 2 + 2); st[si] = st[si * 2 + 1] + st[si * 2 ...
16
#include <bits/stdc++.h> using namespace std; char s[1000005]; int a[1000005]; int main() { int i, l, r, n; gets(s); n = strlen(s); l = 0; r = n - 1; for (i = 0; i < n; ++i) { if (s[i] == 'l') { a[r] = i + 1; r--; } else { a[l] = i + 1; l++; } } for (i = 0; i < n; ++i...
4
#include <bits/stdc++.h> int a, b; int n; long long int fact[1000006]; long long int c[1000006]; int digits(int i) { int flag = 0; while (i != 0) { int rem = i % 10; if (rem != a && rem != b) { flag = 1; break; } i = i / 10; } return (flag + 1) % 2; } long long int power(long long in...
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; vector<int> a[MAXN]; int used[MAXN]; int ar[MAXN], vyv[MAXN]; int n, m, h, t; void print(int u, int v) { unordered_set<int> U, V, O; for (int to : a[u]) { if (to != v) U.insert(to); } for (int to : a[v]) { if (to != u) V.insert(to); ...
12
#include <algorithm> #include <iostream> #include <sstream> #include <string> #include <vector> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cctype> #include <cmath> #include <cstring> #include <list> #include <cassert> #include <climits> #inclu...
18
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const long long int MOD = 1e9 + 7; const long long int N_MAX = 100100; void solve() { long long int n; cin >> n; long long ...
7
#include <bits/stdc++.h> using namespace std; const int maxn = 2e6 + 100; const int mod = 1e9 + 7; const int inf = 1e9; int n, rnk[maxn], per[maxn], r2[maxn], p2[maxn], ps[maxn], cnt, psm[maxn], spm[maxn]; bool ok[maxn]; string s; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >>...
19
#include <bits/stdc++.h> using namespace std; int n; vector<int> A, B, aa, bb; int q[555][555]; int in[555][555]; int second, T; int f[555]; void er() { puts("Impossible"); exit(0); } bool prime(int x) { for (int i = 2; i < x; i++) if (x % i == 0) return 0; return 1; } void ae(int x, int y, int f) { q[x][...
15
#include <bits/stdc++.h> using namespace std; typedef struct trieNode *tnode; const int maxn = 3e5; int enc[maxn + 1]; struct trieNode { tnode child[2]; int freq; }; tnode getNode() { tnode newNode = new trieNode; for (int i = 0; i <= 1; ++i) newNode->child[i] = nullptr; newNode->freq = 0; return newNode; }...
10
#include <bits/stdc++.h> using namespace std; struct Value { long long int input; long long int output; }; long long int findOutput(long long int n) { long long int k = 0; while (n) { if (n & 1) return pow(2, k); k++; n >>= 1; } return 0; } void Solve() { long long int sum, limit; cin >> sum...
7
#include <bits/stdc++.h> using namespace std; int v[100001]; vector<pair<int, int>> lefttt, rightttt; int main() { int n, d, b; int sumaLeft = 0; int sumaRight = 0; cin >> n >> d >> b; for (int i = 1; i <= n; ++i) { cin >> v[i]; } for (int i = 1; i <= n; ++i) { if (sumaLeft + v[i] >= ((n + 1) / 2)...
15
#include <bits/stdc++.h> using namespace std; template <class T = int> T read() { T x = 0; char ch = 0; bool f = false; while (!isdigit(ch)) f |= (ch = getchar()) == '-'; for (; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ '0'); return f ? -x : x; } const int N = 1.1e6; const int X = 5.01e5; int n; int c...
24
#include <bits/stdc++.h> using namespace std; int32_t main() { long long n, m; cin >> n >> m; set<long long> used; vector<long long> ans; queue<pair<long long, long long> > q; for (long long i = 0; i < n; i++) { long long x; cin >> x; q.push({x, 0}); used.insert(x); } long long tot = 0; ...
10
#include <bits/stdc++.h> using namespace std; int main() { string s[1001]; int n, m, r[1001] = {0}, c[1001] = {0}; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s[i]; for (int j = 0; j < m; j++) { if (s[i][j] == '*') { r[i]++; c[j]++; } } } long long ans = 0; ...
8
#include <bits/stdc++.h> using namespace std; int k; string s; int l[111111], r[111111]; vector<int> pos[26]; void process() { for (int i = 0; i < s.size(); ++i) { pos[s[i] - 'a'].push_back(i); l[i] = r[i] = 0; } } bool del(int pos) { if (l[pos] + r[pos] + 1 >= k) return false; int pl = pos - l[pos] - 1...
11
#include <bits/stdc++.h> using namespace std; int main() { int d, n, m; cin >> d >> n >> m; vector<long long> pos(m + 1); vector<long long> price(m + 1); if (n >= d) { cout << "0"; return 0; } pos[0] = 0; price[0] = 0; vector<pair<long long, long long>> v; v.push_back(pair<long long, long lo...
14
#include <bits/stdc++.h> using namespace std; vector<int> solve(const vector<int> &a) { vector<int> d(a.size(), 0); for (int i = 0; i < a.size(); ++i) d[i] = i; vector<bool> visited(a.size(), false); queue<int> q; q.push(0); visited[0] = true; while (!q.empty()) { int v = q.front(); q.pop(); i...
8
#include <bits/stdc++.h> using namespace std; long long lmao(long long len, long long ind) { if (len == 1) return 0; long long bse = len / 4; long long res = 0; if (ind / bse == 0) { res = 0; res += lmao(len / 4, ind % bse); } else if (ind / bse == 1) { res = 2 * bse; res += lmao(len / 4, ind ...
14
#include <bits/stdc++.h> using namespace std; inline long long read() { char ch = getchar(); int nega = 1; while (!isdigit(ch)) { if (ch == '-') nega = -1; ch = getchar(); } long long ans = 0; while (isdigit(ch)) { ans = ans * 10 + ch - 48; ch = getchar(); } if (nega == -1) return -ans; ...
25
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } int result[n], count = 0; for (int i = 0; i < n; i++) { bool b = true; for (int j = 0; j < n; j++) { if (a[i]...
1
#include <bits/stdc++.h> using namespace std; unordered_map<long long, long long> mp[26]; int N, K; long long S; vector<long long> lft, rht; long long pow3[26], fact[26]; int main() { cin >> N >> K >> S; fact[0] = 1; for (int i = 0; i <= 25; i++) { pow3[i] = pow(3, i); } for (long long i = 1; i <= 19; i++...
13
#include <bits/stdc++.h> using namespace std; const int MaxN = 105; int Perm[MaxN]; long long n, Length; long long Ans = 1; bool used[MaxN]; void TransposLength(int pos) { if (used[pos]) { return; } used[pos] = true; ++Length; TransposLength(Perm[pos]); } inline long long GCD(long long a, long long b) { ...
8
#include <bits/stdc++.h> using namespace std; const long long lzy = 998244353; const long long maxn = 2005; long long n, A, B, p, dp[maxn], f[maxn], g[maxn][maxn]; namespace sl { const long long maxn = 1e6 + 5; long long sum1[maxn], sum2[maxn], inv[maxn]; inline void sager_great_epic() { sum1[0] = sum2[0] = inv[0] = ...
20
#include <bits/stdc++.h> using namespace std; int a[1005][1005] = {0}; int b[1005][1005] = {0}; char s[1005]; int xy[50][3] = {0}; int tk; int dir[4][2] = {-1, 0, 1, 0, 0, 1, 0, -1}; int cmpint(const void *a, const void *b) { if (((int *)a)[0] == ((int *)b)[0]) return ((int *)a)[1] - ((int *)b)[1]; return ((int *)a...
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 1013; int n, m; vector<string> vec; bool toDel[MAXN]; string ans; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) { string s; cin >> s; vec.push_back(s); } int num; cin >> num; num = num - 1; ans = vec[num]; toDel[num] =...
5
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; int get_rounds(int atk, int hp) { if (atk <= 0) return INF; return hp / atk + ((hp % atk) > 0); } int main(int argc, const char* argv[]) { srand((unsigned)time(0)); int hy, ay, dy, hm, am, dm, ph, pa, pd; cin >> hy >> ay >> dy >> hm >> ...
10
#include <bits/stdc++.h> int main() { double x = 0, y = 0, p = 1; int k; int m, n, h; scanf("%d%d%d", &n, &m, &h); int sum[m + 1]; for (k = 1; k <= m; k++) { scanf("%d", &sum[k]); } for (k = 1; k <= m; k++) { y += sum[k]; } y -= sum[h]; x = sum[h]; if (x + y < n) { printf("-1.0"); ...
8
#include <bits/stdc++.h> using namespace std; long long b[100005]; long long s; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { long long a; cin >> a; s += a; } for (int i = 1; i <= n; i++) cin >> b[i]; sort(b + 1, b + n + 1); if (b[n] + b[n - 1] >= s) cout << "YES"; else ...
1
#include <bits/stdc++.h> using namespace std; vector<vector<int> > e, ne; int n, m; int dfn[500000], low[500000], TIME, START; int c, belong[500000], ins[500000]; stack<int> dfsn; void Tarjan(int v) { dfsn.push(v); ins[v] = 1; dfn[v] = low[v] = ++TIME; for (int i = 0; i < e[v].size(); i++) { if (dfn[e[v][i]...
14
#include <bits/stdc++.h> using namespace std; int n, m, ans[100010], tot = 1; set<int> E[100010]; map<set<int>, vector<int> > M; inline int read() { register int w = 0, X = 0; register char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) { X = (X << 1) + (X << ...
11
#include <bits/stdc++.h> using namespace std; long long digit(int num) { long long ans = 0; while (num) { ans += 1; num /= 10; } return ans; } bool nine(long long x) { while (x) { if (x % 10 != 9) return 0; x /= 10; } return 1; } long long construct(int num) { long long ans = 0; while ...
10
#include <bits/stdc++.h> using namespace std; int main() { int n, k; scanf("%d%d", &n, &k); int A[n], i; vector<int> B(k, 0); for (i = 0; i < n; i++) { scanf("%d", &A[i]); B[A[i] - 1] += 1; } int c = 0, a = 0; for (i = 0; i < k; i++) { c += B[i] % 2; a += (B[i] / 2) * 2; } a += (c + ...
2
#include <bits/stdc++.h> using namespace std; void in() { return; } template <typename T, typename... Types> void in(T &a, Types &...b) { cin >> (a); in(b...); } void o() { return; } template <typename T, typename... Types> void o(T a, Types... b) { cout << (a); cout << ' '; o(b...); } bool sortin(const pair<...
4
#include <bits/stdc++.h> using namespace std; const long long mxn = 1e5 + 5; const long long mxm = 5e5 + 5; const long long INF = 0x3f3f3f3f; const long long mod = 1e9 + 7; const double eps = 1e-7; long long gcd(long long x, long long y) { if (y == 0) return x; else return gcd(y, x % y); } long long qpow(lo...
8
#include <bits/stdc++.h> using namespace std; int main() { int n, d, h; cin >> n >> d >> h; bool check = 0; if (d == 1 && n != 2) check = 1; if (d > (2 * h)) check = 1; if (check) { cout << "-1" << endl; return 0; } for (int i = 1; i <= h; i++) { cout << i << " " << i + 1 << endl; } bool...
8
#include <bits/stdc++.h> using namespace std; int n, op, tot; int son[20000010][2]; int cnt[20000010]; vector<int> vec[100010]; void insert(int k, int x) { int now = k; cnt[now] = min(cnt[now], x); for (register int i = 18; i >= 0; i--) { int p = (1 & (x >> i)); if (son[now][p]) { now = son[now][p];...
14
#include <bits/stdc++.h> using namespace std; struct node { int a, b; friend bool operator<(node x, node y) { if (x.b < 0 && y.b < 0) return x.a + x.b > y.a + y.b; return x.a < y.a; } } a[105]; vector<node> b, c; int n, m; int f[105][300005]; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) { ...
13
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const int N = 1e5 + 10; const int MOD = 1e9 + 7; long long in[N], ex[N], col[N]; vector<int> child[N]; long long fex(int r); long long fin(int r) { if (in[r] != -1) return in[r]; if (col[r]) { in[r] = 1LL; for (long long i = 0; i < (c...
12
#include <bits/stdc++.h> using namespace std; const long double PI = acos(-1), EPS = 1e-8; const int mod = 1000000007; const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1}; const int dc[]{0, 1, 1, 1, 0, -1, -1, -1}; void run() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } string s, p; vector<pair<int, int>...
14
#include <bits/stdc++.h> using namespace std; int a[300005]; int asum[300005]; vector<pair<int, int> > recent; int F[300005]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%d", a + i); asum[i + 1] = (asum[i] + a[i]) % 1000000009; } F[1] = 1; F[2] = 1; for (int ...
16
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); long long t; cin >> t; while (t--) { long long n; cin >> n; long long sum = 0; vector<long long> v; for (long long i = 0; i < n; i++) { long long x; ...
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 200000 + 5; int n, m, a[20][10000 + 10], vis[30], lk[30][30], ck[30][30]; vector<int> edge[20]; int s, tag, mask[maxn][20]; int dfs(int u, int cnt, int mk) { if (cnt == n) { if (ck[s][u] < tag) return 0; return 1; } mk...
12
#include <bits/stdc++.h> using namespace std; int n, m, a[100000 + 1]; vector<int> b[100000 + 1]; int main(int argc, char *argv[]) { ios ::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= m; ++i) cin >> a[i]; for (int i = 1; i <= m; ++i) { if (i > 1 && a[i - 1] != a[i]) b[a[i - 1]].push_back(a[i]...
10
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; long long rd() { long long x = 0, f = 1; char ch = getchar(); 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;...
1
#include <bits/stdc++.h> using namespace std; const int mxa = 1000000; int T; int N, A[105]; vector<int> dv; int D; int F[mxa + 5]; int B[105]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> T; for (int t = 1; t <= T; t++) { cin >> N; for (int i = 1; i <= N; i++) cin >> A[i]; sort(A + ...
8
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const long long mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const int N = 1e2 + 10; long long qpow(long long x, long long y, long long MOD) { long long a = 1; while (y) { if (y & 1) a = a * x % MOD; x = x * x % MOD; y >>= 1; } ...
12
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); long long int n, a, b, diff = INT_MAX; cin >> n; for (long long int i = 1; i <= sqrt(n); i++) { for (long long int j = i; i * j <= n; j++) { if (i * j == n && j - i < diff) { a = i, b = j, diff = j - i; ...
0
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, n, a[3][2], res[3], temp = 0; cin >> n; for (i = 0; i < 3; i++) { for (j = 0; j < 2; j++) { cin >> a[i][j]; } } for (i = 0; i < 3; i++) { res[i] = a[i][0]; } for (i = 0; i < 3; i++) { temp = res[i] + temp; }...
3
#include <bits/stdc++.h> using namespace std; vector<int> a, b, c, d; struct fig { long long x, y; char c; fig(long long x, long long y, char c) : x(x), y(y), c(c){}; }; int main() { vector<vector<fig>> point(3, (vector<fig>(3, fig(0, 0, 'k')))); int n; long long x, y; cin >> n >> x >> y; for (int i = 0...
9
#include <bits/stdc++.h> using namespace std; int n; string temp; long long k; vector<int> b, s; void make(vector<pair<long long, int>> d) { long long sum = 0; for (auto x : d) sum += x.first; int c = 0; vector<int> a; while (sum > 0) { c += sum % 10; a.push_back(sum % 10); sum /= 10; } revers...
17
#include <bits/stdc++.h> using namespace std; int n, m, c, first; int a[1005]; int main() { for (int l = 0; l < 1002; ++l) a[l] = -1; cin >> n >> m >> c; for (int l = 0; l < m; ++l) { cin >> first; if (first <= c / 2) { for (int l2 = 1; l2 <= n; ++l2) { if (a[l2] == -1 || a[l2] > first) { ...
12
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-11; const int INFINITE = 0x3f3f3f3f; template <class T> inline void checkmin(T &a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T &a, T b) { if (b > a) a = b; } template <class T> inline T sqr(T x...
5
#include <bits/stdc++.h> #pragma warning(disable : 4786) #pragma warning(disable : 4996) template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1LL; for (; e > 0LL; e >>= 1LL) { if (e & 1LL) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T modinverse(T a,...
9
#include <bits/stdc++.h> using namespace std; vector<vector<int> > vv; long long dp[200001][2]; const int INF = 1e9 + 7; vector<int> a; void dfs(int v, int p) { dp[v][0] = 1; dp[v][1] = 0; for (int i = 0; i < vv[v].size(); i++) { int to = vv[v][i]; if (to != p) { dfs(to, v); dp[v][1] *= dp[to]...
12
#include <iostream> using namespace std; int main() { int t; uint64_t n; uint64_t s; cin >> t; while (t--) { s = 0; cin >> n; for (int i = 0, temp = 0; i < n; i++) { cin >> temp; s += temp; } if (n == 1) { cout << 0 << '\n'; } else { cout << (uint64_t)((s % n) * (n - (s % n))) << '\...
1
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a < b) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } int main() { int size = 0; cin >> size; vector<int> in(size); for (int i = 0; i < size; i++) { cin >> in[i]; } sort(in.begin(), in.end(...
8
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int mod = 1e9 + 7; int n, q, f[N]; char c; int binPow(int x, int y) { int ans = 1; while (y > 0) { if (y & 1) ans = (1LL * ans * x) % mod; x = (1LL * x * x) % mod; y >>= 1; } return ans; } int main() { ios::sync_with_stdio(...
8
#include <bits/stdc++.h> using namespace std; int n; char s[2000]; long long dp[2000]; int isp[2000][2000]; bool isPal(int i, int j, int type) { int b, e; if (type == 1) b = i - j, e = i + j; else b = i - j + 1; e = i + j; if (isp[b][e] >= 0) return (bool)isp[b][e]; if (b == e) return isp[b][e] = tr...
7
#include <bits/stdc++.h> using namespace std; int a, b, c, sums; int x, y, z; int main() { while (cin >> a >> b >> c) { sums = a + b + c; if (sums % 2 != 0 || sums < 2 * a || sums < 2 * b || sums < 2 * c) { cout << "Impossible" << endl; continue; } sums /= 2; x = sums - c; y = sums...
4
#include<bits/stdc++.h> using namespace std; #define endl "\n" int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int t; scanf("%d", &t); while (t--) { string s; cin >> s; int x = s.size(); for (int i = 0; i < x; i++) { if (i % 2 == 0) { if (s...
0
#include <bits/stdc++.h> using namespace std; int main() { int a[2001]; int n, k; cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i]; int cnt = 0; for (int i = 0; i < n; i++) if (5 - a[i] >= k) cnt++; int ans = cnt / 3; cout << ans; return 0; }
0
/* -- author: Devansh_0007 (CF) -- */ #include <bits/stdc++.h> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; #define endl "\n" #define vi vector<int> #define vll vector<ll> #define for0(i, n) for (int i = 0; i < n; i++) #define for1(i, n) for (int i = 1; i <= n; i++) ...
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, x, y; cin >> n >> x >> y; string s; cin >> s; int ans = 0, ind = s.length() - 1; for (int i = 0; ind >= 0 && i < y; ind--, i++) { if (s[ind] != '0') ans++; } if (s[ind--] != '1') an...
3
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f, mod = 1000000007; const double pi = 3.1415926535897932, eps = 1e-6; int n, m, a[200005], siz[200005], cnt, f[200005], fa[200005], ans, p; bool g[200005], vis[200005]; vector<pair<int, int> > v[200005]; vector<int> ord, an, gx[200005]; int gcd(int...
15
#include <bits/stdc++.h> using namespace std; int ans = 0; bool vis[1001] = {}; bool called[1001] = {}; vector<int> data; void f(int x, int cnt, vector<int> &v) { if (called[x]) return; called[x] = 1; if (x == 0) { ans = max(ans, cnt); data = v; return; } for (int i = 1; i <= x; i++) { if (vis...
2
#include <bits/stdc++.h> using namespace std; int a[1100000], m, n, b[1100000], v[1100000]; int main() { cin >> m; for (int x = 2; x <= m; x++) { cin >> a[x]; } for (int x = 2; x <= m; x++) { b[a[x]]++; v[a[x]]++; if (a[a[x]] != 0) { if (a[x] <= x) { b[a[a[x]]]--; } a[a...
4
#include <bits/stdc++.h> const int N = 1e3 + 5; const int MOD = 998244353; int n, k, a[N], p[N], f[N][N], sum[N][N]; void add(int &x, int y) { (x += y) >= MOD && (x -= MOD); } int solve(int x) { for (int i = 0; i <= k; i++) { for (int j = 0; j <= n; j++) { f[i][j] = sum[i][j] = 0; } } for (int i = 1...
17
#include <bits/stdc++.h> using namespace std; int main() { int n, k, s = 0; cin >> n >> k; for (int i = 0, a, b; i < n; ++i) { cin >> a >> b; s += b - a + 1; } if (s % k == 0) cout << 0; else cout << k - s % k; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); for (int i = 0, j = n - 1; i <= j; i++, j--) { if (i == j) cout << v[i] << endl; else cout << v[i] << " " << v[j] << " "; } ...
2
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { int id = (n + 2) / 3; int s = n % 3; if (s == 0) s = 1; else s = 0; cout << 2 * id - 1 + s << endl; } }
0
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!a) return b; return gcd(b % a, a); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } long long fac(long long a) { long long ans = 1; for (long long i = 1; i <= a; i++) ans *= i; return ans; } b...
4
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5; long long a[maxn]; long long b[20][30]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); for (long long i = 1; i <= 10; i++) { b[1][i] = i; b[2][i] = i * 2; b[3][i] = i * 3; b[4][i] = i * 4; b[5][i] = i...
4
#include <bits/stdc++.h> using namespace std; vector<int> v[3001]; bool flag, vis[3001]; int pi[3001], detectedHere; int dfs(int u) { vis[u] = true; int sz = (int)(v[u].size()), temp; for (int i = 0; i < sz; ++i) { if (v[u][i] == pi[u]) continue; pi[v[u][i]] = u; if (vis[v[u][i]]) { flag = true;...
8
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") const int N = 200005; long long n; int main() { scanf("%lld", &n); if (n < 5) return 0 * printf("%lld", n * (n - 1) / 2); int cnt = 0; long long sum = 1, ans = 0, sum1 = 0; for (long long num = 5; num <= n; ...
10
#include <bits/stdc++.h> using namespace std; const long long N = 5000 + 10; int OO = 1e9; int Mod = 1e9 + 7; struct SuffixArray { int n; string str; vector<int> cnt, order, tmp; vector<int> ra[21]; SuffixArray(string s) { n = s.size(); str = s; cnt.assign(max(n + 1, 200), 0), order.assign(n, 0), ...
12