func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; int countbit(int n) { return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); } int lowbit(int n) { return (n ^ (n - 1)) & n; } const double pi = acos(-1.0); const double eps = 1e-11; template <class T> T sqr(T x) { return x * x; } int n, m, k; int c[101], clen; bool Nim(int k) { for (int i = 0; i < 8; ++i) { int sum = 0; for (int j = 0; j < clen; ++j) sum += (c[j] >> i) & 1; if (sum % (k + 1)) return true; } return false; } int main() { scanf( %d%d%d , &n, &m, &k); int aw = 0, bw = 0; for (int i = 0; i < n; ++i) { char str[101]; char *pos, *pos1, *pos2; scanf( %s , str); pos = strchr(str, - ); pos2 = strchr(str, R ); pos1 = strchr(str, G ); if (pos && !pos1 && pos2) bw = 1; if (pos && !pos2 && pos1) aw = 1; if (pos1 && pos2) c[clen++] = abs(pos1 - pos2) - 1; } if (aw && bw) { printf( Draw n ); return 0; } else if (aw) { printf( First n ); return 0; } else if (bw) { printf( Second n ); return 0; } if (Nim(k)) printf( First n ); else printf( Second n ); return 0; }
#include <bits/stdc++.h> using namespace std; template <class C> inline void mini(C& a4, C b4) { a4 = min(a4, b4); } template <class C> inline void maxi(C& a4, C b4) { a4 = max(a4, b4); } const int INF = 1e9 + 7; const int base = 1e9; const int SIGMA = 26; const int N = 1e5 + 7; int n; string s; vector<int> ans; void rec(int v) { if (v == n) return; if (s[v] == l ) { ans.push_back(v); rec(v + 1); } else { rec(v + 1); ans.push_back(v); } } int main() { std::ios_base::sync_with_stdio(0); cout << setprecision(15) << fixed; cin >> s; n = ((int)(s).size()); rec(0); reverse((ans).begin(), (ans).end()); for (__typeof((ans).begin()) i = (ans).begin(); i != (ans).end(); ++i) printf( %d n , *i + 1); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; bool cmax(int &a, int b) { return (a < b) ? a = b, 1 : 0; } bool cmin(int &a, int b) { return (a > b) ? a = b, 1 : 0; } template <typename T> T read() { T ans = 0, f = 1; char ch = getchar(); while (!isdigit(ch) && ch != - ) ch = getchar(); if (ch == - ) f = -1, ch = getchar(); while (isdigit(ch)) ans = (ans << 3) + (ans << 1) + (ch - 0 ), ch = getchar(); return ans * f; } void file() {} int n, dep[N]; int same; void input() { n = read<int>(); for (register int i = (int)1; i <= (int)n + 1; ++i) { dep[i] = read<int>(); if (dep[i] > 1 && dep[i - 1] > 1) same = i; } } void work() { int old = 0, now = 0; if (!same) { puts( perfect ); return; } puts( ambiguous ); for (register int i = (int)1; i <= (int)n + 1; ++i) { for (register int j = (int)1; j <= (int)dep[i]; ++j) { now++; printf( %d , old); } old = now; } printf( n ); old = now = 0; for (register int i = (int)1; i <= (int)n + 1; ++i) { for (register int j = (int)1; j <= (int)dep[i]; ++j) { now++; if (i == same) { printf( %d , old - 1); same = 0; } else printf( %d , old); } old = now; } } int main() { file(); input(); work(); return 0; }
#include <bits/stdc++.h> using namespace std; long long a[100010]; long long dif[100010]; int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); for (int i = 1; i <= n - 1; i++) { dif[i] = a[i + 1] - a[i]; } long long d = 0; for (int i = 1; i <= n - 1; i++) if (dif[i] > 0) d += dif[i]; int q; scanf( %d , &q); printf( %lld n , ((a[1] + d) % 2) ? (((a[1] + d) >> 1) + 1) : (a[1] + d) >> 1); for (int i = 0; i < q; i++) { int l, r; long long x; scanf( %d%d%lld , &l, &r, &x); if (l == 1) a[1] += x; if (l != 1) { dif[l - 1] += x; if (dif[l - 1] > 0) d += dif[l - 1] - (dif[l - 1] - x > 0 ? (dif[l - 1] - x) : (0)); else d -= ((dif[l - 1] - x) > 0 ? (dif[l - 1] - x) : (0)); } if (r != n) { dif[r] -= x; if (dif[r] > 0) d += dif[r] - (dif[r] + x > 0 ? (dif[r] + x) : (0)); else d -= (dif[r] + x > 0 ? (dif[r] + x) : (0)); } printf( %lld n , ((a[1] + d) % 2) ? (((a[1] + d) >> 1) + 1) : (a[1] + d) >> 1); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; constexpr int N = 2e5 + 5; int t, a, b, p; string s; void Solve() { cin >> t; while (t--) { cin >> a >> b >> p >> s; s.pop_back(); vector<ll> suf; suf.resize(s.size()); char last = 0 ; for (int i = s.size() - 1; i >= 0; i--) { if (s[i] != last) suf[i] = (s[i] == A ? a : b); last = s[i]; } for (int i = suf.size() - 2; i >= 0; i--) suf[i] += suf[i + 1]; int id = 0; while (id < suf.size() && suf[id] > p) id++; cout << id + 1 << n ; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); Solve(); return 0; }
#include <bits/stdc++.h> const int N = 1e5 + 1e4; struct edg { int now, to, val; } ed[N]; struct edge { int to, next; } e[N]; int ehead[N], ecnt; inline void add_edge(int now, int to) { ecnt++; e[ecnt].to = to; e[ecnt].next = ehead[now]; ehead[now] = ecnt; } int n, m, left, rig, mid; int pos[N], ans[N], acnt, pcnt; void dfs(int now) { pos[now] = 1; for (int i = ehead[now]; i; i = e[i].next) if (pos[e[i].to] == 0) dfs(e[i].to); pos[now] = ++pcnt; } inline bool check(int lim) { ecnt = acnt = 0; memset(ehead, 0, sizeof(ehead)); memset(pos, 0, sizeof(pos)); for (int i = 1; i <= m; i++) if (ed[i].val > lim) add_edge(ed[i].now, ed[i].to); for (int i = 1; i <= n; i++) if (!pos[i]) dfs(i); for (int i = 1; i <= m; i++) if (pos[ed[i].now] < pos[ed[i].to]) if (ed[i].val > lim) return false; else ans[++acnt] = i; return true; } int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= m; i++) scanf( %d%d%d , &ed[i].now, &ed[i].to, &ed[i].val); rig = 1e9; while (left != rig) { mid = (left + rig) >> 1; if (check(mid)) rig = mid; else left = mid + 1; } check(left); printf( %d %d n , left, acnt); for (int i = 1; i <= acnt; i++) printf( %d , ans[i]); }
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 2e5 + 10; int beg[N]; int nxt[N]; int to[N]; int tot; int a[N]; void add(int x, int y) { ++tot; nxt[tot] = beg[x]; to[tot] = y; beg[x] = tot; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; ++i) { int x; cin >> x; add(x, i); } int ans = 0; for (int i = 1; i <= m; ++i) { int siz = 0; for (int j = beg[i]; j; j = nxt[j]) a[++siz] = to[j]; if (siz == 0) continue; reverse(a + 1, a + 1 + siz); for (int r = 1, l = 1; r <= siz; ++r) { for (; l < r && a[r] - a[l] + 1 > k + r - l + 1; ++l) ; ans = max(ans, r - l + 1); } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k, p, x; cin >> n >> k >> p; std::vector<int> even, odd; for (int i = 0; i < n; ++i) { cin >> x; if (x % 2) odd.emplace_back(x); else even.emplace_back(x); } if (odd.size() < k - p || (odd.size() - k - p) % 2 != 0) { cout << NO ; return 0; } if ((even.size() + (odd.size() - (k - p)) / 2) < p) { cout << NO << n ; return 0; } cout << YES << n ; long long cnt = 0; if (k - p) { while (even.size() > 0 && cnt != p) { cout << 1 << << even.back() << n ; even.pop_back(); cnt++; } while (odd.size() > 0 && cnt != p) { cout << 2 << << odd.back() << ; odd.pop_back(); cout << odd.back() << n ; odd.pop_back(); cnt++; } for (int i = 0; i < k - p - 1; i++) { vector<int> ans; while (even.size() > 0) { ans.emplace_back(even.back()); even.pop_back(); } ans.emplace_back(odd.back()); odd.pop_back(); cout << ans.size() << ; for (auto i : ans) { cout << i << ; } cout << n ; } vector<int> ans; while (odd.size() > 0) { ans.emplace_back(odd.back()); odd.pop_back(); } while (even.size() > 0) { ans.emplace_back(even.back()); even.pop_back(); } cout << ans.size() << ; for (auto i : ans) { cout << i << ; } cout << n ; } else { vector<int> ans; for (int i = 0; i < p - 1; i++) { if (even.size() > 0) { cout << 1 << << even.back() << n ; even.pop_back(); } else { cout << 2 << << odd.back() << ; odd.pop_back(); cout << odd.back() << ; odd.pop_back(); } } while (even.size() > 0) { ans.emplace_back(even.back()); even.pop_back(); } while (odd.size()) { ans.emplace_back(odd.back()); odd.pop_back(); } cout << ans.size() << ; for (auto I : ans) { cout << I << ; } } }
#include <bits/stdc++.h> template <class T> int gmax(T &a, T b) { if (b > a) { a = b; return 1; } return 0; } template <class T> int gmin(T &a, T b) { if (b < a) { a = b; return 1; } return 0; } using namespace std; string to_string(string s) { return + s + ; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? true : false ); } template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } template <typename A> string to_string(A v) { bool first = true; string res = { ; for (const auto &x : v) { if (!first) { res += , ; } first = false; res += to_string(x); } res += } ; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << << to_string(H); debug_out(T...); } const int MAXN = 2e5 + 5; const int MOD = 998244353; int a[MAXN]; int b[MAXN]; int id[MAXN]; long long cnt[MAXN]; int cmp(int x, int y) { return cnt[x] > cnt[y]; } int main() { int n; while (~scanf( %d , &n)) { for (int i = 0; i < n; ++i) scanf( %d , &a[i]); for (int i = 0; i < n; ++i) scanf( %d , &b[i]); sort(b, b + n); for (int i = 0; i < n; ++i) { id[i] = i; cnt[i] = 1ll * (i + 1) * (n - i) * a[i]; } sort(id, id + n, cmp); long long result = 0; for (int i = 0; i < n; ++i) { result = (result + cnt[id[i]] % MOD * b[i] % MOD) % MOD; } printf( %lld n , result); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; const int MOD = 998244353; int n, q; long long seg[4 * MAXN], mul[4 * MAXN], plu[4 * MAXN]; set<pair<int, int>> number[MAXN]; void shift(int l, int r, int id) { int mid = l + r >> 1; seg[id << 1] = (seg[id << 1] * mul[id]) % MOD; seg[id << 1] = (seg[id << 1] + plu[id] * (mid - l)) % MOD; seg[id << 1 | 1] = (seg[id << 1 | 1] * mul[id]) % MOD; seg[id << 1 | 1] = (seg[id << 1 | 1] + plu[id] * (r - mid)) % MOD; mul[id << 1] = (mul[id << 1] * mul[id]) % MOD; plu[id << 1] = (plu[id << 1] * mul[id]) % MOD; mul[id << 1 | 1] = (mul[id << 1 | 1] * mul[id]) % MOD; plu[id << 1 | 1] = (plu[id << 1 | 1] * mul[id]) % MOD; plu[id << 1] = (plu[id << 1] + plu[id]) % MOD; plu[id << 1 | 1] = (plu[id << 1 | 1] + plu[id]) % MOD; mul[id] = 1; plu[id] = 0; } long long seg_get(int s, int e, int l = 0, int r = n, int id = 1) { if (l >= s && r <= e) return seg[id]; if (l >= e || r <= s) return 0; shift(l, r, id); int mid = l + r >> 1; return (seg_get(s, e, l, mid, id << 1) + seg_get(s, e, mid, r, id << 1 | 1)) % MOD; } void seg_do(int s, int e, int l = 0, int r = n, int id = 1) { if (l >= s && r <= e) { seg[id] = (seg[id] + (r - l)) % MOD; plu[id] = (plu[id] + 1) % MOD; return; } if (l >= e || r <= s) return; int mid = l + r >> 1; shift(l, r, id); seg_do(s, e, l, mid, id << 1); seg_do(s, e, mid, r, id << 1 | 1); seg[id] = (seg[id << 1] + seg[id << 1 | 1]) % MOD; return; } void seg_mul(int s, int e, int l = 0, int r = n, int id = 1) { if (s == e) return; if (l >= s && r <= e) { seg[id] = (seg[id] * 2) % MOD; plu[id] = (plu[id] * 2) % MOD; mul[id] = (mul[id] * 2) % MOD; return; } if (l >= e || r <= s) return; int mid = l + r >> 1; shift(l, r, id); seg_mul(s, e, l, mid, id << 1); seg_mul(s, e, mid, r, id << 1 | 1); seg[id] = (seg[id << 1] + seg[id << 1 | 1]) % MOD; return; } void handle(int l, int r, int x) { auto itl = number[x].upper_bound({l, -2}); itl--; if ((*itl).second <= l) itl++; auto itr = number[x].lower_bound({r, -2}); if ((*itl).first > l) { seg_do(l, min(r, (*itl).first)); } for (auto it = itl; it != itr; it++) { int L = max(l, (*it).first); int R = min(r, (*it).second); seg_mul(L, R); if ((*it).second >= r) continue; auto it2 = it; it2++; L = (*it).second; R = r; R = min(R, (*it2).first); seg_do(L, R); } l = min(l, (*itl).first); itr--; r = max(r, (*itr).second); itr++; number[x].erase(itl, itr); number[x].insert({l, r}); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> q; for (int i = 0; i < 4 * MAXN; i++) mul[i] = 1; for (int i = 0; i <= n; i++) { number[i].insert({-1, -1}); number[i].insert({n, n}); } while (q--) { int tp; cin >> tp; if (tp == 1) { int l, r, x; cin >> l >> r >> x; handle(l - 1, r, x); } else { int l, r; cin >> l >> r; cout << seg_get(l - 1, r) << n ; } } cin >> n; return 0; }
#include <bits/stdc++.h> int main(void) { int a[100][5], n, i, j, k = 0, min = 10000; scanf( %d , &n); for (i = 0; i < n; i++) { scanf( %d%d%d%d , &a[i][0], &a[i][1], &a[i][2], &a[i][3]); a[i][4] = i + 1; } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (a[j][0] > a[i][0] && a[j][1] > a[i][1] && a[j][2] > a[i][2]) { a[i][3] = 10000; } } if (a[i][3] < min) { min = a[i][3]; k = a[i][4]; } } printf( %d , k); return 0; }
#include <bits/stdc++.h> using namespace std; long long a[2210], b[2210]; long long inf = 1e13; int main() { memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); int n = 0, k = 0; scanf( %d%d , &n, &k); for (int i = 1; i <= n; ++i) { scanf( %I64d , &a[i]); } for (int i = 1; i <= n; ++i) { scanf( %I64d , &b[i]); } long long l = 0, r = inf, res = 0; while (l <= r) { long long mid = (l + r) >> 1; priority_queue<long long, vector<long long>, greater<long long>> qa; priority_queue<long long> qb; long long sz = 0, cur = 0; for (int i = 1; i <= n; ++i) { qa.push(a[i]); long long tmp1 = qa.top() + b[i] - mid; long long tmp2 = qb.empty() ? inf : (b[i] - qb.top()); if (tmp1 <= tmp2 && tmp1 <= 0) { cur += tmp1; ++sz; qb.push(b[i]); qa.pop(); } else if (tmp2 < tmp1 && tmp2 < 0) { cur += tmp2; qb.pop(); qb.push(b[i]); } } if (sz >= k) { res = cur + (long long)k * mid; r = mid - 1; } else { l = mid + 1; } } printf( %I64d , res); return 0; }
#include <bits/stdc++.h> using namespace std; int a[17] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 0}, b; int main() { cin >> b; cout << a[b]; return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const int iinf = INT_MAX; const long long linf = 2e18; const double dinf = 1e30; const int MOD = 1000000007; inline int read() { int X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == - ; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } void write(int x) { if (x < 0) putchar( - ), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + 0 ); } void print(int x) { cout << x << n ; exit(0); } void PRINT(string x) { cout << x << n ; exit(0); } void douout(double x) { printf( %lf n , x + 0.0000000001); } long long ans = 1e18, a, b, mx = 1e18; int main() { cin >> a >> b; for (long long i = 1; i * i <= (a + b); i++) { if (a % i == 0) mx = min(mx, a / i); if (b % i == 0) mx = min(mx, b / i); if ((a + b) % i == 0 && (a + b) / i >= mx) ans = min(ans, 2 * i + 2 * ((a + b) / i)); } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, p; cin >> n >> p; int cnt = 0; for (int i = 1; i < n; ++i) { for (int j = i + 1; j <= n; ++j) { if (cnt == 2 * n + p) break; cout << i << << j << n ; cnt++; } } } return 0; }
#include <bits/stdc++.h> using namespace std; double getangle(pair<int, int> a, pair<int, int> b) {} int main() { int n; cin >> n; double arr[n + 1]; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; arr[i] = atan2(x, y); if (arr[i] < 0) arr[i] += 2.0 * M_PI; arr[i] *= 180.0 / M_PI; } sort(arr, arr + n); double a = 0.0; if (n <= 2) a = min(360.0 - (arr[0] - arr[n - 1] + 360.0), 360.0 - (arr[n - 1] - arr[0])); else { a = 360.0 - (arr[0] - arr[n - 1] + 360.0); for (int i = 0; i < n - 1; ++i) a = min(a, 360.0 - (arr[i + 1] - arr[i])); } printf( %.8lf n , a); }
#include <bits/stdc++.h> using namespace std; int main() { string aux; cin >> aux; int flag_Invalid_Input = 0; int memo[10] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512}; int counter = 0; int len = aux.size(); int i, j; for (i = 0; i < len; i++) { if (aux[i] > 1 ) { for (j = 0; j < len - i; j++) { counter += memo[j]; } break; } if (aux[i] == 1 ) { counter += memo[len - i - 1]; } } cout << counter; }
#include <bits/stdc++.h> using namespace std; const int maxn = 210010; const int mod = 1e9 + 7; int dp[110][maxn], sum[maxn]; int a, b, k, t; int main() { scanf( %d%d%d%d , &a, &b, &k, &t); dp[0][0] = 1; for (int i = 1; i <= t; i++) { for (int j = 0; j < maxn; j++) sum[j] = ((j > 0 ? sum[j - 1] : 0) + dp[i - 1][j]) % mod; for (int j = 0; j < maxn; j++) dp[i][j] = (sum[j] - (j - 2 * k - 1 >= 0 ? sum[j - 2 * k - 1] : 0) + mod) % mod; } for (int i = 0; i < maxn; i++) { sum[i] = ((i > 0 ? sum[i - 1] : 0) + dp[t][i]) % mod; } int ans = 0; for (int i = 0; i <= 2 * k * t; i++) { (ans += (1LL) * dp[t][i] * (i + a - b - 1 < 0 ? 0 : sum[i + a - b - 1]) % mod) %= mod; } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; void dfs(long long n, long long i, unordered_map<long long, vector<long long>> &adj, vector<bool> &vis, vector<long long> &a1, vector<long long> &a2) { if (vis[n]) return; vis[n] = true; if (i == 0) a1.push_back(n); else a2.push_back(n); for (auto &it : adj[n]) { if (!vis[it]) dfs(it, i ^ 1, adj, vis, a1, a2); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long n, m; cin >> n >> m; unordered_map<long long, vector<long long>> adj; vector<bool> vis(n + 1, false); vector<long long> a1, a2; for (long long i = 0; i < m; i++) { long long x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } queue<pair<long long, long long>> q; q.push({1, 0}); while (q.size() > 0) { auto it = q.front(); q.pop(); long long x, y; tie(x, y) = it; if (vis[x]) continue; vis[x] = true; if (y == 0) a1.push_back(x); else a2.push_back(x); for (auto &it : adj[x]) { if (!vis[it]) q.push({it, y ^ 1}); } } if (a1.size() <= a2.size()) { cout << a1.size() << n ; for (auto x : a1) cout << x << ; cout << endl; ; } else { cout << a2.size() << n ; for (auto x : a2) cout << x << ; cout << endl; ; } } return 0; }
#include <bits/stdc++.h> using namespace std; const long double PI = acos(-1); struct Point { long long x, y, r; Point() {} Point(long long a, long long b, long long c) { x = a; y = b; r = c; } }; long double rad; long double cor(long double angle) { if (angle < 0) { return angle + 2 * PI; } return angle; } pair<long double, long double> fnd(Point& p) { long double kor = sqrt(p.x * p.x + p.y * p.y); if (kor > rad + p.r) { return {20, 20}; } if (rad - p.r > kor) { return {20, 20}; } else { long double an1 = acos((rad * rad + p.x * p.x + p.y * p.y - p.r * p.r) / (2 * rad * kor)); long double tmp = sqrt(p.x * p.x + p.y * p.y - p.r * p.r); long double l = cor(atan2(p.y, p.x)); return {l - an1, l + an1}; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long double d; long long n; cin >> n; cin >> d; rad = 0; vector<Point> sp(n); long long x, y, r; for (long long i = 0; i < n; ++i) { cin >> x >> y >> r; sp[i].x = x; sp[i].y = y; sp[i].r = r; } vector<pair<long double, long long> > al(0); for (long long kol = 0; kol < 2850; ++kol) { rad += d; for (long long i = 0; i < n; ++i) { pair<long double, long double> now = fnd(sp[i]); if (now.first == 20) { continue; } else if (now.first == 30) { continue; } else if (now.first < 0 && now.second > 0) { al.push_back({cor(now.first), 0}); al.push_back({2 * PI, 1}); al.push_back({0, 0}); al.push_back({now.second, 1}); } else if (now.second > 2 * PI) { al.push_back({now.first, 0}); al.push_back({2 * PI, 1}); al.push_back({0, 0}); al.push_back({now.second - 2 * PI, 1}); } else { al.push_back({now.first, 0}); al.push_back({now.second, 1}); } } } sort(al.begin(), al.end()); long long now = 0; long long mx = 0; for (auto p : al) { if (p.second == 0) { ++now; } else { --now; } mx = max(mx, now); } cout << mx << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int maxn = n * n - 1; int c = 0; for (int i = 0; i <= maxn; i++) { if (i % 2 == 0) { cout << i << ; c++; } if (c == n) { cout << endl; c = 0; } } for (int i = 0; i <= maxn; i++) { if (i % 2 == 1) { cout << i << ; c++; } if (c == n) { cout << endl; c = 0; } } }
#include <bits/stdc++.h> using namespace std; long long n, m, k, x, i, sum, j, r = 1, B[20][20]; char A[20][20]; int main() { cin >> n >> m; for (i = 0; i < n; i++) { cin >> x; sum += x; } if (sum == 0) cout << 0; else { if (abs(sum) <= m) cout << 1 ; else cout << (abs(sum) / m) + (abs(sum) % m != 0); } return 0; }
#include <bits/stdc++.h> template <class T> int gmax(T &a, T b) { if (b > a) { a = b; return 1; } return 0; } template <class T> int gmin(T &a, T b) { if (b < a) { a = b; return 1; } return 0; } using namespace std; string to_string(string s) { return + s + ; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? true : false ); } template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } template <typename A> string to_string(A v) { bool first = true; string res = { ; for (const auto &x : v) { if (!first) { res += , ; } first = false; res += to_string(x); } res += } ; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << << to_string(H); debug_out(T...); } const int MAXN = 2e5 + 5; int X[MAXN]; int solve(int x, int y) { if (x > y) return y; else if (x == y) return 0; else return y - 1; } int has[MAXN]; int main() { int n, m; while (~scanf( %d %d , &n, &m)) { memset(has, 0, sizeof(has)); map<int, long long> m1; map<int, long long> m2; map<int, int> m3; long long tmp = 0; for (int i = 0; i < m; ++i) { scanf( %d , &X[i]); } for (int i = 1; i < m; ++i) { if (X[i] == X[i - 1]) continue; tmp += abs(X[i] - X[i - 1]); m1[X[i]] += abs(X[i] - X[i - 1]); m1[X[i - 1]] += abs(X[i] - X[i - 1]); m2[X[i]] += solve(X[i], X[i - 1]); m2[X[i - 1]] += solve(X[i - 1], X[i]); m3[X[i]]++; m3[X[i - 1]]++; int t1 = X[i]; int t2 = X[i - 1]; if (t1 > t2) swap(t1, t2); has[t1]++; has[t2 + 1]--; } for (int i = 1; i <= n; ++i) has[i] += has[i - 1]; vector<long long> result; result.push_back(tmp); for (int i = 2; i <= n; ++i) { long long tmp2 = tmp - m1[i]; result.push_back(tmp2 + m2[i] - (has[i] - m3[i])); } for (int i = 0; i < n; ++i) { if (i) printf( ); printf( %lld , result[i]); } printf( n ); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2005; struct Point { long long x, y; Point() {} Point(long long x, long long y) : x(x), y(y) {} bool operator<(const Point& rhs) const { if (y != rhs.y) return y < rhs.y; else return x < rhs.x; } }; Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } Point operator*(Point v, long long mul) { return Point(v.x * mul, v.y * mul); } long long cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } int n; Point p[MAXN]; inline long long C(long long x) { return x * (x - 1) / 2; } long long calc(Point o, vector<pair<Point, int> >& v) { sort(v.begin(), v.end(), [&o](const pair<Point, int>& a, const pair<Point, int>& b) -> bool { return cross(a.first - o, b.first - o) < 0; }); long long cnt[2] = {0}, ret = 0; for (auto& i : v) cnt[i.second]++; for (auto& i : v) { cnt[i.second]--; ret += C(cnt[0]) * C(cnt[1]); cnt[i.second ^ 1]++; } return ret; } int main() { scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %lld%lld , &p[i].x, &p[i].y); long long ans = 0; for (int i = 0; i < n; i++) { vector<pair<Point, int> > v; for (int j = 0; j < n; j++) if (j != i) { if (p[i] < p[j]) v.emplace_back(p[j], 1); else v.emplace_back(p[i] * 2 - p[j], 0); } ans += calc(p[i], v); } ans /= 2; printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; using VI = vector<int>; const int NN = 200011; int a[NN], b[NN], arr[NN]; VI vec[NN], cyc[NN]; int nc; int dp[NN], vst[NN]; int n, s; void dfs(int u) { while (not vec[u].empty()) { int v = vec[u].back(); vec[u].pop_back(); dfs(a[v]); cyc[nc].push_back(v); } } int solve() { cin >> n >> s; for (int i = 1; i <= n; i++) scanf( %d , a + i), arr[i] = a[i], dp[i] = i; sort(arr + 1, arr + n + 1); int m = unique(arr + 1, arr + n + 1) - arr - 1; for (int i = 1; i <= n; i++) { b[i] = a[i] = lower_bound(arr + 1, arr + m + 1, a[i]) - arr; } sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) if (a[i] ^ b[i]) { vec[b[i]].push_back(i); } for (int i = 1; i <= m; i++) { if (not vec[i].empty()) { dfs(i); int r = ((int)cyc[nc].size()); for (int j = 0; j < r; j++) { dp[cyc[nc][(j + 1) % r]] = cyc[nc][j]; s--; } nc++; } if (s < 0) return puts( -1 ); } s = min(s, nc); if (s >= 2) { cout << nc - s + 2 << endl; cout << s << endl; for (int i = 0; i < s; i++) printf( %d , cyc[i][0]); puts( ); int tmp = dp[cyc[s - 1][0]]; for (int i = s; --i;) { dp[cyc[i][0]] = dp[cyc[i - 1][0]]; } dp[cyc[0][0]] = tmp; } else cout << nc << endl; for (int i = 1; i <= n; i++) { if (dp[i] == i or vst[i]) continue; VI ans; for (int u = i; not vst[u];) { ans.push_back(u); vst[u] = 1; u = dp[u]; } printf( %d n , ans.size()); for (int u : ans) printf( %d , u); puts( ); } } int main() { solve(); }
#include <bits/stdc++.h> using namespace std; long long int n, i, j, t, ans, arr[3009], crr[3009][3009]; int main() { scanf( %lld , &t); while (t--) { scanf( %lld , &n); ans = 0; for (i = 1; i <= n; i++) scanf( %lld , &arr[i]); for (i = n; i >= 1; i--) { for (j = i - 1; j >= 1; j--) { ans += crr[arr[i]][arr[j]]; } for (j = i + 1; j <= n; j++) { crr[arr[j]][arr[i]]++; } } printf( %lld n , ans); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) crr[i][j] = 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, b, one, two; string a; one = 0; two = 0; cin >> n >> a; for (b = 0; b < n; b++) { if (a[b] == 1 ) { one++; } else { two++; } } if (one > two) { cout << one - two; } else { cout << two - one; } }
#include <bits/stdc++.h> using namespace std; int n, k; int a[2000000]; int dem; int maxa; int dp[10000001]; void mmax(int &a, int b) { a = max(a, b); } bool ok(int len) { for (int i = 0; i <= maxa; i++) dp[i] = 0; dp[len] = 1; for (int i = len + 1; i <= maxa; i++) { mmax(dp[i], dp[i - 1]); mmax(dp[i], dp[i >> 1] + dp[(i + 1) >> 1]); } long long sum = 0; for (int i = 1; i <= n; i++) sum += dp[a[i]]; return sum >= k; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i], maxa = max(maxa, a[i]); int d = 1, c = 10000000, g, fin = -1; while (d <= c) { g = (d + c) / 2; if (ok(g)) { fin = g; d = g + 1; } else c = g - 1; } cout << fin; return 0; }
#include <bits/stdc++.h> using namespace std; map<long long, long long> mp1, mp2; long long n, x, yy, c; vector<pair<int, int>> v; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> x >> yy; v.push_back({x, yy}); mp1[x + yy]++; mp2[x - yy]++; } for (int i = 0; i < n; i++) { x = v[i].first; yy = v[i].second; c += mp1[x + yy]; c += mp2[x - yy]; c -= 2; } cout << c / 2 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 200010; int nowarn; int n, m; namespace Tree { int head[MAXN], nxt[MAXN], to[MAXN], eidx; void init() { eidx = 0; memset(head, -1, sizeof(head)); } void adde(int u, int v) { to[eidx] = v, nxt[eidx] = head[u], head[u] = eidx++; } } // namespace Tree namespace DFN { int dfn[MAXN], dfn_cnt, son[MAXN], dep[MAXN], top[MAXN], size[MAXN], pa[MAXN]; void dfs1(int u, int fa, int d) { using namespace Tree; pa[u] = fa, dep[u] = d, son[u] = 0, size[u] = 1; for (int i = head[u]; ~i; i = nxt[i]) { int v = to[i]; dfs1(v, u, d + 1); size[u] += size[v]; if (size[v] > size[son[u]]) son[u] = v; } } void dfs2(int u, int tp) { using namespace Tree; top[u] = tp, dfn[u] = ++dfn_cnt; if (son[u]) dfs2(son[u], tp); for (int i = head[u]; ~i; i = nxt[i]) { int v = to[i]; if (v == son[u]) continue; dfs2(v, v); } } void prelude() { size[0] = dfn_cnt = 0; dfs1(1, 0, 1); dfs2(1, 1); } int lca(int u, int v) { while (top[u] != top[v]) { if (dep[top[u]] < dep[top[v]]) swap(u, v); u = pa[top[u]]; } return min(u, v); } } // namespace DFN int lowbit(int x) { return x & (-x); } struct BIT { long long c[MAXN]; void init() { memset(c, 0, sizeof(c)); } void add(int p, long long v) { for (; p <= n; p += lowbit(p)) c[p] += v; } long long sum(int p) const { long long s = 0; for (; p; p -= lowbit(p)) s += c[p]; return s; } long long sum(int l, int r) const { return sum(r) - sum(l - 1); } }; struct Triple { int first, second, val; Triple() {} Triple(int a, int b, int c) : first(a), second(b), val(c) {} }; vector<Triple> chain[MAXN]; BIT sf, sg; long long query(int u, int v, const BIT &bit) { using namespace DFN; long long s = 0; while (top[u] != top[v]) { s += bit.sum(dfn[top[v]], dfn[v]); v = pa[top[v]]; } s += bit.sum(dfn[u], dfn[v]); return s; } long long f[MAXN], g[MAXN]; void dp(int u) { using namespace Tree; using namespace DFN; g[u] = 0; for (int i = head[u]; ~i; i = nxt[i]) dp(to[i]), g[u] += f[to[i]]; sg.add(dfn[u], g[u]); f[u] = g[u]; for (int i = 0; i < (int)chain[u].size(); ++i) { Triple tp = chain[u][i]; int s = tp.first, t = tp.second, w = tp.val; if (t == u) swap(s, t); if (s == u) { f[u] = max(f[u], query(u, t, sg) - query(u, t, sf) + w); } else { f[u] = max(f[u], query(u, s, sg) + query(u, t, sg) - g[u] - query(u, s, sf) - query(u, t, sf) + w); } } sf.add(dfn[u], f[u]); } int main() { nowarn = scanf( %d%d , &n, &m); Tree::init(); for (int i = 2; i <= n; ++i) { int p; nowarn = scanf( %d , &p); Tree::adde(p, i); } DFN::prelude(); while (m--) { int u, v, w; nowarn = scanf( %d%d%d , &u, &v, &w); int lca = DFN::lca(u, v); chain[lca].push_back(Triple(u, v, w)); } sf.init(), sg.init(); dp(1); printf( %lld n , f[1]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, s = 1, i; cin >> n; int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] == s) { s++; } } cout << s - 1 << endl; s = 1; for (i = 0; i < n; i++) { if (a[i] == s) { cout << i + 2001 << ; s++; } } }
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf( %d , &n); long long broj = 3; int i; for (i = 0; i < 3 * n - 1; i++) { broj *= 3; broj %= 1000000007; } long long broj1 = 7; for (i = 0; i < n - 1; i++) { broj1 *= 7; broj1 %= 1000000007; } cout << (broj - broj1 + 1000000007) % 1000000007 << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100005; int a[N], cnt[N]; int f[N], bj[N]; vector<int> d[N]; int n, mx; long long ans; void init(int n) { for (int i = 1; i <= n; i++) f[i] = 1; for (int i = 2; i <= n; i++) { if (bj[i]) continue; f[i] = -1; for (int j = i * 2; j <= n; j += i) { bj[j] = 1; f[j] = (j % (i * i)) ? f[j] * -1 : 0; } } } int count(int x) { int res = 0; for (int i = 0; i < d[x].size(); i++) { res += f[d[x][i]] * cnt[d[x][i]]; } return res; } void add(int x, int v) { for (int i = 0; i < d[x].size(); i++) { cnt[d[x][i]] += v; } } int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { int x; scanf( %d , &x); a[x]++; mx = max(mx, x); } ans = mx; for (int i = 1; i <= mx; i++) { for (int j = i; j <= mx; j += i) { d[j].push_back(i); } } init(mx); for (int g = 1; g <= mx; g++) { stack<int> st; for (int i = mx / g; i >= 1; i--) { if (!a[i * g]) continue; int c = count(i); while (c) { if (gcd(i, st.top()) == 1) { ans = max(ans, (long long)i * g * st.top()); c--; } add(st.top(), -1); st.pop(); } st.push(i); add(i, 1); } while (!st.empty()) { add(st.top(), -1); st.pop(); } } printf( %lld , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1005, mod = 1e9 + 7; int yh[N][N], a[N]; long long kpow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans; } int main() { for (int i = (0); i < (N); ++i) yh[i][0] = 1; for (int i = (1); i < (N); ++i) for (int j = (1); j < (N); ++j) { yh[i][j] = yh[i - 1][j - 1] + yh[i - 1][j]; if (yh[i][j] >= mod) yh[i][j] -= mod; } int n, m; cin >> n >> m; a[0] = 0; a[m + 1] = n + 1; for (int i = (1); i < (m + 1); ++i) cin >> a[i]; sort(a + 1, a + m + 1); long long ans = 1, k = n - m; for (int i = (1); i < (m + 2); ++i) { int c = a[i] - a[i - 1] - 1; ans = ans * yh[k][c] % mod; if (i != 1 && i != m + 1 && c) ans = ans * kpow(2, c - 1) % mod; k -= c; } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int dp[81][81][81], n, m, i, j, k, ans, inf = 1000007, x, y, l, len; pair<int, int> p; vector<pair<int, int> > g[82]; int main() { cin >> n >> m; if (m > n) { cout << -1 << endl; return 0; } cin >> k; for (i = 0; i < k; i++) { cin >> x >> y >> l; if (x == y) continue; p.first = y; p.second = l; g[x].push_back(p); } for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) for (k = 1; k <= n; k++) dp[i][j][k] = inf; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) dp[i][j][1] = 0; for (k = 2; k <= m; k++) for (j = 1; j < n; j++) for (i = 1; i <= n - j; i++) { len = g[i].size(); for (l = 0; l < len; l++) { if (g[i][l].first < i) continue; if (g[i][l].first > i + j) continue; dp[i][i + j][k] = min(dp[i][i + j][k], min(dp[g[i][l].first][i + 1][k - 1], dp[g[i][l].first][i + j][k - 1]) + g[i][l].second); } len = g[i + j].size(); for (l = 0; l < len; l++) { if (g[i + j][l].first > i + j) continue; if (g[i + j][l].first < i) continue; dp[i + j][i][k] = min(dp[i + j][i][k], min(dp[g[i + j][l].first][i + j - 1][k - 1], dp[g[i + j][l].first][i][k - 1]) + g[i + j][l].second); } } ans = inf; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) ans = min(ans, dp[i][j][m]); if (ans == inf) cout << -1 << endl; else cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int n; long long arr[MAXN][3]; map<pair<long long, long long>, pair<long long, int> > height; void update(long long x, long long y, long long z, int id) { if (height.find(make_pair((x), (y))) == height.end()) height[make_pair((x), (y))] = make_pair((z), (id)); else height[make_pair((x), (y))] = max(height[make_pair((x), (y))], make_pair((z), (id))); } int main() { cin >> n; int idx1 = 0, idx2[2] = {0, 0}; long long best1 = 0, best2 = 0; for (int i = 1; i <= n; i++) { cin >> arr[i][0] >> arr[i][1] >> arr[i][2]; sort(arr[i], arr[i] + 3); if (arr[i][0] > best1) { best1 = arr[i][0]; idx1 = i; } if (height.find(make_pair((arr[i][1]), (arr[i][2]))) != height.end()) { long long t = min(arr[i][0] + height[make_pair((arr[i][1]), (arr[i][2]))].first, min(arr[i][1], arr[i][2])); if (t > best2) { idx2[0] = i; idx2[1] = height[make_pair((arr[i][1]), (arr[i][2]))].second; best2 = t; } } update(arr[i][0], arr[i][1], arr[i][2], i); update(arr[i][0], arr[i][2], arr[i][1], i); update(arr[i][1], arr[i][2], arr[i][0], i); } if (best1 > best2) cout << 1 << endl << idx1 << endl; else cout << 2 << endl << idx2[0] << << idx2[1] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string s; int len; int main() { cin >> len; cin >> s; int sum = 0; cout << s[0]; for (int i = 1;; i++) { sum += i; if (sum >= len) break; cout << s[sum]; } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; cin >> t; while (t--) { int n; cin >> n; for (long long int i = (long long int)0; i < (long long int)n; i++) cout << i + 1 << n [i == n - 1]; } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:1000000000 ) #pragma GCC optimize( Ofast,no-stack-protector ) using namespace std; const int maxn = (int)2e5 + 10; vector<int> ed[maxn]; int t[maxn], h[maxn]; long long dp[maxn][2]; void go(int v, int p) { vector<pair<long long, int> > g; int in = 0; int out = 0; long long sc = 0; for (int u : ed[v]) { if (u == p) { continue; } go(u, v); if (h[u] > h[v]) { out++; sc += dp[u][0]; } else if (h[u] < h[v]) { in++; sc += dp[u][1]; } else { out++; g.push_back(make_pair(-dp[u][0] + dp[u][1], u)); sc += dp[u][0]; } } sort((g).begin(), (g).end()); for (int it = 0; it < 2; it++) { if (p != -1) if (it == 0) { in++; } else { out++; } long long ans = (long long)1e18; long long cur = sc; for (int i = 0; i <= (int)g.size(); i++) { ans = min(ans, cur + max(in + i, out - i) * (long long)t[v]); if (i != (int)g.size()) cur += g[i].first; } dp[v][it] = ans; if (p != -1) if (it == 0) { in--; } else { out--; } } } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { scanf( %d , &t[i]); } for (int i = 0; i < n; i++) { scanf( %d , &h[i]); } for (int i = 0; i < n - 1; i++) { int x, y; scanf( %d %d , &x, &y); x--; y--; ed[x].push_back(y); ed[y].push_back(x); } go(0, -1); cout << dp[0][0] << n ; }
#include <bits/stdc++.h> using namespace std; pair<long double, pair<long long, long long> > c[250001]; long double p, q, abcd, ABCD; long long a, b, n, i, x, y; bool comp(pair<long double, pair<long long, long long> > a, pair<long double, pair<long long, long long> > b) { if (a.first < b.first) return 1; if (a.first > b.first) return 0; if (a.second.second < b.second.second) return 1; if (a.second.second == b.second.second && a.second.first < b.second.first) return 1; return 0; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> x >> y >> n; abcd = x; ABCD = y; for (i = 1; i <= n; i++) { b = i; a = x * b / y; p = a; q = b; c[i * 2 - 1] = make_pair(abs(abcd / ABCD - p / q), make_pair(a, b)); b = i; a = (x * b + y - 1) / y; p = a; q = b; c[i * 2] = make_pair(abs(abcd / ABCD - p / q), make_pair(a, b)); } sort(c + 1, c + 1 + n * 2, comp); cout << c[1].second.first << / << c[1].second.second << n ; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, k, t, x, y, z, w, ct, ans, f; string s, l; long long a[200005]; set<long long> si; queue<long long> qi; map<long long, long long> mi; vector<long long> vi; priority_queue<long long> pqdi; priority_queue<long long, vector<long long>, greater<long long>> pqai; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cerr.tie(NULL); cin >> t; while (t--) { si.clear(); vi.clear(); cin >> n; for (long long i = 1; i <= n; i++) { cin >> a[i]; si.insert(a[i]); } if (si.size() != n) { cout << NO << n ; } else { f = 0; vector<long long> diff; sort(a + 1, a + n + 1); for (long long i = 2; i < n + 1; i++) { for (long long j = 1; j < i; j++) { diff.push_back(a[i] - a[j]); } } long long valid[1000000]; long long j = 2; memset(valid, 0, sizeof(valid)); vi.push_back(1); valid[1] = 1; for (long long i = 1; i < n; i++) { for (auto it : diff) { valid[(vi[i - 1] + it)] = 1; } while (valid[j]) j++; vi.push_back(j); valid[j] = 1; if (j > 1000000) { f = 1; break; } } if (f) cout << NO << n ; else { cout << YES << n ; ; for (long long i = 1; i <= n; i++) cout << vi[i - 1] << ; cout << n ; } } } }
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 10; const int MAXN = 1e4 + 10; const int MOD = 1e9 + 7; const int inf = 1e9; const double pi = acos(-1.0); const double eps = 1e-6; int dx[] = {0, -1, 0, 1}; int dy[] = {1, 0, -1, 0}; map<int, int> mymap, mymap1; int n, m; vector<int> Ve, Vo; int save[200100]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; int res = 0; for (int i = 1; i <= n; i++) { int x; cin >> x; if (x % 2) { if (!mymap[x]) { Vo.push_back(x); mymap[x] = 1; mymap1[x] = i; } } else { if (!mymap[x]) { Ve.push_back(x); mymap[x] = 1; mymap1[x] = i; } } } int o = Vo.size(); int e = Ve.size(); int r = -1; if (o >= n / 2 && e <= n / 2) r = min(o - n / 2, n / 2 - e); else if (o <= n / 2 && e >= n / 2) r = min(n / 2 - o, e - n / 2); if (r != -1) { res += r; if (o <= n / 2) { o += r; e -= r; } else { o -= r; e += r; } } res += max(n / 2 - o, 0); res += max(n / 2 - e, 0); for (int i = 1; i <= min(n, m); i++) if (i % 2) { if (!mymap[i]) { mymap[i] = 1; Vo.push_back(i); } } else { if (!mymap[i]) { mymap[i] = 1; Ve.push_back(i); } } o = Vo.size(); e = Ve.size(); if (o >= n / 2 && e >= n / 2) { cout << res << endl; vector<int> K; K.clear(); for (int i = 0; i < n / 2; i++) if (mymap1[Vo[i]]) save[mymap1[Vo[i]]] = Vo[i]; else K.push_back(Vo[i]); for (int i = 0; i < n / 2; i++) if (mymap1[Ve[i]]) save[mymap1[Ve[i]]] = Ve[i]; else K.push_back(Ve[i]); int p = 1; int q = 0; while (p <= n && q < K.size()) if (save[p]) p++; else { save[p] = K[q]; p++; q++; } for (int i = 1; i <= n; i++) cout << save[i] << ; } else cout << -1; return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; template <typename T> struct modular { constexpr modular() : val(0) {} constexpr modular(const modular<T>& _m) : val(_m.val) {} template <typename U> constexpr modular(const U& _r = U()) { val = -MOD <= _r && _r < MOD ? _r : _r % MOD; if (val < 0) { val += MOD; } } const T operator()() { return val; } template <typename U> explicit operator U() const { return static_cast<U>(val); } modular<T>& operator+=(const modular<T>& _m) { if ((val += _m.val) >= MOD) { val -= MOD; } return *this; } modular<T>& operator-=(const modular<T>& _m) { if ((val -= _m.val) < 0) { val += MOD; } return *this; } modular<T>& operator*=(const modular<T>& _m) { val = modular<T>(static_cast<int64_t>(val) * static_cast<int64_t>(_m.val)) .val; return *this; } modular<T>& operator/=(const modular<T>& _m) { T a = _m.val, b = MOD, u = 0, v = 1; while (a != 0) { T q = b / a; b -= q * a; swap(a, b); u -= q * v; swap(u, v); } return *this *= u; } modular<T>& operator=(const modular<T>& _m) { val = _m.val; return *this; } template <typename U> modular<T>& operator+=(const U& _r) { return *this += modular<T>(_r); } template <typename U> modular<T>& operator-=(const U& _r) { return *this -= modular<T>(_r); } template <typename U> modular<T>& operator*=(const U& _r) { return *this *= modular<T>(_r); } template <typename U> modular<T>& operator/=(const U& _r) { return *this /= modular<T>(_r); } template <typename U> modular<T>& operator=(const U& _r) { val = modular<T>(_r).val; return *this; } modular<T> operator-() { return modular<T>(-val); } template <typename U> friend bool operator==(const modular<U>&, const modular<U>&); friend std::istream& operator>>(std::istream& os, modular<T>& _m) { os >> _m.val; _m *= 1; return os; } friend std::ostream& operator<<(std::ostream& os, const modular<T>& _m) { return os << _m.val; } template <typename U> modular<T> exp(U e) { modular<T> res = 1; modular<T> b = val; if (e < 0) { b = 1 / b; e *= -1; } for (; e; e >>= 1) { if (e & 1) { res *= b; } b *= b; } return res; } private: T val; }; template <typename T> inline modular<T> operator+(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) += _rhs; } template <typename T, typename U> inline modular<T> operator+(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) += _rhs; } template <typename T, typename U> inline modular<T> operator+(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) += _rhs; } template <typename T> inline modular<T> operator-(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) -= _rhs; } template <typename T, typename U> inline modular<T> operator-(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) -= _rhs; } template <typename T, typename U> inline modular<T> operator-(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) -= _rhs; } template <typename T> inline modular<T> operator*(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) *= _rhs; } template <typename T, typename U> inline modular<T> operator*(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) *= _rhs; } template <typename T, typename U> inline modular<T> operator*(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) *= _rhs; } template <typename T> inline modular<T> operator/(const modular<T>& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) /= _rhs; } template <typename T, typename U> inline modular<T> operator/(const modular<T>& _lhs, const U& _rhs) { return modular<T>(_lhs) /= _rhs; } template <typename T, typename U> inline modular<T> operator/(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) /= _rhs; } template <typename T> inline bool operator==(const modular<T>& _lhs, const modular<T>& _rhs) { return _lhs.val == _rhs.val; } template <typename T, typename U> inline bool operator==(const modular<T>& _lhs, const U& _rhs) { return _lhs == modular<T>(_rhs); } template <typename T, typename U> inline bool operator==(const U& _lhs, const modular<T>& _rhs) { return modular<T>(_lhs) == _rhs; } template <typename T> inline bool operator!=(const modular<T>& _lhs, const modular<T>& _rhs) { return !(_lhs == _rhs); } template <typename T, typename U> inline bool operator!=(const modular<T>& _lhs, const U& _rhs) { return !(_lhs == _rhs); } template <typename T, typename U> inline bool operator!=(const U& _lhs, const modular<T>& _rhs) { return !(_lhs == _rhs); } string to_string(string s) { return + s + ; } string to_string(bool x) { return (x ? T : F ); } string to_string(modular<int> x) { return to_string(int(x)); } string to_string(const char* s) { return to_string((string)s); } template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } template <typename V> string to_string(V v) { bool f = true; string res = [ ; for (const auto& x : v) { if (!f) res += , ; f = false; res += to_string(x); } res += ] ; return res; } void debug() { cerr << endl; } template <typename H, typename... T> void debug(H h, T... t) { cerr << << to_string(h); debug(t...); } const int N = 5050; modular<int> a[N], c[N], dn[N][N << 1], res[N << 1]; void solve() { int n; cin >> n; for (int i = 1; i < n; i++) { cin >> a[i]; } c[1] = 1; for (int i = 1; i < n; i++) { c[i + 1] = c[i] * a[i]; } for (int i = n; i >= 1; i--) { dn[i][0] = 1; for (int j = 1; i + j <= n; j++) { dn[i][j] = a[i] * dn[i + 1][j - 1]; res[j] += dn[i][j] * c[i]; } } for (int i = 1; i <= n; i++) { dn[i][0] = 1; for (int j = 2 * n - 2; j >= 1; j--) { dn[i][j] = dn[i - 1][j - 1]; res[j] += dn[i][j] * c[i]; dn[i][j] += (a[i] - 1) * dn[i + 1][j - 1]; } } modular<int> half = modular<int>(1) / 2; for (int j = 1; j <= 2 * n - 2; j++) { cout << res[j] * half << ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); cout << endl; }
/** * Author: Daniel * Created Time: 2021-05-20 00:17:05 **/ // time-limit: 2000 // problem-url: https://codeforces.com/contest/1525/problem/E #include <bits/stdc++.h> using namespace std; #define F first #define S second #define ER erase #define IS insert #define PI acos(-1) #define PB pop_back #define MP make_pair #define MT make_tuple #define LB lower_bound #define UB upper_bound #define EB emplace_back #define lowbit(x) (x & -x) #define SZ(x) ((int)x.size()) #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() #define SOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<fixed<<setprecision(10) typedef long long LL; typedef vector<LL> VL; typedef vector<int> VI; typedef vector<bool> VB; typedef pair<LL, LL> PLL; typedef vector<string> VS; typedef vector<double> VD; typedef pair<int, int> PII; typedef unsigned long long ULL; typedef pair<double, double> PDD; typedef vector<pair<LL, LL> > VPLL; typedef vector<pair<int, int> > VPII; template <typename A> using VE = vector<A>; template <typename A> using USET = unordered_set<A>; template <typename A> using HEAP = priority_queue<A>; template <typename A, typename B> using PA = pair<A, B>; template <typename A, typename B> using UMAP = unordered_map<A, B>; template <typename A> using RHEAP = priority_queue<A, vector<A>, greater<A> >; template <typename A> A MAX(const A &a) { return a; } template <typename A> A MIN(const A &a) { return a; } template <typename A> A MAX(const A *a, const A *b) { return *max_element(a, b); } template <typename A> A MIN(const A *a, const A *b) { return *min_element(a, b); } template <typename A, typename... B> A MAX(const A &a, const B&... b) { return max(a, MAX(b...)); } template <typename A, typename... B> A MIN(const A &a, const B&... b) { return min(a, MIN(b...)); } template <typename A, typename B = typename std::iterator_traits<A>::value_type> B MAX(A a, A b) { return *max_element(a, b); } template <typename A, typename B = typename std::iterator_traits<A>::value_type> B MIN(A a, A b) { return *min_element(a, b); } mt19937 rng((unsigned int)chrono::steady_clock::now().time_since_epoch().count()); template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return + s + ; } string to_string(const char* s) { return to_string((string)s); } string to_string(const char c) { return to_string((string) + c); } string to_string(bool b) { return (b ? true : false ); } string to_string(vector<bool> v) { bool first = true; string res = { ; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) res += , ; first = false; res += to_string(v[i]); } res += } ; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ; for (size_t i = 0; i < N; i++) res += static_cast<char>( 0 + v[i]); return res; } template <typename A> string to_string(A v) { bool first = true; string res = { ; for (const auto &x : v) { if (!first) res += , ; first = false; res += to_string(x); } res += } ; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return ( + to_string(get<0>(p)) + , + to_string(get<1>(p)) + , + to_string(get<2>(p)) + ) ; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return ( + to_string(get<0>(p)) + , + to_string(get<1>(p)) + , + to_string(get<2>(p)) + , + to_string(get<3>(p)) + ) ; } void debug_out() { cout << n ; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cout << [ << #__VA_ARGS__ << ]: , debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif /////////////////////////////////////////////////////////////////////////// //////////////////// DO NOT TOUCH BEFORE THIS LINE //////////////////////// /////////////////////////////////////////////////////////////////////////// // check the limitation!!! const int N = 100010, M = 1010, mod = 998244353; int qp(int a, int b) { int res = 1; while (b) { if (b & 1) res = (LL)res * a % mod; a = (LL)a * a % mod; b >>= 1; } return res; } // read the question carefully!!! int main() { SOS; int n, m; cin >> n >> m; VE<VI> d(n, VI(m)); for (auto &u : d) for (auto &v : u) cin >> v; int fact_inv = 1; for (int i = 1; i <= n; i ++ ) fact_inv = (LL)fact_inv * i % mod; fact_inv = qp(fact_inv, mod - 2); int res = 0; for (int j = 0; j < m; j ++ ) { VI cnt(n + 1); for (int i = 0; i < n; i ++ ) cnt[n - d[i][j] + 1] ++ ; int ans = 1, tot = 0; for (int i = 0; i < n; i ++ ) { tot += cnt[i]; ans = (LL)ans * tot % mod; tot = max(0, tot - 1); } res = (res + 1 - (LL)fact_inv * ans) % mod; } res = (res + mod) % mod; cout << res << n ; return 0; } // GOOD LUCK!!!
#include <bits/stdc++.h> using namespace std; long long n, s, a, ans, a11, r; vector<long long> aa, ab, c; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> s >> a; if (s == 11) { a11++; ans += a; } if (s == 0) c.push_back(a); if (s == 10) aa.push_back(a); if (s == 1) ab.push_back(a); } sort(aa.begin(), aa.end()); reverse(aa.begin(), aa.end()); sort(ab.begin(), ab.end()); reverse(ab.begin(), ab.end()); r = min(aa.size(), ab.size()); for (int i = r; i < aa.size(); i++) { c.push_back(aa[i]); } for (int i = r; i < ab.size(); i++) { c.push_back(ab[i]); } sort(c.begin(), c.end()); reverse(c.begin(), c.end()); for (int i = 0; i < min(a11, (long long)c.size()); i++) ans += c[i]; for (int i = 0; i < r; i++) { ans += aa[i]; ans += ab[i]; } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> v(n); for (int i = (int)(0); i < (int)(n); ++i) cin >> v[i]; sort(v.begin(), v.end()); cout << v[(n - 1) / 2] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } const double EPS = 1e-10; const double PI = acos(-1.0); vector<int> x, y; int xo, yo; int n, d; int dp[1000][1000]; int sqr(int a) { return a * a; } int dfs(int x1, int y1) { if (sqr(x1) + sqr(y1) > sqr(d)) return dp[x1 + 500][y1 + 500] = 1; if (dp[x1 + 500][y1 + 500] != -1) return dp[x1 + 500][y1 + 500]; for (int i = (0); i < (int((x).size())); ++i) if (!dfs(x1 + x[i], y1 + y[i])) return dp[x1 + 500][y1 + 500] = 1; return dp[x1 + 500][y1 + 500] = 0; } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); memset(dp, -1, sizeof dp); cin >> xo >> yo >> n >> d; for (int i = (0); i < (n); ++i) { int xi, yi; cin >> xi >> yi; x.push_back(xi); y.push_back(yi); } if (dfs(xo, yo)) cout << Anton << endl; else cout << Dasha << endl; return 0; }
#include <bits/stdc++.h> long long mpow(long long a, long long n, long long mod) { long long ret = 1; long long b = a; while (n) { if (n & 1) ret = (ret * b) % mod; b = (b * b) % mod; n >>= 1; } return (long long)ret; } using namespace std; vector<int> v, v1; pair<int, int> p[100004]; int a[100004], b[100004]; int main() { int n, i, j; cin >> n; for (j = 0; j < n; j++) { cin >> p[j].first; p[j].second = j; } sort(p, p + n); long long z = 0; int need = n - (n + 2) / 3; for (j = n - need; j < n; j++) { if (z % 2 == 0) { a[p[j].second] = p[j].first - z / 2; b[p[j].second] = z / 2; } else { a[p[j].second] = z / 2; b[p[j].second] = p[j].first - z / 2; } z++; } for (j = 0; j < n - need; j++) { a[p[j].second] = 0; b[p[j].second] = p[j].first; } cout << YES << endl; for (j = 0; j < n; j++) cout << a[j] << ; cout << endl; for (j = 0; j < n; j++) cout << b[j] << ; return 0; }
#include <bits/stdc++.h> using namespace std; int n, k, ans = -1; int a1[1000001], b1[1000001], a[1000001], b[1000001], t[1000001], d[1000001], in[1000001], Max[2000001]; int s = 1; int getmax(int v, int vl, int vr, int l, int r) { if (l > vr || r < vl) { return 0; } if (vl >= l && vr <= r) { return Max[v]; } return max(getmax(v * 2, vl, (vl + vr) / 2, l, r), getmax(v * 2 + 1, (vl + vr) / 2 + 1, vr, l, r)); } void update(int v) { v /= 2; if (v <= 0) { return; } Max[v] = max(Max[v * 2], Max[v * 2 + 1]); update(v); } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a1[i]; t[a1[i]] = i + 1; } for (int i = 0; i < n; i++) { cin >> b1[i]; } for (int i = 0; i < n; i++) { a[i] = i; b[i] = t[b1[i]]; } while (n > s) { s *= 2; k++; } for (int i = 1; i < 2 * s; i++) { Max[i] = 0; } for (int i = 0; i < n; i++) { d[i] = getmax(1, 1, s, b[i], n) + 1; Max[s + b[i] - 1] = d[i]; update(s + b[i] - 1); } for (int i = 0; i < n; i++) { ans = max(ans, d[i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3fffffff; const int SINF = 0x7fffffff; const long long LINF = 0x3fffffffffffffff; const long long SLINF = 0x7fffffffffffffff; const long double DINF = pow(2, 100); const int MAXN = 1007; const int MOD = static_cast<int>(1e9 + 7); const long double EPS = 1e-11; struct cT { cT() : x(0), y(0) {} cT(long double _x, long double _y) : x(_x), y(_y) {} cT operator+(const cT &a) const { return cT(x + a.x, y + a.y); } cT operator-(const cT &a) const { return cT(x - a.x, y - a.y); } cT operator/(const int a) { return cT(x / a, y / a); } bool operator==(const cT &a) const { return ((((x - a.x) >= 0) ? (x - a.x) : (-(x - a.x))) < EPS && (((y - a.y) >= 0) ? (y - a.y) : (-(y - a.y))) < EPS); } bool operator!=(const cT &a) const { return !((*this) == a); } bool operator<(const cT &a) const { return ((((x - a.x) >= 0) ? (x - a.x) : (-(x - a.x))) > EPS) ? x < a.x : y < a.y - EPS; } long double getk() { return (((x) >= 0) ? (x) : (-(x))) < EPS ? DINF : (y / x); } long double x, y; } cr[MAXN]; struct dT { dT() : k(0) {} dT(cT _cr, long double _k) : cr(_cr), k(_k) {} bool operator<(const dT &a) const { return (cr != a.cr) ? cr < a.cr : k < a.k - EPS; } cT cr; long double k; } med[MAXN * MAXN]; int n; int km; void init(); void input(); void work(); int getans(int l, int r); int add(int x, int y) { x += y; if (x >= MOD) x -= MOD; return x; } void addv(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } int dec(int x, int y) { x -= y; if (x < 0) x += MOD; return x; } void decv(int &x, int y) { x -= y; if (x < 0) x += MOD; } int main() { init(); input(); work(); } void init() { ios::sync_with_stdio(false); } void input() { scanf( %d , &n); int a, b, c, d; for (int i = 1; i <= n; ++i) { scanf( %d%d%d%d , &a, &b, &c, &d); cr[i].x = static_cast<long double>(a * b * d * d) / (a * a * d * d + b * b * c * c); cr[i].y = static_cast<long double>(c * d * b * b) / (a * a * d * d + b * b * c * c); } } void work() { for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { med[++km] = dT((cr[i] + cr[j]) / 2, (cr[j] - cr[i]).getk()); } } sort(med + 1, med + 1 + km); med[km + 1] = dT(cT(DINF, DINF), 1); int pre = 1, ans = 0; for (int i = 1; i <= km + 1; ++i) { if (med[i].cr != med[pre].cr) { addv(ans, getans(pre, i - 1)); pre = i; } } cout << ans << endl; } int getans(int l, int r) { int pre = l; int ans = 1; for (int i = l; i <= r; ++i) { if ((((med[i].k - med[pre].k) >= 0) ? (med[i].k - med[pre].k) : (-(med[i].k - med[pre].k))) > EPS) { ans = static_cast<long long>(ans) * (i - pre + 1) % MOD; pre = i; } } ans = static_cast<long long>(ans) * (r - pre + 2) % MOD; return dec(ans, r - l + 2); }
#include <bits/stdc++.h> using namespace std; char s[1010]; int main() { int f1 = 0, f2 = 0; scanf( %s , s); int len = strlen(s); for (int i = 0; i < len; i++) { if (s[i] == 0 ) { if (f1 == 0) { printf( 1 1 n ); f1 = 1; } else { printf( 3 1 n ); f1 = 0; } } if (s[i] == 1 ) { if (f2 == 0) { printf( 4 3 n ); f2 = 1; } else { printf( 4 1 n ); f2 = 0; } } } }
#include <bits/stdc++.h> using namespace std; int oo = 1000000000; long long n; long long a[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; if (n == 1) { cout << 1 << << 1 << endl; cout << -a[1] << endl; a[1] = 0; cout << 1 << << 1 << endl; cout << -a[1] << endl; cout << 1 << << 1 << endl; cout << -a[1] << endl; return 0; } cout << 1 << << 1 << endl; cout << n - (a[1] % n) << endl; a[1] += n - (a[1] % n); cout << 2 << << n << endl; for (int i = 2; i <= n; i++) { cout << (a[i] % n) * (n - 1) << ; a[i] += (a[i] % n) * (n - 1); } cout << endl; cout << 1 << << n << endl; for (int i = 1; i <= n; i++) cout << -a[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> me; long long sum(long long x) { return (x * (x + 1)) / 2; } int main() { int k; string s; cin >> k >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == 1 ) { me.push_back(i); } } long long ans = 0, cnt = 0, idx = -1; if (k == 0) { for (int i = 0; i < s.size(); i++) { if (s[i] == 1 ) { ans += sum(cnt); cnt = 0; } else { cnt++; } } ans += sum(cnt); } else { long long fst = -1; for (int i = 0; i < me.size(); i++) { int idx = me[i]; cnt = 0; if ((i + k - 1) == me.size() - 1) { ans += ((idx - fst) * (s.size() - me[i + k - 1])); } else if (i + k - 1 < me.size() - 1) { ans += ((idx - fst) * (me[i + k] - me[i + k - 1])); } fst = me[i]; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int f[50][50], mn[50], mx[50]; const int INF = 1e9; int gm, gg, sm, ss; long long d[51][51] = {}, nd[51][51]; bool cang[50], cans[50], canb[50]; int main() { scanf( %d%d , &n, &m); for (int i = 0; i < (int)(n); ++i) for (int j = 0; j < (int)(n); ++j) f[i][j] = INF; for (int i = 0; i < (int)(n); ++i) f[i][i] = 0; for (int i = 0; i < (int)(m); ++i) { int from, to, w; scanf( %d%d%d , &from, &to, &w), --from, --to; f[from][to] = min(f[from][to], w); f[to][from] = min(f[to][from], w); } scanf( %d%d%d%d , &gm, &gg, &sm, &ss); for (int k = 0; k < (int)(n); ++k) for (int i = 0; i < (int)(n); ++i) for (int j = 0; j < (int)(n); ++j) { f[i][j] = min(f[i][j], f[i][k] + f[k][j]); } for (int i = 0; i < (int)(n); ++i) { mn[i] = INF, mx[i] = -INF; for (int j = 0; j < (int)(n); ++j) if (i != j) { mn[i] = min(mn[i], f[i][j]); mx[i] = max(mx[i], f[i][j]); } } long long ans = 0; for (int gco = 0; gco < (int)(n); ++gco) for (int bco = 0; bco < (int)(n); ++bco) if (mn[gco] < mx[bco] || (mn[gco] == mx[bco] && gco < bco)) { if (gco == bco) continue; bool ok = true; for (int i = 0; i < (int)(n); ++i) { if (i == gco) { cang[i] = true; cans[i] = canb[i] = false; } else if (i == bco) { canb[i] = true; cang[i] = cans[i] = false; } else { cang[i] = mn[i] < mn[gco] || (mn[i] == mn[gco] && i < gco); canb[i] = mx[i] > mx[bco] || (mx[i] == mx[bco] && i > bco); cans[i] = false; for (int j = 0; j < (int)(n); ++j) if (j != i && (f[i][j] > mn[gco] || (f[i][j] == mn[gco] && i > gco)) && (f[i][j] < mx[bco] || (f[i][j] == mx[bco] && i < bco))) { cans[i] = true; break; } if (!cang[i] && !cans[i] && !canb[i]) { ok = false; break; } } } if (!ok) continue; memset(d, 0, sizeof d); d[0][0] = 1; for (int i = 0; i < (int)(n); ++i) { memset(nd, 0, sizeof nd); for (int g = 0; g < (int)(gg + 1); ++g) for (int s = 0; s < (int)(ss + 1); ++s) if (d[g][s] != 0) { if (cang[i]) { nd[g + 1][s] += d[g][s]; } if (cans[i]) { nd[g][s + 1] += d[g][s]; } if (canb[i]) { nd[g][s] += d[g][s]; } } memcpy(d, nd, sizeof nd); } for (int g = gm; g <= gg; ++g) for (int s = sm; s <= ss; ++s) { ans += d[g][s]; } d[gg][ss] -= ans; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; struct P { long long x, y; P() {} P(long long _x, long long _y) : x(_x), y(_y) {} P operator+(const P &o) { return P(x + o.x, y + o.y); } P operator-(const P &o) { return P(x - o.x, y - o.y); } long long operator*(const P &o) { if (!x && !o.x) return 0; if (!x) return -y; if (!o.x) return o.y; if (fabs(1.0 * o.y / o.x - 1.0 * y / x) < 1e-10) { return 0; } if (1.0 * y / x < 1.0 * o.y / o.x) { return 1; } else { return -1; } } bool operator<(const P &o) const { return x == o.x ? y < o.y : x < o.x; } }; struct E { int tot; int lnk[N], nxt[N << 1], son[N << 1]; P w[N << 1]; E() { tot = 1; } void add(int x, int y, P pt) { nxt[++tot] = lnk[x], son[tot] = y, w[tot] = pt, lnk[x] = tot; } void adde(int x, int y, P pt) { add(x, y, pt), add(y, x, pt); } } S, T; int n, m, k, all, mx, gid, gu, gv; int cnt, na, nb, s1, s2, qaq; int las[N], sz[N], dl[N]; P pa[N << 3], qa[N << 3], qb[N << 3], ans[N << 3]; void build(int x, int p) { for (int j = S.lnk[x]; j; j = S.nxt[j]) { if (S.son[j] != p) { T.adde(las[x], ++k, P(0, 0)); T.adde(k, S.son[j], S.w[j]); las[x] = k; build(S.son[j], x); } } } void dfs_sz(int x, int p) { sz[x] = 1; for (int j = T.lnk[x], v; j; j = T.nxt[j]) { if (!dl[j >> 1] && T.son[j] != p) { dfs_sz(T.son[j], x); sz[x] = sz[x] + sz[T.son[j]]; v = min(sz[T.son[j]], all - sz[T.son[j]]); if (mx < v) { mx = v; gid = j; gu = x, gv = T.son[j]; } } } } void dfs_calc(int x, int p, P pt) { pa[++cnt] = pt; for (int j = T.lnk[x]; j; j = T.nxt[j]) { if (!dl[j >> 1] && T.son[j] != p) { dfs_calc(T.son[j], x, pt + T.w[j]); } } } void making(int &num, P *pt) { sort(pa + 1, pa + cnt + 1); pt[num = 1] = pa[1]; for (int i = 2; i <= cnt; ++i) { for (; num >= 2; --num) { if ((pa[i] - pt[num - 1]) * (pt[num] - pt[num - 1]) > 0) { break; } } pt[++num] = pa[i]; } } void merge() { if (!na || !nb) { for (int i = 1, t = max(na, nb); i <= t; ++i) { ans[++qaq] = na ? qa[i] : qb[i]; } return; } ans[++qaq] = qa[1] + qb[1]; for (int u = 2, v = 2; u <= na || v <= nb;) { if (u > na) { ans[++qaq] = qa[u - 1] + qb[v++]; } else if (v > nb) { ans[++qaq] = qa[u++] + qb[v - 1]; } else { P du = qa[u] - qa[u - 1], dv = qb[v] - qb[v - 1]; if (du * dv < 0) { ans[++qaq] = qa[u++] + qb[v - 1]; } else { ans[++qaq] = qa[u - 1] + qb[v++]; } } } } void divide(int x, int t) { if (t == 1) { return; } all = t, mx = gu = gv = 0; dfs_sz(x, 0); int x1 = gv, s1 = sz[gv], x2 = gu, s2 = all - s1; dl[gid >> 1] = 1; cnt = 0, dfs_calc(x1, x2, P(0, 0)), making(na, qa); cnt = 0, dfs_calc(x2, x1, T.w[gid]), making(nb, qb); merge(); divide(x1, s1); divide(x2, s2); } long long val(P pt, int sl) { return pt.x * sl + pt.y; } int main() { scanf( %d%d , &n, &m), k = n; for (int i = 1, x, y, z1, z2; i < n; ++i) { scanf( %d%d%d%d , &x, &y, &z1, &z2); S.adde(x, y, P(z1, z2)); } for (int i = 1; i <= n; ++i) { las[i] = i; } build(1, 0); divide(1, k); cnt = qaq; memcpy(pa, ans, sizeof pa); making(qaq, ans); for (int i = 0, cur = 1; i < m; ++i) { for (; cur < qaq; ++cur) { if (val(ans[cur], i) > val(ans[cur + 1], i)) { break; } } printf( %lld , val(ans[cur], i)); putchar(i < m - 1 ? : n ); } return 0; }
#include <bits/stdc++.h> using namespace std; bool Check(long long A, long long B, long long T1, long long T2) { if (!A && !B) return !T1 && !T2; return !((T1 * A + T2 * B) % (A * A + B * B)) && !((T2 * A - T1 * B) % (A * A + B * B)); } int main() { long long X0, Y0, X1, Y1, X2, Y2; cin >> X0 >> Y0 >> X1 >> Y1 >> X2 >> Y2; cout << ((Check(X2, Y2, X1 - X0, Y1 - Y0) || Check(X2, Y2, Y1 - X0, -X1 - Y0) || Check(X2, Y2, -X1 - X0, -Y1 - Y0) || Check(X2, Y2, -Y1 - X0, X1 - Y0)) ? YES : NO ) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); ; cin.tie(NULL); ; int n, b; double total = 0, maxx = 0; cin >> n >> b; double ar[n]; for (int i = 0; i < n; i++) { cin >> ar[i]; total += ar[i]; maxx = max(maxx, ar[i]); } if (b + total < maxx * n) { cout << -1 << endl; } else { double e = (b + total) / n; for (int i = 0; i < n; i++) { cout << fixed << setprecision(6) << e - ar[i] << endl; } } }
#include <bits/stdc++.h> using namespace std; int main() { long long int l, r; cin >> l >> r; cout << YES << endl; for (long long int i = l; i <= r; i += 2) { cout << i << << i + 1 << endl; } }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long n, m; char s[111111], t[111111]; bool ar[111111]; long long fail[111111], f[111111], ff[111111], dp[111111]; void kmp() { int i = 0, j = -1; fail[0] = -1; while (i < m) { while (j > -1 && t[i] != t[j]) j = fail[j]; i++, j++; fail[i] = j; } i = 1, j = 0; while (i <= n) { while (j > -1 && s[i] != t[j]) j = fail[j]; j++; if (j == m) ar[i] = 1, j = fail[j]; i++; } } int main() { scanf( %s , s + 1); scanf( %s , t); n = strlen(s + 1); m = strlen(t); kmp(); for (int i = 1; i <= n; i++) dp[i] = (ar[i] ? i - m + 1 : dp[i - 1]); for (int i = 1; i <= n; i++) { int l = dp[i]; if (l) { f[i] = (f[i - 1] + l) % mod; f[i] = (f[i] + ff[l - 1]) % mod; ff[i] = (ff[i - 1] + f[i]) % mod; } } cout << f[n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const int dx4[] = {-1, 0, 1, 0}; const int dy4[] = {0, 1, 0, -1}; const int dx8[] = {-1, -1, -1, 0, 0, 1, 1, 1}; const int dy8[] = {-1, 0, 1, -1, 1, -1, 0, 1}; const int dxk[] = {-2, -2, -1, -1, 1, 1, 2, 2}; const int dyk[] = {-1, 1, -2, 2, -2, 2, -1, 1}; inline long long Int() { long long inputNumber = 0; char c = getchar_unlocked(); bool sign = true; while (!((c >= 0 && c <= 9 ) || c == - )) c = getchar_unlocked(); if (c == - ) sign = false, c = getchar_unlocked(); while (c >= 0 && c <= 9 ) inputNumber = (inputNumber << 1) + (inputNumber << 3) + c - 0 , c = getchar_unlocked(); return sign ? inputNumber : -inputNumber; } template <class S, class T> ostream& operator<<(ostream& out, const pair<S, T>& Pair) { out << { << Pair.first << , << Pair.second << } ; return out; } template <class T> ostream& operator<<(ostream& out, const vector<T>& Vector) { out << [ ; for (int i = 0; i < (int)Vector.size(); ++i) { out << Vector[i]; if (i + 1 < (int)Vector.size()) out << , ; } out << ] ; return out; } template <class S, class T> ostream& operator<<(ostream& out, const map<S, T>& Map) { out << [ n ; for (const auto& x : Map) { out << { Key: << x.first << , Value: << x.second << } n ; } out << ] n ; return out; } template <class T> ostream& operator<<(ostream& out, const multiset<T>& Set) { out << { ; for (auto it = Set.begin(); it != Set.end();) { out << *it; it++; if (it != Set.end()) out << , ; } out << } ; return out; } void solve(); int main() { solve(); } const double eps = 1e-10; const int mod = 1e9 + 7; const int N = 2e5 + 5; void solve() { string s; cin >> s; int len = ((int)(s).size()); string sd = ; string org = ; int br = -1; for (int i = len - 1; i >= 0; --i) { if (s[i] != a ) { sd += s[i]; } else { br = i; break; } } if (sd == ) { bool ok = true; for (int i = 0; i < (((int)(s).size())); ++i) { if (s[i] != a ) { ok = false; break; } } if (ok) { cout << s; } else { puts( :( ); } return; } reverse((sd).begin(), (sd).end()); for (int i = 0; i <= br; ++i) { org += s[i]; } string strip = ; for (int i = 0; i < (((int)(org).size())); ++i) if (org[i] != a ) strip += org[i]; if (strip == sd) { cout << org; return; } string tot = strip + sd; if (((int)(tot).size()) % 2) { puts( :( ); return; } int mid = ((int)(tot).size()) / 2; string a = tot.substr(0, mid); string b = tot.substr(mid); if (a != b) { puts( :( ); return; } reverse((s).begin(), (s).end()); reverse((a).begin(), (a).end()); if (s.substr(0, mid) == a) { reverse((s).begin(), (s).end()); int len = ((int)(s).size()) - mid; for (int i = 0; i < (len); ++i) putchar(s[i]); return; } puts( :( ); }
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9; const long long inf64 = 1e18; const long long MOD = inf + 7; long long a[100005], p[100005]; long long n, m; bool check(long long mid) { long long last = 0; for (long long i = 0; i < (long long)n; i++) { if (last == m) return true; if (p[last] > a[i]) { while (last < m && p[last] - a[i] <= mid) last++; continue; } long long mn = a[i] - mid; if (mn > p[last]) return false; long long id1 = upper_bound(p, p + m, a[i] + mid - 2 * (a[i] - p[last])) - p; long long id2 = upper_bound(p, p + m, a[i] + (mid - (a[i] - p[last])) / 2) - p; last = max(last, id1); last = max(last, id2); } return (last == m); } int32_t main() { ios::sync_with_stdio(false), cin.tie(nullptr); ; cin >> n >> m; for (long long i = 0; i < (long long)n; i++) cin >> a[i]; for (long long i = 0; i < (long long)m; i++) cin >> p[i]; long long ans, low = 0, high = 1e18, mid; while (low <= high) { mid = (low + high) / 2; if (check(mid)) { ans = mid; high = mid - 1; } else { low = mid + 1; } } cout << ans << n ; }
#include <bits/stdc++.h> using namespace std; const int N = 1007, K = 1007, INF = LONG_MAX; int i, j, n, x, y; int gcd(int x, int y) { while (x && y) { if (x > y) x %= y; else y %= x; } return x | y; } multiset<int> m; vector<int> ans; int main() { cin >> n; for (i = 0; i < n * n; i++) { cin >> x; m.insert(x); } ans.push_back(*--m.end()); m.erase(--m.end()); while (!m.empty()) { x = *--m.end(); m.erase(--m.end()); for (j = 0; j < ans.size(); j++) { int v = gcd(x, ans[j]); m.erase(m.find(v)); m.erase(m.find(v)); } ans.push_back(x); } for (i = 0; i < ans.size(); i++) { cout << ans[i] << ; } }
#include <bits/stdc++.h> using namespace std; const int N = 1e7 + 5; const int M = 25; int A[M], B[M], n, m, x, y, cnt; int dp[M * 4][M * 4]; void Min(int &x, int y) { x = min(x, y); } int main() { cin >> n >> m; for (int i = 0; i < n; ++i) cin >> A[i]; for (int i = 0; i < n; ++i) cin >> B[i]; memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; for (int i = 0; i < n * 4; ++i) { for (int j = 0; j <= n + n; ++j) if (dp[i][j] <= 1e9) { Min(dp[i + 1][j + 1], dp[i][j] + A[i % n]); if (j) Min(dp[i + 1][j - 1], dp[i][j] + B[i % n]); } } if (m <= 3) cout << dp[n * m][0] << endl; else cout << dp[n * 4][0] + (m - 4) / 2 * (dp[n * 4][0] - dp[n * 2][0]) << endl; }
#include <bits/stdc++.h> using namespace std; const double pi = 2 * acos(0.0); const int INF = 0x3f3f3f3f; const double inf = 1.0 / 0.0; void solve(); void Adi() { ios::sync_with_stdio(false); cin.tie(0); } int main() { Adi(); solve(); cerr << time taken : << (float)clock() / CLOCKS_PER_SEC << secs << n ; return 0; } void solve() { long long n, x, tt = 0, dis = 0, rem = 0; char s; cin >> n >> x; vector<long long> v; for (int i = 0; i < n; i++) { long long q; cin >> s >> q; if (s == - ) q = -q; v.push_back(q); } for (int i = 0; i < n; i++) { if (v[i] >= 0) x += v[i]; else if (v[i] < 0) { if (x >= abs(v[i])) x += v[i]; else dis++; } } cout << x << << dis; }
#include <bits/stdc++.h> using namespace std; int main() { int x, y, z, t1, t2, t3; cin >> x >> y >> z >> t1 >> t2 >> t3; unsigned long long xy, xz; if (x > y) { xy = x - y; } else { xy = y - x; } if (x > z) { xz = x - z; } else { xz = z - x; } unsigned long long stair = xy * t1; unsigned long long lift = (xz + xy) * t2 + t3 * 3; if (lift > stair) { cout << NO ; } else { cout << YES ; } return 0; }
#include <bits/stdc++.h> int mat[1010][1010]; int counts = 0; int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) scanf( %d , &mat[i][j]); } counts = 0; for (int i = 1; i <= n; i++) counts += mat[i][i]; counts %= 2; int m; scanf( %d , &m); for (int i = 1; i <= m; i++) { int a; scanf( %d , &a); if (a == 1 || a == 2) { int b; scanf( %d , &b); mat[b][b] = 1 - mat[b][b]; counts = 1 - counts; } else printf( %d , counts); } printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 20; int x[maxn], y[maxn]; int main() { int n, m; while (cin >> n >> m) { for (int i = 1; i <= n; i++) scanf( %d , &x[i]); for (int j = 1; j <= m; j++) scanf( %d , &y[j]); int ans = 0; int p1 = 1, p2 = 1; long long sum1 = 0, sum2 = 0; while (p1 <= n || p2 <= m) { if (sum1 <= sum2) { if (p1 > n) break; sum1 += x[p1++]; } else { if (p2 > m) break; sum2 += y[p2++]; } if (sum1 == sum2) ans++; } printf( %d n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int w, h, k; cin >> w >> h >> k; int s = 0; w -= 2; for (int i = 0; i < k; i++) { s += (w + h) * 2; w -= 4; h -= 4; } cout << s; }
#include <bits/stdc++.h> using namespace std; long long w[100005], mon[305], droz[305], wlazi[305], t; long long dfs(long long co) { long long x; if (!droz[co]) return mon[co]; x = dfs(droz[co]); t -= x; mon[co] += x; return mon[co]; } int main() { long long n, q, a, b, c, mod = 1000000007; scanf( %I64d%I64d%I64d , &n, &q, &t); for (a = 1; a <= n; a++) scanf( %I64d , &mon[a]); while (q--) { scanf( %I64d%I64d , &a, &b); droz[b] = a; wlazi[a] = 1; } for (a = 1; a <= n; a++) { for (b = droz[a]; b && b != a; b = droz[b]) ; if (b == a) { printf( 0 n ); return 0; } } for (a = 1; a <= n; a++) if (!wlazi[a]) dfs(a); if (t < 0) { printf( 0 n ); return 0; } w[0] = 1; for (a = 1; a <= n; a++) { for (b = mon[a]; b <= t; b++) { w[b] += w[b - mon[a]]; w[b] %= mod; } } printf( %I64d n , w[t]); getchar(); getchar(); return 0; }
#include <bits/stdc++.h> const bool DEBUG = false; using namespace std; const long long maxn = 2e5 + 5; const long long INF = 1e18 + 1; const long long M = 1e9 + 7; const int lg = 21; int n, m, x[maxn], b[maxn], a[maxn], y[maxn]; vector<pair<int, int> > v; set<int> s; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; for (int i = 0; i < m; i++) cin >> a[i] >> b[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[j] == b[j] or x[i] == y[i]) continue; if (a[j] == x[i] and b[j] == y[i]) continue; if (a[j] == y[i] and b[j] == x[i]) continue; if (a[j] != x[i] and a[j] != y[i] and b[j] != x[i] and b[j] != y[i]) continue; if (a[j] == x[i] or a[j] == y[i]) s.insert(a[j]); else s.insert(b[j]); } } if (s.size() == 1) return cout << (*s.begin()) << endl, 0; for (int i = 0; i < n; i++) { vector<int> v; set<int> s; for (int j = 0; j < m; j++) { if (a[j] == b[j] or x[i] == y[i]) continue; if (a[j] == x[i] and b[j] == y[i]) continue; if (a[j] == y[i] and b[j] == x[i]) continue; if (a[j] != x[i] and a[j] != y[i] and b[j] != x[i] and b[j] != y[i]) continue; if (x[i] == a[j] or x[i] == b[j]) s.insert(x[i]); else s.insert(y[i]); } if (s.size() > 1) return cout << -1 << endl, 0; } for (int i = 0; i < m; i++) { vector<int> v; set<int> s; for (int j = 0; j < n; j++) { if (a[i] == b[i] or x[j] == y[j]) continue; if (a[i] == x[j] and b[i] == y[j]) continue; if (a[i] == y[j] and b[i] == x[j]) continue; if (a[i] != x[j] and a[i] != y[j] and b[i] != x[j] and b[i] != y[j]) continue; if (x[j] == a[i] or a[i] == y[j]) s.insert(a[i]); else s.insert(b[i]); } if (s.size() > 1) return cout << -1 << endl, 0; } cout << 0 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int list[100000]; for (int i = 0; i < n; i++) { cin >> list[i]; } int max = 1; int temp = 1; for (int i = 0; i < n - 1; i++) { if (list[i] < list[i + 1]) { temp++; } else { if (max < temp) { max = temp; } temp = 1; } } if (max < temp) { max = temp; } cout << max << n ; }
#include <bits/stdc++.h> using namespace std; long long T, A, B, res; vector<long long> P; int main() { cin >> T >> A >> B; if (T == 1) { if (A == 1) { if (B == 1) { res = -1; } } else { if (B % A == 0) { ++res; } while (B > 0) { P.push_back(B % A); B /= A; } long long cp = 1; for (int i = 0; i < ((int)P.size()); i++) { A -= P[i] * cp; cp *= T; } if (A == 0) { ++res; } } } else { if (A < T) { if (B == A) { res = 1; } } else if (A == T) { if (B == A) { res = 2; } } else { if (A == B) { ++res; } while (B > 0) { P.push_back(B % A); B /= A; } long long cp = 1; for (int i = 0; i < ((int)P.size()); i++) { A -= P[i] * cp; cp *= T; } if (A == 0) { ++res; } } } if (res == -1) { cout << inf n ; } else { cout << res << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1000 * 1000 * 1000 + 7; const int N = 1000 * 1000 + 7; long long a[N]; multiset<long long> st; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) scanf( %I64d , a + i); long long ans = 0; for (int i = 0; i < n; ++i) { if (st.size() > 0) { if (*st.begin() < a[i]) { ans += a[i] - *st.begin(); st.erase(st.begin()); st.insert(a[i]); } } st.insert(a[i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, a[200010], cnt, id, k[200010]; double ans = -1, pre[200010]; double f(long long x, long long mid) { return ((pre[n] - pre[n - x]) + (pre[mid] - pre[mid - x - 1])) / (x * 2 + 1); } signed main() { scanf( %lld , &n); for (long long i = 1; i <= n; i++) { scanf( %lld , &a[i]); } sort(a + 1, a + 1 + n); for (long long i = 1; i <= n; i++) { pre[i] = pre[i - 1] + a[i]; } for (long long i = 1; i <= n; i++) { long long l = 1, r = min(i - 1, n - i); long long num = 0; while (l <= r) { long long mid = (l + r) / 2; if (f(mid, i) >= f(mid - 1, i)) l = mid + 1, num = mid; else r = mid - 1; } if (f(num, i) - (pre[i] - pre[i - 1]) > ans) { ans = f(num, i) - (pre[i] - pre[i - 1]); cnt = num, id = i; } } printf( %lld n , 2 * cnt + 1); for (long long i = id - cnt; i <= id; i++) printf( %lld , a[i]); for (long long i = n - cnt + 1; i <= n; i++) printf( %lld , a[i]); return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } const int MAX = 1e5; const long long MOD = 1e9 + 7; const long long inf = 1e18; int a[MAX + 5]; int main() { ios_base::sync_with_stdio(false); int n, m, r, i, j; long long ans = 0; cin >> n >> m; a[0] = 1; for ((i) = (1); (i) <= (m); ++i) cin >> a[i]; i = 0; while (i < m) { if (a[i + 1] > a[i]) ans += (a[i + 1] - a[i]); else if (a[i + 1] < a[i]) ans += (n - a[i] + a[i + 1]); i++; } cout << ans; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e5 + 10; class Edge { public: long long dest, weight, cost; Edge() { dest = 0; weight = 0; cost = 0; } }; class Compare { public: bool operator()(pair<long long, long long>& a, pair<long long, long long>& b) { long long cura = a.first * a.second; long long curb = b.first * b.second; long long futa = (a.first / 2) * a.second; long long futb = (b.first / 2) * b.second; return (cura - futa) < (curb - futb); } }; vector<Edge> adj[MAXN]; long long numLeaves[MAXN], par[MAXN]; bool visited[MAXN]; void dfs(long long cur) { visited[cur] = true; if (adj[cur].size() == 1 && visited[adj[cur][0].dest]) { numLeaves[cur] = 1; return; } numLeaves[cur] = 0; for (auto i : adj[cur]) { if (!visited[i.dest]) { par[i.dest] = cur; dfs(i.dest); numLeaves[cur] += numLeaves[i.dest]; } } } void solve() { long long n, s; cin >> n >> s; for (long long i = 0; i < n + 1; i++) { adj[i].clear(); numLeaves[i] = 0; par[i] = -1; visited[i] = false; } for (long long i = 1; i < n; i++) { long long x, y, w, c; cin >> x >> y >> w >> c; x--, y--; Edge cur; cur.dest = y; cur.weight = w; cur.cost = c; adj[x].push_back(cur); cur.dest = x; adj[y].push_back(cur); } dfs(0); priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, Compare> oneCost, twoCost; long long sum = 0; for (long long i = 0; i < n; i++) { for (auto j : adj[i]) { if (par[j.dest] == i) { sum += (numLeaves[j.dest]) * (j.weight); if (j.cost == 1) oneCost.push(make_pair(j.weight, numLeaves[j.dest])); else twoCost.push(make_pair(j.weight, numLeaves[j.dest])); } } } vector<long long> oneDif, twoDif; while (!oneCost.empty()) { pair<long long, long long> cur = oneCost.top(); oneCost.pop(); long long fut = (cur.first / 2) * (cur.second); long long dif = (cur.first) * (cur.second) - fut; oneDif.push_back(dif); cur.first /= 2; if (cur.first != 0) oneCost.push(cur); } while (!twoCost.empty()) { pair<long long, long long> cur = twoCost.top(); twoCost.pop(); long long fut = (cur.first / 2) * (cur.second); long long dif = (cur.first) * (cur.second) - fut; twoDif.push_back(dif); cur.first /= 2; if (cur.first != 0) twoCost.push(cur); } long long cost = LLONG_MAX; for (long long i = 1; i < oneDif.size(); i++) oneDif[i] += oneDif[i - 1]; for (long long i = 1; i < twoDif.size(); i++) twoDif[i] += twoDif[i - 1]; if (sum <= s) cost = 0; long long l = 0, h = twoDif.size() - 1; long long ans = -1; while (l <= h) { long long mid = (l + h) / 2; if (sum - twoDif[mid] <= s) { ans = mid; h = mid - 1; } else l = mid + 1; } if (ans != -1) { cost = min(cost, 2 * (ans + 1)); } for (long long i = 0; i < oneDif.size(); i++) { long long l = 0, h = twoDif.size() - 1; long long cur = sum - oneDif[i]; if (cur <= s) { cost = min(cost, i + 1); break; } long long ans = -1; while (l <= h) { long long mid = (l + h) / 2; if (cur - twoDif[mid] <= s) { ans = mid; h = mid - 1; } else l = mid + 1; } if (ans != -1) { long long temp = (i + 1) + (ans + 1) * 2; cost = min(temp, cost); } } cout << cost << n ; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; long long n, mx, now, ans; int main() { scanf( %lld , &n); mx = 2 * n - 1; while (now * 10 + 9 <= mx) now = now * 10 + 9; for (long long k = now; k <= mx; k += now + 1) { long long d = max(k - n, 1ll), u = max(k - d, d); ans += (u - d + 1) / 2; } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const long long mo = 1e9 + 7; int n, m, k, nn; template <typename T> inline void read(T &X) { X = 0; int w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == - ; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); if (w) X = -X; } vector<pair<int, int>> vc[3], vv[3], ys[maxn * 5]; int sum[(maxn * 5) << 2], fg[(maxn * 5) << 2]; void build(int rt, int l, int r, int v) { sum[rt] = fg[rt] = v; if (l == r) { return; } int mid = (l + r) >> 1; build((rt << 1), l, mid, v); build((rt << 1 | 1), mid + 1, r, v); } void pu(int rt) { sum[rt] = sum[(rt << 1)] + sum[(rt << 1 | 1)]; } void pd(int rt) { if (fg[rt]) { sum[(rt << 1)] += fg[rt]; sum[(rt << 1 | 1)] += fg[rt]; fg[(rt << 1)] += fg[rt]; fg[(rt << 1 | 1)] += fg[rt]; fg[rt] = 0; } } void pdd(int rt) { if (fg[rt]) { sum[(rt << 1)] = sum[rt]; sum[(rt << 1 | 1)] = sum[rt]; fg[(rt << 1)] = fg[(rt << 1 | 1)] = 1; fg[rt] = 0; } } void update(int rt, int l, int r, int ql, int qr, int v) { if (ql <= l && r <= qr) { sum[rt] = v; fg[rt] = 1; return; } pdd(rt); int mid = (l + r) >> 1; if (ql <= mid) update((rt << 1), l, mid, ql, qr, v); if (qr > mid) update((rt << 1 | 1), mid + 1, r, ql, qr, v); } void add(int rt, int l, int r, int p, int v) { if (p <= l && r <= p) { sum[rt] += v; fg[rt] += v; return; } pd(rt); int mid = (l + r) >> 1; if (p <= mid) add((rt << 1), l, mid, p, v); if (p > mid) add((rt << 1 | 1), mid + 1, r, p, v); pu(rt); } int query1(int rt, int l, int r, int q) { if (q <= l && r <= q) { return sum[rt]; } pdd(rt); int mid = (l + r) >> 1; if (q <= mid) return query1((rt << 1), l, mid, q); if (q > mid) return query1((rt << 1 | 1), mid + 1, r, q); } long long query2(int rt, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) { return sum[rt]; } pd(rt); int mid = (l + r) >> 1; long long ans = 0; if (ql <= mid) ans = query2((rt << 1), l, mid, ql, qr); if (qr > mid) ans += query2((rt << 1 | 1), mid + 1, r, ql, qr); return ans; } void solve() { nn = 1000000; read(n); read(m); for (int i = (1); i <= (n); i++) { int y, lx, rx; read(y); read(lx); read(rx); if (lx == 0) vc[0].push_back({rx, y}); else vc[1].push_back({lx, y}); } for (int i = (1); i <= (m); i++) { int x, ly, ry; read(x); read(ly); read(ry); if (ly == 0) vv[0].push_back({x, ry}); else vv[1].push_back({x, ly}); } for (int i = (0); i <= (1); i++) { if (!vc[i].empty()) sort(vc[i].begin(), vc[i].end()); if (!vv[i].empty()) sort(vv[i].begin(), vv[i].end()); } reverse(vc[1].begin(), vc[1].end()); int sz0 = vv[0].size() - 1; int sz1 = vv[1].size() - 1; build(1, 0, nn, 0); for (auto i : vc[1]) { int fg = 1; while (fg) { fg = 0; if (sz1 >= 0 && sz0 >= 0 && vv[0][sz0].first < vv[1][sz1].first && vv[1][sz1].first >= i.first) { update(1, 0, nn, vv[1][sz1].second, nn, vv[1][sz1].first); sz1--; fg = 1; } else if (sz0 < 0 && sz1 >= 0 && vv[1][sz1].first >= i.first) { update(1, 0, nn, vv[1][sz1].second, nn, vv[1][sz1].first); sz1--; fg = 1; } else if (sz1 >= 0 && sz0 >= 0 && vv[0][sz0].first > vv[1][sz1].first && vv[0][sz0].first >= i.first) { update(1, 0, nn, 0, vv[0][sz0].second, vv[0][sz0].first); sz0--; fg = 1; } else if (sz1 < 0 && sz0 >= 0 && vv[0][sz0].first >= i.first) { update(1, 0, nn, 0, vv[0][sz0].second, vv[0][sz0].first); sz0--; fg = 1; } } int pos = query1(1, 0, nn, i.second); ys[pos].push_back(i); } build(1, 0, nn, 0); int j = 0, k = 0; vv[0].push_back({nn, nn}); int nj = vv[0].size(), nk = vv[1].size(); for (auto i : vc[0]) { add(1, 0, nn, i.second, 1); } int i = 0, ni = vc[0].size(); long long ans = 0; while (j < nj || k < nk) { pair<int, int> p; if (j == nj) p = vv[1][k++]; else if (k == nk) p = vv[0][j++]; else if (vv[0][j].first < vv[1][k].first) p = vv[0][j++]; else p = vv[1][k++]; while (i < ni && vc[0][i].first < p.first) { add(1, 0, nn, vc[0][i].second, -1); i++; } if (k && p == vv[1][k - 1]) ans += query2(1, 0, nn, p.second, nn); else ans += query2(1, 0, nn, 0, p.second); if (p.second == nn) ans++; for (auto s : ys[p.first]) add(1, 0, nn, s.second, 1); } printf( %lld n , ans); } int main() { int T = 1, cas = 1; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p); template <class T> ostream &operator<<(ostream &os, vector<T> &v); template <class T> ostream &operator<<(ostream &os, set<T> &v); template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v); const long long mod = 100000007; const int N = 15; int n, k; bool check(vector<int> &a, int x) { bool flag = true; vector<int> v; for (int i = 0; i < n; i++) { if (v.size() == k) break; if ((int)v.size() % 2) { v.push_back(a[i]); } else if (a[i] <= x) { v.push_back(a[i]); } } if (v.size() == k) { return true; } v.clear(); for (int i = 0; i < n; i++) { if (v.size() == k) break; if ((int)v.size() % 2 == 0) { v.push_back(a[i]); } else if (a[i] <= x) { v.push_back(a[i]); } } if (v.size() == k) { return true; } return false; } void TEST_CASES(int cas) { scanf( %d %d , &n, &k); vector<int> a(n); for (int i = 0; i < n; i++) { scanf( %d , &a[i]); } int low = *min_element(a.begin(), a.end()), high = *max_element(a.begin(), a.end()); int ans = -1; while (low <= high) { int mid = (low + high) / 2; ; ; if (check(a, mid)) { high = mid - 1; ans = mid; } else { low = mid + 1; } } printf( %d n , ans); } int main() { int t = 1, cas = 0; while (t--) { TEST_CASES(++cas); } return 0; } template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p) { os << { << p.first << , << p.second << } ; return os; } template <class T> ostream &operator<<(ostream &os, vector<T> &v) { os << [ ; for (int i = 0; i < v.size(); i++) { os << v[i] << ; } os << ] ; return os; } template <class T> ostream &operator<<(ostream &os, set<T> &v) { os << [ ; for (T i : v) { os << i << ; } os << ] ; return os; } template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) { for (auto i : v) { os << Key : << i.first << , Value : << i.second << endl; } return os; }
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int num[maxn]; long long ret; int n; void init() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &num[i]); } void print() { for (int i = 1; i < n; i++) { int j = 0; for (; j < 30; j++) if (i + (1 << j) > n) break; j--; ret += num[i]; num[i + (1 << j)] += num[i]; printf( %I64d n , ret); } } int main() { init(); print(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t, cont = 0, z = 0, x, n; int a[] = {1, 3, 6}; cin >> t; for (int i = 0; i < t; i++) { cin >> n; x = n; while (x > 0) { x = x / 10; z++; } if (z == 4) { for (int k = 1; k <= 9; k++) { if (n % 10 == k) { cout << k * 10 << endl; } } } else { for (int j = 0; j < 3; j++) { if (z == (j + 1)) { cont += a[j]; } } for (int l = 1; l <= 9; l++) { if (n % 10 == l) { cout << ((l - 1) * 10) + cont << endl; } } } cont = 0; z = 0; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T = 1; cin >> T; while (T--) { int64_t w, h; cin >> w >> h; vector<int64_t> hor1, hor2, ver1, ver2; int64_t k; int64_t q; cin >> k; for (int i = 0; i < k; ++i) cin >> q, hor1.push_back(q); cin >> k; for (int i = 0; i < k; ++i) cin >> q, hor2.push_back(q); cin >> k; for (int i = 0; i < k; ++i) cin >> q, ver1.push_back(q); cin >> k; for (int i = 0; i < k; ++i) cin >> q, ver2.push_back(q); cout << max({(hor1.back() - hor1[0]) * h, (hor2.back() - hor2[0]) * h, (ver1.back() - ver1[0]) * w, (ver2.back() - ver2[0]) * w}) << n ; } }
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; long long i; cin >> n >> m; long long cnt = 0; for (i = 1; i <= n; i++) { cnt += (m + i) / 5 - i / 5; } cout << cnt; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T, size_t N> long long SIZE(const T (&t)[N]) { return N; } template <typename T> long long SIZE(const T &t) { return t.size(); } string to_string(const string s, long long x1 = 0, long long x2 = 1e9) { return + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : ) + ; } string to_string(const char *s) { return to_string((string)s); } string to_string(const bool b) { return (b ? true : false ); } string to_string(const char c) { return string({c}); } template <size_t N> string to_string(const bitset<N> &b, long long x1 = 0, long long x2 = 1e9) { string t = ; for (long long __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1); __iii__ <= __jjj__; ++__iii__) { t += b[__iii__] + 0 ; } return + t + ; } template <typename A, typename... C> string to_string(const A(&v), long long x1 = 0, long long x2 = 1e9, C... coords); long long l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = ( + to_string(p.first) + , + to_string(p.second) + ) ; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A(&v), long long x1, long long x2, C... coords) { long long rnk = rank<A>::value; string tab(t_a_b_s, ); string res = ; bool first = true; if (l_v_l_v_l == 0) res += n ; res += tab + [ ; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2 - x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += , ; } first = false; l_v_l_v_l++; if (e != l) { if (rnk > 1) { res += n ; t_a_b_s = l_v_l_v_l; }; } else { t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += ] ; if (l_v_l_v_l == 0) res += n ; return res; } void dbgm() { ; } template <typename Heads, typename... Tails> void dbgm(Heads H, Tails... T) { cerr << to_string(H) << | ; dbgm(T...); } void FIO() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const long long N = 2e5 + 6; int32_t main() { FIO(); long long tc; cin >> tc; while (tc--) { long long x, y; cin >> x >> y; long long n = x; if (x == y) n = x; else if (x > y) { n = x * y + y; } else { long long r = y % x; n = y - r / 2; } assert(n <= (long long)2e18 and (n % x == y % n)); cout << n << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int k[1000010]; long long qpow(long long x, int n) { long long ans = 1; for (; n; n >>= 1, x = x * x % mod) if (n & 1) ans = ans * x % mod; return ans; } long long lpow(long long x, int n) { long long ans = 1; for (; n; n >>= 1, x *= x) { if (n && x > mod) return mod; if (n & 1) ans *= x; if (ans > mod) return mod; } return ans; } int main() { int t; scanf( %d , &t); while (t--) { int n, p; scanf( %d%d , &n, &p); for (int i = 0; i < n; i++) scanf( %d , &k[i]); sort(k, k + n); long long d = 0; long long sl = 0, sr = 0, q = k[n - 1]; for (int i = n - 1; i >= 0; i--) { if (q > k[i]) { sl = sl * qpow(p, q - k[i]) % mod; sr = sr * qpow(p, q - k[i]) % mod; d = min(d * lpow(p, q - k[i]), mod); q = k[i]; } if (d) d--, sr++; else d++, sl++; } sl = sl * qpow(p, q) % mod; sr = sr * qpow(p, q) % mod; printf( %lld n , (sl - sr + mod) % mod); } }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int UNDEF = -1; const long long INF = 1e18; template <typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; } template <typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; } int rint(); char rch(); long long rlong(); const int mv = 160; const int mc = ((mv * (mv - 1)) / 2) + 1; const int ma = 31; bitset<mc> f[mv][ma]; void reset() { for (int v = 0; v < mv; v++) for (int ai = 0; ai < ma; ai++) f[v][ai].reset(); } vector<int> solve(vector<int> &a) { vector<int> fail; sort(a.begin(), a.end()); int alim = a.size(); f[0][0][a[0]] = true; { int v = 0; int klim = (v * (v + 1)) / 2; if (f[(v)][alim - 1][klim]) { return vector<int>({a[0]}); } } for (int v = 1; v < mv; v++) { for (int sai = 0; sai < alim; sai++) { for (int dai = sai; dai <= min(alim - 1, sai + 1); dai++) { int shift = a[dai]; f[(v)][dai] |= (f[(v)-1][sai] << shift); } } int klim = (v * (v + 1)) / 2; if (f[(v)][alim - 1][klim]) { vector<int> final; int ai = alim - 1; int k = klim; for (int x = v; x >= 1; x--) { final.push_back(a[ai]); k -= a[ai]; if (f[x - 1][ai][k]) { ai = ai; } else { ai--; } } final.push_back(a[0]); sort(final.begin(), final.end()); return final; } bitset<mc> filt; filt.set(); filt >>= klim; filt <<= klim; for (int ai = 0; ai < alim; ai++) { f[(v)][ai] &= filt; } } return fail; } bool vans[mv + 2][mv + 2]; void recurse(vector<int> &r) { int n = r.size() - 1; if (n == 0) return; vector<int> R; R.push_back(-1); int sum = 0; for (int i = 1; i <= n; i++) { sum += r[i]; R.push_back(sum); } int s = n - 1 - r[n]; vector<int> F; F.resize(n + 1); for (int v = 1; v <= n; v++) F[v] = R[v] - (v * (v - 1)) / 2; int j = 1; vector<int> rp = r; for (int x = 1; x <= n; x++) vans[n][x] = 1; vans[n][n] = 0; vector<int> vj; vj.resize(s + 1); for (int d = 1; d <= s; d++) { int vmin = max(1, vj[d - 1] + 1); int ans = vmin; for (int v = n - 1; v >= vmin; v--) { if (F[v] < d) { ans = v + 1; break; } } vj[d] = ans; } for (int d = 1; d <= s; d++) { int j = vj[d]; assert(j <= n - 1); vans[n][j] = 0; vans[j][n] = 1; rp[j]--; } rp.resize(rp.size() - 1); recurse(rp); } vector<int> fgot; int comb(vector<int> &va) { auto got = solve(va); fgot = got; if (got.size() == 0) { printf( =( n ); return 0; } else { vector<int> r; r.push_back(-1); for (auto &w : got) r.push_back(w); recurse(r); int sz = got.size(); printf( %d n , sz); for (int x = 1; x <= sz; x++) { for (int y = 1; y <= sz; y++) { printf( %d , (int)vans[x][y]); } printf( n ); } return sz; } } void test() { reset(); memset(vans, 0, sizeof vans); vector<int> va; int p = (rand() % 100); for (int x = 0; x <= 30; x++) { if (rand() % 100 <= p) va.push_back(x); } if (va.size() == 0) va.push_back(rand() % 31); int got = comb(va); if (got != 0) { set<int> s; for (int x = 1; x <= got; x++) { assert(vans[x][x] == 0); int degout = 0; for (int y = 1; y <= got; y++) { degout += vans[x][y]; } s.insert(degout); } for (int x = 1; x <= got; x++) { for (int y = 1; y <= got; y++) { if (x == y) continue; assert(vans[x][y] + vans[y][x] == 1); } } set<int> sa; for (auto &w : va) sa.insert(w); if (s != sa) { for (auto &w : sa) printf( %d , w); printf( sa n ); for (auto &w : s) printf( %d , w); printf( s n ); for (auto &w : fgot) printf( %d , w); printf( fgot n ); exit(1); } } } void go() { for (int k = 0; k < 100000; k++) { srand(clock()); test(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n = rint(); vector<int> va; va.reserve(n); for (int i = 0; i < n; i++) { int x = rint(); va.push_back(x); } comb(va); } static char stdinBuffer[1024]; static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer); static const char *stdinPos = stdinDataEnd; void readAhead(size_t amount) { size_t remaining = stdinDataEnd - stdinPos; if (remaining < amount) { memmove(stdinBuffer, stdinPos, remaining); size_t sz = fread(stdinBuffer + remaining, 1, sizeof(stdinBuffer) - remaining, stdin); stdinPos = stdinBuffer; stdinDataEnd = stdinBuffer + remaining + sz; if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0; } } int rint() { readAhead(16); int x = 0; bool neg = false; while (*stdinPos == || *stdinPos == n ) ++stdinPos; if (*stdinPos == - ) { ++stdinPos; neg = true; } while (*stdinPos >= 0 && *stdinPos <= 9 ) { x *= 10; x += *stdinPos - 0 ; ++stdinPos; } return neg ? -x : x; } char rch() { readAhead(16); while (*stdinPos == || *stdinPos == n ) ++stdinPos; char ans = *stdinPos; ++stdinPos; return ans; } long long rlong() { readAhead(32); long long x = 0; bool neg = false; while (*stdinPos == || *stdinPos == n ) ++stdinPos; if (*stdinPos == - ) { ++stdinPos; neg = true; } while (*stdinPos >= 0 && *stdinPos <= 9 ) { x *= 10; x += *stdinPos - 0 ; ++stdinPos; } return neg ? -x : x; }
#include <bits/stdc++.h> using namespace std; bool ps[1111111]; const int mod = 1e9 + 7; int main() { int p, k, c = 0; long long ans = 1; scanf( %d%d , &p, &k); for (int i = 0; i < p; ++i) { if (ps[i]) continue; c++; long long b = i; while (!ps[b]) ps[b] = 1, b = (b * k) % p; } if (k != 1) c--; while (c--) ans = (ans * p) % mod; printf( %d n , int(ans)); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf( %d%d , &n, &m); if (m == 3 && (n == 5 || n == 6)) { printf( -1 n ); return 0; } for (int i = 0; i < m; ++i) printf( %d %d n , i, i * i + 10000000); for (int i = 0; i < n - m; ++i) printf( %d %d n , i, -i * i - 10000000); return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > vec[2102100]; pair<int, int> tr[2102100]; int lz[2102100]; int id[2102100]; int l[2102100]; int r[2102200]; int b[2102100]; int path[2102100]; int st[2102100]; void build(int p, int l, int r) { tr[p] = {0, -1}; lz[p] = 0; if (l == r) { return; } int mid = l + r >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); } void pushup(int p) { tr[p] = max(tr[p << 1], tr[p << 1 | 1]); } void pushdown(int p) { if (lz[p]) { lz[p << 1] = lz[p << 1 | 1] = lz[p]; lz[p] = 0; tr[p << 1] = tr[p << 1 | 1] = tr[p]; } } pair<int, int> query(int p, int l, int r, int ll, int rr) { if (ll <= l && rr >= r) { return tr[p]; } pushdown(p); int mid = l + r >> 1; pair<int, int> res = {0, -1}; if (ll <= mid) res = max(query(p << 1, l, mid, ll, rr), res); if (rr > mid) res = max(query(p << 1 | 1, mid + 1, r, ll, rr), res); return res; } void update(int p, int l, int r, int ll, int rr, pair<int, int> res) { if (ll <= l && rr >= r) { tr[p] = res; lz[p] = 1; return; } pushdown(p); int mid = l + r >> 1; if (ll <= mid) update(p << 1, l, mid, ll, rr, res); if (rr > mid) update(p << 1 | 1, mid + 1, r, ll, rr, res); pushup(p); } int main() { int n, t; cin >> n >> t; for (int i = 1; i <= t; i++) { cin >> id[i] >> l[i] >> r[i]; b[i * 2 - 1] = l[i]; b[i * 2] = r[i]; } sort(b + 1, b + 1 + t * 2); int len = unique(b + 1, b + 1 + t * 2) - b - 1; build(1, 1, len); for (int i = 1; i <= t; i++) { l[i] = lower_bound(b + 1, b + 1 + len, l[i]) - b; r[i] = lower_bound(b + 1, b + 1 + len, r[i]) - b; vec[id[i]].push_back({l[i], r[i]}); } for (int i = 1; i <= n; i++) { pair<int, int> mx = {0, -1}; for (auto tt : vec[i]) { mx = max(mx, query(1, 1, len, tt.first, tt.second)); } mx.first++; path[i] = mx.second; mx.second = i; for (auto tt : vec[i]) { update(1, 1, len, tt.first, tt.second, mx); } } pair<int, int> ans = query(1, 1, len, 1, len); int u = ans.second; int aa = n; while (u != -1) { st[u] = 1; aa--; u = path[u]; } cout << aa << endl; for (int i = 1; i <= n; i++) { if (!st[i]) cout << i << ; } }
#include <bits/stdc++.h> using namespace std; long long int MOD = 1000000007; long long int MOD1 = 998244353; mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count()); unsigned long long power(unsigned long long x, int y, int p) { unsigned long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } void solve() { long long int n; cin >> n; long long int res = 1; for (int i = 1; i <= n; i++) { res = (res * i) % MOD; } long long int p = power(2, n - 1, MOD); res = (res - p + MOD) % MOD; cout << res << n ; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
#include <bits/stdc++.h> using namespace std; long long qu(long long a, long long b) { cout << ? << a << << b << endl; fflush(stdout); string s; cin >> s; if (s[0] == x ) return 1; return 2; } int main() { string s; while (1) { cin >> s; if (s != start ) return 0; long long l = 1; if (qu(0, 1) == 1) { cout << ! 1 n ; fflush(stdout); continue; } while (qu(2 * l, l) == 1) l = l * 2LL; long long r = min((l * 2LL), 1000000000LL); l++; long long x = l; while (l < r) { long long mid = (l + r) / 2; if (qu(l - 1, mid) == 2) l = mid + 1; else r = mid; } cout << ! << l << n ; fflush(stdout); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MD = 1000000007; inline void add(int &a, const int b) { a += b; if (a >= MD) a %= MD; } inline int mul(const int &a, const int &b) { long long r = 1ll * a * b; if (r >= MD) r %= MD; return r; } int a[9], n, k; bool vis[9]; bool ok(int x) { if (x == 1) return 1; vis[x] = 1; if (!vis[a[x]] && ok(a[x])) return 1; return 0; } bool check() { for (int i = 1; i <= k; ++i) { memset(vis, 0, sizeof vis); if (!ok(a[i])) return 0; } return 1; } int dfs(int x) { if (x == k + 1) { if (check()) return 1; return 0; } int ret = 0; for (int i = 1; i <= k; ++i) a[x] = i, ret += dfs(x + 1); return ret; } int main() { scanf( %d%d , &n, &k); int ans = 1; for (int i = k + 1; i <= n; ++i) ans = mul(ans, n - k); printf( %d n , mul(ans, dfs(1))); return 0; }
#include <bits/stdc++.h> using namespace std; const int M = 1e5 + 10; const int INF = 0x3f3f3f3f; const int mod = 998244353; vector<long long> v; long long dfs(vector<long long> &v, int bit) { vector<long long> v0, v1; if (bit < 0 || v.size() == 0) return 0; for (long long &j : v) { if ((j >> bit) & 1) v1.push_back(j); else v0.push_back(j); } if (v1.size() == 0) return dfs(v0, bit - 1); if (v0.size() == 0) return dfs(v1, bit - 1); return min(dfs(v0, bit - 1), dfs(v1, bit - 1)) + (1 << bit); } int main() { int n; v.clear(); scanf( %d , &n); for (int i = 0; i < n; i++) { long long x; scanf( %lld , &x); v.push_back(x); } long long ans = dfs(v, 30); printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << = << a << endl; err(++it, args...); } inline int add(int a, int b) { a += b; return a >= 1000000007 ? a - 1000000007 : a; } inline int sub(int a, int b) { a -= b; return a < 0 ? a + 1000000007 : a; } inline int mul(int a, int b) { return (long long int)a * b % 1000000007; } int Set(int N, int pos) { return N = N | (1 << pos); } int Reset(int N, int pos) { return N = N & ~(1 << pos); } bool Cheek(int N, int pos) { return (bool)(N & (1 << pos)); } int a[1000005], b[1000005], lft[1000005], rgt[1000005], addina[1000005]; void Solve(int t) { int i, j, k, n, ans = 0; scanf( %d , &(n)); for (__typeof(i) i = 1; i <= n; i++) { scanf( %d , &(a[i])); addina[a[i]] = i; } for (__typeof(i) i = 1; i <= n; i++) scanf( %d , &(b[i])); for (i = 1; i <= n; i++) { int address = addina[b[i]]; int needlft = (n - address + i) % n; lft[needlft]++; int needrgt = (address - i + n) % n; rgt[needrgt]++; } for (i = 0; i <= n; i++) { ans = max(ans, lft[i]); ans = max(ans, rgt[i]); } cout << ans << endl; } int main() { int t, T; T = 1; for (__typeof(t) t = 1; t <= T; t++) { Solve(t); } return 0; }
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-9; struct Point { long long x, y; Point() {} Point(long long x, long long y) : x(x), y(y) {} Point operator-(const Point& a) { return Point(x - a.x, y - a.y); } long long operator*(Point a) { return x * a.x + y * a.y; } long long operator%(const Point& a) { return x * a.y - y * a.x; } long double norm() { return x * x + y * y; } long double len() { return sqrt(norm()); } void read() { scanf( %lld%lld , &x, &y); } } A[5], B[5]; bool operator==(const Point& a, const Point& b) { return a.x == b.x && a.y == b.y; } long long ccw(Point A, Point B, Point C) { long long t = (B - A) % (C - A); if (t == 0) return 0; if (t < 0) return -1; return 1; } const long double PI = acos((long double)-1.0); long double angle(Point a, Point o, Point b) { a = a - o; b = b - o; long double res = acos((a * b) / sqrt(a.norm()) / sqrt(b.norm())); while (res < -EPS) res += 2 * PI; while (res >= 2 * PI) res -= 2 * PI; return res; } bool order(Point A, Point B, Point C) { return min(A.x, C.x) <= B.x && B.x <= max(A.x, C.x) && min(A.y, C.y) <= B.y && B.y <= max(A.y, C.y); } bool good(Point A, Point B, Point C) { if (ccw(A, B, C)) return false; if (!order(A, B, C)) return false; long double x = (B - A).len(); long double y = (C - B).len(); if (x * 4 < y - EPS) return false; if (y * 4 < x - EPS) return false; return true; } bool check() { long long t[5]; for (long long i = (1), _b = (3); i <= _b; ++i) t[i] = i; do { for (long long r1 = 0, _a = (2); r1 < _a; ++r1) { for (long long r2 = 0, _a = (2); r2 < _a; ++r2) { for (long long r3 = 0, _a = (2); r3 < _a; ++r3) { if (A[t[1]] == A[t[2]] && ccw(A[t[1]], B[t[1]], B[t[2]]) && angle(B[t[1]], A[t[1]], B[t[2]]) < PI / 2.0 + EPS && good(A[t[1]], A[t[3]], B[t[1]]) && good(A[t[2]], B[t[3]], B[t[2]])) { return true; } swap(A[3], B[3]); } swap(A[2], B[2]); } swap(A[1], B[1]); } } while (next_permutation(t + 1, t + 4)); return false; } int main() { long long ntest; scanf( %lld , &ntest); while (ntest--) { for (long long i = (1), _b = (3); i <= _b; ++i) { A[i].read(); B[i].read(); } puts(check() ? YES : NO ); } }
#include <bits/stdc++.h> using namespace std; const int maxn = 100000007; const double eps = 1e-6; const long long Inf = 1LL << 62; const long long mod = 1000000007; const long long pnum = 5761460; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << = << a << n ; ; err(++it, args...); } void solve_with_case(int cas) {} long long maxCalories(vector<vector<long long>> arr, int n, int m) { vector<vector<long long>> dp1(1005, vector<long long>(1005, 0)); vector<vector<long long>> dp2(1005, vector<long long>(1005, 0)); vector<vector<long long>> dp3(1005, vector<long long>(1005, 0)); vector<vector<long long>> dp4(1005, vector<long long>(1005, 0)); for (int i = (1); i <= (n); i++) { for (int j = (1); j <= (m); j++) { dp1[i][j] = arr[i][j] + max(dp1[i - 1][j], dp1[i][j - 1]); } } for (int i = (n); i >= (1); i--) { for (int j = (m); j >= (1); j--) { dp2[i][j] = arr[i][j] + max(dp2[i + 1][j], dp2[i][j + 1]); } } for (int i = (n); i >= (1); i--) { for (int j = (1); j <= (m); j++) { dp3[i][j] = arr[i][j] + max(dp3[i + 1][j], dp3[i][j - 1]); } } for (int i = (1); i <= (n); i++) { for (int j = (m); j >= (1); j--) { dp4[i][j] = arr[i][j] + max(dp4[i - 1][j], dp4[i][j + 1]); } } long long ans = 0; for (int i = (2); i <= (n - 1); i++) { for (int j = (2); j <= (m - 1); j++) { long long ans1 = dp1[i - 1][j] + dp2[i + 1][j] + dp3[i][j - 1] + dp4[i][j + 1]; long long ans2 = dp1[i][j - 1] + dp2[i][j + 1] + dp3[i + 1][j] + dp4[i - 1][j]; ans = max({ans, ans1, ans2}); } } return ans; } void solve_without_case() { int n, m; cin >> n >> m; vector<vector<long long>> arr(1005, vector<long long>(1005, 0)); for (int i = (1); i <= (n); i++) { for (int j = (1); j <= (m); j++) { cin >> arr[i][j]; } } cout << maxCalories(arr, n, m) << n ; ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); solve_without_case(); return 0; }
#include <bits/stdc++.h> using namespace std; long long M = 1e9 + 7; double pi = acos(-1.0); long long powerm(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x) % M; y = y >> 1; x = (x * x) % M; } return res % M; } long long power(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long long gcd(long long a, long long b) { if (b > a) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } const int N = 2e5 + 5; vector<int> adj[N]; long long a[N]; bool b[N], c[N]; vector<pair<int, int> > pp(N); long long ans; void dfs(int u, int par, long long mn) { if (b[u] == 1 && c[u] == 0) pp[u].first++; else if (b[u] == 0 && c[u] == 1) pp[u].second++; a[u] = min(a[u], mn); for (auto it : adj[u]) { if (it == par) continue; dfs(it, u, a[u]); pp[u].first += pp[it].first; pp[u].second += pp[it].second; } long long jj = min(pp[u].first, pp[u].second); ans += jj * 2 * a[u]; pp[u].first -= jj; pp[u].second -= jj; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i] >> c[i]; } for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1, -1, M); if (pp[1].first > 0 || pp[1].second > 0) cout << -1; else cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 7e5 + 10; const int inf = 0x3f3f3f3f; int n, m, p, q, nx, ny; long long sum[300][300]; int dis[300][300]; int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}; char maze[300][300]; void bfs(int sx, int sy, long long k) { dis[sx][sy] = 0, sum[sx][sy] += k; queue<pair<pair<int, int>, int> > q; q.push(make_pair(make_pair(sx, sy), k)); while (q.empty() == 0) { pair<pair<int, int>, int> tmp = q.front(); q.pop(); for (int i = 0; i < 4; i++) { nx = tmp.first.first + dx[i], ny = tmp.first.second + dy[i]; if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && maze[nx][ny] != * && dis[nx][ny] == inf) { dis[nx][ny] = dis[tmp.first.first][tmp.first.second] + 1; int res = tmp.second / 2; if (res <= 0) { break; } sum[nx][ny] += res; q.push(make_pair(make_pair(nx, ny), res)); } } } } int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> q >> p; memset(sum, 0, sizeof(sum)); int cnt = 0; for (int i = 1; i <= n; i++) { cin >> maze[i] + 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (maze[i][j] >= A && maze[i][j] <= Z ) { memset(dis, inf, sizeof(dis)); bfs(i, j, (maze[i][j] - A + 1) * q); } } } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (sum[i][j] > p && maze[i][j] != * ) { ans++; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; int q; cin >> q; while (q--) { int n; cin >> n; vector<vector<int> > occs(n); for (int i = 0; i < (int)(n); ++i) { int x; cin >> x; --x; occs[x].push_back(i); } int dif = 0; for (int i = 0; i < (int)(n); ++i) if (!occs[i].empty()) ++dif; int ch = 0, maxch = 0, R = -1; for (int i = 0; i < (int)(n); ++i) { if (occs[i].empty()) continue; if (occs[i][0] > R) ++ch, uax(maxch, ch); else ch = 1; R = occs[i].back(); } cout << dif - maxch << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 100 + 50; vector<long long> nb[MAXN]; long long n, m, k, cmp, sz[MAXN]; bool mrk[MAXN]; long long pw(long long x, long long y) { if (!y) return 1; if (y == 1) return x % k; long long tmp = pw(x, y / 2); if (y % 2) return ((tmp * tmp) % k * x) % k; else return (tmp * tmp) % k; } void dfs(int x) { if (mrk[x]) return; mrk[x] = 1; sz[cmp]++; for (int i = 0; i < nb[x].size(); i++) dfs(nb[x][i]); } int main() { cin >> n >> m >> k; for (int i = 0; i < m; i++) { int tmp1, tmp2; cin >> tmp1 >> tmp2; nb[tmp1 - 1].push_back(tmp2 - 1); nb[tmp2 - 1].push_back(tmp1 - 1); } for (int i = 0; i < n; i++) { if (!mrk[i]) { dfs(i); cmp++; } } if (cmp == 1) { cout << 1 % k << endl; return 0; } long long ans = pw(n, cmp - 2) % k; for (int i = 0; i < cmp; i++) { ans *= sz[i]; ans %= k; } cout << ans << endl; return 0; }