solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e3 + 10; int main() { int a, b; cin >> a >> b; int n = a - b; if (n < 0) { cout << 0; return 0; } if (n == 0) { cout << "infinity"; return 0; } vector<int> dvs; for (int i = 1; i * i <= n; i++) { if (n % i == 0) { ...
8
#include <bits/stdc++.h> using namespace std; string s0, s1, s2, s3; vector<vector<unsigned long long>> matrix(100001, vector<unsigned long long>(5, 0)); int q, x, a0, a1, a2, a3; unsigned long long k; void precompute() { matrix[0][4] = a0; for (int i = 1; i <= 100000; i++)...
9
#include <bits/stdc++.h> using namespace std; struct pt { long long x, y, idx; pt operator+(pt p) { return {x + p.x, y + p.y}; } pt operator-(pt p) { return {x - p.x, y - p.y}; } bool operator==(pt p) { return (idx == p.idx); } }; pt p[12333]; long double dis(pt a, pt b) { return sqrt((a.x - b.x) * (a.x - b.x...
10
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #define int long long using namespace std; void solve() { int n,m; string s,t; cin>>n>>m; cin>>s>>t; //try to maximise the difference ...
7
#include <bits/stdc++.h> using namespace std; const int N = 710; long long a[512345], mark[N + 9]; vector<pair<long long, int> > v[N + 9]; void build(int n) { for (int i = 0; i < n; i++) { v[i / N].push_back(make_pair(a[i], i)); } int R = (n - 1) / N; for (int i = 0; i <= R; i++) { sort(v[i].begin(), v[...
17
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 + 5; const int maxh = 10; short rg[maxn][maxn], dw[maxn][maxn], mm[maxn], d[maxn][maxn][maxh][maxh]; int a[maxn][maxn]; int rmq(int x1, int y1, int x2, int y2) { int k1 = mm[x2 - x1 + 1], k2 = mm[y2 - y1 + 1]; return max(max(d[x1][y1][k1][k2], d[x1...
19
#include <bits/stdc++.h> using namespace std; long long inv[100010], fac[100010], p[100010], t[100010][20]; int n, l, r, P, tot, Phi; int phi(int k) { int i, s; s = k; for (i = 2; i * i <= k; i++) { if (k % i == 0) s = s / i * (i - 1); while (k % i == 0) k /= i; } if (k > 1) s = s / k * (k - 1); ret...
21
#include <bits/stdc++.h> using namespace std; char S[100005], T[100005]; int N; int cnt(int x, int y) { int ret = 0; while (x < N && y < N) { if (S[x] != T[y]) { ret++; (x < y) ? x++ : y++; } else x++, y++; } while (x < N) ret++, x++; while (y < N) ret++, y++; if (ret <= 1) return ...
10
#include <bits/stdc++.h> using namespace std; const long long nInf = -1000000000; const long long pInf = 1000000000; const long long mod = 1000000007; int main() { int a[100][100]; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &a[i][j]); } } bo...
1
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 50; int a[maxn], b[maxn]; int n, m, k; vector<int> v; bool flag; bool cmp(const int &a, const int &b) { return a > b; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) scanf("%d", a + i); for (int i = 0; i < m; i++) scan...
8
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int R, C; cin >> R >> C; vector<string> fld(R); for (int i = (int)(0); i < (int)(R); ++i) cin >> fld[i]; vector<int> tate(C, 0), yoko(R, 0); int sum = 0; for (int i = (int)(0); i < (int)(R); ++i) fo...
6
#include <bits/stdc++.h> using namespace std; int32_t main() { long long t; cin >> t; while (t--) { long long n; cin >> n; vector<long long> a(n); long long sum = 0; for (long long i = 0; i < n; i++) { cin >> a[i]; sum = sum + a[i]; } cout << (ceil((double)sum / n) - floor(...
0
#include <bits/stdc++.h> using namespace std; int n, X, Y, a[505][505], u[505][505]; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; char cc[4] = {'R', 'D', 'L', 'U'}, anticc[4] = {'L', 'U', 'R', 'D'}; char ans[3001005]; int ansi; void fail() { puts("NO"); exit(0); } void dfs(int x, int y) { if (x <= 0 || x > n...
16
#include <bits/stdc++.h> using namespace std; const long long MIN_DIST = 0; const long long MAX_DIST = 1e18; const long long MAX_COORD = 1e18; ; const long long MIN_COORD = -1e18; bool InnerCheck(const vector<pair<long long, long long>>& x, const vector<pair<long long, long long>>& prefix, ...
21
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 10, M = 2e5 + 100, MOD = 1e9 + 7, ML = 25; long long a[M]; set<long long> st; map<long long, long long> mp; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, ans = 0; cin >> n; for (int i = 0; i < n; i++) { ...
9
#include <bits/stdc++.h> using namespace std; const int maxN = 1000 + 5; const int mod = 1000 * 1000 * 1000 + 7; int main() { ios::sync_with_stdio(0); cin.tie(); int n, x; cin >> n >> x; if (n == 5) { cout << ">...v\nv.<..\n..^..\n>....\n..^.<\n1 1\n"; return 0; } if (n == 3) { cout << ">vv\n^...
17
#include <bits/stdc++.h> using namespace std; const int N = (1 << 18), M = 20; int n, k, lim, ch[M], st[M], og[N][M]; long long ans[N], sum[N], g[M][N], p[M][N], dp[N][M]; bool fl[M][M]; char s[M]; inline void fwt(long long *s) { for (int d = 1; d < lim; d = d << 1) for (int i = 0, r = d << 1; i < lim; i += r) ...
18
#include <bits/stdc++.h> using namespace std; int n, k; int main(int argc, char const *argv[]) { cin >> n >> k; vector<long long> v; for (int i = 1; i <= k + 1; i++) { cout << "? "; vector<int> ask; for (int j = 1; j <= k + 1; j++) { if (j == i) continue; ask.push_back(j); } for (a...
11
#include <bits/stdc++.h> using namespace std; int dp[510], a[510], b[510], f[510]; int main() { int n, m; while (~scanf("%d", &n)) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); memset(dp, 0, sizeof(dp)); memset(f, 0, sizeof(f...
20
#include <bits/stdc++.h> using namespace std; int a[1002][1002]; int32_t main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n; cin >> n; int c = 0; for (int i = 0; i < n / 4; i++) { for (int k = 0; k < n; k++) { for (int j = 0; j < 4; j++) { a[k][4 * i + j] = c++; ...
10
#include <bits/stdc++.h> using namespace std; struct point { long long x, y; long long num = 0; point() { x = 0, y = 0; } point(long long a, long long b) { x = a, y = b; } }; struct Vector { long long x, y; Vector() { x = 0, y = 0; } Vector(point a, point b) { x = b.x - a.x; y = b.y - a.y; } }; ...
18
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t))...
11
#include <bits/stdc++.h> using namespace std; const int siz = 1e6 + 5; const int MOD = 1e9 + 7; void solve() { int n; cin >> n; int x; int pos[n + 1], a[n]; for (int i = 0; i < n; i++) { cin >> x; pos[x] = i + 1; a[i] = x; } int flag = 0, last = n; for (int i = 1; i < n; i++) { if (pos[i...
7
#include <bits/stdc++.h> using namespace std; char s[1100000], a[1100000], b[1100000]; int ss[1100000], aa[1100000], bb[1100000]; int p[27], used[27], alp[27]; int len; int k; int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &k); scanf("%s", s + 1); scanf("%s", a + 1); scanf("%s", b ...
15
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; const double eps = 1e-8; const int DIM = 110; int n; pair<double, double> p[DIM][4]; double ch, ans; double an[3]; vector<double> vec; double sqr(double q) { return q * q; } double dist(pair<double, double> A, pair<double, double> ...
15
#include <bits/stdc++.h> using namespace std; int n, ex, ey; int dx[200010], dy[200010]; string s; int abs(int num) { return num > 0 ? num : -num; } bool check(int data) { int need_x, need_y, all; for (int i = 1; i + data - 1 <= n; i++) { need_x = ex - (dx[n] - dx[i + data - 1]) - dx[i - 1]; need_y = ey - (...
10
#include <bits/stdc++.h> using namespace std; template <typename T> std::ostream& operator<<(std::ostream& str, const std::vector<T>& v) { str << "["; for (auto n : v) str << n << ", "; str << "]"; return str; } const int MOD = 1e9 + 7; int main() { cin.sync_with_stdio(false); cout.sync_with_stdio(false); ...
20
#include <bits/stdc++.h> using namespace std; long long int tree[1000009]; long long int child[1000009]; long long int rev[20], swa[20]; pair<long long int, long long int> children(long long int v, long long int d) { pair<long long int, long long int> res = {2 * v, (2 * v) + 1}; if (swa[d] ^ rev[d]) swap(res.first,...
16
#include <bits/stdc++.h> using lint = int64_t; constexpr int MOD = int(1e9) + 7; constexpr int INF = 0x3f3f3f3f; constexpr int NINF = 0xcfcfcfcf; constexpr lint LINF = 0x3f3f3f3f3f3f3f3f; const long double PI = acosl(-1.0); int cmp_double(double a, double b = 0, double eps = 1e-9) { return a + eps > b ? b + eps > a ?...
14
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); const int N = 1e5 + 7; vector<int> g[N]; int sz[N], lvl[N]; int cur_sz[N]; bool used[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; if (n < 3) { cout ...
10
#include <bits/stdc++.h> using namespace std; int n, r, p, s, k; int x[210], y[210], z[210], ax[9], ay[9], tx[9], ty[9]; void go(int m, int p) { if (p == n) { int u = 0; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) u += (tx[i] - tx[j]) * (tx[i] - tx[j]) + (ty[i] -...
19
#include <bits/stdc++.h> using namespace std; int len; char s[100005]; stack<int> st; char ans[100005]; int las[100005]; int main() { cin >> s; len = strlen(s); if (len == 1) { cout << s << endl; return 0; } int now = 1; st.push(s[0]); las[len - 1] = s[len - 1] - 'a'; for (int i = len - 2; i >= ...
9
#include <bits/stdc++.h> using namespace std; int n, m, ans; char s[400100]; int sum = 0, head[400100], a[400100][27]; struct Node { int to, next; } e[400100]; int d[400100]; void insert(int u, int v) { e[++sum].to = v; e[sum].next = head[u]; head[u] = sum; d[v]++; } stack<int> vec; void init() { cin >> n >...
9
#include <bits/stdc++.h> using namespace std; struct test { int test; int index; }; bool compareTests(test t1, test t2) { return t1.test < t2.test; } vector<test> tests, sortedTests; int total; void insertTest(test t) { total += t.test; tests.push_back(t); sortedTests.push_back(t); sort(sortedTests.begin(),...
4
#include <bits/stdc++.h> using namespace std; int n, m; long long pos[1000000 + 1000]; long long dis1[1000000 + 1000]; long long dis2[1000000 + 1000]; int cnt; int ans; int main() { scanf("%d %d", &n, &m); dis2[n - 1] = 0; for (int i = 0; i < n; i++) scanf("%lld", &pos[i]); for (int i = 1; i < n; i++) { if ...
12
#include <bits/stdc++.h> using namespace std; long long low[1000005], dp[1000005][20], sum[20] = {1}; int main() { long long i, j, r, n, q, ans, cnt, p; for (i = 2; i <= 1000000; i++) if (!low[i]) for (j = i; j <= 1000000; j += i) low[j] = i; for (i = 0; i <= 1000000; i++) dp[i][0] = 1; for (i = 1; i ...
17
#include <bits/stdc++.h> using namespace std; int n, pt, k, ans; struct in { int ind, val; } d[155]; struct compare { bool operator()(in a, in b) { return a.val < b.val; } }; int main() { scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &d[i].val); d[i].ind = i + 1; } sort(d, d + n,...
2
#include <bits/stdc++.h> using namespace std; int a, w, h, t, p; double x; int gcd(int u, int v) { return u % v == 0 ? v : gcd(v, u % v); } int main() { scanf("%d%d%d", &a, &h, &w); h += a, w += a, t = gcd(h, w); if (t < a || h < a + a || w < a + a) printf("-1"); else { p = t / a; x = t * 1.00000000...
19
#include <bits/stdc++.h> using namespace std; const int max_n = 55, inf = 111111111; int n, m, k, napr, dp[max_n][max_n][2], cnt[222]; string q; vector<int> v1, v2; int main() { scanf("%d%d%d\n", &n, &m, &k); getline(cin, q); if (q == "to head") { napr = 0; } else { napr = 1; } cin >> q; for (int ...
7
#include <bits/stdc++.h> using namespace std; vector<vector<int> > al; long long dp[100011][4]; int x[100011]; long long p = 1000 * 1000 * 1000 + 7; void DFS(int v) { dp[v][0] = 1; dp[v][1] = 0; for (int i = 0; i < al[v].size(); i++) { int u = al[v][i]; DFS(u); dp[v][1] = (dp[v][1] * dp[u][0]) % p; ...
12
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; string s[n]; for (int i = 0; i <= n - 1; i++) { cin >> s[i]; } int maxi = 0; for (int i = 0; i <= n - 1; i++) { int cnt = 1; for (int j = i + 1; j <= n - 1; j++) { ...
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<pair<int, int>> books(n + 1), books2(n + 1); vector<int> query(m); map<int, int> isMapExist, posX; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); books[i] = {i, x}; } int u = 0; for (int i = 0...
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 2; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int i, j, n, m, k; string str[1000]; long long int mark[200][200]; memset(mark, false, sizeof(mark)); vector<pair<long long int, pair<long long ...
9
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int N = (int)5e7; int T; int n, m; char a[505], b[505]; int pi[505], poz[505][2]; int16_t dp[505][505][505]; void precalc() {} void solve() { cin >> n >> m >> (a + 1) >> (b + 1); int p = 0; for (int i = 2; i <= m; i++) { while (...
14
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; const int MAX = 10000000; double eps = 1e-9; const long long BIG_INF = 7000000000000000000LL; const int INF = 2000000000; double PI = 3.1415926535897932384626433832795; template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b;...
2
#include <bits/stdc++.h> using namespace std; int main() { long long n, cnt[2], cnt1[2], Max; ; long long a[200000]; cnt[1] = 0; cnt[0] = 0; cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; cnt[a[i]]++; } cnt1[0] = cnt[0]; cnt1[1] = cnt[1]; for (int i = 1; i <= n; ++i) { cnt[a[i]]...
0
#include <bits/stdc++.h> using namespace std; vector<int> tu[200005]; int b[200005], a[200005], check[200005], cnt[200005]; int n; int gcd(int x, int y) { if (y == 0) return x; else return gcd(y, x % y); } void dfs(int son, int fa, int deep) { int i, flag; b[son] = gcd(a[son], b[fa]); check[son] = b[f...
12
#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...
13
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; string s1, s2; int t1[maxn], t2[maxn]; int main() { cin >> s1 >> s2; int l1 = (int)s1.size(), l2 = (int)s2.size(); for (int i = 0; i < l1; i++) { int n; scanf("%d", &n); t1[i] = n - 1; } int L = 0; int R = l1 - l2; int r...
9
#include <bits/stdc++.h> using namespace std; const long long int MOD = 1e9 + 7; long long int _gcd(long long int a, long long int b) { if (a == 0) return b; return _gcd(b % a, a); } long long int qpow(long long int a, long long int n) { long long int res = 1; while (n) { if (n % 2) { res = (res * a) ...
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 2000 + 5, P = 1000000007; int N, K, a[MAXN]; long long b[MAXN], cn[MAXN]; long long FastPow(long long a, int p, int m) { long long ret = 1; while (p) { if (p & 1) ret = ret * a % m; if (p >>= 1) a = a * a % m; } return ret; } long long comb(...
11
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "O3", "unroll-loops") using namespace std; const long long INF = (long long)(1e18); const long long inf = 1e18 + 7; long double eps = 1e-15; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a...
16
#include <bits/stdc++.h> using namespace std; char s[10005]; int n, len, ans; vector<int> g[26]; int main() { scanf("%s", s + 1); len = strlen(s + 1); for (int i = 1; i <= len; i++) g[s[i] - 'a'].push_back(i), s[i + len] = s[i]; for (int i = 0; i < 26; i++) { int l1 = g[i].size(); int an = 0; for (i...
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; vector<int> ry; for (int i = 1; i < 8; i++) { ry.push_back(i); } while (t--) { int a, b, c; cin >> a >> b >> c; int ans = 0; if (a) { a--; ans++; } ...
1
#include <bits/stdc++.h> using namespace std; inline void Read(int &x) { int f = 1; char c = getchar(); x = 0; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - '0'; c = getchar(); } x *= f; } const int p = 10...
19
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; const int MAXA = 8191; bool dp[MAXA + 1]; int n, a[MAXN], minv[MAXA + 1]; void solve() { cin >> n; for (int i = (1); i <= (n); ++i) cin >> a[i]; for (int i = (0); i <= (511); ++i) { minv[i] = 0x7f7f7f7f; } dp[0] = 1; minv[0] = -1; ...
10
#include <bits/stdc++.h> using namespace std; int maxArr[100100]; int difArr[100100]; int nextArr[100100]; int arrx[100100], arry[100100]; int n, m; void input() { int i, x, y; for (i = 0; i <= n; i++) { maxArr[i] = 0; } m = 0; for (i = 0; i < n; i++) { scanf("%d %d", &x, &y); if (maxArr[x] < y) {...
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e6 + 9; vector<long long> ca, cb; long long cnt(int l, int r) { if (l > r) return 0; return ca[r] - ca[l - 1]; } long long sum(int l, int r) { if (l > r) return 0; return cb[r] - cb[l - 1]; } int main() { ios_base::sync_with_stdio(0); int n, x,...
13
#include <bits/stdc++.h> using namespace std; int n, a[200100]; int le[200100], ri[200100]; int sta[200100], sz; int pre[200100][32]; long long ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) { while (sz > 0 && a[sta[sz]] < a[i]) --sz; le[i]...
14
#include <bits/stdc++.h> using namespace std; const char first[] = "input.txt"; const char fo[] = ""; int m, n, sl = 0; char s[2002][2002]; void input() { cin >> m >> n; for (int i = 0; i < m; ++i) cin >> s[i]; } vector<int> res; string pa = "^>v<#"; int bx[] = {-1, 0, 1, 0, -3}, by[] = {0, 1, 0, -1, -3}; pair<int,...
14
#include <bits/stdc++.h> using namespace std; const int N = 101000; const long long MOD = 1000000007; long long C[N][110]; long long a[N]; long long F[N][110]; long long d[110]; void add(long long &x, long long y) { x += y; while (x >= MOD) x -= MOD; while (x < 0) x += MOD; } int main() { for (int i = 0; i < N;...
17
#include <bits/stdc++.h> using namespace std; int n, m, a[200], b, kol, j, ost, vid; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &b); if (a[b - 1] == 0) { vid++; } a[b - 1]++; j++; } for (int i = 0; i < 101; i++) { for (int j = 0; j < 101; j++) { ...
1
#include <bits/stdc++.h> using namespace std; int dp[100010][101]; vector<int> adjList[100010]; queue<int> q[101]; int N, M, K, S; int main() { cin >> N >> M >> K >> S; for (int i = 0; i < N; i++) for (int j = 0; j < K; j++) dp[i][j] = N + 1; for (int i = 0; i < N; i++) { int x; cin >> x; x--; ...
8
#include <bits/stdc++.h> using namespace std; const int maxN = 5000; string s[5001]; int n, m, ans, t; int **l, **r, **u, **d; void build() { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (!i) u[i][j] = -1; else { u[i][j] = u[i - 1][j]; if (s[i - 1][j] != '.') u[...
15
#include <bits/stdc++.h> using namespace std; typedef struct var { string name; string type; } var; typedef struct fun { string name; vector<string> par; int count; } fun; int main() { int n; vector<fun> func; string tmp; cin >> n; getline(cin, tmp); for (int j = 0; j < n; j++) { fun f; ge...
10
#include <bits/stdc++.h> using namespace std; int d0[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; int d1[8][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}, {-1, 1}, {1, 1}, {1, -1}, {-1, -1}}; int n, m; string t[1001]; int ans = 0; int b1[1001][1001]; int id = 1; bool ok; set<pair<int, int>> lst; bool valid(int...
17
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:1048576") using namespace std; int IT_MAX = 1 << 18; const long long MOD = 1000000009; const int INF = 0x3f3f3f3f; const long long LL_INF = 1234567890123456789ll; const double PI = acos(-1); const double EPS = 1e-8; char u[10005]; ...
0
#include <bits/stdc++.h> using namespace std; int n, k, m, N; vector<vector<long long> > g; vector<long long> w, c; long long res = 0; void upd(long long& x, long long y) { if (x < y) x = y; } void add(vector<long long>& dp, int e) { if (!w[e]) return; for (int i = k; i >= w[e]; --i) upd(dp[i], dp[i - w[e]] + c[e...
20
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n; long long v; vector<pair<long long, long long> > A, ret; int main() { scanf("%d", &n); for (int i = 0, a, b; i < n; ++i) scanf("%d %d", &a, &b), ret.emplace_back(a, b); scanf("%lld", &v); for (auto &first : ret) A.emplace_back(f...
15
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y, x1, y1, ar[5], ar1[5]; cin >> n; if (n == 1) { cout << -1 << endl; return 0; } else if (n == 2) { cin >> x >> y >> x1 >> y1; if (x != x1 && y != y1) { cout << abs((x - x1) * (y - y1)) << endl; return 0; } }...
3
#include <bits/stdc++.h> using namespace std; const long long m = 1000000000 + 7; long long binom[4001][4001]; long long bell[4001]; int main() { int n; cin >> n; binom[0][0] = 1; for (int i = 1; i <= n; ++i) for (int j = 0; j <= i; ++j) binom[i][j] = (j == 0 || j == i) ? 1 ...
11
#include <bits/stdc++.h> using namespace std; const int N = 1015; int n, x, y, qwq1, qwq2; int zn[N], cnt; bitset<N> s[N], t[N], r; pair<int, int> Ans1[N], Ans2[N]; void get1(int u, int v) { if (u + 1 == v) return; r = s[u] & s[v]; for (int i = u; i <= v; i++) if (r[i]) { Ans1[++qwq1] = pair<int, int>(u...
21
#include <bits/stdc++.h> using namespace std; char a[110][110]; int n, m; int main() { register int i, j; while (cin >> n >> m) { char c; cin >> c; memset((a), (0), sizeof(a)); for (int(i) = 0; (i) < (n); (i)++) for (int(j) = 0; (j) < (m); (j)++) cin >> a[i][j]; bool visited[27] = {false};...
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int last[2]; char ch; int ctr = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> ch; if (ch == 'B') { ctr++; last[0] = i + 1; last[1] = j + 1; } } } ...
0
#include <bits/stdc++.h> using namespace std; int MAX = numeric_limits<int>::max(); const int N = 1e6 + 5; int main() { long long ans = 0, x; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &x); ans += ((x - 1) * (i) + x); } cout << ans << "\n"; }
3
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; bool p = true; for (int i = 0; i < s.size(); i++) { if (s[i] != s[s.size() - i - 1]) { p = false; break; } } if (p) { cout << 0 << endl; } else { cout << 3 << endl; cout << "L " << 2 << endl; ...
6
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[n], count[101] = {0}; for (int i = 0; i < n; i++) { cin >> a[i]; count[a[i]]++; } sort(count + 1, count + 101); int dish = (count[100] + k - 1) / k, num = 0; for (int i = 100; i > 0; i--) { if (count[i]...
1
#include <bits/stdc++.h> using namespace std; int f[555][555], deg[555][555], tot, rel[555]; unordered_map<int, int> mp; struct P { int x, y; } u, v; queue<P> q; void pre() { for (int i = 0; i <= 8; ++i) for (int ii = 0; i + ii <= 8; ++ii) for (int iii = 0; i + ii + iii <= 8; ++iii) for (int iv = ...
18
#include <bits/stdc++.h> using namespace std; string ch = ""; string chr = ""; string table[1000000]; string cht[1000000]; string cht2[1000000]; string mat[100][100]; long long int t[1000000]; long long int t2[1000000]; long long int t3[1000000]; long long int t4[1000000]; long long int n, m, k, s, s1, p, x, y, z; stri...
0
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; int Transform(char c) { if (c == 'A') return 0; if (c == 'T') return 9; if (c == 'J') return 10; if (c == 'Q') return 11; if (c == 'K') return 12; return c - '1'; } bool cmp(const pair<int, int> &a, const pair<int, int> &b) { retu...
8
#include <bits/stdc++.h> using namespace std; double a[100500]; double d[100500]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) scanf("%lf", &a[i]); double res = 0; for (int i = 0; i < n; i++) res += a[i]; d[0] = 0; for (int i = 1; i < n; i++) d[i] = (d[i - 1] + a[i - 1]) * a[i]; for (int i = ...
12
#include <bits/stdc++.h> using namespace std; int main() { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; if (abs(x1 - x2) == 0) cout << 4 + (abs(y1 - y2) + 1) * 2; else if (abs(y1 - y2) == 0) cout << 4 + (abs(x1 - x2) + 1) * 2; else cout << (abs(x1 - x2) + 1) * 2 + (abs(y1 - y2) + 1) * 2; ret...
3
#include <bits/stdc++.h> using namespace std; bool simple[1000001]; vector<int> q; int a[1000001]; vector<int> p; vector<int> gr[1000001]; int l[1000001], r[1000001]; bool was[1000001]; int w[1000001]; bool dfs(int v) { if (was[v]) return false; was[v] = true; for (int i = 0; i < int(gr[v].size()); i++) if (w...
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; long long a[n], dp1[n], dp2[n], ans; for (int i = 0; i < n; i++) { cin >> a[i]; } if (n == 1) { cout << a[0] << "\n"; return 0; } dp1[0] = a[0]; ...
13
#include <bits/stdc++.h> using namespace std; int main(){ int t; cin >> t; while (t -- ){ int n, k; cin >> n >> k; for (int i = 0; i < k - 3; i ++ ) printf("1 "); n -= k - 3; if (n & 1) printf("%d %d %d\n", 1, n / 2, n / 2); else if (n % 4) printf("%d %d %d\n", 2, n / 2 - 1, n / 2 - 1); el...
8
#include <bits/stdc++.h> using namespace std; int dis = 0; int mv = 0; int is_there(int n) { if (dis + 5 <= n) dis += 5; else if (dis + 4 <= n) dis += 4; else if (dis + 3 <= n) dis += 3; else if (dis + 2 <= n) dis += 2; else if (dis + 1 <= n) dis += 1; mv += 1; if (dis == n) return mv;...
0
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.length(); int dp[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dp[i][j] = 0; for (int i = 0; i < n; i++) dp[i][i] = 1; for (int i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) dp[i][i + 1...
11
#include <bits/stdc++.h> using namespace std; int main() { int n, k, j, a = -1, b = 1, i; cin >> n; k = n; for (int r = 1; r <= 1000; r++) { i = r; if ((i >= 1) && (i <= 9)) j = 1; if ((i >= 10) && (i <= 99)) j = 2; if ((i >= 100) && (i <= 999)) j = 3; if ((k - j) > 0) k = k - j; e...
0
#include <bits/stdc++.h> using namespace std; pair<int, int> dp[501][501]; int a[501], n; void solve(int tl, int tr) { if (tl == tr) { dp[tl][tr].first = 1; dp[tl][tr].second = a[tl]; return; } if (dp[tl][tr].first != 0) return; dp[tl][tr].first = n; for (int i = tl + 1; i <= tr; i++) { solve(...
13
#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(long long &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 <clas...
18
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res % mod; } long long gcd(long long a, long long b...
11
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 100500; char buf[10]; string scan_token() { scanf("%s", buf); return buf; } int n; string name[N]; bool sample[N]; int samples_cnt; string temp_file; set<string> all_names; vector<pair<string, string>> ops; vector<string> sample_holes...
14
#include <bits/stdc++.h> using namespace std; using ll = long long; template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return 0; } template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return 0; } void solve() { ll N; ...
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 101, INF = -1 * (1e+9); long long n, k, p, x; bool check(long long x) { if (k == -1) return 0; if (x == n + 1) return 1; if (k == 0) return 0; if (k <= n / 2) { if (x % 2 || x < n - (k - 1) * 2) return 0; return 1; } if (x % 2 == ...
11
#include <bits/stdc++.h> using namespace std; int pct(int x) { return __builtin_popcount(x); } int pct(long long x) { return __builtin_popcountll(x); } int bt(int x) { return 31 - __builtin_clz(x); } int bt(long long x) { return 63 - __builtin_clzll(x); } int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); ...
13
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const int inf = 1e9 + 7; const double eps = 1e-8; long long qpow(long long a, long long b, long long m) { long long r = 1; a %= m; for (; b; b >>= 1) { if (b & 1) r = r * a % m; a = a * a % m; } return (r + m) % m; } const ...
17
#include <bits/stdc++.h> using namespace std; const int N = 101; const long long int mod = 1e9 + 7; long long int dp[N][2]; int n, k, d; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> d; dp[0][0] = 1; dp[0][1] = 0; for (int v = 1; v <= n; v++) { for (int aresta = 1; aresta <= k; ares...
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; long long s = n - k - 1; long long r = s % k; s = s / k; if (r == 1) { cout << 2 + s + s + 1 << endl; } else if (r >= 2) { cout << 2 + s + 1 + s + 1 << endl; } else { cout << 2 + s + s << endl; } lo...
10
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; void JIZZ(string output = "") { cout << output; exit(0); } const long double PI = 3.14159265358979323846264338327950288; const long double eps = 1e-13; const long lo...
20
#include <bits/stdc++.h> using namespace std; template <typename T> string toString(T Number) { stringstream st; st << Number; return st.str(); } int stringconvert(string s) { int p; istringstream st(s); st >> p; return p; } long long bigmod(long long a, long long b, long long m) { long long res = 1; ...
4