solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; using lint = long long int; struct pt { lint x, y; bool isbase; int idx; bool operator<(const pt &ref) const { if (x == ref.x) return y < ref.y; else return x < ref.x; } }; lint ccw(const pt &a, const pt &b, const pt &c) { return a.x * b.y + ...
9
#include <bits/stdc++.h> using namespace std; long long base[18]; long long lim, cost, st; void predo() { base[0] = 1; for (int i = 1; i < 18; i++) base[i] = base[i - 1] * 10; } int main() { cin >> lim >> st >> cost; lim /= cost; predo(); int len = 0; long long f = st; while (f) { ++len; f /= 10...
4
#include <bits/stdc++.h> using namespace std; long long int zz = pow(2, 34); struct node { node *a[2]; long long int cnt[2]; }; node *newNode() { node *temp = new node; for (long long int i = 0; i < 2; i++) { temp->cnt[i] = 0; temp->a[i] = NULL; } return temp; } struct trie { node *root; } tr; voi...
5
#include <bits/stdc++.h> int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; const double PI = acos(-1), EPS = 1e-7; const int OO = 0x3f3f3f3f, N = 1e7 + 5, mod = 1e9 + 7; using namespace std; long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); } long long lcm(long lon...
4
#include <bits/stdc++.h> using namespace std; const int N = 6e5 + 10, mod = 1000000007; char s[N]; struct node { int fa, len, v[26]; } tr[N]; int n, mx, len, last = 1, tot = 1, b[N], c[N], now, f[N][3]; void add(int c) { int p = last; if (tr[p].v[c]) { int q = tr[p].v[c], y; if (tr[p].len + 1 == tr[q].len...
8
#include <bits/stdc++.h> using namespace std; int color[111]; bool vis[111]; int sm = 0; set<pair<int, int> > st; vector<vector<int> > v; void dfs(int u, int p, int pc) { if (vis[u]) { if (color[u] == pc) { int mn = min(u, p); int mx = max(u, p); st.insert(make_pair(mn, mx)); } return; ...
4
#include <bits/stdc++.h> using namespace std; const int N = 200000 + 7; const int M = 59; const int mid = M / 2; const int mod = 1e9 + 9; const int inf = 1e9 + 7; const long long linf = 1ll * inf * inf; const double pi = acos(-1); const double eps = 1e-7; const double ep = 1e-5; const int maxn = 1e5 + 7; const double P...
3
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1e6 + 5; long long x[maxn]; long long y[maxn]; int main() { long long x0, y0, ax, ay, bx, by; cin >> x[0] >> y[0] >> ax >> ay >> bx >> by; long long xs, ys, t; cin >> xs >> ys >> t; long long pos = 1; long long pre = ...
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); ; long long n, p, w, d; cin >> n >> p >> w >> d; long long x = -1, y = -1, z = -1; const int ITER = 1e7; for (y = 0; y < ITER; ++y) { if ((p - (y * d)) % w == 0) { x = (p - (y * d)) / w; z = n - x ...
6
#include <bits/stdc++.h> using namespace std; int main() { vector<pair<string, string>> v; v.push_back(make_pair("Time", "green")); v.push_back(make_pair("Mind", "yellow")); v.push_back(make_pair("Soul", "orange")); v.push_back(make_pair("Power", "purple")); v.push_back(make_pair("Reality", "red")); v.pus...
0
#include <bits/stdc++.h> using namespace std; int fun(int n, int* input, int m) { queue<pair<int, int>> curr; for (int i = 0; i < n; i++) { pair<int, int> temp; temp.first = input[i]; temp.second = i + 1; curr.push(temp); } while (curr.size() > 0) { if (curr.size() == 1) return curr.front()....
1
#include <bits/stdc++.h> using namespace std; int main() { int f; cin >> f; string s; cin >> s; if (f == 12) { if (s[3] > '5') s[3] = '0'; if (s[0] > '1') { if (s[1] == '0') s[0] = '1'; else s[0] = '0'; } if (s[0] == '1') { if (s[1] > '2') s[0] = '0'; } ...
2
#include <bits/stdc++.h> using namespace std; using ll = long long; using pi = pair<ll, ll>; const int N = 5e5 + 5, inf = 1e9 + 7; ll dp[N][2]; pi a[N]; int main() { ios ::sync_with_stdio(0), cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i].first >> a[i].second; auto cmp = [&](pi a, pi b)...
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); vector<int> b; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 0) b.push_back(i); } int cnt = 0; for (int i = 0; i < n; i++) { if (a[i] == 0) { cout << 0 << " "; cnt++; } else...
2
#include <bits/stdc++.h> using namespace std; int b, g; void start() { cin >> b >> g; cout << b + g - 1 << '\n'; int i = 1, j = 1, times = 0; bool flag = false; while (i <= b && j <= g) { if (g >= b) { cout << i << " " << j << '\n'; i += (times == 0 || i == b) ? 0 : ((times % 2 == 1) ? 1 : 0);...
1
#include <bits/stdc++.h> using namespace std; const int N = 18; const int M = (1 << 17) + 10; int dp[N][M][2][2], is[M]{0}; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= k; i++) { int x; scanf("%d", &x); is[x - 1] = 1; } memset(dp, 0xcf, sizeof(dp)); int m = (1 << n); for (int j =...
8
#include <bits/stdc++.h> using namespace std; int INF = 999999999; double EPSILON = 0.00000001; int check(int r, int dif, int req) { int n = 0; int changes = 0; while (r > 1) { changes++; int k = (r - dif) / dif; n += k; r -= k * dif; if (r != 1 and r == dif) return -1; dif = r - dif; } ...
6
#include <bits/stdc++.h> using namespace std; int dp[101000][2][3], vis[101000][2][3], n, T, val[101000], sz[101000], lc[101000], rc[101000]; void pdfs(int u) { sz[u] = 0; if (val[u] != -1) return; pdfs(lc[u]), pdfs(rc[u]); sz[u] = sz[lc[u]] + sz[rc[u]] + 1; if (sz[rc[u]] & 1) swap(lc[u], rc[u]); } int me...
12
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(12); for (int i = 0; i < 12; i++) { cin >> a[i]; } sort(a.begin(), a.end(), greater<int>()); int s = 0; if (n != 0) { for (int i = 0; i < 12; i++) { s = s + a[i]; if (s >= n) { cout << i...
0
#include <bits/stdc++.h> using namespace std; const int N = 8e8; int n, a, ans; ; int arr[110]; vector<int> ve; int sum(int x) { int ret = 0; while (x) { ret += (x % 10); x /= 10; } return ret; } int main() { scanf("%d", &n); while (n--) { scanf("%d", &a); int x = sum(a); ++arr[x]; v...
2
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int a[N]; int main() { int T; cin >> T; while (T--) { int n; cin >> n; int cnt = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] & 1) cnt++; } if (cnt == 0) printf("NO\n"); else if (cnt == n ...
0
#include <bits/stdc++.h> using namespace std; using namespace chrono; void _print(long long t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(unsigned long...
4
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long maxn = 2e5 + 5, mod = 1e9 + 7, inf = mod; long long n; set<long long> s[3]; long long q; struct { vector<long long> sum = vector<long long>(maxn, 0); void add(long long i, long long v) ...
8
#include <bits/stdc++.h> using namespace std; const int N = (int)209; long long power(long long x, long long y) { long long ans = 1; while (y > 0) { if (y % 2 == 1) ans = ans * x; x = x * x; y = y >> 1; } return ans; } int n, k, a[N], sz = 0; map<int, int> m; set<int> s; queue<int> q; int main() { ...
1
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") const long long MOD = 1e9 + 7; const long long MAXN = 1e5 + 7; const long long INF = 2e9; const long long BIG_INF = 2e18; const long long LOG = 20; const double eps = 1e-7; mt19937 engine(chrono::system_cloc...
2
#include <bits/stdc++.h> using namespace std; int n, m; char s1[600][600], s[600][600]; int sum[600][600]; long long ans1, ans2; bool c1[510][510][13]; bool c0[510][510][13]; bool c2[510][510][13]; bool c3[510][510][13]; bool c4[510][510][13]; bool c5[510][510][13]; bool allblack(int x, int y, int k, int l) { return ...
6
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10, mod = 998244353; const int INF = 1e8; int n, m; char a[N]; int b[N]; int c[N]; int d[N]; bool check(int x) { int mx = x; for (int i = 1; i <= n; i++) { b[i] = a[i] - '0'; d[i] = 0; } for (int i = n; i >= 1; i--) { if (b[i] <= mx) ...
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { unsigned long long n; cin >> n; string s = bitset<64>(n).to_string(); int count = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { count++; } } cout << (2 * n - count...
3
#include <bits/stdc++.h> using namespace std; int main() { int n, h, k; vector<int> p; scanf("%d %d %d", &n, &h, &k); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); p.push_back(x); } long long g = 0, c = 0; long long resp = 0; while (1) { while (g + p[c] <= h && c < p.size()) { ...
3
#include <bits/stdc++.h> using namespace std; void solve() { int n; string s; cin >> s; n = s.size(); int row = ceil((float)s.size() / 20); int val = n / row, flag = 0; if (n % row != 0) { flag = (row - (n % row)); val++; } vector<string> ans; int i = 0, count = 1; while (count <= row) { ...
2
#include <bits/stdc++.h> using namespace std; template <class T> inline void umax(T &a, T b) { if (a < b) a = b; } template <class T> inline void umin(T &a, T b) { if (a > b) a = b; } template <class T> inline T abs(T a) { return a > 0 ? a : -a; } const int inf = 1e9 + 143; const long long longinf = 1e18 + 143; i...
1
#include <bits/stdc++.h> using namespace std; const int N = 100, S = 100, M = N * S, INF = 1e9; int c[N][S], s[N], n; int main() { cin >> n; int ciel = 0, jiro = 0; vector<int> remain; for (int i = 0; i < n; ++i) { cin >> s[i]; for (int j = 0; j < s[i]; ++j) cin >> c[i][j]; for (int x = 0, y = s[i] ...
6
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; int n, m, a[maxn], mm, ans, res[maxn]; map<int, int> mp; int x, tot = 1, l, r; inline void write() { cout << ans << endl; for (int i = 1; i < tot; i++) cout << res[i] << (i == tot - 1 ? '\n' : ' '); } int main() { ios::sync_with_stdio(false...
5
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const int MOD = 1e9 + 7; int dx[8] = {0, 0, 1, -1, -1, 1, -1, 1}, dy[8] = {1, -1, 0, 0, -1, -1, 1, 1}; void INPUT() {} void OUTPUT() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { INPUT(); OUTPUT(); str...
3
#include <bits/stdc++.h> using namespace std; const long long prime_const = 1e9 + 7; bool sort_function(long long a, long long b) { return a < b; } long long minim(long long x, long long y) { if (x < y) return x; else return y; } long long maxim(long long x, long long y) { if (x > y) return x; else ...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const long long mod = 1e9 + 7; const int inf = INT_MAX; long long cnt[maxn]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long i, j, k, m, n; cin >> n; while (n--) { cin >> m; long long root...
3
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10, pi = 3.1415926535898; const int mod = 998244353, maxn = 2e5 + 10, maxm = 18; int T, n, m, k, x, y, z; long long dp[maxn][20][2], v, dd[maxn][2]; long long qpow(long long x, int k) { long long cnt = 1; while (k) { if (k & 1) cnt = cnt * x % ...
8
#include <bits/stdc++.h> using namespace std; int N, M; vector<int> edge[500005], ans[500005]; int pA; struct UFS { int fa[500005]; void init(int n) { int i; for (i = 0; i < n; i++) fa[i] = i; } int find(int x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); } void uni(int x, int y) { fa[find(x)] = f...
6
#include <bits/stdc++.h> using namespace std; set<vector<int> > s; int kol; int del[1005]; int main() { int n; cin >> n; vector<int> v; v.clear(); for (int i = 0; i < n; ++i) v.push_back(0); s.insert(v); for (int i = 2; i <= n; ++i) { v.clear(); for (int j = 0; j < n; ++j) v.push_back(0); for ...
3
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b) { long long res = 1ll; while (b > 0) { if (b % 2 != 0) res = (res * a) % 1000000007; a = (a * a) % 1000000007; b /= 2; } return res; } long long ncr(long long n, long long k) { if (k > n - k) k = n - k; long ...
5
#include <bits/stdc++.h> using namespace std; map<string, int> m; int main() { m["void"] = 0; int N; scanf("%d", &N); ; for (int(n) = 0; n < (int)N; n++) { string s1; cin >> s1; if (s1 == "typeof") { string s2; cin >> s2; string help; int num = 0; for (int(n) = 0; n <...
5
#include <bits/stdc++.h> using namespace std; const long long max1 = 11; const long long max2 = 102; const long long max3 = 1003; const long long max4 = 10004; const long long maxx = 100005; const long long max6 = 1000006; const long long max7 = 10000007; const long long lg2 = 7; const long long lg3 = 10; const long lo...
5
#include <bits/stdc++.h> using namespace std; const int maxN = 1000; const int key = 701; int n; int perm[maxN]; int weight[maxN]; vector<pair<int, int> > edges; int height[maxN]; set<int> parents[maxN]; int query(int v, vector<int> a, vector<int> b) { cout << (int)a.size() << endl; for (int i = 0; i < (int)a.size(...
11
#include <bits/stdc++.h> using namespace std; const char lend = '\n'; int good(int k, int n, int m) { int a, b, c; c = k / 6; a = k / 2 - c; b = k / 3 - c; n = max(0, n - a); m = max(0, m - b); return n + m <= c; } int main() { ios::sync_with_stdio(0); int n, m; cin >> n >> m; for (int i = 2;; ++i...
4
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const int N = 4e3 + 10; int dp[N][N]; int a[N], b[N]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; } fill(dp[0], dp[0] + N, 1); for (int i = 1; i <= n; i++) { for (int j = 0; j <...
5
#include <bits/stdc++.h> using namespace std; int isLower(char c) { return 97 <= c && c <= 122; } int isUpper(char c) { return 65 <= c && c <= 90; } char toLower(char ch) { if (isLower(ch)) return ch; return ch + 32; } string toLower(string str) { string ans; int i; for ((i) = 0; (i) < (int)(((int)(str).size(...
4
#include <bits/stdc++.h> using namespace std; int main() { int i, n; scanf("%d", &n); printf("%d\n", (n - 1) / 2); return 0; }
1
#include <bits/stdc++.h> using namespace std; struct cc { int a; int b; } xx[100005]; bool vis[100005]; int ss[100005]; vector<cc> vec; bool cmp(cc x, cc y) { return x.b < y.b; } int main() { int n, m, i, j, k, sum; long long ans; while (~scanf("%d%d", &n, &m)) { ans = 0; sum = 0; memset(vis, fals...
0
#include <bits/stdc++.h> using namespace std; const int N = 1001100; int arr[N]; const int mod = 100000000; struct point { int i; int j; int strength; } points[N]; bool cmp(point p1, point p2) { return p1.strength > p2.strength; } int main() { int n; while (cin >> n) { int cnt = 0; for (int i = 2; i <...
2
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const int maxn = 1010000; char str[maxn], a[maxn]; char xsk[10][5] = {"1869", "1968", "1689", "6198", "1698", "1986", "1896"}; int s1, s2, s3, s4; int main() { while (~scanf("%s%*c", str)) { s1 = s2 = s3 = s4 = 1; int t = 0, sum1 = 0, sum2...
4
#include <bits/stdc++.h> using namespace std; int a[222]; int main() { int n; cin >> n; for (int i = 0; i < 2 * n; i++) { cin >> a[i]; } int ans = 0; for (int i = 0; i < n; i++) { int pos = 2 * i + 1; for (int j = 2 * i + 1; j < 2 * n; j++) { if (a[j] == a[2 * i]) pos = j; } for (i...
3
#include <bits/stdc++.h> using namespace std; long long n, m, yes, i, j, k, t, x, y, l, r, tmp, mn, curr, ans; long long arr[1000005]; vector<pair<long long, long long> > v; string second; int main() { cin >> n >> k; for (i = 0; i < n; i++) cin >> arr[i]; for (i = 0; i < n; i++) { if (arr[i] == 1) { tmp...
6
#include <bits/stdc++.h> using namespace std; long long int Pow(long long int a, long long int b, long long int md, long long int ans = 1) { for (; b; b >>= 1, a = a * a % md) if (b & 1) ans = ans * a % md; return ans % md; } const long long int MAXN = 1e5 + 10; const long long int INF = 8e18;...
3
#include <bits/stdc++.h> using namespace std; const int N = 305; int n, x[N], y[N], a[N], ans; int f(int i, int j, int k, int l) { return x[i] * y[j] + x[j] * y[k] + x[k] * y[l] + x[l] * y[i] - x[j] * y[i] - x[k] * y[j] - x[l] * y[k] - x[i] * y[l]; } int main() { ios::sync_with_stdio(0); cin.tie(0); ci...
6
#include <bits/stdc++.h> using namespace std; long long n, w, s, k, t; long long fr(long long n) { w = 0; while (n) { w += n / 10 * 9; n /= 10; } return w; } int main() { cin >> n >> s; for (long long i = s; i <= n; i++) { if (fr(i) >= s) { cout << n - i + 1; return 0; } } co...
4
#include <bits/stdc++.h> using namespace std; const double eps = 1e-7; const int inf = 0x3f3f3f3f; char str[1000005]; const int N = 4000005; struct node { int open, close, done; node() {} node(int a, int b, int c) : open(a), close(b), done(c) {} void foo(const node &a, const node &b) { int k = min(a.open, b...
6
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") #pragma GCC optimize("O3") const long long inf = 4e18; const int infi = 2e9; int i, j, n, m, k, qq = 1, fr, to, w, x, y, mod = 998244353; char ch; vector<vector<int> > g; vector<int> ans; int max_pos; void d...
7
#include <bits/stdc++.h> using namespace std; template <typename T> void getFoo(T &x) { x = 0; register char c = getchar_unlocked(); for (; c < 48 || c > 57; c = getchar_unlocked()) ; for (; c > 47 && c < 58; c = getchar_unlocked()) x = (x << 1) + (x << 3) + c - 48; } template <typename T1, typename T2>...
3
#include <bits/stdc++.h> using namespace std; template <class T, class U> void ckmin(T &a, U b) { if (a > b) a = b; } template <class T, class U> void ckmax(T &a, U b) { if (a < b) a = b; } int N, K; vector<int> str; vector<int> dp; bitset<5013> ok[5013]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0...
9
#include <bits/stdc++.h> using namespace std; const int N = 2009; int n, pos, vis[N]; long long dis[N], g[N][N], mi; void dij(int u) { memset(dis, 0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); vis[u] = 1; for (int i = 1; i <= n; i++) { dis[i] = g[u][i]; for (int j = 1; j <= n; j++) if (j != i) ...
9
#include <bits/stdc++.h> using namespace std; bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; long long ln, lk, lm; const int maxn = 30000, J = 10; struct Aca { int next[maxn][J], fail[maxn]; int deep[maxn]; int root, L; int newNode() { for (int(i) = 0; (i) < (int)(J); (i)++) ...
12
#include <bits/stdc++.h> template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } using namespace std; string to_string(int a) { stringstream ss; ss << a; string s = ss.str(); return s; } int dp[103][3]; int main() { memset(dp, 0, sizeof(dp)); ; int n; cin >> n; int x;...
3
/*-- ILSH --*/ #include "bits/stdc++.h" using namespace std; typedef long long ll; typedef double ld; #define pb push_back #define pop pop_back #define mp make_pair #define vii vector < ll > #define dt cout<<"HERE\n"; #define pii pair < ll , ll > #define vpi vector < pii > #define fi first #define se second #define ...
1
#include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 1; const int lgN = 19; vector<int> G[maxN]; int N, M, d[maxN], p[lgN][maxN], root, size[maxN]; void dfs(int u, int f) { size[u] = 1; for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (v == f) continue; else { p[0]...
6
#include <bits/stdc++.h> using namespace std; int n, ans, a[100005]; int main() { scanf("%d", &n); for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]); sort(a + 1, a + 1 + n); for (int i = (2); i <= (n - 1); ++i) if (a[i] != a[1] && a[i] != a[n]) ++ans; printf("%d", ans); }
0
#include <bits/stdc++.h> using namespace std; long long int sr[26]; long long int sl[26]; int main() { long long int n; cin >> n; string a, b; cin >> a >> b; double ans = 0; for (int i = 0; i < a.length(); i++) { sl[b[i] - 'A'] = sl[b[i] - 'A'] + i + 1; ans = ans + sl[a[i] - 'A'] * (n - (i + 1) + 1)...
6
#include <bits/stdc++.h> using namespace std; template <class T> inline T GCD(T a, T b) { if (a < 0) return GCD(-a, b); if (b < 0) return GCD(a, -b); return (b == 0) ? a : GCD(b, a % b); } template <class T> inline T LCM(T a, T b) { if (a < 0) return LCM(-a, b); if (b < 0) return LCM(a, -b); return a * (b /...
1
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int len = 0, ans = 0; char s[N]; struct node { int id; int num; node() { id = num = 0; } node(int i, int n) { id = i, num = n; } } p[N]; void unit() { int k = len; len = 0; for (int i = 1; i <= k; i++) { if (p[i].num > 0) { if ...
6
#include <bits/stdc++.h> using namespace std; int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1}; char dc[4] = {'N', 'S', 'W', 'E'}; int main() { int t; string s; cin >> t; while (t--) { cin >> s; stack<char> st; for (int i = 0; i < s.size(); i++) { if (s[i] == 'A') st.push(s[i]); ...
1
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; if (n == 2 || n == 3) { cout << -1 << endl; continue; } if (n % 2 == 0) { for (long...
4
#include <bits/stdc++.h> using namespace std; bitset<26> ans, tmp; int n, cnt = 0; char ch; string s; int main() { ans.set(); cin >> n; for (int i = 1; i <= n; i++) { cin >> ch >> s; if (ans.count() == 1 && i < n && ch != '.') cnt++; else { tmp.reset(); for (int i = 0; i <= s.length(...
4
#include <bits/stdc++.h> using namespace std; char _; const int MOD = 1000000007; const int INF = 0x3f3f3f3f; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long fpow(long long b, long long exp, long long mod) { if (exp == 0) return 1; long long t = fpow(b, exp / 2, mod); if (...
4
#include <bits/stdc++.h> using namespace std; int n; double f[55][55], g[55], w[55]; pair<int, int> p[55]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &p[i].first); } for (int i = 1; i <= n; i++) { scanf("%d", &p[i].second); } double st = clock(); sort(p + 1, p + n + 1...
8
#include <bits/stdc++.h> using namespace std; const int N = 1000010; int a[N]; int main() { int n, x, res = 1000000007; cin >> n; for (int i = 1; i <= n; i++) { scanf("%d", &x); a[x] = 1; } a[0] = a[1] = 1; for (int i = 1; i <= (1e6); i++) { if (a[i] == 0) a[i] = a[i - 1]; else a...
1
#include <bits/stdc++.h> using namespace std; const int N = 1005, MOD = 1e9 + 7; using ll = long long; ll fast_pow(ll a, int n) { if (n == 0) return 1; if (n % 2 == 0) return fast_pow(a * a % MOD, n / 2); return fast_pow(a * a % MOD, n / 2) * a % MOD; } ll dp[N][N]; int main() { int k; ll pa, pb; scanf("%d%...
7
#include <bits/stdc++.h> using namespace std; long long a[3000000]; long long b[3000000]; long long c[3000000]; int main() { long long t; scanf("%lld", &t); while (t--) { long long n; scanf("%lld", &n); long long sum = 0; for (int i = 1; i <= n; i++) { scanf("%lld %lld", &a[i], &b[i]); }...
4
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const int N = 3e5 + 10; int a[N]; int main() { int n; scanf("%d", &n); int z; scanf("%d", &z); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } sort(a + 1, a + n + 1); int l = 0, r = n / 2; int ans = 0; while (l <= ...
6
#include <bits/stdc++.h> const int N = 300300; using namespace std; struct edge { int ul, ur, dl, dr, a, al, ar; long long xl, xr; }; edge comb(edge a, edge b, int sa, int sb) { edge g; g.a = max(a.a, b.a); g.al = a.al; g.xl = a.xl; g.ul = a.ul; g.dl = a.dl; g.ar = b.ar; g.xr = b.xr; g.ur = b.ur; ...
8
#include <bits/stdc++.h> using namespace std; const int INFint = 2147483647; const long long INF = 9223372036854775807ll; const long long MOD = 1000000007ll; int d[110][10100]; int main() { ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { string s; cin >> s; int p...
3
#include <bits/stdc++.h> using namespace std; const long double PI = 3.14159265359; const long long MOD = (long long)998244353; const long long MAXN = (long long)2e5 + 10; const long long INF = (long long)2242545357980376863; const long double EPS = (long double)1e-8; const long long LOG = (long long)30; long long mul(...
5
#include <bits/stdc++.h> using namespace std; const int Z = (int)2e3 + 111; const int inf = (int)1e9 + 111; const long long llinf = (long long)1e18 + 5; const int MOD = (int)1e9 + 7; long long a[Z], b[Z]; int main() { srand(time(0)); ios_base::sync_with_stdio(false); int n; long long sum1 = 0, sum2 = 0; cin >...
7
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); template <typename T> static T randint(T lo, T hi) { return uniform_int_distribution<T>(lo, hi)(rng); } int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; vector<pai...
9
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 10; const int mod = 1e9 + 7; vector<int> g[N]; int n; long long d[N], f[N]; bool ye[N]; void dfs1(int u, int fa) { int sz = g[u].size(); d[u] = 1; for (int i = 0; i <= sz - 1; ++i) if (g[u][i] != fa) { int v = g[u][i]; dfs1(v, u)...
6
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios_base ::sync_with_stdio(false); int n; cin >> n; vector<int> a; int z = 0; for (int i = 0; i < n; i++) { int x; cin >> x; if (x != 0) a.push_back(x); } vector<int> b; int z1 = 0; for (int i = 0; i < n; i++) { ...
2
#include <bits/stdc++.h> using namespace std; const int MaxN = 100224; vector<int> ans; int n, d, f, s; pair<int, int> a[MaxN]; int main() { cin.sync_with_stdio(0); cin >> n >> d; cin >> f >> s; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a[i].first = f * x + s * y; a[i].second = i + ...
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } char s[100]; int main() { cin >> s; stringstream ss(s); long long val; if (ss >> val) { if (-128 <= val && val <= ...
2
#include <bits/stdc++.h> using namespace std; int xorr(int a, int b) { return ((a | b) & (~a | ~b)); } int main() { int n, m; scanf("%d %d", &n, &m); char a[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } int r = 0; int c = 0; int p = 0; int j = 0; ...
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, s, p, k, m, t, x, y, ans, res; cin >> t; while (t--) { cin >> n; long long a[n + 1]; for (int i = 1; i <= n; i++) { cin >> a[i]; } ans = 0; m = INT_MAX; for (int i = n; i >= 1; i--) { if (a[i] > m) ans+...
1
#include <bits/stdc++.h> using namespace std; template <class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } template <class T> void db(T a, int p = -1) { if (p >= 0) cout << fixed << setprecision(p); cout << a << ...
5
#include <bits/stdc++.h> using namespace std; template <class T> inline T CHMAX(T& x, const T y) { return x = (x < y) ? y : x; } template <class T> inline T CHMIN(T& x, const T y) { return x = (x > y) ? y : x; } using PII = pair<int, int>; using PLL = pair<long long, long long>; using LL = long long; using ULL = un...
7
#include <bits/stdc++.h> using namespace std; const int maxn = 2e6 + 3; long long int x[maxn], y[maxn]; long long int w[maxn]; int ct[maxn]; vector<int> v[maxn]; int ans[maxn]; int done_per[maxn]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> w[i]; } memset(ct, 0, sizeof(ct));...
8
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; string x, y; stringstream ss; for (int i = a + 1; i <= 200000; i++) { ss.str(""); ss << i; x = ss.str(); y = ""; for (int j = 0; j < x.size(); j++) { if (x[j] == '4' || x[j] == '7') { y += x[j...
2
#include <bits/stdc++.h> using namespace std; int read() { char s; int k = 0, base = 1; while ((s = getchar()) != '-' && s != EOF && !(s >= '0' && s <= '9')) ; if (s == EOF) exit(0); if (s == '-') base = -1, s = getchar(); while (s >= '0' && s <= '9') { k = k * 10 + (s - '0'); s = getchar(); }...
2
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << "\n"; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(nam...
3
#include <bits/stdc++.h> using namespace std; long long i, j, r[100], l[100], n, x, cnt = 1, ans; int main() { cin >> n >> x; for (i = 1; i <= n; i++) { cin >> l[i] >> r[i]; ans += (l[i] - cnt) % x; ans += (r[i] - l[i] + 1); cnt = r[i] + 1; } cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; struct node { char decimal; int flag; bool operator<(const node &b) const { if (decimal > b.decimal) return 1; else if (decimal == b.decimal) { if (flag < b.flag) return 1; else return 0; } else return 0; } }; int ...
3
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(nam...
3
#include <bits/stdc++.h> using namespace std; using ll = long long; const int mod = 1e9 + 7; ll pwr(ll a, ll b); int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n); for (int& x : a) cin >> x; int i = 0, j = n - 1...
2
#include <bits/stdc++.h> using namespace std; int n, k; void init() { scanf("%d%d", &n, &k); printf("%d\n", (6 * n - 1) * k); for (int i = 0; i < n; ++i) { printf("%d %d %d %d\n", (i * 6 + 1) * k, (i * 6 + 2) * k, (i * 6 + 3) * k, (i * 6 + 5) * k); } } int main() { init(); return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long int INF = 9e18L; const long long int mod = 1e9 + 7; long long int power(long long int x, long long int y) { long long int res = 1; int mod = 1e9 + 7; while (y > 0) { if (y & 1) (res = res * x) %= mod; (x = x * x) %= mod; y = y >> 1; } r...
3