solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const long long MOD = 1e9 + 7; template <class T> int getbit(T x, int pos) { return (x >> (pos - 1)) & 1; } template <class T> void turn_on(T &x, int pos) { x = x | ((T)1 << (pos - 1)); } template <class T> void turn_off(T &x, int pos) { x = x & ~...
13
#include <bits/stdc++.h> using namespace std; const int N = 20000 + 5; int A[N], B[N]; vector<int> adjA[N], adjB[N]; int F[N]; int n; inline int inc(int at, int d) { at += d; if (at == 0) at = n; if (at == n + 1) at = 1; return at; } inline int diff(int a, int b) { int d = b - a; if (d < 0) d += n; return...
10
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 22; long long n, a, b, k, q, req[MAXN], fen[MAXN][2]; void add(long long h, long long x, long long val) { for (; x < MAXN; x += x & -x) fen[x][h] += val; } long long fans(long long h, long long x) { long long ret = 0; for (; x; x -= x & -x...
9
#include <bits/stdc++.h> using namespace std; int main() { int t = 1; scanf("%d", &t); while (t--) { long long n; cin >> n; long long x; cin >> x; long long A[n]; int flag = 0; map<int, int> arr; for (int i = 0; i < n; i++) { cin >> A[i]; if (A[i] % x != 0) { fl...
6
#include <bits/stdc++.h> using namespace std; struct rec { long long Hsize, Asize, Cople, Val, Ctrib; long long Lazy; } E[200005]; long long Ans; int fa[200005], lc[200005], rc[200005]; int pa[200005], n; void Add(int t, long long Ret) { if (!t) return; E[t].Lazy += Ret; E[t].Asize += Ret; E[t].Cople += 2 *...
24
#include <bits/stdc++.h> using namespace std; const int maxn = 3003, maxc = 20, maxv = 100, mod = 1e9 + 7, maxa = 1005, maxs = 820, maxb = 10, base = 737, base2 = 3079, mod3 = 998244353, delt = 10513; const long long inf = 2e16; const int infint = 1e9 + 11; long long max(long long x, long long y) { ...
19
#include <bits/stdc++.h> #pragma 03 using namespace std; long long minv[200005], occ[1000001] = {0}; bool c[200005] = {0}; bool cor[200005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; for (int i = 0; i < n; i++) { long long l; cin >> l; long long m...
6
#include <bits/stdc++.h> using namespace std; ofstream coutt("o.txt", ios::out); struct p { int value; int position; }; bool cmp(p x, p y) { return x.position < y.position; } int main() { int n = 0; vector<int> first; vector<int> second; cin >> n; long long fi = 0; long long se = 0; bool f = true; f...
6
#include <bits/stdc++.h> using std::multiset; const int maxn = 1e5 + 1; inline long long max(long long a, long long b) { return a > b ? a : b; } inline long long min(long long a, long long b) { return a < b ? a : b; } struct Edge { long long u, v, w, c; inline void clear() { u = 0; v = 0; w = 0; c =...
14
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long mpow(long long a, long long b, long long p = MOD) { a = a % p; long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % p; a = (a * a) % p; b = b >> 1; } return res % p; } const long long N = 2 * 1e5 + 2;...
11
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> a, pair<int, int> b) { return (a.first * double(a.second) / (100 - a.second)) > (b.first * (double(b.second) / (100 - b.second))); } int n, xc, pc; double ans; double sum[100005]; pair<int, int> a[100005]; int main() { scanf("%d", &n); ...
13
#include <bits/stdc++.h> using namespace std; int dcmp(double x) { if (fabs(x) < 1e-6) return 0; return x < 0 ? -1 : 1; } struct Line { pair<double, double> p, v; }; bool operator<(Line l1, Line l2) { return atan2(l1.v.second, l1.v.first) < atan2(l2.v.second, l2.v.first); } bool equal(double x, double y) { retu...
15
#include <bits/stdc++.h> using namespace std; const long long int N = 1e5 + 5; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) { long long int n; cin >> n; string s, t; cin >> s >> t; vector<long long int> ind; for (int i = 0; i < n...
2
#include <iostream> #include <cstring> #include <string> #include <algorithm> #include <cmath> #include <vector> #include <map> #include <queue> #include <set> #include <iomanip> #include <functional> #include <stack> using namespace std; //{ void printb(bool b) { if(b) cout<<"YES"<<endl; else cout<<"NO"<<endl;...
0
#include <bits/stdc++.h> using namespace std; int d[100000 + 5]; int temp[100000 + 5]; int main() { int N; cin >> N; for (int i = 1; i <= N; ++i) { scanf("%d", &d[i]); temp[i] = d[i]; } for (int i = 1; i < N; ++i) { if (temp[i] > temp[i + 1]) { swap(temp[i], temp[i + 1]); } } for (in...
5
#include <bits/stdc++.h> using namespace std; const int H = 32, Q = 100010; struct node { node *l, *r; double val; } root[H * Q], *nodes = root + 1; double calc(node *u, double mx) { if (!u || u->val <= mx) return mx; return (calc(u->l, max(mx, u->val - (u->l ? u->l->val : 0))) + calc(u->r, max(mx, u-...
17
#include <bits/stdc++.h> using namespace std; pair<int, int> card[1010]; int n, cnt, ans; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> card[i].second >> card[i].first; sort(card, card + n); cnt = 1; for (int i = n - 1; i >= 0; i--) { if (cnt == 0) break; ans += card[i].second; cnt = c...
3
#include <bits/stdc++.h> using namespace std; const int N = 200100; int n, k, ans = 1e9, mx; vector<int> v[N]; int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { int x, cnt = 1; scanf("%d", &x); mx = max(mx, x); v[x].push_back(0); while (x) { x /= 2; v[x].push_back(cnt...
8
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #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 inf = numeric_limits<long long>::max() / 2; const long double eps = 1...
15
#include <bits/stdc++.h> using namespace std; int main() { int n; int r_o, r_e, o_e; cin >> n >> r_o >> r_e >> o_e; int dis = 0; if (n <= 1) goto ans; else { n--; char curr = 'r'; again:; if (curr == 'r') { if (n == 0) goto ans; if (r_o < r_e) { dis += r_o; curr...
1
#include <bits/stdc++.h> using namespace std; char s[2000]; int main() { scanf("%s", s); int n = strlen(s); int cur = 0; for (int i = 0; i < n; i++) { if (s[i] >= 'a' && s[i] <= 'z') { if (s[i - 1] == '/') { cur--; for (int j = 0; j < 2 * cur; j++) { printf(" "); } ...
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> L(n), R(n); for (int i = 0; i < n; i++) cin >> L[i] >> R[i]; vector<long double> prob(10000 + 1, 0.0); vector<int> trits(n); int pow3 = (int)(pow(3.0, n) + 0.001); for (int st = 0; st < pow3; st++) { int cnt2 = 0...
12
#include <bits/stdc++.h> using namespace std; int s[200005]; int p[200005]; int main() { int n, d, final; cin >> n >> d; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 1; i <= n; i++) cin >> p[i]; s[d] += p[1]; multiset<int, greater<int> > st; for (int i = n - d + 2, j = 1; i <= n and j < d; i++, ...
6
#include <bits/stdc++.h> using namespace std; inline string toString(long long x) { string ans = ""; bool negtive = x < 0; while (x) ans.push_back(x % 10 + '0'), x /= 10; if (ans.size() == 0) ans = "0"; if (negtive) ans.push_back('-'); for (long long i = 0; i < ans.size() / 2; i++) swap(ans[i], ans[ans....
12
#include <bits/stdc++.h> using namespace std; struct Num { int mod; int sum; string tviton; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int d, s; cin >> d >> s; string res = "-1"; vector<vector<bool>> used(505); for (int i = 0; i < 505; i++) { vector<bool> cur(5005, false); used...
14
#include <bits/stdc++.h> using namespace std; const int N = 100005; char s[N]; int main() { scanf("%s", s); int c0 = 0, c1 = 0, ca = 0; int i; for (i = 0; s[i]; ++i) { if (s[i] == '0') { ++c0; } else if (s[i] == '1') { ++c1; } else { ++ca; } } char last = s[i - 1]; if (c0...
11
#include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, r; cin >> n >> r; double k = pow(2, n); vector<long long> c((long long)k); double su = 0; for (long long int i = 0; i < k; i++) { cin >> c[i]; su += c[i]; } ...
17
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; const ll MOD = 1e9 + 7; ll cnt[1000050], ans; bool bit[1000050]; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); ll n; cin >> n; ll arr[n]; for (ll i = 0; i < (n); ++i) cin >> arr[i]; ; sort(ar...
7
#include <bits/stdc++.h> using namespace std; void solve() { int64_t n, m, l = 0, r = 1E18 + 1; cin >> n >> m; if (m >= n) { cout << n << endl; return; } double th = sqrt(2 * (n - m)); while (r - l > 1) { uint64_t mi = l + (r - l) / 2, p; if (((double)mi) > th) { r = mi; } else { ...
8
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> static inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> static inline void amax(T &x, U y) { if (y > x) x = y; } const long long inf = (long long int)1e18 + 17; const int iinf = 1500000000; const int d...
10
#include <bits/stdc++.h> using namespace std; vector<long long> arr; bool check(long long x, long long m) { long long n = arr.size(); long long tmp = 0; for (long long i = 0; i < n; i++) { if (arr[i] + x < tmp) return false; if ((arr[i] <= tmp && tmp <= arr[i] + x) || (arr[i] <= tmp + m && tmp + m...
9
#include <bits/stdc++.h> using namespace std; const int maxn = 2 * 1e5 + 10; long long fen[maxn]; long long gro[maxn]; long long jaye[maxn]; vector<long long> v[maxn]; vector<long long> ans[maxn]; int h[maxn]; long long w; void add1(int i, int x) { if (i != 0) { while (i < maxn) { fen[i] += x; i += i ...
13
#include <bits/stdc++.h> using namespace std; void solve(int limak_votes, int arr[], int n) { ios_base::sync_with_stdio(false); int count = 0; sort(arr, arr + n, greater<int>()); while (limak_votes <= arr[0]) { limak_votes++; arr[0]--; count++; sort(arr, arr + n, greater<int>()); } cout << c...
4
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long int u; cin >> u; while (u--) { long long int n, f = 0, x, y, ans = -1, k, mx, ans1; cin >> n >> k; vector<long long int> a; map<long long int, long long i...
6
#include <bits/stdc++.h> using namespace std; inline int safeMod(int x, int p) { return (x + p) % p; } inline int ls(int x) { return x << 1; } inline int rs(int x) { return x << 1 | 1; } const int N = 1e5 + 10; const int mod = 1e9 + 7; int n, m, k; int main() { int _ = 1; while (_--) { cin >> n >> m >> k; v...
7
#include <bits/stdc++.h> const int N = 100010; const int M = 30000010; int a[N], dp[N], cnt[M], max = 0; int n; std::vector<int> e[N]; void dfs(int u) { max = std::max(max, dp[u]); for (auto v : e[u]) { dp[v] = dp[u] + 1; dfs(v); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { sca...
15
#include <bits/stdc++.h> using namespace std; vector<long long> vc(4); bool ok() { long long x = 0; for (auto i : vc) if (i & 1) x++; return (x <= 1); } bool si() { long long x = 0; for (int i = 0; i < 3; i++) if (vc[i] > 0) vc[i]--, x++; if (x == 3) { vc[3] += 3; return ok(); } else r...
2
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; struct Sync_stdio { Sync_stdio() { cin.tie(NULL); ios_base::sync_with_stdio(false); } } _sync_stdio; struct FAIL { FAIL() { cout << "CHANGE!!!" << "\n"; } }; int n, x, y; int add = 0; int real_n; int ask(vector<int> q...
16
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; const int N = 3003; int sum[N]; int in[N]; int main() { int r, c, n, k; scanf("%d %d %d %d", &r, &c, &n, &k); vector<int> viol[N]; int ant[N], prox[N]; sum[0] = k; sum[r + 1] = k; for (int i = 0; i < n; i++) { ...
22
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const int MAXN = 60; long long n; long long m, k, matrix[MAXN][MAXN]; void matrix_mul(const long long n, long long x[][MAXN], long long y[][MAXN]) { long long i, j, k; long long tmp[MAXN][MAXN]; memset(tmp, 0, sizeof(tmp)); for (i =...
11
#include <bits/stdc++.h> using namespace std; struct Status { int x; int y; int step; }; char MAP[10][10]; bool limit[10][10][10]; bool is(int x, int y, int step) { if (x < 1) return false; if (x > 8) return false; if (y < 1) return false; if (y > 8) return false; if (step <= 8 && limit[x][y][step]) ret...
7
#include <bits/stdc++.h> using namespace std; const int N = 55; map<string, int> mp; int n, m, num, ans; int cnt[N], vis[N], link[N][N], group[N]; vector<int> vec; int dfs(int x, int tot) { for (int i = x + 1; i <= num; i++) { if (cnt[i] + tot <= ans) return 0; if (link[x][i]) { int flag = 0; for ...
14
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") const long long INF = 0x3f3f3f3f3f3f3f3f; const long long llinf = (1LL << 62); const int inf = (1 << 30); const int nmax = 1e5 + 50; const long long mod = 1e9 + 7; using namespace std; int n, m, viz[nmax], bd[nmax], x, y, i, j, t, ...
22
#include <bits/stdc++.h> using namespace std; struct zj { int x, y; }; bool cmp(struct zj w, struct zj r) { return (w.x < r.x); } struct zj a[100005]; int main() { int i, j, k, m, n, count = 0; scanf("%d%d", &n, &m); if (m % 2 != 0) { for (i = 1; i <= m; i++) { a[i].y = i; if (i < (m + 1) / 2) ...
5
#include <bits/stdc++.h> using namespace std; void solve() { string N; int K; cin >> N >> K; while (true) { set<char> st; for (int i = 0; i < N.size(); i++) st.insert(N[i]); if (st.size() <= K) { cout << N << endl; return; } st.clear(); int nw = 0; for (;; nw++) { s...
11
#include <bits/stdc++.h> using namespace std; class llll { public: long long first; long long second; llll() {} llll(long long f, long long s) : first(f), second(s) {} friend bool operator<(const llll &l1, const llll &l2) { if (l1.first == l2.first) return l1.second < l2.second; else retu...
12
#include <bits/stdc++.h> using namespace std; const long double pi = acos(-1.0); long double len[105]; const long double eps = 1e-1; int n; int main() { srand(time(0)); ios_base::sync_with_stdio(false); cin >> n; if (n == 3 || n == 4) { cout << "No solution"; return 0; } while (1) { complex<long...
15
#include <bits/stdc++.h> #pragma GCC optimize("O2,O3,Ofast") using namespace std; int a[5005], ton[1000005], cnt[1000005]; int abs(int x) { return x > 0 ? x : -x; } int main() { int n, k; scanf("%d%d", &n, &k); for (register int i = 1; i <= n; ++i) { scanf("%d", &a[i]); for (register int j = 1; j < i; ++j...
16
#include <bits/stdc++.h> using namespace std; int formula(int n) { return (n * (n - 1)) / 2; } int main() { int no_of_input; string input; int ans = 0; vector<int> vect1(26, 0); cin >> no_of_input; for (int i = 0; i < no_of_input; i++) { cin >> input; vect1[input.at(0) - 'a']++; } for (int i = 0...
1
#include <bits/stdc++.h> using namespace std; int main() { int n, a[100001]; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; int x = a[0]; bool res = true; while (x % 2 == 0) x /= 2; while (x % 3 == 0) x /= 3; for (int i = 1; i < n; ++i) { while (a[i] % 2 == 0) a[i] /= 2; while (a[i] % 3 == 0...
5
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; void solve() { int n; cin >> n; long long ans = 0; if (n & 1) cout << n / 2 << "\n"; else cout << (n / 2) - 1 << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) solve(...
0
#include <bits/stdc++.h> using namespace std; inline long long getnum() { char c = getchar(); long long num, sign = 1; for (; c < '0' || c > '9'; c = getchar()) if (c == '-') sign = -1; for (num = 0; c >= '0' && c <= '9';) { c -= '0'; num = num * 10 + c; c = getchar(); } return num * sign; }...
10
#include <bits/stdc++.h> using namespace std; inline long long rd() { long long _x = 0; int _ch = getchar(), _f = 1; for (; !isdigit(_ch) && (_ch != '-') && (_ch != EOF); _ch = getchar()) ; if (_ch == '-') { _f = 0; _ch = getchar(); } for (; isdigit(_ch); _ch = getchar()) _x = _x * 10 + _ch - '0...
18
#include <bits/stdc++.h> using namespace std; int cnt[1000], sum[1000]; int n, m, K, P; int main() { scanf("%d%d%d", &n, &m, &K); if (abs(n - m) % 2 == 1) { printf("0\n"); return 0; } bool flg = false; if (n > m) swap(n, m), flg = true; for (int i = 0; i < K; i++) { int x, y, z; scanf("%d%d%...
17
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long i; for (i = 1; i < s.length() - 1; i++) { if (s[i] == 'A' && (s[i - 1] == 'B' || s[i + 1] == 'B') && (s[i - 1] == 'C' || s[i + 1] == 'C')) { cout << "Yes\n"; return 0; } else if (s[i] == 'B' && (...
1
#include <bits/stdc++.h> using namespace std; int arr[100005]; map<int, int> mp; int main() { int n; scanf("%d", &n); getchar(); char c; int sum = 0; int Max = 0; mp[0] = -1; for (int i = 0; i < n; i++) { scanf("%c", &c); if (c == '0') sum += -1; else sum += 1; arr[i] = sum; ...
7
#include <bits/stdc++.h> using namespace std; int arr[1000]; int main() { int n; cin >> n; int s = 0; int l = 0, u = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; s += arr[i]; } if (s % n != 0) cout << "Unrecoverable configuration." << endl; else { bool all = true; for (int i = 0; ...
5
#include <bits/stdc++.h> using namespace std; const long long N = 800001; long long n, m, tot, k, ans, q; long long head[N], f[N], d[N], c[N]; struct lll { long long to, next; } e[N]; inline long long read() { long long x = 0, k = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') k = -1;...
13
#include <bits/stdc++.h> using namespace std; int n; long long k, mxd; vector<long long> a, b; vector<long long> luk; void solve() { long long disL = 0, disR = 0, nL = n, nR = 0; int ans = 0; double dL = 0, dR = 0; for (int i = 0; i < n; i++) dL += a[i], disL += a[i]; for (int i = 1, j = 1, pa = 0, pb = 0; i ...
17
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-9; template <class T> bool INRANGE(T x, T a, T b) { return a <= x && x <= b; } template <class T> void amin(T &a, T v) { if (a > v) a = v; } template <class T> void amax(T &a, T v) { if (a < v) a = v; } int ROUND(double x) { return (int)(x...
6
#include <bits/stdc++.h> using namespace std; int const MAXN = 2e6 + 100; int const P = 1e6 + 10; int pi[MAXN]; bool v[MAXN]; vector<int> rem; struct BIT { int x[MAXN + 10], v[MAXN + 10], n, st; void init(int _n = MAXN, int _st = P - 1) { n = _n, st = _st; memset(x, 0, sizeof(x)); memset(v, 0, sizeof(v)...
14
#include <bits/stdc++.h> using namespace std; long long gcd(long long u, long long v) { while (v != 0) { long long r = u % v; u = v; v = r; } return u; } int n, m; int fst[100000]; int nxt[200000]; int to[200000]; int qh, qt; int q[100000]; int par[100000]; int d[100000]; int md[100000][3]; int mdi[10...
20
#include <bits/stdc++.h> using namespace std; double m, n, s; int main() { scanf("%lf%lf", &m, &n); for (int i = 1; i < m; i++) s += pow((double)i / m, n); printf("%.5lf", m - s); }
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long t; cin >> t; while (t--) { long long a; cin >> a; long long c[a], d[a]; for (long long i = 0; i < a; i++) { d[i] = i; } map<long long, long long> mp; for ...
11
#include <bits/stdc++.h> using namespace std; int p[101]; void initialize(int n) { int i; for (i = 0; i < n; i++) { p[i] = i; } } int findpar(int x) { if (x == p[x]) return x; p[x] = findpar(p[x]); return p[x]; } bool find(int x, int y) { return (findpar(x) == findpar(y)); } void unio(int x, int y) { ...
8
#include <bits/stdc++.h> using namespace std; void Fast_IO() { ios::sync_with_stdio(0); cin.tie(0); } int main() { Fast_IO(); long long n, q; cin >> n >> q; long long a[n]; for (long long i = (long long)0; i <= (long long)n - 1; i++) cin >> a[i]; sort(a, a + n); long long sum = 0; for (long long i =...
13
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int max; if (n % 2 == 0) max = (n * n) / 2; else max = ((n / 2) * n) + (n / 2) + 1; if (k > max) printf("NO\n"); else { if (k == 0) { printf("YES\n"); for (int i = 0; i < n; i++) { for (...
6
#include <bits/stdc++.h> using namespace std; template <typename T> void Outln(const string &sep, const T &val) { cout << val << '\n'; } template <typename T, typename... Args> void Outln(const string &sep, const T &val, Args... args) { cout << val << sep; Outln(sep, std::forward<Args>(args)...); } template <type...
4
#include <bits/stdc++.h> using namespace std; void FLASH() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } void SETF() { cout.ios_base::setf(ios_base::fixed); } void UNSETF() { cout.ios_base::unsetf(ios_base::fixed); } int ar[2000001], az[2000001], fmv[2000001], smv[2000001], ST[2000001][21];...
18
#include <bits/stdc++.h> using namespace std; int main() { int tc; scanf("%d", &tc); while (tc--) { int n; scanf("%d", &n); vector<set<int> > cn(n); vector<bool> visited(n); vector<int> nxt(n, -1); vector<vector<int> > a(n, vector<int>(3)); for (int i = 0; i < n - 2; ++i) { for (...
16
#include <bits/stdc++.h> using namespace std; vector<int> d[200005]; int last[200005]; void fun() { int i, j; for (i = 1; i < 200005; i++) { for (j = 1; i * j < 200005; j++) d[i * j].push_back(i); } } int arr[200005]; int main() { fun(); int i, j, n, m, k, a, b, ans; scanf("%d", &n); ; for (i = 1; i...
11
#include <bits/stdc++.h> using namespace std; long long sm(long long x) { return (x * (x + 1)) / 2; } bool cp(long long i, long long j) { return i > j; } long long a[200001]; long long n; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { c...
10
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int SIZE = 1e6 + 10; long long f(long long N, long long K) { if (K == 1) return N / 2; K--; map<long long, long long> q[2]; q[0][N / 2] = 1; q[1][N - N / 2] = 1; long long use[2] = {}; while (1) { pair<long long, long long> v...
21
#include <bits/stdc++.h> char s[5005], t[5005]; int a[5005][5005], i, j, ans; int main(void) { gets(s); gets(t); for (i = 0; s[i]; i++) { for (j = 0; t[j]; j++) { a[i + 1][j + 1] = (a[i + 1][j] + (t[j] == s[i]) * (a[i][j] + 1)) % 1000000007; } } for (i = 0; s[i]; i++) ans = (ans + a[i ...
9
#include <bits/stdc++.h> using namespace std; inline int rd() { int x = 0; char c = getchar(); while (!isdigit(c)) { c = getchar(); } while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); } return x; } inline int max(const int &x, const int &y) { return x < y ? y : x; } inlin...
15
#include <bits/stdc++.h> using namespace std; vector<long long> pre; int main() { long long a, b; cin >> a >> b; for (long long i = 1; i < 60; i++) { long long iz = (1ll << (i + 1)) - 1; iz--; pre.push_back(iz); for (long long j = 0; j < i - 1; j++) { iz = iz | (1ll << j); iz = iz & (~...
5
#include <bits/stdc++.h> using namespace std; int kase; string s; int n, a[3005]; int main() { cin >> kase; while (kase--) { cin >> n; cin >> s; if (n == 1) { cout << "-1\n"; continue; } else { string ans; int cnt = 0; for (int i = 0; i < n; i++) { if ((s[i] - '...
1
#include <bits/stdc++.h> using namespace std; const int maxn = 100 * 1000 + 1; int n; int a[maxn]; long long dp[2][2][maxn]; int main() { cin >> n; for (int i = 1; i < n; ++i) cin >> a[i]; for (int i = 2; i <= n; ++i) { if (a[i - 1] > 1) dp[0][1][i] = dp[0][1][i - 1] + a[i - 1] - a[i - 1] % 2; } for (int ...
12
#include <bits/stdc++.h> using namespace std; const int INF = ((long long)1E9 + 7); template <class C> void mini(C& a, C b) { a = min(a, b); } template <class C> void maxi(C& a, C b) { a = max(a, b); } const int MAXN = 100100; int main() { ios_base::sync_with_stdio(false); int k, n, mb, t; cin >> k >> n >> mb...
18
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long INF = 1e18; long long ceel(long long a, long long b) { return ((a + b - 1ll) / b); } void solve() { long long n; cin >> n; string s; long long ans[n + 1]; long long flag = 0; long long x; long long idx = 1; whil...
10
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int n, m, k; map<pair<int, int>, vector<int> > sensor; bool visitado[100000 + 5 + 100000 + 5 + 100000 + 5 + 100000 + 5]; long long v[100000 + 5 + 100000 + 5 + 100000 + 5 + 100000 + 5]; long long dist(...
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e3 + 5, Mod = 1e9 + 7; char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc; int ny; inline char gc() { return pp1 == pp2 && (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2) ? EOF : *pp1++; } inline int ...
24
#include <bits/stdc++.h> using namespace std; int a[100000]; int main() { int t; cin >> t; int n; for (int i = 1; i <= t; i++) { cin >> n; if (n < 2) cout << "0" << endl; else cout << n - 2 << endl; } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, c = 0; cin >> n; if (n % 2 == 0) { for (i = 1; i <= n / 2; i++) { if (n % 2 == 0) { c++; } } cout << c << endl; for (i = 1; i <= c; i++) { cout << 2 << " "; } } if (n % 2 != 0) { co...
0
#include <bits/stdc++.h> using namespace std; const int N = 100005; long long mod = 1e9 + 7; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } bool is_vowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return...
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; double ans, cnt, s; double p[maxn]; int n; void solve() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lf", p + i); p[0] = 0; for (int i = 1; i <= n; i++) { cnt = (cnt + s * 2 + 1 - p[i - 1]) * p[i]; s = (s + 1 - p[i - 1]) ...
12
#include <bits/stdc++.h> using namespace std; set<int> s; int a[200010], num[200010], dig[111]; int main() { int n, m; cin >> n >> m; long long sum = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; if (sum <= m) num[i] = 0; else { long long ans = sum - m; int p = 0;...
9
#include <bits/stdc++.h> using namespace std; struct G { int n, m; int Next[1000003]; int head[200003]; int ver[1000003]; int val[1000003]; int tot; void add(int x, int y, long long w) { ver[++tot] = y, Next[tot] = head[x], head[x] = tot, val[tot] = w; } void clear() { tot = 0; memset(head...
11
#include <bits/stdc++.h> using namespace std; const int inf = ~0U >> 1; const long long LINF = ~(((long long)0x1) << 63) / 2; template <class T> bool get_max(T& a, T& b) { return b > a ? a = b, 1 : 0; } template <class T> bool get_min(T& a, T& b) { return b < a ? a = b, 1 : 0; } int n, x[1003], y[1003]; int main() ...
17
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const long long INF_LL = 1000000000000000000LL; const double pi = 2 * acos(0.0); template <class T> void smin(T& a, T b) { if (a > b) a = b; } template <class T> void smax(T& a, T b) { if (a < b) a = b; } template <class T> T gcd(T a, T b) { ...
15
#include <bits/stdc++.h> using namespace std; bool check(const vector<string> &a, const vector<string> &b) { int n = int(a.size()), m = int(b.size()); int i = 0, s = 0; while (i < n) { int p = -1; for (int(j) = (int)(s); (j) < (int)(m); ++(j)) if (b[j] == a[i]) { p = j; break; ...
18
#include <bits/stdc++.h> #pragma GCC target("popcnt") using namespace std; using ll = long long; void solve() { int n; cin >> n; ll arr[n], mn = -1e15 - 20, mx = 1e15 + 20; for (int i = 0; i < n; i++) { cin >> arr[i]; mn = min(mn, arr[i]); mx = max(mx, arr[i]); } if (mx - mn == 0 || (mn == mx &&...
3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 3511; pair<int, int> a[N]; void init() { srand((unsigned)time(NULL)); } int main() { long long i, n, r, avg, sum = 0, p = 0, num = 1, qi = 0, x = 0; init(); cin >> n >> r >> avg; for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].second, &a[i...
6
#include <bits/stdc++.h> using namespace std; long long INF = 1000000007; void solve() { int n; cin >> n; n--; vector<int> inp; vector<int> need; int zero_pos = 0; for (int i = 0; i < n + 1; ++i) { int tmp; cin >> tmp; if (tmp != 0) inp.push_back(tmp); } int start = 0; for (int i = 0; i ...
5
#include <bits/stdc++.h> using namespace std; long long pos[100]; int main() { long long h, n; cin >> h >> n; long long ans = 0; long long p = h; while (p >= 0) { pos[p] = n; n = (n + 1) / 2; p--; } int cr = 1; for (int i = 0; i < h; i++) { if (cr != pos[i + 1] % 2) { ans += (1LL <...
9
#include <bits/stdc++.h> using namespace std; const int N = 19; int mx[N][1 << N], mn[N][1 << N]; pair <int, int> query(int l, int r) { assert(l <= r); int p = 31 - __builtin_clz(r - l + 1); int a = min(mn[p][l], mn[p][r - (1 << p) + 1]); int b = max(mx[p][l], mx[p][r - (1 << p) + 1]); retur...
13
#include <bits/stdc++.h> using namespace std; int n, m, md, hd; vector<int> hud; bool used[7] = {0, 0, 0, 0, 0, 0, 0}; long long f(int i) { long long ret = 0; if (i == hud.size()) { int hr = 0, mn = 0; for (int d = 0; d < hd; d++) hr *= 7, hr += hud[d]; for (int d = hd; d < hud.size(); d++) mn *= 7, mn ...
9
#include <bits/stdc++.h> using namespace std; using cd = complex<double>; const int Inf = 2750131; const int mod = 1000000007; const double Pi = acos(-1); void Fastio() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } long long n, ans = 0; void Solve() { cin >> n; for (long long i = 2; i <= n; i...
10
#include <bits/stdc++.h> using namespace std; const int Maxn = 1010; const int Maxm = Maxn * Maxn * 2 + Maxn * 2 * 2; const int Inf = 2100000000; struct Edge { int v, nxt; int f; Edge() {} Edge(int v0, int n0, int f0) { v = v0; nxt = n0; f = f0; } }; Edge e[Maxm * 2]; int head[Maxn]; int nume = 1;...
14
#include <bits/stdc++.h> using namespace std; const long long maxll = -1u >> 1; const long long inf = 0x3f3f3f3f; const long long maxn = 1e5 + 10; long long n, k; long long sum[maxn]; long long bin() { long long l = 1, r = maxn - 5; while (l < r) { long long mid = l + r >> 1; if (sum[mid] >= k) r = mi...
5