solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; namespace IO { template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(long long &x) { scan...
20
#include <bits/stdc++.h> using namespace std; long long n, q, x; int main() { cin >> n >> q; while (q--) { cin >> x; if (x % 2 == 1) cout << (x + 1) / 2 << endl; else { long long k = n + x / 2; while (k % 2 == 0) { k = n + k / 2; } cout << (k + 1) / 2 << endl; }...
9
#include <bits/stdc++.h> using namespace std; void solve() { long long int a; cin >> a; long long int m = a; for (long long int i = 2; i * i <= a; i++) { if (a % i == 0) { m = i; break; } } long long int p = a / m; cout << p << ' ' << (m - 1) * p << endl; } int main() { ios::sync_wit...
5
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e6 + 10; long long raw[maxn]; vector<long long> sufa, sufb; signed main() { ios::sync_with_stdio(0); cin.tie(0); long long n, d, m; cin >> n >> d >> m; for (long long i = 1; i <= n; i++) { cin >> raw[i]; } sufb.push_back(0); sort(...
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000010; int h[MAXN], ls[MAXN], a[MAXN]; int solve(int n, long long b) { int ptr = n - 1; long long sum = 0; for (int i = n - 1; i >= 0; i--) { sum += a[i]; while (sum > b) { sum -= a[ptr]; ptr--; } if (ptr + 1 == n) { ...
16
#include <bits/stdc++.h> using namespace std; int M; int dp[405][405]; void add(int &a, int b) { a += b; if (a >= M) { a -= M; } } int mul(long long a, int b) { return (int)((a * b) % M); } int modpow(long long base) { long long answer = 1; int p = M - 2; while (p) { ...
14
#include <bits/stdc++.h> using namespace std; double a, b, c, ans; int xceptor() { cin >> a >> b >> c; ans = ((a - b) * c) / b; cout << (long long)(ceil(ans * 1LL)) << endl; return 0; } int main() { ios::sync_with_stdio(0); cin.tie(0); return xceptor(); }
2
#include <bits/stdc++.h> using namespace std; char s[100005]; int len, nlen, olen; int k, v[100005], nv[100005]; void func(int step) { long long tot = 0, cnt = 0; for (int i = 0; i < 26; i++) { tot = 0; cnt = 0; nlen = 0; for (int j = 0; j < len; j++) if (v[j] + step < olen && s[v[j] + step] -...
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n], sum = 0; for (int i = 0; i < n; i++) cin >> a[i], sum += a[i]; if (sum == 0) { ...
1
#include <bits/stdc++.h> using namespace std; double sum; int Check(double t1, double g, double r) { double on = 1, tw; sum = 0.00; int ans; for (;;) { if (on) { sum += g; on = 0; ans = 1; } else { sum += r; on = 1; ans = 2; } if (sum > t1) return ans; } } i...
7
#include <bits/stdc++.h> using namespace std; int n, i; int a[1002]; int flag; int main() { int c[1002]; cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; } for (i = 1; i <= n; i++) { flag = 0; c[0] = i; c[1] = a[i]; for (int j = 2;; j++) { c[j] = a[c[j - 1]]; for (int k = 0; k...
2
#include <bits/stdc++.h> using namespace std; int n1, n2, n3, a, b, c, ans; int main() { scanf("%d %d %d", &n1, &n2, &n3); b = sqrt((n1 * n3) / n2); a = n1 / b; c = n2 / a; printf("%d\n", 4 * (a + b + c)); return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m; long long f[2][10100], a[10100], b[10100], res = 0x3f3f3f3f3f3f3f3f; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); memset(f, 0x3f, sizeof(f)), f[0][0] = 0; for (int i...
21
#include <bits/stdc++.h> using namespace std; void solve() { long long n, s; cin >> n >> s; if (n == 1) { cout << s; return; } if (s == 1) { cout << 0; return; } cout << s / (n / 2 + 1); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while ...
0
#include <bits/stdc++.h> using namespace std; const int N = 150; int a, b, c, n, m; int root[N]; void make_set(int vertex) { root[vertex] = vertex; } int find_set(int vertex) { if (root[vertex] != vertex) root[vertex] = find_set(root[vertex]); return root[vertex]; } void union_set(int vertex1, int vertex2) { int ...
7
#include <bits/stdc++.h> using namespace std; const int N = 55; int read() { int s = 0; char c = getchar(), lc = '+'; while (c < '0' || '9' < c) lc = c, c = getchar(); while ('0' <= c && c <= '9') s = s * 10 + c - '0', c = getchar(); return lc == '-' ? -s : s; } void write(int x) { if (x < 0) { putchar(...
5
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; long long fastpower(long long x, long long n, long long M) { if (n == 0) return 1; else if (n % 2 == 0) return fastpower((x * x) % M, n / 2, M); else return (x * fastpower((x * x) % M, (n - 1) / 2, M)) % M; } long long GCD(long long ...
8
#include <bits/stdc++.h> using namespace std; bool adj[702][702]; int mid[702][702]; int deg[702]; int m, n; long long sum = 0; bool notEq(int i, int j, int k) { return i != j && i != k && j != k; } int main() { scanf("%d %d", &n, &m); for (int i = 0, a, b; i < m; ++i) { scanf("%d %d", &a, &b); --a, --b; ...
16
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; const int MAXN = 50005; int H[MAXN], pre[MAXN], post[MAXN]; vector<int> ch[MAXN]; int pn; void go(int x) { pre[x] = pn++; for (int y : ch[x]) { H[y] = H[x] + 1; go(y); } post[x] = pn; } int SA[...
20
#include <bits/stdc++.h> using namespace std; string s; vector<int> a, b; vector<int> r; void carp(vector<int> &a, vector<int> &b) { for (long long int i = 0; i <= a.size() - 1; i++) r[i] = a[b[i]]; } vector<int> fe(vector<int> &a, int b) { vector<int> now = a, res; int ok = 0; while (b) { if (b & 1) { ...
18
#include <bits/stdc++.h> using namespace std; int a[100005], b[100005]; int main(void) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(b, b + n); int ok = 1; for (int i = 0; i < n; i++) if (a[i] != b[i]) { ok = 0; break; } if (o...
11
#include <bits/stdc++.h> using namespace std; int level[2007]; int pos[2007]; int n, k, sum; map<int, int> res; int main() { scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", &level[i]); pos[i] = level[i]; } sort(level, level + n); for (int i = n - k; i < n; i++) { sum += level[i]; ...
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; double p[maxn], s[maxn], ans; vector<int> V[maxn]; int fa[maxn]; void dfs(int u) { ans += p[fa[u]] * (1.0 - p[u]); for (int i = 0; i < V[u].size(); i++) { if (V[u][i] == fa[u]) continue; fa[V[u][i]] = u; dfs(V[u][i]); s[u] += 1...
14
#include <bits/stdc++.h> const int MN = 2000; const int S = 0; const int T = 1001; 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 ...
23
#include <bits/stdc++.h> #pragma GCC optimize("-Ofast", "-funroll-all-loops") using namespace std; const int N = 1e5 + 10; int n, m, a[N], vis[N][2]; vector<int> g[N][2]; void bfs(int id, int k) { queue<int> q; for (int i = 1; i <= n; i++) if (a[i] == id) vis[i][k] = 1, q.push(i); while (q.size()) { int u...
9
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; bool use[110][110]; int e[2010]; vector<int> ans; vector<int> a[100100]; int cnt = 0, n, m; vector<pair<int, int>> help; void dfs(int v) { for (int i = 0; i < a[v].size(); i++) { if (use[v][a[v][i]]) continue; use[a[v][i]...
15
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) { cerr << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << ", "; err(++it, args...); } void solve() { int n, r; cin >> n >> r; int n2 = 1 <...
17
#include <bits/stdc++.h> using namespace std; double v, r, s, f; const double pi = 3.1415926535897932384626433832795; const double eps = 1e-15; int n; int main() { scanf("%d %lf %lf", &n, &r, &v); while (n--) { scanf("%lf %lf", &s, &f); double dis = f - s; double yu = fmod(dis, 2 * pi * r); int t = ...
17
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1E9 + 7; const long double PI = acosl(-1.0); const int MIRO[10] = {0, 1, 5, -1, -1, 2, -1, -1, 8, -1}; long long ceil_div(long long index_one, long long index_two) { return (index_one + index_two - 1) / index_two; } long long bin_pow(long long base, l...
2
#include <bits/stdc++.h> using ll = long long; const int maxn = 4.1e5 + 3; struct table { ll delta; std::map<int, ll> t; std::multiset<ll> vs; void debug() { printf("%lld| ", delta); for(auto &&[k, v] : t) { printf("%d:%lld| ", k, v); } for(auto &&v : vs) { ...
21
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; vector<pair<int, int>> t[N]; int s[N]; long long g[N], f[N]; vector<int> q; void dfs(int x, int y) { s[x] = 1; for (auto i : t[x]) { if (y == i.first) continue; g[i.first] = i.second; dfs(i.first, x); s[x] += s[i.first]; g[x] +...
15
#include <bits/stdc++.h> using namespace std; int per[250000]; bool visited[250000]; void dfs(int s) { visited[s] = true; if (!visited[per[s]]) dfs(per[s]); } int main() { int n; cin >> n; for (long long i = 1; i < n + 1; i++) cin >> per[i]; bool b[n + 1]; for (long long i = 1; i < n + 1; i++) cin >> b[i]...
9
#include <bits/stdc++.h> int n; char s[105]; int cnt[105]; int main() { scanf("%d", &n); scanf("%s", &s[1]); for (int i = 1; i <= n; i++) { cnt[i] = cnt[i - 1] + (s[i] == '1' ? 1 : -1); } if (cnt[n] != 0) { printf("1\n"); printf("%s", &s[1]); } else { printf("2\n"); printf("%c %s", s[1],...
0
#include <bits/stdc++.h> using namespace std; long long p, k, a[100]; void DFS(int x) { if (x == 70) return; a[x] = 1 - a[x - 1] / k; if (0 <= a[x - 1] + k * a[x] && a[x - 1] + k * a[x] < k) { if (a[x] > 0 && a[x] < k) { printf("%d\n", x + 1); for (int i = 1; i <= x; i++) printf("%I64d ", a[i] * k...
12
#include <bits/stdc++.h> using namespace std; int n, k, cnt[40], Pow[40], dem, dem1; int main() { cin >> n >> k; if (k > n) { cout << "NO"; return 0; } while (n > 0) { if (n % 2 == 1) { cnt[dem1]++; dem++; } dem1++; n /= 2; } if (dem > k) { cout << "NO"; return 0;...
6
#include <bits/stdc++.h> const int inf_int = 2e9; const long long inf_ll = 2e18; const int maxn = 5e2 + 10; using namespace std; int main() { int n, l, f[30], f2[105], k = 1, fff; memset(f, 0, sizeof(f)); char s[105]; cin >> n; getchar(); gets(s); l = strlen(s); if (l % 2 != 0) fff = l / 2 + 1; el...
8
#include <bits/stdc++.h> using namespace std; int dp[110][1100], a[110], pref[110], n; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { char c; cin >> c; a[i] = c - '0'; pref[i] = pref[i - 1] + a[i]; } for (int i = 1; i < n; i++) ...
5
#include <bits/stdc++.h> using namespace std; const int N = 123456; int a[N]; map<int, int> s; vector<pair<int, int>> ans; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; int f = 1; for (int i = 1; i <= n; ++i) { cin >> a[i]; s[a[i]]++; while (s.rbegin()->fir...
11
#include <bits/stdc++.h> using namespace std; int m, n, x[100][100], b[100][100], z[100][100]; void coverRow(int r) { for (int i = 0; i < n; i++) z[r][i] = 1; } void coverCol(int c) { for (int i = 0; i < m; i++) z[i][c] = 1; } bool checkRow(int r) { for (int i = 0; i < n; i++) if (x[r][i] == 0) return false; ...
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; int n, m; int a[1000007]; int b[1000007]; int c[1000007]; int Next[1000007]; char s[1000007]; char t[1000007]; void mul(int a[], int b[]) { for (int i = 0; i < n; i++) c[i] = a[b[i]]; for (int i = 0; i < n; i++) a[i] = c[i]; }...
18
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int t, n, a, b, u, v, par[N], branch[N], depth[N]; vector<int> adj[N], Q[N]; void findpivots(int x, int p) { Q[x].assign(3, 0); branch[x] = 0; for (int y : adj[x]) { if (y != p) { depth[y] = 1 + depth[x]; findpivots(y, x); ...
22
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 10; const int mod = 1e9 + 7; int N, M, Q, K; int A[110]; int main() { int i, j, k; scanf("%d", &N); for (i = 1; i <= N / 2; ++i) { scanf("%d", &A[i]); } int cnt = 0; sort(A + 1, A + 1 + N / 2); int mini = 0x3f3f3f3f; for (i = 1; i <...
3
#include <bits/stdc++.h> #pragma GCC optimize(3, "Ofast", "inline") using namespace std; bool Finish_read; template <class T> inline void read(T &x) { Finish_read = 0; x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; if (ch == EOF) return; ch = getchar(); } ...
16
#include <bits/stdc++.h> using namespace std; const int maxn = 100500; const int inf = 2e9; const double eps = 1e-8; const int base = 1e6 + 3; int bin(int c, long long st) { if (st == 0LL) return 1; int cnt = bin(c, st / 2LL); if (st & 1LL) return (1LL * c * cnt * cnt) % base; return (1LL * cnt * cnt) % base; }...
15
#include <bits/stdc++.h> using namespace std; template <typename T> inline T GCD(T a, T b) { a = abs(a); b = abs(b); while (b) { a = a % b; swap(a, b); } return a; } template <typename T> inline T LCM(T x, T y) { T tp = GCD(x, y); if ((x / tp) * 1. * y > 9e18) return 9e18; return (x / tp) * y; }...
3
#include <bits/stdc++.h> using namespace std; long long int n, k, a, b; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int dist(long long int st, long long int en) { if (en >= st) return (n * k) / gcd(n * k, en - st); else return (n * k) / g...
9
#include <bits/stdc++.h> using namespace std; inline string GetString() { string res; char st[1000005]; scanf("%s", st); res = st; return res; } inline string GetSent() { string res; char st[1000005]; gets(st); res = st; return res; } template <typename t> t getnum() { t res = 0; char c; int s...
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); long long int n, m; cin >> n >> m; long long int arr[n]; long long int sum = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; sum += arr[i]; } if (sum < m) { cout << "-1"; return...
9
#include <bits/stdc++.h> using namespace std; int h, w; char buffer[25000000]; int cur = 0; int map[2000][2000]; int core[2000][2000]; int sun[2000][2000]; int cnt[101010]; int next_map[2000][2000]; const int dirx[] = {0, 0, 1, -1}; const int diry[] = {1, -1, 0, 0}; int cnt_sun = 0; void del_ray(int time) { for (int ...
14
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:336777216") using namespace std; int IT_MAX = 1 << 17; const long long MOD = 1000000007; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3f; const double PI = acos(-1); const double ERR = 1e-10; long long dp[40...
20
#include <bits/stdc++.h> using namespace std; const long long MX = 100000; const long long MOD = 1000000007; const int inf = 222222222; long long fast_exp(long long base, long long exp, long long mod) { long long res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base) % mod; base = (base * base) % m...
6
#include <bits/stdc++.h> using namespace std; inline void read(long long &x) { x = 0; char ch = getchar(); long long pd = 1; while (ch < '0' || ch > '9') { if (ch == '-') { pd = -pd; } ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } x...
24
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 11; int a[N]; int v[N * 4], k[N * 4], w[N * 4]; void push(int i) { w[i + i] += w[i]; w[i + i + 1] += w[i]; v[i + i] += w[i]; v[i + i + 1] += w[i]; w[i] = 0; } void update(int i, int l, int r, int tl, int tr, int x) { if (tl <= l && r <= tr) {...
22
#include <bits/stdc++.h> using namespace std; string str; struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; void solve() { int m, n, a, b, c; cin >> n; ...
7
#include <bits/stdc++.h> int orange[1000], col = 1; int main() { int n, k, i, j, record[40], m, l; while (scanf("%d %d", &n, &k) != EOF) { m = n * k; for (i = 0; i < k; i++) { scanf("%d", &record[i]); orange[record[i]] = col; } l = 1; for (i = 0; i < k; i++) { printf("%d", reco...
1
#include <bits/stdc++.h> using namespace std; int main() { long long int i, n, t, s; s = 0; cin >> t; while (t--) { cin >> n; s = n * (n + 1) / 2; long long int pow = 1; while (pow <= n) { s -= pow * 2; pow *= 2; } cout << s << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; template <class _T> inline void ina(_T a[], int n) { int i; for (i = 0; i < n; i++) cin >> a[i]; } template <class _T> inline void outa(_T a[], int n) { int i; for (i = 0; i < n - 1; i++) cout << a[i], cout << " "; if (i) printf("%d", i); cout << "\n"; } const d...
4
#include <bits/stdc++.h> using namespace std; struct grr { int x, y, z; }; bool foo(grr a, grr b) { return a.y < b.y; } int N, M, k = 0, k1 = 0, tn = 0, pos = 0, res[100023], k2 = 0; grr a[100023], b[100023]; int main() { scanf("%d%d", &N, &M); for (int i = 0; i < N; i++) { scanf("%d%d", &a[i].x, &a[i].y); ...
7
#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; } template <typename T, typename U> inline ostream &operator<<(ostream &_out, const pair<T, ...
12
#include <bits/stdc++.h> using namespace std; struct INT { int w; }; bool operator<(INT a, INT b) { return a.w > b.w; } int main() { int w, h, n; while (~scanf("%d%d%d", &w, &h, &n)) { getchar(); multiset<INT> W, H; multiset<INT> WW, HW; INT a; a.w = 0; W.insert(a); a.w = w; W.inse...
7
#include <bits/stdc++.h> using namespace std; const int N = 1050; int a[N]; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int cnt = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] == 1) cnt++; } if (n == 2 && (a[1] == 0 || a[2] == 0)) { cout << 1 <<...
3
#include <bits/stdc++.h> using namespace std; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '{'; for (int i = (0), __i = (((int)(vec).size())); i < __i; i++) { os << vec[i]; if (i + 1 != ((int)(vec).size())) os << ','; } os << '}'; return os; } template <class T1, c...
13
#include <bits/stdc++.h> using namespace std; int main() { long long n, x, y, v, w; cin >> x >> y >> v >> w; cin >> n; int cnt = 0; for (int i = 0; i < n; i++) { long long a, b, c; scanf("%lld%lld%lld", &a, &b, &c); if (a * x + b * y + c < 0 and a * v + b * w + c > 0) cnt++; else if (a *...
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<vector<int> > tree(n); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; tree[a].push_back(b); tree[b].push_back(a); } ve...
11
#include <bits/stdc++.h> using namespace std; ifstream in("in.txt"); ofstream out("out.txt"); int ha[100000]; int main() { memset(ha, -1, 400000); int h, n, m, ans = 0; map<int, int> A; cin >> h >> m >> n; for (int i = 0; i < n; i++) { char t; cin >> t; if (t == '+') { int v, d, kl, j = 0; ...
8
#include <bits/stdc++.h> using namespace std; template <class T> class IsIterable__ { static void f(...); template <class U> static typename U::const_iterator f(const U &); public: const static bool value = !std::is_same<void, decltype(f(std::declval<T>()))>::value; }; template <class F, class S> ostrea...
23
#include <bits/stdc++.h> using namespace std; long long M = 1000000007; void fast() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long power(long long base, long long n) { if (n == 0) return 1; if (n == 1) return base; long long halfn = power(base, n / 2); if (n % 2 == 0) return (hal...
3
#include <bits/stdc++.h> using namespace std; unsigned long long b[1000000]; int main() { ios::sync_with_stdio(false); unsigned long long n, m, a; unsigned long long ans = 0; unsigned long long int q = 0; cin >> n >> m; for (int i = 0; i < 2 * m; i++) { cin >> a; b[a]++; } q = (n * (n - 1) * (n ...
11
#include <bits/stdc++.h> using namespace std; long long int DP1[1000000][2]; long long int DP2[1000000][2]; int n; long long int k; long long int compute1(int i, int j) { if (DP1[i][j] != -1) return DP1[i][j]; if (j == 0) { DP1[i][j] = compute1(i - 1, 0) * (k - 2) + compute1(i - 1, 1) * (k - 1); DP1[i][j] %...
14
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-5; const long double PI = acos(-1); const int INF = (int)1e9; const long long INFF = (long long)1e18; const int mod = (int)1e9 + 7; const int MXN = (int)2e5 + 7; char s1[MXN], s2[MXN], s3[MXN]; int fail[MXN]; int l1, l2, l3; vector<int> st; int Z[...
18
#include <bits/stdc++.h> using namespace std; const int MAX = 5e3 + 9, INF = 1e9 + 9; int n, a, b, root, dp[MAX][MAX], sz[MAX], dp2[MAX]; vector<int> g[MAX]; void dfs(int v, int p) { dp[v][0] = 0; for (auto u : g[v]) if (u != p) { dfs(u, v); for (int i = 0; i <= sz[v] + sz[u]; i++) dp2[i] = INF; ...
16
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, m, x = 1, y = 1; cin >> n; cout << n / 2 + 1 << "\n"; for (long long i = 0; i < n; i++) { cout << x << " " << y << "\n"; ++(i % 2 ? x : y); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long n; while (cin >> n) { long long x, y; cin >> x; long long a[n + 1]; memset(a, 0, sizeof a); for (long long i = 1; i < n; i++) { cin >> y; a[i - 1] = abs(y - x); x = y; } long long ans = 0LL; long l...
8
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 5; const long long inf = 0x3f3f3f3f3f; long long n, a[maxn], dp[maxn][3], x; int main() { cin >> n >> x; for (int i = 1; i <= n; i++) cin >> a[i]; long long ans = -inf; for (int i = 1; i <= n; i++) { dp[i][0] = max(0LL, dp[i - 1][0] + a[i]...
11
#include <bits/stdc++.h> using namespace std; int main() { long a, b, x, n, m; vector<long> v1, v2; cin >> a >> b; cin >> n >> m; for (long i = 0; i < a; i++) { cin >> x; v1.push_back(x); } for (long i = 0; i < b; i++) { cin >> x; v2.push_back(x); } if (v1[n - 1] < v2[b - m]) { cou...
1
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; const int mod = 998244353; const long long INF = 1e18; map<long long, long long> mp; void exgcd(long long a, long long b, long long& d, long long& x, long long& y) { if (!b) d = a, x = 1, y = 0; else exgcd(b, a % b, d, y, x), y -= x * (...
14
#include <bits/stdc++.h> using namespace std; const unsigned short maxn = 24; const unsigned int six = (1 << 22); bool adj[maxn][maxn]; unsigned int Msk; unsigned short vis; bool mark[maxn]; unsigned short n, m; unsigned short ans; unsigned int ans_msk; vector<pair<unsigned int, unsigned short> > msk[maxn]; void dfs(un...
16
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); string s; cin >> s; if (n == 1 && s[0] == '0') { cout << "No" << endl; return 0; } for (int i = 0; i < s.size(); i++) { if (s[i] == '0' && i == n - 1 && s[i - 1] == '0') { cout << "No" << endl; retur...
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); long long t; cin >> t; while (t--) { long long n, k; vector<long long> v, b; cin >> n >> k; long long a[n], f = 0; set<long long> s; set<long long>::iterator itr; for (long long i = 0; i < n;...
6
#include <bits/stdc++.h> using namespace std; long long fen[200009] = {0}; const long long M = 200005; void update(long long ind, long long b, long long *fen) { long long i = ind; while (i <= M - 2 && i > 0) { fen[i] += b; i = i + (i & (-i)); } } long long sum(long long ind, long long *fen) { long long ...
10
#include <bits/stdc++.h> using namespace std; vector<int> ele; vector<int> temp; int main() { int piles; int sumA = 0, sumB = 0; scanf("%d", &piles); while (piles--) { int n; scanf("%d", &n); temp.clear(); temp.resize(n); for (auto &i : temp) scanf("%d", &i); if (n & 1) ele.push_back(tem...
12
#include <bits/stdc++.h> using namespace std; int a[1111][1111]; int n, k; int f[1111]; int dem = 0; bool ans; int main() { if (0 == 1) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int n, k; cin >> n >> k; ans = true; for (int i = 1; i <= n; i++) f[i] = 0; for (int i =...
6
#include <bits/stdc++.h> using namespace std; int k, a, b, n, dp[209]; string s; int main() { memset(dp, -1, sizeof(dp)); cin >> k >> a >> b >> s; n = s.size(); for (int i = 0; i < k && n >= 0; i++) { n -= a; dp[i] = a; } if (dp[k - 1] == -1 || n < 0) { cout << "No solution"; return 0; } ...
6
#include <bits/stdc++.h> using namespace std; int ara[110][110]; int main() { int n, k; cin >> n >> k; if (k > n * n) { cout << -1 << endl; return 0; } int a = k; for (int i = 1; i <= n; i++) { if (k > 0) { ara[i][i] = 1; k--; } for (int j = i + 1; j <= n; j++) { if (k ...
6
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j; bool flag_row = false, flag_col = false; cin >> n >> m; int m1[n][m], m2[n][m]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> m1[i][j]; } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { ci...
6
#include <bits/stdc++.h> using namespace std; void Max(long long &x, long long y) { x = max(x, y); } void Min(long long &x, long long y) { x = min(x, y); } const long long N = 6e5 + 5; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << '(' << a.first << ", " << a.seco...
18
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int maxn = 20005; namespace flow { struct edge { int v, c, w, nex; edge(int v = 0, int c = 0, int w = 0, int nex = 0) : v(v), c(c), w(w), nex(nex) {} } e[100000]; int head[maxn], a[maxn], dist[maxn], from[maxn], en, s, t; void init() { m...
18
#include <bits/stdc++.h> using namespace std; string s; bool found; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; if (s[0] == 'f') { cout << "ftp://"; for (int i = 3; i < s.size() - 1; i++) { if (s[i] == 'r' && s[i + 1] == 'u' && !found && i != 3) { if (i == s.size() - 2...
3
#include <bits/stdc++.h> using namespace std; char buff[1000005]; char res[1000005]; int best = 2000000; int n, r; void make(int T, int B) { int i = n - 1; while (n >= 0 && T != 0 && B != 0) { if (T >= B) { T -= B; buff[i--] = 'T'; } else { B -= T; buff[i--] = 'B'; } } if (i ...
13
#include <bits/stdc++.h> using namespace std; struct node { int v, c, l, r, id; } data[110000]; int dp[110000 * 2], f[110000], pre[110000], n, ans, num, now, a[110000], maxx[110000 * 2], sta[110000], top = 0; bool cmp(const node &A, const node &B) { return (A.c + A.l + A.r < B.c + B.l + B.r || ((A.c +...
16
#include <bits/stdc++.h> using namespace std; const int iinf = 1 << 29; const long long inf = 1ll << 60; const long long mod = 998244353; void GG() { cout << "-1\n"; exit(0); } long long mpow(long long a, long long n, long long mo = mod) { long long re = 1; while (n > 0) { if (n & 1) re = re * a % mo; a...
17
#include <iostream> #include <complex> #include <vector> #include <string> #include <algorithm> #include <cstdio> #include <numeric> #include <cstring> #include <ctime> #include <cstdlib> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <list> #include <cmath> #include <bitset> #...
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, m, j, k, l, i; long double S = 0; long double a[3]; long double b[3]; cin >> S; long double s = 0; for (long long i = 0; i < 3; i++) { cin >> a[i]; s += a[i]; b[i] = a[i]; } ...
10
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, oo = 2e9 + 10; int n, c, l, f[N], sub[N]; bool flag, mark[N]; long long ans; vector<pair<long long, int> > adj[N], all[N], cur[N]; int query(int m) { m++; int res = 0; while (m) res += f[m], m -= m & -m; return res; } void update(int m, int c)...
19
#include <bits/stdc++.h> const int N = 1010; int l[N], r[N]; char map[N][N]; int main() { int n, m, i, j, s; long long ans = 0, sum; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) scanf("%s", map[i]); memset(l, 0, sizeof(l)); memset(r, 0, sizeof(r)); for (i = 0; i < n; i++) for (j = 0; j < m; j++) ...
17
#include <bits/stdc++.h> using namespace std; int main() { int t, f = 0, ans = -1, k = 0, n, i, z = 0, o = 0, e = 0; string s; vector<char> s1; cin >> n; for (t = 0; t < n; t++) { cin >> s; z = 0; o = 0; e = 0; for (i = 0; i < s.length(); i++) { if (s[i] == '0') ++z; el...
6
#include <bits/stdc++.h> using namespace std; long long int k; string toString(long long int n) { stringstream second; second << n; string ret = second.str(); return ret; } void func() { long long int t = 1, res = 9, cnt = 1, totDigit = 0, sum = 0, LastTotDigit = 0, LastSum = 0; while (1) { ...
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 200000; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; int ans = 0; long long a = 1; long long b = 1; while (b <= n) { ans++; long long c = b; b = a + b; a = c; } cout << ans - 1; r...
8
#include <bits/stdc++.h> using namespace std; long long n, m, k, i, j, res; long long a[100001]; int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; m += a[i]; } sort(a + 1, a + n + 1); cout << max(a[n], (m + n - 2) / (n - 1)); }
8
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.writ...
8