solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; int dep[maxn], fa[maxn], f[maxn]; int d[maxn]; int n, m; int q1, q2, p1, p2; int s, t; vector<int> G[maxn]; void dfs(int u, int pre) { dep[u] = dep[pre] + 1; fa[u] = pre; for (int i = 0; i < G[u].size(); ++i) if (!dep[G[u][i]]) dfs(G...
5
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using namespace std; using ll = int64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vect...
0
#include <bits/stdc++.h> using namespace std; const int maxN = 8002; int n, bc[maxN + 1]; int f[maxN + 1], w[maxN + 1][maxN + 1]; int nxt[maxN + 1]; char s[maxN + 1], t[maxN + 1]; inline void work(int x) { int len = n - x + 1; for (int i = 1; i <= len; i++) t[i] = s[i + x - 1]; t[len + 1] = '#'; nxt[1] = 0; f...
6
#include <bits/stdc++.h> int n = 0; int k = 0; int a = 0; int b = 0; int q = 0; const int N = 200000; int ta[N + 10]; int tb[N + 10]; void add(int* t, int p, int v) { for (; p <= N; p += (p & -p)) t[p] += v; } int ask(int* t, int p) { int res = 0; for (; p >= 1; p -= (p & -p)) res += t[p]; return res; } int ask...
2
#include <bits/stdc++.h> using namespace std; template <class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template <class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second ...
5
#include <bits/stdc++.h> using namespace std; struct PersistantTrieMap { struct TrieMap { long long val = -1; int inSubtree = 0; TrieMap *children[26]; TrieMap() { memset(children, 0, 26 * sizeof(TrieMap *)); } }; int sz = 1, autoTree = 0; vector<TrieMap> trees; PersistantTrieMap() { trees.pus...
4
#include <bits/stdc++.h> using namespace std; const double PI = 2.0 * acos(0.0); const double EPS = 1e-9; const int MOD = 1000000009; const int INF = 1000000000; const long long LINF = 1000000000000000000LL; int main() { int t, n, p, x; scanf("%d", &t); while (t--) { scanf("%d %d", &n, &p); x = 2 * n + p;...
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 + 10; int main() { char a[MAXN], b[MAXN]; ios::sync_with_stdio(false); cin >> a >> b; int cntA = 0, cntB = 0; for (int i = 0; a[i]; ++i) { cntA += a[i] == '1'; } if (cntA & 1) { ++cntA; } for (int i = 0; b[i]; ++i) { cntB ...
3
#include<bits/stdc++.h> using namespace std; #define pfin(a) printf("%d\n",a); #define pfln(a) printf("%lld\n",a); #define pfis(a) printf("%d ",a); #define pfls(a) printf("%lld ",a); #define sfi(a) scanf("%d",&a); #define sfl(a) scanf("%lld",&a); #define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL...
2
#include <bits/stdc++.h> using namespace std; const int MAX_N = 200005; int F[MAX_N][30]; bool mark[MAX_N]; int n, m; long long mini, maxi; class DSU { private: int ro[MAX_N], ra[MAX_N]; public: void init(int n) { for (int i = 0; i <= n; i++) { ro[i] = i; ra[i] = 1; } } int findRoot(int i...
6
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); int T; cin >> T; while (T--) { int n; cin >> n; bool flag = true; int lst; cin >> lst; for (int i = 2; i <= n; i++) { int x; cin >> x; if (x != lst) flag = false; } if (...
1
#include <bits/stdc++.h> using namespace std; int n, t, d[1000005][2][2]; string temp = "ab"; pair<int, int> dp[1000005]; char s[1000005]; bool check(int l, int r) { if (s[l] == 'b') return 0; int temp = l & 1; if (d[r][temp][1] - d[l - 1][temp][1] != 0) return 0; temp = !temp; if (d[r][temp][0] - d[l - 1][te...
5
#include <bits/stdc++.h> using namespace std; const int p = 1e9 + 7; const int inv2 = 500000004; template <typename T> inline T read() { register T sum = 0; register char cc = getchar(); int sym = 1; while (cc != '-' && (cc > '9' || cc < '0')) cc = getchar(); if (cc == '-') sym = -1, cc = getchar(); sum = s...
6
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10, inf = 1e9 + 7; inline long long rd() { long long x = 0; char c = getchar(); int neg = 1; while (c < '0' || c > '9') { if (c == '-') neg = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); ...
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int counter = 0; while (n > 0) { int r = 0; int a = n; while (a > 0) { r = max(r, a % 10); a /= 10; } n -= r; counter++; } cout << counter << endl; }
3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int a[N]; int pref[N], suf[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; pref[1] = 1; for (int i = 2; i <= n; i++) { if (a[i - 1] < a[i]) pref[i] = pref[i ...
4
#include <bits/stdc++.h> using namespace std; class Graph { public: Graph(int N, int M) { this->N = N; this->M = M; edges.resize(N + 1); } void AddEdge(int x, int y) { edges[x].push_back(y); } void GetMatching(vector<pair<int, int> > &ret) { vector<int> to(N + 1), from(M + 1); vector<bool> ...
5
#include <iostream> using namespace std; int main(){long long h;cin >> h;long long ans = 0;long long i = 1; while (h > 0) {h = h / 2; ans += i; i *= 2;}cout << ans << endl;}
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; using lint = long long; using pi = pair<int, int>; int n; struct rect { int sx, ex, sy, ey, idx; } a[MAXN]; auto cmp1 = [](const rect &a, const rect &b) { if (a.sx != b.sx) return a.sx < b.sx; if (a.ex != b.ex) return a.ex < b.ex; return a.i...
5
#include <bits/stdc++.h> using namespace std; const int N = 100010; int rk[N], len, n; char ch[N], v[N]; int a[N]; namespace lzr { int sa[N], lcp[N]; struct cmp { const int h, *g; cmp(int h, int *g) : h(h), g(g) {} bool operator()(int l, int r) { return g[l] != g[r] ? g[l] < g[r] : g[l + h] < g[r + h]; } };...
4
#include <bits/stdc++.h> using namespace std; const long long N = 2e4 + 10; const long long mod = 998244353; const long long G = 3; inline long long read() { long long p = 0; long long f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0...
5
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const int inf = 100000000; int main() { char str[100005]; scanf("%s", str); int sz = strlen(str), i; bool is[30]; int lastpos[30]; memset(is, 0, sizeof(is)); for (i = 0; i < sz; i++) { is[str[i] - 'a'] = 1; lastpos[str...
1
#include <bits/stdc++.h> using namespace std; double distance(double x1, double y1, double x2, double y2) { return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); } int main() { ios::sync_with_stdio(false); int n, k; cin >> n >> k; double total = 0, x1, y1, x2, y2; cin >> x1 >> y1; cout.precision(9); for (int ...
1
#include <bits/stdc++.h> using namespace std; typedef struct node { long long a[51][51]; long long scale; struct node operator *(const struct node & tmp) { struct node ret; long long i,j,k; ret.scale=scale; memset(ret.a,0,sizeof(ret.a)); for(k=1;k<=scale;k++) for(i=1;i<=scale;i++) for(j=1;j<=scale;...
0
#include<iostream> using namespace std; int main(){ int n,a=0,b=0,aa,bb; cin>>n; while(n!=0){ for(int i=0;i<n;i++){ cin>>aa>>bb; if(aa<bb){ b+=aa+bb; }else if(aa>bb){ a+=aa+bb; }else if(aa==bb){ a+=aa; b+=bb; } } cout<<a<<" "<<b<<endl; cin>>n; a=0; ...
0
#include "bits/stdc++.h" using namespace std; using ll = long long; #define rep(i,n) for (int i=0;i<(n);i++) #define rep2(i,a,b) for (int i=(a);i<(b);i++) template<class I, class BiOp = I (*) (I, I)> class SegTree { private: int n; std::vector<I> dat, lazy; BiOp op; I e; public: SegTree(int n_, BiOp op, I e...
0
#include <bits/stdc++.h> using namespace std; int main() { long long int x, y; cin >> x >> y; vector<int> v, v1; for (int i = 0; i < x; i++) { int z; cin >> z; v.push_back(z); } int flg = 1; for (int i = 0; i < v.size(); i++) { flg = 1; if (i == 0) { v1.push_back(v[i]); } els...
2
#include <bits/stdc++.h> using namespace std; struct gam { int id; int value; int sol; }; bool comp(gam i, gam j) { return i.value > j.value; } bool my_comp(gam i, gam j) { return i.id < j.id; } int main() { int n; cin >> n; vector<gam> nab(n); vector<int> sol(n); for (int i = 0; i < n; i++) { cin >...
1
#include <iostream> using namespace std; int f[101][10]; int g_arr[8]; #define mod 1000000007 int g(int x) { if (x < 0) return 1; else return g_arr[x]; } int main(int argc, char **argv) { int h, w, k; cin >> h >> w >> k; g_arr[0] = 1; g_arr[1] = 2; for (int i = 2; i < w - 1; i++) { g_arr[i] = g_arr[i -...
0
#include <bits/stdc++.h> using namespace std; uint64_t rnd_data = 0xDEADBEEFDULL; inline void my_srand(int seed) { rnd_data = ((uint64_t)seed << 16) | 0x330E; } inline int my_rand() { rnd_data = rnd_data * 0x5DEECE66DULL + 0xB; return (rnd_data >> 17) & 0x7FFFFFFF; } template <typename T> void my_random_shuffle(T b...
2
#include <bits/stdc++.h> using namespace std; const long long arr = 2e5 + 100; long long a[arr]; void solve() { long long n; cin >> n; long long k; cin >> k; for (long long i = 1; i <= k; i++) cin >> a[i]; long long ans = 0; for (long long j = 1; j <= n; j++) { long long now = 1e9; for (long long ...
1
#include <bits/stdc++.h> int s[333]; int main() { int n, t; scanf("%d%d", &n, &t); s[n] = t; for (int i = 1; i < n; i++) { int T = 18; bool ok = 0; while (T--) { printf("? %d %d\n", i + 1, n); fflush(stdout); int tt; scanf("%d", &tt); if (tt != n - t) { ok = 1; ...
6
#include <iostream> #include <cstdio> #include <cstring> #define MN 201000 #define mod 1000000007 int inv[MN], cnt[MN], a[MN], nxt[MN]; int f[MN], s[MN]; int c[MN]; int n; int qpow(int x, int p) { int ans = 1; for(; p; p >>= 1, x = 1ll * x * x % mod) if(p & 1) ans = 1ll * ans * x % mod; return ans; } void add(i...
0
#include <bits/stdc++.h> using namespace std; const int INF = 4e8; const int MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); set<int> sets; set<int>::iterator ite; string cmd; int q, x, head = 0, tail = INF; cin >> q; int res = 1, temp = 1; sets.insert(0); sets.insert(INF); while (q--) { ...
4
#include <cstdio> #include <iostream> using namespace std; typedef long long ll; const int MAXN = 16; int n, a[MAXN][MAXN], b[MAXN+1]; ll f[1<<MAXN], g[1<<MAXN]; inline int count(int x) { int res = 0; for(int i=0;i<n;++i) if(x&(1<<i)) ++res; return res; } int main() { scanf("%d",&n); for(int i=0;i<n;i++) ...
0
#include <bits/stdc++.h> int N, a[4001], dp[4001][4001], last[1000001], prev[4001], ans = 1; inline int max(int a, int b) { return a > b ? a : b; } int main() { int i, j, t; scanf("%d", &N); for (i = 1; i <= N; ++i) { scanf("%d", a + i); t = prev[i] = last[a[i]]; last[a[i]] = i; dp[i][0] = 1; ...
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000005; const int MAXINT = 1073741823; int parent_of[MAXN], dp[MAXN], maximum[MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, current, parent; cin >> n; parent_of[0] = -1; dp[0] = 1; maximum[0] = 0; for (int i = 1; i <= ...
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int N = 512; vector<int> dp(N); dp[0] = 0; for (int i = 1; i < N; ++i) { set<int> s = {0, 1, 2}; s.erase(dp[i - 1]); if (i % 2 == 0) s.erase(dp[i / 2]); dp[i] = *s.begin(); } vector<int> wt...
5
#include<bits/stdc++.h> #define inf 0x7fffffff #define RG register #define maxn 1000005 #define maxm 205 using namespace std; typedef long long ll; const ll mod = 1e9 + 7; inline ll read(){ ll x = 0, f = 1; char ch = getchar(); while(ch > '9' || ch < '0') {if(ch == '-') f = -1;ch = getchar();} while(ch ...
0
#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; int par[26]; int ran[26]; void init() { for(int i=0;i<26;i++) par[i]=i,ran[i]=0; } int find(int x) { if(par[x] == x) return x; else return par[x]=find(par[x]); } void unite(int x,int y) { x=find(x); y=find(y); if(x == y) return ; if...
0
#include <bits/stdc++.h> using namespace std; int main(void) { int n, k, x; cin >> n >> k >> x; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } int color = arr[0], cnt = 1; vector<pair<int, int> > v; for (int i = 1; i < n; i++) { if (color == arr[i]) { cnt++; } else { ...
2
# include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, a[N], par[N], ind; long long dp[N], mx = -1e18; vector <int> ans; int main(){ scanf("%d", &n); for(int i = 1; i <= n; i ++){ scanf("%d", &a[i]); dp[i] = a[i]; par[i] = i; } for(...
0
#include<stdio.h> #include<math.h> #include<string.h> int a,s,d[501],f[501],g[501],h,j,i,k,q=2100000000; char z[501][501]; int main(void) { scanf("%d %d",&a,&s); for(i=0;i<a;i++) scanf("%s",z[i]); for(i=0;i<a;i++){ d[i]=0; f[i]=0; g[i]=0; } for(i=0;i<a;i++){ for(j=0;j<s;j++){ if(z[i][j]!='W') d[i]++; ...
0
#include<bits//stdc++.h> #define rep(i,n) for(int i = 0;i < n;i++) #define MOD 1000000007 using namespace std; typedef long long int ll; int n,a,b,ans = 0; vector<ll> sz, pw;vector<vector<int>> v; void dfs(int node, int p) {// now , before sz[node] = 1; int pr = pw[1]; for (int u : v[node]) { if (u ...
0
#include <bits/stdc++.h> using namespace std; const double eps = 1e-7; const double PI = acos(-1); const int MOD = 998244353; const int INF = 0x3f3f3f3f; const int N = 1e6 + 7; int n; int sum[2][N], nex[N], res[N]; char s[N]; vector<int> vl[N]; void update(const int &p) { for (int i = p; nex[i] > p && i >= 0; --i) ne...
6
#include <bits/stdc++.h> using namespace std; int n, m, k, x, y, H[300005], d, ant[300005]; vector<int> v[300005], st, L; void dfs(int nod) { int ok = 0; st.push_back(nod); H[nod] = st.size(); if (H[nod] >= d) { cout << "PATH\n"; cout << st.size() << '\n'; for (auto it : st) cout << it << ' '; e...
5
#include <bits/stdc++.h> using namespace std; int main(){ int64_t N,ans; cin >>N; for (int i = 1; i <= (int)(sqrt(N+1)-1); i++)if((N-i) % i == 0)ans+=(N-i)/i; cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(int argc, char **argv) { int n, r = 0; cin >> n; int h[n], v[n]; for (int k = 0; k < n; k++) { cin >> h[k] >> v[k]; } for (int k = 0; k < n; k++) { for (int j = 0; j < n; j++) { if (h[k] == v[j] && k != j) r++; } } cout << r << end...
1
#include <bits/stdc++.h> using namespace std; void deb() { cerr << endl; } template <typename T, typename... t> void deb(T a, t... b) { cerr << a << " "; deb(b...); } int64_t dx[] = {-1, 1, 0, 0, -1, -1, 1, 1}; int64_t dy[] = {0, 0, -1, 1, -1, 1, -1, 1}; void max_self(int64_t& x, int64_t y) { x = max(x, y); } templ...
2
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k; cin >> n >> k; if (n == k || (n % 2 != 0 && k % 2 != 0 && k < n) || (n % 2 == 0 && k % 2 == 0 && k < n)) { cout << 0 << "\n"; return; } else if ((n % 2 != 0 && k < n) || (n % 2 == 0 && k < n)) { cout << 1 << "\n"; ...
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; char c[100005]; int n, m, q; int main() { int i, j; while (~scanf("%d%d%d", &n, &m, &q)) { m = m + q; scanf("%s", c); int op, le, ri, type; while (m--) { scanf("%d%d%d%d", &op, &le, &ri, &type...
5
#include <bits/stdc++.h> using namespace std; const int N = 100005; const long long int P = 1000000007; const double eps = 1e-6; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; bool rev = 0; if (n > m) { swap(n, m); rev = 1; } if (n == 1 && m == 1) ...
3
#include<bits/stdc++.h> using namespace std; int main(){ long long int N; cin >> N; long long int max=1; for (long long int i=1;i<=sqrt(N)+1;i++){ if(N%i==0) max = i; } cout << max+N/max-2; }
0
#include<bits/stdc++.h> using namespace std; const double pi=acos(-1); const int dx[]={0,1,0,-1}; const int dy[]={1,0,-1,0}; #define ll long long #define inf int(1e9+7) #define pb push_back #define mp make_pair string s,t; bool ch[30]; int main() { cin>>s; int n=s.size(); for(int i=0;i<n;i++) ch[s[i]-'a']=1; if(n...
0
#include <iostream> #include <algorithm> #include <iomanip> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> static const int MOD = 1000000007; using ll = long long; using u32 = uint32_t; using namespace std; template<class T> constexpr T INF = ::nu...
0
#include <bits/stdc++.h> using namespace std; int a, b, c; int g; int sum; int mx; int mn; int main() { cin >> a >> b >> c; sum = a + b + c; g = a * b * c; int mx = max(a, max(b, c)); int mn = min(a, min(b, c)); if (mx == 1) { cout << "3" << endl; return 0; } if (a == 1 && c == 1) { cout << ...
1
#include <bits/stdc++.h> using namespace std; int arr1[26]; int arr2[26]; bool fun(string str1, string str2, int m, int n) { int j = 0; for (int i = 0; i < n && j < m; i++) if (str1[j] == str2[i]) j++; return (j == m); } int main() { string s, t; cin >> s >> t; if (fun(t, s, t.size(), s.size())) { c...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 105; int n, m; int a[maxn]; int p[maxn]; int main() { cin.sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; p[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; p[i] = p[i - 1] + a[i]; } int l, r; long long sum = 0; for (in...
2
#include <bits/stdc++.h> using namespace std; int n; struct Heap { long long sum; int siz; priority_queue<long long> q, del; int size() { return siz; } void work() { while (del.size() && q.top() == del.top()) { q.pop(); del.pop(); } return; } void push(long long x) { q.push(x);...
6
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int size; set<int> a; cin >> size; for (int i = 0; i < size; i++) { int v; cin >> v; a.insert(v); } if (a.size() == 1) cout <...
1
#include <bits/stdc++.h> using namespace std; int x[5011], a, b, c, n; int main() { cin >> n; printf("? 1 2\n"); fflush(stdout); scanf("%d", &a); printf("? 1 3\n"); fflush(stdout); scanf("%d", &b); printf("? 2 3\n"); fflush(stdout); scanf("%d", &c); int q = a - c; x[1] = (q + b) / 2; x[2] = a ...
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; int even = 0, odd = 0; while (t > 0) { t--; int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] % 2 == 0) even = 1; if (a[i] % 2 != 0) odd = 1; } if (n % 2 == 0) {...
1
#include<queue> #include<cstdio> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; const int INF=1<<29; struct edge{ int v,cost; }; int main(){ for(int n,m,c;scanf("%d%d%d",&n,&m,&c),n;){ vector<edge> G[100]; rep(i,m){ int u,v,cost; scanf("%d%d%d",&u,&v,&cost); u--; v--; G[u].push_back((edge)...
0
#include <bits/stdc++.h> using namespace std; int main() { long N, P; string S; cin >> N >> P >> S; reverse(S.begin(), S.end()); if (10 % P == 0) { long ans = 0; for (long i = 0; i < N; i++) { if ((S.at(i) - '0') % P == 0) ans += N - i; } return cout << ans << "\n", 0; } vector<lo...
0
#include <bits/stdc++.h> using namespace std; int ar[6]; int ans; int f(int x) { return x * x; } int main() { for (int i = 0; i < 6; i++) cin >> ar[i]; ans = f(ar[0] + ar[1] + ar[2]) - f(ar[0]) - f(ar[2]) - f(ar[4]); cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; return gcd(b % a, a); } long long power(long long a, long long b) { long long res = 1; while (b > 0) { if (b % 2 != 0) res = res * 1ll * a; b /= 2; a = a * 1ll * a; ...
1
#include <bits/stdc++.h> using namespace std; const int N = 200100; const long long mod = 1000000007; long long segtree[N << 2], bl[N << 2], lazy[N << 2]; int fa[N], val[N], now = 1, id[N], to[N], tot = 2, q, sz[N]; struct ope { int xz, poi; }; vector<ope> Q; vector<int> son[N]; void dfs(int node) { id[node] = now+...
4
#include<bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { int n,m; cin>>n>>m; vector<vector<int>> mat(n,vector<int>(m,0)); vector<vector<int>> dup(n,vector<int>(m,0)); vector<int> a; for(int i=0; i<n; i++) { ...
2
#include <iostream> #include <iomanip> #include <vector> #include <set> #include <string> #include <queue> #include <algorithm> #include <map> #include <cmath> #include <numeric> #include <list> #include <stack> #include <cstdio> #include <cstdlib> #include <cstring> #include <tuple> #define rep(i, n) for (int i = 0; ...
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int m = 0, k = 0; sort(a, a + n); for (int i = 0; i < n; i++) { if (a[i] == m) m++; else if (a[i] == k) ...
1
#include <bits/stdc++.h> int inp() { char c = getchar(); int neg = 1; while (c < '0' || c > '9') { if (c == '-') neg = -1; c = getchar(); } int sum = 0; while (c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); } return sum * neg; } struct SEG { int l; int r; long lon...
3
#include <bits/stdc++.h> using namespace std; int main() { int goals; cin >> goals; string x; cin >> x; string y; string l; int ycount = 0; int xcount = 1; for (int i = 0; i < goals - 1; i++) { cin >> l; if (l == x) ++xcount; else { y = l; ++ycount; } } if (xcount...
1
#include <bits/stdc++.h> using namespace std; int main() { int x, d; cin >> x; d = x; vector<int> vec, vec2; while (x--) { int q, r; cin >> q >> r; vec.push_back(q); vec2.push_back(r); } int c = 0; for (int i = 0; i < vec.size(); i++) { for (int j = 0; j < vec2.size(); j++) if ...
1
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double inf = 1e8; int ans1, ans2; double best; double t1, t2, x1, x2, t0; void work() { best = inf; ans1 = ans2 = 0; for (double r1 = 0.0; r1 < x1 + eps; r1 += 1.0) { double rig = r1 * t1 - r1 * t0, lef = t0 - t2; double r2; ...
3
#include <bits/stdc++.h> using namespace std; mt19937 rnd(228); const int N = 5000 + 7; int dp[N][N]; int cnt[N][N]; bitset<N> grab[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k, v; cin >> n >> k >> v; vector<long long> a(n); long long s = 0; for (int i = 0; i < n; i++) { cin >> a[i];...
4
#include<bits/stdc++.h> using namespace std; int main(){ long long int n,m; cin >> n >> m; long long int res = 1e18; for(int turn = 0; turn <= 1; turn++){ swap(n,m); res = min(res, (n%3) == 0 ? 0 : m); for(int j=1;j<=n-1;j++){ long long int mx = j*m, mn = j*m; mx = max(mx, (n-j)*(m/2 + m%2)); ...
0
#include <bits/stdc++.h> using namespace std; long long int p = 1000000007; long long int modPow(long long int x, long long int n) { if (n == 0) { return 1; } long long int y = modPow(x, n / 2); if (n % 2 == 0) { return (y * y) % p; } else { return (((y * y) % p) * x) % p; } } long long int inve...
1
#include <iostream> using namespace std; int main () { int x, y; cin >> x >> y; cout << (0 == x % y ? -1 : x) << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 111; const int MAXT = 111; int N, T, L, B; int G[MAXN]; double dp[MAXT][MAXN]; int main() { while(cin >> N >> T >> L >> B && (N|T|L|B)) { memset(G, 0, sizeof(G)); for(int i = 0; i < L; ++i) { int Lose; cin >> Lose; G[Lose] = 1; }...
0
#include <bits/stdc++.h> using namespace std; #define lli long long int int main() { // your code goes here lli n,q,i,s=0,b,c; cin>>n; lli a[n]; map<lli,lli>mp; for(i=0;i<n;i++) { cin>>a[i]; s=s+a[i]; mp[a[i]]++; } cin>>q; while(q--) { cin>>b>>c; s=s-b*mp[b]; s=s+c*mp[b]; mp[c]=mp[c]+mp[b]; ...
0
#include <bits/stdc++.h> using namespace std; int dp[2][2]; int main() { int n; cin >> n; int cur = 0; dp[0][0] = 1; dp[0][1] = 0; for (int i = 0; i <= n; ++i) { dp[cur ^ 1][0] = (3ll * (dp[cur][1]) % 1000000007) % 1000000007; dp[cur ^ 1][1] = ((2ll * (dp[cur][1]) % 1000000007) % 1000000007 + ...
5
#include <bits/stdc++.h> using namespace std; int main() { int m, s; int i; int tmp; string largest, smallest; cin >> m >> s; if (s == 0) { cout << ((m == 1) ? "0 0" : "-1 -1"); return 0; } for (i = 0; i < m; ++i) { tmp = (s > 9) ? 9 : s; largest += tmp + '0'; s -= tmp; } if (s >...
3
#include <bits/stdc++.h> using namespace std; int n, a[100010]; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } sort(a, a + n); int wait = a[0]; int c = n; for (int i = 1; i < n; ++i) { if (wait > a[i]) { c--; } else { wait += a[i]; } } pri...
4
#include <bits/stdc++.h> using namespace std; void test() { string s; cin >> s; int n; n = s.size(); vector<pair<int, string>> ans; string cur = ""; vector<pair<int, pair<char, char>>> history; auto add = [&]() { if (cur.size() > 10) { string temp = ""; for (int i = 0; i < 2; ++i) { ...
5
#include<bits/stdc++.h> int i,a[26],x; char c; int main(){ while(std::cin>>c)++a[c-'a']; for(;i<26;++i)a[i]%2?x=1:0; std::cout<<(x?"No\n":"Yes\n"); }
0
#include <bits/stdc++.h> using namespace std; int main(){ int X, Y; cin >> X >> Y; cout << (max(0, 4-X) + max(0, 4-Y) + max(0, 2-X*Y)*4)*100000 << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; string s; cin >> n; cin >> s; int p = n - 1; for (int i = 0; i < n - 1; i++) { if (s[i] > s[i + 1]) { p = i; break; } } cout << s.substr(0, p) + s.substr(p + 1); }
1
#include <bits/stdc++.h> using namespace std; void solve() { long long int a, b; cin >> a >> b; long long int c[a], d[b], i; for (i = 0; i < a; i++) cin >> c[i]; for (i = 0; i < b; i++) { cin >> d[i]; } for (i = 0; i < a; i++) { for (auto j = 0; j < b; j++) if (d[j] == c[i]) cout << c[i] << ...
1
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); mt19937 rnf(2106); const int N = 202; int n; set<int> a[N]; int ans[N]; set<int> s[N]; void solv() { scanf("%d", &n); for (int i = 1; i < n; ++i) a[i].clear(); for (int i = 1; i < n; ++i) { int q...
6
#include<bits/stdc++.h> #define LL long long using namespace std; const int N=3005; LL n,mo,fac[N],inv[N],c[N][N],g[N][N]; LL qpow(LL x,LL k,LL p) { LL ret=1; while(k){ if(k&1) ret*=x,ret%=p; x*=x,x%=p;k>>=1; } return ret; } int main() { cin>>n>>mo; fac[0]=1; for(int i=1;i<=n+1;i++) fac[i]=(fac[i-1]*i)%mo; ...
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; const int oo = 0x3f3f3f3f; inline long long read() { char ch = getchar(); int x = 0, f = 1; while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch...
3
#include <bits/stdc++.h> using namespace std; char ch[200020]; int k, ans; int main() { scanf("%s%d", ch + 1, &k); int len = strlen(ch + 1); int now, cnt = 0, num = 0; for (int i = 1; i <= len; i++) { if (ch[i] == '0') num++; } if (num == 1 && len == 1) { printf("0\n"); return 0; } else if (nu...
2
#include <bits/stdc++.h> using namespace std; int ans, n, m, t1, d, i, x, j, mx, now, cnt[1200001], f[1200001], v[1200001], mp[1200001]; int main() { scanf("%d%d%d", &n, &m, &d); for (i = 0; i < m; i++) { scanf("%d", &t1); while (t1--) { scanf("%d", &x); v[x] = i; } } mx = (1 << m) -...
4
#include <bits/stdc++.h> using namespace std; int main() { int c, v0, v1, a, l; cin >> c >> v0 >> v1 >> a >> l; int cpag = v0; int add = v0; int ans = 1; while (cpag < c) { add = min(v1, add + a); cpag += add - l; ans++; } cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; char t[77]; int cnt[26]; int z[10010]; int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < n; ++i) { scanf("%s", t); ++cnt[t[0] - 'A']; } int l = 0, r = 10010; while (r - l > 1) { int s = (l + r) / 2; int pos = 0; memset(z, ...
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int sizeA, sizeB, aux, cont = 0; bool ok = true; cin >> sizeA; vector<long long> a, b; for (int i = 0; i < sizeA; i++) { cin >> aux; a.push_back(aux); } cin >> sizeB; for (int i = 0; i < sizeB; i+...
4
#include <bits/stdc++.h> using iii = int; using yeee = iii; const iii DEBUG = -1; using namespace std; using namespace chrono; template <typename T1> void DOUTf(ostringstream &os, int names_i, vector<string> &names, T1 &&val) { os << names[names_i] << " = " << val; } template <typename T1, typename... Ts> void DOUTf(...
4
#include <bits/stdc++.h> using namespace std; int dp[1LL << 19][2]; vector<string> s(5); int a11[] = {3, 4, 5, 4, 3}; int a[5][5]; bool check(int mask, int x) { x = mask | x; if (mask == x) { return true; } return false; } int f(int mask, int flag) { int &result = dp[mask][flag]; ; if (result != -1) {...
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m, ma, mi; int valMax = 0; int valMin = 0; int aux1, aux2; int swap; while (cin >> n >> m) { if (m == 0) { cout << n - 1; } else { valMax = n; valMin = 0; for (int i = 0; i < m; i++) { cin >> aux1; ...
2
#include<bits/stdc++.h> using namespace std; int N; int fid[3][100010]; int rev[100010]; int id[100010]; int bit[300010][2]; int mem[100010]; int res[100010]; void init() { for(int i = 0; i <= N; i++)bit[i][0] = bit[i][1] = 0; return; } void add(int i,int k)//x = 1 { while(i <= N) { bit[i][k]...
0