solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); long long int csb(long long int x) { if (x <= 0) return 0; return (x % 2 == 0 ? 0 : 1) + csb(x / 2); } long long int tb(long long int n) { long long int count = 0; while (n) { count++; n >>= 1; } return count; } bool ip2(lon...
8
#include <bits/stdc++.h> using namespace std; int k, du[505], hav[505][505], A[505 * 505], B[505 * 505]; int main() { int n, i, j, num = 0; scanf("%d", &k); if (k % 2 == 0) { puts("NO"); return 0; } if (k == 1) { puts("YES\n2 1\n1 2"); return 0; } n = k + 2; for (i = 0; i < n; i++) f...
11
#include <bits/stdc++.h> using namespace std; set<int> c; int main() { string s; cin >> s; int sz = s.size(); for (int i = s.size() - 1; i > 0; --i) { if (s[i] == '0') { sz--; } else break; } int k = sz - 1; for (int i = 0; i < (sz + 1) / 2; ++i) { if (s[i] == s[k]) { c.inser...
1
#include <bits/stdc++.h> using namespace std; int main() { long long k, d; cin >> k >> d; string ans; ans += 48 + d; if (d > 0) { while (k >= 2) { ans += '0'; k--; } cout << ans; } else if (d == 0 && k == 1) { cout << 0; } else cout << "No solution"; }
3
#include <bits/stdc++.h> using namespace std; int n, m, k, arr[71][71], dp[71][71][71][36]; int mulMod(long long, long long); int addMod(int, int); void readArray(int *, int); void printArray(int *, int); int solve(int i, int j, int mod, int left) { if (i == n - 1 && (!left || j == m)) return (mod == 0 ? 0 : -1e8); ...
13
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; template <typename A, typename B> A min(A x, B y) { if (x < y) return x; return y; } template <typename A, typename B> A max(A x, B y) { if (x > y) return x; return y; } template <typename D> istream &...
1
#include <bits/stdc++.h> using namespace std; const int N = (int)3e5 + 5; const int inf = (int)1e9 + 7; int n; int a[N], sgn[N], now[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); sgn[i] = 1; if (a[i] < 0) { a[i] *= -1; sgn[i] = -1; } } int sum =...
2
#include <bits/stdc++.h> using namespace std; const long mod = 1e9 + 7; int main(void) { ios_base::sync_with_stdio(false); cin.tie(0); int T, A, B; cin >> T; for (int i = 0; i < T; ++i) { cin >> A >> B; cout << A + B << endl; } return 0; }
0
#include <bits/stdc++.h> int n, m, x, y, a, b; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { scanf("%d%d%d%d%d%d", &n, &m, &x, &y, &a, &b); int c = gcd(a, b); a /= c, b /= c; int mr = n / a, mc = m / b; if (mr > mc) a *= mc, b *= mc; else a *= mr, b *= mr; int midr = (a...
9
#include <bits/stdc++.h> using namespace std; const int maxN = 1000 + 10; int n, m, c[maxN][maxN]; long long k1, k2, k3, k4, k5; int main() { cin >> n >> m; for (int I = 0; I < (n); I++) for (int K = 0; K < (m); K++) scanf("%d", &c[I][K]); for (int I = 0; I < (n); I++) for (int K = 0; K < (m); K++) { ...
10
#include <bits/stdc++.h> using namespace std; static int N, M, K, D, T, dist[200003]; static vector<pair<int, int>> edge[200003], pre[200003]; static char output[200003]; static vector<string> answer; inline void bfs(int S) { queue<int> Q; Q.push(S); dist[S] = 1; while (!Q.empty()) { int u = Q.front(); ...
13
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); bool flag[3003] = {false}; int n; cin >> n; for (int i = 1; i <= n; ++i) { int temp; cin >> temp; flag[temp] = true; } for (int i = 1; i <= n + 1; ++i) { if (!flag[i]) { cout << i << "\n"; r...
4
#include <bits/stdc++.h> int main() { int m, sum = 0; scanf("%d", &m); char arr[m][m]; for (int i = 0; i < m; i++) { scanf("%s", arr[i]); } for (int i = 0; i < m; ++i) { for (int j = 0; j < m; j++) { if (arr[i][j] == 'C') { for (int k = j + 1; k < m; k++) { if (arr[i][k] == '...
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 10; int x, y; int a[maxn]; int b[maxn]; int mp[maxn][maxn]; const int mv[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; void move(int tx, int ty) { int vx = tx == x ? 0 : (tx - x) / abs(tx - ...
17
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ch = 0, nech = 0; while (n--) { long long a; cin >> a; a % 2 == 0 ? ch++ : nech++; } nech > ch ? cout << ch : cout << nech; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int t, i, j, ones, zeros, n; cin >> t; for (i = 0; i < t; i++) { ones = 0; zeros = 0; cin >> n; vector<int> high; vector<int> low; map<int, int> connect; int p[n]; int q[n]; string s; for (j = 0; j < n; j++) { ...
2
#include <bits/stdc++.h> using namespace std; int main() { int m, n; cin >> m >> n; long double sol = 0; for (int i = 1; i <= m; ++i) { sol += i * powl(1.L * i / m, n); sol -= i * powl(1.L * (i - 1) / m, n); } cout << sol << '\n'; }
8
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vi>; using vvl = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; using vpi = vector<pii>; using vpl = vector<pll>; int main() { ios_base::sync_with_stdio(false); ...
6
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; char s1[maxn], s2[maxn]; int L[maxn], R[maxn], Last[maxn]; int main() { scanf("%s%s", s1 + 1, s2 + 1); int len1 = strlen(s1 + 1); int len2 = strlen(s2 + 1); memset(L, -1, sizeof(L)); memset(R, -1, sizeof(R)); memset(Last, -1, sizeof(La...
11
#include <bits/stdc++.h> #pragma GCC optimize("O3") #define endl '\n' using namespace std; int l, r; void read() { cin >> l >> r; } struct edge { int a, b, c; edge() {} edge(int _a, int _b, int _c) { a = _a; b = _b; c = _c; } }; vector <edge> edges; void solve() { cout << "YES" << endl; if (l == r...
17
#include <bits/stdc++.h> using namespace std; namespace IO { const int BUFFER_SIZE = 1 << 15; char input_buffer[BUFFER_SIZE]; int input_pos = 0, input_len = 0; char output_buffer[BUFFER_SIZE]; int output_pos = 0; char number_buffer[100]; uint8_t lookup[100]; void _update_input_buffer() { input_len = fread(input_buffe...
27
#include <iostream> using namespace std; int dp[35005],lst[35005],l[35005],qwq[35005]; struct stree { int now,l,r,add,mn; }t[132000]; const int X=2e9; inline void build(int now,int l,int r) { t[now].l=l,t[now].r=r,t[now].add=0; if(l==r) { t[now].mn=X; return ; } int mid=(l+r)/2; build(now*2,l,mid),build(now*...
17
#include <bits/stdc++.h> int n, A, B, C, D; unsigned int ans; std::bitset<100000003> vis; void solve(int p) { unsigned int v = 1u * A * p * p * p + 1u * B * p * p + 1u * C * p + 1u * D; for (long long i = p; i <= n; i = i * p) { ans = ans + 1u * (n / i) * v; } } int main() { std::ios::sync_with_stdio(0); ...
17
#include <bits/stdc++.h> using namespace std; int a[int(1e5)]; int b[int(1e5)]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, l, r; cin >> n >> r >> l; if (((l - (n - 1)) * (l - (n - 1)) + n - 1) < r || n > l) puts("-1"), exit(0); for (int i = 0; i < n - 1; i++) puts("1"); ...
6
#include <bits/stdc++.h> using namespace std; struct triple { string name; long long w, h; triple() {} triple(string n, long long ww, long long hh) { name = n, w = ww, h = hh; } bool operator<(const triple &a) const { return name < a.name; } }; int n, T; string name[110]; map<string, long long> mp; long long ...
15
#include <bits/stdc++.h> using namespace std; long long a[5]; long long ans; int main() { cin >> a[1] >> a[2] >> a[3]; sort(a + 1, a + 1 + 3); if (a[3] > 2 * a[1] + 2 * a[2]) a[3] = 2 * (a[1] + a[2]); cout << (a[1] + a[2] + a[3]) / 3 << endl; }
10
#include <bits/stdc++.h> int arr[1005]; int main() { int parity = 0; int n; scanf("%d", &n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { int current; scanf("%d", &current); if (i == j) parity ^= current; } int m; scanf("%d", &m); while (m-- > 0) { int x, y; ...
8
#include <bits/stdc++.h> using namespace std; int a[2100]; int main() { int n, b, i, j; while (scanf("%d%d", &n, &b) != EOF) { for (i = 1; i <= n; i++) scanf("%d", &a[i]); int now, max = b; for (i = 1; i <= n; i++) for (j = i + 1; j <= n; j++) if (a[j] > a[i]) { now = b / a[i] * ...
6
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long fpow(long long a, long long b) { long long r = 1; for (a %= mod; b; b >>= 1) { if (b & 1) r = r * a % mod; a = a * a % mod; } return r; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >...
17
#include <bits/stdc++.h> using namespace std; const int MAXN = 5200 + 15; const int INF = 0x3f3f3f3f; inline void rdInt(int &x) { scanf("%d", &x); } inline void ptInt(int x) { printf("%d", x); } inline void ptInts(int x, char c) { printf("%d%c", x, c); } inline void rdStr(char *x) { scanf("%s", x); } inline void ptStr(...
10
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int a[] = {4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346, 355, 378, 382, 391, 438, 454, 483, 517, 526, 535, 562, 576, 588, 627, 634, 636, 645, 648, 654, 663, 666, 690, 7...
9
#include <bits/stdc++.h> using namespace std; const int MaxK = 20; const int MaxN = 500000; long long d[MaxN], dp[MaxN]; int k; long long a, b; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int lcm(int a, int b) { return (a * b) / gcd(a, b); } int main() { scanf("%I64d%I64d%d", &a, &b, &k)...
12
#include <bits/stdc++.h> using namespace std; int n, l, r, ans, cntr, cntl; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> l >> r; cntl += l; cntr += r; } if (cntl > n / 2) ans += n - cntl; else ans += cntl; if (cntr > n / 2) ans += n - cntr; else ans += cntr; c...
0
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-9; const int maxn = 3e2; const long long inf = 1e9 + 7; vector<pair<int, int> > f, u, ho; string s[100]; int used[10000]; int n; bool check(int dx, int dy) { for (int i = 0; i < f.size(); ++i) { if (f[i].first + dx >= 0 && f[i].first + dx < ...
10
#include <bits/stdc++.h> using namespace std; int main() { int X[3], Y[3]; map<string, int> dic_x, dic_y; for (int i = 0; i < 3; i++) cin >> X[i] >> Y[i]; vector<string> ABC(3); ABC[0] = "A"; dic_x["A"] = X[0]; dic_y["A"] = Y[0]; ABC[1] = "B"; dic_x["B"] = X[1]; dic_y["B"] = Y[1]; ABC[2] = "C"; ...
9
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; struct edge { int u, v, w, id; } c[maxn]; unordered_map<long long, int> mp; int n, m, q, fa[maxn]; long long L[maxn], R[maxn]; int find_set(int x) { return fa[x] ? fa[x] = find_set(fa[x]) : x; } bool cmp(edge a, edge b) { return a.w < b.w; } int ...
15
#include <bits/stdc++.h> using namespace std; long long a, b, t, n, ar[1001], ans1, ans2, i, j, k; int main() { cin >> t; while (t--) { cin >> n; for (long long i = 1; i <= n; i++) cin >> ar[i]; a = 0; b = 0; i = 1; j = n; k = 0; ans1 = 0; ans2 = 0; while (true) { k++; ...
5
#include <bits/stdc++.h> void solution() { long long n; std::cin >> n; std::cout << "0 1" << std::endl; std::string s, t; std::cin >> s; long long l = 1, r = 1e9 - 1; for (long long i = (2); i <= (n); ++i) { long long mid = (l + r) >> 1; std::cout << mid << " 1" << std::endl; std::cin >> t; ...
11
#include <bits/stdc++.h> int main() { int n, i, d = 0, a = 0; scanf("%d", &n); char w[n]; scanf("%s", &w); for (i = 0; i < n; i++) { if (w[i] == 'D') { d++; } if (w[i] == 'A') { a++; } } if (d > a) { printf("Danik"); } if (d < a) { printf("Anton"); } if (d == a)...
0
#include <bits/stdc++.h> using namespace std; static const int maxn = 4e5 + 5; static const long long int is_query = LLONG_MIN; struct Line { long long int m, b; mutable function<const Line *()> succ; bool operator<(const Line &rhs) const { if (rhs.b != is_query) return m < rhs.m; const Line *s = succ(); ...
17
#include <bits/stdc++.h> using namespace std; int v[200200]; int s[7][200200]; int get(int t, int a, int b) { int r = s[t][b]; if (a) r -= s[t][a - 1]; return r; } int main() { int n; scanf("%d", &n); int a, b, c; scanf("%d%d%d", &a, &b, &c); if (a > b) swap(a, b); if (b > c) swap(b, c); if (a > b) ...
16
#include <bits/stdc++.h> const int M = 6e5 + 10; const int mod = 1e9 + 7; using namespace std; inline long long read() { long long e = 1, sum = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') e = -1; c = getchar(); } while (isdigit(c)) { sum = sum * 10 + c - 48; c = getchar(); } ...
17
#include <bits/stdc++.h> using namespace std; string s; int main() { cin >> s; string f = s; long long sum = 0; for (int i = 0; i < s.size(); i++) { int y = (s[i] - 48) % 3; sum += y; s[i] = char(y + 48); } int h = sum % 3, id = -1; if (h == 0) { cout << f; return 0; } for (int i =...
12
#include <bits/stdc++.h> using namespace std; struct atom { int num, w; }; int operator<(atom k1, atom k2) { return k1.w < k2.w; } set<atom> S; int n, m, d, x[210000], p[210000], a[210000]; int compare(int k1, int k2) { return x[k1] < x[k2]; } int main() { scanf("%d%d%d", &d, &n, &m); for (int i = 1; i <= m; i++)...
14
#include <bits/stdc++.h> using namespace std; int w[100005]; int wf[100005]; int main() { ios::sync_with_stdio(0); int n, cl, cr, ql, qr; cin >> n >> cl >> cr >> ql >> qr; wf[0] = 0; for (int i = 1; i <= n; i++) { cin >> w[i]; wf[i] = wf[i - 1] + w[i]; } int tot = 0, cur, l, r; int sum = wf[n]; ...
7
#include <bits/stdc++.h> using namespace std; const int maxn = 262, hm = 52 * 52, maxt = 200; const long long INF = 1000; const long double EPS = 1E-9, EPS2 = 1E-8; const long double M_PIS = 3.14159265358979323846264338327950288; unsigned char color[hm]; string s[maxn]; pair<int, int> deltas[4]; pair<int, int> pix[hm][...
13
#include <bits/stdc++.h> using namespace std; struct Team { string name; int points; int zab; int prop; int playGames; }; bool comp(Team a, Team b) { bool res = true; if (a.points > b.points) return res; else if (a.points == b.points) { if ((a.zab - a.prop) > (b.zab - b.prop)) return res; ...
10
#include <bits/stdc++.h> using namespace std; long long tavan(long long x, long long y) { long long res = 1; while (y) { res *= (y % 2) ? x : 1; res %= ((long long)1e9 + 7); x *= x; x %= ((long long)1e9 + 7); y /= 2; } return res; } long long n, dp[((long long)210)][((long long)210)][((long ...
15
#include <bits/stdc++.h> using namespace std; int ans[31]; void query(int l, int r, int ord, int len) { if (l == r) { cout << l << endl; fflush(stdout); return; } int res; int m = (l + r) >> 1; cout << m << endl; fflush(stdout); cin >> res; if (res == 0) return; if (ans[ord % len] == -1) r...
10
#include <bits/stdc++.h> using namespace std; struct node { long long sum; node() { sum = INT_MAX; } }; class segte { public: long long n{}; vector<node> tree; vector<long long> a; node neutral; void init(long long N) { n = N; tree.resize(4 * n + 1); a.assign(n, INT_MAX); } void put(vecto...
11
#include <bits/stdc++.h> using namespace std; void solve() { int A,B,k; cin >> A >> B >> k; vector<int> a(A),b(B); vector<pair<int,int>> edges(k); for(auto &[x, y] : edges) { cin >> x; } for(auto &[x, y] : edges) { cin >> y; } for(auto &[x,y]: edges) { x--; y--; a[x]++; b[y]++; } long long res...
6
#include <bits/stdc++.h> using namespace std; long long s[500010], ss[500010]; map<long long, int> m; vector<vector<int> > v; int main() { long long n, c = 1, cnt = 0; cin >> n; v.resize(n + 1); for (int i = 1; i <= n; i++) scanf("%lld", &s[i]); for (int i = n; i > 0; i--) { ss[i] = s[i] + ss[i + 1]; ...
9
#include <bits/stdc++.h> using namespace std; int a[26]; char s[100005]; bool compa(int a, int b) { return a > b; } int main() { long long res = 1; bool z = false; for (int i = 0; i < 26; i++) a[i] = 0; cin >> s; if (s[0] == '?') res *= 9; else if (s[0] == '0') { res = 0; cout << "0" << endl; ...
3
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; template <class T> void dbs(string str, T t) { cout << str << " : " << t << endl; } template <class T, class... second> void dbs(string str, T t, second... s) { int idx = str.find(','); cout << str.substr(0, idx) << " : " << t << ","; dbs(st...
14
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int m = 2 * k + 1; int l = n / m; int rest = n % m; if (!rest) { rest = 2 * k; } else { l++; } cout << l << endl; for (int i = rest / 2 + 1; i <= n; i = i + m) { cout << i << " "; } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long int dxx[] = {1, -1, 0, 0}; long long int dyy[] = {0, 0, -1, 1}; long long int modexpo(long long int x, long long int y) { if (y == 0) return 1; if (y % 2) { long long int viky = modexpo(x, y / 2); return (((x * viky) % 1000000007) * viky) % 1000000007;...
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long N1 = 2e5 + 10; int code() { long long n, d; string s; cin >> n >> d >> s; int cnt = 0; long long i = 0; while (i < n - 1) { bool flag = 0; for (long long j = min(i + d, n - 1); j > i; --j) { if (s[j] =...
0
#include <bits/stdc++.h> const double inf = 2000000000; const int pr = 1993; using namespace std; long long res; int n, m; int a[100005]; bool w[100005]; vector<int> ord, g[100005]; bool cmp(int u, int v) { return a[u] > a[v]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int ...
6
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int a[n][n]; long long int i = 0; long long int k = 0; map<long long int, long long int> m; for (i = 0; i < n; i++) { long long int h = k; for (long long int j = 0; j < n; j++) { m[h]++; a[i...
10
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int root = 3; const int maxn = 1 << 20; int f[maxn]; inline int sub(const int& a, const int& b) { return a < b ? a - b + mod : a - b; } inline int add(const int& a, const int& b) { return sub(a, mod - b); } inline int mul(const int& a, con...
16
#include <bits/stdc++.h> int max(int x, int y) { return (x > y ? y : x); } int geGcd(int a, int b) { if (a == 0 || b == 0) return 0; for (int i = max(a, b); i >= 1; i--) if (a % i == 0 && b % i == 0) return i; } using namespace std; int main() { int a, b, n; cin >> a >> b >> n; int cnt = 0; while (n > 0...
0
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; void qread(int &x) { int neg = 1; x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') neg = -1; c = getchar(); } while (c >= '0' && c <= '9') x = 10 * x + c - '0', c = getchar(); x *= neg; } const int maxn = 20...
21
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m, i, j, a, b, o = 0, e = 0, o1 = 0, e1 = 0, c, d; cin >> n >> m; for (i = 0; i < n; i++) { cin >> a; if (a % 2 == 1) o++; else e++; } for (j = 0; j < m; j++) { ...
0
#include <bits/stdc++.h> using namespace std; const int N = 105; int n, a[N]; vector<pair<vector<int>, int>> c; int main() { scanf("%d", &n); vector<int> x(n - 1); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 0, u; i < 32768; i++) { u = __builtin_popcount((a[1] & 32767) ^ i); for (int j...
16
#include <bits/stdc++.h> using namespace std; long long GCD(long long a, long long b) { if (b == 0) return a; return (a % b == 0 ? b : GCD(b, a % b)); } long long POW(long long base, long long exp) { long long val; val = 1; while (exp > 0) { if (exp % 2 == 1) { val = (val * base) % 1000000007; }...
11
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const long long Inf = 1ll << 60ll; namespace io { char buf[(1 << 23)], *p1 = buf, *p2 = buf, c; int f; template <typename T> T read() { T x = 0; f = 0; c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, (1 << 23), stdin), p1 == p2) ?...
11
#include <bits/stdc++.h> using namespace std; const int maxN = 3e5 + 10; struct Segment { int l, r; pair<long long, long long> num; } tree[maxN * 30 + 1]; struct Node { int x, op, a, b; Node(int X = 0, int Y = 0, int Z = 0, int W = 0) { x = X; op = Y; a = Z; b = W; } } a[maxN + 1]; int n, m, c...
17
#include <bits/stdc++.h> using namespace std; map<string, int> mp; set<string> st; int main(int argc, char *argv[]) { int n; string s; int ans = 0; cin >> n; for (int i = 1; i <= n; i++) cin >> s, mp[s]++, st.insert(s); for (int i = 1; i <= n; i++) cin >> s, mp[s]--; set<string>::iterator it; for (it = ...
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; map<char, int> freq; string types = "ACGT"; for (int i = 0; i < n; i++) { if (s[i] != '?') freq[s[i]]++; } for (int i = 0; i < n; i++) if (s[i] == '?') { int maxFreq = -1; bool isEqual =...
1
#include <bits/stdc++.h> using namespace std; long long q[10000010], ans[10000010]; int main() { long long p, k; scanf("%lld%lld", &p, &k); q[0] = -p / k; ans[0] = p % k; int maxd = 0; bool no_sol = false; if (q[0] >= k || q[0] < 0) { for (int d = 1; !no_sol; d++) { maxd = d; if (q[d - 1] ...
12
#include <bits/stdc++.h> using namespace std; int moveX[4] = {0, 1, 0, -1}; int moveY[4] = {1, 0, -1, 0}; int r, c; string board[1005]; int men; int ans; int vis[1005][1005]; queue<pair<int, int> > Q; void solve(int x, int y) { for (int i = int(0); i < int(r); i++) for (int j = int(0); j < int(c); j++) vis[i][j] ...
7
#include <bits/stdc++.h> using namespace std; int main() { for (long long i = 0; i < 2e8; i++) ; int n; scanf("%d", &n); int a[20]; for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); for (int i = 0; i < n; i++) printf("%d ", a[i]); }
11
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n), lst(n + 1, -1), dist(n + 1, -1); vector<int> ans(n + 1, INT_MAX); for (int i = 0; i < n; i++) { cin >> a[i]; if ...
7
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 10; int n; int b[N], ans[N]; pair<int, int> a[N]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &b[i]); } b[n + 1] = b[1]; for (int i = 1; i <= n; i++) { a[i] = make_pair(b[i] + b[i + 1], i - 1); } sort...
13
#include <bits/stdc++.h> using namespace std; static const int mod = 1e9 + 7; void cj() { int tt; cin >> tt; for (int qq = 1; qq <= tt; ++qq) { cout << "Case #" << qq << ": "; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, s; cin >> n >> s; int mx = -1; int sum = 0; for (int ...
0
#include <bits/stdc++.h> using namespace std; int main() { int t, n, m, rb, cb, rd, cd, s, r, c, c1, r1; cin >> t; while (t) { cin >> n >> m >> rb >> cb >> rd >> cd; if (cd >= cb) { c = cd - cb; } else { c = cb - cd; c1 = 2 * (m - cb); c = c + c1; } if (rd >= rb) { ...
0
#include <bits/stdc++.h> typedef struct { long long num; int sub; bool gaboleh0; } store_num; using namespace std; int n; long long res = 0; store_num num[10] = {}; bool compare(store_num num1, store_num num2) { return num1.num > num2.num; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)...
9
#include <bits/stdc++.h> using namespace std; const int MAXN = int(3e5) + 99; int n, m, k; int arr[MAXN]; long long bst[MAXN]; long long psum[MAXN]; long long sum(int l, int r) { l = max(l, 0); return psum[r] - (l == 0 ? 0 : psum[l - 1]); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ...
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t, i, a, b, c, sum; cin >> t; int d[t]; for (i = 0; i < t; i++) { cin >> a >> b >> c; sum = (a - b) * 2; if (sum < 0) { sum = -sum; } if (sum < 4 || max...
0
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; int s[maxn], g[maxn]; int l[maxn], r[maxn]; int ans[maxn]; int main() { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> s[i] >> g[i]; if (i == 0) { l[i] = s[i]; r[i] = s[i] + g[i]; ...
10
#include <bits/stdc++.h> using namespace std; map<int, int> pwr; int main() { int t, n; cin >> t; while (t--) { pwr.clear(); cin >> n; int a, ans = 0; for (int i = 0; i < n; i++) { cin >> a; int two_cnt = 0; while (a % 2 == 0) { two_cnt++; a /= 2; } if...
4
#include <bits/stdc++.h> using namespace std; bool visited[150000]; vector<vector<int>> lst(150000); long long nodes = 0; long long edges = 0; void dfs(int e) { nodes++; visited[e] = true; for (int p = 0; p < lst[e].size(); p++) { edges++; if (!visited[lst[e][p]]) dfs(lst[e][p]); } } void makefalse() { ...
7
#include <bits/stdc++.h> using namespace std; int main() { long long n, n2, a = 0, b; cin >> n; n2 = n; while (n2 > 0) { n2 = n2 / 10; a++; } b = pow(10, a); if ((n % b > 0) && (a > 1)) cout << (b / 10) - (n % (b / 10)); else cout << 1; }
1
#include <bits/stdc++.h> using namespace std; const int N = 20; long long GCD(long long a, long long b) { return (b == 0) ? a : GCD(b, a % b); } inline long long LCM(long long a, long long b) { return a / GCD(a, b) * b; } inline long long normalize(long long x, long long mod) { x %= mod; if (x < 0) x += mod; retu...
18
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("fast-math") using namespace std; template <typename T> void uin(T &a, T b) { if (b < a) a = b; } template <typename T> void uax(T &a, T b) { if (b > a) a = b; ...
16
#include <bits/stdc++.h> using namespace std; int main() { long long int a; cin >> a; char b[a][a]; for (int i = 0; i < a; i++) { for (int j = 0; j < a; j++) { if (i % 2 == 0) { if (j % 2 == 0) { b[i][j] = 'W'; } else b[i][j] = 'B'; } else { if (j % 2 ...
3
#include <bits/stdc++.h> using namespace std; template <typename num_t> inline void add_mod(num_t& a, const long long& b, const int& m) { a = (a + b) % m; if (a < 0) a += m; } template <typename num_t> inline bool update_max(num_t& a, const num_t& b) { return a < b ? a = b, true : false; } template <typename num_...
19
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; if (n < 11) cout << "NO" << endl; else { int p = n; for (int i = 0; i < n; i++) { if (s[i] == '8') { p = i; break; ...
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 12e5 + 3; int Te[MAXN], Lz[MAXN], N, M, A, B, C; void up(int idx, int v) { if (!Lz[idx] || v == -1) Te[idx] = max(v, 0), Lz[idx] = max(v, 0); } void shift(int idx) { up(idx * 2, Lz[idx]); up(idx * 2 + 1, Lz[idx]); Lz[idx] = Te[idx] = 0; } void updat...
7
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back int main() { int t; cin>>t; while(t--){ int n,c=0; cin>>n; while(n--){ int x; cin>>x; if(x==1 or x==3) c++; } cout<<c<<endl; } return 0; }
0
#include "bits/stdc++.h" #define ll long long #define pb push_back using namespace std; const int mxN = 300001; int arr[mxN]; // input array int cnt[mxN]; // cnt[x] = number of occurrences of x in the array bool ans[mxN]; // the final answer int n; void solve() { int n; cin >> n; for (int i = 0; i <= n; +...
10
#include <bits/stdc++.h> using namespace std; int read() { 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 = 10 * x + ch - '0'; ch = getchar(); } return x * f; } int gcd(int x, int y) { return...
11
#include <bits/stdc++.h> using namespace std; void solve() {} signed main() { long long int n1, n2, k1, k2; cin >> n1 >> n2 >> k1 >> k2; if (n1 > n2) cout << "First" << endl; else cout << "Second" << endl; }
0
#include <bits/stdc++.h> using namespace std; struct node { int va, num; bool operator<(const node &o) const { if (va != o.va) return va < o.va; return num > o.num; } } a[2005]; int b[2005], n; bool mark[2005]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i].va); ...
14
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, q, i, j; cin >> n >> m >> q; string a, b; cin >> a >> b; vector<long long> v(n + 1); for (i = 0; i <= n - m; i++) { string tmp = a.substr(i, m); if (tmp == b) v[i +...
5
#include <iostream> using namespace std; bool cal(string &a, int k) { int n=a.length(),count=0; for(int i=0; i<n; i++) { int val=a[i]-'A'; if((k&(1<<val))) count++; else count--; if(count<0) return 0; } if(count==0) return 1; return 0; } int main() { // your code goes here int t; cin>>t; w...
1
#include <bits/stdc++.h> using namespace std; int a[3005]; int main() { memset(a, 0, sizeof a); int n, l = 0, h = 0; cin >> n; for (int i = 2; i <= 3005; i++) { if (a[i] == 0) { for (int j = i; j * i <= n; j++) { a[j * i] = 1; } } } for (int i = 1; i <= n; i++) { l = 0; f...
1
#include <bits/stdc++.h> int main() { int t; scanf("%d\n", &t); while (t--) { int n; scanf("%d\n", &n); int a[n]; for (int i = 0; i < n; ++i) { scanf("%d", &(a[i])); } std::sort(a, a + n); int steps = 0; int sum = std::accumulate(a, a + n, 0); int numOfZeros = std::count(...
0
#include <bits/stdc++.h> int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } using namespace ::std; const long long maxn = 5e5 + 500; const long long inf = 1e17 + 800; long long tav2(long long a) { return a * a; } struct CHT { struct line { long long m, b; long double x; long long val; bool i...
19
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int t; int n, k; int a[200010], b[200010], pos[200010]; int qwq[200010]; int lst[200010], nxt[200010]; inline void rmain() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", a + i), pos[a[i]] = i, qwq[i] = 0; for (int i = 1; i...
10