solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int N = 3004; int n, m, poparcie[N], odw[N][N]; long long ans = LLONG_MAX; vector<pair<int, int> > G[N]; pair<int, pair<int, int> > T[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { int p, c; cin...
9
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, s, f; char a[15], m[15]; cin >> a; strcpy(m, a); f = 0; n = 1; int len = strlen(a); for (i = 0; i < len; i++) if (a[i] != a[len - 1 - i]) { n = 0; break; } if (n && len % 2 == 0) { for (i = 0; i < len / 2; ...
22
#include <bits/stdc++.h> using namespace std; struct Graph { unordered_map<int, unordered_set<int>> list; void add_edge(int u, int v) { list[u].insert(v); list[v].insert(u); } }; int dfs(Graph& g, int u, unordered_set<int>& visited, vector<int>& succ) { for (int v : g.list[u]) { if (visited.count(v)...
8
#include <bits/stdc++.h> using namespace std; int main() { int i, n, j, k, l, m, t, c = 0, ans; scanf("%d", &n); char s[20]; char s1[20][20]; while (n--) { scanf("%s", s); l = strlen(s); vector<pair<int, int> > v; for (i = 1; i <= 12; i++) { if (12 % i == 0) { j = 12 / i; ...
2
#include <bits/stdc++.h> using namespace std; struct UF { vector<int> e; UF(int n) : e(n, -1) {} int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); } bool join(int a, int b) { a = find(a), b = find(b); if (a == b) return false; if (e[a] > e[b]) swap(a, b); e[a] += e[b]; e[b] = a; ...
16
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; int n; long long x[maxn]; long long ans = 0x7FFFFFFFFFFFFFFF, cnt = 0; long long absl(long long a) { if (a < 0) return 0 - a; return a; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> x[i]; } sort(x, x + n); for (int ...
3
#include <bits/stdc++.h> using namespace std; int main() { long long n, x, y, x_mid, y_mid; cin >> n; vector<pair<int, int> > v; for (int i = (0); i < (n); i++) { cin >> x >> y; v.push_back(make_pair(x, y)); } if (n % 2) { cout << "NO" << endl; return 0; } else { x_mid = v[0].first + v...
10
#include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; long double a[300000], t[300000]; bool check(long long i, long long j) { return (abs(t[i]) < abs(t[j])); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, tt; cin >> n >> tt; for (long long i ...
12
#include <bits/stdc++.h> using namespace std; const __int128 maxn = 2e5 + 100; const __int128 mod = 1e9 + 7; const __int128 inf = 1e18; long long s; __int128 n, k, x, a[maxn]; vector<__int128> c[maxn]; bool val(__int128 t) { x = 0; x += a[0]; x += (t + 1) * a[1]; if (n > 2 && t < 1e10) x += (t + 1) * (t + 2...
16
#include <bits/stdc++.h> using namespace std; inline long long int modPow(long long int base, long long int exp, long long int modulus) { base %= modulus; long long int result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % modulus; base = (base * base) % modulus...
7
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx,avx2,fma,sse4") namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; inline void...
12
#include <bits/stdc++.h> using namespace std; bool prime(long long int n) { for (int d = 2; d <= sqrt(n); d++) if (n % d == 0) return false; return true; } long long int putere(long long int x, long long int y, long long int mod) { int put = 1; while (y) { if (y % 2) put = (put * x) % mod; x = (x * ...
15
#include <bits/stdc++.h> using namespace std; template <class T> void read(T& x) { T res = 0, f = 1; char num = getchar(); while (!isdigit(num)) { if (num == '-') f = -1; num = getchar(); } while (isdigit(num)) { res = (res << 3) + (res << 1) + num - '0'; num = getchar(); } x = res * f; } ...
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 5005; int main() { int n; int a[MAXN]; int vis[MAXN]; int sum[MAXN]; while (~scanf("%d", &n)) { memset(sum, 0, sizeof(sum)); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= n; i++) { memset(vis...
7
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { for (; b; a %= b, swap(a, b)) ; return a; } const int INF = 987654321; int n, m; string a, b; int dp[5001][5001]; int solve(int ai, int bi) { if (ai >= n || bi >= m) return 0; int& ret = dp[ai][bi]; if (ret != -INF) re...
10
#include <bits/stdc++.h> using namespace std; int main() { string s, t; int n, a = 0, b = 0, f = 0; cin >> n >> s; for (int i = 0; i <= n; i++) if (s[i] == '_' || s[i] == '(' || s[i] == ')' || i == n) { if (!f) a = max(a, (int)t.length()); else if (t != "") b++; if (i < n) ...
3
#include <bits/stdc++.h> using namespace std; int main() { string second; cin >> second; long long int len = second.length(); if (len % 2 == 0) { long long int coun = 0; for (long long int i = 0; i < (len); ++i) if (second.at(i) == second.at(0)) coun++; if (coun == len) cout << "Impossib...
10
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long arr[n]; long long brr[n]; long long ans1 = 0, ans2 = 0, finans = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < n; i++) { cin >> brr[i]; } for (int i = 0; i < n; i++) { ans1...
1
#include <bits/stdc++.h> using namespace std; int main() { int n, i, d; scanf("%d", &n); if (n == 1) { printf("1"); return 0; } if (n == 2) { printf("1 2"); return 0; } if (n == 3) { printf("1 1 3"); return 0; } d = 1; while (n > 3) { for (i = 1; i <= n; i += 2) printf("%...
8
#include <bits/stdc++.h> using namespace std; const int MaxN = 1e5 + 1; int n, q, x, y, tree[60][MaxN << 2]; void Modify(int x, int l, int r, int p, int v) { if (l > r || l > p || r < p) return; if (l == r) { for (int i = 0; i <= 59; i++) tree[i][x] = (i % v) ? 1 : 2; return; } int mid = (l + r) >> 1; ...
16
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; if (s[s.length() - 1] == 'o') cout << "FILIPINO"; else if (s[s.length() - 1] == 'a') cout << "KOREAN"; else if (s[s.length() - 1] == 'u') cout << "JAPANESE"; if (...
0
#include <bits/stdc++.h> using namespace std; vector<vector<int> > data; int ara[3005], is[3005], total, par[3005], n; int root(int v) { return par[v] < 0 ? v : (par[v] = root(par[v])); } void union_set(int x, int y) { if ((x = root(x)) == (y = root(y))) return; if (par[y] < par[x]) swap(x, y); par[x] += par[y]; ...
13
#include <bits/stdc++.h> using namespace std; vector<long long> v; void dfs(long long num, int len) { if (len == 10) { return; } v.push_back(num * 10 + 4); v.push_back(num * 10 + 7); dfs(num * 10 + 4, len + 1); dfs(num * 10 + 7, len + 1); } int main() { ios_base::sync_with_stdio(false); long long l,...
3
#include <bits/stdc++.h> using namespace std; void solve() {} int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, a, b; cin >> n >> a >> b; n--; int ans = min(n - a, b) + 1; cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, b; cin >> n; b = (n - 4) % 6; if (n < 3) { cout << -1; } else if (n == 3) { cout << 210; } else { if (b == 0) { cout << 1; for (int i = 0; i < n - 4; i++) { cout << 0; } cout << "050"; } if ...
6
#include <bits/stdc++.h> using namespace std; int m[28][28]; char s[103]; int maxi, maxj; int main() { int n; scanf("%d", &n); getchar(); scanf("%c", &s[0]); for (int i = 1; i < n; i++) { scanf("%c", &s[i]); ++m[(int)s[i - 1] - (int)'A'][(int)s[i] - (int)'A']; } for (int i = 0; i < 28; i++) fo...
1
#include <bits/stdc++.h> using namespace std; const long long N = 2e5; vector<long long> v; void ck(long long x) { v.clear(); while (x) { long long r = x % 16; v.push_back(r); x /= 16; } reverse(v.begin(), v.end()); } long long dp[12 + 2][(1 << 16) + 2][2][2]; long long dfs(long long i, long long ms...
17
#include <bits/stdc++.h> using namespace std; const long long INF = 5000000000000LL; int s[5000], e[5000], x[2 * 5000 + 1]; long long dp[2][2 * 5000 + 1]; int main() { ios::sync_with_stdio(0); int n, start; cin >> n >> start; x[0] = start; for (int i = 0; i < n; ++i) { cin >> s[i] >> e[i]; x[2 * i + 1...
13
#include <bits/stdc++.h> using namespace std; struct Point { int x; int y; }; bool onSegment(Point p, Point q, Point r) { if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) && q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y)) return true; return false; } int orientation(Point p, Point q, Point r) { int va...
8
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const int base = 29; struct BIT { int cnt[maxn]; void Init(void) { fill(begin(cnt), end(cnt), 0); } void upd(int i, int v) { for (; i; i -= i & -i) cnt[i] += v; } int sum(int i) { int res = 0; for (; i <= maxn - 5; i += i & -i...
13
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const long double eps = 1e-11; const long double pi = acos(-1); const int oo = 1 << 30; const long lon...
14
#include <bits/stdc++.h> using namespace ::std; int n, m; int a[105]; int pile[105]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); for (int i = 0; i < n; i++) { int j; for (j = m - 1; j >= 0; j--) if (pile[j] <= a[i]) break; if (j == -1) pi...
6
#include <bits/stdc++.h> using namespace std; int Par[300]; bool Used[300]; int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { int p; scanf("%d", &p); if (!Used[p]) { int Min = max(0, p - k + 1); int Last = p; int New; while (!Used[Last] && Last > Min...
9
#include <bits/stdc++.h> using namespace std; const int N = 5E5 + 5, MX = 1E6 + 5, INF = 1E9 + 7; int n, q, u, v, a[N]; struct node { pair<int, int> mi; bool on; node(pair<int, int> _mi = {0, 1}, bool _on = false) : mi(_mi), on(_on) {} node operator+(const node &oth) const { pair<int, int> ans = {INF, 0}; ...
25
#include <bits/stdc++.h> using namespace std; const int maxn = 66, maxm = 111111, mod = 1000000007; inline long long read() { char ch = getchar(); long long x = 0, f = 0; while (ch < '0' || ch > '9') f |= ch == '-', ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return f...
23
#include <bits/stdc++.h> using namespace std; int n, m, arr[100][2], dp[201], suma; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d %d", &arr[i][0], &arr[i][1]); suma += arr[i][0]; } for (int i = 0; i < suma; i++) dp[i] = 1000000000; for (int i = 0; i < n; i++) for (int j = a...
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long n, a = 0, b = 0, i = 0; string s, t; cin >> s >> t; n = s.length(); map<char, long long> m, m1; for (i = 0; i < t.size(); i++) ++m[t[i]]; for (i = 0; i < n; i++) { ++m1[s[i]]; ...
6
#include <bits/stdc++.h> using namespace std; const int maxN = 100 * 1000 + 5; int n, res; int x[maxN], y[maxN]; struct node { int x, y; node(int x = -1, int y = -1) : x(x), y(y) {} } a[maxN], b[maxN]; inline bool cmpX(const node &a1, const node &a2) { return (a1.x != a2.x) ? a1.x < a2.x : a1.y < a2.y; } inline b...
12
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, s, p, q; map<int, int> bm, sm; char a; cin >> n >> s; for (int i = 0; i < n; i++) { cin >> a >> p >> q; if (a == 'B') { bm[p] += q; } else { sm[p] += q; } } int c = 0; i...
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; int pre = -1, u; cin >> n >> u; int a[n], k = 1, r = -1; a[0] = 1; pre = u; for (int i = 1; i < n; ++i) { int j; cin >> j; if (j == pre) a[i] = a[i - 1], r = i; ...
10
#include <bits/stdc++.h> using namespace std; long long int a[200001], b[200001]; long long int smallest_prime[2000001]; void seive() { long long int i, j, k, l, m, n, o, p; for (i = 1; i <= 2000000; i++) { smallest_prime[i] = 1; } smallest_prime[0] = smallest_prime[1] = 1; for (i = 2; i <= 2000000; i++) ...
7
#include <bits/stdc++.h> using namespace std; struct debugger { template <typename T> debugger& operator,(const T& v) { cerr << v << " "; return *this; } } dbg; int dx[] = {0, 0, -1, 1}; int dy[] = {1, -1, 0, 0}; string graph[2000]; int r, c, enx, eny, stx, sty; int level[2000][2000], vis[2000][2000]; voi...
7
#include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.fi...
16
#include <bits/stdc++.h> using namespace std; map<int, int> mp2; map<int, int> mp1; map<int, int> mp; int n, B, C; int main() { cin >> n; int a[n], b[n - 1], c[n - 2]; for (int i = 0; i < n; i++) { cin >> a[i]; mp[a[i]]++; } for (int i = 0; i < n - 1; i++) { cin >> b[i]; mp1[b[i]]++; } for...
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string s; cin >> s; int lo = 0, hi = 3 * n; auto works = [&](int t) { int togo = -1, seen = n + 1; for (int i = 0; i < (n); ++i) { if (s[i] == '*' && i > togo) seen = min(s...
10
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; if(n%2==0) cout<<(n/2+1)*(n/2+1)<<endl; else cout<<2*(n/2+1)*(n/2+2)<<endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long n, p = 0; cin >> n; string s; cin >> s; bool ok = true; for (long long i = 0; i < s.length(); i++) { if (s[i] == '(') p++; else p--; if (p < -1) ok = false; } if (p != 0) ok = false; (ok) ? cout << "Yes" : cou...
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a, fl; fl = 0; for (int i = 0; i < n; i++) { cin >> a; if (a == 1) fl = 1; } if (fl == 1) cout << "-1" << endl; else cout << "1" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MX = 200020; const int N = 1000060; const int mod = (int)1e9 + 7; int bc[1111][1111]; int main() { cin.tie(0); cin.sync_with_stdio(0); int n, k; cin >> n >> k; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a, a + n); for (int i ...
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int F[N]; string s, t; int n, m; vector<vector<int>> dp; int memo[26][N]; void init() { int i = 0, j = -1; F[0] = -1; while (i < m) { while (j >= 0 && t[i] != t[j]) j = F[j]; i++, j++; F[i] = j; } } int solve(int x, int y) { if (...
15
#include <bits/stdc++.h> using namespace std; int tab[200007]; int main() { int q, i, j, n, k, ln; scanf("%d", &q); for (j = 0; j < q; j++) { ln = 0; scanf("%d %d", &n, &k); for (i = 0; i < n; i++) { scanf("%d", &tab[ln]); if (tab[ln] % 2 == 1) { tab[ln] = i + 1; ln++; ...
4
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MX = 2004; char s[MX][MX]; int d1[MX][MX], d2[MX][MX]; char ans[MX * 2]; int xi[] = {0, 1}; int yi[] = {1, 0}; struct node { int x, y; node() {} node(int x, int y) : x(x), y(y) {} }; void bfs(int n, int m) { queue<node> que; q...
11
#include <bits/stdc++.h> using namespace std; const int N = 10000; const long double pi = acos(-1.0); int n, f[200200], minf[200200], g[200200], o[200200]; int sum[200200]; pair<int, int> a[200200]; int read() { int x = 0, f = 1; char ch = getchar(); while ((ch < '0') || (ch > '9')) { if (ch == '-') f = -1; ...
15
#include <bits/stdc++.h> using namespace std; inline 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 = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int MAXN = 300010...
7
#include <bits/stdc++.h> using namespace std; long long poww(long long a, long long b, long long md) { return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md)); } const long long MAXN = 5000 + 10; const long long INF = 1e9; const long long ...
18
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int mxN = 200010; long long val; long long mxTree[mxN << 2], mnTree[mxN << 2], sumTree[mxN << 2]; bool lz[mxN << 2]; int N; void push(int n, int l, int r) { if (l == r || !lz[n]) return; int md = (l + r) >> 1; mxTree[n << 1] = m...
20
#include <bits/stdc++.h> using namespace std; long long bgm(long long a, long long b, long long m) { a %= m; long long rem = 1; while (b) { if (b & 1) rem = (rem * a) % m; a = (a * a) % m; b >>= 1; } return rem; } long long in() { long long a; if (scanf("%lld", &a) == EOF) return EOF; return...
8
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); int main() { int n; cin >> n; string w; cin >> w; int numChars[26]; memset(numChars, 0, 26 * sizeof(int)); int mx = 0; for (int j = 0; j < n; j++) { for (int i = j; i < n; i++) { numChars[w[i] - 'a']++; mx = max(...
2
#include <bits/stdc++.h> using namespace std; long n, m; char s[600][600]; long f[600][30][30] = {0}, nho1[600][30][30] = {0}, nho2[600][30][30] = {0}; int main() { cin >> n >> m; scanf("\n"); for (int i = 1; i <= n; i = i + 1) { scanf("%s\n", &s[i]); } for (int k = 1; k <= n; k = k + 1) { for (int i ...
12
#include <bits/stdc++.h> using namespace std; int n, m, ans = 1e9; int a[1001][1001]; int t[1001]; int di[4] = {-1, 0, 1, 0}; int dj[4] = {0, -1, 0, 1}; int d[4][1001][1001]; char c; deque<pair<int, int>> q; bool valid(int i, int j, int s) { return i >= 1 && i <= n && j >= 1 && j <= m && d[s][i][j] == -1 && ...
14
#include <bits/stdc++.h> using namespace std; inline int inp() { return 0; } inline int inp(bool& A) { if (cin >> A) return 1; return -1; } inline int inp(char& A) { return scanf(" %c", &A); } inline int inp(int& A) { return scanf("%d", &A); } inline int inp(float& A) { if (cin >> A) return 1; return -1; } inli...
18
#include <bits/stdc++.h> using namespace std; const int N = 4e5 + 99; int n, m, q, x, y, ans = 1, Fen[2][N]; void update(int x, int val, int s) { if (s) x = n + 1 - x, val = m + 1 - val; x++; while (x <= N) { Fen[s][x] = min(Fen[s][x], val); ; x += (x & -x); } } int Get(int x, int s) { if (s) x = ...
19
#include <bits/stdc++.h> int read() { register int x = 0; register char ch = getchar(), f = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = !f; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0'); return f ? x : -x; } const int d[8][2] = {{-1, -2}, {-1, 2}, {-2, -1}, {-2, 1...
21
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int p, n, t, i, a[101], maxd = 1; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); cin >> t; for (i = 0; i < n; i++) { if (a[i] + t >= a[n - 1]) { if (n - i > m...
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int TC; cin >> TC; while (TC--) { int n, m, k; cin >> n >> m >> k; int l = n * m / 2 - k; if (!((k % 2 == n % 2 && n % 2 == 0 && (k + n - 1) / n * 2 < m) || ...
13
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(); int tot = 0; for (int i = 0; i < n; i++) { if (s[i] != 'Q') continue; for (int j = i + 1; j < n; j++) { if (s[j] != 'A') continue; for (int k = j + 1; k < n; k++) { if (s[k] != 'Q') con...
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 * 1000 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int q; cin >> q; string meh = "RGB"; while (q--) { int n, k; string cad; cin >> n >> k >> cad; int ans = n; for (int ini = 0; ini + k <= n; ++ini) { ...
7
#include <bits/stdc++.h> using namespace std; template <class T> inline T modinv(T a, T n) { T i = n, v = 0, d = 1; while (a > 0) { T t = i / a, x = a; a = i % x; i = x; x = d; d = v - t * x; v = x; } return (v + n) % n; } long long modpow(long long n, long long k, long long mod) { lon...
2
#include <bits/stdc++.h> using namespace std; int r[1000010]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { char s; long long a; cin >> s >> a; int sum = 0, t = 1; while (a > 0) { sum = sum + (a % 2) * t; a /= 10; t *= 2; } if (s == '+') r[sum]++; if...
6
#include <bits/stdc++.h> const int N = 1010; const int M = 1010; double lf[N * M]; void init() { lf[0] = lf[1] = 0; for (int i = 2; i < N * M; i++) lf[i] = lf[i - 1] + log(i); } int n, m; double lC(int n, int m) { return lf[n] - lf[m] - lf[n - m]; } int main() { init(); while (~scanf("%d %d", &n, &m)) { dou...
13
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9 + 1; struct node { int mx1, mx2, cntmx1; long long sum; node() { mx1 = inf, mx2 = -1, cntmx1 = 1, sum = inf; } friend node combine(const node &l, const node &r) { node res; res.mx1 = max(l.mx1, r.mx1); if (l.mx1 > r.mx1) re...
23
#include <bits/stdc++.h> using namespace std; signed main() { long long t; cin >> t; while (t--) { long long n; string str; cin >> n >> str; vector<long long> a(n), pref(n + 1, 0); map<long long, long long> m; for (long long i = 0; i < n; i++) { a[i] = str[i] - '0'; pref[i + 1]...
8
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(NULL); ios::sync_with_stdio(false); int T; cin >> T; while (T--) { bool flag = false; int n; vector<int> v; cin >> n; v.resize(n); for (int i = 0; i < n; i++) { cin >> v[i]; } for (int i = 0; i < n - 1; i+...
4
#include <bits/stdc++.h> using namespace std; template <class T> inline T __cin() { T ret; cin >> ret; return ret; } int main() { ios_base::sync_with_stdio(0); int n; string inp; cin >> n >> inp; string a = inp.substr(0, n); string b = inp.substr(n, n); sort(a.begin(), a.end()); sort(b.begin(), b....
3
#include <bits/stdc++.h> using namespace std; int main() { long long int q; cin >> q; long long int n, m, k, z, z1; for (int i = 0; i < q; i++) { cin >> n >> m >> k; if ((n + m) % 2 == 1) { k--; m--; z1 = max(n, m); if (z1 > k) { z1 = -1; } else { z1 += ((k ...
8
#include <bits/stdc++.h> using namespace std; string a[110]; int dp[2][110], c[2][110]; int main() { int N, M, ans = 0; cin >> N >> M; for (int i = N; i > 0; i--) { cin >> a[i]; } for (int f = 1; f <= N; f++) { for (int i = 0; i < M + 1 && a[f][i] == '0'; i++) c[0][f]++; for (int i = M + 1; i > 0 ...
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { string p, h; cin >> p >> h; if (h.size() < p.size()) { cout << "NO\n"; continue; } vector<int> cnt_p(26, 0); for (int i = 0; i < p....
2
#include <bits/stdc++.h> using namespace std; int isValid(int i, int j, int n) { if (i < 0 || j < 0 || i >= n || j >= n) { return false; } return true; } int main() { int n; cin >> n; if (n <= 2) { cout << 0 << endl; } else { char arr[1000][1000]; for (int i = 0; i < n; i++) { for (i...
0
#include <bits/stdc++.h> using namespace std; const int Maxn = 400 + 7; int t, h, r, n, m; long long dis[Maxn][Maxn], dp[Maxn][Maxn]; pair<int, int> ta[Maxn], he[Maxn]; bool mark[Maxn][Maxn], dor, ivan, vis[Maxn][Maxn]; queue<pair<int, int> > que; long long dfs(int v1, int v2) { vis[v1][v2] = true; mark[v1][v2] = t...
13
#include <bits/stdc++.h> using namespace std; char a[100005]; char b[100005]; long long c[100005]; long long d[100005]; long long ans1[100005]; long long ans2[100005]; long long now = 0; void F2(long long n); void F1(long long n) { if (a[n] == '0' || a[n + 1] == '0') F2(n + 1); if (now >= 100000) return; a[n]--; ...
19
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; for (long long i = 2; i * i <= n; i++) { while (n % (i * i) == 0) { n = n / i; } } cout << n; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int inf = 1e9; const int mod = 1e9 + 7; int l[maxn]; int r[maxn]; int main() { int n, p; cin >> n >> p; long double ans = 0.0; for (int i = 0; i < n; i++) cin >> l[i] >> r[i]; for (int i = 0; i < n; i++) { int j = (i + 1) % n; ...
9
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int maxn = 500000 + 5; const int INF = 0x3f3f3f3f; const int P = 1000000007; const double PI = acos(-1.0); template <typename T> inline T read(T& x) { x = 0; int _f = 0; char ch = getchar(); while (ch < '0'...
9
#include <bits/stdc++.h> using namespace std; const int max_n = 100000; int n; vector<int> neigh[max_n]; int val[max_n]; int c; int s[max_n], s_ed; int b[2 * max_n], b_ed; void run_dfs(int v) { s[++s_ed] = v; val[v] = s_ed; b[++b_ed] = val[v]; for (int j = 0; j < int(neigh[v].size()); j++) { int w = neigh[v...
14
#include <bits/stdc++.h> using namespace std; int main() { long double n, t; cin >> n >> t; cout << n * pow(1.000000011, t); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 100005; struct edge { int x, y, l, Num; void fill(int _x, int _y, int _l) { x = _x, y = _y, l = _l; } bool friend operator<(const edge A, const edge B) { return A.l > B.l; } } E[N], q[N]; int v[N], Res[N]; int n, Q; struct segment { int l, r, mid, len,...
23
#include <bits/stdc++.h> using namespace std; void PV(vector<long long> v) { for (long long i = 0; i < (long long)v.size(); i++) cout << v[i] << " "; cout << "\n"; } void PVV(vector<pair<long long, long long> > v) { for (long long i = 0; i < (long long)v.size(); i++) cout << v[i].first << " " << v[i].second <...
9
#include <bits/stdc++.h> using namespace std; map<char, int> mp; int main() { string s; int l, i; char ch; cin >> l; cin >> s; for (i = 0; i < l; i++) { s[i] = tolower(s[i]); mp[s[i]]++; } for (ch = 'a'; ch <= 'z'; ch++) { if (mp[ch] == 0) { puts("NO"); return 0; } } puts...
0
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; t = 1; while (t--) { int n; cin >> n; string s, t; cin >> s >> t; unordered_map<char, int> mp; if (s.length() != t.length()) { cout << -1 << "\n"; ...
4
#include <bits/stdc++.h> using namespace std; vector<int> aray[100001]; vector<int> euler; int dist[100001]; void dfs(int root, int count) { dist[root] = count; euler.push_back(root); for (int i = 0; i < aray[root].size(); i++) { int val = aray[root][i]; if (dist[val] == -1) { dfs(val, count + 1); ...
12
#include <bits/stdc++.h> using namespace std; long long dp[4005][105]; int n; long long f(long lett, long len) { int i, sum = 0; if (lett < 0 || len < 0) return 0; if (lett == 0 && len == 0) return 1; if (dp[lett][len] >= 0) return dp[lett][len]; for (i = 1; i <= 26; i++) { sum += f(lett - i, len - 1); ...
12
#include <bits/stdc++.h> const int mod = 1e9 + 7; const int N = 1e6 + 5; inline int max(int a, int b) { return a > b ? a : b; } int n, m, a, b, fac[N], inv[N], pw_n[N], pw_m[N], ans; inline int combn(int n, int m) { if (m < 0 || m > n) return 0; return (long long)fac[n] * inv[m] % mod * inv[n - m] % mod; } int main...
16
#include <bits/stdc++.h> using namespace std; const int N = 100010; int t[5], b[5], f[5][N][18], m, n, k; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) scanf("%d", &f[j][i][0]); } int len = int(log(n) / log(2)); for (int j = 0; j < m; j++) for (int...
12
#include <bits/stdc++.h> using namespace std; int n; set<long long> s; long long a[201000], ans; void solve(long long x) { long long res = 0; for (int i = 0; i < n; i++) { if (a[i] < x) res += x - a[i]; else res += min(a[i] % x, x - a[i] % x); } ans = min(ans, res); } void add(long long x) {...
17
#include <bits/stdc++.h> using namespace std; typedef long double ldb; const int maxn = 1 << 21; const int N = int(1e5); const long long mod = int(1e9) + 7; const long long inf = 1e18; const double PI = 3.1415926536; inline int Set(int n, int pos) { return n = n | 1 << pos; } inline bool check(int n, int pos) { return ...
4
#include <bits/stdc++.h> using namespace std; int k, a, b; vector<int> ans; int dp[201][201]; bool solve(int l, int kp, vector<int> v) { if (dp[l][kp] != -1) return dp[l][kp]; if (kp == k && l == 0) { ans = v; return true; } if (l <= 0 && kp != k) return false; v.push_back(l); bool x = false; for ...
6
#include <bits/stdc++.h> using namespace std; inline long long int inp() { long long int n = 0; int ch = getchar(); int sign = 1; while (ch < '0' || ch > '9') { if (ch == '-') sign = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') n = (n << 3) + (n << 1) + ch - '0', ch = getchar(); n = n ...
13
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& o, const vector<T>& v) { int b = 0; for (const auto& a : v) o << (b++ ? ", " : "") << a; return o; } template <typename T, typename U> ostream& operator<<(ostream& o, const pair<T, U>& p) { o << p.X << ' ' << p.Y; ...
1
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("O3") #pragma GCC target("avx2") using namespace std; int main() { cin.sync_with_stdio(0); cin.tie(0); long long T; cin >> T; for (int t = (0); t <= ((int)(T)-1); ++t) { string s, ta, tb; cin >> s; for (long long i = 0; i < 5...
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 105; const long long INF = 1e18; long long f[MAXN][MAXN], a[MAXN][MAXN]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) scanf("%lld", &a[i][j]); } srand((unsigned)time(NULL)); int c[...
15