func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; void prepare() {} const int maxn = 100005; int n; int a[maxn], b[maxn]; vector<int> res; int q[maxn]; void go(int x) { int l = 0, r = 0; long long cur = a[0]; while (true) { cur -= b[r]; (++r) %= n; while (cur < 0) { ((--l) += n) %= n; cur -= b[l]; cur += a[l]; if (l == r) break; } if (l != r) cur += a[r]; else break; } assert(cur == 0); int s = l; do { ((--l) += n) %= n; cur -= b[l]; cur += a[l]; if (cur >= 0) res.push_back(x ? q[l] : l); } while (l != s); } bool solve() { int i; scanf( %d , &n); for (i = (0); i < (n); i++) scanf( %d , &a[i]); for (i = (0); i < (n); i++) scanf( %d , &b[i]); for (i = (0); i < (n); i++) q[i] = n - i - 1; go(0); reverse(a, a + n); reverse(b, b + n - 1); go(1); sort(res.begin(), res.end()); res.resize(unique(res.begin(), res.end()) - res.begin()); printf( %d n , (int)(res).size()); for (i = (0); i < ((int)(res).size()); i++) printf( %d , res[i] + 1); return false; } int main() { prepare(); while (solve()) ; return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int imax = 2147483647; const int imin = -2147483647; const long long lmax = 9223372036854775807; const long long lmin = -9223372036854775807; long long power(long long a, long long b, long long m) { long long p = 1; while (b) { if (b & 1) p = (p * a) % m; b >>= 1; a = (a * a) % m; } return (p % m); } int main() { int n; cin >> n; int arr[100005]; int distinct[100005] = {0}, mp[100005] = {0}; for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = n - 1; i >= 0; i--) { if (i == n - 1) { mp[arr[i]] = 1; distinct[i] = 1; } else { if (mp[arr[i]] == 0) { mp[arr[i]] = 1; distinct[i] = 1 + distinct[i + 1]; } else distinct[i] = distinct[i + 1]; } } memset(mp, 0, sizeof(mp)); long long ans = 0; for (int i = 0; i < n - 1; i++) { if (mp[arr[i]] == 0) { mp[arr[i]] = 1; ans += (long long)distinct[i + 1]; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int socks, day; int main() { cin.sync_with_stdio(0); cin >> socks >> day; int i = 0; while (socks) { i++; if (!(i % day)) socks++; socks--; } cout << i; }
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } void solve(int test) { int n, k; cin >> n >> k; vector<pair<pair<int, int>, int>> v(n); int i, j, l; for (i = (0); i < (n); ++i) cin >> v[i].first.first >> v[i].first.second; for (i = (0); i < (n); ++i) if (v[i].first.first == 0 && v[i].first.second == 0) break; if (i == n) throw 0; if (i != 0) swap(v[0], v[i]); for (i = (1); i < (n); ++i) { v[i].second = gcd(abs(v[i].first.first), abs(v[i].first.second)); v[i].first.first /= v[i].second; v[i].first.second /= v[i].second; } sort(v.begin() + 1, v.end()); int prev = 1; vector<double> all(1, 0.0), neg; for (i = (2); i < (n + 1); ++i) if (i == n || v[prev].first != v[i].first) { long long x = v[prev].first.first; long long y = v[prev].first.second; double len = sqrt(x * x + y * y + 0.0); vector<double> w; for (j = (prev); j < (i); ++j) w.push_back(len * v[j].second); reverse((w).begin(), (w).end()); int mid = min((k + 1) / 2, int((w).size())); for (j = (0); j < (mid); ++j) all.push_back(w[j] * (k - 1 - 2 * j)); for (j = (mid); j < (int((w).size())); ++j) neg.push_back(w[j]); prev = i; } sort((all).begin(), (all).end()); sort((neg).begin(), (neg).end()); if (int((all).size()) < k) { int cnt = k - int((all).size()); int mid = (k + 1) / 2; for (i = (0); i < (cnt); ++i) all.push_back(neg[i] * (k - 1 - 2 * (mid + cnt - 1 - i))); sort((all).begin(), (all).end()); } reverse((all).begin(), (all).end()); double res = 0; for (i = (0); i < (k); ++i) res += all[i]; cout.precision(11); cout << res << endl; } int main() { int T = 1, t; for (t = (0); t < (T); ++t) solve(t); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 50, M = 55; int B, bl[N]; vector<int> bcc[N]; inline void update(int &a, int b) { a = max(a, b); } int n, m, d; int dp[N * M]; char op[N][M]; vector<int> G[N], G2[N], G3[N]; void add(int u, int v, vector<int> G[]) { G[u].push_back(v); } bool vis[N][M]; bool tv[N]; int val[N * M]; int c[N][M], tot; vector<int> path; void dfs(int u, int j, vector<int> G[], int col) { vis[u][j] = 1; c[u][j] = col; if (op[u][j] == 1 && !tv[u]) { tv[u] = 1; ++val[col]; path.push_back(u); } for (int v : G[u]) { if (!vis[v][(j + 1) % d]) dfs(v, (j + 1) % d, G, col); } } void doit(int bid) { for (int bx : bcc[bid]) for (int j = 0; j < d; ++j) if (!vis[bx][j]) { path.clear(); dfs(bx, j, G2, ++tot); for (int x : path) tv[x] = 0; } } int topo(vector<int> G[]) { for (int i = 0; i < B; ++i) { for (int bx : bcc[i]) for (int j = 0; j < d; ++j) { int uc = c[bx][j], u = bx; for (int v : G[u]) { int vc = c[v][(j + d - 1) % d]; update(dp[vc], dp[uc] + val[uc]); } } } int cc = c[0][0]; return dp[cc] + val[cc]; } pair<int, int> e[N]; namespace tarjan { int low[N], dfn[N], clk; void init(int n) { B = clk = 0; memset(dfn, 0, sizeof(int) * (n + 1)); for (int i = 0; i < n; ++i) bcc[i].clear(); } void tarjan(int u) { static int st[N], p; static bool in[N]; dfn[u] = low[u] = ++clk; st[p++] = u; in[u] = true; for (int &v : G[u]) { if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else if (in[v]) low[u] = min(low[u], dfn[v]); } if (dfn[u] == low[u]) { while (1) { int x = st[--p]; in[x] = false; bl[x] = B; bcc[B].push_back(x); if (x == u) break; } ++B; } } void go(int n) { init(n); for (int i = 0; i < n; ++i) { if (!dfn[i]) tarjan(i); } } } // namespace tarjan int main(int argc, char const *argv[]) { scanf( %d%d%d , &n, &m, &d); for (int i = 0; i < m; ++i) { scanf( %d%d , &e[i].first, &e[i].second); --e[i].first; --e[i].second; add(e[i].first, e[i].second, G); } for (int i = 0; i < n; ++i) scanf( %s , op[i]); tarjan::go(n); for (int i = 0; i < m; ++i) { int u = e[i].first, v = e[i].second; if (bl[u] == bl[v]) add(u, v, G2); else add(v, u, G3); } for (int i = 0; i < B; ++i) doit(i); int ans = topo(G3); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, unsigned long long int y) { long long int res = 1; while (y > 0) { if (y & 1) { res = res * x; } y = y >> 1; x = x * x; } return res; } long long int powermod(long long int x, unsigned long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } int32_t main() { long long int n, m; cin >> n >> m; vector<long long int> f(m + n); f[0] = 1, f[1] = 1; for (long long int i = (long long int)(2); i < (long long int)(m + n); i++) { f[i] = f[i - 1] + f[i - 2]; f[i] %= 1000000007; } long long int ans = 2 * f[n]; ans %= 1000000007; ans += 2 * f[m]; ans %= 1000000007; ans -= 2; if (ans < 0) ans += 1000000007; cout << ans << n ; return 0; }
#include <bits/stdc++.h> int B = 400; using namespace std; const long long inf = 1e8; int MOD = 1e9 + 7; const int mxn = 300010; const int N = 1000005; const double pi = 3.14159; int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}; int dy[] = {1, 0, -1, 0, -1, 1, 1, -1}; int n, d, k; vector<int> g[mxn]; vector<pair<int, int> > q[mxn]; long long add[mxn]; long long ans[mxn]; void dfs(int v, int p = -1, int d = 1, long long cur = 0) { for (auto i : q[v]) { int d_ = i.first, val = i.second; cur += (long long)val; int tot = min(d + d_ + 1, mxn); add[tot] -= (long long)val; } cur += add[d]; ans[v] = cur; for (auto i : g[v]) { if (i == p) continue; dfs(i, v, d + 1, cur); } for (auto i : q[v]) { int d_ = i.first, val = i.second; int tot = min(d + d_ + 1, mxn); add[tot] += (long long)val; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; g[u].push_back(v), g[v].push_back(u); } int qn; cin >> qn; while (qn--) { int n, d, val; cin >> n >> d >> val; q[n].push_back({d, val}); } dfs(1); for (int i = 1; i <= n; i++) cout << ans[i] << ; }
#include <bits/stdc++.h> using namespace std; int main() { int t; t = 1; for (int r = 0; r < t; r++) { long long n, m, i, x, y, z, sum = 0; cin >> n >> m; vector<string> v(51); char a = a , b = A ; for (i = 0; i <= 50; i++) { if (b > Z ) b = A ; v[i].push_back(b); if (i >= 26) { v[i].push_back(a); a++; } b++; } x = 0, y = 0; vector<string> ans; string temp; while (y < m - 1) { ans.push_back(v[y]); y++; } x = y; for (i = 0; i < (n - m + 1); i++, x++) { cin >> temp; if (temp == NO ) { ans.push_back(ans[x - (m - 1)]); } else { ans.push_back(v[y]); y++; } } for (i = 0; i < n; i++) cout << ans[i] << ; cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int i, j, k; int n, m, idx; string s, s2; cin >> s; n = s.length(); int cnt = 0; for (i = 0; i < n; i++) { if (s[i] == a ) cnt++; } k = 0; idx = 0; for (i = 0; i < n; i++) { if (k == cnt) break; if (s[i] == a ) k++, idx = i + 1; else s2.push_back(s[i]); } k = n - idx; k -= s2.length(); if (k % 2 == 0) { k /= 2; j = idx; idx += k; while (k > 0) { s2.push_back(s[j]); j++; k--; } k = s.length() - 1; for (i = s2.length() - 1; i >= 0; i--) { if (s2[i] != s[k]) { cout << :( ; return 0; } k--; } for (i = 0; i < idx; i++) cout << s[i]; } else cout << :( ; return 0; }
#include <bits/stdc++.h> using namespace std; int p1[400010], p2[400010], p3[400010], p0[400010]; int cnt1, cnt2, cnt3, cnt0; inline bool cmp(const int a, const int b) { return a > b; } int main() { int n; cin >> n; for (int i = 1, a, b, va; i <= n; i++) { scanf( %1d%1d %d , &a, &b, &va); if (a + b == 0) p0[++cnt0] = va; else if (a + b == 2) p3[++cnt3] = va; else if (a == 1) p1[++cnt1] = va; else p2[++cnt2] = va; } sort(p1 + 1, p1 + 1 + cnt1, cmp); sort(p2 + 1, p2 + 1 + cnt2, cmp); long long ans = 0; int fl = 0; if (cnt2 < cnt1) swap(cnt2, cnt1), fl = 1; for (int i = 1; i <= cnt1; i++) { ans += p2[i] + p1[i]; } if (fl) for (int i = cnt1 + 1; i <= cnt2; i++) p0[++cnt0] = p1[i]; else for (int i = cnt1 + 1; i <= cnt2; i++) p0[++cnt0] = p2[i]; sort(p0 + 1, p0 + 1 + cnt0, cmp); for (int i = 1; i <= cnt3; i++) { ans += p0[i] + p3[i]; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a, y, flag = 0, count, z, x; cin >> y; if (y % 400 == 0 || y % 4 == 0 && y % 100 != 0) { x = 1; } else { x = 2; } z = y + 1; if (x == 1) count = 3; else count = 2; while (flag != 1) { if (count == 1) { if (z % 400 == 0 || z % 4 == 0 && z % 100 != 0) { a = 1; } else a = 2; if (a == x) { flag = 1; break; } } if (z % 400 == 0 || z % 4 == 0 && z % 100 != 0) { if (count == 7) { count = 2; } else if (count == 6) count = 1; else count += 2; } else { if (count == 7) count = 1; else count += 1; } z++; } cout << z; return 0; }
#include <bits/stdc++.h> using namespace std; int n = 0, m = 0, k; vector<string> a; int CountSpiders(int j) { int sum = 0; int jR = j, jL = j; for (int i = 1; i < n; ++i) { if (!(i % 2) && a[i][j] == U ) ++sum; if (jR > 0) { --jR; if (a[i][jR] == R ) ++sum; } if (jL < m - 1) { ++jL; if (a[i][jL] == L ) ++sum; } } return sum; } int main() { cin >> n >> m >> k; cin.ignore(100, n ); a.resize(n); for (int i = 0; i < n; ++i) getline(cin, a[i]); for (int j = 0; j < m; ++j) { cout << CountSpiders(j) << ; } }
#include <bits/stdc++.h> using namespace std; int xx1, xx2, yy1, yy2; int x, y, r, ans; char v[30000000]; int zhuan(int x, int y) { return (x + 1000) * 10000 + y + 1000; } void swap(int &a, int &b) { int h = a; a = b; b = h; } bool dis(int xx, int yy) { return (xx - x) * (xx - x) + (yy - y) * (yy - y) <= r * r; } void fun(int x1, int y1, int x2, int y2) { if (x1 > x2) swap(x1, x2); if (y1 > y2) swap(y1, y2); for (int i = x1; i <= x2; i++) for (int j = y1; j <= y2; j++) { int k = zhuan(i, j); if (v[k] == 0 && dis(i, j)) { v[k] = 1; ans++; } } } int main() { while (scanf( %d %d %d %d , &xx1, &yy2, &xx2, &yy1) != EOF) { if (xx1 > xx2) swap(xx1, xx2); if (yy1 > yy2) swap(yy1, yy2); int n, i, j, k, w; memset(v, 0, sizeof(v)); scanf( %d , &n); ans = 0; for (i = 1; i <= n; i++) { scanf( %d %d %d , &x, &y, &r); fun(xx1, yy1, xx1, yy2); fun(xx1, yy2, xx2, yy2); fun(xx2, yy2, xx2, yy1); fun(xx2, yy1, xx1, yy1); } w = (xx2 - xx1 + 1) * 2 + (yy2 - yy1 + 1) * 2 - 4; if (xx2 == xx1 && yy1 == yy2) w = 1; printf( %d n , w - ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.length(); int ans = 0; int num = 0; for (int i = 0; i < n; i++) { if ((s[i] - 0 ) % 3 == 0) { ans++; num = 0; } else { num++; if (num == 2) { if ((s[i - 1] - 0 + s[i] - 0 ) % 3 == 0) { ans++; num = 0; } } if (num == 3) { ans++; num = 0; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long t, n, i, b[1010], ta, tb, k1, a[1010], tem, x[1010], y[1010], k2, tom; vector<long long> v[1010]; void dfs(long long aa, long long bb) { b[aa] = bb; long long ii; for (ii = 0; ii < v[aa].size(); ii++) if (!b[v[aa][ii]]) dfs(v[aa][ii], bb); } int main() { cin >> t; while (t--) { cin >> n; for (i = 1; i <= n; i++) v[i].clear(); for (i = 1; i <= n; i++) b[i] = 0; for (i = 1; i < n; i++) { cin >> ta >> tb; v[ta].push_back(tb); v[tb].push_back(ta); } for (i = 1; i <= n; i++) b[i] = 0; for (i = 1; i <= n; i++) x[i] = 0; for (i = 1; i <= n; i++) y[i] = 0; cin >> k1; for (i = 1; i <= k1; i++) { cin >> a[i]; b[a[i]] = a[i]; x[a[i]] = 1; } for (i = 1; i <= k1; i++) dfs(a[i], a[i]); cin >> k2; for (i = 1; i <= k2; i++) { cin >> ta; y[ta] = 1; } cout << B << ta << endl; cin >> tem; if (x[tem]) cout << C << tem << endl; else { cout << A << b[tem] << endl; cin >> tom; if (y[tom]) cout << C << b[tem] << endl; else cout << C << -1 << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { string s = qwertyuiopasdfghjkl;zxcvbnm,./ ; char lr; cin >> lr; string message; cin >> message; for (int i = 0; i < message.length(); i++) { int it = s.find(message[i]); if (lr == R ) { cout << s[it - 1]; } else { cout << s[it + 1]; } } }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e6 + 3; long long n, k, x; pair<long long, long long> ans; long long powmod(long long a, long long b, long long c) { long long ans = 1; while (b > 0) { if (b & 1) ans = ans * a % c; a = a * a % c; b /= 2; } return ans; } pair<long long, long long> work(long long n, long long k) { if (k == 0) { return make_pair(0, 1); } long long dq = powmod(2, n, mod), now = 1; for (int i = 1; i < 2 * mod; i += 2) { now = now * (dq - i + 2 * mod) % mod; } long long ans; ans = powmod(now, k / (2 * mod), mod); for (int i = 1; i <= (k % (2 * mod)); i += 2) ans = ans * (dq - i + 2 * mod) % mod; pair<long long, long long> cur = work(n - 1, k / 2); ans = ans * cur.second % mod; return make_pair((k / 2 + cur.first) % (mod - 1), ans); } int main() { cin >> n >> k; x = 0; while ((1LL << x) < k) x++; if (n < x) { cout << 1 1 << endl; return 0; } pair<long long, long long> ans = work(n, k - 1); long long fz, fm; fz = ans.second; fm = (n % (mod - 1)) * ((k - 1) % (mod - 1)) % (mod - 1); fm = (fm - ans.first + (mod - 1)) % (mod - 1); fm = powmod(2, fm, mod); fz = (fm - fz + mod) % mod; cout << fz << << fm << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 40; const long long INFLL = 1e18; long long dp[N][3][2][2]; signed main() { srand(time(NULL)); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; long long r1, r2, r3, d; cin >> n >> r1 >> r2 >> r3 >> d; vector<long long> a(n); for (int(i) = 0; (i) != (n); (i)++) cin >> a[i]; for (int(i) = 0; (i) != (n); (i)++) { for (int(j) = 0; (j) != (3); (j)++) { for (int(k) = 0; (k) != (2); (k)++) { dp[i][j][k][0] = dp[i][j][k][1] = INFLL; } } } dp[0][2][0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < 30; j++) { dp[i][2][0][0] = min(dp[i][2][0][0], dp[i][2][1][0] + 2 * d + r1); dp[i][2][0][0] = min(dp[i][2][0][0], dp[i][2][1][1] + 2 * d + r1); dp[i][1][0][0] = min(dp[i][1][0][0], dp[i][1][1][0] + 2 * d + r1); dp[i][1][0][0] = min(dp[i][1][0][0], dp[i][1][1][1] + 2 * d + r1); dp[i][0][0][0] = min(dp[i][0][0][0], dp[i][0][1][0] + 2 * d + r1); dp[i][0][0][0] = min(dp[i][0][0][0], dp[i][0][1][1] + 2 * d + r1); dp[i][0][0][0] = min(dp[i][0][0][0], dp[i][2][0][0] + a[i] * r1 + r3); dp[i][0][1][0] = min(dp[i][0][1][0], dp[i][2][1][0] + a[i] * r1 + r3); dp[i][1][0][1] = min(dp[i][1][0][1], dp[i][2][0][0] + min(r2, a[i] * r1 + r1)); dp[i][1][1][1] = min(dp[i][1][1][1], dp[i][2][1][0] + min(r2, a[i] * r1 + r1)); dp[i][0][0][0] = min(dp[i][0][0][0], dp[i][1][0][0] + r1); dp[i][0][1][0] = min(dp[i][0][1][0], dp[i][1][1][0] + r1); dp[i + 1][2][0][0] = min(dp[i + 1][2][0][0], dp[i][0][0][0] + d); dp[i + 1][2][0][0] = min(dp[i + 1][2][0][0], dp[i][0][0][1] + d); dp[i + 1][2][1][0] = min(dp[i + 1][2][1][0], dp[i][1][0][0] + d); dp[i + 1][2][1][0] = min(dp[i + 1][2][1][0], dp[i][1][0][1] + d); } } cout << min(dp[n - 1][0][0][0], dp[n - 1][0][1][0] + d + r1); }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; template <typename T> inline void read(T &x) { int ch = getchar(); x = 0; bool f = false; double d = 1; for (; ch != - && (ch < 0 || ch > 9 ); ch = getchar()) ; if (ch == - ) { f = true; ch = getchar(); } for (; ch >= 0 && ch <= 9 ; ch = getchar()) x = x * 10 + ch - 0 ; if (ch == . ) for (ch = getchar(); ch >= 0 && ch <= 9 ; ch = getchar()) x += (ch - 0 ) * (d *= 0.1); if (f) x = -x; } template <typename T> inline bool chkmax(T &a, const T &b) { if (a < b) return a = b, 1; return 0; } template <typename T> inline bool chkmin(T &a, const T &b) { if (a > b) return a = b, 1; return 0; } const int K = 81, N = 170, M = N << 4, INF = 0x3f3f3f3f; int n, k, S, T, a[K], c[K], head[N], to[M], nxt[M], cap[M], w[M], tmp[K]; inline void add(int a, int b, int c, int d) { static int cnt = 1; to[++cnt] = b; nxt[cnt] = head[a]; head[a] = cnt; cap[cnt] = c; w[cnt] = d; to[++cnt] = a; nxt[cnt] = head[b]; head[b] = cnt; cap[cnt] = 0; w[cnt] = -d; } int dis[N], inc[N], q[N], f, r, pre[N], ans; bool inq[N]; inline void AMOD(int &x) { ++x; if (x >= N) x = 0; } inline bool spfa() { memset(inq, 0, sizeof inq); memset(dis, 0x3f, sizeof dis); f = r = 0; q[r++] = S; inc[S] = INF; dis[S] = 0; inq[S] = true; while (f != r) { int u = q[f]; AMOD(f); inq[u] = false; for (register int i = head[u]; i; i = nxt[i]) if (cap[i] && chkmin(dis[to[i]], dis[u] + w[i])) { inc[to[i]] = min(inc[u], cap[i]); pre[to[i]] = i; if (!inq[to[i]]) { inq[to[i]] = true; q[r] = to[i]; AMOD(r); } } } return dis[T] < INF; } int main() { read(n); read(k); for (register int i = 1; i <= n; ++i) read(a[i]); for (register int i = 1; i <= n; ++i) read(c[i]); T = 1 + (S = n << 1 | 1); for (register int i = 1; i <= n; ++i) { add(S, i, 1, c[a[i]]); add(i, i + n, 1, 0); add(i + n, T, 1, 0); if (i < n) add(i, i + 1, k - 1, 0); if (tmp[a[i]]) add(i - 1, tmp[a[i]] + n, 1, -c[a[i]]); tmp[a[i]] = i; } while (spfa()) { ans += inc[T] * dis[T]; for (register int u = T; u; u = to[pre[u] ^ 1]) { cap[pre[u]] -= inc[T]; cap[pre[u] ^ 1] += inc[T]; } } printf( %d , ans); }
#include <bits/stdc++.h> using namespace std; char s[110], ss[110]; int a[210], ans[210], n, k, namlen; bool boo; bool check() { int i, j; for (i = 1; i <= n; i++) if (ss[i] == 0 ) { bool bk = true; for (j = 1; j <= namlen; j++) if (a[i + j - 1] != s[j]) { bk = false; break; } if (bk) return false; } return true; } void search(int x) { if (x == n + 1) { if (check()) { for (int i = 1; i <= n; i++) ans[i] = a[i]; boo = true; } return; } if (ss[x] == 1 ) { search(x + 1); return; } if (a[x] != 0) { search(x + 1); return; } for (int i = 0; i < k; i++) { a[x] = i + a ; search(x + 1); if (boo) return; } } int main() { int len, i, j; scanf( %d%d , &n, &k); getchar(); gets(s + 1); gets(ss + 1); namlen = strlen(s + 1); len = strlen(ss + 1); for (i = 1; i <= namlen; i++) if (s[i] - a + 1 > k) { printf( No solution n ); return 0; } bool bk = true; for (i = 1; i <= n; i++) a[0] = 0; for (i = 1; i <= len; i++) { if (ss[i] == 1 ) { if (i + namlen - 1 > n) { bk = false; break; } for (j = 1; j <= namlen; j++) { if (a[i + j - 1] != 0 && a[i + j - 1] != s[j]) { bk = false; break; } a[i + j - 1] = s[j]; } if (!bk) break; } } if (!bk) { printf( No solution n ); return 0; } boo = false; search(1); if (!boo) printf( No solution n ); else { for (i = 1; i <= n; i++) printf( %c , char(ans[i])); printf( n ); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string a; cin >> a; string res = ; int kolvo = k / 2; int i = 0; int s = 0; int bla = 0; while (i < kolvo) { res += a[s]; if (a[s] == ( ) { ++i; } else { ++bla; } ++s; } while (bla < kolvo) { res += ) ; ++bla; } cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5, M = 1e18 + 5, OO = 0x3f3f3f3f; const double pi = 3.141592653589793238462643383279502884; double n, r; double calc() { double ang = 180 / n; ang = (ang / 180) * pi; double a = (sin(ang / 2) * r) / sin(pi - (1.5 * ang)); double ans = n * a * r * sin(ang); return ans; } int main() { cin >> n >> r; printf( %.12lf n , calc()); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 3228; const int MOD = 1000 * 1000 * 1000 + 7; long long pw(long long a, int first) { if (!first) return 1; if (first % 2) return a * pw(a * a, first / 2); return pw(a * a, first / 2); } void solve() { int pl[3] = {0}; int n, p1, p2; cin >> n >> p1 >> p2; pl[1] -= p1; pl[2] -= p2; for (int i = 0; i < n; ++i) { int nxt; cin >> nxt; if (nxt == 2 && pl[2] >= 0) ++pl[1]; else ++pl[nxt]; } cout << max(0, pl[1]) + max(0, pl[2]); } int main() { double tt = clock(); cout.tie(nullptr); cin.tie(nullptr); ios_base::sync_with_stdio(false); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int N = 300005; const double PI = 4 * atan(1); const long long MAX = 9223372036854775807; int dx[] = {1, -1, 0, 0, 1, -1, 1, -1}; int dy[] = {0, 0, 1, -1, 1, 1, -1, -1}; long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } bool issquare(long long w) { return trunc(sqrt(w)) * trunc(sqrt(w)) == w; } bool isprime(long long u) { for (long long i = 2; i <= (int)sqrt(u); i++) { if (u % i == 0) return 0; } return 1; } long long mod(long long to_mod, long long MOD) { to_mod %= MOD; while (to_mod < 0) to_mod += MOD; return to_mod % MOD; } long long moduloMultiplication(long long a, long long b, long long mod) { long long res = 0; a %= mod; while (b) { if (b & 1) res = (res + a) % mod; a = (2 * a) % mod; b >>= 1; } return res; } long long power(long long x, long long y) { long long res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long long n, m, x, y, res, t, k, ans = 0, q, z, w, l, a, b; vector<pair<long long, pair<long long, long long> > > v; long long dp[N]; vector<long long> vv; long long curr[N]; int main() { ios::sync_with_stdio(0); memset(dp, 0, sizeof(dp)); memset(curr, 0, sizeof(curr)); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> x >> y >> z; x--; y--; v.push_back(make_pair(z, make_pair(x, y))); } sort(v.begin(), v.end()); ans = 1; for (int i = 0; i < m;) { long long j = i; vv.clear(); while (j < m && v[j].first == v[i].first) { curr[v[j].second.second] = max(curr[v[j].second.second], 1 + dp[v[j].second.first]); vv.push_back(v[j].second.second); j++; } i = j; for (auto t : vv) { dp[t] = max(dp[t], curr[t]); } } for (int i = 0; i < n; i++) { ans = max(ans, dp[i]); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int res = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == - ) f = -f; ch = getchar(); } while (isdigit(ch)) { res = (res << 3) + (res << 1) + ch - 0 ; ch = getchar(); } return res * f; } namespace qiqi { const int N = 6e5 + 5; int n, m, siz[N], si[N], fa[N], ch[N][2], rev[N], cnt, mx[N]; bool del[N]; struct Edge { int u, v, w, id; inline bool friend operator<(Edge a, Edge b) { return a.w < b.w; } } e[N]; priority_queue<Edge> q; inline bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; } inline void push_up(int x) { siz[x] = (x <= n) + si[x] + siz[ch[x][0]] + siz[ch[x][1]]; mx[x] = x > n ? x - n : 0; for (int i = (0); i <= (1); ++i) if (e[mx[ch[x][i]]].w > e[mx[x]].w) mx[x] = mx[ch[x][i]]; } inline void push_down(int x, bool k) { if (k && !isroot(x)) push_down(fa[x], k); if (rev[x]) { swap(ch[x][0], ch[x][1]); rev[ch[x][0]] ^= 1; rev[ch[x][1]] ^= 1; rev[x] = 0; } } inline void rotate(int x) { int y = fa[x], z = fa[y], l = x == ch[y][1]; if (!isroot(y)) ch[z][y == ch[z][1]] = x; fa[x] = z; push_up(fa[ch[y][l] = ch[x][l ^ 1]] = y); push_up(fa[ch[x][l ^ 1] = y] = x); } inline void splay(int x) { push_down(x, 1); while (!isroot(x)) { int y = fa[x]; if (!isroot(y)) rotate(x ^ y ^ ch[y][0] ^ ch[fa[y]][0] ? x : y); rotate(x); } push_up(x); } inline void access(int x) { for (int i = 0; x; x = fa[i = x]) { splay(x); si[x] += siz[ch[x][1]]; si[x] -= siz[ch[x][1] = i]; push_up(x); } } inline void makeroot(int x) { access(x); splay(x); rev[x] ^= 1; } inline void split(int x, int y) { makeroot(x); access(y); splay(y); } inline int find(int x) { access(x); splay(x); while (ch[x][rev[x]]) x = ch[x][rev[x]]; splay(x); return x; } inline int qry(int x, int y) { split(x, y); return mx[y]; } inline void cut(int x, int y) { split(x, y); cnt -= siz[y] & 1; ch[y][0] = fa[x] = 0; push_up(y); cnt += (siz[x] & 1) + (siz[y] & 1); } inline void link(int x, int y) { split(x, y); cnt -= (siz[x] & 1) + (siz[y] & 1); si[fa[x] = y] += siz[x]; push_up(y); cnt += siz[y] & 1; } inline int upd(int x, int y, int id) { bool flg = 1; if (find(x) == find(y)) { int k = qry(x, y); if (e[k].w > e[id].w) { del[k] = 1; cut(e[k].u, k + n); cut(e[k].v, k + n); } else flg = 0; } if (flg) { link(x, id + n); link(y, id + n); } if (cnt) return -1; while (1) { Edge e = q.top(); q.pop(); if (del[e.id]) continue; cut(e.u, e.id + n); cut(e.v, e.id + n); if (cnt) { q.push(e); link(e.u, e.id + n); link(e.v, e.id + n); return q.top().w; } } } void main() { e[0].w = 0; cnt = n = read(); m = read(); for (int i = (1); i <= (m); ++i) { q.push(e[i] = (Edge){read(), read(), read(), i}); printf( %d n , upd(e[i].u, e[i].v, i)); } } } // namespace qiqi int main() { qiqi::main(); return 0; }
#include <bits/stdc++.h> using namespace std; int max(int a, int b) { if (a > b) return a; else return b; } long long min(long long a, long long b) { if (a < b) return a; else return b; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int lcm(int a, int b) { return a * (b / gcd(a, b)); } vector<bool> isPrime(1000005 + 1, true); string tos(int n) { stringstream ss; ss << n; return ss.str(); } void seive() { isPrime[0] = 0, isPrime[1] = 0; for (long long i = 2; i * i <= 1000005; i++) if (isPrime[i]) for (long long j = i * i; j <= 1000005; j += i) isPrime[j] = 0; } int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; const int val = 1 << 20; long long n, m, st, e, ans = 1, a[1111111], b[1111111]; int main() { string s1, s2; cin >> s1 >> s2; long long i = 0, j = 0, beg = -1; while (j < s2.size()) { if (s1[i] == s2[j]) { if (beg == -1) { beg = j; a[0] = j; } i++; } if (i == s1.size()) { b[0] = j; break; } j++; } i = s1.size(); j = s2.size(); beg = -1; while (j >= 0) { if (s1[i] == s2[j]) { if (beg == -1) { beg = j; b[1] = j; } i--; } if (i < 0) { a[1] = j; break; } j--; } ans = max(0, a[1] - b[0]); cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x < 0 ? -x : x; } const double pi = asin(1.0) * 2; int n; int m; long long pl, pr, vl, vr; int k; vector<long long> h; double prob(long long l, long long r, long long L, long long R) { L = max(l, L); R = min(R, r); if (L > R) return 0.0; return (R - L + 1) / (double)(r - l + 1); } int main() { cin >> pl >> pr >> vl >> vr >> k; h.push_back(0); int cur = 0; for (int i = cur; h[i] * 10 + 4 <= 1e10; i++) { h.push_back(h[i] * 10 + 4); h.push_back(h[i] * 10 + 7); } sort((h).begin(), (h).end()); double ans = 0; for (int i = 1; i < (int)((h).size()); i++) { if (i + k >= (int)((h).size())) break; long long l1 = h[i - 1] + 1, r1 = h[i]; long long l2 = h[i + k - 1], r2 = h[i + k] - 1; ans += prob(pl, pr, l1, r1) * prob(vl, vr, l2, r2); ans += prob(pl, pr, l2, r2) * prob(vl, vr, l1, r1); ans -= prob(pl, pr, max(l2, l1), min(r1, r2)) * prob(vl, vr, max(l2, l1), min(r1, r2)); } printf( %.10lf n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; long long calc2(long long a, long long b, long long c, long long ab, long long ac, long long bc, long long abc) { long long parts[] = {a - ab - ac + abc, b - ab - bc + abc, c - ac - bc + abc, ab - abc, ac - abc, bc - abc, abc}; int masks[3] = {1 + 8 + 16 + 64, 2 + 8 + 32 + 64, 4 + 16 + 32 + 64}; static int mark[64 * 8] = {0}; static int id = 0; ++id; long long res = 0; for (int i = 0; i < 7; ++i) if ((masks[0] >> i) & 1) for (int j = 0; j < 7; ++j) if ((masks[1] >> j) & 1) for (int k = 0; k < 7; ++k) if ((masks[2] >> k) & 1) { int a1 = min(i, min(j, k)); int a3 = max(i, max(j, k)); int a2 = i + j + k - a1 - a3; if (mark[64 * a1 + 8 * a2 + a3] == id) continue; mark[64 * a1 + 8 * a2 + a3] = id; if (i == j && j == k) { long long x = parts[i]; res += +x * (x - 1) * (x - 2) / 6 + x * (x - 1) + x; } else if (i == j || j == k || i == k) { long long x = parts[i]; long long y = parts[j]; if (i == j) y = parts[k]; else if (j == k) swap(x, y); res += x * (x + 1) / 2 * y; } else { res += parts[i] * parts[j] * parts[k]; } } return res; } int get_common_num(vector<int> &a, vector<int> &b) { int ia = 0, ib = 0; int res = 0; while (ia < a.size() && ib < b.size()) { if (a[ia] == b[ib]) { ++res; ++ia, ++ib; } else if (a[ia] < b[ib]) ++ia; else ++ib; } return res; } int get_common_num(vector<int> &a, vector<int> &b, vector<int> &c) { int ia = 0, ib = 0, ic = 0; int res = 0; while (ia < a.size() && ib < b.size() && ic < c.size()) { if (a[ia] == b[ib] && b[ib] == c[ic]) { ++res; ++ia, ++ib, ++ic; } else if (a[ia] <= min(b[ib], c[ic])) ++ia; else if (b[ib] <= min(a[ia], c[ic])) ++ib; else ++ic; } return res; } vector<int> dividers[100111]; int main() { ios::sync_with_stdio(false); dividers[1].push_back(1); vector<int> smallest_fact(100111 + 1, 0); for (int i = 2; i < 100111; ++i) { if (!smallest_fact[i]) { smallest_fact[i] = i; for (long long j = i * (long long)i; j < 100111; j += i) { if (!smallest_fact[j]) smallest_fact[j] = i; } } int p = smallest_fact[i]; int x = i; while (x % p == 0) x /= p; int y = i; int pp = p; dividers[i] = dividers[x]; while (y % p == 0) { for (int d : dividers[x]) dividers[i].push_back(pp * d); pp *= p; y /= p; } sort(dividers[i].begin(), dividers[i].end()); } int t; cin >> t; while (t--) { int a, b, c; cin >> a >> b >> c; int x = dividers[a].size(); int y = dividers[b].size(); int z = dividers[c].size(); int xy = get_common_num(dividers[a], dividers[b]); int xz = get_common_num(dividers[a], dividers[c]); int yz = get_common_num(dividers[b], dividers[c]); int xyz = get_common_num(dividers[a], dividers[b], dividers[c]); long long ans = calc2(x, y, z, xy, xz, yz, xyz); cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; double p, f[220][220]; int x, k; int main() { scanf( %d%d%lf , &x, &k, &p); p /= 100; for (int i = 0; i <= k; i++) for (int j = x + i; j % 2 == 0; j /= 2) f[0][i]++; for (int i = 0; i < k; i++) { for (int j = 0; j <= k; j++) { if (j) f[i + 1][j - 1] += (1 - p) * f[i][j]; if (j * 2 <= k) f[i + 1][j * 2] += p * (f[i][j] + 1); } } printf( %.10lf n , f[k][0]); return 0; }
#include <bits/stdc++.h> using namespace std; void inout() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; } int main() { inout(); long long n, i, u, v; cin >> n; map<long long, long long> prv, nxt, cnt1, cnt2; vector<long long> ans(n + 5); for (i = 0; i < n; i++) { cin >> u >> v; prv[v] = u; nxt[u] = v; cnt1[u]++; cnt2[v]++; } for (auto k : cnt1) if (cnt1[k.first] > cnt2[k.first]) ans[1] = k.first; for (auto k : cnt2) if (cnt1[k.first] < cnt2[k.first]) ans[n] = k.first; i = 3; while (i <= n) { ans[i] = nxt[ans[i - 2]]; i += 2; } i = n + n % 2 - 2; while (i > 0) { ans[i] = prv[ans[i + 2]]; i -= 2; } for (i = 1; i <= n; i++) cout << ans[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 105000; const int M = 105; int n, m, p; long long hill[N]; long long a[N], s[N]; long long dp[N]; int top, tail; long long duix[N], duiy[N]; int getint() { char e = getchar(); int res = 0; while (e < 0 || e > 9 ) e = getchar(); while ( 0 <= e && e <= 9 ) { res = (res << 3) + (res << 1) + e - 0 ; e = getchar(); } return res; } int main() { int i, j; n = getint(); m = getint(); p = getint(); for (i = 2; i <= n; i++) hill[i] = getint(); for (i = 2; i <= n; i++) hill[i] += hill[i - 1]; for (i = 1; i <= m; i++) { a[i] = -hill[getint()]; a[i] += getint(); } sort(a + 1, a + m + 1); for (i = 1; i <= m; i++) s[i] = a[i] + s[i - 1]; for (i = 1; i <= m; i++) dp[i] = i * a[i]; for (j = 2; j <= p; j++) { top = 1; tail = 0; for (i = 1; i <= m; i++) { long long nowx = i; long long nowy = dp[i]; while (tail >= 2) { double xla = duiy[tail] - duiy[tail - 1]; xla /= duix[tail] - duix[tail - 1]; double xlb = nowy - duiy[tail - 1]; xlb /= nowx - duix[tail - 1]; if (xla - xlb > -1e-10) tail--; else break; } tail++; duix[tail] = nowx; duiy[tail] = nowy; } for (i = 1; i <= m; i++) { dp[i] = duiy[top] - a[i] * duix[top]; while (top < tail && duix[top + 1] <= i) { long long now = duiy[top + 1] - a[i] * duix[top + 1]; if (now <= dp[i]) { top++; dp[i] = now; } else break; } dp[i] += i * a[i]; } } printf( %I64d n , dp[m] - s[m]); return 0; }
#include <bits/stdc++.h> using namespace std; int n, h; int main() { scanf( %d %d , &n, &h); for (int i = 1; i < n; i++) { printf( %.10lf n , sqrt(1. * h * h * i / n)); } }
#include <bits/stdc++.h> using namespace std; void sprint(int x) { cout << x; } void sprint(long x) { cout << x; } void sprint(long long x) { cout << x; } void sprint(unsigned x) { cout << x; } void sprint(unsigned long x) { cout << x; } void sprint(unsigned long long x) { cout << x; } void sprint(float x) { cout << x; } void sprint(double x) { cout << x; } void sprint(long double x) { cout << x; } void sprint(char x) { cout << << x << ; } void sprint(const char *x) { cout << << x << ; } void sprint(const string &x) { cout << << x << ; } void sprint(bool x) { cout << (x ? true : false ); } template <typename T, typename V> void sprint(const pair<T, V> &x); template <typename T> void sprint(const vector<T> &x); template <typename T, typename V> void sprint(const pair<T, V> &x) { cout << { ; sprint(x.first); cout << , ; sprint(x.second); cout << } ; } template <typename T> void sprint(const vector<T> &x) { cout << { ; for (int u69 = 0; u69 < x.size(); u69++) { sprint(x[u69]); cout << (u69 + 1 == x.size() ? : , ); }; cout << } ; } template <typename T> void sprint(const T &x) { int f = 0; cout << { ; for (auto &i : x) cout << (f++ ? , : ), sprint(i); cout << } ; } void sprint() { cout << ] n ; } template <typename T, typename... V> void sprint(T t, V... v) { sprint(t); if (sizeof...(v)) cout << , ; sprint(v...); } inline long long ceil2(long long a, long long b) { return (a + b - 1) / b; } inline bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); } bool isPerfectSquare(long long x) { long long sr = sqrt(x); return ((sr * sr) == x); } bool isIn(string &s2, string &s1) { if (s1.find(s2) != string::npos) return true; return false; } bool isSorted(vector<int> &a) { for (int i = 0; i < (int)a.size() - 1; i++) if (a[i] > a[i + 1]) return false; return true; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; char a[n][m]; for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { cin >> a[i][j]; } } int count = 0; for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { if (a[i][j] == W && ((a[i + 1][j] == P && i + 1 < n) || (a[i - 1][j] == P && i - 1 >= 0) || (a[i][j + 1] == P && j + 1 < m) || (a[i][j - 1] == P && j - 1 >= 0))) { count++; } } } cout << count << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int l[100005], r[100005]; int main(int argc, char* argv[]) { std::ios::sync_with_stdio(false); int n, k; scanf( %d%d , &n, &k); int sum = 0; for (int i = 0; i < n; i++) { scanf( %d%d , &l[i], &r[i]); sum += r[i] - l[i] + 1; } int res = 0; while (sum % k != 0) { sum++; res++; } printf( %d n , res); return 0; }
#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); } template <class T> inline T lcm(T a, T b) { a = abs(a); b = abs(b); return (a / _gcd(a, b)) * b; } template <class T, class X> inline bool getbit(T a, X i) { T t = 1; return ((a & (t << i)) > 0); } template <class T, class X> inline T setbit(T a, X i) { T t = 1; return (a | (t << i)); } template <class T, class X> inline T resetbit(T a, X i) { T t = 1; return (a & (~(t << i))); } inline long long power(long long a, long long b) { long long multiply = 1; for (int i = (0); i < (b); i++) { multiply *= a; } return multiply; } char given[505][505]; long long dp[505][505]; int n, m, modby; long long calc(int x, int y) { long long &ret = dp[x][y]; if (x == 2 && y == 0) return 1; if (x == 0 && y == 1) return 0; if (ret != -1) return ret; ret = 0; long long a; if (x > 1) { a = calc(x - 2, y) % modby; a = (a * ((x * (x - 1)) / 2) % modby) % modby; ret += a % modby; } if (y > 1) { a = calc(x + 2, y - 2) % modby; a = (a * ((y * (y - 1)) / 2) % modby) % modby; ret += a % modby; } if (x != 0 && y != 0) { a = calc(x, y - 1) % modby; a = (a * (x * y) % modby) % modby; ret += a % modby; } return ret; } int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> modby; memset(dp, -1, sizeof(dp)); for (int i = (0); i < (m); i++) { for (int j = (0); j < (n); j++) cin >> given[i][j]; } int zeros = 0, ones = 0; for (int i = (0); i < (n); i++) { int x = 0; for (int j = (0); j < (m); j++) { if (given[j][i] == 1 ) x++; } if (x == 0) zeros++; else if (x == 1) ones++; } if (n == m) cout << 1 << endl; else cout << calc(ones, zeros) % modby << endl; return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:64000000 ) using namespace std; const int MAXN = -1; const int MOD = 1; const long double EPS = 1e-11; long double sqr(long double x) { return x * x; } bool ok(long double x) { return x >= -EPS && x <= 1 + EPS; } void norm(long double &x) { if (x < 0) x = 0; if (x > 1) x = 1; } int main() { int n; while (scanf( %d , &n) == 1) { vector<long double> mn(n), mx(n); for (int i = 0; i < n; i++) { double o; scanf( %lf , &o); mx[i] = o; } for (int i = 0; i < n; i++) { double o; scanf( %lf , &o); mn[i] = o; } vector<double> a(n), b(n); long double sa = 0.0, sb = 0.0; for (int i = 0; i < n; i++) { long double g = mn[i] + mx[i]; long double A = -1; long double B = (sb + g - sa); long double C = g * sa - mx[i]; long double D = B * B - 4 * A * C; assert(D >= -EPS); D = fabs(D); long double na = (-B - sqrt(D)) / (2 * A); long double nb = g - na; assert(ok(na)); assert(ok(nb)); a[i] = na; b[i] = nb; sa += na; sb += nb; } for (int i = 0; i < n; i++) printf( %.15f%c , a[i], n [i + 1 == n]); for (int i = 0; i < n; i++) printf( %.15f%c , b[i], n [i + 1 == n]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 65; char g[maxn][maxn]; int r, c, row[maxn], col[maxn]; void solve() { memset(row, 0, sizeof row); memset(col, 0, sizeof col); int sum = 0; for (int i = 1; i <= r; i++) { for (int j = 1; j <= c; j++) { if (g[i][j] == A ) col[i]++, row[j]++, sum++; } } if (sum == 0) printf( MORTAL n ); else if (sum == r * c) printf( 0 n ); else if (row[1] == r || row[c] == r || col[1] == c || col[r] == c) printf( 1 n ); else { int flag = 0; for (int i = 2; i < r; i++) if (col[i] == c) flag = 1; for (int i = 2; i < c; i++) if (row[i] == r) flag = 1; if (flag == 1 || g[1][1] == A || g[r][1] == A || g[1][c] == A || g[r][c] == A ) printf( 2 n ); else if (row[1] > 0 || row[c] > 0 || col[1] > 0 || col[r] > 0) printf( 3 n ); else printf( 4 n ); } } int main() { int T; scanf( %d , &T); while (T--) { scanf( %d%d , &r, &c); for (int i = 1; i <= r; i++) scanf( %s , g[i] + 1); solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[300005], pre[300005], L[300005], R[300005]; int st[300005][19], f[300005], ans; int Max(int l, int r) { int s = f[r - l]; return a[st[l][s]] > a[st[r - (1 << s)][s]] ? st[l][s] : st[r - (1 << s)][s]; } void solve(int l, int r) { if (l > r) return; if (l == r) { ans += a[l] == 1; return; } int k = Max(l, r); if (k - l < r - k) { for (int i = l; i <= k; i++) if (i + a[k] - 1 >= k && i + a[k] - 1 <= r && R[i] >= i + a[k] - 1) ans++; } else for (int i = k; i <= r; i++) if (i - a[k] + 1 <= k && i - a[k] + 1 >= l && L[i] <= i - a[k] + 1) ans++; solve(l, k - 1), solve(k + 1, r); } int main() { scanf( %d , &n); for (int i = 2; i <= n; i++) f[i] = f[i >> 1] + 1; for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int i = 1; i < n; i++) st[i][0] = a[i] > a[i + 1] ? i : (i + 1); for (int i = 1; i <= n; i++) L[i] = max(L[i - 1], pre[a[i]] + 1), pre[a[i]] = i; for (int i = 1; i <= n; i++) pre[i] = n + 1; R[n] = n, pre[a[n]] = n; for (int i = n - 1; i >= 1; i--) R[i] = min(R[i + 1], pre[a[i]] - 1), pre[a[i]] = i; for (int i = 1; 1 << i < n; i++) for (int j = 1; j <= n - (1 << i); j++) st[j][i] = a[st[j][i - 1]] > a[st[j + (1 << i - 1)][i - 1]] ? st[j][i - 1] : st[j + (1 << i - 1)][i - 1]; solve(1, n); printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int c[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; char a[60][60], color[2510]; int to[60][60], minx[2510], maxx[2510], miny[2510], maxy[2510], f[2510][4][2]; void dfs(int x0, int y0, int value) { to[x0][y0] = value; minx[value] = min(minx[value], x0); miny[value] = min(miny[value], y0); maxx[value] = max(maxx[value], x0); maxy[value] = max(maxy[value], y0); for (int i = 0; i < 4; i++) { int x = x0 + c[i][0], y = y0 + c[i][1]; if (to[x][y] == 0 && a[x][y] == a[x0][y0]) dfs(x, y, value); } } int calc(int x, int y, int z) { int x1 = minx[x], x2 = maxx[x], y1 = miny[x], y2 = maxy[x]; if (y == 0) return (z == 0 ? to[x1][y2 + 1] : to[x2][y2 + 1]); else if (y == 1) return (z == 0 ? to[x2 + 1][y2] : to[x2 + 1][y1]); else if (y == 2) return (z == 0 ? to[x2][y1 - 1] : to[x1][y1 - 1]); else return (z == 0 ? to[x1 - 1][y1] : to[x1 - 1][y2]); } int main() { int n, K; scanf( %d%d , &n, &K); for (int i = 1; i <= n; i++) scanf( %s , a[i] + 1); int m = strlen(a[1] + 1), tot = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (a[i][j] != 0 && to[i][j] == 0) { tot++; color[tot] = a[i][j]; minx[tot] = miny[tot] = 1 << 30; maxx[tot] = maxy[tot] = 0; dfs(i, j, tot); } for (int i = 1; i <= tot; i++) for (int j = 0; j < 4; j++) for (int k = 0; k < 2; k++) f[i][j][k] = calc(i, j, k); int bp = 1, dp = 0, cp = 0; for (int i = 1; i <= K; i++) { int p = f[bp][dp][cp]; if (p == 0) if (cp == 0) cp = 1; else { cp = 0; dp = dp + 1 & 3; } else bp = p; } printf( %c n , color[bp]); return (0); }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const unsigned long long MAX = LONG_MAX; void fasterIO() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int sumDig(int a) { int sum = 0; while (a != 0) { sum += a % 10; a /= 10; } return sum; } int main() { int test; cin >> test; while (test--) { long long p, k; long long arr[150]; cin >> p >> k; for (unsigned long long i = 0; i < (p); i++) cin >> arr[i]; sort(arr, arr + p); if (abs((arr[0] + k) - arr[p - 1]) > k) cout << -1 << endl; else cout << arr[0] + k << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long a[18] = {0}; long long int get(long long int n, int l) { if (!n) return 0; if (l == 1) return n; long long int t = a[l]; if (n == t) return l; if (n < t) return min(get(n, l - 1), l + get(t - n, l - 1)); else return min((n / t) * l + get(n % t, l - 1), ((1 + (n / t)) * l) + get(t - n % t, l - 1)); } int main() { long long int n, i, ans; scanf( %I64d , &n); for (int i = 1; i < 18; i++) a[i] = a[i - 1] * 10 + 1; for (i = 1; n > a[i]; i++) ; ans = get(n, i); printf( %I64d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int t, n, p, c; int main() { scanf( %d , &t); while (t--) { scanf( %d%d , &n, &p), c = 2 * n + p; for (int i = 1; i <= n; ++i) for (int j = i + 1; j <= n; ++j) if (c) printf( %d %d n , i, j), --c; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > g[500100]; long long f[500100][2]; int n, k; void dfs(int u, int fa) { vector<long long> d; d.clear(); f[u][0] = f[u][1] = 0; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i].first, w = g[u][i].second; if (v == fa) continue; dfs(v, u); f[u][0] += f[v][1]; f[u][1] += f[v][1]; d.push_back(-(w + f[v][0] - f[v][1])); } sort(d.begin(), d.end()); for (int i = 0; i < d.size() && i < k; i++) { long long D = -d[i]; if (D <= 0) break; if (i < k - 1) f[u][0] += D; f[u][1] += D; } } void ae(int x, int y, int z) { g[x].push_back(make_pair(y, z)); } void solve() { scanf( %d%d , &n, &k); for (int i = 1; i <= n; i++) g[i].clear(); int x, y, z; for (int i = 1; i < n; i++) { scanf( %d%d%d , &x, &y, &z); ae(x, y, z); ae(y, x, z); } dfs(1, 0); printf( %lld n , f[1][1]); } int main() { int tc; scanf( %d , &tc); while (tc--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int a[MAXN], b[MAXN], f[260], n, k; int main(void) { ios::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; b[i] = a[i]; } memset(f, -1, sizeof f); for (int i = 1; i <= n; ++i) { if (f[a[i]] == -1) { int low = -1; for (int j = a[i]; j >= a[i] - k + 1 && j >= 0; --j) { if (f[j] != -1) { if (f[j] >= a[i] - k + 1) low = f[j]; break; } low = j; } for (int j = low; j <= a[i]; ++j) { f[j] = low; } } b[i] = f[a[i]]; } for (int i = 1; i <= n; ++i) { cout << b[i] << ; } cout << n ; return 0; }
#include <bits/stdc++.h> using namespace std; char s[100005]; int n, a[100005], ans[100005]; inline bool find() { int i = 1, j = n; while (i <= j) { if (a[i] == a[j]) { i++; j--; } else if (a[i] == a[j] + 10) { a[j - 1]--; a[j] += 10; } else if ((a[i] == a[j] + 1) || (a[i] == a[j] + 11)) { a[i]--; a[i + 1] += 10; } else return false; } if ((i - 1 == j + 1) && ((a[i - 1] > 18) || (a[i - 1] < 0) || (a[i - 1] % 2 == 1))) return false; if (a[1] == 0) return false; i--; j++; while (i) { if ((a[i] > 18) || (a[i] < 0)) return false; ans[i] = (a[i] + 1) / 2; ans[j] = a[i] - ans[i]; i--; j++; } for (int i = 1; i <= n; i++) printf( %d , ans[i]); printf( n ); return true; } int main() { scanf( %s , s + 1); for (int i = 1; i <= strlen(s + 1); i++) a[i] = s[i] - 0 ; n = strlen(s + 1); if (find()) return 0; if ((s[1] != 1 ) || (n == 1)) { printf( 0 n ); return 0; } for (int i = 1; i <= n; i++) a[i - 1] = s[i] - 0 ; a[1] += 10; n--; if (find()) return 0; printf( 0 n ); return 0; }
#include <bits/stdc++.h> using namespace std; int ben[11]; int n, k; int mem[505][505 * 11]; int arr[505 * 505]; int freqFav[100005]; int freqCards[100005]; int solve(int num, int rem) { if (rem == 0 || num == 0) return 0; int &ret = mem[num][rem]; if (~ret) return ret; for (int i = 0; i < min(k, rem); i++) { ret = max(ret, ben[i] + solve(num - 1, rem - i - 1)); } return ret; } int main() { memset(mem, -1, sizeof(mem)); scanf( %d %d , &n, &k); for (int i = 0; i < k * n; i++) { scanf( %d , arr + i); freqCards[arr[i]]++; } for (int i = 0; i < n; i++) { int fav; scanf( %d , &fav); freqFav[fav]++; } for (int i = 0; i < k; i++) { scanf( %d , ben + i); } int res = 0; for (int i = 0; i < 100005; i++) { res += solve(freqFav[i], freqCards[i]); } printf( %d n , res); return 0; }
#include <bits/stdc++.h> using namespace std; const int sz = 4e6 + 50; int n, m; string str; int s[sz], e[sz], four[sz], seven[sz], non_dec[sz], non_inc[sz]; vector<bool> lazy(sz, 0); void calculate(int node) { int lc = node * 2; int rc = lc + 1; int f1 = four[lc], s1 = seven[lc], nd1 = non_dec[lc], ni1 = non_inc[lc]; int f2 = four[rc], s2 = seven[rc], nd2 = non_dec[rc], ni2 = non_inc[rc]; if (lazy[lc]) { swap(f1, s1); swap(nd1, ni1); } if (lazy[rc]) { swap(f2, s2); swap(nd2, ni2); } four[node] = f1 + f2; seven[node] = s1 + s2; non_dec[node] = max(f1 + nd2, nd1 + s2); non_inc[node] = max(s1 + ni2, ni1 + f2); } void modify(int node) { int lc = node * 2; int rc = lc + 1; four[node] = four[lc] + four[rc]; seven[node] = seven[lc] + seven[rc]; non_dec[node] = max(four[lc] + non_dec[rc], non_dec[lc] + seven[rc]); non_inc[node] = max(seven[lc] + non_inc[rc], non_inc[lc] + four[rc]); } void lazyPropagation(int node) { if (lazy[node]) { lazy[2 * node] = !lazy[2 * node]; lazy[2 * node + 1] = !lazy[2 * node + 1]; } lazy[node] = 0; } void build(int node, int l, int r) { s[node] = l; e[node] = r; if (l == r) { if (str[l] == 4 ) { four[node] = 1; seven[node] = 0; non_dec[node] = 1; non_inc[node] = 1; } else { four[node] = 0; seven[node] = 1; non_dec[node] = 1; non_inc[node] = 1; } return; } int mid = (l + r) / 2; build(2 * node, l, mid); build(2 * node + 1, mid + 1, r); modify(node); } void update(int node, int l, int r) { if (s[node] > r || e[node] < l) return; if (l <= s[node] && e[node] <= r) { lazy[node] = !lazy[node]; return; } lazyPropagation(node); update(2 * node, l, r); update(2 * node + 1, l, r); calculate(node); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string tt; int x, y; cin >> n >> m; cin >> str; str = 0 + str; build(1, 1, n); while (m--) { cin >> tt; if (tt[0] == c ) { if (!lazy[1]) cout << non_dec[1] << n ; else cout << non_inc[1] << n ; } else { cin >> x >> y; update(1, x, y); } } return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int l, int r) { return l + rng() % (r - l + 1); } const int N = 200010; int n; long long ret; int a[N], lz[N], b[N]; int f[N][25]; int que(int i, int j) { int t = lz[j - i + 1]; return max(f[i][t], f[j - (1 << t) + 1][t]); } void dnc(int l, int r) { if (l >= r) return; int x = que(l, r); int p = b[x]; if (p - l < r - p) for (int i = l, _c = p - 1; i <= _c; ++i) ret += r >= b[x - a[i]] && b[x - a[i]] > p; else for (int i = p + 1, _c = r; i <= _c; ++i) ret += l <= b[x - a[i]] && b[x - a[i]] < p; dnc(l, p - 1); dnc(p + 1, r); } int main() { n = ({ int x = 0; int c = getchar(), n = 0; for (; !isdigit(c); c = getchar()) n = (c == - ); for (; isdigit(c); c = getchar()) x = x * 10 + c - 0 ; n ? -x : x; }); for (int i = 1, _c = n; i <= _c; ++i) a[i] = f[i][0] = ({ int x = 0; int c = getchar(), n = 0; for (; !isdigit(c); c = getchar()) n = (c == - ); for (; isdigit(c); c = getchar()) x = x * 10 + c - 0 ; n ? -x : x; }), b[a[i]] = i, lz[i] = log2(i); for (int j = 1, _c = 20; j <= _c; ++j) for (int i = 1, _c = n; i <= _c; ++i) { if (i + (1 << j - 1) > n) break; f[i][j] = max(f[i][j - 1], f[i + (1 << j - 1)][j - 1]); } dnc(1, n); cout << ret; }
#include <bits/stdc++.h> using namespace std; int arr[3010][3010], height[3010][3010]; deque<pair<int, int> > mdq; void push(int val, int pos) { while (!mdq.empty() && mdq.back().first > val) mdq.pop_back(); mdq.push_back(make_pair(val, pos)); } int front(int lim) { while (!mdq.empty() && mdq.front().second < lim) mdq.pop_front(); return mdq.front().first; } void clear() { mdq.clear(); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int row, column, r1, c1; long long int g0, x, y, z; cin >> row >> column >> r1 >> c1; cin >> g0 >> x >> y >> z; long long int g = g0; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { arr[i][j] = g; g = (g * x + y) % z; } } for (int i = 0; i < row; i++) { clear(); for (int j = 0; j < c1; j++) push(arr[i][j], j); for (int j = 0; j + c1 <= column; j++) { height[i][j] = front(j); if (j + c1 < column) push(arr[i][j + c1], j + c1); } } for (int j = 0; j + c1 <= column; j++) { clear(); for (int i = 0; i < r1; i++) push(height[i][j], i); for (int i = 0; i + r1 <= row; i++) { height[i][j] = front(i); if (i + r1 < row) push(height[i + r1][j], i + r1); } } long long int ans = 0; for (int i = 0; i + r1 <= row; i++) for (int j = 0; j + c1 <= column; j++) ans += height[i][j]; cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * f; } double ans; int n, cnt, C; int head, tail; int q[3005], d[3005], d2[3005]; bool vis[3005]; vector<int> e[3005]; void dfs(int x) { vis[x] = 1; for (int i = 0; i < e[x].size(); i++) if (!vis[e[x][i]]) { d2[e[x][i]] = d2[x] + 1; if (!d[e[x][i]]) { d[e[x][i]] = d[x] + 1; ans += 1.0 / d[e[x][i]]; } else ans = ans + 1.0 / d2[e[x][i]] - 2.0 / (d[e[x][i]] + d2[e[x][i]] + C - 2); dfs(e[x][i]); } vis[x] = 0; } int main() { n = read(); for (int i = 1; i <= n; i++) { int u = read(), v = read(); u++; v++; e[u].push_back(v); e[v].push_back(u); d[u]++; d[v]++; } for (int i = 1; i <= n; i++) if (d[i] == 1) q[tail++] = i; while (head != tail) { int now = q[head]; head++; for (int i = 0; i < e[now].size(); i++) { d[e[now][i]]--; if (d[e[now][i]] == 1) q[tail++] = e[now][i]; } } C = n - tail; for (int i = 1; i <= n; i++) { memset(d, 0, sizeof(d)); memset(d2, 0, sizeof(d2)); d[i] = d2[i] = 1; dfs(i); } printf( %.10lf n , ans + n); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T& num) { bool start = false, neg = false; char c; num = 0; while ((c = getchar()) != EOF) { if (c == - ) start = neg = true; else if (c >= 0 && c <= 9 ) { start = true; num = num * 10 + c - 0 ; } else if (start) break; } if (neg) num = -num; } inline int Q(int x, int y) { printf( 0 %d %d n , x, y); fflush(stdout); scanf( %d , &x); return x; } set<int> c; void dfs(int l, int r) { if (l > r) return; int mid = l + (r - l) / 2; int d = Q(mid, mid); if (!d) c.insert(mid), d++; dfs(l, mid - d); dfs(mid + d, r); } set<int> V, H; int main() { int n = 0, m = 0; int mx = (int)(1e8); dfs(-mx, mx); int s = mx; for (; c.count(s); s--) ; for (set<int>::iterator it = c.begin(); it != c.end(); it++) { int x = (*it); if (!Q(x, s)) V.insert(x), ++n; if (!Q(s, x)) H.insert(x), ++m; } printf( 1 %d %d n , n, m); for (set<int>::iterator it = V.begin(); it != V.end(); it++) printf( %d , (*it)); putchar( n ); for (set<int>::iterator it = H.begin(); it != H.end(); it++) printf( %d , (*it)); putchar( n ); fflush(stdout); return 0; }
#include <bits/stdc++.h> using namespace std; int a[1000000 + 5]; int b[1000000 + 5]; int main() { ios_base::sync_with_stdio(false); int i, j, n, k; queue<pair<int, int> > q; cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; for (i = 1; i <= n; i++) cin >> b[i]; for (i = 1; i <= n; i++) { if (a[i] != b[i]) { for (j = i + 1; j <= n; j++) { if (a[i] == b[j]) { k = j; break; } } for (j = k; j > i; j--) { q.push(make_pair(j - 1, j)); int t = b[j]; b[j] = b[j - 1]; b[j - 1] = t; } } } cout << q.size() << endl; while (q.empty() != 1) { cout << q.front().first << << q.front().second << endl; q.pop(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int bignumlen = 2200; const int Blen = 8; const long long base = 100000000; struct bignum { int len; long long data[bignumlen]; long long &operator[](int x) { return (data[x]); } const long long &operator[](int x) const { return (data[x]); } bignum() { memset(data, 0, sizeof(data)); len = 0; } void clear() { for (int i = len; i >= 1; --i) data[i] = 0; len = 0; } int check(const bignum &a, const bignum &b) { if (a.len > b.len) return (0); if (b.len > a.len) return (1); for (int i = a.len; i >= 1; --i) { if (a.data[i] < b.data[i]) return (1); if (b.data[i] < a.data[i]) return (0); } return 2; } bool operator<(const bignum &b) { return (check(*this, b) == 1); } bool operator>(const bignum &b) { return (check(*this, b) == 0); } bool operator<=(const bignum &b) { return (check(*this, b) >= 1); } bool operator>=(const bignum &b) { return (check(*this, b) % 2 == 0); } bool operator!=(const bignum &b) { return (check(*this, b) != 2); } bool operator==(const bignum &b) { return (check(*this, b) == 2); } bignum operator=(const bignum &x) { for (int i = x.len + 1; i <= len; ++i) data[i] = 0; for (int i = 1; i <= x.len; ++i) data[i] = x.data[i]; len = x.len; return *this; } bignum operator=(long long x) { for (int i = len; i >= 0; --i) data[i] = 0; len = 0; while (x) { data[++len] = x % base; x /= base; } return *this; } bignum(long long x) { memset(data, 0, sizeof(data)); len = 0; (*this) = x; } bignum operator*(const bignum &b) { int i, j; bignum tmp; for (i = 1; i <= len; ++i) if (data[i] != 0) for (j = 1; j <= b.len; ++j) if (b.data[j] != 0) { tmp.data[i + j - 1] += data[i] * b.data[j]; tmp.data[i + j] += tmp.data[i + j - 1] / base; tmp.data[i + j - 1] %= base; } tmp.len = len + b.len - 1; while (tmp.data[tmp.len + 1]) tmp.len++; return tmp; } bignum operator*(long long x) { int i; bignum tmp; for (i = 1; i <= len; ++i) tmp[i] = data[i] * x; tmp.len = len; for (i = 1; i <= len; ++i) { tmp[i + 1] += tmp[i] / base, tmp[i] %= base; if (tmp[i + 1] && i + 1 > tmp.len) tmp.len++; } return tmp; } bignum operator/(long long x) { int i; bignum tmp; long long y = 0; for (i = len; i >= 1; --i) { y = y * base + data[i]; tmp[i] = y / x; y %= x; } tmp.len = len; while (tmp[tmp.len] == 0 && tmp.len > 1) tmp.len--; return tmp; } bignum operator/(const bignum &b) { if (b.len <= 1 && b[1] == 0) { printf( error! ?0?! ); for (;;) ; } int i, l1 = (len - 1) * Blen, l2 = (b.len - 1) * Blen; long long x = data[len], y = b[b.len]; while (x) x /= 10, l1++; while (y) y /= 10, l2++; bignum tmp, chu, B; chu = *this; B = b; for (i = 1; i * Blen <= l1 - l2; ++i) B *= base; for (i = 1; i <= (l1 - l2) % Blen; ++i) B *= 10; for (i = l1 - l2; i >= 0; --i) { x = 0; while (chu >= B) chu -= B, x++; tmp[i / Blen + 1] = tmp[i / Blen + 1] * 10 + x; B /= 10; } tmp.len = (l1 - l2) / Blen + 1; while (tmp.len >= 1 && !tmp[tmp.len]) tmp.len--; return tmp; } bignum operator+(const bignum &b) { bignum tmp; int i, l = max(len, b.len); for (i = 1; i <= l; ++i) tmp[i] = data[i] + b[i]; for (i = 1; i <= l; ++i) tmp[i + 1] += tmp[i] / base, tmp[i] %= base; tmp.len = l; if (tmp[tmp.len + 1]) tmp.len++; return tmp; } bignum operator+(long long x) { bignum tmp; tmp = *this; tmp[1] += x; for (int i = 1; i <= len && tmp[i] >= base; ++i) tmp[i + 1] += tmp[i] / base, tmp[i] %= base; while (tmp[tmp.len + 1]) tmp.len++; return tmp; } bignum operator-(const bignum &b) { int i; bignum tmp; for (i = 1; i <= len; ++i) tmp.data[i] = data[i] - b.data[i]; for (i = 1; i <= len; ++i) { if (tmp[i] < 0) tmp.data[i] += base, tmp.data[i + 1]--; } tmp.len = len; while (tmp[tmp.len] == 0 && tmp.len > 1) tmp.len--; return tmp; } bignum operator-(long long x) { bignum tmp; tmp = *this; tmp[1] -= x; for (int i = 1; i <= len && tmp[i] < 0; ++i) { tmp[i + 1] += (tmp[i] + 1) / base - 1; tmp[i] = (tmp[i] + 1) % base + base - 1; } while (!tmp[tmp.len] && tmp.len > 1) tmp.len--; return tmp; } long long operator%(long long x) { int i; long long y = 0; for (i = len; i >= 1; --i) y = (y * base + data[i]) % x; return y; } bignum operator%(const bignum &b) { if (b.len <= 1 && b[1] == 0) { printf( error! ?0 mod! ); for (;;) ; } int i, l1 = (len - 1) * Blen, l2 = (b.len - 1) * Blen; long long x = data[len], y = b[b.len]; while (x) x /= 10, l1++; while (y) y /= 10, l2++; bignum chu, B; chu = *this; B = b; for (i = 1; i * Blen <= l1 - l2; ++i) B *= base; for (i = 1; i <= (l1 - l2) % Blen; ++i) B *= 10; for (i = l1 - l2; i >= 0; --i) { while (chu >= B) chu -= B; B /= 10; } return chu; } bignum operator+=(const bignum &b) { return *this = (*this + b); } bignum operator*=(const bignum &b) { return *this = (*this * b); } bignum operator-=(const bignum &b) { return *this = (*this - b); } bignum operator/=(const bignum &b) { return *this = (*this / b); } bignum operator%=(const bignum &b) { return *this = (*this % b); } bignum operator*=(long long x) { return (*this = (*this * x)); } bignum operator+=(long long x) { return (*this = (*this + x)); } bignum operator-=(long long x) { return (*this = (*this - x)); } bignum operator/=(long long x) { return (*this = (*this / x)); } void read() { char c[bignumlen * Blen + 10]; scanf( %s , c + 1); int l = strlen(c + 1); (*this).clear(); long long x; for (int i = 1; i <= (l - 1) / Blen + 1; ++i) { x = 0; for (int j = l - Blen * i + 1; j <= l - Blen * i + Blen; ++j) if (j >= 1) x = x * 10 + c[j] - 48; data[++len] = x; } } void write() { printf( %lld , data[len]); for (int i = len - 1; i >= 1; --i) printf( %0*I64d , Blen, data[i]); } } p, q, pp, qq; bignum gcd(const bignum &A, const bignum &B) { bignum a = A, b = B, res = 1; while (!(a[1] & 1) && !(b[1] & 1)) a /= 2, b /= 2, res *= 2; for (;;) { if (a.len == 1 && a[1] == 0) return b * res; if (b.len == 1 && b[1] == 0) return a * res; while (!(a[1] & 1)) a /= 2; while (!(b[1] & 1)) b /= 2; if (a > b) a -= b; else b -= a; } } void YF(bignum &A, bignum &B) { bignum tmp = gcd(A, B); A /= tmp; B /= tmp; } int n, i, x, num; int main() { scanf( %d , &n); while (n % 2 == 0) { n /= 2; num++; } if (n == 1) { printf( %d/%d n , num, 1); return 0; } p = 0; q = 1; pp = 1; qq = 1; for (i = n + 1;;) { if (i > n) { if (q % (i - n) == 0) q /= i - n; else p *= i - n; if (qq % (i - n) == 0) qq /= i - n; else pp *= i - n; if (p % i == 0) p /= i; else q *= i; if (pp % i == 0) pp /= i; else qq *= i; } else p += q; if (i == 1) break; if (i % 2 == 0) i /= 2; else i += n; } YF(p, q); p = p * qq; q = q * (qq - pp); p += q * num; bignum tmp = gcd(p, q); (p / tmp).write(); printf( / ); (q / tmp).write(); }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10, MSQ = 1010, SQ = 10, inf = 2e9; pair<pair<int, int>, int> p[maxn]; map<int, vector<int> > mp; vector<int> vec, vec2; int n, k, sm[maxn], l[maxn], r[maxn]; long long ans; void add(int ind, int val) { while (ind < maxn) { sm[ind] += val; ind += (ind & -ind); } } int ask(int ind) { int ans = 0; while (ind > 0) { ans += sm[ind]; ind -= (ind & -ind); } return ans; } void Add(int id, int val) { if (val == 1) ans += ask(r[id]) - ask(l[id]); add(id + 1, val); } bool CMP(int a, int b) { return p[a].first.second > p[b].first.second; } void Add(vector<int>& vec) { sort(vec.begin(), vec.end(), CMP); for (int id : vec) Add(id, 1); for (int id : vec) Add(id, -1); } int main() { scanf( %d%d , &n, &k); for (int i = 0; i < n; i++) { scanf( %d%d%d , &p[i].first.first, &p[i].first.second, &p[i].second); vec.push_back(p[i].second); } sort(p, p + n); for (int i = 0; i < n; i++) { pair<pair<int, int>, int> PP1 = {{p[i].first.first - p[i].first.second, -1}, -1}, PP2 = { {p[i].first.first + p[i].first.second, inf}, inf}; l[i] = lower_bound(p, p + n, PP1) - p; r[i] = upper_bound(p, p + n, PP2) - p; } for (int i = 0; i < n; i++) { mp[p[i].second].push_back(i); } sort(vec.begin(), vec.end()); vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); for (int i = 0; i < int((vec).size()); i++) { long long bf = ans; Add(mp[vec[i]]); vec2.push_back(ans - bf); } for (int i = 0; i < int((vec).size()); i++) { for (int j = i + 1; j < int((vec).size()) && vec[j] - vec[i] <= k; j++) { vector<int> vv(int((mp[vec[i]]).size()) + int((mp[vec[j]]).size())), v1 = mp[vec[i]], v2 = mp[vec[j]]; merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vv.begin()); Add(vv); ans -= vec2[i] + vec2[j]; } } printf( %lld n , ans); }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long x, y, p, q; cin >> x >> y >> p >> q; if (p == q && x == y) { printf( 0 n ); continue; } if (p == 0 && x == 0) { printf( 0 n ); continue; } if (p == q || p == 0 || q == 0) { printf( -1 n ); continue; } long long i = max( (x - p * ((y + q - 1) / q) + p - 1) / p, (p * ((y + q - 1) / q) - (x + ((y + q - 1) / q) * q - y) + q - p - 1) / (q - p)); printf( %lld n , i * q + ((y + q - 1) / q) * q - y); } return 0; }
#include <bits/stdc++.h> using namespace std; const double sq = 1.73205080757; int main() { long long k, k1; cin >> k; k1 = k * 2; k *= 2; k1 -= k1 % 3; k1++; if (k1 > k) k1 -= 3; long long h = 1, h1, an = 0; while (k1 > 0) { while (k1 * k1 + 3LL * h * h <= k * k && (k1 + 1) * (k1 + 1) + 3LL * (h - 1) * (h - 1) <= k * k) h++; h--; if ((k1 - 1) % 6 == 0) h1 = max(h - (h % 2 == 0), 0LL); else h1 = h - h % 2; an += h1; k1 -= 3; } cout << an * 2 - h1 << endl; }
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { fast(); long long int t = 1; while (t--) { long long int n, m; cin >> n >> m; long long int a[n][m], b[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> b[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] > b[i][j]) { a[i][j] = b[i][j] + a[i][j]; b[i][j] = a[i][j] - b[i][j]; a[i][j] = a[i][j] - b[i][j]; } } } bool ans = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m - 1; j++) { if (a[i][j] >= a[i][j + 1]) { ans = 0; break; } if (b[i][j] >= b[i][j + 1]) { ans = 0; break; } } } for (int i = 0; i < n - 1; i++) { for (int j = 0; j < m; j++) { if (a[i][j] >= a[i + 1][j]) { ans = 0; break; } if (b[i][j] >= b[i + 1][j]) { ans = 0; break; } } } if (!ans) cout << Impossible ; else cout << Possible ; } }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int inf = 0x3f3f3f3f; const double eps = 1e-10; const long long INF = (1LL << 60); int main() { int a, b; scanf( %d%d , &a, &b); if (a == b) { printf( %d n , a); return 0; } double ans = -1; if (a >= b) { int k = (int)floor((a * 1.0 / b - 1) / 2); ans = (a - b) * 1.0 / (2 * k); ans = min(ans, (a + b) * 1.0 / (2 * k + 2)); } printf( %0.12f n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int a[100000 + 10]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { long long k; cin >> k; for (int i = 1; i <= k; i++) cin >> a[i]; sort(a + 1, a + k + 1); int ans = 1; for (int i = 1; i < k; i++) { if (a[i] != a[i + 1]) { ans++; continue; } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); long long n, m, k; cin >> n >> m >> k; long long x, s; cin >> x >> s; pair<long long, long long> a[m]; for (int i = 0; i < m; i++) cin >> a[i].second; for (int i = 0; i < m; i++) cin >> a[i].first; sort(a, a + m); pair<long long, long long> c[k]; for (int i = 0; i < k; i++) cin >> c[i].first; for (int i = 0; i < k; i++) cin >> c[i].second; long long mn[m]; mn[0] = a[0].second; for (int i = 1; i < m; i++) { mn[i] = min(mn[i - 1], a[i].second); } long long ans = n * x; for (int i = 0; i < m; i++) { if (a[i].first <= s) ans = min(ans, n * a[i].second); } for (int i = 0; i < k; i++) { if (c[i].second > s) break; int t = c[i].first; int k = upper_bound(a, a + m, pair<long long, long long>(s - c[i].second, 1LL << 60)) - a; k--; if (k >= 0) { ans = min(ans, (n - t) * min(x, mn[k])); } ans = min(ans, (n - t) * x); } ans = max(ans, 0LL); printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> const int MAXN = 2e5 + 10; using namespace std; int T, N, cmp; int p[MAXN], c[MAXN]; vector<int> comp[MAXN]; bool vis[MAXN]; void dfs(int x) { vis[x] = true; comp[cmp].push_back(c[x]); if (!vis[p[x]]) dfs(p[x]); } int min_ans(int idx_comp) { int n_comp = (int)(comp[idx_comp].size()); vector<int> div; for (long long i = 1; i * i <= 1LL * n_comp; i++) if (n_comp % i == 0) { div.push_back((int)i); if (i * i != 1LL * n_comp) div.push_back((int)(n_comp / i)); } sort(div.begin(), div.end()); for (int d : div) for (int i = 0; i < d; i++) { int g = (i + d) % n_comp; bool found = true; while (g != i) { if (comp[idx_comp][g] != comp[idx_comp][i]) found = false; g = (g + d) % n_comp; } if (found) return d; } } inline void init() { cmp = 0; for (int i = 1; i < N + 1; i++) { vis[i] = false; comp[i].clear(); } } int main() { scanf( %d , &T); while (T--) { scanf( %d , &N); init(); for (int i = 1; i < N + 1; i++) scanf( %d , &p[i]); for (int i = 1; i < N + 1; i++) scanf( %d , &c[i]); for (int i = 1; i < N + 1; i++) if (!vis[i]) { cmp++; dfs(i); } int mn = N; for (int i = 1; i < cmp + 1; i++) mn = min(mn, min_ans(i)); printf( %d n , mn); } }
#include <bits/stdc++.h> using namespace std; bool st[1000000]; int main() { int N, K; cin >> N >> K; for (int i = 0; i < 1000000; i++) st[i] = false; int cnt = 0; for (int i = 0; i < N; i++) { int te1, te2; cin >> te1 >> te2; te1 += 100000, te2 += 100000; for (int j = te1; j <= te2; j++) { if (!st[j]) { st[j] = true; cnt++; } } } cout << ((cnt + K - 1) / K) * K - cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxint = -1u >> 1; template <class T> bool get_max(T& a, const T& b) { return b > a ? a = b, 1 : 0; } template <class T> bool get_min(T& a, const T& b) { return b < a ? a = b, 1 : 0; } int main() { int f[21]; int a, b, c; cin >> a >> b >> c; f[0] = a; f[1] = b; for (int i = 2; i < 21; i++) f[i] = f[i - 1] + f[i - 2]; cout << f[c] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct edge { int u; int v; long long cost; edge() {} edge(int u, int v, long long c) : u(u), v(v), cost(c) {} }; long long comb2(long long n) { return (n * (n - 1)) / 2; } const int MAXN = 100100; int n; vector<int> G[MAXN]; edge es[MAXN]; int childNum[MAXN]; long long usedNum[MAXN]; int dfs(int v, int par) { if (childNum[v] >= 0) return childNum[v]; int ret = 1; for (int i = 0; i < (int)G[v].size(); i++) { int next = G[v][i]; if (next != par) ret += dfs(next, v); } return childNum[v] = ret; } int main(void) { cin >> n; for (int i = 0; i < n - 1; i++) { cin >> es[i].u >> es[i].v >> es[i].cost; G[es[i].u].push_back(es[i].v); G[es[i].v].push_back(es[i].u); } for (int i = 1; i <= n; i++) childNum[i] = -1; dfs(1, 0); double sum = 0; for (int i = 0; i < n - 1; i++) { int x = min(childNum[es[i].u], childNum[es[i].v]); usedNum[i] = 2 * (x * comb2(n - x) + (n - x) * comb2(x)); sum += (double)usedNum[i] * (double)es[i].cost; } int q; long long div = (long long)n * (long long)(n - 1) * (long long)(n - 2) / 6; cin >> q; while (q--) { int id; long long cost; cin >> id >> cost; id--; long long diff = es[id].cost - cost; es[id].cost = cost; sum -= (double)diff * (double)usedNum[id]; printf( %.10lf n , sum / (double)div); } return 0; }
#include <bits/stdc++.h> const int N = 5e5, mo = 1e6 + 3; int n, m, i, j, ans; bool h[N][2], l[N][2]; char s[N]; int main() { scanf( %d%d , &n, &m); ans = 1; for (i = 0; i < n; i++) for (scanf( %s , s), j = 0; j < m; j++) if (s[j] != . ) { int x = s[j] - 48; h[i][(x <= 2) ^ (j & 1)] = 1; l[j][(x == 1 || x == 4) ^ (i & 1)] = 1; } for (i = 0; i < n; i++) ans = ans * (2 - h[i][0] - h[i][1]) % mo; for (j = 0; j < m; j++) ans = ans * (2 - l[j][0] - l[j][1]) % mo; printf( %d , ans); }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int t = n % 4; int a = 0; for (int i = 0; i < 4; i++) { if ((i * 7) % 4 == t) a = i; } int b = -1, c; for (int j = (n - a) / 4 * 4 + a; j >= a; j -= 4) { if ((n - 7 * j) % 4 == 0 && (n - 7 * j >= 0)) { c = j; b = (n - 7 * j) / 4; break; } } if (b == -1) { cout << -1; return 0; } for (int i = 0; i < b; i++) cout << 4; for (int i = 0; i < c; i++) cout << 7; return 0; }
#include <bits/stdc++.h> using namespace std; class Solution { public: void solve(vector<pair<int, int>>& edges, vector<int>& xs, vector<int>& ys, int n) { vector<vector<int>> graph(n + 1); for (auto& p : edges) { int u = p.first, v = p.second; graph[u].push_back(v); graph[v].push_back(u); } cout << B << ys[0] << endl; fflush(stdout); int root = -1; cin >> root; vector<int> dis(n + 1, INT_MAX); dis[root] = 0; queue<int> q; q.push(root); while (!q.empty()) { auto u = q.front(); q.pop(); int d = dis[u]; for (auto v : graph[u]) { if (dis[v] > d + 1) { dis[v] = d + 1; q.push(v); } } } int x = -1; for (auto v : xs) { if (x == -1 || dis[x] > dis[v]) { x = v; } } cout << A << x << endl; fflush(stdout); int y = -1; cin >> y; bool found = false; for (auto v : ys) { if (v == y) { found = true; break; } } if (found) { cout << C << x << endl; } else { cout << C << -1 << endl; } fflush(stdout); return; } }; int main(int argc, char** argv) { ios::sync_with_stdio(false); cin.tie(0); Solution sol; int t; cin >> t; while (t-- > 0) { int n; cin >> n; vector<pair<int, int>> edges; edges.reserve(n - 1); for (int i = 0, u = 0, v = 0; i < n - 1; ++i) { cin >> u >> v; edges.emplace_back(u, v); } int k; cin >> k; vector<int> xs(k); for (int i = 0; i < k; ++i) { cin >> xs[i]; } cin >> k; vector<int> ys(k); for (int i = 0; i < k; ++i) { cin >> ys[i]; } sol.solve(edges, xs, ys, n); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000 * 1000 * 1000 + 9; const int MAX_N = 1000 * 1000 + 10; long long dp1[MAX_N]; int main() { int n; scanf( %d , &n); n /= 2; if (n == 1) { printf( %d n , 10); return 0; } dp1[1] = 5; for (int i = 2; i <= n - 2; i++) dp1[i] = (3 + 2 * dp1[i - 1]) % MOD; long long dp2 = 1; for (int i = 0; i < (n - 2); i++) dp2 = (1 + dp1[n - 2 - i] * (3 + dp2)) % MOD; dp2 += 5; dp2 = 1 + dp2 * dp2; dp2 *= 2; dp2 %= MOD; printf( %d n , (int)dp2); return 0; }
#include <bits/stdc++.h> int a[100100]; int f[100100][3]; int s[100100]; char tmp[100100]; int n; void maximize(int &x, const int &y) { if (x < y) x = y; } void swap(int &a, int &b) { int sw; sw = a; a = b; b = sw; } void init(void) { scanf( %d , &n); int i; for (i = 1; i <= n; i = i + 1) scanf( %d , &a[i]); scanf( %s , tmp); for (i = 0; i < n; i = i + 1) s[i + 1] = tmp[i] - 48; for (i = 1; i <= n; i = i + 1) { if (i >= n + 1 - i) break; swap(a[i], a[n + 1 - i]); swap(s[i], s[n + 1 - i]); } } void optimize(void) { int i, j, k; memset(f, -1, sizeof f); f[0][0] = 0; for (i = 0; i < n; i = i + 1) for (j = 0; j < 2; j = j + 1) if (f[i][j] >= 0) for (k = 0; k < 2; k = k + 1) if (j || (k <= s[i + 1])) maximize(f[i + 1][j || (k < s[i + 1])], f[i][j] + a[i + 1] * k); if (f[n][0] > f[n][1]) printf( %d , f[n][0]); else printf( %d , f[n][1]); } int main(void) { init(); optimize(); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e9 + 9; const long long N = 500500; char s[4][4]; long long dot, cross, nought, first_wins, second_wins; long long first_win_rows, first_win_col, second_win_row, second_win_col; long long total; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); for (long long i = 1; i <= 3; i++) { for (long long j = 1; j <= 3; j++) { cin >> s[i][j]; if (s[i][j] == . ) dot++; if (s[i][j] == X ) cross++; if (s[i][j] == 0 ) nought++; } } total += cross + nought; if (!(cross == nought || cross == nought + 1)) { cout << illegal ; return 0; } for (long long i = 1; i <= 3; i++) { set<char> st; for (long long j = 1; j <= 3; j++) { st.insert(s[i][j]); } if (st.size() == 1 && *st.begin() == X ) { first_wins++; } if (st.size() == 1 && *st.begin() == 0 ) { second_wins++; } } for (long long j = 1; j <= 3; j++) { set<char> st; for (long long i = 1; i <= 3; i++) { st.insert(s[i][j]); } if (st.size() == 1 && *st.begin() == X ) { first_wins++; } if (st.size() == 1 && *st.begin() == 0 ) { second_wins++; } } set<char> st; st.insert(s[1][1]); st.insert(s[2][2]); st.insert(s[3][3]); if (st.size() == 1 && *st.begin() == X ) { first_wins++; } if (st.size() == 1 && *st.begin() == 0 ) { second_wins++; } st.clear(); st.insert(s[1][3]); st.insert(s[2][2]); st.insert(s[3][1]); if (st.size() == 1 && *st.begin() == X ) { first_wins++; } if (st.size() == 1 && *st.begin() == 0 ) { second_wins++; } if (first_wins > 0 && second_wins > 0) { cout << illegal ; return 0; } if (first_wins > 0 || second_wins > 0) { if (first_wins == 1 && total & 1) { cout << the first player won ; } else if (second_wins == 1 && total % 2 == 0) { cout << the second player won ; } else if (first_wins == 2 && total & 1) { if (first_win_col == 2 || first_win_rows == 2) { cout << illegal ; } else { cout << the first player won ; } } else if (second_wins == 2 && total % 2 == 0) { if (second_win_col == 2 || second_win_row == 2) { cout << illegal ; } else { cout << the second player won ; } } else { cout << illegal ; } return 0; } if (dot > 0) { if (cross == nought) cout << first ; else cout << second ; } else { cout << draw ; } return 0; }
#include <bits/stdc++.h> using namespace std; int n; int mx = 0, sum = 0; int main() { cin >> n; for (int i = 1; i <= n; ++i) { int a; cin >> a; mx = max(mx, a); sum += a; } sum *= 2; sum += n; sum /= n; cout << max(sum, mx) << endl; return 0; }
/* Author : Prakhar Rai */ #include<bits/stdc++.h> #define ll long long #define ld long double #define LB(x,num) lower_bound(x.begin(),x.end(),num) - x.begin() #define UB(x,num) upper_bound(x.begin(),x.end(),num) - x.begin() #define BS(x,num) binary_search(x.begin(),x.end(),num) #define pb push_back #define mp make_pair #define fs first #define sc second #define vci vector<int> #define vcll vector<ll> #define vcd vector<long double> #define line(x) sort(x.begin(),x.end()) #define all(x) x.begin(),x.end() #define newl n #define vc vector #define loop(i,a,b) for(int i = a; i < b; i++) #define lol(i,a,b) for(ll i = a; i < b; i++) #define lod(i,a,b) for(ld i = 0; i < b; i++) #define mod 1000000007 #define read(v,n) lol(i,0,n) {ll x; cin >> x; v.pb(x);} #define run(a,x) for(auto x : a) #define yes YES #define no NO ll cnt; using namespace std; // ll dp[10000000]; // memset(dp, -1, sizeof(dp)); void solve() { ll n; cin >> n; vcll v; read(v, n + 2); sort(all(v)); ll sum = 0; lol(i, 0, n) { sum += v[i]; } if (sum == v[n]) { lol(i, 0, n) { cout << v[i] << ; } cout << newl; return; } sum += v[n]; lol(i, 0, n + 1) { if (sum - v[i] == v[n + 1]) { lol(j, 0, n + 1) { if (i == j) { continue; } else { cout << v[j] << ; } } cout << newl; return; } } cout << -1 << newl; return; } // FILE WITH TEST CASE void init() { #ifndef ONLINE_JUDGGE freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); #endif } int main() { // init(); ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) solve(); } /* This is my journey and i shall endure the work to reach the top .*/
#include <bits/stdc++.h> using namespace std; int play(int n) { int x = 1; for (;;) { if (x * 2 > n) break; else x *= 2; } return x / 2; } int main() { int n, b, p, k, x = 1, btl = 0, twl = 0; scanf( %d %d %d , &n, &b, &p); twl = n * p; while (1) { int pl = play(n); btl += (2 * b + 1) * pl; n = pl + n - pl * 2; if (n == 1) break; } cout << btl << << twl << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; string line; char table[h + 2][w + 2]; memset(table, 0 , sizeof table); for (int i = 1; i <= h; i++) { cin >> line; line = + line; for (int j = 1; j <= w; j++) { table[i][j] = line[j]; } } int dph[h + 3][w + 3]; int dpv[h + 3][w + 3]; int dp[h + 3][w + 3]; int c = 0; memset(dp, 0, sizeof dp); memset(dpv, 0, sizeof dpv); memset(dph, 0, sizeof dph); for (int y = 1; y <= h; y++) { for (int x = 1; x <= w; x++) { c = 0; if (table[y][x] == . ) { if (table[y + 1][x] == . && y != h) { c += 1; dpv[y][x] = 1; } if (table[y][x + 1] == . && x != w) { dph[y][x] = 1; c += 1; } } dp[y][x] = dp[y][x - 1] + c; } } int tt; cin >> tt; while (tt--) { int y1, x1, y2, x2, x, y, ans = 0; cin >> y1 >> x1 >> y2 >> x2; for (int y = y1; y < y2; y++) { ans += dp[y][x2 - 1] - dp[y][x1 - 1]; } for (int x = x1; x < x2; x++) { ans += dph[y2][x]; } for (int y = y1; y < y2; y++) { ans += dpv[y][x2]; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename _T, typename _T2> string to_string(pair<_T, _T2> x) { return < + to_string(x.first) + , + to_string(x.second) + > ; } template <typename _T> string to_string(vector<_T> vec) { string s = [ ; for (auto el : vec) s += to_string(el) + , ; s[s.size() - 1] = ] ; s[s.size() - 2] = ; return s; } template <typename _T> string to_string(set<_T> vec) { string s = { ; for (auto el : vec) s += to_string(el) + , ; s[s.size() - 1] = } ; s[s.size() - 2] = ; return s; } template <typename _T, typename _T2> string to_string(map<_T, _T2> vec) { string s = { ; for (auto el : vec) s += mp[ + to_string(el.first) + ]= + to_string(el.second) + , ; s[s.size() - 1] = } ; s[s.size() - 2] = ; return s; } template <typename _T> inline void _DBG(const char *s, _T x) { cerr << s << = << to_string(x) << n ; } template <typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while (*s != , ) cerr << *s++; cerr << = << to_string(x) << n ; _DBG(s + 1, a...); } mt19937 rng___(chrono::steady_clock::now().time_since_epoch().count()); const long long MOD = 998244353; const long long LLINF = 1e18 + 7; long long d, m; long long powe(long long n, long long k) { if (k == 0) return 1; long long pom = powe(n, k / 2); pom = (pom * pom) % m; if (k & 1) pom = (pom * n) % m; return pom; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ; int t; cin >> t; while (t--) { cin >> d >> m; long long dp[32][32]; for (int i = 0; i < 32; i++) for (int j = 0; j < 32; j++) dp[i][j] = 0; dp[0][0] = 1; long long ans = 0; for (int i = 1; i < 32; i++) { for (int j = 1; j < 32; j++) { int b = j - 1; if ((1 << b) > d) break; long long summ = 0; for (int k = 0; k < j; k++) { summ += dp[i - 1][k]; } summ = summ % m; dp[i][j] = (summ * 1LL * min(d - (1 << b) + 1, 1LL << b)) % m; ans = (ans + dp[i][j]) % m; } } cout << ans << n ; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long s[100005]{0}; pair<long long, long long> p[100005]; for (int i = 0; i < m; i++) { int u, v; long long d; cin >> u >> v >> d; u--; v--; s[u] -= d; s[v] += d; } for (int i = 0; i < n; i++) p[i] = pair<long long, long long>(s[i], i); sort(p, p + n); vector<int> u, v; vector<long long> d; int i = 0; int j = n - 1; while (i < j) { if (p[i].first + p[j].first < 0) { u.push_back(p[i].second); v.push_back(p[j].second); d.push_back(p[j].first); p[i].first += p[j].first; p[j].first = 0; j--; } else if (p[i].first + p[j].first > 0) { u.push_back(p[i].second); v.push_back(p[j].second); d.push_back(-p[i].first); p[j].first += p[i].first; p[i].first = 0; i++; } else { if (p[i].first == 0) { i++; j--; continue; } u.push_back(p[i].second); v.push_back(p[j].second); d.push_back(p[j].first); p[i].first = 0; p[j].first = 0; i++; j--; } } int l = u.size(); cout << l << endl; for (int i = 0; i < l; i++) cout << u[i] + 1 << << v[i] + 1 << << d[i] << endl; }
#include <bits/stdc++.h> using namespace std; inline void read(int &res) { register char ch = getchar(); res = 0; register int g = 1; while (!isdigit(ch)) { if (ch == - ) g = -1; ch = getchar(); } while (isdigit(ch)) { res = (res << 3) + (res << 1) + (ch ^ 48); ch = getchar(); } res *= g; } priority_queue<int> q; int n, m, a[202000], ans[202000], sum = 0, t = 0; int main() { read(n); read(m); for (register int i = 1; i <= n; i++) { read(a[i]); } for (register int i = 1; i <= n; i++) { t = 0; sum = 0; while (!q.empty()) q.pop(); for (register int j = 1; j <= i - 1; j++) { t += a[j]; q.push(a[j]); } while (t + a[i] > m) { t -= q.top(); q.pop(); sum++; } printf( %d , sum); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, i; long long r, c = 1000000000000000000LL; void solve(int n, long long x) { for (int i = 1; i * i <= n; i++) if (!(n % i)) { r = max(r, (i + 2LL) * (n / i + 2LL) * x); c = min(c, (i + 2LL) * (n / i + 2LL) * x); } } int main() { scanf( %d , &n); for (int i = 1; i * i <= n; i++) if ((n % i) == 0) { solve(i, n / i + 1LL); solve(n / i, i + 1LL); } printf( %I64d %I64d n , c - n, r - n); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long i, t, n, m, c, d, a[100005]; cin >> n >> m; if (n == 1 && m == 0) { cout << 10000000; return 0; } if (m < n / 2 || n < 2) { cout << -1; return 0; } c = 1000000000LL; for (int i = n - 1; i > 1; i--) { a[i] = c--; } int tem; if (n % 2) m = m - (n - 3) / 2; else m = m - (n - 2) / 2; a[1] = m; a[0] = m * 2; for (__typeof(0) i = 0; i <= n - 1; i++) cout << a[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; map<string, string> p; int ans = 0; string tmp = abcdef ; void doit(int n, string s) { if (n == 0) { int f = 0; while (true) { if (s.length() == 2) { if (p.find(s) == p.end()) { f = 1; break; } if (p[s] != a ) { f = 1; } break; } string s1 = s.substr(0, 2); string s2 = ; if (s.length() > 2) { s2 += s.substr(2, s.length() - 2); } if (p.find(s1) == p.end()) { f = 1; break; } else { s = p[s1] + s2; } } if (f == 0) ans++; return; } for (int i = 0; i < 6; i++) { string tmp1 = s; tmp1.push_back(tmp[i]); doit(n - 1, tmp1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, q; cin >> n >> q; for (int i = 0; i < q; i++) { string s1, s2; cin >> s1 >> s2; p.insert(make_pair(s1, s2)); } for (int i = 0; i < 6; i++) { string s; s.push_back(tmp[i]); doit(n - 1, s); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; unsigned long long const Mod = 1000000007; unsigned long long sq_sum(unsigned long long n) { unsigned long long ret = n; ret = (n * (n + 1)); ret /= 2; if (ret % 3 == 0) { return ((ret / 3) % Mod * (2 * n + 1)) % Mod; } else { return ((ret % Mod) * ((2 * n + 1) / 3)) % Mod; } } unsigned long long check(unsigned long long a, unsigned long long b) { unsigned long long ret = 0; for (unsigned long long x = b; x < 100000; ++x) { if (x % b != 0) { unsigned long long t1 = x / b; unsigned long long t2 = x % b; if (t1 % t2 == 0 && t1 / t2 <= a) ret += x; } } return ret; } int main() { unsigned long long a, b; while (cin >> a >> b) { cout << (((a * (a + 1) / 2) % Mod * b + a) % Mod * ((b * (b - 1) / 2) % Mod)) % Mod << endl; } }
#include <bits/stdc++.h> using namespace std; long long segs[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; int n, m, k; long long a[200005]; vector<pair<int, int> > q; int res[200005]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { int r, t; cin >> r >> t; if (q.empty()) { q.push_back(make_pair(r, t - 1)); } else { while (!q.empty() && t - 1 >= q.back().second) { q.pop_back(); } q.push_back(make_pair(r, t - 1)); } } q.push_back(make_pair(1, 0)); int l = 0, r = n - 1, ind = n - 1; for (int i = 0; i < q.size(); i++) { if (i == q.size() - 1) { res[ind] = a[l]; break; } if (!i) { while (r > q[0].second) { res[r] = a[r]; r--; ind--; } sort(a, a + r + 1); } if (q[i].first == 1) { while (ind > q[i + 1].second) { res[ind] = a[r]; ind--; r--; } } else { while (ind > q[i + 1].second) { res[ind] = a[l]; ind--; l++; } } } for (int i = 0; i < n; i++) { if (i) cout << ; cout << res[i]; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long a, b, c; long long exgcd(long long a, long long b, long long& x, long long& y) { if (b == 0) { x = 1; y = 0; return a; } long long d = exgcd(b, a % b, x, y); long long z = x; x = y; y = z - y * (a / b); return d; } int main(int argc, char const* argv[]) { cin >> a >> b >> c; long long x, y; long long d = exgcd(a, b, x, y); if (c % d) { cout << -1 << endl; } else { x = -c / d * x; y = -c / d * y; cout << x << << y << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long t, cn, n, nr, p10; int main() { cin >> t; while (t--) { cin >> n; cn = n; nr = 0; p10 = 1; while (cn) { if (cn % 10) nr++; cn /= 10; } cout << nr << n ; while (n) { if (n % 10) cout << n % 10 * p10 << ; n /= 10; p10 *= 10; } cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, amazing = 0, maxx, minn, k; cin >> n; cin >> k; maxx = k; minn = k; for (int i = 1; i < n; i++) { cin >> k; if (k > maxx) { maxx = k; amazing++; } else if (k < minn) { minn = k; amazing++; } } cout << amazing << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX; const int N = 2 * 1e3 + 10; const long double eps = 1e-9; const int mod = 998244353; char a[N][N]; int d[N][N], v[N][N], n, m; bool used[N][N]; void nxt(char c, int &x, int &y) { if (c == U ) --x; else if (c == D ) ++x; else if (c == R ) ++y; else --y; return; } int cx, cy; void fillcycle() { int x = cx, y = cy, cnt = 1; nxt(a[x][y], x, y); while (!(x == cx && y == cy)) { ++cnt; nxt(a[x][y], x, y); } x = cx, y = cy; d[x][y] = cnt; v[x][y] = 2; nxt(a[x][y], x, y); while (!(x == cx && y == cy)) { v[x][y] = 2; d[x][y] = cnt; nxt(a[x][y], x, y); } return; } void cycle(int x, int y) { while (!(x <= 0 || y <= 0 || x > n || y > m) && !used[x][y]) { v[x][y] = 0; used[x][y] = true; nxt(a[x][y], x, y); } if (x <= 0 || y <= 0 || x > n || y > m) return; if (v[x][y] != 0) return; cx = x; cy = y; fillcycle(); return; } void gg(int x, int y) { while (x > 0 && y > 0 && x <= n && y <= m && v[x][y] == 0) { v[x][y] = 1; nxt(a[x][y], x, y); } return; } void dfs(int x, int y) { int tx = x, ty = y; nxt(a[x][y], tx, ty); stack<pair<int, int> > vc; vc.push({x, y}); int c = 1; while (!(tx <= 0 || ty <= 0 || x > n || y > m) && d[tx][ty] == -1) { vc.push({tx, ty}); nxt(a[tx][ty], tx, ty); } if (tx <= 0 || ty <= 0 || x > n || y > m) c = 1; else c = d[tx][ty] + 1; while (!vc.empty()) { tx = vc.top().first; ty = vc.top().second; vc.pop(); d[tx][ty] = c; ++c; } return; } void solve() { cin >> n >> m; for (int i = 0; i <= n + 1; ++i) { fill(d[i], d[i] + m + 1, -1); fill(used[i], used[i] + m + 1, false); fill(v[i], v[i] + m + 1, -1); if (i > 0 && i <= n) { for (int j = 1; j <= m; ++j) cin >> a[i][j]; } } for (int i = 0; i <= n + 1; ++i) d[i][0] = d[i][m + 1] = 0; for (int i = 0; i <= m + 1; ++i) d[0][i] = d[n + 1][i] = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (!used[i][j]) { cycle(i, j); gg(i, j); } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (d[i][j] == -1) dfs(i, j); } } int x = 1, y = 1, ans = -2; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (ans < d[i][j]) ans = d[i][j], x = i, y = j; } } cout << x << << y << << ans << n ; return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long a[300005]; string s, p; vector<long long> v; int main() { ios_base::sync_with_stdio(false); long long i, j, k, l, m, x, y, r, n, t; cin >> n >> m; for (i = 1; i <= n; i++) { cin >> x; if (x < 300005) a[x] = 1; } long long msd = 0; long long cnt = 0; for (i = 1; i <= 300000; i++) { if (msd >= m) { break; } if (a[i] == 0) { if ((msd + i) <= m) { cnt++; v.push_back(i); msd += i; } } } cout << cnt << endl; for (i = 0; i < v.size(); i++) { cout << v[i] << ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long m = 100000, n = 0, a[1005], b[1005], u, i, j, k = 0, c, d, s = 0, r = 0; char str1[1005], str2[1005]; cin >> c >> d; scanf( %s , str1); scanf( %s , str2); for (i = 0, j = 0;; i++, j++) { if (str1[i] != str2[j]) { k++; a[n] = i + 1; n++; } if (i == c - 1) { i = -1; s++; j = -1; j += s; if (k < m) { m = k; for (r = 0; r < k; r++) { b[r] = a[r]; } } r = 0; k = 0; n = 0; if (s == (d - c + 1)) break; } } cout << m << endl; for (u = 0; u < m; u++) cout << b[u] << ; }
#include <bits/stdc++.h> int ab(int x) { if (x < 0) { x = x * -1; } return x; } int main() { int x, y, z, x1, x2, y1, y2; scanf( %d %d , &x, &y); z = ab(x) + ab(y); if (x < 0) { x1 = -1 * z; x2 = 0; if (y < 0) { y1 = 0; y2 = -1 * z; } else { y1 = 0; y2 = z; } } else { x1 = 0; x2 = z; if (y < 0) { y2 = 0; y1 = -1 * z; } else { y2 = 0; y1 = z; } } printf( %d %d %d %d , x1, y1, x2, y2); }
#include <bits/stdc++.h> using namespace std; char a[310][310]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } int t = min(m, n); for (int d = 1; d < t; d++) { for (int i = d; i < n - d; i++) { for (int j = d; j < m - d; j++) { if (a[i][j] == * && a[i][j - d] == * && a[i][j + d] == * && a[i - d][j] == * && a[i + d][j] == * ) k--; if (k == 0) { cout << i + 1 << << j + 1 << endl; cout << i + 1 - d << << j + 1 << endl; cout << i + 1 + d << << j + 1 << endl; cout << i + 1 << << j - d + 1 << endl; cout << i + 1 << << j + d + 1 << endl; return 0; } } } } cout << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 0 , ch = getchar(); return x; } const int maxn = 100005; int n; long long zero = 0, one = 0, z1 = 0, o1 = 0; char s[maxn], t[maxn]; int main() { n = read(); scanf( %s%s , s, t); for (int i = 0; i < n; ++i) { if (s[i] == 0 ) { zero++; if (t[i] == 0 ) z1++; } else { one++; if (t[i] == 0 ) o1++; } } printf( %lld n , z1 * one + o1 * zero - z1 * o1); return 0; }
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; char ch[] = { N , S , E , W }; template <class T> inline T biton(T n, T pos) { return n | ((T)1 << pos); } template <class T> inline T bitoff(T n, T pos) { return n & ~((T)1 << pos); } template <class T> inline T ison(T n, T pos) { return (bool)(n & ((T)1 << pos)); } template <class T> inline T gcd(T a, T b) { while (b) { a = a % b; swap(a, b); } return a; } template <class T> inline T bigmod(T p, T e, T m) { T ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % m; p = (p * p) % m; } return (T)ret; }; vector<int> primes; void sieve(int n) { vector<int> vec(n + 1); vec[0] = vec[1] = 1; for (int i = 2; i <= n; i++) { if (vec[i] == 0) { for (int j = i * 2; j <= n; j = j + i) { vec[j] = 1; } } } for (int i = 0; i <= n; i++) { if (vec[i] == 0) { primes.push_back(i); } } } void solve() { int n; cin >> n; vector<int> items(n); for (int i = 0; i < n; i++) { cin >> items[i]; } map<int, vector<int>> mp; for (int i = 0; i < n; i++) { int it = items[i]; for (int j = 0; j < primes.size() && primes[j] * primes[j] <= it; j++) { int cnt = 0; while (it % primes[j] == 0) { cnt++; it = it / primes[j]; } if (cnt > 0) { mp[primes[j]].push_back(cnt); } } if (it > 1) { mp[it].push_back(1); } } long long res = 1; for (auto m : mp) { sort(m.second.begin(), m.second.end()); if (m.second.size() == n - 1) { res = res * pow(m.first, m.second[0]); } else if (m.second.size() == n) { res = res * pow(m.first, m.second[1]); } } cout << res << n ; ; } int main() { ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(10); cout.tie(nullptr); cin.tie(nullptr); sieve(2e5); int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 105; vector<long long> a, b, v; string s; long long n, m, x; long long killAll() { vector<long long> hlp; long long i = 0; for (long long x : b) { while (i < v.size() and v[i] <= x) { hlp.push_back(v[i]); i++; } if (i == v.size()) return 0; else i++; } while (i < v.size()) { hlp.push_back(v[i]); i++; } long long ans = 0; long long j = hlp.size() - 1; for (__typeof(((long long)a.size() - 1)) i = ((long long)a.size() - 1); i >= (0); i--) { long long x = a[i]; if (j < i) return 0; if (hlp[j] >= x) ans += (hlp[j] - x); else return 0; j--; } while (j >= 0) { ans += hlp[j]; j--; } return ans; } long long killExact(long long k) { if (k > a.size()) return 0; long long i = k - 1; long long j = v.size() - 1; long long ans = 0; while (i >= 0 and j >= 0) { if (v[j] >= a[i]) ans += (v[j] - a[i]); else return 0; j--; i--; } return ans; } long long killFew() { long long ans = 0; for (long long i = 1; i <= m; i++) ans = max(ans, killExact(i)); return ans; } long long solve() { cin >> n >> m; for (__typeof((n)) i = (1); i <= (n); i++) { cin >> s >> x; if (s == ATK ) a.push_back(x); else b.push_back(x); } for (__typeof((m)) i = (1); i <= (m); i++) { cin >> x; v.push_back(x); } sort((v).begin(), (v).end()); sort((a).begin(), (a).end()); sort((b).begin(), (b).end()); cout << max(killAll(), killFew()) << n ; return 0; } signed main() { long long t; ios_base::sync_with_stdio(false); t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = n - 1; i >= 0; --i) { int temp = i % 2 != 0 ? -a[i] : a[i]; cout << temp << ; } cout << endl; } }
#include <bits/stdc++.h> using namespace std; template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; stack<char> st; string s; cin >> s; long long cnt = 0; for (int i = 0; i < n; i++) { if (s[i] == ( ) { cnt++; } else { cnt--; } } if (cnt != 0) { cout << -1 ; return 0; } long long ans = 0, len = 0, len1 = 0; for (int i = 0; i < n; i++) { if (s[i] == ( ) { if (len == 0) { st.push( ( ); } else { len1++; } } else { if (st.size() > 0) { st.pop(); } else { len++; } } if (len == len1) { ans += len + len1; len = 0; len1 = 0; } } cout << ans << n ; }
#include <bits/stdc++.h> using namespace std; const int maxn = (int)1e5 + 100; const int mod = (int)1e9 + 7; int n, k, a[maxn], c[maxn]; bool was[maxn]; vector<int> g[maxn]; bool check(int pos) { memset(was, 0, sizeof(was)); int used = 0, unused = pos; for (int i = pos; i >= 1; i--) { if (was[a[i]] || a[i] == 0) continue; if (unused > c[a[i]]) { was[a[i]] = 1; unused -= 1 + c[a[i]]; } } for (int i = (1); i <= (k); ++i) if (!was[i]) return 0; return 1; } int main() { scanf( %d%d , &n, &k); vector<int> st; for (int i = (1); i <= (n); ++i) { scanf( %d , a + i); if (a[i] > 0) { g[a[i]].push_back(i); st.push_back(i); } } for (int i = (1); i <= (k); ++i) scanf( %d , c + i); int l = 0, r = (int)st.size() - 1, ans = -1; while (l <= r) { int m = (l + r) >> 1; if (check(st[m])) ans = m, r = m - 1; else l = m + 1; } if (ans == -1) puts( -1 ); else printf( %d , st[ans]); return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { string s; int first; cin >> s >> first; string w = string(s.length(), w ); for (int i = 0; i < s.length(); i++) { if (s[i] == 0 ) { if (i - first >= 0) w[i - first] = 0 ; if (i + first < s.length()) w[i + first] = 0 ; } } for (int i = 0; i < s.length(); i++) { if (w[i] == w ) w[i] = 1 ; } string newS = ; for (int i = 0; i < s.length(); i++) { bool one = false; if (i - first >= 0 && w[i - first] == 1 ) one = true; if (i + first < s.length() && w[i + first] == 1 ) one = true; if (one) { newS += 1 ; } else newS += 0 ; } if (s == newS) { cout << w << n ; } else { cout << -1 n ; } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int t = 1; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; char s[10][10]; int t, i, j, o, x[2], y[2], ok; int main() { scanf( %d , &t); while (t--) { for (i = o = 0; i < 8; i++) { scanf( %s , s + i); for (j = 0; j < 8; j++) if (s[i][j] == K ) x[o] = i, y[o++] = j; } if (abs(x[0] - x[1]) % 4 || abs(y[0] - y[1]) % 4) puts( NO ); else puts( YES ); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4 ) #pragma GCC optimize( unroll-loops ) using namespace std; using i64 = int64_t; using ui64 = uint64_t; template <class A, class B> bool smin(A &x, B &&y) { if (y < x) { x = y; return true; } return false; } template <class A, class B> bool smax(A &x, B &&y) { if (x < y) { x = y; return true; } return false; } int const N = int(1e5 + 10); int const INF = int(1.2e9); struct node { int min_pos = INF; int min_neg = INF; }; node a[N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int q; cin >> q; while (q--) { int t, l, r; cin >> t >> l >> r; --l, --r; if (t == 1) { int k; cin >> k; if (k > 0) { for (int i = l; i < r; ++i) { smin(a[i].min_pos, k); } } else { k = -k; for (int i = l; i < r; ++i) { smin(a[i].min_neg, k); } } } else { i64 ans = 0; for (int i = l; i < r; ++i) { int const x = a[i].min_pos; int const y = a[i].min_neg; if (x != INF && y != INF) { ans += x + y; } } cout << ans << n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, ans = 1, bell[1005], c[1005][1005]; char s[55][1005]; map<long long, int> w; long long get(int x) { long long res = 0; for (int i = 1; i <= n; i++) res = (res << 1) + s[i][x] - 48; return res; } int main() { scanf( %d%d , &m, &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf( %c , &s[i][j]); for (int i = 1; i <= m; i++) w[get(i)]++; c[0][0] = 1; bell[0] = 1; for (int i = 1; i <= m; i++) for (int j = 0; j <= i; j++) { c[i][j] = (c[i - 1][j - 1] + (long long)j * c[i - 1][j]) % 1000000007; bell[i] = (bell[i] + c[i][j]) % 1000000007; } for (int i = 1; i <= m; i++) { long long num = get(i); ans = (long long)ans * bell[w[num]] % 1000000007; w[num] = 0; } printf( %d n , ans); return 0; }