solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; const int lg = 31; struct node { long long inv, vni, mx; node *left, *right; node() : mx(0), inv(0), vni(0), left(0), right(0) {} ~node() { delete left; delete right; } }; int n; int A[300000]; void ins(node*& nd, int k, int i) { if (nd == 0) nd = new node; nd->mx += 1; if (k < 0) return; if (A[i] & (1 << k)) { ins(nd->left, k - 1, i); if (nd->right) nd->vni += nd->right->mx; } else { ins(nd->right, k - 1, i); if (nd->left) nd->inv += nd->left->mx; } return; } long long dp[lg + 1][2]; int mx; void count(node* nd, int i) { if (!nd) return; count(nd->left, i - 1); count(nd->right, i - 1); dp[i][0] += nd->inv; dp[i][1] += nd->vni; return; } int main() { cin.tie(NULL)->sync_with_stdio(false); cin >> n; node* root = 0; for (int i = 0; i < n; ++i) { cin >> A[i]; ins(root, lg, i); } count(root, lg); long long nx = 0; for (int i = lg; i > -1; --i) { if (dp[i][0] > dp[i][1]) { mx |= (1 << i); nx += dp[i][1]; } else nx += dp[i][0]; } cout << nx << ' ' << mx; delete root; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; long long a[N]; signed main() { int T; cin >> T; while (T--) { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]); long long ans = -4e18; for (int i = 1; i <= n; ++i) { for (int j = i - 1; j >= max(i - 200, 1); --j) { ans = max(ans, (long long)i * j - (long long)k * (a[i] | a[j])); } } printf("%lld\n", ans); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int a[105] = {0}, b[105] = {0}, n, x, j = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); x = n / 2; if (n % 2 != 0) { for (int i = n - 1; i >= 0; i--) { if (i % 2 == 0) b[x - j] = a[i]; else b[x + j] = a[i]; if (i % 2 == 0) j++; } } else { for (int i = n - 1; i >= 0; i = i - 2) { b[x + j] = a[i]; b[x - 1 - j] = a[i - 1]; j++; } } if (n > 3) { for (int i = 0; i < n; i++) { if (i < n - 1) printf("%d ", b[i]); else printf("%d\n", b[i]); } } else if (n == 3) printf("%d %d %d\n", a[0], a[1], a[2]); else printf("%d %d\n", a[0], a[1]); return 0; }
2
#include <bits/stdc++.h> using namespace std; int Kol_vo(int a, int b) { int kol = 0, kol1 = 0, value = a; while (value != b) { kol++; value--; if (value == -1) value = 9; } while (a != b) { kol1++; a++; if (a == 10) a = 0; } return (kol > kol1) ? kol1 : kol; } int main() { string input; string kod; int output, kol = 0, n; cin >> n; cin >> input; cin >> kod; for (int i = 0; i < n; i++) { kol += Kol_vo(input[i] - 48, kod[i] - 48); } cout << kol; }
0
#include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n, k, a, b, c, d; std::cin >> n >> k >> a >> b >> c >> d; --a; --b; --c; --d; if (n == 4) { std::cout << -1 << std::endl; return 0; } if (k <= n) { std::cout << -1 << std::endl; return 0; } std::vector<int> free; for (int i = 0; i < n; ++i) { if (i == a || i == b | i == c || i == d) { continue; } free.push_back(i); } std::cout << a + 1 << " " << c + 1 << " "; for (int i = 0; i < free.size(); ++i) { std::cout << free[i] + 1 << " "; } std::cout << d + 1 << " " << b + 1 << std::endl; std::cout << c + 1 << " " << a + 1 << " "; for (int i = 0; i < free.size(); ++i) { std::cout << free[i] + 1 << " "; } std::cout << b + 1 << " " << d + 1 << std::endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int INF = 1e9; int n; vector<pair<int, int> > adj[N]; int target, col[N]; vector<int> nodes; bool dfs(int u, int c, int &sz, int &p) { if (col[u] != -1) { if (col[u] != c) return false; return true; } col[u] = c; sz++; p += c; nodes.push_back(u); bool res = true; for (auto e : adj[u]) { int v, x; tie(v, x) = e; res &= dfs(v, c ^ (x != target), sz, p); } return res; } int best(int u) { int sz = 0, p = 0; nodes.clear(); if (!dfs(u, 0, sz, p)) return INF; int a = p, b = sz - p; if (a > b) { swap(a, b); for (int u : nodes) col[u] ^= 1; } return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int m; cin >> n >> m; while (m--) { int u, v; char c; cin >> u >> v >> c; u--; v--; adj[u].emplace_back(v, c == 'B'); adj[v].emplace_back(u, c == 'B'); } int ans = INF, t = -1; for (target = 0; target < 2; target++) { fill_n(col, n, -1); long long int cand = 0; for (int u = 0; u < (int)(n); u++) if (col[u] == -1) cand += best(u); if (cand < ans) ans = cand, t = target; } if (ans == INF) cout << -1 << '\n'; else { cout << ans << endl; target = t; fill_n(col, n, -1); for (int u = 0; u < (int)(n); u++) if (col[u] == -1) best(u); for (int u = 0; u < (int)(n); u++) if (col[u]) cout << u + 1 << ' '; } return 0; }
7
#include <bits/stdc++.h> using namespace std; double start[100005]; double goal[100005]; int n, sum[100005]; vector<int> E[100005]; double ans = 0.0; double start_sum, goal_sum; void cal(int num, int parent = -1) { for (long long int i = 0; i < ((long long int)(((long long int)(E[num]).size()))); ++i) if (E[num][i] != parent) { cal(E[num][i], num); sum[num] += sum[E[num][i]]; start[num] += start[E[num][i]]; ans += 2.0 * (start[E[num][i]] / start_sum) * (goal[num] / goal_sum) * sum[E[num][i]]; } ans += 2.0 * (start_sum - start[num]) / start_sum * goal[num] / goal_sum * (n - ++sum[num]); } int main() { int a, b; cin >> n; for (long long int i = 0; i < ((long long int)(n - 1)); ++i) { scanf("%d%d", &a, &b); a--; b--; E[a].push_back(b); E[b].push_back(a); } for (long long int i = 0; i < ((long long int)(n)); ++i) scanf("%lf%lf", &start[i], &goal[i]); for (long long int i = 0; i < ((long long int)(n)); ++i) start_sum += start[i]; for (long long int i = 0; i < ((long long int)(n)); ++i) goal_sum += goal[i]; cal(0); printf("%0.20lf\n", ans / 2.0); }
8
#include<iostream> #include<stdio.h> #include<cstdio> #include<string.h> #include<algorithm> #include<cmath> #include<vector> #include <unordered_map> #include<climits> #include<set> #include <iomanip> #include<numeric> #include<queue> #include<stack> using namespace std; #define ff first #define ss second #define ll long long #define pb push_back #define mp make_pair #define mt make_tuple #define pii pair<ll,ll> #define vi vector<ll> #define mii map<ll,ll> #define pqb priority_queue<ll> #define pqs priority_queue<ll,vi,greater<ll> > #define mod 1000000007 #define inf 1e18 #define all(v) (v).begin(),(v).end() #define ps(x,y) fixed<<setprecision(y)<<x #define mk(arr,n,type) type *arr=new type[n]; #define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define endl "\n" unordered_map<ll,ll> m ; ll arr[100005] ; void sum1(int s, int e){ if(s == e){ m[arr[s]] = 1 ; return ; } ll mid = (arr[s] + arr[e])/2 ; ll s1 = 0 ; ll s2 = 0 ; int mi = -1 ; for(int i = s ; i <= e ; i++){ if(mid >= arr[i]){ s1+=arr[i] ; } else{ if(mi == -1){ mi = i ; } s2+=arr[i] ; } } m[s1] = 1; m[s2] = 1 ; if(mi != -1){ sum1(s,mi-1) ; sum1(mi,e) ; } } int main(){ #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin) ; freopen("output.txt","w",stdout) ; #endif FIO ; int t ; t =1 ; cin >> t ; while(t--){ m.clear() ; ll n,q ; cin >> n >> q ; for(int i =0 ; i < n ; i++){ cin >> arr[i] ; } sort(arr,arr+n) ; ll sum = 0 ; for(int i =0 ; i < n ; i++){ sum += arr[i] ; } m[sum] = 1 ; sum1(0,n-1) ; while(q--){ ll s ; cin >> s; if(m.count(s)){ cout << "Yes" <<endl ; } else{ cout << "No" << endl ; } } } return 0 ; }
4
#include <bits/stdc++.h> int main() { int a[2], m, n; scanf("%d:%d", &a[0], &a[1]); if (((a[0] < 6) || (a[0] > 9)) && ((a[0] < 15) || (a[0] > 19))) { m = a[0]; n = (10 * (m % 10)) + (m / 10); if (a[1] < n) { if (m < 10 && n >= 10) printf("0%d:%d", m, n); else if (n < 10 && m >= 10) printf("%d:0%d", m, n); else if (m < 10 && n <= 10) printf("0%d:0%d", m, n); else printf("%d:%d", m, n); } else { if (a[0] == 23) printf("00:00"); else { m = a[0] + 1; n = (10 * (m % 10)) + (m / 10); if (m < 10 && n >= 10) printf("0%d:%d", m, n); else if (n < 10 && m >= 10) printf("%d:0%d", m, n); else if (m < 10 && n <= 10) printf("0%d:0%d", m, n); else printf("%d:%d", m, n); } } } else if (a[0] == 15) { if (a[1] < 51) printf("15:51"); else printf("20:02"); } else if (a[0] < 10) printf("10:01"); else printf("20:02"); return 0; }
1
#include <bits/stdc++.h> using namespace std; set<long long> numbers; int main() { long long n, l, x, y; cin >> n >> l >> x >> y; for (int i = 0; i < n; ++i) { long long x; cin >> x; numbers.insert(x); } long long sub = y - x; bool find_x = false, find_y = false; int v = -1; for (auto d : numbers) { if (numbers.find(d + x) != numbers.end()) { find_x = true; } if (numbers.find(d + y) != numbers.end()) { find_y = true; } if (numbers.find(d + sub) != numbers.end()) { if (d - x >= 0) v = d - x; if (d + y <= l) v = d + y; } if (numbers.find(d + x + y) != numbers.end() && d + x <= l) { v = d + x; } } if (!find_x && !find_y) { if (v != -1) { cout << "1\n" << v; } else { cout << "2\n" << x << ' ' << y; } } else if (!find_x && find_y) { cout << "1\n" << x; } else if (find_x && !find_y) { cout << "1\n" << y; } else { cout << 0; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int cnt[1 << 20], mi[1 << 20], n, c, a, ans; int main() { cin >> n >> c; while (n--) { cin >> a; mi[a] = min(mi[a], cnt[a] - cnt[c]); ++cnt[a]; ans = max(cnt[a] - cnt[c] - mi[a], ans); } cout << ans + cnt[c] << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int dist[1010][1010] = {{0}}; int vis[1010][1010] = {{0}}; vector<string> arr; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; arr.resize(n); for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = (int)(0); i < (int)(1010); i++) for (int j = (int)(0); j < (int)(1010); j++) vis[i][j] = dist[i][j] = 0; queue<pair<int, int> > bfs; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (arr[i][j] == 'E') { bfs.push(make_pair(i, j)); vis[i][j] = 1; dist[i][j] = 1; } } } int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; while (!bfs.empty()) { pair<int, int> ele = bfs.front(); bfs.pop(); for (int i = 0; i < 4; i++) { int nx = ele.first + dx[i]; int ny = ele.second + dy[i]; if (nx < 0 or nx >= n or ny < 0 or ny >= m) continue; if (vis[nx][ny]) continue; if (arr[nx][ny] == 'T') continue; dist[nx][ny] = dist[ele.first][ele.second] + 1; vis[nx][ny] = 1; bfs.push(make_pair(nx, ny)); } } int maxDist = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (arr[i][j] == 'S') maxDist = dist[i][j]; int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (dist[i][j] <= maxDist && vis[i][j]) if (arr[i][j] >= '0' && arr[i][j] <= '9') ans += arr[i][j] - '0'; } } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long maxn = 110, mod = 1e9 + 7; long long n, k; long long a[maxn], cnt[2]; long long c[maxn][maxn]; long long dp[maxn][maxn][2], num[maxn][maxn][2]; queue<long long> q; bool check(long long x, long long y) { return (x * 50) + (y * 100) <= k; } void solve(long long x, long long y, long long term) { if (term == 0) { for (long long i = 0; i < maxn; i++) { for (long long j = 0; j < maxn; j++) { if (i == 0 && j == 0) continue; if (!check(i, j)) continue; if (x - i < 0 || y - j < 0) continue; if (!num[x - i][y - j][1]) { num[x - i][y - j][1] = num[x][y][0] + 1; dp[x - i][y - j][1] = dp[x][y][0] * c[i][x] * c[j][y]; dp[x - i][y - j][1] %= mod; q.push(x - i); q.push(y - j); q.push(1); } else { if (num[x - i][y - j][1] == num[x][y][0] + 1) { dp[x - i][y - j][1] += dp[x][y][0] * c[i][x] * c[j][y]; dp[x - i][y - j][1] %= mod; } } } } } else { for (long long i = 0; i < maxn; i++) { for (long long j = 0; j < maxn; j++) { if (i == 0 && j == 0) continue; if (!check(i, j)) continue; if (x + i > cnt[0] || y + j > cnt[1]) continue; if (!num[x + i][y + j][0]) { num[x + i][y + j][0] = num[x][y][1] + 1; dp[x + i][y + j][0] = ((((dp[x][y][1] * c[i][cnt[0] - x]) % mod) * c[j][cnt[1] - y]) % mod); q.push(x + i); q.push(y + j); q.push(0); } else { if (num[x + i][y + j][0] == num[x][y][1] + 1) { dp[x + i][y + j][0] += dp[x][y][1] * c[i][cnt[0] - x] * c[j][cnt[1] - y]; dp[x + i][y + j][0] %= mod; } } } } } } void pre() { for (long long i = 0; i < maxn; i++) c[0][i] = 1, c[i][i] = 1, c[1][i] = i; for (long long i = 0; i < maxn; i++) for (long long j = i + 1; j < maxn; j++) c[i][j] = (c[i - 1][j - 1] + c[i][j - 1]) % mod; cerr << "c[2][5]" << ": " << c[2][5] << '\n'; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); pre(); cin >> n >> k; for (long long i = 0; i < n; i++) cin >> a[i], (a[i] == 50 ? cnt[0]++ : cnt[1]++); num[cnt[0]][cnt[1]][0] = 0, dp[cnt[0]][cnt[1]][0] = 1; q.push(cnt[0]); q.push(cnt[1]); q.push(0); while (!q.empty()) { long long x = q.front(); q.pop(); long long y = q.front(); q.pop(); long long term = q.front(); q.pop(); solve(x, y, term); } cout << (num[0][0][1] == 0 ? -1 : num[0][0][1]) << '\n'; cout << dp[0][0][1] << '\n'; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<long long> s{0}; for (int i = 0; i < n; i++) { int a; scanf("%d", &a); s.push_back(s.back() + a); } vector<pair<long long, int>> mxl(s.size()), mxr(s.size()); mxl[0] = {s.front(), 0}; for (int i = 1; i < s.size(); i++) if (s[i] > mxl[i - 1].first) mxl[i] = {s[i], i}; else mxl[i] = mxl[i - 1]; mxr[s.size() - 1] = {s.back(), s.size() - 1}; for (int i = s.size() - 2; i >= 0; i--) if (s[i] > mxr[i + 1].first) mxr[i] = {s[i], i}; else mxr[i] = mxr[i + 1]; long long mx = LLONG_MIN; int d1, d2, d3; for (int i = 0; i <= n; i++) if (mxl[i].first + mxr[i].first - s[i] > mx) { mx = mxl[i].first + mxr[i].first - s[i]; d1 = mxl[i].second, d2 = i, d3 = mxr[i].second; } printf("%d %d %d\n", d1, d2, d3); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, k, v; string buff; int main(int argc, char** argv) { cin >> n >> k >> buff; for (int i = 0; i < n - 1; i++) if (buff.substr(0, i + 1) == buff.substr(n - i - 1)) v = i + 1; string res = buff; for (int i = 2; i <= k; i++) { res += buff.substr(v); } cout << res; return 0; }
2
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T& x) { x = 0; int fl = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') fl = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } x *= fl; } template <typename T, typename... Args> inline void read(T& t, Args&... args) { read(t); read(args...); } const int N = 100005; int n, mxdeg, deg[N]; vector<int> edge[N]; vector<pair<int, int> > ans; void dfs(int x, int f, int t) { ans.emplace_back(make_pair(x, t)); if (t == mxdeg) { t = mxdeg - deg[x]; ans.emplace_back(make_pair(x, t)); } for (auto to : edge[x]) { if (to == f) continue; dfs(to, x, t + 1); t++; if (ans.back().second != t - 1) ans.emplace_back(make_pair(to, t - 1)); ans.emplace_back(make_pair(x, t)); if (f && t == mxdeg) { t = mxdeg - deg[x]; ans.emplace_back(make_pair(x, t)); } } } int main() { read(n); if (n == 1) { puts("1"); puts("1 0"); return 0; } for (int i = 1; i < n; i++) { int u, v; read(u, v); edge[u].emplace_back(v), edge[v].emplace_back(u); deg[u]++, deg[v]++; mxdeg = max(mxdeg, deg[u]); mxdeg = max(mxdeg, deg[v]); } dfs(1, 0, 0); printf("%d\n", (int)ans.size()); for (auto k : ans) printf("%d %d\n", k.first, k.second); return 0; }
9
#include <bits/stdc++.h> const int maxn = 1 << 18, mod = 998244353, proot = 3, base = (mod - 1) / maxn, inv2 = mod + 1 >> 1; int w[maxn], rw[maxn]; inline int pow_mod(int x, int n) { int y = 1; while (n) { if (n & 1) { y = 1ll * y * x % mod; } x = 1ll * x * x % mod; n >>= 1; } return y; } void fft_init() { w[0] = rw[0] = 1; w[1] = pow_mod(proot, base); rw[1] = pow_mod(w[1], mod - 2); for (int i = 2; i < maxn; ++i) { w[i] = 1ll * w[i - 1] * w[1] % mod; rw[i] = 1ll * rw[i - 1] * rw[1] % mod; } return; } inline void swap(int &x, int &y) { x ^= y; y ^= x; x ^= y; return; } inline void dft(int n, int *a, int *W, bool rev = 0) { for (int i = 0, j = 0; i < n; ++i) { if (i > j) { swap(a[i], a[j]); } for (int k = n >> 1; (j ^= k) < k; k >>= 1) ; } for (int hl = 1, l = 2, stp = maxn / l; l <= n; hl = l, l <<= 1, stp >>= 1) { for (int i = 0; i < n; i += l) { for (int j = i, k = i | hl, p = 0; j < (i | hl); ++j, ++k, p += stp) { int t = 1ll * a[k] * W[p] % mod; a[k] = (a[j] - t + mod) % mod; (a[j] += t) %= mod; } } } if (rev) { int inv = pow_mod(n, mod - 2); for (int i = 0; i < n; ++i) { a[i] = 1ll * a[i] * inv % mod; } } return; } int a[maxn], b[maxn], c[maxn], tmp[maxn]; void inv(int N, int *a, int *b) { memset(b, 0, sizeof(int) * N << 1); b[0] = pow_mod(a[0], mod - 2); for (int m = 1, n = 2, nn = 4; n <= N; m = n, n = nn, nn <<= 1) { memcpy(tmp, a, sizeof(int) * n); memset(tmp + n, 0, sizeof(int) * n); dft(nn, b, w); dft(nn, tmp, w); for (int i = 0; i < nn; ++i) { b[i] = 1ll * b[i] * ((2ll - 1ll * b[i] * tmp[i]) % mod + mod) % mod; } dft(nn, b, rw, 1); memset(b + n, 0, sizeof(int) * n); } return; } void sqrrt(int N, int *a, int *b) { b[0] = 1; for (int m = 1, n = 2, nn = 4; n <= N; m = n, n = nn, nn <<= 1) { inv(n, b, c); memcpy(tmp, a, sizeof(int) * n); memset(tmp + n, 0, sizeof(int) * n); dft(nn, tmp, w); memset(c + n, 0, sizeof(int) * n); dft(nn, c, w); for (int i = 0; i < nn; ++i) { tmp[i] = 1ll * tmp[i] * c[i] % mod; } dft(nn, tmp, rw, 1); for (int i = 0; i < n; ++i) { b[i] = (b[i] + tmp[i] & 1 ? (b[i] + tmp[i] >> 1) + 499122177 : b[i] + tmp[i] >> 1); } } return; } char s[10000000], *S; inline void R(int &x) { x = 0; while (*S < '0' || *S > '9') { ++S; } while (*S >= '0' && *S <= '9') { (x *= 10) += *S - '0'; ++S; } return; } inline void W(int x) { if (!x) { *S++ = '0'; } else { static char c[12]; char *t = c; while (x) { int y = x / 10; *t++ = x - y * 10 + '0'; x = y; } while (t != c) { *S++ = *--t; } } *S++ = '\n'; return; } int main() { fft_init(); int N, m, n; fread(s, 1, 10000000, stdin); S = s; R(N); R(m); while (N--) { int x; R(x); a[x] = 998244349; } a[0] = 1; n = 1; while (n <= m) { n <<= 1; } sqrrt(n, a, b); ++b[0]; inv(n, b, a); S = s; for (int i = 1; i <= m; ++i) { W((a[i] << 1) % mod); } fwrite(s, 1, S - s, stdout); return 0; }
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 201; int n, m, fr, len, c, fnow, pnow; vector<int> f[MAXN]; int l[MAXN]; pair<int, int> p[MAXN]; vector<pair<int, int> > ans; int main() { scanf("%d %d\n", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d", &len); l[i] = len; for (int j = 0; j < len; j++) { scanf("%d", &c); f[i].push_back(c); p[c] = make_pair(i, j); } } for (int i = 1; i <= n; i++) if (p[i].first == 0) fr = i; fnow = 1; pnow = 0; for (int i = 1; i <= n; i++) { if (pnow == l[fnow]) { fnow++; pnow = 0; if (fnow > m) break; } int pos = f[fnow][pnow]; if (i == pos) { pnow++; continue; } if (p[i] == make_pair(0, 0)) { ans.push_back(make_pair(pos, i)); f[fnow][pnow] = i; p[i] = p[pos]; p[pos] = make_pair(0, 0); fr = pos; } else { ans.push_back(make_pair(i, fr)); f[p[i].first][p[i].second] = fr; p[fr] = p[i]; p[i] = make_pair(0, 0); fr = i; ans.push_back(make_pair(pos, fr)); f[fnow][pnow] = i; p[i] = p[pos]; p[pos] = make_pair(0, 0); fr = pos; } pnow++; } printf("%d\n", (int)ans.size()); for (int i = 0; i < (int)ans.size(); i++) printf("%d %d\n", ans[i].first, ans[i].second); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; vector<int> pos; pos.push_back(0); for (int i = 0; i < int(s.size()); ++i) { if (s[i] == 'R') pos.push_back(i + 1); } pos.push_back(s.size() + 1); int a = 0; for (int i = 0; i < int(pos.size()) - 1; ++i) { a = max(a, pos[i + 1] - pos[i]); } cout << a << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; struct v { int l; int r; int cost; }; const long long int INF = 2000000001; const int SIZE = 200001; vector<v> sortL; vector<v> sortR; long long int arr[SIZE]; int n, x; bool CompareL(const v& a, const v& b) { return a.l < b.l; } bool CompareR(const v& a, const v& b) { return a.r < b.r; } long long int min(long long int a, long long int b) { return a < b ? a : b; } int main() { v tmp; long long int ans = INF; scanf("%d %d", &n, &x); for (int i = 0; i < n; i++) { scanf("%d %d %d", &tmp.l, &tmp.r, &tmp.cost); sortL.push_back(tmp); sortR.push_back(tmp); } sort(sortL.begin(), sortL.end(), CompareL); sort(sortR.begin(), sortR.end(), CompareR); for (int i = 0; i < SIZE; i++) arr[i] = INF; int idx = 0; v first = sortR[0]; for (int i = 0; i < sortL.size(); i++) { v second = sortL[i]; while (first.r < second.l) { int length = first.r - first.l + 1; arr[length] = min(arr[length], (long long int)first.cost); idx++; if (idx == sortR.size()) break; first = sortR[idx]; } if (x < second.r - second.l + 1) continue; ans = min(ans, second.cost + arr[x - (second.r - second.l + 1)]); } if (ans >= INF) printf("-1\n"); else printf("%d\n", ans); }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<vector<int>> cycles; const function<void(int)> solve = [&](const int n) { if (n == 3) { for (int i = 0; i < 2; i++) cycles.push_back({1, 2, 3}); } else if (n == 4) { cycles.push_back({2, 3, 4}); cycles.push_back({1, 3, 4}); cycles.push_back({1, 2, 4}); cycles.push_back({1, 2, 3}); } else { solve(n - 2); cycles.push_back({1, n - 1, n}); for (int v = 1; v <= n - 3; v++) cycles.push_back({v, n - 1, v + 1, n}); cycles.push_back({n - 2, n - 1, n}); } }; solve(n); cout << cycles.size() << endl; for (const vector<int>& cycle : cycles) { cout << cycle.size() << " "; for (const int v : cycle) cout << v << " "; cout << endl; } }
10
#include <bits/stdc++.h> using namespace std; long long max(long long a, long long b) { return a > b ? a : b; } long long min(long long a, long long b) { return a < b ? a : b; } int dfs(long long ma, long long mi) { long long mod = ma % mi; if (mod == 0) { return 1; } if (dfs(mi, mod)) { return (ma / mi % (mi + 1) + 1) % 2; } else { return 1; } } int main() { int n; scanf("%d", &n); while (n--) { long long a, b; scanf("%I64d%I64d", &a, &b); long long ma = max(a, b), mi = min(a, b); if (mi == 0) printf("Second\n"); else { if (dfs(ma, mi)) printf("First\n"); else printf("Second\n"); } } return 0; }
7
#include <bits/stdc++.h> using namespace std; char s[100100], p[100100]; int main() { gets(s); gets(p); int n = strlen(s), q = 0, w = 0; for (int i = 0; i < (n); i++) if (s[i] != p[i]) { if (s[i] == '7') q++; else w++; } printf("%d", min(q, w) + max(q, w) - min(q, w)); }
2
#include <bits/stdc++.h> using namespace std; int n; int main() { ios_base::sync_with_stdio(0); cin >> n; if (n & 1) { cout << "black" << '\n'; } else { cout << "white" << '\n'; cout << "1 2" << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 100000 + 20; const double eps = 0.01; const double PI = acos(-1.0); double dgcd(double x, double y) { if (x < eps) return y; if (y < eps) return x; return dgcd(y, fmod(x, y)); } int main() { double x[3], y[3], l[3]; for (int i = 0; i < 3; ++i) { scanf("%lf%lf", x + i, y + i); } double a, b, c; c = sqrt((x[0] - x[1]) * (x[0] - x[1]) + (y[0] - y[1]) * (y[0] - y[1])); b = sqrt((x[0] - x[2]) * (x[0] - x[2]) + (y[0] - y[2]) * (y[0] - y[2])); a = sqrt((x[2] - x[1]) * (x[2] - x[1]) + (y[2] - y[1]) * (y[2] - y[1])); double s = (a + b + c) / 2.0; double S = sqrt(s * (s - a) * (s - b) * (s - c)); double r = a * b * c / S / 4.0; double A, B, C; A = acos(1 - (a * a / (2.0 * r * r))); B = acos(1 - (b * b / (2.0 * r * r))); C = 2.0 * PI - A - B; double g = dgcd(A, B); g = dgcd(g, C); double ans = r * r * sin(g) * PI / g; printf("%.6lf\n", ans); }
6
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define pb push_back #define mp make_pair #define fi first #define se second #define mod 1000000007 #define modx 998244353 #define inf 2000000000000000000 #define mem(name,val) memset(name,val,sizeof(name)) #define precise cout<<fixed<<setprecision(15) const long double pi = acos(-1); int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin>>t; while(t--) { int n; cin>>n; string a,b; cin>>a>>b; int f=1,fc=0,c1=0,c0=0; for(int i=0;i<n;i++) { if(a[i]=='0') c0++; else c1++; } for(int i=n-1;i>=0;i--) { if((a[i]!=b[i]&&fc%2==0)||(a[i]==b[i]&&fc%2==1)) { if(c0==c1) { fc++; swap(c0,c1); } else { f=0; break; } } if(fc%2==0) { if(a[i]=='0') c0--; else c1--; } else { if(a[i]=='0') c1--; else c0--; } } if(f==1) cout<<"YES\n"; else cout<<"NO\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int sum(int a, int b) { int s = a + b; if (s >= mod) s -= mod; return s; } int sub(int a, int b) { int s = a - b; if (s < 0) s += mod; return s; } int mult(int a, int b) { return (1LL * a * b) % mod; } const int maxN = (int)1e6 + 100; int a[maxN]; int k; int fact[maxN]; int inv[maxN]; int invfact[maxN]; int num[maxN]; int cnk(int a, int b) { if (a < b) return 0; return mult(fact[a], mult(invfact[b], invfact[a - b])); } void init() { fact[0] = invfact[0] = fact[1] = invfact[1] = inv[1] = 1; for (int i = 2; i < maxN; i++) { fact[i] = mult(fact[i - 1], i); inv[i] = mult(inv[mod % i], mod - mod / i); invfact[i] = mult(invfact[i - 1], inv[i]); } } int add_val[maxN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); init(); cin >> k; int S = 0; int S_res = 0; for (int i = 0; i < k; i++) { cin >> a[i]; S += a[i]; int res = a[i] % k; S_res += res; add_val[res + 1] += k; } for (int i = 0; i < k; i++) { num[a[i]]++; } for (int i = 1; i <= k; i++) num[i] += num[i - 1]; bool fnd = false; int ans = 0; for (int moves = 1; moves < k; moves++) { if (num[moves - 1] >= moves + 1) { fnd = true; break; } int vals = moves - num[moves - 1]; ans = sum(ans, cnk(vals + k - 1, vals)); } if (fnd) { cout << sum(ans, 1); return 0; } int total = 0; for (int res = 0; res < k; res++) { if (res > 0) add_val[res] += add_val[res - 1]; int have = (S - S_res - add_val[res] + res * k) / k; total = sum(total, cnk(have + k - 1, k - 1)); } cout << total; return 0; }
12
#include <bits/stdc++.h> using namespace std; int n, cnt[100009], occ[100009]; bool can[100009]; set<int> s; int main() { cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; if (cnt[occ[x]] == 1) s.erase(occ[x]); cnt[occ[x]]--; occ[x]++; s.insert(occ[x]); cnt[occ[x]]++; if (s.size() == 2) { int x = *s.begin(), y = *(++s.begin()); int mx = max(x, y), mn = min(x, y); if ((mx - mn == 1 && cnt[mx] == 1) || (cnt[mn] == 1 && mn == 1)) can[i] = 1; } if (s.size() == 1 && (cnt[*s.begin()] == 1 || *s.begin() == 1)) can[i + 1] = can[i] = 1; } for (int i = n - 1; i >= 0; i--) if (can[i] == 1) { if (i == 0 && n > 1) cout << 2; else cout << i + 1; return 0; } return 0; }
3
#include<bits/stdc++.h> using namespace std; typedef long long int ll; typedef long double ld; #define rep(i,a,b) for(ll i=a;i<b;i++) #define nl cout<<endl #define pii pair<ll,ll> #define vi vector<ll> #define vii vector<pii> #define mi map<ll,ll> #define all(a) (a).begin(),(a).end() #define pb push_back #define ff first #define ss second #define hell 1000000007 #define test4(x,y,z,a) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<" a is "<<a<<endl; #define test3(x,y,z) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<endl; #define test2(x,y) cout<<"x is "<<x<<" y is "<<y<<endl; #define test1(x) cout<<"x is "<<x<<endl; #define N 200009 ll power(ll a,ll b,ll m) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%m; b/=2; a=(a*a)%m; } return ans; } ll n; ll a[N],b[N]; ll pre[N] = {0}; ll dp[N]; ll go(ll ind) { if(ind == n) return 1; // if start from here and win if( dp[ind] != -1) return dp[ind]; bool ans = 0; ll have = pre[ind]; if( have >= b[ind+1]) { ans = go(ind+1); } return dp[ind] = ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t;cin>>t; while(t--) { cin>>n; rep(i,1,n+1) { pre[i] = 0; dp[i] = -1; } rep(i,1,n+1 ) { cin >> a[i]; b[i] = a[i]; } sort(b+1,b+n+1); rep(i,1,n+1) { if(i == 1) pre[i] = b[i]; else pre[i] = pre[i-1] + b[i]; } /*rep(i,1,n+1) { cout << b[i] << " "; }nl;*/ set<ll>s; rep(i,1,n+1) { //go(i); if(go(i)) s.insert(b[i]); } vi ans; rep(i,1,n+1) { if( s.find(a[i]) != s.end()) ans.pb(i); } cout << ans.size()<< endl; for(auto it: ans) cout << it <<" "; nl; } }
3
#include <bits/stdc++.h> using namespace std; const int N = 5e3 + 1, P = 1e9 + 7; int n, x[N], y[N], mark[N]; vector<int> adj[N]; bool dfs(int u, int d) { bool res = 1; for (int v = 0; v < n; v++) if (abs(x[u] - x[v]) + abs(y[u] - y[v]) > d) if (mark[v] == mark[u]) res = 0; else if (!mark[v]) { mark[v] = 3 - mark[u]; res &= dfs(v, d); } return res; } int calc(int mid) { int res = 1; memset(mark, 0, sizeof mark); for (int u = 0; u < n; u++) if (!mark[u]) { mark[u] = 1; res = res * 2 * dfs(u, mid) % P; } return res; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; int l = -1, r = 2 * N; while (r - l > 1) { int mid = l + r >> 1; (calc(mid) ? r : l) = mid; } cout << r << endl << calc(r) << endl; }
9
#include <bits/stdc++.h> using namespace std; int main() { int a, c; cin >> a >> c; string s1, s2; while (a) { s1 += (a % 3 + '0'); a /= 3; } while (c) { s2 += (c % 3 + '0'); c /= 3; } if (s1.size() > s2.size()) { int add = s1.size() - s2.size(); for (int i = 0; i < add; i++) { s2 = s2 + '0'; } } else { int add = s2.size() - s1.size(); for (int i = 0; i < add; i++) { s1 = s1 + '0'; } } reverse(&s1[0], &s1[s1.length()]); reverse(&s2[0], &s2[s2.length()]); string s3; for (int i = 0; i < s1.length(); i++) { int g = s1[i] - '0'; int h = s2[i] - '0'; char c; if (h - g == 0) c = '0'; if (h - g == -1) c = '2'; if (h - g == -2) c = '1'; if (h - g == 1) c = '1'; if (h - g == 2) c = '2'; s3 += c; } long long h = 0; for (int i = 0; i < s3.length(); i++) h += (s3[i] - '0') * pow(3, s3.length() - 1 - i); cout << h; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; if (b % a == 0) { cout << b / a; } else { cout << b / a + 1 << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; pair<int, int> inp[n]; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; if (y < x) swap(x, y); inp[i].first = y; inp[i].second = x; } sort(inp, inp + n); int ls = -10005; vector<int> ans; for (int i = 0; i < n; i++) { if (inp[i].second > ls) { ans.push_back(inp[i].first); ls = inp[i].first; } } cout << ans.size() << "\n"; for (auto x : ans) cout << x << " "; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 250; const int INF = 0x3f3f3f3f; char arr[10][10]; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } bool judge(long long t, long long a, long long b) { if (log(a * 1.0) + log(b * 1.0) - log(gcd(a, b) * 1.0) > log(t)) return true; return false; } int main() { long long xx, yy, t, a, b, lcm, temp; scanf("%I64d%I64d%I64d", &t, &a, &b); if (a == b) { cout << "1/1" << endl; return 0; } if (b > a) swap(a, b); yy = t; if (judge(t, a, b)) xx = min(b - 1, t); else { lcm = a / gcd(a, b) * b; long long x = t / lcm; xx = x * b + min((t - lcm * x), b - 1); } temp = gcd(xx, yy); xx /= temp; yy /= temp; cout << xx << '/' << yy << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int x = 0; int y = 0; cin >> x; int a[x]; for (int i = 0; i < x; i++) { cin >> a[i]; } for (int i = 1; i < x - 1; i++) { if (a[i] > a[i - 1] && a[i] > a[i + 1]) { y++; } if (a[i] < a[i - 1] && a[i] < a[i + 1]) { y++; } } cout << y; return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; const int mxn = 200006; const int MXL = 0x3f3f3f3f; map<int, int> s; int d, n, m; void EXEC() { cin >> d >> n >> m; s[0] = 0; s[n] = MXL; s[d] = MXL; for (int i = 1; i <= m; ++i) { int X, P; scanf("%d%d", &X, &P); if (!s.count(X)) s[X] = MXL; s[X] = min(s[X], P); if (!s.count(X + n)) s[X + n] = MXL; } ll ans = 0; multiset<pair<int, int>> P; for (auto it = s.begin(); it != s.end(); ++it) { auto &i = *it; if (i.first == d) break; if (i.second != MXL) P.insert({i.second, i.first}); while (!P.empty() && P.begin()->second + n <= i.first) P.erase(P.begin()); if (P.empty()) { cout << -1 << endl; return; } ans += ll(next(it)->first - i.first) * P.begin()->first; } cout << ans << endl; } int main() { EXEC(); return 0; }
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 2E3, MAXM = 5E5 + 10; int n, m; bitset<MAXN> a[MAXN], b[MAXN]; int x[MAXM], y[MAXM]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d%d", x + i, y + i); a[--x[i]].set(--y[i]); } for (int i = 0; i < n; ++i) b[i].set(i); int r = 0; for (int c = 0; c < n; ++c) { int i = r; for (; i < n && !a[i].test(c); ++i) ; if (i == n) break; swap(a[r], a[i]), swap(b[r], b[i]); for (i = r + 1; i < n; ++i) if (a[i].test(r)) a[i] ^= a[r], b[i] ^= b[r]; ++r; } for (int i = n - 1; i > 0; --i) { for (int j = i - 1; j >= 0; --j) if (a[j].test(i)) a[j] ^= a[i], b[j] ^= b[i]; } for (int i = 0; i < m; ++i) puts(r == n && !b[y[i]].test(x[i]) ? "YES" : "NO"); return 0; }
10
#include <bits/stdc++.h> using namespace std; int k, n, m, q; string bas[55]; pair<string, map<string, int> > comp[55]; map<string, int> ans[111]; inline bool isdig(char ch) { return '0' <= ch && ch <= '9'; } inline int s2i(string s) { int res = 0; for (int i = (0); i < ((int)s.size()); i++) res = res * 10 + s[i] - '0'; return res; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> k >> n >> m >> q; for (int i = (1); i < (n + 1); i++) cin >> bas[i]; for (int i = (1); i < (m + 1); i++) { string s; cin >> s; s = s.substr(0, (int)s.size() - 1); map<string, int> Map; while (1) { string x, y; cin >> x >> y; bool end = 0; char ch = y[(int)y.size() - 1]; if (isdig(ch)) end = 1; else y = y.substr(0, (int)y.size() - 1); Map[x] = s2i(y); if (end) break; } comp[i] = make_pair(s, Map); } while (q--) { int x; string y; cin >> x >> y; ans[x][y]++; for (int i = (1); i < (m + 1); i++) { bool ok = 1; for (auto u : comp[i].second) { if (ans[x][u.first] < u.second) { ok = 0; break; } } if (ok) { ans[x][comp[i].first]++; for (auto u : comp[i].second) { ans[x][u.first] -= u.second; } break; } } } for (int i = (1); i < (k + 1); i++) { int cnt = 0; for (auto u : ans[i]) if (u.second) ++cnt; cout << cnt << "\n"; for (auto u : ans[i]) if (u.second) cout << u.first << " " << u.second << "\n"; } return 0; }
6
#include <bits/stdc++.h> using namespace std; template <class T1> void deb(T1 e) { cout << e << endl; } template <class T1, class T2> void deb(T1 e1, T2 e2) { cout << e1 << " " << e2 << endl; } int arr[100005]; int main() { int N; while (cin >> N) { int same = 1; int pre; for (__typeof(0) i = (0); i < N; i++) { cin >> arr[i]; if (i == 0) { pre = arr[i]; continue; } same &= (pre == arr[i]); } if (N == 1 or N == 2 or same) { cout << -1 << endl; return 0; } else { for (__typeof(0) i = (0); i < N - 1; i++) { if (arr[i] != arr[i + 1]) { swap(arr[i], arr[i + 1]); int a = 1, d = 1; for (__typeof(0) j = (0); j < N - 1; j++) { a &= (arr[j] <= arr[j + 1]); d &= (arr[j] >= arr[j + 1]); } if (a or d) { swap(arr[i], arr[i + 1]); continue; } else { cout << i + 1 << " " << i + 1 + 1 << endl; return 0; } } } cout << -1 << endl; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAX = (int)1e9; const long long LMAX = (long long)1e15; void fasto() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } void custom() { freopen("input.txt", "r", stdin); freopen("output.txt", "2", stdout); } void need() { fasto(); } long long power(long long a, long long b) { long long ans = 1; while (b) { if (b % 2) ans *= a; a *= a; b /= 2; } return ans; } int main() { need(); long long t; cin >> t; while (t--) { long long n; cin >> n; long long nn = 4 * n; long long a[nn]; for (int i = 0; i < nn; i++) cin >> a[i]; long long cnt[10005] = {0}; vector<long long> f; for (int i = 0; i < nn; i++) { cnt[a[i]]++; if (cnt[a[i]] == 1) f.push_back(a[i]); } sort(f.begin(), f.end()); n = f.size(); long long mul = f[0] * f[n - 1]; bool f1 = 1; long long i = 0, j = n - 1; while (i <= j) { if (cnt[f[i]] != cnt[f[j]] || cnt[f[i]] % 2 || f[i] * f[j] != mul) { f1 = 0; break; } i++; j--; } if (f1) cout << "YES" << '\n'; else cout << "NO" << '\n'; } return 0; }
2
#include <bits/stdc++.h> using namespace std; static vector<long long> DEFAULT_VECTOR; void subsetProds(vector<long long> arr, long long l, long long r, long long m, vector<long long> &v = DEFAULT_VECTOR, long long prod = 1) { if (l > r) { prod %= m; v.push_back(prod); return; } subsetProds(arr, l + 1, r, m, v, prod * arr[l]); subsetProds(arr, l + 1, r, m, v, prod); } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } string decToBinary(long long n) { string binary = ""; bool check = false; for (long long i = 63; i >= 0; i--) { long long k = n >> i; if (k & 1) { binary += "1"; check = true; } else if (check) binary += "0"; } return binary; } void binarySearch(vector<long long> v, long long mini, long long maxi, long long k, long long ans) { long long mid = (maxi + mini) / 2; if (mini == maxi || mid == mini) { cout << ans << "\n"; return; } long long total = 0; for (long long i = 0; i < v.size(); i++) { total += v[i] / mid; } if (total >= k) { ans = mid; binarySearch(v, mid, maxi, k, ans); } else { binarySearch(v, mini, mid, k, ans); } } long long modpower(long long x, long long y, long long p) { long long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } unsigned long long modInverse(unsigned long long n, long long p) { return modpower(n, p - 2, p); } unsigned long long nCrModPFermat(unsigned long long n, long long r, long long p) { if (r == 0) return 1; unsigned long long fac[n + 1]; fac[0] = 1; for (long long i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i); return (fac[n] * modInverse(fac[r], p) * modInverse(fac[n - r], p)); } long long fact(int n); long long nCr(int n, int r) { return fact(n) / (fact(r) * fact(n - r)); } long long fact(int n) { long long res = 1; for (long long i = 2; i <= n; i++) res = res * i; return res; } long long power(long long x, long long y) { long long temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else { if (y > 0) return x * temp * temp; else return (temp * temp) / x; } } long long FindMaxSumOfNoneConSubset(long long arr[], long long n) { long long incl = arr[0]; long long excl = 0; long long excl_new; long long i; for (i = 1; i < n; i++) { excl_new = (incl > excl) ? incl : excl; incl = excl + arr[i]; excl = excl_new; } return ((incl > excl) ? incl : excl); } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long checkRecursive(long long x, long long n, long long curr_num = 1, long long curr_sum = 0) { long long results = 0; long long p = power(curr_num, n); while (p + curr_sum < x) { results += checkRecursive(x, n, curr_num + 1, p + curr_sum); curr_num++; p = power(curr_num, n); } if (p + curr_sum == x) results++; return results; } long long merge(long long arr[], long long temp[], long long left, long long mid, long long right) { long long inv_count = 0; long long i = left; long long j = mid; long long k = left; while ((i <= mid - 1) && (j <= right)) { if (arr[i] <= arr[j]) temp[k++] = arr[i++]; else { temp[k++] = arr[j++]; inv_count = inv_count + (mid - i); } } while (i <= mid - 1) temp[k++] = arr[i++]; while (j <= right) temp[k++] = arr[j++]; for (i = left; i <= right; i++) arr[i] = temp[i]; return inv_count; } long long _mergeSort(long long arr[], long long temp[], long long left, long long right) { long long mid, inv_count = 0; if (right > left) { mid = (right + left) / 2; inv_count = _mergeSort(arr, temp, left, mid); inv_count += _mergeSort(arr, temp, mid + 1, right); inv_count += merge(arr, temp, left, mid + 1, right); } return inv_count; } long long countSwaps(long long arr[], long long n) { long long temp[n]; return _mergeSort(arr, temp, 0, n - 1); } bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); } long long modInverse(long long a, long long m) { long long m0 = m; long long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } bool isPower(long long x, long long y) { long long res1 = log(y) / log(x); double res2 = log(y) / log(x); return (res1 == res2); } set<long long> st; void generateNumbers(long long n, long long num, long long a, long long b) { if (num > 0 && num < n) st.insert(num); if (num > n) return; if (num * 10 + a > num) generateNumbers(n, num * 10 + a, a, b); generateNumbers(n, num * 10 + b, a, b); } void printNumbers(long long n) { for (long long i = 0; i <= 9; i++) for (long long j = i + 1; j <= 9; j++) generateNumbers(n, 0, i, j); long long c = 0; while (!st.empty()) { cout << *st.begin() << " "; st.erase(st.begin()); } } void solve() { long long n, c1 = 0, c2 = 0; cin >> n; string s; cin >> s; for (long long i = 0; i < n - 1; i++) { if (s[i] == '1' && s[i + 1] == '1') { c1++; } if (s[i] == '0' && s[i + 1] == '0') { c2++; } } cout << max(c1, c2) << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<int> V[100005]; int n, m; int bus() { int d1, d2, i; if (n != m + 1) return 0; if (n == 1) return 1; d1 = d2 = 0; for (i = 1; i <= n; i++) { if (V[i].size() == 1) d1++; else if (V[i].size() == 2) d2++; else return 0; } if (d1 == 2 && d2 == n - 2) return 1; return 0; } int ring() { if (n != m) return 0; int i; for (i = 1; i <= n; i++) if (V[i].size() != 2) return 0; return 1; } int star() { if (n != m + 1) return 0; int i, x = 0; for (i = 1; i <= n; i++) if (V[i].size() > 1) x++; if (x == 1) return 1; return 0; } int main() { int i, u, v; scanf("%d %d", &n, &m); for (i = 0; i < m; i++) { scanf("%d %d", &u, &v); V[u].push_back(v); V[v].push_back(u); } if (bus()) printf("bus topology\n"); else if (ring()) printf("ring topology\n"); else if (star()) printf("star topology\n"); else printf("unknown topology\n"); return 0; }
2
#include <bits/stdc++.h> const int mod = 1e9 + 7; int n, m, cnt, C[55][55], f[55][55][105], g[55][55][105], ans; int main() { scanf("%d%d%d", &n, &m, &cnt); n /= 2; C[0][0] = 1; for (int i = 1; i <= n; ++i) { C[i][0] = 1; for (int j = 1; j <= i; ++j) C[i][j] = std ::min(C[i - 1][j - 1] + C[i - 1][j], 105); } for (int k = 1; k <= m; ++k) { if (k > 1) for (int i = 1; i <= n; ++i) g[i][i][1] = 1; for (int i = 1; i <= n; ++i) for (int j = 1; j <= i; ++j) for (int c = 1; c <= cnt; ++c) if (f[i][j][c]) for (int l = 1; i + l <= n && c * C[j - 1 + l][l] <= cnt; ++l) (g[i + l][l][c * C[j - 1 + l][l]] += f[i][j][c]) %= mod; for (int i = 1; i <= n; ++i) for (int j = 1; j <= i; ++j) for (int c = 1; c <= cnt; ++c) (ans += g[i][j][c]) %= mod; memcpy(f, g, sizeof(g)); memset(g, 0, sizeof(g)); } printf("%d\n", ans); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4 + 4; int par[maxn]; int rankx[maxn]; int n, m, k; int gov[maxn]; int num[maxn]; void init(int n) { for (int i = 1; i <= n; i++) { par[i] = i; rankx[i] = 0; } } int findx(int x) { if (par[x] == x) return x; else return par[x] = findx(par[x]); } void unite(int x, int y) { x = findx(x); y = findx(y); if (x == y) return; if (rankx[x] < rankx[y]) { par[x] = y; num[y] += num[x]; } else { par[y] = x; num[x] += num[y]; if (rankx[x] == rankx[y]) rankx[x]++; } } bool same(int x, int y) { return findx(x) == findx(y); } int main() { scanf("%d%d%d", &n, &m, &k); init(n); for (int i = 1; i <= n; i++) num[i] = 1; int x, y; for (int i = 1; i <= k; i++) { scanf("%d", &x); gov[i] = x; } for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); unite(x, y); } int nm = 0; for (int i = 1; i <= k; i++) nm = max(nm, num[findx(gov[i])]); bool flag = 1; int number = 0; long long ans = 0; for (int i = 1; i <= k; i++) { int fa = findx(gov[i]); if (num[fa] == nm && flag) flag = 0; else number += num[fa]; } flag = 1; for (int i = 1; i <= k; i++) { int fa = findx(gov[i]); if (num[fa] == nm && flag) { flag = 0; ans += (long long)(n - number) * (n - number - 1) / 2; } else ans += (long long)(num[fa]) * (num[fa] - 1) / 2; } ans -= m; cout << ans << endl; }
3
#include <bits/stdc++.h> using namespace std; string s; int x[100009], y[100009], z[100009], m, l, r; int main() { cin >> s; cin >> m; for (int i = 1; i <= s.size(); i++) { x[i] = x[i - 1]; y[i] = y[i - 1]; z[i] = z[i - 1]; if (s[i - 1] == 'x') x[i]++; if (s[i - 1] == 'y') y[i]++; if (s[i - 1] == 'z') z[i]++; } for (int i = 1; i <= m; i++) { cin >> l >> r; if (min(x[r] - x[l - 1], min(y[r] - y[l - 1], z[r] - z[l - 1])) + 1 >= max(x[r] - x[l - 1], max(y[r] - y[l - 1], z[r] - z[l - 1]))) { cout << "YES\n"; continue; } if (r - l + 1 <= 2) { cout << "YES\n"; continue; } cout << "NO\n"; } }
3
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 10; int main() { ios_base::sync_with_stdio(false); int n, m, q, l, r, tlen, cnt; char s[N], t[N]; bool match[N] = {0}; cin >> n >> m >> q >> s >> t; tlen = strlen(t); for (int i = 0; s[i]; ++i) { match[i] = !strncmp(s + i, t, tlen); } while (q-- > 0) { cin >> l >> r; --l; --r; cnt = 0; for (int i = l; i <= r - tlen + 1; ++i) { cnt += match[i]; } cout << cnt << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long n, m, k, tmp[509]; string ret, Fib[15]; inline bool is_pre(long long f_pos, string& s, long long s_start, unsigned cmp_len) { if (f_pos > 14) f_pos = 12 | (f_pos & 1); return cmp_len <= Fib[f_pos].size() && Fib[f_pos].substr(0, cmp_len) == s.substr(s_start, cmp_len); } inline bool is_suf(long long f_pos, string& s, long long s_start, unsigned cmp_len) { if (f_pos > 14) f_pos = 14; return cmp_len <= Fib[f_pos].size() && Fib[f_pos].substr(Fib[f_pos].size() - cmp_len) == s.substr(s_start, cmp_len); } signed main() { Fib[0] = "0", Fib[1] = "1"; for (long long i = 2; i <= 14; ++i) Fib[i] = Fib[i - 2] + Fib[i - 1]; scanf("%lld%lld%lld", &n, &k, &m); for (long long i = 1; i <= m; ++i) { long long Tag = (i == 1 ? 0 : is_suf(n, ret, 0, ret.size())); if (k <= Tag) return puts(ret.c_str()), 0; long long cnt = Tag + [&]() { string test0 = ret + '0'; tmp[0] = (test0 == "0"), tmp[1] = (test0 == "1"); unsigned len = test0.size(); for (long long i = 2; i <= n; ++i) { tmp[i] = tmp[i - 1] + tmp[i - 2]; for (unsigned j = 1; j < len; ++j) if (is_suf(i - 2, test0, 0, j) && is_pre(i - 1, test0, j, len - j)) ++tmp[i]; if (tmp[i] + Tag >= k) return k; } return tmp[n]; }(); if (k <= cnt) { k -= Tag; ret += '0'; } else { k -= cnt; ret += '1'; } } puts(ret.c_str()); return 0; }
9
#include <bits/stdc++.h> using namespace std; int n; long long jie(int x) { long long sum = 1; for (int i = 1; i <= x; i++) { sum *= i; sum = sum % (1000000007); } return sum; } long long zhi(int x) { long long sum = 1; for (int i = 0; i < x; i++) { sum *= 2; sum = sum % 1000000007; } return sum; } long long ans(int x) { long long sum = jie(x); sum += 1000000007; sum -= zhi(x - 1); sum = sum % 1000000007; return sum; } int main() { cin >> n; cout << ans(n); cin >> n; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int cmx = 2e5 + 5; const long long mod = 1e9 + 7; long long dp[200100]; long long temp[200100]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int r, g, s; cin >> r >> g; int h = 1; for (;; h++) { int sum = (h * (h + 1)) / 2; if (sum > (r + g)) break; } h--; if (r) dp[r - 1] = 1; if (g) dp[r] = 1; for (int i = 1; i < h; i++) { memset(temp, 0, sizeof temp); for (int red = 0; red <= r; red++) { int tot = (i * (i + 1)) / 2; int used_red = r - red; if (used_red > tot) continue; int used_green = tot - used_red; int rem_green = g - used_green; if (used_green > g) continue; if (red >= (i + 1)) { temp[red - (i + 1)] = (temp[red - (i + 1)] + dp[red]) % mod; } if (rem_green >= (i + 1)) { temp[red] = (temp[red] + dp[red]); } } for (int i = 0; i < cmx; i++) dp[i] = temp[i]; } int ans = 0; for (int i = 0; i <= r; i++) { ans = (ans + dp[i]); if (ans >= mod) ans %= mod; } cout << ans << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; int n; vector<long long> f(200100, 0); vector<long long> cf(200100, 0); long long rangeSum(int l, int r) { long long sec = 0; if (r >= 200100) { sec = n; } else { sec = cf[r - 1]; } return sec - cf[l - 1]; } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) { int a; cin >> a; f[a]++; } cf[0] = f[0]; for (int i = 0; i < 200100; i++) { cf[i + 1] = cf[i] + f[i + 1]; } long long best = 0; for (int i = 1; i < 200100; i++) { if (f[i] == 0) continue; long long sum = 0; for (int j = i; j < 200100; j += i) { sum += j * rangeSum(j, j + i); } best = max(best, sum); } cout << best << endl; }
5
#include <bits/stdc++.h> const long long INF = 4e18L + 1; const int IINF = 2e9 + 1; const int limit = 1048576; using namespace std; int main() { vector<int> XOR(1000005); for (int i = 1; i <= 1000000; i++) XOR[i] = XOR[i - 1] ^ i; int n, m; cin >> n >> m; vector<int> V(n); for (auto& el : V) cin >> el; vector<pair<int, int> > Zapyt(m); for (auto& el : Zapyt) { cin >> el.first >> el.second; el.first--; el.second--; } vector<int> wyn(m, 0); for (int i = 0; i < n; i++) { vector<int> T(n), M(n); M[i] = T[i] = V[i]; for (int j = i + 1; j < n; j++) { T[j] = XOR[max(V[i], V[j])] ^ XOR[min(V[i], V[j]) - 1]; M[j] = max(M[j - 1], T[j]); } for (int j = 0; j < m; j++) { if (Zapyt[j].first <= i && Zapyt[j].second >= i) wyn[j] = max(wyn[j], M[Zapyt[j].second]); } } for (int i = 0; i < m; i++) cout << wyn[i] << "\n"; }
10
#include <bits/stdc++.h> using namespace std; double const e = 0.000001; int main() { int n, a, d; cin >> n >> a >> d; int prevv = 10000000; double prevt = 0; for (int i = 0; i < n; i++) { int t, v; cin >> t >> v; double t1 = (double)v / a; double s1 = a * t1 * t1 / 2; double t2 = 0; if (s1 - d > e) { t1 = sqrt((2.0 * d) / a); } else { double s2 = d - s1; t2 = s2 / v; } double tt = t1 + t2; if (t + tt - prevt > e) { prevt = t + tt; prevv = v; } printf("%f\n", prevt); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m, q, i, t, r, c, x, j, temp; int a[10100][110]; for (i = 0; i < 10001; i++) for (j = 0; j < 110; j++) a[i][j] = 0; cin >> n >> m >> q; vector<string> com; string s; getline(cin, s); for (i = 0; i < q; i++) { getline(cin, s); com.push_back(s); } for (i = com.size() - 1; i >= 0; i--) { int t; sscanf(com[i].c_str(), "%d%d%d%d", &t, &r, &c, &x); if (t == 1) { temp = a[r][m]; for (j = m; j > 1; j--) a[r][j] = a[r][j - 1]; a[r][1] = temp; } else if (t == 2) { c = r; temp = a[n][c]; for (j = n; j > 1; j--) a[j][c] = a[j - 1][c]; a[1][c] = temp; } else { a[r][c] = x; } } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) cout << a[i][j] << ' '; cout << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long modpow(long long base, long long exp) { long long res = 1; base = base % mod; while (exp) { if (exp & 1) { res = (res * base) % mod; } exp >>= 1; base = (base * base) % mod; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; vector<int> bill; for (int i = 0; i < n; ++i) { int amt; cin >> amt; bill.emplace_back(amt); } sort(bill.begin(), bill.end()); int q; cin >> q; while (q--) { int req; cin >> req; int ans = 1e9; for (int i = 0; i < n; ++i) { for (int j = 1; j <= k; ++j) { int cur = bill[i] * j; if (cur > req) { break; } if (cur == req) { ans = min(ans, j); } for (int x = 1; x <= k - j; ++x) { int rem = req - cur; if (rem % x != 0) { continue; } rem /= x; if (binary_search(bill.begin(), bill.end(), rem)) { ans = min(ans, j + x); } } } } cout << (ans == 1e9 ? -1 : ans) << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int max(int a, int b) { int t; if (a >= b) return a; else return b; } int min(int a, int b) { int t; if (a <= b) return a; else return b; } int main() { int m, n; int t; cin >> m >> n; if (m % 2 == 0 || n % 2 == 0) { if (m % 2 != 0) { t = m; m = n; n = t; } t = m / 2 * n; cout << t; return 0; } else { if (m == 1 && n == 1) { cout << "0"; return 0; } else { t = (max(m, n) - 1) / 2 * min(m, n) + (min(m, n) - 1) / 2; cout << t; return 0; } } }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { if (a[i] % 2 == 0) { a[i] = 0; } else a[i] = 1; } int sum = 0; for (int i = 0; i < n; i++) { sum += a[i]; } cout << min(sum, n - sum); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; int a[n]; vector<int> dp(1000005, 0); for (int i = 0; i < n; i++) { cin >> a[i]; dp[a[i]]++; } sort(a, a + n); for (int i = 0; i < n; i++) { for (int j = 2 * a[i]; j <= 1000005; j += a[i]) { dp[j] = max(dp[j], dp[a[i]] + 1); } } int ans = 1; for (int i = 0; i < n; i++) { ans = max(ans, dp[a[i]]); } cout << endl; cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; void in() { return; } template <typename T, typename... Types> void in(T &a, Types &...b) { cin >> (a); in(b...); } void o() { return; } template <typename T, typename... Types> void o(T a, Types... b) { cout << (a); cout << ' '; o(b...); } bool sortin(const pair<long long int, long long int> &e, const pair<long long int, long long int> &f) { return (e.first < f.first); } bool POT(long long int x) { return x && (!(x & (x - 1))); } int i, j, k, l, m, n, p, q, r, a, b, c, x, y, z, ts, mn = 1e18, mod = 1e9 + 7; int ar[250005], xr[250005], tree[4 * 100000]; void lucky(long long int num) { if (num > 10000) return; xr[num] = 1; lucky(num * 10 + 4); lucky(num * 10 + 7); } void initial(int nod, long long int st, long long int en) { if (st == en) { tree[nod] = xr[ar[st]]; return; } int mid = (st + en) / 2; initial(nod * 2, st, mid); initial(nod * 2 + 1, mid + 1, en); tree[nod] = tree[nod * 2] + tree[nod * 2 + 1]; } void update(int nod, int st, int en, int pos, int val) { if (st > pos or en < pos) return; if (st == pos and en == pos) { tree[nod] += val; return; } int mid = (st + en) / 2; update(nod * 2, st, mid, pos, val); update(nod * 2 + 1, mid + 1, en, pos, val); tree[nod] = tree[nod * 2] + tree[nod * 2 + 1]; } int query(int nod, int st, int en, int from, int to) { if (st > to or en < from) return 0; if (st >= from and en <= to) return tree[nod]; int mid = (st + en) / 2; return query(nod * 2, st, mid, from, to) + query(nod * 2 + 1, mid + 1, en, from, to); } int main() { { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }; scanf("%d %d", &n, &m); lucky(0); for (int i = 1; i <= n; i++) scanf("%d", &ar[i]); initial(1, 1, n); while (m--) { char s[10]; scanf("%s", s); if (s[0] == 'a') { scanf("%d %d %d", &l, &r, &x); for (int i = l; i <= r; i++) { if (xr[ar[i]]) update(1, 1, n, i, -1); ar[i] += x; if (xr[ar[i]]) update(1, 1, n, i, 1); } } else { scanf("%d %d", &l, &r); o(query(1, 1, n, l, r)); cout << endl; } } }
8
#include <bits/stdc++.h> using namespace std; int n, x; int a[100005]; int main(void) { ios_base::sync_with_stdio(0); cin.tie(NULL); int ntest; cin >> ntest; while (ntest--) { cin >> n >> x; for (int i = 1; i <= n; ++i) cin >> a[i]; sort(a + 1, a + 1 + n); int ans = 1e9 + 7; for (int i = 1; i <= n; ++i) if (x == a[i]) ans = 1; if (ans == 1) { cout << ans << '\n'; continue; } int hops = x / a[n]; if (hops <= 1) { cout << 2 << '\n'; continue; } ans = hops + 1; x -= a[n] * (hops - 1); for (int i = 1; i <= n; ++i) if (x == a[i]) ans = hops; cout << ans << '\n'; } return 0; }
2
#include <bits/stdc++.h> long long p2[1000005], mod = 1000000007; char in[1000005]; int main() { int i, n, keep = 0; long long ans = 0; p2[0] = 1; for (i = 1; i <= 1000000; i++) p2[i] = (p2[i - 1] * 2) % mod; for (i = 0; i <= 1000000; i++) { p2[i]--; if (p2[i] < 0) p2[i] += mod; } scanf("%s", in); n = strlen(in); for (i = 0; i < n; i++) { if (in[i] == 'a') keep++; if (in[i] == 'b') { ans += p2[keep]; ans %= mod; } } printf("%lld", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { char str1[10], str2[10], str3[10]; cin >> str1 >> str2 >> str3; if (strcmp(str1, "rock") == 0) if (strcmp(str2, "scissors") == 0 && strcmp(str3, "scissors") == 0) { cout << 'F'; return 0; } if (strcmp(str1, "scissors") == 0) if (strcmp(str2, "paper") == 0 && strcmp(str3, "paper") == 0) { cout << 'F'; return 0; } if (strcmp(str1, "paper") == 0) if (strcmp(str2, "rock") == 0 && strcmp(str3, "rock") == 0) { cout << 'F'; return 0; } if (strcmp(str2, "rock") == 0) if (strcmp(str1, "scissors") == 0 && strcmp(str3, "scissors") == 0) { cout << 'M'; return 0; } if (strcmp(str2, "scissors") == 0) if (strcmp(str1, "paper") == 0 && strcmp(str3, "paper") == 0) { cout << 'M'; return 0; } if (strcmp(str2, "paper") == 0) if (strcmp(str1, "rock") == 0 && strcmp(str3, "rock") == 0) { cout << 'M'; return 0; } if (strcmp(str3, "rock") == 0) if (strcmp(str1, "scissors") == 0 && strcmp(str2, "scissors") == 0) { cout << 'S'; return 0; } if (strcmp(str3, "scissors") == 0) if (strcmp(str1, "paper") == 0 && strcmp(str2, "paper") == 0) { cout << 'S'; return 0; } if (strcmp(str3, "paper") == 0) if (strcmp(str1, "rock") == 0 && strcmp(str2, "rock") == 0) { cout << 'S'; return 0; } cout << '?'; return 0; }
0
#include <bits/stdc++.h> int a[200001]; int main() { int n, m, T; double c; scanf("%d%d%lf", &n, &T, &c); double rreal{0}; double rapprox{0}; for (auto i = 0; i < n; i++) { scanf("%d", &a[i]); if (i < T) { rreal += (double)a[i] / (double)T; rapprox = (rapprox + (double)a[i] / (double)T) / c; } } int preal = T - 1; scanf("%d", &m); for (auto j = 0; j < m; j++) { int pj; scanf("%d", &pj); while (preal < pj - 1) { preal++; rreal = rreal + (-(double)a[preal - T] + (double)a[preal]) / (double)T; rapprox = (rapprox + (double)a[preal] / (double)T) / c; } printf("%.9lf %.9lf %.9lf\n", rreal, rapprox, fabs(rreal - rapprox) / rreal); } }
3
#include <bits/stdc++.h> using namespace std; int main() { int a = 0; cin >> a; int c1 = 0, c2 = 0; for (int _ = 0; _ < a; _++) { int b = 0; int c = 0; cin >> b >> c; if (b > c) { c1++; } if (b < c) { c2++; } } if (c1 > c2) { cout << "Mishka"; } else if (c2 > c1) { cout << "Chris"; } else { cout << "Friendship is magic!^^"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long x, y, n, m, k, i, j, q, w; long long cur[1488228 * 2], pres[1488228], a[1488228], t[1488228 * 2]; vector<long long> f; long long phis[1005000]; long long pr[1005000]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); for (int i = 2; i <= 1000000; i++) { pr[i] = 1; } for (int i = 1; i <= 1000000; i++) { phis[i] = i; } for (int i = 1; i <= 1000000; i++) { for (int j = i + i; j <= 1000000; j += i) { phis[j] -= phis[i]; } } cin >> n >> k; long long ans = (long long)1e+18; for (int j = 0; j < 2; j++) { f.clear(); for (int i = 3; i <= n; i++) { if (j == 0 || i % 2 == 1) { f.push_back(phis[i]); } } if (k > f.size()) { continue; } long long sum = 1 - j; sort(f.begin(), f.end()); for (int i = 0; i < k; i++) { sum += f[i]; } ans = min(ans, sum); } cout << ans + 1 << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; int N, ans; int a[10]; int main() { scanf("%d", &N); if (N & 1) ans += 16; if (N & 2) ans += 2; if (N & 4) ans += 8; if (N & 8) ans += 4; if (N & 16) ans += 1; if (N & 32) ans += 32; printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, tt = 0; string a, b; void cal(int n) { if (n == 1) { a = "", b = ('a' + tt), tt++; return; } if (n == 2) { char c = 'a' + tt; tt++; a = b = c; return; } if (n % 2) { cal((n - 1) / 2); char c = 'a' + tt; tt++; b = b + c; a = a + c + c; } else { cal((n - 2) / 2); char c = 'a' + tt; tt++; b = b + c; a = c + a + c + c; } } int main() { cin >> n; if (n == 1) return printf("a a\n"), 0; cal(n); cout << b << a << " " << b << endl; }
7
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const long long INF = (long long)1e18 + 10; const int MOD = 998244353; int _abs(int x) { return x < 0 ? -x : x; } int add(int x, int y) { x += y; return x >= MOD ? x - MOD : x; } int sub(int x, int y) { x -= y; return x < 0 ? x + MOD : x; } void Add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } void Sub(int &x, int y) { x -= y; if (x < 0) x += MOD; } void Mul(int &x, int y) { x = (long long)(x) * (y) % MOD; } int qpow(int x, int y) { int ret = 1; while (y) { if (y & 1) ret = (long long)(ret) * (x) % MOD; x = (long long)(x) * (x) % MOD; y >>= 1; } return ret; } void checkmin(int &x, int y) { if (x > y) x = y; } void checkmax(int &x, int y) { if (x < y) x = y; } void checkmin(long long &x, long long y) { if (x > y) x = y; } void checkmax(long long &x, long long y) { if (x < y) x = y; } inline int read() { int x = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return x * f; } const int N = 1005; const int M = N * N; char s[N]; int n, m, lcp[N][N]; long long k; __int128_t dp[N][N], sum[N][N]; pair<int, int> a[M]; int tot = 0; bool cmp(pair<int, int> a, pair<int, int> b) { int len = lcp[a.first][b.first]; int ra = a.first + len, rb = b.first + len; if (ra > a.second || rb > b.second) return a.second - a.first >= b.second - b.first; return s[ra] >= s[rb]; } bool check(pair<int, int> now) { for (int i = 0; i <= n + 1; i++) for (int j = 0; j <= m; j++) dp[i][j] = 0, sum[j][i] = 0; dp[n + 1][0] = 1; sum[0][n + 1] = 1; for (int i = n; i >= 1; i--) { for (int j = 1; j + i - 1 <= n && j <= m; j++) { int len = now.second - now.first + 1; int nxt = i + min(lcp[i][now.first], len); if (cmp(make_pair(i, n), now)) { if (nxt - i == len) dp[i][j] = sum[j - 1][nxt]; else dp[i][j] = sum[j - 1][nxt + 1]; } if (dp[i][j] > INF) dp[i][j] = INF; sum[j][i] = sum[j][i + 1] + dp[i][j]; } sum[0][i] = 1; } return dp[1][m] >= k; } int main() { n = read(); m = read(); scanf("%lld", &k); scanf("%s", s + 1); for (int i = n; i >= 1; i--) { for (int j = n; j >= 1; j--) { if (s[i] == s[j]) lcp[i][j] = lcp[i + 1][j + 1] + 1; else lcp[i][j] = 0; } } for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) a[++tot] = make_pair(i, j); } sort(a + 1, a + tot + 1, cmp); int l = 1, r = tot, mid, best; while (l <= r) { mid = (l + r) >> 1; if (check(a[mid])) r = mid - 1, best = mid; else l = mid + 1; } for (int i = a[best].first; i <= a[best].second; i++) printf("%c", s[i]); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; string to_string(string s) { return '"' + s + '"'; } string to_string(char s) { return string(1, s); } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = 1; string r = "{"; for (const auto& x : v) { if (!f) r += ", "; f = 0; r += to_string(x); } return r + "}"; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; double C, T; cin >> C >> T; vector<pair<int, int>> a(n); int sum = 0; for (auto i = (0); i <= (n - 1); ++i) cin >> a[i].first >> a[i].second, sum += a[i].second; sort((a).begin(), (a).end()), reverse((a).begin(), (a).end()); vector<double> p9(n + 1); p9[0] = 1; for (auto i = (1); i <= (n); ++i) p9[i] = p9[i - 1] / 0.9; vector<vector<double>> dp(n + 1, vector<double>(sum + 1 + 1, 1e18)); dp[0][0] = 0; sum = 0; for (auto i = (1); i <= (n); ++i) { int w = a[i - 1].second, t = a[i - 1].first; sum += w; for (auto k = (i); k >= (1); --k) { for (auto s = (w); s <= (sum); ++s) { dp[k][s] = min(dp[k][s], dp[k - 1][s - w] + t * p9[k]); } } } int ans = 0; for (auto k = (1); k <= (n); ++k) { double t = T - k * 10; double s = max(0.0, (C * t - 1) / 2 / C); if (t >= 0 and s <= t) { double at = (t - s) * (1 + s * C); for (auto s = (0); s <= (sum); ++s) if (dp[k][s] <= at + 1e-6) { ans = max(ans, s); } } } cout << ans << "\n"; } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, s; cin >> n >> s; int a[n]; map<int, int> m; for (int i = 0; i < n; ++i) { cin >> a[i]; m[a[i]]++; } int dis = m.size(); auto i = m.begin(); vector<int> v; while (i != m.end()) { v.push_back(i->second); i++; } s = s * 8; s = s / n; if (s > 20) { cout << 0; return 0; } s = pow(2, s); if (s >= dis) { cout << 0; return 0; } long long int sum = 0; long long int summi[v.size() + 1]; summi[0] = 0; for (int i = 1; i <= v.size(); i++) { summi[i] = 0; summi[i] = summi[i - 1] + v[i - 1]; } for (int i = 1; i <= v.size() - s + 1; i++) { long long int kkk = summi[i + s - 1] - summi[i - 1]; if (sum < kkk) sum = kkk; } cout << n - sum; }
4
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 5010; const int MOD = 998244353; const int mod = 998244353; long long f[MAXN][MAXN]; long long fac[MAXN]; void init() { f[0][0] = 1; fac[0] = 1; for (int i = 1; i <= 5000; i++) { f[i][0] = 1; for (int j = 1; j <= i; j++) f[i][j] = (f[i - 1][j] + f[i - 1][j - 1]) % MOD; } for (int i = 1; i <= 5000; ++i) fac[i] = (long long)fac[i - 1] * i % MOD; } long long solve(long long a, long long b) { long long res = 0; for (int i = 0; i <= min(a, b); i++) { res = (res + (long long)f[a][i] * f[b][i] % mod * fac[i] % mod) % MOD; } return res; } int main() { init(); long long a, b, c; while (~scanf("%lld%lld%lld", &a, &b, &c)) { printf("%lld\n", (long long)solve(a, b) * solve(a, c) % mod * solve(b, c) % mod); } return 0; }
5
#include <bits/stdc++.h> using namespace std; map<long long, long long> a; map<long long, vector<long long>> adj; long long cnt, mx; pair<long long, long long> dfs(long long x) { if (adj[x].size() == 0) { mx = max(mx, (long long)a[x]); return {1, a[x]}; } long long sum = 0, ct = 0; for (auto y : adj[x]) { pair<long long, long long> f = dfs(y); sum += f.second; ct += f.first; } sum += a[x]; mx = max(mx, (sum + ct - 1) / ct); return {ct, sum}; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; for (long long i = 2; i <= n; i++) { long long x; cin >> x; adj[x].push_back(i); } long long sum = 0; for (long long i = 1; i <= n; i++) { long long x; cin >> x; a[i] += x; } dfs(1); cout << mx << endl; }
5
#include <bits/stdc++.h> using namespace std; int vis[200005]; vector<int> v[200005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; int ar[n + 2]; for (int i = 0; i < n; i++) { cin >> ar[i]; } for (int i = 0; i < n; i++) { int d = ar[i]; int cnt = 0; while (d) { v[d].push_back(cnt); cnt++; d /= 2; } v[d].push_back(cnt); } int ans = INT_MAX; for (int i = 0; i <= 200000; i++) { if (v[i].size() < k) continue; sort(v[i].begin(), v[i].end()); int sum = 0; for (int j = 0; j < k; j++) { sum += v[i][j]; } ans = min(ans, sum); } cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; inline long long read() { char ch = getchar(); long long x = 0, f = 1; while (ch < '0' || ch > '9') f = ch == '-' ? -1 : 1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * f; } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); cin.tie(0); long long base = (1LL << 7) - 1; long long res = 0, cur; printf("?"); for (long long i = (1); i <= (100); i += (1)) printf(" %lld", i); putchar('\n'); fflush(stdout); cur = read(); res += (cur >> 7) << 7; printf("?"); for (long long i = (1); i <= (100); i += (1)) printf(" %lld", i << 7); putchar('\n'); fflush(stdout); cur = read(); res += cur & base; printf("! %lld\n", res); fflush(stdout); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int M = 1000100; long long n, m, a[M], b[M]; long long suma[M], sumb[M]; map<long long, int> f; int main() { scanf("%d", &n); int pl1 = 0, pl2 = 0; for (int i = 1; i <= n; i++) { scanf("%lld", a + i); suma[i] = suma[i - 1] + a[i]; } scanf("%d", &m); for (int i = 1; i <= m; i++) { scanf("%lld", b + i); sumb[i] = sumb[i - 1] + b[i]; } if (suma[n] != sumb[m]) { printf("-1\n"); } else { int ans = 0; int j = 0; for (int i = 1; i <= m; i++) { while (suma[j] - suma[pl1] < sumb[i] - sumb[pl2]) j++; if (suma[j] - suma[pl1] == sumb[i] - sumb[pl2]) { ans++; pl1 = j; pl2 = i; } } cout << ans << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T bpow(T p, T e) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p); p = (p * p); } return (T)ret; } int toInt(string s) { int sm; stringstream ss(s); ss >> sm; return sm; } int toLlint(string s) { long long int sm; stringstream ss(s); ss >> sm; return sm; } int ts, kk = 1; int n, m, sp; vector<int> al[55]; int e[55][55]; int fr[55]; int dfs(int par, int u) { int rs = fr[u]; int i; for (i = 0; i < al[u].size(); i++) { int v = al[u][i]; if (v != par) rs += dfs(u, v); } return rs; } long long int p[52][52][52][52]; long long int dp(int par, int u, int q1, int q2) { if (al[u].size() == 1) q2 = 0; long long int &pr = p[par][u][q1][q2]; if (pr != -1) return pr; if (q1 + q2 == 0) return pr = 0; pr = (1LL << 60); if (al[u].size() == 1) return pr = dp(u, par, 0, q1) + e[u][par]; int i, j, k; long long int nw[q2 + 1], res = 0; long long int pq[2][q2 + 1]; bool g = 0; bool fs = 0; for (i = 0; i < al[u].size(); i++) { int v = al[u][i]; if (v == par) continue; if (!fs) { pq[g][0] = (1LL << 60); for (j = 1; j < q2 + 1; j++) pq[g][j] = dp(u, v, q1 + q2 - j, j) + e[u][v]; } else { g ^= 1; nw[0] = (1LL << 60); for (j = 1; j < q2 + 1; j++) nw[j] = dp(u, v, q1 + q2 - j, j) + e[u][v]; for (k = 0; k < q2 + 1; k++) { pq[g][k] = 0; for (j = 0; j < k + 1; j++) { pq[g][k] = max(pq[g][k], min(nw[j], pq[g ^ 1][k - j])); } } } fs = 1; } return pr = pq[g][q2]; } int main() { int t, i, j, k; memset(p, -1, sizeof(p)); scanf("%d", &n); for (i = 1; i < n; i++) { scanf("%d%d%d", &j, &k, &t); al[j].push_back(k); al[k].push_back(j); e[j][k] = e[k][j] = t; } int sp; scanf("%d%d", &sp, &m); for (i = 0; i < m; i++) scanf("%d", &k), fr[k]++; long long int rs = (1LL << 60); for (i = 0; i < al[sp].size(); i++) { int v = al[sp][i]; int cnt = dfs(sp, v); if (cnt) rs = min(rs, dp(sp, v, m - cnt, cnt) + e[sp][v]); } printf("%lld\n", rs); return 0; }
9
#include <bits/stdc++.h> using namespace std; vector<int> a[15]; int n, m; set<vector<int> > set1; set<vector<int> > set2; struct Node { vector<int> v; int sum; bool operator<(const Node& rhs) const { return sum < rhs.sum; } }; void solve() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int t, t1; scanf("%d", &t); a[i].push_back(0); for (int j = 0; j < t; j++) { scanf("%d", &t1); a[i].push_back(t1); } } scanf("%d", &m); for (int i = 0; i < m; i++) { vector<int> v; int t; for (int j = 0; j < n; j++) { scanf("%d", &t); v.push_back(t); } set1.insert(v); } priority_queue<Node> q; Node t2; t2.sum = 0; t2.v.clear(); for (int i = 1; i <= n; i++) { t2.v.push_back(a[i].size() - 1); t2.sum += a[i][a[i].size() - 1]; } q.push(t2); set2.insert(t2.v); vector<int> ans; while (!q.empty()) { Node n1 = q.top(); q.pop(); if (set1.find(n1.v) == set1.end()) { ans = n1.v; break; } for (int i = 1; i <= n; i++) { Node n2 = n1; if (n2.v[i - 1] >= 2) { int t = n2.v[i - 1]; n2.sum = n2.sum + a[i][t - 1] - a[i][t]; n2.v[i - 1] = t - 1; if (set2.find(n2.v) == set2.end()) { q.push(n2); set2.insert(n2.v); } } } } for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]); printf("\n"); } int main() { solve(); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, m, min, ans = 0; cin >> n >> m; min = (n < m ? n : m); for (int i = 0; i <= min; ++i) { for (int j = 0; j <= min; ++j) { if (i * i + j == n && j * j + i == m) ++ans; } } cout << ans << '\n'; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5, M = 1000000007; int tot, p[N]; long long n, m, f[N], f1[N], f2[N], f3[N], g[N]; bool check[N]; long long calc(long long x) { long long a = x, b = x + 1, c = 2 * x + 1; if (a % 3 == 0) a /= 3; else if (b % 3 == 0) b /= 3; else c /= 3; return a * b % M * c % M; } void init() { f[1] = 1ll; for (int i = 2; i < N; ++i) { if (!check[i]) p[++tot] = i, f[i] = i - 1; for (int j = 1; j <= tot && i * p[j] < N; ++j) { check[i * p[j]] = true; if (i % p[j] == 0) { f[i * p[j]] = f[i] * p[j]; break; } else f[i * p[j]] = f[i] * (p[j] - 1); } } for (int i = 1; i < N; ++i) for (int j = i; j < N; j += i) g[j] = (g[j] + 1ll * f[j / i] * i % M) % M; for (int i = 1; i < N; ++i) { g[i] = (g[i] * 2 + 1ll * i * i % M * i % M - 1ll * i * i % M * (i + 1) % M - 2ll * i * i % M + calc(i) + M * 2) % M; } for (int i = 1; i < N; ++i) { f1[i] = (f1[i - 1] + 1ll * g[i]) % M; f2[i] = (f2[i - 1] + 1ll * g[i] * i % M) % M; f3[i] = (f3[i - 1] + 1ll * g[i] * i % M * i % M) % M; } } int main() { init(); int T; scanf("%d", &T); while (T--) { scanf("%I64d%I64d", &n, &m); if (n > m) swap(n, m); long long ans = ((n * m + n + m + 1) % M * f1[n] % M - (m + n + 2) * f2[n] % M + f3[n] + M) % M; printf("%I64d\n", ans); } return 0; }
10
#include <bits/stdc++.h> using namespace std; inline int ckmax(int &a, int b) { return a < b ? a = b, 1 : 0; } inline int ckmin(int &a, int b) { return a > b ? a = b, 1 : 0; } const int maxn = 200100; int n, p[maxn], fa[maxn], ok[maxn]; int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", p + i); for (int i = 1; i <= n; ++i) fa[i] = i; for (int i = 1; i <= n; ++i) { if (p[i] == i) continue; int u = find(i), v = find(p[i]); if (u != v) { ok[i] = 1; fa[v] = u; } } bool flag = 0; int last; for (int i = 1; i <= n; ++i) if (p[i] == i) { ok[i] = 1; flag = 1; last = find(i); break; } int ans = 0; for (int i = 1; i <= n; ++i) if (!ok[i]) { if (!flag) { p[i] = i; flag = 1; last = find(i); ans++; } else { p[i] = last; last = find(i); ans++; } } printf("%d\n", ans); for (int i = 1; i <= n; ++i) printf("%d ", p[i]); return 0; }
4
#include <bits/stdc++.h> using namespace std; map<int, int> mp[1001]; int n, k, cnt; int main(void) { scanf("%d %d", &n, &k); if ((n - 1) * n / 2 < k * n) { puts("-1"); return 0; } printf("%d\n", k * n); for (int i = 0; i < n; i++) { cnt = 0; for (int j = 0; j < n; j++) { if (i != j && !mp[i][j]) { printf("%d %d\n", i + 1, j + 1); mp[i][j] = 1; mp[j][i] = 1; cnt++; } if (cnt == k) break; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int p = 998244353; vector<vector<long long>> cache(1001, vector<long long>(1001, -1)); long long ncr(int n, int r) { if (cache[n][r] == -1) cache[n][r] = (ncr(n - 1, r) % p + ncr(n - 1, r - 1) % p) % p; return cache[n][r]; } long long dp(int l, int i, vector<int> &a, int n, vector<int> &table) { if (l <= 0) return 0; long long ans = 0; int j = i + l + 1; if (j > n) return 0; if (table[i] != -1) return table[i]; while (j < n) { ans = (ans + (ncr(j - i - 1, l) % p * (dp(a[j], j, a, n, table)) % p) % p) % p; j++; } ans = (ans + ncr(n - i - 1, l) % p) % p; table[i] = ans; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 1; i <= n; i++) { cache[i][0] = 1; cache[i][i] = 1; } vector<int> a(n, 0), table(n, -1); for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0; for (int i = 0; i < n; i++) ans = (ans % p + dp(a[i], i, a, n, table) % p) % p; cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { if (e == 0) return 1; if (e % 2 == 0) { T t = bigmod(p, e / 2, M); return (t * t) % M; } return (bigmod(p, e - 1, M) * p) % M; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } struct Z { int node, edge; long long w, c; Z() {} Z(int _, long long __, int ___, long long ____) { node = _; w = __; edge = ___; c = ____; } bool operator<(const Z& A) const { if (w == A.w) return c > A.c; return w > A.w; } }; bool vis[300009]; long long cost[300009]; long long edgecost[300009]; vector<Z> V[300009]; void DIJKSTRA(int st) { priority_queue<Z> PQ; PQ.push(Z(st, 0, 0, 0)); memset(vis, false, sizeof vis); while (!PQ.empty()) { Z top = PQ.top(); PQ.pop(); int cn = top.node; long long cc = top.w; int edge = top.edge; long long c = top.c; if (vis[cn]) continue; vis[cn] = 1; cost[cn] = cc; edgecost[edge] = c; int sz = V[cn].size(); for (int i = 0; i < sz; i++) { int adn = V[cn][i].node; long long adc = V[cn][i].w + cc; int ed = V[cn][i].edge; if (!vis[adn]) { PQ.push(Z(adn, adc, ed, V[cn][i].w)); } } } } int main() { int i, j, k; string str, inp; int m, t, n; int u, v; long long w; cin >> n >> m; for (i = 0; i < m; i++) { cin >> u >> v >> w; V[u].push_back(Z(v, w, i, -1)); V[v].push_back(Z(u, w, i, -1)); } int st; cin >> st; DIJKSTRA(st); long long res = 0ll; for (i = 0; i < m; i++) res += edgecost[i]; cout << res << endl; for (i = 0; i < m; i++) { if (edgecost[i]) cout << i + 1 << " "; } return 0; }
6
#include <bits/stdc++.h> using namespace std; vector<bool> used; double go(vector<vector<int>> &adj, int node, int l) { if (used[node]) return 0; used[node] = 1; int cnt = 0; for (int &i : adj[node]) cnt += (!used[i]); if (!cnt) return l; double res = 0; for (auto &i : adj[node]) res += (1.0) / cnt * go(adj, i, l + 1); return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; used.assign(n + 1, 0); vector<vector<int>> adj(n + 1); int t, f; for (int i = 0; i < n - 1; i++) cin >> f >> t, adj[f].push_back(t), adj[t].push_back(f); printf("%0.10f\n", go(adj, 1, 0)); return 0; }
3
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb(x) push_back(x) #define N (ll)1e6+9 //global array vector<int> seive(N,2); vector<int> garr(N,0); // bool check(ll n){ // ll s=1,e=10000,mid; // while(s<=e){ // mid = (s+e+1)/2; // if(mid*mid*mid == n)return true; // else if(mid*mid*mid < n)s=mid+1; // else e=mid-1; // } // return false; // } void solve(){ int n,k1,k2;cin>>n>>k1>>k2; int w,b;cin>>w>>b; int rem = 2*n - k1-k2; if(rem%2==1){ if(k1+k2-1 >= 2*w){ if(rem-1 >= 2*b){ cout<<"YES\n"; } else cout<<"NO\n"; } else cout<<"NO\n"; return; } if(k1+k2 >= 2*w){ if(rem >= 2*b){ cout<<"YES\n"; } else cout<<"NO\n"; } else cout<<"NO\n"; } int main(){ ios::sync_with_stdio(false); cin.tie(0); // seive algorithm // seive[0]=seive[1]=0; // for(int i=2;i<=sqrt(nm);i++){ // if(seive[i]){ // for(int j=i*i;j<=nm;j+=i){ // seive[j]=0; // } // } // } // ******for single test case****** // solve(); // ******for multiple test cases!****** ll tt;cin>>tt; while(tt--){ solve(); } return 0; } /* problem statement ---- https://codeforces.com/contest/1382/problem/B int n,a[n];cin>>n; bool allgreaterthan1=true; for(int i=0;i<n;i++){ cin>>a[i]; if(a[i]==1)allgreaterthan1=false; } if(allgreaterthan1){ cout<<"First\n";return; } string current="First"; for(int i=0;i<n;i++){ if(a[i]==1 && current=="First"){current="Second";} else if(a[i]==1 && current=="Second"){current="First";} } if(current=="First")cout<<"Second\n"; else if(current=="Second")cout<<"First\n"; */
0
#include <bits/stdc++.h> using namespace std; using LL = long long; using PII = pair<int, int>; int main() { int u, v, p; scanf("%d%d%d", &u, &v, &p); function<int(int)> rev = [&](int x) -> int { return x <= 1 ? 1 : 1LL * rev(p % x) * (p - p / x) % p; }; auto add = [&](int x) { return (x + 1) % p; }; auto sub = [&](int x) { return (x - 1 + p) % p; }; auto bfs = [&](int src, int tgt) { unordered_set<int> S[2]; unordered_map<int, PII> Pre[2]; queue<PII> Q; S[0].insert(src); S[1].insert(tgt); Q.emplace(src, 0); Q.emplace(tgt, 1); auto ins = [&](int x, int u, int t, int op) { if (S[t].count(x)) return false; if (S[t ^ 1].count(x)) { vector<PII> Path; Path.emplace_back(x, op); int tmp = u; while (tmp != src && tmp != tgt) { Path.push_back(Pre[t][tmp]); tmp = Pre[t][tmp].first; } reverse(Path.begin(), Path.end()); tmp = x; while (tmp != src && tmp != tgt) { Path.push_back(Pre[t ^ 1][tmp]); tmp = Pre[t ^ 1][tmp].first; } if (t == 1) reverse(Path.begin(), Path.end()); cout << Path.size() << endl; for (auto p : Path) printf("%d ", p.second); puts(""); return true; } S[t].insert(x); Q.emplace(x, t); Pre[t][x] = PII{u, op}; return false; }; while (!Q.empty()) { auto p = Q.front(); Q.pop(); int x = p.first; int t = p.second; if (ins(add(x), x, t, t == 0 ? 1 : 2)) return; if (ins(sub(x), x, t, t == 1 ? 1 : 2)) return; if (ins(rev(x), x, t, 3)) return; } }; bfs(u, v); return 0; }
9
#include <bits/stdc++.h> using namespace std; typedef struct Data { int id, val; } tri; typedef struct Data1 { int x, y; } point; void update(int BIT[], int n, int idx, int val) { if (idx == 0) { BIT[0] = 1; return; } while (idx <= n) { BIT[idx] += val; idx += (idx & -idx); } return; } int query(int BIT[], int n, int idx) { int ans = 0; while (idx > 0) { ans += BIT[idx]; idx -= (idx & -idx); } return ans + BIT[0]; } bool compare(tri a, tri b) { if (a.val < b.val) return true; if (a.val > b.val) return false; if (a.id < b.id) return true; return false; } long long solve(tri A[], int BIT[], int n, int val) { int j = 0, ans = 0; for (int i = 0; i <= n; i++) { while (A[j].val + val <= A[i].val && j <= n) { update(BIT, n, A[j].id + 1, 1); j++; } ans += query(BIT, n, A[i].id); } return (long long)ans; } int main() { ios_base::sync_with_stdio(0); int n, m, i, x, max = 0, min = 9999999, min1 = 9999999; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> x; max = std::max(max, x); min = std::min(min, x); } for (int i = 1; i <= m; i++) { cin >> x; min1 = std::min(min1, x); } int t1 = min * 2, t2 = max, t3 = min1; t1 = std::max(t1, t2); if (t1 >= t3) puts("-1"); else cout << t1 << endl; return 0; }
2
// Does this make the program faster? #if not LOCAL #define NDEBUG 1 #endif #include<bits/stdc++.h> struct Tree{ std::vector<int> data; std::vector<int> changedList; Tree(int size): data(size*2, INT_MIN), changedList() {} int offset()const{ return int(data.size()/2); } void fastReset(){ for(auto node: changedList) data[node]=INT_MIN; changedList.clear(); } void setMaximum(int left, int right, int value){ auto const process=[&](int node){ if(data[node]==INT_MIN) changedList.push_back(node); data[node]=std::max(data[node], value); }; for( left+=offset(), right+=offset(); left!=right; left>>=1, right>>=1){ if(left&1) process(left++); if(right&1) process(--right); } } int operator[](int index) const{ int result=INT_MIN; for(int node=index+offset(); node; node>>=1) result=std::max(result, data[node]); return result; } }; int main(){ std::ios::sync_with_stdio(0);std::cin.tie(0); int number; std::cin>>number; int resultAll{}; std::vector<Tree> data; // reuse the data structure to avoid memset time int const globalMaxValue=100000; for(int _=0; _<number; ++_){ int len; std::cin>>len; assert(len>=1); std::vector<int> value(len); for(auto& it: value) std::cin>>it; assert(std::is_sorted(begin(value), end(value))); assert(value.back()<=globalMaxValue); if(number==1){ assert(len!=0); std::cout<<"YES\n"; return 0; } for(auto& it: data) it.fastReset(); // if _3, the amortized complexity of this function is just existing+20*(number<=1000) //int const maxValue=value.back(); // data[g][a] = maximum b such that f(a, b)==g (and b in value), or INT_MIN // conjecture: there are only log(maxValue) distinct data values (_3) // update: _3 was wrong. (really?) // so result==447 with 1e5 values. // Let's special-case it. // grundy value of states reachable from the initial state. // Used to compute mex -> grundy value of the initial state. std::vector<char> initialMark(value.size()); // total complexity: 1e5 // f(a, b) | b in value = grundy value, last=a for(auto index=value.size(); index--;){ // total number of iterations: 1e5 int const b=value[index]; /* compute all f(*, b) values formula: f(a, b)=Mex f(b, c) |c>2b-a properties: f(a, b)<=f(a+1, b) a<=b to compute: * for all a, f(a, b)>=0 * find a0 = minimum a such that {f(b, c)} >= {0} <=> f(a, b)>=1 so a0=2*b-(maximum c0 such that {f(b, c) | c>c0} >= {0}) c0 = (maximum c0 such that {f(b, c) | c>c0} >= {0}) = (maximum c0 such that 0 in {f(b, c) | c>c0}) = (maximum c0 such that any(f(b, c)==0 | c>=c0+1)) = (maximum c0' such that any(f(b, c)==0 | c>=c0'))-1 = (maximum c0' such that f(b, c0')==0)-1 = data[0][b]-1 a0=2*b+1-data[0][b] find a1>=a0 = maximum a such that {f(b, c)} >= {0, 1} <=> f(a, b)>=2 a1=max(a0, 2*b+1-data[1][b]) similarly, a_i = max(a_(i-1), 2*b+1-data[i][b]) where a_(-1) = 0 (given that all data values (values in `value`) are >0) if data[][]==INT_MIN, then a_i==INT_MAX i.e. there's no f(a, b)==i+1 assert(a_i==INT_MAX or a_i<=b for all i); update the data structure f[0..a_0][b] = 0 -> data[0][0..a_0] max= b f[a_0..a_1][b] = 1 -> data[1][a_0..a_1] max= b ... similarly, data[j][a_(j-1)..a_i] max= b for all 0<=j<=i. special case: set a_i=min(a_i, b) (_2) */ for(int i=0, lasta=0;; ++i){ // if _3 then i<=20 // => each loop iteration does 20*log(globalMaxValue) ~= 400 steps at most // excluding _4 // => total: 1e5*400=4e7. int ai, tmp; if(i>=(int)data.size() or (tmp=data[i][b])==INT_MIN){ assert(lasta<=b+1); ai=b+1; // _2 }else{ // at the moment, data[i][b]>=b if it's not INT_MIN (it's only set at _1) // so b*2+1-data[i][b] <= b+1 ai=std::max(lasta, b*2+1-tmp ); assert(ai<=b+1); } assert((int)data.size()>=i); if(i==(int)data.size()) // _4 data.emplace_back(globalMaxValue+1); // total complexity: data.size()*globalMaxValue // <= 20*1e5 == 2e6 if _3 // -> 4e6 int in main, 4e6 for fastReset -> 8e6 int -> 32 MB #if 0 for(int j=lasta; j<ai; ++j) data[i][j]=std::max(data[i][j], b); #else assert(lasta<=ai); data[i].setMaximum(lasta, ai, b); // _1 #endif if(ai==b+1){ assert(lasta<ai); // a state with g==f[b][b] == i is reachable from the initial state if(i<(int)initialMark.size()) initialMark[i]=true; // this line is run at most value.size()==initialMark.size() times break; } lasta=ai; } } int result=int(std::find(begin(initialMark), end(initialMark), false)-initialMark.begin()); // grundy value of the initial state resultAll^=result; } std::cout<<(resultAll!=0 ? "YES\n": "NO\n"); }
13
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("-ffloat-store") using namespace std; const auto start_time = std::chrono::high_resolution_clock::now(); void aryanc403() {} const long long int INF = 0xFFFFFFFFFFFFFFFL; long long int seed; mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count()); inline long long int rnd(long long int l = 0, long long int r = INF) { return uniform_int_distribution<long long int>(l, r)(rng); } class CMP { public: bool operator()(pair<long long int, long long int> a, pair<long long int, long long int> b) { return !(a.first < b.first || (a.first == b.first && a.second <= b.second)); } }; void add(map<long long int, long long int> &m, long long int x, long long int cnt = 1) { auto jt = m.find(x); if (jt == m.end()) m.insert({x, cnt}); else jt->second += cnt; } void del(map<long long int, long long int> &m, long long int x, long long int cnt = 1) { auto jt = m.find(x); if (jt->second <= cnt) m.erase(jt); else jt->second -= cnt; } bool cmp(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return a.first < b.first || (a.first == b.first && a.second < b.second); } const long long int mod = 1000000007L; long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y; long long int m; string s; vector<vector<long long int> > e; vector<pair<long long int, long long int> > edges; unsigned long long int ans, a, b, c; unsigned long long int C2(long long int n) { if (n <= 0) return 0; return n * (n + 1) / 2; } unsigned long long int solve1() { unsigned long long int ans = 0; for (auto x : edges) { const long long int u = x.first, v = x.second; ans += (a * u + b * v) * (n - 1 - v); ans += c * (C2(n - 1) - C2(v)); ans += (b * u + c * v) * (u); ans += a * C2(u - 1); ans += (a * u + c * v) * (v - u - 1); ans += b * (C2(v - 1) - C2(u)); }; return ans; } unsigned long long int solveMid(unsigned long long int cst, const vector<long long int> &sm, const vector<long long int> &lr) { unsigned long long int ans = 0; ans += cst * (((long long int)(sm).size())) * (((long long int)(lr).size())); for (auto x : sm) ans += a * x * ((long long int)(lr).size()); for (auto x : lr) ans += c * x * ((long long int)(sm).size()); return ans; } unsigned long long int solveCorner(unsigned long long int cst, unsigned long long int a, unsigned long long int b, const vector<long long int> &e) { unsigned long long int ans = 0; unsigned long long int n = ((long long int)(e).size()), m = 0; ans += cst * ((n) * (n - 1) / 2); for (auto x : e) { n--; ans += (x * a) * n; ans += (x * b) * m; m++; } return ans; } unsigned long long int solve2() { unsigned long long int ans = 0; for (long long int i = 0; i < n; ++i) { vector<long long int> dd, ee; for (auto x : e[i]) { if (i < x) ee.push_back(x); else dd.push_back(x); } ans += solveMid(b * i, dd, ee); reverse(begin(ee), end(ee)); ans += solveCorner(a * i, c, b, ee); ans += solveCorner(c * i, a, b, dd); }; return ans; } bool chk(const vector<long long int> &a, long long int x) { auto it = lower_bound(begin(a), end(a), x); return it != a.end() && *it == x; } const long long int MM = 200000; const long long int KK = 40; bitset<MM> edgHeavy[1 + 2 * MM / KK]; unsigned long long int solve3() { unsigned long long int ans = 0; vector<long long int> bb; vector<pair<long long int, long long int> > qur; for (long long int i = 0; i < n; ++i) { if (((long long int)(e[i]).size()) > KK) { bb.push_back(i); continue; } for (auto x : e[i]) { if (x > i) break; for (auto y : e[i]) { if (y < i) continue; if (chk(e[x], y)) ans += x * a + i * b + c * y; } } } const long long int mm = ((long long int)(bb).size()); assert(mm < 2 * MM / KK + 1); for (long long int i = 0; i < mm; ++i) { for (auto x : e[bb[i]]) edgHeavy[i][x] = 1; } for (auto x : edges) { const long long int u = x.first, v = x.second; auto it = lower_bound(begin(bb), end(bb), u + 1) - bb.begin(); while (it < mm && bb[it] < v) { if (edgHeavy[it][u] && edgHeavy[it][v]) ans += a * u + bb[it] * b + c * v; ++it; } }; return ans; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); { cin >> n >> m; cin >> a >> b >> c; e.resize(n); for (i = 0; i < (m); ++i) { cin >> u >> v; e[u].push_back(v); e[v].push_back(u); if (u > v) swap(u, v); edges.push_back({u, v}); } for (i = 0; i < (n); ++i) { sort(begin(e[i]), end(e[i])); ans += (c * i) * (i * (i - 1) / 2); ans += (b * i) * i * (n - i - 1); ans += (a * i) * ((n - i - 2) * (n - i - 1) / 2); }; ; unsigned long long int ans1 = solve1(); unsigned long long int ans2 = solve2(); unsigned long long int ans3 = solve3(); ; ans += -ans1 + ans2 - ans3; ; ; cout << ans << "\n"; } aryanc403(); return 0; }
9
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, r; cin >> n >> r; vector<int> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; vector<int> inc, dec; for (int i = 0; i < n; i++) { if (b[i] >= 0) { inc.push_back(i); } else { dec.push_back(i); } } sort(begin(inc), end(inc), [&](int i, int j) { return a[i] < a[j]; }); int ans = 0; for (auto i : inc) { if (r >= a[i]) { r += b[i]; ans++; } } sort(begin(dec), end(dec), [&](int i, int j) { return a[i] + b[i] > a[j] + b[j]; }); vector<vector<int>> dp(int((dec).size()) + 1, vector<int>(r + 1, -1)); function<int(int, int)> solve = [&](int i, int j) { if (i == int((dec).size())) return 0; if (dp[i][j] != -1) return dp[i][j]; int id = dec[i]; dp[i][j] = solve(i + 1, j); if (j >= max(a[id], -b[id])) dp[i][j] = max(dp[i][j], solve(i + 1, j + b[id]) + 1); return dp[i][j]; }; ans += solve(0, r); cout << ans << endl; }
6
#include <bits/stdc++.h> using namespace std; const int N = 100100; int n; int nums[N]; int l[N]; int r[N]; int mini[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> nums[i]; } int cur = 0; for (int i = n - 1; i >= 0; i--) { r[i] = cur; cur = max(cur - 1, nums[i] - 1); } cur = 0; for (int i = 0; i < n; i++) { l[i] = cur; cur = max(cur, nums[i] + 1); } for (int i = 0; i < n; i++) mini[i] = max(l[i], max(r[i], nums[i])); long long int ans = 0; for (int i = 0; i < n; i++) { if (i == n - 1 || mini[i + 1] <= mini[i]) { ans += max(0, mini[i] - nums[i] - 1); } else { ans += max(0, mini[i] - nums[i]); } } cout << ans << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 100100; const int MOD = (int)1e9 + 7; const int INF = (int)1e9; const int C = 2e4 + 10; struct Array { long long a[MAXN]; Array() { fill(a, a + MAXN, 0); } long long& operator[](const int& i) { return a[i + C]; } }; Array cnt; Array mask_a, mask_b; int a[MAXN]; int b[MAXN]; int bp[MAXN]; int get__builtin_popcount(long long x) { int ans = 0; for (int i = 0; i < 4; ++i) { ans += bp[x & 65535]; x >>= 16; } return ans; } void solve() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int j = 0; j < m; ++j) scanf("%d", &b[j]); vector<int> v; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { int x = a[i] + b[j]; v.push_back(x); cnt[x] += 2; mask_a[x] |= (1LL << i); mask_b[x] |= (1LL << j); } sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); int res = 0; for (int i = 0; i < (int)v.size(); ++i) { int x = v[i]; res = max(res, get__builtin_popcount(mask_a[x]) + get__builtin_popcount(mask_b[x])); for (int j = i + 1; j < (int)v.size(); ++j) { int y = v[j]; res = max(res, get__builtin_popcount(mask_a[x] | mask_a[y]) + get__builtin_popcount(mask_b[x] | mask_b[y])); } } printf("%d\n", res); } int main() { for (int i = 0; i < 65536; ++i) { bp[i] = __builtin_popcount(i); } solve(); return 0; }
6
#include <bits/stdc++.h> using namespace std; queue<int> q1, q2, q3, q4, q5, q6; int main() { int n, x, ans = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &x); if (x == 4) q1.push(i); else if (x == 8) q2.push(i); else if (x == 15) q3.push(i); else if (x == 16) q4.push(i); else if (x == 23) q5.push(i); else q6.push(i); } if (q1.empty() || q2.empty() || q3.empty() || q4.empty() || q5.empty() || q6.empty()) { printf("%d\n", n - ans); return 0; } while (1) { if (q2.front() > q1.front()) { if (q3.front() > q2.front()) { if (q4.front() > q3.front()) { if (q5.front() > q4.front()) { if (q6.front() > q5.front()) { ans += 6; q1.pop(); q2.pop(); q3.pop(); q4.pop(); q5.pop(); q6.pop(); } else q6.pop(); } else q5.pop(); } else q4.pop(); } else q3.pop(); } else q2.pop(); if (q1.empty() || q2.empty() || q3.empty() || q4.empty() || q5.empty() || q6.empty()) break; } printf("%d\n", n - ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { vector<pair<int, int> > v; vector<int>::iterator it; int n, p = -1; cin >> n; int a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; for (int i = 0; i < n; i++) v.push_back(make_pair(a[i], b[i])); sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { if (p <= v[i].second) { p = v[i].second; continue; } else p = v[i].first; } cout << p; }
3
#include <bits/stdc++.h> using namespace std; const long long N = 200000 + 10; const long long MOD = 1000000007; const long long LOG = 20; const long long INF = 1000000010; const long long delta = 11353; struct node { long long mn, mx, ans; }; node seg[N << 2]; set<long long> row[N * 2]; long long n, m, q, a[N * 2]; map<long long, long long> mp[N * 2]; node merge(node x, node y) { node res = x; res.ans = min(res.ans, y.ans); res.ans = min(res.ans, x.mn - y.mx); res.mn = min(res.mn, y.mn); res.mx = max(res.mx, y.mx); return res; } void build(long long id, long long l, long long r) { if (r - l == 1) { seg[id].mn = a[l * 2 - 1]; seg[id].mx = a[l * 2]; seg[id].ans = seg[id].mn - seg[id].mx; return; } long long md = (l + r) >> 1; build(id << 1, l, md); build(id << 1 | 1, md, r); seg[id] = merge(seg[id << 1], seg[id << 1 | 1]); } void Setmn(long long id, long long lq, long long rq, long long x, long long l, long long r) { if (rq <= l || r <= lq) return; if (lq <= l && r <= rq) { seg[id].mn = x; seg[id].ans = seg[id].mn - seg[id].mx; return; } long long md = (l + r) >> 1; Setmn(id << 1, lq, rq, x, l, md); Setmn(id << 1 | 1, lq, rq, x, md, r); seg[id] = merge(seg[id << 1], seg[id << 1 | 1]); } void Setmx(long long id, long long lq, long long rq, long long x, long long l, long long r) { if (rq <= l || r <= lq) return; if (lq <= l && r <= rq) { seg[id].mx = x; seg[id].ans = seg[id].mn - seg[id].mx; return; } long long md = (l + r) >> 1; Setmx(id << 1, lq, rq, x, l, md); Setmx(id << 1 | 1, lq, rq, x, md, r); seg[id] = merge(seg[id << 1], seg[id << 1 | 1]); } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> q; for (long long i = 1; i <= 2 * n; i++) { if (i % 2 == 0) a[i] = -INF, row[i].insert(-INF); else row[i].insert(INF), a[i] = INF; } build(1, 1, n + 1); for (long long i = 1; i <= q; i++) { long long x, y; cin >> x >> y; if (x % 2 == 0) { if (mp[x][y] == 0) { mp[x][y] = 1; row[x].insert(y); if (a[x] < y) { a[x] = y; Setmx(1, (x / 2), (x / 2) + 1, y / 2, 1, n + 1); } } else { mp[x][y] = 0; row[x].erase(y); if (a[x] == y) { a[x] = *row[x].rbegin(); Setmx(1, x / 2, x / 2 + 1, a[x] / 2, 1, n + 1); } } } else { if (mp[x][y] == 0) { mp[x][y] = 1; row[x].insert(y); if (a[x] > y) { a[x] = y; Setmn(1, (x + 1) / 2, (x + 1) / 2 + 1, (y + 1) / 2, 1, n + 1); } } else { mp[x][y] = 0; row[x].erase(y); if (a[x] == y) { a[x] = *row[x].begin(); Setmn(1, (x + 1) / 2, (x + 1) / 2 + 1, (a[x] + 1) / 2, 1, n + 1); } } } cout << (seg[1].ans <= 0 ? "NO\n" : "YES\n"); } return 0; }
10
#include <bits/stdc++.h> int main() { char arr[1005]; scanf("%s", arr); int x = strlen(arr); int spasi = 0; for (int i = 1; i < x; i += 3) { if (arr[i] != '/') { for (int i = 0; i < spasi; i++) { printf(" "); } spasi += 2; printf("<%c>\n", arr[i]); } else { spasi -= 2; for (int i = 0; i < spasi; i++) { printf(" "); } printf("</%c>\n", arr[i + 1]); i++; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m, k, c; int v[22][22], ans; void get(int a, int b, int c, int d) { int j = 0; for (int i = a; i <= c; i++) for (int h = b; h <= d; h++) j += v[i][h]; if (j >= k) ans++; } void tap(int a, int b) { for (int i = a; i <= n; i++) for (int h = b; h <= m; h++) get(a, b, i, h); } int main() { cin >> n >> m >> c >> k; while (c--) { int a, b; cin >> a >> b; v[a][b] = 1; } for (int i = 1; i <= n; i++) for (int h = 1; h <= m; h++) tap(i, h); cout << ans; }
1
#include <bits/stdc++.h> using namespace std; int A[1000]; void quicksort(int l, int r) { int b = l; if (r - l < 1) { return; } for (int i = l; i < r; ++i) { if (A[i] < A[r]) { swap(A[i], A[b]); b = b + 1; } } swap(A[r], A[b]); quicksort(l, b - 1); quicksort(b + 1, r); } int main() { int r; cin >> r; for (int i = 0; i < r; ++i) { cin >> A[i]; } quicksort(0, r - 1); if (r % 2 > 0) { cout << A[(r / 2)]; } else { cout << min(A[r / 2], A[r / 2 - 1]); } return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> string to_str(T str) { stringstream stream; stream << str; return stream.str(); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, s; cin >> n >> s; vector<int> v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } int sum = 0; sort(v.begin(), v.end()); for (int i = 0; i < n - 1; i++) { sum += v[i]; } if (sum <= s) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
0
#include <bits/stdc++.h> const int N = 52 + 5; int n; std::string str[N]; int dp[N][N][N][N]; bool able(int i, int j) { return str[i][0] == str[j][0] || str[i][1] == str[j][1]; } bool dfs(int n, int i, int j, int k) { int& ret = dp[n][i][j][k]; if (ret != -1) { return ret; } else if (n == 3) { return ret = (able(j, k) && able(k, i)); } ret = 0; if (able(j, k)) { if (dfs(n - 1, n - 4, i, k)) { return ret = 1; } } if (able(n - 4, k)) { if (dfs(n - 1, k, i, j)) { return ret = 1; } } return ret = 0; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { std::cin >> str[i]; } if (n == 1) { puts("YES"); return 0; } else if (n == 2) { puts(able(0, 1) ? "YES" : "NO"); return 0; } else { memset(dp, -1, sizeof(dp)); puts(dfs(n, n - 3, n - 2, n - 1) ? "YES" : "NO"); return 0; } return 0; }
5
#include <bits/stdc++.h> using namespace std; bool mark[100005]; bool jark[100005]; vector<long long> adj[100005]; vector<long long> bdj[100005]; long long counter; long long cnt = 0; vector<string> forsubs; long long b[100005]; long long a; long long ans = 0; long long n; queue<long long> q; long long train; long long bus; void addedge(long long u, long long v) { adj[u].push_back(v); adj[v].push_back(u); } void bedge(long long u, long long v) { bdj[u].push_back(v); bdj[v].push_back(u); } void bfs() { long long i = 1; q.push(i); mark[i] = 1; long long a = b[i]; while (q.size()) { long long x = q.front(); q.pop(); for (auto j : adj[x]) { long long parent = x; if (!mark[j]) { q.push(j); mark[j] = 1; b[j] = b[parent] + 1; if (j == n) train = b[j]; } } } } void gfs() { long long i = 1; q.push(i); jark[i] = 1; long long a = b[i]; while (q.size()) { long long x = q.front(); q.pop(); for (auto j : bdj[x]) { long long parent = x; if (!jark[j]) { q.push(j); jark[j] = 1; b[j] = b[parent] + 1; if (j == n) bus = b[j]; } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long m; cin >> n >> m; long long u, v; for (long long i = 1; i <= m; i++) { cin >> u >> v; addedge(v, u); } for (long long i = 1; i <= n - 1; i++) { for (long long j = i + 1; j <= n; j++) { cnt = 0; for (auto k : adj[i]) { if (j == k) cnt++; } if (cnt) continue; else bedge(i, j); } } b[1] = 0; bfs(); gfs(); if (train == 0 || bus == 0) cout << "-1" << endl; else cout << max(train, bus) << endl; }
4