solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; long long qpow(long long a, long long n) { long long r = 1; for (; n; n >>= 1, a *= a) if (n & 1) r *= a; return r; } int main() { int T, n; long long k; scanf("%d", &T); while (T--) { scanf("%d%lld", &n, &k); if (n <= 30 && k > (qpow(4, n) - 1) / ...
6
#include<iostream> #include<string> #include<cstring> #include<stack> #include<queue> #include<map> #include<algorithm> #include<cmath> #include<iomanip> #include<set> #include<cstdio> using namespace std; #define goup(i,a,b) for(int i=a;b;i++) #define Goup(i,a,b) for(int i=a;i<=b;i++) #define godown(i,a,b) for(int i=a...
5
#include <bits/stdc++.h> using namespace std; int n, m, a[105]; int main() { int s = 0, t = 0; ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = 0; i < n; i++) { if (s >= m) break; else { s += a[n - i - 1]; ...
0
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; int mx = 10000; auto mex = [&](int a, int b, int c) { int res = 0; while (res == a || res == b || res == c) { res++; } return res; }; while (...
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 5010; int n, m; char a[MAXN][MAXN]; int r[MAXN][MAXN]; int cnt[MAXN]; void makedp() { for (int i = 0; i < n; ++i) { bool flag = false; int st; for (int j = m - 1; j >= 0; --j) { if (a[i][j] == '1' && !flag) { flag = true; ...
4
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 10; const int MAX_N = 1e5 + 10; inline int in() { int32_t x; scanf("%d", &x); return x; } inline string get() { char name[1000]; scanf("%s", name); return name; } long long a[] = { 1, 2, 4, 8, 16, ...
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; } long long ans; if (n % 2 == 0) { ans = ((n / 2) - 1); } else { ans = (n ...
1
#include <bits/stdc++.h> using namespace std; const int MAXSIZE = 10000020; const int mod = 1000000007; int bufpos; char buf[MAXSIZE]; void init() { buf[fread(buf, 1, MAXSIZE, stdin)] = '\0'; bufpos = 0; } long long readint() { long long val = 0; for (; !isdigit(buf[bufpos]); bufpos++) ; for (; isdigit(bu...
8
#include <bits/stdc++.h> const long long int MOD = 1e9 + 7; using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, m; cin >> n >> m; int arr[n + 1], indxm; for (int i = 1; i <= n; ++i) { i...
5
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int freq[N]; int A[N]; int prefix[N]; int main() { int n, s, ans = INT_MAX - 5, max_val = 0; cin >> n >> s; for (int i = 1; i <= n; i++) { cin >> A[i]; if (i != s) { freq[A[i]]++; max_val = max(max_val, A[i]); } } if ...
5
#include <bits/stdc++.h> using namespace std; using ll = int64_t; class CerrDummy { } cerrDummy; template <class T> CerrDummy& operator<<(CerrDummy& cd, const T&) { return cd; } using charTDummy = char; using traitsDummy = char_traits<charTDummy>; CerrDummy& operator<<(CerrDummy& cd, basic_ostre...
6
#include <bits/stdc++.h> using namespace std; const long long inf = 1e10; const int N = 3e5 + 77; int n, m, k, a, b; char adj[1001][1001]; int main() { cin >> n >> a >> b; if (a > 1 and b > 1) { puts("NO"); return 0; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) adj[i][j] = '0'; if (a ...
4
#include <bits/stdc++.h> using namespace std; template <class T1> void deb(T1 e1) { cout << e1 << endl; } template <class T1, class T2> void deb(T1 e1, T2 e2) { cout << e1 << " " << e2 << endl; } template <class T1, class T2, class T3> void deb(T1 e1, T2 e2, T3 e3) { cout << e1 << " " << e2 << " " << e3 << endl; ...
2
#include <bits/stdc++.h> using namespace std; int n; vector<pair<int, int> > son[200005]; long long valid = 0, One, Zero, OneZero, ZeroOne; int mark[200005]; int sub[200005]; void fillsub(int u, int p) { sub[u] = 1; for (auto e : son[u]) { if (e.first == p) continue; if (mark[e.first]) continue; fillsub...
7
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n; char a[N], b[N]; set<int> l, r; int ans; int main() { ios::sync_with_stdio(false); cin >> n >> a + 1 >> b + 1; for (int i = 1; i <= n; i++) { if (a[i] != b[i]) { if (a[i] == '0') l.insert(i); else r.insert...
6
#include <bits/stdc++.h> using namespace std; int pre[3000000]; char a[3000000]; int find(int x) { if (x != pre[x]) pre[x] = find(pre[x]); return pre[x]; } void combine(int a, int b) { int fx = find(a); int fy = find(b); if (fx > fy) pre[fy] = fx; else pre[fx] = fy; } int main() { char ans[3000000...
4
#include <bits/stdc++.h> using namespace std; long long n, m; vector<long long> a, b, last; const long long INF = 4e18; long long cal(long long A, long long B) { return B * (A - B * B); } long long calUp(long long A, long long B) { return -A + 3 * B * B + 3 * B + 1; } long long calDown(long long A, long long B) { ret...
9
#include <bits/stdc++.h> using namespace std; string Compare(string s, int n) { if (n & 1 == 1) return s; string temp1 = s.substr(0, n / 2); string temp2 = s.substr(n / 2); string s1 = Compare(temp1, n / 2); string s2 = Compare(temp2, n / 2); if (s1 < s2) return s1 + s2; return s2 + s1; } int main() { s...
4
#include <bits/stdc++.h> using namespace std; bool prime(int x) { if (x < 2) return false; for (int i = 2; i <= sqrt(x); i++) { if ((x % i) == 0) return false; } return true; } int main() { int d, n, x; scanf("%d", &d); scanf("%d", &n); long long int res = 0; for (int i = 0; i < n - 1; ++i) { ...
1
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7; const int M = 1e9; const int inf = 1e9 + 7; const long long base = 1e18; const double pi = acos(-1); const double ep = 1e-9; int n, k, m = 0; int a[N]; long long S; vector<pair<long long, int> > lef; map<pair<long long, int>, int> rig; long long f[20]...
6
#include <bits/stdc++.h> using namespace std; const int maxn = 3 + 10; int kry[maxn][maxn]; int stal[maxn][maxn]; int main() { int a; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { scanf("%d", &a); kry[i][j] = a % 2; stal[i][j] = 1; } for (int i = 0; i < 3; i++) { for (int...
0
#include <bits/stdc++.h> using namespace std; pair<int, int> p[200005]; int main() { int n, i, w, x, s = 0, l = -(1 << 31); cin >> n; for (i = 0; i < n; i++) cin >> x >> w, p[i] = make_pair(x + w, x - w); sort(p, p + n); for (i = 0; i < n; i++) if (p[i].second >= l) l = p[i].first, s++; cout << s; }
5
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class t, class u> void chmax(t& first, u second) { if (first < second) first = second; } template <class t, class u> void chmin(t& first, u second) { if (second < first) first = second; } template <class t> using vc = vector<t>; template ...
10
#include <bits/stdc++.h> using namespace std; const int maxn = 2e3; const int score_max = 5e3; int num[maxn + 10]; long long count_winscore[score_max + 10], A_score[score_max << 1 + 10]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &num[i]); sort(num, num + n); for (int i = 0; ...
5
#include <bits/stdc++.h> using namespace std; void solve() { int n; scanf("%d ", &n); int r = 0x7FFFFFFF; vector<int> sz(n); for (int i = 0; i < (int)(n); i++) scanf("%d ", &sz[i]); for (int i = 0; i < (int)(n); i++) { int s = 0; for (int j = 0; j < (int)(sz[i]); j++) { int a; scanf("%d ...
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; const int inf = 0x3f3f3f3f; int main() { int n; scanf("%d", &n); if (n % 2 == 0) { printf("NO\n"); } else { printf("YES\n"); if (n == 1) printf("1 2\n"); else { printf("1 "); int j = 4; while (1) { ...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 5e2 + 5; int C[maxn]; bool dp[maxn][2 * maxn][maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, p; cin >> n >> p; for (int i = 1; i <= n; i++) cin >> C[i]; dp[0][0][0] = 1; for (int i = 1; i <= n; i++) for...
5
#include <bits/stdc++.h> inline int read() { int s = 0; char c; while ((c = getchar()) < '0' || c > '9') ; do { s = s * 10 + c - '0'; } while ((c = getchar()) >= '0' && c <= '9'); return s; } const int N = 21, INF = 0x7f7f7f7f; int n, n2, w1[N][N], w2[N][N], w[N][N], ans, tans, lx[N], ly[N], sl[N], ...
9
#include <bits/stdc++.h> using namespace std; const int MOD(1000000007); const int INF((1 << 30) - 1); const int MAXN(1000005); int a[MAXN]; int main() { int n, q; scanf("%d%d", &n, &q); int pos[2] = {0, 1}; while (q--) { int t; scanf("%d", &t); if (t == 1) { int x; scanf("%d", &x); ...
5
#include <bits/stdc++.h> using namespace std; int n, x[5005], y[5005], p[5005], ta; bool u[5005], o; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { printf("? %d 0\n", i); fflush(stdout); scanf("%d", x + i); } y[0] = x[0]; for (int i = 1; i < n; i++) { printf("? 0 %d\n", i); ff...
6
#include <bits/stdc++.h> using namespace std; namespace io { const int _SZIE = (1 << 21) + 1; char ibuf[_SZIE], *iS, *iT, c, stk[46]; int tot; template <class I> inline void read(I &_x) { I fl = 1; for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, _SZIE, stdin), (iS == iT ? EOF : *iS++)...
10
#include <bits/stdc++.h> char a[100000 + 2]; void solve(int n, int k) { for (int i = 0; i < n - 1 && k; i++) { if (a[i] == '4' && a[i + 1] == '7') { if (i > 0 && a[i - 1] == '4' && i % 2) k = k % 2; if (k == 0) break; if (i % 2 == 0) a[i + 1] = '4'; else a[i] = '7'; -...
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; cout << (n - 1) / 2 << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { int count = 0; int T; cin >> T; while (count++ < T) { int a, b, low = 0, high = 0, n; int nbOperations = 0; cin >> a >> b >> n; int pol = 1; if (a > n || b > n) { cout << "0" << endl; continue; } if (a < b) { ...
0
#include <bits/stdc++.h> using namespace std; int T, k, p; long long l, r; int pw(int base, long long n, int mod) { if (!n) return 1; int ret = pw(base, n / 2, mod); ret = 1LL * ret * ret % mod; if (n & 1) ret = 1LL * ret * base % mod; return ret; } int main() { scanf("%d", &T); while (T--) { scanf("%...
9
#include <bits/stdc++.h> using namespace std; int data[(int)2e5]; int dp[5001][5001]; int used[5001][5001]; string s; int magic(int l, int r) { if (l > r) return 1; if (used[l][r] != -1) return used[l][r]; if (l == r) { data[1]++; return used[l][r] = 1; } int lk, rk, k, flag = 1, middle = (l + r) / 2;...
5
#include <bits/stdc++.h> using namespace std; namespace wode { template <typename T> inline void read(T &x) { static char ch; bool f = 1; for (x = 0, ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') f = 0; for (; isdigit(ch); x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar()) ; x = f ? x...
5
#include <bits/stdc++.h> using namespace std; const long double pi = 3.14159265359; inline long long add(long long a, long long b, long long m) { if ((a + b) >= m) return (a + b) % m; return a + b; } inline long long mul(long long a, long long b, long long m) { if ((a * b) < m) return a * b; return (a * b) % m;...
1
#include <bits/stdc++.h> using namespace std; int matrix[1005][1005]; int n; int rowSum[1005]; int restot; struct { int c; int a; int b; } rc[100005]; void printM() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { printf("%d ", matrix[i][j]); } printf("\n"); } printf("\n"); ...
6
#include <bits/stdc++.h> using namespace std; void init() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); } long long dx[4] = {0, 1, -1, 0}; long long dy[4] = {1, 0, 0, -1}; vector<long long> adj[100005]; void read(vector<long long> &v) { for (auto &x : v) cin >> x; } void show(vector<long long> &v) { ...
2
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:10240000,10240000") using namespace std; const double R = 0.5772156649015328606065120900; const int N = 1e5 + 5; const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const double eps = 1e-8; const double pi = acos(-1.0); int n; long long a[N], ini, M; long long j...
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; int id[MAXN]; set<int> se[MAXN]; struct Data { char op; int x, y; } d[MAXN]; struct node { int l, r, mx; inline int mid() { return (l + r) >> 1; } } tree[MAXN << 2]; void build(int l, int r, int rt) { tree[rt].l = l; tree[rt].r = r; t...
10
#include <bits/stdc++.h> using namespace std; bool arr[1001]; vector<int> vec; void sieve() { arr[0] = arr[1] = 1; for (int i = 2; i <= 1000; i++) { if (arr[i] == 0) { vec.push_back(i); for (int j = i * i; j <= 1000; j += i) arr[j] = 1; } } } int main() { int n, m; sieve(); while (cin >>...
0
#include <bits/stdc++.h> using namespace std; long long r, m, n, a[500005], b[500005], c[500005], d[500005], mod = 998244353; vector<long long> v[500005]; long long dfs(long long x, long long pr) { if (v[x].size() == 1 && v[x][0] == pr) { r++; return 1; } long long i, j, k = 1e9, l = 0; for (i = 0; i < ...
5
#include <bits/stdc++.h> using namespace std; int ans[501][501]; signed main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(0); int t; cin >> t; while (t--) { int n, m, x; cin >> n >> m; map<int, vector<int> > q; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> x, q[x]...
1
#include <bits/stdc++.h> using namespace std; int f[100005]; int x[100005], y[100005], t[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int r, n; cin >> r >> n; for (int i = 1; i <= n; i++) { cin >> t[i] >> x[i] >> y[i]; f[i] = -1e9; } x[0] = 1; y[0] = 1; t[...
6
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const double EPS = 1e-13; double pre[100005], suf[100005]; pair<double, double> ans[100005]; pair<double, double> getsol(double xy, double rxy) { double a = -1.0, b = xy + 1.0 - rxy, c = -xy; double d = sqrt(b * b - a * c * 4.0 + EPS); return ...
8
#include <bits/stdc++.h> using namespace std; vector<int> lps(string& str) { int n = str.size(); vector<int> ans(n + 1); int i = 1; int l = 0; while (i < n) { if (str[i] == str[l]) { ans[++i] = ++l; continue; } if (l > 0) { l = ans[l]; continue; } ans[++i] = 0; } ...
9
#include <bits/stdc++.h> const int N = rand() % 1141; 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; cout << n << "\n"; for (int i = 1; i <= n; i++) cout << i << " "; cout << "\n"; } }
0
#include <bits/stdc++.h> using namespace std; int main() { int x, y, x1, y1, r; cin >> r >> x >> y >> x1 >> y1; int d = 2 * r; if (x == x1 && y == y1) { cout << "0\n"; return 0; } float a = x1 - x; float b = y1 - y; float p = sqrt(a * a + b * b); int m = sqrt(a * a + b * b); if (p < d) { ...
3
#include <bits/stdc++.h> using namespace std; const long long int N = 1e5 + 100; const long long int mod = 1e9 + 7; long long int dx[] = {1, -1, 0, 0}; long long int dy[] = {0, 0, 1, -1}; long long int a[N]; long long int n, x, k; long long int func(long long int val) { long long int low = 1; long long int high = n...
4
#include <bits/stdc++.h> int main() { long long int n, s; scanf("%I64d", &n); if (n < 3) printf("%I64d", n); else { if (n % 2 == 0) { if (n % 3 != 0) printf("%I64d", n * (n - 1) * (n - 3)); else printf("%I64d", (n - 1) * (n - 2) * (n - 3)); } else printf("%I64d", n ...
4
#include <bits/stdc++.h> using namespace std; long long my_pow(long long x) { return x * x * x; } long long steps(long long m) { if (m <= 7) return m; long long x = 1; while (my_pow(x + 1) <= m) ++x; return 1 + steps(max(m - my_pow(x), my_pow(x) - 1 - my_pow(x - 1))); } int main() { long long m; scanf("%lld...
7
#include <bits/stdc++.h> using namespace std; int main() { int i, ans, n; n = 3; int ar[n]; for (i = 0; i < n; i++) { cin >> ar[i]; } sort(ar, ar + n); if (ar[0] + ar[1] > ar[2]) { cout << 0 << "\n"; } else { ans = ar[2] - (ar[0] + ar[1]); cout << ans + 1 << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int tc, cs = 1; int n, Max, r; int p[100005], s[100005], d[100005], l[100005]; int e[100005][2]; bool col[100005]; vector<int> V[100005]; void bfs(int st) {} int main() { int u, v; scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d%d", &u, &v); d[u]++; ...
6
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; if (n == 1 || n == 2) { cout << 1 << endl; } else { cout << (n - 1) / 2 + 1 << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, a, c = 0, d, g = 0; cin >> n; vector<int> v; d = n; for (int i = 0; i < n; i++) { cin >> a; v.push_back(a); } for (int i = 0; i < v.size(); i++) { for (int j = i + 1; j < v.size(); j++) { if (v[i] == v[j]) { ...
1
#include <bits/stdc++.h> using namespace std; template <class T> inline T lowbit(T x) { return x & (-x); } template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> inline T Pow(T a, T b, T p) { T ret = 1; for (; b; b >>= 1, a = a * a % p) if (b & 1) (ret *= a) %= p; return r...
9
#include <bits/stdc++.h> using namespace std; int main() { double a, b, c, d; cin >> a >> b >> c >> d; double p = 1.0; p = p * a / b; double q = 1.0; q = q * (1 - (c / d)) * (1 - (a / b)); long double ans = (p / (1 - q)); cout << fixed << setprecision(15) << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int mid = (n + 1) / 2; int po = 0, ne = 0; for (int i = 0; i < n; ++i) { int t; scanf("%d", &t); if (t > 0) po++; else if (t < 0) ne++; } if (po >= mid) printf("1\n"); else if (ne >= mid) ...
0
#include <bits/stdc++.h> using namespace std; bool ok(long long F, long long N) { return (3ll * F * (F + 1) / 2 - F <= N); } int main() { long long N, l, r; scanf("%I64d", &N); l = 1; r = 1e6; while (l <= r - 1) { if (l == r - 1) { if (ok(r, N)) { break; } else { r = l; ...
4
#include <bits/stdc++.h> using namespace std; vector<pair<long long int, long long int> > co[55]; vector<long long int> s; long long int n, x[55], y[55], a[55], b[55]; long long int distance(pair<long long int, long long int> firstone, pair<long long int, long long int> secondone) { return (fir...
6
#include <bits/stdc++.h> using namespace std; const int maxN = (int)1e5 + 100; const int MAX = (int)1e5 + 1; int l[maxN], d[maxN], cnt[maxN], q[maxN]; vector<int> pool[maxN]; multiset<int> p; void solveTask() { int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> l[i]; for (int i = 1; i <= n; ++i) cin >> d[i]; ...
5
#include <bits/stdc++.h> using namespace std; int sz[200005]; int bd[200005]; int mn, res; int n, k; int a[200005]; vector<int> v[200005]; void dfs(int x, int y) { sz[x] = 1; bd[x] = (a[x] < mn ? 1 : 0); for (int i = 0; i < v[x].size(); ++i) { int to = v[x][i]; if (to == y) continue; dfs(to, x); s...
9
#include <bits/stdc++.h> using namespace std; void solve() { int n; scanf("%d", &n); int ar[n + 2]; for (int i = 1; i <= n; i++) scanf("%d", &ar[i]); sort(ar + 1, ar + n + 1); int cnt = 0; for (int i = n; i >= 1; i--) { if (i >= ar[i]) { cnt = i; break; } } cout << cnt + 1 << "\n";...
1
#include <bits/stdc++.h> using namespace std; char rs[20]; int main() { int a, b, k, m, f = 0, ans = 1; cin >> a >> b >> k; m = 1000000000 % k; if (b + 1 >= k || m == 0 || a == 0) cout << "2"; else { while (ans <= min(a, k)) { if ((long long)ans * m % k < k - b && (long long)ans * 1000...
5
#include <bits/stdc++.h> using namespace std; set<long long int> s[100005]; long long int d[100005]; vector<char> prime(100000 + 1, true); void sieve(long long int n) { prime[0] = prime[1] = false; for (long long int i = 2; i <= n; ++i) { if (prime[i]) { s[i].insert(i); for (long long int j = 2 * i;...
6
#include <bits/stdc++.h> using namespace std; char s[2000000], s1[2000000]; int t[2000000], t1[2000000]; int n, m, T; int f[2000000 * 2], g[2000000 * 2]; void build(char* S, int* f, int* t, int l, int r, int s) { if (l == r) { f[s] = (S[l] == 'B' || S[l] == 'C'); if (!f[s]) t[s] = 1; return; } build(S...
8
#include <bits/stdc++.h> using namespace std; long long int solve(long long int a, long long int b, long long int x) { if (a <= 0 || b <= 0) return x; if (a >= b) return solve(a % b, b, x + (a / b)); else return solve(a, b % a, x + (b / a)); } int main() { int t; cin >> t; while (t--) { long lon...
0
#include <bits/stdc++.h> using namespace std; int a[100]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; if (n == 1) { cout << -1 << endl; return 0; } if (n == 2 && a[0] == a[1]) { cout << -1 << endl; return 0; } int ans = 10000000; int ind = 0; for (int i = 0...
1
#include <bits/stdc++.h> using namespace std; const int z = 200100; int arr[2 * z]; int get(int x, int y) { if (x > y) return 0; if (x == y) return arr[x]; if (x % 2 == 1) return arr[x] + get(x + 1, y); if (y % 2 == 0) return arr[y] + get(x, y - 1); return get(x / 2, y / 2); } void inc(int x) { while (x) { ...
6
#include <bits/stdc++.h> using namespace std; set<int> tab[2001]; int ai[2001]; long long int p = 1000000007; long long int f(int nr, int max, int min, int rodzic, int i) { long long int wynik = 1; set<int>::iterator it; for (it = tab[nr].begin(); it != tab[nr].end(); ++it) { if (rodzic != *it && ai[*it] <= m...
6
#include <bits/stdc++.h> using namespace std; set<pair<pair<long long, long long>, long long> > se; set<pair<pair<long long, long long>, long long> >::iterator it; int main() { long long a, b, c, n, num = 0, cost = 0; cin >> a >> b >> c; cin >> n; for (int i = 1; i <= n; i++) { long long val; char cc[10...
3
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 123; int test; string s; int n; const int md = 1e9 + 7; void ReadData() { cin >> test; cin >> s; } int pwr(int a, int n) { int res = 1; while (n) { if (n & 1) res = 1LL * res * a % md; a = 1LL * a * a % md; n >>= 1; } return...
8
#include <bits/stdc++.h> using namespace std; long long m, k, n, s; long long ar[500005], br[500005]; long long need[500005]; long long cur[500005], diff; int main() { cin >> m >> k >> n >> s; for (int i = 1; i < m + 1; i++) cin >> ar[i]; for (int i = 0; i < s; i++) { long long x; cin >> x; need[x]++;...
5
#include <bits/stdc++.h> using namespace std; inline int Read() { int x(0), f(1); char c(getchar()); while (c < '0' || '9' < c) { if (c == '-') { f = -1; } c = getchar(); } while ('0' <= c && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return f * x; } void Wri...
7
#include <bits/stdc++.h> const int N = 100005; std::map<int, int> rec[N]; std::set<int> adj[N]; int n, m, c, q, f[N]; int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); } void _union(int x, int y) { x = find(x), y = find(y); if (x == y) return; if (adj[x].size() > adj[y].size()) std::swap(x, y); f[x] =...
8
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; vector<long long> v(n); map<long long, long long> mp; for (long long i = 0; i < n; i++) { cin >> v[i]; mp[v[i] % m]++; } long long f = 0; if (...
4
#include <bits/stdc++.h> using namespace std; bool cus(pair<int, int> a, pair<int, int> b) { if (a.first < b.first) return true; if (a.first > b.first) return false; return a.second < b.second; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long n, m, A; long lo...
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 7; set<pair<long long, int> > num; int ans[maxn]; typedef struct node { long long l, r; int pos; } Node; Node p[maxn]; bool cmp(Node a, Node b) { if (a.r == b.r) return a.l < b.l; else return a.r < b.r; } int main() { int n, m; sca...
6
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("fast-math") using namespace std; void solve(); signed main() { srand(time(0)); ios_base::sync_with_stdio(...
4
#include <bits/stdc++.h> using namespace std; constexpr auto max_n = 305; int n, m; struct Edge { int u, v, cap, flow; }; struct Dinic { vector<Edge> edges; vector<vector<int>> graph; vector<int> ptr; vector<int> level; int nodes_count, source, sink; Dinic(int n, int s, int t) : nodes_count(n), source(s),...
10
#include <bits/stdc++.h> using namespace std; int n; string s[22]; int id[22][22]; vector<pair<int, int> > v[44]; map<int, int> r[44]; int go(int pos, int mask) { if (pos == n + n - 2) return 0; if (r[pos].count(mask)) return r[pos][mask]; vector<int> t; for (int i = (0); i < (20); i++) if (mask & (1 << i))...
8
#include <bits/stdc++.h> using namespace std; int N; void process(const string& x) { set<char> for_sure; char last_char = '!'; int cnt = 0; for (int i = 0; i < x.size(); i++) { if (x[i] != last_char) { if (cnt % 2 != 0) { for_sure.insert(last_char); } last_char = x[i]; cnt = ...
1
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-9; const int inf = 2000000000; const long long infLL = 9000000000000000000; inline int in() { int x; scanf("%d", &x); return x; } inline long long inl() { long long x; scanf("%I64d", &x); return x; } inline doubl...
6
#include <bits/stdc++.h> using namespace std; int main() { int t, a; cin >> t; for (int i = 0; i < t; i++) { cin >> a; if (360 % (180 - a) == 0) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
1
#include <bits/stdc++.h> const int N = 300005, K = 20; int m, maxd, dep[N], fa[N][K], s1[N], s2[N], top1, top2; void add(int u, int v) { dep[u] = dep[v] + 1, fa[u][0] = v; for (int i = 1; i < K; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1]; } int lca(int u, int v) { if (dep[u] < dep[v]) u ^= v ^= u ^= v; int ret = d...
10
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); string s = "", t = ""; cin >> s; for (int i = 0; i < s.length(); i++) t += "0"; stack<int> st; for (int i = 0; i < s.length(); i++) { if (s[i] == '0' && !st.empty()) { t[st.top()] = '1'; st.pop(); } else ...
6
#include <bits/stdc++.h> using namespace std; const long long MAX_N = 2009; vector<long long> g[MAX_N]; vector<long long> rg[MAX_N]; string s[MAX_N]; long long n, m; bool used[MAX_N]; long long ts[MAX_N]; long long cnt; long long c[MAX_N]; vector<long long> g1[MAX_N]; long long h[MAX_N]; void dfs(long long v) { used[...
6
#include <bits/stdc++.h> using namespace std; int v[4]; bool ok() { for (int i = 0; i < 4; ++i) if (v[i] != 1) return 0; return 1; } inline int next(int p) { return (++p == 4) ? (0) : (p); } int main() { for (int i = 0; i < 4; ++i) scanf("%d", &v[i]); while (!ok()) { bool done = 0; for (int i = 0; i...
7
#include <bits/stdc++.h> using namespace std; const int maxn = 100050; int f[maxn]; int main() { int m, n; scanf("%d%d", &m, &n); for (int i = 0; i <= 100; ++i) f[i] = 1; int l = 1, r = m; for (int i = 1; i <= 60; ++i) { int y; if (i <= n) { printf("%d\n", m); fflush(stdout); scanf("...
5
#include <bits/stdc++.h> using namespace std; int32_t main() { long long t, n, i, j, k; cin >> t; string s; bool hi; while (t--) { cin >> s; n = s.size(); map<char, long long> m; for (i = 0; i < n; i++) m[s[i]]++; map<char, long long>::iterator it = m.begin(); hi = true; char ch = ...
0
#include <bits/stdc++.h> using namespace std; const int mx = 100050; vector<int> vec[mx]; int dp[mx][2]; bool func(int idx, int pl) { if (dp[idx][pl] != -1) { return dp[idx][pl]; } int i, j, k, l = vec[idx].size(); if (l == 0) { dp[idx][pl] = 0; return dp[idx][pl]; } bool ret = 0; for (i = 0; ...
4
#include "bits/stdc++.h" #define int long long #define forn(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef unsigned long long ull; int mod = 998244353; int r = 0; vector<vector<int>> a; vector<vector<int>> b; bool cmp(int i, int j) { return a[i][r] < a[j][r]; } int32_t main() { ios_...
9
#include <bits/stdc++.h> const int MOD = 1e9 + 7; const int MAX = 1e9 + 7; const int MIN = -1e9 + 7; using namespace std; int a, b, n; long long power(long long x, long long y) { if (y == 0) return 1; long long s = power(x, y / 2); s *= s; if (y % 2 == 1) s *= x; return s; } int dp[100005][35][2]; bool solve(...
6
#include <bits/stdc++.h> using namespace std; int n, x, f[5]; int main() { scanf("%d", &n); for (register int(i) = (1); (i) <= (n); (i)++) scanf("%d", &x), f[1] += (x == 1), f[2] = max(f[1], f[2] + (x == 2)), f[3] = max(f[2], f[3] + (x == 1)), ...
5
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 6; using hash_t = unsigned long long; const unsigned long long MOD = 1000000000000000000ll + 3; const __int128_t ONE = 1; string s; hash_t mul(hash_t a, hash_t b) { return ONE * a * b % MOD; } hash_t mod_pow(hash_t a, hash_t k) { hash_t res = 1; whil...
8
#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; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args....
5
#include <bits/stdc++.h> using namespace std; struct Point { int first, second; Point(){}; Point(int a, int b) { first = a, second = b; } friend bool operator<(Point a, Point b) { return a.first == b.first ? a.second < b.second : a.first < b.first; } }; char a[110][110]; char ctrl[1010]; Point p[1010], s,...
7
#include <bits/stdc++.h> using namespace std; const int MaxN = 1000 * 1000; const int mod = 1000 * 1000 * 1000 + 7; char s[MaxN]; int dp[MaxN][5]; void edit(int pos, char x) { if (x == '*') { dp[pos][0] = (((dp[pos][0] + dp[pos - 1][3]) % mod + dp[pos - 1][4]) % mod + dp[pos - 1][0]) % ...
5