solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; struct Node { int xl, yl, xr, yr, p; } a[100500 << 2]; struct Point { int x, y, p; } p[100500]; bool cmpX(const Point &A, const Point &B) { return A.x < B.x; } bool cmpY(const Point &A, const Point &B) { return A.y < B.y; } void build(int l, int r, int u, bool kd) { i...
17
#include <bits/stdc++.h> using namespace std; const int N = 500000; bool ok[N + 10]; int dist[N + 10]; int main() { int n, d; cin >> n >> d; vector<int> c(n); for (int i = 0; i < (n); i++) cin >> c[i]; ok[0] = 1; for (int i = 0; i < (n); i++) { for (int j = N; j >= c[i]; j--) { if (ok[j - c[i]]) o...
14
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int k = 1; k <= n; k++) { if (k * (k + 1) / 2 <= n && n < (k + 1) * (k + 2) / 2) { if (n - k * (k + 1) / 2 > k) { cout << (k + 1) << endl; for (int j = 1; j <= k; j++) { cout << j << " "; }...
2
#include <bits/stdc++.h> using namespace std; int n, k, mark[100010] = {0}; int main() { scanf("%d%d", &n, &k); int l = 1, r = n + n; if (k != 0) { printf("%d %d ", 1, 1 + k); mark[1] = mark[1 + k] = 1; l++; } while (l <= r) { while (mark[l]) l++; while (mark[r]) r++; if (l == r) ...
6
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; inline long long cmod(long long x) { x %= mod; return x < 0 ? x + mod : x; } long long bigmod(long long a, long long b) { if (b == 0) return 1; if (b & 1) return cmod(bigmod(a, b - 1) * a); long long m = bigmod(a, b >> 1); retur...
21
#include <bits/stdc++.h> using namespace std; long long int tree_max[4 * 100009], tree_sum[4 * 100009]; int a[100009]; void build_sum(int node, int start, int end) { if (start == end) { tree_sum[node] = a[start]; return; } int mid = (start + end) >> 1; build_sum(2 * node + 1, start, mid); build_sum(2 ...
15
#include <bits/stdc++.h> using namespace std; const int mod = (int)1e9 + 7; int n, m, k; vector<int> v[200010]; int x[100010], y[100010]; int ans[200010]; void dfs(int u, int fa) { ans[u] = 3 - fa; for (int i = 0; i < (int)v[u].size(); i++) { if (ans[v[u][i]]) continue; dfs(v[u][i], ans[u]); } } int main(...
18
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); long long n, ans = 0; cin >> n; if (log10(n) < 1) { ans = n; } else if (log10(n) < 2) { ans = 9 + (n - 9) * 2; } else if (log10(n) < 3) { ans = 9 + 90 * 2 + (n - 99) * 3; } else if (log10(n) < 4) { ans ...
4
#include <bits/stdc++.h> using namespace std; int n, q; vector<pair<long long int, long long int> > a[100010]; long long int ds[100010], d[100010]; vector<pair<long long int, long long int> > st; long long int ans[100010]; long long int dfs(long long int x, long long int p) { long long int ret = 0; for (int i = 0; ...
20
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const long double eps = 1e-11; const long double pi = acos(-1); const int oo = 1 << 30; const long lon...
15
#include <bits/stdc++.h> using namespace std; template <class C> inline bool in_(const typename C::key_type& k, const C& A) { return A.find(k) != A.end(); } inline bool in_(const string& s, const string& S) { return S.find(s) != string::npos; } const int inf = 1e9; int main() { ios::sync_with_stdio(false); int ...
7
#include <bits/stdc++.h> using namespace std; int main() { int n, sum = 0, minimum = 0, sum_min = 0; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; sum = sum + a[i]; } sort(a, a + n); sum = sum / 2; for (int i = n - 1; i >= 0; i--) { sum_min = sum_min + a[i]; minimum++; ...
1
#include <bits/stdc++.h> using namespace std; string s; long long memo[20][2][10][10]; long long go(long long pos, long long par, long long f, long long l) { if (pos == s.size()) { if (f == l) { return 1; } return 0; } if (memo[pos][par][f][l] != -1) return memo[pos][par][f][l]; long long ans ...
7
#include <bits/stdc++.h> using namespace std; long long llabs(long long a, long long b) { long long res = a - b; return res >= 0 ? res : -res; } int main() { long long N, M, X, Y, MR, orgN, start, end, NR; scanf( "%l" "ld" "%l" "ld" "%l" "ld" "%l" "ld", &N, ...
15
#include <bits/stdc++.h> using namespace std; class Graph { public: Graph(int V) { this->V = V; G.resize(V); } void addEdge(int v, int w) { G[v].push_back(w); } void DFS(int s) { bool *visited = new bool[this->V]; for (int i = 0; i < this->V; i++) visited[i] = false; int *arrival = new int[...
2
#include <bits/stdc++.h> using namespace std; int mn[2222222], laz[2222222], cnt[2222222]; void create(int l, int r, int cur) { if (l == r) { cnt[cur] = 1; return; } int mid = (l + r) / 2; create(l, mid, cur * 2); create(mid + 1, r, cur * 2 + 1); cnt[cur] = cnt[cur * 2] + cnt[cur * 2 + 1]; } void up...
22
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int t, n, q; int sum[maxn]; int sum2[maxn]; map<int, vector<int> > a; void init() { a.clear(); } int sol(int l, int r) { int now = sum[r] + sum[l - 1]; int tp = lower_bound(a[now].begin(), a[now].end(), l) - a[now].begin(); return a[now][tp]...
14
#include <bits/stdc++.h> using namespace std; const int N = 1e5; long long arr1[N]; long long arr2[N]; long long vis[N]; map<long long, long long> cnt; vector<long long> arr; int main() { long long n, l; long long ans = 0; cin >> n; for (int i = 0; i < n; i++) { scanf("%I64d", arr1 + i); cnt[arr1[i]]++;...
9
#include <bits/stdc++.h> using namespace std; int node[2048]; int expo(int a, int b) { int res = 1; while (b) { if (b & 1) res = res * a; b >>= 1; a *= a; } return res; } int main() { ios::sync_with_stdio(0); int n, a; cin >> n; int nd = 2; while (cin >> a) node[nd++] = a; int M = nd - 1...
6
#include <bits/stdc++.h> using namespace std; map<string, int> mp; string s[1510]; int main() { int n, m; cin >> n >> m; int ans = 0; for (int i = 1; i <= n; i++) { cin >> s[i]; mp[s[i]]++; } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { string aim; for (int k = 0...
7
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { long long r; while (b != 0) { r = a % b; a = b; b = r; } return a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long fpow(long long n, long long k, int p = (int)1e9 + 7) { long l...
16
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long bigmod(long long base, long long power, long long mod) { if (power == 0) return 1; ...
5
#include<bits/stdc++.h> using namespace std; int t[1000005]; int n1,n2,n3,m1=1000000001,m2=m1,m3=m1; long long a,b,c,d,sum,sum1,sum2,sum3; int main() { cin>>n1>>n2>>n3; for (int i =0;i<n1;i++) { cin>>t[i]; sum+=t[i]; m1=min(m1, t[i]); sum1+=t[i]; } for (int i =n1;i<n1+...
11
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; string s = str; reverse(s.begin(), s.end()); cout << (str + s) << endl; }
2
#include <bits/stdc++.h> using namespace std; struct Edge { int v, id, next; } edge[400009]; struct node { int l, r; } p[200009]; int first[200009], tot, m; bool vis[200009], ed[400009]; int du[200009], ans[200009]; int X[200009], num; vector<int> G; map<int, int> mp; void addedge(int u, int v, int id) { edge[tot...
8
#include <bits/stdc++.h> using namespace std; int n, m, sx, sy; struct coord { int l; int u; }; coord fillCol(int posx, int posy) { int orx = posx; int ory = posy; cout << posx << " " << posy << endl; while (posx > 1) { posx--; cout << posx << " " << posy << endl; } posx = orx; while (posx < n...
3
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; char s[105][105]; cin >> n >> m; for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { cin >> s[i][j]; } } int flag = 1; for (long long i = 1; i < n; i++) { for (long long j = 0; j < m; j++) { ...
0
#include <bits/stdc++.h> using namespace std; int main() { int n, i, a[100100], b[100100], c[100100]; scanf("%d", &n); for (i = 0; i <= n - 1; i++) scanf("%d", &a[i]); for (i = 0; i <= n - 2; i++) scanf("%d", &b[i]); for (i = 0; i <= n - 3; i++) scanf("%d", &c[i]); sort(a, a + n); sort(b, b + n - 1); so...
3
#include <bits/stdc++.h> using namespace std; const int N = 310000; int n, m, a[N], b[N], p[N], smax[N], rnext[N]; vector<int> v[N]; priority_queue<int> q; void calc() { smax[n] = a[n]; for (int i = n - 1; i >= 1; i--) { smax[i] = max(smax[i + 1], a[i]); } for (int i = 1; i <= m; i++) { int l = 1, r = n...
16
#include <bits/stdc++.h> using namespace std; int sum; int a[60]; int n; int main() { int i, j, k, m; scanf("%d%d", &n, &k); for (i = 0; i < n; i++) scanf("%d", &a[i]); sum = 0; for (i = 0; i < n; i++) sum += a[i]; sort(a, a + n); int t = k; t--; printf("%d", n); for (i = 0; i < n; i++) printf(" %d"...
8
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int execute() { int n; cin >> n; vector<long long int> a(n), b(n), vals(n); for (int i = 0; i < n; i++) { cin >> a[i]; vals[i] = (n - i) * 1ll * (i + 1) * 1ll * a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } sort(...
8
#include <bits/stdc++.h> using namespace std; struct ww { vector<int> a; int ty, flag, cov; } a[510 * 8]; int i, j, k, n, m, t, an; int re[510], s[510 * 8], size[510 * 8]; char p[510][510]; inline void Cov(int x) { if (a[x].ty != 2 || a[x].flag) return; a[x].cov ^= 1; reverse(a[x].a.begin(), a[x].a.end()); } ...
22
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const long long inf = 1e13 + 10; struct edge_t { int v, to; }; vector<edge_t> E; int L[N]; void init() { E.clear(); memset(L, -1, sizeof(L)); } void add(int u, int v) { edge_t t = {v, L[u]}; L[u] = E.size(); E.push_back(t); } long long gc...
13
#include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <math.h> #include <map> #include <stack> using namespace std; const int maxn = 1e5 + 5; const int N = 55; char a[N]; int f[maxn]; void solve(){ int n; memset(f, 0, sizeof(f)); scanf("%d %s", &n, a); for(int i = 0; i < n; i++){ i...
0
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const int MAXM = 1e5; long long M; long long E[MAXM + 10]; long long modpow(long long b, long long p) { long long res = 1; while (p > 0) { if (p & 1LL) res = res * b % MOD; p >>= 1LL; b = b * b % MOD; } return res; } vector...
15
#include <bits/stdc++.h> using namespace std; int main() { char a[1000][1000]; int n, f; while (~scanf("%d", &n)) { f = 0; for (int i = 1; i <= n; i++) { scanf("%s", a[i]); } for (int k = 1; k <= n; k++) { for (int l = 1; l <= n; l++) { if (a[k][l] == 'X' && a[k - 1][l - 1] == ...
0
#include <bits/stdc++.h> using namespace std; long long gcd(long long n1, long long n2) { if (!n1) return n2; if (!n2) return n1; if (n1 % n2 == 0) return n2; return gcd(n2, n1 % n2); } long long powmod(long long base, long long exponent) { base %= 1000000007; long long ans = 1; while (exponent) { if ...
8
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; const int N = 255; int m, n, p, q; int i, j; int Qx[N * N + 5]; int Qy[N * N + 5]; int Qv[N * N + 5]; int sum[N][N]; bool mark[N][N]; bool avail[N][N]; char a[N][N]; inline bool Valid(int i, int j) { return (i && (i <= m...
11
#include <bits/stdc++.h> using namespace std; const short int N = 1000; short int n, m, ax[] = {0, 0, 1, -1}, ay[] = {1, -1, 0, 0}, man_dis[N][N]; bool vis[N][N], v[N][N], g[N][N]; void good() { for (short int i = 0; i < n; i++) { for (short int j = 0; j < m; j++) { bool f = 0; if (i and v[i][j] == v[...
12
#include <bits/stdc++.h> using namespace std; template <typename T> void Read(T &x) { char ch; bool neg = 0; x = 0; while (!isdigit(ch = getchar()) && ch != '-') ; if (ch == '-') { neg = 1; ch = getchar(); } do { x = x * 10 + ch - 48; } while (isdigit(ch = getchar())); if (neg) x = -x;...
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, a, b; cin >> n; int ans1 = 0; int ans2 = 0; for (int i = 1; i <= n; i++) { cin >> a; ans1 |= a; } for (int i = 1; i <= n; i++) { cin >> b; ans2 |= b; } cout << ans1 + ...
1
#include <bits/stdc++.h> using namespace ::std; const int Nmax = 4000 + 10; long long mod = 1000000000 + 7; long long comb[Nmax][Nmax]; long long s[Nmax][Nmax]; long long dp[Nmax]; int N; long long mod_pow(long long a, long long b, long long m) { long long res = 1; while (b != 0) { if (b % 2 == 1) { res =...
11
#include <bits/stdc++.h> using namespace std; template <class T> void fmax(T &a, const T &b) { if (a < b) a = b; } template <class T> void fmin(T &a, const T &b) { if (a > b) a = b; } template <class T> T sqr(const T &a) { return a * a; } int n, m; long long rec[33333]; int main() { cin >> n >> m; long long a...
19
#include <bits/stdc++.h> int main() { int n, a, i; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a); if (a % 2 != 0) { printf("First\n"); return 0; } } printf("Second\n"); return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.writ...
10
#include <bits/stdc++.h> using namespace std; int Q[300005], tot, s[300005], ans; bool vis[300005], op[300005]; long long dis[300005]; struct edge { int v, c, n, num; } e[300005 << 1]; inline void push(int u, int v, int c, int num) { e[++tot] = (edge){v, c, s[u], num}; s[u] = tot; } inline void bfs() { deque<in...
10
#include <bits/stdc++.h> using namespace std; const long long MO = 1e9 + 7; const long long INV_2 = 5e8 + 4; int main() { long long n, m; cin >> n >> m; long long ans = ((n % MO) * (m % MO)) % MO; for (long long i = 1, l, r; i <= min(n, m); i = r + 1) { l = i; r = n / (n / i); if (r >= m) r = m; ...
14
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= 1130; i++) printf("5"); printf("\n"); for (int i = 1; i <= 1129; i++) printf("4"); printf("5\n"); }
4
#include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0LL ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0LL) ret...
17
#include <bits/stdc++.h> void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } template <class T> inline void gmax(T &a, T b) { if (b > a) a = b; } template <class T> inline void gmin(T &a, T b) { if (b < a) a = b; } using namespace std; const int N = 0, M = 0, ...
13
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 8500; vector<int> calc(const vector<int> &p) { int n = ((int)(p).size()); vector<int> ret(n, -1); if (n <= 4) { for (int i = (0); i < (n); ++i) { int x = p[i]; ...
24
#include <bits/stdc++.h> using namespace std; struct Size { uint64_t n; uint64_t m; }; bool operator==(const Size& d1, const Size& d2) { return (d1.n == d2.n) && (d1.m == d2.m); } bool operator<(const Size& d1, const Size& d2) { if (d1.n < d2.n) { return true; } else { if (d1.n == d2.n) { if (d1...
11
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T, class U> ostream &operator<<(ostream &o...
10
#include <bits/stdc++.h> using namespace std; bool imp = false; void solve() { long long n = 0, m = 0, i = 0, j = 0, k = 0, c = 0, flag = 0, sum = 0; string s, t; cin >> n; long long a[n + 5]; for (i = 0; i < n; i++) { cin >> a[i]; k ^= a[i]; } if (n % 2 == 0 and k) { cout << "NO\n"; retur...
14
#include <bits/stdc++.h> using namespace std; map<char, int> m; int main() { string ar; cin >> ar; int l = ar.length(); for (int i = 0; i < l / 2; i++) { if (ar[i] != ar[l - i - 1]) { cout << l; return 0; } } for (int i = 0; i < l; i++) { m[ar[i]]++; } if (m[ar[0]] == l) { co...
1
#include <bits/stdc++.h> using namespace std; vector<int> adj[5005]; bool flag; int col[5005], grp[5005], g, cc[2][5005], tot; void dfs(int x, bool f) { for (int i = 0; i < adj[x].size(); i++) { int y = adj[x][i]; if (col[y] == -1) { grp[y] = g; col[y] = f ^ 1; cc[col[y]][g]++; dfs(y, ...
13
#include <bits/stdc++.h> using namespace std; const long long N = 1000010; long long a[7]; int main() { for (int i = 1; i <= 6; i++) scanf("%lld", &a[i]); long long cur = a[1] * 2 + 1, res = cur, l = 1, r = 1, fl = 0, fr = 0; while (!(fl && fr && l == a[5] && r == a[3])) { l++, r++; if (fl) cur--; ...
8
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long power(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % mod; a = (a * a) % mod; b >>= 1; } return ans; } void solve(int test_case) { string s; cin >> s; int n = s.length(); ...
9
#include <bits/stdc++.h> using namespace std; struct node { long long n, d, idx, s; friend bool operator<(node a, node b) { if (a.s != b.s) return a.s < b.s; return a.n * b.d < b.n * a.d; } }; long long k[100005] = {0}, b[100005] = {0}; vector<node> V; int main() { int n, i; long long K = 0, B = 0, pK...
11
#include <bits/stdc++.h> using namespace std; template <class t> inline t read(t &x) { bool f = 0; char c = getchar(); x = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); if (f) x = -x; return x; } template <class t> inline void wr...
18
#include <bits/stdc++.h> using namespace std; template <class T> bool isEven(T a) { return (a % 2 == 0); } template <class T> T sq(T a) { return (a * a); } template <class T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <class T> T lcm(T a, T b) { return (a / gcd(a, b)) * b; } template <class...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int N, M, K; long long ans; map<int, int> A[maxn << 1], B[maxn << 1]; map<pair<int, int>, int> S; int GO(int X, int Y, int D) { int Sx(X), Sy(Y), Sd(D); (S.insert(make_pair(pair<int, int>(X, Y), 0)).first->second |= (0)); A[((X) + (Y))].inser...
22
#include <bits/stdc++.h> using namespace std; int A[100010]; struct sf { bool operator()(int a, int b) const { if (A[a] == A[b]) return a < b; return A[a] < A[b]; } }; int main() { int N; long long K; cin >> N >> K; long long sum = 0; for (int i = 0; i < (N); ++i) scanf("%d", A + i), sum += A[i]; ...
10
#include <bits/stdc++.h> const long long N = 4e5 + 5; const long long INF = 0x3f3f3f3f3f3f3f3f; long long a[N], fa[N][25], sum[N], cnt = 2; long long read() { long long x = 0, f = 1; char ch = getchar(); while ('0' > ch or ch > '9') f = ch == '-' ? -1 : 1, ch = getchar(); while ('0' <= ch and ch <= '9') x = x *...
14
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int t, n; long long qpow(long long b, long long a) { long long ans = 1; while (a) { if (a & 1) ans = ans * b; a >>= 1; b *= b; } return ans; } long long a[100005]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a...
7
#include <bits/stdc++.h> using namespace std; const int N = 300005; struct edge { int to, next; } e[N << 1]; int h[N], xb, dfn[N], low[N], n, m, i, j, x, y, w, stx[N], sty[N], ste[N]; vector<int> ans; bool b[N], cant[N]; inline void addedge(int x, int y) { e[++xb] = (edge){y, h[x]}; h[x] = xb; e[++xb] = (edge){...
16
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> ans(0); int h = 100, x = 0, s = 0, p; if (n < h) h = n; while (h > 0) { x = n - h; p = x; while (p > 0) { s += p % 10; p /= 10; } if (s == h) { ans.push_back(x); } s = 0; ...
4
#include <bits/stdc++.h> using namespace std; int n; long double T; struct Test { long double p, t; long double first, last; long double value; } a[150001]; inline bool cmp(Test a, Test b) { return a.value > b.value; } inline bool cmp_(Test a, Test b) { return a.p > b.p; } inline bool check(long double c) { lon...
20
#include <bits/stdc++.h> using namespace std; int n, m, res[1005][2]; bitset<1005> bs[5005][2]; map<string, int> mp; int main() { mp["?"] = 0; bs[0][1].set(); cin >> n >> m; for (int i = 0; i < n; i++) { string x, tmp, y, op, z; cin >> x >> tmp >> y; mp[x] = i + 1; if (y[0] == '0' || y[0] == '1'...
10
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; if (n < 4) { cout << -1 << endl; } else { long long cou; if (n % 2 == 0) { cou = n - 1; } else ...
8
#include <bits/stdc++.h> using namespace std; class tree { public: pair<string, int> split(string s) { int left = 0, right = s.size() - 1; i = right / 2; pair<string, int> a; while (s[i] != ' ') { if (isalpha(s[i])) left = i; else if (isdigit(s[i])) right = i; i = (l...
11
#include <bits/stdc++.h> using namespace std; inline int rd() { int 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; } int n, m, W, fa[1010], w[...
8
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; const int M = 5e5 + 5; int n, m; int head[N]; int Next[M * 2]; int ver[M * 2]; int a[N]; int book[N]; int id[N]; int tot; int ans[N]; int cnt; void add(int u, int v) { ver[++cnt] = v; Next[cnt] = head[u]; head[u] = cnt; } bool cmp(int aa, int bb...
9
#include <iostream> #include <vector> #include <set> #include <map> #include <math.h> #include <unordered_set> #include <unordered_map> #include <numeric> #include <cassert> #include <algorithm> #include <bitset> using namespace std; #define ll long long #define dbl double const int M = 1e5 + 7; void trueMain() ...
7
#include <bits/stdc++.h> using namespace std; int X1[200000], X2[200000], Y1[200000], Y2[200000]; int xx[2], yy[2], p = 0, n; bool pos(int x, int y) { int cnt = 0; for (int i = 0; i < n; ++i) { cnt += (x >= X1[i] && x <= X2[i] && y >= Y1[i] && y <= Y2[i]); } return (cnt >= n - 1); } int main() { ios_base:...
8
#include <bits/stdc++.h> using namespace std; char S[11000]; int n, Ans; inline int Min(register int A, register int B) { return A < B ? A : B; } int main() { scanf("%d%s", &n, S + 1); for (register int i = 1; i <= n; i++) Ans += (S[i] == '8'); printf("%d\n", Min(n / 11, Ans)); }
0
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 5; const long long INF = 1e18; const int MOD = 1e9 + 7; long long fac[15]; long long pf[20]; vector<int> fin; long long n, q; void des(long long x) { int i = 14; while (i > 0) { pf[i] = x / fac[i]; x %= fac[i--]; } vector<int> pos; long...
16
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const int BASE = 1e9 + 7; const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; const int N = 2e5 + 1; pair<int, int> a[N]; vector<int> V; int n, Bit[N], cnt[N]; long long ans = 0; bool comp(pair<int, int> A, pair<int, int> B) { return...
12
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n; int id[N + N]; int a[N + N]; int where[N + N], color[N + N]; vector<int> E[N + N]; bool cmp(int i, int j) { return a[i] < a[j]; } void dfs(int u, int c) { if (color[u] == -1) { color[u] = c; } else { return; } for (int v : E[u]) ...
22
#include <bits/stdc++.h> const int MOD = 998244353; int n; char s[1005]; int a[1005], b[1005]; int currLen; bool seta(int, int); bool setb(int pos, int value) { if (pos >= currLen) return true; int pos2 = currLen - 1 - pos; if (b[pos] == (value ^ 1)) return false; if (b[pos2] == (value ^ 1)) return false; if ...
16
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); int n, tmp, sum = 1; int map[200000] = {0}; cin >> n; for (int i = 0; i < n; i++) { cin >> tmp; if (map[tmp] == 0) map[tmp]++; else sum++; } c...
5
#include <bits/stdc++.h> using namespace std; int n; vector<vector<int> > a; vector<int> color; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int j = 0; j < n; j++) { bool flag = true; for (int i = 0; i < n; i++) { if (i % 2 == 0) { ...
3
#include <bits/stdc++.h> using namespace std; void solve() { int n, i, j; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; ; int ans[100000 + 1] = {0}; int m; for (i = (0); (i) <= (n - 1); ++(i)) { m = arr[i]; for (j = 2; j * j <= m; j++) { if (m % j == 0) { while (...
6
#include <bits/stdc++.h> using namespace std; vector<int> vt[400000]; int vis[400000]; int a[400000]; int b[400000]; map<int, map<int, int> > ma; int ans = 1; int flag = 0; int t, n, m; void dfs(int x) { for (int i = 0; i < vt[x].size(); i++) { int ks = vt[x][i]; if (flag) return; if (vis[ks] == 0) { ...
9
#include <bits/stdc++.h> using namespace std; const int MN = 202, MM = 20, MK = 505, MOD = 1000000007; struct node { int val; node *c[MM], *jump[MM], *fail; node() { val = 0; memset(c, 0, sizeof c); memset(jump, 0, sizeof jump); fail = 0; } void add(const vector<int> &, int, int); }; node xm[M...
17
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int INF = 1e9; struct Node { int mn = INF; int mx = -INF; int minAdj = INF; void pull(const Node& l, const Node& r, int bit) { mn = min(l.mn, r.mn + bit); mx = max(l.mx, r.mx + bit); minAdj = min(l.minA...
21
#include <bits/stdc++.h> using namespace std; struct ee { int d; int t; int rr; }; ee e1[100100], e2[100100]; bool ok1(ee x, ee y) { if (x.d == y.d) return x.t < y.t; return x.d < y.d; } bool ok2(ee x, ee y) { if (x.t == y.t) return x.d < y.d; return x.t < y.t; } int main() { int n; while (cin >> n) {...
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { bool bb = false; int n; cin >> n; int a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; int diff = 0; bool res = true; for (int i = 0; i < n; i+...
2
//If you can dream it, you can do it! //author@nowadays - 2021-07-13 11:01:41 #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; vector<map<char, int>> a(...
4
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; #define fi first #define se second #define pb push_back #define lowbit(x) ((x)&(-x)) #define all(x) (x).begin(), (x).end() const int mod = 998244353; int add(int x, int y) { return (x += y) ...
16
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10, M = 1e7 + 10, skc = 1e9 + 7; int mu[M], c[M], ans, po[N], n, vi[M], pri[M], len; signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; int w = 0, x; po[0] = 1; for (register int i = (1); i <= (n); ++i) cin >> x, c[x]++, p...
21
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); mt19937 rnf(2106); const int N = 500005; struct ban { int ty; int x, y; ban() { ty = -1; x = y = -1; } }; int n, q, k; ban a[N]; vector<pair<int, int> > v[N * 4]; void ubd(int tl, int tr, i...
17
#include <bits/stdc++.h> using namespace std; struct col { int index; int diff; }; bool comp(col a, col b) { return a.diff < b.diff; } int main() { int n; scanf("%d", &n); col in[n]; int l, r; int cnt = 0; for (int i = 0; i < n; ++i) { scanf("%d %d", &l, &r); in[i].index = i + 1; in[i].diff ...
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; bool odd = false, even = false; for (int i = 1; i <= n; i++) { if (i % 2 != 0) { odd...
1
#include <bits/stdc++.h> using namespace std; int n; long long d[5020][5020], res[5020], inv[5020], fac[5020], ifac[5020], MOD = 998244353; void init_math(int N) { inv[1] = 1LL; for (int i = 2; i <= N; i++) inv[i] = (MOD - ((MOD / i) * inv[MOD % i]) % MOD); fac[0] = 1LL; ifac[0] = 1LL; for (int i = 1;...
23
#include <bits/stdc++.h> using namespace std; const int di[] = {-1, 0, 1, 0}; const int dj[] = {0, 1, 0, -1}; const int MOD = 1e9 + 7; const int INF = 1e9; string s1, s2; vector<vector<int> > ind(26); int main() { ios::sync_with_stdio(false), cin.tie(0); cin >> s1 >> s2; for (int i = 0; i < s1.length(); i++) ind[...
7
#include <bits/stdc++.h> using namespace std; int main() { int x, y; cin >> x >> y; if (y == 0) { cout << "No"; return 0; } if (y == 1 && x > 0) { cout << "No"; return 0; } y -= 1; x -= y; if (x % 2 == 0 && x >= 0) cout << "Yes"; else cout << "No"; }
5
#include <bits/stdc++.h> using namespace std; void dfs1(int u); void dfs2(int u); const int MAXN = 100 * 1000 + 13; bool mark1[MAXN], mark2[MAXN]; int state[MAXN]; vector<int> adj[MAXN], badj[MAXN]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> state[i]; int a, b; for (int i = 1; i...
9
#include <bits/stdc++.h> using namespace std; const int mx = 55; int a[mx], b[mx]; int main() { int n, l; scanf("%d %d", &n, &l); for (int i = 0; i < n; i++) { scanf("%d", a + i); } vector<int> x; for (int i = 1; i < n; i++) x.push_back(a[i] - a[i - 1]); x.push_back(a[0] + l - a[n - 1]); for (int i ...
5
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> a, b; int n, i, s = 1, x; cin >> n; for (i = 1; i <= n; i++) { cin >> x; a[x]++; b[a[x]]++; if (a[x] * b[a[x]] == i && i != n) s = i + 1; else if (a[x] * b[a[x]] == i - 1) s = i; } cout << s << endl; }
7