solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; const int M = 1e3 + 5; const int INF = 1e9 + 5; const int mod = 1e9 + 7; int n, m, a[N], dp[M]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; if (n > m) { cout << "YES\...
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n + 1, 0); int kl = 0, ans = 0; for (int i = 0; i < 2 * n; i++) { int x; cin >> x; if (a[x] > 0) { a[x]--; kl--; } else { a[x]++; kl++; } an...
0
#include <bits/stdc++.h> using namespace std; int main() { int n, k, x, z; cin >> n >> k; string s; cin >> s; for (int i = 0; i < n; i++) if (s[i] == 'G') x = i; else if (s[i] == 'T') z = i; if (z < x && (x - z) % k == 0) for (int j = x; j > z;) { j -= k; if (s[j] == '#')...
0
#include <bits/stdc++.h> using namespace std; const int maxn = 100 + 5; int a[maxn], b[maxn]; int toggle[maxn]; bool on[maxn]; int main(void) { int n; while (cin >> n) { string s; cin >> s; queue<int> q; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; toggle[i] = b[i]; if (s[i ...
5
#include <bits/stdc++.h> using namespace std; inline long long pow(long long a, long long b, long long mo) { if (!a) return 0; long long tmp = 1; for (; b; b >>= 1) { if (b & 1) tmp = (long long)tmp * a % mo; a = (long long)a * a % mo; } return tmp; } template <class T> inline void R(T &xx) { xx = 0...
18
#include <bits/stdc++.h> using namespace std; int n, a[1000000], num[1000000], ban[1000000], tot, now, ret; int ac() { scanf("%d", &n), ret = n; for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); if (n == 1) return 0; for (int i = 1; i <= n; ++i) { if (a[i] < 0) num[++tot] = i; else if (a[i] == 0) {...
9
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; const long long linf = 1ll * inf * inf; const int N = 300000 + 7; const int M = 19; const int multipleTest = 0; struct RMQ { int a[N]; int RMQ[N][20]; int n; int& operator[](int pos) { return a[pos]; } void build() { for (int i = (1), ...
21
#include <bits/stdc++.h> using namespace std; const int inf_int = 1e9 + 100; const long long inf_ll = 1e18; const double pi = 3.1415926535898; template <class T1, class T2> std::ostream &operator<<(std::ostream &out, const std::pair<T1, T2> &rhs) { out << "( " << rhs.first << " , " << rhs.second << " )"; return out...
27
#include <bits/stdc++.h> using namespace std; const double pi = 2 * acos(0.0); const int mod = 1e9 + 7; class Solver { int n; vector<int> a; vector<int> g; public: long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) ...
8
#include <bits/stdc++.h> using namespace std; const int MAX = 66666; const int maxn = 105; int f[maxn][5]; int a[maxn]; int main() { int n; scanf("%d", &n); memset(f, 0, sizeof f); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { f[i][0] = min(f[i - 1][0] + 1, f[i - 1][1] + ...
6
#include <bits/stdc++.h> using namespace std; const long long base = 31LL; const long long mod = 1000000000009LL; long long h(char *x) { long long hash = 0; int len = strlen(x); for (int e = 0; e < len; e++) hash = (hash * base + (tolower(x[len - e - 1]) - 'a' + 1)) % mod; return hash; } char text[10100], a...
11
#include <bits/stdc++.h> using namespace std; int main() { int vert, edge, u, v; scanf("%d %d", &vert, &edge); vector<int> Adj[vert + 1]; int Graph[vert + 1][vert + 1]; memset(Graph, 0, sizeof(Graph)); for (int i = 0; i < edge; i++) { scanf("%d %d", &u, &v); Graph[u][v] = 1; Adj[u].push_back(v);...
9
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T t) { return t < 0 ? -t : t; } const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } inline int fp(int i) { if (i == 2) return 2; if (i == 1) return 1; return !(i & 1); } int fi(int i) { if (i...
14
#include <bits/stdc++.h> using namespace std; struct DSU { int N, components; vector<int> par, s, val; DSU(int n) { N = n; components = n; par.resize(N + 1); s.resize(N + 1); s.assign(N + 1, 1); val.resize(N + 1); iota((par).begin(), (par).end(), 0); } int find_par(int node) { ...
13
#include <bits/stdc++.h> using namespace std; int n, k; long long sum; const long long inf = 1000000000000000000LL; int pos; int a[100010], d[100010]; inline long long solve0() { long long ans = -inf, s = 0; for (int i = n; i; --i) { s += a[i]; ans = max(ans, s - d[i]); } return max(0LL, ans); } long lo...
14
#include <bits/stdc++.h> using namespace std; #define ll long long int #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define lf(i, a, b) for(ll i=a;i<b;i++) #define lb(i, a, b) for(ll i=a;i>b;i--) #define pb push_back void solve(){ int n, o = 0, z = 0, flag = 0, ans = 1; cin>>n;...
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1e6 + 3; inline long long pow(long long a, long long b) { long long ans = 1; for (; b; b >>= 1, a = a * a % mod) if (b & 1) ans = ans * a % mod; return ans; } long long farc[1000005]; inline void prepare(long long a) { farc[0] = 1; for (l...
10
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, SQ = 1500; int a[N], t[N], x[N], p[N], Cnt[2 * N][N / SQ + 5], T[N][N / SQ + 5], n; vector<int> num; void Build() { for (int i = 0; i < n; i++) { a[i] = lower_bound(num.begin(), num.end(), a[i]) - num.begin(); Cnt[a[i]][i / SQ + 1]++; } ...
23
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> a(n + 1); vector<int> yes(n + 1), no(n + 1); int cntn = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] > 0) { yes[a[i]]++; } else { cntn++; ...
8
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 2; long long n, a[maxn], dem = 0, sum = 0; map<long long, int> m; void Enter() { cin >> n; int i = 1, j = 1; while (i <= 2 * n) { if (j <= n) { a[j] = i; a[j + n] = i + 1; j = j + n + 1; } else { a[j] = i; a...
4
#include <bits/stdc++.h> using namespace std; char local = '0'; void solve() { long long int n; cin >> n; long long int e[n]; for (long long int i = (long long int)0; i < (long long int)n; i++) cin >> e[i]; sort(e, e + n, greater<long long int>()); int end = n - 1; int size = 1; int count = 0; whi...
4
#include <bits/stdc++.h> using namespace std; struct idt { int v, idx; }; idt a[300015]; bool cmp(idt a, idt b) { return a.v < b.v; } int main() { int n, x1, x2; cin >> n >> x1 >> x2; for (int i = 0; i < n; i++) { cin >> a[i].v; a[i].idx = i + 1; } sort(a, a + n, cmp); int data = n; bool ans = 0...
9
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-8; const int INF = 0x3F3F3F3F; const int64_t LINF = 4611686018427387903; const int MOD = 1e9 + 7; void solve() { string a, b; int n; cin >> n >> a >> b; set<pair<char, char>> has; for (int i = 0; i < n; i++) { if (a[i] > b[i]) { cou...
9
#include <bits/stdc++.h> using namespace std; template <typename T> T In() { T x; cin >> x; return x; } string shorter(string S, string T) { return S.size() <= T.size() ? S : T; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int k2 = In<int>(), k3 = In<int>(), k5 = In<int>(), k6 = In<int>(), ...
0
#include <bits/stdc++.h> using std ::max; using std ::min; template <class T> bool chkmin(T &x, const T &y) { return y < x ? x = y, true : false; } template <class T> bool chkmax(T &x, const T &y) { return y > x ? x = y, true : false; } template <class T> inline void read(T &in) { char ch; int f = 1; for (ch ...
1
#include <bits/stdc++.h> using namespace std; const int maxn = (int)333; ; int num[maxn][maxn], geo[maxn][maxn], sp[2 * maxn][2 * maxn], n, m; long long ans; int pr[maxn * maxn], sz[maxn * maxn], col[maxn * maxn]; vector<pair<int, int> > gem[maxn * maxn]; map<int, int> fp; queue<pair<int, int> > q; int find(int a) { re...
19
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int ans = 0; int p = 1; int diff; if (a > b) diff = a - b; else diff = b - a; for (int i = 0; i < diff / 2; i++) { ans += 2 * p; p++; } ans += diff % 2 * p; cout << ans << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9; const long long MOD = (int)1e9 + 7; const int HWMAX = (int)1e5 + 5; const int NMAX = 2000 + 5; int h, w, n; long long dp[NMAX]; long long jc[2 * HWMAX], jcinv[2 * HWMAX]; long long qpow(long long a, int b) { long long c = 1; while (b) { if ...
14
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; map<char, string> a; set<char> b; vector<char> answ; for (int i = 0; i < s.size(); ++i) { if (t[i] != a[s[i]][0]) a[s[i]] += t[i]; b.insert(t[i]); b.insert(s[i]); } int ans = 0; for (char c = 'a'; c <= '...
7
#include <stdio.h> #include <stdlib.h> int main() { int n,t,m,a[222222],i,flag; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for( i=0;i<n;i++) scanf("%d",&a[i]); long long sum=0,temp=1; flag=1; while(flag) { for(i=0;i<n;i++) { if(a[i...
3
#include <bits/stdc++.h> using namespace std; int n, ans[1111]; bool b[1111]; vector<int> a; void query() { if (a.size() == 0) return; memset(b, true, sizeof(b)); printf("%d\n", a.size()); printf("%d", a[0]); b[a[0]] = false; for (int i = 1; i < a.size(); i++) { printf(" %d", a[i]); b[a[i]] = false;...
11
#include <bits/stdc++.h> using namespace std; void solve() { int n, mn = INT_MAX; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int ans = 0; for (int i = n - 1; i >= 0; i--) { if (arr[i] > mn) ans++; mn = min(arr[i], mn); } cout << ans << "\n"; ; } int main() { i...
3
#include <bits/stdc++.h> using namespace std; ; struct _dbg { template <class T> _dbg& operator,(const T& v) { cerr << v << " "; return *this; }; }; _dbg _d; long long a[100100], N, s; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }; int main() { cin >> N; for (int i = 0, _i ...
8
#include <bits/stdc++.h> using namespace std; int main() { long long int m, k, l, n; scanf("%lld%lld%lld%lld", &n, &m, &k, &l); if (m > n) { cout << -1; return 0; } if ((l + k) % m == 0) { if ((long long)(l + k) > n) cout << -1; else cout << (long long)(l + k) / m; } else { i...
6
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") using namespace std; template <typename T> T sqr(T a) { return a * a; } template <typename T> int sign(T a) { if (a == 0) return 0; return (a > 0 ? 1 : -1); } template <typename T> bool uax(T& a,...
18
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)1e7 + 10; int a[101]; bool used[101]; int main() { int n; scanf("%d", &n); if (n % 2 == 0) { for (int i = 1; i <= n; i++) { if (i % 2 == 0) printf("%d ", i - 1); else printf("%d ", i + 1); } } else printf...
0
#include <bits/stdc++.h> using namespace std; bool debug; const int inf = 1e9 + 5; const int nax = 1e6 + 5; struct wezel { int lewy; int prawy; wezel(int l = -1, int p = -1) : lewy(l), prawy(p) {} }; wezel d[nax]; int n, c, czas, a, b; int najwi[nax], najmi[nax]; char t[1000]; vector<int> vl[nax], vp[nax]; bool m...
16
#include <bits/stdc++.h> using namespace std; const int INF = (int)2E9; const long long LINF = (long long)9E18; const long double PI = acos(-1.0); const long double EPS = 1E-11; int t[6][6] = { {2, 3, 4, 1, 5, 6}, {4, 1, 2, 3, 5, 6}, {1, 5, 3, 6, 4, 2}, {1, 6, 3, 5, 2, 4}, {5, 2, 6, 4, 3, 1}, {6, 2, 5, 4, 1, 3}...
9
#include <bits/stdc++.h> using namespace std; const int N = 1000010; int n, m; int a[N], b[N]; long long w[N], sum1[N], sum2[N], dp[N]; int dfn[N], low[N], st[N], cp[N]; int dn = 0, sn = 0, cn = 1; vector<int> adj[N], adj2[N]; void init() { for (int i = 1; i <= 15000; i++) { sum1[i] = i; sum1[i] += sum1[i - 1...
13
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long temp = 1, cnt = 1; while (temp < n) { cnt++; temp += cnt; } if (temp >= n) temp -= cnt; n = n - temp; cout << n << endl; }
2
#include <bits/stdc++.h> using namespace std; double x[2], y[2], r[2]; inline double dist() { return (x[0] - x[1]) * (x[0] - x[1]) + (y[0] - y[1]) * (y[0] - y[1]); } inline bool cover(int a, int b) { return sqrt(dist()) + r[b] <= r[a]; } int main() { for (int i = 0; i < 2; i++) scanf("%lf %lf %lf", &x[i], &y[i], &r...
10
#include <bits/stdc++.h> using namespace std; bool is_leap(int x) { if (x % 400 == 0) return 1; else if (x % 100 == 0) return 0; else if (x % 4 == 0) return 1; else return 0; } int y, cnt; int main() { scanf("%d", &y); for (int z = (y + 1); z <= (999999); z++) { if (is_leap(z - 1)) ...
8
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; const int inf = 1e9; const long long INF = 1e18; const int mod = 1e9 + 7; const int N = 1e5 + 5; void solve() { int a, b; cin >> a >> b; cout << (a * b + 1) / 2 << "\n"; } int main() { ios_base::sync_with_stdio(...
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int dp[1005][1005]; void chk(int &x, int y) { x = (x + y) % mod; } int main() { int n, m, K; cin >> n >> m >> K; int mx = max(n - 2, m - 2); for (int i = 1; i <= mx; i++) dp[1][i] = 1; for (int i = 2; i <= K; i++) for (int j = 0; j <= ...
12
#include <bits/stdc++.h> using namespace std; int main() { int x, t, a, b, da, db; scanf("%d%d%d%d%d%d", &x, &t, &a, &b, &da, &db); if (x == 0) { printf("YES\n"); return 0; } for (int i = 0; i < t; ++i) if (b - i * db == x) { printf("YES\n"); return 0; } for (int i = 0; i < t; ++...
4
#include <bits/stdc++.h> using namespace std; long long t = 1, ans = 0; int n; int main() { cin >> n; for (int i = 2; i <= n; ++i) { t = t * 3 % 1000000007; if (i % 2 == n % 2) ans += t; else ans -= t; ans %= 1000000007; } cout << (ans + 1000000007) % 1000000007 << endl; return 0; ...
7
#include <bits/stdc++.h> using namespace std; #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define int long long #define pb push_back #define vi vector<int> #define size(x) (int)((x).size()) #define memf(a) memset(a,false,sizeof(a)) #define F first #define S second #def...
6
#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; int main() { int tux, foo = 0, bar = 0, baz = 0, quz = 1; cin >> tux; for (int i = 0; i < tux; i++) { int pur; cin >> pur; foo += pur; bar++; int a = foo * quz; int b = bar * baz; if (a >= b) { baz = foo; quz = bar; } else ...
9
#include <bits/stdc++.h> const int INF = (int)1e9 + 7; const int N = (int)1e5 + 7; const int MAXN = (int)5e5 + 228; using namespace std; int t[MAXN * 4][2], a[MAXN], n, b[MAXN]; void build(int v = 1, int l = 1, int r = n + n + n, bool isb = 0) { if (l == r) { if (!isb) t[v][0] = t[v][1] = a[l]; else ...
12
#include <bits/stdc++.h> using namespace std; const long long modd = (1000LL * 1000LL * 1000LL + 7LL); int main(int argc, char *argv[]) { mt19937 rang(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int> rand_gen(0, modd); ios_base::sync_with_stdio(false); cin.tie(0); cout.pr...
5
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; } using ll = long long; using ld = long double; const string FILENAME = "input"; const int M...
25
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int x = sqrt(n); int extra = n - (x * x); if (extra == 0) printf("%d", 4 * x); else if (extra <= x) printf("%d", (4 * x) + 2); else printf("%d", (4 * x) + 4); return 0; }
2
/**Bismillahir Rahmanir Rahim.**/ #include <bits/stdc++.h> using namespace std; //#include <ext/pb_ds/assoc_container.hpp> //using namespace __gnu_pbds; //typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set; typedef unsigned long long ull; typedef long long ll; typedef lon...
4
#include <bits/stdc++.h> using namespace std; string basic = "What are you doing at the end of the world? Are you busy? Will you save " "us?"; string st = "What are you doing while sending \""; string se = "\"? Are you busy? Will you send \""; string ed = "\"?"; int pt1 = st.length(); int pt2 = se.length(); int...
9
#include <bits/stdc++.h> int ans[] = {0, 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51, 1, 2, 1, 14, 1, 2, 2, 14, 1, 6, 1, 4, 2, 2, 1, 52, 2, 5, 1, 5, 1, 15, 2, 13, 2, 2, 1, 13, 1, 2, 4, 267, 1...
6
#include <bits/stdc++.h> using namespace std; struct edge { int r, nxt; } e[100100 << 1]; struct data { int lc, rc, cnt; vector<int> v; } s[100100 << 2]; int dy[100100], anses[100100], dep[100100], ptr; int Q[100100], Qsz, bel[100100], idsz, A[100100], Asz; int ai[100100], rt[100100], bi[100100], ci[100100], di[1...
24
#include <bits/stdc++.h> using namespace std; class RTree { public: vector<int> data; vector<int> lft; vector<int> rgt; int sz; int tsz; void Init(int size) { sz = size; tsz = 2; while (tsz < sz) tsz *= 2; data.resize(1 + tsz - 1 + tsz); lft.resize(1 + tsz - 1 + tsz); rgt.resize(1 +...
11
#include <bits/stdc++.h> using namespace std; void solve(long long int caseno) { long long int i, j, n, o = 0; cin >> n; vector<long long int> arr(n); for (i = 0; i < n; i++) { cin >> arr[i]; } if (arr[0] == 1) { cout << n + 1 << ' '; for (i = 1; i <= n; i++) cout << i << ' '; cout << '\n'; ...
4
#include <bits/stdc++.h> using namespace std; class item { public: int v; int dir; long long dist; item(int _v, int _dir, long long _dist) { v = _v; dir = _dir; dist = _dist; } }; int main() { ios::sync_with_stdio(false); int n; cin >> n; vector<int> adj[n]; for (int i = 0; i < n - 1; +...
12
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long extgcd(long long a, long long b, long long& x, long long& y) { if (b == 0) { x = 1; y = 0; return a; } long long x1, y1; long long d = extgcd(b, a % b, ...
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int a1, b1, a2, b2; cin >> a1 >> b1 >> a2 >> b2; int sum = a1 * b1 + a2 * b2; if (a1 == b1 || a2 == b2) { cout << "No" << endl; continue; } int flag = 0; for (int i = 1...
1
#include <bits/stdc++.h> using namespace std; int n, a[555555], t[555555], pos[555555], m; vector<int> ans; inline int mp(const int &first) { return lower_bound(t, t + m, first) - t; } void init() { for (int i = (1); i <= (n); ++i) scanf("%d", &a[i]), t[i - 1] = a[i]; sort(t, t + n); m = unique(t, t + n) - t; } i...
15
#include <bits/stdc++.h> int main() { char s1[10], s2[10], s3[10]; gets(s1); int m, t1, t2, tm, hr, minn, xh, xm; scanf("%d", &m); s2[0] = s1[0]; s2[1] = s1[1]; s2[2] = '\0'; s3[0] = s1[3]; s3[1] = s1[4]; s3[2] = '\0'; int n1, n2; n1 = atoi(s2); n2 = atoi(s3); if (m > 60) { t1 = m - (60 ...
1
#include <bits/stdc++.h> const int N = 3e5 + 5, mod = 998244353; int n, nxt[N << 1], to[N << 1], head[N], f[N][3], cnt; int read() { int x = 0, f = 1; char s; while ((s = getchar()) > '9' || s < '0') if (s == '-') f = -1; while (s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar(); ...
16
#include <bits/stdc++.h> using namespace std; int maxx(int fd, int sd) { return (fd > sd) ? fd : sd; } int main() { int n; vector<int> a; scanf("%d", &n); int min = 0; int max = 0; for (int i = 0; i < n; ++i) { int cur; scanf("%d", &cur); a.push_back(cur); if (cur < a[min]) min = i; if (...
0
#include <bits/stdc++.h> using namespace std; long long int ans; int arr[300009]; vector<int> vr; void solve() { int a, b, c; cin >> a >> b >> c; a = max(a, b); a = max(a, c); cout << a << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int testcases; cin >> testcases; for (int t ...
0
#include <bits/stdc++.h> int f_min(int x, int y) { if (x < y) return x; else return y; } int f_max(int x, int y) { if (x < y) return y; else return x; } void swap(int& x, int& y) { x = x ^ y; y = x ^ y; x = x ^ y; } int f_abs(int x) { return (x) > (0) ? (x) : (-x); } int lowbit(int x) { re...
8
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << "\n"...
7
#include <bits/stdc++.h> using namespace std; int main() { int x = 1; int output = 0; cin >> x; if (x == 3) output = 5; else { for (int n = 1; n <= 100;) { int temp = (n * n + 1) / 2; if (x <= temp) { output = n; break; } n = n + 2; } } cout << output; }...
9
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int n; cin >> n; string s; cin >> s; bool res = false; if (n <= 1) { cout << "-1" << " " << "-1" << endl; res = true; } for (int i = 0; i < n - 1; i++) { ...
0
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } long long powmod(long long a, long long b) { long long res = 1; a %= 1000000007; assert(b >= 0);...
9
#include <bits/stdc++.h> using namespace std; long long int ans = 0; long long int takethree(long long int t) { return t * (t - 1) * (t - 2) / 6; } long long int taketwo(long long int t) { return t * (t - 1) / 2; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } map<int, int> mapper; void ini() { int Tot...
11
#include <bits/stdc++.h> using namespace std; using ll = long long; int sum() { int a, b, c, sum; sum = a + b + c; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<pair<ll, ll> > a(n); for (auto &[f, s] : a) cin >> f; for (auto &[f, s] : a) cin >> s; sort(a.begin()...
6
#include <bits/stdc++.h> using namespace std; bool a[1100]; vector<int> ans[100]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= k; i++) { int x; cin >> x; a[x] = true; ans[i].push_back(x); } int x = 1; for (int i = 1; i <= k; i++) { while (ans[i].size() != n) { if (a[x...
1
#include <bits/stdc++.h> using namespace std; struct Eve { int color; int num; Eve(int num, int color) : num(num), color(color){}; }; bool operator<(Eve& a, Eve& b) { return a.num < b.num; } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); vector<vector<Eve> > r(n + 2, vector<Eve>()); vector<vector<...
4
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, m, i, j, k, sa[N], rnk[N], h[N], tree[N * 4]; int ch[N * 2][26], fa[N * 2], len[N * 2], tot, last, pos[N * 2], vis[N * 2]; int head[N * 2], Next[N * 2], adj[N * 2]; long long ans, siz[N * 2]; char c[N]; struct asd { int x, y; bool operator<(...
26
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; int n, m, fa[maxn], f[maxn], d[maxn]; vector<int> G[maxn]; int id(int x, int y) { return (x - 1) * m + y; } int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } void unite(int x, int y) { fa[find(x)] = find(y); } int main() { scanf("%...
14
#include <bits/stdc++.h> using namespace std; int a[300], b[300], c[300]; void swap(int i) { int temp = b[i]; b[i] = b[i + 1]; b[i + 1] = temp; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) { scanf("%d", &b[i]); c[i] = b[i]; } int c...
7
#include <bits/stdc++.h> using namespace std; int main() { double a[10005], k, max1, min1, sum, x; int n, i; cin >> n >> k; for (i = 0; i < n; i++) cin >> a[i]; min1 = 0; max1 = 1000; while (max1 - min1 > 0.00000001) { sum = 0; x = (max1 + min1) / 2; for (i = 0; i < n; i++) if (a[i] > x)...
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, x, y; cin >> n >> x >> y; int a[n + 7]; for (long long i = 0; i < n; i++) { cin >> a[i]; } int count = 0; int index = 0; for (long long i = 0; i < n; i++) { co...
2
#include <bits/stdc++.h> using namespace std; struct node { int n; int m; int x; int k; } p[200005]; bool cmp(node a, node b) { return a.x > b.x; } int main(void) { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { scanf("%d", &p[i].n); p[i].k = i; } for (int i = 0; i < n; i++) { scan...
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; double l[MAXN], r[MAXN]; double add[MAXN]; int main() { int n; long double sy, a, b; cin >> sy >> a >> b; cin >> n; add[0] = 0; for (int i = 1; i <= n; i++) { scanf("%lf%lf", &l[i], &r[i]); add[i] = add[i - 1] + (r[i] - l[i]); ...
16
#include <bits/stdc++.h> using namespace std; template <typename T> void printRange(T &x) { for (auto y : x) cout << y << ' '; cout << endl; } const int maxn = 2e5 + 10; int n, m, u, v; vector<int> adj[maxn]; int color[maxn]; bool odd[maxn]; const int white = 0; const int gray = 1; const int black = 2; vector<tuple...
15
#include <bits/stdc++.h> using namespace std; const int N = 1000005; int n, d, ans; struct ky { int x, y, u, v; } a[N]; inline void gcd(const int a, const int b) { return !b ? d = a, void() : gcd(b, a % b); } inline int A(const int x) { return x < 0 ? -x : x; } map<pair<double, double>, int> mp; template <class I> ...
16
#include <bits/stdc++.h> using namespace std; long long n, i, s, t1 = 1, t2; int main() { cin >> n; for (i = 1; i <= n; i *= 10) s += n - i + 1; cout << s; return 0; }
4
#include <bits/stdc++.h> int main() { char s[101], t[101]; gets(s); gets(t); int flag = 0, i; if (strcmp(s, t) != 0) { for (i = 0; i < strlen(s); i++) { if (s[i] < t[i]) { if (t[i] - s[i] > 1) { s[i] = s[i] + 1; flag = 1; break; } if (t[i] - s[i]...
8
#include <bits/stdc++.h> using namespace std; int main() { int n, res = 0; cin >> n; for (int i = 2; i < n; i++) res += i * (i + 1); cout << res; return 0; }
4
#include <bits/stdc++.h> using namespace std; string g[] = {"Anka", "Chapay", "Cleo", "Troll", "Dracul", "Snowy", "Hexadecimal"}; int main() { set<pair<string, string> > M; int n; scanf("%d", &n); string t1, t2; for (int i = 0; i < n; i++) { cin >> t1 >> t2 >> t2; M.insert(make_...
6
#include <bits/stdc++.h> using namespace std; int n, m; int a[1 << 22]; int flag[1 << 22], vis[1 << 22]; void dfs(int x) { vis[x] = 1; int tmp = (1 << n) - 1 - x; if (flag[x] && !vis[tmp]) dfs(tmp); for (int i = 0; i < n; i++) { int cnt = x ^ (1 << i); if (x & (1 << i) && !vis[cnt]) dfs(cnt); } } int ...
17
#include <bits/stdc++.h> using namespace std; int n, m; long long s[1000005], l[1000005], r[1000005]; long long ans = (1LL << 60); int main() { scanf("%d %d", &n, &m); for (int i = (1); i <= (n); i++) scanf("%lld", &s[i]); m = min(m, n); for (int i = (1); i <= (n); i++) { l[i] = l[i - 1]; if ((i - 1) % ...
12
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, res; int p[N], c[N]; vector<int> color; bool vis[N]; void get_res(int n, int d) { for (int i = 0; i < d; i++) { bool success = true; for (int j = i + d; j < n; j += d) if (color[i] != color[j]) { success = false; ...
14
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353, maxn = 3e5 + 1, inf = 1e9, alpha = 26, maxk = 20; long long t[maxn]; long long bp(long long a, long long n) { long long res = 1; while (n) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } retu...
11
#include <bits/stdc++.h> using namespace std; using namespace std; struct ins { int ans; ins() { ans = 1; } void read() {} void read1(char &s) { char c = getchar(); for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar()) ; s = c; if (c == EOF) ans = 0; } void read1(str...
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s1, s2; cin >> s1 >> s2; long long cnt = 0; long long l = s1.size(); for (long long i = 0; i < l; i++) { if (s1[i] != s2[i]) { cnt += 1; } } if (cnt % 2) cout << "imposs...
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:367772116") using namespace std; long long getsum(long long cur) { long long mn = 1; while (cur) { mn *= cur % 10; cur /= 10; } return mn; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >...
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000,100000000000") using namespace std; const double eps = 1E-9; const double Exp = 2.718281828459045; const double Pi = 3.1415926535897932; const int NMAX = 100000 + 5; const int MMAX = 300 + 5; const int INF = 1000000000; const int BASE = 1000000007; t...
15
#include <bits/stdc++.h> using namespace std; const int MAXN = 2001 * 2001; const int dx[5] = {0, 0, 1, -1}; const int dy[5] = {1, -1, 0, 0}; int main() { int n, m; cin >> n >> m; vector<string> A(n); vector<vector<int>> G(n * m, vector<int>(4, -1)); int clear = 0; for (int i = 0; i < n; ++i) cin >> A[i]; ...
12
#include <bits/stdc++.h> const int N = 257, p = 31, mod = 1e9 + 7; char tab[N][N]; long long hash[N][N]; long long lit[30]; int n, m; int wyst[30]; bool kk[N][N][N]; int man[N], manparz[N]; void init() { long long akt = 1; for (int i = 0; i < 30; i++) { lit[i] = akt; akt *= p; akt %= mod; } } void has...
16
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; template <typename _T> inline void read(_T &f) { f = 0; _T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fu = -1; c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + ...
18