solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 1000; const int mod = 998244353; int n; long long qpow(long long n, long long m) { long long ans = 1; while (m) { if (m & 1) ans = ans * n % mod; n = n * n % mod; m >>= 1; } return ans; } long long inv(long long n) { return qpow(n, mo...
13
#include <bits/stdc++.h> using namespace std; long long n, m, p, s[15], ans[15]; char mdo[1010][1010]; queue<pair<long long, long long> > c; queue<pair<pair<long long, long long>, long long> > busq; pair<pair<long long, long long>, long long> A, B; pair<long long, long long> padre, h; long long dx[] = {1, 0, -1, 0}; lo...
11
#include <bits/stdc++.h> int ATTENTION() { http: return 0; } using namespace std; const int iinf = 0x7fffffff; const long long linf = ~(1LL << 63); template <typename T> inline T gcd(T a, T b) { if (a < 0) return gcd(-a, b); if (b < 0) return gcd(a, -b); if (a < b) return gcd(b, a); if (b == 0) return a; re...
17
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; ostream& operator<<(ostream& o, vector<long long int> s) { for (long long int i = 0; i < (long long int)s.size(); i++) o << s[i] << " "; o << "\n"; return o; } long long int add(long long int a, long long int b) { long long int c =...
0
#include <bits/stdc++.h> using namespace std; int n, k, a[100005 << 2], amin[100005 << 2], amax[100005 << 2]; int ql[100005], qr[100005]; void build(int l, int r, int rt) { if (l == r) { scanf("%d", &a[rt]); amin[rt] = amax[rt] = a[rt]; return; } int m = (l + r) >> 1; build(l, m, rt << 1); build(m...
11
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int n, m, k; long double frac[maxn], ans; bool tf; long double C(int n, int m) { return frac[n] - frac[m] - frac[n - m]; } long double sum(long double x, long double y) { if (x > log(10.0) * 99 || y > log(10.0) * 99) return log(10.0) * 100; retu...
19
#include <bits/stdc++.h> using namespace std; int n, m, x[2], a[60][60], ans = 1000000000, z; int f(int k, int q, int l) { int ret = 0; for (int i = k; i < k + x[l]; i++) for (int j = q; j < q + x[!l]; j++) ret += a[i][j]; return ret; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) for (int ...
4
#include <bits/stdc++.h> using namespace std; int main() { int n; long long a[100000]; int j = 1; int l; int z = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (int i = 1; i < n - 1; i++) if (a[i] > a[0] && a[i] < a[n - 1]) z = z + 1; cout << z << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, f[1000005], fa[1000005], m1[1000005], m2[1000005]; int main() { scanf("%d", &n); for (int i = 2, a; i <= n + 1; i++) { scanf("%d", &fa[i]); f[i] = 1; int t = i; while (t != 1) { if (f[t] > m1[fa[t]]) m2[fa[t]] = m1[fa[t]], m1[fa[t]] ...
18
#include <bits/stdc++.h> using namespace std; typedef long long ll; int n; vector<int> a, b; void solve(){ scanf("%d", &n); a.clear(); b.clear(); int num; for(int i=1; i<=n; i++){ scanf("%d", &num); if(num<=0) a.push_back(num); else b.push_back(num); } sort(a.begin(), a....
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); long long n, k; cin >> n >> k; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; vector<pair<long long, long long> > v; map<long long, long long> m; map<long long, long long...
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1000003; long long mod_pow(long long x, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long pwc(long long n, long long m) { m = min(m, n - m); lon...
10
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const double eps = 1e-15; const double PI = atan(1.0); const int N = 200001; const int MOD = 1e9 + 7; string s; int a; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> s; int h = (s[0] - '0') * 10 + (s[1] - '0'); i...
1
#include <bits/stdc++.h> using namespace std; int index(char c) { return int(c) - int('a'); } void min_color(string s) { int len = s.length(); vector<int> v(26); vector<int> color(len); int count = 0; for (int i = 0; i < len; i++) { int idx = index(s[i]), color_max = 0; for (int j = idx + 1; j < 26; j...
12
#include <bits/stdc++.h> using namespace std; const int N = 30; int n; int dp[40][1 << 20], f[N][N]; char g[N][N]; bool u[40][1 << 20]; int dfs(int c, int zt, int t) { if (c == n + n - 2) { if (g[n - 1][n - 1] == 'a') return 1; if (g[n - 1][n - 1] == 'b') return -1; return 0; } if (u[c][zt]) return dp...
16
#include <bits/stdc++.h> using namespace std; int main() { int x, input, past = 0, max = 0; vector<int> arr, arr2; cin >> x; while (x--) { cin >> input; arr.push_back(input); if (find(arr2.begin(), arr2.end(), input) == arr2.end()) arr2.push_back(input); } for (int i = 0; i < arr2.size(); ...
0
#include <bits/stdc++.h> using namespace std; inline void Fail() { printf("No"); exit(0); } const int maxn = 100005; int n, m; vector<int> a[maxn], edge[maxn], must; bool vis[maxn]; bool mustnot[maxn]; void dfs(int x) { vis[x] = true; if (mustnot[x]) Fail(); else must.push_back(x); for (int(i) = 0; ...
13
#include <bits/stdc++.h> using namespace std; long long n, c0, c1; map<pair<long long, long long>, long long> m; set<pair<long long, long long> > u; void add(long long x, long long y, priority_queue<pair<long long, pair<long long, long long> > > &q) { if (u.find(make_pair(x, y)) != u.end()) return; u.inser...
19
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { out << '(' << p.first << ',' << p.second << ')'; return out; } template <class T> istream& operator>>(istream& in, vector<T>& v) { for (auto& x : v) in >> x; return in; } template <cla...
11
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, const U &b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, const U &b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), ...
6
#include <bits/stdc++.h> using namespace std; int n, m, ans, c[10]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; int a[m], b[m]; for (int i = 0; i < m; i++) { cin >> a[i] >> b[i]; if (c[b[i] - 1] == 0) { c[b[i] - 1] = i + 1; } else { a[c[b[i] - 1] - 1] += a[i];...
1
#include <bits/stdc++.h> using namespace std; const int n = 2 * 1e5 + 500; const int m = 1 * 1e2 + 50; int N; int K; int S; int T; int optimal_cap, best = -1; int cost[n], cap[n], gas[n]; bool ok(int op) { int ind = 1, mn_T = 0, last = 0; while (ind <= K + 1) { int d = gas[ind] - last; if (d > op) return 0;...
9
#include <bits/stdc++.h> using namespace std; const int N = 1e2 + 5; const int mod = 1e8; int r[N], g[N], b[N], d, dp[N][N][N], dp1[N][N][N], dis[N]; char o; pair<int, int> p; bool mark, h = false; string s; vector<int> vt[N]; deque<int> dq; int main() { ios::sync_with_stdio(false); cin.tie(0); long long t, n1, n...
9
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const int maxN = 1e5 + 9; int N; int a[2 * maxN], pos[2 * maxN], now[2 * maxN], f[2 * maxN][3]; pair<int, int> b[2 * maxN]; void enter() { cin >> N; for (int i = 1; i <= N; i++) { cin >> a[i]; b[i] = {a[i], i}; pos[i] = i; now[i]...
12
#include <bits/stdc++.h> template <typename _T> using MaxHeap = std::priority_queue<_T>; template <typename _T> using MinHeap = std::priority_queue<_T, std::vector<_T>, std::greater<_T>>; template <typename _T> inline bool chmax(_T &_l, const _T _b) { if (_l < _b) { _l = _b; return true; } return false; }...
14
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; bool cmp2(pair<long long, long long> a, pair<long long, long long> b) { if (a.first < b.first) return true; if (a.first == b.first) { if (a.second > b.second) { return true; } } return false; } bool cmp1(pair<long long, long ...
8
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long k, n; cin >> k >> n; cout << ((k - 1) * 9) + n << endl; } }
2
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int FFTMOD = 1007681537; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = acos((long double)-1); const long double EPS = 1e-9; inline long long gcd(long long a, long long b) { long long r; while...
21
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& out, const pair<T1, T2> p) { out << '(' << p.first << ',' << p.second << ')'; return out; } template <class T1, class T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return...
15
#include <bits/stdc++.h> using namespace std; int main() { long long int m, n; cin >> m >> n; long long int s = 0; do { if (m == n) { s += 1; break; } if (m > n) { s += m / n; m = m % n; } else { s += n / m; n = n % m; } } while (m != 0 && n != 0); cou...
3
#include <bits/stdc++.h> using namespace std; int main() { long long x, d, now; vector<long long> p; scanf("%lld%lld", &x, &d); now = 1; for (int i = 30; i >= 0; i--) { while (x >= (1 << i) - 1) { for (int j = 0; j < i; j++) p.push_back(now); now += d + 1; x -= (1 << i) - 1; if (x ...
9
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int X[N]; long long W[4]; int main() { int n; scanf("%d\n", &n); char c; for (long long i = 1; i <= n; i++) scanf("%c", &c), X[i] = (int)(c == '1'); scanf("%c", &c); for (long long i = 1; i <= n; i++) scanf("%c", &c), X[i] += 2 * (int)...
4
#include <bits/stdc++.h> using namespace std; int N; int a[100011]; int b[100011]; int main() { cin >> N; int c = 0; int t = 0; for (int(i) = 0; (i) < (int)(N + 1); (i)++) { cin >> a[i]; b[i] = a[i]; t += a[i]; if (i != 0 && a[i - 1] != 1 && a[i] != 1) c = i; } if (c == 0) { cout << "per...
7
#include <bits/stdc++.h> using namespace std; const int N = 100005; const int oo = 1000000007; int n, m, k; pair<int, int> sen[N]; vector<pair<int, int> > h0[2][N], hm[2][N], v0[2][N], vn[2][N]; bool corner(int x, int y) { if (x == 0 && y == 0) return true; if (x == n && y == m) return true; if (x == n && y == 0)...
10
#include <bits/stdc++.h> using namespace std; int a[100010], b[100010], c[100010]; int main() { ios_base::sync_with_stdio(0); int n; long long k; while (cin >> n >> k) { for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); int i = 0, j = 0; while (i < n) { int p = upper_bound(a, a + n,...
9
#include <bits/stdc++.h> using namespace std; const int MAX_INT = 0x7FFFFFFF; const int INF = 0x3f3f3f3f; const int maxn = 1050; const double eps = 1e-8; const double inf = 1e20; ; const double pi = acos(-1.0); const int maxp = 1e5 + 50; int sgn(double x) { if (fabs(x) < eps) return 0; if (x < 0) return -1; e...
13
#include <bits/stdc++.h> using std::min; const int N = 4e5 + 7; const long long INF = 1e17; int n; long long X, Y; char s[N]; inline int read() { int now = 0; register char c = getchar(); for (; !isdigit(c); c = getchar()) ; for (; isdigit(c); now = now * 10 + c - '0', c = getchar()) ; return now; } i...
7
#include <bits/stdc++.h> using namespace std; long long int llin() { long long int x; scanf("%lld", &x); return x; } long double ldin() { long double x; scanf("%Lf", &x); return x; } char chin() { char ch; cin >> ch; return ch; } string strin() { string str; cin >> str; return str; } void No() {...
0
#include <bits/stdc++.h> using namespace std; const int AI1 = 10005; const int AIMAX = 1e6 + 1; int main() { int k; cin >> k; int a[k]; for (int i = 0; i < k; i++) cin >> a[i]; sort(a, a + k); if (a[k - 1] > 25) cout << a[k - 1] - 25 << endl; else cout << 0 << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, i; cin >> n; char ch; multiset<char> s; for (i = 0; i < n; ++i) { cin >> ch; s.insert(ch); } for (auto j : s) cout << j; cout << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 3e5; const int INF = 1e9; int t, n; int a[N + 2], l[N + 2], r[N + 2], dp[N + 2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n; i++) { dp[i] = 0; l[i] ...
12
#include <bits/stdc++.h> using namespace std; string s; long long k[10], a[10], jc[20], ans; void dfs(int x) { if (x == 10) { int cnt = 0; for (int i = 0; i < 10; ++i) cnt += a[i]; long long sum = jc[cnt]; for (int i = 0; i < 10; ++i) sum /= jc[a[i]]; if (a[0] >= 1) sum -= (sum * a[0] / cnt); ...
10
#include <bits/stdc++.h> using namespace std; namespace IO { const int sz = 1 << 15; char inbuf[sz], outbuf[sz]; char *pinbuf = inbuf + sz; char *poutbuf = outbuf; inline char _getchar() { if (pinbuf == inbuf + sz) fread(inbuf, 1, sz, stdin), pinbuf = inbuf; return *(pinbuf++); } inline void _putchar(char x) { if...
27
#include <bits/stdc++.h> using namespace std; const int MAX = 200100, INF = 1000000100; int n, a[MAX]; void wczytaj_dane() { cin >> n; for (int i = 0; i < (n); i++) cin >> a[i]; } int rozwiaz() { int w = INF; for (int i = 0; i < (n / 2); i++) w = min(w, a[i + n / 2] - a[i]); return w; } void zrob_test() { w...
15
#include <bits/stdc++.h> using namespace std; int main() { string st; cin >> st; int l = st.length(); int d = st[l - 1] - '0'; int f = -1; int k = -1; for (int i = 0; i < l - 1; i++) { if ((st[i] - '0') % 2 == 0 && st[i] - '0' < d) { f = i; break; } if ((st[i] - '0') % 2 == 0) { ...
5
#include <bits/stdc++.h> using namespace std; int arr[200002]; vector<int> v[201]; int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); ; int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 1; i <= 200; i++) { v[i].clear(); } int ans = 0; for (int i = 1; i <= n...
10
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, m, x[N], q[N]; long long c, a[N]; int query(long long r) { int v = lower_bound(a, a + n, r) - a - 1; if (q[v] == 1) return x[v]; return query((r - a[v] - 1) % x[v] + 1); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { ...
8
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; template <class T, class S> ostream &operator<<(ostream &o, const pair<T, S> &p) { return o << '(' << p.first << ", " << p.second << ')'; } template <template <class, class...> clas...
24
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-7; const int N = 5010; long double f[N * 2][1010]; int k, q, p; int main() { scanf("%d%d", &k, &q); f[0][0] = 1.0; for (int i = 0; i < N * 2 - 1; i++) { for (int j = 0; j <= min(i, k); j++) { if (j < k) f[i + 1][j + 1] += f[i][j] *...
14
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 * 1000 + 10; const long long mod = 998244353; const long long inf = 1LL * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 10; long long pw(long long a, long long b) { return (b == 0 ? 1LL : ((pw(a * a % mod, b / 2) * (b & 1 == 1 ? a : 1LL)...
4
#include <bits/stdc++.h> using namespace std; int n, q, l[100010], r[100010], pre[100010], nxt[100010]; char s[100010]; bool vis[100010], activate[100010]; vector<int> in[100010], out[100010]; int cur[10], vl[100010][10]; void solve(int pos) { int dir = 1, npos = pos; memset(cur, 0, sizeof(cur)); pre[pos] = pos -...
21
#include <bits/stdc++.h> using namespace std; int ri() { int x; scanf("%d", &x); return x; } double rd() { double x; scanf("%lf", &x); return x; } long long rll() { long long x; scanf("%lld", &x); return x; } string rs() { string x; cin >> x; return x; } string rsf() { string s; char c; wh...
9
#include <bits/stdc++.h> using namespace std; struct _ { ios_base::Init i; _() { cin.sync_with_stdio(0); cin.tie(0); } } _; int main() { long long r, h; cin >> r >> h; long long A = h / r; long long rest1 = h - A * r; if (rest1 * 2 >= r) ++A; long long h1 = 2 * A * r - r; long long h2 = h * ...
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(10); size_t n, failedStep = 0; long long a; unordered_set<long long> nums; cin >> n; for (size_t i = 0; i < n; ++i) { ++failedStep; cin >> a; if (a != 0 && nums.find(a - 1) == ...
2
#include <bits/stdc++.h> using namespace std; long long n, a[1000006], sum[1000006]; long long Exponentiation(long long x, long long n) { long long res = 1; while (n) { if (n % 2) { res *= x; res %= 1000000007; } x *= x; x %= 1000000007; n /= 2; } return res % 1000000007; } int m...
7
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; 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; vector<pair<long long, long long>> v; long long i, x, y; for (i = 0; i < ...
4
#include <bits/stdc++.h> using namespace std; int V1 = 0, V2 = 0, l[100000], r[100000]; int E, to[800000], nex[800000], last[100000]; void hk_init() { E = 0; V1 = 0; V2 = 0; memset(last, -1, sizeof last); } void hk_add_edge(int u, int v) { to[E] = v; nex[E] = last[u]; last[u] = E++; } bool visited[100000]...
14
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; int n, m, q, sol[N], b[1000][1000], sh[1000], all[1000]; vector<int> g[N], at[N]; void DFS(int v, int sum) { bool inv = false; if (at[v][0] == 1) { int x = b[at[v][1]][at[v][2]] ^ sh[at[v][1]]; if (x == 0) { ++sum; inv = true; ...
14
#include <bits/stdc++.h> using namespace std; const unsigned int M = 1000000007; void solution() { long long n; cin >> n; long long* a = new long long[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; } long long* b = new long long[n]; for (long long i = 0; i < n; i++) { cin >> b[i]; } long...
0
#include <bits/stdc++.h> using namespace std; char a[1005]; string s; int main() { cin >> s; for (int i = 0; i < s.length(); i++) a[i] = s[i]; long long ans = 0, t = 1; for (int i = 0; a[i]; i++) { ans = ans * 4; if (a[i] == '1') ans += t; t = t * 2; ans = ans % 1000000007; t = t % 100000000...
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 4e5 + 50; struct edge { int nxt, to, col, tag; } e[MAXN * 2]; int head[MAXN], edgetot = 1; int data[MAXN], l[MAXN], r[MAXN], cnt, deg[MAXN]; int N, it; void add(int x, int y) { e[++edgetot].to = y; e[edgetot].nxt = head[x]; head[x] = edgetot; deg[...
8
#include <bits/stdc++.h> using namespace std; int num0[200050 << 2], num1[200050 << 2], lazy[200050 << 2]; int in[200050], out[200050], val[200050]; int a[200050]; int n, q, step; vector<int> e[200050]; void init() { memset(in, 0, sizeof(in)), memset(out, 0, sizeof(out)); memset(num0, 0, sizeof(num0)), memset(num1,...
12
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int i, a[n]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int x = a[n - 1]; i = n - 2; while (i > 0) { if (x % a[i] != 0) break; else if (x % a[i] == 0 && (a[i - 1] == a[i] || a[i] == a[i + 1])) break...
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; int sum1 = 0, sum2 = 0, index = 0, mi = 999999; map<int, int> mp; for (int i = 0; i < n; i++) { cin >> arr[i]; mp[arr[i]]++; if (mi > arr[i]) { mi = arr[i]; index = i; } } if (n == 1 || mp...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1000; vector<int> g[maxn]; bool was[maxn]; int n; set<int> s1, s2; void Read() { s1.clear(); s2.clear(); for (int i = 0; i < maxn; ++i) g[i].clear(), was[i] = false; scanf("%d", &n); int a, b; for (int i = 1; i < n; ++i) { scanf("%d%d", &a, ...
11
#include <bits/stdc++.h> using namespace std; const int maxh = 200 * 1000 + 5; struct item { int pos, hash; item(int pos, int hash) : pos(pos), hash(hash) {} }; bool operator<(item a, item b) { return a.pos < b.pos; } int n, m, h; set<item> S[maxh]; map<int, int> M; int pos[maxh]; int Len[maxh]; int Base[maxh]; boo...
11
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; int b[a], c = 0, d = 0; for (int i = 0; i < a; i++) { cin >> b[i]; if (b[i] < 0) { c++; } } sort(b, b + a); if (a == 1) { cout << b[a - 1] << endl; return 0; } if (c % 2 != 0) { c--; } for (in...
6
#include <bits/stdc++.h> using namespace std; long long t, n, m, k, l, r, a[30], b[10004], ans, sum, tot; char s[300050][55]; long long find(long long m) { return m == a[m] ? m : a[m] = find(a[m]); } int main() { cin >> n; for (int i = 0; i < 30; i++) a[i] = i; memset(a, -1, sizeof(a)); for (int i = 0; i < n; i...
7
#include <bits/stdc++.h> using namespace std; template <typename S, typename T> ostream& operator<<(ostream& out, pair<S, T> const& p) { out << '(' << p.first << ", " << p.second << ')'; return out; } template <typename T> ostream& operator<<(ostream& out, vector<T> const& v) { int l = v.size(); for (int i = 0;...
12
#include <bits/stdc++.h> using namespace std; const int maxn = 100005, maxm = 100005, INF = 1000000000; inline int read() { int out = 0, flag = 1; char c = getchar(); while (c < 48 || c > 57) { if (c == '-') flag = -1; c = getchar(); } while (c >= 48 && c <= 57) { out = (out << 3) + (out << 1) + c...
17
#include <bits/stdc++.h> using namespace std; int N, X, Y; int deg[200100]; int DP[200100][2]; vector<int> G[200100]; void DFS(int node, int father) { int nrSons = 0; for (vector<int>::iterator it = G[node].begin(); it != G[node].end(); it++) { if (*it != father) { nrSons++; DFS(*it, node); } ...
14
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool smin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <typename T> inline bool smax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const long long N = (long long)2e5 + 10; long long n, q; string s; struct node { long lo...
18
#include <bits/stdc++.h> using namespace std; int main() { int i, j, n; long long int a, sq; cin >> n; while (n--) { cin >> a; sq = sqrt(a); for (j = 2; j * j <= sq; j++) { if (sq % j == 0) break; } if ((j * j > sq) && (sq * sq == a) && (a > 1)) cout << "YES\n"; else co...
5
#include <bits/stdc++.h> using namespace std; const int INFL = (int)1e9; const long long int INFLL = (long long int)1e18; const double INFD = numeric_limits<double>::infinity(); const double PI = 3.14159265358979323846; bool nearlyeq(double x, double y) { return abs(x - y) < 1e-9; } bool inrange(int x, int t) { return ...
9
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const int INF = 1 << 30; const long long LINF = 1ll << 60; const int BUFFER_SZ = 1 << 15; const int MOD = 1000 * 1000 * 1000 + 7; char BUFFER[BUFFER_SZ]; int gi() { int x; scanf("%d", &x); return x; } double gd() { double x; scanf("%lf", &...
10
#include <bits/stdc++.h> using namespace std; using ll = long long; constexpr int NMAX = 100010; int pfac[NMAX]; ll byKFreePart[NMAX]; int kFreeConjugate[NMAX]; int kFreePart[NMAX]; void fillWithPFactors(int x, map<int, int> &m) { if (x == 1) return; ++m[pfac[x]]; fillWithPFactors(x / pfac[x], m); } int main() { ...
10
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int *p = new int[100010]; fill(p, p + 100010, -1000000000); for (int i = 0; i < n; ++i) { int x, y; int res = 0; scanf("%d%d", &x, &y); int q = (int)sqrt((double)x); for (int j = 1; j <= q; ++j) { if (x % j =...
11
#include <bits/stdc++.h> using namespace std; struct Edge{ int to, nxt, ind; Edge() {} }; struct EDGE{ int u, v; EDGE() {} }; const int MAX_N = 2e5 + 7; Edge e[MAX_N << 1]; EDGE E[MAX_N]; int f[33] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 1771...
16
#include <bits/stdc++.h> using namespace std; int x[1000000]; int s[1000000]; bool vis[1000][2]; long long dp[1001][2]; long long DP(int i, int m) { if (i == 40) return m == 0; if (vis[i][m]) return dp[i][m]; vis[i][m] = 1, dp[i][m] = 0; if (x[i] == 1) { if ((m + 1) % 2 != s[i]) return dp[i][m] = 0; ...
9
#include <bits/stdc++.h> using namespace std; long long dem[102], res = 0, n, k, x; int i, j; void En() { cin >> n >> k; for (i = 1; i <= n; i++) { cin >> x; dem[x % k]++; } } void Sol() { for (i = 1; i <= k - 1; i++) { if (i < k - i) { res += min(dem[i % k], dem[(k - i) % k]); } else if (...
4
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int mod = 100000; int main() { int i, n; unsigned long long ans; int a[11]; while (~scanf("%d", &n)) { for (i = 5; i >= 1; --i) { a[i] = n % 10; n /= 10; } swap(a[2], a[5]); swap(a[2], a[3]); for (i...
6
#include <bits/stdc++.h> long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } int main() { long long n, m; scanf("%I64d%I64d", &n, &m); long long cn = 1, ci; while (n--) scanf("%I64d", &ci), cn = lcm(cn, ci) % m; if (cn...
10
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 500; const long long mod = 1e9 + 7; const long long INF = 1LL << 57; const int LN = 22; long long n, m, u, v, k, t, q, x, d, p, a, b, c, s; int arr[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> k; n = 2000; arr[0] = -1; for...
12
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-12; const int N = 100000 + 10; long double sqr(long double x) { return x * x; } struct PT { long double x, y; PT(long double x = 0, long double y = 0) : x(x), y(y) {} void in() { double xx, yy; scanf("%lf%lf", &xx, &yy); x = xx, ...
20
#include <bits/stdc++.h> using i64 = long long; using namespace std; const int nxM = int(1e4); struct edge { int u, v, w; }; bool cmp(const edge &a, const edge &b) { return a.w < b.w; } i64 n, sz[nxM], sum_x[nxM], par[nxM], s, x[nxM]; vector<edge> adj; int find(int u) { if (par[u] != u) par[u] = find(par[u]); ret...
19
#include <bits/stdc++.h> using namespace std; int N, M, last; long long dp[2005][2 * 2005][2]; bool visited[2005]; vector<int> edges[2005], a; map<int, int> cnt; int dfs(int v) { visited[v] = true; for (int u : edges[v]) { if (visited[u]) continue; return 1 + dfs(u); } return 1; } long long solve(int po...
18
#include <bits/stdc++.h> using namespace std; int main() { int i, n; scanf("%d", &n); int s[n]; for (i = 0; i < n; i++) { scanf("%1d", &s[i]); } int c, d; c = d = 0; for (i = 0; i < n; i++) { if (s[i] == 1) { c++; } else { d++; } } if (c != d) { printf("1\n"); for...
0
#include <bits/stdc++.h> using namespace std; const int maxi = ~0U >> 1; const int mn = 100010; int n, m, tot; vector<pair<int, int> > mp[mn]; int dp[mn][3]; int vis[mn]; void dpp(int x) { vis[x] = 1; int max1 = 0, max2 = 0; for (int i = 0; i < mp[x].size(); i++) { int j = mp[x][i].first; int tp = mp[x][i...
13
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; int n, m, q, op[100010], X[100010], Y[100010], Head[100010], Next[100010], Go[100010], Ans[100010], Cnt = 0, nans = 0; bool B[1010][1010]; inline void addedge(int x, int y) { Go[++Cnt] = y, Next[Cnt] = Head[x], Hea...
14
#include <bits/stdc++.h> using namespace std; int main() { int A, B; cin >> A >> B; int N = A + B + 1; for (int i = 0; i < A; ++i) cout << i + 1 << " "; for (int i = N; i >= N - B; --i) cout << i << " "; cout << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e6; int a[N + 2], b[N + 2], fr[N + 2], vis[N + 2], cl[N + 2]; vector<int> pa[N + 2], pb[N + 2], bf; vector<pair<int, int> > adj[N + 2]; int f; void dfs(int node, int c) { if (vis[node]) { if (cl[node] != c) f = 1; return; } bf.push_back(node); ...
15
#include <bits/stdc++.h> using namespace std; const int inf = (1 << 30) - 1; const long long linf = (1ll << 62) - 1; const int N = 1 << 16; pair<int, int> d[N]; vector<pair<int, int> > ans; set<pair<pair<int, int>, int> > s; void decrease(int u, int v) { s.erase(make_pair(d[v], v)); d[v].second ^= u; d[v].first--...
7
#include <bits/stdc++.h> using namespace std; int a[1000010]; int main() { int n, i, j, k; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } int curr = 1; sort(a, a + n); for (i = 0; i < n; i++) { if (a[i] >= curr) { curr++; } } cout << curr << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int main() { int x, y, a, b, gc; scanf("%d%d%d%d", &x, &y, &a, &b); gc = gcd(x, y); printf("%d", b / (x * y / gc) - (a - 1) / (x * y / gc)); return 0; }
4
#include <bits/stdc++.h> using namespace std; int cnt[1 << 7]; int main() { int n, k; scanf("%d %d", &n, &k); memset(cnt, 0, sizeof cnt); for (int i = 0; i < n; ++i) { int tt = 0, x; for (int j = k - 1; j >= 0; --j) { scanf("%d", &x); tt |= (x << j); } cnt[tt]++; for (int i = 0; ...
7
#include<bits/stdc++.h> using namespace std; #define int long long const int N=2e5+10; int t,n,m,a[N],b[N]; signed main(){ cin>>t; while(t--){ int n,x; cin>>n>>x; for(int i=1;i<=n;i++)cin>>a[i]; for(int i=1;i<n;i++)cin>>b[i]; int ans=INT_MAX,sum=0,day=0; for(int i=1;i<=n;i++){ if(i==1){ ans=min(an...
11
#include <bits/stdc++.h> using namespace std; int du[100010]; int h, t; int p1[200010]; int q1; int p2[200010]; int q2; int pp[200010]; int qq; vector<int> g[100010]; bool judge(int x, int y) { if (du[x] - 1 < h || du[y] - 1 < t) return false; int i, j; i = j = 0; qq = q1 = q2 = 0; while (i < du[x] || j < du[...
12
#include <bits/stdc++.h> char a[100 * 1024], b[100 * 1024]; int main() { gets(a); gets(b); int c7 = 0, e7 = 0, e4 = 0; int i; for (i = 0; a[i]; i++) { c7 += (a[i] == '7'); c7 -= (b[i] == '7'); e7 += (a[i] != b[i] && a[i] == '7'); e4 += (a[i] != b[i] && a[i] == '4'); } int res = 0; if (c7...
4
#include <bits/stdc++.h> #pragma gcc optimize(2) using namespace std; const int maxn = 1e5 + 10; vector<int> g[maxn]; pair<int, int> pt[maxn]; int ans; int main() { int t; cin >> t; while (t--) { int n; ans = -1; cin >> n; for (int i = 1; i <= n; i++) g[i].clear(); for (int i = 1; i <= n; i++)...
5
#include <bits/stdc++.h> using namespace std; const int N = (int)1e6 + 500; int a[N]; int n; long long ans, x, y; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); x = -a[0], y = a[0]; for (int i = 0; i < n; i++) { ans = max(x + a[i], y - a[i]); x = max(x, ans - a[i + 1]); ...
16