solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 10; string s; int sol[MAX]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n >> s; int last1, last2, counter1, counter2; bool can; for (char i = '0'; i <= '9'; ++i) { ...
7
#include <bits/stdc++.h> char s[1000]; int main() { while (~scanf("%s", s)) { int sum1 = 0; int flag = 0; for (int i = 0; i < strlen(s); i++) { if (flag == 0 && s[i] == '1') flag = 1; if (flag == 1 && s[i] == '0') { sum1++; } } if (sum1 >= 6) { printf("yes\n"); ...
2
#include <bits/stdc++.h> using namespace std; long long a[1003][1003]; long long dp1[1003][1003]; long long dp2[1003][1003]; long long dp3[1003][1003]; long long dp4[1003][1003]; int main() { int i, j, k, m, n; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { scanf("%ll...
8
#include <bits/stdc++.h> using namespace std; char s[5000 + 5]; bool dp[5000 + 5]; int lg(int n) { return n == 1 ? 0 : 1 + lg(n / 2); } int main() { scanf("%s", s + 1); int n = strlen(s + 1), k = lg(n); dp[0] = 1; for (int i = 1; i <= n - (1 << k) + 1; i++) { for (int msk = 0; msk <= (1 << k) - 1; msk++) ...
19
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { unsigned long long x; string s2; cin >> x >> s2; unsigned long long len = s2.size(); vector<char> s; for (char i : s2) s.push_back(i); for (unsigned long long i = 1; i <= x; i++) { unsigned ...
9
#include <bits/stdc++.h> using namespace std; int vis[2000]; int check(int i, int a[]) { while (!vis[i]) { vis[i] = 1; i = a[i]; } return i; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(20); int n; cin >> n; int a[n + 1]; for (int i = 1; i <= n; ...
2
#include <bits/stdc++.h> using namespace std; vector<int> G[200005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t, n, m, l, r, ar, id = 0; cin >> n >> m; vector<int> vc; for (int i = 1; i <= n; i++) { cin >> ar; vc.push_back(ar); } sort(vc.begin(), vc.end()); for (int i ...
11
#include <bits/stdc++.h> using namespace std; bool happyg[110] = {}; bool happyb[110] = {}; int main() { int N, M; cin >> N >> M; int B, G; cin >> B; int b, g; for (int i = 0; i < B; i++) { cin >> b; happyb[b] = true; } cin >> G; for (int i = 0; i < G; i++) { cin >> g; happyg[g] = true...
5
#include <bits/stdc++.h> using namespace std; void solve() { string s; cin >> s; vector<int> v(4); v[0] = 1; v[1] = 8; v[2] = 6; v[3] = 9; sort(v.begin(), v.end()); unordered_map<int, int> mp; int n = s.length(); do { int x = v[0] * 1000 + v[1] * 100 + v[2] * 10 + v[3]; int m = x % 7; ...
8
#include <bits/stdc++.h> using namespace std; using namespace std; const int mod = (int)1e9 + 7; const long long INF = 10000005; const int N = 100005; int a[N]; int f[N]; int main() { int n, k, k1 = 1; cin >> n >> k; for (int(i) = 0; (i) < (n); ++(i)) cin >> a[i]; int r = 1, l = 1; f[a[0]] = 1; while (r < n...
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int **b; b = new int *[n]; for (int i = 0; i < n; i++) b[i] = new int[2]; for (int i = 0; i < n; i++) { cin >> b[i][0]; cin >> b[i][1]; } int max_h = b[0][1]; int max_i = 0; int sum = 0; int max_h_2 = 1; for (int...
3
#include <bits/stdc++.h> using namespace std; string rec(string k, string st) { string ans = k + st; string ans1 = st + k; string ans2 = min(ans, ans1); return ans2; } bool chfunc(string &a, string &b) { if (a + b > b + a) return true; else return false; } int main() { int n; cin >> n; vector<...
9
#include <bits/stdc++.h> using namespace std; struct node { int x1; int y1; int x2; int y2; } l[150000], r[150000], middle[150000], a[150000]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i].x1 >> a[i].y1 >> a[i].x2 >> a[i].y2; } l[0] = a[0]; for (int i = 1; i < n - 1; i+...
8
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false), cin.tie(0); int n; cin >> n; vector<int> a(n + 1, 0); for (int i = 1; i <= n; i++) cin >> a[i]; int up = 30 * n + 10; vector<vector<int>> g(up); map<int, int> mp; int cnt = 0; vector<int> ok(up, 0); vector...
19
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int F0 = 0; int F1 = 1; int temp; int F2 = 1; int a, b, c; if (n >= 0 && n < 1000000000) { int target = n; while (F2 < target) { F2 = F1 + F0; F0 = F1; F1 = F2; } if (target == F2) { a = F0; temp ...
1
#include <bits/stdc++.h> using namespace std; using namespace std; long long int n, m, a, b; int main() { cin >> n >> m >> a >> b; if (n % m == 0) { cout << 0 << endl; } else { long long int p = n % m; long long int q = m - p; cout << min(q * a, p * b) << endl; } }
2
#include <bits/stdc++.h> using namespace std; const int N = 7005; int n, m, K; int p[200000]; int L[N], R[N], C[N], V[N]; int LL[N], RR[N]; int cnt[1000005], res; void work() { int i, j, x, y; scanf("%d%d", &n, &m); for (i = 0; 1 << i < 200000; ++i) p[1 << i] = 1; for (i = 1; i < 200000; ++i) p[i] += p[i - 1] +...
16
#include <bits/stdc++.h> unsigned long long n, q, tmp, kt[100000], a[100000], i, j; using namespace std; int main() { bool ck; cin >> q; while (q--) { cin >> n; tmp = 0; memset(kt, 0, 15000); for (i = 1; i <= n; i++) { cin >> a[i]; kt[a[i]] = 1; } for (i = 1; i <= 1024; i++) { ...
4
#include <bits/stdc++.h> using namespace std; double EPS = 1e-9; int INF = 1000000005; long long INFF = 1000000000000000005LL; double PI = acos(-1); int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; long long power(long long x, long long y) { if (y == 0) return 1; return x * powe...
8
#include <bits/stdc++.h> using namespace std; int N; int reach[5000 + 1]; bool CHECK[5000 + 1]; list<pair<int, int> > adj[5000]; int child[5000]; vector<int> res; void foo(int s) { memset(reach, 0, sizeof reach); reach[0] = 1; int upperB = 0; int counter = 1; for (list<pair<int, int> >::iterator it = adj[s].b...
7
#include <bits/stdc++.h> using namespace std; long long pows[51]; long long sumpows[51]; void solve() { long long a, b, m; cin >> a >> b >> m; if (a == b) { cout << 1 << " " << a << "\n"; return; } for (int terms = 2; terms < 51; terms++) { if (pows[terms] >= 1e18 / a) break; long long sum = p...
14
#include <bits/stdc++.h> const int N = 45; using namespace std; int f[N][N][N][N], up[N][N]; int n, m, Q, a[N][N]; char cc; void READ() { scanf("%d%d%d", &n, &m, &Q); for (int i = 1; i <= n; i++) { scanf("\n"); for (int j = 1; j <= m; j++) { scanf("%c", &cc); a[i][j] = cc - '0'; } } } void...
11
#include <bits/stdc++.h> using namespace std; const int N = 200007; int n; int perm[3][N]; struct fen { int n; int v[N]; void build(int s) { n = s; memset(v, 0, sizeof v); } void insert(int i, int x) { for (i += 2; i <= n + 3; i += (i & -i)) v[i] += x; } int get(int i) { int res = 0; f...
12
#include <bits/stdc++.h> using namespace std; pair<int, int> dp[1000001]; int f_len(int v) { if (v < 0) return -1; if (v == 0) return 0; return dp[v].second; } int main() { dp[0] = make_pair(0, 0); int j, v, data[9], i, mxlen, mxlen_id, mn_paint = 1000000000, len[9]; scanf("%d", &v); for (i = 0; i < 9; i+...
9
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, sum = 0, n, a, b, c, d, e, f; cin >> n; cin >> a >> b; cin >> c >> d; cin >> e >> f; if (c < a && d < b) { if (e < a && f < b) { cout << "YES"; } else cout << "NO"; } else if (c > a && d > b) { if (e > a && ...
2
#include <bits/stdc++.h> using namespace std; const int M = 100005; int read() { int x = 0, flag = 1; char c; while ((c = getchar()) < '0' || c > '9') if (c == '-') flag = -1; while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return x * flag; } int n, m, tp, dep[M], c[M],...
17
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main() { int n, m; scanf("%d%d", &n, &m); int pos[300005], arr[n]; long long pairs[n + 1]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); pos[arr[i]] = i; pairs[i] ...
10
#include <bits/stdc++.h> using namespace std; int main() { int n; int ans = 1, r = 0, l = 0; char x; cin >> n; for (int i = 0; i < n; ++i) { cin >> x; if (x == 'R') ++r; else ++l; } cout << ans + r + l; return 0; }
0
#include <bits/stdc++.h> int main() { int x, y; int r; scanf("%d%d", &x, &y); int fir = x * x + y * y; int st = 0; if (x == 0 || y == 0) { st = 1; } else { for (r = 1; r <= 3005; r++) { if (r * r >= fir) break; } if (x * y > 0) st = 1; else st = 0; if ((r - 1) % 2...
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m; char s[1000][35]; int a[101]; int i, j, k; int tmp[1000], flag[1000], l, sum_min, sum_max; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) scanf("%d", &a[i]); getchar(); sort(a, a + n); for (i = 0; i < m; i++) gets(s[i]); for (i =...
3
#include <bits/stdc++.h> using namespace std; int a[100010], b[100010]; int c = 0, n, m; void f(int x, int y) { if (x > n || y > n || c == m) return; a[c] = x; b[c] = y; ++c; f(x, x + y); f(y, x + y); } int main() { scanf("%d%d", &n, &m); if (m < n - 1) { printf("Impossible\n"); return 0; } ...
9
#include <bits/stdc++.h> using namespace std; int da[62]; long long ans[62][32]; long long f[62]; int qe[62]; long long dfs(int s, int l, int ll, int up) { if (!s) return 1; if (!up && f[s] != -1) return f[s]; int stop = up ? da[s] : 1; long long res = 0; for (int i = 0; i <= stop; i++) { if (s == ll && i...
13
#include <bits/stdc++.h> using namespace std; const long long oo = 1e18; struct MONOTONIC_QUEUE { deque<pair<long long, long long>> D; long long sz = 0; long long pt = 0; void insert(long long x) { sz++; while (!D.empty() && D.back().first >= x) D.pop_back(); D.push_back({x, pt++}); } void erase...
12
#include <bits/stdc++.h> using namespace std; int main() { int n, m, p; cin >> n >> m; if (n >= m) { p = n - 1; cout << p << ' ' << n + m - 1 - p; } else if (m > n) { p = m - 1; cout << p << ' ' << n + m - 1 - p; } }
5
#include <bits/stdc++.h> using namespace std; long long a[1005], b[1005], n; double p, m; int main() { cin >> n >> m; p = m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; double f = p / (b[1] - 1); if (a[1] == 1 || b[1] == 1) { cout << -1 << endl; return 0; } ...
7
#include <bits/stdc++.h> using namespace std; int MAX(int a, int b) { return (a > b) ? a : b; } double FMAX(double a, double b) { return (a > b) ? a : b; } int MIN(int a, int b) { return (a < b) ? a : b; } double FMIN(double a, double b) { return (a < b) ? a : b; } int GCD(int a, int b) { return b ? GCD(b, a % b) : a; ...
5
#include <bits/stdc++.h> const int d[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}; const int N = 1e5 + 10; const int mod = 1e9 + 7; using namespace std; int n; int main() { ios_base::sync_with_stdio(false); cin >> n; if (n % 2 == 0) cout << -1; else ...
5
#include <bits/stdc++.h> using namespace std; vector<int> even; vector<int> odd; vector<int> ev; vector<int> od; map<int, int> ma; map<int, int> mm; int arr[200005]; int brr[200005]; int main() { int m, n; scanf("%d %d", &m, &n); int i, j; for (i = 0; i < m; i++) { scanf("%d", &arr[i]); brr[i] = arr[i];...
11
#include <bits/stdc++.h> using namespace std; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(int64_t &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class ...
8
#include <bits/stdc++.h> using namespace std; long long solve(long long x, long long y) { long long a1 = x * (x + 1) / 2; long long d = x + 1; return (y + 1) * (2 * a1 + y * d) / 2; } int main() { long long m, b; cin >> m >> b; long long mx = 0; for (long long i = 0; i <= b; i++) { mx = max(mx, solve(...
5
#include <bits/stdc++.h> using namespace std; int main() { int n, x, TotalTime = 0, BoringTime = 15; cin >> n; for (int i = 1; i <= n; i++) { cin >> x; if (x > BoringTime) { TotalTime = BoringTime; break; } else if (x == BoringTime) { TotalTime = x + 15; BoringTime = x + 15; ...
0
#include <bits/stdc++.h> using namespace std; int n, m; long long int b; vector<long long int> a; int main() { scanf("%d%d", &n, &m); a.resize(n); for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } sort(a.begin(), a.end()); while (m--) { scanf("%lld", &b); printf("%d ", upper_bound(a.begin(),...
5
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; vector<pair<long long int, long long int> > a; int asdasd[N]; int main() { int d, n, m; cin >> d >> n >> m; for (int i = 0; i < m; i++) { int x, p; cin >> x >> p; a.push_back(make_pair(x, p)); } a.push_back(make_pair(d, 0)); a...
14
#include <bits/stdc++.h> using namespace std; int main() { int n, m, p, t, idx, maxm = 0, minm = 1000000, maxRes = 0, minRes = 0; cin >> n >> m; t = n; vector<int> planes; vector<int> minPlanes; while (m--) { cin >> p; planes.push_back(p); minPlanes.push_back(p); } while (n--) { maxm = 0...
3
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const double EPS = 1e-9; const int MOD = 1e9 + 7; int main() { string s; bool merda = false; int ab = -1, fc = 1; ; cin >> s; for (int i = (0); i < (s.size()); i++) { if (s[i] == '[' && ab == -1) ab = i; else if (s[i] ==...
5
// Author: Joe2k #include<bits/stdc++.h> using namespace std; #define gc getchar_unlocked #define ll long long #define PI 3.1415926535897932384626 #define scan(x) cin>>x #define print(x) cout<<x<<endl #define printSpace(x) cout<<x<<" " #define br cout<<endl #define fo(i, n) for(i=0;i<n;i++) #define Fo(i, k, n) for(i=...
3
#include <bits/stdc++.h> using namespace std; double x[100010], y, ans, newtag; int n, k; double len(int i) { if (i <= 0) return 0; double dx = x[n + 1] - x[i], dy = y; return sqrt(dx * dx + dy * dy); } double ll(int i) { double xi = newtag; double xj = x[i]; if (xj < xi) { int tmp = xi; xi = xj; ...
18
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const long long MAX = (long long)2e6 + 100; long long inf = (long long)1e18; long long mod = (long long)1e13 + 2987429; long long n, m, ans[MAX], b[MAX]; set<long long> gr[MAX]; void solve() { cin >> n >> m; for (int i = 0; i < m; i++) { ...
10
#include <bits/stdc++.h> using namespace std; map<pair<int, int>, int> dis; set<pair<int, int> > edge; int main() { int x0, y0, x1, y1; cin >> x0 >> y0 >> x1 >> y1; int n, r, L, R; cin >> n; while (n--) { cin >> r >> L >> R; for (int i = L; i <= R; i++) { edge.insert(make_pair(r, i)); } } ...
10
#include <bits/stdc++.h> using namespace std; using INT = long long; int main() { int n; cin >> n; if (n % 4 == 2 || n % 4 == 3) return puts("NO"), 0; puts("YES"); vector<pair<int, int> > ans; for (int i = 1; i < n; i += 2) for (int j = i + 2; j < n; j += 2) { ans.push_back({i, j}); ans.push...
23
#include <bits/stdc++.h> using namespace std; int main() { int t; long long n, m; cin >> t; while (t--) { cin >> n >> m; if ((n * m) % 2) cout << (n * m) / 2 + 1 << endl; else cout << (n * m) / 2 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int R, C; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> R >> C; if (R == 1 && C == 1) { cout << "0\n"; return 0; } if (R == 1) { for (long long c = 0; c < C; c++) { cout << c + 2 << " "; } cout << "\n"; return 0; }...
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int a[MAXN], f[260], n, k; int main(void) { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> a[i]; memset(f, -1, sizeof f); for (int i = 1; i <= n; ++i) { if (f[a[i]] == -1) { int lo...
9
#include <bits/stdc++.h> using namespace std; const long long MD = 1e9 + 7; const long long Inf = 2e16; const long long N = 1e5 + 5; long long arr[N], ps[N], dp[N][2]; long long sqr(long long n) { long long l = 0, r = 1e9 + 1; while (r - l > 1) { long long mid = (r + l) / 2; if (mid * mid <= n) l = mi...
12
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; const int inf = INT_MAX; const long long inff = 1e18; const long long mod = 1e9 + 7; int seg[maxn * 5]; int n; int a[maxn]; void modify(int node, int l, int r, int x, int v) { if (l == r) { seg[node] = v; return; } int m = (l + r) >> ...
11
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, i, sq, maxm = 0; cin >> n; int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (i = 0; i < n; i++) { sq = min(a[i], (n - i)); if (sq > maxm) { m...
0
#include <bits/stdc++.h> using namespace std; vector<int> G[100001]; int n, m, k; bool vol[100001]; int source, target; bool solve(int q) { vector<bool> in(n + 1, false); vector<int> dist(n + 1, 0); queue<int> Q; Q.push(source); dist[source] = q; in[source] = true; while (!Q.empty()) { int u = Q.front...
12
#include <bits/stdc++.h> using namespace std; int a[1010100], fa[1010100], id[10010001]; int n, limit = 10000000; int ok(int x) { if (fa[x] == x) return x; fa[x] = ok(fa[x]); return fa[x]; } void merge(int x, int y) { x = id[x], y = id[y]; x = ok(x); y = ok(y); fa[x] = y; } int gcd(int a, int b) { return ...
17
#include <bits/stdc++.h> using namespace std; const long long linf = 1e18 + 5; const int mod = (int)1e9 + 7; const int logN = 17; const int inf = 1e9; const int N = 3e3 + 5; int n, a[N], h[N], x, y, cur, m; vector<int> ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int cur = n...
13
#include <bits/stdc++.h> using namespace std; const int N = 100005, P = 1000000007; using ll = long long; struct TNode { int l, r, mid, s1, s2, s3, t1, t2; } t[N << 2]; inline int lc(int pos) { return pos << 1; } inline int rc(int pos) { return pos << 1 | 1; } inline void pushup(int pos) { t[pos].s1 = (t[lc(pos)].s...
27
#include <bits/stdc++.h> using namespace std; inline long long rd() { long long _x = 0; int _ch = getchar(), _f = 1; for (; !isdigit(_ch) && (_ch != '-') && (_ch != EOF); _ch = getchar()) ; if (_ch == '-') { _f = 0; _ch = getchar(); } for (; isdigit(_ch); _ch = getchar()) _x = _x * 10 + _ch - '0...
11
#include <bits/stdc++.h> using namespace std; struct edge { int y; edge* next; }; edge* new_edge() { static edge a[200005]; static int top = 0; return &a[top++]; } edge* li[100005]; void insert_edge(int x, int y) { edge* t = new_edge(); t->y = y; t->next = li[x]; li[x] = t; } bool vis[100005][2]; int ...
13
#include <bits/stdc++.h> using namespace std; const int Maxn = 1e6; struct Line { long long k, b; Line(long long _k, long long _b) { k = _k, b = _b; } long long get_val(long long x) { return k * x + b; } long long intersect_x(Line rhs) { return (b - rhs.b) / (rhs.k - k); } }; struct Rect { int x, y; long lo...
16
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int cnt = 0; int t = 0; int j = 1; for (int i = 0; i < k; ++i) { int len; cin >> len; vector<int> ab; for (int k = 0; k < len; ++k) { int y; cin >> y; ab.push_back(y); } if (ab[0] ==...
7
#include <bits/stdc++.h> using namespace std; int n, a[105], vis[105], ans; 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 = 1; i <= n; i++) if (!vis[i]) { ans++; for (int j = 1; j <= n; j++) if (a[j] % a[i] == 0) vis[j] ...
0
#include <bits/stdc++.h> using namespace std; int main() { int n[10] = {0, 52, 52, 52, 52, 53, 53, 52}; int a, l, h; string b1, b2; cin >> a >> b1 >> b2; l = b2.length(); if (l == 4) printf("%d\n", n[a]); else if (l == 5) { if (a <= 29) h = 12; if (a == 30) h = 11; if (a == 31) h = 7; ...
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); int c = 0; for (int i = 1; i < n; i++) { c += arr[i] - arr[i - 1] - 1; } cout << c; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr); int n; cin >> n; vector<int> stk; int inp; int mx = 0; while (n--) { cin >> inp; while (stk.size() && stk.back() <= inp) { mx = max(mx, stk.back() ^ inp); ...
10
#include <bits/stdc++.h> using namespace std; int main() { int a[8] = {0}; int n, m; cin >> n; for (int i = 0; i < n; i++) { cin >> m; a[m]++; } if (!a[5] && !a[7] && 3 * a[1] == n && a[2] + a[3] == a[4] + a[6] && a[4] <= a[2]) { int u = a[3]; int v = a[4]; int s = n / 3 - u - v; ...
4
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const int mod = 1e9 + 7; const int inv2 = (mod + 1) >> 1; int gi() { int x = 0, o = 1; char ch = getchar(); while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') o = -1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ...
22
#include <bits/stdc++.h> using namespace std; double const pi = acos(-1.0); double cal(int n, double a) { double arc = 360 / n; double half = arc / 2; double r = 0.5 * a / tan(half * pi / 180); double R = 0.5 * a / sin(half * pi / 180); double h = sqrt(a * a - R * R); double s = 0.5 * n * a * r; return s ...
9
#include <bits/stdc++.h> using namespace std; string s; int main() { ios::sync_with_stdio(false); long long int T, a, b, i, t, k; cin >> T; while (T--) { cin >> s; t = 0; k = 0; for (i = 0; i < s.size(); i++) { if (k == 0 && s[i] == '-') { t = t + i + 1; } else if (s[i] == '-...
5
#include <bits/stdc++.h> using namespace std; void ONLINE__JUDGE(); pair<long long, long long> swapii(pair<long long, long long> pr) { return {pr.second, pr.first}; } int dx[] = {0, 0, -1, +1, -1, +1, -1, +1}; int dy[] = {-1, +1, 0, 0, -1, +1, +1, -1}; const int mod = 7901; const int MAX = 1e3 + 9; map<char, long lon...
1
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007, W = 300000; vector<int> cc[301000]; int n, fac[301000], ivf[301000], coef[301000]; inline int calc(int n, int k) { if (n < k) return 0; return 1ll * fac[n] * ivf[k] % mod * ivf[n - k] % mod; } int pr(int a, int z) { int s = 1; do { if...
23
#include <bits/stdc++.h> int a[22], b[22], f[22]; int main(void) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); f[i] = i; } for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (a[f[i]] < a[f[j]]) { int t = f[i]; f[i] = f[j]; f[j...
12
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k; cin >> n >> k; vector<long long> v(n); for (long long i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); if (k == 0) { long long ans = v[0] - 1; if (ans <= 0) { cout << -1 << endl; return; } else { ...
4
#include <bits/stdc++.h> using namespace std; long long int n, ans = 0; void calc(long long int a) { if (n < a) return; if (n >= a) ans++; calc(a * 10 + 1); calc(a * 10); } int main(void) { cin >> n; calc(1); cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; bool valid(char a) { return a != '.'; } bool add(char &a, char &b) { if (valid(a) && a != b) return false; a = b; return true; } string s[6]; vector<string> ans; int main() { for (int i = 0; i < 6; i++) cin >> s[i]; sort(s, s + 6); do { vector<string> v(s[0]...
12
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int m, n; cin >> m >> n; vector<int> p; for (int i = 0; i < n; ++i) { cout << 1 << endl; fflush(stdout); int cur; cin >> cur; if (cur > 0) p.push_back(1); else if (cu...
10
#include <bits/stdc++.h> using namespace std; int a[501]; set<int> s; int n; long long ans = 998244353998244347; void check(int x) { int i; long long cnt = 0; for (i = 1; i <= n; i++) { if (a[i] % (x + 1) == 0) { cnt += a[i] / (x + 1); continue; } if (x - a[i] % (x + 1) > a[i] / (x + 1)) r...
17
#include <bits/stdc++.h> using namespace std; int f(pair<int, int> a) { return a.second - a.first - 1; } int main() { int n, m; cin >> n >> m; string s; cin >> s; set<pair<int, int> > A; int len = 0; s += "~"; long long sum = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '.') { len++; ...
8
#include <bits/stdc++.h> using namespace std; const double eps = 1e-12; struct point { double x, y; }; int t; double cross(point a, point b, point c) { b.x -= a.x; b.y -= a.y; c.x -= a.x; c.y -= a.y; return b.x * c.y - b.y * c.x; } bool check(point a, point b, point c) { double kx1 = 2.0 * (b.x - a.x), ky...
18
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 5; const long long MOD = 1e9 + 7; long long power(long long x, long long y, long long m) { long long ans = 1; x = x % m; if (x == 0) return 0; while (y > 0) { if (y & 1) ans = (ans * x) % m; y = y >> 1; x = (x * x) % m; } ...
6
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 6; long long int t[N], w[N]; vector<long long int> v; priority_queue<pair<long long int, long long int> > v2; int main() { int n; cin >> n; int best_place = 0; for (int i = 1; i <= n; i++) { cin >> t[i] >> w[i]; if (i > 1) { if (t[i...
10
#include <bits/stdc++.h> using namespace std; const int LEN = (int)1e3; struct rect { pair<int, int> x, y; }; bool comp1(rect a, rect b) { return a.x < b.x; }; bool comp2(rect a, rect b) { return a.y < b.y; }; int n; rect a[LEN]; bool solve(int i, int I, bool m) { if (i == I) return true; bool res = true; bool ...
17
#include <bits/stdc++.h> using namespace std; struct Node { long long l, r, sg; Node() {} Node(long long a, long long b, long long c) { l = a; r = b; sg = c; } } cover[10]; int main() { int n; long long x; cover[0] = Node(1LL, 3LL, 0LL); cover[1] = Node(4LL, 15LL, 1LL); cover[2] = Node(16L...
14
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int N_MAX = 100002; int n; struct Segment { int la, ra; int lb, rb; int index; long long h1; long long h2; }; Segment v[N_MAX]; bool fa(const Segment &a, const Segment &b) { return a.ra < b.ra; } long long p[N_MAX]; long long sp[N_MA...
13
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int y) { long long int res = 1; while (y > 0) { if (y & 1) res = res * x; y = y >> 1; x = x * x; } return res; } int main() { long long int test; cin >> test; while (test--) { long long int l, r; ...
0
#include <bits/stdc++.h> using namespace std; const int maxn = 2 * 1000 * 100 + 100; long long n, a[maxn], dp[maxn], sum; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; dp[1] = 0; if (a[1]) sum++; for (int i = 2; i <= n; i++) { if (!a[i]) { dp[i] = dp...
8
#include <bits/stdc++.h> using namespace std; int64_t n, p[100005], s[100005], i, A; int main() { cin >> n; for (i = 2; i <= n; ++i) cin >> p[i]; for (i = 1; i <= n; ++i) cin >> s[i]; for (i = 2; i <= n; ++i) (~s[i]) ? s[p[i]] = min(s[i], s[p[i]]) : s[i] = 1e9 + 1; for (i = 1; i <= n; ++i) if (s[i] < ...
8
#include <bits/stdc++.h> using namespace std; int main() { char s[100001]; string lexeme; cin.getline(s, sizeof(s) / sizeof(s[0])); char* p = s; bool quote(false); while (*p != 0) { if (quote) { if (*p != '\"') { lexeme.push_back(*p); } else { cout << "<" << lexeme << ">" << ...
5
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3fLL; const int maxn = 1e5 + 10; const int maxm = 4.1e7 + 10; const int mod = 1e9 + 7; int dp[4005], ans[4005]; int a[4005][4005], c[4005][4005]; inline void read(int &x) { x = 0; char c; for (c = getch...
18
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const long long MOD = 1e9 + 7; const int OO = 1e9; const long long N = 1e5 + 5; int f[N]; int joy[11]; int fav[505]; int k, n; int mem[505][5005]; int dp(int idx, int rem) { if (!idx) return 0; int mn = min(k, rem); int &ret = mem[idx][rem]; ...
12
#include <bits/stdc++.h> using namespace std; namespace Vectors { struct Vector { long long x, y; inline Vector(long long _x = 0, long long _y = 0) { x = _x, y = _y; } }; inline Vector operator+(const Vector& a, const Vector& b) { return Vector(a.x + b.x, a.y + b.y); } inline Vector operator-(const Vector& a, con...
18
#include "bits/stdc++.h" #define debug(...) 0 #ifdef LOCAL #include "../../_library/cc/debug.h" #endif using namespace std; void solve(); //#define FILE "family" int main() { cin.tie(nullptr)->sync_with_stdio(false); #if defined(FILE) && !defined(LOCAL) freopen(FILE ".in", "r", stdin), freopen(FILE ".out", "w", st...
16
#include <bits/stdc++.h> using namespace std; int n, L; char S[200010][21]; int a[200010]; int dp[200010], *g[22], pool[(1 << 22) + 10], *p = pool; inline int mask(int i) { return (1 << i) - 1; } int common(int S1, int S2) { for (int i = L; i >= 0; i--) if ((S1 & mask(i)) == (S2 >> (L - i))) return i; } const int...
20
#include <bits/stdc++.h> using namespace std; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int n, m, vis[101][101], cnt; string s[101]; int dfs(int x, int y) { if (x < 0 || x >= n || y < 0 || y >= m || vis[x][y] || s[x][y] == '.') return 0; vis[x][y] = 1; for (int i = 0; i < 4; i++) dfs(x + dx[i], y + ...
9
#include <bits/stdc++.h> using namespace std; const int N = 600005; int vec[N], pre[N], base[N], c[N], tot; int p[N]; int vis[N]; int ev[N]; void link(int x, int y) { vec[++tot] = y; pre[tot] = base[x]; base[x] = tot; } int dfs1(int u) { int ret = 1; vis[u] = 1; for (int now = base[u]; now; now = pre[now]) ...
11
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 123; int mark[maxn]; vector<int> Res; int n, x, maxd = 1000000, t; int main() { cin >> n; while (n--) { cin >> x; mark[x] = 1; } for (int i = 1; i <= maxd; i++) if (mark[i] == 1 && mark[maxd - i + 1] == 0) { mark[maxd - i + 1...
9
#include <bits/stdc++.h> using namespace std; int l; char s[(1 << 20)]; char z[(1 << 20)]; int go(int f, int k, int eq) { int t = l - k; if (!t) return !f; if (t < f) return 0; if (!eq) { if (f) { z[k] = '4'; return go(f - 1, k + 1, 0); } else { z[k] = '7'; return go(f, k + 1, 0)...
10