func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; int G[16][110]; bool B[110][2]; int main() { memset(G, -1, sizeof(G)); for (int i = 0; i < 16; i++) { G[i][0] = 0; } for (int j = 1; j <= 105; j++) { for (int i = 0; i < 16; i++) { if ((i & 5) == 5 || (i & 10) == 10) { continue; } vector<int> gs; for (int k = (i & 4 ? 1 : 0); k + (i & 8 ? 1 : 0) < j; k++) { gs.push_back(G[(i & 5) | 2][max(0, k)] ^ G[(i & 10) | 1][max(0, j - k - 1)]); } for (int k = (i & 1 ? 1 : 0); k + (i & 2 ? 1 : 0) < j; k++) { gs.push_back(G[(i & 5) | 8][max(0, k)] ^ G[(i & 10) | 4][max(0, j - k - 1)]); } sort(gs.begin(), gs.end()); gs.resize(unique(gs.begin(), gs.end()) - gs.begin()); G[i][j] = 0; assert(gs.empty() || gs[0] >= 0); for (int& g = G[i][j]; g < gs.size() && gs[g] == g; g++) ; } } int R, N; cin >> R >> N; for (int i = 0; i < N; i++) { int r, c; cin >> r >> c; r--; c--; B[r][c] = true; B[r][1 - c] = true; if (r > 0) B[r - 1][1 - c] = true; if (r + 1 < R) B[r + 1][1 - c] = true; } int gg = 0; for (int i = 0;;) { while (i < R && B[i][0] && B[i][1]) i++; if (i == R) break; int sz = 0; int lf = 0; int rh = 0; while (i + sz < R && !(B[i + sz][0] && B[i + sz][1])) { if (B[i + sz][0]) { lf++; } else if (B[i + sz][1]) { rh++; } sz++; } if (lf == 0 && rh == 0) { gg ^= G[0][sz]; } else if (lf + rh == 1) { gg ^= G[1][sz]; } else if (lf == 2 || rh == 2) { gg ^= G[3][sz]; } else if (lf == 1 && rh == 1) { gg ^= G[9][sz]; } else { assert(0); } i += sz; } if (gg) { cout << WIN << endl; } else { cout << LOSE << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; long long int maxSubArraySum(long long int a[], long long int size) { long long int max_so_far = INT_MIN, max_ending_here = 0; for (long long int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } int32_t main() { long long int t; t = 1; while (t--) { long long int n; cin >> n; long long int sum = 0; long long int a[n]; string s; vector<long long int> v; long long int k; cin >> k; long long int b[n]; for (long long int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } sort(b, b + n); long long int p = 0; for (long long int i = 0; i < n; i++) { if (a[i] != b[i]) { p = 1; break; } } if (p == 0) { cout << maxSubArraySum(a, n); } else { long long int ansfinal = -1e9; for (long long int i = 0; i < n; i++) { for (long long int j = i; j < n; j++) { long long int u = k; long long int ans = 0; priority_queue<long long int> pq; priority_queue<long long int, vector<long long int>, greater<long long int>> pqmin; for (long long int k = 0; k < n; k++) { if (k >= i && k <= j) { ans += a[k]; pqmin.push(a[k]); } else { pq.push(a[k]); } } while (u && pq.size() && pqmin.size()) { if (pqmin.top() >= pq.top()) { break; } else { ans -= pqmin.top(); pqmin.pop(); ans += pq.top(); pq.pop(); } u--; } ansfinal = max(ans, ansfinal); } } cout << ansfinal; } } } |
#include <bits/stdc++.h> using namespace std; const int N = 200010; long long a[N], f[N]; int main() { long long o, t, n = 1; long long s = 0; scanf( %I64d , &o); while (o--) { scanf( %I64d , &t); if (t == 2) { n++; scanf( %I64d , &a[n]); f[n] = 0; s += a[n]; } else if (t == 1) { long long cnt, x; scanf( %I64d%I64d , &cnt, &x); f[cnt] += x; s += cnt * x; } else if (t == 3) { s -= (f[n] + a[n]); f[n - 1] += f[n]; f[n] = 0; n--; } printf( %.9lf n , s * 1.0 / n); } } |
#include <bits/stdc++.h> using namespace std; long long n, d[500][500], dist[500][500]; long long t, a, c, b; int main() { cin >> n; for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { cin >> d[i][j]; dist[i][j] = d[i][j]; } } long long tong; cin >> t; while (t--) { cin >> a >> b >> c; tong = 0; dist[a][b] = dist[b][a] = min(dist[a][b], min(dist[b][a], c)); for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { dist[i][j] = min(dist[i][j], min(dist[i][a] + c + dist[b][j], dist[i][b] + c + dist[a][j])); } } for (long long i = 1; i <= n; i++) { for (long long j = i + 1; j <= n; j++) { tong += dist[i][j]; } } cout << tong << ; } } |
#include <bits/stdc++.h> #pragma GCC optimize( O2 ) using namespace std; const int MAX = 2e5 + 5; const long long MAX2 = 11; const int MOD = 1000000000 + 7; const long long INF = 20000; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1}; const double pi = acos(-1); long long n, h, ans; bool st, ada; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> h >> n; n--; for (int i = (int)h - 1; i >= (int)0; i--) { ada = (1ll << i) & n; if (ada ^ st) ans += (1ll << (i + 1)); else ans++, st ^= 1; } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { while (a % b != 0) { long long aux = a % b; a = b; b = aux; } return b; } int main() { ios::sync_with_stdio(0); cin.tie(); int n; cin >> n; for (int i = 1; i <= n; i++) { long long a, b, d; cin >> a >> b >> d; long long div = gcd(a, b); a /= div; b /= div; while (true) { div = gcd(b, d); if (1 == div) break; else { while (b % div == 0) b /= div; d = div; } } if (b == 1) cout << Finite n ; else cout << Infinite n ; } return 0; } |
#include <bits/stdc++.h> int main() { int n; scanf( %d , &n); double exp, ans, pi; exp = ans = 0.0; for (int i = 0; i < n; i++) { scanf( %lf , &pi); exp = exp * pi + pi; ans = ans + 2 * exp - pi; } printf( %.12lf n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> inline T sqr(T x) { return x * x; } const double EPS = 1e-10; const int INF = 0x3fffffff; const long long LINF = INF * 1ll * INF; const double PI = acos(-1.0); using namespace std; int sign(double x) { return x > 0 ? 1 : -1; } int main(void) { int x1, y1, x2, y2; scanf( %d %d %d %d , &x1, &y1, &x2, &y2); int vmax, t; scanf( %d %d , &vmax, &t); double low = 0, high = t; double windX, windY; scanf( %lf %lf , &windX, &windY); double newX, newY; scanf( %lf %lf , &newX, &newY); bool find = false; while (low + EPS <= high) { double mid = (low + high) / 2.0; double deltaX = x2 - x1; double deltaY = y2 - y1; double vx = deltaX / mid - windX; if (abs(vx) - abs(vmax) > 0) { low = mid; continue; } double vy = sqrt(vmax * vmax - vx * vx) + windY; double mx = vy * mid; vy = -sqrt(vmax * vmax - vx * vx) + windY; double mi = vy * mid; if (mx < mi) swap(mx, mi); if (mi - EPS <= deltaY && deltaY <= mx + EPS) { high = mid; find = true; } else low = mid; } if (!find) { low = t, high = 1e9; while (low + EPS <= high) { double mid = (low + high) / 2.0; double deltaX = x2 - x1; double deltaY = y2 - y1; double vx = deltaX - t * windX - newX * (mid - t); vx /= mid; if (abs(vx) - abs(vmax) > 0) { low = mid; continue; } double vy = sqrt(vmax * vmax - vx * vx); double mx = vy * mid + t * windY + (mid - t) * newY; vy = -sqrt(vmax * vmax - vx * vx); double mi = vy * mid + t * windY + (mid - t) * newY; if (mx < mi) swap(mx, mi); if (mi - EPS <= deltaY && deltaY <= mx + EPS) { high = mid; } else low = mid; } } printf( %.10lf n , low); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int k, rt, i, j; char a[] = { a , e , i , o , u }; cin >> k; rt = (int)sqrt(k); for (; k % rt != 0; rt--) ; if (rt > 4) { for (i = 0; i != k / rt; i++) { for (j = 0; j != rt; j++) { cout << a[(i + j) % 5]; } } } else cout << -1 ; } |
#include <bits/stdc++.h> using namespace std; const int N = 505; int dp[N][N]; int a[N]; int calc(int l, int r) { if (dp[l][r] != -1) return dp[l][r]; if (l > r) { return dp[l][r] = 1; } if (l == r) { return dp[l][r] = 1; } dp[l][r] = N; if (a[l] == a[r]) { dp[l][r] = min(dp[l][r], calc(l + 1, r - 1)); } for (int j = l; j < r; j++) { dp[l][r] = min(dp[l][r], calc(l, j) + calc(j + 1, r)); } return dp[l][r]; } int main() { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) dp[i][j] = -1; int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; cout << calc(0, n - 1) << endl; } |
#include <bits/stdc++.h> const long long int M = 2000000005; using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long int t; t = 1; while (t--) { long long int n, i, c, j; string s; cin >> s; n = s.size(); vector<long long int> v[26]; for (i = 0; i < 26; i++) { v[i].push_back(-1); } for (i = 0; i < n; i++) { c = (long long int)s[i] - 97; v[c].push_back(i); } for (i = 0; i < 26; i++) { v[i].push_back(n); } long long int ans = 1e18, ans1; for (i = 0; i < 26; i++) { if (v[i].size() == 2) { continue; } ans1 = -1e18; for (j = 1; j < v[i].size(); j++) { ans1 = max(ans1, v[i][j] - v[i][j - 1]); } ans = min(ans, ans1); } cout << ans; } } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; long long an1 = 1, an2 = 1; for (int i = 0; i < n; ++i) { an1 = (an1 * 7) % 1000000007; } for (int i = 0; i < 3 * n; ++i) { an2 = (an2 * 3) % 1000000007; } long long ot = an2 - an1; while (ot < 0) ot += 1000000007; cout << ot; return 0; } |
#include <bits/stdc++.h> int p, prime[100005], count[100005][20]; void Prime() { int i, j; for (i = 2, p = 0; i < 100005; i++) { for (j = 2; j <= sqrt(i); j++) if (i % j == 0) break; if (j > sqrt(i)) prime[p++] = i; } } void Count() { int i, j; memset(count, 0, sizeof(count)); for (i = 0; i < p; i++) for (j = prime[i]; j < 100005; j += prime[i]) count[j][++count[j][0]] = prime[i]; } int main() { Prime(); Count(); char sym; int n, m, i, j, ind, flag[100005], num[100005]; while (scanf( %d%d , &n, &m) != EOF) { memset(flag, 0, sizeof(flag)); memset(num, 0, sizeof(num)); for (i = 0; i < m; i++) { getchar(); scanf( %c %d , &sym, &ind); if (sym == + ) { if (flag[ind]) printf( Already on n ); else { for (j = 1; j <= count[ind][0]; j++) if (num[count[ind][j]]) break; if (j > count[ind][0]) { printf( Success n ); flag[ind] = 1; for (j = 1; j <= count[ind][0]; j++) num[count[ind][j]] = ind; } else printf( Conflict with %d n , num[count[ind][j]]); } } else { if (!flag[ind]) printf( Already off n ); else { printf( Success n ); flag[ind] = 0; for (j = 1; j <= count[ind][0]; j++) num[count[ind][j]] = 0; } } } } return 0; } |
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << = << a << endl; err(++it, args...); } long long powMod(long long x, long long y) { long long p = 1; while (y) { if (y % 2) { p = (p * x) % ((long long)1e9 + 7); } y /= 2; x = (x * x) % ((long long)1e9 + 7); } return p; } long long CpowMod(long long x, long long y, long long w) { long long p = 1; while (y) { if (y % 2) { p = (p * x) % w; } y /= 2; x = (x * x) % w; } return p; } long long invMod(long long x) { return powMod(x, ((long long)1e9 + 7) - 2); } long long CinvMod(long long x, long long w) { return CpowMod(x, w - 2, w); } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } void solve() { long long n; cin >> n; long long ans = 1; for (long long i = 1; i <= n - 1; i++) { if (i != n / 2) ans *= i; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(20); solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; struct node { int x, id; friend bool operator<(node a, node b) { if (a.x == b.x) return a.id < b.id; return a.x < b.x; } } a[200005]; priority_queue<node> Q; struct edge { int s, e; } ans[2000005]; int id; int main() { int n, s; while (scanf( %d %d , &n, &s) != EOF) { int i, j, k; for (i = 1; i <= n; ++i) { scanf( %d , &a[i].x); a[i].id = i; Q.push(a[i]); } node x; id = 0; while (!Q.empty()) { x = Q.top(); Q.pop(); j = 0; for (i = 0; i < x.x && (!Q.empty()); ++i) { a[j] = Q.top(); Q.pop(); ans[id].s = x.id; ans[id].e = a[j].id; a[j].x--; j++; id++; } if (i == x.x) { --j; while (j >= 0) { if (a[j].x) { Q.push(a[j]); } j--; } } else { s = 0; break; } } if (s == 0) { puts( No ); } else { puts( Yes ); printf( %d n , id); for (i = 0; i < id; ++i) { printf( %d %d n , ans[i].s, ans[i].e); } } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1E5 + 1; long long int n, m, rk, ans; set<pair<long long int, long long int> > dep[N], dpt; vector<pair<long long int, pair<long long int, long long int> > > query; pair<long long int, long long int> arrival[N]; long long int setarrival() { for (long long int i = 0; i <= n; i++) { arrival[i].first = -1; arrival[i].second = 1E17; } long long int i = 0; set<long long int> s; for (i = 0; i < query.size(); i++) { if (s.size() < n) { long long int c1 = query[i].second.second; long long int f = query[i].second.first; if (arrival[f].second > c1) { arrival[f].second = c1; arrival[f].first = query[i].first; } s.insert(f); } else break; } if (s.size() < n) return -1; return i; } long long int setd() { for (long long int i = 1; i <= n; i++) { if (dep[i].empty()) return -1; set<pair<long long int, long long int> >::iterator it = dep[i].begin(); long long int c = (*it).first; long long int d = (*it).second; ans += c; dpt.insert({d, i}); } return 1; } long long int check() { long long int mx = 0; for (long long int i = 1; i <= n; i++) { long long int d = arrival[i].first; ans += arrival[i].second; if (d == -1) return -1; mx = max(d, mx); } return mx; } long long int setdept(long long int day) { long long int rd = day + rk + 1; while (1) { set<pair<long long int, long long int> >::iterator k = dpt.begin(); long long int d = (*k).first; long long int i = (*k).second; if (d >= rd) break; long long int f = 0, in = 0; while (!dep[i].empty()) { set<pair<long long int, long long int> >::iterator it = dep[i].begin(); long long int c = (*it).first; d = (*it).second; if (d < rd) { if (in == 0) ans -= c; in++; dep[i].erase(*dep[i].begin()); } else { f = 1; ans += c; dpt.erase(k); dpt.insert({d, i}); break; } } if (f == 0) return -1; } return 1; } int main() { cin >> n >> m >> rk; vector<long long int> v; while (m--) { long long int d, f, t, c; cin >> d >> f >> t >> c; if (f == 0) dep[t].insert({c, d}); else query.push_back({d, {f, c}}); } sort(query.begin(), query.end()); ans = 0; long long int idx = setarrival(); long long int ik = setd(); if (idx == -1 || ik == -1) { REP: cout << -1; return 0; } long long int day = check(); if (day == -1) goto REP; long long int sd = setdept(day); if (sd == -1) goto REP; v.push_back(ans); for (long long int i = idx; i < query.size(); i++) { long long int f = query[i].second.first; long long int d = query[i].first; long long int c = query[i].second.second; if (arrival[f].second > c) { ans = v.back(); ans -= arrival[f].second; ans += c; sd = setdept(d); if (sd == -1) break; v.push_back(ans); arrival[f].second = c; arrival[f].first = d; } } ans = *min_element(v.begin(), v.end()); cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string a, b; cin >> a >> b; int pa = 0; while (pa + 1 < a.size() && a[pa + 1] < b[0]) pa++; for (int i = 0; i <= pa; i++) cout << a[i]; cout << b[0]; } |
#include <bits/stdc++.h> using namespace std; void SieveOfErat() { bool prime[1000001]; memset(prime, true, sizeof(prime)); for (long long p = 2; p * p <= 1000000; p++) { if (prime[p] == true) { for (long long i = p * 2; i <= 1000000; i += p) prime[i] = false; } } } long long gcd(long long x, long long y) { if (x == 0) { return y; } else return gcd(x % y, y); } long long n, m; vector<vector<long long> > v; vector<long long> res; signed main() { ios_base::sync_with_stdio(false); cin >> n >> m; v.assign(n, vector<long long>(m, 0)); for (long long i = 0; i < n; ++i) { for (long long j = 0; j < m; ++j) cin >> v[i][j]; } if (n < m) { for (long long i = 0; i < n; ++i) { long long mi = 600; for (long long j = 0; j < m; ++j) mi = min(mi, v[i][j]); if (mi == 0) continue; for (long long j = 0; j < mi; ++j) res.push_back(i + 1); for (long long j = 0; j < m; ++j) v[i][j] -= mi; } for (long long j = 0; j < m; ++j) { long long mi = 600; for (long long i = 0; i < n; ++i) mi = min(mi, v[i][j]); if (mi == 0) continue; for (long long i = 0; i < mi; ++i) res.push_back(-(j + 1)); for (long long i = 0; i < n; ++i) v[i][j] -= mi; } } else { for (long long j = 0; j < m; ++j) { long long mi = 600; for (long long i = 0; i < n; ++i) mi = min(mi, v[i][j]); if (mi == 0) continue; for (long long i = 0; i < mi; ++i) res.push_back(-(j + 1)); for (long long i = 0; i < n; ++i) v[i][j] -= mi; } for (long long i = 0; i < n; ++i) { long long mi = 600; for (long long j = 0; j < m; ++j) mi = min(mi, v[i][j]); if (mi == 0) continue; for (long long j = 0; j < mi; ++j) res.push_back(i + 1); for (long long j = 0; j < m; ++j) v[i][j] -= mi; } } long long sum = 0; for (long long i = 0; i < n; ++i) for (long long j = 0; j < m; ++j) sum += v[i][j]; if (sum > 0) { cout << -1 << endl; return 0; } cout << res.size() << n ; for (long long i = 0; i < res.size(); ++i) { if (res[i] > 0) cout << row << res[i] << n ; else cout << col << -res[i] << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, dx, dy, ans = 0, a, b; map<long long, int> num; double k; scanf( %lld%lld%lld%lld , &n, &m, &dx, &dy); long long x, y; for (int i = 0; i < m; i++) { scanf( %lld%lld , &x, &y); k = ((dy * x - dx * y) % n + n) % n; num[k]++; if (ans < num[k]) { a = x; b = y; ans = num[k]; } } printf( %lld %lld n , a, b); return 0; } |
#include <bits/stdc++.h> int get() { int x = 0; char s = getchar(); while (s < 0 || s > 9 ) s = getchar(); while (s >= 0 && s <= 9 ) x = (x << 3) + (x << 1) + (s ^ 48), s = getchar(); return x; } const int M = 1e5 + 5; int n, m, head[M], cntedge, ans, s, a[M << 2]; bool c[M], vis[M]; struct edge { int to, nxt; edge() {} edge(const int T, const int NX) { to = T, nxt = NX; } } e[M << 1]; void add(const int u, const int v) { e[++cntedge] = edge(v, head[u]), head[u] = cntedge; } void dfs(int u, int fa) { vis[u] = 1; a[++ans] = u; c[u] ^= 1; for (int i = head[u]; i; i = e[i].nxt) if (!vis[e[i].to]) { dfs(e[i].to, u); a[++ans] = u; c[u] ^= 1; } if (c[u]) { a[++ans] = fa; c[fa] ^= 1; a[++ans] = u; c[u] ^= 1; } } int main() { n = get(), m = get(); for (int u, v; m--;) { u = get(), v = get(); add(u, v), add(v, u); } for (int i = 1; i <= n; ++i) { c[i] = get(); if (c[i]) s = i; } if (s) { dfs(s, -1); for (int i = 1; i <= n; ++i) if (!vis[i] && c[i]) return !printf( -1 ); } if (ans > 1 && a[ans - 1] == -1) ans -= 3; printf( %d n , ans); for (int i = 1; i <= ans; ++i) printf( %d , a[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1 << 17; const int LOG = 17; int n, m, q, tick; int id[N], st[N], nd[N], ch[N], dep[N], lca[LOG][N]; vector<int> v[N]; void dfs(int p, int x) { dep[x] = dep[p] + 1; lca[0][x] = p; for (int i = 1; i < LOG; i++) lca[i][x] = lca[i - 1][lca[i - 1][x]]; ch[x] = 1; for (auto u : v[x]) { if (u != p) { dfs(x, u); ch[x] += ch[u]; } } } int head[N], ww[N]; void dfs2(int p, int x, int h) { id[x] = ++tick; st[x] = tick; ww[tick] = x; head[x] = h; int mx = -1; for (auto u : v[x]) { if (u != p) { if (mx == -1 or ch[u] > ch[mx]) mx = u; } } if (mx != -1) dfs2(x, mx, h); for (auto u : v[x]) { if (u != p and u != mx) dfs2(x, u, u); } nd[x] = tick; } int getLca(int x, int y) { if (!x) return y; if (!y) return x; if (dep[x] < dep[y]) swap(x, y); for (int i = LOG - 1; i >= 0; i--) if (dep[x] - (1 << i) >= dep[y]) x = lca[i][x]; if (x == y) return x; for (int i = LOG - 1; i >= 0; i--) if (lca[i][x] != lca[i][y]) x = lca[i][x], y = lca[i][y]; return lca[0][x]; } vector<int> jun[N]; namespace tree { pair<long long, int> t[N << 1]; long long lazy[N << 1]; void Do(int x, long long v) { t[x].first += v; lazy[x] += v; } void push(int x) { Do(x + x, lazy[x]); Do(x + x + 1, lazy[x]); lazy[x] = 0; } void up(int x, int l, int r, int x1, int x2, long long v) { if (x2 < l or r < x1) return; if (x1 <= l and r <= x2) { Do(x, v); return; } push(x); int m = (l + r) >> 1; up(x + x, l, m, x1, x2, v); up(x + x + 1, m + 1, r, x1, x2, v); t[x] = min(t[x + x], t[x + x + 1]); } void update(int x, int l, int r, int x1) { if (l == r) { if (jun[ww[l]].size()) t[x] = {jun[ww[l]].back() + lazy[x], ww[l]}; else t[x] = {1e18, -1}; return; } push(x); int m = (l + r) >> 1; if (x1 <= m) update(x + x, l, m, x1); else update(x + x + 1, m + 1, r, x1); t[x] = min(t[x + x], t[x + x + 1]); } void init(int x, int l, int r) { if (l == r) { if (jun[ww[l]].size()) t[x] = {jun[ww[l]].back(), ww[l]}; else t[x] = {1e18, -1}; } else { int m = (l + r) >> 1; init(x + x, l, m); init(x + x + 1, m + 1, r); t[x] = min(t[x + x], t[x + x + 1]); } } pair<long long, int> get(int x, int l, int r, int x1, int x2) { if (x2 < l or r < x1) return {1e18, -1}; if (x1 <= l and r <= x2) return t[x]; push(x); int m = (l + r) >> 1; return min(get(x + x, l, m, x1, x2), get(x + x + 1, m + 1, r, x1, x2)); } } // namespace tree pair<long long, int> get(int x, int y) { pair<long long, int> res = {1e18, -1}; while (dep[x] >= dep[y]) { int a = head[x]; if (dep[a] < dep[y]) a = y; res = min(res, tree ::get(1, 1, n, id[a], id[x])); x = lca[0][a]; } return res; } int w[N]; int main() { scanf( %d %d %d , &n, &m, &q); for (int i = 1; i < n; i++) { int x, y; scanf( %d %d , &x, &y); v[x].push_back(y); v[y].push_back(x); } for (int i = 1; i <= m; i++) { scanf( %d , w + i); jun[w[i]].push_back(i); } for (int i = 1; i <= n; i++) { reverse(jun[i].begin(), jun[i].end()); } dfs(0, 1); tick = 0; dfs2(0, 1, 1); tree ::init(1, 1, n); for (int i = 1; i <= q; i++) { int c; scanf( %d , &c); if (c == 1) { int x, y, k; scanf( %d %d %d , &x, &y, &k); int lca = getLca(x, y); vector<int> v; for (int it = 0; it < k; it++) { auto tmp = min(get(x, lca), get(y, lca)); if (tmp.first > 5e17) break; v.push_back(jun[tmp.second].back()); jun[tmp.second].pop_back(); tree ::update(1, 1, n, st[tmp.second]); } printf( %d , v.size()); for (auto x : v) printf( %d , x); puts( ); } else { int x, k; scanf( %d %d , &x, &k); tree ::up(1, 1, n, st[x], nd[x], k); } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; scanf( %d %d %d %d , &a, &b, &c, &d); int M, M1, M2, V, V1, V2; M1 = (3 * a) / 10; M2 = (a - (a / 250) * c); if (M1 > M2) { M = M1; } else { M = M2; } V1 = (3 * b) / 10; V2 = (b - (b / 250) * d); if (V1 > V2) { V = V1; } else { V = V2; } if (M > V) { printf( Misha n ); } else { if (V > M) { printf( Vasya ); } else { printf( Tie ); } } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long LINF = 1e18; const long long MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const int MAXN = 200050; int a[MAXN], cnt[105]; unordered_map<int, int> mp; void solve(int T) { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); cnt[a[i]]++; } int maxx = 0, idx; for (int i = 1; i <= 100; i++) { if (cnt[i] > maxx) { maxx = cnt[i]; idx = i; } } int tmp = 0; for (int i = 1; i <= 100; i++) { if (cnt[i] == maxx) tmp++; } if (tmp >= 2) { printf( %d n , n); return; } int ans = 0; for (int i = 1; i <= 100; i++) { if (i == idx) continue; mp.clear(); mp[idx] = 0; int sum = 0; for (int j = 1; j <= n; j++) { if (a[j] == idx) sum++; else if (a[j] == i) sum--; if (sum == 0) ans = max(ans, j); else if (mp[sum]) { ans = max(ans, j - mp[sum]); } else mp[sum] = j; } } printf( %d , ans); } signed main() { int t = 1; for (int i = 1; i <= t; i++) { solve(i); } } |
#include <bits/stdc++.h> using namespace std; long long int mod1 = 1000000007; long long int mod2 = 67280421310721; long long int mod3 = 998244353; long long int INF = 1e18; long long int binpow1(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } long long int binpow2(long long int a, long long int b, long long int m) { a %= m; long long int res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; else return gcd(b, a % b); } map<long long int, long long int> fact(long long int n) { map<long long int, long long int> factorization; long long int c = 0; while (n % 2 == 0) { c++; n /= 2; } if (c > 0) { factorization.insert({2, c}); } c = 0; for (long long int d = 3; d * d <= n; d += 2) { while (n % d == 0) { c++; n /= d; } if (c > 0) { factorization.insert({d, c}); } c = 0; } if (n > 1) { factorization.insert({n, 1}); } return factorization; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { string s; cin >> s; long long int n = s.length(); long long int ans = 0; long long int c = 0, d = 0; long long int k = 0; long long int f = 0; long long int p = n; for (int i = (0); i < (n); i++) { if (s[i] == A ) { p = i; break; } } for (int i = (0); i < (n); i++) { if (s[i] == A ) { c++; f++; } else { d++; k++; } if (d >= c) { ans = ans + c; c = 0; d = 0; } } ans = ans + min(c, d); cout << n - 2 * ans - (k - ans - (k - ans) % 2) << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int freq[3]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; string s; cin >> s; for (int i = 0; i < n; i++) { freq[s[i] - 0 ]++; } for (int i = n - 1; i >= 0; i--) { if (freq[0] <= n / 3) { break; } if (s[i] == 0 ) { if (freq[2] < n / 3) { freq[2]++; s[i] = 2 ; freq[0]--; } else { freq[1]++; s[i] = 1 ; freq[0]--; } } } for (int i = 0; i < n; i++) { if (freq[1] <= n / 3) break; if (s[i] == 1 ) { if (freq[0] < n / 3) { freq[0]++; freq[1]--; s[i] = 0 ; } } } for (int i = n - 1; i >= 0; i--) { if (freq[1] <= n / 3) break; if (s[i] == 1 ) { if (freq[2] < n / 3) { freq[2]++; freq[1]--; s[i] = 2 ; } } } for (int i = 0; i < n; i++) { if (freq[2] <= n / 3) break; if (s[i] == 2 ) { if (freq[0] < n / 3) { freq[0]++; freq[2]--; s[i] = 0 ; } else { freq[1]++; freq[2]--; s[i] = 1 ; } } } cout << s; return 0; } |
#include <bits/stdc++.h> using namespace std; long long n, m, ps[69][69]; bitset<69> a[69]; long long sm(long long y1, long long x1, long long y2, long long x2) { return max(ps[y2][x2] - ps[y1 - 1][x2] - ps[y2][x1 - 1] + ps[y1 - 1][x1 - 1], 0ll); } int main() { long long t, rr, i, j; string s; bool bad; scanf( %lld , &t); for (rr = 0; rr < t; rr++) { scanf( %lld%lld , &n, &m); for (i = 1; i <= n; i++) { cin >> s; for (j = 1; j <= m; j++) { a[i][j] = s[j - 1] == A ; ps[i][j] = ps[i - 1][j] + ps[i][j - 1] - ps[i - 1][j - 1] + a[i][j]; } } bad = 1; for (i = 2; i < n; i++) { if (sm(i, 1, i, m) == m) { bad = 0; } } for (i = 2; i < m; i++) { if (sm(1, i, n, i) == n) { bad = 0; } } if (sm(1, 1, n, m) == n * m) { printf( 0 n ); } else if (sm(1, 1, 1, m) == m || sm(1, 1, n, 1) == n || sm(n, 1, n, m) == m || sm(1, m, n, m) == n) { printf( 1 n ); } else if (a[1][1] || a[1][m] || a[n][1] || a[n][m] || !bad) { printf( 2 n ); } else if (sm(1, 2, 1, m - 1) > 0 || sm(2, 1, n - 1, 1) > 0 || sm(n, 2, n, m - 1) > 0 || sm(2, m, n - 1, m) > 0) { printf( 3 n ); } else if (sm(1, 1, n, m)) { printf( 4 n ); } else { printf( MORTAL n ); } } } |
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3f; const int MOD = (int)1e9 + 7; const int N = (int)1e6 + 7; int n, k; int a[N], bit[N]; map<int, int> cnt[11]; long long dp[N][11]; void Init() { for (int i = (1); i < (11); ++i) cnt[i].clear(); for (int i = (0); i < (n); ++i) scanf( %d , a + i); for (int i = (0); i < (n); ++i) { int x = a[i]; bit[i] = 0; while (x) { bit[i]++; x /= 10; } } } int Solve() { for (int i = (0); i < (n); ++i) { cnt[bit[i]][a[i] % k]++; dp[i][0] = a[i] % k; for (int j = (1); j < (11); ++j) dp[i][j] = dp[i][j - 1] * 10 % k; } long long ans = 0; for (int i = (0); i < (n); ++i) { for (int j = (1); j < (11); ++j) { if (bit[i] == j && (dp[i][j] + a[i]) % k == 0) --ans; int t = k - dp[i][j] != k ? k - dp[i][j] : 0; if (cnt[j].count(t)) ans += cnt[j][t]; } } return !printf( %lld n , ans); } int main() { while (~scanf( %d%d , &n, &k)) { Init(); Solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; c *= 2; int d = a / c; if (d > 1) d -= 1; int e = b / c; if (e > 1) e -= 1; int res = d * e; if (!res % 2) cout << Second n ; else cout << First n ; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 55; const int M = 1000000007; int n, p, c[maxn]; long long ans = 0, pw[maxn], od[maxn], ev[maxn], f[maxn][maxn][maxn][maxn]; void init() { f[0][0][0][0] = 1; pw[0] = 1; for (int i = 1; i < maxn; i++) pw[i] = pw[i - 1] * 2 % M; ev[0] = 1, od[0] = 0; for (int i = 1; i < maxn; i++) ev[i] = od[i] = pw[i - 1]; } void find_ans(int ob, int eb, int ow, int ew, int col, long long &ret) { if (col != 0 && ew != 0) (ret += f[ob][eb][ow][ew - 1] * pw[ow + ew - 1 + eb] % M * od[ob] % M) %= M; if (col != 0 && ow != 0) (ret += f[ob][eb][ow - 1][ew] * pw[ow + ew - 1 + eb] % M * ev[ob] % M) %= M; if (col != 1 && eb != 0) (ret += f[ob][eb - 1][ow][ew] * pw[ob + eb - 1 + ew] % M * od[ow] % M) %= M; if (col != 1 && ob != 0) (ret += f[ob - 1][eb][ow][ew] * pw[ob - 1 + eb + ew] % M * ev[ow] % M) %= M; return; } int main() { init(); scanf( %d%d , &n, &p); for (int i = 1; i <= n; i++) scanf( %d , c + i); for (int i = 1; i <= n; i++) for (int ob = 0; ob <= i; ob++) for (int eb = 0; ob + eb <= i; eb++) for (int ow = 0; ob + eb + ow <= i; ow++) { int ew = i - ob - eb - ow; find_ans(ob, eb, ow, ew, c[i], f[ob][eb][ow][ew]); if (i == n && ((ob + ow) & 1) == p) (ans += f[ob][eb][ow][ew]) %= M; } printf( %I64d , ans); return 0; } |
#include <bits/stdc++.h> using ll = long long; int n, q; namespace BIT { int seg[100005]; inline void modify(int pos, int val) { for (; pos <= n; pos += pos & -pos) seg[pos] += val; } inline int query(int pos) { int res = 0; for (; pos; pos -= pos & -pos) res += seg[pos]; return res; } inline int calc_idx(int x) { int l = 1, r = n, res = n + 1; while (l <= r) { int m = l + r >> 1; if (query(m) > x) { res = m; r = m - 1; } else l = m + 1; } modify(res, -1); return res; } } // namespace BIT namespace segT { int lson[20000005], rson[20000005], sum[20000005], t_cnt; void modify(int &u, int v, int l, int r, int pos) { u = ++t_cnt; lson[u] = lson[v]; rson[u] = rson[v]; sum[u] = sum[v] + 1; if (l == r) return; int m = l + r >> 1; if (pos <= m) modify(lson[u], lson[v], l, m, pos); else modify(rson[u], rson[v], m + 1, r, pos); } int query_rk(int u, int l, int r, int rk) { if (l == r) return l; int m = l + r >> 1; if (sum[lson[u]] > rk) return query_rk(lson[u], l, m, rk); return query_rk(rson[u], m + 1, r, rk - sum[lson[u]]); } int query_sum(int u, int l, int r, int pos) { if (r <= pos) return sum[u]; int m = l + r >> 1, res = 0; if (m < pos) res = query_sum(rson[u], m + 1, r, pos); return query_sum(lson[u], l, m, pos) + res; } } // namespace segT int rep[100005], enp[100005], arr[100005], idx[100005], rt[100005]; ll pre[100005]; std::pair<ll, int> calc(int out, int in, int t) { in += rep[out] - t; int res = std::upper_bound(rep, rep + n, in) - rep; return {(ll)in * res - pre[res], res}; } int query_out(int x, int t) { int l = 0, r = n - 1, res = -1; while (l <= r) { int m = l + r >> 1; if (calc(m, 0, t).first <= x) { res = m; l = m + 1; } else r = m - 1; } return res; } int query_in(int out, int x, int t) { int l = 0, r = rep[out + 1] - rep[out], res = -1; if (out == n - 1) r = x / n + t + 1; while (l <= r) { int m = l + r >> 1; if (calc(out, m, t).first <= x) { res = m; l = m + 1; } else r = m - 1; } return res; } int main() { scanf( %d%d , &n, &q); for (int i = 0; i < n; i++) scanf( %d , arr + i); int stp = arr[0]; for (int i = 0; i < n; i++) arr[i] -= stp; for (int i = 1; i < n; i++) { rep[i] = rep[i - 1] + (arr[i] - enp[i - 1]) / i; enp[i] = enp[i - 1] + (rep[i] - rep[i - 1]) * i; pre[i + 1] = pre[i] + rep[i]; } for (int i = 1; i <= n; i++) BIT::modify(i, 1); for (int i = n - 1; i >= 0; i--) idx[i] = BIT::calc_idx(arr[i] - enp[i]) - 1; for (int i = 0; i < n; i++) segT::modify(rt[i + 1], rt[i], 0, n - 1, idx[i]); while (q--) { int x, t; scanf( %d%d , &x, &t); if (x < stp) { printf( %d n , x); continue; } x -= stp; int a = query_out(x, t), b = query_in(a, x, t); auto res = calc(a, b, t); int rem = x - res.first, rk = segT::query_rk(rt[res.second], 0, n - 1, rem); printf( %lld n , stp + enp[a] + (ll)b * (a + 1) + (rk ? segT::query_sum(rt[a + 1], 0, n - 1, rk - 1) : 0)); } return 0; } |
#include <bits/stdc++.h> using namespace std; struct Query { int id, left, right, time, blockLeft, blockRight; Query() : id(-1), left(-1), right(-1), time(-1), blockLeft(-1), blockRight(-1) {} Query(int id, int left, int right, int time, int blockLeft, int blockRight) : id(id), left(left), right(right), time(time), blockLeft(blockLeft), blockRight(blockRight) {} bool operator<(const Query &otherQuery) const { if (blockLeft != otherQuery.blockLeft) { return blockLeft < otherQuery.blockLeft; } if (blockRight != otherQuery.blockRight) { return blockRight < otherQuery.blockRight; } return time < otherQuery.time; } }; struct Operation { int pos, oldValue, newValue; Operation() : pos(-1), oldValue(-1), newValue(-1) {} Operation(int pos, int oldValue, int newValue) : pos(pos), oldValue(oldValue), newValue(newValue) {} }; void del(int value, int *cnts, int *saveValues) { saveValues[cnts[value]]--; cnts[value]--; saveValues[cnts[value]]++; } void add(int value, int *cnts, int *saveValues) { saveValues[cnts[value]]--; cnts[value]++; saveValues[cnts[value]]++; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; int sqrtValue = pow(n, 2.0 / 3.0), *values = new int[n + 1], *tmpValues = new int[n + 1]; if (sqrtValue == 0) sqrtValue = 1; vector<int> disValues; for (int i = 1; i <= n; i++) { cin >> values[i]; tmpValues[i] = values[i]; disValues.push_back(values[i]); } Query *querys = new Query[q + 1]; Operation *opts = new Operation[q + 1]; int cntQuerys = 0, cntOpts = 0; for (int i = 1; i <= q; i++) { int opt; cin >> opt; if (opt == 1) { int left, right; cin >> left >> right; ++cntQuerys; querys[cntQuerys] = Query(cntQuerys, left, right, cntOpts, left / sqrtValue, right / sqrtValue); } else { int pos, value; cin >> pos >> value; ++cntOpts; opts[cntOpts] = Operation(pos, tmpValues[pos], value); tmpValues[pos] = value; disValues.push_back(value); } } sort(disValues.begin(), disValues.end()); disValues.resize(unique(disValues.begin(), disValues.end()) - disValues.begin()); for (int i = 1; i <= cntOpts; i++) { Operation &tmpOpt = opts[i]; tmpOpt.oldValue = lower_bound(disValues.begin(), disValues.end(), tmpOpt.oldValue) - disValues.begin(); tmpOpt.newValue = lower_bound(disValues.begin(), disValues.end(), tmpOpt.newValue) - disValues.begin(); } for (int i = 1; i <= n; i++) { values[i] = lower_bound(disValues.begin(), disValues.end(), values[i]) - disValues.begin(); } int valueSize = static_cast<int>(disValues.size()), *cnts = new int[valueSize + 3], *saveValues = new int[n + 2]; memset(cnts, 0, sizeof(int) * (valueSize + 3)); memset(saveValues, 0, sizeof(int) * (n + 2)); int *results = new int[cntQuerys + 1]; sort(querys + 1, querys + cntQuerys + 1); int l = 1, r = 0, time = 0; for (int i = 1; i <= cntQuerys; i++) { Query &tmpQuery = querys[i]; while (r < tmpQuery.right) { r++; add(values[r], cnts, saveValues); } while (l > tmpQuery.left) { l--; add(values[l], cnts, saveValues); } while (r > tmpQuery.right) { del(values[r], cnts, saveValues); r--; } while (l < tmpQuery.left) { del(values[l], cnts, saveValues); l++; } while (time < tmpQuery.time) { time++; Operation &tmpOpt = opts[time]; if (tmpOpt.pos >= l && tmpOpt.pos <= r) { del(tmpOpt.oldValue, cnts, saveValues); add(tmpOpt.newValue, cnts, saveValues); } values[tmpOpt.pos] = tmpOpt.newValue; } while (time > tmpQuery.time) { Operation &tmpOpt = opts[time]; if (tmpOpt.pos >= l && tmpOpt.pos <= r) { del(tmpOpt.newValue, cnts, saveValues); add(tmpOpt.oldValue, cnts, saveValues); } values[tmpOpt.pos] = tmpOpt.oldValue; time--; } for (int i = 1;; i++) { if (i > n) { results[tmpQuery.id] = 0; } if (saveValues[i] == 0) { results[tmpQuery.id] = i; break; } } } for (int i = 1; i <= cntQuerys; i++) { cout << results[i] << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int arr[n]; int count = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < n; i++) { int x = 5 - arr[i]; if (x >= k) { count++; } } cout << count / 3 << endl; return 0; } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:64000000 ) using namespace std; const int MAXN = -1; const int MOD = 1; int main() { int n; while (cin >> n) { string s; cin >> s; vector<int> d(n); for (int i = 0; i < n; i++) cin >> d[i]; int cur = 0; vector<int> used(n, 0); while (1) { if (cur < 0 || cur >= n) { cout << FINITE << endl; break; } if (used[cur]) { cout << INFINITE << endl; break; } used[cur] = 1; if (s[cur] == > ) cur += d[cur]; else cur -= d[cur]; } } return 0; } |
#include <bits/stdc++.h> const long long inf = 1000000000ll; const long long inf64 = inf * inf; const long long base = inf + 7; const long long MOD = inf + 9; const double pi = acos(-1.0); using namespace std; bool solve() { int n, q; scanf( %d %d , &n, &q); vector<long long> a(2 * n + 1); for (int i(1); i <= n; i++) scanf( %I64d , &a[i]), a[i + n] = a[i]; vector<int> from(2 * n + 1), dp(2 * n + 1), last(2 * n + 1), used(2 * n + 1); vector<long long> sum(2 * n + 1, 0); for (int i(1); i <= 2 * n; i++) sum[i] = sum[i - 1] + a[i]; for (int i(0); i < q; i++) { long long b; scanf( %I64d , &b); int j = -1; for (int z(1); z <= n; z++) { from[z] = dp[z] = used[z] = last[z] = 0; if (sum[z] > b) { if (j == -1) j = 1; while (sum[z] - sum[j - 1] > b) j++; from[z] = j - 1; } dp[z] = dp[from[z]] + 1; if (from[z]) { last[z] = last[from[z]]; } else last[z] = z; } int ans = n + 1; long long S = 0; for (int z(n); z >= 1; z--) { if (S > b) break; int tmp = dp[z]; if (sum[last[z]] + S > b) tmp++; ans = min(ans, tmp); S += a[z]; } printf( %d n , ans); } return true; } int main() { solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long inf = 1e18; vector<long long> factorial; void fill_factorial(long long n) { factorial.resize(n + 1); factorial[0] = 1; for (long long i = 1; i <= n; i++) factorial[i] = (factorial[i - 1] * i) % mod; } long long gcd(long long a, long long b, long long& x, long long& y) { if (b == 0) { x = 1; y = 0; return a; } long long x1, y1; long long d = gcd(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return d; } long long binpow(long long a, long long b) { a %= mod; long long res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } long long inv(long long a) { long long x, y; long long g = gcd(a, mod, x, y); x = (x % mod + mod) % mod; return x; } long long CC(long long n, long long k) { return factorial[n] * inv(factorial[k]) % mod * inv(factorial[n - k]) % mod; } void solve() { long long n; cin >> n; long long a[n + 1]; for (long long i = 1; i <= n; ++i) cin >> a[i]; long long h = 0; queue<pair<long long, long long>> q; q.push(make_pair(1, 0)); long long j = 2; long long ans = 0; while (true) { long long val = q.front().first, t = q.front().second; ans = max(ans, t); q.pop(); q.push(make_pair(a[j], t + 1)); j++; while (j <= n && a[j] > a[j - 1]) { if (j > n) break; q.push(make_pair(a[j], t + 1)); j++; } if (j > n) break; } while (!q.empty()) { ans = max(ans, q.front().second); q.pop(); } cout << ans << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long no_of_test_cases; cin >> no_of_test_cases; while (no_of_test_cases--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { char s[1100000]; scanf( %s , s); int l = strlen(s); int ans = 0; int m = 0; int p = 0; while (s[p] == F ) p++; for (int i = p; i < l; i++) { if (s[i] == M ) { m++; } if (s[i] == F ) { ans = max(ans + 1, m); } } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> long long int dx[] = {-2, -2, -1, 1, 2, 2, 1, -1}; long long int dy[] = {-1, 1, 2, 2, 1, -1, -2, -2}; using namespace std; bool comp(long long int x, long long int y) { return abs(x) > abs(y); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int te = 1; cin >> te; while (te--) { long long int n; cin >> n; long long int arr[n]; long long int mxi = -INT_MAX; for (long long int i = 0; i < n; i++) cin >> arr[i], mxi = max(mxi, arr[i]); sort(arr, arr + n, comp); if (mxi < 0) { cout << arr[n - 1] * arr[n - 2] * arr[n - 3] * arr[n - 4] * arr[n - 5] << n ; continue; } long long int ans = arr[0] * arr[1] * arr[2] * arr[3] * arr[4]; if (ans < 0) { for (long long int i = 5; i < n; i++) { for (long long int j = 0; j < 5; j++) { long long int tmp = arr[i]; for (long long int k = 0; k < 5; k++) { if (k != j) tmp *= arr[k]; } ans = max(ans, tmp); } } } cout << ans << endl; } } |
#include <bits/stdc++.h> using namespace std; int Gcd(int x, int y) { while (x > 0 && y > 0) { if (x > y) { x = x % y; } else { y = y % x; } } return x + y; } int Sign(int x) { return (x > 0) ? 1 : -1; } int Abs(int x) { return (x > 0) ? x : -x; } void ReduceFraction(int& x, int& y) { if (x == 0) { y = 1; } else if (y == 0) { x = 1; } else { int sgn = Sign(x) * Sign(y); x = Abs(x); y = Abs(y); int g = Gcd(x, y); x /= g; y /= g; x *= sgn; } } struct Vector { Vector(int vx, int vy) { ReduceFraction(vx, vy); x = vx; y = vy; } int x; int y; bool operator==(const Vector& other) const { return (x == other.x) && (y == other.y); } }; struct Line { Line(int x0, int y0, int x1, int y1) : a(x1 - x0, y1 - y0) { if (a.y != 0) { u = a.y * x0 - a.x * y0; v = a.y; ReduceFraction(u, v); } else { u = y0; v = 10001; } } int u; int v; Vector a; bool operator==(const Line& other) const { return (u == other.u) && (v == other.v) && (a == other.a); } }; struct VectorHasher { size_t operator()(Vector v) const { const size_t coef = 39916801; const hash<int> int_hasher; return coef * int_hasher(v.x) + int_hasher(v.y); } }; struct LineHasher { size_t operator()(const Line& l) const { const size_t coef = 514229; const hash<int> int_hasher; const VectorHasher vector_hasher; return coef * coef * int_hasher(l.u) + coef * int_hasher(l.v) + vector_hasher(l.a); } }; long long Cn2(long long n) { return n * (n - 1) / 2; } int main() { int n; cin >> n; vector<pair<int, int>> pts(n); for (int i = 0; i < n; ++i) { cin >> pts[i].first >> pts[i].second; } unordered_map<Vector, unordered_set<Line, LineHasher>, VectorHasher> store; for (int i = 0; i + 1 < n; ++i) { for (int j = i + 1; j < n; ++j) { Line new_line(pts[i].first, pts[i].second, pts[j].first, pts[j].second); store[new_line.a].insert(new_line); } } long long result = 0; long long count = 0; for (const auto& p : store) { result -= Cn2(p.second.size()); count += p.second.size(); } cout << result + Cn2(count); return 0; } |
#include <bits/stdc++.h> using namespace std; long long add(long long a, long long b) { a += b; if (a >= 1000000007) a -= 1000000007; return a; } long long mul(long long a, long long b) { return ((a % 1000000007) * (b % 1000000007)) % 1000000007; } long long binary_expo(long long base, long long expo) { long long res = 1; while (expo) { if (expo % 2) { res = mul(res, base); expo /= 2; } base = mul(base, base); } return res; } vector<pair<long long, pair<long long, long long> > > v[1000002]; const long long N = 100002; long long Arr[N], dep[N]; multiset<long long> S1[N], S2[N]; long long n; long long BIT1[N], BIT2[N]; void update(long long BIT[], long long idx, long long val) { while (idx <= n) { BIT[idx] += val; idx += (idx & (-idx)); } } long long query(long long BIT[], long long idx) { long long sum = 0; while (idx > 0) { sum += BIT[idx]; idx -= (idx & (-idx)); } return sum; } void solve() { long long m, k; cin >> n >> m >> k; for (long long i = 1; i <= n; i++) { Arr[i] = 1e12; dep[i] = -1e12; } for (long long i = 0; i < m; i++) { long long d, f, t, c; cin >> d >> f >> t >> c; v[d].push_back({f, {t, c}}); if (t == 0) { Arr[f] = min(Arr[f], d); } else { dep[t] = max(dep[t], d); } } long long minn = 1e12; long long maxx = -1e12; for (long long i = 1; i <= n; i++) { if (Arr[i] == 1e12 or dep[i] == -1e12) { cout << -1 << endl; return; } maxx = max(maxx, Arr[i]); minn = min(minn, dep[i]); } long long ms = maxx + 1; long long me = minn - 1; if (me - ms + 1 < k) { cout << -1 << endl; return; } for (long long i = 1000000; i > (ms + k - 1); i--) { for (long long j = 0; j < v[i].size(); j++) { pair<long long, pair<long long, long long> > p = v[i][j]; if (p.first == 0) { long long val1 = 0; if (S1[p.second.first].size() != 0) { val1 = *(S1[p.second.first].begin()); } S1[p.second.first].insert(p.second.second); long long val = *(S1[p.second.first].begin()); update(BIT2, p.second.first, val - val1); } } } for (long long i = 1; i < ms; i++) { for (long long j = 0; j < v[i].size(); j++) { pair<long long, pair<long long, long long> > p = v[i][j]; if (p.second.first == 0) { long long val1 = 0; if (S2[p.first].size() != 0) { val1 = *(S2[p.first].begin()); } S2[p.first].insert(p.second.second); long long val = *(S2[p.first].begin()); update(BIT1, p.first, val - val1); } } } long long ans = query(BIT1, n) + query(BIT2, n); for (long long i = ms + k; i <= me; i++) { long long ar = i - k; long long de = i; for (long long j = 0; j < v[i].size(); j++) { pair<long long, pair<long long, long long> > p = v[i][j]; if (p.first == 0) { long long val1 = 0; if (S1[p.second.first].size() != 0) { val1 = *(S1[p.second.first].begin()); } S1[p.second.first].erase(S1[p.second.first].find(p.second.second)); long long val = *(S1[p.second.first].begin()); update(BIT2, p.second.first, val - val1); } } for (long long j = 0; j < v[ar].size(); j++) { pair<long long, pair<long long, long long> > p = v[ar][j]; if (p.second.first == 0) { long long val1 = 0; if (S2[p.first].size() != 0) { val1 = *(S2[p.first].begin()); } S2[p.first].insert(p.second.second); long long val = *(S2[p.first].begin()); update(BIT1, p.first, val - val1); } } ans = min(ans, query(BIT1, n) + query(BIT2, n)); } cout << ans << endl; } signed main() { long long t; ios_base::sync_with_stdio(false); t = 1; while (t--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int ans, n, m, f1[1000005], vis[1000005], vis1[1000005], num, g1[1000005], k; long long f[1000005], g[1000005]; struct he { int d, s, t, c; } a[1000005]; bool cmp(he a, he b) { return a.d < b.d; } int main() { scanf( %d%d%d , &n, &m, &k); for (int i = 1; i <= m; i++) { scanf( %d%d%d%d , &a[i].d, &a[i].s, &a[i].t, &a[i].c); } sort(a + 1, a + 1 + m, cmp); for (int i = 1; i <= m; i++) { f[i] = f[i - 1]; if (a[i].t == 0) { if (!vis[a[i].s]) { vis[a[i].s] = a[i].c; num++; f[i] += (long long)a[i].c; } else { if (a[i].c < vis[a[i].s]) { f[i] = f[i] - (long long)vis[a[i].s] + (long long)a[i].c; vis[a[i].s] = a[i].c; } } } f1[i] = num; } num = 0; for (int i = m; i >= 1; i--) { g[i] = g[i + 1]; if (a[i].s == 0) { if (!vis1[a[i].t]) { vis1[a[i].t] = a[i].c; num++; g[i] += (long long)a[i].c; } else { if (a[i].c < vis1[a[i].t]) { g[i] = g[i] - (long long)vis1[a[i].t] + (long long)a[i].c; vis1[a[i].t] = a[i].c; } } } g1[i] = num; } int pos1 = 1; int pos2 = m; while ((a[pos1].t != 0 || f1[pos1] != n) && pos1 <= m) pos1++; if (pos1 > m) { printf( -1 n ); return 0; } while ((a[pos2].s != 0 || a[pos2].d - a[pos1].d > k || g1[pos2] != n) && pos2 >= 0) pos2--; if (pos2 == 0) { printf( -1 n ); return 0; } while ((a[pos2].d - a[pos1].d <= k || a[pos2].s != 0) && pos2 <= m) pos2++; if (g1[pos2] != n || pos2 > m) { printf( -1 n ); return 0; } pos1 = pos2; while (pos1 >= 1 && (a[pos1].t != 0 || a[pos2].d - a[pos1].d <= k)) pos1--; long long ans = (long long)1e16; while (pos2 <= m && pos1 <= m) { ans = min(ans, f[pos1] + g[pos2]); pos1++; while ((a[pos1].t != 0 || f1[pos1] != n) && pos1 <= m) pos1++; while ((a[pos2].s != 0 || a[pos2].d - a[pos1].d <= k || g1[pos2] != n) && pos2 <= m) pos2++; } printf( %I64d n , ans); } |
#include <bits/stdc++.h> using namespace std; int n, k; bool check(int x) { int s[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int t; while (x > 0) { t = x % 10; if (t <= k) s[t]++; x /= 10; } for (int i = 0; i <= k; i++) { if (s[i] == 0) return false; } return true; } int main() { cin >> n >> k; int res = 0; int t; for (int i = 0; i < n; i++) { cin >> t; if (check(t)) res++; } cout << res; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, k, x[100005], pn, st; pair<int, pair<int, int> > p[100005]; priority_queue<pair<int, pair<int, int> > > q, qq; int main() { scanf( %d%d , &n, &m); for (int a = (1); a <= (n); a++) for (int b = (1); b <= (m); b++) p[pn++] = make_pair(a + b, make_pair(a, b)); sort(p, p + pn); scanf( %d , &k); for (int a = (0); a < (k); a++) scanf( %d , x + a); sort(x, x + k); for (int a = (0); a < (k); a++) { while (st < pn && p[st].first <= x[a]) { q.push(make_pair(p[st].second.first + m + 1 - p[st].second.second, p[st].second)); st++; } if (q.empty()) { printf( NO n ); return 0; } q.pop(); } while (!q.empty()) { qq.push(q.top()); q.pop(); } while (st < pn) { qq.push(make_pair(p[st].second.first + m + 1 - p[st].second.second, p[st].second)); st++; } scanf( %d , &k); for (int a = (0); a < (k); a++) scanf( %d , x + a); sort(x, x + k, greater<int>()); for (int a = (0); a < (k); a++) { if (qq.top().first > x[a]) { printf( NO n ); return 0; } qq.pop(); } printf( YES n ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; int n, sa[N], sb[N], a1[N], b1[N], suma, sumb, q[N]; struct nd { int w, id; } a[N], b[N]; bool cmp(nd a, nd b) { return a.w > b.w; } int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) scanf( %d , &a[i].w), a[i].id = i, a1[i] = a[i].w, suma += a[i].w; for (int i = 1; i <= n; ++i) scanf( %d , &b[i].w), b[i].id = i, b1[i] = b[i].w, sumb += b[i].w; sort(a + 1, a + n + 1, cmp); q[++q[0]] = a[1].id; for (int i = 2; i < n; i += 2) { if (b1[a[i].id] > b1[a[i + 1].id]) q[++q[0]] = a[i].id; else q[++q[0]] = a[i + 1].id; } if (n % 2 == 0) q[++q[0]] = a[n].id; cout << n / 2 + 1 << endl; for (int i = 1; i <= q[0]; ++i) cout << q[i] << ; } |
#include <bits/stdc++.h> using namespace std; const int N = 5; const int M = 1e7 + 1; const long long INF = 1LL << 61; long long a[N], t[N], cost[N]; inline long long bs(long long bytes, long long maxt) { long long res = -1; if (t[0] <= t[2]) { if ((t[0] * bytes) <= maxt) { res = 0; } return res; } if (bytes <= 0) { return 0; } int l = 0; int r = M; while (l <= r) { int mid = l + (r - l) / 2; long long b_left = bytes - mid * a[2]; long long t_left = maxt - mid * a[2] * t[2]; if (b_left < 0) { if (llabs(b_left) < a[2]) { t_left += (b_left * (-t[2])); b_left = 0; } else { r = mid - 1; continue; } } t_left -= (b_left * t[0]); if (t_left >= 0) { res = mid; r = mid - 1; } else { l = mid + 1; } } return res; } int main() { ios_base::sync_with_stdio(false); long long size, maxt; cin >> size >> maxt; cin >> t[0]; for (int i = int(1); i < int(3); i++) { cin >> a[i] >> t[i] >> cost[i]; } if (t[2] < t[1]) { swap(a[1], a[2]); swap(t[1], t[2]); swap(cost[1], cost[2]); } long long ans = INF; for (int i = int(0); i < int(M); i++) { long long money = i * cost[1]; long long bytes = size - i * a[1]; long long tt = maxt - (i * a[1] * t[1]); if (bytes > 0) { long long use2 = bs(bytes, tt); money += (use2 * cost[2]); if (use2 >= 0) { ans = min(ans, money); } } else if (bytes < 0) { tt += (bytes * (-t[1])); } if (bytes <= 0 and tt >= 0) { ans = min(ans, money); } } if (ans >= INF) { ans = -1; } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; double round(double x) { return ((long long)(x * 1e8 + 0.5)) / 1e8; } double x[5], y[5], r[5]; set<pair<double, double> > c[5]; set<pair<double, double> > u; void intersection(int i, int j) { double d = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); if ((d <= r[i] + r[j] && d >= abs(r[i] - r[j]))) { } else { return; } double a, h, x1, y1, x2, y2, _x, _y; a = (r[i] * r[i] - r[j] * r[j] + d * d) / (2 * d); h = sqrt(r[i] * r[i] - a * a); _x = x[i] + a * (x[j] - x[i]) / d; _y = y[i] + a * (y[j] - y[i]) / d; x1 = _x + h * (y[j] - y[i]) / d; y1 = _y - h * (x[j] - x[i]) / d; x2 = _x - h * (y[j] - y[i]) / d; y2 = _y + h * (x[j] - x[i]) / d; x1 = round(x1); x2 = round(x2); y1 = round(y1); y2 = round(y2); c[i].insert({x1, y1}); c[i].insert({x2, y2}); c[j].insert({x2, y2}); c[j].insert({x1, y1}); u.insert({x1, y1}); u.insert({x2, y2}); } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i] >> r[i]; } if (n == 1) { cout << 2 << endl; return 0; } else if (n == 2) { intersection(0, 1); if (u.size() == 2) { cout << 4 << endl; } else cout << 3 << endl; return 0; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { intersection(i, j); } } int v = u.size(); if (v == 0) { cout << 4 << endl; return 0; } int cc = 0; int e = 0; for (int i = 0; i < n; i++) { e += c[i].size(); if (c[i].size() == 0) { cc++; } } int f = e - v + 2 + cc; cout << f << endl; } |
#include <bits/stdc++.h> const int INF = 1000000; using namespace std; int main() { int a, b, m; vector<int> r(1); scanf( %d %d %d %d , &a, &b, &m, &r[0]); for (int i = 1; i < 2 * m + 2; i++) { r.push_back((a * r[i - 1] + b) % m); } vector<int> ans(2 * m + 2, INF); int res = 1; for (int i = 1; i < m + 1; i++) { for (int t = 1; t < m + 1; t++) if (r[i] == r[i + t]) { ans[i] = min(t, ans[i - 1]); res = ans[i]; cout << res; return 0; } } cout << res; return 0; } |
#include <bits/stdc++.h> const int N = 120000; using namespace std; vector<string> fig; int n, m; vector<vector<string> > figs; int ans; vector<pair<pair<int, int>, int> > avec; int board[200][200]; bool bad(int a, int b) { return (a < 0 || a >= n || b < 0 || b >= m || board[a][b] == 1); } vector<string> rotate(vector<string> cur) { char temp[10][10]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { temp[i][j] = cur[i][j]; } } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cur[i][j] = temp[j][2 - i]; } } return cur; } vector<pair<pair<int, int>, int> > cur; bool can_put(int x, int y, int id) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (figs[id][i][j] == # ) { if (bad(i + x, j + y)) return false; } } } return true; } void do_put(int x, int y, int id) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (figs[id][i][j] == # ) { board[x + i][y + j] = 1; } } } cur.push_back(make_pair(make_pair(x, y), id)); } void unroll(int x, int y, int id) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (figs[id][i][j] == # ) { board[x + i][y + j] = 0; } } } cur.pop_back(); } int flag, iters; void dfs(int a, int b) { int rem_area = n * m - (a * m + b); if (cur.size() + rem_area / 5 <= avec.size()) return; ++iters; if (iters % 1024 == 0) { double R = clock() * 1.0 / CLOCKS_PER_SEC; if (R > 1.4) flag = 1; } if (flag) return; if (cur.size() > avec.size()) { avec = cur; } if (a >= n - 2) { return; } for (int i = 0; i < 4; i++) { if (can_put(a, b, i)) { do_put(a, b, i); if (b + 1 == m) dfs(a + 1, 0); else dfs(a, b + 1); unroll(a, b, i); } } if (b + 1 == m) dfs(a + 1, 0); else dfs(a, b + 1); } int main() { ios_base::sync_with_stdio(0); fig.push_back( ### ); fig.push_back( .#. ); fig.push_back( .#. ); figs.push_back(fig); for (int i = 0; i < 3; i++) { figs.push_back(rotate(figs.back())); } cin >> n >> m; dfs(0, 0); cout << avec.size() << endl; for (int i = 0; i < avec.size(); i++) { int qi, qj; qi = avec[i].first.first; qj = avec[i].first.second; for (int a = 0; a < 3; a++) { for (int b = 0; b < 3; b++) { if (figs[avec[i].second][a][b] == # ) { board[qi + a][qj + b] = i + 1; } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (board[i][j] == 0) cout << . ; else cout << char( A + board[i][j] - 1); } cout << endl; } cin.get(); cin.get(); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long LLINF = 9223372036854775807LL; const int INF = 2147483647; const int MOD = 1000000007; const double eps = 1e-7; const double PI = acos(-1.0); int dr[] = {1, 0, -1, 0, -1, 1, 1, -1}; int dc[] = {0, -1, 0, 1, 1, 1, -1, -1}; int m; pair<int, int> arr[100005]; vector<int> v; bool done[100005]; bool cf(pair<int, int> a, pair<int, int> b) { return a.first > b.first; } int main() { scanf( %d , &m); for (int(i) = (1); (i) <= (m); (i)++) scanf( %d , &arr[i].first), arr[i].second = i; sort(arr + 1, arr + 1 + m); int cur = arr[1].first; done[arr[1].second] = true; v.push_back(cur); for (int(i) = (2); (i) <= (m); (i)++) { if (arr[i].first > cur) { cur = arr[i].first; done[arr[i].second] = true; v.push_back(cur); } } sort(arr + 1, arr + 1 + m, cf); for (int(i) = (1); (i) <= (m); (i)++) { if (arr[i].first < cur && !done[arr[i].second]) { cur = arr[i].first; v.push_back(cur); } } cout << (int)(v.size()) << endl; for (int(i) = (0); (i) < ((int)(v.size())); (i)++) printf( %d , v[i]); puts( ); return 0; } |
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 1e12; const int maxnnn = 2e8 + 30; const long long zero = 0; long long gcd(long long a, long long b) { if (a < b) swap(a, b); if (a % b == 0) return b; else gcd(b, a % b); } string ntos(long long a) { string str; stringstream ss; ss << a; ss >> str; return str; } long long ston(string a) { long long x; stringstream ss; ss << a; ss >> x; return x; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); bool flag; long long zimu[26]; long long n, m, a, b, t, y, fans, c, x, z, sw; string s, s1, s2; cin >> m >> n; vector<long long> py(m + 1); sw = 0; py.clear(); for (long long i = 0; i < n; ++i) { cin >> a >> b; py[min(a, b)]++; if (py[min(a, b)] == 1) sw++; } cin >> c; while (c--) { cin >> x; if (x == 3) { cout << m - sw << endl; } else if (x == 1) { cin >> y >> z; py[min(y, z)]++; if (py[min(y, z)] == 1) sw++; } else { cin >> y >> z; py[min(y, z)]--; if (py[min(y, z)] == 0) sw--; } } } |
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; long long int x = 0; vector<long long int> sum = {}; vector<long long int> val = {}; for (int i = 0; i < (int)q; i++) { int op; cin >> op; if (op == 1) { cin >> x; sum.push_back(((sum.size()) ? sum.back() : 0) + x); long long int v[2]; v[0] = sum.size() * sum.back(); v[1] = (sum.size() >= 2) ? ((sum.size() + 1) * sum[sum.size() - 2]) : 0; val.push_back(v[0] - v[1]); } else { auto itr = upper_bound(val.begin(), val.end(), x); itr--; long long int v = sum[distance(val.begin(), itr)]; cout << setprecision(16) << x - (double)(x + v) / (double)(distance(val.begin(), itr) + 2) << endl; } } } |
#include <bits/stdc++.h> using namespace std; template <typename T> T inverse(T a, T m) { T u = 0, v = 1; while (a != 0) { T t = m / a; m -= t * a; swap(a, m); u -= t * v; swap(u, v); } assert(m == 1); return u; } template <typename T> class Modular { public: using Type = typename decay<decltype(T::value)>::type; constexpr Modular() : value() {} template <typename U> Modular(const U& x) { value = normalize(x); } template <typename U> static Type normalize(const U& x) { Type v; if (-mod() <= x && x < mod()) v = static_cast<Type>(x); else v = static_cast<Type>(x % mod()); if (v < 0) v += mod(); return v; } const Type& operator()() const { return value; } template <typename U> explicit operator U() const { return static_cast<U>(value); } constexpr static Type mod() { return T::value; } Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; } template <typename U> Modular& operator+=(const U& other) { return *this += Modular(other); } template <typename U> Modular& operator-=(const U& other) { return *this -= Modular(other); } Modular& operator++() { return *this += 1; } Modular& operator--() { return *this -= 1; } Modular operator++(int) { Modular result(*this); *this += 1; return result; } Modular operator--(int) { Modular result(*this); *this -= 1; return result; } Modular operator-() const { return Modular(-value); } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) { value = normalize(static_cast<int64_t>(value) * static_cast<int64_t>(rhs.value)); return *this; } template <typename U = T> typename enable_if<is_same<typename Modular<U>::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) { int64_t q = static_cast<int64_t>(static_cast<long double>(value) * rhs.value / mod()); value = normalize(value * rhs.value - q * mod()); return *this; } template <typename U = T> typename enable_if<!is_integral<typename Modular<U>::Type>::value, Modular>::type& operator*=(const Modular& rhs) { value = normalize(value * rhs.value); return *this; } Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); } template <typename U> friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs); template <typename U> friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs); template <typename U> friend std::istream& operator>>(std::istream& stream, Modular<U>& number); private: Type value; }; template <typename T> bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value == rhs.value; } template <typename T, typename U> bool operator==(const Modular<T>& lhs, U rhs) { return lhs == Modular<T>(rhs); } template <typename T, typename U> bool operator==(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) == rhs; } template <typename T> bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(const Modular<T>& lhs, U rhs) { return !(lhs == rhs); } template <typename T, typename U> bool operator!=(U lhs, const Modular<T>& rhs) { return !(lhs == rhs); } template <typename T> bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) { return lhs.value < rhs.value; } template <typename T> Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) += rhs; } template <typename T, typename U> Modular<T> operator+(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) += rhs; } template <typename T> Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) -= rhs; } template <typename T, typename U> Modular<T> operator-(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) -= rhs; } template <typename T> Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) *= rhs; } template <typename T, typename U> Modular<T> operator*(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) *= rhs; } template <typename T> Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(const Modular<T>& lhs, U rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> operator/(U lhs, const Modular<T>& rhs) { return Modular<T>(lhs) /= rhs; } template <typename T, typename U> Modular<T> powmod(const Modular<T>& a, const U& b) { assert(b >= 0); Modular<T> x = a, res = 1; U p = b; while (p > 0) { if (p & 1) res *= x; x *= x; p >>= 1; } return res; } template <typename T> string to_string(const Modular<T>& number) { return to_string(number()); } template <typename T> std::ostream& operator<<(std::ostream& stream, const Modular<T>& number) { return stream << number(); } template <typename T> std::istream& operator>>(std::istream& stream, Modular<T>& number) { typename common_type<typename Modular<T>::Type, int64_t>::type x; stream >> x; number.value = Modular<T>::normalize(x); return stream; } constexpr int md = 998244353; using Mint = Modular<std::integral_constant<decay<decltype(md)>::type, md>>; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); Mint m, n; cin >> n >> m; Mint res = 0, pre = 1; for (int i = 1; i <= (int)n + 1; i++) { Mint tmp = i / (n + m - i + 1); if (i > 1) pre *= (n - i + 2) / (n + m - i + 2); tmp *= pre; res += tmp; } res *= m; Mint h = 0; for (Mint i = 1; i != (n + 1); i++) h += 1 / i; h = h * m + 1; cout << res * h; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 10; const long long INF = numeric_limits<long long>::max(); enum { EVEN, ODD }; int A[MAXN]; long long dp[2][MAXN]; int main() { int N; cin >> N; for (int i = 1; i <= N; ++i) cin >> A[i]; dp[ODD][1] = dp[EVEN][1] = dp[ODD][2] = -INF; dp[EVEN][2] = abs(A[2] - A[1]); long long ans = dp[EVEN][2]; for (int i = 3; i <= N; ++i) { dp[ODD][i] = dp[EVEN][i - 1] - abs(A[i] - A[i - 1]); dp[EVEN][i] = max(dp[ODD][i - 1] + abs(A[i] - A[i - 1]), abs(A[i] - A[i - 1]) * 1ll); ans = max(ans, max(dp[ODD][i], dp[EVEN][i])); } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = INT_MAX, df = 8e5 + 7; int i, j, k = 1, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, le[df], rig[df], c[df][26], ma[df << 1], head[df], pos[df << 1], tot, ans[df], fail[df], cnt, yet[df][26]; struct node { int sum, id; }; vector<node> vec[df]; struct edge { int to, nxt; } e[df << 1]; void add(int u, int v) { e[++o] = (edge){v, head[u]}, head[u] = o; return; } char ch[df], st[df]; void upd(int x, int y) { if (!x) return; for (int i = x; i <= 800000; i += (i & -i)) ma[i] += y; return; } int qry(int x) { int as = 0; for (int i = x; i; i -= (i & -i)) as += ma[i]; return as; } void insert1(int now, int v, int id) { if (!c[now][v]) c[now][v] = ++tot; pos[id] = c[now][v]; return; } void insert2(char *s, int id) { int len = strlen(s), now = 0; for (int i = (0); i <= (len - 1); ++i) { int v = s[i] - a ; if (!c[now][v]) c[now][v] = ++tot; now = c[now][v]; } pos[id + n] = now; return; } void init() { scanf( %d , &n); for (int i = (1); i <= (n); ++i) { int op, x; scanf( %d , &op); if (op == 1) { scanf( %s , ch), insert1(0, ch[0] - a , i); } if (op == 2) { scanf( %d , &x); scanf( %s , ch); insert1(pos[x], ch[0] - a , i); } } scanf( %d , &t); for (int i = (1); i <= (t); ++i) { int x; scanf( %d , &x), scanf( %s , st); insert2(st, i); vec[pos[x]].push_back((node){pos[i + n], i}); } return; } void build() { queue<int> que; for (int i = (0); i <= (25); ++i) if (c[0][i]) fail[c[0][i]] = 0, que.push(c[0][i]), yet[0][i] = 1; while (!que.empty()) { int u = que.front(); que.pop(); for (int i = (0); i <= (25); ++i) { if (c[u][i]) fail[c[u][i]] = c[fail[u]][i], yet[u][i] = 1, que.push(c[u][i]); else c[u][i] = c[fail[u]][i]; } } return; } void dfs1(int u) { le[u] = ++cnt; for (int i = head[u]; i; i = e[i].nxt) dfs1(e[i].to); rig[u] = cnt; return; } void dfs2(int u) { upd(le[u], 1); int siz = vec[u].size() - 1; for (int i = (0); i <= (siz); ++i) { int l = le[vec[u][i].sum], r = rig[vec[u][i].sum]; ans[vec[u][i].id] = qry(r) - qry(l - 1); } for (int i = (0); i <= (25); ++i) if (yet[u][i]) dfs2(c[u][i]); upd(le[u], -1); return; } int main() { init(); build(); for (int i = (1); i <= (tot); ++i) add(fail[i], i); dfs1(0); dfs2(0); for (int i = (1); i <= (t); ++i) printf( %d n , ans[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int size_c = 26; const int Maxn = 500010 << 1; string s[100020]; int w[100020]; int n; struct Suffix_Automaton { int ch[Maxn][size_c]; int f[Maxn]; int ml[Maxn]; int sz, head, tail; long long val[Maxn]; int cnt[Maxn], id[Maxn]; inline int getidx(char c) { return c - a ; } int node() { sz++; f[sz] = ml[sz] = 0; memset(ch[sz], 0, sizeof(ch[sz])); return sz; } void init() { head = tail = 1; sz = 0; node(); } void insert(int idx) { head = tail = 1; for (int i = 0; i < s[idx].size(); i++) add(getidx(s[idx][i])); } void add(int c) { int p = tail; if (ch[p][c]) { int q = ch[p][c]; if (ml[q] == ml[p] + 1) { tail = ch[p][c]; } else { int nq = node(); memcpy(ch[nq], ch[q], sizeof(ch[q])); f[nq] = f[q]; f[q] = nq; ml[nq] = ml[p] + 1; for (; p && ch[p][c] == q; p = f[p]) ch[p][c] = nq; tail = nq; } return; } int np = node(); ml[np] = ml[p] + 1; tail = np; for (; p && !ch[p][c]; p = f[p]) ch[p][c] = np; if (!p) f[np] = head; else if (ml[ch[p][c]] == ml[p] + 1) f[np] = ch[p][c]; else { int q = ch[p][c]; int nq = node(); memcpy(ch[nq], ch[q], sizeof(ch[q])); f[nq] = f[q]; f[q] = f[np] = nq; ml[nq] = ml[p] + 1; for (; p && ch[p][c] == q; p = f[p]) ch[p][c] = nq; } } void solve() { init(); cin >> n; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 1; i <= n; i++) scanf( %d , w + i); for (int i = 1; i <= n; i++) insert(i); memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= sz; i++) cnt[ml[i]]++; for (int i = 1; i <= sz; i++) cnt[i] += cnt[i - 1]; for (int i = 1; i <= sz; i++) id[cnt[ml[i]]--] = i; memset(cnt, 0, sizeof(cnt)); for (int idx = 1; idx <= n; idx++) { int rt = 1; for (int i = 0; i < s[idx].size(); i++) { rt = ch[rt][getidx(s[idx][i])]; val[rt] += w[idx]; } } for (int i = sz; i >= 1; i--) { int cur = id[i]; val[f[cur]] += val[cur]; } long long ans = 0; for (int i = 1; i <= sz; i++) ans = max(ans, 1LL * ml[i] * val[i]); cout << ans << endl; } } suf; int main() { suf.solve(); } |
#include <bits/stdc++.h> using namespace std; void time() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int main() { time(); int a, s, d, f; cin >> a >> s >> d >> f; if (a == s || s > a) { cout << Second << endl; } else { cout << First << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int main() { string s; cin >> s; int n = s.length(); vector<int> dist(n); for (int i = 0; i < n; i++) if (s[i] == 0 ) dist[i] = (i ? dist[i - 1] : 0) + 1; vector<int> dp(n + 2), nxt(n + 2, n); auto get = [&](int i) { return nxt[i] < n ? dp[nxt[i]] : 0; }; for (int i = n - 1; i >= 0; i--) { dp[i] = ((dist[i] <= dist.back()) + get(0) + get(dist[i] + 1)) % mod; nxt[dist[i]] = i; } int ans = n; if (nxt[0] < n) ans = ((long long)get(0) * (nxt[0] + 1)) % mod; cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using std::cin; using std::string; string x; int a, b, c; int main() { cin >> x; switch (x[0]) { case r : a = 0; break; case s : a = 2; break; case p : a = 5; break; } cin >> x; switch (x[0]) { case r : b = 0; break; case s : b = 2; break; case p : b = 5; break; } cin >> x; switch (x[0]) { case r : c = 0; break; case s : c = 2; break; case p : c = 5; break; } if (a == b && ((c == 0 && a == 2) || (c == 2 && a == 5) || (c == 5 && a == 0))) printf( S ); else if (b == c && ((a == 0 && b == 2) || (a == 2 && b == 5) || (a == 5 && b == 0))) printf( F ); else if (a == c && ((b == 0 && a == 2) || (b == 2 && a == 5) || (b == 5 && a == 0))) printf( M ); else printf( ? ); } |
#include <bits/stdc++.h> using namespace std; bool isPrime(long long int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; long long int p = sqrt(n); for (int i = 5; i <= p; i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long int b2d(string n) { string num = n; long long int dec_value = 0; long long int base = 1; long long int len = num.length(); for (int i = len - 1; i >= 0; i--) { if (num[i] == 1 ) dec_value += base; base = base * 2; } return dec_value; } int f, f1, f2; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; cin >> t; while (t--) { int n; cin >> n; f = 0; int a[n]; int cnt = 0, sum = 0; int m1 = 0, m2 = INT_MAX; for (long long i = 0; i < n; i++) { cin >> a[i]; if (a[i] != -1) f = 1; } for (long long i = 0; i < n; i++) { if (a[i] != -1) { if ((i > 0 && a[i - 1] == -1) || (i < n - 1 && a[i + 1] == -1)) { m1 = max(m1, a[i]); m2 = min(m2, a[i]); } } } if (!f) { cout << 0 1 n ; continue; } int avg1 = (m1 + m2) / 2; for (long long i = 0; i < n; i++) { if (a[i] == -1) { a[i] = avg1; } } int ma1 = 0; for (long long i = 0; i < n - 1; i++) { ma1 = max(ma1, abs(a[i + 1] - a[i])); } cout << ma1 << << avg1 << endl; } } |
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, k, p, s; cin >> n; int a[5][5]; for (i = 1; i <= 4; i++) { for (j = 1; j <= 4; j++) { cin >> a[i][j]; } } s = 0; for (i = 1; i <= 4; i++) { k = min(a[i][1], a[i][2]); p = min(a[i][3], a[i][4]); if (k + p <= n) { cout << i << << k << << n - k << endl; s++; i = n; } } if (s == 0) cout << -1 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; struct Cup { uint cap; uint ix; uint cur; }; int main() { uint n; uint total; cin >> n >> total; vector<Cup> cups(n); for (uint i = 0, wi; i < n; ++i) { cin >> wi; cups[i] = {wi, i, 0}; } sort(cups.begin(), cups.end(), [](const Cup& left, const Cup& right) { return left.cap < right.cap; }); uint cur = 0; bool done = true; for (Cup& cup : cups) { cur = cup.cap / 2 + (cup.cap & 1); if (cur > total) { done = false; break; } cup.cur = cur; total -= cup.cur; } uint diff; for (int i = n - 1; i > -1; --i) { diff = min(total, cups[i].cap - cups[i].cur); total -= diff; cups[i].cur += diff; } if (total > 0) { done = false; } if (!done) { cout << -1 n ; } else { sort(cups.begin(), cups.end(), [](const Cup& left, const Cup& right) { return left.ix < right.ix; }); for (Cup& c : cups) { cout << c.cur << ; } cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, A[405][405], B[405][405], a[405][405]; char s[405][405]; void Solve() { cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> (s[i] + 1); for (int j = 1; j <= m; ++j) { a[i][j] = s[i][j] - 0 ; } } for (int j = 1; j <= m; ++j) { for (int i = 1; i <= n; ++i) { B[i][j] = B[i - 1][j] + a[i][j]; A[i][j] = A[i - 1][j] + !a[i][j]; } } int ans = n * m; for (int i = 2; i < n; ++i) { for (int j = i + 2; j < n; ++j) { static int s[405], pre[405]; pre[0] = n * m; for (int k = 1; k <= m; ++k) { int w = B[j][k] - B[i - 1][k] + !a[j + 1][k] + !a[i - 1][k]; s[k] = s[k - 1] + w; pre[k] = min(pre[k - 1], A[j][k] - A[i - 1][k] - s[k]); } for (int k = 4; k <= m; ++k) { ans = min(ans, pre[k - 3] + s[k - 1] + A[j][k] - A[i - 1][k]); } } } cout << ans << n ; } int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) Solve(); return 0; } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:256000000 ) using namespace std; int n[2], k[2]; string s[] = { First , Second }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n[0] >> n[1] >> k[0] >> k[1]; int cur = 0; while (1) { if (n[cur] == 0) break; n[cur]--; cur ^= 1; } cout << s[cur ^ 1]; return 0; } |
#include <bits/stdc++.h> inline int getint() { register char ch; while (!isdigit(ch = getchar())) ; register int x = ch ^ 0 ; while (isdigit(ch = getchar())) x = (((x << 2) + x) << 1) + (ch ^ 0 ); return x; } const int N = 101, K = 41, mod = 1e9 + 7; int k, f[N][K], tmp[K]; std::forward_list<int> e[N]; inline void add_edge(const int &u, const int &v) { e[u].push_front(v); e[v].push_front(u); } void dfs(const int &x, const int &par) { f[x][0] = f[x][k + 1] = 1; for (int &y : e[x]) { if (y == par) continue; dfs(y, x); std::fill(&tmp[0], &tmp[k * 2] + 1, 0); for (register int i = 0; i <= k * 2; i++) { for (register int j = 0; j <= k * 2; j++) { (tmp[i + j <= k * 2 ? std::min(i, j + 1) : std::max(i, j + 1)] += (long long)f[x][i] * f[y][j] % mod) %= mod; } } std::copy(&tmp[0], &tmp[k * 2] + 1, f[x]); } } int main() { const int n = getint(); k = getint(); for (register int i = 1; i < n; i++) { add_edge(getint(), getint()); } dfs(1, 0); int ans = 0; for (register int i = 0; i <= k; i++) { (ans += f[1][i]) %= mod; } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; vector<long long> p(n + 1); for (long long i = 1; i < n + 1; i++) { long long x; cin >> x; p[x] = i; } multiset<pair<long long, long long> > k; vector<long long> ct(n + 1, 1); for (long long i = 1; i < n + 1; i++) k.insert({1, i}); for (long long i = 1; i < n + 1; i++) { long long pos = p[i]; if (ct[pos] != k.rbegin()->first) { cout << No << n ; return; } k.erase({ct[pos], pos}); auto it = k.upper_bound({ct[pos], pos}); if (it == k.end()) continue; long long newpos = it->second; k.erase(it); ct[newpos] += ct[pos]; k.insert({ct[newpos], newpos}); } cout << Yes << n ; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long TESTS = 1; cin >> TESTS; while (TESTS--) { solve(); } return 0; } |
#include <bits/stdc++.h> int d, n, m; int deq[200100]; int qs, qe; struct stt { int x, p; bool operator<(const stt& r) const { return x < r.x; } }; stt a[200100]; int main() { int i, j; long long int ans; scanf( %d%d%d , &d, &n, &m); for (i = 0; i < m; i++) { scanf( %d%d , &a[i].x, &a[i].p); } a[m].x = 0; a[m].p = 0; m++; a[m].x = d; a[m].p = 0; m++; std::sort(a, a + m); for (i = 1; i < m; i++) { if (a[i].x > a[i - 1].x + n) { printf( -1 ); return 0; } } qs = 0; qe = 0; j = 0; ans = 0; for (i = 0; i < m; i++) { while (qs < qe && a[deq[qs]].x + n < a[i].x) { ans += ((long long int)a[deq[qs]].x + n - j) * a[deq[qs]].p; j = a[deq[qs]].x + n; qs++; } ans += ((long long int)a[i].x - j) * a[deq[qs]].p; j = a[i].x; while (qs < qe && a[deq[qe - 1]].p > a[i].p) { qe--; } deq[qe] = i; qe++; } printf( %I64d , ans); } |
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; bool size = false, small = false, cap = false, digit = false; if (s.size() >= 5) { size = true; for (int i = 0; i < s.size(); i++) { if (s[i] == ! || s[i] == _ || s[i] == ; || s[i] == , || s[i] == . ) continue; else if (s[i] >= a && s[i] <= z ) small = true; else if (s[i] >= A && s[i] <= Z ) cap = true; else if (s[i] >= 48 && s[i] <= 57) digit = true; } if (size && small && cap && digit) { cout << Correct << endl; return 0; } else { cout << Too weak << endl; return 0; } } else { cout << Too weak << endl; return 0; } return 0; } |
#include <bits/stdc++.h> using namespace std; string q; string d; int b; void f(string s, int p) { char a = 0; int c = 0; if (s.size() <= p) return; for (int i = p; i < s.size(); i++) { if (s[i] > a) { a = s[i]; b = i; c = 1; } else if (s[i] == a) { b = i; c++; } } for (int i = 0; i < c; i++) cout << a; f(s, b + 1); } int main() { cin >> q; f(q, 0); return 0; } |
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == - ) f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - 0 ; c = getchar(); } return x * f; } const int mod = 998244353; inline int mul(int x, int y) { return (long long)x * y % mod; } inline void sub(int &x, int y) { y = x - y; x = y < 0 ? y + mod : y; } inline void add(int &x, int y) { y += x; x = y >= mod ? y - mod : y; } inline int inc(int x, int y) { x += y; return x >= mod ? x - mod : x; } inline int qpow(int x, int y) { int res = 1; for (; y; y >>= 1, x = mul(x, x)) if (y & 1) res = mul(res, x); return res; } inline int inv(int x) { return x ? qpow(x, mod - 2) : 1; } const int maxn = 3005; const int maxm = 305; int n, c, a[maxn]; namespace task1 { int f[maxn][maxm], g[maxn][maxm], pw[maxn], sum[maxn], ipw[maxn]; void solve() { pw[0] = ipw[0] = 1; for (int i = 1; i <= n; i++) pw[i] = mul(pw[i - 1], 2); for (int i = 1; i <= n; i++) ipw[i] = inv(pw[i] - 1); int m = n / c; f[n + 1][0] = g[n + 1][0] = 1; for (int i = n; i >= 1; i--) { int cnt = 0, coef = 1; memset(sum, 0, sizeof sum); for (int j = i; j <= n; j++) { if (a[j] != a[i]) { if (!sum[a[j]]) ++sum[a[j]], ++cnt; else coef = mul(coef, mul(ipw[sum[a[j]]], pw[sum[a[j]] + 1] - 1)), sum[a[j]]++; } else { if (!sum[a[j]]) ++sum[a[j]], ++cnt; else coef = mul(coef, 2); } if (cnt == c && a[i] != a[j]) { int now_coef = mul(coef, ipw[sum[a[j]]]); for (int k = 1; k <= m; k++) if (g[j + 1][k - 1]) add(f[i][k], mul(now_coef, g[j + 1][k - 1])); } } int all = pw[n - i]; for (int k = 1; k <= m; k++) g[i][k] = inc(g[i + 1][k], f[i][k]); for (int k = 1; k <= m; k++) sub(all, f[i][k]); f[i][0] = all, g[i][0] = inc(g[i + 1][0], all); } sub(g[1][0], 1); for (int i = 0; i <= m; i++) printf( %d , g[1][i]); for (int i = m + 1; i <= n; i++) printf( 0 ); putchar(10); } } // namespace task1 namespace task2 { int f[2][maxn][(1 << 10) + 10]; void solve() { f[0][0][0] = 1; int mx = (1 << c), m = n / c; for (int i = 0; i < n; i++) { int np = i & 1, p = np ^ 1; memset(f[p], 0, sizeof f[p]); for (int j = 0; j <= m; j++) for (int k = 0; k < mx; k++) if (f[np][j][k]) { int now = f[np][j][k]; add(f[p][j][k], now); int t = k | (1 << (a[i + 1] - 1)); if (t == mx - 1) add(f[p][j + 1][0], now); else add(f[p][j][t], now); } } for (int i = 0; i <= n; i++) { int s = 0; for (int j = 0; j < mx; j++) add(s, f[n & 1][i][j]); if (!i) sub(s, 1); printf( %d , s); } putchar(10); } } // namespace task2 int main() { n = read(), c = read(); for (int i = 1; i <= n; i++) a[i] = read(); if (c <= 10) task2::solve(); else task1::solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m; vector<vector<int> > G; double dfs(int u, int p = -1) { double res = 0; int cnt = 0; for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (v == p) continue; cnt++; res += dfs(v, u); } return (cnt ? res / cnt + 1 : 0); } int main() { cin >> n; m = n - 1; G.resize(n + 1); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } cout << setprecision(7) << fixed << dfs(1); return 0; } |
#include <bits/stdc++.h> using namespace std; int n; int x[1000005], y[1000005]; int s[1000005], _sum[1000006], _sumx[1000006], _sumy[1000006], *sum = _sum + 1, *sumx = _sumx + 1, *sumy = _sumy + 1; long long _tot[1000006], *tot = _tot + 1; const int MOD = 1e9 + 7; inline int area(int x, int y, int xx, int yy) { int ret = ((long long)x * yy - (long long)xx * y) % MOD; if (ret < 0) ret += MOD; return ret; } inline long long areaL(int x, int y, int xx, int yy) { return (long long)x * yy - (long long)xx * y; } int main() { scanf( %d , &n); for (int i = 0; i < (int)(n); ++i) scanf( %d%d , x + i, y + i); int ans = 0, mul = -1; for (int times = 0; times < (int)(2); ++times) { for (int i = 0; i < (int)(n + 5); ++i) { x[n + i] = x[i]; y[n + i] = y[i]; } int N = 2 * n + 4; for (int i = 0; i < (int)(N); ++i) s[i] = area(x[i], y[i], x[i + 1], y[i + 1]); tot[-1] = sum[-1] = sumx[-1] = sumy[-1] = 0; for (int i = 0; i < (int)(N); ++i) tot[i] = tot[i - 1] + areaL(x[i], y[i], x[i + 1], y[i + 1]); for (int i = 0; i < (int)(N); ++i) sum[i] = (sum[i - 1] - (long long)i * s[i]) % MOD; for (int i = 0; i < (int)(N); ++i) sumx[i] = (sumx[i - 1] + x[i]) % MOD; for (int i = 0; i < (int)(N); ++i) sumy[i] = (sumy[i - 1] + y[i]) % MOD; for (int i = 0, j = 1; i < n; ++i) { if (j == i) ++j; while (j < i + n - 2 && abs(tot[j] - tot[i - 1] + areaL(x[j + 1], y[j + 1], x[i], y[i])) + times <= abs(tot[n + i - 1] - tot[j] + areaL(x[i], y[i], x[j + 1], y[j + 1]))) { ++j; } if (j == i + 1) continue; ans = (ans + mul * (sum[j - 1] - sum[i - 1] + (tot[j - 1] - tot[i - 1]) % MOD * j - s[i])) % MOD; ans = (ans + mul * areaL(sumx[j] - sumx[i + 1], sumy[j] - sumy[i + 1], x[i], y[i])) % MOD; } if (times == 0) { reverse(x + 1, x + n); reverse(y + 1, y + n); mul = 1; } } int ss = 0; for (int i = 0; i < (int)(n); ++i) ss = (ss + area(x[i], y[i], x[i + 1], y[i + 1])) % MOD; ans = ((long long)ss * ((long long)n * (n - 3) / 2 % MOD) - ans) % MOD; if (ans < 0) ans += MOD; printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; struct node { int l, c; } a[100005]; int b, c, d, e, f[205], g, h, i, j, k, l, m, n, s[100005]; inline bool cmp(node a, node b) { return a.l < b.l; } int main() { scanf( %d , &n); for (i = 1; i <= n; i++) scanf( %d , &a[i].l); for (i = 1; i <= n; i++) scanf( %d , &a[i].c); sort(a + 1, a + 1 + n, cmp); for (i = 1; i <= n; i++) s[i] = s[i - 1] + a[i].c; h = s[n]; for (i = 1; i <= n; i = j + 1) { for (j = i; a[j + 1].l == a[i].l; j++) ; k = j - i + 1; g = s[n] - s[j]; for (l = 1, d = i - k; l <= 200 && d > 0; l++) { if (d > f[l]) { d = d - f[l]; g = g + f[l] * l; } else { g = g + d * l; d = 0; } } h = min(h, g); for (l = i; l <= j; l++) f[a[l].c]++; } printf( %d n , h); return 0; } |
#include <bits/stdc++.h> using namespace std; int gcd(int u, int v) { return (v != 0) ? gcd(v, u % v) : u; } struct disjoint_sets { int parent[100000 + 10]; int ccOfParent[100000 + 10]; int cc; disjoint_sets(int V) { cc = V; for (int i = 1; i <= V; i++) parent[i] = i, ccOfParent[i] = 1; } int getParent(int a) { if (parent[a] == a) return a; return parent[a] = getParent(parent[a]); } void link(int a, int b) { ccOfParent[getParent(b)] += ccOfParent[getParent(a)]; ccOfParent[getParent(a)] = ccOfParent[getParent(b)]; parent[getParent(a)] = getParent(b); } bool unionSets(int a, int b) { int p1 = getParent(a); int p2 = getParent(b); if (p1 == p2) return 0; link(a, b); cc--; return 1; } }; vector<int> adj[100000 + 10]; int V, E; vector<pair<int, int> > vec; bool vis[100000 + 10] = {0}; double solve() { disjoint_sets s(V); long long total = 0; for (int i = 0; i < V; i++) { int a = vec[i].second; long long cost = vec[i].first; for (int j = 0; j < adj[a].size(); j++) { int b = adj[a][j]; if (!vis[b]) continue; int cc1 = s.ccOfParent[s.getParent(a)]; int cc2 = s.ccOfParent[s.getParent(b)]; if (s.unionSets(a, b)) total += 2 * cost * cc1 * cc2; } vis[a] = 1; } return (total / ((double)V * (V - 1))); } int main() { ios_base::sync_with_stdio(false); cin >> V >> E; vec.resize(V); for (int i = 1; i <= V; i++) cin >> vec[i - 1].first, vec[i - 1].second = i; int a, b; for (int j = 0; j < E; j++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } sort(vec.rbegin(), vec.rend()); double res = solve(); cout << res << endl; } |
#include <bits/stdc++.h> using namespace std; int intcmp(const void *v1, const void *v2) { return *(int *)v1 - *(int *)v2; } long long n; long long x[1010]; long long y[1010]; int main() { cin >> n; for (int i = 0; i < n / 3; i++) { printf( %d %d n , 0, i * 2 + 1); } for (int i = 0; i < n - n / 3; i++) { printf( %d %d n , 3, i); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 2010; struct Point { long long x, y; int id; Point(int _x = 0, int _y = 0, int _id = 0) : x(_x), y(_y), id(_id) {} void read(int i) { id = i; scanf( %lld%lld , &x, &y); } } ps[maxn]; Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } long long cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } Point base; bool cmp(Point a, Point b) { return cross(a - base, b - base) > 0; } char s[maxn]; int vis[maxn], ans[maxn]; Point t[maxn]; int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) { ps[i].read(i); } Point start = ps[1]; for (int i = 2; i <= n; i++) { if (ps[i].y < start.y) start = ps[i]; } ans[1] = start.id; vis[start.id] = 1; base = start; scanf( %s , s + 1); for (int i = 2; i < n; i++) { int cnt = 0; for (int j = 1; j <= n; j++) if (!vis[j]) t[cnt++] = ps[j]; sort(t, t + cnt, cmp); if (s[i - 1] == L ) { ans[i] = t[0].id; vis[t[0].id] = 1; base = t[0]; } else { ans[i] = t[cnt - 1].id; vis[t[cnt - 1].id] = 1; base = t[cnt - 1]; } } for (int i = 1; i <= n; i++) if (!vis[i]) ans[n] = i; for (int i = 1; i <= n; i++) printf( %d , ans[i]); printf( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long a, b, f = 0; long long min = 500, max = 0; string s = ; long long suma = 0, sumb = 0; for (int i = 0; i < n; i++) { cin >> a >> b; if (abs((suma + a) - sumb) <= 500) { s += A ; suma += a; } else if (abs(suma - (sumb + b)) <= 500) { s += G ; sumb += b; } else { cout << -1 n ; f = 1; break; } } if (f == 0) cout << s << endl; } |
#include <bits/stdc++.h> using namespace std; const double Pi = acos(-1.0); int main() { int n, m, cnt, i, j; while (~scanf( %d%d , &n, &m)) { if ((n % 2 == 0 && m * 2 <= n * n) || (n % 2 && m * 2 <= n * n + 1)) { printf( YES n ); cnt = 0; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (i % 2 == j % 2 && cnt < m) { printf( L ); cnt++; } else printf( S ); } printf( n ); } } else printf( NO n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int C = 26; void add(int &x, int y) { x += y; while (x >= MOD) x -= MOD; while (x < 0) x += MOD; } int fix(int x) { while (x >= MOD) x -= MOD; while (x < 0) x += MOD; return x; } int pw(int a, int b) { int ret = 1; while (b) { if (b & 1) ret = 1ll * ret * a % MOD; b >>= 1; a = 1ll * a * a % MOD; } return ret; } const int MAXN = 1e6 + 10; bool pal(string s) { string t = s; reverse(t.begin(), t.end()); return s == t; } void solve() { string s; cin >> s; int n = s.size(); int ans = 10; int m = (n + 1) / 2; for (int i = m; i + 1 < n; i++) { if (s[i] != s[i + 1]) { ans = 3 + n % 2; break; } } for (int i = 0; i < n / 2; i++) { string t = s.substr(0, i + 1); if (!pal(t)) { ans = 2; break; } } for (int i = 1; i < n; i++) { string t = s.substr(i) + s.substr(0, i); if (t != s && pal(t)) { ans = 1; break; } } if (ans < 10) { cout << ans << n ; } else { cout << Impossible n ; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int te = 1; for (int w = 1; w <= te; w++) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; using llong = long long; inline char Flip(char x) { return (x == 0 ) ? 1 : 0 ; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; vector<int> ans; while (t--) { int n; cin >> n; n *= 2; vector<int> a(n); for (int& i : a) cin >> i; vector<int> b; int i = n - 1; while (i >= 0) { int j = max_element(a.begin(), a.begin() + i + 1) - a.begin(); b.push_back(i - j + 1); i = j - 1; } bitset<4001> dp; dp[0] = 1; for (int len : b) dp = dp | (dp << len); if (dp[n / 2]) cout << YES n ; else cout << NO n ; } } |
#include <bits/stdc++.h> using namespace std; int n, m, u, v, flag, num, vis[100100]; vector<int> edge[100100]; queue<int> sk[100100]; struct G { int a, b, c; } ans[100100]; void dfs(int u, int father) { vis[u] = true; for (int i = 0; i < edge[u].size(); ++i) { int v = edge[u][i]; if (vis[v] == false) dfs(v, u); else if (v != father && vis[v] == true) sk[v].push(u); } while (sk[u].size() >= 2) { int x = sk[u].front(); sk[u].pop(); int y = sk[u].front(); sk[u].pop(); ans[++num] = {x, u, y}; } if (sk[u].size() == 1) { if (father == 0) flag = 1; int x = sk[u].front(); sk[u].pop(); ans[++num] = {x, u, father}; } else sk[father].push(u); } int main() { cin >> n >> m; if (m % 2 == 1) { cout << No solution << endl; return 0; } for (int i = 1; i <= m; ++i) { cin >> u >> v; edge[u].push_back(v); edge[v].push_back(u); } dfs(1, 0); if (flag == 1) cout << No solution << endl; else { for (int i = 1; i <= num; ++i) { cout << ans[i].a << << ans[i].b << << ans[i].c << endl; } } return 0; } |
#include <bits/stdc++.h> int main() { int n, k, i, start; bool b; while (scanf( %d%d , &n, &k) != EOF) { if (n < k || k == 1 && n > 1) { printf( -1 n ); continue; } start = k; for (i = b = 0; i < n; i++) { if (b && n - i == k - 2) { start = 2; break; } if (!b && n - i == k - 1) { start = 1; break; } printf(i& 1 ? b = 1, b : a ); } for (i = start; i < k; i++) printf( %c , i + a ); printf( n ); } return 0; } |
#include<bits/stdc++.h> #define pb push_back using namespace std; typedef long long ll; const int maxn=1e6+100; const int M=1000000007; typedef array<int,41> A; struct hasher { size_t operator()(const A& p) const { size_t result = 0; for (int i=1;i<=40;i++) result = result * 19260817 + p[i]; return result; } }; bool vis[50]; int f[50],n,mx,F[50][50],G[50][50],p[50],nf[50],inv[50]; int ways[50][50][50]; unordered_map<A,int,hasher> dp[41][41][41]; void add(int &x,int y){x+=y;if (x>=M)x-=M;} int C(int x,int y){ if (y<0||y>x) return 0; return 1ll*f[x]*nf[y]%M*nf[x-y]%M; } void init(){ inv[1]=1; for (int i=2;i<=n;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M; f[0]=nf[0]=1; for (int i=1;i<=n;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M; F[0][0]=f[n-1]; for (int i=0;i<=n-1;i++) for (int j=1;j+i<=n-1;j++){ F[i][j]=0; for (int x=0;x<=i;x++) for (int y=0;y<=j;y++){ add(F[i][j],1ll*C(i,x)*C(j,y)%M*f[n-1-x-y]%M*f[x+y]%M); if (!y) add(G[i][j],1ll*C(i,x)*C(j,y)%M*f[n-1-x-y]%M*f[x+y]%M); } } for (int i=0;i<=n-1;i++) for (int j=0;j+i<=n-1;j++){ int x=1,y=1; for (int k=1;k<=n;k++){ x=1ll*x*F[i][j]%M; y=1ll*y*G[i][j]%M; ways[i][j][k]=(x-y+M)%M; } } } int solve(int lasttot,int tot,int block,A a){ if (dp[lasttot][tot][block].find(a)!=dp[lasttot][tot][block].end()){ return dp[lasttot][tot][block][a]; } int ret=0,sum=n; for (int i=1;i<=mx;i++) sum-=a[i]*i; if (sum==n) return dp[lasttot][tot][block][a]=nf[block]; A tmp=a; for (int i=1;i<=mx;i++) { if (a[i]){ tmp[i]--; add(ret,1ll*ways[sum-tot-lasttot][lasttot][i]*a[i]%M*solve(lasttot,tot+i,block+1,tmp)%M); if (block) add(ret,1ll*ways[sum-tot][tot][i]*a[i]%M*nf[block]%M*solve(tot,i,1,tmp)%M); tmp[i]++; } } return dp[lasttot][tot][block][a]=ret; } int main(){ scanf( %d ,&n); init(); for (int i=1;i<=n;i++) scanf( %d ,&p[i]); A a; for (int i=0;i<=40;i++) a[i]=0; for (int i=1;i<=n;i++) if (!vis[i]){ int w=0,tmp=i; while (!vis[tmp]){ w++; vis[tmp]=1; tmp=p[tmp]; } a[w]++; mx=max(mx,w); } printf( %d n ,solve(0,0,0,a)); return 0; } |
#include <bits/stdc++.h> using namespace std; const int NN = 1000100; int link[NN], fail[NN], len[NN], dif[NN], nxt[NN][26]; int ed, sz, cur; char s[NN], S[NN], T[NN]; int tp[NN], dp[NN], pre[NN], pr[NN]; void clear(int u, int L = 0) { len[u] = L; } void init() { sz = 2; ed = 1; cur = 0; s[cur] = -1; clear(0); clear(1, -1); fail[0] = 1; } inline int get_fail(int u) { while (s[cur - len[u] - 1] != s[cur]) u = fail[u]; return u; } int insert(char c) { s[++cur] = c -= a ; ed = get_fail(ed); if (!nxt[ed][c]) { int p1 = sz++; clear(p1, len[ed] + 2); fail[p1] = nxt[get_fail(fail[ed])][c]; nxt[ed][c] = p1; dif[p1] = len[p1] - len[fail[p1]]; if (dif[p1] == dif[fail[p1]]) link[p1] = link[fail[p1]]; else link[p1] = fail[p1]; } ed = nxt[ed][c]; return ed; } int main() { scanf( %s%s , S, T); int n = strlen(S); init(); memset(tp, 0x3f, sizeof tp); for (int i = 1, now; i <= n * 2; i++) { if (i & 1) now = insert(S[i - 1 >> 1]); else now = insert(T[i - 1 >> 1]); dp[i] = 0x3f3f3f3f; if ((~i & 1) && S[i - 1 >> 1] == T[i - 1 >> 1]) { dp[i] = dp[i - 2]; pre[i] = i - 2; } for (int u = now; u; u = link[u]) { pr[u] = i - len[link[u]] - dif[u]; tp[u] = dp[pr[u]]; if (link[u] ^ fail[u]) { if (tp[u] > tp[fail[u]]) { tp[u] = tp[fail[u]]; pr[u] = pr[fail[u]]; } } if (~i & 1) { if (dp[i] > tp[u] + 1) { dp[i] = tp[u] + 1; pre[i] = pr[u]; } } } } if (dp[n * 2] == 0x3f3f3f3f) return puts( -1 ); vector<pair<int, int>> ans; int now = n * 2; while (now) { int pre = ::pre[now]; if (now - pre > 2) ans.emplace_back((pre >> 1) + 1, now >> 1); now = pre; } cout << ans.size() << endl; for (auto &a : ans) printf( %d %d n , a.first, a.second); return 0; } |
#include <bits/stdc++.h> using namespace std; struct Fenwick { vector<long long> BIT; long long N; Fenwick(long long n) { N = n + 2; BIT.resize(N + 10); } void reset() { for (long long i = 0; i <= N; i++) { BIT[i] = 0; } } void add(long long r, long long value) { for (long long i = r; i <= N; i = i | (i + 1)) { BIT[i] += value; } } long long qry(long long r) { long long ans = 0; for (long long i = r; i >= 0; i = (i & (i + 1)) - 1) { ans += BIT[i]; } return ans; } }; const long long N = (long long)1e6; void solve() { long long n, m; cin >> n >> m; vector<array<long long, 3>> x(n), y(m); for (auto &i : x) { cin >> i[0] >> i[1] >> i[2]; } for (auto &i : y) { cin >> i[0] >> i[1] >> i[2]; } m += 1; y.push_back({N, 0, N}); long long ans = 0; Fenwick sg(N + 10); sg.add(N, 1); sg.add(0 * 1LL, 1); sort(y.begin(), y.end()); vector<vector<long long>> add(N + 10), remove(N + 10); for (auto &i : x) { if (i[1] == 0) { add[0].push_back(i[0]); remove[i[2]].push_back(i[0]); } else { add[i[1]].push_back(i[0]); remove[N - 1].push_back(i[0]); } } long long cur = 0; 42; for (long long i = 0; i <= N; i++) { for (auto &j : add[i]) { sg.add(j, 1); } while (cur < m and y[cur][0] == i) { long long u = max(y[cur][1], y[cur][2]); long long v = min(y[cur][1], y[cur][2]); if (v == 0) { ans += (sg.qry(u) - 1); } else { ans += (sg.qry(u) - sg.qry(v - 1) - 1); } cur += 1; } for (auto &j : remove[i]) { sg.add(j, -1); } } cout << ans << n ; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); } |
#include <bits/stdc++.h> using namespace std; const long long LINF = 1e18; const long long INF = 1e9; const long long MOD = 1000000007; const long long MAX = 100005; #pragma GCC optimize( O3 ) #pragma GCC optimize( Ofast ) #pragma GCC optimize( unroll-loops ) #pragma GCC optimize( -ffloat-store ) #pragma GCC optimize( -fno-defer-pop ) long long strtoll(string second) { stringstream str(second); long long number = 0; str >> number; return number; } string lltostr(long long n) { stringstream ss; ss << n; return ss.str(); } bool prime[MAX]; void sieve() { memset(prime, true, sizeof(prime)); prime[0] = prime[1] = false; for (long long p = 2; p * p <= MAX; p++) { if (prime[p] == true) { for (long long i = p * 2; i <= MAX; i += p) prime[i] = false; } } } long long power(long long a, long long b, long long m) { if (b == 0) return 1; if (b == 1) return a % m; long long t = power(a, b / 2, m); t = (t * t) % m; if (b & 1) t = (t * a) % m; return t; } long long modInverse(long long a, long long m) { return power(a, m - 2, m); } long long __gcd(long long a, long long b) { if (b == 0) return a; return __gcd(b, a % b); } bool cmp(const pair<long long, long long> &l, const pair<long long, long long> &r) { return l.first < r.first; } void solve() { long long n, m; cin >> n >> m; long long a[n + 1], c[n + 1]; for (long long i = 0; i < n; i++) cin >> a[i + 1]; for (long long i = 0; i < n; i++) cin >> c[i + 1]; map<long long, long long> mm; set<pair<long long, long long> > second; for (long long i = 1; i <= n; i++) second.insert({c[i], i}); for (long long i = 1; i <= n; i++) mm[i] = a[i]; while (m--) { long long ans = 0; long long type, q; cin >> type >> q; if (mm[type] >= q) { ans += c[type] * q; mm[type] -= q; } else { ans += mm[type] * c[type]; q -= mm[type]; mm[type] = 0; while (q && !second.empty()) { pair<long long, long long> p = *second.begin(); long long x = p.first; long long y = p.second; if (mm[y] >= q) { mm[y] -= q; ans += (c[y] * q); q = 0; } else { ans += mm[y] * c[y]; q -= mm[y]; mm[y] = 0; second.erase(second.begin()); } } if (q != 0) ans = 0; } cout << ans << n ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); ; long long t; t = 1; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; map<int, vector<int> > map1; for (int i = 1; i <= n; i++) { int a; cin >> a; map1[i].push_back(a); map1[a].push_back(i); } int counter = 0; map<int, bool> check; for (int i = 1; i <= n; i++) { if (!check[i]) { counter++; queue<int> que; que.push(i); while (que.size()) { int front = que.front(); que.pop(); if (!check[front]) for (int j = 0; j < map1[front].size(); j++) { que.push(map1[front][j]); } check[front] = true; } } } cout << counter; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 10; const long long inf = 1e18; const long long mod = 1e9 + 7; long long n, q; long long a[maxn]; long long cost(long long g) { long long ans = 0; for (long long i = 1; i <= n; i++) { if (a[i] < g) ans += g - a[i]; else ans += min(a[i] % g, g - a[i] % g); } return ans; } long long ans = inf; void DO(long long x) { if (x <= 1) return; for (long long i = 2; i <= sqrt(x); i++) if (x % i == 0) { ans = min(ans, cost(i)); while (x % i == 0) x /= i; } if (x > 1) ans = min(ans, cost(x)); } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (long long i = 1; i <= n; i++) cin >> a[i]; random_shuffle(a + 1, a + n + 1); for (long long i = 1; i <= 10; i++) DO(a[i] - 1), DO(a[i]), DO(a[i] + 1); for (long long i = n / 3; i <= n / 3 + 15; i++) DO(a[i] - 1), DO(a[i]), DO(a[i] + 1); for (long long i = 2 * n / 3; i <= 2 * n / 3 + 15; i++) DO(a[i] - 1), DO(a[i]), DO(a[i] + 1); for (long long i = n; i >= n - 10; i--) DO(a[i] - 1), DO(a[i]), DO(a[i] + 1); cout << ans; } |
#include <bits/stdc++.h> using namespace std; long long h, n; int main() { cin >> h >> n; long long ans = 0; long long l = 1, r = (1LL << h); int dir = 0; for (int i = h - 1; i >= 0; i--) { ans++; long long mid = (l + r) / 2; if (dir == 0) { if (mid < n) { ans += (1LL << (i + 1)) - 1; dir = 0; l = mid + 1; } else { r = mid; dir = 1; } } else { if (n <= mid) { ans += (1LL << (i + 1)) - 1; dir = 1; r = mid; } else { l = mid + 1; dir = 0; } } } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int two = 0, one = 0; int x[n + 1000]; for (int i = 0; i < n; ++i) cin >> x[i]; for (int i = 0; i < n; ++i) { if (x[i] == 2) ++two; else ++one; } if (two == 0) { for (int i = 0; i < one; ++i) cout << 1 ; } else if (one == 0) { for (int i = 0; i < two; ++i) cout << 2 ; } else { --two; cout << 2 ; --one; cout << 1 ; for (int i = 0; i < two; ++i) cout << 2 ; for (int i = 0; i < one; ++i) cout << 1 ; } } |
#include <bits/stdc++.h> using namespace std; template <class T> T sqr(T x) { return x * x; } const int maxn = 3 * (int)1e5 + 5; int n; int a[maxn]; int p[maxn]; int main() { srand(time(NULL)); ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; p[a[i]] = i; } int q; cin >> q; while (q--) { int tst, x, y; cin >> tst >> x >> y; if (tst == 2) { swap(a[x], a[y]); p[a[x]] = x; p[a[y]] = y; } else { int ans = 1; int last = -1; for (int i = x; i <= y; ++i) { if (p[i] < last) ++ans; last = p[i]; } cout << ans << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string colors = ROYGBIV ; string remainder = GBIVGB ; string answer = ; for (int i = 0; i < n / 7; ++i) { answer += colors; } for (int i = 0; i < n % 7; ++i) { answer += remainder[i]; } cout << answer; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7, N = 1e5 + 5; int n, m, sta[N], tp, tot, siz[N], low[N], dfn[N], be[N], bl, dep[N], f[N][22], w[N]; vector<int> e[N], vec[N]; int read() { int x = 0, f = 1; char s; while ((s = getchar()) > 9 || s < 0 ) if (s == - ) f = -1; while (s >= 0 && s <= 9 ) x = (x << 1) + (x << 3) + (s ^ 48), s = getchar(); return x * f; } void addEdge(int u, int v) { e[u].push_back(v); e[v].push_back(u); } void tarjan(int u, int fa) { sta[++tp] = u; dfn[u] = low[u] = ++tot; for (int i = 0; i < e[u].size(); ++i) { int v = e[u][i]; if (v == fa) continue; if (!dfn[v]) tarjan(v, u), low[u] = min(low[u], low[v]); else low[u] = min(low[u], dfn[v]); } if (low[u] == dfn[u]) { int v = 0; ++bl; do { v = sta[tp--]; be[v] = bl; ++siz[bl]; } while (v ^ u); } } int LCA(int u, int v) { if (dep[u] < dep[v]) swap(u, v); for (int i = 20; i >= 0; --i) if (dep[f[u][i]] >= dep[v]) u = f[u][i]; if (u == v) return u; for (int i = 20; i >= 0; --i) if (f[u][i] ^ f[v][i]) u = f[u][i], v = f[v][i]; return f[u][0]; } void dfs(int u, int fa) { dep[u] = dep[fa] + 1; f[u][0] = fa; w[u] = w[fa] + (siz[u] > 1); for (int i = 1; i <= 20; ++i) f[u][i] = f[f[u][i - 1]][i - 1]; for (int i = 0; i < vec[u].size(); ++i) if (vec[u][i] ^ fa) dfs(vec[u][i], u); } int qkpow(int x, int y) { int r = 1; while (y) { if (y & 1) r = 1ll * r * x % mod; x = 1ll * x * x % mod; y >>= 1; } return r; } int main() { n = read(), m = read(); for (int i = 1; i <= m; ++i) addEdge(read(), read()); tarjan(1, 0); for (int i = 1; i <= n; ++i) for (int j = 0; j < e[i].size(); ++j) if (be[i] ^ be[e[i][j]]) vec[be[i]].push_back(be[e[i][j]]); dfs(1, 0); int u, v; for (int t = read(); t; --t) { u = be[read()], v = be[read()]; int lca = LCA(u, v); printf( %d n , qkpow(2, w[u] + w[v] - w[lca] * 2 + (siz[lca] > 1))); } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 7; const long long INF = 1e18 + 5; const long long maxn = 5e5 + 5; long long n = 100000000000, a[1555555], d[555555], mx = -1000000001; long long u[1511111], b[1100005], sum, cnt, t[555555]; long long bi(long long x, long long y) { if (y == 0) return 1ll; if (y == 1) return x; long long z = bi(x, y / 2); z = z * z % n; if (y & 1) z = z * x % n; return z % n; } pair<int, int> p[555555]; vector<pair<int, int> > v, x; int c[555555], l[555555], y[555555], k[555555]; long long dp[555555][3][3]; int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(NULL); int n, x; cin >> n >> x; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) for (int j = 0; j <= 2; j++) for (int k = 0; k <= 2; k++) dp[i][j][k] = -1e15; dp[0][0][0] = 0; for (int i = 1; i <= n; i++) for (int j = 0; j <= 2; j++) for (int k = 0; k <= 2; k++) { if (j > 0) dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]); if (k > 0) dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1]); dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k] + (j == 1 ? a[i] : 0) * (k == 1 ? x : 1)); } cout << dp[n][2][2]; return 0; } |
#include <bits/stdc++.h> using namespace std; const double pi = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862; void gn(int &x) { int sg = 1; char c; while (((c = getchar()) < 0 || c > 9 ) && c != - ) ; if (c == - ) sg = -1, x = 0; else x = c - 0 ; while ((c = getchar()) >= 0 && c <= 9 ) x = x * 10 + c - 0 ; x *= sg; } void gn(long long &x) { int sg = 1; char c; while (((c = getchar()) < 0 || c > 9 ) && c != - ) ; if (c == - ) sg = -1, x = 0; else x = c - 0 ; while ((c = getchar()) >= 0 && c <= 9 ) x = x * 10 + c - 0 ; x *= sg; } int mo = 1000000007; int inf = 1061109567; double eps = 1e-6; int qp(int a, long long b) { int ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int a[333]; int n; void out(int i, int t) { for (int j = 0; j < t; j++) putchar(i - 1 + a ); } int ord[333]; int main() { gn(n); for (int i = 1; i <= n; i++) gn(a[i]); int su = 0; int numodd = 0; for (int i = 1; i <= n; i++) numodd += a[i] & 1, su += a[i]; if (numodd > 1) { printf( 0 n ); for (int i = 1; i <= n; i++) out(i, a[i]); putchar( n ); return 0; } int d = 0; for (int i = 1; i <= n; i++) d = gcd(d, a[i]); int no1 = 0; for (int i = 1; i <= n; i++) no1 += (a[i] / d) & 1; if (no1 > 1 && d % 2 == 0) d /= 2, no1 = 0; for (int i = 1; i <= n; i++) a[i] /= d; for (int i = 1; i <= n; i++) ord[i] = i; for (int i = 1; i <= n; i++) if (a[i] & 1) swap(ord[i], ord[n]); if (no1) { printf( %d n , d); } else printf( %d n , d * 2); for (int tt = 1; tt <= d; tt++) { for (int i = 1; i <= n - 1; i++) { out(ord[i], a[ord[i]] / 2); } out(ord[n], a[ord[n]]); for (int i = n - 1; i >= 1; i--) { out(ord[i], a[ord[i]] / 2); } } return 0; } |
#include <bits/stdc++.h> using namespace std; int pct(int x) { return __builtin_popcount(x); } int pct(long long x) { return __builtin_popcountll(x); } int bt(int x) { return 31 - __builtin_clz(x); } int bt(long long x) { return 63 - __builtin_clzll(x); } int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); } long long cdiv(long long a, long long b) { return a / b + !(a < 0 || a % b == 0); } int nxt_C(int x) { int c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; } long long nxt_C(long long x) { long long c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; } vector<int> get_bits(int mask) { vector<int> bb; while (mask) { int b = bt(mask); bb.push_back(b); mask ^= (1 << b); } reverse(bb.begin(), bb.end()); return bb; } int get_mask(vector<int> v) { int mask = 0; for (int x : v) { mask ^= (1 << x); } return mask; } template <typename T> void uniq(vector<T> &v) { sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long rand(long long l, long long r) { uniform_int_distribution<long long> uid(l, r); return uid(rng); } void sc() {} template <typename Head, typename... Tail> void sc(Head &H, Tail &...T) { cin >> H; sc(T...); } const int mod = 1e9 + 7; int pwr(int a, long long b) { int ans = 1; while (b) { if (b & 1) ans = (ans * 1LL * a) % mod; a = (a * 1LL * a) % mod; b >>= 1; } return ans; } const int N = 1005; vector<int> s[N]; int in[N]; int query(vector<int> v) { cout << ? << (int)v.size() << ; for (int x : v) { cout << x << ; } cout << endl; int y; sc(y); return y; } int go(int l, int r, int maxi) { if (l == r) return l; int m = (l + r) >> 1; vector<int> v; for (int i = l; i <= m; i++) { v.push_back(i); } int q = query(v); if (q == maxi) { return go(l, m, maxi); } else { return go(m + 1, r, maxi); } } void solve() { memset(in, 0, sizeof in); int n, k; sc(n, k); for (int i = 1; i <= k; i++) { s[i].clear(); int c; sc(c); for (int j = 1; j <= c; j++) { int x; sc(x); s[i].push_back(x); in[x] = i; } } vector<int> v; for (int i = 1; i <= n; i++) { v.push_back(i); } int maxi = query(v); int idx = go(1, n, maxi); int kk = in[idx]; v.clear(); for (int i = 1; i <= n; i++) { if (in[i] != kk) v.push_back(i); } int z = query(v); cout << ! ; for (int i = 1; i <= k; i++) { if (i == kk) { cout << z << ; } else { cout << maxi << ; } } cout << endl; string s; sc(s); if (s == Incorrect ) { exit(0); } } int main() { ios ::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; for (int tt = 1; tt <= t; tt++) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; struct number { long long sum, a, b; bool operator<(const number& in) const { return sum < in.sum; } bool operator>(const number& in) const { return sum > in.sum; } }; vector<number> v; const long long MN = 2000 + 1; long long a[MN], b[MN]; long long n, m, suma, sumb; int main() { cin >> n; for (long long i = 0; i < n; i++) { cin >> a[i]; suma += a[i]; } cin >> m; for (long long j = 0; j < m; j++) { cin >> b[j]; sumb += b[j]; } for (long long i = 0; i < m; i++) for (long long j = i + 1; j < m; j++) { number temp; temp.a = i; temp.b = j; temp.sum = 2 * (b[i] + b[j]); v.push_back(temp); } sort(v.begin(), v.end()); long long best = abs(suma - sumb), in1 = -1, in2, in3 = -1; for (long long i = 0; i < n; i++) for (long long j = 0; j < m; j++) if (best > abs(suma - sumb - a[i] - a[i] + b[j] + b[j])) { best = abs(suma - sumb - a[i] - a[i] + b[j] + b[j]); in1 = i; in2 = j; } if (m > 1 && n > 1) { for (long long i = 0; i < n; i++) for (long long j = i + 1; j < n; j++) { long long want = suma - sumb - a[i] - a[j] - a[i] - a[j]; want *= -1; vector<number>::iterator low, up; number temp; temp.sum = want; low = std::lower_bound(v.begin(), v.end(), temp); up = upper_bound(v.begin(), v.end(), temp); if (low >= v.begin() && low < v.end()) if (best > abs(want - v[low - v.begin()].sum)) { best = abs(want - v[low - v.begin()].sum); in1 = i; in2 = j; in3 = low - v.begin(); } while (up >= v.end()) up--; if (best > abs(want - v[up - v.begin()].sum)) { best = abs(want - v[up - v.begin()].sum); in1 = i; in2 = j; in3 = up - v.begin(); } } } if (in1 == -1) { cout << best << endl << 0 << endl; return 0; } if (in3 == -1) { cout << best << endl << 1 << endl << in1 + 1 << << in2 + 1 << endl; return 0; } cout << best << endl; cout << 2 << endl; cout << in1 + 1 << << v[in3].a + 1 << endl; cout << in2 + 1 << << v[in3].b + 1 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3f; const int xx[4] = {1, -1, 0, 0}; const int yy[4] = {0, 0, 1, -1}; int inline nextin() { int t; if (scanf( %d , &t) != 1) exit(0); return t; } int inline in() { int x = 0, c; for (; (unsigned)((c = getchar()) - 0 ) >= 10;) { if (c == - ) return -in(); if (!~c) throw ~0; } do { x = (x << 3) + (x << 1) + (c - 0 ); } while ((unsigned)((c = getchar()) - 0 ) < 10); return x; } int n, k; string s[10005]; map<int, int> m; int rhyme(int pos) { string a = s[pos].substr(m[pos], ((int)(s[pos]).size()) - m[pos]); string b = s[pos + 1].substr(m[pos + 1], ((int)(s[pos + 1]).size()) - m[pos + 1]); string c = s[pos + 2].substr(m[pos + 2], ((int)(s[pos + 2]).size()) - m[pos + 2]); string d = s[pos + 3].substr(m[pos + 3], ((int)(s[pos + 3]).size()) - m[pos + 3]); if (a == b && b == c && c == d) return 0; if (a == b && c == d) return 1; if (a == c && b == d) return 2; if (a == d && b == c) return 3; return -1; } const string ans[4] = { aaaa , aabb , abab , abba }; int main() { cin.sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 0; i < (4 * n); i++) cin >> s[i]; for (int i = 0; i < (4 * n); i++) { int cnt = 0; bool ok = false; for (int j = ((int)(s[i]).size()) - 1; j >= 0; j--) { if (s[i][j] == a || s[i][j] == e || s[i][j] == i || s[i][j] == o || s[i][j] == u ) cnt++; if (cnt == k) { m[i] = j; ok = true; break; } } if (!ok) { puts( NO ); return 0; } } set<int> Set; for (int i = 0; i < (n); i++) { Set.insert(rhyme(i * 4)); } set<int>::iterator it = Set.begin(); if (*it == -1) puts( NO ); else if (((int)(Set).size()) >= 3) puts( NO ); else if (((int)(Set).size()) == 2) { if (*it == 0) { it++; cout << ans[*it] << endl; } else puts( NO ); } else cout << ans[*it] << endl; return 0; } |
#include <bits/stdc++.h> int main() { long long int a, i, j, k = 1; scanf( %lld , &a); for (i = 1; i <= a; i++) { k = k * 2; } k = 2 * (k - 1); printf( %lld n , k); return 0; } |
#include <bits/stdc++.h> using namespace std; int a[500500], b[500500]; int main() { int n, q; int p1 = 1; int p2 = 1; scanf( %d%d , &n, &q); for (int i = 1; i <= n; i++) { if (i % 2) a[i / 2 + 1] = i; else b[i / 2] = i; } int tt = 0; for (int i = 0; i < q; i++) { int tp; scanf( %d , &tp); if (tp == 2) { tt = 1 - tt; swap(p1, p2); } if (tp == 1) { int x; scanf( %d , &x); if (x % 2 == 0) { p1 = (p1 - (x / 2) + (n / 2) - 1) % (n / 2) + 1; p2 = (p2 - (x / 2) + (n / 2) - 1) % (n / 2) + 1; } else { x--; p1 = (p1 - (x / 2) + (n / 2) - 1) % (n / 2) + 1; p2 = (p2 - (x / 2) + (n / 2) - 1) % (n / 2) + 1; tt = 1 - tt; swap(p1, p2); p1 = (p1 - 1 + (n / 2) - 1) % (n / 2) + 1; } } } if (tt) swap(a, b); for (int i = 0; i < n / 2; i++) { printf( %d %d , a[p1], b[p2]); p1++; p2++; if (p1 > n / 2) p1 = 1; if (p2 > n / 2) p2 = 1; } return 0; } |
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; namespace number_theory { long long gcd(long long x, long long y) { if (x == 0) return y; if (y == 0) return x; return gcd(y, x % y); } bool isprime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } bool prime[15000105]; void sieve(long long n) { for (long long i = 0; i <= n; i++) prime[i] = 1; for (long long p = 2; p * p <= n; p++) { if (prime[p] == true) { for (long long i = p * p; i <= n; i += p) prime[i] = false; } } prime[1] = prime[0] = 0; } vector<long long> primelist; bool __primes_generated__ = 0; void genprimes(long long n) { __primes_generated__ = 1; sieve(n + 1); for (long long i = 2; i <= n; i++) if (prime[i]) primelist.push_back(i); } vector<long long> factors(long long n) { if (!__primes_generated__) { cerr << Caint genprimes you dope << endl; exit(1); } vector<long long> facs; for (long long i = 0; primelist[i] * primelist[i] <= n && i < primelist.size(); i++) { if (n % primelist[i] == 0) { while (n % primelist[i] == 0) { n /= primelist[i]; facs.push_back(primelist[i]); } } } if (n > 1) { facs.push_back(n); } sort(facs.begin(), facs.end()); return facs; } vector<long long> getdivs(long long n) { vector<long long> divs; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { divs.push_back(i); divs.push_back(n / i); } } { sort(divs.begin(), divs.end()); divs.erase(unique(divs.begin(), divs.end()), divs.end()); }; return divs; } } // namespace number_theory void solve() { long long n, k; cin >> n >> k; string s; cin >> s; long long win = 0, loss = 0, winning_streak_cnt = 0; vector<long long> lossing_streak; for (long long i = 0; i < n; i++) { if (s[i] == W ) { win++; if (i == 0 or s[i - 1] == L ) winning_streak_cnt++; } else { loss++; if (i == 0 or s[i - 1] == W ) lossing_streak.push_back(0); lossing_streak.back()++; } } if (k >= loss) { cout << 2 * n - 1 << n ; return; } if (win == 0) { if (k == 0) cout << 0; else cout << 2 * k - 1; cout << n ; return; } win += k; if (s[0] == L ) lossing_streak[0] = 1e8; if (s[n - 1] == L ) lossing_streak.back() = 1e8; sort(lossing_streak.begin(), lossing_streak.end()); for (long long ls : lossing_streak) { if (ls > k) break; k -= ls; winning_streak_cnt--; } cout << 2 * win - winning_streak_cnt << n ; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); long long t; cin >> t; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; #pragma warning(disable : 4996) int main(int argc, char **argv) { ifstream from; const int nmin = 2, nmax = 300, kmin = 1, kmax = 300; int n, rt, rt1, rt2, i, j, j0, j1, k; int err = 0; char ch; cin >> n; vector<vector<int>> d(n); for (i = 0; i < n; i++) d[i].resize(n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { cin >> d[i][j]; } } cin >> k; vector<int> a(k); vector<int> b(k); vector<int> c(k); for (i = 0; i < k; i++) { cin >> a[i] >> b[i] >> c[i]; a[i]--; b[i]--; } vector<long long> q(k); for (i = 0; i < k; i++) { if (d[a[i]][b[i]] > c[i]) { d[a[i]][b[i]] = c[i]; d[b[i]][a[i]] = c[i]; } q[i] = 0; for (j0 = 0; j0 < n - 1; j0++) { for (j1 = j0 + 1; j1 < n; j1++) { rt1 = d[j0][a[i]] + d[a[i]][b[i]] + d[b[i]][j1]; rt2 = d[j0][b[i]] + d[a[i]][b[i]] + d[a[i]][j1]; rt = (rt1 < rt2) ? rt1 : rt2; if (rt < d[j0][j1]) { d[j0][j1] = rt; d[j1][j0] = rt; } q[i] += d[j0][j1]; } } } for (i = 0; i < k - 1; i++) { cout << q[i] << ; } cout << q[k - 1] << endl; return 0; } |
Subsets and Splits