solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; long long kk[2005]; int main() { int n; scanf("%d", &n); for (int i = 0; i < 2 * n; i++) scanf("%lld", &kk[i]); sort(kk, kk + 2 * n); long long s1, s2; for (int i = 0; i < n; i++) s1 += kk[i]; for (int i = n; i < 2 * n; i++) s2 += kk[i]; if (s1 != s2) { ...
2
#include <bits/stdc++.h> using namespace std; const int INF = 2000000005; int n; vector<vector<uint32_t>> Mult(const vector<vector<uint32_t>>& A, const vector<vector<uint32_t>>& B) { vector<vector<uint32_t>> C(2 * n, vector<uint32_t>(2 * n, INF)); for (int i = 0; i < 2 * n; i++) { ...
17
#include <bits/stdc++.h> using namespace std; #define LL long long #define PB push_back #define double long double const double eps=1e-10,MAXN=1e18; const int N=6e5+10,mod=1e9+7; int n,q; LL a[N]; LL s[N]; void solve(){ scanf("%d",&n); for(int i=0;i<61;i++)s[i]=0; for(int i=1;i<=n;i++){ scanf("%lld...
10
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T, class U> ostream& operator<<(ostream& o, const pair<T, U>& p) { o << "(" << p.first << "," << p.second << ")"; return o; } template <class T> ostream& operator<<(ostream& o, const vector<T>& v) { o << "["; for (T t : v) { ...
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cout.precision(10); cout << fixed; int n, m; cin >> n >> m; vector<vector<int> > adj(n, vector<int>()); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; --a; --b; adj[a].push_back(b); ...
13
#include <bits/stdc++.h> using namespace std; int b, d, c[300004], g[300005], n, m, i, j, ok, k = 1, maxi, a, r, ul; char s[100005], v[1000050], z; bool cmm(int a, int b) { if (a > b) return 1; return 0; } int main() { cin >> n >> m; cin.get(); cin.get(v + 1, n + 3); for (i = 1; i <= n; i++) { if (v[i -...
8
#include <bits/stdc++.h> using namespace std; static const int maxn = 2e5 + 5; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int tc; cin >> tc; for (int tcase = 1; tcase <= tc; tcase++) { int n; cin >> n; vector<vector<int> > store(n + 1); for (int i ...
4
#include <bits/stdc++.h> using namespace std; struct node { int l, r; node() {} node(int a, int b) { l = a, r = b; } }; const int N = 1000005 * 4; int n, k; node tm[1000005]; int cmp(node a, node b) { return a.l < b.l; } map<int, int> to_num; map<int, int> to_val; int tmp_num[N]; int tree[N]; int lowbit(int x) { ...
10
#include <bits/stdc++.h> using namespace std; bool jud[5]; int num[5]; string str1 = "Ya"; string str2 = "Tidak"; void JUDGE() { const int fuhao = num[1] + num[2]; if (num[1] != 0) { if (fuhao & 1) { if (num[2] != 0 || num[3] != 0) jud[1] = jud[2] = true; else jud[1] = true; } el...
5
#include <bits/stdc++.h> using namespace std; mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); template <typename T> inline void chkmax(T &x, T y) { if (x < y) x = y; } template <typename T> inline void chkmin(T &x, T y) { if (x > y) x = y; } inline int read() { int x = 0; char c = ...
17
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int x, long long int y) { return y == 0 ? x : gcd(y, x % y); } long long int lcm(long long int x, long long int y) { return x * y / gcd(x, y); } bool valid(long long int x, long long int mx, long long int mn) { return (mx + x) % (mn + x) ==...
10
#include <bits/stdc++.h> using namespace std; unsigned long long x, y, z; int main() { ios::sync_with_stdio(0); cin.tie(0); string ans; cin >> x >> y; if (y < 10) { ans = to_string(y); while (x != 1) { ans += to_string(y); x--; } cout << ans << endl; } else { if (x == 1) ...
2
#include <bits/stdc++.h> using namespace std; const long long INF = 1e12; const int MAXN = 1e5 + 1, MODM = 1e9 + 7, P = 1e6 + 3, MULT1 = 30, MULT2 = 239, LUCKY = 13, ELEVEN = 11, MAXM = 101, k = 200; int main() { ios_base::sync_with_stdio(0); vector<char> a(4), b(4); string s; cin >> s; a[0] = s[0],...
4
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; class segment_tree_add_multiply { vector<int> l, r; vector<long long> v, lzadd, lzmul; void unlazy(int vr) { v[vr * 2 + 1] = (((v[vr * 2 + 1] * lzmul[vr]) % mod) + (lzadd[vr] * 1ll * (r[vr * 2 + 1] - l[vr * 2 + 1] ...
17
#include <bits/stdc++.h> using namespace std; int main() { char str1[200001], str2[200001]; int i, j, res, num = 0; cin >> str1 >> str2; int len1 = strlen(str1); int len2 = strlen(str2); if (len1 >= len2) { j = len1 - 1; for (i = strlen(str2) - 1; i >= 0; i--) { if (str2[i] != str1[j]) { ...
1
#include <bits/stdc++.h> int f(const void* p, const void* q); using namespace std; typedef struct pp { double x1; double x2; double t; double p; } ss; ss a[100005]; int main(void) { int n, i, j, k, p, q; double x1, x2, y1, y2; while (scanf("%d", &n) != EOF) { scanf("%lf %lf", &x1, &x2); for (i = 0...
8
#include <bits/stdc++.h> using namespace std; const int inf = 2e9; const int MOD = 1e9 + 7; const int N = 0; int n; int main() { cin >> n; if (n % 2) printf("black"); else printf("white\n1 2"); }
9
#include <bits/stdc++.h> using namespace std; struct point { double x; double y; }; point temp; double dist(point p1, point p2) { return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)); } void foot(point a, point b, point c) { temp.x = (c.x * (b.y - a.y) - c.y * (b.x - a.x) + a.y * b.x - a.x * b.y) / ...
11
#include <bits/stdc++.h> using namespace std; template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { for (auto i = begin(v); i != end(v); i++) os << *i << (i == end(v) - 1 ? "" : " "); return os; } template <class T> istream& operator>>(istream& is, vector<T>& v) { for (auto i = begin(v); i...
16
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; void __print(long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) {...
2
#include <bits/stdc++.h> using namespace std; long long n, m, k; int main() { scanf("%I64d %I64d %I64d", &n, &m, &k); if (k + 2 > n + m) { printf("-1\n"); return 0; } long long sol = 0; long long x = min(n - 1, k); long long y = k - x; x++; y++; sol = ((long long)(n / x)) * ((long long)(m / y)...
9
#include <bits/stdc++.h> using namespace std; int main() { long long n, l, r, ql, qr, w[100100], mn, looper, accum = 0, currAccum = 0; cin >> n >> l >> r >> ql >> qr; for (int i = 0; i < n; ++i) { cin >> w[i]; accum += w[i]; } mn = accum * r; mn += (n - 1) * qr; for (int i = 1; i <= n; ++i) { ...
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long int t, n, i, j, x, k, sum = 0, res = 0, maxi = -1, mini = 1000000100; cin >> n; string s; cin >> s; long long int ch[26]; long long int dp[n + 10], dp2[n + 10]...
9
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long mpow(long long a, long long b, long long p = MOD) { a = a % p; long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % p; a = (a * a) % p; b = b >> 1LL; } return res % p; } const long long N = 1e5 + 2, M...
18
#include <bits/stdc++.h> using namespace std; char s[100010]; int main() { long long n, m, g = 1, i, j; scanf("%lld", &n); scanf("%lld", &m); scanf("%s", &s); for (i = 1; i < n; ++i) if (s[i] != s[i - 1]) g++; long long ans = g * n * (m - 1); long long cnt = 0; for (i = 0; i < n - 1; ++i) { if (...
19
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long power(long long x, long long y) { long long ans = 1; for (int i = 1; i <= y; i++) ans = ans * x % mod; return ans; } int main() { long long n, m, k; cin >> n >> m >> k; long long res; if (k > n || k == 1) res = p...
8
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int r = n % 10; int ans = (r - 1) * 10; if (n / 10 == 0) ans += 1; else if (n / 100 == 0) ans += 3; else if (n / 1000 == 0) ans += 6; else if (n / 10000 == 0) ans += 10; cout << ans << "\n"; } int main() { ...
0
#include <bits/stdc++.h> using namespace std; vector<int> sz[100001]; vector<int> sor; bool volt[100001]; int szint[100001]; int sk = 0; int n, m; vector<int> a, b; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; sz[x].push_back(y); sz[y].push_back(x); } for (...
7
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e5 + 5; map<long long, long long> mp; void solve() { long long n; cin >> n; long long a[2 * n]; long long count = 0; for (long long i = 0; i < 2 * n; i++) { cin >> a[i]; if (a[i] == 1) count--; else count++; } mp...
9
#include <bits/stdc++.h> using namespace std; vector<int> h[2010]; pair<int, pair<int, int> > v[2010 * 2010]; int R[2010], T[2010], d[2010][2010], n, viz[2010], t; long long a[2010][2010]; int find(int x) { int aux = x; while (x != T[x]) { x = T[x]; } int tata = x; x = aux; while (x != tata) { aux =...
11
#include <bits/stdc++.h> using namespace std; int main() { string num; cin >> num; int end = num.size(); while (num[end - 1] == '0' && end--) ; for (int i = 0, j = end - 1; i <= j; i++, j--) { if (num[i] != num[j]) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; retu...
1
#include <bits/stdc++.h> using namespace std; int ts, kk = 1, _DB = 1; inline int LEN(string a) { return a.length(); } inline int LEN(char a[]) { return strlen(a); } template <class T> inline T _abs(T n) { return (n < 0 ? -n : n); } template <class T> inline T _max(T a, T b) { return (a > b ? a : b); } template <cl...
14
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; int k0, k1; cin >> k0; vector<int> a(k0); for (int i = 0; i < k0; i++) cin >> a[i]; cin >> k1; vector<int> b(k1); for (int i = 0; i < k1; i++) cin >> b[i]; sort(a....
12
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); printf("%d\n", n / 3 * 2 + ((n % 3) ? 1 : 0)); return 0; }
0
#include <bits/stdc++.h> using namespace std; string q; int n, m = 0; int yi[1000001]; int main() { cin >> n >> q; for (int a = 0; a < q.size(); a++) if (q[a] == '1') yi[++m] = a + 1; if (n == 0) { long long we = 0; for (int a = 1; a <= m; a++) we += 1LL * ((yi[a] - yi[a - 1] - 1) + 1) * (yi[a] ...
8
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } ...
18
#include <bits/stdc++.h> using namespace std; inline bool func(string &s, int n, int ind) { int f[10] = {0}; for (int j = 0; j <= ind; j++) { f[s[j] - '0'] ^= 1; } vector<int> a; for (int j = 0; j < 10; j++) { if (f[j]) a.push_back(j); } int m = a.size(); if (n - ind - 1 < m) return false; for...
14
#include <bits/stdc++.h> using namespace std; using ll = long long; using pi = pair<int, int>; using pll = pair<ll, ll>; ll n, k, a, b; ll gcd(ll x, ll y) { return y == 0 ? x : gcd(y, x % y); } ll d_nearest(ll x) { return min(x % k, k - (x % k)); } int main() { ios_base::sync_with_stdio(false); cout << setprecision...
9
#include <bits/stdc++.h> using namespace std; int main() { map<string, string> mp; int n; string x, y; cin >> n; for (int i = 0; i < n; i++) { cin >> x >> y; if (mp.count(x) == false) { mp[y] = x; } else { mp[y] = mp[x]; mp.erase(x); } } cout << mp.size() << endl; for (...
3
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cer...
13
#include <bits/stdc++.h> using namespace std; vector<int> f(int k); int main() { int n, m; cin >> n >> m; vector<int> a(f(n)), b(f(m)); for (int i(0); i < n; i++) for (int j(0); j < m; j++) cout << a[i] * b[j] << (j == m - 1 ? '\n' : ' '); return 0; } vector<int> f(int k) { vector<int> ans(k); if (k =...
16
#include <bits/stdc++.h> using namespace std; long long pow2(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % 1000000007; a = (a * a) % 1000000007; b >>= 1; } return res; } int main() { long long n; cin >> n; cout << (1000000007 + pow2(27, n) - pow2(7...
7
#include <bits/stdc++.h> using namespace std; template <typename T> bool elem(set<T> st, T val) { return (st.find(val) != st.end()); } template <typename T> void printv(vector<T> vt) { if (vt.size() == 0) return; for (int i = 0; i < (int)vt.size() - 1; ++i) cout << vt[i] << " "; cout << vt[vt.size() - 1] << end...
8
#include <bits/stdc++.h> using namespace std; int n, k; string a[2005], at[2005]; int z[2005][2005], zt[2005][2005]; template <class T, class U> void solve(T a, U z) { for (int i = 0; i < n; i++) { int l = -1, r = -1; for (int j = 0; j < n; j++) { if (a[i][j] == 'B') { r = j; if (l == -1...
11
#include <bits/stdc++.h> using namespace std; int tc; int n; string s; struct node { vector<int> id; node *prv, *nxt, *rp; int pos; node(int key) { id.clear(); id.push_back(key); prv = nxt = rp = 0; pos = 0; } } *head = 0, *tail = 0, *cur = 0; int dp[1000005]; node *H[1000005]; node *T[1000005...
13
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 5; vector<int> adj[MAX]; int a[MAX]; int b[MAX]; long long w[MAX]; long long st[MAX]; long long dpmax[MAX]; long long dpmin[MAX]; long long sub[MAX]; int get(int ed, int v) { if (a[ed] == v) { return b[ed]; } else { return a[ed]; } } void...
18
#include <bits/stdc++.h> using namespace std; const int N = 52; const int INF = 0x3fffffff; int setx[N], num[N]; bool vis[N]; int find(int x) { if (setx[x] == x) return x; return setx[x] = find(setx[x]); } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { setx[i] = i; num[i] = 1; ...
7
#include <bits/stdc++.h> using namespace std; inline long long rd() { long long x = 0; int ch = getchar(), f = 1; while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar(); if (ch == '-') { f = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getch...
11
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; template <class S, class T> inline S smax(S &l, T r) { return l = max(l, (S)r);...
13
#include <bits/stdc++.h> using namespace std; int main() { int n, k, l; cin >> n >> k >> l; n /= 2; if (((k == n + 1) || (k == n)) && ((l == n + 1) || (l == n))) cout << "NO"; else cout << "YES"; }
4
#include <bits/stdc++.h> using namespace std; struct data { int i, j, val; }; int main() { data mx = {0, 0, 0}; int n, k, last, i, j, a, b, c, x, y; scanf("%d %d", &n, &k); char ara[n][n]; int ans[n][n]; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { ans[i][j] = 0; } } for (i = 0; i...
5
#include <bits/stdc++.h> int n; int m; int s; int e; std::vector<std::pair<int, int>> p; void read_data() { std::cin >> n >> m; s = 1; e = n - 1; for (int i = 0; i < m; ++i) { int a, b; std::cin >> a >> b; p.push_back(std::make_pair(std::min(a, b), std::max(a, b))); } } void calculate_and_print_re...
5
#include <bits/stdc++.h> using namespace std; int arr[109]; int n, k, x, asdaadaasddfffsqwe; struct cont { int c, n; cont() {} cont(int c, int n) : c(c), n(n) {} }; vector<cont> con; int main() { scanf("%d%d%d", &n, &k, &x); memset(arr, 0, sizeof(arr)); for (int i = 1; i <= n; i++) scanf("%d", &arr[i]); i...
6
#include <bits/stdc++.h> using namespace std; void solve() { int n, k1, k2, x = 0; cin >> n >> k1 >> k2; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < k1; i++) { if (a[i] == n) { cout << "YES" << endl; x = 1; break; } } if (x == 0) cout << "NO" <<...
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; int cnt = 0; string c = s; sort(s.begin(), s.end()); for (int j = 0; j < n; j++) { if (s[j] != c[j]) { cnt++; } } cout << cnt <...
0
#include <bits/stdc++.h> using namespace std; const int N = 300005; int X[N]; char col[N]; int n; long long int solve(int l, int r) { long long int push_back, pr; pr = push_back = -1; long long int res = 0; if (l == -1) { for (int i = 0; i < r + 1; i++) { if (col[i] == 'B' || i == r) { if (pus...
16
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k, i, j, p[105], s[105], h; cin >> n >> m >> k; for (i = 0; i < n; i++) { cin >> p[i]; } for (i = 0; i < n; i++) { cin >> s[i]; } int c = 0; for (i = 0; i < k; i++) { cin >> h; for (j = 0; j < n; j++) { if (s[j] =...
3
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,abm,mmx,tune=native") #pragma GCC optimize("fast-math") using namespace std; template <typename T> inline void uin(T &a, T b) { if (b < a) a = b; } t...
12
#include <bits/stdc++.h> using namespace std; int main() { long long int n, l, r, j; cin >> n >> l >> r; long long int m = n, ct = 0; if (n == 0) { cout << 0 << endl; return 0; } vector<long long int> v; while (m > 0) { v.push_back(m % 2); m = m / 2ll; } m = n; reverse(v.begin(), v.e...
8
#include <bits/stdc++.h> using namespace std; inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; } inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; } inline bool setmin(long long &x, long long y) { return (y < x) ? x = y, 1 : 0; } inline bool setmax(long long &x, long long y) { return ...
16
#include <bits/stdc++.h> using namespace std; int main(int argc, char *argv[]) { int N, K; scanf("%d%d", &N, &K); if (N < 4 || K == 1 || K > 3) printf("-1\n"); else if (K == 2) { if (N == 4) printf("-1\n"); else { printf("%d\n", N - 1); for (int i = 0; i < (N - 1); i++) printf("%d ...
16
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; string odd = ""; string even = ""; for (int i = 0; i < s.size(); i++) { if (s[i] % 2 == 0) even += s[i]; else odd += s[i]; } sort(ev...
10
#include <bits/stdc++.h> using namespace std; const int maxn = 4000 * 100 + 100; long long n, m, a[maxn], h[maxn], w[maxn], flag[maxn], mx[maxn]; void mmax(int node, int s, int e, int lo, int hi, long long val) { if (s > hi || e < lo) return; if (s <= lo && e >= hi) { flag[node] = max(flag[node], val); mx[n...
7
#include <bits/stdc++.h> const long long inf = 9e18; inline int read() { char c = getchar(); int x = 0, ft = 1; for (; c < '0' || c > '9'; c = getchar()) if (c == '-') ft = -1; for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - '0'; return x * ft; } const int N = 2e3 + 5; const int M = 1e5 + 5; ...
21
#include <bits/stdc++.h> using namespace std; const int mod = 1000003; struct A { char a[500005]; int n, m; char& at(const int& x, const int& y) { return a[x * m + y]; } } ma; int main() { int i, j, n, m; scanf("%d%d", &n, &m); ma.n = n; ma.m = m; char str[500005]; for (i = 0; i < n; ++i) { scanf(...
14
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; char ch = getchar(); bool positive = 1; for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') positive = 0; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; return positive ? x : -x; } int N; double a...
16
#include <bits/stdc++.h> using namespace std; #pragma GCC target("avx,avx2,fma") #pragma GCC optimize("unroll-loops") struct Update { int type, c, h; }; const int NN = 4005; int dp[NN][NN]; struct Knapsack { int cur = 0; Knapsack() { cur = 0; memset(dp[0], 0, sizeof dp[0]); } void addUpdate(Update u) ...
19
#include <bits/stdc++.h> using namespace std; int limit[3]; int cut[3]; int k; int main() { cin >> limit[0] >> limit[1] >> limit[2]; cin >> k; sort(limit, limit + 3); limit[0]--; limit[1]--; limit[2]--; if (k >= limit[0] + limit[1] + limit[2]) { long long res = limit[0] + 1; res *= limit[1] + 1; ...
8
#include <bits/stdc++.h> using namespace std; const int maxn = 2000 + 7; int a[maxn], b[maxn]; bool vis[maxn]; set<int> ans; int main() { int n, k; scanf("%d %d", &k, &n); for (int i = 0; i < k; i++) scanf("%d", a + i); for (int i = 0; i < n; i++) scanf("%d", b + i); sort(b, b + n); for (int i = 0; i < k; i...
9
#include <bits/stdc++.h> using namespace std; long long t; long long fd(long long n, long long k) { if (!n) return k * (k + 1) / 2; long long v = fd(n / k, k), o = n % k; return k * (k + 1) / 2 - o + max(0ll, min(k, (o + 1) * k - v + 1)); } signed main() { cin >> t; while (t--) { long long n, k; cin >...
26
#include <bits/stdc++.h> using namespace std; const int MAXN = (1 << 10); template <class T> struct fenwick { int sz; T tr[MAXN]; void init(int n) { sz = n + 1; memset(tr, 0, sizeof(tr)); } T query(int idx) { T ans = 0; for (; idx >= 1; idx -= (idx & -idx)) ans ^= tr[idx]; return ans; } ...
17
#include <bits/stdc++.h> using namespace std; typedef int64_t ll; typedef uint64_t ull; char s[100001]; bool Check(ll day_cnt, const vector<int> &sumwindx, const vector<int> &sumwindy, const int tdx, int tdy) { int n = sumwindx.size(); ll cycle_cnt = day_cnt / n; ll dx = sumwindx.back() * cycle_cnt; ...
11
#include <bits/stdc++.h> using namespace std; char a[100005]; int cnt[1000] = {}; int main() { cin >> a; int n = strlen(a), tot = 0; for (int i = 0; i < n; i++) if (cnt[a[i]]++ == 0) tot++; if (tot > 4) return 0 * printf("No\n"); if (tot == 4) return 0 * printf("Yes\n"); if (tot == 3) { bool flag = ...
6
#include <bits/stdc++.h> using namespace std; long long c[200001]; long long n, k, a1, b1, q; class minTree { long long a, b; long long maxSum; long long minSum; minTree* left; minTree* right; public: minTree(long long a, long long b) { this->a = a; this->b = b; if (a != b) { left = new ...
9
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); double x[n], y[n]; for (int i = 0; i < n; i++) scanf("%lf %lf", &x[i], &y[i]); for (int i = 0; i < n; i++) if (((y[0] > 0) && (y[i] < 0)) || ((y[0] < 0) && (y[i] > 0))) { cout << -1 << endl; return 0; } fo...
14
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 9; int n, m, a[N]; long long ans = 0; map<pair<int, int>, int> mp; int main() { scanf("%d", &n); for (int i = (1); i <= (n); i++) { scanf("%d", &a[i]); ans += a[i]; } scanf("%d", &m); for (int i = (1); i <= (m); i++) { int x, y, z; ...
13
#include <bits/stdc++.h> using namespace std; const int N = 1e4 + 10; int dp[N][5], n; set<string> ans; string s; int main() { cin >> s; n = s.size(); s = '.' + s; if (n <= 6) puts("0"); else { dp[n + 1][2] = dp[n + 1][3] = 1; for (int i = n - 1; i > 5; i--) { if (i <= n - 3) dp[i][2...
10
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double pi = acos(-1.0); int dcmp(double x) { if (fabs(x) <= eps) return 0; return (x > 0) ? 1 : -1; }; void umax(int &a, int b) { a = max(a, b); } void umin(int &a, int b) { a = min(a, b); } void file() { freopen("a.in", "r", stdin); } nam...
10
#include <bits/stdc++.h> using namespace std; namespace SGT { double val[400010 << 2], tag[400010 << 2]; void set_tag(int u, double v) { val[u] += v; tag[u] += v; } void push_down(int u) { if (tag[u]) set_tag(u << 1, tag[u]), set_tag(u << 1 | 1, tag[u]); tag[u] = 0; } void insert(int u, int l, int r, int L, int...
25
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast", "unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const long long inf = numeric_limits<long long>::max() / 2; const long double eps = 1e-9; const long doubl...
17
#include <bits/stdc++.h> using namespace std; const int N = 100100, MOD = 1e9 + 7; int n, m, nxt[N], dp[N][2]; int F[N], PI[N]; string s, t; int failure(char c, int l) { while (l > 0 && c != t[l]) { l = F[l - 1]; } return l + (c == t[l]); } void KMP() { for (int i = 1, l = 0; t[i]; ++i) { l = F[i] = fai...
12
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); char level[100 + 1]; scanf("%s", level); int a, d; for (a = 0; a <= n - 5; a++) { for (d = 1; d <= (n - 1) / 4; d++) { if (a + 4 * d < n) { if (level[a] == '*' && level[a + d] == '*' && level[a + 2 * d] == '*' && lev...
5
#include <bits/stdc++.h> using namespace std; int a[1000], b[1000], c[1000], d[1000]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> c[i] >> d[i]; int best = -1; for (int i = 1; i <= n; i++) { int j; for (j = 1; j <= n; j++) if (a[j] > a[i] and b[j] > b[i] and ...
2
#include <bits/stdc++.h> using namespace std; int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(10); int N; cin >> N; vector<long long> A(N); long long X0 = 0; for (int i = 0; i < N; i++) { long long a, b; cin >> a >> b; A[i] = a ^ b; X0 = X0 ^ a; } vecto...
16
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const long long INF = 1e18; const int MX = 100001; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { long long a; cin >> a; if (!a) { printf("1\n"); continue; ...
4
#include <bits/stdc++.h> using namespace std; int main() { char f[100005], h[100005]; scanf("%s", f); scanf("%s", h); int x = strlen(f); int y = strlen(h); if (x != y) printf("NO\n"); else { int i, j, c = 0, g = 0; char k, d, m, n; for (i = 0; i < x; i++) { if (f[i] != h[i]) { ...
3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; long long a[N], b[N]; long long T, n, m, t, l, r, ans; bool check(long long x, long long md) { long long tim = 0, now = 0, tot = 0, cnt = 0; for (int i = 1; i <= n; i++) if (b[i] <= x) { tot++, cnt++, tim += b[i], now += b[i]; if ...
13
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 9, mod = 1e9 + 7, Mod = mod; int n, m; int ar[N]; struct D { long long s[6]; }; struct node { int l, r; long long lz; D it; long long num; } t[N << 2]; long long tri[10][10]; long long sum[N][6], pi[N][6]; void pre() { tri[0][0] = 1; for (i...
17
#include <bits/stdc++.h> using namespace std; const int MAXN = 15, MOD = 1000000007; int n, m, k, c[MAXN][MAXN], ans; int use[MAXN], now[MAXN][MAXN]; inline int bit(int x) { return 1 << (x - 1); } int dfs(int x, int y) { if (y > m) y = 1, x++; if (x == n + 1) return 1; now[x][y] = now[x - 1][y] | now[x][y - 1]; ...
19
#include <bits/stdc++.h> using namespace std; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } const int64_t INF = 1 << 29; inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int& n, int b) { n |= two(b); } inline void unset_bit(int& n, i...
5
#include <bits/stdc++.h> const int MAXN = 1e5 + 7; int n, k; long long f[MAXN][30], sum; int t[MAXN], a[MAXN], L = 1, R; inline void add(int x) { sum += t[x]; t[x]++; } inline void del(int x) { --t[x]; sum -= t[x]; } void calc(int l, int r) { while (L > l) add(a[--L]); while (L < l) del(a[L++]); while (R ...
17
#include <bits/stdc++.h> using namespace std; vector<long long> G[100002]; long long v[100002]; long long pl[100002]; long long minu[100002]; void dfs(long long i, long long par) { long long p = 0; long long m = 0; for (long long j = 0; j < G[i].size(); j++) { long long v = G[i][j]; if (v != par) { ...
10
#include <bits/stdc++.h> using namespace std; map<int, int> A, B; map<int, bool> K; int main() { int n, m, k; cin >> n >> m >> k; int a; for (int i = 0; i < n; i++) { cin >> a; A[a]++; K[a] = true; } for (int i = 0; i < m; i++) { cin >> a; B[a]++; K[a] = true; } vector<int> v; ...
8
#include <bits/stdc++.h> using namespace std; map<char, int> mp; int vis[10010]; string s; int main() { char c; for (int i = 0; i <= 9; i++) { c = i + '0'; mp[c] = i; } int num = 0; for (int i = 10; i <= 35; i++) { c = num + 'A'; mp[c] = i; num++; } num = 0; for (int i = 36; i <= 61;...
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 410; const int INF = 1 << 30; int L[MAXN], C[MAXN]; unordered_map<int, int> dp[MAXN]; int gcd(int a, int b) { int t; while (b) { t = a % b; a = b; b = t; } return a; } vector<pair<int, int> > fact(int x) { vector<pair<int, int> > facts...
11
#include <bits/stdc++.h> using namespace std; long long Eular(long long n) { long long ret = n; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { ret -= ret / i; while (n % i == 0) n /= i; } } if (n > 1) ret -= ret / n; return ret; } const int maxn = 1e6; int phi[maxn + 5]; void i...
13
#include <bits/stdc++.h> using namespace std; int main() { long long m, n, t, ln, lm, ans = -1; cin >> n >> m; for (long long i = 0; i <= m; i++) { t = i; ln = n - 2 * i; lm = m - i; if (ln >= 0) { t += min(lm / 2, ln); ans = max(ans, t); } } cout << ans << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; struct Node { int l, r, mx, lw; }; int H, N, M; Node seg[300005]; long long MOD = 1e9 + 7; stack<pair<int, int>> stk[100005]; vector<pair<pair<int, int>, pair<int, int>>> qu; long long add(long long a, long long b) { return (a + b) % MOD; } void build(int l, int r, int id...
19
#include <bits/stdc++.h> using namespace std; const long long N = 100005; const long long M = 200005; const long long MOD = 1e9 + 7; long long n, m; struct qq { long long x, y, z, last; } e[M * 2]; long long num, last[N]; void init(long long x, long long y, long long z) { num++; e[num].x = x; e[num].y = y; e[...
18
#include <bits/stdc++.h> using namespace std; using namespace chrono; template <class T1, class T2> istream &operator>>(istream &in, pair<T1, T2> &P) { in >> P.first >> P.second; return in; } template <class T> istream &operator>>(istream &in, vector<T> &arr) { for (auto &x : arr) in >> x; return in; } template...
17