solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> #pragma warning(disable : 4996) char A[1000010]; char B[1000010]; char C[1000010]; int dyn[1000000]; char dir[4] = {'N', 'E', 'W', 'S'}; int main() { int n; scanf("%d", &n); scanf("%s", A); scanf("%s", B); n--; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { if...
3
#include <bits/stdc++.h> using namespace std; char a[111]; int minn(int a, int b) { if (a > b) { return b; } else { return a; } } int main() { int i, k, r; while (~scanf("%s", &a[1])) { a[0] = 'a'; int l = strlen(a); int sum = 0, min; for (int i = 1; i < l; i++) { min = minn(abs(...
1
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, m; cin >> n >> m; if (n == 1) { cout << "0" << endl; } else if (n == 2) { cout << m << endl; } else { cout << 2 * m << endl; } } }
1
#include<iostream> #include<algorithm> #include<cmath> #define LL long long LL n, s; int main() { std::cin >> n >> s; int q = std::sqrt(n); if (n == s) { std::cout << s + 1 << std::endl; return 0; } for (LL i = 2; i <= q; i++) { LL copy = n; LL sum = 0; while (copy) { sum += copy % i; copy /= i; ...
0
#include <bits/stdc++.h> using namespace std; template <class T> T gcd(T a, T b) { if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } long long h[100000]; long long l[100000]; long long r[100000]; int main() { long long n; cin >> n; for (l...
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); string a, b; cin >> a >> b; reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); int cnt = 0; while (cnt != a.size() && cnt != b.size() && a[cnt] == b[cnt]) { cnt++; } cout << a.size() - cnt + b....
2
#include <stdio.h> int main(){ int i,j,n,m,a,b,c; int matrixA[101][101]; int matrixB[101]; scanf("%d %d",&n,&m); for(i=0;i<n;i++){ for(j=0;j<m;j++){ scanf("%d",&a); matrixA[i][j] = a; } } for(j=0;j<m;j++){ scanf("%d",&b); matrixB[j] = b; } c=0; for(i=0;i<n;i++){ for(j=0;j<m;j++){ c += ...
0
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,m; int a[100][10010]; int maxn=0; void dfs(int x) { if(x==n+1) { int t=0; for(int i=0;i<m;i++) { int s=0; for(int j=0;j<n;j++) if(a[j][i]==1) s++; t+=max(s,n-s); } maxn=max(maxn,t); retur...
0
#include <bits/stdc++.h> using namespace std; string s; int i; int main() { cin >> s; for (i = 0; i < s.size(); i++) if (s[i] == '0') { s.erase(i, 1); cout << s; return 0; } for (i = 1; i < s.size(); i++) cout << s[i]; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7; const int maxn = 510000; int n, K; bool flag; char A[maxn], B[maxn]; namespace Ninit { void init() { int i; scanf("%d%s", &K, A + 1); n = strlen(A + 1); for (i = n; i >= 1; --i) if (A[i] == 'z') A[i] = 'a'; else { ++A[i]; ...
4
#include <bits/stdc++.h> using namespace std; template <class T> inline void smax(T &x, T y) { x = max((x), (y)); } template <class T> inline void smin(T &x, T y) { x = min((x), (y)); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; long long a, b, c, d, t...
1
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; vector<vector<int> > v(100001); vector<int> vis(100001); long long binpow(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res % mod; } int d...
3
#include <bits/stdc++.h> using namespace std; const int maxn = 505; const int inf = 0x3f3f3f3f; int n, m; int g[maxn][maxn]; int d[maxn][maxn]; int cnt[maxn][maxn]; int ans[maxn][maxn]; int main() { int a, b, c; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (i != j)...
5
#include <bits/stdc++.h> using namespace std; const string NAME = "645D"; const int INF = 1e9 + 7; const int N = 1e5 + 7; int n, m, f[N], L[N], Time; bool vis[N]; vector<pair<int, int> > a[N]; void Enter() { cin >> n >> m; int u, v; for (int i = (1), _b = (m); i <= _b; ++i) { cin >> u >> v; a[v].push_back...
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n], i; for (i = 0; i < n; i++) { cin >> a[i]; } int x = 0; int m = a[n - 1]; for (i = n - 2; i >= 0; i--) { if (a[i] > m) x++; m = min(m, a[i]); } ...
2
#include <bits/stdc++.h> using namespace std; int main() { int n, mini, res, R; cin >> n >> mini >> R; res = R - mini; mini = min(mini, R); for (int i = 2; i < n; i++) { cin >> R; res = max(res, R - mini); mini = min(mini, R); } cout << res << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } 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 power(long long int x, long long int y, long long int p) { long long int res = ...
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int hash[26] = {0}; string str; cin >> str; int flag = 0; if (n == 1) { cout << "Yes" << endl; return 0; } for (int i = 0; i < n; i++) { hash[str[i] - 'a']++; if (hash[str[i] - 'a'] == 2) { flag = 1; ...
1
#include <bits/stdc++.h> using namespace std; struct edge { int to, next; } e1[300005], e2[300005]; int idx1[150005], idx2[150005]; int top1 = 0; int top2 = 0; int color[150005]; int cnt[150005]; int n, m; int dis[150005]; void add1(int x, int y) { e1[++top1].to = y; e1[top1].next = idx1[x]; idx1[x] = top1; } v...
4
#include <bits/stdc++.h> using namespace std; int n, inp[1001000][2], firstNotChange[1001000], f[1001000]; string st[1001000]; void dfs(int u) { if (st[u] == "IN") f[u] = inp[u][0]; else { dfs(inp[u][0]); if (st[u] != "NOT") dfs(inp[u][1]); if (st[u] == "AND") f[u] = f[inp[u][0]] & f[inp[u][1]]; ...
4
#include <bits/stdc++.h> using namespace std; template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &a) { for (const A &it : a) out << it << " "; return out...
4
#include <bits/stdc++.h> using namespace std; vector<bitset<100005> > g; vector<int> f; int import[65], n, ans[100005], cnt[100005]; long long d[100005]; void gauss(bitset<100005> A, int B) { int m = f.size(); for (int i = 0; i < m; i++) if (A[import[i]]) { A ^= g[i]; B ^= f[i]; } int index = ...
4
#include <bits/stdc++.h> int diru[] = {-1, -1, -1, 0, 0, 1, 1, 1}; int dirv[] = {-1, 0, 1, -1, 1, -1, 0, 1}; using namespace std; template <class T> T sq(T n) { return n * n; } template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); } template <class T> T lcm(T a, T b) { return (a / gcd<T>(a,...
3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<int> order, order1; int e = 0, e1 = 0; for (int i = 0; i < n; i++) { int tem; scanf("%d", &tem); if (tem == 0) e++; else order.push_back(tem); } for (int i = 0; i < n; i++) { int tem; ...
1
#include <bits/stdc++.h> const int MAXN = 5e3 + 19; const long long INF = 1e18; int n, k, x, a[MAXN]; long long dp[2][MAXN]; int q[MAXN], h, t; void solve(long long *f, long long *g, int m) { for (int i = 0; i <= n; ++i) f[i] = -INF; q[h = t = 0] = m - 1; for (int i = m; i <= n; ++i) { while (h <= t && i - q[...
6
#include <cstdio> #include <stack> using namespace std; typedef struct{ int color; int num; } Stone; int opcolor(int color){ return 1 - color; } stack<Stone> st; int n; int main(){ while(scanf("%d",&n), n != 0){ //init st = stack<Stone>(); Stone init_stone = {0,0}; st.push(init_stone); // put for(...
0
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 5; const int MAX_N = 105; struct State { int i, j, k; State(int _i = -1, int _j = -1, int _k = -1) : i(_i), j(_j), k(_k) {} }; bool operator<(State s, State t) { return false; } bool operator==(State s, State t) { return s.i == t.i && s.j == t.j ...
2
#include <bits/stdc++.h> using namespace std; long long ans[100005]; int main() { long long n, k, i; scanf("%lld%lld", &n, &k); if (n == k) { printf("-1\n"); return 0; } for (i = 2; i <= (k + 2 - 1); i++) { ans[i] = i; } long long d = n - i + 1; long long lim; if (d % 2 == 0) lim = n, ...
2
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class t, class u> void chmax(t& first, u second) { if (first < second) first = second; } template <class t, class u> void chmin(t& first, u second) { if (second < first) first = second; } template <class t> using vc = vector<t>; template ...
2
#include <bits/stdc++.h> int count(const std::string& s) { int c = 0; std::string v = "aiueo"; int n = s.size(); for (int i = 0; i < n; ++i) { if (v.find(s[i]) != std::string::npos) { ++c; } } return c; } int main() { std::string line1; std::getline(std::cin, line1); std::string line2; ...
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const int INF = (int)1e9 + 7; const int MOD = (int)1e9 + 7; const double EPS = (double)1e-10; int main() { int n; long long p; cin >> n >> p; vector<pair<long long, long long> > a(n); for (int i = 0; i < (int)n; ++i) cin...
3
#include <bits/stdc++.h> using namespace std; int cnt[205]; vector<int> v[205]; vector<int>::iterator v1, v2; int main() { int q; cin >> q; while (q--) { int n, i, j, ans = 0, x, y, a, b, k1, k2, ind1, ind2; cin >> n; int ara[n + 1]; map<int, int> mp; for (i = 1; i <= n; i++) { scanf("%d...
2
#include <bits/stdc++.h> using namespace std; int n, m; int main() { int x, y; long long r = 0; scanf("%d%d%d%d", &n, &x, &m, &y); r += n + m + 1; if (x > y) { swap(x, y); swap(m, n); } for (int i = 1; i <= n; i++) { if (x + i <= y - m || x + i >= y + m) continue; if (x + i > y) { in...
3
#include<cmath> #include<math.h> #include<ctype.h> #include<algorithm> #include<bitset> #include<cassert> #include<cctype> #include<cerrno> #include<cfloat> #include<ciso646> #include<climits> #include<clocale> #include<complex> #include<csetjmp> #include<csignal> #include<cstdarg> #include<cstddef> #include<cstdio> #i...
0
#include <bits/stdc++.h> using namespace std; int asked = 0; int ask(int dis) { printf("%d\n", dis); fflush(stdout); int result; scanf("%d", &result); asked++; return result; } int main() { int n, m; scanf("%d%d", &n, &m); int round[30], r = 0; for (int i = 0; i < m; i++, r = (r + 1) % m) { int ...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 400 + 100; int a[maxn], b[maxn], sto[maxn]; struct node { int le; int ri; } q[maxn]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= m; i++) { scanf("%d %d", &q[i...
5
#include <stdio.h> #include <algorithm> using namespace std; int n; int m; bool check[7368792]; int go; int x; void reset(){ for(x=0;x<=7368791;x++) check[x] = 0; } int i; int main(){ while(1){ scanf("%d%d",&m,&n); if(n==0 && m==0) break; reset(); go = m; while(n--){ for(i=1;go*i<=7368791;i++){ ...
0
#include <bits/stdc++.h> using namespace std; long long sod(long long inp) { long long l = inp; long long s = 0; while (l != 0) { s += l % 10; l = l / 10; } return s; } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, s; cin >> n >> s; long lon...
3
#include <iostream> using namespace std; typedef long long ll; int main() { ll x,k,d; cin>>x>>k>>d; x=abs(x); ll ans; if(k<=(x/d)){ ans=x-d*k; } else{ if((k-(x/d))%2){ ans=d-x%d; } else{ ans=x%d; } } cout<<ans; return 0; }
0
#include <bits/stdc++.h> int main() { int n, count = 0; scanf("%d", &n); while (n--) { int t = 3, s = 0; while (t--) { int a; scanf("%d", &a); s = s + a; } if (s > 1) { count++; } } printf("%d", count); return 0; }
1
#include <bits/stdc++.h> #define INF 1000000000 #define LINF 1000000000000000000 #define MOD 1000000007 #define mod 998244353 #define INF63 1061109567 #define INF127 9187201950435737471 #define UINF 18446744073709551615 #define F first #define S second #define ll long long #define N 200010 using namespace std; ll n,num...
0
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; long long h[N], p[N]; long long n, k, m; long long phi(long long n) { long long x = n, y = n; for (long long i = 1; p[i] * p[i] <= n; i++) if (y % p[i] == 0) { x -= x / p[i]; while (y % p[i] == 0) y /= p[i]; } if (y > 1) x -=...
5
#include <bits/stdc++.h> using namespace std; const int OFFSET = 5; const int MAXN = 1000000; int N; int heights[MAXN], r[MAXN], l[MAXN], c[MAXN]; int main() { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &heights[i]); } rotate(heights, max_element(heights, heights + N), heights + N); height...
5
#include <bits/stdc++.h> int main() { int n, m, i, a[200], l = 0, j, temp, sum = 0, min; scanf("%d", &n); scanf("%d", &m); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i < n; i++) { min = i; for (j = i + 1; j < n; j++) { if (a[j] < a[min]) { min = j; } } ...
1
#include <bits/stdc++.h> using namespace std; int main(){ string S,T; cin>>S>>T; int X=-1; for(int i=0;i<S.size()-T.size()+1;i++){ bool ans=true; for(int j=0;j<T.size();j++){ if(S.at(i+j)!=T.at(j)&&S.at(i+j)!='?') ans=false; } if(ans) X=i; } if(X==-1...
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 50; const int MOD = 1e9 + 9; int n, k, a, b, c, d, m, L, R, tot = 1; pair<int, int> cities[MAXN]; struct TreeNode { int x, l, r, lson, rson; pair<int, int> v; } tree[MAXN]; int newNode(int &x) { return x = ++tot; } pair<int, int> calc(pair<int, in...
4
#include<iostream> #include<cstdio> #include<cstring> const int N=105; int s[N][N]; int n,m,c; void go(int &x,int &y) { if(x&1) { y++; if(y>m)y=m,x++; } else { y--; if(!y)y=1,x++; } } int main() { scanf("%d%d%d",&n,&m,&c); for(int i=1,cnt,x=1,y=1;i<=c;i++) { for(scanf("%d",&cnt);cnt--;go(x,y)) s[...
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100, mod = 1e9 + 7; int n, q, cnt[55], dp[N], f[N], fac[N], inv[N], res[55][55]; char s[N]; int id(char c) { return 'A' <= c && c <= 'Z' ? c - 'A' : c - 'a' + 26; } int ksm(int x, int y) { int s = 1; for (; y; y >>= 1, x = (long long)x * x % mod) ...
4
#include <bits/stdc++.h> using namespace std; typedef int64_t ll; ll max_pool[4 * 100000] = {0}; void update(int u, int l, int r, int cur, ll v) { if ((cur < l) || (cur > r)) { return; } if (l == r) { max_pool[u] = v; return; } int mid = (l + r) / 2; update(2 * u, l, mid, cur, v); update(2 * u...
4
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 30; const int MAXN = 100100; int x[MAXN], y[MAXN]; int N; int main() { scanf("%d", &N); for (int i = 0; i < N; ++i) { scanf("%d %d", x + i, y + i); } int m = inf; int M = 0; for (int i = 0; i < N; ++i) { m = min(m, x[i]); M = max...
2
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; if (n > m + 1 || 2 * (n + 1) < m) cout << "-1" << endl; else { int v = min(n, m); int mn = v; int n1 = n; int m1 = m; while (v && m) { if (mn == n1 && v < m) { cout << "110"; v--; ...
3
#include <bits/stdc++.h> using namespace std; vector<int> x(500000); long long s = 0; int mx = 0, mn = 1e9; long long ss, sss; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x[i]; s += x[i]; mx = max(x[i], mx); ...
4
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f, mod = 998244353; const double pi = 3.1415926535897932, eps = 1e-6; void chmax(int &x, int y) { if (x < y) x = y; } void chmin(int &x, int y) { if (x > y) x = y; } int n, m, dp[500005], t[500005]; char s[500005]; struct mat { int a[2][2]; ...
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << (n < 1000 ? "ABC" : "ABD") << endl; }
0
#include <bits/stdc++.h> using namespace std; const int IT = 200; const int N = 100100; int n; double k; double a[N]; bool solve(double x) { double bal = 0; for (int i = 0; i < n; i++) { if (a[i] >= x) bal += k * (a[i] - x); else bal += a[i] - x; } return bal >= 0; } int main() { scanf("%d...
2
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const long long INF = 1000000000000000007; void AC_aaega() { long long n, m; cin >> n >> m; vector<vector<long long> > vv(n); for (long long i = 0; i < m; i++) { long long aa, bb; cin >> aa >> bb; aa--; bb--; vv[...
1
#include <bits/stdc++.h> using namespace std; using ll=long long; #define rep(i,n) for(int i=0;i<n;i++) int main() { int n; string s; cin>>n>>s; int ans=0; rep(i,n-1){ vector<bool>a(26); rep(j,i+1){ a[s[j]-'a']=true; } int answer=0; for(int k=i+1;k<n;k++){ if(a[s[k]-'a']) { ...
0
#include <bits/stdc++.h> using namespace std; vector<long long> a; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); a = vector<long long>(6); int total = 0; for (int i = 0; i < 6; i++) { cin >> a[i]; total += a[i]; } for (int i = 0; i < 6; i++) { for (int j = i + 1; j < 6; j++) { ...
1
#include <bits/stdc++.h> const double E = exp(1.0); const double pi = 3.1415926; const int INF = 0x3f3f3f3f; const int N = 2e5 + 5; using namespace std; pair<int, int> pa[N]; priority_queue<pair<int, int>, vector<pair<int, int> >, less<pair<int, int> > > q; int ans1[N], ans2[N], idx; int main() { int t; scanf("...
4
#include <bits/stdc++.h> using namespace std; string s; int cnt[9]; int mins = 1e6; const string t = "Bulbasr"; int main() { cin >> s; for (int i = 0; i < s.size(); i++) for (int j = 0; j < t.size(); j++) { if (s[i] == t[j]) { cnt[j]++; break; } } cnt[1] /= 2; cnt[4] /= 2; ...
1
#include <bits/stdc++.h> using namespace std; static struct IO { char tmp[1 << 10]; char cur; inline char nextChar() { return cur = getc(stdin); } inline char peekChar() { return cur; } inline operator bool() { return peekChar(); } inline static bool isBlank(char c) { return (c < '-' && c); } inline bool ...
2
#include<iostream> #include<string> using namespace std; int main(){ int n; while(true){ cin>>n; if(n==0) break; int x,y,h,w; int ans=0; for(int u=0;u<n;u++){ cin>>x>>y>>h>>w; int count=0; if(x+y+h<=160&&w<=25) count=1600; if(x+y+h<=140&&w<=20) count=1400; if(x+y+h<=120&&w<=15) count=1200; ...
0
#include <bits/stdc++.h> using namespace std; int rd() { int x = 0; char c = getchar(); while (c > '9' || c < '0') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar(); return x; } void wt(long long x) { if (x >= 10) wt(x / 10); putchar(x % 10 + 48); } const int N = 3e6 + 10; str...
5
#include<iostream> #include<math.h> #include<vector> #include<algorithm> #include<cstdio> #include <string> #include <complex> #include <functional> using namespace std; typedef pair<int,int> P; int main(){ int a,b,i; int w[100]; while(cin>>a>>b&&a){ int max=1500000; int min=1,res=0,dan=1; ...
0
#include <bits/stdc++.h> using namespace std; int N, M, all, all2; int op[8]; int dp[44][1 << 8][1 << 8], sol; int gain[1 << 8]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> N >> M; if (N > M) swap(N, M); all = 1 << N; all2 = 1; for (int I = 0; I < N; I++) all2 *= 5; for (int I = 0; I <...
3
#include <bits/stdc++.h> using namespace std; const int maxn = 200; int arr[maxn]; bool cmp(int num1, int num2) { return num1 > num2; } int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n, cmp); for (int i = 0;...
2
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <stack> using namespace std; const int maxn = 100005; int R, C, N, n; pair<int,int> v[maxn << 1]; stack<int> S; int trans(int x, int y) { if(x == 0) return y; if(y == C) return C + x; if(x == R) return C + R + C - y; if(y == 0) ret...
0
#include <bits/stdc++.h> using namespace std; int n; int main() { scanf("%d", &n); printf("%d\n", 6 * n * (n - 1) + 1); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long x1, y1, x2, y2, men; cin >> x1 >> y1 >> x2 >> y2; if (x1 == x2 && y1 == y2) { cout << "0\n"; } else if (x1 == x2) { cout << abs(y2 - y1) << "\n"; } else if (y1 == y2) { cout << abs(x2 - x1) << "\n"; } else { if (x1 < x2 && ...
1
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; const int N = 1e3 + 7; const int inf = 0x3f3f3f3f; int res[3000]; int main() { int q; scanf("%d", &q); while (q--) { memset(res, 0, sizeof(res)); int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x; scanf(...
1
#include<bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; vector<int>E[100000]; int a[100000], b[100000]; int ans[100000]; void dfs(int v, int p, int d) { for (int u : E[v]) { if (u == p)continue; dfs(u, v, d ^ 1); a[v] = max(a[v], a[u] - 1); b[v] = min(b[v], b[u] + 1); } if (a[v]...
0
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> p1, pair<int, int> p2) { return p1.second != p2.second ? p1.second < p2.second : p1 < p2; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, l, r, cnt = 0; cin >> n; vector<int> ans, vis(n); vector<pair<int, int> ...
4
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; const long long mod = 998244353; const int inf = INT_MAX; struct item { long long n; item(int x = inf) : n(x) {} item operator+(const item &oth) { item r; r.n = min(n, oth.n); return r; } }...
5
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 1; const long long INF = 1e9 + 7; const long long inf = 1e18; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) { long long n; string s; cin >> n; cin >> s; ...
2
#include <bits/stdc++.h> using namespace std; int len[2200]; int main() { int t; for (int i = 0; i < 6; i++) { cin >> t; len[t]++; } int n = 0; for (int i = 1; i <= 9; i++) { if (len[i] >= 4) { n = i; len[i] -= 4; } } if (n == 0) { cout << "Alien"; return 0; } for (...
1
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; long long inv[5050 + 10], fac[5050 + 10], ifac[5050 + 10]; void setComb() { inv[0] = 1; inv[1] = 1; fac[1] = 1; ifac[1] = 1; fac[0] = 1; ifac[0] = 1; for (int i = 2; i < 5050; i++) { inv[i] = (-MOD / i) * inv[MOD % i] % MOD...
6
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chkmin(T& x, T y) { return x > y ? x = y, true : false; } template <class T> inline bool chkmax(T& x, T y) { return x < y ? x = y, true : false; } inline long long read(void) { long long x, f = 1; char ch = getchar(); for (; !isdigit...
4
#include <bits/stdc++.h> using namespace std; int n, num[120000], type[120000], i, fa[120000], f[120000], id, bo[120000], x; void dfs(int x) { if (bo[x]) return; bo[x] = 1; if (type[x] == 1) { f[x] = 0; return; } if (num[x] == 1) { dfs(fa[x]); f[x] = f[fa[x]] + 1; } } int main() { scanf("%...
2
#include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; typedef vector<...
0
#include <bits/stdc++.h> using namespace std; unsigned seed = chrono::system_clock::now().time_since_epoch().count(); mt19937 Rand(seed); uniform_int_distribution<long long> range(0, 1ll << 32); inline void ucin() { ios::sync_with_stdio(0); cin.tie(0); } template <class T> inline void chkmax(T &x, const T &y) { i...
4
#include <bits/stdc++.h> using namespace std; const int ux[] = {1, -1, 0, 0}; const int uy[] = {0, 0, 1, -1}; char c[1010][1010]; bool was[1010][1010]; int n, m, i, j, X, Y, x2, y2, d[1010][1010], qx[1010 * 1010], qy[1010 * 1010]; int bfs(int x, int y) { qx[0] = x, qy[0] = y, d[x][y] = 0, was[x][y] = true; i = 0, j...
2
#include <bits/stdc++.h> using namespace std; int N; vector<int> graph[500005]; int nxt; int lft[500005], rht[500005]; void dfs(int n, int p) { nxt += (int)(graph[n].size()) + !p; int ed = nxt; int lst = ed; rht[n] = ed; for (int e : graph[n]) { if (e != p) { lft[e] = --lst; dfs(e, n); } ...
5
#include <bits/stdc++.h> using namespace std; const int K = 1e5 + 7, M = 1e9 + 9; int fp(int x, int y) { int ret = 1; for (; y; y >>= 1, x = (long long)x * x % M) if (y & 1) ret = (long long)ret * x % M; return ret; } int n, a, b, k; char s[K]; int main() { scanf("%d%d%d%d", &n, &a, &b, &k); scanf("%s", s...
1
#include <cstdio> using namespace std; const int N = 31; int num[N]; int main() { int n; scanf("%d", &n); int xr = 0; for (int i = 0; i < n; i++) { int a; scanf("%d", &a); int pw = 0; xr ^= a; while (!(a & 1)) pw++, a >>= 1; num[pw]++; ...
0
#include <bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin >> a >> b >> c; cout << min({b/a,c}); }
0
#include <bits/stdc++.h> using namespace std; const long long MAX = 123456789; const long long inf = 123456789000000000; const double EPS = 1e-10; const double PI = 2 * asin(1.0); const long long mod = 1e9 + 7; inline int cmp(double x, double y = 0, double tol = EPS) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 :...
4
#include <bits/stdc++.h> using namespace std; const int maxN = 1005, MOD = 1e7 + 7, eps = 1e-7; double dp[maxN]; int ans[maxN]; int k, q, d; void prob() { for (int x = k; x > 0; --x) { dp[x] = (x * dp[x] + (k - x + 1) * dp[x - 1]) / k; } } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); d...
4
#include<bits/stdc++.h> using namespace std; int main(){ string s; cin >> s; if (s.back() == 's') s += "es"; else s += "s"; cout << s << endl; }
0
#include <bits/stdc++.h> using namespace std; const double eps = 1e-7; const int inf = 0x3f3f3f3f; const int hinf = 0x3f3f3f3f; const long long mod = 1000000007; int n, k; int len; string name; string limit; char ans[110]; int main() { cin >> n >> k >> name >> limit; len = name.length(); memset(ans, 0, sizeof(ans...
4
#include <bits/stdc++.h> using namespace std; #define all(c) (c).begin(),(c).end() #define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--) #define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++) #define rep(i,n) REP(i,0,n) #define iter(c) __typeof((c).begin()) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) #def...
0
#include <bits/stdc++.h> using namespace std; int main() { int x, y, z, a, i, j, c = 0, b, p, q, r; string s; cin >> x; int ar[x]; for (i = 1; i <= x; i++) { cin >> ar[i]; } if (ar[x] == 15) { cout << "DOWN" << endl; } else if (ar[x] == 0) { cout << "UP" << endl; } else if (x == 1) { c...
1
#include <bits/stdc++.h> using namespace std; void fun(long long int a, long long int b, long long int c, long long int d) { long long int x = b, y = c, z = c; cout << x << " " << y << " " << z << endl; } int main() { int t; cin >> t; while (t--) { long long int a, b, c, d; cin >> a >> b >> c >> d; ...
1
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 1; map<long long, long long> mp, pm; deque<long long> v, vc, ve, vv, vd; deque<char> p, pe; deque<string> ts, ss, st; deque<double> sd, ds; long long a, b, c, d, e, f, cnt, ans, r, n, l, x, y; long double q, t, cnt1, cnt2; string g, h, w, o, m, s; ...
1
#include <bits/stdc++.h> using namespace std; int n,m; vector<int>v[100009]; int dp[100009]; int bt(int node){ if(dp[node]!=-1) return dp[node]; int ans=0; for(int i=0;i<v[node].size();i++){ ans=max(ans,bt(v[node][i])+1); } dp[node]=ans; return ans; } int main(){ memset(dp,-1,sizeof(dp)); cin>>n>>m; for(int ...
0
#include <bits/stdc++.h> using namespace std; int main() { char ch; int n, m, x, y; list<pair<int, int> > l; vector<list<pair<int, int> >::iterator> v; scanf("%d%d", &n, &m); l.push_back(pair<int, int>(m, 0)); v.push_back(l.end()); for (int i = 0; i < n; ++i) { scanf(" %c%*s%d", &ch, &x); if (ch...
2
#include <iostream> #include <vector> using namespace std; typedef vector<int> VI; int main ( void ) { int n, q; while ( cin >> n >> q, n | q ) { VI date_cnts( 101, 0 ); for ( int i = 0; i < n; ++i ) { int m; cin >> m; for ( int j = 0; j < m; ++j ) { int date; cin >> date; ++date_cnts[date...
0
#include <bits/stdc++.h> using namespace std; int main() { long long n; scanf("%I64d", &n); vector<long long> a(n); vector<long long> b(n); for (long long i = 0; i < n; i++) { scanf("%I64d", &a[i]); } for (long long i = 0; i < n; i++) { scanf("%I64d", &b[i]); } sort(a.begin(), a.end()); sort...
3
#include <bits/stdc++.h> using namespace std; int n, a[100005], sum, ans, ans2; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; ans2 = max(ans2 + a[i], 0); ans = max(ans, ans2); } cout << ans - (sum - ans); return 0; }
3
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") long long int power(long long int x, long long int n) { long long int result = 1; while (n) { if (n % 2 == 1) result = result * x; n = n / 2; x = x * x; } return result;...
1
#include <bits/stdc++.h> using namespace std; bool ind = 0; const int N = 2e5 + 100, M = 1e9 + 7; int64_t n, m, ans = 1, vis[N], x, y, s[N], t, nex[N], ans1, val[N], sig; vector<int64_t> adjt[N], adj[N]; int64_t dfs(int64_t node) { int64_t cnt = 1; vis[node] = 2; for (int64_t i : adjt[node]) cnt += dfs(i); retu...
5