solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int arr[N]; int n; long long solve(int l, int r) { if (l == r) { return 1ll * arr[l] * (n - arr[l] + 1); } int half = (l + r) >> 1; long long res = solve(l, half) + solve(half + 1, r); int L = min(arr[half], arr[half + 1]); int R = max...
6
#include <bits/stdc++.h> using namespace std; struct node { int h, m; int hehe; } a[110]; int n; char s[10000]; int main() { int i, j; cin >> n; ; getchar(); for (i = 1; i <= n; i++) { gets(s); a[i].h = (s[1] - '0') * 10 + s[2] - '0'; a[i].m = (s[4] - '0') * 10 + s[5] - '0'; if (a[i].h == ...
5
#include <bits/stdc++.h> using namespace std; int main() { int n, i, c = 0, zero = 0, five = 0; cin >> n; while (n--) { cin >> i; if (i == 0) zero++; else five++; } if (zero == 0) { cout << -1; return 0; } five /= 9; five *= 9; if (five <= 0) cout << 0; else { ...
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); double a, b, c, d, e, f; cin >> a >> b >> c; d = pow(b, 2) - (4 * a * c); if (d < 0) { cout << 0; } else if (a == 0 and b == 0 and c == 0) { cout << -1 << endl; } else if (a == 0 and b == ...
6
#include <bits/stdc++.h> using namespace std; #define REP(i,a,n) for(int i=(a);i<(n);++i) #define BREP(i,a,n) for(int i=(a);i>(n);--i) #define TRAV(i, v) for(auto &i: v) #define fi first #define se second #define pb push_back #define SZ(x) (int((x).size())) #define ALL(x) (x).begin(), (x).end() #define INDEX(arr,ind)...
4
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int arr[n]; for (long long int &i : arr) cin >> i; sort(arr, arr + n); set<long long int> x; set<long long int> y; for (long long int i = 0; i < ...
0
#include <bits/stdc++.h> using namespace std; int main() { int r, c, n, k; cin >> r >> c >> n >> k; vector<int> xcords(n); vector<int> ycords(n); for (int i = 0; i < n; i++) { cin >> xcords[i]; cin >> ycords[i]; } int counter = 0; for (int i = 1; i <= r; i++) { for (int j = 1; j <= c; j++) {...
1
#include <bits/stdc++.h> using namespace std; int main() { short n, m, i, j; string s; cin >> n >> m; int d[m][n], ans, cv, t; for (i = 0; i < n; i++) { cin >> s; size_t pos = s.find('1'); if (pos != string::npos) { t = 0; for (j = 0; j < m; j++) { if (s[(j + pos) % m] == '1') ...
3
#include <bits/stdc++.h> using namespace std; int n, a, b, x; vector<vector<int> > v; vector<int> u, w, SUM, sum; int g() { int res = 0; for (int i = 0; i < 16; ++i) { int a = i / 4; int b = i % 4; if (a == b) { if (u[a]) res += SUM[i]; } else { if (u[a] || u[b]) { if ((v[i].size...
8
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; struct rec { long long v; int inc; }; multiset<rec> tr; multiset<rec>::iterator it; bool operator<(rec a, rec b) { return a.v < b.v; } long long tmp, l, r, mid; int a[N], b[N], cnt, n, K, i; void run(int x) { tmp = cnt = 0; tr.clear(); for (...
8
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y, a[100001], i, j; bool flag; cin >> n >> x >> y; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) { flag = true; for (j = i - 1; j >= i - x && j >= 0; j--) if (a[i] > a[j]) flag = false; for (j = i + 1; j <= i...
1
#include <bits/stdc++.h> int main() { int N, M, Q, r, c; int grounds[502], grid[502][502], i, max, mmax, count, j; for (i = 0; i < 502; i++) { for (j = 0; j < 502; j++) { grid[i][j] = 0; } } for (i = 0; i < 502; i++) grounds[i] = 0; scanf("%d%d%d", &N, &M, &Q); for (i = 1; i <= N; i++) { ...
3
#include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<string> #include<set> using namespace std; #define pb emplace_back #define PII pair<int,int> #define ll long long #define mp make_pair const int maxn = 2e6 + 10; void solve() { long long q, a, b ,c,ans = 0; cin >> q >> a >> b >> c; an...
0
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const int MAX = 200009; int a[MAX] = {0}; char s[MAX]; long long pw(long long base, long long expo, long long c) { if (expo == 0) return 1; long long f = pw(base, expo / 2, c); if (expo & 1) return (((f * f) % c) * base) % c; return (f * f) ...
4
#include <bits/stdc++.h> using namespace std; short n, x[2003], y[2003]; int ans = 0; int main() { scanf("%hd", &n); for (int i = 1; i <= n; ++i) scanf("%hd %hd", x + i, y + i); for (int i = 1; i < n - 1; ++i) for (int j = i + 1; j < n; ++j) for (int k = j + 1; k <= n; ++k) ans += !!(x[i] * (y[j...
5
#include <bits/stdc++.h> using namespace std; int a[200005], v[200005]; int main() { int n, minm, maxm, k, f; long long int cnt; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } minm = a[0]; for (int i = 1; i < n; i++) { minm = min(minm, a[i]); } cnt = (long long int)minm *...
2
/* ###################################################################################### # # # ,.--------._ # # / ''. ...
5
#include <bits/stdc++.h> using namespace std; const int inf = (1 << 30) - 1; const long long linf = (1ll << 62) - 1; const double eps = 1e-9; const double pi = 4 * atan(1.0); const int N = 1e5 + 100; int n, k; long long a[N], b[N], c[N]; pair<long long, int> inter[N], pinter[N]; int cnt[N]; long long gcd(long long a, l...
10
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > MST1, MST2; int n, m; int w, v; pair<int, int> ans[1000005]; int main() { cin >> n >> m; for (int i = 0; i < m; i++) { cin >> w >> v; if (v) MST1.push_back({w, i}); else MST2.push_back({w, i}); } sort(MST1.begin(), MST...
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m, j, k, i, a[100], c = 0, l = 0; cin >> n; cin >> m; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (a[i] < a[j]) { k = a[i]; a[i] = a[j]; a[j] = k; ...
0
#include <bits/stdc++.h> using namespace std; long long n, k, x, y, res; long long a[222222], lvl[222222], d[222222], u[222222]; vector<long long> g[222222]; string s; void dfs1(long long v, long long depth = 0, long long par = -1) { lvl[v] = depth; for (long long to : g[v]) { if (par != to) { dfs1(to, de...
5
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5, M = 20; int n, m, q; int p[N], arr[N], to[N]; int last[N], ans[N], par[N][M]; int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < n; i++) { scanf("%d", p + i); if (i) to[p[i - 1]] = p[i]; } to[p[n - 1]] = p[0]; for (int i =...
6
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; int cnt[maxn * 2]; int a[maxn]; int n; int q; int pos[maxn]; int block = 2000; int cntcnt[maxn]; int b[maxn * 2]; int ans[maxn]; struct node { int l, r; int md; int id; int ans; bool operator<(const node &p) const { if (pos[this->l] =...
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int test; test = 1; cin >> test; while (test--) { long long int n, k, ans = 0; cin >> n >> k; while (n > 0) { if (n % k) { ans += n % k; n -= n ...
0
#include <bits/stdc++.h> using namespace std; long long s[20001][2]; int main() { int n; cin >> n; for (size_t i = 0; i < 20001; i++) { s[i][0] = 0; s[i][1] = 0; } long long sum = 0; for (int i = 0; i < n; i++) { int a; cin >> a; for (size_t j = 0; j < 20001; j++) { s[j][i % 2] = 0...
7
#include <bits/stdc++.h> using namespace std; const int64_t posinf = 9223372036854775807LL; const int64_t neginf = 9223372036854775808LL; const uint64_t ullinf = 18446744073709551615ULL; const uint64_t bigprime = 2305843009213693951ULL; const uint64_t smallprime = 2147483647ULL; const int littleprime = 3571; const int ...
4
#include <bits/stdc++.h> using namespace std; inline long long ceil(long long a, long long b) { return (a + b - 1) / b; } void func() { long long k; cin >> k; long long n = 3, x = 1; for (long long i = 4; i < 101; i++) { long long temp = (i * (i - 1) * (i - 2)) / 6; if (temp <= k) { n = i; x...
4
#include <bits/stdc++.h> using namespace std; int x[400000 + 5], y[400000 + 5], l1[400000 + 5], r1[400000 + 5], l2[400000 + 5], r2[400000 + 5]; vector<int> v[400000 + 5]; int main() { int n, i, j; long long ans = 0; scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%d%d", &x[i], &y[i]); x[i] += 1e5...
8
#include <bits/stdc++.h> using namespace std; int n, m; char s[600]; int dp[501][26][26]; pair<int, int> fr[501][26][26]; void output(int i, int j, int k) { if (!i) return; output(i - 1, fr[i][j][k].first, fr[i][j][k].second); for (int i = 0; i < m; i++) putchar((i % 2 ? k : j) + 'a'); puts(""); } int main() { ...
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char arr[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> arr[i][j]; } } vector<int> v; for (int i = 0; i < n; i++) { short counter = 0; short counter1 = 0; for (int j = 0; j < n;...
0
#include <bits/stdc++.h> using namespace std; unordered_map<long long, long long> r; vector<pair<long long, long long> > g[1024]; long long md; int k; int hs(long long a) { return (((a << 30) ^ (a << 17) ^ a) >> 17) & 1023; } long long gt1(long long a) { if (a == 0) return 0; if (a == 1) return 1ll * k * (k + 1) / ...
13
#include <bits/stdc++.h> using namespace std; const int di[] = {-1, 0, 1, 0}; const int dj[] = {0, 1, 0, -1}; const long long MOD = 1e9 + 7; const long long INF = 1e9; string s; vector<int> parent(1000, -1); int nd = 0, r = 0; vector<string> name(1000); int a = 0, b = -1; void add(int prnt) { a = ++b; while (isalph...
4
#include <bits/stdc++.h> using namespace std; int t; int n; int sol, temp; vector<string> a; void duplicates(int i); int main() { cin >> t; for (; t; --t) { sol = 0; cin >> n; a.resize(n); for (int i = 0; i < n; ++i) { temp = 0; cin >> a[i]; duplicates(i); if (temp) ++sol; ...
3
#include <bits/stdc++.h> using namespace std; template <class T> struct rge { T b, e; }; template <class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }; template <class A, class B> ostream &operator<<(ostream &os, pair<A, B> p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ost...
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; template <class T> inline void scan_d(T &ret) { char c; int flag = 0; ret = 0; while (((c = getchar()) < '0' || c > '9') && c != '-') ; if (c == '-') { flag = 1; c = getchar(); } while (c >= '0'...
4
#include <bits/stdc++.h> using namespace std; int n, t, ans = 1, cost[100100]; set<int> nums; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> t; set<int>::iterator it = nums.upper_bound(t); if (it == nums.end()) cost[t]--; else { int to = *it; it++; if (it == nums...
4
#include <bits/stdc++.h> using namespace std; int main() { int n, *ar, *br; cin >> n; ar = new int[n]; br = new int[n]; for (int i = 0; i < n; i++) { cin >> ar[i]; br[i] = ar[i]; } sort(br, br + n); int sum = 0; for (int i = n - 1; i >= 0; i--) { sum += br[i] * (n - i - 1) + 1; } cout ...
0
#include <bits/stdc++.h> using namespace std; bool isprime(long long n) { long long i; if (n == 2 || n == 3) return 1; long long flag = 0; for (i = 2; i <= sqrt(n) + 1; i++) { if (n % i == 0) { flag = 1; break; } } if (flag) return 0; else return 1; } int main() { ios_base::s...
4
#include <bits/stdc++.h> char s[1005]; int n, m, k, ans[23], G, R, has, oG, oR; int main(void) { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; ++i) { has = G = R = 0; scanf("%s", s + 1); for (int j = 1; j <= m; ++j) if (s[j] == '-') has = 1; else if (s[j] == 'G') G = j...
9
#include <bits/stdc++.h> using namespace std; unordered_map<long long, long long> mp; void mark_current(char a[][100], int i, int j, int r, int c) { if (i < 0 || j < 0 || i >= r || j >= c || a[i][j] != 'B') return; a[i][j] = '.'; mark_current(a, i, j - 1, r, c); mark_current(a, i, j + 1, r, c); mark_current(a...
2
#include <bits/stdc++.h> using namespace std; int map[2010][2010]; int map2[2010][2010]; int vis[2010][2010]; int minx, maxx, miny, maxy; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int n; int cnt; vector<pair<int, int> > pts; int que[4000100][2], qs, qe; inline void dfs(int x, int y) { qs = qe = 0; que[qe][0...
7
#include <bits/stdc++.h> using namespace std; int K, N; int O; void input() { scanf("%d%d", &N, &K); O = 0; for (int i = 0; i < N; i++) { int v; scanf("%d", &v); if (v % 2 == 1) O++; } } void even() { printf("Daenerys\n"); exit(0); } void odd() { printf("Stannis\n"); exit(0); } int main() { ...
7
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; vector<int> V[maxn]; int d[maxn]; int son[maxn]; long long ans = 0; void dfs(int u, int fa, int k) { son[u] = 1; d[u] = k; for (auto v : V[u]) { if (v == fa) continue; dfs(v, u, k ^ 1); son[u] += son[v]; } } int main() { int n...
6
#include <bits/stdc++.h> using namespace std; int a[111][111][11], n, q, c, x, y, rx, ry, t, v; int main() { cin >> n >> q >> c; for (int i = 1; i <= n; i++) { cin >> x >> y >> t; a[x][y][t]++; } for (int l = 0; l <= c; l++) { for (int i = 1; i <= 100; i++) for (int j = 1; j <= 100; j++) a[i][...
4
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1000 + 10; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int k; for (k = 0; k < s.size(); k++) if (s[k] != '0') break; int cnt = 0; for (int i = k; i < s.size()...
1
#include <bits/stdc++.h> using namespace std; int i, j, dp[2000][2000], n, k, m; int main() { dp[0][0] = 1; cin >> n >> m; for (int kk = 0; kk < n; kk++) { int x; scanf("%d", &x); for (i = 500; i >= 0; i--) for (j = 500; j >= 0; j--) if (dp[i][j]) dp[i + x][j] = dp[i][j + x] = 1; } f...
5
#include <bits/stdc++.h> using namespace std; int n, m; int e = 1e5; set<pair<int, int> > s1, s2, used1, used2; set<int> p1[200000], p2[200000], p3[200000], p4[200000]; int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy[8] = {1, -1, 1, 0, -1, 1, 0, -1}; void dfs1(int x, int y) { used1.insert(make_pair(x, y)); for (int...
8
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e6 + 20; const int INF = 0x3f3f3f3f; long long a[MAX_N]; long long diff[MAX_N]; int n, m; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); long long sum = 0; for (int i = 1; i <= n; i++) { diff[i] = a[i] - a[i ...
7
#include <bits/stdc++.h> using namespace std; int N, K; int ts[5000]; int ds[5000]; int m[5000][5000]; int main() { scanf("%d%d", &N, &K); for (int i = 0; i < (N); ++i) { scanf("%d%d", ts + i, ds + i); --ts[i]; } ts[N] = 86400; m[0][0] = 0; int best = ts[0]; for (int i = (1); i <= (N); ++i) { ...
5
#include <bits/stdc++.h> using namespace std; int n, m, i, j, k, x, y, z, fi, fr, p[555][555][2], v[555][555][2], qx[660000], qy[660000], qz[660000]; vector<int> g[555], r[2]; int main() { scanf("%d%d", &n, &m); for (i = 0; i < m; i++) { scanf("%d%d", &x, &y); g[x].push_back(y); g[y].push_back(x); ...
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; const int MAX = 2e5 + 6; char ara[MAX]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin ...
8
#include <bits/stdc++.h> int main() { long long t, i, x, y, a, b; scanf("%lld", &t); for (i = 1; i <= t; i++) { scanf("%lld%lld%lld%lld", &x, &y, &a, &b); if ((y - x) % (a + b) == 0) printf("%lld\n", (y - x) / (a + b)); else printf("-1\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; int v[maxn], n, m, ans; int main() { string s; int i, t, p; cin >> m >> s; if (s[0] != '-') s = "+" + s; for (i = 0; i < s.length(); i += 3) { p = s[i++] == '+' ? 1 : -1; for (t = 0; s[i] >= '0' && s[i] <= '9'; ++i) t = t * 10 + s[i...
6
#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 a, b, n; cin >> a >> b >> n; vector<int> v(3); v[0] = a; v[1] = b; v[2] = a ^ b; cout << v[n % 3] << '\n'; } return 0; ...
0
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<int> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; int res = 360; for (int i = 0; i < n; ++i) { int cur = 0; for (int j = 0; j < n; ++j) { cur += v[(i + j) % n]; res = min(res, abs(cur - (360 - cur...
2
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, a[N], suf[2][N], o, e, ans; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = n - 1; i >= 0; i--) { suf[(i % 2)][i] = a[i] + suf[(i % 2)][i + 1]; suf[...
2
#include <bits/stdc++.h> using namespace std; double t1, t2; struct node { double x, y; } c, h, s; double dist(double x1, double y1, double x2, double y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } double dist(node a, node b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)...
9
#include <bits/stdc++.h> using i64 = long long; const int MAXN = 100010; const double PI = 3.14159265358979323846; struct Pos { double x, y; Pos() = default; Pos(double x_, double y_) : x(x_), y(y_) {} }; int n, k; std::vector<Pos> arr; std::vector<double> pd; std::vector<double> pa; bool check(double d) { std:...
7
#include <bits/stdc++.h> using namespace std; int n, m; long long a[2][1001][2][1001], v; long long f(int x, int y) { long long r = 0; for (int i = y; i >= 1; i ^= i & -i) for (int j = x; j >= 1; j ^= j & -j) r ^= a[y & 1][i][x & 1][j]; return r; } void g(int x, int y) { for (int i = y; i <= n; i += i & -i)...
8
#include <bits/stdc++.h> using namespace std; int t, n, x, a[1010]; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> t; while (t--) { cin >> n >> x; if (n < 3) cout << "1\n"; else cout << ((n - 3) / x) + 2 << '\n'; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; long long A[N], n, p, k, ans; vector<long long> resp; long long fexp(long long x, long long y) { if (y == 0) return 1; else if (y % 2 == 0) return fexp((x * x) % p, y / 2) % p; else return (x * fexp((x * x) % p, y / 2)) % p; } int ma...
7
#include <bits/stdc++.h> using namespace std; const int N = 1000 + 10; int n, m, a; int w[N], b[N], fa[N]; int f[N]; vector<int> adj[N]; int find(int x) { if (fa[x] == x) return x; return fa[x] = find(fa[x]); } int main() { cin >> n >> m >> a; for (int i = 1; i <= n; i++) cin >> w[i], fa[i] = i; for (int i = ...
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int maxI = 0, index = 0, k = 0; cin >> n >> m; string s, t; cin >> s; cin >> t; vector<int> count(m - n + 1); for (int i = 0; i < m - n + 1; ++i) { count[i] = 0; for (int j = 0; j < n; ++j) { if (s[j] != t[i + j]) { ...
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, ans = 1000000007; cin >> n; int a[2][2 * n][2 * n], b[4][n][n], c[2 * n][2 * n]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2 * n; j++) { for (int k = 0; k < 2 * n;...
3
#include <bits/stdc++.h> using namespace std; const int N = 200005; vector<int> adj[N], rev[N]; int n, m, k, s, t; int p[N], dist[N]; bool vis[N]; void bfs(int u) { queue<pair<int, int> > q; q.emplace(u, 0); vis[u] = 1; while (!q.empty()) { u = q.front().first; int d = q.front().second; q.pop(); ...
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007LL; long long k; int n; int dx[] = {2, 2, -2, -2, 1, 1, -1, -1}; int dy[] = {1, -1, 1, -1, 2, -2, 2, -2}; long long arithm(long long a, long long b, long long n) { long long s1 = a + b; long long s2 = n; if (s1 % 2 == 0) s1 /= 2; ...
11
#include <bits/stdc++.h> using namespace std; int main() { int n, cnt = 0; cin >> n; int a[n], b; vector<int> v; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n; i++) { scanf("%d", &b); v.push_back(b); } int idx = 0; for (int i = 0; i < n; i++) { auto it = f...
3
#include <bits/stdc++.h> using namespace std; const int MOD(1000000007); const int INF((1 << 30) - 1); const int MAXN(105); int a[MAXN], add[MAXN]; int main() { int n; char c; int numadd = 1, k = 1; add[0] = 1; while (scanf(" %c", &c) == 1) { if (c == '=') { scanf("%d", &n); break; } else ...
5
#include <bits/stdc++.h> using namespace std; struct node { int u, v, len; } edge[100010]; unsigned long long dp[100010]; unsigned long long n; unsigned long long sum[100010]; vector<pair<int, int> > G[100010]; void dfs(int root, int fa) { sum[root] = 1; for (int i = 0; i < G[root].size(); i++) { int son = G[...
5
#include <bits/stdc++.h> using namespace std; long long L, A[200005], B[200005]; int N, D, Q, Ans, S[200005], F[200005], fa[200005], st[200005], bel[200005]; struct Lin { int v, c, next; } E[200005 * 2]; void Link(int u, int v, int c) { E[++D] = (Lin){v, c, st[u]}; st[u] = D; E[++D] = (Lin){u, c, st[v]}; st[v...
10
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int maxn = 1e5 + 10; string s; int main() { std::ios::sync_with_stdio(0); ; cin.tie(0); cout.tie(0); cin >> s; if (s.size() == 1) { return cout << 1 << "\n", 0; } bool af = 0; for (int i = 0; i < s.size() - 1; i++) ...
3
#include <bits/stdc++.h> using namespace std; const int c = 110; int main() { int n; scanf("%d", &n); int arr[c] = {0}; int matr[c][c]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { int t; scanf("%d", &matr[i][j]); if (i != j) arr[i] |= matr[i][j]; } for (int i = 0; i <...
3
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 2, maxm = 1e6 + 5; vector<int> seg[4 * maxm]; void add(int id, int L, int R, int idx, int val) { if (idx < L || idx >= R) return; if (R == L + 1) { seg[id].push_back(val); return; } int mid = (L + R) / 2; add(2 * id + 0, L, mid, idx,...
7
#include <bits/stdc++.h> using namespace std; int inline read() { int num = 0, neg = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') neg = -1; c = getchar(); } while (isdigit(c)) { num = (num << 3) + (num << 1) + c - '0'; c = getchar(); } return num * neg; } const int maxn = 100...
9
#include <bits/stdc++.h> using namespace std; int main() { int m, n, z, s; cin >> n; cin >> m; cin >> z; s = 0; for (int i = 1; i <= z; i++) if (i % n == 0) if (i % m == 0) s++; cout << s << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, w, h; pair<int, int> wyn[N]; vector<pair<pair<int, int>, pair<int, int>>> v[2 * N]; int main() { scanf("%d%d%d", &n, &w, &h); for (int i = 1; i <= n; ++i) { int g, p, t; scanf("%d%d%d", &g, &p, &t); v[p - t + N].push_back(make_p...
5
#include <bits/stdc++.h> using namespace std; vector<int> arr; vector<int> seg; long long ans = 0; long long expo(long long a, long long b) { if (b == 0) { return 1; } long long z = expo(a, b / 2); z *= z; z %= 1000000007; if (b % 2 != 0) { z *= a; z %= 1000000007; } return z; } void Merge(i...
5
#include <bits/stdc++.h> using namespace std; int cnt[205]; int n, m; int has[205], other[205], hsize, osize, sum; int main() { while (cin >> n >> m) { for (int i = 0; i < n; i++) { cin >> cnt[i]; } int ans = -1005; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { hsize ...
3
#include <bits/stdc++.h> using namespace std; const int inf = 987654321; const long long int INF = 123456789987654321; int N, T; vector<char> V; int last_home, home_cnt = 0, shop_cnt = 0; vector<int> shop_idx; int f(int x) { int last_shop = home_cnt - x - 1 < 0 ? -1 : shop_idx[home_cnt - x - 1]; int last_point = ma...
7
#include <bits/stdc++.h> using namespace std; int n, m, b[100005], c[100005]; vector<int> a[100005], v2, ar[100005]; vector<pair<int, int> > v[100005]; set<pair<int, int> > st; set<pair<int, int> >::iterator it; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { ar[i].resize(m); for (int j =...
7
#include <bits/stdc++.h> using namespace std; void boost() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long cubic_root(long long x) { long long l = 0, r = 2000005; while (l != r) { long long m = (l + r + 1) / 2; if (m * m * m > x) r = m - 1; else l = m; ...
4
#include <bits/stdc++.h> using namespace std; vector<pair<long long int, long long int> > ans; deque<long long int> dq; int main() { long long int n, q; scanf("%lld", &n); scanf("%lld", &q); long long int ar[n], mx = -1; for (long long int i = 0; i < n; i++) scanf("%lld", &ar[i]), mx = max(mx, ar[i]); f...
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; long long a, b, c; while (t--) { cin >> a >> b >> c; printf("%lld\n", a + b + c - 1); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 100010; const long long inf = 1ll << 60; int n, tot; int to[N << 1], nex[N << 1], head[N]; long long ans = 0, p[N], sum[N]; bool flag; void SE(int u, int v) { to[++tot] = v; nex[tot] = head[u]; head[u] = tot; return; } long long _GCD(long long x, long ...
6
#include <bits/stdc++.h> using namespace std; long long findpal(long long n) { long long m = n, m1 = 0, ctr = 0, rem; while (n != 0) { rem = n % 10; m1 = m1 * 10 + rem; ctr++; n /= 10; } m = m * pow(10, ctr) + m1; return m; } int main() { long long k, p, i, rev, sum = 0; cin >> k >> p; i...
2
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cer...
5
#include <bits/stdc++.h> using namespace std; const double ef = 0.000000001; double S, ans; struct node { double x, y; } p[305][1005], pp[305][1005], P[305][1005]; int q[1005], qq[1005], Last, A, n, k, i; bool v[1005], V[1005]; int cmp(node i, node j) { return i.x < j.x; } double work(double l, double r, double x) { ...
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int cnt[maxn]; int dp[maxn][3][3]; int main() { int n, m; scanf("%d%d", &n, &m); while (n--) { int x; scanf("%d", &x); cnt[x]++; } memset(dp, -1, sizeof(dp)); dp[0][0][0] = 0; for (int i = 1; i <= m; i++) { for (int a...
7
#include <bits/stdc++.h> using namespace std; double dist(int x1, int y1, int x2, int y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } int main() { int x, y, n, k; long double sum = 0; bool first = true; int lastX, lastY; cin >> n >> k; while (n--) { cin >> x >> y; if (first) { ...
0
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int INF = (int)1e9 + 1; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } void...
3
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; struct node { int to, val; }; int n, u, v, w; vector<node> vec[maxn]; int dp[maxn][2]; template <typename T> inline void read(T &x) { x = 0; int f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch =...
7
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, INF = N; int n, L, R, ANS_U, ANS_V, ANS; struct st { int nxt, to, w, dis; } ed[N << 1]; int head[N], num; void add(int from, int to, int w) { ed[++num].to = to, ed[num].w = w; ed[num].nxt = head[from]; head[from] = num; } int sum, rt, siz[N], ...
11
#include <bits/stdc++.h> using namespace std; const int maxn = 500; char mat[maxn][maxn]; int main() { int n; cin >> n; for (int i = 0; i < (n); i++) scanf("%s", mat[i]); char a = mat[0][0], b = mat[0][1]; int ok = 1; if (a == b) { cout << "NO" << endl; return 0; } for (int i = 0; i < (n); i++) ...
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 102; struct matrix { int arr[MAXN][MAXN]; int n, m; matrix(int x, int y) { n = x, m = y; }; matrix(int x, int y, int v) { n = x, m = y; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) arr[i][j] = v; }; matrix operator*(cons...
7
#include <bits/stdc++.h> using namespace std; const int N = 555; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; int n, k; bool G[N][N]; int mark[N][N]; int m = 0, size[N * N]; inline bool inside(int x, int y) { return x >= 0 && y >= 0 && x < n && y < n; } void floodfill(int x, int y, int m0) { size[m0]++; ...
8
#include <bits/stdc++.h> using namespace std; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(int64_t &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class ...
9
#include <bits/stdc++.h> using namespace std; const int N = 2005; int n, a[N], m, ans = 0; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { if (a[i] > a[j]) ++ans; ans = ans & 1; ...
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; cin >> TESTS; while (TESTS--) { long long n, i, j; cin >> n; long long a[n + 1]; for (i = 0; i < n; i++) cin >> a[i]; long long mx = a[0], l = 1, r = -1, f...
2
#include <bits/stdc++.h> int main() { int t, movesFirst{0}, movesSecond{0}; std::cin >> t; for (int i = 0; i != t; i++) { int number_of_piles, ones{0}; std::cin >> number_of_piles; int stones_on_piles[number_of_piles]; for (int j = 0; j < number_of_piles; j++) { std::cin >> stones_on_piles[j...
1
#include <bits/stdc++.h> using namespace std; const int N = 200005; const int mod = 1e9; int n, m, f[N], pre[N], a[N]; struct mat { int a[3][3]; mat() { memset(a, 0, sizeof(a)); } mat operator*(const mat& b) const { mat ans; int t; for (int i = (1), iend = (2); i <= iend; i++) for (int k = (1), ...
7