func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 5; int g[10][10]; int a[5]; int ans; int cal() { int sum = 0; for (int i = 0; i < 5; ++i) { for (int j = i; j < 4; j += 2) sum += g[a[j]][a[j + 1]] + g[a[j + 1]][a[j]]; } return sum; } int main() { for (int i = 0; i < 5; ++i) for (int j = 0; j < 5; ++j) cin >> g[i][j]; for (int i = 0; i < 5; ++i) a[i] = i; ans = -1; do { ans = max(ans, cal()); } while (next_permutation(a, a + 5)); cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; long long arr[330]; set<long long> st; map<long long, bool> mp; int main() { long long t, n, i, j, k, l, m; ios_base::sync_with_stdio(0); cin.tie(0); long long p; cin >> t; while (t--) { cin >> n; for (i = 1; i <= n; i++) cin >> arr[i], mp[arr[i]] = 1; sort(arr + 1, arr + n + 1); i = 1, j = n; while (i <= j) { long long op = arr[i] * arr[j]; st.insert(op); i++; j--; } if (st.size() == 1) { bool flag = 1; long long ans = *(st.begin()); for (i = 2; i <= sqrt(ans); i++) { if (ans % i == 0) { if (mp.find(i) == mp.end() || mp.find(ans / i) == mp.end()) flag = false; } } if (flag) cout << *(st.begin()) << n ; else cout << -1 << n ; } else cout << -1 << n ; st.clear(); mp.clear(); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, s; int even_odd[100005][2]; int visited[100005]; vector<int> ans; vector<int> v[100005]; int dfs(int s, int p) { if (v[s].empty() && p) { ans.push_back(s); return 1; } for (auto it : v[s]) { if (even_odd[it][p ^ 1] == 0) { even_odd[it][p ^ 1] = s; if (dfs(it, p ^ 1) == 1) { ans.push_back(s); return 1; } } } return 0; } bool dfs2(int source) { visited[source] = 1; for (auto it : v[source]) if (visited[it] == 1 || (visited[it] == 0 && dfs2(it))) return true; visited[source] = 2; return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int a; cin >> a; v[i + 1].push_back(a); } } cin >> s; if (dfs(s, 0)) { cout << Win n ; for (int i = ans.size() - 1; i >= 0; i--) cout << ans[i] << ; return 0; } if (dfs2(s)) { cout << Draw n ; return 0; } cout << Lose n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t; scanf( %lld , &t); while (t--) { long long a, b, c, d, k; scanf( %lld%lld%lld , &a, &b, &c); scanf( %lld %lld , &d, &k); long long x = (a - 1 + c) / c; long long y = (b + d - 1) / d; if (x + y <= k) { printf( %lld %lld n , k - y, y); } else { printf( -1 n ); } } return 0; }
#include <bits/stdc++.h> int a[100010], i, j, k, n, x, y; int main() { while (scanf( %d , &n) == 1) { if (n % 4 != 1 && n % 4 != 0) { printf( -1 n ); continue; } if (n == 1) { printf( 1 n ); continue; } if (n % 4 == 1) { a[n / 2 + 1] = n / 2 + 1; x = 1; y = n; while (y != x) { a[x] = x + 1; a[x + 1] = y; a[y - 1] = x; a[y] = y - 1; x += 2; y -= 2; } } else { x = 1; y = n; while (x - 1 != y) { a[x] = x + 1; a[x + 1] = y; a[y - 1] = x; a[y] = y - 1; x += 2; y -= 2; } } for (i = 1; i <= n - 1; i++) printf( %d , a[i]); printf( %d n , a[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; double n, sum = 0; int a[21], b[21]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < 1000000; i++) { for (int j = 0; j < n; j++) { if (i % a[j] == b[j]) { sum++; break; } } } printf( %.6f , (double)sum / 1000000.0); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxc = 1000000000; int main() { long long n, k, l, r; scanf( %lld%lld%lld%lld , &n, &l, &r, &k); long long d = r - l + 1; if (l > r) { d = n - l + 1 + r; } if (d == n) { long long ans = -1; for (long long i = 1; i * i <= k + 1; i++) { if (k % i == 0) { if (i >= n && i <= 2 * n) ans = max(ans, i - n); if (k / i >= n && k / i <= 2 * n) ans = max(ans, k / i - n); } if ((k + 1) % i == 0) { if (i > n && i <= 2 * n) ans = max(ans, i - n); if ((k + 1) / i > n && (k + 1) / i <= 2 * n) ans = max(ans, (k + 1) / i - n); } } printf( %lld n , ans); return 0; } if (n < 3e7) { long long ans = -1; for (int i = 0; i <= n; i++) { long long res = k % (n + i); if (res < d) continue; if (res > 2 * d) continue; long long u = res - d; if (u > i) continue; long long uu = i - u; if (n - d < uu - 1) continue; ans = i; } printf( %lld n , ans); return 0; } long long best = -1; for (int cyc = 0; cyc < 5000; cyc++) { long long l = -1, r = 1e12; long long lo = 0, hi = n + 1; while (lo != hi) { long long mid = (lo + hi) / 2; int ok = 1; if (k / (n + mid) > cyc) ok = 0; long long res = k - (long long)cyc * (n + mid); if (res > 2 * d) ok = 0; if (res > d + mid) ok = 0; if (ok) hi = mid; else lo = mid + 1; } l = lo; lo = -1, hi = n + 1; while (lo != hi) { long long mid = (lo + hi + 1) / 2; int ok = 1; if (k / (n + mid) < cyc) ok = 0; long long res = k - (long long)cyc * (n + mid); if (res < d) ok = 0; long long need = max(0ll, mid - (n - d) - 1); if (cyc == 0 && mid == 5) ( ! %lld %lld n , need, res); if (res < d + need) ok = 0; if (ok) lo = mid; else hi = mid - 1; } r = lo; if (l == n + 1 || k < (long long)cyc * n) r = -1; ( cyc %d l %lld r %lld n , cyc, l, r); if (l > r) continue; best = max(best, r); ; } printf( %lld n , best); }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; signed main() { int a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f; int ans = 0; if (e >= f) { int dec = min(a, d); a -= dec; d -= dec; ans += dec * e; ans += min(min(b, c), d) * f; } else { int dec = min(min(b, c), d); b -= dec, c -= dec, d -= dec; ans += dec * f; ans += min(a, d) * e; } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; template <class T> T ABS(const T& x) { return x > 0 ? x : -x; } long long int gcd(long long int n1, long long int n2) { return n2 == 0 ? ABS(n1) : gcd(n2, n1 % n2); } long long int lcm(long long int n1, long long int n2) { return n1 == 0 && n2 == 0 ? 0 : ABS(n1 * n2) / gcd(n1, n2); } long long int ceil2(long long int a, long long int b) { return (a + b - 1) / b; } int n; void divisors(vector<int>& d) { vector<int> v; for (int i = 1; i <= sqrt(n); ++i) { if (n % i == 0) { if (n / i == i) { d.push_back(i); } else { d.push_back(i); v.push_back(n / i); } } } for (int i = v.size() - 1; i >= 0; --i) { d.push_back(v[i]); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; int arr[n]; for (int i = 0; i < (n); ++i) { cin >> arr[i]; } vector<int> d; divisors(d); bool v[n]; for (int i = d.size() - 1; i >= 0; --i) { int size = n / d[i]; if (size < 3) continue; memset(v, false, sizeof(v)); int k = d[i], cnt = 0; for (int l = 0; l < n; ++l) { int prev = (l - k + n) % n, next = (l + k) % n; if (arr[l] && arr[prev] && arr[next]) { v[l] = true; cnt++; } else { v[l] = false; } } if (cnt >= size) { cout << YES n ; return 0; } } cout << NO n ; return 0; }
#include <bits/stdc++.h> using namespace std; namespace jyy { const int mod = 998244353, N = 600010; const double eps = 1e-8; inline int read() { static int x = 0, f = 1; x = 0, f = 1; static char ch = getchar(); while (ch < 0 || ch > 9 ) { (ch == - ) ? f *= -1 : f; ch = getchar(); } while (ch >= 0 && ch <= 9 ) x = x * 10 + int(ch - 48), ch = getchar(); return x * f; } inline void fw(int x) { if (x < 0) putchar( - ), x = -x; x >= 10 ? fw(x / 10), 1 : 0; putchar(char(x % 10 + 48)); } inline int qmo(int x) { return x + ((x >> 31) & mod); } inline int ksm(int x, int y) { int tmp = 1; for (; y; y >>= 1, x = 1ll * x * x % mod) if (y & 1) tmp = 1ll * tmp * x % mod; return tmp; } inline int inv(int x) { return ksm(x, mod - 2); } int tot, head[N], fa[N]; struct edge { int to, next; } dat[N * 2]; void add_edge(int x, int y) { dat[++tot] = (edge){y, head[x]}, head[x] = tot; } inline int find(int x) { return fa[x] ? fa[x] = find(fa[x]) : x; } inline int merge(int x, int y) { return find(x) != find(y) ? fa[find(x)] = find(y), 0 : 1; } inline int low(int x) { return x & -x; } int ifac[N], fac[N]; inline void init(int x) { fac[0] = ifac[0] = 1; for (int i = 1; i <= x; i++) fac[i] = 1ll * fac[i - 1] * i % mod; ifac[x] = inv(fac[x]); for (int i = x - 1; i >= 1; i--) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % mod; } inline int C(int x, int y) { return (x < y || y < 0 || x < 0) ? 0 : 1ll * fac[x] * ifac[y] % mod * ifac[x - y] % mod; } } // namespace jyy using namespace jyy; int pd[N], n, R[N], a[N], vis[N], dep[N], m, b[N]; char c[N]; void dfs(int x, int f) { vis[x] = 1; dep[x] = dep[f] + 1; fa[x] = f; for (int i = head[x]; i; i = dat[i].next) { int t = dat[i].to; if (t == f || vis[t]) continue; dfs(t, x); } } void work(int x, int y) { deque<int> q1, q2; while (dep[x] > dep[y]) q1.push_back(x), x = fa[x]; while (dep[x] < dep[y]) q2.push_front(y), y = fa[y]; while (x != y) q1.push_back(x), q2.push_front(y), x = fa[x], y = fa[y]; q1.push_back(x); cout << q1.size() + q2.size() << endl; while (q1.size()) printf( %d , q1.front()), q1.pop_front(); while (q2.size()) printf( %d , q2.front()), q2.pop_front(); puts( ); } void solve() { n = read(), m = read(); for (int i = 1, x, y; i <= m; i++) x = read(), y = read(), add_edge(x, y), add_edge(y, x); dfs(1, 0); int q = read(); for (int i = 1; i <= q; i++) { a[i] = read(), b[i] = read(); pd[a[i]] ^= 1; pd[b[i]] ^= 1; } int cnt = 0; for (int i = 1; i <= n; i++) cnt += pd[i]; if (cnt) { puts( NO ); cout << cnt / 2 << endl; return; } puts( YES ); for (int i = 1; i <= q; i++) { work(a[i], b[i]); } } int main() { int T = 1; while (T--) solve(); }
#include <bits/stdc++.h> using namespace std; bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) { return (a.first > b.first); } double gcd(double a, double b) { if (b == 0) return a; return gcd(b, double(int(a) % int(b))); } long long int power(long long int b, long long int e, long long int m) { if (e == 0) return 1; if (e & 1) return b * power(b * b % m, e / 2, m) % m; return power(b * b % m, e / 2, m); } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int a; cin >> a; ; long long int b; cin >> b; ; long long int c; cin >> c; ; long long int d; cin >> d; ; long long int e; cin >> e; ; long long int f; cin >> f; ; long long int z = 0, ans = 0; if (f >= e) { z = min(c, d); z = min(z, b); ans += (f * z); d -= z; z = min(d, a); ans += (e * z); } else { z = min(a, d); ans += (e * z); d -= z; z = min(b, c); z = min(z, d); ans += (f * z); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k, p; cin >> n >> k >> p; bool odd = false, even = false; string ans = ; if (n % 2 == 0) even = true; else { odd = true; k--; } for (int i = 0; i < p; i++) { long long tmp; cin >> tmp; if (even) { if (tmp % 2 == 0 && n - 2 * k < tmp) ans += X ; else if (tmp % 2 == 1 && n - 2 * (k - n / 2) < tmp) ans += X ; else ans += . ; } else { if (tmp == n && k >= 0) { ans += X ; continue; } if (tmp % 2 == 0 && n - 2 * k <= tmp) ans += X ; else if (tmp % 2 == 1 && n - 2 * (k - n / 2) <= tmp) ans += X ; else ans += . ; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int n, start, x; int order[50005]; pair<int, int> ask(int idx) { printf( ? %d n , idx); fflush(stdout); int val, nxt; scanf( %d%d , &val, &nxt); return pair<int, int>(val, nxt); } int main() { srand(time(0)); for (int i = 1; i < 50005; i++) order[i] = i; scanf( %d%d%d , &n, &start, &x); random_shuffle(order + 1, order + n + 1); int ptr = start, ptr_val = INT_MIN; for (int i = 1; i <= min(n, 1200); i++) { pair<int, int> p = ask(order[i]); if (p.first <= x && p.first >= ptr_val) ptr = order[i], ptr_val = p.first; } int ans = -1; while (ptr != -1) { pair<int, int> y = ask(ptr); if (y.first >= x) { ans = y.first; break; } ptr = y.second; } printf( ! %d n , ans); }
#include <bits/stdc++.h> int main() { long int i, j, l, i1, k = 0, c1 = 0, t, c[100000]; char a[100000], b[100000]; scanf( %s , &a); l = strlen(a); for (j = 0; j < l; j++) { if (((int)a[j]) % 2 == 0) { b[k] = a[j]; c[k] = j; k++; } } if (k == 0) printf( -1 ); else { for (i = 0; i < k; i++) { if (a[l - 1] > b[i]) { c1 = 1; break; } } if (c1 == 0) { t = a[c[k - 1]]; a[c[k - 1]] = a[l - 1]; a[l - 1] = t; } else if (c1 == 1) { t = a[c[i]]; a[c[i]] = a[l - 1]; a[l - 1] = t; } for (i = 0; i < l; i++) { printf( %c , a[i]); } } return 0; }
#include <bits/stdc++.h> using namespace std; void optIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const long long mod = 1e9 + 7, mod2 = 998244353, mod3 = 1e9 + 9; long long p[5020], pp = 31, hh[5020]; long long p2[5020], pp2 = 37, hh2[5020]; int dp[5020]; bool can[5020][5020]; map<long long, bool> ms; void solve() { int n, a, b; cin >> n >> a >> b; string s; cin >> s; p[0] = 1; p2[0] = 1; for (int i = 1; i < n + 1; ++i) { p[i] = (p[i - 1] * pp) % mod; p2[i] = (p2[i - 1] * pp2) % mod2; hh[i] = (hh[i - 1] + (s[i - 1] - a + 1) * p[i]) % mod; hh2[i] = (hh2[i - 1] + (s[i - 1] - a + 1) * p2[i]) % mod2; } for (int j = 1; j < n + 1; ++j) { for (int i = 1; i < n + 1; ++i) { if (i - 2 * j < 0) continue; long long y = ((hh[i - j] - hh[i - 2 * j] + mod) % mod * p[n - (i - j)]) % mod; long long y2 = ((hh2[i - j] - hh2[i - 2 * j] + mod2) % mod2 * p2[n - (i - j)]) % mod2; ms[y * mod2 + y2] = 1; long long x = ((hh[i] - hh[i - j] + mod) % mod * p[n - i]) % mod; long long x2 = ((hh2[i] - hh2[i - j] + mod2) % mod2 * p2[n - i]) % mod2; if (ms.find(x * mod2 + x2) != ms.end()) can[i][j] = 1; } ms.clear(); } for (int i = 1; i < n + 1; ++i) { dp[i] = dp[i - 1] + a; for (int j = 1; j < i + 1; ++j) { if (can[i][j]) dp[i] = min(dp[i], dp[i - j] + b); } } cout << dp[n] << n ; } int main() { optIO(); int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> int n, x, y, att, cnt; using namespace std; int main() { cin >> n >> x >> y; if (x > y) return cout << n, 0; for (int i = (1); i <= (n); i++) { cin >> att; if (att <= x) cnt++; } cout << (cnt + 1) / 2; }
#include <bits/stdc++.h> using namespace std; const int M = 6e5; int n, d, f[M]; int main() { cin >> n >> d; f[0] = 1; int now = 0, times = 0; for (int i = 0; i < n; i++) { int x; cin >> x; for (int j = M - 1; j >= x; j--) f[j] |= f[j - x]; } while (1) { int old = now; for (int i = 1; i <= d; i++) if (f[i + old]) now = old + i; if (now == old) break; times++; } cout << now << << times << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct point { double x, y; point operator-(point A) { return (point){x - A.x, y - A.y}; } double operator*(point A) { return x * A.y - y * A.x; } double dis() { return sqrt(x * x + y * y); } } a[1005][1005], t[1005], E[1005]; int n, vis[1005][1005]; double r[1005][1005]; point Mid(point a, point b) { return (point){(a.x + b.x) / 2, (a.y + b.y) / 2}; } double disline(int i, point A) { return abs(E[i] * (A - t[i]) / E[i].dis()); } int ch(int R) { return R % n ? R % n : n; } int main() { cin >> n; for (int i = 1; i <= n; i++) scanf( %lf%lf , &t[i].x, &t[i].y); for (int i = 1; i <= 2 * n; i++) t[n + i] = t[i]; for (int i = 1; i <= 3 * n; i++) E[i] = t[i + 1] - t[i]; for (int L = 1, R = L + 1, nw = L; L <= n; L++) for (R--, nw = max(L, nw); R == L || E[L] * E[R] > 0.5; R++) { while (nw < R && disline(L, t[nw + 1]) < disline(R, t[nw + 1])) ++nw; point le = t[nw], ri = t[nw + 1]; for (int j = 1; j <= 42; j++) { point mid = Mid(le, ri); if (disline(L, mid) < disline(R, mid)) le = mid; else ri = mid; } point P = Mid(le, ri); a[L][ch(R)] = P; r[L][ch(R)] = max(disline(L, P), disline(R, P)); vis[L][ch(R)] = 1; } double ans = 1e7; point a1, a2; for (int L = 1; L <= n; L++) for (int R = L; R <= L + n; R++) if (vis[L][ch(R)] && vis[ch(R + 1)][ch(L - 1 + n)]) { double newr = max(r[L][ch(R)], r[ch(R + 1)][ch(L - 1 + n)]); if (newr < ans) ans = newr, a1 = a[L][ch(R)], a2 = a[ch(R + 1)][ch(L - 1 + n)]; } printf( %.10f n%.10f %.10f n%.10f %.10f n , ans, a1.x, a1.y, a2.x, a2.y); return 0; }
#include <bits/stdc++.h> using namespace std; int Solve(int n) { int ten = 1; int ans = 0; while (n != 0) { if (n % 10 != 0) { ans += (ten * (n % 10)); ten *= 10; } n /= 10; } return ans; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } bool prime(long long int i) { if (i == 1) return false; if ((i == 2) || (i == 3)) return true; if ((i % 2 == 0) || (i % 3 == 0)) return false; else { for (long long int j = 5; j * j <= i; j += 6) if ((i % j == 0) || (i % (j + 2) == 0)) return false; } return true; } int main() { int flag = 0; bool ok; int n, m, test, sum = 0, id1 = 0, id2 = 0, id3 = 0, dig1, dig2, dig3; int a, b; cin >> a >> b; int c = a + b; dig1 = Solve(a); dig2 = Solve(b); dig3 = Solve(c); if (dig1 + dig2 == dig3) { cout << YES ; } else { cout << NO ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long n; long long calcCards(long long x) { return x * (x + 1) + (x - 1) * x / 2; } bool P(int x) { return calcCards(x) <= n; } long long bs(long long l = 0, long long h = n) { long long m; while (l < h) { m = (l + h + 1) / 2; if (P(m)) l = m; else h = m - 1; } return l; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << fixed; cout << setprecision(10); int t; cin >> t; while (t--) { cin >> n; int ans = 0; while (n >= 2) { ans++; n -= calcCards(bs()); } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); void Fast() { std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long fact(long long n) { long long f = 1; for (int i = 2; i <= n; i++) { f *= i; } return f; } long long ncr(long long n, long long r) { return fact(n) / (fact(r) * fact(n - r)); } long long npr(long long n, long long r) { return fact(n) / fact(n - r); } bool prime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { return 0; break; } } return 1; } char arr[55][55]; void solve() { int n, m; cin >> n >> m; int r1 = INT_MIN, r2 = INT_MAX, c1 = INT_MIN, c2 = INT_MAX; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> arr[i][j]; if (arr[i][j] == * ) { r1 = max(r1, i); r2 = min(r2, i); c1 = max(c1, j); c2 = min(c2, j); } } } for (int i = r2; i <= r1; i++) { for (int j = c2; j <= c1; j++) { cout << arr[i][j]; } cout << n ; } } int main() { Fast(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, d; cin >> n >> d; int ld = 0, rd = n * (n - 1) / 2; for (int i = 1, cd = 0; i <= n; ++i) { if (!(i & (i - 1))) ++cd; ld += cd - 1; } if (!(ld <= d && d <= rd)) { cout << NO << endl; continue; } vector<int> par(n); iota(par.begin(), par.end(), -1); vector<int> cnt(n, 1); cnt[n - 1] = 0; vector<int> bad(n); vector<int> dep(n); iota(dep.begin(), dep.end(), 0); int cur = n * (n - 1) / 2; while (cur > d) { int v = -1; for (int i = 0; i < n; ++i) { if (!bad[i] && cnt[i] == 0 && (v == -1 || dep[v] > dep[i])) { v = i; } } assert(v != -1); int p = -1; for (int i = 0; i < n; ++i) { if (cnt[i] < 2 && dep[i] < dep[v] - 1 && (p == -1 || dep[p] < dep[i])) { p = i; } } if (p == -1) { bad[v] = 1; continue; } assert(dep[v] - dep[p] == 2); --cnt[par[v]]; --dep[v]; ++cnt[p]; par[v] = p; --cur; } cout << YES << endl; for (int i = 1; i < n; ++i) cout << par[i] + 1 << ; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int N, K, D[1010], S[1010], ans, tt, crtMax; int main() { int i; scanf( %i %i , &N, &K); for (i = 1; i <= N; i++) scanf( %i , &D[i]); for (i = 1; i <= N; i++) scanf( %i , &S[i]); for (i = 1; i <= N; i++) { crtMax = max(crtMax, S[i]); ans += S[i]; while (ans < D[i]) { ans += crtMax; tt += K; } tt += D[i]; ans -= D[i]; } printf( %i n , tt); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200000 + 10; const long long INF = 0x3f3f3f3f3f3f3f3fLL; long long dp[105][105][105]; long long c[105], val[105][105], ans = INF; int main() { int i, j, K, n, m, k; cin >> n >> m >> K; for (i = 1; i <= n; i++) cin >> c[i]; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { cin >> val[i][j]; } } memset(dp, 0x3f, sizeof(dp)); dp[0][0][0] = 0; for (i = 0; i < n; i++) { for (j = 0; j <= m; j++) { for (k = 0; k <= i; k++) { if (dp[i][j][k] != INF) { if (c[i + 1]) { dp[i + 1][c[i + 1]][k + (c[i + 1] != j)] = min(dp[i + 1][c[i + 1]][k + (c[i + 1] != j)], dp[i][j][k]); } else { for (int p = 1; p <= m; p++) { dp[i + 1][p][k + (p != j)] = min(dp[i][j][k] + val[i + 1][p], dp[i + 1][p][k + (p != j)]); } } } } } } for (i = 1; i <= m; i++) { ans = min(dp[n][i][K], ans); } if (ans == INF) cout << -1 << endl; else cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long fast_power(long long x, long long y) { if (y == 0) return 1; long long temp = fast_power(x, y / 2); temp = ((temp % 1000000007) * (temp % 1000000007)) % 1000000007; if (y % 2 != 0) temp = ((temp % 1000000007) * (x % 1000000007)) % 1000000007; return temp; } long long f[1000001]; long long inv[1000001]; long long ncr(long long n, long long r) { return ((((f[n] % 1000000007) * (inv[n - r] % 1000000007)) % 1000000007) * inv[r]) % 1000000007; } long long nCr(long long n, long long r) { long long f = 1; long long x = 1; for (long long i = n; i > n - r; i--) { f *= i; f /= x++; } return f; } long long npr(long long n, long long r) { return (f[n] % 1000000007 * inv[n - r] % 1000000007) % 1000000007; } int main() { long long n, r, t; cin >> n >> r >> t; f[0] = 1; inv[0] = 1; for (long long i = 1; i <= n; i++) { f[i] = (f[i - 1] * i) % 1000000007; inv[i] = fast_power(f[i], 1000000007 - 2); } long long sum = 0; if (n >= 4) for (long long i = 4; i < t; i++) { if (t - i <= r) sum += nCr(n, i) * nCr(r, t - i); } cout << sum << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> a(n); for (auto& x : a) cin >> x, --x; vector<int> k(m); for (auto& x : k) cin >> x; for (int i = 0; i < n; ++i) for (int j = i + 1; j <= n; ++j) { vector<int> cur(m); for (int l = i; l < j; ++l) ++cur[a[l]]; if (cur == k) { cout << YES n ; return 0; } } cout << NO n ; }
#include <bits/stdc++.h> using namespace std; template <typename T> void nexts(T *, int); template <typename T> void dump(T *arr, int cnt, char sep = ); void __init__(bool, bool); const int MAXMAXMAX = 1000 * 1000 * 1000; string a, b, c; char ans[100015]; vector<int> cnt_a(26); vector<int> cnt_b(26); vector<int> cnt_c(26); int main() { __init__(1, 0); cin >> a >> b >> c; for (int(i) = (0); (i) < (a.size()); ++(i)) ++cnt_a[a[i] - a ]; for (int(i) = (0); (i) < (b.size()); ++(i)) ++cnt_b[b[i] - a ]; for (int(i) = (0); (i) < (c.size()); ++(i)) ++cnt_c[c[i] - a ]; int b_max = MAXMAXMAX; for (int(i) = (0); (i) < (26); ++(i)) if (cnt_b[i] != 0) b_max = min(b_max, cnt_a[i] / cnt_b[i]); int max_ans = 0; int max_b = 0; int max_c = 0; if (b_max == MAXMAXMAX) b_max = 0; vector<int> tmp = cnt_a; for (int(i) = (0); (i) < (b_max + 1); ++(i)) { if (i != 0) for (int(i) = (0); (i) < (26); ++(i)) tmp[i] -= cnt_b[i]; int c_max = MAXMAXMAX; for (int(i) = (0); (i) < (26); ++(i)) if (cnt_c[i] != 0) c_max = min(c_max, tmp[i] / cnt_c[i]); if (c_max == MAXMAXMAX) b_max = 0; if (i + c_max > max_ans) { max_ans = i + c_max; max_b = i; max_c = c_max; } } for (int(i) = (0); (i) < (max_b); ++(i)) cout << b; for (int(i) = (0); (i) < (max_c); ++(i)) cout << c; for (int(i) = (0); (i) < (26); ++(i)) { cnt_a[i] -= max_b * cnt_b[i] + max_c * cnt_c[i]; for (int(j) = (0); (j) < (cnt_a[i]); ++(j)) cout << (char)( a + i); } cout << n ; return 0; } template <typename T> void nexts(T *arr, int cnt) { for (int i = 0; i < cnt; ++i) cin >> arr[i]; } template <typename T> void dump(T *arr, int cnt, char sep) { for (int i = 0; i < cnt; ++i) cout << arr[i] << sep; } void __init__(bool fastIOstream, bool acmp) { if (fastIOstream) { ios_base::sync_with_stdio(false); cin.tie(0); } if (acmp) { freopen( INPUT.TXT , rt , stdin); freopen( OUTPUT.TXT , wt , stdout); } }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int x = sqrt(a * c / b), y = sqrt(a * b / c), z = sqrt(b * c / a); cout << (x + y + z) * 4; return 0; }
#include <bits/stdc++.h> using namespace std; long long nMod = 1e9 + 7; inline long long GCD(long long a, long long b) { while (b != 0) { long long c = a % b; a = b; b = c; } return a; }; inline long long LCM(long long a, long long b) { return (a / GCD(a, b)) * b; }; int m, n, k; vector<set<int>> st(55); void vietnakid() { cin >> m >> n; for (int i = 0; i < m; i++) { cin >> k; for (int j = 0; j < k; j++) { int x; cin >> x; st[i].insert(x); } } bool check = true; for (int i = 0; i < m; i++) { if (!check) break; for (int j = 0; j < m; j++) { bool ok = false; for (auto x : st[i]) { if (ok) break; if (st[j].count(x)) { ok = true; } } if (!ok) { check = false; break; } } } if (check) { cout << possible n ; } else { cout << impossible n ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(); vietnakid(); cerr << nTime elapsed: << 1000 * clock() / CLOCKS_PER_SEC << ms n ; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( O3,unroll-loops ) #pragma pack(0) using namespace std; vector<string> v[1000001]; string s; int ptr = 0, maxd = 0; void dfs(int dis) { maxd = max(maxd, dis); int k = 0, f = ptr; for (; ptr < s.length(); ptr++) if (s[ptr] == , ) break; string t = s.substr(f, ptr - f); ptr++; for (; ptr < s.length(); ptr++) { if (s[ptr] == , ) break; k = k * 10 + (s[ptr] - 0 ); } ptr++; v[dis].emplace_back(t); for (int i = 1; i <= k; i++) dfs(dis + 1); } signed main() { ios::sync_with_stdio(0), cin.tie(0); cin >> s; while (ptr < s.length()) dfs(0); cout << maxd + 1 << n ; for (int i = 0; !v[i].empty(); i++) { for (auto j : v[i]) cout << j << ; cout << n ; } }
#include <bits/stdc++.h> using namespace std; class Solution { int n, x[2]; vector<pair<long long, int>> A; public: void run() { cin >> n >> x[0] >> x[1]; for (int i = 0; i < n; ++i) { int c; cin >> c; A.emplace_back(c, i); } sort(A.begin(), A.end()); for (int i = 0; i < n; ++i) { int a = A[i].first; int j = (x[0] + a - 1) / a + i; if (j < n and x[1] <= A[j].first * (n - j)) { cout << Yes n ; cout << j - i << << n - j << n ; for (int k = i; k < j; ++k) cout << A[k].second + 1 << n [k == j - 1]; for (int k = j; k < n; ++k) cout << A[k].second + 1 << n [k == n - 1]; return; } j = (x[1] + a - 1) / a + i; if (j < n and x[0] <= A[j].first * (n - j)) { cout << Yes n ; cout << n - j << << j - i << n ; for (int k = j; k < n; ++k) cout << A[k].second + 1 << n [k == n - 1]; for (int k = i; k < j; ++k) cout << A[k].second + 1 << n [k == j - 1]; return; } } cout << No n ; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); Solution().run(); }
#include <bits/stdc++.h> using namespace std; bool isPrime(int a, int p) { return (a <= p) || ((a % p != 0) && (isPrime(a, p + 1))); } int main() { const int N = 256; int freq[N] = {0}; string s; cin >> s; int len = s.size(); for (int p = 0; p < len; p++) { ++freq[s[p]]; } string t(len, 0); int *m = max_element(freq, freq + N); for (int p = 1; p < len; p++) { if ((p < len / 2) || (!isPrime(p + 1, 2))) { t[p] = m - freq; (*m)--; } } if (*m < 0) { cout << NO << endl; } else { int q = 0; for (int p = 0; p < len; p++) { if (!t[p]) { while (!freq[q]) { q++; } freq[t[p] = q]--; } } cout << YES << endl << t << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; bool cmp(pair<int, int> a, pair<int, int> b) { if (a.first != b.first) return a.first > b.first; return a.second < b.second; } bool cmp2(pair<int, int> a, pair<int, int> b) { return a.second < b.second; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; vector<pair<int, int> > v; for (int i = 1; i <= n; i++) { int x; cin >> x; v.push_back({x, i}); } sort(v.begin(), v.end(), cmp); cin >> m; while (m--) { int k, pos; cin >> k >> pos; vector<pair<int, int> > res; for (int i = 0; i < k; i++) { res.push_back(v[i]); } sort(res.begin(), res.end(), cmp2); cout << res[pos - 1].first << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using PII = pair<int, int>; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 200000 + 5; int n, tot, tin[maxn], tout[maxn], rid[maxn], vis[maxn], deg[maxn]; vector<int> edge[maxn]; int query(int x, int y) { cout << ? << x << << y << endl; cin >> x; assert(x >= 1 && x <= n); return x; } int ok[maxn]; void dfs(int u, int f) { tin[u] = ++tot; rid[tot] = u; for (int v : edge[u]) { if (v == f) continue; dfs(v, u); } tout[u] = tot; } bool check(int x, int y) { return tin[x] <= tin[y] && tout[y] <= tout[x]; } int main() { scanf( %d , &n); ok[1] = true; for (int i = 2, u, v; i <= n; i++) { scanf( %d%d , &u, &v); ok[i] = true; edge[u].push_back(v); edge[v].push_back(u); deg[u]++; deg[v]++; } dfs(1, 0); vector<int> v; for (int i = 1; i <= n; i++) if (deg[i] == 1) v.push_back(i); assert(v.size() >= 2); for (int i = 1; i < (int)v.size(); i += 2) { int ans = query(v[i - 1], v[i]); for (int y : edge[ans]) { if (check(ans, y)) { if (check(y, v[i - 1]) || check(y, v[i])) { for (int j = tin[y]; j <= tout[y]; j++) { ok[rid[j]] = false; } } } else { if (!check(ans, v[i - 1]) || !check(ans, v[i])) { for (int j = 1; j < tin[ans]; j++) { ok[rid[j]] = false; } for (int j = tout[ans] + 1; j <= n; j++) { ok[rid[j]] = false; } } } } } v.clear(); for (int i = 1; i <= n; i++) { if (!ok[i]) continue; int c = 0; for (int v : edge[i]) { if (ok[v]) c++; } assert(c <= 2); if (c == 1) { v.push_back(i); } } if (v.size() == 0) { for (int i = 1; i <= n; i++) { if (ok[i]) { cout << ! << i << endl; return 0; } } } else if (v.size() == 2) { int r = query(v[0], v[1]); cout << ! << r << endl; } else { assert(false); } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const long long LINF = 4000000000000000000LL; const double eps = 1e-9; void prepare() {} int a, n; int day[10000008]; void read() { scanf( %d%d , &a, &n); } void solve() { read(); for (int i = 1; i * i < a + n; i++) { int ii = i * i; for (int j = ii; j < a + n; j += ii) day[j] = i * i; } long long res = 0; for (int i = a; i < a + n; i++) res += i / day[i]; cout << res << endl; } int main() { prepare(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 100005; long long t, l, i, j, k, p, q, u, v, n, color[1000005]; string s; vector<long long> zro; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> q; for (l = 1; l <= q; l++) { cin >> n >> k >> s; for (i = 0; i < n; i++) { if (s[i] == 0 ) zro.push_back(i); else color[i] = 1; } u = 0; v = 0; for (i = 0; i < zro.size(); i++) { if (u == n || k == 0) break; for (; u < n; u++) { if (zro[i] < u) break; if (color[u] == 1 && zro[i] - u <= k) { k -= (zro[i] - u); color[zro[i]] = 1; color[u] = 0; swap(s[u], s[zro[i]]); u++; break; } } } cout << s << endl; for (i = 0; i <= n; i++) color[i] = 0; zro.clear(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, sum = 0; string s; cin >> a; for (int i = 0; i < a; i++) { cin >> s; if (s == Tetrahedron ) sum = sum + 4; else if (s == Cube ) sum = sum + 6; else if (s == Octahedron ) sum = sum + 8; else if (s == Dodecahedron ) sum = sum + 12; else if (s == Icosahedron ) sum = sum + 20; } cout << sum; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( O3 , unroll-loops ) using namespace std; namespace { const int MOD107 = 1000000007; const int MOD998 = 998244353; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; template <class T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <class T> using maxheap = priority_queue<T, vector<T>, less<T>>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); struct Modular { private: long long MODVALUE = 0; long long val = 0; Modular initModular(long long val) const { return Modular(val, MODVALUE); } inline void validate() const {} inline void validate(const Modular& other) const {} public: explicit operator long long() const { return val; } template <class T> Modular& operator=(const T& other) { val = (other % MODVALUE + MODVALUE) % MODVALUE; return *this; } long long getMod() { return MODVALUE; } template <class T> void setMod(const T& MOD) { MODVALUE = MOD; } bool operator==(const Modular& other) const { validate(other); return val == other.val; } bool operator!=(const Modular& other) const { return !(*this == other); } Modular& operator+=(const Modular& other) { validate(other); val += other.val; if (val >= MODVALUE) val -= MODVALUE; return *this; } Modular operator+(const Modular& other) const { Modular tmp = *this; return tmp += other; } template <class T> Modular& operator+=(const T& other) { return *this += initModular(other); } template <class T> Modular operator+(const T& other) const { return *this + initModular(other); } template <class T> friend Modular operator+(const T& other, const Modular& m) { return m.initModular(other) + m; } Modular& operator++() { return *this += 1; } Modular operator++(int) { Modular tmp = *this; ++*this; return tmp; } Modular operator-() const { return initModular(-val); } Modular& operator-=(const Modular& other) { validate(other); val -= other.val; if (val < 0) val += MODVALUE; return *this; } Modular operator-(const Modular& other) const { Modular tmp = *this; return tmp -= other; } template <class T> Modular& operator-=(const T& other) { return *this -= initModular(other); } template <class T> Modular operator-(const T& other) const { return *this - initModular(other); } template <class T> friend Modular operator-(const T& other, const Modular& m) { return m.initModular(other) - m; } Modular& operator--() { return *this -= 1; } Modular operator--(int) { Modular tmp = *this; --*this; return tmp; } Modular& operator*=(const Modular& other) { validate(other); val *= other.val; val %= MODVALUE; if (val < 0) val += MODVALUE; return *this; } Modular operator*(const Modular& other) const { Modular tmp = *this; return tmp *= other; } template <class T> Modular& operator*=(const T& other) { return *this *= initModular(other); } template <class T> Modular operator*(const T& other) const { return *this * initModular(other); } template <class T> friend Modular operator*(const T& other, const Modular& m) { return m.initModular(other) * m; } Modular pow(long long power) const { Modular m = *this, ans = initModular(1); while (power) { if (power & 1) ans *= m; m *= m; power >>= 1; } return ans; } Modular pow(const Modular& other) const { return pow(other.val); } Modular inv() const { return pow(MODVALUE - 2); } Modular& operator/=(const Modular& other) { val *= other.inv().val; val %= MODVALUE; if (val < 0) val += MODVALUE; return *this; } Modular operator/(const Modular& other) const { Modular tmp = *this; return tmp /= other; } template <class T> Modular& operator/=(const T& other) { return *this /= initModular(other); } template <class T> Modular operator/(const T& other) const { return *this / initModular(other); } template <class T> friend Modular operator/(const T& other, const Modular& m) { return m.initModular(other) / m; } friend istream& operator>>(istream& in, Modular& m) { in >> m.val; m.val %= m.MODVALUE; if (m.val < 0) m.val += m.MODVALUE; return in; } friend ostream& operator<<(ostream& out, const Modular& m) { return out << m.val; } Modular() { val = 0; MODVALUE = 0; } Modular(long long val, long long MOD) : MODVALUE(MOD), val((val % MOD + MOD) % MOD) {} }; using Mint = Modular; inline ll binpow(ll b, ll p, ll mod) { b %= mod; ll ans = 1; for (; p > 0; p >>= 1) { if (p & 1) { ans *= b; ans %= mod; } b *= b; b %= mod; } return ans; } inline ll max(ll a, int b) { return (a > b ? a : b); } inline ll max(int a, ll b) { return (a > b ? a : b); } inline ll min(ll a, int b) { return (a < b ? a : b); } inline ll min(int a, ll b) { return (a < b ? a : b); } template <class T> inline bool chkmin(T& x, const T& y) { return y < x ? x = y, 1 : 0; } template <class T> inline bool chkmax(T& x, const T& y) { return x < y ? x = y, 1 : 0; } } // namespace namespace IO { struct InputWrapper { private: istream& in; public: template <class T> inline void _R(T& x) { in >> x; } template <class T1, class T2> inline void _R(pair<T1, T2>& x) { _R(x.first); _R(x.second); } template <class T> inline void _R(vector<T>& x) { for (auto& i : x) _R(i); } template <class T> inline void _R(deque<T>& x) { for (auto& i : x) _R(i); } inline void R() {} template <class T1, class... T2> inline void R(T1& x, T2&... y) { _R(x); R(y...); } template <class... T> inline void operator()(T&... x) { R(x...); } InputWrapper(istream& in) : in(in) {} } R(cin); struct OutputWrapper { private: ostream& out; public: template <class Container> inline void printContainer(const Container& x) { bool isFirst = 1; for (auto i : x) { if (!isFirst) out << ; isFirst = 0; _W(i); } } template <class T> inline void _W(const T& x) { out << x; } template <class T1, class T2> inline void _W(const pair<T1, T2>& p) { _W(p.first); _W( ); _W(p.second); } template <class... T> inline void _W(const vector<T...>& x) { printContainer(x); } template <class... T> inline void _W(const deque<T...>& x) { printContainer(x); } template <class... T> inline void _W(const forward_list<T...>& x) { printContainer(x); } template <class... T> inline void _W(const list<T...>& x) { printContainer(x); } template <class... T> inline void _W(const set<T...>& x) { printContainer(x); } template <class... T> inline void _W(const multiset<T...>& x) { printContainer(x); } template <class... T> inline void _W(const unordered_set<T...>& x) { printContainer(x); } template <class... T> inline void _W(const unordered_multiset<T...>& x) { printContainer(x); } template <class... T> inline void _W(const map<T...>& x) { printContainer(x); } template <class... T> inline void _W(const multimap<T...>& x) { printContainer(x); } template <class... T> inline void _W(const unordered_map<T...>& x) { printContainer(x); } template <class... T> inline void _W(const unordered_multimap<T...>& x) { printContainer(x); } inline void W() { out << n ; } template <class T> inline void W(const T& x) { _W(x); W(); } template <class T1, class... T2> inline void W(const T1& x, const T2&... y) { _W(x); _W( ); W(y...); } template <class... T> inline void operator()(const T&... x) { W(x...); } OutputWrapper(ostream& out) : out(out) {} } W(cout), D(cerr); } // namespace IO using namespace IO; namespace outputFormat { inline string Case(int tc) { return Case # + to_string(tc) + : ; } inline string YN(bool b) { return b ? Yes : No ; } } // namespace outputFormat using namespace outputFormat; namespace std { template <class T1, class T2> struct hash<pair<T1, T2>> { std::size_t operator()(pair<T1, T2> _) const { return hash<T1>{}(_.first) ^ hash<T2>{}(_.second); } }; } // namespace std int main() { int t; cin >> t; while (t--) { int n; R(n); string s, t; R(s, t); vector<vector<Mint>> cnt_table(n + 1, vector<Mint>(2 * n + 7, Mint(0, MOD107))), sum_table = cnt_table; vector<Mint*> cnt(n + 1), sum(n + 1); for (int i = 0; i <= n; i++) { cnt[i] = &cnt_table[i][n + 3]; sum[i] = &sum_table[i][n + 3]; } cnt[0][0] = 1; vector<vector<bool>> can(4, vector<bool>(n + 1, false)); for (int i = 1; i <= n; i++) { can[0][i] = s[i - 1] != 1 && t[i - 1] != 1 ; can[1][i] = s[i - 1] != 1 && t[i - 1] != 0 ; can[2][i] = s[i - 1] != 0 && t[i - 1] != 1 ; can[3][i] = s[i - 1] != 0 && t[i - 1] != 0 ; if (i % 2 == 1) swap(can[1][i], can[2][i]); } for (int i = 1; i <= n; i++) { for (int j = -i; j <= i; j++) { if (can[0][i]) { cnt[i][j] += cnt[i - 1][j]; sum[i][j] += sum[i - 1][j]; } if (can[1][i]) { cnt[i][j] += cnt[i - 1][j - 1]; sum[i][j] += sum[i - 1][j - 1] + cnt[i - 1][j - 1] * i * (abs(j) < abs(j - 1) ? +1 : -1); } if (can[2][i]) { cnt[i][j] += cnt[i - 1][j + 1]; sum[i][j] += sum[i - 1][j + 1] + cnt[i - 1][j + 1] * i * (abs(j) < abs(j + 1) ? +1 : -1); } if (can[3][i]) { cnt[i][j] += cnt[i - 1][j]; sum[i][j] += sum[i - 1][j]; } } } W(sum[n][0]); } return 0; } static auto __init__ = []() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(10); return 42; }();
#include <bits/stdc++.h> using namespace std; ofstream file( debug.txt ); template <typename Int> ostream &operator<<(ostream &s, const vector<Int> &v) { s << [ ; for (auto i = v.begin(); i != v.end(); i++) { s << *i; if (i != prev(v.end())) s << , ; } s << ] ; return s; } template <typename Int, typename INT> ostream &operator<<(ostream &s, const map<Int, INT> &mp) { s << [ ; for (auto &i : mp) { s << [ << i.first << , << i.second << ] ; if (i != *mp.rbegin()) s << , ; } s << ] ; return s; } template <typename Int> ostream &operator<<(ostream &s, const set<Int> &st) { s << [ ; for (auto &i : st) { s << i; if (i != *st.rbegin()) s << , ; } s << ] ; s << n ; return s; } template <typename Int, typename INt> ostream &operator<<(ostream &s, const pair<Int, INt> &x) { s << [ << x.first << , << x.second << ] ; return s; } template <typename T> void print(T *x, int n) { for (int i = 0; i < n; i++) { file << x[i] << n [i == n - 1]; } } template <typename T> ostream &operator<<(ostream &s, const complex<T> &a) { s << ( << a.real() << , << a.imag() << ) ; return s; } template <typename T> T debug(const T &x, const string &s = ) { file << s << = << x << n << flush; return x; } const int dr[]{-1, -1, 0, 1, 1, 1, 0, -1}; const int dc[]{0, 1, 1, 1, 0, -1, -1, -1}; const int N = 2e5 + 10; int n, m, q, ans; bool dead[N]; set<int> edg[N]; void add(int u, int v) { if (!dead[u]) { dead[u] = 1; ans--; } edg[u].insert(v); } void rem(int u, int v) { if (dead[u] && edg[u].size() == 1) { dead[u] = 0; ans++; } edg[u].erase(v); } int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> n >> m; ans = n; for (int i = 0, u, v; i < m; i++) { cin >> u >> v; if (u > v) swap(u, v); add(u, v); } cin >> q; while (q--) { int t; cin >> t; if (t < 3) { int u, v; cin >> u >> v; if (u > v) swap(u, v); if (t == 1) add(u, v); else rem(u, v); } else cout << ans << n ; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, s, ans, x, y, a; bool flag = false; cin >> n >> s; s = s * 100; ans = -1; for (int i = 0; i < n; i++) { cin >> x >> y; a = x * 100 + y; if (a <= s) { ans = max(ans, (100 - a % 100) % 100); } } cout << ans; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; inline int read() { char c = getchar(); int x = 0; bool f = 0; for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); if (f) x = -x; return x; } bool work() { int a = read(), b = read(); if (a > b) swap(a, b); if (a == 0) return (b == 0); if (a * 2 < b) return 0; if (a * 2 == b) return 1; int c = (abs(b - a - a)); return (a + b) % 3 == 0; } signed main() { int n = read(); while (n--) if (work()) puts( YES ); else puts( NO ); return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 100010; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long n, m, p, a[N], b[N], q[N], yue[N], miu[N], f[N], tot; long long ans; long long power(long long a, long long b, long long m) { long long re = 1; while (b) { if (b & 1) re = (long long)re * a % m; a = (long long)a * a % m; b >>= 1; } return re; } long long calc(long long x) { long long i, j, k, l; for (i = 1; i <= tot; i++) if (power(x, yue[i], p) == 1) return yue[i]; } int main() { long long i, j, k; scanf( %I64d%I64d%I64d , &n, &m, &p); for (i = 1; i * i <= p - 1; i++) { if ((p - 1) % i == 0) { yue[++tot] = i; if (i * i < p - 1) yue[++tot] = (p - 1) / i; } } sort(yue + 1, yue + tot + 1); for (i = 1; i <= n; i++) scanf( %I64d , &a[i]); for (i = 1, k = 0; i <= m; i++) { scanf( %I64d , &j); k = gcd(k, j); } k = gcd(k, p - 1); for (i = 1; i <= n; i++) { q[i] = calc(a[i]); a[i] = (p - 1) * gcd(q[i], k) / q[i]; } for (i = 1; i <= tot; i++) for (j = 1; j <= n; j++) if (yue[i] % a[j] == 0) { f[i] = 1; break; } for (i = 1; i <= tot; i++) { ans += (f[i] - miu[i]) * ((p - 1) / yue[i]); for (j = i + 1; j <= tot; j++) if (yue[j] % yue[i] == 0) miu[j] += (f[i] - miu[i]); miu[i] += (f[i] - miu[i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double pi = acos(-1.0); int n, m; bool check(int x) { int a2 = x / 2, a3 = x / 3, a6 = x / 6; a2 -= a6; a3 -= a6; if (max(n - a2, 0) + max(m - a3, 0) <= a6) return true; else return false; } int main() { cin >> n >> m; int l = 1, r = 1e7; int ans = -1; while (l <= r) { int mid = (l + r) / 2; if (check(mid)) r = mid - 1; else l = mid + 1; } cout << l << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; const int K = 23; const long long INF = 1e18; int id[N], a[N], b[N], w[N]; int n, m; vector<int> pos[N]; long long dp[N]; int ls[N * K], rs[N * K], rootcnt = 0; long long vs[N * K]; void copy(int root, int newroot) { ls[newroot] = ls[root]; rs[newroot] = rs[root]; vs[newroot] = vs[root]; } void updates(int root, int& newroot, int l, int r, int pos, int v) { newroot = ++rootcnt; copy(root, newroot); vs[newroot] += v; if (l == r || v == 0) return; int m = (l + r) >> 1; if (pos <= m) updates(ls[root], ls[newroot], l, m, pos, v); else updates(rs[root], rs[newroot], m + 1, r, pos, v); } long long querys(int lt, int rt, int l, int r, int a, int b) { if (l == a && r == b) { return vs[rt] - vs[lt]; } int m = (l + r) >> 1; if (b <= m) return querys(ls[lt], ls[rt], l, m, a, b); else if (a > m) return querys(rs[lt], rs[rt], m + 1, r, a, b); else { return querys(ls[lt], ls[rt], l, m, a, m) + querys(rs[lt], rs[rt], m + 1, r, m + 1, b); } } long long val[N << 2]; long long tag[N << 2]; void build(int root, int l, int r) { val[root] = -INF; tag[root] = 0; if (l == r) return; int m = (l + r) >> 1; build((root << 1), l, m); build((root << 1 | 1), m + 1, r); } void pushdown(int root) { long long c = tag[root]; tag[root] = 0; val[(root << 1)] += c; tag[(root << 1)] += c; val[(root << 1 | 1)] += c; tag[(root << 1 | 1)] += c; } void setv(int root, int l, int r, int pos, long long v) { if (l == r) { val[root] = v; return; } pushdown(root); int m = (l + r) >> 1; if (pos <= m) setv((root << 1), l, m, pos, v); else setv((root << 1 | 1), m + 1, r, pos, v); val[root] = max(val[(root << 1)], val[(root << 1 | 1)]); } void update(int root, int l, int r, int a, int b, long long v) { if (v == 0) return; if (a > b) return; if (l == a && r == b) { val[root] += v; tag[root] += v; return; } pushdown(root); int m = (l + r) >> 1; if (b <= m) update((root << 1), l, m, a, b, v); else if (a > m) update((root << 1 | 1), m + 1, r, a, b, v); else { update((root << 1), l, m, a, m, v); update((root << 1 | 1), m + 1, r, m + 1, b, v); } val[root] = max(val[(root << 1)], val[(root << 1 | 1)]); } long long query(int root, int l, int r, int a, int b) { if (a > b) return -INF; if (l == a && r == b) return val[root]; pushdown(root); int m = (l + r) >> 1; if (b <= m) return query((root << 1), l, m, a, b); else if (a > m) return query((root << 1 | 1), m + 1, r, a, b); else return max(query((root << 1), l, m, a, m), query((root << 1 | 1), m + 1, r, m + 1, b)); } bool hasAns() { int len = 0; for (int i = 1; i <= n && len < m; i++) { if (a[i] == b[len + 1]) len++; } return len == m; } int root[N]; long long lessSum(int l, int r, int v) { if (l > r || v <= 0) return 0; return querys(root[l - 1], root[r], 1, n, 1, v); } void solve() { scanf( %d , &n); root[0] = 0; for (int i = 1; i <= n; i++) scanf( %d , a + i); for (int i = 1; i <= n; i++) { scanf( %d , w + i); updates(root[i - 1], root[i], 1, n, a[i], max(0, w[i])); } scanf( %d , &m); for (int i = 1; i <= m; i++) scanf( %d , b + i), id[b[i]] = i; for (int i = 1; i <= n; i++) { if (id[a[i]]) pos[id[a[i]]].push_back(i); } if (!hasAns()) { printf( NO n ); return; } printf( YES n ); for (int i = 1; i <= n; i++) dp[i] = -INF; build(1, 1, n); for (auto p : pos[m]) { dp[p] = lessSum(p + 1, n, b[m]) + w[p]; setv(1, 1, n, p, dp[p]); } for (int i = m - 1; i >= 1; i--) { vector<int> vec; for (auto j : pos[i + 1]) vec.push_back(j); for (auto j : pos[i]) vec.push_back(j); sort(vec.begin(), vec.end()); for (int j = (int)vec.size() - 2; j >= 0; j--) { update(1, 1, n, vec[j + 1], n, lessSum(vec[j] + 1, vec[j + 1] - 1, b[i])); if (a[vec[j]] == b[i]) { dp[vec[j]] = query(1, 1, n, vec[j + 1], n) + w[vec[j]]; if (w[vec[j]] > 0) { update(1, 1, n, vec[j + 1], n, w[vec[j]]); } } } for (int j = (int)vec.size() - 2; j >= 0; j--) { update(1, 1, n, vec[j + 1], n, -lessSum(vec[j] + 1, vec[j + 1] - 1, b[i])); if (a[vec[j]] == b[i]) { if (w[vec[j]] > 0) { update(1, 1, n, vec[j + 1], n, -w[vec[j]]); } } } for (auto j : pos[i + 1]) { setv(1, 1, n, j, -INF); } assert(val[1] == -INF); for (auto j : pos[i]) { setv(1, 1, n, j, dp[j]); } } long long ans = 0; for (int i = 1; i <= n; i++) ans += w[i]; ans -= val[1]; printf( %lld n , ans); } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long Pow_Mod(long long a, long long b) { if (b == 0) return 1; long long tmp = Pow_Mod(a, b / 2); tmp = (tmp * tmp) % 1000003; if (b % 2) return (tmp * a) % 1000003; return tmp; } long long C(long long a, long long b) { if (b == 0 || b == a) return 1; long long ans = 1; for (int i = 1; i <= b; i++) { ans = (ans * (a - i + 1)) % 1000003; ans = (ans * Pow_Mod(i, 1000003 - 2)) % 1000003; } return ans; } int main() { long long a, b; cin >> a >> b; a += b; cout << (C(a, b) - 1 + 1000003) % 1000003 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t; cin >> t; while (t--) { int n, k, x, rem; cin >> n >> k; x = k / (n - 1); rem = k - x * (n - 1); if (!rem) cout << x * n - 1 << n ; else cout << x * n + rem << n ; } }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC optimize( unroll-loops ) using namespace std; using pii = pair<int, int>; const int MOD = 1e9 + 7, N = 2e3 + 10; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<pair<long long, long long>> v(n); for (long long i = 0; i < n; i++) { long long x; cin >> x; long long cm = -1; long long salary; for (long long i = 0; i < x; i++) { cin >> salary; if (salary > cm) cm = salary; } v[i].first = cm; v[i].second = x; } sort(v.begin(), v.end()); long long sum = 0; for (long long i = 0; i < n; i++) { sum += (v[n - 1].first - v[i].first) * v[i].second; } cout << sum; }
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 123; const int mod = 1e9 + 9; int n; long long k, d; int a[maxn]; int m; int L[maxn]; long long t[maxn * 4]; long long ad[maxn * 4]; void build(int v, int l, int r) { ad[v] = 0; t[v] = 0; if (l == r) { } else { int mid = (l + r) / 2; build(v * 2, l, mid); build(v * 2 + 1, mid + 1, r); } } void push(int v, int tl, int tr) { if (tl == tr) return; t[v * 2] += ad[v]; t[v * 2 + 1] += ad[v]; ad[v * 2] += ad[v]; ad[v * 2 + 1] += ad[v]; ad[v] = 0; } int get(int v, int tl, int tr, int l, int r, long long k) { push(v, tl, tr); if (tl > r || l > tr) { return -1; } if (t[v] > k) { return -1; } if (tl == tr) { return tl; } int mid = (tl + tr) / 2; int res = get(v * 2, tl, mid, l, r, k); if (res != -1) return res; return get(v * 2 + 1, mid + 1, tr, l, r, k); } void add(int v, int tl, int tr, int l, int r, long long d) { push(v, tl, tr); if (tl > r || l > tr) { return; } if (l <= tl && tr <= r) { t[v] += d; ad[v] += d; return; } int mid = (tl + tr) / 2; add(v * 2, tl, mid, l, r, d); add(v * 2 + 1, mid + 1, tr, l, r, d); t[v] = min(t[v * 2], t[v * 2 + 1]); } pair<int, int> calc(vector<int> b) { m = b.size(); map<int, int> c; for (int i = 0; i < m; i++) { if (c.count(b[i]) == 0) { L[i] = 0; } else { L[i] = c[b[i]] + 1; } c[b[i]] = i; } for (int i = 1; i < m; i++) { L[i] = max(L[i], L[i - 1]); } build(1, 0, m - 1); vector<int> mx; vector<int> mi; pair<int, int> ans = make_pair(0, 0); for (int i = 0; i < m; i++) { while (mx.size() > 0 && b[mx.back()] < b[i]) { int cur = mx.back(); mx.pop_back(); if (mx.size() > 0) { add(1, 0, m - 1, mx.back() + 1, cur, -b[cur]); } else { add(1, 0, m - 1, 0, cur, -b[cur]); } } if (mx.size() > 0) { add(1, 0, m - 1, mx.back() + 1, i, b[i]); } else { add(1, 0, m - 1, 0, i, b[i]); } mx.push_back(i); while (mi.size() > 0 && b[mi.back()] > b[i]) { int cur = mi.back(); mi.pop_back(); if (mi.size() > 0) { add(1, 0, m - 1, mi.back() + 1, cur, b[cur]); } else { add(1, 0, m - 1, 0, cur, b[cur]); } } if (mi.size() > 0) { add(1, 0, m - 1, mi.back() + 1, i, -b[i]); } else { add(1, 0, m - 1, 0, i, -b[i]); } mi.push_back(i); int cnt = i - get(1, 0, m - 1, L[i], i, k) + 1; if (ans.first < cnt) { ans.first = cnt; ans.second = i; } add(1, 0, m - 1, 0, i, -1); } return ans; } void solve() { cin >> n >> k >> d; for (int i = 0; i < n; i++) { cin >> a[i]; } int mi = *min_element(a, a + n); for (int i = 0; i < n; i++) { a[i] -= mi; } pair<int, int> ans = make_pair(0, 0); if (d == 0) { int cnt = 1; for (int i = 1; i < n; i++) { if (a[i] != a[i - 1]) { if (ans.first < cnt) { ans.first = cnt; ans.second = i - 1; } cnt = 1; } else { cnt++; } } if (ans.first < cnt) { ans.first = cnt; ans.second = n - 1; } cout << ans.second - ans.first + 2 << << ans.second + 1 << n ; return; } for (int i = 0; i < n;) { int j = i; vector<int> cur; while (i < n && a[j] % d == a[i] % d) { cur.push_back(a[i] / d); i++; } pair<int, int> res = calc(cur); res.second += j; if (res.first > ans.first) { ans = res; } } cout << ans.second - ans.first + 2 << << ans.second + 1 << n ; } int main() { int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 123, BITS = 11, MAX = 7; int c[N], num[N]; int a[MAX][MAX]; char s[N]; int bit(int x) { int res = 0; while (x != 0) { res += x % 2; x /= 2; } return res; } int main() { int n; scanf( %d , &n); for (int i = 0; i < n; ++i) { scanf( %s , s); if (s[0] == R ) c[i] = 0; if (s[0] == G ) c[i] = 1; if (s[0] == B ) c[i] = 2; if (s[0] == Y ) c[i] = 3; if (s[0] == W ) c[i] = 4; num[i] = s[1] - 1 ; } int m = 0; for (int i = 0; i < n; ++i) { if (a[c[i]][num[i]] == 0) { ++m; a[c[i]][num[i]] = 1; } } int ans = 10; for (int cnt = 0; cnt < (1 << BITS); ++cnt) { int count = 0; for (int i = 0; i < 5; ++i) { for (int j = 0; j < 5; ++j) { for (int k = 0; k < 5; ++k) { for (int l = 0; l < 5; ++l) { if (a[i][j] == 1 && a[k][l] == 1) { int tmp = 0; if (i != k) tmp = ((1 << (i + 5)) & cnt) || ((1 << (k + 5)) & cnt); int tmp2 = 0; if (j != l) tmp2 = ((1 << j) & cnt) || ((1 << l) & cnt); if (tmp || tmp2) ++count; } } } } } if (count == m * (m - 1)) { int temp = bit(cnt); if (ans > temp) ans = temp; } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; bool graf[100][100]; int n, m; bool visited[100]; bool spoken[100]; int stupid; int tab[100]; int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; i++) { int x; cin >> x; if (x == 0) stupid++; for (int j = 0; j < x; j++) { cin >> tab[j]; tab[j]--; spoken[tab[j]] = true; } for (int j = 0; j < x; j++) { for (int q = j + 1; q < x; q++) { graf[tab[j]][tab[q]] = true; graf[tab[q]][tab[j]] = true; } } } queue<int> q; int add = 0; for (int i = 0; i < m; i++) { if (!spoken[i] || visited[i]) continue; q.push(i); visited[i] = true; add++; while (!q.empty()) { int v = q.front(); q.pop(); for (int j = 0; j < m; j++) { if (graf[v][j] && !visited[j]) { visited[j] = true; q.push(j); } } } } if (add > 0) add--; stupid += add; cout << stupid << endl; }
#include <bits/stdc++.h> using namespace std; int a, b, n, m, l, t; bool check(int r) { if (a + 1ll * b * (r - 1) <= t && (1ll * a * (r - l + 1) + 1ll * b * (1ll * r * (r - 1) / 2 - 1ll * (l - 1) * (l - 2) / 2)) <= 1ll * m * t) return 1; else return 0; } int main() { scanf( %d %d %d , &a, &b, &n); int res; while (n--) { scanf( %d %d %d , &l, &t, &m); res = l; if (check(res) == 0) cout << -1 << endl; else { int low = l; int mid; int up = 1000100; while (low <= up) { mid = (low + up) / 2; if (check(mid) == 1) { res = mid; low = mid + 1; } else { up = mid - 1; } } printf( %d n , res); } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, k; int a[100005]; set<long long> present; int ans; int main() { scanf( %d%d , &n, &k); for (int i = 0; i < n; i++) scanf( %d , &a[i]); sort(a, a + n); for (int i = n - 1; i >= 0; i--) { if (present.find((long long)a[i] * k) == present.end()) { ans++; present.insert(a[i]); } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void pp(T v) { for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it) cout << *it << ; cout << endl; } template <class T> void pp(T v, int n) { for (int i = 0; i < (int)n; i++) cout << v[i] << ; cout << endl; } const int INF = 1 << 28; const double EPS = 1.0e-9; bool solve(const vector<pair<int, string> > &data) { const int n = data.size(); for (int i = 0; i < (int)n; i++) { if (data[i].first > i) return false; } vector<int> height(n); vector<int> ans; for (int i = 0, h = n; i < n; i++, h--) { ans.insert(ans.begin() + data[i].first, i); height[i] = h; } for (int i = 0; i < (int)n; i++) { cout << data[ans[i]].second << << height[ans[i]] << endl; } return true; } int main(void) { int n; cin >> n; vector<pair<int, string> > data(n); for (int i = 0; i < (int)n; i++) { string name; int a; cin >> name >> a; data[i] = make_pair(a, name); } sort((data).begin(), (data).end()); bool ans = solve(data); if (ans == false) cout << -1 << endl; return 0; }
// Created by aadi // Never give up , Miracles never happen in real life. #include<bits/stdc++.h> using namespace std ; #define FAST ios_base::sync_with_stdio(false);cin.tie(NULL); #define int long long int #define MIN LLONG_MIN #define MAX LLONG_MAX #define set1D(a,val,n) { for(int i=0;i<n;i++) a[i]=val; } #define set2D(a,val,n,m) { for(int i=0;i<n;i++) for(int j=0;j<m;j++)a[i][j]=val; } const double pi = 3.14159265358979323846; #define endl n #define db1(x) cout<<#x<< = <<x<< n #define db2(x,y) cout<<#x<< = <<x<< , <<#y<< = <<y<< n #define db3(x,y,z) cout<<#x<< = <<x<< , <<#y<< = <<y<< , <<#z<< = <<z<< n #define all(x) x.begin(),x.end() int32_t main() { //code; FAST int tt; cin >> tt; while (tt--) { //write code int n; cin >> n; string a, b; cin >> a >> b; int red = 0; int blue = 0; for (int i = 0; i < n; i++) { if (a[i] - 0 > b[i] - 0 ) { red++; } else if (a[i] - 0 < b[i] - 0 ) { blue++; } } if (red > blue) { cout << RED ; } else if (blue > red) { cout << BLUE ; } else { cout << EQUAL ; } cout << endl; } return 0; }
#include <bits/stdc++.h> const int N = 2001, INF = 1e9 + 7; long long g[N][N], f[N][N]; int main() { int n, k; std::cin >> n >> k; std::string s; std::cin >> s; f[0][0] = g[0][0] = 1; for (int i = 1; i <= n; ++i) for (int j = 0; j <= k; ++j) { g[i][j] = f[i - 1][j] * (s[n - i] - a ) + g[i - 1][j]; f[i][j] = g[i][j]; for (int p = 1; p <= i + 1 - p && p * (i + 1 - p) <= j; ++p) { f[i][j] += f[i - p][j - p * (i + 1 - p)] * ( z - s[n - i + p - 1]); if (p < i + 1 - p) f[i][j] += f[p - 1][j - p * (i + 1 - p)] * ( z - s[n - p]); } g[i][j] %= INF; f[i][j] %= INF; } std::cout << f[n][k]; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &t) { t = 0; char ch = getchar(); int f = 1; while ( 0 > ch || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } do { (t *= 10) += ch - 0 ; ch = getchar(); } while ( 0 <= ch && ch <= 9 ); t *= f; } int n, pos, ans[2010], vis[2010]; struct node { long long x, y; bool operator<(node t) { if (x != t.x) return y < t.y; return x < t.x; } node operator-(node t) { return (node){x - t.x, y - t.y}; } long long operator*(node t) { return x * t.y - y * t.x; } } d[2010]; long long t; char s[2010]; int main() { read(n); for (int i = 1; i <= n; i++) read(d[i].x), read(d[i].y); scanf( %s , s + 1); for (int i = 1; i <= n; i++) if (!pos || d[i] < d[pos]) pos = i; ans[1] = pos; vis[pos] = 1; int tmp; for (int i = 2; i < n; i++) { tmp = 0; for (int j = 1; j <= n; j++) if (!vis[j]) { t = (d[j] - d[pos]) * (d[tmp] - d[pos]); if (!tmp || (s[i - 1] == L && t > 0) || (s[i - 1] == R && t < 0)) tmp = j; } ans[i] = pos = tmp; vis[pos] = 1; } for (int i = 1; i <= n; i++) if (!vis[i]) { ans[n] = i; break; } for (int i = 1; i <= n; i++) printf( %d , ans[i]); printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T0, typename T1> std::ostream& operator<<(std::ostream& os, const map<T0, T1>& v) { for (typename map<T0, T1>::const_iterator p = v.begin(); p != v.end(); p++) { os << p->first << : << p->second << ; } return os; } template <typename T0, typename T1> std::ostream& operator<<(std::ostream& os, const pair<T0, T1>& v) { os << v.first << : << v.second << ; return os; } template <typename T> std::ostream& operator<<(std::ostream& os, const vector<T>& v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << ; } return os; } template <typename T> std::ostream& operator<<(std::ostream& os, const vector<vector<T> >& v) { for (int i = 0; i < (int)v.size(); i++) { os << v[i] << endl; } return os; } template <typename T> std::ostream& operator<<(std::ostream& os, const set<T>& v) { vector<T> tmp(v.begin(), v.end()); os << tmp; return os; } template <typename T> std::ostream& operator<<(std::ostream& os, const deque<T>& v) { vector<T> tmp(v.begin(), v.end()); os << tmp; return os; } int main() { long long N, W, H; while (cin >> H >> W) { vector<string> w(H); for (int y = 0, _n = (H); (y) < (int)_n; ++y) cin >> w[y]; vector<vector<long long> > tb(H, vector<long long>(W)); vector<vector<long long> > tbX(H, vector<long long>(W)); vector<vector<long long> > tbY(H, vector<long long>(W)); for (int y = 0, _n = (H); (y) < (int)_n; ++y) for (int x = 0, _n = (W); (x) < (int)_n; ++x) { { long long v = 0; if (0 <= y - 1) v += tb[y - 1][x]; if (0 <= y - 1 && w[y - 1][x] == . && w[y][x] == . ) v++; if (0 <= x - 1) v += tb[y][x - 1]; if (0 <= x - 1 && w[y][x - 1] == . && w[y][x] == . ) v++; if (0 <= x - 1 && 0 <= y - 1) v -= tb[y - 1][x - 1]; tb[y][x] = v; } { long long v = 0; if (0 <= x - 1) v += tbY[y][x - 1]; if (0 <= y - 1 && w[y - 1][x] == . && w[y][x] == . ) v++; tbY[y][x] = v; } { long long v = 0; if (0 <= y - 1) v += tbX[y - 1][x]; if (0 <= x - 1 && w[y][x - 1] == . && w[y][x] == . ) v++; tbX[y][x] = v; } } cin >> N; for (int i = 0, _n = (N); (i) < (int)_n; ++i) { long long y1, x1, y2, x2; cin >> y1 >> x1 >> y2 >> x2; y1--; x1--; y2--; x2--; long long v = tb[y2][x2]; if (0 <= y1 - 1) v -= tb[y1 - 1][x2]; { long long vv = tbY[y1][x2]; if (0 <= x1 - 1) vv -= tbY[y1][x1 - 1]; v -= vv; } if (0 <= x1 - 1) v -= tb[y2][x1 - 1]; { long long vv = tbX[y2][x1]; if (0 <= y1 - 1) vv -= tbX[y1 - 1][x1]; v -= vv; } if (0 <= x1 - 1 && 0 <= y1 - 1) v += tb[y1 - 1][x1 - 1]; cout << v << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; int n, g, p, d, a, b, act, nxt; long long res; int Next[(1000000 + 6)]; pair<int, int> Stacje[(1000000 + 6)]; stack<int> S; int main() { scanf( %d %d %d , &d, &g, &n); p = g; for (int i = 1; i <= n; i++) { scanf( %d %d , &a, &b); Stacje[i] = make_pair(a, b); } Stacje[0] = make_pair(0, INF); Stacje[n + 1] = make_pair(d, -INF); sort(Stacje + 1, Stacje + n + 1); for (int i = n + 1; i >= 0; i--) { while (!S.empty() && Stacje[S.top()].second > Stacje[i].second) { S.pop(); } Next[i] = S.empty() ? -1 : S.top(); S.push(i); } while (act != n + 1) { if (Stacje[Next[act]].first - Stacje[act].first <= g) { int dis = Stacje[Next[act]].first - Stacje[act].first; p -= dis; if (p < 0) { res += (-p) * (long long)Stacje[act].second; p = 0; } act = Next[act]; } else { int dis = Stacje[act + 1].first - Stacje[act].first; if (dis > g) { break; } res += (g - p) * (long long)Stacje[act].second; p = g - dis; act++; } } printf( %lld n , act != n + 1 ? -1 : res); }
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; char c = getchar(); T sig = 1; for (; !isdigit(c); c = getchar()) if (c == - ) sig = -1; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - 0 ; x *= sig; } class Solution { public: void solve() { int n; read(n); string s; cin >> s; string t(s); for (int i = 0; i < n; ++i) if (s[i] == ? ) s[i] = 0 , t[i] = 1 ; vector<int> p(n + 2), q(n + 2); for (int i = n; i >= 1; --i) p[i] = s[i - 1] == 0 ? p[i + 1] + 1 : 0, q[i] = t[i - 1] == 1 ? q[i + 1] + 1 : 0; vector<int> np(n + 1), nq(n + 1); for (int i = n, pp = 0x3f3f3f3f, qq = 0x3f3f3f3f, npp = 0x3f3f3f3f, nqq = 0x3f3f3f3f; i >= 1; --i) { if (s[i - 1] == 0 ) pp = i; if (s[i - 1] == 1 ) npp = pp; np[i] = npp; if (t[i - 1] == 0 ) nqq = qq; if (t[i - 1] == 1 ) qq = i; nq[i] = nqq; } printf( %d , n); for (int i = 2; i <= n; ++i) { int cnt = 0, idx = 1, last = 0; while (idx + i - 1 <= n) { int delta = max(p[idx], q[idx]); if (p[idx] <= i && np[last] == idx) np[last] = np[idx]; if (q[idx] <= i && nq[last] == idx) nq[last] = nq[idx]; last = idx; if (delta >= i) cnt += delta / i, idx += delta / i * i; else idx = min(np[idx], nq[idx]); } printf( %d , cnt); } } }; int main() { Solution solution = Solution(); solution.solve(); }
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; template <typename T> static T aabs(T x) { if (x < 0) return -x; return x; } template <typename T> static void read(T &S) { char K[1000009]; scanf( %s , &K); S = K; } template <typename T, typename U> static void amax(T &x, U y) { if (x < y) x = y; } template <typename T, typename U> static void amin(T &x, U y) { if (x > y) x = y; } template <typename T, typename U> static void aswap(T &x, U &y) { T temp; temp = x; x = y; y = temp; } template <typename T, typename U> static long long gcd(T a, U b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } template <typename T, typename U> static long long lcm(T a, U b) { return a / gcd(a, b) * b; } template <typename T, typename U> static long long fpow(T n, U k, int p = MOD) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } const int N5 = 100009; const int N6 = 1000009; int st[2 * N5 * 4]; string s; set<int> mst[200]; void build(int id, int l, int r) { if (l == r) { st[id] = 1; return; } int mid = (l + r) / 2; build(id * 2, l, mid); build(id * 2 + 1, mid + 1, r); st[id] = st[id * 2] + st[id * 2 + 1]; } void update(int id, int l, int r, int pos, int value) { if (r < pos || l > pos) return; if (l == r) { st[id] = value; return; } int mid = (l + r) / 2; update(id * 2, l, mid, pos, value); update(id * 2 + 1, mid + 1, r, pos, value); st[id] = st[id * 2] + st[id * 2 + 1]; } int query(int id, int l, int r, int pos) { if (l == r) return l; int mid; mid = (l + r) / 2; if (pos > st[id * 2]) query(id * 2 + 1, mid + 1, r, pos - st[id * 2]); else query(id * 2, l, mid, pos); } void finish(int id, int l, int r) { if (l == r) { if (st[id]) printf( %c , s[l]); return; } int mid; mid = (l + r) / 2; finish(id * 2, l, mid); finish(id * 2 + 1, mid + 1, r); } int conv(char c) { if (c >= a && c <= z ) return c - a ; if (c >= A && c <= z ) return c - A + 27; return c - 0 + 55; } int main() { int n, q; scanf( %d%d , &n, &q); string ss; read(ss); s = + ss; for (int i = 1; i <= n; i++) mst[conv(s[i])].insert(i); for (int i = 0; i <= 100; i++) mst[i].insert(n + 1); build(1, 1, n); while (q--) { int l, r; string re; char ch; scanf( %d%d , &l, &r); read(re); ch = re[0]; l = query(1, 1, n, l); r = query(1, 1, n, r); set<int>::iterator it, ait; it = mst[conv(ch)].lower_bound(l); while (*it <= r) { update(1, 1, n, *it, 0); ait = it; ++it; mst[conv(ch)].erase(ait); } } finish(1, 1, n); return 0; return 0; }
#include <bits/stdc++.h> int main() { int n; scanf( %d , &n); int S = 0; for (int i = 1; S < n; ++i) S += i; puts(S == n ? YES : NO ); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 2000000000; struct pkt { int k, r; }; struct prze { int k, r1, r2; }; bool porownanie(pkt a, pkt b) { if (a.k < b.k) return true; if (a.k > b.k) return false; if (a.r < b.r) return true; return false; } int main() { ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; vector<pkt> pkty(m); for (int i = 0; i < m; ++i) { cin >> pkty[i].r >> pkty[i].k; --pkty[i].r; --pkty[i].k; } sort(pkty.begin(), pkty.end(), porownanie); vector<vector<prze> > zab; for (int L = 0; L < m;) { int P = L; while (P < m && pkty[L].k == pkty[P].k) ++P; --P; vector<prze> t; for (int l = L; l <= P;) { int p = l; while (p <= P && pkty[p].r - pkty[l].r == p - l) ++p; --p; t.push_back({pkty[l].k, pkty[l].r, pkty[p].r}); l = p + 1; } zab.push_back(t); L = P + 1; } for (int i = 0; i < zab.size(); ++i) { if (zab[i].size() == 1 && zab[i][0].r1 == 0 && zab[i][0].r2 == n - 1) { cout << -1 ; return 0; } } vector<vector<prze> > doz; if (zab[0][0].k != 0) { vector<prze> t; t.push_back({0, 0, n - 1}); doz.push_back(t); } for (int i = 0; i < zab.size(); ++i) { if (i != 0) { if (zab[i - 1][0].k != zab[i][0].k - 1) { vector<prze> t; t.push_back({zab[i][0].k - 1, 0, n - 1}); doz.push_back(t); } } int ile_prze = zab[i].size(); vector<prze> t; if (zab[i][0].r1 - 1 >= 0) t.push_back({zab[i][0].k, 0, zab[i][0].r1 - 1}); for (int j = 0; j < ile_prze - 1; ++j) t.push_back({zab[i][0].k, zab[i][j].r2 + 1, zab[i][j + 1].r1 - 1}); if (zab[i][ile_prze - 1].r2 < n - 1) t.push_back({zab[i][0].k, zab[i][ile_prze - 1].r2 + 1, n - 1}); doz.push_back(t); } if (zab.back()[0].k != n - 1) { vector<prze> t; t.push_back({n - 1, 0, n - 1}); doz.push_back(t); } zab.clear(); vector<vector<int> > lo(doz.size()); for (int i = 0; i < doz.size(); ++i) for (int j = 0; j < doz[i].size(); ++j) lo[i].push_back(INF); for (int i = 0; i < lo[0].size(); ++i) { if (i == 0) lo[0][i] = 0; else lo[0][i] = INF; } for (int i = 1; i < doz.size(); ++i) { vector<int> g(doz[i].size(), INF); int k = 0; for (int j = 0; j < doz[i].size(); ++j) { while (k < doz[i - 1].size() && doz[i - 1][k].r2 < doz[i][j].r1) ++k; if (k < doz[i - 1].size()) g[j] = k; } vector<int> d(doz[i].size(), INF); k = doz[i - 1].size() - 1; for (int j = doz[i].size() - 1; j >= 0; --j) { while (k >= 0 && doz[i - 1][k].r1 > doz[i][j].r2) --k; if (k >= 0) d[j] = k; } for (int j = 0; j < doz[i].size(); ++j) { if (d[j] != INF && g[j] != INF && g[j] <= d[j]) { for (int k = g[j]; k <= d[j]; ++k) { if (lo[i - 1][k] != INF) { if (lo[i - 1][k] < doz[i][j].r1) lo[i][j] = min(lo[i][j], doz[i][j].r1); else { if (lo[i - 1][k] <= doz[i][j].r2) lo[i][j] = min(lo[i][j], lo[i - 1][k]); } } } } } } int x = doz.size(); int y = doz[x - 1].size(); if (doz[x - 1][y - 1].r2 == n - 1 && lo[x - 1][y - 1] != INF) cout << 2 * (n - 1); else cout << -1 ; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxi = 1e6 + 10; const long long mo = 1e9 + 7; long long a[maxi]; long long m, l, r, k; string s; vector<int> v[maxi]; long long mat[53][3][3]; void mul_mat(int idx1, int idx2, int idx) { for (int i = 1; i <= 2; i++) for (int j = 1; j <= 2; j++) for (int k = 1; k <= 2; k++) mat[idx][i][j] += mat[idx1][i][k] * mat[idx2][k][j]; for (int i = 1; i <= 2; i++) for (int j = 1; j <= 2; j++) mat[idx][i][j] %= m; } void fib(long long x) { long long fi = 1; long long fj = 1; mat[51][1][1] = 1; mat[51][2][2] = 1; for (int i = 0; i <= 50; i++) if (x & (1ll << i)) { mul_mat(i, 51, 52); for (int j = 1; j <= 2; j++) for (int o = 1; o <= 2; o++) { mat[51][j][o] = mat[52][j][o]; mat[52][j][o] = 0; } } cout << mat[51][2][1] << endl; return; } int main() { cin >> m >> l >> r >> k; long long x = 1; mat[0][1][1] = 1; mat[0][1][2] = 1; mat[0][2][1] = 1; mat[0][2][2] = 0; for (long long i = 1; i * i <= r; i++) { long long last = r / i; long long first = (r / (i + 1)) + 1; if (i - k >= 0) { long long val = i - k; long long li = last; long long ri = first + 1; while (li < ri - 1) { long long mid = (li + ri) / 2; if ((l - 1) / mid <= val) li = mid; else ri = mid; } if ((l - 1) / li <= val) { x = max(x, li); } } } long long sqr = floor(sqrt(r)); for (long long num = sqr; num > 0; num--) { long long i = r / num; long long last = r / i; long long first = r / (i + 1) + 1; if (i - k >= 0) { long long val = i - k; long long li = last; long long ri = first + 1; while (li < ri - 1) { long long mid = (li + ri) / 2; if ((l - 1) / mid <= val) li = mid; else ri = mid; } if ((l - 1) / li <= val) { x = max(x, li); } } } for (int i = 1; i <= 50; i++) mul_mat(i - 1, i - 1, i); fib(x); return 0; }
#include <bits/stdc++.h> using namespace std; long long read() { char c = getchar(); long long x = 1; long long s = 0; while (c < 0 || c > 9 ) { if (c == - ) x = -1; c = getchar(); } while (c >= 0 && c <= 9 ) { s = s * 10 + c - 0 ; c = getchar(); } return s * x; } const long long N = 4e5 + 5; vector<long long> v[N]; long long n, x, y, siz[N], tong; void dfs(long long u, long long fa) { long long cnt = 1, tmp = 0; for (unsigned long long i = 0; i < v[u].size(); i++) { if (v[u][i] == fa) continue; dfs(v[u][i], u); cnt++; if (siz[v[u][i]] == 1) tmp++; } siz[u] = cnt; if (tmp) { tong = tong + tmp - 1; } else { siz[u] = 1; } } void solve() { n = read(); for (long long i = 1; i < n; i++) { x = read(); y = read(); v[x].push_back(y); v[y].push_back(x); } if (n == 2) { cout << 1 << n ; } else { dfs(1, 0); cout << tong + 1 << n ; } for (long long i = 1; i <= n; i++) v[i].clear(), siz[i] = 0; tong = 0; } signed main() { long long T; cin >> T; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1100; const long long MOD = 998244353; int N, K; int arr[MAXN]; int dp[MAXN][MAXN]; int ps[MAXN][MAXN]; long long figure(int k) { if (k * (K - 1) > arr[N - 1]) return 0; for (int i = 0; i <= N; i++) for (int j = 0; j <= K; j++) dp[i][j] = ps[i][j] = 0; for (int i = 1; i <= N; i++) dp[i][1] = 1; int cstop = 0; for (int i = 0; i < N; i++) { while (cstop < i && arr[i] - arr[cstop] >= k) cstop++; for (int j = 1; j < K; j++) { dp[i + 1][j + 1] = ps[cstop][j]; } for (int j = 0; j <= K; j++) { ps[i + 1][j] = (ps[i][j] + dp[i + 1][j]) % MOD; } } return ps[N][K]; } int main() { ios_base::sync_with_stdio(0); cin >> N >> K; for (int i = 0; i < N; i++) cin >> arr[i]; sort(arr, arr + N); long long ans = 0; for (int i = 1; i <= 100000; i++) ans = (ans + figure(i)) % MOD; cout << ans << n ; }
#include <bits/stdc++.h> using namespace std; const int N = 200010; inline int gi() { register int x = 0; register char c = getchar(); bool f = 0; while (c != - && (c < 0 || c > 9 )) c = getchar(); if (c == - ) c = getchar(), f = 1; while (c >= 0 && c <= 9 ) x = x * 10 + c - 0 , c = getchar(); return f ? -x : x; } struct node { int to, next; } g[N << 1]; int last[N], gl; inline void add(int x, int y) { g[++gl] = (node){y, last[x]}; last[x] = gl; return; } int sum, f[N], siz[N], rt; bool vis[N]; void getroot(int u, int fa) { siz[u] = 1; f[u] = 0; for (int i = last[u]; i; i = g[i].next) { int v = g[i].to; if (v == fa || vis[v]) continue; getroot(v, u); f[u] = max(f[u], siz[v]); siz[u] += siz[v]; } f[u] = max(f[u], sum - siz[u]); if (f[rt] > f[u]) rt = u; return; } char a[N]; int s[N], t[10050000]; void dfs(int x, int fa, int p, int S) { t[S ^= (1 << s[x])] += p; for (int i = last[x]; i; i = g[i].next) { int v = g[i].to; if (v == fa || vis[v]) continue; dfs(v, x, p, S); } } long long ans[N]; long long calc(int x, int fa, int S) { S ^= (1 << s[x]); long long cnt = t[S]; for (int i = 0; i < 20; i++) cnt += t[S ^ (1 << i)]; for (int i = last[x]; i; i = g[i].next) { int v = g[i].to; if (v == fa || vis[v]) continue; cnt += calc(v, x, S); } ans[x] += cnt; return cnt; } void solve(int x) { vis[x] = 1; dfs(x, 0, 1, 0); long long cnt = t[0]; for (int i = 0; i < 20; i++) cnt += t[1 << i]; for (int i = last[x]; i; i = g[i].next) { int v = g[i].to; if (vis[v]) continue; dfs(v, x, -1, 1 << s[x]); cnt += calc(v, x, 0); dfs(v, x, 1, 1 << s[x]); } dfs(x, 0, -1, 0); ans[x] += cnt / 2; for (int i = last[x]; i; i = g[i].next) { int v = g[i].to; if (vis[v]) continue; sum = siz[v]; rt = 0; getroot(v, 0); solve(rt); } return; } int main() { int n = gi(); for (int i = 1; i < n; i++) { int u = gi(), v = gi(); add(u, v); add(v, u); } scanf( %s , a); for (int i = 0; i < n; i++) s[i + 1] = a[i] - a ; f[0] = sum = n; rt = 0; getroot(1, 0); solve(rt); for (int i = 1; i <= n; i++) printf( %lld , ans[i] + 1); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int r[200], l[200]; int main() { memset(l, 10, sizeof(l)); memset(r, -1, sizeof(r)); cin.sync_with_stdio(false); cin >> n >> m; string s; int w = 0; for (int i = 0; i < n; ++i) { cin >> s; for (int j = 0; j < m; ++j) if (s[j] == W ) { if (l[i] > m) l[i] = j; r[i] = j; w++; } } if (!w) { cout << 0; return 0; } int e = 0; for (int i = n - 1; i >= 0; --i) if (l[i] < m) { e = i + 1; break; } int j = 0; int res = 0; for (int i = 0; i < n; ++i) { if (i == e) { --res; break; } if (i & 1) { if (i == n - 1) res += j - l[i]; else { int k = min(l[i], l[i + 1]); if (k >= m) k = j; res += abs(j - k) + 1; j = k; } } else { if (i == n - 1) res += r[i] - j; else { int k = max(r[i], r[i + 1]); if (k == -1) k = j; res += abs(k - j) + 1; j = k; } } } cout << res; return 0; }
#include <bits/stdc++.h> int main() { int sum, n, i, t, p, sum1; char z[260]; scanf( %d , &n); getchar(); for (i = 0; i < n; i++) { scanf( %c , &z[i]); } getchar(); sum = 0; sum1 = 0; p = 0; t = 0; for (i = 0; i < n; i++) { if (z[i] >= a && z[i] <= z ) { sum++; if (sum >= t) t = sum; } if (z[i] >= A && z[i] <= Z ) { sum++; if (sum >= t) t = sum; } if (z[i] == _ ) { sum = 0; } if (z[i] == ( ) { sum = 0; i++; for (;; i++) { if (z[i] >= a && z[i] <= z ) { sum++; if (sum > p) p = sum; } if (z[i] >= A && z[i] <= Z ) { sum++; if (sum > p) p = sum; } if (z[i] == _ ) { p = sum; if (sum != 0) sum1++; sum = 0; } if (z[i] == ) ) { if (sum != 0) sum1++; sum = 0; break; } } } } printf( %d %d , t, sum1); return 0; }
#include <bits/stdc++.h> using namespace std; template <class A, class B> istream &operator>>(istream &o, pair<A, B> &p) { o >> p.first >> p.second; return o; } template <class type> istream &operator>>(istream &o, vector<type> &v) { for (int i = 0; i < (int)v.size(); i++) o >> v[i]; return o; } template <class A, class B> ostream &operator<<(ostream &o, pair<A, B> p) { o << ( << p.first << , << p.second << ) ; return o; } template <class type> ostream &operator<<(ostream &o, vector<type> v) { o << { ; for (int i = 0; i < (int)v.size(); i++) { if (i > 0) o << , ; o << v[i]; } o << } ; return o; } long long toNum(string a) { if (a.size() == 0u) return 0; long long ret = 0; for (int i = (a[0] == - ); i < int(a.size()); i++) ret = ret * 10 + a[i] - 0 ; return ret * (a[0] == - ? -1 : 1); } string toStr(long long a) { string ret = ; bool sgn = a < 0; a = a < 0 ? -a : a; do { ret.push_back(a % 10 + 0 ); a /= 10; } while (a > 0); if (sgn) ret.push_back( - ); reverse(ret.begin(), ret.end()); return ret; } int dp[1005][2][2]; vector<int> cost, a, b; int countCost(int a, int t) { switch (t) { case 0: return a * cost[t]; default: return cost[t]; } } int solve(int n, int bs, int tr) { if (n < 0) return 0; if (bs == 1 && tr == 1) return 0; int &ret = dp[n][bs][tr]; if (ret != -1) return ret; ret = (1 << 30); for (int i = 0; i < int(3); i++) for (int j = 0; j < int(3); j++) { int t = countCost(a[n] * (!bs), i) + countCost(b[n] * (!tr), j); ret = min(ret, t + solve(n - 1, (i == 2) || bs, (j == 2) || tr)); } return ret; } int main() { ios_base::sync_with_stdio(false); cost = vector<int>(4); cin >> cost; int m, n; cin >> m >> n; a = b = vector<int>(max(m, n)); for (int i = 0; i < int(m); i++) cin >> a[i]; for (int i = 0; i < int(n); i++) cin >> b[i]; n = max(m, n); memset(dp, -1, sizeof(dp)); cout << min(cost[3], solve(n - 1, 0, 0)); return 0; }
#include <bits/stdc++.h> using namespace std; long long a[100010], b[100010], x[100010], y[100010]; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, m, ans = 0; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; a[0] = 0; a[n + 1] = m; for (int i = 1; i <= n + 1; i++) { b[i] = a[i] - a[i - 1]; if (i & 1) ans += b[i]; x[i] = x[i - 1]; y[i] = y[i - 1]; if (i & 1) x[i] += b[i]; else y[i] += b[i]; } for (int i = 1; i <= n + 1; i++) { if (b[i] > 1) { ans = max(ans, x[i - 1] + y[n + 1] - y[i] + b[i] - 1); } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int S = 1003; bool a[S]; bool b[S]; string s; int k; int i, j; int main() { cin >> s; j = s.size() - 1; while (i < j) { if (s[i] == ( && s[j] == ) ) { b[i] = 1; b[j] = 1; k++; i++; j--; } else { if (s[i] == ) ) i++; if (s[j] == ( ) j--; } } if (k == 0) { cout << 0 ; return 0; }; cout << 1 << endl; cout << 2 * k << endl; for (int i = 0; i < s.size(); i++) if (b[i]) cout << i + 1 << ; return 0; }
#include <bits/stdc++.h> using namespace std; int N, M; string str[2005]; long long pr[2005][2005], pc[2005][2005]; long long dpr[2005][2005], dpd[2005][2005]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M; for (int i = 0; i <= N - 1; i++) cin >> str[i]; bool has = 0; for (int i = N - 1; i >= 0; i--) { for (int j = M - 1; j >= 0; j--) { pr[i][j] = (i == N - 1) ? 0 : pr[i + 1][j] + (str[i + 1][j] == R ); pc[i][j] = (j == M - 1) ? 0 : pc[i][j + 1] + (str[i][j + 1] == R ); if (str[i][j] == R ) has = 1; } } if (N == 1 || M == 1) { cout << (has ? 0 : 1) << n ; return 0; } long long ans = 0; for (int i = N - 1; i >= 0; i--) { for (int j = M - 1; j >= 0; j--) { if (i == N - 1) { if (pc[i][j] == 0) dpr[i][j] = 1; dpr[i][j] = (dpr[i][j] + dpr[i + 1][j]) % 1000000007; } else if (j == M - 1) { if (pr[i][j] == 0) dpd[i][j] = 1; dpd[i][j] = (dpd[i][j] + dpd[i][j + 1]) % 1000000007; } else { int c = pc[i][j]; dpr[i][j] = (dpd[i][j + 1] - dpd[i][M - c] + 1000000007) % 1000000007; c = pr[i][j]; dpd[i][j] = (dpr[i + 1][j] - dpr[N - c][j] + 1000000007) % 1000000007; if (i == 0 && j == 0) ans = (dpd[0][0] + dpr[0][0]) % 1000000007; dpr[i][j] = (dpr[i][j] + dpr[i + 1][j]) % 1000000007; dpd[i][j] = (dpd[i][j] + dpd[i][j + 1]) % 1000000007; } } } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > b, c; map<vector<pair<int, int> >, int> d; ; int n, m, ask, a[100086]; long long ans; int main() { scanf( %d %d , &n, &m); ans = 0ll; for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); b.clear(); c.clear(); int qs = a[i]; for (int j = 2; j * j <= qs; j++) { if (a[i] % j == 0) { int sum = 0; while (a[i] % j == 0) { a[i] /= j; sum++; } sum %= m; if (sum) { b.push_back(make_pair(j, sum)); c.push_back(make_pair(j, m - sum)); } } } if (a[i] > 1) { b.push_back(make_pair(a[i], 1)); c.push_back(make_pair(a[i], m - 1)); } ans = ans + 1ll * d[c]; d[b]++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long _pow(long long base, long long exp) { return exp == 0 ? 1 : base * _pow(base, exp - 1); } string int_to_str(int x) { string xs; stringstream ss; ss << x; xs = ss.str(); return xs; } int str_vec_find(string x, vector<string> y) { vector<string>::iterator it; it = find(y.begin(), y.end(), x); int pos = distance(y.begin(), it); return pos; } int main() { long long temp, tempa, tempb, maxa, maxb; vector<long long> a, b; int n; cin >> n; maxa = maxb = -1; for (int i = 0; i < n; i++) { cin >> tempa >> tempb; if (tempa >= maxa) maxa = tempa; if (tempb >= maxb) maxb = tempb; a.push_back(tempa); b.push_back(tempb); } for (int w = 0; w < b.size(); w++) { for (int r = 0; r < b.size() - 1; r++) { if (a[r] > a[r + 1]) { temp = a[r]; a[r] = a[r + 1]; a[r + 1] = temp; temp = b[r]; b[r] = b[r + 1]; b[r + 1] = temp; } } } if (n == 1) cout << maxb << endl; else { long long stamp = b[0]; for (int i = 1; i < n; i++) { if (b[i] >= stamp) stamp = b[i]; else { if (a[i] != a[i - 1]) stamp = a[i]; } } cout << stamp << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long nc2[(int)1e5]; void pre() { for (long long i = 1; i < 1e5; ++i) { nc2[i] = i * (i - 1) / 2; } } int binSearch(int l, int r, int val) { while (l <= r) { int mid = (l + r) / 2; if (nc2[mid] == val) return mid; else if (nc2[mid] < val) l = mid + 1; else r = mid - 1; } return -1; } bool isPossible(int a00, int a01, int a10, int a11, int n, int m) { if (a10 + a01 != n * m or abs(a10 - a01) % 2 != n * m % 2) { return false; } if (!n) { for (int i = 0; i < m; ++i) cout << 0; return true; } if (!m) { for (int i = 0; i < n; ++i) cout << 1; return true; } for (int i = 0; i < a01 / n; ++i) cout << 0; for (int i = 0; i < n - (a01 % n); ++i) cout << 1; if (a01 / n < m) cout << 0; for (int i = 0; i < a01 % n; ++i) cout << 1; for (int i = 0; i < (m - a01 / n - 1); ++i) cout << 0; return true; } int main() { pre(); int a00, a01, a10, a11; cin >> a00 >> a01 >> a10 >> a11; int n = binSearch(1, 1e5 - 1, a11), m = binSearch(1, 1e5 - 1, a00); if (n == -1 or m == -1 or (n == 0 and m == 0)) { cout << Impossible ; return 0; } if (!n) { for (int i = 0; i < m; ++i) cout << 0; return 0; } if (!m) { for (int i = 0; i < n; ++i) cout << 1; return 0; } bool ans = false; if (n == 1 and m == 1) { ans = ans or isPossible(a00, a01, a10, a11, n, m) or isPossible(a00, a01, a10, a11, n, 0) or isPossible(a00, a01, a10, a11, 0, m); if (!ans) cout << Impossible ; } else if (n == 1) { ans = ans or isPossible(a00, a01, a10, a11, 0, m) or isPossible(a00, a01, a10, a11, n, m); if (!ans) cout << Impossible ; } else if (m == 1) { ans = ans or isPossible(a00, a01, a10, a11, n, 0) or isPossible(a00, a01, a10, a11, n, m); if (!ans) cout << Impossible ; } else { ans = isPossible(a00, a01, a10, a11, n, m); if (!ans) cout << Impossible ; } }
#include <bits/stdc++.h> using namespace std; vector<long long> matches; void computeLPSArray(vector<pair<char, long long> >& pat, long long M, long long* lps); void KMPSearch(vector<pair<char, long long> >& pat, vector<pair<char, long long> >& txt) { long long M = pat.size(); long long N = txt.size(); long long* lps = (long long*)malloc(sizeof(long long) * M); long long j = 0; computeLPSArray(pat, M, lps); long long i = 0; while (i < N) { if (pat[j] == txt[i]) { j++; i++; } if (j == M) { matches.push_back(i - j); j = lps[j - 1]; } else if (i < N && pat[j] != txt[i]) { if (j != 0) j = lps[j - 1]; else i = i + 1; } } free(lps); } void computeLPSArray(vector<pair<char, long long> >& pat, long long M, long long* lps) { long long len = 0; long long i; lps[0] = 0; i = 1; while (i < M) { if (pat[i] == pat[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } } int main() { long long N, M; cin >> N >> M; bool s = false; if (N == 199999) s = true; vector<pair<char, long long> > txt(N), pat(M); for (long long i = 0; i < N; i++) scanf( %I64d-%c , &txt[i].second, &txt[i].first); for (long long i = 0; i < M; i++) scanf( %I64d-%c , &pat[i].second, &pat[i].first); vector<pair<char, long long> > txtM; char currc = txt[0].first; long long currn = txt[0].second; for (long long i = 1; i < txt.size(); i++) { if (txt[i].first == currc) currn += txt[i].second; else { txtM.push_back(make_pair(currc, currn)); currc = txt[i].first; currn = txt[i].second; } } txtM.push_back(make_pair(currc, currn)); vector<pair<char, long long> > patM; currc = pat[0].first; currn = pat[0].second; for (long long i = 1; i < pat.size(); i++) { if (pat[i].first == currc) currn += pat[i].second; else { patM.push_back(make_pair(currc, currn)); currc = pat[i].first; currn = pat[i].second; } } patM.push_back(make_pair(currc, currn)); long long cnt = 0; if (patM.size() == 1) { for (long long i = 0; i < txtM.size(); i++) { if (txtM[i].first == patM[0].first) cnt += max(0LL, txtM[i].second - patM[0].second + 1); } cout << cnt << endl; return 0; } else if (patM.size() == 2) { for (long long i = 0; i < txtM.size() - 1; i++) { if (txtM[i].first == patM[0].first && txtM[i + 1].first == patM[1].first) { if (txtM[i].second >= patM[0].second && txtM[i + 1].second >= patM[1].second) cnt += 1; } } cout << cnt << endl; return 0; } vector<pair<char, long long> > mod(patM.begin() + 1, patM.begin() + patM.size() - 1); KMPSearch(mod, txtM); for (long long i = 0; i < matches.size(); i++) { if (matches[i] - 1 >= 0 && txtM[matches[i] - 1].first == patM[0].first && txtM[matches[i] - 1].second >= patM[0].second) { long long fin = matches[i] + patM.size() - 2; long long le = patM.size() - 1; if (fin < txtM.size() && txtM[fin].first == patM[le].first && txtM[fin].second >= patM[le].second) { cnt += 1; } } } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> ostream& operator<<(ostream& os, vector<T> v) { for (T x : v) os << x << ; return os; } template <class T> istream& operator>>(istream& is, vector<T> v) { for (T& x : v) is >> x; return is; } const long long INF = 1LL << 60; bool startsWith(string s, string t) { return s.size() >= t.size() && s.substr(0, t.size()) == t; } string chomp(string s) { if (s.empty()) return ; long long start = -1, end = -1; for (long long i = 0; i < (long long)(s.size()); i++) { if (s[i] == ) continue; start = i; break; } for (long long i = s.size() - 1; i >= 0; i--) { if (s[i] == ) continue; end = i; break; } string ans = ; for (long long i = start; i <= end; i++) ans.push_back(s[i]); return ans; } string parseThrow(string s) { long long a, b; for (long long i = 0; i < (long long)(s.size()); i++) { char c = s[i]; if (c == ( ) a = i; else if (c == ) ) b = i; } return chomp(s.substr(a + 1, b - a - 1)); } pair<string, string> parseCatch(string s) { string a, b; long long x, y = -1, z, w; for (long long i = 0; i < (long long)(s.size()); i++) { char c = s[i]; if (c == ( ) { x = i; } else if (c == ) { if (y == -1) y = i; else z = i; } else if (c == , ) w = i; } a = chomp(s.substr(x + 1, w - x - 1)); b = chomp(s.substr(y + 1, z - y - 1)); return make_pair(a, b); } void run() { long long n; cin >> n; cin.ignore(); vector<string> v; string buf; for (long long i = 0; i < (long long)(n); i++) { getline(cin, buf); v.push_back(chomp(buf)); } string exn; bool flg = false; long long depth = 0; for (long long i = 0; i < (long long)(n); i++) { string s = v[i]; if (s.empty()) continue; if (startsWith(s, throw )) { flg = true; exn = parseThrow(s); } if (!flg) continue; if (startsWith(s, try )) { depth++; } else if (startsWith(s, catch )) { if (depth > 0) { depth--; continue; } else { pair<string, string> e = parseCatch(s); if (e.first == exn) { cout << e.second << endl; return; } } } } cout << Unhandled Exception << endl; } int main() { run(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 + 5; const int mod = 1000 * 1000 * 1000 + 7; vector<int> p[maxn]; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) { int k; cin >> k; p[i].resize(k); for (int j = 0; j < k; j++) cin >> p[i][j]; sort(p[i].begin(), p[i].end()); } int ans = 0; for (int i = 0; i < n; i++) { int pre = i ? i - 1 : n - 1; int nxt = (i + 1) % n; int p1 = 0, p2 = 0; for (int j = 0; j < (int)p[i].size(); j++) { int cnt = 0; while (p1 < (int)p[pre].size() && p[pre][p1] < p[i][j]) cnt++, p1++; while (p2 < (int)p[nxt].size() && p[nxt][p2] < p[i][j]) cnt--, p2++; if (cnt != 0 && j != 0) ans++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) using namespace std; int t; long long n; vector<int> v; long long cnt[30]; long long pos[30]; long long fact[21]; void init() { for (int i = 0; i < int(30); i++) cnt[i] = pos[i] = 0; v.clear(); } int main() { cin.tie(0); ios_base::sync_with_stdio(false); fact[0] = 1; for (int i = 1; i <= 20; i++) fact[i] = fact[i - 1] * i; cin >> t; while (t--) { init(); long long n; cin >> n; int cur = 2; while (n) { v.push_back(n % cur); n /= cur; cur++; } sort(v.begin(), v.end()); for (int i = 0; i < int(v.size()); i++) cnt[v[i]]++; pos[1] = cnt[0]; for (int i = 2; i <= v.size() + 1; i++) pos[i] = pos[i - 1] + cnt[i - 1]; long long A = 1; for (int i = 2; i <= v.size() + 1; i++) A *= (pos[i] - (i - 2)); long long B = 1; if (cnt[0] == 0) B = 0; else { B = cnt[0]; for (int i = 2; i <= v.size(); i++) B *= (pos[i] - (i - 1)); } long long ans = A - B; for (int i = 0; i < int(21); i++) ans /= fact[cnt[i]]; cout << ans - 1 << n ; } }
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, m, K, w[N]; inline char nc() { static char buf[100000], *p1 = buf, *p2 = buf; if (p1 == p2) { p2 = (p1 = buf) + fread(buf, 1, 100000, stdin); if (p1 == p2) return EOF; } return *p1++; } inline void read(int& x) { char tr = nc(), b = 1; for (; tr < 0 || tr > 9 ; tr = nc()) if (tr == - ) b = -1; for (x = 0; tr >= 0 && tr <= 9 ; x = (x << 3) + (x << 1) + tr - 48, tr = nc()) ; x *= b; } inline int count() { int ret = 0, x; for (int i = 0; i < n - 1; i++) for (int j = 0; j < m - 1; j++) x = i * m + j, ret += (w[x] & w[x + m] & w[x + 1]) + (w[x + m + 1] & w[x + m] & w[x + 1]) + (w[x] & w[x + m] & w[x + m + 1]) + (w[x] & w[x + 1] & w[x + m + 1]); return ret; } int Q[N], l, r, ins[N]; const int dx[6] = {0, 0, 1, -1}; const int dy[6] = {1, -1, 0, 0}; inline int pd() { l = r = -1; for (int i = 0; i < n * m; i++) ins[i] = 0; for (int i = 0; i < n * m; i++) if (w[i]) { Q[++r] = i; break; } while (l < r) { ++l; int x = Q[l] / m, y = Q[l] % m; for (int k = 0; k < 4; k++) { int sx = x + dx[k], sy = y + dy[k]; if (sx < 0 || sy < 0 || sx >= n || sy >= m || ins[sx * m + sy] || !w[sx * m + sy]) continue; Q[++r] = sx * m + sy; ins[sx * m + sy] = 1; } } for (int i = 0; i < n * m; i++) if (w[i] && !ins[i]) return 0; return 1; } inline int check(int mid) { for (int i = 0; i < n * m; i++) w[i] = (i <= mid); return count(); } inline int Solve() { int C = 4 * (n - 1) * (m - 1), L = -1, R = n * m, MID; if (K > C) return 0; if (n == 3 || m == 3) { if (K == C - 1 || K == C - 2 || K == C - 4 || K == C - 5) return 0; } else { if (K == C - 1 || K == C - 2 || K == C - 4 || K == C - 5 || K == C - 8) return 0; } while (L + 1 < R) if (check(MID = (L + R) >> 1) >= K) R = MID; else L = MID; int cur = check(R); if (cur == K) return 1; vector<int> bl, em; for (int i = 0; i < n * m; i++) if (min(i / m, abs(i / m - R / m)) <= 1) w[i] ? bl.push_back(i) : em.push_back(i); int x, p; while (cur != K) { if (cur < K) { x = em[p = rand() % ((int)em.size())]; w[x] = 1; if (!pd()) { w[x] = 0; continue; } em.erase(em.begin() + p); bl.push_back(x); cur = count(); } else { x = bl[p = rand() % ((int)bl.size())]; w[x] = 0; if (!pd()) { w[x] = 1; continue; } bl.erase(bl.begin() + p); em.push_back(x); cur = count(); } } return 1; } int main() { int T; read(T); while (T--) { read(n); read(m); read(K); int flag = 0; if (n < m) swap(n, m), flag = 1; if (Solve()) { if (!flag) { for (int i = 0; i < n; i++, putchar( n )) for (int j = 0; j < m; j++) putchar(w[i * m + j] ? * : . ); } else { for (int j = 0; j < m; j++, putchar( n )) for (int i = 0; i < n; i++) putchar(w[i * m + j] ? * : . ); } putchar( n ); } else printf( -1 n n ); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[12345]; int calc(int x, int y) { int ret = 0; vector<int> v; for (int i = 0; i < 2 * n; ++i) { if (i != x && i != y) v.push_back(a[i]); } for (int i = 0; i < v.size(); i += 2) ret += abs(v[i] - v[i + 1]); return ret; } int main() { cin >> n; int mn = 1e9, i; for (i = 0; i < 2 * n; i++) cin >> a[i]; sort(a, a + 2 * n); for (int i = 0; i < n * 2; i++) { for (int j = i + 1; j < n * 2; j++) { mn = min(mn, calc(i, j)); } } cout << mn; }
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <vector> #include <map> #include <set> #include <cmath> #include <queue> #include <algorithm> #include <numeric> using namespace std; typedef long long ll; const int N=1e6+5; const int M=1e3+5; const int mod=1e9+7; int n,m; int T,a[N]; std::vector<int> ans; int rns; bool cal(vector<int> v,std::vector<int> v1){ for(int i=0;i<n*2;i++){ if(v[i]!=v1[i])return 0; } return 1; } map<std::vector<int>,int>mp; void bfs(){ queue<vector<int> >q; std::vector<int>G; for(int i=1;i<=n*2;i++)G.push_back(i); rns=-1; q.push(G); mp[G]=0; while(!q.empty()){ std::vector<int> now=q.front(); q.pop(); if(cal(now,ans)==1){ rns=mp[now]; return ; } std::vector<int> v1=now; for(int i=0;i+1<n*2;i+=2){ swap(v1[i],v1[i+1]); } std::vector<int> v2=now; for(int i=0;i+n<n*2;i++){ swap(v2[i],v2[i+n]); } if(!mp.count(v1)){ q.push(v1); mp[v1]=mp[now]+1; } if(!mp.count(v2)){ q.push(v2); mp[v2]=mp[now]+1; } } } int main() { scanf( %d ,&n); for(int i=1;i<=n*2;i++)scanf( %d ,&a[i]),ans.push_back(a[i]); bfs(); cout<<rns<<endl; }
#include <bits/stdc++.h> using namespace std; int main() { int t, c, i, a, m, l, r; cin >> t >> i >> m >> a >> l; r = i; c = 1; t = t - r; while (t > 0) { c++; r = r + a; if (r > m) r = m; t = t + l - r; if (t < 0) t = 0; } cout << c; return 0; }
#include <bits/stdc++.h> using namespace std; int q, n, m, u1, d1, l1, r1, u2, d2, l2, r2, u3, d3, l3, r3; long long count(int a, int b) { return (1ll * a * b + 1) / 2; } long long count(int u, int d, int l, int r) { return count(d, r) + count(u - 1, l - 1) - count(d, l - 1) - count(u - 1, r); } int main() { scanf( %d , &q); while (q--) { scanf( %d %d , &m, &n); scanf( %d %d %d %d , &u1, &l1, &d1, &r1); scanf( %d %d %d %d , &u2, &l2, &d2, &r2); long long ans = count(n, m); u3 = max(u1, u2), l3 = max(l1, l2); d3 = min(d1, d2), r3 = min(r1, r2); ans -= count(u2, d2, l2, r2); ans += 1ll * (d1 - u1 + 1) * (r1 - l1 + 1) - count(u1, d1, l1, r1); if (d3 >= u3 && r3 >= l3) ans -= 1ll * (d3 - u3 + 1) * (r3 - l3 + 1) - count(u3, d3, l3, r3); printf( %I64d %I64d n , ans, 1ll * n * m - ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; string in[1003]; int a[1003][1003]; int X[] = {-1, 0, 0, 1}; int Y[] = {0, -1, 1, 0}; bool d[1003][1003]; int getSm(int i, int j, int u, int v) { return a[u][v] - a[i - 1][v] - a[u][j - 1] + a[i - 1][j - 1]; } bool check() { for (int i = 1; i <= n; i++) { bool ok = 0; int cnt = 0; for (int j = 1; j <= m; j++) { if (in[i][j] == # ) { if (!ok) { cnt++; ok = 1; } } else { ok = 0; } } if (cnt > 1) { return 0; } if (cnt == 0) { ok = 0; for (int j = 1; j <= m; j++) { if (getSm(1, j, n, j) == 0) { ok = 1; continue; } } if (!ok) { return 0; } } } for (int j = 1; j <= m; j++) { bool ok = 0; int cnt = 0; for (int i = 1; i <= n; i++) { if (in[i][j] == # ) { if (!ok) { cnt++; ok = 1; } } else { ok = 0; } } if (cnt > 1) { return 0; } if (cnt == 0) { ok = 0; for (int i = 1; i <= n; i++) { if (getSm(i, 1, i, m) == 0) { ok = 1; continue; } } if (!ok) { return 0; } } } return 1; } bool canC(int u, int v) { return (1 <= u) && (u <= n) && (1 <= v) && (v <= m) && (in[u][v] == # ) && (d[u][v] == 0); } void DFS(int u, int v) { d[u][v] = 1; for (int k = 0; k < 4; k++) { int nu = u + X[k]; int nv = v + Y[k]; if (canC(nu, nv)) { DFS(nu, nv); } } } int main() { ios::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> in[i]; in[i].insert(0, ); for (int j = 1; j <= m; j++) a[i][j] = (in[i][j] == # ) + a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; } if (a[n][m] == 0) { cout << 0; return 0; } if (n == 1) { int cnt = 0; for (int j = 1; j <= m; j++) { cnt += (in[1][j] == # ); } if ((0 < cnt) && (cnt < m)) { cout << -1; return 0; } } if (m == 1) { int cnt = 0; for (int i = 1; i <= n; i++) { cnt += (in[i][1] == # ); } if ((0 < cnt) && (cnt < n)) { cout << -1; return 0; } } if (!check()) { cout << -1; return 0; } memset(d, 0, sizeof d); int res = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if ((in[i][j] == # ) && (d[i][j] == 0)) { res++; DFS(i, j); } } cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 3e3 + 5; const int mod = 998244353; char s[maxn], t[maxn]; int m, n, dp[maxn][maxn]; int main() { scanf( %s%s , s + 1, t + 1); n = strlen(s + 1); m = strlen(t + 1); for (int i = 1; i <= n; i++) { if (i > m || t[i] == s[1]) dp[i][i] = 2; } for (int len = 2; len <= n; len++) { char ch = s[len]; for (int l = 1; l + len - 1 <= n; l++) { int r = l + len - 1; if (l > m || ch == t[l]) dp[l][r] = (dp[l][r] + dp[l + 1][r]) % mod; if (r > m || ch == t[r]) dp[l][r] = (dp[l][r] + dp[l][r - 1]) % mod; } } int ans = 0; for (int i = m; i <= n; i++) ans = (ans + dp[1][i]) % mod; cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << : << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, , ); cerr.write(names, comma - names) << : << arg1 << | ; __f(comma + 1, args...); } const int N = 1e5 + 5; vector<pair<int, int> > aa, bb; bool valid(pair<int, int> p1, pair<int, int> p2) { if (p1.first == p2.first || p1.second == p2.second || p1.first == 0 || p1.second == 0 || p2.first == 0 || p2.second == 0) return 0; if (p1.first * p2.first + p1.second * p2.second != 0) return 0; return 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int a, b; cin >> a >> b; for (int i = 1; i <= a; i++) { for (int j = 1; j <= a; j++) { if (i * i + j * j == a * a) aa.push_back({i, j}); } } for (int i = 1; i <= b; i++) { for (int j = 1; j <= b; j++) { if (i * i + j * j == b * b) bb.push_back({-i, j}); } } for (auto p1 : aa) { for (auto p2 : bb) { if (valid(p1, p2)) { cout << YES n ; cout << 0 0 n ; cout << p1.first << << p1.second << n ; cout << p2.first << << p2.second << n ; return 0; } } } cout << NO n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int k = 0, t = 0, l = 0, q = 0, z = 0; string s, s1, s2; cin >> s; if (s[0] == - ) { q = -1; s.erase(0, 1); } k = s.size(); for (int i = 0; i < s.size(); i++) if (s[i] == . ) { t = i; break; } if (t == 0) s += .00 , t = k; if (s.size() - t == 2) s += 0 ; l = t; if (q == -1) cout << ( ; cout << $ ; if (l % 3 != 0) z = l % 3; else z = 3; for (int i = 0; i < l + 3; i++) { if (i == z && z < l) { cout << , ; z += 3; } if (s[i] != - ) cout << s[i]; } if (q == -1) cout << ) ; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; long long a3, b3; bool check1(long long a1, long long b1, long long a2, long long b2) { if ((a2 * a3 + b2 * b3 + a1 * b3 - a3 * b1) % (a3 * a3 + b3 * b3)) return false; if ((a2 * b3 - b2 * a3 - a1 * a3 - b1 * b3) % (a3 * a3 + b3 * b3)) return false; return true; } bool check2(long long a1, long long b1, long long a2, long long b2) { if ((a2 * a3 + b2 * b3 - a1 * a3 - b1 * b3) % (a3 * a3 + b3 * b3)) return false; if ((a1 * b3 - b1 * a3 - a2 * b3 + b2 * a3) % (a3 * a3 + b3 * b3)) return false; return true; } bool check(long long a1, long long b1, long long a2, long long b2) { if (a1 == a2 && b1 == b2) return true; if (b1 == a2 && -a1 == b2) return true; if (-a1 == a2 && -b1 == b2) return true; if (-b1 == a2 && a1 == b2) return true; return false; } int main() { long long a1, a2, b1, b2; while (~scanf( %lld%lld%lld%lld%lld%lld , &a1, &b1, &a2, &b2, &a3, &b3)) { if (a3 == b3 && a3 == 0) { puts(check(a1, b1, a2, b2) ? YES : NO ); } else { int ok = 0; ok += check(a1, b1, a2, b2); ok += check1(a1, b1, a2, b2); ok += check1(a1, b1, b2, -a2); ok += check1(a1, b1, -a2, -b2); ok += check1(a1, b1, -b2, a2); ok += check2(a1, b1, a2, b2); ok += check2(a1, b1, b2, -a2); ok += check2(a1, b1, -a2, -b2); ok += check2(a1, b1, -b2, a2); puts(ok ? YES : NO ); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; template <bool directed> struct Euler { int N; vector<vector<pair<int, int>>> adj; vector<vector<pair<int, int>>::iterator> its; vector<bool> used; Euler(int N_) : N(N_), adj(N), its(N) {} void ae(int a, int b) { int M = used.size(); used.push_back(0); adj[a].emplace_back(b, M); if (!directed) adj[b].emplace_back(a, M); } vector<pair<int, int>> solve(int src = 0) { while (!adj[src].size()) src++; for (int i = 0; i < N; i++) its[i] = begin(adj[i]); vector<pair<pair<int, int>, int>> ret, second = {{{src, -1}, -1}}; while (second.size()) { int x = second.back().first.first; auto &it = its[x], en = end(adj[x]); while (it != en && used[it->second]) it++; if (it == en) { if (ret.size() && ret.back().first.second != x) return {}; ret.push_back(second.back()), second.pop_back(); } else { second.push_back({{it->first, x}, it->second}); used[it->second] = 1; } } if (ret.size() != used.size() + 1) return {}; vector<pair<int, int>> ans; for (auto& t : ret) ans.push_back({t.first.first, t.second}); reverse(ans.begin(), ans.end()); return ans; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int N; cin >> N; vector<pair<int, int>> v(N); for (auto& i : v) cin >> i.first >> i.second; for (int i = 20; i >= 0; i--) { Euler<false> E(1 << i); for (auto [l, r] : v) E.ae(l & ((1 << i) - 1), r & ((1 << i) - 1)); auto ans = E.solve(); if (!ans.size()) continue; if (ans[0].first != ans.back().first) continue; ans.erase(ans.begin()); cout << i << n ; for (auto [j, id] : ans) { int a = 2 * id + 1, b = 2 * id + 2; if ((v[id].first & ((1 << i) - 1)) == j) swap(a, b); cout << a << << b << ; } cout << n ; break; } exit(0); }
#include <bits/stdc++.h> using namespace std; const int mxN = 1e3 + 1; int n, d[mxN], k; vector<int> adj[mxN]; vector<string> a[mxN]; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; for (int i = 0, c; i < n; i++) { cin >> c; string s1; for (int j = 0; j < k; j++) { cin >> s1; a[c].push_back(s1); } } vector<string> s; for (int i = 0; i < n; i++) for (int j = 0; j < k; j++) s.push_back(a[i][j]); n *= k; vector<bool> ok(26); int al = 0; for (string x : s) for (char c : x) { if (!ok[c - a ]) ++al, ok[c - a ] = 1; } for (int i = 1; i < n; i++) { int n1 = s[i - 1].size(), n2 = s[i].size(), i1 = 0; for (; i1 < n1 || i1 < n2; i1++) { if (i1 >= n2 && i1 < n1) { cout << IMPOSSIBLE ; return 0; } else if (i1 >= n1 || i1 >= n2) break; if (s[i - 1][i1] == s[i][i1]) continue; adj[s[i - 1][i1] - a ].push_back(s[i][i1] - a ); d[s[i][i1] - a ]++; break; } } queue<int> qu; string ans; for (int i = 0; i < 26; i++) if (!d[i] && ok[i]) qu.push(i); while (qu.size()) { int u = qu.front(); qu.pop(); for (int x : adj[u]) { --d[x]; if (!d[x]) qu.push(x); } ans += (u + a ); } cout << (ans.size() == al ? ans : IMPOSSIBLE ); }
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } long long bit[400001]; void add(int x, int v) { for (int i = x; i <= 400000; i += i & -i) bit[i] += v; } long long query(int x) { long long ret = 0; for (int i = x; i > 0; i -= i & -i) ret += bit[i]; return ret; } long long ar[100001][4]; map<long long, long long> mx, my; vector<pair<long long, long long> > horizontal[400001], vertical[400001]; vector<int> in[400001], out[400001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = (0); i < (n); ++i) { for (int j = (0); j < (4); ++j) cin >> ar[i][j]; mx[ar[i][0]] = mx[ar[i][2]] = my[ar[i][1]] = my[ar[i][3]] = 1; if (ar[i][0] > ar[i][2]) swap(ar[i][0], ar[i][2]); if (ar[i][1] > ar[i][3]) swap(ar[i][1], ar[i][3]); } int c1 = 0, c2 = 0; for (auto &v : mx) { v.second = ++c1; } for (auto &v : my) { v.second = ++c2; } for (int i = (0); i < (n); ++i) { if (ar[i][0] == ar[i][2]) horizontal[mx[ar[i][0]]].push_back(make_pair(ar[i][1], ar[i][3])); else vertical[my[ar[i][1]]].push_back(make_pair(ar[i][0], ar[i][2])); } long long res = 0; for (int i = 1; i <= c1; i++) { sort(horizontal[i].begin(), horizontal[i].end()); if (horizontal[i].size() == 0) continue; int last = 0; for (int j = 1; j < horizontal[i].size(); j++) { if (horizontal[i][j].first <= horizontal[i][last].second) { horizontal[i][last].second = max(horizontal[i][last].second, horizontal[i][j].second); } else { res += horizontal[i][last].second - horizontal[i][last].first + 1; in[my[horizontal[i][last].first]].push_back(i); out[my[horizontal[i][last].second]].push_back(i); last = j; } } res += horizontal[i][last].second - horizontal[i][last].first + 1; in[my[horizontal[i][last].first]].push_back(i); out[my[horizontal[i][last].second]].push_back(i); } for (int i = 1; i <= c2; i++) { sort(vertical[i].begin(), vertical[i].end()); for (int j = 0; j < in[i].size(); j++) { add(in[i][j], 1); } if (vertical[i].size() == 0) { for (int j = 0; j < out[i].size(); j++) { add(out[i][j], -1); } continue; } int last = 0; for (int j = 1; j < vertical[i].size(); j++) { if (vertical[i][j].first <= vertical[i][last].second) { vertical[i][last].second = max(vertical[i][last].second, vertical[i][j].second); } else { res += vertical[i][last].second - vertical[i][last].first + 1; res -= query(mx[vertical[i][last].second]) - query(mx[vertical[i][last].first] - 1); last = j; } } res += vertical[i][last].second - vertical[i][last].first + 1; res -= query(mx[vertical[i][last].second]) - query(mx[vertical[i][last].first] - 1); for (int j = 0; j < out[i].size(); j++) { add(out[i][j], -1); } } cout << res << endl; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) using namespace std; clock_t clk = clock(); long long int i, j, k; void solve(void); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int t = 1; while (t--) solve(); return 0; } void solve() { long long int n, m; cin >> n >> m; bool rev = 0; for (i = 1; i <= (n + 1) / 2; ++i) { for (j = 1; j <= m; ++j) if (!rev) { if (n + 1 != 2 * i) { cout << i << << j << n ; cout << n + 1 - i << << m + 1 - j << n ; } else if (j & 1) cout << i << << (j - 1) / 2 + 1 << n ; else cout << i << << m - j / 2 + 1 << n ; } else { if (n + 1 != 2 * i) { cout << i << << m + 1 - j << n ; cout << n + 1 - i << << j << n ; } else if (j & 1) cout << i << << m - (j - 1) / 2 << n ; else cout << i << << j / 2 << n ; } rev ^= 1; } }
#include <bits/stdc++.h> using namespace std; int main() { vector<set<string> > N, F; vector<pair<string, string> > ans; set<string> p; for (int i = 0; i < 26; ++i) { N.push_back(p); F.push_back(p); } int n, k, c; cin >> n; c = (int) A ; string s; for (int i = 0; i < n; ++i) { cin >> s; k = (int)s[0]; N[k - c].insert(s); } for (int i = 0; i < n; ++i) { cin >> s; k = (int)s[0]; F[k - c].insert(s); } queue<string> qN, qF; set<string>::iterator el, el1; for (int i = 0; i < 26; ++i) { if (N[i].size() <= F[i].size()) { while (N[i].size() < F[i].size() && !qN.empty()) { el = F[i].begin(); ans.push_back(make_pair(qN.front(), *el)); F[i].erase(el); qN.pop(); } while (!N[i].empty()) { el = F[i].begin(); el1 = N[i].begin(); ans.push_back(make_pair(*el1, *el)); F[i].erase(el); N[i].erase(el1); } while (!F[i].empty()) { el = F[i].begin(); qF.push(*el); F[i].erase(el); } } else { while (F[i].size() < N[i].size() && !qF.empty()) { el = N[i].begin(); ans.push_back(make_pair(*el, qF.front())); N[i].erase(el); qF.pop(); } while (!F[i].empty()) { el = F[i].begin(); el1 = N[i].begin(); ans.push_back(make_pair(*el1, *el)); F[i].erase(el); N[i].erase(el1); } while (!N[i].empty()) { el = N[i].begin(); qN.push(*el); N[i].erase(el); } } } sort(ans.begin(), ans.end()); int i; for (i = 0; i < ans.size() - 1; ++i) cout << ans[i].first << << ans[i].second << , ; cout << ans[i].first << << ans[i].second; return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long double PI = 4 * atan((long double)1); const long long INF = 1e18; const long long NINF = -1e18; long long get_hash(string s) { long long N = 1000001; long long base[N], A = 11, MD = 1110111110111; base[0] = 1; for (long long i = (1); i < (N); ++i) base[i] = (base[i - 1] * A) % MD; long long hs = 0; for (long long i = (0); i < (s.size()); ++i) { hs += (s[i] * base[i]); hs %= MD; } return hs; } long long power(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res *= a; a *= a; n /= 2; } return res; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long k = 1, m = 2; while (k != n + 1) { long long p = (k + 1) * k; long long u = k + 1; long long v = (k + 1) * (k + 1) * k; cout << v - (m) / k << n ; m = p; k++; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 505; char g[N][N]; int id[N][N]; int cnt = 1; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int amt[N * N]; int sz[N * N]; int sum; int n, k; void dfs(int i, int j, int idx) { if (g[i][j] == X or id[i][j]) return; id[i][j] = idx; sz[idx]++; for (int k = 0; k < 4; k++) dfs(i + dx[k], j + dy[k], idx); } void add(int c, int l, int r, bool row, bool in, bool rem = false) { for (int i = l; i <= r; i++) { int idx; if (row) idx = id[c][i]; else idx = id[i][c]; if (idx == 0) { if (in) sum = sum + !rem - rem; } else { amt[idx] = amt[idx] + !rem - rem; if (amt[idx] == 1 and !rem) sum += sz[idx]; if (amt[idx] == 0 and rem) sum -= sz[idx]; } } } void addEnv(int i, int j, bool rem = false) { add(i - 1, j, j + k - 1, true, false, rem); add(i + k, j, j + k - 1, true, false, rem); add(j - 1, i, i + k - 1, false, false, rem); add(j + k, i, i + k - 1, false, false, rem); } int main() { cin >> n >> k; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> g[i][j]; for (int i = 0; i <= n + 1; i++) g[i][0] = X , g[i][n + 1] = X , g[0][i] = X , g[n + 1][i] = X ; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (!id[i][j] and g[i][j] != X ) dfs(i, j, cnt++); add(0, 1, k, true, false, false); add(k + 1, 1, k, true, false, false); add(0, 1, k, false, false, false); add(k + 1, 1, k, false, false, false); for (int i = 1; i <= k; i++) add(i, 1, k, true, true, false); int ans = sum; int dir = 1; for (int i = 1; i <= n - k + 1;) { int lim = n - k + 1, bg = 1; int j; if (dir == -1) swap(lim, bg); for (j = bg; j != lim;) { addEnv(i, j, true); if (dir == 1) add(j, i, i + k - 1, false, true, true), add(j + k, i, i + k - 1, false, true, false); else add(j + k - 1, i, i + k - 1, false, true, true), add(j - 1, i, i + k - 1, false, true, false); j += dir; addEnv(i, j, false); ans = max(ans, sum); } if (i == n - k + 1) break; addEnv(i, j, true); add(i, j, j + k - 1, true, true, true), add(i + k, j, j + k - 1, true, true, false); i++; dir = -dir; addEnv(i, j, false); ans = max(ans, sum); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; struct pair_hash { template <class T1, class T2> size_t operator()(pair<T1, T2> const &pair) const { size_t h1 = std::hash<T1>()(pair.first); size_t h2 = std::hash<T2>()(pair.second); return h1 ^ h2; } }; unordered_set<pair<int, int>, pair_hash> segmentos; int main() { int n, m; scanf( %d %d , &n, &m); vector<pair<int, int> > a(m); for (int i = 0; i < m; i++) { cin >> a[i].first >> a[i].second; segmentos.insert({a[i].first, a[i].second}); } for (int i = 1; i < n; i++) { if (n % i) continue; bool solved = true; for (int j = 0; j < m && solved; j++) { int optA = a[j].first; int optB = a[j].second; optA = (a[j].first + i > n ? 1 : 0) + (a[j].first + i) % (n + 1); optB = (a[j].second + i > n ? 1 : 0) + (a[j].second + i) % (n + 1); if (segmentos.find({optA, optB}) == segmentos.end() && segmentos.find({optB, optA}) == segmentos.end()) { solved = false; } } if (solved) { printf( Yes ); exit(0); } } printf( No ); return 0; }
#include <bits/stdc++.h> using namespace std; int p, first, cnts, cntw, S, W; int solve(long long picked) { int cap1 = p, cap2 = first, cnt1 = cnts, cnt2 = cntw; long long temp = picked; cap1 -= (picked * S); cnt1 -= picked; if (cap1 >= W) { temp += min(cap1 / W, cnt2); cnt2 -= min(cap1 / W, cnt2); } if (S <= W) { temp += min(cap2 / S, cnt1); cap2 -= (min(cap2 / S, cnt1) * S); temp += min(cap2 / W, cnt2); } else { temp += min(cap2 / W, cnt2); cap2 -= (min(cap2 / W, cnt2) * W); temp += min(cap2 / S, cnt1); } return temp; } int main() { int t; scanf( %d , &t); while (t--) { scanf( %d %d , &p, &first); scanf( %d %d , &cnts, &cntw); scanf( %d %d , &S, &W); long long ans = 0, temp; for (int pickedS = 0; pickedS <= min(cnts, p / S); pickedS++) { temp = solve(pickedS); ans = max(ans, temp); } printf( %lld n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long a, b, x, y, i, k, j, t, n, s; cin >> t; while (t--) { cin >> n >> k; if (n % 2 == 0) { s = n + (k * 2); cout << s << endl; } else { s = (k - 1) * 2; a = 0; for (i = 3; i * i <= n; i += 2) { if (n % i == 0) { a = i; break; } } if (a == 0) a = n; s = a + n + s; cout << s << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long p, k; cin >> p >> k; long long d = (k - p) / k; if ((((k - p) % k) + k) % k == 0 || (d <= 0 && p > k)) d--; vector<long long> res; res.push_back(d); if (k > p) return cout << 1 n << p, 0; while (1) { long long r = res.back(); d = (k - r) / k; if ((((k - r) % k) + k) % k == 0 || d <= 0) d--; res.push_back(d); if (d < k && d >= 0) break; } cout << res.size() + 1 << endl; long long last = p; for (int i = 0; i < res.size(); i++) { cout << k * res[i] + last << ; last = res[i]; } cout << d; return 0; }