solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; template <typename T> void join(vector<T>& arr, string sep) { bool first = true; for (auto t : arr) { if (first) { first = false; cout << t; } else { cout << sep << t; } } cout << "\n"; } string convertToNewString(vector<char>& qq) { ...
3
#include <bits/stdc++.h> using namespace std; string s; int n; vector<int> ans; bool check(int cl, int b, int turn) { if (turn == n) return true; for (int i = 1; i < s.size(); i++) { bool k = true; if (s[i] == '1' && i > cl && i != b) { if (check(i - cl, i, turn + 1)) { ans.push_back(i); ...
4
#include <bits/stdc++.h> int main(void) { int t; scanf("%d", &t); while (t--) { int l, i, f = 0; scanf("%d", &l); char a[l + 1]; scanf("%s", a); for (i = 0; i < l / 2; i++) { if (a[i] == a[l - 1 - i] || abs(a[i] - a[l - i - 1]) == 2) continue; else { f = 1; ...
1
#include <bits/stdc++.h> using namespace std; int main() { int i, j, n, x, y, loc; cin >> n >> x >> y; int a[n + 1]; for (i = 1; i <= n; i++) { cin >> a[i]; } for (i = 1; i <= n; i++) { int flag = 0; for (j = i - x; j <= i + y; j++) { if (j >= 1 && j <= n) { if (a[i] <= a[j]) { ...
1
#include <bits/stdc++.h> using namespace std; const long long M = 10004205361450474; long long dp[10010][6]; long long t[10010]; int main() { for (int i = 1; i <= 10000; i++) dp[i][0] = 0; for (int j = 1; j <= 4; j++) for (int i = 1; i <= 10000; i++) { long long now = i; dp[i][j] = dp[i][j - 1]; ...
11
#include <bits/stdc++.h> using namespace std; int main() { long long a, b; string s; cin >> a >> b >> s; int dix = 1, diy = 1; if (a < 0) dix = -1, a = -a; if (b < 0) diy = -1, b = -b; map<char, int> dx, dy; dx['U'] = 0; dx['D'] = 0; dx['L'] = -1; dx['R'] = 1; dy['U'] = 1; dy['D'] = -1; dy['...
4
#include <bits/stdc++.h> using namespace std; int main() { char a[55]; scanf("%s", a); int len = strlen(a); int count = 0, c = 0; for (int i = 0; i < len - 1; i++) { if (a[i] == a[i + 1]) count++; } for (int i = 0; i < len; i++) { if (a[i] != a[len - i - 1]) { c++; } } if (count == l...
0
#include <bits/stdc++.h> using namespace std; int x[100000]; int y[100000]; int n, a; const double PI = acos(-1); double normalize(double theta) { if (theta < -PI) theta += 2 * PI; if (theta >= PI) theta -= 2 * PI; return theta; } bool seen[100005]; bool can(double r) { vector<pair<double, int> > events; for ...
12
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; const long long INF = 1e9; const long long MOD = 1e9 + 7; const int NMAX = 50; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } vector<long long> del(long long n) { vector<long long> ans; long long i = 1; for (i...
2
#include <bits/stdc++.h> using namespace std; struct hammer { double x[4]; double y[4]; } box[200]; int n, m; double xt; double yt; int main() { cin >> n; box[1].x[3] = box[1].y[3] = 0; box[1].x[2] = 10; box[1].y[2] = 0; double agg = 10 * cos(acos(-1) * 72.0 / 180.0); double aee = 10 * sin(acos(-1) * 72...
7
#include <bits/stdc++.h> using namespace std; int main() { int i, j, x = 1; char str[10]; for (i = 0; i < 8; i++) { scanf("%s", str); for (j = 0; j < 7; j++) if (str[j] == str[j + 1]) { x = 0; break; } } if (x == 1) printf("YES"); else printf("NO"); return 0; }
1
#include <bits/stdc++.h> using namespace std; int a, b, c; int main() { cin >> a >> b; if (a == b) cout << a; else cout << 2; return 0; }
1
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } int n, m; bool isPrime[10000005]; int sp[10000005]; map<int, int> num; map<int,...
5
#include <bits/stdc++.h> using namespace std; int main() { long long id = 1, x, y, q, i, j; long double s, ans, temp; vector<long long> a; cin >> q; cin >> x >> y; a.push_back(y); s = y * 1.0; for (i = 1; i < q; i++) { scanf("%lld", &x); if (x == 2) { ans = a.back() * 1.0000 - ...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; vector<int> v[maxn]; int f[maxn]; int du[maxn]; int n; struct node { int cnt, deep; }; int pos; void work() { queue<node> q; node c; c.cnt = 1; c.deep = 0; q.push(c); int ans = -1; pos = -1; while (!q.empty()) { node d = q.fro...
3
#include <bits/stdc++.h> using namespace std; vector<int> dp(2002); vector<int> cumdp(2002); int main() { ios_base::sync_with_stdio(false); string s; cin >> s; dp[0] = 1; int n = s.length(); cumdp[0] = 1; for (register int i = 1; i < n; i++) { dp[i] = 1; for (register int j = 0; j < i; j++) { ...
6
#include <bits/stdc++.h> using namespace std; set<pair<int, int> > a, b; int f[100005], g[100005]; int main() { int n, s, l, i, x = 0; scanf("%d%d%d", &n, &s, &l); for (i = 1; i <= n; i++) scanf("%d", &g[i]); a.clear(); b.clear(); f[0] = 0; for (i = 1; i <= n; i++) { a.insert(make_pair(g[i], i)); ...
6
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k = 0, c = 1, a[6], res[2], n = 0; cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5]; sort(a, a + 6); for (i = 1; i < 6; i++) { if (a[i] != a[i - 1]) c++; } if (c > 3) cout << "Alien\n"; else if (c == 1) cout << "Elephant\n...
1
#include <bits/stdc++.h> using namespace std; int mp[27][27]; int dp[111][111][26]; bool flag[111][111][26]; char s[111]; inline void checkmax(int &a, int b) { if (a < b) a = b; } int main() { int i, j, k, n, m; scanf("%s", s); scanf("%d", &k); scanf("%d", &n); for (i = 1; i <= n; i++) { getchar(); ...
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); long long n, k; cin >> n >> k; vector<long long> d; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { d.push_back(i); if ((n / i) != i) { d.push_back(n / i); } } } sort(d...
3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int MOD = 1000000007; long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } void solve() { int ...
3
#include <bits/stdc++.h> using namespace std; struct edge { int v, k; edge(int _v, int _k) { v = _v; k = _k; } }; const int N = 1e5 + 5; int n, ptr[N], f[N], par[N]; vector<edge> a[N]; vector<int> vec; void enter() { scanf("%d", &n); for (int(i) = (1), _b = (n - 1); (i) <= _b; ++(i)) { int u, v, k...
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string s; cin >> s; while (m--) { int l, r; char c1, c2; cin >> l >> r >> c1 >> c2; for (int i = l - 1; i < r; i++) { if (s[i] == c1) s[i] = c2; } } cout << s; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> void deb(initializer_list<T> l) { for (auto &e : l) cout << e << ' '; cout << '\n'; } void solve() { vector<long long> e(4); for (auto &g : e) cin >> g; sort(e.begin(), e.end()); cout << e[3] - e[0] << ' ' << e[3] - e[1] << ' ' << e[3] - e[...
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; bool mat[1000][1000]; int ans[1000][1000]; bool visited[1000][1000]; int way[4][2] = {{0, 1}, {-1, 0}, {1, 0}, {0, -1}}; bool inGrid(int i, int j, int N, int M) { return ((0 <= i && i < N) && (0 <= j && j < M)); } int realAnswers...
4
#include <bits/stdc++.h> using namespace std; static inline void canhazfast() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0,...
4
// Problem: B. DMCA // Contest: Codeforces - April Fools Day Contest 2021 // URL: https://codeforces.com/contest/1505/problem/B // Memory Limit: 256 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) #include <bits/stdc++.h> #define pb push_back #define eb emplace_back using namespace std; t...
4
#include <bits/stdc++.h> using namespace std; #pragma warning(disable : 4996) const double eps = 1e-9; struct point { double x, y; }; bool comp(double x, double y) { if (x + eps < y) return true; else return false; } int main(void) { vector<point> points[4]; vector<double> angles[4]; vector<double> ...
5
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; vector<int> solve(int n, const vector<int> &sender, const vector<vector<int> > &mention) { int m = sender.size(); if (0) { for (int i = (int)(0); i < (int)(m); i++) { cout << sender[i] << ":"; for (int...
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1000; int n, k, x, y, pre[1111], dp[1111], dpp[1111], mx, num[1111], ans; vector<int> c[111111]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= k; i++) { scanf("%d%d", &x, &y); c[y].push_back(n - x + 1); } for (int i = 0; i <= maxn...
10
#include <bits/stdc++.h> using namespace std; const int N = 234567; int n, w, k; struct Song { int pleasure; int length; int id; Song() {} inline bool operator<(const Song &other) const { if (length != other.length) { return length < other.length; } return id < other.id; } }; Song a[N]; st...
7
#include <bits/stdc++.h> using namespace std; long long sq(long long x) { return x * x; } long long first(long long l, long long p) { long long w = l / p; long long p2 = l % p; long long p1 = p - p2; return p1 * sq(w) + p2 * sq(w + 1); } void solve() { long long n, k; cin >> n >> k; vector<long long int> ...
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; map<long long, long long> mp; string st = "codeforces"; for (int i = 0; i < 10; i++) { mp[i] = 1; } long long sum = 1; int in = 0; while (sum < n...
3
#include <bits/stdc++.h> using namespace std; int main() { long long a, k, mi, ma, ans, test, digit, t; cin >> t; while (t--) { cin >> a >> k; ans = a; for (int i = 1; i < k; ++i) { mi = 10; ma = 0; test = ans; a = ans; while (a > 0) { digit = a % 10; mi =...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; struct sofa { int x1, y1, x2, y2; int minx() { return min(x1, x2); } int maxx() { return max(x1, x2); } int miny() { return min(y1, y2); } int maxy() { return max(y1, y2); } }; sofa sofas[maxn]; int cntup[maxn], cntdo[maxn], cntle[maxn], c...
6
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int a[3], s = 0; for (int i = 0; i < 3; i++) { cin >> a[i]; } sort(a, a + 3); if (a[2] > a[1] + a[0]) { cout << a[0] + a[1] << endl; } else { cout << (a[0] + a[1]...
1
#include <bits/stdc++.h> using namespace std; long long sum(vector<long long> arr) { long long s = 0; for (int i = 0; i < arr.size(); i++) s += arr[i]; return s; } int main() { int n, i; long long x; cin >> n; vector<long long> arr, arr1; for (i = 0; i < n; i++) { cin >> x; arr.push_back(x); }...
1
#include<bits/stdc++.h> #define ll long long #define fori(i,init,end) for(ll i=init;i<end;i++) #define makearr(arr,n) ll arr[n]; fori(i,0,n) cin>>arr[i]; #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); using namespace std; void solve() { ll n,k; cin>>n>>k; while(k>3) { cout<<1<...
4
#include <bits/stdc++.h> using namespace std; const int N = 1e5; long long x[N], m[N]; vector<long long> solve(long long n) { if (n <= 1) return vector<long long>(1, n); long long p = 1; while (p < n) p <<= 1; if (p > n) p >>= 1; --p; int bits = 0; for (long long temp = p; temp > 0; temp >>= 1) ++bits; ...
6
#include <bits/stdc++.h> using namespace std; const int maxv = 10000001; const int maxq = 4000; int r[maxv]; void calc() { for (int i = 1; i <= maxq; ++i) { int t = i * i; for (int j = t, jj = 1; j < maxv; ++jj, j += t) r[j] = jj; } } int main() { calc(); int a, n; cin >> a >> n; long long R = 0; ...
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int n_n = n * n; int answer = 0; for (int a = 1; a <= n; a++) { for (int b = a; b <= n; b++) { int c = (a * a) + (b * b); if (c > n_n) { break; } int temp = sqrt(c); temp *= temp; if (te...
2
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 23; const int MOD = 1e9 + 9; const int MAXN = 1e3 + 100; string name[MAXN]; int main() { ios_base::sync_with_stdio(0); string s, cur = ""; cin >> s; int slen = s.length(); int ans = 0, level = 0; for (int i = 0; i < slen; ++i) { if (isa...
4
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; long long vvod, n; vector<long long> mas; long long ost(long long div, long long mod) { if (div >= 0) { return div % mod; } else { return mod - (div * -1 % mod); } } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i =...
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; string s; cin >> n >> s; int a[n], cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == 'R') { a[i] = 0; } else if (s[i] == 'G') { a[i] = 1; } else { ...
3
#include <bits/stdc++.h> using namespace std; const int N = 1000 * 1000; int lens[N]; int k; long long solve(long long a, long long b) { k = 0; for (long long i = 1; i * i <= b; ++i) if (b % i == 0) lens[k++] = i; long long ans = 2 * (a + b) + 2; long long x = a + b; int l = 0; for (long long i = 1; i *...
6
#include <bits/stdc++.h> template <typename T> std::istream& operator>>(std::istream& input, std::vector<T>& v) { for (T& a : v) input >> a; return input; } void answer(unsigned v) { std::cout << v << '\n'; } void solve(unsigned n, unsigned k, const std::vector<unsigned>& a) { const size_t c = a.size(); unsigne...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 50; int xx[4] = {0, 0, -1, 1}; int yy[4] = {1, -1, 0, 0}; const long long mod = 1e9 + 7; long long c, n, k; double d; int a[maxn]; queue<pair<int, int> > q; long long ans[maxn]; int b[maxn], m; multiset<pair<int, int> > v[maxn]; int check(int x) { i...
4
#include <bits/stdc++.h> using namespace std; const int maxn = 5 * 100005; vector<int> a[maxn]; int n, m, k, lim; int head[maxn], Next[maxn * 2], to[maxn * 2], tot = 0; int d[maxn], fa[maxn], size[maxn]; bool vis[maxn]; void add(int x, int y) { to[++tot] = y; Next[tot] = head[x]; head[x] = tot; } void dfs_tree(in...
9
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; const long long inf = 4e18; int n, l; long long lx = -inf, rx = inf, ly = -inf, ry = inf; void rd(int &x) { std::cin >> x; } void rd(long long &x) { std::cin >> x; } void prints(char *x) { puts(x); } struct P { long long t, x, y, k; int w; inlin...
11
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; long long hcf = gcd(b, a % b); return hcf; } long long findhcf(long long *arr, int n) { long long num = arr[0]; for (int i = 1; i < n; i++) { num = gcd(arr[i], num); if (num == 1) return 1; ...
2
#include <bits/stdc++.h> using namespace std; int l, n, ans, a[100 + 5], b[100 + 5], vis[3000 + 5][100 + 5][5], memo[3000 + 5][100 + 5][5]; int dp(int now, int last, int state) { if (now == l) return 1; if (now > l) return 0; if (memo[now][last][state] > -1) return memo[now][last][state]; int i, k, ret = 0;...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 105; int a[maxn]; int main() { int t; cin >> t; while (t--) { int n, d; cin >> n >> d; for (int i = 1; i <= n; i++) { cin >> a[i]; } if (n == 1) { cout << a[1] << endl; } else { for (int i = 2; i <= n; i++) { ...
0
#include <bits/stdc++.h> using namespace std; #define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl; #define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl; #define Get(x,y) scanf("%I64d%I64d",&x,&y); #define is_bit(x,y) (x%(1<<(y+1))>=(1<<y)) #define eror(x) cout<<#x<<'='<<(x)<<endl; #define f_(i,a,b) for(int...
12
#include <bits/stdc++.h> using namespace std; const long INF = (long)1e09; int n, m, k, p = 0, id, inv, minv, ans, d = -2; int main() { scanf("%d", &n); if (n == 3) { cout << 5; return 0; } if (n == 1) { cout << 1; return 0; } for (m = 3; (m * m + 1) / 2 < n; m += 2) ; printf("%d", m);...
4
#include <bits/stdc++.h> using namespace std; int a[100050], b[100050]; long double x[100050]; int main() { int n, p; scanf("%d%d", &n, &p); for (int(i) = 0; (i) < (n); (i)++) { scanf("%d%d", &a[i], &b[i]); } long long sum = 0; for (int(i) = 0; (i) < (n); (i)++) sum += a[i]; if (sum <= p) { printf...
5
#include <bits/stdc++.h> using namespace std; const long long BASE = 1e9 + 7; const long double EPS = 1e-9; const long long EPS_INT = 1e12; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } inline long long lcm(long long a, long long b) ...
0
#include <bits/stdc++.h> using namespace std; const int N = 2005; string s; long long dp[N][N]; bool pl[N][N]; inline void go() { int n = int(s.size()); for (int i = int(0); i < int(n); ++i) pl[0][i] = pl[1][i] = true; for (int tam = int(2); tam < int(n + 1); ++tam) for (int i = int(0); i < int(n - tam + 1); ...
3
#include <bits/stdc++.h> using namespace std; int main() { string x, y; cin >> x >> y; for (int i = x.length() - 1; i >= 0; i--) { while (x[i] < 'z') { ++x[i]; if (x < y) { cout << x << endl; return 0; } } if (x[i] == 'z') { x[i] = 'a'; } } cout << "No s...
4
#include <bits/stdc++.h> using namespace std; void solve() { long long n, m; cin >> n >> m; vector<long long> v(1001, 0); for (int i = 0; i < n; i++) { long long input; cin >> input; v[input]++; } long long ans = 0; for (int i = 0; i < m; i++) { long long input; cin >> input; if (v...
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1000001; int nums[maxn]; pair<int, int> stk[maxn]; int main() { int N = 0; scanf("%d", &N); int M = 0, MPos = -1, MNum = 1; for (int i = 0; i < N; i++) { scanf("%d", &nums[i]); if (M < nums[i]) { M = nums[i]; MPos = i; MNum...
8
#include <bits/stdc++.h> using std::max; using std::pair; const int maxn = 1000010, maxk = 19; const int P = 1000000009; int n; pair<int, int> a[maxn]; int w[maxn][maxk]; int l[maxn], r[maxn], lb[maxn], rb[maxn]; pair<int, int> f[maxn]; inline int gmax(const int &x, const int &y) { return a[x].second > a[y].second ? ...
11
#include <bits/stdc++.h> using namespace std; int st[100100], ed[100100], vst[100100]; pair<pair<int, int>, int> p[100100]; set<pair<int, int> > s; set<pair<int, int> >::iterator it; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int r, c; scanf("%d %d", &r, &c); st[i] = n - c + ...
7
#include <bits/stdc++.h> using namespace std; vector<int> adj[200105]; int xMove[] = {0, 1, 0, -1, 1, 1, -1, -1, 2, 2, -2, -2, 1, 1, -1, -1}; int yMove[] = {1, 0, -1, 0, 1, -1, 1, -1, 1, -1, 1, -1, 2, -2, 2, -2}; int main() { cin.tie(0), cout.tie(0); ios_base::sync_with_stdio(0); cout.setf(ios::fixed); cout.pre...
6
#include <bits/stdc++.h> using namespace std; const int N = 2036; int a[N]; int c[N], r[N], d, d2; int sc[N], sr[N], sd, sd2; const int ITER = 1999; int main() { int n; scanf("%d", &n); for (int i = (0); i < (int)(n * n); i++) scanf("%d", a + i); int s = accumulate(a, a + n * n, 0) / n; int sco = 1999; whil...
6
#include <bits/stdc++.h> using namespace std; class Solution { public: void solve(std::istream &in, std::ostream &out) { long long n, k, l; in >> n >> k >> l; std::vector<long long> a(n * k); for (auto &el : a) in >> el; std::sort(a.begin(), a.end()); std::vector<bool> b(n * k); auto l_in...
3
#include <bits/stdc++.h> using namespace std; int main() { int i, j, n; string s; cin >> n; for (i = 0; i < n; i++) { s += "a"; } for (i = 2; i < n; i = i + 4) { s[i] = 'b'; if (i + 1 < n) { s[i + 1] = 'b'; } } cout << s << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const double eps = 1e-7; bool g[5][5]; int main() { int m; cin >> m; for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; g[a - 1][b - 1] = true; g[b - 1][a - 1] = true; } for (int i = 0; i < 5; ++i) for (int j = i + 1; j < 5; ++j) for ...
2
#include <bits/stdc++.h> using namespace std; long long n, m; char ch[255][255]; int main() { cin >> m >> n; for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= m; j++) { cin >> ch[i][j]; ch[i + n][j] = ch[i][j]; } } for (long long i = 1; i <= m; i++) { for (long long j = 1; ...
2
#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, m; cin >> n >> m; vector<vecto...
4
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = 1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return f ? -x : x; } int dp[210][210], n; int main() { n = read(...
7
#include <bits/stdc++.h> using namespace std; long long n, q; long long a[300005], dp_child[300005], dp_par[300005], con[300005]; long long now, tin[300005], tout[300005], up[300005][19], val[300005][19], val2[300005][19]; vector<pair<long long, long long> > g[300005]; void get_child(long long u, long long p) { f...
9
#include <bits/stdc++.h> using namespace std; int main() { int num[100000]; int n; int indexa, indexb; int suma, sumb; suma = sumb = 0; indexa = indexb = 1; cin >> n; for (int i = 0; i < n; i++) { cin >> num[i]; } suma += num[0]; sumb += num[n - 1]; if (n == 1) { cout << "1 0" << endl; ...
2
#include <bits/stdc++.h> using namespace std; const int INF = 1000000009; const int MOD = 1000000007; vector<string> v; int n, m; void borra(int p) { for (int i = 0; i < n; i++) { v[i].erase(v[i].begin() + p); } } int problem() { int flag = 1; int filaP = 0; for (int i = 1; i < n; i++) { if (v[i] < v[...
3
#include <bits/stdc++.h> using namespace std; int k[30][100002]; void die() { printf("NO"); exit(0); } int l[100001], r[100001], v[100001], ans[100000]; int main() { int n, m, i, j, a, b, c; scanf("%d%d", &n, &m); for (i = 0; i < m; i++) scanf("%d%d%d", l + i, r + i, v + i); for (i = 0; i < m; i++) { fo...
5
#include <bits/stdc++.h> using namespace std; long long a[] = {0, 74, 7744, 777444, 77774444, 7777744444}; long long b[] = {0, 47, 4477, 444777, 44447777, 4444477777}; long long ans = 1e18; void dfs(long long x, long long n, int L1, int L2) { if (L1 == 0 && L2 == 0) { if (x >= n) ans = min(x, ans); return; ...
2
#include <bits/stdc++.h> using namespace std; const int N = (int)101; int a[N][N], n, m, q, t[N * N], r[N * N], c[N * N], x; int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= q; i++) { scanf("%d", &t[i]); if (t[i] == 1) { scanf("%d", &r[i]); } else if (t[i] == 2) { scanf("%d", &...
3
#include <bits/stdc++.h> using namespace std; const int N = 100004; char s[N]; int sum[N][4]; char t[N]; vector<int> cntZero, pos; int main() { int n; scanf("%s", s + 1); n = strlen(s + 1); int i, j; int cntz = 0, cnto = 0, cntt = 0; for (i = 1; i <= n; i++) { if (s[i] == '2') { pos.push_back(i); ...
3
#include <bits/stdc++.h> using namespace std; int ask(string s) { cout << s << endl; int k; cin >> k; if (!k) exit(0); return k; } int main() { int x, y, n = ask("a") + 1; string s(n, 'a'); x = n == 301 ? n : ask(s); x == n&& ask(string(n - 1, 'b')); for (char& c : s) { c = 'b'; y = ask(s); ...
7
#include <bits/stdc++.h> using namespace std; const long long mod = (long long)1e9 + 9; map<pair<int, int>, int> number; struct cube { int x; int y; int num; cube() { x = y = num; } cube(int _x, int _y, int _num) { x = _x; y = _y; num = _num; } }; bool operator<(const cube &left, const cube &rig...
6
#include <bits/stdc++.h> using namespace std; long long int MOD = 1000000007; long long int inverse(long long int i) { if (i == 1) return 1; return (MOD - ((MOD / i) * inverse(MOD % i)) % MOD + MOD) % MOD; } int main() { int t; cin >> t; while (t--) { int n, k, l; cin >> n >> k >> l; int d[n + 1];...
5
#include <bits/stdc++.h> using namespace std; inline long long read() { long long f = 1, x = 0; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int main() ...
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int degree[n], xorsum[n]; queue<int> Q; int used = 0; for (int i = 0; i < n; ++i) { cin >> degree[i] >> xorsum[i]; if (degree[i] == 1) Q.push(i); if (degree[i] == 0) { used++; } } vector<pair<int, int> > an...
3
using namespace std; #include <iostream> #include <string> #include <vector> #include <algorithm> #include <sstream> #include <queue> #include <deque> #include <bitset> #include <iterator> #include <list> #include <stack> #include <map> #include <unordered_map> #include <set> #include <functional> #include <numeric> #i...
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; int dp[n]; for (int i = 0; i < n; i++) { cin >> a[i]; dp[i] = a[i]; } for (int i = n - 2; i >= 0; i--) { if (dp[i] == 0) { continue; } else { if (dp[i] == dp[i + 1] && dp[i] != 3) { dp...
3
#include <bits/stdc++.h> using namespace std; void solve() { string s; cin >> s; int m; scanf("%d", &m); int l, r, k; char sw[10002]; for (int i = 0; i < m; ++i) { scanf("%d%d%d", &l, &r, &k); --l; --r; k = k % (r - l + 1); for (int j = l; j <= r; ++j) { sw[j] = s[j]; } f...
2
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9; int main() { string s, t; cin >> s >> t; s = "##" + s; t = "##" + t; vector<vector<int> > dp(s.size() + 1, vector<int>(t.size() + 1, INF)); dp[0][0] = 0; for (int i = 1; i < s.size(); i++) { for (int j = 1; j < t.size(); j++) { ...
6
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long mod = 20071027; const double eps = 1e-6; template <class T> T gcd(T a, T b) { if (!b) return a; return gcd(b, a % b); } const int maxn = 2e3 + 10, sgm_sz = 26; long long a[maxn], n, k, dp[maxn]; inline long long d(long long x,...
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { long long n, x; cin >> n >> x; vector<long long> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } sort(v.begin(), v.end()); lon...
3
#include <bits/stdc++.h> using namespace std; long long d[100005]; long long c[100005]; long long a[100005]; int main() { long long n, p, q, r; while (cin >> n >> p >> q >> r) { for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { if (i == 0) { d[i] = a[i] * p; } else {...
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000000; int n, m; long long a[200100]; long long fibo[200100]; int main() { fibo[0] = fibo[1] = 1; for (int i = 2; i < 200100; i++) { fibo[i] = (fibo[i - 2] + fibo[i - 1]) % mod; } scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) ...
3
#include<bits/stdc++.h> using namespace std; #define fo(i,n) for(int i=0;i<n;i++) #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1) #define ll long long #define l long #define PI 3.1415926535897932384626 int main() { int t = 1; cin >> t; int n; while (t--) { cin>>n; int arr[n]; ...
0
#include <bits/stdc++.h> using namespace std; int n, m; set<pair<int, int> > vp; int main() { ios::sync_with_stdio(false); cin >> n >> m; if (n == 0) { cout << 0 << ' ' << 1 << endl; cout << 0 << ' ' << m << endl; cout << 0 << ' ' << 0 << endl; cout << 0 << ' ' << m - 1 << endl; return 0; } ...
5
#include <bits/stdc++.h> using namespace std; int n, k; vector<set<int>> adj; vector<int> leaf; void dfs(int cur, int par) { for (int x : adj[cur]) { if (x == par) continue; dfs(x, cur); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tt; cin >> tt; while (tt--) {...
7
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 100, INF = 2e9 + 100; string s; long long n, x1, y11, x2, y2, dx, dy; bool check(long long k) { long long X = dx * k; long long Y = dy * k; X += x1; Y += y11; if (abs(x2 - X) + abs(y2 - Y) <= n * k) return true; return false; } int32_t ...
5
#include <bits/stdc++.h> using namespace std; const int N = 200007, inf = 0x3f3f3f3f, M = 1000000007; vector<int> edges[N]; pair<long long, long long> cnt[N]; long long a[N]; pair<long long, long long> dfs(int u, int prev) { long long par, imp; par = imp = 0; for (int v : edges[u]) { if (v == prev) continue; ...
7
#include <bits/stdc++.h> using namespace std; int main() { string temp; bool freda, rinbow; int n; cin >> n; string* list = new string[n]; getline(cin, temp); for (int i = 0; i < n; i++) getline(cin, list[i]); for (int i = 0; i < n; i++) { freda = rinbow = false; if (list[i] == " " || list[i].le...
1
#include <bits/stdc++.h> using namespace std; int main() { int r, c; cin >> r >> c; set<int> row; set<int> col; for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { char c; cin >> c; if (c == 'S') { row.insert(i); col.insert(j); } } } int x = row.size...
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int q; cin >> q; string a[1010], b[1010], c[1010]; for (long long i = 1; i <= q; i++) cin >> a[i] >> b[i], c[i] = a[i]; int f[1010]; for (long long i = 1; i <= q; i++) { f[i] = i; for (long long j = 1; j <...
1
#include <bits/stdc++.h> using namespace std; template <class T> int getbit(T x, int pos) { return (x >> (pos - 1)) & 1; } template <class T> void turn_on(T &x, int pos) { x = x | ((T)1 << (pos - 1)); } template <class T> void turn_off(T &x, int pos) { x = x & ~((T)1 << (pos - 1)); } int read_int() { char r; ...
5
#include <bits/stdc++.h> using namespace std; int main() { int c, d, m, n, k, s = 0; cin >> c >> d >> n >> m >> k; if (k >= m * n) cout << "0"; else { int x = n * m - k; if (c > n * d) s = ceil((float)x * d); else { s = x / n * c + min(c, x % n * d); } cout << s; } }
3