solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; sort(s.begin(), s.end()); int cnt = s.end() - lower_bound(s.begin(), s.end(), s.back()); while (cnt--) cout << s.back(); cout << '\n'; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m, i, k, p; string c; int main() { cin >> c; n = c.size(); while (c[i] == '0') m++; p = 0; for (i = m; i < n - 1; i++) { if (c[i] != '0') cout << c[i]; else if (p == 1) cout << c[i]; else p = 1; } if (p == 1) cout << c[n - 1]; }
3
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; int a[n + 1]; for (int i = 1; i <= n; i++) a[i] = 1; string s; cin >> s; char ch[n]; for (int i = 1; i < n; i++) ch[i] = s[i - 1]; for (int i = 1; i < n; i++) { char c = ch[i]; if (c == 'L') { if (a[i] <= a[i + 1]) a[i] = a[i + 1] + 1; } else if (c == 'R') { if (a[i] >= a[i + 1]) a[i + 1] = a[i] + 1; } else { if (a[i] != a[i + 1]) a[i + 1] = a[i]; } } for (int i = n - 1; i >= 1; i--) { char c = ch[i]; if (c == 'L') { if (a[i] <= a[i + 1]) a[i] = a[i + 1] + 1; } else if (c == 'R') { if (a[i] >= a[i + 1]) a[i + 1] = a[i] + 1; } else { if (a[i + 1] != a[i]) a[i] = a[i + 1]; } } for (int i = 1; i <= n; i++) cout << a[i] << " "; return 0; }
10
#include <bits/stdc++.h> using namespace std; template <class T> void dbs(string str, T t) { cerr << str << " : " << t << "\n"; } template <class T, class... S> void dbs(string str, T t, S... s) { int idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...); } template <class S, class T> ostream& operator<<(ostream& os, const pair<S, T>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class T> ostream& operator<<(ostream& os, const set<T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class S, class T> ostream& operator<<(ostream& os, const map<S, T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class T> void prc(T a, T b) { cerr << "["; for (T i = a; i != b; ++i) { if (i != a) cerr << ", "; cerr << *i; } cerr << "]\n"; } struct trie { int nnodes; vector<vector<int> > T, S; trie() { nnodes = 1; T.push_back(vector<int>(2, -1)); S.push_back(vector<int>(2, 1000000007)); } void insert(int x) { int node = 0; for (int i = 19; i >= 0; i--) { int bit = (x >> i) & 1; if (T[node][bit] == -1) { T[node][bit] = nnodes++; T.push_back(vector<int>(2, -1)); S.push_back(vector<int>(2, 1000000007)); } S[node][bit] = min(S[node][bit], x); node = T[node][bit]; } } int query(int x, int s) { int node = 0; int ret = 0; for (int i = 19; i >= 0; i--) { int bit = (x >> i) & 1; bit = 1 - bit; if (T[node][bit] != -1 && S[node][bit] <= s) { ret += (1 << i); node = T[node][bit]; } else if (T[node][1 - bit] != -1 && S[node][1 - bit] <= s) { node = T[node][1 - bit]; } else return -1; } return ret; } }; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int q; cin >> q; int lim = sqrt(100000); trie tr[lim + 1]; vector<bool> mark(100009); while (q--) { int ch; cin >> ch; if (ch == 1) { int x; cin >> x; for (long long int i = (long long int)(1); i <= (long long int)(lim); i++) if (x % i == 0) tr[i].insert(x); mark[x] = 1; } else { int x, k, s; cin >> x >> k >> s; s -= x; if (x % k != 0) { cout << -1 << "\n"; continue; } if (k > lim) { int ans = -1; int maxx = -1; for (int i = k; i <= s; i += k) if (mark[i]) { if ((i ^ x) > maxx) { maxx = i ^ x; ans = i; } } cout << ans << "\n"; } else { int ans = tr[k].query(x, s); if (ans == -1) cout << ans << "\n"; else cout << (ans ^ x) << "\n"; } } } }
14
#include <bits/stdc++.h> using namespace std; int main() { int a, b, x1, y1, x2, y2; while (cin >> a >> b >> x1 >> y1 >> x2 >> y2) { tie(x1, y1) = make_pair(x1 + y1, y1 - x1); tie(x2, y2) = make_pair(x2 + y2, y2 - x2); a *= 2; b *= 2; x1 = x1 / a + (x1 > 0); y1 = y1 / b + (y1 > 0); x2 = x2 / a + (x2 > 0); y2 = y2 / b + (y2 > 0); cout << max(abs(y2 - y1), abs(x2 - x1)) << "\n"; } }
10
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); #pragma GCC diagnostic ignored "-Wsign-compare" vector<long long> b; vector<long long> w; vector<vector<long long> > adj; long long n, m; vector<vector<pair<long long, long long> > > dp; pair<long long, long long> mx(pair<long long, long long> a, pair<long long, long long> b) { if (a.first > b.first) return a; if (b.first > a.first) return b; if (a.second > b.second) return a; return b; } void comb(vector<pair<long long, long long> >& a, vector<pair<long long, long long> >& b) { vector<pair<long long, long long> > ret( (long long)(a).size() + (long long)(b).size(), make_pair(-1, 0)); for (long long i = 0; i < (long long)(a).size(); i++) { for (long long j = 0; j < (long long)(b).size(); j++) { ret[i + j] = mx(ret[i + j], make_pair(a[i].first + b[j].first, a[i].second + b[j].second)); ret[i + j + 1] = mx(ret[i + j + 1], make_pair(a[i].first + b[j].first + (long long)(b[j].second > 0), a[i].second)); } } a = ret; } void dfs(long long u, long long v) { 42; dp[u].push_back(make_pair(0, w[u] - b[u])); for (auto& first : adj[u]) { if (first == v) continue; dfs(first, u); comb(dp[u], dp[first]); } } void init() { b.clear(); w.clear(); adj.clear(); dp.clear(); b.resize(n); w.resize(n); adj.resize(n); dp.resize(n); } void pr(vector<vector<pair<long long, long long> > >& a) { 42; for (long long i = 0; i < (long long)(a).size(); i++) { 42; } } void pr(vector<vector<long long> >& a) { 42; for (long long i = 0; i < (long long)(a).size(); i++) { 42; } } void solve() { cin >> n >> m; init(); for (long long i = 0; i < n; i++) { cin >> b[i]; } for (long long i = 0; i < n; i++) { cin >> w[i]; } 42; 42; for (long long i = 0; i < n - 1; i++) { long long u, v; cin >> u >> v; u--, v--; adj[u].push_back(v); adj[v].push_back(u); } pr(adj); dfs(0, -1); cout << dp[0][m - 1].first + (long long)(dp[0][m - 1].second > 0) << '\n'; pr(dp); } int32_t main() { cin.sync_with_stdio(0); cin.tie(0); long long t; cin >> t; while (t--) { solve(); } return 0; }
17
#include <bits/stdc++.h> using namespace std; const int MAXN = 105; char str[MAXN]; inline int getTime(int h, int m, char ch) { if (h == 12) h = 0; int tmp = (ch == 'a') ? 0 : 12; return (h + tmp) * 60 + m; } int main() { int n, h, m, ch; while (cin >> n) { getchar(); int days = 0; int pre = -1, cnt = 0; for (int i = 0; i < n; i++) { gets(str); sscanf(str, "[%d:%d %c", &h, &m, &ch); int time = getTime(h, m, ch); if (pre == -1) { days++; pre = time; } else { if (time == pre) { if (++cnt > 10) days++, cnt = 1; } else { if (time < pre) days++; cnt = 1; pre = time; } } } cout << days << endl; } }
11
#include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0; cin >> n; string s; cin >> s; for (int i = 0; i < s.size(); i++) { int c = 0; int z = i; while (z + 1 < s.size() && s[z] == s[z + 1]) { z++; } c = z - i; c++; if (c % 2) { for (int j = 0; j < c; j++) { if (j % 2) { if (s[i + j] == 'R') s[i + j] = 'G'; else if (s[i + j] == 'G') s[i + j] = 'R'; else if (s[i + j] == 'B') s[i + j] = 'G'; ans++; } } } else { for (int j = 0; j < c; j++) { if (j % 2 == 0) { if (i + j - 1 >= 0 && s[i + j] == 'R' && s[i + j - 1] == 'G') { s[i + j] = 'B'; } else if (i + j - 1 >= 0 && s[i + j] == 'R' && s[i + j - 1] == 'B') { s[i + j] = 'G'; } else if (i + j - 1 >= 0 && s[i + j] == 'R' && s[i + j - 1] == 'R') { s[i + j] = 'G'; } else if (i + j - 1 >= 0 && s[i + j] == 'G' && s[i + j - 1] == 'R') s[i + j] = 'B'; else if (i + j - 1 >= 0 && s[i + j] == 'G' && s[i + j - 1] == 'B') s[i + j] = 'R'; else if (i + j - 1 >= 0 && s[i + j] == 'G' && s[i + j - 1] == 'G') s[i + j] = 'R'; else if (i + j - 1 >= 0 && s[i + j] == 'B' && s[i + j - 1] == 'R') s[i + j] = 'G'; else if (i + j - 1 >= 0 && s[i + j] == 'B' && s[i + j - 1] == 'G') s[i + j] = 'R'; else if (i + j - 1 >= 0 && s[i + j] == 'B' && s[i + j - 1] == 'B') s[i + j] = 'R'; else if (i + j - 1 < 0 && s[i + j] == 'R') s[i + j] = 'G'; else if (i + j - 1 < 0 && s[i + j] == 'G') s[i + j] = 'R'; else if (i + j - 1 < 0 && s[i + j] == 'B') s[i + j] = 'G'; ans++; } } } i += c - 1; } cout << ans << endl; cout << s; return 0; }
6
#include <bits/stdc++.h> const unsigned int M = 1000000007; using namespace std; bool check_prime(int n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, a, b, t = 1, f = 0, j = 0; std::vector<int> v; while (t--) { cin >> n; int i; for (i = 2; i <= n; i++, f = 0) { f = check_prime(i); j = i; while (!f && j <= n) v.push_back(j), j *= i; } cout << v.size() << endl; for (int j = 0; j < v.size(); j++) cout << v[j] << " "; } return 0; }
7
#include <bits/stdc++.h> using namespace std; int val[200005]; int nxt[200005]; vector<pair<int, int> > vec; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, big = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> val[i]; nxt[i] = (i + 1); big = max(big, val[i]); vec.push_back(make_pair(val[i], i)); } sort(vec.begin(), vec.end()); for (int i = 0; i < vec.size(); i++) { pair<int, int> temp = vec[i]; int tval = temp.first; int tidx = temp.second; int trav = nxt[tidx]; if (val[tidx] == -1 || tval == big) continue; val[tidx] = -1; while ((val[trav] == -1 || val[trav] == tval) && trav <= n) { val[trav] = -1; trav = nxt[trav]; } int cnt = trav - tidx; if (cnt % 2 == 0) { nxt[tidx] = trav; val[tidx] = -1; } else { cout << "NO\n"; exit(0); } } cout << "YES\n"; return 0; }
14
#include <bits/stdc++.h> using namespace std; int a[2000]; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; ans += min(a[x - 1], a[y - 1]); } cout << ans; }
6
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 1; const long long inf = 1e18 + 7; long long t, n, a[maxn], i, j; long long getnum(long long m) { long long cnt = 0; for (i = n; i > 0; i--) if (a[i] > m) cnt++; return cnt; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); long long l, m, h; l = 0; h = n - 2; while (l <= h) { m = (l + h) / 2; if (getnum(m) >= 2) l = m + 1; else h = m - 1; } cout << h << "\n"; } }
1
#include <bits/stdc++.h> using namespace std; int a[5][2000]; int pos[5][2000]; int dp[2000]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 0; i <= k - 1; ++i) for (int j = 0; j <= n - 1; ++j) cin >> a[i][j], a[i][j]--, pos[i][a[i][j]] = j; dp[0] = 1; for (int i = 1; i <= n - 1; ++i) { dp[i] = 1; for (int j = 0; j <= i - 1; ++j) { bool can = 1; for (int l = 1; l <= k - 1; ++l) can &= (pos[l][a[0][i]] > pos[l][a[0][j]]); if (can) dp[i] = max(dp[i], dp[j] + 1); } } for (int i = 0; i <= n - 1; ++i) dp[0] = max(dp[0], dp[i]); cout << dp[0]; return 0; }
11
#include <bits/stdc++.h> using namespace std; int N, P; signed long long dp[51][51][51][51]; signed long long p2[51]; const int CN = 101; signed long long C[CN][CN]; signed long long CS[CN][2]; signed long long mo = 1000000007; int col[51]; void solve() { int i, j, k, l, r, x, y; string s; for (i = 0; i < (CN); i++) for (j = 0; j <= i; j++) { C[i][j] = (j == 0 || j == i) ? 1 : (C[i - 1][j - 1] + C[i - 1][j]) % mo; (CS[i][j % 2] += C[i][j]) %= mo; } p2[0] = 1; for (i = 0; i < (50); i++) p2[i + 1] = p2[i] * 2 % mo; cin >> N >> P; for (i = 0; i < (N); i++) cin >> col[i]; dp[0][0][0][0] = 1; signed long long ret = 0; for (int b0 = 0; b0 <= N; b0++) for (int b1 = 0; b1 <= N; b1++) for (int w0 = 0; w0 <= N; w0++) for (int w1 = 0; b0 + b1 + w0 + w1 <= N; w1++) { signed long long d = dp[b0][b1][w0][w1]; int done = b0 + b1 + w0 + w1; if (col[done] == 0 || col[done] == -1) { signed long long pat = p2[b0 + b1 + w1]; (dp[b0 + 1][b1][w0][w1] += d * pat % mo * CS[w0][0] % mo) %= mo; (dp[b0][b1 + 1][w0][w1] += d * pat % mo * CS[w0][1] % mo) %= mo; } if (col[done] == 1 || col[done] == -1) { signed long long pat = p2[b1 + w0 + w1]; (dp[b0][b1][w0 + 1][w1] += d * pat % mo * CS[b0][0] % mo) %= mo; (dp[b0][b1][w0][w1 + 1] += d * pat % mo * CS[b0][1] % mo) %= mo; } if (b0 + b1 + w0 + w1 == N && (b0 + w0) % 2 == P) (ret += dp[b0][b1][w0][w1]) %= mo; } cout << ret % mo << endl; } int main(int argc, char** argv) { string s; int i; if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n'; for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); cout.tie(0); solve(); return 0; }
16
#include <bits/stdc++.h> using namespace std; int n, m; int sum[101][30030]; char g[101][30030]; int main() { int i, j, ok = true; cin >> n >> m; for (i = 0; i < n; i++) { bool f = false; scanf("%s", g[i]); for (j = 0; j < 3 * m; j++) { if (j == 0) sum[i][j + 1] = (g[i][j] == '1'); else sum[i][j + 1] = sum[i][j] + (g[i][j % m] == '1'); if (sum[i][j + 1]) f = true; } if (!f) ok = false; } if (!ok) { puts("-1"); return 0; } int res = 1000000000; for (i = m + 1; i <= 2 * m; i++) { int cnt = 0; for (j = 0; j < n; j++) { int l = 1, r = i, mid, p1, p2; while (l <= r) { mid = l + r >> 1; if (sum[j][i] - sum[j][mid - 1]) { p1 = mid; l = mid + 1; } else r = mid - 1; } l = i, r = 3 * m; while (l <= r) { mid = l + r >> 1; if (sum[j][mid] - sum[j][i - 1]) { p2 = mid; r = mid - 1; } else l = mid + 1; } cnt += min(i - p1, p2 - i); } res = min(res, cnt); } cout << res << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int mequal[53][53], in[53] = {0}; int visit[53][53][53][53], dp[53][53][53][53] = {{{{0}}}}; char card[53][3]; int mequall(int a, int b) { return card[a][0] == card[b][0] || card[a][1] == card[b][1]; } int check(int n, int a, int b, int c) { if (n == 1) return 2; else if (n == 2) return mequal[c][b] + 1; else return (mequal[c][b] && mequal[c][a]) + 1; } int dfs(int n, int a, int b, int c) { if (visit[n][a][b][c] != 0) return dp[n][a][b][c]; visit[n][a][b][c] = 1; if (n == 0) dp[n][a][b][c] = check(3, a, b, c); else { if (mequal[c][b]) dp[n][a][b][c] = dfs(n - 1, in[n], a, c); if (dp[n][a][b][c] != 2 && mequal[n][c]) dp[n][a][b][c] = dfs(n - 1, c, a, b); } return dp[n][a][b][c]; } int main() { int n; map<string, int> ma; memset(visit, 0, sizeof(visit)); scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%s", card[i]), ma[card[i]] = i; for (int i = 1; i <= n; ++i) in[i] = ma[card[i]]; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) mequal[i][j] = mequall(i, j); if (n <= 3) printf("%s", check(n, n - 2, n - 1, n) == 2 ? "YES" : "NO"); else printf("%s", dfs(n - 3, in[n - 2], in[n - 1], in[n]) == 2 ? "YES" : "NO"); return 0; }
11
#include <bits/stdc++.h> using namespace std; long double n, p, a[100001], b[100001], sum; bool Ver(long double time) { long double x = 0; for (int i = 1; i <= n; i++) { x += max((long double)0, time * a[i] - b[i]); } return p * time >= x; } int main() { cin >> n >> p; for (int i = 1; i <= n; i++) cin >> a[i] >> b[i]; for (int i = 1; i <= n; i++) sum += a[i]; if (sum <= p) { cout << "-1"; return 0; } long double li, ls, mid; li = 0; ls = 1000000000000000000; for (int i = 1; i <= 300; i++) { mid = (li + ls) / 2; if (Ver(mid)) li = mid; else ls = mid; } cout << fixed << setprecision(10) << mid; }
10
#include <bits/stdc++.h> using namespace std; struct node { int l; int r; int len; }; bool operator<(node a, node b) { if (b.len > a.len) return true; else if (b.len < a.len) return false; else { return b.l < a.l; } } int main() { int t; cin >> t; while (t--) { int n; cin >> n; priority_queue<node> q; int ans[n]; node t1, t2, t3; t1.l = 0; t1.r = n - 1; t1.len = n; q.push(t1); int ct = 0; while (!q.empty()) { t2 = q.top(); q.pop(); ans[(t2.l + t2.r) / 2] = ct + 1; ct++; if (t2.l == t2.r) { continue; } if ((t2.r - t2.l + 1) % 2 == 0) { if (((t2.r + t2.l) / 2 + 1) <= t2.r) { t3.l = (t2.r + t2.l) / 2 + 1; t3.r = t2.r; t3.len = t3.r - t3.l + 1; q.push(t3); } if (((t2.l + t2.r) / 2 - 1) >= t2.l) { t3.l = t2.l; t3.r = (t2.l + t2.r) / 2 - 1; t3.len = t3.r - t3.l + 1; q.push(t3); } } else { if (((t2.l + t2.r) / 2 - 1) >= t2.l) { t3.l = t2.l; t3.r = (t2.l + t2.r) / 2 - 1; t3.len = t3.r - t3.l + 1; q.push(t3); } if (((t2.r + t2.l) / 2 + 1) <= t2.r) { t3.l = (t2.r + t2.l) / 2 + 1; t3.r = t2.r; t3.len = t3.r - t3.l + 1; q.push(t3); } } } for (int i = 0; i < n; ++i) { cout << ans[i] << " "; } cout << endl; } }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m; map<string, int> pol; map<string, int> en; cin >> n >> m; int whole = 0; string j; for (int i = (0); i < (n); i++) { cin >> j; pol[j] = 1; } for (int i = (0); i < (m); i++) { cin >> j; en[j] = 1; if (pol[j] == 1) whole++; } int p = 0, em = 0; if (whole % 2 == 0) { p += whole / 2; em += whole / 2; } else { whole--; p += whole / 2; p += 1; em += whole / 2; } for (map<string, int>::iterator it = pol.begin(); it != pol.end(); it++) { if (en[it->first] == 0) p++; } for (map<string, int>::iterator it = en.begin(); it != en.end(); it++) { if (pol[it->first] == 0) em++; } if (p > em) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <class R> inline void Readin(R &K) { bool F = K = 0; char C = getchar(); while (C < '0' || C > '9') { if (C == '-') F = 1; C = getchar(); } while (C <= '9' && C >= '0') K = (K << 1) + (K << 3) + C - '0', C = getchar(); if (F) K = -K; } template <class R> inline bool Checkmax(R &A, R B) { return A < B ? A = B, 1 : 0; } template <class R> inline bool Checkmin(R &A, R B) { return A > B ? A = B, 1 : 0; } long long N, M, Ans; bitset<10000000> Vis; signed main() { Readin(N); Readin(M); for (register long long i = 0; ++i <= N;) { Ans += min(i * i, M) << 1; for (register long long j = i; --j >= 0;) { if ((i + j) * (i - j) > M) break; if (Vis[i + j]) --Ans; else Vis[i + j] = true; if (Vis[i - j]) --Ans; else Vis[i - j] = true; } } return not printf("%lld\n", Ans); }
15
#include <bits/stdc++.h> using namespace std; const long long mxsz = 2e5 + 5; vector<long long> X[mxsz], Y[mxsz]; struct DSU { vector<long long> p; inline long long find(long long n) { return p[n] == n ? n : p[n] = find(p[n]); } inline void join(long long a, long long b) { p[find(a)] = find(b); } DSU(long long n) : p(n) { for (long long i = 0; i < n; i++) p[i] = i; } }; signed main() { ios::sync_with_stdio(0); cin.tie(0); ; long long n, m, k; cin >> n >> m >> k; if (!k) return cout << n + m - 1 << '\n', 0; vector<tuple<long long, long long> > v(k); for (long long i = 0, x, y; i < k; i++) cin >> x >> y, --x, --y, v[i] = tie(x, y), X[x].push_back(y), Y[y].push_back(x); long long sum = 0; for (long long i = 0; i < n; i++) sum += X[i].size() == 0; for (long long i = 0; i < m; i++) sum += Y[i].size() == 0; DSU dsu(n + m); for (long long x = 0; x < n; x++) for (long long y : X[x]) dsu.join(x, n + y); for (long long y = 0; y < m; y++) for (long long x : Y[y]) dsu.join(x, n + y); set<long long> st; for (auto it : v) { long long x, y; tie(x, y) = it; st.insert(dsu.find(x)); } sum += st.size() - 1; cout << sum << '\n'; }
11
#include <bits/stdc++.h> using namespace std; int getInp() { int t; cin >> t; if (t == 0 || t == -1) { exit(0); } return t; } template <typename T> int test(const T &inp) { cout << inp << endl; return getInp(); } int main() { int n; n = test('a') + 1; int na = test(string(n - 1, 'b')); int nb = n - na; if (nb == 0) { test(string(n, 'a')); } else { string t(n, 'a'); for (int i = 0; i < n - 1; ++i) { t[i] = 'b'; int r = test(t); if (r == nb + 1) { t[i] = 'a'; --na; } else { --nb; if (nb == 0) { break; } } } t[n - 1] = 'b'; test(t); } return 1; }
15
#include <bits/stdc++.h> using namespace std; template <class T> inline T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { return (a / gcd(a, b)) * b; } int main() { string s; while (cin >> s) { int len = s.size(); int up = 0; for (int i = 0; i < len; i++) { if (s[i] >= 'A' && s[i] <= 'Z') { up++; } } if (up == len) { for (int i = 0; i < len; i++) { s[i] = tolower(s[i]); } cout << s << endl; } else if (islower(s[0]) && (up == len - 1)) { s[0] = toupper(s[0]); for (int i = 1; i < len; i++) { s[i] = tolower(s[i]); } cout << s << endl; } else cout << s << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, k, a[100010], j; int main() { cin >> n >> k; for (long long i = 1; i <= n; i += 1) cin >> a[i]; bool ok = 1; for (long long i = n - 1; i >= 1; i -= 1) if (a[i] != a[i + 1]) { j = i; ok = 0; break; } if (ok) { cout << 0; return 0; } for (long long i = k; i <= n - 1; i += 1) if (a[i] != a[i + 1]) { cout << "-1"; return 0; } if (j < k) cout << j; else cout << n - k; return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, m, mmp[110][110], ans = 100000, now = 0, sum[110][110]; int dfs(int x, int y, int k) { if (x + k - 1 > n || y + k - 1 > m) return 10100000; int cnt = 0, cur = sum[x + k - 1][y + k - 1] - sum[x - 1][y + k - 1] - sum[x + k - 1][y - 1] + sum[x - 1][y - 1]; if (cur == now) { return k * k - now; } return 10100000; } int main() { cin >> n >> m; char str[1000]; for (int i = 1; i <= n; i++) { cin >> str; for (int j = 0; j < m; j++) { if (str[j] == 'B') mmp[i][j + 1] = 1, now++; else mmp[i][j + 1] = 0; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { sum[i][j] = sum[i - 1][j] + sum[i][j - 1] + mmp[i][j] - sum[i - 1][j - 1]; } } for (int k = 1; k <= min(n, m); k++) for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int nn = dfs(i, j, k); if (nn != 10100000) { cout << nn; return 0; } } } cout << -1; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int mod = 100000007; const int maxe = 1e6 + 85; long long n, m, x, ans; string s; int a[15]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; int cnt[4]{4, 9, 25, 49}; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 0; i < 15; i++) { cout << a[i] << endl; fflush(stdout); cin >> s; if (s == "yes") { n++; if (n == 2) { cout << "composite"; return 0; } } } for (int i = 0; i < 4; i++) { cout << cnt[i] << endl; fflush(stdout); cin >> s; if (s == "yes") { cout << "composite"; return 0; } } cout << "prime"; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; long long p; long long t[maxn]; int od[maxn]; vector<long long> lsh; vector<int> id[maxn]; queue<int> que; priority_queue<long long, vector<long long>, greater<long long> > pq1; priority_queue<int, vector<int>, greater<int> > pq2; deque<pair<int, long long> > dq; map<long long, bool> has; long long ans[maxn]; struct BIT { int bit[maxn]; inline void add(int x, int v) { while (x < maxn) { bit[x] += v; x += x & -x; } } inline int sum(int x) { int ret = 0; while (x > 0) { ret += bit[x]; x -= x & -x; } return ret; } } tree; int main() { int n; scanf("%d%I64d", &n, &p); for (int i = 0; i < n; i++) { scanf("%I64d", &t[i]); lsh.emplace_back(t[i]); tree.add(i + 1, 1); } sort(lsh.begin(), lsh.end()); lsh.erase(unique(lsh.begin(), lsh.end()), lsh.end()); for (int i = 0; i < lsh.size(); i++) { pq1.push(lsh[i]); has[lsh[i]] = 1; } for (int i = 0; i < n; i++) { od[i] = lower_bound(lsh.begin(), lsh.end(), t[i]) - lsh.begin(); id[od[i]].emplace_back(i); } while (!pq1.empty()) { long long tim = pq1.top(); pq1.pop(); int pos = lower_bound(lsh.begin(), lsh.end(), tim) - lsh.begin(); if (lsh[pos] == tim) { for (int i = 0; i < id[pos].size(); i++) { pq2.push(id[pos][i]); } } if (!dq.empty() && dq.front().second == tim) { tree.add(dq.front().first + 1, 1); dq.pop_front(); } if (!pq2.empty()) { int id = pq2.top(); if (tree.sum(id) == id) { pq2.pop(); long long pret = (dq.empty() ? tim : dq.back().second); pret += p; if (!has.count(pret)) { pq1.push(pret); has[pret] = 1; } dq.push_back(make_pair(id, pret)); ans[id] = pret; tree.add(id + 1, -1); } } } for (int i = 0; i < n; i++) printf("%I64d ", ans[i]); return 0; }
15
#include <bits/stdc++.h> using namespace std; namespace io { const int SZ = 1 << 13; char buff[SZ], *pos = buff + SZ - 1; char gc() { if (++pos == buff + SZ) fread(pos = buff, 1, SZ, stdin); return *pos; } } // namespace io template <typename T> inline void read(T &x) { x = 0; int f = 1, c = io ::gc(); for (; !isdigit(c); c = io ::gc()) if (c == '-') f = -f; for (; isdigit(c); c = io ::gc()) x = (x << 3) + (x << 1) + (c ^ 48); x *= f; } const int N = 1 << 20; int n, q; char s[N]; int lst[26], nxt[N]; pair<int, int> P[N]; struct BIT { struct H { int mul, imul, mod; int pw[N], ipw[N]; int h[N]; int qp(int x, int n) { int ans = 1; for (; n; n >>= 1, x = 1LL * x * x % mod) if (n & 1) ans = 1LL * ans * x % mod; return ans; } void init(int a, int b) { memset(h, 0, sizeof h); mul = a; mod = b; imul = qp(mul, mod - 2); pw[0] = 1; for (int i = 0; i < n; ++i) pw[i + 1] = 1LL * pw[i] * mul % mod; ipw[0] = 1; for (int i = 0; i < n; ++i) ipw[i + 1] = 1LL * ipw[i] * imul % mod; } void upd(int x, int v) { int y = pw[x]; for (++x; x <= n; x += x & -x) { h[x] += 1LL * v * y % mod; h[x] %= mod; h[x] += mod; h[x] %= mod; } } int qry(int x) { int ans = 0; for (; x; x -= x & -x) { ans += h[x]; ans %= mod; } return ans; } int qry(int x, int y) { return 1LL * (qry(y) + mod - 1LL * qry(x) % mod) % mod * ipw[x] % mod; } }; H h1, h2; void init() { h1.init(233, 998244353); h2.init(19260817, 1000000007); } void upd(int x, int v) { h1.upd(x, v); h2.upd(x, v); } pair<int, int> qry(int x, int y) { return make_pair(h1.qry(x, y), h2.qry(x, y)); } }; BIT bit; struct Qnode { int l, r, id; bool operator<(const Qnode &x) const { return l < x.l; } }; vector<Qnode> query; int main() { read(n); read(q); for (int i = 0; i < n; ++i) s[i] = io ::gc(); bit.init(); for (int i = 0; i < 26; ++i) lst[i] = n; for (int i = n - 1; ~i; --i) { nxt[i] = lst[s[i] - 'a']; lst[s[i] - 'a'] = i; } for (int i = 0; i < q; ++i) { int x, y, l; read(x); read(y); read(l); --x, --y; query.push_back((Qnode){x, x + l, i << 1}); query.push_back((Qnode){y, y + l, i << 1 | 1}); } sort(query.begin(), query.end()); for (int i = 0; i < n; ++i) { if (nxt[i] < n) { bit.upd(nxt[i], nxt[i] - i); } } for (int i = 0, j = 0; i < n; ++i) { while (j < q + q && query[j].l <= i) { P[query[j].id] = bit.qry(query[j].l, query[j].r); ++j; } if (nxt[i] < n) bit.upd(nxt[i], i - nxt[i]); } for (int i = 0; i < q; ++i) { puts(P[i << 1] == P[i << 1 | 1] ? "YES" : "NO"); } return 0; }
15
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <set> using namespace std; const int maxn = 1e5+5; int a[maxn],b[maxn]; int main(){ int T; cin >> T; while(T--){ int n; cin >> n; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); for(int i=1;i<=n;++i){ int x1,y1; cin >> x1 >> y1; a[i] = x1;b[i] = y1; } sort(a+1,a+n+1); sort(b+1,b+n+1); if(n&1)cout << 1 << endl; else cout << int64_t(abs(int64_t(a[n/2+1]-a[n/2]+1))*abs(int64_t(b[n/2+1]-b[n/2]+1))) << endl; } }
7
#include <bits/stdc++.h> using namespace std; int a[101]; int level, t, x; int main() { cin >> t; cin >> level; for (int i = 1; i <= level; ++i) { cin >> x; a[x]++; } cin >> level; for (int i = 1; i <= level; ++i) { cin >> x; a[x]++; } bool check = true; for (int i = 1; i <= t; ++i) if (a[i] == 0) check = false; if (check) cout << "I become the guy."; else cout << "Oh, my keyboard!"; }
0
#include <bits/stdc++.h> using namespace std; const int mx = 5e4 + 1, th = 16, t8 = 1 << 8; int n, q, a[mx]; int child[mx * th][2]; int vans[mx * th]; vector<int> e[mx]; int par[mx], part8[mx]; bool mark[mx]; int h[mx]; int vs[mx][t8]; int pt; bool bith; int get(long long int val, long long int v = 0, long long int sbit = 1LL << (th - 1)) { if (sbit == 1 << 7) return vans[v]; bith = val & sbit; if (bith) { if (child[v][0]) return sbit | get(val, child[v][0], sbit >> 1); else return get(val, child[v][1], sbit >> 1); } else { if (child[v][1]) return sbit | get(val, child[v][1], sbit >> 1); else return get(val, child[v][0], sbit >> 1); } return 0; } void add(long long int val, long long int v = 0, long long int sbit = 1LL << (th - 1)) { if (sbit == 1 << 7) { vans[v] = max(0LL + vans[v], val % t8); return; } bith = val & sbit; if (!child[v][bith]) child[v][bith] = ++pt; add(val, child[v][bith], sbit >> 1); } void dfs(int x) { mark[x] = 1; int z = 0, v = x; while (z < t8 && v != -1) { add(a[v] ^ z); v = par[v]; z++; } for (int i = 0; i < t8; i++) vs[x][i] = get(i * t8); for (int i = 0; i <= pt; i++) { child[i][0] = 0; child[i][1] = 0; vans[i] = 0; } pt = 0; if (z == t8 && v != -1) part8[x] = v; else part8[x] = -1; for (int i = 0; i < e[x].size(); i++) if (!mark[e[x][i]]) { par[e[x][i]] = x; h[e[x][i]] = h[x] + 1; dfs(e[x][i]); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int u, v, m, dist8, ans; cin >> n >> q; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) { cin >> u >> v; u--, v--; e[u].push_back(v); e[v].push_back(u); } par[0] = -1; dfs(0); while (q--) { cin >> u >> v; u--, v--; ans = -1; dist8 = 0; m = v; while (part8[m] != -1 && h[part8[m]] >= h[u]) { ans = max(ans, vs[m][dist8]); m = part8[m]; dist8++; } while (m != -1 && h[m] >= h[u]) { ans = max(ans, (h[v] - h[m]) ^ a[m]); m = par[m]; } cout << ans << '\n'; } return 0; }
24
#include <bits/stdc++.h> using namespace std; vector<int> ans; int main() { int T; string s; scanf("%d", &T); while (T--) { cin >> s; int l = s.size(); ans.clear(); for (int i = 0; i < l - 2; ++i) { if (i < s.size() - 4 && s[i] == 't' && s[i + 1] == 'w' && s[i + 2] == 'o' && s[i + 3] == 'n' && s[i + 4] == 'e') s[i + 2] = '#', ans.push_back(i + 3); else if (s[i] == 'o' && s[i + 1] == 'n' && s[i + 2] == 'e') s[i + 1] = '#', ans.push_back(i + 2); else if (s[i] == 't' && s[i + 1] == 'w' && s[i + 2] == 'o') s[i + 1] = '#', ans.push_back(i + 2); } int len = ans.size(); printf("%d\n", len); for (int i = 0; i < len; ++i) printf("%d%c", ans[i], i == len - 1 ? '\n' : ' '); if (!len) printf("\n"); } return 0; }
6
#include <bits/stdc++.h> using namespace std; long long int N = 1e9 + 7; int n, m; char mat[504][504]; int vis[504][504]; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; void solve() { cin >> n >> m; memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> mat[i][j]; } } int r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2; if (r1 == r2 && c1 == c2) { for (int j = 0; j < 4; j++) { int x1 = r1 + dx[j], y1 = c1 + dy[j]; if (x1 > 0 && x1 <= n && y1 > 0 && y1 <= m) { if (mat[x1][y1] == '.') { cout << "YES" << endl; return; } } } cout << "NO" << endl; return; } queue<pair<int, int> > q; q.push({r1, c1}); vis[r1][c1] = 1; while (!q.empty()) { pair<int, int> u = q.front(); q.pop(); int x = u.first, y = u.second; for (int j = 0; j < 4; j++) { int x1 = x + dx[j], y1 = y + dy[j]; if (x1 > 0 && x1 <= n && y1 > 0 && y1 <= m) { if (x1 == r2 && y1 == c2) { if (mat[x1][y1] == 'X') { cout << "YES" << endl; return; } int cnt = 0; for (int k = 0; k < 4; k++) { int x2 = x1 + dx[k], y2 = y1 + dy[k]; if (x2 > 0 && x2 <= n && y2 > 0 && y2 <= m) { if (mat[x2][y2] == '.') { cnt++; } } } if (mat[x][y] == 'X') { if (cnt >= 1) { cout << "YES" << endl; return; } cout << "NO" << endl; return; } if (cnt >= 2) { cout << "YES" << endl; return; } cout << "NO" << endl; return; } else { if (mat[x1][y1] == '.' && !vis[x1][y1]) { q.push({x1, y1}); vis[x1][y1] = 1; } } } } } cout << "NO" << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } }
12
#include <bits/stdc++.h> #define jh(x, y) (x ^= y ^= x ^= y) #define loc(x, y) ((x - 1) * m + y) #define lowbit(x) (x & -x) typedef long long ll; const int N = 1e6 + 5, INF = 0x3f3f3f3f, mod = 1e9 + 7; const double PI = acos(-1); using namespace std; namespace fast_IO { ll read() { ll x = 0; char c; bool flag = false; while ((c = getchar()) == ' ' || c == '\n' || c == '\r'); if (c == '-')flag = true; else x = c ^ 48; while ((c = getchar()) != ' ' && c != '\n' && c != '\r' && ~c) x = (x << 1) + (x << 3) + (c ^ 48); if (flag)return -x; return x; } void write(ll x) { if (x < 0)putchar('-'), x = -x; if (x >= 10)write(x / 10); putchar(x % 10 ^ 48); } } bool tag[N]; int main() { ll k = fast_IO::read(), l = fast_IO::read(), r = fast_IO::read(), t = fast_IO::read(), x = fast_IO::read(), y = fast_IO::read(); if ((k - x < l && k + y > r) || (k + y <= r && k + y - x < l)) { puts("No"); return 0; } if (x == y)puts("Yes"); else if (x > y) { ll tt; if (k + y <= r)tt = (k + y - x - l) / (x - y) + 1; else tt = (k - x - l) / (x - y) + 1; if (tt >= t)puts("Yes"); else puts("No"); } else { ll tt = (k - l) / x; ll now = l + (k - l) % x; while (!tag[now - l]) { tag[now - l] = true; if (now + y > r)break; now += y; tt += (now - l) / x; now = l + (now - l) % x; if (tag[now - l]) { puts("Yes"); return 0; } } if (t <= tt)puts("Yes"); else puts("No"); } return 0; }
14
#include <bits/stdc++.h> using ll = long long; using ld = long double; using namespace std; const ll N = 400001, L = 19; vector<ll> g[N]; vector<pair<ll, ll>> ng[N]; ll tin[N], tout[N], ct, ord[N], fv[N], s[N], qq[N], h[N], up[L][N], vir[N], ft[N], k; set<vector<ll>> qs; void dfs(ll v, ll p) { ord[ct] = v; tin[v] = ct++; for (ll u : g[v]) { if (u == p) continue; h[u] = h[v] + 1; up[0][u] = v; dfs(u, v); } tout[v] = ct; } ll lca(ll u, ll v) { if (h[u] < h[v]) swap(u, v); for (ll i = L - 1; i >= 0; i--) if (h[up[i][u]] >= h[v]) u = up[i][u]; assert(h[u] == h[v]); if (u == v) return u; for (ll i = L - 1; i >= 0; i--) if (up[i][u] != up[i][v]) { u = up[i][u]; v = up[i][v]; } return up[0][u]; } void add(ll v, ll ml) { for (auto &pp : ng[v]) { if (vir[pp.first] != -1) continue; if (ml >= pp.second) qs.insert({ft[v], pp.first, ml - pp.second}); else { ll dt = (pp.second - ml + s[vir[v]] - 1) / s[vir[v]]; qs.insert({ft[v] + dt * k, pp.first, dt * s[vir[v]] + ml - pp.second}); } } } ll isp(ll u, ll v) { return tin[u] <= tin[v] && tout[v] <= tout[u]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, u, v, q, m; cin >> n; for (ll i = 0; i < (n - 1); i++) { cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } dfs(0, 0); for (ll i = 0; i < (L - 1); i++) for (ll j = 0; j < (n); j++) up[i + 1][j] = up[i][up[i][j]]; cin >> q; for (ll i = 0; i < (n); i++) vir[i] = -1; for (ll z = 0; z < (q); z++) { cin >> k >> m; vector<ll> li; for (ll i = 0; i < (k); i++) { cin >> fv[i] >> s[i]; fv[i]--; vir[fv[i]] = i; ft[fv[i]] = i; li.push_back(tin[fv[i]]); } for (ll i = 0; i < (m); i++) { cin >> qq[i]; qq[i]--; li.push_back(tin[qq[i]]); } sort((li).begin(), (li).end()); li.resize(unique((li).begin(), (li).end()) - li.begin()); ll cs = li.size(); for (ll i = 0; i < (cs); i++) { v = ord[li[i]]; ll l1 = lca(v, ord[li[(i + 1) % cs]]); li.push_back(tin[l1]); } sort((li).begin(), (li).end()); li.resize(unique((li).begin(), (li).end()) - li.begin()); vector<ll> st; int cnt = 0; for (ll i : li) { while (!st.empty() && !isp(ord[st.back()], ord[i])) st.pop_back(); if (!st.empty()) { v = ord[i]; u = ord[st.back()]; ng[v].push_back({u, h[v] - h[u]}); ng[u].push_back({v, h[v] - h[u]}); cnt++; } st.push_back(i); } assert(cnt + 1 == li.size()); for (ll i = 0; i < (k); i++) add(fv[i], 0); while (!qs.empty()) { ll ct = (*qs.begin())[0], cv = (*qs.begin())[1], cl = (*qs.begin())[2]; qs.erase(qs.begin()); if (vir[cv] != -1) continue; vir[cv] = ct % k; ft[cv] = ct; add(cv, cl); } for (ll i = 0; i < (m); i++) cout << vir[qq[i]] + 1 << ' '; cout << '\n'; for (ll i : li) { v = ord[i]; ng[v].clear(); vir[v] = -1; ft[v] = 0; } } return 0; }
22
#include <bits/stdc++.h> using namespace std; const int N = 1000000 + 10; long long data[N * 2], sum[N * 2]; int last[N * 2], f[N * 2]; struct AC_IO { long long read() { char c = getchar(); while (!isdigit(c)) c = getchar(); long long x = 0ll; while (isdigit(c)) { x = x * 10ll + c - '0'; c = getchar(); } return x; } } acfox; int main() { int n, q; long long val; scanf("%d %d", &n, &q); for (int i = 1; i <= n; i++) data[i] = acfox.read(), data[i + n] = data[i]; for (int i = 1; i <= n * 2; i++) sum[i] = sum[i - 1] + data[i]; for (int i = 1; i <= n; i++) last[i] = i; for (int i = 1; i <= q; i++) { val = acfox.read(); int pos = 1; for (int j = n + 1; j <= n + n; j++) { while (sum[j] - sum[pos] > val) pos++; last[j] = last[pos]; f[j] = f[pos] + 1; if (j - last[j] >= n) { printf("%d\n", f[j]); break; } } } return 0; }
16
#include <bits/stdc++.h> using namespace std; const int LIM = 100001; bool prime[LIM]; void sieve(int n) { prime[1] = false; for (int i = 2; i <= n; ++i) { prime[i] = true; } for (int i = 2; i * i <= n; ++i) { if (prime[i]) { for (int j = i * i; j <= n; j += i) { prime[j] = false; } } } } int f[256]; void output(vector<char>& a) { for (int i = 0; i < a.size(); ++i) { cout << a[i]; } cout << endl; } int main() { string a; cin >> a; sieve(a.size()); int k = 0; for (int i = 2; i <= a.size(); ++i) { if (!prime[i] || 2 * i <= a.size()) { ++k; } } for (int i = 0; i < a.size(); ++i) { ++f[int(a[i])]; } vector<pair<int, int> > b; for (int i = 0; i < 256; ++i) { if (f[i]) { b.push_back(make_pair(f[i], i)); } } sort(b.rbegin(), b.rend()); if (b[0].first < k) { cout << "NO" << endl; } else { cout << "YES" << endl; vector<char> first(k, b[0].second); vector<char> second(b[0].first - k, b[0].second); for (int i = 1; i < b.size(); ++i) { for (int j = 0; j < b[i].first; ++j) { second.push_back(b[i].second); } } int p = 0; int q = 1; cout << second[0]; for (int i = 2; i <= a.size(); ++i) { if (!prime[i] || 2 * i <= a.size()) { cout << first[p++]; } else { cout << second[q++]; } } cout << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; bool i1, i2, i3, i4, d1, d2, d3, d4, t1, t2, o1; cin >> i1 >> i2 >> i3 >> i4; d1 = i1 ^ i2; d2 = (i3 || i4); d3 = i2 && i3; d4 = i1 ^ i4; t1 = d1 && d2; t2 = (d3 || d4); o1 = t1 ^ t2; cout << o1 ? 1 : 0; }
12
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; const long long N = 101; long long mul(long long a, long long b) { return (a * b) % MOD; } long long add(long long a, long long b) { return (a + b) % MOD; } long long binpow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = mul(res, a); a = mul(a, a); b >>= 1; } return res; } long long divv(long long a, long long b) { return mul(a, binpow(b, MOD - 2)); } long long C(long long n, long long k) { long long lol = 1; for (long long j = 0; j < k; j++) { lol = mul(lol, n - j); } for (long long j = 1; j <= k; j++) { lol = divv(lol, j); } return lol; } long long solve(long long x, long long y) { return C(x + y - 1, y); } struct Seg { long long l; long long r; long long len; Seg() {} Seg(long long a, long long b) { l = a; r = b; len = r - l + 1; } }; long long dp[N][N]; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; vector<long long> l(n), r(n); vector<long long> br; for (long long i = 0; i < n; i++) { cin >> l[i] >> r[i]; br.push_back(l[i]); br.push_back(r[i] + 1); } reverse(l.begin(), l.end()); reverse(r.begin(), r.end()); sort(br.begin(), br.end()); br.resize(unique(br.begin(), br.end()) - br.begin()); vector<Seg> s; for (long long i = 0; i + 1 < (long long)br.size(); i++) { s.push_back(Seg(br[i], br[i + 1] - 1)); } long long m = (long long)s.size(); dp[0][0] = 1; for (long long i = 0; i < m; i++) { for (long long j = 0; j <= n; j++) { for (long long tk = 0; j + tk <= n; tk++) { if (tk > 0) { if (l[j + tk - 1] <= s[i].l && s[i].r <= r[j + tk - 1]) { } else { break; } } dp[i + 1][j + tk] = add(dp[i + 1][j + tk], mul(dp[i][j], solve(s[i].len, tk))); } } } long long ans = dp[m][n]; for (long long i = 0; i < n; i++) { ans = divv(ans, r[i] - l[i] + 1); } cout << ans << endl; }
19
#include <bits/stdc++.h> using namespace std; int main() { { int y, b, r; cin >> y >> b >> r; if (r > b) { if (b > y) cout << (3 * y) + 3; else cout << 3 * b; } else { if (y < (r - 2)) cout << (3 * y) + 3; else cout << (3 * r - 3); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string arr[n]; for (int i = 0; i < n; ++i) { arr[i] = string(m, '0'); for (int j = 0; j < m; cin >> arr[i][j], ++j) ; } int r = 2 * n; while (!((r / 2) % 2)) { r /= 2; for (int i = 0; i < n; i += r) { for (int a = i; a < i + r / 2; ++a) if (arr[a] != arr[i + r - a - 1]) goto rrr; } continue; rrr: cout << r; return 0; } cout << r / 2; }
5
#include <bits/stdc++.h> using namespace std; const int NMAX = 51; const int WMAX = 1000; int N; int a[2 * NMAX]; int main() { scanf("%d", &N); for (int i = 1; i <= 2 * N; i++) scanf("%d", &a[i]); sort(a + 1, a + 1 + 2 * N); int res = WMAX * NMAX; for (int i = 1; i < 2 * N; i++) for (int j = i + 1; j <= 2 * N; j++) { int t = 0, cnt = 0, first = 0; for (int x = 1; x <= 2 * N; x++) if (x != i && x != j) { cnt++; if (cnt == 2) t += abs(a[x] - first); else first = a[x]; cnt %= 2; } if (t < res) res = t; } printf("%d", res); return 0; }
7
#include <bits/stdc++.h> using namespace std; int dx[] = {-2, -2, 2, 2}; int dy[] = {2, -2, -2, 2}; long long toInt(string s) { stringstream aa(s); long long k; aa >> k; return k; } string toString(long long t) { stringstream y; y << t; return y.str(); } int gcd(int a, int b) { if (!b) return a; else return gcd(b, a % b); } int lcm(int a, int b) { return (a * b) / gcd(a, b); } long long m, k, n, d, sum; long long a[111111], diff[111111], ll[111111]; map<int, bool> MAP; int main(void) { cin >> n >> m; for (int i = (0); i < (n); i++) cin >> a[i]; diff[n - 1] = 1; MAP[a[n - 1]] = true; for (int i = n - 2; i >= 0; i--) { if (!MAP[a[i]]) { MAP[a[i]] = true; diff[i] = diff[i + 1] + 1; } else diff[i] = diff[i + 1]; } vector<int> ans; for (int i = (0); i < (m); i++) { cin >> d; ans.push_back(diff[d - 1]); } for (int i = (0); i < (m); i++) cout << ans[i] << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 200100, INF = 2009000999; const long long MOD = 1000 * 1000 * 1000 + 7; long long a[N], opt[N]; int main() { ios::sync_with_stdio(0); int n; cin >> n; int l90 = 0, l1440 = 0; cin >> a[0]; opt[0] = 20; cout << 20 << endl; for (int i = 1; i < n; i++) { cin >> a[i]; while (a[i] - a[l90] >= 90) l90++; while (a[i] - a[l1440] >= 1440) l1440++; long long cur_opt = opt[i - 1] + 20; if (l90 > 0) cur_opt = min(cur_opt, opt[l90 - 1] + 50); else cur_opt = min(cur_opt, 50ll); if (l1440 > 0) cur_opt = min(cur_opt, opt[l1440 - 1] + 120); else cur_opt = min(cur_opt, 120ll); opt[i] = cur_opt; cout << cur_opt - opt[i - 1] << endl; } return 0; }
8
#include <bits/stdc++.h> using namespace std; long long calc(int x1, int y1, int x2, int y2) { return (abs(x1 - x2) + abs(y1 - y2)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, h, l, u, q; cin >> n >> h >> l >> u >> q; int a, b, x, y; long long dis; while (q--) { dis = 0; cin >> a >> b >> x >> y; if (a == x) { dis += abs(b - y); } else { if (b > u) dis += abs(b - u); else if (b < l) dis += abs(b - l); dis += abs(a - x); if (b > u) dis += abs(y - u); else if (b < l) dis += abs(y - l); else dis += abs(b - y); } cout << dis << endl; } return 0; }
2
#include <bits/stdc++.h> int main() { int t, n; long s1, s2; scanf("%d", &t); for (int i = 0; i < t; i++) { s1 = 0; s2 = 0; scanf("%d", &n); for (int j = n / 2; j < n; j++) { s1 += pow(2, j); } for (int j = 1; j < n / 2; j++) { s2 += pow(2, j); } s2 += pow(2, n); printf("%ld\n", s2 - s1); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int prime[123456]; vector<int> v; int dp[300][1200][20], tak[50][1200], w[50], cost[50], d, t, n, k; int foo(int i, int val, int c) { int p1, p2; if (c > k || i >= v.size()) return 0; if (c == k) return val == n ? 1 : 0; if (dp[i][val][c] != -1) return dp[i][val][c]; if (val + v[i] <= n) { p1 = foo(i + 1, val + v[i], c + 1); } else p1 = 0; p2 = foo(i + 1, val, c); dp[i][val][c] = p1 + p2; return dp[i][val][c]; } void sieve() { v.push_back(2); for (int i = 4; i <= 1130; i += 2) prime[i] = 1; for (int i = 3; i <= 1130; i++) { if (!prime[i]) { v.push_back(i); for (int j = i + i; j <= 1120; j += i) prime[j] = 1; } } } int fr[2123], vis; int main() { long long q, a, b, n, r; cin >> n; b = n; while (n--) { cin >> r; for (int i = 0; i < r; i++) { cin >> a; fr[a]++; } } for (int i = 1; i < 101; i++) { if (fr[i] == b) cout << i << " "; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int dis[105], rdis[105]; double k[105], k2[105]; vector<int> nei[105]; int n, m; void bfs(int s, int d[], double f[]) { queue<int> q; q.push(s); d[s] = 0; f[s] = 1; while (!q.empty()) { int h = q.front(); q.pop(); for (int i = 0; i < nei[h].size(); i++) { int k = nei[h][i]; if (d[k] > d[h] + 1) { d[k] = d[h] + 1; q.push(k); f[k] = f[h]; } else if (d[k] == d[h] + 1) { f[k] += f[h]; } } } } int main() { fill(dis, dis + 105, 1e9); fill(rdis, rdis + 105, 1e9); scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); x--; y--; nei[x].push_back(y); nei[y].push_back(x); } bfs(0, dis, k); bfs(n - 1, rdis, k2); int l = dis[n - 1]; double ans = double(0); for (int i = 0; i < n; i++) { if (dis[i] + rdis[i] == l) { double ddd = k[i] * k2[i] / k2[0]; if (i != 0 && i != n - 1) ddd *= double(2); ans = max(ans, ddd); } } printf("%.9f\n", ans); return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { map<string, int> mp; string str; long long int p[110], a[110]; long long int n, m, mn = 0, mx = 0; cin >> n >> m; cin.ignore(); for (int i = 0; i < n; i++) { cin >> p[i]; cin.ignore(); } for (int i = 0; i < m; i++) { getline(cin, str); if (!mp.count(str)) mp[str] = 1; else mp[str]++; } sort(p, p + n); map<string, int>::iterator ite = mp.begin(); int i; for (i = 0; ite != mp.end(); i++, ite++) a[i] = (*ite).second; sort(a, a + i); int c = 0; for (int j = i - 1; j >= 0; j--, c++) { mn += (a[j]) * p[c]; mx += (a[j]) * p[n - 1 - c]; } cout << mn << " " << mx << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long int INF = 1e18; const long long int MOD = 1e9 + 7; const long long int mod = 998244353; void solve() { int n, q; cin >> n; vector<long long int> ar(n); long long int sum = 0; for (int i = 0; i < n; i++) { cin >> ar[i]; sum += ar[i]; } sort(ar.begin(), ar.end()); cin >> q; while (q--) { vector<long long int> res; long long int x, y; cin >> x >> y; int l = 0, r = n - 1, mid; while (l <= r) { mid = l + (r - l) / 2; if (ar[mid] > x) r = mid - 1; else l = mid + 1; } if (ar[r] >= 0) { long long int t = max(0ll, x - ar[r]) + max(0ll, y - (sum - ar[r])); res.push_back(t); } if (r + 1 < n) { long long int t = max(0ll, x - ar[r + 1]) + max(0ll, y - (sum - ar[r + 1])); res.push_back(t); } sort(res.begin(), res.end()); cout << res[0] << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long INF = 1e15 + 7; const long long MAX = 1e5; const long long MOD = 1e9 + 7; typedef vector<long long> vi; typedef vector<vi> vvi; typedef vector<pair<long long, long long> > vii; typedef vector<vii> vvii; typedef vector<bool> vb; const long long maxn = 5005, maxm = 1e5 + 5; long long n, m, k, parent[maxn], ans, num, cnt, t[maxn]; struct node { long long x, y, w, p, id; bool operator<(const node &B) const { return p == B.p ? x < B.x : p < B.p; } } e[maxm]; long long Find(long long a) { if (parent[a] == a) return a; return parent[a] = Find(parent[a]); } void Union(long long mid) { for (long long i = 1; i <= m; i++) e[i].p = e[i].w + (e[i].x == 1 ? mid : 0); sort(e + 1, e + 1 + m); for (long long i = 1; i <= n; i++) parent[i] = i; ans = num = cnt = t[0] = 0; for (long long i = 1, x, y; i <= m && cnt < n - 1; i++) if ((x = Find(e[i].x)) != (y = Find(e[i].y)) && num + (e[i].x == 1) <= k) parent[x] = y, ans += e[i].p, num += (e[i].x == 1), cnt++, t[++t[0]] = e[i].id; } signed main() { cin >> n >> m >> k; vi band; band.push_back(0); for (long long i = 1; i <= m; i++) { cin >> e[i].x >> e[i].y >> e[i].w, e[i].id = i; band.push_back(e[i].w); if (e[i].x > e[i].y) swap(e[i].x, e[i].y); } Union(-MAX); if (cnt < n - 1 || num < k) cout << -1 << endl; else { long long start = -MAX, end = MAX; while (start < end) { long long mid = (start + end + 1) / 2; Union(mid); if (num >= k) start = mid; else end = mid - 1; } Union(start); long long sum = 0; cout << t[0] << endl; for (long long i = 1; i <= t[0]; i++) sum += band[t[i]], cout << t[i] << " "; cout << endl; } return 0; }
16
#include <bits/stdc++.h> using namespace std; using i64 = long long int; using ii = pair<int, int>; using ii64 = pair<i64, i64>; vector<ii> find(vector<int>& val, bool rev) { vector<ii> res; for (int i = 0; i < val.size(); i++) { if (val[i] == -1) return vector<ii>(); if (rev) res.emplace_back(i, val[i]); else res.emplace_back(val[i], i); } return res; } int main() { int n; scanf("%d", &n); vector<string> board(n); vector<int> cell(n, -1); vector<int> rev(n, -1); for (int i = 0; i < n; i++) { cin >> board[i]; for (int j = 0; j < n; j++) { if (board[i][j] == '.') { if (cell[j] == -1) cell[j] = i; if (rev[i] == -1) rev[i] = j; } } } vector<ii> a = find(cell, false); vector<ii> b = find(rev, true); if (!a.empty()) for (auto& r : a) printf("%d %d\n", r.first + 1, r.second + 1); else if (!b.empty()) for (auto& r : b) printf("%d %d\n", r.first + 1, r.second + 1); else printf("-1"); return 0; }
7
#include <bits/stdc++.h> int main() { int a = 0, b; char c[1001], d[1001]; gets(c); while (c[a] != '.') { d[a] = c[a]; a++; } d[a] = '\0'; if (c[a - 1] == '9') printf("GOTO Vasilisa.\n"); else if (c[a + 1] < '5') puts(d); else { d[a - 1] += 1; puts(d); } return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<complex<double> > fft(vector<complex<double> > v, bool rev = false) { int n = v.size(), i, j, m; for (i = 0, j = 1; j < n - 1; j++) { for (int k = n >> 1; k > (i ^= k); k >>= 1) ; if (i > j) swap(v[i], v[j]); } for (int m = 2; m <= n; m *= 2) { double deg = (rev ? -1 : 1) * 2 * acos(-1) / m; complex<double> wr(cos(deg), sin(deg)); for (i = 0; i < n; i += m) { complex<double> w(1, 0); for (int j1 = i, j2 = i + m / 2; j2 < i + m; j1++, j2++) { complex<double> t1 = v[j1], t2 = w * v[j2]; v[j1] = t1 + t2, v[j2] = t1 - t2; w *= wr; } } } if (rev) for (i = 0; i < (n); i++) v[i] *= 1.0 / n; return v; } vector<complex<double> > MultPoly(vector<complex<double> > P, vector<complex<double> > Q) { P = fft(P), Q = fft(Q); for (int i = 0; i < P.size(); i++) P[i] *= Q[i]; return fft(P, true); } int T, N; string S; int NG[505050]; const int prime_max = 700000; int NP, prime[100000]; vector<int> divp[prime_max]; map<int, int> M; void cprime() { for (int i = 2; i < prime_max; i++) if (divp[i].empty()) { prime[NP++] = i; for (signed long long j = i; j >= i && j < prime_max; j += i) divp[j].push_back(j / i); } } void solve() { int i, j, k, l, r, x, y; string s; cprime(); cin >> T; while (T--) { cin >> N >> S; int L = 1; while (L <= 2 * N) L *= 2; vector<complex<double> > VA(L, 0), KA(L, 0), VB(L, 0), KB(L, 0); for (i = 0; i < (N); i++) { if (S[i] == 'V') VA[i] = VB[N - 1 - i] = 1; if (S[i] == 'K') KA[i] = KB[N - 1 - i] = 1; } auto A = MultPoly(VA, KB); auto B = MultPoly(KA, VB); NG[N] = 0; for (i = 1; i < N; i++) { NG[i] = 0; if ((int)(A[N - 1 - i].real() + 0.2) > 0 || (int)(B[N - 1 - i].real() + 0.2) > 0) NG[i] = 1; } int cnt = 0; for (i = N; i >= 1; i--) { if (NG[i] == 0) cnt++; else { for (auto& e : divp[i]) NG[e] = 1; } } (void)printf("%d\n", cnt); for (i = 1; i <= N; i++) if (NG[i] == 0) (void)printf("%d%c", i, (i == N) ? '\n' : ' '); } } int main(int argc, char** argv) { string s; int i; if (argc == 1) ios::sync_with_stdio(false), cin.tie(0); for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n'; for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); solve(); return 0; }
19
#include <bits/stdc++.h> using namespace std; int main() { string s1, s, s2; while (cin >> s1 >> s) { int f = 0, l = s.size(); for (int i = 0; i < l; i++) if (s1[i] < s[i]) { f = 1; break; } else s2.push_back(s[i]); if (f) cout << "-1" << endl; else cout << s2 << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main(void) { long long n, m; cin >> n >> m; long long cnt = 0; for (long(i) = 0; (i) < (n); (i)++) { for (long(j) = 0; (j) < (m); (j)++) { if ((i + 1 + j + 1) % 5 == 0) { cnt += (m - (j + 1)) / 5; ++cnt; break; } } } cout << cnt << endl; }
3
#include <bits/stdc++.h> using namespace std; const int maxk = 51; struct matrix { bool e[maxk][maxk]; int sz; void print() { for (int i = 0, _n = (sz); i < _n; i++) { for (int j = 0, _n = (sz); j < _n; j++) { printf("%d", e[i][j]); } printf("\n"); } printf("\n"); } }; matrix operator*(const matrix &A, const matrix &B) { matrix ret; memset(&ret, 0, sizeof(ret)); ret.sz = A.sz; for (int i = 0, _n = (A.sz); i < _n; i++) for (int k = 0, _n = (A.sz); k < _n; k++) if (A.e[i][k]) for (int j = 0, _n = (A.sz); j < _n; j++) ret.e[i][j] ^= B.e[k][j]; return ret; } matrix one; matrix pow2(matrix M, long long deg) { matrix ret; memset(&ret, 0, sizeof(ret)); for (int i = 0, _n = (M.sz); i < _n; i++) ret.e[i][i] = 1; ret.sz = M.sz; while (deg) { if (deg & 1) ret = ret * M; M = M * M; deg >>= 1; } return ret; } long long my_rand() { long long ret = 0; for (int i = 0, _n = (4); i < _n; i++) ret = (ret << 15) + rand() % (1 << 15); return ret; } vector<long long> prime_div; int check_one(const matrix &tmp) { bool zero = 1; for (int i = 0, _n = (tmp.sz); i < _n; i++) for (int j = 0, _n = (tmp.sz); j < _n; j++) if (tmp.e[i][j]) zero = 0; if (zero) return 0; for (int i = 0, _n = (tmp.sz); i < _n; i++) for (int j = 0, _n = (tmp.sz); j < _n; j++) if (tmp.e[i][j] != (i == j)) return -1; return 1; } bool check(long long c, int k) { matrix cM; memset(&cM, 0, sizeof(cM)); cM.sz = k; for (int i = 0, _n = (k - 1); i < _n; i++) cM.e[i][i + 1] = 1; for (int i = 0, _n = (k); i < _n; i++) cM.e[k - 1][k - i - 1] = ((c & (1ll << i)) > 0); matrix tmp = pow2(cM, (1ll << k) - 1); long long m = (1ll << k) - 1; if (check_one(tmp) == 1) { for (int i = 0, _n = (prime_div.size()); i < _n; i++) { tmp = pow2(cM, m / prime_div[i]); if (check_one(tmp) != -1) return 0; } return 1; } return 0; } long long find_ans(int k) { prime_div.clear(); long long m = (1ll << k) - 1; for (long long q = 2; q * q <= m; q++) { if (m % q == 0) { while (m % q == 0) m /= q; prime_div.push_back(q); } } if (m > 1) prime_div.push_back(m); one.sz = k; long long c = 0; for (c = ((1ll << k) - 1); !check(c, k); c--) ; return c; } void generate_ans() { memset(&one, 0, sizeof(one)); for (int k = 0, _n = (50); k < _n; k++) one.e[k][k] = 1; for (int k = 2, _b = (50); k <= _b; k++) { long long c = find_ans(k); cout << c << "ll, " << endl; fprintf(stderr, "%d\n", k); } } long long const_ans[51] = { 0ll, 0ll, 3ll, 5ll, 9ll, 18ll, 33ll, 65ll, 142ll, 264ll, 516ll, 1026ll, 2089ll, 4109ll, 8213ll, 16385ll, 32790ll, 65540ll, 131091ll, 262163ll, 524292ll, 1048578ll, 2097153ll, 4194320ll, 8388621ll, 16777220ll, 33554467ll, 67108883ll, 134217732ll, 268435458ll, 536870953ll, 1073741828ll, 2147483735ll, 4294967337ll, 8589934707ll, 17179869186ll, 34359738427ll, 68719476767ll, 137438953521ll, 274877906952ll, 549755813916ll, 1099511627780ll, 2199023255583ll, 4398046511148ll, 8796093022258ll, 17592186044429ll, 35184372088983ll, 70368744177680ll, 140737488355419ll, 281474976710712ll, 562949953421326ll, }; int k; int main() { scanf("%d", &k); for (int i = 0, _n = (k); i < _n; i++) { if (i) printf(" "); printf("%d", (const_ans[k] & (1ll << i)) > 0); } printf("\n"); for (int i = 0, _n = (k - 1); i < _n; i++) printf("0 "); printf("1\n"); return 0; }
19
#include <bits/stdc++.h> using namespace std; const long long MAXN = (long long)1e5 + 8; const long long MOD = (long long)1e9 + 7; int main() { int i, j, k; long long M, N, x, y, p, q; long long K; cin >> N >> M >> K; string b[N]; char st[MAXN]; for (i = 0; i < N; i++) { scanf("%s", st); b[i].assign(st); } int a[N][M]; long long c = 0; memset(a, 0, sizeof(a)); for (i = 0; i < N; i++) for (j = 0; j < M; j++) if (b[i][j] == '.') a[i][j] = 1, c++; if (K == 1) { printf("%lld\n", c); return 0; } int h[N][M], v[N][M]; for (i = 0; i < N; i++) h[i][0] = a[i][0]; for (i = 0; i < N; i++) for (j = 1; j < M; j++) h[i][j] = h[i][j - 1] + a[i][j]; for (i = 0; i < M; i++) v[0][i] = a[0][i]; for (i = 1; i < N; i++) for (j = 0; j < M; j++) v[i][j] = v[i - 1][j] + a[i][j]; long long cnt = 0; for (i = 0; i < N; i++) { for (j = 0; j <= M - K; j++) { int r = j + K - 1; if (h[i][r] - h[i][j] + a[i][j] == K) cnt++; } } for (i = 0; i <= N - K; i++) { for (j = 0; j < M; j++) { int r = i + K - 1; if (v[r][j] - v[i][j] + a[i][j] == K) cnt++; } } printf("%lld\n", cnt); return 0; }
5
#include <bits/stdc++.h> using namespace std; struct ed { int u, v, cost, idx; ed() {} ed(int uu, int vv, int cc, int idx) : u(uu), v(vv), cost(cc), idx(idx) {} bool operator<(const ed& a) const { return cost < a.cost; } }; struct edge { int next, cost; edge() {} edge(int a, int c) : next(a), cost(c) {} }; struct node { int height; int par[23]; int chainNum; int heavyNode; int segNum; int cost[23]; int childcnt; }; int n, m; node info[200001]; int chainHead[200001]; vector<edge> adj[200001]; bool visited[200001]; void dfs(int cur, int height) { int cnt = 1; int maxV = 0; int maxnode = 0; for (auto& a : adj[cur]) { if (!visited[a.next]) { visited[a.next] = true; info[a.next].height = height + 1; info[a.next].par[0] = cur; info[a.next].cost[0] = a.cost; dfs(a.next, height + 1); if (info[a.next].childcnt > maxV) { maxV = info[a.next].childcnt; maxnode = a.next; } cnt += info[a.next].childcnt; } } info[cur].heavyNode = maxnode; info[cur].childcnt = cnt; return; } void processLca(int n) { for (int i = 1; i < 23; i++) for (int j = 1; j <= n; j++) { info[j].par[i] = info[info[j].par[i - 1]].par[i - 1]; info[j].cost[i] = max(info[info[j].par[i - 1]].cost[i - 1], info[j].cost[i - 1]); } } vector<int> seg; vector<int> lazy; void lazy_update(int idx, int s, int e) { if (lazy[idx] == (1987654321)) return; if (lazy[idx] < seg[idx]) { seg[idx] = lazy[idx]; lazy[idx] = (1987654321); if (s != e) { lazy[idx * 2] = min(lazy[idx * 2], seg[idx]); lazy[idx * 2 + 1] = min(lazy[idx * 2 + 1], seg[idx]); } } return; } void seg_update(int idx, int s, int e, int l, int r, int val) { lazy_update(idx, s, e); if (e < l || s > r) return; if (l <= s && e <= r) { seg[idx] = min(seg[idx], val); if (s != e) { lazy[idx * 2] = min(lazy[idx * 2], val); lazy[idx * 2 + 1] = min(lazy[idx * 2 + 1], val); } return; } int mid = (s + e) / 2; seg_update(idx * 2, s, mid, l, r, val); seg_update(idx * 2 + 1, mid + 1, e, l, r, val); return; } int seg_min(int idx, int s, int e, int l, int r) { lazy_update(idx, s, e); if (e < l || s > r) return (1987654321); if (l <= s && e <= r) return seg[idx]; int mid = (s + e) >> 1; int a = seg_min(idx * 2, s, mid, l, r); int b = seg_min(idx * 2 + 1, mid + 1, e, l, r); return min(a, b); } int chainCnt = 1; int segcnt = 1; int NodeNum[200001]; void HLD(int cur) { NodeNum[segcnt] = cur; info[cur].segNum = segcnt++; info[cur].chainNum = chainCnt; if (info[cur].heavyNode != 0) { visited[info[cur].heavyNode] = true; HLD(info[cur].heavyNode); } for (auto& a : adj[cur]) { if (!visited[a.next]) { visited[a.next] = true; chainCnt++; chainHead[chainCnt] = a.next; HLD(a.next); } } return; } int getLca(int a, int b) { int ha = info[a].height; int hb = info[b].height; if (ha != hb) { int cha; if (ha > hb) { swap(hb, ha); swap(a, b); } cha = hb - ha; for (int i = 22; i >= 0; i--) { if (cha & (1 << i)) { b = info[b].par[i]; } } } if (a == b) return a; else { for (int i = 22; i >= 0; i--) { if (info[a].par[i] != info[b].par[i]) a = info[a].par[i], b = info[b].par[i]; } } return info[a].par[0]; } int getMax(int lca, int b) { int hlc = info[lca].height; int hb = info[b].height; int cha = hb - hlc; int maxV = 0; for (int i = 23; i >= 0; i--) { if (cha & (1 << i)) { maxV = max(maxV, info[b].cost[i]); b = info[b].par[i]; } } return maxV; } void update(int lca, int b, int v) { while (info[lca].chainNum != info[b].chainNum) { seg_update(1, 1, n, info[chainHead[info[b].chainNum]].segNum, info[b].segNum, v); b = info[chainHead[info[b].chainNum]].par[0]; } if (b == lca) return; seg_update(1, 1, n, info[lca].segNum + 1, info[b].segNum, v); } int getMin(int a) { return seg_min(1, 1, n, info[a].segNum, info[a].segNum); } ed edL[200001]; int par[200001]; int rr[200001]; bool inMst[200001]; int find(int a) { if (par[a] == a) return a; return par[a] = find(par[a]); } void merge(int a, int b) { int pa = find(a); int pb = find(b); if (rr[pa] > rr[pb]) swap(pa, pb); par[pa] = pb; if (rr[pa] == rr[pb]) rr[pb]++; } void MakeKruscal() { for (int i = 0; i < m; i++) { if (find(edL[i].u) == find(edL[i].v)) inMst[edL[i].idx] = false; else { inMst[edL[i].idx] = true; merge(edL[i].u, edL[i].v); adj[edL[i].u].push_back(edge(edL[i].v, edL[i].cost)); adj[edL[i].v].push_back(edge(edL[i].u, edL[i].cost)); } } } int ans[200001]; int main(void) { scanf("%d %d", &n, &m); for (int i = 0, a, b, c; i < m; i++) { scanf("%d %d %d", &a, &b, &c); edL[i] = ed(a, b, c, i); } sort(edL, edL + m); for (int i = 1; i <= n; i++) par[i] = i; MakeKruscal(); visited[1] = true; info[1].height = 1; dfs(1, 1); processLca(n); memset(visited, false, sizeof(visited)); visited[1] = true; chainHead[chainCnt] = 1; HLD(1); seg.resize(533333, (1987654321)); lazy.resize(533333, (1987654321)); for (int i = 0; i < m; i++) { if (!inMst[edL[i].idx]) { int lc = getLca(edL[i].u, edL[i].v); ans[edL[i].idx] = max(getMax(lc, edL[i].u), getMax(lc, edL[i].v)); update(lc, edL[i].u, edL[i].cost); update(lc, edL[i].v, edL[i].cost); } } for (int i = 0; i < m; i++) { if (inMst[edL[i].idx]) { if (info[edL[i].u].par[0] == edL[i].v) ans[edL[i].idx] = getMin(edL[i].u); else ans[edL[i].idx] = getMin(edL[i].v); } } for (int i = 0; i < m; i++) { if (ans[i] == (1987654321)) printf("-1 "); else printf("%d ", ans[i] - 1); } return 0; }
19
#include <bits/stdc++.h> using namespace std; int main() { unsigned long long l, r, k, ans = 1; cin >> l >> r >> k; bool flag = false; while (ans <= r) { if (ans >= l && ans <= r) { printf("%lld ", ans); flag = true; } if (r / ans < k) break; ans = ans * k; } if (flag == false) cout << -1 << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int n, q, ans, t, i, j, f1[1001], f2[1001], a[1001][1001]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%d", &a[i][j]); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) ans += (a[i][j] & a[j][i]); ans %= 2; scanf("%d", &q); for (int k = 1; k <= q; k++) { scanf("%d", &t); if (t == 1) { scanf("%d", &i); ans ^= 1; } if (t == 2) { scanf("%d", &j); ans ^= 1; } if (t == 3) { printf("%d", ans % 2); } } }
8
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { string a, b; cin >> a >> b; if (a == b) cout << "-1"; else cout << max(a.size(), b.size()); return 0; }
2
#include <bits/stdc++.h> using namespace std; bool ok = true; stringstream ss; const long long N = (long long)1e9 + 7; long double ans[1000007]; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long test; cin >> test; while (test--) { long long a, b; cin >> a >> b; long long delta = abs(a - b); long long ans = delta / 5 + delta % 5 / 2 + delta % 5 % 2; cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> const int N = 210; int n, t, m; long long x, a[N], b[N], c[N], f[N][N][2], g[N][N][2]; inline void upd(long long &a, long long b) { if (b < a) a = b; } inline long long ceildiv(long long a, long long b) { return (a + b - 1) / b; } int main() { memset(f, 63, sizeof(f)), memset(g, 63, sizeof(g)); memset(f[0], 0, sizeof(f[0])), memset(g[0], 0, sizeof(g[0])); scanf("%d%d%d", &n, &t, &m); for (int i = 1; i <= n; i++) scanf("%lld%lld%lld", &a[i], &b[i], &c[i]); ++n, a[n] = c[n] = 1e12; for (int i = 2; i <= n; i++) a[i] += a[i - 1], b[i] += b[i - 1]; for (int i = 1; i <= n; i++) for (int j = 0; j <= t; j++) for (int k = 0; k < 2; k++) { if (k * (a[i] - a[i - 1]) + j * (b[i] - b[i - 1]) <= c[i] && f[i - 1][j][k] <= 1e18) { upd(f[i][j][k], f[i - 1][j][k]); x = ceildiv(k * a[i - 1] + j * b[i - 1], m); if (x * m <= k * a[i] + j * b[i]) upd(g[i][j][k], x); } for (int r = 0; r < j; r++) if (g[i][r][k] < 1e18 && f[i - 1][j - r][0] < 1e18) { x = std::max( ceildiv(k * a[i] + (r - j) * b[i - 1] + j * b[i] - c[i], m), g[i][r][k]); if (x * m > k * a[i] + r * b[i]) continue; upd(f[i][j][k], x + f[i - 1][j - r][0]); x = (k * a[i - 1] + j * b[i - 1] + x * m - k * a[i - 1] - r * b[i - 1] + m - 1) / m; if (x * m > k * a[i] + j * b[i]) continue; upd(g[i][j][k], x); } } printf("%lld\n", f[n][t][1]); }
26
#include <bits/stdc++.h> using namespace std; queue<long long> q; map<pair<long long, long long>, long long> m; pair<long long, long long> p; long long n, u, v, cnt[1000000], a[1000000], pos; signed main() { scanf("%lld", &n); for (long long i = 1; i < n; i++) { scanf("%lld %lld", &u, &v); cnt[u]++; cnt[v]++; p.first = u; p.second = v; m[p] = 1; p.first = v; p.second = u; m[p] = 1; } for (long long i = 1; i <= n; i++) { scanf("%lld", &a[i]); } if (a[1] != 1) printf("No"); else { pos = 1; q.push(1); while (pos < n) { u = q.front(); q.pop(); p.first = u; for (long long i = 1; i <= cnt[u]; i++) { p.second = a[++pos]; if (m[p] != 1) { printf("No"); return 0; } else { cnt[a[pos]]--; q.push(a[pos]); } } } printf("Yes"); } }
9
#include <bits/stdc++.h> using namespace std; string sconvert(long long int n) { stringstream ss; ss << n; string str = ss.str(); return str; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second > b.second); } template <typename T> void imax(T &x, T y) { x = max(x, y); } template <typename T> void imin(T &x, T y) { x = min(x, y); } void single() { long long int n; cin >> n; long long int m; cin >> m; vector<long long int> stacks(n); long long int total = 0; for (long long int &x : stacks) { cin >> x; total += x; } sort((stacks).rbegin(), (stacks).rend()); long long int answer = 0; for (int i = 0; i < n - 1; i++) { if (stacks[i + 1] > stacks[i]) { stacks[i + 1] = stacks[i]; } if (stacks[i] == stacks[i + 1]) { if (stacks[i + 1] != 1) stacks[i + 1]--; } } for (int i = 0; i < n - 1; i++) { if (stacks[i] == 1) { answer += 1; } else { answer += (stacks[i] - stacks[i + 1]); } } answer += stacks[n - 1]; cout << total - answer << '\n'; return; } void multiple() { int asdf; cin >> asdf; while (asdf--) { single(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); ; single(); }
6
#include <bits/stdc++.h> using namespace std; inline int qr() { int f = 0, fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fu = -1; c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + c - 48; c = getchar(); } return f * fu; } int T, n; int calc(int x) { if (x < 2) return 0; int i = 1; while (1ll * i * (i - 1) / 2 + 1ll * i * (i + 1) <= x) i++; return (i - 1) * (i - 2) / 2 + i * (i - 1); } int main() { T = qr(); while (T--) { n = qr(); int cnt = 0; while (true) { int res = calc(n); if (res) { cnt++; n -= res; } else break; } printf("%d\n", cnt); } return 0; }
3
#include <bits/stdc++.h> using namespace std; set<int> b[100020]; vector<pair<int, int> > a[100020]; int d[100020]; int v[200020]; priority_queue<pair<int, int> > q; int n, m, x, y, z; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d%d", &x, &y, &z); a[x].push_back(pair<int, int>(y, z)); a[y].push_back(pair<int, int>(x, z)); } for (int i = 1; i <= n; i++) { for (scanf("%d", &x); x--;) { scanf("%d", &y); b[i].insert(y); } } memset(d, 0x3f, sizeof d); d[1] = 0; q.push(pair<int, int>(0, 1)); while (q.size()) { pair<int, int> u = q.top(); q.pop(); if (v[u.second]) continue; v[u.second] = 1; int w = -u.first; while (b[u.second].count(w)) w++; for (__typeof(a[u.second].begin()) i = a[u.second].begin(); i != a[u.second].end(); i++) if (d[i->first] > w + i->second) { d[i->first] = w + i->second; q.push(pair<int, int>(-d[i->first], i->first)); } } printf("%d", d[n] != d[0] ? d[n] : -1); }
9
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 1000005; vector<int> g[maxn]; int pa[maxn]; int dep[maxn]; vector<int> v[maxn]; void dfs(int now) { v[dep[now]].push_back(now); for (auto &i : g[now]) { if (dep[i]) { continue; } pa[i] = now; dep[i] = dep[now] + 1; dfs(i); } } void solve() { int n, m; cin >> n >> m; for (int i = 1; i < n + 1; i++) { g[i].clear(); dep[i] = 0; pa[i] = 0; v[i].clear(); } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } dep[1] = 1; dfs(1); int tar = (n + 1) / 2; for (int i = 1; i < n + 1; i++) { if (dep[i] >= tar) { vector<int> ans{i}; while (ans.back() != 1) { ans.push_back(pa[ans.back()]); } cout << "PATH\n"; cout << ans.size() << '\n'; for (auto &i : ans) { cout << i << ' '; } cout << '\n'; return; } } cout << "PAIRING\n"; vector<pair<int, int>> ans; for (int i = 1; i < tar + 1; i++) { for (int j = 0; j + 1 < v[i].size(); j += 2) { ans.push_back({v[i][j], v[i][j + 1]}); } } cout << ans.size() << '\n'; for (auto &i : ans) { cout << i.first << ' ' << i.second << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int c = 1; int t; if (!c) { t = 1; } else { cin >> t; } while (t--) { solve(); } }
18
#include <bits/stdc++.h> using namespace std; const int maxn = 200500; int n; string s1, s2; long long s[maxn]; double get(const vector<int> &a, const vector<int> &b) { for (int i = 0; i < (int)(n); i++) { s[i] = b[i] ? (i + 1) : 0; if (i) s[i] += s[i - 1]; } double res = 0; for (int i = 0; i < (int)(n); i++) if (i && a[i]) res += s[i - 1] * (n - i); return res; } void solve() { double s = 0; vector<int> a(n), b(n); for (int I = 0; I < (int)(2); I++) { for (char c = 'A'; c <= 'Z'; c++) { for (int i = 0; i < (int)(n); i++) a[i] = s1[i] == c; for (int i = 0; i < (int)(n); i++) b[i] = s2[i] == c; s += get(a, b); } swap(s1, s2); } for (int i = 0; i < (int)(n); i++) if (s1[i] == s2[i]) s += double(i + 1) * (n - i); cout.precision(10); cout << fixed; cout << (double)s / ((double)n * (n + 1) * (2 * n + 1) / 6.) << endl; } int main() { cin >> n >> s1 >> s2; solve(); return 0; }
12
#include <bits/stdc++.h> using namespace std; inline string GetString() { char GS[1000005]; scanf("%s", GS); string ret = GS; return ret; } inline char getc() { char c = ' '; while (c == ' ' || c == '\t' || c == '\r' || c == '\n') c = getchar(); return c; } int n, m, i; vector<int> adj[100005]; int v[100005]; int rrank[100005]; int parent[100005]; bool done[100005]; int size[100005]; int root(int x) { if (parent[x] == x) return parent[x]; parent[x] = root(parent[x]); return parent[x]; } void join(int x, int y) { x = root(x); y = root(y); if (rrank[x] > rrank[y]) { swap(x, y); } if (rrank[x] == rrank[y]) ++rrank[y]; size[y] += size[x]; parent[x] = y; } int ind[100005]; bool cmp(int a, int b) { return v[a] > v[b]; } int main() { scanf("%d %d", &n, &m); for (i = 0; i < n; ++i) scanf("%d", &v[i]); for (i = 0; i < m; ++i) { int a, b; scanf("%d %d", &a, &b); --a; --b; adj[a].push_back(b); adj[b].push_back(a); } for (i = 0; i < n; ++i) ind[i] = i; sort(ind, ind + n, cmp); for (i = 0; i < n; ++i) { parent[i] = i; rrank[i] = 0; size[i] = 1; } long long sum = 0; for (i = 0; i < n; ++i) { done[ind[i]] = true; for (int j = 0; j < (int)(adj[ind[i]]).size(); ++j) { if (done[ind[i]] && done[adj[ind[i]][j]]) { int ri = root(ind[i]); int rj = root(adj[ind[i]][j]); if (ri != rj) { sum += (long long)size[ri] * (long long)size[rj] * (long long)v[ind[i]]; join(ri, rj); } } } } printf("%.10lf\n", sum / (((long long)n * ((long long)n - 1)) / 2.0)); }
11
#include <bits/stdc++.h> using namespace std; struct nod { int l, r; mutable long long v; inline bool operator<(const nod b) const { return l < b.l; } nod(int a, int b = -1, long long c = -1) { l = a, r = b, v = c; } }; int n; set<nod> li; set<nod>::iterator split(int p) { set<nod>::iterator a = li.lower_bound(nod(p, -1)); if (a != li.end() && a->l == p) return a; --a; int l = a->l, r = a->r; long long v = a->v; li.erase(a); li.insert(nod(l, p - 1, v)); return li.insert(nod(p, r, v)).first; } void add(int l, int r, long long x) { set<nod>::iterator b = split(r + 1), a = split(l); for (set<nod>::iterator i = a; i != b; ++i) { (i->v) += x; } } inline void assign(int l, int r, long long x) { set<nod>::iterator b = split(r + 1), a = split(l); li.erase(a, b); li.insert(nod(l, r, x)); } long long g_rank(int l, int r, int x) { x--; vector<pair<long long, int> > v; set<nod>::iterator b = split(r + 1), a = split(l); for (; a != b; a++) { v.push_back(make_pair(a->v, (a->r) - (a->l) + 1)); } sort(v.begin(), v.end()); vector<pair<long long, int> >::iterator iter = v.begin(); for (; x >= iter->second; x -= iter->second, iter++) ; return iter->first; } long long gsc(long long a, long long b, long long mod) { long long ans = 0; a %= mod, b %= mod; while (b) { if (b & 1) { ans = (ans + a) % mod; } a = (a + a) % mod; b >>= 1; } return ans; } long long ksm(long long a, long long b, long long mod) { long long ans = 1; a %= mod; while (b) { if (b & 1) { ans = (ans * a) % mod; } a = (a * a) % mod; b >>= 1; } return ans; } long long get_ans(int l, int r, long long x, long long y) { set<nod>::iterator b = split(r + 1), a = split(l); long long ans = 0; for (; a != b; a++) { ans += gsc(ksm(a->v, x, y), (a->r) - (a->l) + 1, y); ans %= y; } return ans; } void print(int p) { set<nod>::iterator a = split(p); cout << a->v << endl; return; } int m; long long seed, vmax; inline long long rnd() { long long ret = seed; seed = (seed * 7 + 13) % 1000000007; return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> seed >> vmax; for (int i = 1; i <= n; i++) { li.insert(nod(i, i, (rnd() % vmax) + 1)); } for (int i = 0; i < m; i++) { int op = (rnd() % 4) + 1; int a = (rnd() % n) + 1; int b = (rnd() % n) + 1; if (a > b) swap(a, b); if (op == 1) { long long c = (rnd() % vmax) + 1; add(a, b, c); } else if (op == 2) { long long c = (rnd() % vmax) + 1; assign(a, b, c); } else if (op == 3) { int c = (rnd() % (b - a + 1)) + 1; cout << g_rank(a, b, c) << "\n"; } else if (op == 4) { long long c = (rnd() % vmax) + 1; long long d = (rnd() % vmax) + 1; cout << get_ans(a, b, c, d) << "\n"; } } return 0; }
18
#include <bits/stdc++.h> const int N = 220; const long long mod = 998244353; using namespace std; int n; int m; int a[N][N]; pair<string, string> solve(int k) { string s{}; string t{}; s.reserve(m); t.reserve(m); for (int i = 1; i <= m; i++) { int need = (i <= k) ^ 1; int one = a[1][i] != need; t += (char)('0' + one); } vector<pair<int, int> > v; if (k < m) { v.push_back({0, 1}); } else { v.push_back({0, 0}); } for (int i = 2; i <= n; i++) { vector<int> g; for (int j = 1; j <= m; j++) { int x = a[i][j] ^ (t[j - 1] - '0'); if (g.empty() || x != g.back()) { g.push_back(x); } } if (g.size() == 1) { g.push_back(g.back()); } if (g.size() > 2) { return {"", ""}; } v.push_back({g[0], g[1]}); } int cnt = 0; for (auto p : v) { cnt += p.first ^ p.second; } if (cnt > 1) { return {"", ""}; } int cur = 0; for (auto p : v) { if (cur == 0) { if (p.first) { s += "1"; p.second ^= 1; } else { s += "0"; } cur = p.second; } else { if (p.first) { s += "0"; } else { s += "1"; p.second ^= 1; } assert(p.second == 1); } } return {s, t}; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> a[i][j]; } } for (int i = 1; i <= m; i++) { auto res = solve(i); if (res.first != "") { cout << "YES" << "\n"; cout << res.first << "\n"; cout << res.second << "\n"; return 0; } } cout << "NO" << "\n"; }
14
#include <bits/stdc++.h> using namespace std; struct directie { int x, y; }; directie v[1008]; int n; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &v[i].x, &v[i].y); v[n + 1] = v[1]; v[n + 2] = v[2]; v[n + 3] = v[3]; int cate = 0; for (int i = 1; i <= n; i++) { if (v[i + 1].y > v[i].y && v[i + 1].x == v[i].x) { if (v[i + 2].y == v[i + 1].y && v[i + 2].x > v[i + 1].x) { if (v[i + 3].x == v[i + 2].x && v[i + 3].y > v[i + 2].y) cate++; } else if (v[i + 3].x == v[i + 2].x && v[i + 3].y < v[i + 2].y) cate++; } if (v[i + 1].y == v[i].y && v[i + 1].x > v[i].x) { if (v[i + 2].x == v[i + 1].x && v[i + 2].y > v[i + 1].y) { if (v[i + 3].y == v[i + 2].y && v[i + 3].x < v[i + 2].x) cate++; } else if (v[i + 3].y == v[i + 2].y && v[i + 3].x > v[i + 2].x) cate++; } if (v[i + 1].x == v[i].x && v[i + 1].y < v[i].y) { if (v[i + 2].y == v[i + 1].y && v[i + 2].x < v[i + 1].x) { if (v[i + 3].x == v[i + 2].x && v[i + 3].y < v[i + 2].y) cate++; } else if (v[i + 3].x == v[i + 2].x && v[i + 3].y > v[i + 2].y) cate++; } if (v[i + 1].y == v[i].y && v[i + 1].x < v[i].x) { if (v[i + 2].x == v[i + 1].x && v[i + 2].y < v[i + 1].y) { if (v[i + 3].y == v[i + 2].y && v[i + 3].x > v[i + 2].x) cate++; } else if (v[i + 3].y == v[i + 2].y && v[i + 3].x < v[i + 2].x) cate++; } } printf("%d", cate); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> m; vector<int> used(101000, 1); int r = 0; for (int i = 2; i < used.size(); ++i) { if (used[i] > 1) continue; m[i] = r; used[i] = i; ++r; for (int j = i * i; j < used.size(); j += i) used[j] = i; } int n, k; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; map<vector<pair<int, int> >, int> ss; long long ans = 0; for (int i = 0; i < n; ++i) { int q = v[i]; vector<pair<int, int> > a; vector<pair<int, int> > a1; map<int, int> m1; while (q != 1) { m1[used[q]]++; q /= used[q]; } for (auto l : m1) { if (l.second % k == 0) continue; a.push_back({l.first, l.second % k}); a1.push_back({l.first, (k - l.second % k) % k}); } ans += int(ss[a1]); ss[a]++; } cout << ans; return 0; }
10
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; struct node { int nxt, ed, len; } e[2 * N]; int head[N], num, vis[2 * N], p[40], ans, n, used[N], dp[N]; vector<int> v; void add(int x, int y, int z) { e[num].ed = y; e[num].len = z; e[num].nxt = head[x]; head[x] = num++; } void dfs(int u, int t) { used[u] = 1; dp[u] = t; if (u == n) { ans = t; } for (int i = head[u]; ~i; i = e[i].nxt) { int vv = e[i].ed; if (vis[i]) continue; vis[i] = vis[i ^ 1] = 1; if (used[vv]) v.push_back(t ^ e[i].len ^ dp[vv]); else { dfs(vv, t ^ e[i].len); } } } int main() { int m, x, y, z; memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d%d", &x, &y, &z); add(x, y, z), add(y, x, z); } dfs(1, 0); for (int i = 0; i < v.size(); i++) { for (int j = 30; j >= 0; j--) { if (!(v[i] >> j)) continue; if (!p[j]) { p[j] = v[i]; break; } v[i] ^= p[j]; } } for (int i = 30; i >= 0; i--) { if (ans & (1 << i)) { ans ^= p[i]; } } printf("%d\n", ans); return 0; }
15
#include <bits/stdc++.h> template <class T> void smin(T& a, T val) { if (a > val) a = val; } template <class T> void smax(T& a, T val) { if (a < val) a = val; } using namespace std; int a[1000010], n; string s; int c; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; cin >> s; int x = 0, y = 0; int f = -1; long long ans = 0ll; for (int i = 0; i < n; ++i) { if (s[i] == 'U') { y++; } else { x++; } if (x > y) { if (f == 1) { ++ans; } f = 0; } else if (x < y) { if (f == 0) { ++ans; } f = 1; } } cout << ans; return 0; }
1
#include <bits/stdc++.h> template <typename T> inline void read(T &x) { char c; bool nega = 0; while ((!isdigit(c = getchar())) && (c != '-')) ; if (c == '-') { nega = 1; c = getchar(); } x = c - 48; while (isdigit(c = getchar())) x = x * 10 + c - 48; if (nega) x = -x; } template <typename T> inline void writep(T x) { if (x > 9) writep(x / 10); putchar(x % 10 + 48); } template <typename T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } writep(x); putchar(' '); } template <typename T> inline void writeln(T x) { write(x); putchar('\n'); } using namespace std; int t, x, y, d, m, a[100005], b[100005]; long long suma[100005], sumb[100005], ans; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(); if (fopen(".inp", "r")) freopen(".inp", "r", stdin); cin >> t >> d >> m; for (int i = (1); i <= (t); ++i) { int u; cin >> u; if (u > m) b[++y] = u; else a[++x] = u; } sort(a + 1, a + x + 1); sort(b + 1, b + y + 1); for (int i = (1); i <= (x); ++i) { suma[i] = suma[i - 1] + a[i]; } for (int i = (1); i <= (y); ++i) { sumb[i] = sumb[i - 1] + b[i]; } for (int i = (x + 1); i >= (1); --i) { long long temp = suma[x] - suma[i - 1]; int remain = y + i - 1; if (!remain) { ans = max(ans, temp); continue; } int need = (remain - 1) / (d + 1) + 1; if (need <= y) { temp += sumb[y] - sumb[y - need]; } else { need -= y; temp += sumb[y]; temp += suma[i - 1] - suma[i - 1 - need]; } ans = max(ans, temp); } cout << ans; return 0; }
10
#include <bits/stdc++.h> using namespace std; template <typename T> void _R(T &x) { cin >> x; } template <typename T> void _R(vector<T> &x) { for (auto it = x.begin(); it != x.end(); it++) { _R(*it); } } void R() {} template <typename T, typename... K> void R(T &head, K &...tail) { _R(head); R(tail...); } template <typename T> void _W(const T &x, const char c) { cout << x; } template <typename T> void _W(const vector<T> &x, const char c) { for (auto it = x.cbegin(); it != x.cend(); it++) { if (it != x.cbegin()) putchar(c); _W(*it, c); } } void W() {} template <typename T, typename... K> void W(const T &head, const K &...tail) { _W(head, ' '); cout << (sizeof...(tail) ? ' ' : '\n') << flush; W(tail...); } const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230; template <typename T> inline T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> inline void amin(T &x, T y) { if (x > y) x = y; } template <typename T> inline void amax(T &x, T y) { if (x < y) x = y; } template <typename A, typename B> class comp { public: bool operator()(const pair<A, B> &a, const pair<A, B> &b) { if (a.first != b.first) return a.first < b.first; else return a.first > b.first; } }; int n; vector<vector<pair<int, int> > > tree; vector<int> a; vector<int> vis; vector<int> subtree; long long ans = 0; void preprocess(void) { return; } void dfs(int u, int p, long long d) { subtree[u] = 1; if (d > a[u]) { vis[u] = 1; } for (auto v : (tree[u])) { if (v.first == p) { continue; } dfs(v.first, u, v.second + ((d > 0) ? d : 0)); subtree[u] += subtree[v.first]; } return; } void dfs2(int u, int p) { for (auto v : (tree[u])) { if (v.first == p) { continue; } if (vis[v.first] == 1) { ans += subtree[v.first]; } else { dfs2(v.first, u); } } return; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.precision(20); preprocess(); int teeee; teeee = 1; for (int zeeee = (1); zeeee <= (teeee); zeeee++) { R(n); vis.assign(n, 0), a.resize(n), subtree.resize(n), tree.resize(n); R(a); for (int i = (0); i <= ((int)(n - 1) - 1); i++) { int p, c; R(p, c); p--; tree[i + 1].push_back({p, c}); tree[p].push_back({i + 1, c}); }; ; dfs(0, -1, 0LL); ; dfs2(0, -1); W(ans); } cerr << (clock() / (double)CLOCKS_PER_SEC) << endl; return 0; }
8
// frequencyproblem.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int main() { long long n; cin >> n; long long *a= new long long[n]; long long *b = new long long[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } vector<pair<long long, long long>>x1, x2, y1, y2; long long vys = 0; for (int i = 0; i < n; i++) { vys += abs(a[i] - b[i]); if (a[i] < b[i]) { x1.push_back({ a[i],b[i] }); x2.push_back({ b[i],a[i] }); } else { y1.push_back({ a[i],b[i] }); y2.push_back({ b[i],a[i] }); } } long long fin = vys; set<long long>s1; sort(x1.begin(), x1.end()); sort(y2.begin(), y2.end()); long long pocet = 0; for (auto i : x1) { while (pocet < y2.size() && y2[pocet].first <= i.first) { s1.insert(y2[pocet].second); pocet++; } if (s1.size() > 0) { long long last = *s1.rbegin(); fin = min(fin, vys - 2 * (min(i.second, last) - i.first)); } } set<long long>s2; sort(x1.begin(), x1.end()); sort(y2.begin(), y2.end()); long long pocet2 = 0; for (auto i : y2) { while (pocet2 < x1.size() && x1[pocet2].first <= i.first) { s2.insert(x1[pocet2].second); pocet2++; } if (s2.size() > 0) { long long int last; last = *s2.rbegin(); fin = min(fin, vys - 2 * (min(last, i.second) - i.first)); } } cout << fin << endl; return 0; //std::cout << "Hello World!\n"; } // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu // Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
17
#include <bits/stdc++.h> using namespace std; int n, m, q, intersection[500][100100], HeavyN[100100]; bool Light[100100], Heavyin[500][100100]; long long ara[100100], HHupdate[500], HLupdate[500], HeavySum[500]; vector<int> Set[100100], HeavyS; int main() { cin >> n >> m >> q; int sq = sqrt(n); for (int i = 0; i < n; i++) { cin >> ara[i]; } int totelem, index; for (int i = 0; i < m; i++) { cin >> totelem; if (totelem <= sq) { Light[i] = 1; } else HeavyS.push_back(i), HeavyN[i] = HeavyS.size() - 1; for (int j = 0; j < totelem; j++) { cin >> index; index--; Set[i].push_back(index); if (!Light[i]) { Heavyin[HeavyS.size() - 1][index] = 1; HeavySum[HeavyS.size() - 1] += ara[index]; } } } for (int i = 0; i < HeavyS.size(); i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < Set[j].size(); k++) { if (Heavyin[i][Set[j][k]]) intersection[i][j]++; } } } string s; long long Qs, val; for (int i = 0; i < q; i++) { long long sum = 0; cin >> s >> Qs; Qs--; if (Light[Qs]) { if (s == "+") { cin >> val; for (int i = 0; i < Set[Qs].size(); i++) ara[Set[Qs][i]] += val; for (int i = 0; i < HeavyS.size(); i++) { HLupdate[i] += (val * intersection[i][Qs]); } } else { for (int i = 0; i < Set[Qs].size(); i++) sum += ara[Set[Qs][i]]; for (int i = 0; i < HeavyS.size(); i++) sum += (HHupdate[i] * intersection[i][Qs]); } } else { if (s == "+") { cin >> val; HHupdate[HeavyN[Qs]] += val; } else { for (int i = 0; i < HeavyS.size(); i++) { if (HeavyS[i] != Qs) { sum += (HHupdate[i] * intersection[i][Qs]); } } sum += (HHupdate[HeavyN[Qs]] * (Set[Qs].size())); sum += HLupdate[HeavyN[Qs]]; sum += HeavySum[HeavyN[Qs]]; } } if (s == "?") cout << sum << endl; } }
17
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; if (a + c < b) { cout << "-"; return 0; } if (b + c < a) { cout << "+"; return 0; } if (a == b && c == 0) { cout << "0"; return 0; } cout << "?"; }
0
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; if (a % 2 != 0) cout << 0 << endl; else { int b = a / 2; if (b % 2 == 0) cout << (b - 2) / 2 << endl; else cout << (b - 1) / 2 << endl; } }
2
#include <bits/stdc++.h> using namespace std; long long pwr(long long base, long long p, long long mod = (1000000007LL)) { long long ans = 1; while (p) { if (p & 1) ans = (ans * base) % mod; base = (base * base) % mod; p /= 2; } return ans; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int n, state[200005], arr[200005]; vector<int> adj[200005]; bool done[200005], impossible; vector<int> visited; pair<int, int> ans[100005]; void dfs(int v) { done[v] = true; visited.push_back(v); for (auto vv : adj[v]) if (!done[vv]) { state[vv] = 3 - state[v]; dfs(vv); } else if (state[vv] == state[v]) impossible = true; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int b, g; scanf("%d%d", &b, &g); arr[b] = i; arr[g] = -i; adj[b].push_back(g); adj[g].push_back(b); } for (int i = 1; i <= 2 * n; i += 2) { adj[i].push_back(i + 1); adj[i + 1].push_back(i); } for (int i = 1; i <= 2 * n; i++) { if (done[i]) continue; visited.clear(); state[i] = 1; impossible = false; dfs(i); if (!impossible) continue; while (!visited.empty()) { done[visited.back()] = false; state[visited.back()] = 0; visited.pop_back(); } state[i] = 2; impossible = false; dfs(i); if (impossible) { printf("-1\n"); return 0; } } for (int i = 1; i <= 2 * n; i++) if (arr[i] > 0) ans[arr[i]].first = state[i]; else ans[-arr[i]].second = state[i]; for (int i = 1; i <= n; i++) printf("%d %d\n", ans[i].first, ans[i].second); return 0; }
18
#include <bits/stdc++.h> using namespace std; struct point { double x, y, vl; bool operator<(const point &temp) const { return vl < temp.vl; } }; point pt[110001]; int n, l[22222], r[22222]; double a, ans; int main() { int i, j, s, p, q; double xx, yy, k, b, dis; scanf("%d%lf", &n, &a); for (i = 0; i < n; i++) scanf("%lf%lf", &pt[i].x, &pt[i].y); ans = 4 * a * a; for (i = 0; i < n; i++) pt[i].vl = (2 * (pt[i].y * pt[i].y + a * a + pt[i].x * pt[i].x) - sqrt(4 * (pt[i].y * pt[i].y + a * a - pt[i].x * pt[i].x) * (pt[i].y * pt[i].y + a * a - pt[i].x * pt[i].x) + 16 * pt[i].x * pt[i].x * pt[i].y * pt[i].y)); sort(pt, pt + n); for (i = 0; i < n && i <= 500 && pt[i].vl < ans; i++) { for (j = i; j < n && pt[j].vl < ans && j <= 500; j++) { dis = (pt[j].x * pt[i].y - pt[i].x * pt[j].y); dis = dis * dis + a * a * (pt[j].x - pt[i].x) * (pt[j].x - pt[i].x); dis /= ((pt[i].x - pt[j].x) * (pt[i].x - pt[j].x) + (pt[i].y - pt[j].y) * (pt[i].y - pt[j].y)); if (ans > 4 * dis) ans = 4 * dis; } } printf("%.20f\n", sqrt(ans)); }
24
#include <bits/stdc++.h> using namespace std; int n; int flag[10]; int chk(int x) { while (x > 0) { if (flag[x % 10] == 1) return 1; x /= 10; } return 0; } int main() { int t; cin >> n; for (int(i) = 0; (i) < (10); (i)++) flag[i] = 0; int cop = n; while (cop > 0) { int dig = cop % 10; flag[dig] = 1; cop /= 10; } int lim = sqrt(n); int ans = 0; for (int i = 1; i <= lim; i++) { if (n % i == 0) { if (chk(i)) { ans++; } if ((i != n / i) && chk(n / i)) { ans++; } } } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, d; cin >> n; d = n / 2; cout << d << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 10000003; int ppp[MAXN]; int phi[MAXN]; int fen[MAXN]; void upd(int x) { for (int s = x; s < MAXN; s += s & -s) fen[s]++; } int que(int x) { int ans = 0; for (int s = x; s > 0; s -= s & -s) ans += fen[s]; return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) phi[i] = 1; for (int i = 2; i <= n; i++) { if (ppp[i] == 0) { for (int j = i; j <= n; j += i) { if (ppp[j] == 0) ppp[j] = i; int k = 1; int x = j; while (x % i == 0) { x /= i; k *= i; } phi[j] *= k - k / i; } } } long long ans = 0; for (int m = 2; m <= n; m++) { if (ppp[m] * 2 > n) continue; int nans = 0; int tot = m - 2; nans += m - phi[m] - 1; tot -= m - phi[m] - 1; int q = que(n / ppp[m]) - (m - phi[m] - 1); tot -= q; nans += q * 2; tot -= m - 2 - que(n / 2); nans += tot * 3; upd(ppp[m]); ans += nans; } cout << ans << '\n'; return 0; }
19
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 + 10; int a[10][MAXN]; int b[10][MAXN]; int dp[MAXN] = {}; int n, m; bool check(int x, int y) { for (int i = 0; i < m; i++) { if (b[i][x] < b[i][y]) return 0; } return 1; } int main() { scanf("%d%d", &n, &m); int ans = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { scanf("%d", &a[i][j]); b[i][a[i][j]] = j; } } for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (check(a[0][i], a[0][j])) dp[i] = max(dp[i], dp[j] + 1); } ans = max(ans, dp[i]); } printf("%d\n", ans + 1); }
11
#include <bits/stdc++.h> using namespace std; long long n, u, v; int m, p; long long cal(long long l, long long r, long long x, long long y, long long a, long long d) { long long s = r - l + 1; if ((v < a) || (u > a + (s - 1) * d)) return 0; if ((l == x) && (r == y)) { long long h = a + ((max(u, a) - a - 1 + d) / d) * d, e = a + min((v - a) / d, s - 1) * d; long long t = (e - h) / d + 1; if (t % 2 == 0) return (h + e) % p * ((t / 2) % p) % p; return ((h + e) / 2) % p * (t % p) % p; } long long mid = (l + r) / 2; if (y <= mid) return cal(l, mid, x, y, a, d * 2); if (x > mid) return cal(mid + 1, r, x, y, a + d, d * 2); return (cal(l, mid, x, mid, a, d * 2) + cal(mid + 1, r, mid + 1, y, a + d, d * 2)) % p; } int main(void) { scanf("%I64d %d %d", &n, &m, &p); for (int i = 1; i <= m; ++i) { long long j, k; scanf("%I64d %I64d %I64d %I64d", &j, &k, &u, &v); long long ans = cal(1, n, j, k, 1, 1); printf("%I64d\n", ans); } return 0; }
17
#include <bits/stdc++.h> using namespace std; namespace IO { const int BUFFER_SIZE = 1 << 15; char input_buffer[BUFFER_SIZE]; int input_pos = 0, input_len = 0; char output_buffer[BUFFER_SIZE]; int output_pos = 0; char number_buffer[100]; uint8_t lookup[100]; void _update_input_buffer() { input_len = fread(input_buffer, sizeof(char), BUFFER_SIZE, stdin); input_pos = 0; if (input_len == 0) input_buffer[0] = EOF; } inline char next_char(bool advance = true) { if (input_pos >= input_len) _update_input_buffer(); return input_buffer[advance ? input_pos++ : input_pos]; } template <typename T> inline void read_int(T &number) { bool negative = false; number = 0; while (!isdigit(next_char(false))) if (next_char() == '-') negative = true; do { number = 10 * number + (next_char() - '0'); } while (isdigit(next_char(false))); if (negative) number = -number; } template <typename T, typename... Args> inline void read_int(T &number, Args &...args) { read_int(number); read_int(args...); } void _flush_output() { fwrite(output_buffer, sizeof(char), output_pos, stdout); output_pos = 0; } inline void write_char(char c) { if (output_pos == BUFFER_SIZE) _flush_output(); output_buffer[output_pos++] = c; } template <typename T> inline void write_int(T number, char after = '\0') { if (number < 0) { write_char('-'); number = -number; } int length = 0; while (number >= 10) { uint8_t lookup_value = lookup[number % 100]; number /= 100; number_buffer[length++] = (lookup_value & 15) + '0'; number_buffer[length++] = (lookup_value >> 4) + '0'; } if (number != 0 || length == 0) write_char(number + '0'); for (int i = length - 1; i >= 0; i--) write_char(number_buffer[i]); if (after) write_char(after); } void init() { bool exit_success = atexit(_flush_output) == 0; assert(exit_success); for (int i = 0; i < 100; i++) lookup[i] = (i / 10 << 4) + i % 10; } } // namespace IO auto random_address = [] { char *p = new char; delete p; return (uint64_t)p; }; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1)); int N; vector<int> A; vector<pair<long long, int>> sorted; vector<pair<long long, int>> negatives, positives; bool compare_first(const pair<long long, int> &x, const pair<long long, int> &y) { return x.first < y.first; } void output_negative_positive_solution(int neg_count, int pos_count) { vector<int> solution; if (neg_count > 0) { for (int i = 0; i < neg_count; i++) solution.push_back(negatives[i].second); } else { for (int i = (int)negatives.size() + neg_count; i < (int)negatives.size(); i++) solution.push_back(negatives[i].second); } if (pos_count > 0) { for (int i = 0; i < pos_count; i++) solution.push_back(positives[i].second); } else { for (int i = (int)positives.size() + pos_count; i < (int)positives.size(); i++) solution.push_back(positives[i].second); } IO::write_int(solution.size(), '\n'); for (int i = 0; i < (int)solution.size(); i++) IO::write_int(solution[i] + 1, i < (int)solution.size() - 1 ? ' ' : '\n'); } void build_prefixes_and_suffixes() { negatives = positives = {}; for (auto &item : sorted) if (item.first < 0) negatives.push_back(item); else positives.push_back(item); } bool attempt_prefixes_and_suffixes() { int neg = negatives.size(), pos = positives.size(); vector<pair<long long, int>> negative_prefixes, negative_suffixes; long long sum = 0; for (int i = 0; i < neg; i++) { sum += negatives[i].first; negative_prefixes.emplace_back(sum, i + 1); } sum = 0; for (int i = neg - 1; i >= 0; i--) { sum += negatives[i].first; negative_suffixes.emplace_back(sum, -(neg - i)); } reverse(negative_prefixes.begin(), negative_prefixes.end()); reverse(negative_suffixes.begin(), negative_suffixes.end()); assert(is_sorted(negative_prefixes.begin(), negative_prefixes.end(), compare_first)); assert(is_sorted(negative_suffixes.begin(), negative_suffixes.end(), compare_first)); vector<pair<long long, int>> positive_prefixes, positive_suffixes; sum = 0; for (int i = 0; i < pos; i++) { sum += positives[i].first; positive_prefixes.emplace_back(sum, i + 1); } sum = 0; for (int i = pos - 1; i >= 0; i--) { sum += positives[i].first; positive_suffixes.emplace_back(sum, -(pos - i)); } assert(is_sorted(positive_prefixes.begin(), positive_prefixes.end(), compare_first)); assert(is_sorted(positive_suffixes.begin(), positive_suffixes.end(), compare_first)); vector<pair<long long, int>> negative_options(2 * neg), positive_options(2 * pos); merge(negative_prefixes.begin(), negative_prefixes.end(), negative_suffixes.begin(), negative_suffixes.end(), negative_options.begin(), compare_first); merge(positive_prefixes.begin(), positive_prefixes.end(), positive_suffixes.begin(), positive_suffixes.end(), positive_options.begin(), compare_first); assert(is_sorted(negative_options.begin(), negative_options.end(), compare_first)); assert(is_sorted(positive_options.begin(), positive_options.end(), compare_first)); int no = negative_options.size(), po = positive_options.size(); for (int i = 0, j = po - 1; i < no; i++) { while (j >= 0 && negative_options[i].first + positive_options[j].first > 0) j--; if (j >= 0 && negative_options[i].first + positive_options[j].first == 0) { output_negative_positive_solution(negative_options[i].second, positive_options[j].second); return true; } } return false; } void run_case() { IO::read_int(N); A.resize(N); for (int i = 0; i < N; i++) IO::read_int(A[i]); for (int i = 0; i < N; i++) if (A[i] == 0) { IO::write_int(1, '\n'); IO::write_int(i + 1, '\n'); return; } vector<int> location(N + 1, -1); for (int i = 0; i < N; i++) if (A[i] > 0) location[A[i]] = i; for (int i = 0; i < N; i++) if (A[i] < 0 && location[-A[i]] >= 0) { IO::write_int(2, '\n'); IO::write_int(i + 1, ' '); IO::write_int(location[-A[i]] + 1, '\n'); return; } long long negative_sum = 0, positive_sum = 0; for (int i = 0; i < N; i++) if (A[i] < 0) negative_sum += abs(A[i]); else positive_sum += abs(A[i]); sorted = {}; for (int i = 0; i < N; i++) if (A[i] < 0) { if (abs(A[i]) <= positive_sum) sorted.emplace_back(A[i], i); } else { if (abs(A[i]) <= negative_sum) sorted.emplace_back(A[i], i); } sort(sorted.begin(), sorted.end(), compare_first); build_prefixes_and_suffixes(); int shuffles = 0; while (!attempt_prefixes_and_suffixes()) { shuffle(negatives.begin(), negatives.end(), rng); shuffle(positives.begin(), positives.end(), rng); shuffles++; } } int main() { IO::init(); int T; IO::read_int(T); while (T-- > 0) run_case(); }
19
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; const int maxx = 3e5 + 10; char s[maxx]; int num[maxx][26]; int main() { scanf("%s", s + 1); int n = strlen(s + 1); for (int i = 1; i <= n; i++) { num[i][s[i] - 'a']++; for (int j = 0; j < 26; j++) { num[i][j] += num[i - 1][j]; } } int m; cin >> m; while (m--) { int l, r; scanf("%d%d", &l, &r); if (l == r) { puts("Yes"); } else { if (s[l] != s[r]) puts("Yes"); else { int cnt = 0; for (int i = 0; i < 26; i++) { if (num[r][i] - num[l - 1][i]) cnt++; } if (cnt >= 3) puts("Yes"); else puts("No"); } } } }
10
#include <bits/stdc++.h> using namespace std; inline int gi() { register int data = 0, w = 1; register char ch = 0; while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar(); return w * data; } const int MAX_N = 2e5 + 5; struct Graph { int to, next; } e[MAX_N << 1]; int fir[MAX_N], e_cnt; void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; } void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}, fir[u] = e_cnt++; } int N, M; struct Answer { int x, y, z; }; namespace cpp1 { vector<Answer> ans; bool vis[MAX_N]; int f[MAX_N]; void dfs(int x) { vis[x] = 1; int lst = 0; for (int i = fir[x]; ~i; i = e[i].next) { int v = e[i].to; e[i].to = e[i ^ 1].to = 0; if (!v) continue; if (!vis[v]) dfs(v); if (f[v]) ans.push_back((Answer){f[v], v, x}); else if (lst) ans.push_back((Answer){lst, x, v}), lst = 0; else lst = v; } f[x] = lst; } void main() { for (int i = 1; i <= N; i++) if (!vis[i]) dfs(i); printf("%lu\n", ans.size()); for (int i = 0; i < (int)ans.size(); i++) printf("%d %d %d\n", ans[i].x, ans[i].y, ans[i].z); } } // namespace cpp1 int main() { clearGraph(); N = gi(), M = gi(); for (int i = 1; i <= M; i++) { int x = gi(), y = gi(); Add_Edge(x, y), Add_Edge(y, x); } cpp1::main(); return 0; }
15
#include <bits/stdc++.h> using namespace std; const long long N = 100005; vector<long long> v[N]; vector<long long> vcol[105]; vector<long long> dis[N]; void bfs(long long x, long long* d) { queue<long long> q; d[x] = 1; q.push(x); while (!q.empty()) { long long x = q.front(); q.pop(); for (int y : v[x]) if (!d[y]) { d[y] = d[x] + 1; q.push(y); } } } void solve() { long long n, m = 0, i = 1, j = 0, l = 0, r = 0; long long sum = 0, y = 0, q, z = 0, ans = 0, x, k = 0; cin >> n >> m >> k >> x; for (i = 1; i <= n; i++) { cin >> j; vcol[j].push_back(i); } for (i = 0; i < m; i++) { cin >> l >> r; v[l].push_back(r); v[r].push_back(l); } for (int col = 1; col <= k; col++) { long long d[N] = {}; for (long long i : vcol[col]) { v[0].push_back(i); v[i].push_back(0); } bfs(0, d); for (i = 1; i <= n; i++) if (d[i] >= 2) dis[i].push_back(d[i] - 2); v[0].clear(); for (long long i : vcol[col]) v[i].pop_back(); } for (i = 1; i <= n; i++) sort(dis[i].begin(), dis[i].end()); for (i = 1; i <= n; i++) { ans = 0; for (j = 0; j < x; j++) { ans += dis[i][j]; } cout << ans << " "; } } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int T = 1; while (T--) solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; bool isPrime[400]; vector<int> prime; vector<int> factor[12005]; int primeCnt[80]; void make_prime() { for (int i = 0; i < 400; i++) isPrime[i] = true; for (int i = 2; i < 400; i++) { if (isPrime[i]) { for (int j = i * i; j < 400; j += i) isPrime[j] = false; prime.push_back(i); } } } void dfs(int i, int num, int index, int res) { if (i == 79) { if (res != 1) factor[index].push_back(num * res); factor[index].push_back(num); return; } for (int j = 0; j <= primeCnt[i]; j++) { dfs(i + 1, num, index, res); num *= prime[i]; } } void make_factor() { factor[1].push_back(1); for (int i = 2; i <= 12000; i++) { memset(primeCnt, 0, sizeof(primeCnt)); int tmp = i; for (int j = 0; j < prime.size(); j++) while (tmp % prime[j] == 0) { primeCnt[j]++; tmp /= prime[j]; } dfs(0, 1, i, tmp); sort(factor[i].begin(), factor[i].end()); } } int main() { make_prime(); make_factor(); int t; cin >> t; while (t--) { int a, b, c; cin >> a >> b >> c; int ans = 1000000000; int ansA, ansB, ansC; for (int i = b; i >= 1; i--) { if (b - i > ans) break; int tmp = b - i, tmpc; if (abs((c / i) * i - c) < abs((c / i + 1) * i - c)) { tmpc = (c / i) * i; } else { tmpc = (c / i + 1) * i; } auto it = lower_bound(factor[i].begin(), factor[i].end(), a); int tmpa; if (it != factor[i].begin()) { auto it2 = it - 1; if (abs(*it - a) > abs(*it2 - a)) tmpa = *it2; else tmpa = *it; } else tmpa = *it; tmp += abs(a - tmpa) + abs(c - tmpc); if (tmp < ans) { ans = tmp; ansA = tmpa, ansB = i, ansC = tmpc; } } for (int i = b; i <= 12000; i++) { if (i - b > ans) break; int tmp = i - b, tmpc; if (abs((c / i) * i - c) < abs((c / i + 1) * i - c)) { tmpc = (c / i) * i; } else { tmpc = (c / i + 1) * i; } auto it = lower_bound(factor[i].begin(), factor[i].end(), a); int tmpa; if (it != factor[i].begin()) { auto it2 = it - 1; if (abs(*it - a) > abs(*it2 - a)) tmpa = *it2; else tmpa = *it; } else tmpa = *it; tmp += abs(a - tmpa) + abs(c - tmpc); if (tmp < ans) { ans = tmp; ansA = tmpa, ansB = i, ansC = tmpc; } } cout << ans << endl; cout << ansA << " " << ansB << " " << ansC << endl; } }
12
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const unsigned long long INF = 0x3f3f3f3f3f3f3f3fLL; unsigned long long c, hr, hb, wr, wb; unsigned long long gcd(unsigned long long a, unsigned long long b) { return b == 0 ? a : gcd(b, a % b); } unsigned long long lcm(unsigned long long a, unsigned long long b) { return a * (b / gcd(a, b)); } signed main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> c >> hr >> hb >> wr >> wb; unsigned long long ans = 0; unsigned long long common = lcm(wb, wr) * 1000; if (common <= c) { unsigned long long mul = c / common; if ((long double)hr / wr > (long double)hb / wb) ans += (((common / wr) * hr) * mul); else ans += (((common / wb) * hb) * mul); c %= common; } unsigned long long bigw = max(wr, wb); unsigned long long bigh = (wr > wb) ? hr : hb; unsigned long long smw = min(wr, wb); unsigned long long smh = (wr > wb) ? hb : hr; unsigned long long maxh = 0; for (unsigned long long i = 0; i <= c / bigw; i++) { unsigned long long t = i * bigh + ((c - (i * bigw)) / smw) * smh; maxh = max(t, maxh); } cout << ans + maxh << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; const int maxn = 1010; int n, m, a[maxn][maxn]; vector<int> tmp, r[maxn], c[maxn]; int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf("%d", &a[i][j]), r[i].push_back(a[i][j]), c[j].push_back(a[i][j]); } } for (int i = 1; i <= n; i++) { sort(r[i].begin(), r[i].end()), tmp.clear(); int lst = INT_MIN; for (int j : r[i]) if (j ^ lst) tmp.push_back(j), lst = j; r[i] = tmp; } for (int i = 1; i <= m; i++) { sort(c[i].begin(), c[i].end()), tmp.clear(); int lst = INT_MIN; for (int j : c[i]) if (j ^ lst) tmp.push_back(j), lst = j; c[i] = tmp; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int c1 = max(lower_bound(r[i].begin(), r[i].end(), a[i][j]) - r[i].begin(), lower_bound(c[j].begin(), c[j].end(), a[i][j]) - c[j].begin()); int c2 = max(r[i].end() - upper_bound(r[i].begin(), r[i].end(), a[i][j]), c[j].end() - upper_bound(c[j].begin(), c[j].end(), a[i][j])); printf("%d ", c1 + c2 + 1); } printf("\n"); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; const int MAXN = 110; char m[MAXN][MAXN]; int main() { int n, m; scanf("%d%d", &n, &m); if (n == 5) { printf(">...v\nv.<..\n..^..\n>....\n..^.<\n1 1\n"); return 0; } else if (n == 3) { printf(">vv\n^<.\n^.<\n1 3\n"); return 0; } for (int a = 0; a < 100; ++a) { if (a % 2 == 0) { for (int b = 0; b < 50; ++b) printf(">"); for (int b = 0; b < 49; ++b) printf("%c", b % 2 == 0 ? '.' : '>'); printf("v\n"); } else { printf("%c", a == 99 ? '^' : '.'); for (int b = 0; b < 49; ++b) printf("%c", b % 2 == 0 ? '.' : '<'); for (int b = 0; b < 50; ++b) printf("<"); printf("\n"); } } printf("99 1\n"); return 0; }
17
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int line, a, b; cin >> line; while (line--) { cin >> a >> b; if (gcd(a, b) == 1) { cout << "finite" << endl; } else { cout << "infinite" << endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; void merge(int a[], int s1, int e1, int s2, int e2) { int i = s1, j = s2, k = 0; int n1 = e1 - s1 + 1; int n2 = e2 - s2 + 1; int *ans = new int[n1 + n2]; while (i <= e1 && j <= e2) { if (a[i] < a[j]) { ans[k++] = a[i++]; } else { ans[k++] = a[j++]; } } while (i <= e1) { ans[k++] = a[i++]; } while (j <= e2) { ans[k++] = a[j++]; } for (k = 0; k < n1 + n2; k++) { a[s1 + k] = ans[k]; } delete[] ans; } void mergesort(int a[], int s, int e) { if (s == e) { return; } int mid = s + e; mid /= 2; mergesort(a, s, mid); mergesort(a, mid + 1, e); merge(a, s, mid, mid + 1, e); } int main() { int n, a, b; cin >> n >> a >> b; int *ar = new int[n]; for (int i = 0; i < n; i++) { cin >> ar[i]; } sort(ar, ar + n); cout << ar[b] - ar[b - 1]; delete[] ar; return 0; }
0