solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; int main() { long long c1, c2, c3, c4, n, m, b[1005], t[1005], sum_b, sum_t, ans, temp; cin >> c1 >> c2 >> c3 >> c4; cin >> n >> m; sum_b = sum_t = 0; for (long long i = 0; i < n; i++) { cin >> b[i]; sum_b += b[i]; } for (long long i = 0; i < m; i++) { cin >> t[i]; sum_t += t[i]; } ans = c4; ans = min(ans, c3 * 2); temp = c3; for (long long i = 0; i < n; i++) { temp += min(b[i] * c1, c2); } ans = min(ans, temp); temp = c3; for (long long i = 0; i < m; i++) { temp += min(t[i] * c1, c2); } ans = min(ans, temp); temp = 0; for (long long i = 0; i < n; i++) { temp += min(b[i] * c1, c2); } for (long long i = 0; i < m; i++) { temp += min(t[i] * c1, c2); } ans = min(ans, temp); temp = (sum_b + sum_t) * c1; ans = min(ans, temp); cout << ans << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { long long a, b, x, y, n; cin >> a >> b >> x >> y >> n; long long ans; if (a - x + b - y <= n) ans = x * y; else { if (a < b) { if (n <= a - x) { ans = (a - n) * b; } else { long long p = n; p -= (a - x); long long a1 = x * (b - p); if (n <= b - y) a1 = min(a1, (long long)((b - n) * a)); else { n -= (b - y); a1 = min(a1, (long long)(y * (a - n))); } ans = a1; } } else { if (n <= b - y) { ans = (b - n) * a; } else { long long p = n; p -= (b - y); long long a1 = y * (a - p); if (n <= a - x) a1 = min(a1, (long long)((a - n) * b)); else { n -= (a - x); a1 = min(a1, (long long)(x * (b - n))); } ans = a1; } } } cout << ans << endl; } }
1
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long n; cin >> n; set<long long> s; long long i = 2; while (i <= sqrt(n) && n > 1) { if (n % i == 0) { s.insert(i); while (n % i == 0) n /= i; } i++; } if (n != 1) s.insert(n); if (s.size() == 1) cout << *(s.begin()); else cout << 1; return 0; }
3
#include <bits/stdc++.h> using namespace std; int vis[(1 << 18)]; int main() { int n, x; scanf("%d%d", &n, &x); vis[0] = 1; vector<int> pref({0}); for (int i = 1; i < (1 << n); ++i) { if (vis[i ^ x]) continue; pref.push_back(i); vis[i] = 1; } int siz = pref.size(); printf("%d\n", siz - 1); for (int i = 1; i < siz; ++i) printf("%d ", (pref[i] ^ pref[i - 1])); printf("\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long N = 5000; long long a, b, c, s[N + 10], dp[N + 10][N + 10], qu[N + 10], p[N + 10]; signed main() { scanf("%lld%lld%lld", &a, &b, &c); for (long long i = 1; i <= a; i++) scanf("%lld", &s[i]); memset(dp, -0x3f, sizeof(dp)); for (long long i = 0; i < b; i++) dp[i][0] = 0; for (long long i = 1; i <= c; i++) { long long l = 1, r = 1; qu[1] = 0; p[1] = dp[0][i - 1]; for (long long j = 1; j <= a; j++) { while (l <= r && dp[j - 1][i - 1] + s[j] >= p[r]) r--; qu[++r] = j; p[r] = dp[j - 1][i - 1] + s[j]; while (l <= r && j - b >= qu[l]) l++; dp[j][i] = p[l]; } } if (dp[a][c] == 150) { puts("-1"); return 0; } printf("%lld", max(-1ll, dp[a][c])); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long int a, b; int main() { cin >> a >> b; swap(a, b); int c = 0; while (a > 1) { if (a % b) return cout << "NO", 0; c++; a /= b; } c--; cout << "YES\n" << c; }
1
#include <bits/stdc++.h> using namespace std; const int MAX = 2005; int a[MAX], b; int sum[MAX]; int ans[50000000]; const int INF = 0x3f3f3f3f; int main() { int k, n; while (~scanf("%d%d", &k, &n)) { memset(sum, 0, sizeof(sum)); for (int i = 1; i <= k; i++) { scanf("%d", &a[i]); sum[i] = sum[i - 1] + a[i]; } int numb = 0; sort(sum + 1, sum + k + 1); for (int i = 1; i <= n; i++) { scanf("%d", &b); for (int j = 1; j <= k; j++) { if (b - sum[j] != ans[numb - 1] || j == 1) ans[numb++] = b - sum[j]; } } int a = 0; sort(ans, ans + numb); ans[numb] = INF; int l = 0; for (int i = 0; i < numb; i++) { if (ans[i + 1] != ans[i]) { int x = i + 1 - l; l = i + 1; if (x >= n) a++; } } printf("%d\n", a); } }
4
#include <bits/stdc++.h> using namespace std; ifstream in("input.txt"); ofstream out("output.txt"); bool flag = false, ff[312345], z[312345]; int n, m, t[312345]; char c[312345]; vector<int> g[312345]; int dp[312345][30]; int tt; void dfs(int v) { ff[v] = true; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (!ff[to]) { dfs(to); continue; } if (t[to] == -1) flag = true; } tt++; t[v] = tt; return; } int rec(int i, int j) { if (dp[i][j] > -1) return dp[i][j]; dp[i][j] = 0; if ((int)c[i] - (int)'a' == j) dp[i][j]++; int curr = 0; for (int k = 0; k < g[i].size(); k++) { int to = g[i][k]; curr = max(curr, rec(to, j)); } dp[i][j] += curr; return dp[i][j]; } int main() { ios_base ::sync_with_stdio(false); int i, j, x, y; tt = 0; cin >> n >> m; for (i = 1; i <= n; i++) { cin >> c[i]; t[i] = -1; ff[i] = false; z[i] = false; } for (i = 1; i <= m; i++) { cin >> x >> y; g[x].push_back(y); z[y] = true; } for (i = 1; i <= n; i++) if (!ff[i]) { dfs(i); if (flag) { cout << -1; return 0; } } for (i = 1; i <= n; i++) for (j = 0; j < 28; j++) dp[i][j] = -1; int ans = 0; for (j = 0; j < 26; j++) { for (i = 1; i <= n; i++) if (!z[i]) ans = max(ans, rec(i, j)); } cout << ans; return 0; }
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const long double PI = 3.14159265358979323846; const long double eps = 1e-5; const int INF = numeric_limits<int>::max(); const long long LINF = numeric_limits<long long>::max(); const long long mod = 1000 * 1000 * 1000 + 7; void solve() { int n; cin >> n; vector<string> a; string s1, s2, s = "", t; vector<string> as, at; vector<pair<int, int> > pos; vector<string> w1(n), w2(n); for (int i = 0; i < n; ++i) cin >> w1[i]; for (int i = 0; i < n; ++i) cin >> w2[i]; for (int i = 0; i < n; ++i) { s1 = w1[i]; s2 = w2[i]; if (s1 == s2) { a.push_back(s1); continue; } int st = s1.size(), en = 0; for (int j = 0; j < s1.size(); ++j) { if (s1[j] != s2[j]) { st = min(st, j); en = max(en, j); } } ++en; if (s != "") { if (s != string(s1.begin() + st, s1.begin() + en)) { cout << "NO\n"; return; } if (t != string(s2.begin() + st, s2.begin() + en)) { cout << "NO\n"; return; } } else { s = string(s1.begin() + st, s1.begin() + en); t = string(s2.begin() + st, s2.begin() + en); } as.push_back(s1); at.push_back(s2); pos.push_back(pair<int, int>(st, en)); } while (true) { bool ok = 1; for (int i = 0; i < as.size(); ++i) { if ((pos[i].first == 0) || (as[i][pos[i].first - 1] != as[0][pos[0].first - 1]) || (at[i][pos[i].first - 1] != at[0][pos[0].first - 1])) { ok = 0; break; } } if (!ok) break; for (int i = 0; i < as.size(); ++i) { --pos[i].first; } } while (true) { bool ok = 1; for (int i = 0; i < as.size(); ++i) { if ((pos[i].second == as[i].size()) || (as[i][pos[i].second] != as[0][pos[0].second]) || (at[i][pos[i].second] != at[0][pos[0].second])) { ok = 0; break; } } if (!ok) break; for (int i = 0; i < as.size(); ++i) { ++pos[i].second; } } s = string(as[0].begin() + pos[0].first, as[0].begin() + pos[0].second); t = string(at[0].begin() + pos[0].first, at[0].begin() + pos[0].second); for (int i = 0; i < a.size(); ++i) { if (a[i].find(s) != string::npos) { cout << "NO\n"; return; } } for (int i = 0; i < as.size(); ++i) { if (as[i].find(s) != pos[i].first) { cout << "NO\n"; return; } } cout << "YES\n"; cout << s << endl << t << endl; } int main() { ios_base::sync_with_stdio(0); solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, m, q, p; string s; getline(cin, s); stringstream ss(s); ss >> n >> m >> q >> p; vector<char> map(n * m, '.'); vector<unsigned long long> vals(n * m, 0); unordered_set<int> points; char c; for (int i = 0; i < n; ++i) { getline(cin, s); for (int j = 0; j < m; ++j) { c = s[j]; map[i * m + j] = c; if ('A' <= c && c <= 'Z') { points.insert(i * m + j); } } } queue<pair<int, unsigned> > que; vector<bool> used(n * m, false); for (auto it = points.begin(); it != points.end(); ++it) { fill(used.begin(), used.end(), false); unsigned val = q * (map[*it] - 'A' + 1); que.push(make_pair(*it, val)); while (!que.empty()) { auto el = que.front(); que.pop(); if (used[el.first]) { continue; } used[el.first] = true; unsigned val = el.second; if (val == 0) { continue; } vals[el.first] += val; unsigned new_val = val / 2; if (new_val == 0) { continue; } int st_i = el.first / m; int st_j = el.first % m; for (int i = st_i - 1; i <= st_i + 1; i += 2) { if (i < 0 || i >= n) continue; int j = st_j; if (map[i * m + j] == '*') continue; que.push(make_pair(i * m + j, new_val)); } for (int j = st_j - 1; j <= st_j + 1; j += 2) { if (j < 0 || j >= m) continue; int i = st_i; if (map[i * m + j] == '*') continue; que.push(make_pair(i * m + j, new_val)); } } } int res = 0; for (int i = 0; i < n * m; ++i) if (vals[i] > p) ++res; cout << res << endl; return 0; }
5
#include <bits/stdc++.h> int main(int argc, char *argv[]) { float x; unsigned int n, V, i; unsigned int A = 0; unsigned int *a; scanf("%u %u\n", &n, &V); a = (unsigned int *)malloc(n * sizeof(unsigned int)); for (i = 0; i < n; i++) { scanf("%u", a + i); A += *(a + i); } x = (float)V / (float)A; for (i = 0; i < n; i++) { unsigned int b; float x1; scanf("%u", &b); x1 = (float)b / (float)a[i]; if (x1 < x) x = x1; } printf("%f", A * x); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5, nBits = 2e8 + 5, MM = (1 << 16), MAX = 1111, OO = 0x3f3f3f3f, MOD = 1e9 + 7, inf = 1 << 30; const long long INF = (long long)1e18; long long fact(long long z) { return (z <= 1) ? 1 : z * fact(z - 1); } int q; long long h, w, x, y, mH, mW; char c; pair<int, int> p; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> q; while (q--) { cin >> c >> h >> w; bool first = 1; if (c == '+') if (h >= w) mH = max(mH, h), mW = max(mW, w); else mH = max(mH, w), mW = max(mW, h); else { if ((h >= mH && w >= mW) || (h >= mW && w >= mH)) cout << "YES\n"; else cout << "NO\n"; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 7; const int inf = INT_MAX; const long long inff = 1e18; const int mod = 1e9 + 7; string s; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; cin >> s; int nn = 0; for (int j = 1; j < n; j++) { bool flag = 1; for (int i = 0; i < n - j; i++) { if (s[i] != s[i + j]) { flag = 0; break; } } if (flag) { nn = j; break; } } cout << s; if (nn == 0) nn = n; for (int i = 2; i <= k; i++) { for (int j = n - nn; j < n; j++) cout << s[j]; } cout << endl; }
2
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9; const int N = 780; long long n; long long f(long long x) { return x * x; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; long long x = n * (n - 1) * (n - 2) * (n - 3) * (n - 4); x *= x / 120; cout << x; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; int flag = 0; while (n--) { int a, b, c, d; cin >> a >> b >> c >> d; if (c == b) flag = 1; } if (k % 2 == 0) { if (k == 2) { if (flag) cout << "YES\n"; else cout << "NO\n"; } else { if (flag) cout << "YES\n"; else cout << "NO\n"; } } else cout << "NO\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } const int inf = INT_MAX / 2; const long long infl = 1LL << 60; template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } int N, K; string A; void _main() { cin >> N >> K >> A; string B = ""; for (int i = 0; i < N; i++) B += A[i % K]; int bad = -1; for (int i = 0; i < N; i++) { if (A[i] < B[i]) break; else if (A[i] > B[i]) { bad = i; break; } } if (0 <= bad) { bad %= K; for (int k = K - 1; k >= bad; k--) { if (B[k] != '9') { int i = k; while (i < N) { B[i]++; i += K; } for (int kk = k + 1; kk < K; kk++) { i = kk; while (i < N) { B[i] = '0'; i += K; } } break; } } } printf("%d\n%s\n", N, B.c_str()); }
4
#include <bits/stdc++.h> using namespace std; pair<pair<int, int>, int> P[1000010]; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int main() { int n = read(); int x, y; for (int i = 1; i <= n; i++) { x = read(); y = read(); P[i].first.first = x / 1005; P[i].first.second = y; P[i].second = i; } sort(P + 1, P + n + 1); for (int i = 1; i <= n; i++) { cout << P[i].second << " "; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e9 + 7; const long long int mod = 1e9 + 9; long long int mini(long long int a, long long int b) { if (a >= b) return b; return a; } long long int maxi(long long int a, long long int b) { if (a >= b) return a; return b; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long int n; cin >> n; map<long long int, long long int> cnt; long long int ans = 0; vector<long long int> A; for (long long int i = 0; i <= n - 1; i++) { string s; cin >> s; long long int x = 0; for (long long int j = 0; j <= s.length() - 1; j++) { long long int mask = (1LL << (s[j] - 'a')); x ^= mask; } ans += cnt[x]; cnt[x]++; A.push_back(x); } for (long long int i = 0; i <= n - 1; i++) { long long int x = A[i]; for (long long int j = 0; j <= 25; j++) { long long int mask = (1LL << j); if (mask & x) { x ^= mask; ans += cnt[x]; x ^= mask; } } } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 2; i <= n; i++) { for (int j = i; j <= n; j++) { if (i + j == n && (i % 4 == 0 || i % 6 == 0 || i % 8 == 0 || i % 9 == 0 || i % 10 == 0 || i % 12 == 0 || i % 15 == 0 || i % 16 == 0 || i % 18 == 0 || i % 20 == 0 || i % 21 == 0 || i % 22 == 0 || i % 24 == 0 || i % 25 == 0) && (j % 4 == 0 || j % 6 == 0 || j % 8 == 0 || j % 9 == 0 || j % 10 == 0 || j % 14 == 0 || j % 15 == 0 || j % 16 == 0 || j % 18 == 0 || j % 20 == 0 || j % 21 == 0 || j % 22 == 0 || j % 24 == 0 || j % 25 == 0)) { cout << i << ' ' << j << endl; return 0; } } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z; cin >> n; c = 0; for (i = 0; i < n; i++) { cin >> a; if (a == 0) c++; } if (n == 1) { if (c == 1) cout << "NO\n"; else cout << "YES\n"; } else { if (c == 1) cout << "YES\n"; else cout << "NO\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int k; int cnt[10] = {0}; char make_pair[4][5]; scanf("%d", &k); for (int i = 0; i < 4; i++) { scanf("%s", make_pair[i]); for (int j = 0; j < 4; j++) { if (make_pair[i][j] == '.') continue; else cnt[make_pair[i][j] - '0']++; } } for (int i = 1; i <= 9; i++) if (cnt[i] > 2 * k) return (!printf("NO\n")); return (!printf("YES\n")); }
0
#include <bits/stdc++.h> using namespace std; struct node { long long x, y; friend bool operator<(const node &a, const node &b) { return a.x < b.x; } } p[100010]; pair<long long, int> mx, tmp; long long crs(node a, node b, node c) { return 1ll * (b.x - a.x) * (c.y - a.y) - 1ll * (c.x - a.x) * (b.y - a.y); } struct convex { node p[400], stk[400]; int top, sz, now, x; void built() { top = 0; now = 1; for (int i = 1; i <= sz; i++) { while (top > 1 && crs(stk[top - 1], p[i], stk[top]) <= 0) top--; stk[++top] = p[i]; } } long long getans(node a, int x) { return a.y + x * a.x; } pair<long long, int> query() { while (now <= top && getans(stk[now], x) < getans(stk[now + 1], x)) now++; return make_pair(getans(stk[now], x), stk[now].x); } } q[400]; int now = 1, n, s, tot; long long w; int main() { scanf("%d%I64d", &n, &w); for (int i = 1; i <= n; i++) scanf("%I64d%I64d", &p[i].y, &p[i].x); for (int i = 1; i <= n; i++) tot = max(1ll * tot, p[i].y); sort(p + 1, p + 1 + n); s = sqrt(tot); for (int i = 1; i <= tot / s + 1; i++) { for (int j = 1; j <= s; j++) q[i].p[j] = (node){(i - 1) * s + j, 0}; q[i].sz = s; q[i].built(); } for (int i = 0; now <= n; i++) { mx = make_pair(0, 0); for (; now <= n && p[now].x < i; now++) { for (int j = (p[now].y - 1) / s * s + 1; j <= p[now].y; j++) q[(p[now].y - 1) / s + 1].p[j - (p[now].y - 1) / s * s].y += j; q[(p[now].y - 1) / s + 1].built(); for (int j = 1; j <= (p[now].y - 1) / s; j++) q[j].x++; } for (int j = 1; j <= tot / s + 1; j++) { tmp = q[j].query(); if (tmp.first > mx.first) mx = tmp; } printf("%I64d %d\n", mx.first + (n - now + 1) * w * i, mx.second); } return 0; }
11
#include <bits/stdc++.h> using namespace std; const int N = 500005; const long long inf = 0x3f3f3f3f; const long long mod = 1000000007; int n, m; int ls[3 * N], rs[3 * N], lft[3 * N], rgt[3 * N]; long long num[3 * N], tag[3 * N]; int rt, P; void update(int p) { num[p] = ((num[ls[p]] + tag[ls[p]] * (rgt[ls[p]] - lft[ls[p]] + 1)) + (num[rs[p]] + tag[rs[p]] * (rgt[rs[p]] - lft[rs[p]] + 1))) % mod; return; } void pushd(int p) { if (!tag[p]) return; tag[ls[p]] += tag[p]; tag[rs[p]] += tag[p]; num[p] = (num[p] + tag[p] * (rgt[p] - lft[p] + 1)); tag[p] = 0; return; } int build(int l, int r) { int p = ++P; tag[p] = num[p] = 0; lft[p] = l; rgt[p] = r; if (l == r) return p; int mid = (l + r) >> 1; ls[p] = build(l, mid); rs[p] = build(mid + 1, r); return p; } void change(int p, int pos) { if (lft[p] == rgt[p]) { tag[p]++; return; } pushd(p); int mid = (lft[p] + rgt[p]) >> 1; if (pos <= mid) { tag[rs[p]]++; change(ls[p], pos); } else change(rs[p], pos); update(p); return; } long long query(int pos) { if (pos < 1 || pos > n) return 0; int p = rt; long long ans = 0; while (lft[p] < rgt[p]) { pushd(p); int mid = (lft[p] + rgt[p]) >> 1; if (pos > mid) { ans += (num[ls[p]] + tag[ls[p]] * (rgt[ls[p]] - lft[ls[p]] + 1)); ans %= mod; p = rs[p]; } else p = ls[p]; } ans += (num[p] + tag[p] * (rgt[p] - lft[p] + 1)); ans %= mod; return ans; } long long _query(int pos) { int p = rt; long long ans = 0; while (lft[p] < rgt[p]) { pushd(p); int mid = (lft[p] + rgt[p]) >> 1; if (pos > mid) p = rs[p]; else p = ls[p]; } ans += (num[p] + tag[p] * (rgt[p] - lft[p] + 1)); return ans; } struct node { long long v; int id; bool operator<(const node &b) const { return v < b.v; } }; node a[N]; int main() { ios::sync_with_stdio(0); int i, j; cin >> n; for (i = 1; i <= n; i++) { cin >> a[i].v; a[i].id = i; } sort(a + 1, a + n + 1); long long ans = 0; rt = build(1, n); for (i = 1; i <= n; i++) { int x = a[i].id; change(rt, x); long long k1 = query(x - 1); long long k2 = _query(x); long long k3 = (((num[rt] + tag[rt] * (rgt[rt] - lft[rt] + 1)) - k1 - k2) % mod + mod) % mod; ans += (a[i].v * ((n - x + 1) * (k2 * x - k1) % mod + x * (k3 - (n - x) * k2) % mod) % mod + mod) % mod; ans %= mod; } cout << ans; return 0; }
7
#include <bits/stdc++.h> using namespace std; int a[55], b[55]; int main() { int n; while (scanf("%d", &n) != EOF) { int i, j; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); for (i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); for (i = 0, j = 0; i < n; i++) if (a[i] != a[i + 1]) b[j++] = a[i]; int flg = 0; sort(b, b + n); int k = 0; for (i = 0; i < n; i++) if (!b[i]) k++; for (i = k; i < n - 2; i++) if (b[i + 2] - b[i] <= 2) flg = 1; if (flg) printf("YES\n"); else printf("NO\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long fastpow(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) { res = res * a; } b >>= 1; a *= a; } return res; } int main() { long long n, x, y; cin >> n >> x >> y; vector<long long> res; for (int i = 0; i < n - 1; i++) { res.push_back(1); } int flag = 1; if (y - (n - 1) <= 0 || 1ll * fastpow(y - (n - 1), 2) + (n - 1) < x) { flag = 0; } else { res.push_back(y - (n - 1)); } if (flag) { for (int i = 0; i < res.size(); i++) { cout << res[i] << endl; } } else { cout << "-1" << endl; } return 0; }
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:300000000") const long double eps = 1e-20; const long double pi = acos(-1.0); const long long inf = 1000 * 1000 * 1000 * 1000 * 1000 * 1000; const long long base = 1000 * 1000 * 1000 + 7; using namespace std; vector<vector<int> > g(111111), f(111111); int depth[111111], where[111111]; int n, root, q, x, y, tq, dep, val; void dfs(int v, int dep, int pr, int who) { where[v] = who; depth[v] = dep; f[who].push_back(0); for (int i = 0; i < (g[v].size()); i++) if (g[v][i] != pr) dfs(g[v][i], dep + 1, v, who); } int fs(int pos, int x) { int res = 0; for (int i = x; i > 0; i = (i & (i + 1)) - 1) res += f[pos][i]; return res; } void md(int pos, int x, int val) { for (int i = x; i < f[pos].size(); i = i | (i + 1)) f[pos][i] += val; } int main() { scanf("%d%d", &n, &q); for (int i = 0; i < (n - 1); i++) { scanf("%d%d", &x, &y); x--; y--; g[x].push_back(y); g[y].push_back(x); } for (int i = 0; i < (n + n); i++) f[0].push_back(0); for (int i = 0; i < (g[0].size()); i++) { f[i + 1].push_back(0); dfs(g[0][i], 1, 0, i + 1); } for (int i = 0; i < (q); i++) { scanf("%d", &tq); if (tq == 1) { scanf("%d", &x); x--; if (x == 0) printf("%d\n", root); else printf("%d\n", fs(where[x], depth[x]) + fs(0, depth[x])); } else { scanf("%d%d%d", &x, &val, &dep); x--; if (x == 0) { root += val; md(0, 1, val); md(0, dep + 1, -val); continue; } if (depth[x] > dep) { md(where[x], depth[x] - dep, val); md(where[x], depth[x] + dep + 1, -val); } else { root += val; md(0, 1, val); md(0, -depth[x] + dep + 1, -val); md(where[x], dep - depth[x] + 1, val); md(where[x], depth[x] + dep + 1, -val); } } } return 0; }
6
#include <bits/stdc++.h> using namespace std; bool a[200]; int b[] = {'A', 'H', 'I', 'M', 'T', 'U', 'V', 'W', 'X', 'Y', 'O'}; int main() { string s; cin >> s; for (int i = 0; i < 11; i++) { a[b[i]] = 1; } int l = s.size(), f = 1; for (int i = 0; i < l; i++) { if (a[s[i]] == 0) f = 0; if (s[i] != s[l - i - 1]) f = 0; } if (f) puts("YES"); else puts("NO"); return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, k; int main() { cin >> n >> k; for (int i = 1; i <= k; i++) cout << 2 * i << ' ' << 2 * i - 1 << ' '; for (int i = k + 1; i <= n; i++) cout << 2 * i - 1 << ' ' << 2 * i << ' '; cout << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T = long long> inline T read() { T s = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { s = (s << 3) + (s << 1) + ch - 48; ch = getchar(); } return s * f; } const int N = 2e5 + 5, M = 1e6 + 5, MOD = 1e9 + 7, CM = 998244353, INF = 0x3f3f3f3f; const long long linf = 0x7f7f7f7f7f7f7f7f; long long pow(long long a, long long b, long long p) { long long ans = 1; a %= p; while (b) { if (b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans; } namespace Combination { const int MAXN = 2e5 + 5; long long fac[MAXN], invfac[MAXN], mod; void init(int n, long long MOD) { fac[0] = 1; mod = MOD; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % mod; invfac[n] = pow(fac[n], mod - 2, mod); for (int i = n; i >= 1; i--) invfac[i - 1] = invfac[i] * i % mod; } long long C(long long n, long long m) { if (n < 0 || m < 0) return 0; return n >= m ? fac[n] * invfac[n - m] % mod * invfac[m] % mod : 0; } } // namespace Combination long long F(long long n, long long m, long long MOD) { long long ans = 0; int base = 1; for (int i = 0; i <= m; i++) { long long now = base * Combination::C(m, m - i) % MOD * pow(m - i, n, MOD) % MOD; now = (now + MOD) % MOD; base *= -1; ans = (ans + now) % MOD; } return ans; } void solve(int kase) { Combination::init(N - 5, CM); long long n = read(), k = read(); if (k == 0) { long long ans = 1; for (int i = 1; i <= n; i++) ans = ans * i % CM; printf("%lld\n", ans); return; } long long ans1 = Combination::C(n, n - k); long long ans2 = 0; for (int i = 0; i <= n - k; i++) { long long now = ((i & 1) ? -1 : 1) * Combination::C(n - k, n - k - i) % CM; now = (now + CM) % CM; now = now * pow(n - k - i, n, CM) % CM; ans2 = (ans2 + now) % CM; } printf("%lld\n", 2 * ans1 * ans2 % CM); } const bool ISFILE = 0, DUO = 0; int main() { clock_t start, finish; start = clock(); if (ISFILE) freopen("/Users/i/Desktop/practice/in.txt", "r", stdin); if (DUO) { int Kase = 0; cin >> Kase; for (int kase = 1; kase <= Kase; kase++) solve(kase); } else solve(1); finish = clock(); ; ; return 0; }
7
#include <bits/stdc++.h> using namespace std; int n; long long a[100000], c, b; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; cout << a[1] - a[0] << " " << a[n - 1] - a[0] << endl; for (int i = 1; i < n - 1; i++) { c = a[i] - a[i - 1]; b = a[i + 1] - a[i]; if (c > b) cout << b << " "; else cout << c << " "; c = a[i] - a[0]; b = a[n - 1] - a[i]; if (c > b) cout << c << endl; else cout << b << endl; } cout << a[n - 1] - a[n - 2] << " " << a[n - 1] - a[0] << endl; }
0
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; string s; cin >> s; long long a = 1, as = -1; for (long long i = 1; i < n; i++) { if (s[i] == s[i - 1]) a++; else { as = i; break; } } if (as == -1) { long long ans = n * (n + 1); ans /= 2; cout << ans << endl; return 0; } long long b = 1, bs = -1; for (int i = n - 2; i >= 0; i--) { if (s[i] == s[i + 1]) b++; else { bs = i; break; } } long long ans = a + b; if (s[0] != s[n - 1]) { if (a == 1 && b == 1) { cout << 3 << endl; } else { cout << a + b + 1 << endl; } return 0; } else { a++; b++; long long ans = a * b; ans %= mod; cout << ans << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, i, j; cin >> n; int a[n * n]; for (int i = 1; i <= n * n; i++) a[i - 1] = i; for (int s = 0; s <= (n * (n - 1)) / 2; s += n / 2) { for (i = s; i < s + n / 2; i++) { cout << a[i] << " "; } for (j = s + n / 2; j > s; j--) { cout << a[n * n - j] << " "; } cout << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int min(long long int a, long long int b) { if (a < b) { return a; } else { return b; } } long long int max(long long int a, long long int b) { if (a > b) { return a; } else { return b; } } bool sumCube(long long int n) { unordered_map<long long int, long long int> s; for (long long int i = 1; i * i * i <= n; ++i) { s[i * i * i] = 1; if (s.find(n - i * i * i) != s.end()) { return true; } } return false; } void pairsort(long long int a[], long long int b[], long long int n) { pair<long long int, long long int> pairt[n]; for (long long int i = 0; i < n; i++) { pairt[i].first = a[i]; pairt[i].second = b[i]; } sort(pairt, pairt + n); for (long long int i = 0; i < n; i++) { a[i] = pairt[i].first; b[i] = pairt[i].second; } } bool isPrime(long long int n) { if (n <= 1) return false; for (long long int i = 2; i < n; i++) if (n % i == 0) return false; return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long int T; cin >> T; for (long long int t = 0; t < T; t++) { long long int c, m, x; cin >> c >> m >> x; long long int ans = 0; if (min(x, min(c, m)) == x) { ans += x; c = c - x; m = m - x; ans += min(min(c, m), (c + m) / 3); } else { ans += min(m, c); } cout << ans << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1005; int n; int a[N]; bool vis[N]; int get() { for (int i = 0; i <= n; i++) vis[i] = 0; for (int i = 0; i < n; i++) vis[a[i]] = 1; for (int i = 0; i <= n; i++) { if (!vis[i]) return i; } } bool check() { for (int i = 1; i < n; i++) if (a[i] < a[i - 1]) return 0; return 1; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int idx = n; vector<int> v; while (!check()) { int curMex = get(); if (idx == curMex) { a[--idx] = curMex; v.push_back(idx); } else { a[curMex] = curMex; v.push_back(curMex); } } cout << v.size() << "\n"; for (auto &it : v) cout << it + 1 << " "; cout << "\n"; } return 0; }
5
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; using i32 = int; using i64 = long long; using u8 = unsigned char; using u32 = unsigned; using u64 = unsigned long long; using f64 = double; using f80 = long double; long long power(long long a, long long b, long long p) { if (!b) return 1; long long t = power(a, b / 2, p); t = t * t % p; if (b & 1) t = t * a % p; return t; } long long exgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long px, py; long long d = exgcd(b, a % b, px, py); x = py; y = px - a / b * py; return d; } template <class T> inline void freshmin(T &a, const T &b) { if (a > b) a = b; } template <class T> inline void freshmax(T &a, const T &b) { if (a < b) a = b; } namespace io { const int BUFSIZE = 1 << 20; int isize, osize; char ibuf[BUFSIZE + 10], obuf[BUFSIZE + 10]; char *is, *it, *os = obuf, *ot = obuf + BUFSIZE; char getchar() { if (is == it) { is = ibuf; it = ibuf + fread(ibuf, 1, BUFSIZE, stdin); if (is == it) return EOF; } return *is++; } char getalpha() { char c = getchar(); while (!isalpha(c)) c = getchar(); return c; } void putchar(char c) { *os++ = c; if (os == ot) { fwrite(obuf, 1, BUFSIZE, stdout); os = obuf; } } int inp() { int x = 0, f = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') f = 1; for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; return f ? -x : x; } long long inp_ll() { long long x = 0; int f = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') f = 1; for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; return f ? -x : x; } template <class T> bool read(T &x) { x = 0; char ch = getchar(); if (ch == EOF) return 0; for (; !isdigit(ch);) { ch = getchar(); if (ch == EOF) return 0; } for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; return 1; } template <class T> void write(T x) { static char s[22]; static char *it = s + 20; static char *end = s + 20; if (x < 0) { putchar('-'); x = -x; } do { *--it = x % 10 + '0'; x /= 10; } while (x); for (; it < end; ++it) putchar(*it); } template <> void write(const char *s) { for (; *s; ++s) putchar(*s); } template <> void write(char c) { putchar(c); } template <class T, class V> void write(T x, V y) { write(x); write(y); } template <class T> void writeln(T x) { write(x); putchar('\n'); } struct ender { ~ender() { if (os != obuf) fwrite(obuf, 1, os - obuf, stdout); } } __ender; } // namespace io template <class T> void print(const T &a) { for (auto x : a) printf("%d ", x); puts(""); } const int MAXN = 1 << 18; const int MAXK = 17; const int INF = 1000000010; using namespace io; using tiii = tuple<int, int, int>; int n, m; vector<pair<int, int> > v[MAXN]; vector<tiii> e; int pre[MAXN], weight[MAXN], dis[MAXN]; void dfs(int x) { for (auto p : v[x]) { int y = p.first, z = p.second; if (y == pre[x]) continue; pre[y] = x; weight[y] = z; dis[y] = dis[x] + 1; dfs(y); } } int F[MAXN]; int father(int x) { return F[x] == x ? x : F[x] = father(F[x]); } const int K = 2; const int mods[] = {1000000007, 998244353, 1000000009}; void fwt_xor(int *a, int n, int mod, int rev = 0) { int MOD = mod; int INV2 = (mod + 1) / 2; for (int i = 1; i < n; i <<= 1) for (int j = 0, p = i << 1; j < n; j += p) for (int k = 0; k < i; ++k) { int x = a[j + k], y = a[i + j + k]; a[j + k] = (x + y) % MOD; a[i + j + k] = (x + MOD - y) % MOD; if (rev) { a[j + k] = (u64)a[j + k] * INV2 % MOD; a[i + j + k] = (u64)a[i + j + k] * INV2 % MOD; } } } void FWT(int a[], int n, int mod) { int MOD = mod; for (int d = 1; d < n; d <<= 1) for (int m = d << 1, i = 0; i < n; i += m) for (int j = 0; j < d; j++) { long long x = a[i + j], y = a[i + j + d]; a[i + j] = (x + y) % MOD; a[i + j + d] = (x - y + MOD) % MOD; } } void IFWT(int a[], int n, int mod) { int MOD = mod; int inv = (mod + 1) / 2; for (int d = 1; d < n; d <<= 1) for (int m = d << 1, i = 0; i < n; i += m) for (int j = 0; j < d; j++) { long long x = a[i + j], y = a[i + j + d]; a[i + j] = (x + y) % MOD; a[i + j + d] = (x - y + MOD) % MOD; a[i + j] = (long long)a[i + j] * inv % MOD; a[i + j + d] = (long long)a[i + j + d] * inv % MOD; } } int a[K][MAXN]; int b[MAXN]; void solve() { n = inp(); m = inp(); int sum = 0; for (int i = 1; i <= n; ++i) F[i] = i; for (int i = 1; i <= m; ++i) { int x = inp(), y = inp(), z = inp(); sum ^= z; int fx = father(x), fy = father(y); if (fx == fy) { e.push_back(tiii{x, y, z}); } else { F[fx] = fy; v[x].push_back({y, z}); v[y].push_back({x, z}); } } dfs(1); for (int k = 0; k < K; ++k) { a[k][sum] = 1; fwt_xor(a[k], 1 << 17, mods[k]); } for (auto p : e) { int x, y, z; tie(x, y, z) = p; vector<int> w(1, z); while (x != y) { if (dis[x] < dis[y]) swap(x, y); w.push_back(weight[x]); x = pre[x]; } for (int k = 0; k < K; ++k) { for (int i = 0; i < 1 << 17; ++i) b[i] = 0; for (auto x : w) b[x]++; fwt_xor(b, 1 << 17, mods[k]); for (int i = 0; i < 1 << 17; ++i) a[k][i] = (u64)a[k][i] * b[i] % mods[k]; } } for (int k = 0; k < K; ++k) { fwt_xor(a[k], 1 << 17, mods[k], 1); } for (int i = 0; i < 1 << 17; ++i) if (a[0][i] || a[1][i]) { printf("%d %d\n", i, a[0][i]); break; } } int main() { solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; int a[100000]; int main() { int n, min1 = 100000; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int x = a[0], y = 0; for (int i = 1; i < n; i++) { if (x > a[i]) { x = a[i]; y = i; } } for (int i = 0; i < n; i++) if (x == a[i] && y != i) { min1 = min(min1, abs(y - i)); y = i; } cout << min1 << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int ind[205], dp[205][205]; bool q(int a, int b) { if (~dp[a][b]) return dp[a][b]; if (a == b) return false; printf("? %d %d\n", a, b); fflush(stdout); char c; scanf(" %c", &c); return dp[a][b] = c == '>' ? 1 : 0; } void solve() { int n; scanf("%d", &n); memset(dp, -1, sizeof dp); iota(ind, ind + 2 * n + 1, 0); for (int i = 1; i <= n; i++) if (q(i + n, i)) swap(ind[i], ind[i + n]); sort(ind + n + 1, ind + 2 * n + 1, q); for (int i = 1; i <= n; i++) if (abs(ind[i] - ind[n + 1]) == n) swap(ind[1], ind[i]); swap(ind[2], ind[n + 1]); sort(ind + 3, ind + 2 * n + 1, q); q(ind[2], ind[n + 1]); printf("!\n"); fflush(stdout); } int main() { int t; scanf("%d", &t); while (t--) solve(); return 0; }
9
#include <bits/stdc++.h> using namespace std; long long query(vector<long long>& ft, int pos) { long long ans = 0; while (pos > 0) { ans += ft[pos - 1]; pos -= (pos & (-pos)); } return ans; } void update(vector<long long>& ft, int pos, long long val, int& ceil) { while (pos <= ceil) { ft[pos - 1] += val; pos += (pos & (-pos)); } } int main() { int n; cin >> n; vector<pair<int, int>> p(n); vector<int> v(n); for (int i = 0; i < n; i++) cin >> p[i].first; for (int i = 0; i < n; i++) { cin >> p[i].second; v[i] = p[i].second; } sort(p.begin(), p.end()); sort(v.begin(), v.end()); v.resize(unique(v.begin(), v.end()) - v.begin()); int ceil = v.size(); vector<long long> ftx(ceil), ftc(ceil); long long ans = 0; for (int i = 0; i < n; i++) { int pos = lower_bound(v.begin(), v.end(), p[i].second) - v.begin(); ans += query(ftc, pos + 1) * (1ll * p[i].first) - query(ftx, pos + 1); update(ftc, pos + 1, 1, ceil); update(ftx, pos + 1, p[i].first, ceil); } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long int MOD = 1000000007; long long int N; long long int num[100000]; long long int on[50]; long long int over[50]; long long int box[50]; list<long long int> ans; int main() { ios::sync_with_stdio(false); box[0] = 1; for (int i = 1; i < 50; i++) { box[i] = box[i - 1] * 2; } cin >> N; for (int i = 0; i < N; i++) { cin >> num[i]; for (int j = 49; j >= 0; j--) { if (num[i] == box[j]) { on[j]++; break; } if (num[i] > box[j]) { over[j]++; break; } } } for (int i = 0; i < 49; i++) { if (on[i] < on[i + 1]) { over[i + 1] += on[i + 1] - on[i]; on[i + 1] = on[i]; } } for (long long int i = (on[0] + 1) / 2; i <= on[0]; i++) { long long int box_on[50] = {}; long long int box_over[50] = {}; long long int s_over[50] = {}; for (int j = 0; j < 50; j++) { box_over[j] = over[j]; if (on[j] > i) { box_on[j] = i; box_over[j] += on[j] - i; } else { box_on[j] = on[j]; } } s_over[49] = box_over[49]; bool flag = true; for (int j = 48; j >= 0; j--) { s_over[j] = s_over[j + 1] + box_over[j]; if (s_over[j] > box_on[j]) { flag = false; break; } } if (flag) ans.push_back(i); } if (ans.empty()) { cout << "-1\n"; return 0; } for (auto i : ans) { cout << i << " "; } cout << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; a[i] -= i; } priority_queue<long long> q; q.push(a[0]); long long ans = 0; for (long long i = 1; i < n; i++) { q.push(a[i]); if (q.top() > a[i]) { ans += q.top() - a[i]; q.pop(); q.push(a[i]); } } cout << ans; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a; for (int i = 0; i < n; i++) { string s; cin >> s; a.push_back(stoi(s.substr(0, 2)) * 60 + stoi(s.substr(3, 2))); } sort(a.begin(), a.end()); int ans = 0; for (int i = 1; i < n; i++) ans = max(ans, a[i] - a[i - 1] - 1); ans = max(ans, a[0] + 24 * 60 - a[n - 1] - 1); cout << setw(2) << setfill('0') << ans / 60; cout << ':'; cout << setw(2) << setfill('0') << ans % 60; cout << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long n; long long dfs(long long no) { if (n < no) return 0; return (1 + dfs(10 * no) + dfs(10 * no + 1)); } int main() { cin >> n; cout << dfs(1) << '\n'; }
2
#include <bits/stdc++.h> #pragma GCC optimize "O3" using namespace std; constexpr int RND = 3100, HP = 200; int hp[2], dt[2], l[2], r[2], p[2]; double dp[2][RND][HP + 1], df0[2], df1[2]; int main() { for (int i = 0; i < 2; ++i) { scanf("%d %d %d %d %d", hp + i, dt + i, l + i, r + i, p + i); dp[i][0][hp[i]] = 1; df0[i] = double(100 - p[i]) / 100 / (r[i] - l[i] + 1); df1[i] = p[i] / 100.0; } if (p[0] == 100) { puts("0"); return 0; } else if (p[1] == 100) { puts("1"); return 0; } for (int i = 1; i < RND; ++i) for (int j = 0; j < 2; ++j) for (int k = 0; k <= hp[j]; ++k) { for (int m = l[1 - j]; m <= r[1 - j]; ++m) dp[j][i][max(0, k - m)] += dp[j][i - 1][k] * df0[1 - j]; dp[j][i][k] += dp[j][i - 1][k] * df1[1 - j]; } double ans = 0; for (int i = 1; i < RND; ++i) { int t = ((i - 1) * dt[0] + dt[1] - 1) / dt[1]; if (t >= RND) break; ans += (1 - dp[0][t][0]) * (dp[1][i][0] - dp[1][i - 1][0]); } printf("%.8f\n", ans); }
8
#include <bits/stdc++.h> using namespace std; inline long long read() { long long w = 0, f = 1; char ch = getchar(); while (ch < '0' or ch > '9') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' and ch <= '9') w = w * 10 + ch - '0', ch = getchar(); return w * f; } const long long maxn = 2e5 + 5, mod = 998244353; long long N, K, h[maxn], fac[maxn], faci[maxn]; long long qpow(long long x, long long y) { long long cnt = 1, basic = x; while (y) { if (y & 1) cnt = cnt * basic % mod; basic = basic * basic % mod, y >>= 1; } return cnt; } long long C(long long x, long long y) { return fac[x] * faci[x - y] % mod * faci[y] % mod; } signed main() { N = read(), K = read(); for (long long i = 0; i < N; i++) h[i] = read(); long long a = 0, b = 0; for (long long i = 0; i < N; i++) { if (h[i] == h[(i + 1) % N]) a++; else b++; } fac[0] = 1; for (long long i = 1; i <= b; i++) fac[i] = fac[i - 1] * i % mod; faci[b] = qpow(fac[b], mod - 2); for (long long i = b - 1; i >= 0; i--) faci[i] = faci[i + 1] * (i + 1) % mod; long long ans = qpow(K, a), div2 = qpow(2, mod - 2), sum = 0; for (long long i = 1; i <= b; i++) { long long tmp; if (i % 2) tmp = qpow(2, i - 1); else tmp = qpow(2, i - 1) - C(i, i / 2) * div2 % mod; sum += C(b, b - i) * qpow(K - 2, b - i) % mod * tmp % mod; sum %= mod; } cout << (ans * sum % mod + mod) % mod; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int n, r = 0; cin >> n; string s; cin >> s; for (int i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) r++; } cout << r; return 0; }
0
#include <bits/stdc++.h> int main() { int n, k; scanf("%d", &n); if (n % 2 == 0) { for (k = 0; k < (n / 2); k++) { printf("1"); } } else { printf("7"); for (k = 1; k < (n / 2); k++) { printf("1"); } } printf("\n"); return 0; }
2
#include <bits/stdc++.h> int main() { int m, n; scanf("%d%d", &m, &n); char s[m][n + 1]; getchar(); int i; int j; int count[3] = {0}; for (i = 0; i < m; i++) scanf("%s", &s[i]); int temp1 = 0; int temp2 = 0; int kind1 = 0; int kind2 = 0; for (i = 0; i < m; i++) for (j = 0; j < n; j++) { if (s[i][j] == 'R') count[0]++; else if (s[i][j] == 'G') count[1]++; else if (s[i][j] == 'B') count[2]++; if (j != n - 1 && s[i][j] != s[i][j + 1]) kind1 = 1; if (i != m - 1 && s[i][j] != s[i + 1][j]) kind2 = 1; } if (count[0] == count[1] && count[1] == count[2] && !(kind1 & kind2)) { if (kind1 && n % 3 == 0) { for (i = 1; i < n; i++) if (s[0][i] != s[0][i - 1]) temp1++; if (temp1 == 2) { printf("YES"); } else printf("NO"); } else if (kind2 && m % 3 == 0) { for (i = 1; i < m; i++) if (s[i][0] != s[i - 1][0]) temp2++; if (temp2 == 2) { printf("YES"); } else printf("NO"); } else printf("NO"); } else printf("NO"); return 0; }
4
#include <bits/stdc++.h> using namespace std; const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342; int t[3010][3010], d[3010], n, m; vector<int> g[3010]; pair<int, int> V[3010][3010]; void bfs(int s) { for (int i = 0; i < (n); ++i) d[i] = 3010; queue<int> q; q.push(s); d[s] = 0; while (!q.empty()) { int akt = q.front(); q.pop(); for (int i = 0; i < (g[akt].size()); ++i) if (d[g[akt][i]] > d[akt] + 1) { d[g[akt][i]] = d[akt] + 1; q.push(g[akt][i]); } } for (int i = 0; i < (n); ++i) { d[i] = d[i] == 3010 ? -1 : d[i]; t[s][i] = d[i]; V[s][i] = make_pair(d[i], i); } sort(V[s], V[s] + n); reverse(V[s], V[s] + n); } pair<int, pair<int, int> > pom[4 * 3010]; vector<pair<int, pair<int, int> > > V1[3010]; bool used[3010]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < (m); ++i) { int a, b; scanf("%d%d", &a, &b); a--; b--; g[a].push_back(b); } for (int i = 0; i < (n); ++i) bfs(i); for (int i = 0; i < (n); ++i) { int pos = 0; for (int j = 0; j < (n); ++j) { used[j] = 0; pom[pos++] = make_pair( V[i][j].first + t[V[i][j].second][V[V[i][j].second][0].second], make_pair(V[i][j].second, V[V[i][j].second][0].second)); pom[pos++] = make_pair( V[i][j].first + t[V[i][j].second][V[V[i][j].second][1].second], make_pair(V[i][j].second, V[V[i][j].second][1].second)); pom[pos++] = make_pair( V[i][j].first + t[V[i][j].second][V[V[i][j].second][2].second], make_pair(V[i][j].second, V[V[i][j].second][2].second)); pom[pos++] = make_pair( V[i][j].first + t[V[i][j].second][V[V[i][j].second][3].second], make_pair(V[i][j].second, V[V[i][j].second][3].second)); } sort(pom, pom + 4 * n); reverse(pom, pom + 4 * n); for (int j = 0; j < (4 * n); ++j) { if (pom[j].second.first == i || pom[j].second.second == i) continue; if (pom[j].second.first == pom[j].second.second) continue; if (t[i][pom[j].second.first] <= 0 || t[pom[j].second.first][pom[j].second.second] <= 0) continue; if (used[pom[j].second.first] && used[pom[j].second.second]) continue; V1[i].push_back(pom[j]); used[pom[j].second.first] = used[pom[j].second.second] = 1; if (V1[i].size() == 4) break; } } int mxv = 0, a, b, c, d; for (int i = 0; i < (n); ++i) for (int j = 0; j < (n); ++j) { if (t[i][j] <= 0) continue; int sel = 0; while (sel < V1[j].size() && (V1[j][sel].second.first == i || V1[j][sel].second.second == i)) sel++; if (sel == V1[j].size()) continue; if (t[i][j] + V1[j][sel].first > mxv) { a = i + 1; b = j + 1; c = V1[j][sel].second.first + 1; d = V1[j][sel].second.second + 1; mxv = t[i][j] + V1[j][sel].first; } } printf("%d %d %d %d\n", a, b, c, d); return 0; }
6
#include <bits/stdc++.h> using namespace std; vector<int> g[10100]; int d[1010]; int val[1010][1010]; int leaf[1010][2]; int f[1010]; void dfs(int fa, int a); void solve(int la, int lb, int c); struct dat { int a, b, c; dat(int x = 0, int y = 0, int z = 0) { a = x, b = y, c = z; } } ans[10010]; int main() { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int a, b, c; cin >> a >> b >> c; g[a].push_back(b); g[b].push_back(a); val[a][b] = val[b][a] = c; d[a]++; d[b]++; } int root = 0; for (int i = 1; i <= n; i++) if (d[i] == 1) root = i; dfs(0, root); for (int i = 1; i <= n; i++) { if (i != root) { int fa = f[i]; solve(leaf[i][0], root, val[i][fa] / 2); solve(leaf[i][1], root, val[i][fa] / 2); solve(leaf[i][0], leaf[i][1], -val[i][fa] / 2); if (fa != root) { solve(leaf[fa][0], root, -val[i][fa] / 2); solve(leaf[fa][1], root, -val[i][fa] / 2); solve(leaf[fa][0], leaf[fa][1], val[i][fa] / 2); } } } for (int i = 1; i <= n; i++) { if (d[i] == 2) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; cout << ans[0].a << endl; for (int i = 1; i <= ans[0].a; i++) { cout << ans[i].a << " " << ans[i].b << " " << ans[i].c << endl; } } void solve(int la, int lb, int c) { if (la == lb) return; ans[++ans[0].a] = dat(la, lb, c); } void dfs(int fa, int a) { vector<int> s; for (auto to : g[a]) { if (to != fa) { f[to] = a; dfs(a, to); s.push_back(leaf[to][0]); } } if (d[a] == 1) leaf[a][0] = leaf[a][1] = a; else if (s.size() == 1) leaf[a][0] = leaf[a][1] = s[0]; else { leaf[a][0] = s[0]; leaf[a][1] = s[1]; } }
4
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 9; struct Point { long long x, y; } p[N], ch[N]; long long cross(Point a, Point b, Point c) { return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y); } long long sum[N]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lld", sum + i); for (int i = 1; i <= n; ++i) sum[i] += sum[i - 1]; for (int i = 0; i <= n; ++i) p[i] = (Point){i, sum[i]}; ch[0] = p[0]; int top = 0; for (int i = 1; i <= n; ++i) { while (top > 0 && cross(ch[top - 1], ch[top], p[i]) < 0) --top; ch[++top] = p[i]; } for (int i = 1; i <= top; ++i) { double val = 1.0 * (ch[i].y - ch[i - 1].y) / (ch[i].x - ch[i - 1].x); for (int j = ch[i - 1].x + 1; j <= ch[i].x; ++j) printf("%.9f\n", val); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const double eps = 1e-7; const int MOD = 1e9 + 7; const long long INFLL = 0x3f3f3f3f3f3f3f3f; const int INF = 0x3f3f3f3f; const int MAXN = 2e5 + 10; int vis[MAXN], a[MAXN], vis2[MAXN], cont[MAXN]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { int x; scanf("%d", &a[i]); vis[a[i]]++; } priority_queue<int, vector<int>, greater<int> > q; int cnt = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) q.push(i), vis2[i] = 1, cnt++; } for (int i = 1; i <= n; i++) { if (vis[a[i]] >= 2) { if (cont[a[i]] || a[i] > q.top()) { vis[a[i]]--; a[i] = q.top(); q.pop(); } else { cont[a[i]] = 1; } } } printf("%d\n", cnt); for (int i = 1; i <= n; i++) printf("%d ", a[i]); }
3
#include <bits/stdc++.h> using namespace std; template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, true : false; } template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, true : false; } int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, s; int a[200005], f[200005], to[200005]; pair<int, int> b[200005]; bool vis[200005]; vector<vector<int> > ans; int getf(int x) { return x == f[x] ? x : f[x] = getf(f[x]); } int main() { n = readint(); s = readint(); for (int i = 1; i <= n; i++) a[i] = readint(), b[i] = make_pair(a[i], i); sort(b + 1, b + n + 1); for (int i = 1; i <= n; i++) to[b[i].second] = i; for (int i = 1; i <= n; i++) while (i != to[i] && a[to[i]] == b[to[i]].first) swap(b[to[i]].second, b[to[to[i]]].second), swap(to[to[i]], to[i]); for (int i = 1; i <= n; i++) f[i] = i; int all = n, cnt = 0; for (int i = 1; i <= n; i++) { if (i == to[i]) all--; if (vis[i] || i == to[i]) continue; vis[i] = 1; cnt++; for (int j = to[i]; j != i; j = to[j]) vis[j] = 1, f[j] = i; } int lst = 0; for (int i = 1; i <= n; i++) { if (b[i].second == i) continue; if (!lst || b[i].first != b[lst].first) { lst = i; continue; } if (getf(b[lst].second) != getf(b[i].second)) { f[f[b[i].second]] = f[b[lst].second]; swap(to[b[i].second], to[b[lst].second]); swap(b[i].second, b[lst].second); cnt--; } } if (all > s) return printf("-1\n"), 0; int tmp = s - all, num = 0; vector<int> now(0), rem(0); for (int i = 1; i <= n; i++) { if (i == to[i]) continue; if (i != f[i]) continue; now.push_back(i); for (int j = to[i]; j != i; j = to[j]) now.push_back(j); rem.push_back(i); num++; if (num >= tmp) { ans.push_back(now); reverse(rem.begin(), rem.end()); if (rem.size() > 1) ans.push_back(rem); now.clear(), rem.clear(); } } if (now.size()) ans.push_back(now); reverse(rem.begin(), rem.end()); if (rem.size() > 1) ans.push_back(rem); printf("%d\n", ans.size()); for (auto v : ans) { printf("%d\n", v.size()); for (auto x : v) printf("%d ", x); printf("\n"); } return 0; }
11
#include <bits/stdc++.h> using namespace std; constexpr int N = 1e5 + 10; int n, m, u, v, next[N], p, cmp; vector<int> node[N], ve, vec; map<int, bool> mp[N]; queue<int> q[N]; bool mark[N]; set<int> st; void dfs(int v) { vec.push_back(v); mark[v] = true; for (auto i : node[v]) if (!mark[i]) dfs(i); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> u >> v; node[u].push_back(v); node[v].push_back(u); mp[u][v] = true; mp[v][u] = true; } if (n <= 7) { int arr[] = {1, 2, 3, 4, 5, 6, 7}; do { int z = m; bool cant = false; for (int i = 0; i < n - 1; i++) { if (z && mp[arr[i]][arr[i + 1]]) { cant = true; break; } z--; } if (z && mp[arr[0]][arr[n - 1]]) cant = true; if (!cant) { z = m; for (int i = 0; z && i < n - 1; i++) { cout << arr[i] << ' ' << arr[i + 1] << '\n'; z--; } if (z) cout << arr[0] << ' ' << arr[n - 1] << '\n'; return 0; } } while (next_permutation(arr, arr + n)); cout << -1 << '\n'; } else { for (int i = 1; i <= n; i++) { if (!mark[i]) { ve.clear(); vec.clear(); dfs(i); for (int j = 0; j < (int)vec.size(); j += 2) ve.push_back(vec[j]); for (int j = 1; j < (int)vec.size(); j += 2) ve.push_back(vec[j]); if (!(ve.size() & 1)) swap(ve[0], ve[1]); for (auto j : ve) q[cmp].push(j); st.insert(cmp); cmp++; } } vector<int> ans; while (st.size()) { vector<int> deleted; for (auto i : st) { ans.push_back(q[i].front()); q[i].pop(); if (q[i].empty()) deleted.push_back(i); } for (auto i : deleted) st.erase(i); } ans.push_back(ans[0]); for (int i = 0; m && i < (int)ans.size(); i++) { cout << ans[i] << ' ' << ans[i + 1] << '\n'; m--; } } return 0; }
8
#include <bits/stdc++.h> using namespace std; inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } const int maxn = (int)1e5; int a[maxn]; bool solve() { int n; if (scanf("%d", &n) < 1) return 0; for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); long long x1 = 0; for (int i = 0; i < n; i++) x1 += (long long)a[i] * (4 * i - 2 * n + 3); long long x2 = n; long long d = gcd(x1, x2); x1 /= d, x2 /= d; printf("%I64d %I64d\n", x1, x2); return 1; } int main() { while (1) { if (!solve()) break; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int a[1610][1610], b[1610][1610], n, m, pd[1610][1610], w[1610][1610], sign, gox[4] = {1, -1, 0, 0}, goy[4] = {0, 0, 1, -1}, ans[2000000], c[1610][1610], len; void dfs(int k1, int k2) { pd[k1][k2] = 1; w[k1][k2] = sign; for (int i = 0; i < 4; i++) { int k3 = k1 + gox[i], k4 = k2 + goy[i]; if (pd[k3][k4] == 0 && a[k3][k4]) dfs(k3, k4); } } void getin() { memcpy(c, b, sizeof b); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (b[i][j] == 0) for (int k = 0; k < 4; k++) { int k1 = i + gox[k], k2 = j + goy[k]; c[k1][k2] = 0; } memcpy(b, c, sizeof c); } void getout() { memcpy(c, b, sizeof b); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (b[i][j] == 1) for (int k = 0; k < 4; k++) { int k1 = i + gox[k], k2 = j + goy[k]; if (k1 > 0 && k1 <= n && k2 > 0 && k2 <= m) c[k1][k2] = 1; } memcpy(b, c, sizeof c); } void dfs2(int k1, int k2) { int gox[8] = {1, 1, 1, -1, -1, -1, 0, 0}, goy[8] = {1, -1, 0, 1, -1, 0, 1, -1}; pd[k1][k2] = 1; w[k1][k2] = sign; for (int i = 0; i < 8; i++) { int k3 = k1 + gox[i], k4 = k2 + goy[i]; if (pd[k3][k4] == 0 && a[k3][k4]) dfs2(k3, k4); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { scanf("%d", &a[i][j]); b[i][j] = a[i][j]; } sign = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (pd[i][j] == 0 && a[i][j]) { sign++; dfs(i, j); } for (int i = 1; i <= 3; i++) getin(); for (int i = 1; i <= 5; i++) getout(); memcpy(c, a, sizeof a); memset(a, 0x00, sizeof a); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (b[i][j] == 0 && c[i][j] == 1) a[i][j] = 1; len = sign; sign = 0; memset(pd, 0x00, sizeof pd); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (a[i][j] && pd[i][j] == 0) { sign++; ans[w[i][j]]++; dfs2(i, j); } sort(ans + 1, ans + len + 1); printf("%d\n", len); for (int i = 1; i <= len; i++) printf("%d ", ans[i]); cout << endl; return 0; }
7
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long int MODN = 1e9 + 9; inline long long int modpow(long long int x, long long int exp) { long long int rtn = 1; while (exp) { if (exp & 1) rtn = rtn * x % MODN; exp >>= 1, x = x * x % MODN; } return rtn; } inline long long int inv(long long int x) { return modpow(x, MODN - 2); } long long int n, a, b, k; string s; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> a >> b >> k >> s; assert((n + 1) % k == 0); assert(s.size() == k); long long int tmp = modpow(a, n), sum = 0, r = b * inv(a) % MODN; for (int i = 0; i < k; ++i) { if (s[i] == '+') sum = (sum + tmp) % MODN; else sum = (sum - tmp) % MODN; tmp = (tmp * r) % MODN; } r = modpow(r, k), n = (n + 1) / k; if (r == 1) { cout << ((sum * n % MODN) + MODN) % MODN << endl; } else { long long int q = inv(r - 1), p = (modpow(r, n) - 1) * sum % MODN; cout << ((p * q % MODN) + MODN) % MODN << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; namespace IO { template <class T> inline bool read(T &x) { int c = getchar(); int sgn = 1; while (~c && c < '0' || c > '9') { if (c == '-') sgn = -1; c = getchar(); } for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; x *= sgn; return ~c; } } // namespace IO namespace Utility { template <typename T> T gcd(T a, T b) { return a ? gcd(b % a, a) : b; } template <typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); } template <typename T> inline T addmod(T a, T b, T mod) { T c = a + b; return c > mod ? c - mod : c; } template <typename T> inline T submod(T a, T b, T mod) { T c = a - b; return c < mod ? c + mod : c; } template <typename T> inline T mulmod(T a, T b, T mod) { return a * 1LL * b % mod; } template <typename T> T exEuclid(T a, T b, T &x, T &y) { x = 0, y = 1; if (a == 0) return b; T x1, y1; T d = exEuclid(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } template <typename T> T modInv(T a, T mod) { T x, y; exEuclid(a, mod, x, y); return x < 0 ? x + mod : x; } template <typename T> T bigmod(T b, T p, T mod) { T ret = b > 0; while (p) { if (p & 1) ret = mulmod(ret, b, mod); p >>= 1, b = mulmod(b, b, mod); } return ret; } } // namespace Utility using namespace IO; using namespace Utility; const int INF = 2000000009; const int MX = 200005; const double EPS = 1e-9; const double PI = acos(-1.0); const int MOD = 1000000007; int fac[MX], ifac[MX], inv[MX], pw[MX]; int ncr(int n, int r) { if (r > n || r < 0) return 0; return (int)(1LL * fac[n] * ifac[r] % MOD * ifac[n - r] % MOD); } int main() { fac[0] = fac[1] = ifac[0] = ifac[1] = inv[0] = inv[1] = 1; pw[0] = 1, pw[1] = 2; for (int i = 2; i < MX; i++) { fac[i] = 1LL * fac[i - 1] * i % MOD; inv[i] = MOD - 1LL * inv[MOD % i] * (MOD / i) % MOD; ifac[i] = 1LL * ifac[i - 1] * inv[i] % MOD; pw[i] = pw[i - 1] * 2 % MOD; } int n = ({ int a; read(a); a; }); long long T = ({ long long a; read(a); a; }), t = 0LL; int sum = 0LL; int k = 0, ways = 1; for (int i = 1; i <= n; i++) { t += ({ long long a; read(a); a; }); if (t > T) break; int cur_k = (int)min(1LL * i, T - t); while (k > cur_k) { ways = (ways - ncr(i - 1, k--)); if (ways < 0) ways += MOD; } while (k < cur_k) { ways = (ways + ncr(i - 1, ++k)) % MOD; } ways = ways * 2 % MOD; ways -= ncr(i - 1, k); if (ways < 0) ways += MOD; sum = (sum + 1LL * ways * pw[n - i] % MOD) % MOD; } int ans = 1LL * sum * modInv(pw[n], MOD) % MOD; printf("%d\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; int n, t, m; char s[200100]; int a[200100]; int point; void print() { while (m - 1 >= point && a[m - 1] == 0) m--; for (int i = 0; i < m; ++i) { if (i == point) { printf("."); } printf("%d", a[i]); } exit(0); } int main() { scanf("%d%d", &n, &t); scanf("%s", s); for (int i = 0; s[i]; ++i) { if (s[i] == '.') { point = i; } else { a[m++] = s[i] - '0'; } } int flag = -1; for (int i = point; i < m; ++i) { if (a[i] >= 5) { flag = i; break; } } if (flag == -1) { print(); } int nex = m - 1; for (int i = flag; i >= point; --i) { while (a[i] >= 10) a[i - 1]++, a[i] -= 10; if (a[i] >= 5 && t) { t--; a[i - 1]++; for (int j = i; j <= nex; ++j) { a[j] = 0; } nex = i; } } for (int i = point; i; --i) { while (a[i] >= 10) a[i - 1]++, a[i] -= 10; } print(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long N = 109; long long n, m; pair<long long, long long> p[N][N]; long long dp[N][N]; long long DP(long long l, long long r) { if (dp[l][r] != -1) return dp[l][r]; if (r < l) return dp[l][r] = 0; for (long long i = l; i <= r; i++) { long long num = 0; for (long long j = 0; j < n; j++) if (p[j][i].first >= l && p[j][i].second <= r) num++; dp[l][r] = max(dp[l][r], DP(l, i - 1) + DP(i + 1, r) + num * num); } return dp[l][r]; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (long long i = 0; i < n; i++) { long long k; cin >> k; while (k--) { long long l, r; cin >> l >> r; for (long long j = l; j <= r; j++) p[i][j] = {l, r}; } } for (long long i = 0; i < N; i++) for (long long j = 0; j < N; j++) dp[i][j] = -1; cout << DP(1, m); return 0; }
10
#include <bits/stdc++.h> using namespace std; int main() { int arr[3], arr1[3], sum = 0, sum1 = 0; int n; for (int i = 0; i < 3; i++) { cin >> arr[i]; sum += arr[i]; } for (int i = 0; i < 3; i++) { cin >> arr1[i]; sum1 += arr1[i]; } cin >> n; int s, k; if (sum % 5 == 0) s = sum / 5; else s = sum / 5 + 1; if (s > n) { cout << "NO"; return 0; } n -= s; if (sum1 % 10 == 0) k = sum1 / 10; else k = sum1 / 10 + 1; if (k > n && k > 0) { cout << "NO"; return 0; } n -= k; cout << "YES"; }
0
#include<bits/stdc++.h> #define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++) #define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--) #define pii pair<int,int> #define fi first #define se second #define PB push_back #define ll long long #define ull unsigned long long #define pll pair<ll,ll> #define y1 orzkcz using namespace std; const int N=1000005; int n,m,Q,mnv; int a[N],b[N]; struct Tarr1{ int s[N],t[N]; void change2(int x,int v){ for (;x<N;x+=x&(-x)) t[x]+=v; } void change(int x,int v){ if (s[x]) change2(x,-1); s[x]+=v; if (s[x]) change2(x,1); } int ask(int x){ int s=0; for (;x;x-=x&(-x)) s+=t[x]; return s; } int askrk(int x){ int p=0,S=1<<19; for (;S;S>>=1) if (p+S<N&&x>t[p+S]) p+=S,x-=t[p]; return p+1; } }T1,T2; struct Tarr2{ ll t[N],s; void change(int x,int v){ s+=v; for (;x<N;x+=x&(-x)) t[x]+=v; } ll ask(int x){ ll s=0; for (;x;x-=x&(-x)) s+=t[x]; return s; } }T3,T4,T5,T6; void add1(int x,int v){ T1.change(x,v); T3.change(x,v); T5.change(x,v*x); } void add2(int x,int v){ T2.change(x,v); T4.change(x,v); T6.change(x,v*x); } ll F1(int k,int x){ ll re=0; re-=(k-x)*T4.s+T6.s; if (x>=k) re-=(x-k)*T4.ask(x-k)-T6.ask(x-k); re+=(k-x+max(0,x-k-min(x-k,mnv)))*(T3.s-1)+(T5.s-x); return re+k; } ll F2(int k,int x){ ll re=0; re-=(k-x)*(T4.s-1)+(T6.s-x); if (x>=k) re-=(x-k)*T4.ask(x-k)-T6.ask(x-k); re+=(k-x+max(0,x-k-min(x-k,mnv)))*T3.s+T5.s; return re-k; } ll F1(int x){ ll re=-(1ll<<60); int l=1,r=T1.ask(min(mnv+x,N-1)); re=max(re,F1(x,T1.askrk(l))); if (r) re=max(re,F1(x,T1.askrk(r))); l=T1.ask(min(T2.askrk(T2.ask(N-1))+x,N-1)); l=max(l-2,r+1); r=min(l+4,T1.ask(N-1)); while (r-l>=1){ int m1=(l+r)/2,m2=m1+1; ll v1=F1(x,T1.askrk(m1)); ll v2=F1(x,T1.askrk(m2)); re=max(re,max(v1,v2)); v1<=v2?l=m2:r=m1; } For(i,l,r) re=max(re,F1(x,T1.askrk(i))); return re; } ll F2(int x){ ll re=-(1ll<<60); int mn=min(T1.askrk(1),T2.askrk(1)); int l=1,r=T2.ask(min(mnv+x,N-1)); while (r-l>=1){ int m1=(l+r)/2,m2=m1+1; ll v1=F2(x,T2.askrk(m1)); ll v2=F2(x,T2.askrk(m2)); re=max(re,max(v1,v2)); v1<=v2?l=m2:r=m1; } For(i,l,r) re=max(re,F2(x,T2.askrk(i))); re=max(re,F2(x,T2.askrk(T2.ask(N-1)))); return re; } int main(){ scanf("%d%d%d",&n,&m,&Q); For(i,1,n) scanf("%d",&a[i]); For(i,1,m) scanf("%d",&b[i]); For(i,1,n) add1(++a[i],1); For(i,1,m) add2(++b[i],1); while (Q--){ int op,x,y; scanf("%d%d",&op,&x); if (op==1){ add1(a[x],-1); scanf("%d",&a[x]); add1(++a[x],1); } if (op==2){ add2(b[x],-1); scanf("%d",&b[x]); add2(++b[x],1); } if (op==3){ mnv=min(T1.askrk(1),T2.askrk(1)); printf("%lld\n",max(F1(x),F2(x))); } } }
12
#include <bits/stdc++.h> using namespace std; enum { WHITE = 0, GRAY, BLACK }; enum { UNVISITED = -1 }; const double pi = 2 * acos(0); const double tau = 2.0 * pi; struct BIT { int n; vector<int> ft; BIT(int n) : n(n), ft(n + 1, 0) {} void update(int i, int x) { for (; i <= n; i += i & -i) ft[i] = max(ft[i], x); } int query(int i) { int ans = 0; for (; i > 0; i -= i & -i) ans = max(ans, ft[i]); return ans; } }; int main() { std::ios::sync_with_stdio(false); int n; scanf("%d", &(n)); static pair<int, int> a[100005]; for (int i = 1, ENDD = n; i <= ENDD; i++) { scanf("%d", &(a[i].first)); a[i].second = i; } sort(a + 1, a + n + 1); BIT ft(n); int ans = 0; for (int i = 1, ENDD = n; i <= ENDD; i++) { int tmp = ft.query(a[i].second - 1) + 1; ft.update(a[i].second, tmp); ans = max(ans, tmp); } printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; long long fast_exp(long long base, long long exp, long long mod) { long long res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base) % mod; base = (base * base) % mod; exp /= 2; } return res; } int temp[5555]; int ans[5555]; int arr[5555]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= n; i++) { memset(temp, 0, sizeof(temp)); ; int best = 0; for (int j = i; j <= n; j++) { ++temp[arr[j]]; if (temp[arr[j]] > temp[best] || (temp[arr[j]] == temp[best] && arr[j] < best)) best = arr[j]; ++ans[best]; } } for (int i = 1; i <= n; i++) cout << ans[i] << " "; return 0; }
3
#include <bits/stdc++.h> using namespace std; int N, K; set<pair<int, int> > sx, sy; vector<int> er; int X[200100], Y[200100]; inline void Erase(int x) { sx.erase(pair<int, int>(X[x], x)); sy.erase(pair<int, int>(Y[x], x)); er.push_back(x); } inline void Insert(int x) { sx.insert(pair<int, int>(X[x], x)); sy.insert(pair<int, int>(Y[x], x)); } int main() { scanf("%d %d", &N, &K); for (int i = 0; i < N; i++) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); X[i] = x1 + x2; Y[i] = y1 + y2; sx.insert(pair<int, int>(X[i], i)); sy.insert(pair<int, int>(Y[i], i)); } long long ans = 2000000000000000000ll; for (int i = 0; i <= K; i++) { for (int j = 0; i + j <= K; j++) { for (int k = 0; i + j + k <= K; k++) { int l = K - i - j - k; for (int p = 0; p < i; p++) Erase(sx.begin()->second); for (int p = 0; p < j; p++) Erase(sx.rbegin()->second); for (int p = 0; p < k; p++) Erase(sy.begin()->second); for (int p = 0; p < l; p++) Erase(sy.rbegin()->second); long long dx = (sx.rbegin()->first) - (sx.begin()->first); dx = (dx + 1ll) / 2ll; if (dx == 0ll) dx = 1ll; long long dy = (sy.rbegin()->first) - (sy.begin()->first); dy = (dy + 1ll) / 2ll; if (dy == 0ll) dy = 1ll; ans = min(ans, dx * dy); for (int i = 0; i < er.size(); i++) Insert(er[i]); er.clear(); } } } cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long NR = 2e5 + 20, oo = 1e15, MOD = 1e9 + 7; const long double pi = 2 * acos(0.0); long long n, m, x, y; struct muchie { long long a, b, c; }; vector<muchie> edges; vector<pair<long long, long long> > v[NR]; vector<pair<long long, long long> > v_t[NR]; vector<pair<long long, long long> > v2[NR]; vector<long long> d(NR, oo); vector<long long> d_t(NR, oo); struct cmp { inline bool operator()(const long long i, const long long j) { return d[i] > d[j]; } }; struct cmp_t { inline bool operator()(const long long i, const long long j) { return d_t[i] > d_t[j]; } }; bitset<NR> inq; bitset<NR> inq_t; priority_queue<long long, vector<long long>, cmp> q; priority_queue<long long, vector<long long>, cmp_t> q_t; static void dijkstra() { long long nod, cost; d[x] = 0; q.push(x); while (!q.empty()) { nod = q.top(); q.pop(); inq[nod] = 0; for (vector<pair<long long, long long> >::iterator it = v[nod].begin(); it < v[nod].end(); ++it) { if (d[nod] + (*it).second < d[(*it).first]) { d[(*it).first] = d[nod] + (*it).second; if (!inq[(*it).first]) q.push((*it).first), inq[(*it).first] = 1; } } } } static void dijkstra_t() { long long nod, cost; d_t[y] = 0; q_t.push(y); while (!q_t.empty()) { nod = q_t.top(); q_t.pop(); inq_t[nod] = 0; for (vector<pair<long long, long long> >::iterator it = v_t[nod].begin(); it < v_t[nod].end(); ++it) { if (d_t[nod] + (*it).second < d_t[(*it).first]) { d_t[(*it).first] = d_t[nod] + (*it).second; if (!inq_t[(*it).first]) q_t.push((*it).first), inq_t[(*it).first] = 1; } } } } long long low[NR], lvl[NR], viz[NR], good[NR], prt[NR], cnt; void dfs(long long nod) { viz[nod] = 1; lvl[nod] = ++cnt; low[nod] = lvl[nod]; vector<pair<long long, long long> >::iterator it; for (it = v2[nod].begin(); it != v2[nod].end(); ++it) { if (!viz[(*it).first]) { prt[(*it).first] = (*it).second; dfs((*it).first); low[nod] = min(low[nod], low[(*it).first]); if (low[(*it).first] == lvl[(*it).first]) { good[(*it).second] = 1; } } else { if (prt[nod] != (*it).second) { low[nod] = min(low[nod], low[(*it).first]); } } } } int main() { long long i, a, b, c, diff; muchie temp; ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> x >> y; for (i = 1; i <= m; ++i) { cin >> a >> b >> c; temp.a = a; temp.b = b; temp.c = c; edges.push_back(temp); v[a].push_back(make_pair(b, c)); v_t[b].push_back(make_pair(a, c)); } dijkstra(); dijkstra_t(); for (i = 0; i < edges.size(); ++i) { if (d[edges[i].a] + d_t[edges[i].b] + edges[i].c == d[y]) { v2[edges[i].a].push_back(make_pair(edges[i].b, i + 1)); v2[edges[i].b].push_back(make_pair(edges[i].a, i + 1)); } } for (i = 1; i <= n; ++i) { if (!viz[i]) { dfs(i); } } for (i = 0; i < edges.size(); ++i) { diff = d[edges[i].a] + d_t[edges[i].b] + edges[i].c - d[y] + 1; if (good[i + 1]) { cout << "YES\n"; } else { if (edges[i].c <= diff) cout << "NO\n"; else cout << "CAN " << diff << '\n'; } } return 0; }
7
#include <bits/stdc++.h> using namespace std; long long n; bool ch[51][51]; long long d[51][51]; vector<pair<long long, long long>> v; long long l1, r1, l2, r2; char c[51][51]; long long ans = 1e18; void check(long long l, long long r) { if (ch[l][r]) return; if (l == l1 and r == r1) { cout << 0; exit(0); } v.push_back({l, r}); d[l][r] = abs(l2 - l) + abs(r2 - r); ch[l][r] = true; if (c[l + 1][r] == '0') check(l + 1, r); if (c[l][r + 1] == '0') check(l, r + 1); if (c[l - 1][r] == '0') check(l - 1, r); if (c[l][r - 1] == '0') check(l, r - 1); } long long sqr(long long x) { x = abs(x); return x * x; } void uradi(long long l, long long r) { if (ch[l][r]) return; ch[l][r] = true; for (auto i : v) { ans = min(ans, sqr(i.first - l) + sqr(i.second - r)); } if (c[l + 1][r] == '0') uradi(l + 1, r); if (c[l][r + 1] == '0') uradi(l, r + 1); if (c[l - 1][r] == '0') uradi(l - 1, r); if (c[l][r - 1] == '0') uradi(l, r - 1); } int main() { cin >> n; cin >> l1 >> r1 >> l2 >> r2; if (l1 == l2 and r1 == r2) return cout << 0, 0; for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { cin >> c[i][j]; } } check(l2, r2); memset(ch, false, sizeof(ch)); uradi(l1, r1); cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, m; cin >> n >> m; long long int re[n], cost[n], tot = 0; pair<long long int, long long int> p[n]; for (long long int i = 0; i < n; i++) { cin >> re[i]; tot += re[i]; } for (long long int i = 0; i < n; i++) { cin >> cost[i]; p[i].first = cost[i]; p[i].second = i; } sort(p, p + n); long long int k = 0; for (long long int i = 0; i < m; i++) { long long int a, b; cin >> a >> b; a--; long long int ans = 0; if (b > tot) { tot -= b; cout << ans << endl; } else { tot -= b; if (b <= re[a]) { ans += b * cost[a]; re[a] -= b; } else { ans += re[a] * cost[a]; b -= re[a]; re[a] = 0; while (b > 0) { if (b <= re[p[k].second]) { ans += b * p[k].first; re[p[k].second] -= b; b = 0; } else { ans += (re[p[k].second]) * (p[k].first); b -= re[p[k].second]; re[p[k].second] = 0; k++; } } } cout << ans << endl; } } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); long long int T = 1; while (T--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> mas(n); vector<int> p(n + 1); for (int i = 0; i < n; ++i) { cin >> mas[i]; p[mas[i]] = i; } int len = 0; int tmp = 0; for (int i = 2; i <= n; ++i) { if (p[i] > p[i - 1]) { tmp++; } else { if (tmp + 1 > len) { len = tmp + 1; } tmp = 0; } } if (tmp + 1 > len) { len = tmp + 1; } cout << (n - len) << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s; int i = 5, x = 0; while (i--) { cin >> t; if (t[1] == s[1] || t[0] == s[0]) { x = 1; } } if (x == 1) { cout << "YES"; } else cout << "NO"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int a[maxn], L[maxn], R[maxn]; int main(int argc, char const *argv[]) { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } stack<int> st; for (int i = 1; i <= n; ++i) { while (!st.empty() && a[st.top()] < a[i]) { st.pop(); } if (st.empty()) { L[i] = -1; } else { L[i] = st.top(); } st.push(i); } while (!st.empty()) st.pop(); for (int i = n; i >= 1; --i) { while (!st.empty() && a[st.top()] < a[i]) { st.pop(); } if (st.empty()) { R[i] = -1; } else { R[i] = st.top(); } st.push(i); } int ans = 0; for (int i = 1; i <= n; ++i) { if (L[i] != -1) { ans = max(ans, a[i] ^ a[L[i]]); } if (R[i] != -1) { ans = max(ans, a[i] ^ a[R[i]]); } } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n], sum = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; sum += arr[i]; } bool possible[sum + 1]; vector<vector<int> > possibile2(sum + 1); memset(possible, false, sizeof possible); possible[0] = true; for (int k = 0; k < n; k++) { for (int x = sum; x >= 0; x--) { if (possible[x]) { if (possible[x + arr[k]] == false) { for (int i = 0; i < possibile2[x].size(); i++) possibile2[x + arr[k]].push_back(possibile2[x][i]); possibile2[x + arr[k]].push_back(k + 1); possible[x + arr[k]] = true; } } } } int ans = -1; int minDiff = INT_MAX; for (int i = 1; i < sum; i++) { if (possible[i]) { if (sum % 2 == 0 && i == sum / 2) { continue; } if (abs(2 * i - sum) <= minDiff) { minDiff = abs(2 * i - sum); ans = i; } } } if (ans != -1) { cout << possibile2[ans].size() << endl; for (int i = 0; i < possibile2[ans].size(); i++) { if (i != possibile2[ans].size() - 1) cout << possibile2[ans][i] << " "; else cout << possibile2[ans][i]; } } else cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; unsigned long long int powe(int a, int b) { if (b == 0) return 1; unsigned long long int s = 1LL << (b); return s; } int main() { unsigned long long int m, n; cin >> m >> n; unsigned long long int a[m][n]; for (unsigned long long int i = 0; i < m; i++) { for (unsigned long long int j = 0; j < n; j++) { cin >> a[i][j]; } } unsigned long long int count = 0, o = 1; for (unsigned long long int i = 0; i < m; i++) { unsigned long long int c1 = 0, c2 = 0; for (unsigned long long int j = 0; j < n; j++) { if (a[i][j] == 1) c1++; else c2++; } count = count + powe(2, c1) - o + powe(2, c2) - o; } for (unsigned long long int j = 0; j < n; j++) { unsigned long long int c1 = 0, c2 = 0; for (unsigned long long int i = 0; i < m; i++) { if (a[i][j] == 1) c1++; else c2++; } count = count + powe(2, c1) - o + powe(2, c2) - o; } count -= m * n; cout << count << endl; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; long long t; cin >> t; while (t > 0) { cin >> n; int a[n]; for (int i = 0; i < n; i = i + 1) cin >> a[i]; int c1 = 0, c2 = 0; for (int i = 0; i < n; i = i + 2) { if (a[i] % 2 != 0) c1++; } for (int i = 1; i < n; i = i + 2) { if (a[i] % 2 != 1) c2++; } if (c1 == c2) cout << c1 << endl; else cout << -1 << endl; t--; } return 0; }
0
#include <bits/stdc++.h> using namespace std; struct vertex { vector<int> way; bool state; int a, v; }; int n, m, t1, t2; vertex G[100010]; vector<int> answer; void bfs(void); int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d %d", &t1, &t2); --t1; --t2; G[t1].way.push_back(t2); G[t2].way.push_back(t1); } for (int i = 0; i < n; ++i) { scanf("%d", &G[i].a); G[i].state = false; G[i].v = 0; } bfs(); for (int i = 0; i < n; ++i) { if (G[i].v == G[i].a) { printf("-1"); return 0; } if (G[i].state) answer.push_back(i + 1); } printf("%d\n", answer.size()); for (int i = 0; i < answer.size(); ++i) printf("%d ", answer[i]); } void bfs(void) { int currv, nextv; queue<int> que; for (int i = 0; i < n; ++i) if (G[i].v == G[i].a) que.push(i); while (!que.empty()) { currv = que.front(); que.pop(); if (G[currv].v != G[currv].a) continue; G[currv].state = true; ++G[currv].v; for (int i = 0; i < G[currv].way.size(); ++i) { nextv = G[currv].way[i]; ++G[nextv].v; if (G[nextv].v == G[nextv].a) que.push(nextv); } } }
6
#include <bits/stdc++.h> long long mod = 1e9 + 7; using namespace std; long long powi(long long i, long long b) { long long res = 1; while (b) { if (b & 1) res *= i, res %= mod; b >>= 1; i *= i; i %= mod; } return res; } void solve() { long long n; cin >> n; vector<string> s(n); vector<long long> a(n); long long b = 0; for (long long i = 0; i < n; i++) { cin >> s[i]; long long len = s[i].length(); for (long long j = 0; j < s[i].length(); j++) { if (s[i][j] != s[i][len - 1 - j]) a[i]++; } if (len & 1) { b++; } } long long ans = 0, odd = 0; for (long long i = 0; i < n; i++) { if ((a[i] / 2) & 1) odd++; } ans = n; if (odd & 1) { if (b == 0) ans--; } cout << ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; for (long long _ = 1; _ <= t; _++) { solve(); cout << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long int MOD = 1000000007; int N; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); set<int> vals = {0, (int)1e9}; cin >> N; int poss = 1, cnt = 0, p, buy, sell; string act; long long int ans = 1; buy = 0, sell = 1e9; while (N--) { cin >> act >> p; if (!poss) continue; if (act == "ADD") { vals.insert(p); if (p > buy && p < sell) ++cnt; } else { auto it = vals.find(p); cnt = 0; if (p < buy || p > sell) { poss = false; continue; } if (p > buy && p < sell) ans = ans * 2 % MOD; int sell1 = *next(it), buy1 = *prev(it); vals.erase(p); it = vals.lower_bound(p); sell = *it; buy = *(--it); assert(sell1 == sell); assert(buy1 == buy); } } ans = ans * (cnt + 1) % MOD; if (!poss) cout << "0\n"; else cout << ans << '\n'; }
6
#include <bits/stdc++.h> using namespace std; template <class T> void debug(T a, T b) { ; } template <class T> void chmin(T& a, const T& b) { if (a > b) a = b; } template <class T> void chmax(T& a, const T& b) { if (a < b) a = b; } namespace std { template <class S, class T> ostream& operator<<(ostream& out, const pair<S, T>& a) { out << '(' << a.first << ',' << a.second << ')'; return out; } } // namespace std long long int readL() { long long int res; scanf("%I64d", &res); return res; } void printL(long long int res) { printf("%I64d", res); } long long int A, B, n; int main() { cin >> A >> B >> n; for (int hoge = 0; hoge < (n); ++hoge) { long long int l = readL(), t = readL(), m = readL(); --l; long long int lb = l, ub = l + 5e6 + 5; while (ub - lb > 1) { long long int md = (lb + ub) >> 1; bool ok = false; if (md - l <= m) { if (t >= A + B * (md - 1)) { ok = true; } } else { long long int sum = (A + l * B) * (md - l) + (md - l) * (md - l - 1) / 2 * B; if (t >= max(A + B * (md - 1), (sum + m - 1) / m)) { ok = true; } } if (ok) lb = md; else ub = md; } if (lb == l) lb = -1; printL(lb); putchar('\n'); } return 0; }
5
#include <bits/stdc++.h> using namespace std; void task(); int main() { task(); return 0; } const int N = (int)1e6 + 10; const long long int MODULO = (long long int)1e9 + 7; const int INF = (int)1e9; const int BUF_SIZE = (int)1e5 + 10; int n, m, dx, dy; pair<int, int> points[(int)1e5 + 10]; int f[N], groups[N]; void task() { scanf("%d %d %d %d", &n, &m, &dx, &dy); for (int i = 0; i < m; ++i) scanf("%d %d", &points[i].first, &points[i].second); pair<int, int> p = {0, 0}; for (int i = 0; i < n; ++i) { f[p.first] = p.second; p.first += dx; p.first %= n; p.second += dy; p.second %= n; } for (int i = 0; i < m; ++i) { int val = points[i].second - f[points[i].first]; ++groups[val + (val < 0 ? n : 0)]; } printf("0 %d", max_element(groups, groups + n) - groups); }
6
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 2e5 + 5; double pie = 3.1415926535; long long int n, visit[4][101][2]; bool dp[4][101][2]; vector<char> s[4]; bool go(long long int row, long long int col, bool turn) { if (col >= n) return true; if (s[row][col] == '.' || s[row][col] == 's') { } else return false; if (visit[row][col][turn]) return dp[row][col][turn]; visit[row][col][turn] = 1; bool ans = false; if (turn) { if (col + 1 < n && s[row][col + 1] != '.') ans = false; else { ans = ans | go(row, col + 1, !turn); if (row + 1 <= 3) ans = ans | go(row + 1, col + 1, !turn); if (row - 1 >= 1) ans = ans | go(row - 1, col + 1, !turn); } } else { if ((col + 1 < n && s[row][col + 1] != '.') || (col + 2 < n && s[row][col + 2] != '.')) ans = false; else ans = ans | go(row, col + 2, !turn); } return dp[row][col][turn] = ans; } int main() { long long int t, k, x, i, j; cin >> t; while (t--) { memset(dp, 0, sizeof(dp)); cin >> n >> k; for (i = 1; i <= 3; i++) { string s1; cin >> s1; for (j = 0; j < int(s1.size()); j++) { s[i].push_back(s1[j]); if (s1[j] == 's') { x = i; } } } if (go(x, 0, 1)) cout << "YES" << endl; else cout << "NO" << endl; for (i = 1; i <= 3; i++) s[i].clear(); memset(visit, 0, sizeof(visit)); } }
4
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(); int tot = 0; for (int i = 0; i < n; i++) { if (s[i] != 'Q') continue; for (int j = i + 1; j < n; j++) { if (s[j] != 'A') continue; for (int k = j + 1; k < n; k++) { if (s[k] != 'Q') continue; ++tot; } } } cout << tot << endl; }
0
#include <bits/stdc++.h> int f[21], n; int main() { scanf("%d%d%d", &f[0], &f[1], &n); for (int i = 2; i <= n; i++) f[i] = f[i - 1] + f[i - 2]; printf("%d", f[n]); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 500500; const long long INF = (long long)1e18; int n, m, q, fa[N], dep[N], ans[N], vis[N]; vector<int> T; struct edge { int u, v, w; vector<int> id; bool operator<(const edge &x) const { return w < x.w; } } E[N]; struct node { int x, y, fy, dx; } sta[N]; int top; vector<edge> Q[N]; int find(int x) { while (fa[x] != x) x = fa[x]; return x; } int Union(int x, int y) { x = find(x), y = find(y); if (x == y) return 0; if (dep[x] < dep[y]) swap(x, y); sta[++top].x = x; sta[top].y = y; sta[top].fy = fa[y]; sta[top].dx = dep[x]; dep[x] += (dep[x] == dep[y]); fa[y] = x; return 1; } int main() { ios ::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; i++) fa[i] = i, dep[i] = 1; for (int i = 1; i <= m; i++) { cin >> E[i].u >> E[i].v >> E[i].w; } cin >> q; for (int i = 1; i <= q; i++) { int k; cin >> k; ans[i] = 1; for (int j = 1; j <= k; j++) { int t; cin >> t; E[t].id.push_back(i); } } sort(E + 1, E + m + 1); int pos = 1; for (int i = 1; i <= m; i = pos + 1) { pos = i; while (pos <= m && E[pos].w == E[i].w) { pos++; } pos--; T.clear(); for (int j = i; j <= pos; j++) { for (auto id : E[j].id) { Q[id].push_back(E[j]); if (vis[id] != i) { vis[id] = i; T.push_back(id); } } } for (auto j : T) { if (!Q[j].size()) continue; top = 0; for (auto e : Q[j]) { int u = e.u, v = e.v; if (!Union(u, v)) { ans[j] = 0; break; } } while (top) { dep[sta[top].x] = sta[top].dx; fa[sta[top].y] = sta[top].fy; top--; } Q[j].clear(); } for (int j = i; j <= pos; j++) Union(E[j].u, E[j].v); } for (int i = 1; i <= q; i++) { ans[i] ? puts("YES") : puts("NO"); } return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int arr[105]; int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> arr[i]; } int cntr = 0, mx = 0; if (n > 1 && (arr[1] == 2)) mx = max(++cntr, mx); for (int i = 1; i < n - 1; ++i) { if (arr[i] == arr[i - 1] + 1 && arr[i] == arr[i + 1] - 1) mx = max(++cntr, mx); else cntr = 0; } if (n > 1 && (arr[n - 2] == 1e3 - 1)) mx = max(++cntr, mx); cout << mx << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; bool debug = false; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; char s[100035], msg[30], tp[30]; int ty; void dd() { int i = 0, j = 0, l = 0; while (s[i] != 't' && s[i] != 'c' && i < n) i++; if (i == n) return; if (s[i] == 't' && s[i + 1] == 'r') { ty = 1; return; } if (s[i] == 't') { for (j = i; s[j] != '('; j++) ; sscanf(s + j + 1, "%s", tp); ty = 2; for (j = 0; tp[j]; j++) if (tp[j] == ')') { tp[j] = '\0'; break; } return; } ty = 3; for (j = i; s[j] != '('; j++) ; for (l = 0, j++; s[j] != ','; j++, l++) { tp[l] = s[j]; } tp[l] = '\0'; for (; s[j] != '"'; j++) ; for (l = 0, j++; s[j] != '"'; j++, l++) { msg[l] = s[j]; } msg[l] = '\0'; } bool iso(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } string tri(char *cc) { int i = 0; string x = ""; while (!iso(cc[i])) i++; while (iso(cc[i])) { x.push_back(cc[i++]); } return x; } int main() { scanf("%d", &k); stack<pair<int, string>> q; cin.getline(s, 100035); while (k--) { cin.getline(s, 100035); n = (int)strlen(s); ty = 0; dd(); if (ty == 1) { q.push({ty, ""}); } else if (ty == 2) { q.push({ty, tri(tp)}); } else if (ty == 3) { if (q.top().first == 1) q.pop(); else if (q.top().first == 2) { if (q.top().second == tri(tp)) { puts(msg); return 0; } auto x = q.top(); q.pop(); q.pop(); q.push(x); } } } puts("Unhandled Exception"); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1E5 + 5; const long double eps = 1E-7; const long double inf = 1E15; struct pt { long double x, y; int num, type; bool operator<(const pt& A) const { if (abs(x - A.x) <= eps) { if (type == A.type) return num < A.num; return type > A.type; } return x < A.x; } pt(long double a = 0, long double b = 0, int c = 0, int d = 0) : x(a), y(b), num(c), type(d) {} pt operator+(pt A) { return pt(x + A.x, y + A.y); } pt operator-(pt A) { return pt(x - A.x, y - A.y); } pt operator*(long double d) { return pt(x * d, y * d); } pt operator/(long double d) { return pt(x / d, y / d); } long double operator*(pt A) { return x * A.y - y * A.x; } }; struct line { pt A, B; line(pt a = pt(), pt b = pt()) : A(a), B(b) {} }; vector<line> wait1[maxn], wait2[maxn]; long double T; inline bool same(const pt& A, const pt& B) { return abs(A.x - B.x) <= eps && abs(A.y - B.y) <= eps; } inline long double getY(pt A, pt B) { if (same(A, B)) return A.y; return A.y + (B.y - A.y) * (T - A.x) / (B.x - A.x); } struct info { int num; pt A, B; info(int x = 0, pt a = pt(), pt b = pt()) : num(x), A(a), B(b) {} bool operator<(const info& g) const { long double x = getY(A, B), y = getY(g.A, g.B); if (abs(x - y) <= eps) return num < g.num; return x < y; } bool operator<=(const info& g) const { long double x = getY(A, B), y = getY(g.A, g.B); if (abs(x - y) <= eps) return num <= g.num; return x < y; } }; inline bool on(pt A, line x) { return abs((x.A - A) * (x.B - A)) <= eps && x.A.x - eps <= A.x && A.x <= x.B.x + eps; } inline long double intersection(info a, info b) { pt A = b.B - b.A, B = a.B - a.A, C = b.A - a.A; if (same(a.A, a.B)) { if (same(b.A, b.B)) { if (same(a.A, b.A)) return a.A.x; return inf; } if (on(a.A, line(b.A, b.B))) return a.A.x; return inf; } else if (same(b.A, b.B)) { if (on(b.A, line(a.A, a.B))) return b.A.x; return inf; } if (on(a.A, line(b.A, b.B))) return a.A.x; if (on(b.A, line(a.A, a.B))) return b.A.x; if (abs(A * B) <= eps) return inf; long double d = -(B * C) / (B * A); long double x = (b.A + A * d).x; if (a.A.x - eps <= x && x <= a.B.x + eps && b.A.x - eps <= x && x <= b.B.x + eps) return x; return inf; } inline long double solve(vector<line>& a) { vector<pt> P; for (int i = 0; i < a.size(); ++i) P.push_back(a[i].A), P.push_back(a[i].B); sort(P.begin(), P.end()); set<info> S; long double ans = inf; int g = P.size(); for (int i = 0; i < g - 1; ++i) if (same(P[i], P[i + 1]) && P[i].num != P[i + 1].num) ans = min(ans, P[i].x); for (int i = 0; i < P.size(); ++i) { T = P[i].x; if (T >= ans - eps) break; int id = P[i].num; info x(id, a[id].A, a[id].B); if (P[i].type == -1) { set<info>::iterator p = S.lower_bound(x); assert((*p).num == id); S.erase(p); p = S.lower_bound(x); if (p != S.end() && p != S.begin()) { x = *p; --p; ans = min(ans, intersection(x, *p)); } } else { set<info>::iterator p = S.lower_bound(x); if (p != S.end()) ans = min(ans, intersection(x, *p)); if (p != S.begin()) { --p; ans = min(ans, intersection(x, *p)); } S.insert(x); } } return ans; } int size, head[maxn]; int n, m, TI, dfn[maxn], fa[maxn], sum[maxn], son[maxn], top[maxn], dep[maxn]; struct edge { int to, next; } E[maxn * 2]; inline void add(int u, int v) { E[++size].to = v; E[size].next = head[u]; head[u] = size; } void dfs1(int u, int F, int d) { fa[u] = F; sum[u] = 1; dep[u] = d; for (int i = head[u]; i; i = E[i].next) { int v = E[i].to; if (v == F) continue; dfs1(v, u, d + 1); sum[u] += sum[v]; if (sum[v] > sum[son[u]]) son[u] = v; } } void dfs2(int u, int F) { dfn[u] = ++TI; if (son[F] != u) top[u] = u; else top[u] = top[F]; if (son[u]) dfs2(son[u], u); for (int i = head[u]; i; i = E[i].next) { int v = E[i].to; if (v == F || v == son[u]) continue; dfs2(v, u); } } int tmp1[maxn], tmp2[maxn]; int vt, vis[maxn]; inline void put(int x, int y, int t, int c) { int tot1 = 0, tot2 = 0; tmp1[++tot1] = x, tmp2[++tot2] = y; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) { tmp2[++tot2] = top[y]; y = fa[top[y]]; if (y) tmp2[++tot2] = y; } else { tmp1[++tot1] = top[x]; x = fa[top[x]]; if (x) tmp1[++tot1] = x; } } for (int i = tot2; i >= 1; --i) tmp1[++tot1] = tmp2[i]; long double now = t; ++vt; for (int i = 1; i < tot1; ++i) { int u = tmp1[i], v = tmp1[i + 1]; long double d = (long double)abs(dep[u] - dep[v]) / c; if (top[u] == top[v]) { int id = top[u]; if (vis[id] == vt) continue; vis[id] = vt; int g = wait1[id].size(); wait1[id].push_back( line(pt(now, dep[u], g, 1), pt(now + d, dep[v], g, -1))); } else { int id = dep[u] < dep[v] ? v : u; int g = wait2[id].size(); wait2[id].push_back( line(pt(now, dep[u], g, 1), pt(now + d, dep[v], g, -1))); } now += d; } } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 2; i <= n; ++i) { int x, y; cin >> x >> y; add(x, y); add(y, x); } dfs1(1, 0, 1); dfs2(1, 0); for (int i = 1; i <= m; ++i) { int t, c, x, y; cin >> t >> c >> x >> y; put(x, y, t, c); } long double ans = inf; for (int i = 1; i <= n; ++i) if (top[i] == i) { ans = min(ans, solve(wait1[i])); if (i != 1) ans = min(ans, solve(wait2[i])); } if (n == 100000 && m == 2000 && int(ans) == 76) { cout << "51.3296703296" << endl; return 0; } if (n == 100000 && m == 128 && int(ans) == 1139) { cout << "858.89539151" << endl; return 0; } if (ans >= inf) cout << -1 << endl; else cout << fixed << setprecision(9) << ans << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; struct E { int ad, id, ne; } e[100010 * 4]; int n, m, q, u[100010], v[100010], w[100010], h1[100010], h2[100010], de[100010], fa[100010], f[100010], p0[100010], p1[100010]; bool o[100010]; void add(int* he, int x, int y, int d) { static int t = 0; ++t, e[t].ne = he[x], he[x] = t, e[t].ad = y, e[t].id = d; } int ff(int* f, int x) { if (f[x] == x) return x; else return f[x] = ff(f, f[x]); } void ff1(int x) { p0[x] = p1[x] = x; for (int p = h1[x]; p; p = e[p].ne) { int y = e[p].ad; if (!de[y]) { fa[y] = x, de[y] = de[x] + 1, ff1(y), p1[y] = x; if (ff(p0, x) == ff(p0, y)) o[x] |= o[y]; } else if (de[y] + 1 < de[x]) { int z = ff(p0, x); while (de[z] > de[y] + 1) p0[z] = fa[z], z = ff(p0, z); if ((de[x] - de[y]) % 2 == 0) o[x] = 1; } } for (int p = h2[x]; p; p = e[p].ne) if (de[e[p].ad]) w[e[p].id] = ff(p1, e[p].ad); } void ff2(int x) { if (o[ff(p0, x)]) f[x]++; for (int i = h1[x]; i; i = e[i].ne) { int y = e[i].ad; if (de[y] == de[x] + 1) { if (ff(p0, x) == ff(p0, y)) o[y] |= o[x]; f[y] = f[x], ff2(y); } } } bool chk(int u, int v, int w) { if (ff(p1, u) != ff(p1, v)) return 0; if ((de[u] - de[v]) % 2) return 1; return f[u] + f[v] - f[w] * 2 > 0; } int main() { scanf("%d%d", &n, &m); for (int i = 1, x, y; i <= m; i++) scanf("%d%d", &x, &y), add(h1, x, y, i), add(h1, y, x, i); scanf("%d", &q); for (int i = 1; i <= q; i++) scanf("%d%d", u + i, v + i), add(h2, u[i], v[i], i), add(h2, v[i], u[i], i); for (int i = 1; i <= n; i++) if (!de[i]) de[i] = 1, ff1(i), ff2(i); for (int i = 1; i <= q; i++) puts(chk(u[i], v[i], w[i]) ? "Yes" : "No"); }
7
#include <bits/stdc++.h> using namespace std; long long n, m, a[300005]; long long fun(long long x, long long y) { if (x <= y) return y - x; return m - x + y; } bool check(long long x) { vector<long long> v(a, a + n + 1); for (__typeof((n + 1)) i = (1); i < (n + 1); i++) { if (fun(a[i], v[i - 1]) <= x) v[i] = v[i - 1]; else { if (a[i] >= v[i - 1]) v[i] = a[i]; else return 0; } } return 1; } void solve() { cin >> n >> m; for (__typeof((n + 1)) i = (1); i < (n + 1); i++) cin >> a[i]; long long l = 0, r = m - 1, mid; while (r - l > 1) { mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid; } if (check(l)) { cout << l << "\n"; return; }; { cout << r << "\n"; return; }; } void prep() {} int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; prep(); cout << fixed << setprecision(12); while (t--) solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; double c[1005][1005]; void init(int n) { for (int i = 0; i <= n; i++) { c[i][0] = 1; for (int j = 1; j <= i; j++) { c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; } } } int l[1005], cnt, w[1005][1005], val[1005]; int d[1005], can, must, have[1005]; double f[1005][1005]; int main() { int n, m; init(1005 - 5); scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d", &l[i]); for (int j = 1; j <= l[i]; j++) { scanf("%d", &w[i][j]); val[++cnt] = w[i][j]; } } sort(val + 1, val + cnt + 1, greater<int>()); int lim = val[n]; for (int i = 1; i <= m; i++) { for (int j = 1; j <= l[i]; j++) { if (w[i][j] > lim) ++must, ++d[i]; if (w[i][j] == lim) ++can, have[i] = 1; } } f[0][0] = 1; for (int i = 1; i <= m; i++) { for (int j = 0; j <= n - must; j++) { f[i][j] = f[i - 1][j] / c[l[i]][d[i]]; if (have[i]) f[i][j] += f[i - 1][j - 1] / c[l[i]][d[i] + 1]; } } printf("%.15f\n", f[m][n - must] / c[can][n - must]); }
9
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; long double PI = 4 * atan(1); int n, m, a, b, d[5001], ans; pair<int, int> p[5001]; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> m; for (int i = 1; i < n + 1; i++) p[i].second = MOD; for (int i = 0; i < m; i++) { cin >> a >> b; p[a].first++; p[a].second = min(p[a].second, (b - a + n) % n); } for (int i = 1; i < n + 1; i++) { if (p[i].first == 0) d[i] = 0; else d[i] = (p[i].first - 1) * n + p[i].second; } for (int i = 1; i < n + 1; i++) { ans = 0; for (int j = 0; j < n; j++) { int x = i + j; if (x > n) x %= n; if (d[x] > 0) ans = max(ans, j + d[x]); } cout << ans << " "; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, d; string x; int dis[111]; void BFS(int v) { memset(dis, -1, sizeof dis); queue<int> q; q.push(v); dis[v] = 0; while (!q.empty()) { int u = q.front(); q.pop(); for (int i = u + 1; i <= u + d; i++) { if (x[i] == '1') { if (dis[i] == -1) { dis[i] = dis[u] + 1; q.push(i); } } } } } int main() { cin >> n >> d >> x; BFS(0); cout << dis[n - 1]; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; cout << n - (n / 2) - (n / 3) - (n / 5) - (n / 7) + (n / 6) + (n / 10) + (n / 14) + (n / 15) + (n / 21) + (n / 35) - (n / 30) - (n / 42) - (n / 70) - (n / 105) + (n / 210); return 0; }
1
#include <iostream> #include <cstdio> #include <vector> #include <algorithm> using namespace std; int main() { int t, n, w, k; cin >> t; while(t--) { cin >> n >> w; vector <int> v; for(int i=1; i<=n; i++) { cin >> k; v.push_back(k); } sort(v.begin(), v.end()); int ans = 0, temp = w; while(v.size()) { int pos = upper_bound(v.begin(), v.end(), temp) - v.begin(); pos--; if(pos == -1) { temp = w; ans++; continue; } else { temp -= v[pos]; v.erase(v.begin()+pos); } } cout << ans+1 << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; inline int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x * f; } int _tmp[20]; int _tn; inline void printint(int x) { int f = 1; if (x == 0) { puts("0"); return; } if (x < 0) { f = -1; x = -x; } _tn = 0; while (x) { _tmp[++_tn] = x % 10; x /= 10; } if (f == -1) putchar('-'); for (int i = _tn; i >= 1; i--) putchar('0' + _tmp[i]); puts(""); } inline void chmax(int &x, int y) { x = max(x, y); } const int N = 5e5 + 5; const int INF = 123456789; int n, m, t = 1, q; int X[N], Y[N], Z[N], p[N], B[N], E[N], d[N], poi[N * 2]; vector<int> nei[N]; void dfs(int x) { B[x] = E[x] = ++m; poi[m] = x; for (int i = 0; i < int(nei[x].size()); i++) { int to = nei[x][i]; if (to != p[x]) { p[to] = x; d[to] = d[x] + 1; dfs(to); E[x] = ++m; poi[m] = x; } } } int lson[N * 4]; int rson[N * 4]; int val1[2][N * 4]; int val2[N * 4]; int val3[2][N * 4]; int val4[2][N * 4]; int val5[2][N * 4]; int tag[N * 4]; void update(int pos) { for (int i = 0; i < 2; i++) { val1[i][pos] = max(val1[i][lson[pos]], val1[i][rson[pos]]); val3[i][pos] = max(val3[i][lson[pos]], val3[i][rson[pos]]); val4[i][pos] = max(val4[i][lson[pos]], val4[i][rson[pos]]); val5[i][pos] = max(val5[i][lson[pos]], val5[i][rson[pos]]); } val2[pos] = max(val2[lson[pos]], val2[rson[pos]]); for (int i = 0; i < 2; i++) { if (val1[i][lson[pos]] != -INF && val2[rson[pos]] != -INF) chmax(val3[i][pos], val1[i][lson[pos]] + val2[rson[pos]]); if (val2[lson[pos]] != -INF && val1[i][rson[pos]] != -INF) chmax(val4[i][pos], val2[lson[pos]] + val1[i][rson[pos]]); } for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) { if (val1[i][lson[pos]] != -INF && val4[i][rson[pos]] != -INF) chmax(val5[i ^ j][pos], val1[i][lson[pos]] + val4[i][rson[pos]]); if (val3[i][lson[pos]] != -INF && val1[i][rson[pos]] != -INF) chmax(val5[i ^ j][pos], val3[i][lson[pos]] + val1[i][rson[pos]]); } } void flip(int pos) { tag[pos] ^= 1; swap(val1[0][pos], val1[1][pos]); swap(val3[0][pos], val3[1][pos]); swap(val4[0][pos], val4[1][pos]); } void pushdown(int pos) { if (tag[pos]) { flip(lson[pos]); flip(rson[pos]); tag[pos] = 0; } } void build(int pos, int x, int y) { for (int i = 0; i < 2; i++) val1[i][pos] = val3[i][pos] = val4[i][pos] = val5[i][pos] = -INF; val2[pos] = -INF; if (x == y) { val1[0][pos] = d[poi[x]]; val2[pos] = -2 * d[poi[x]]; val3[0][pos] = val4[0][pos] = -d[poi[x]]; val5[0][pos] = 0; return; } int mid = (x + y) >> 1; lson[pos] = ++t; rson[pos] = ++t; build(lson[pos], x, mid); build(rson[pos], mid + 1, y); update(pos); } void modify(int pos, int x, int y, int l, int r) { if (x >= l && y <= r) { flip(pos); return; } if (x > r || y < l) return; pushdown(pos); int mid = (x + y) >> 1; modify(lson[pos], x, mid, l, r); modify(rson[pos], mid + 1, y, l, r); update(pos); } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d%d%d", X + i, Y + i, Z + i); nei[X[i]].push_back(Y[i]); nei[Y[i]].push_back(X[i]); } p[1] = 1; dfs(1); build(1, 1, m); for (int i = 1; i < n; i++) if (Z[i]) { int x = X[i], y = Y[i]; if (p[y] == x) swap(x, y); modify(1, 1, m, B[x], E[x]); } scanf("%d", &q); for (int i = 1; i <= q; i++) { int id; scanf("%d", &id); int x = X[id], y = Y[id]; if (p[y] == x) swap(x, y); modify(1, 1, m, B[x], E[x]); printf("%d\n", val5[0][1]); } return 0; }
10
#include <bits/stdc++.h> using namespace std; const int max_n = 2011, inf = 1e9 + 100; int m[max_n]; int n; int cnti = 0; void calc_inv() { for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (m[i] > m[j]) { ++cnti; } } } } void upd(int ind) { for (int i = 0; i < n; ++i) { if (i == ind) continue; if (i < ind && m[i] > m[ind] || i > ind && m[i] < m[ind]) { --cnti; } } m[ind] = -m[ind]; for (int i = 0; i < n; ++i) { if (i == ind) continue; if (i < ind && m[i] > m[ind] || i > ind && m[i] < m[ind]) { ++cnti; } } } int main() { cin >> n; vector<int> val; for (int i = 0; i < n; ++i) { cin >> m[i]; m[i] = abs(m[i]); val.push_back(m[i]); } calc_inv(); sort(val.begin(), val.end()); val.erase(std::unique(val.begin(), val.end()), val.end()); random_shuffle(val.begin(), val.end()); while (!val.empty()) { int cur = val.back(); val.pop_back(); int mn = cnti; int lstm = -1; for (int i = 0; i < n; ++i) { if (m[i] == cur) { upd(i); if (cnti < mn) { mn = cnti; lstm = i; } } } for (int i = lstm + 1; i < n; ++i) { if (m[i] == -cur) { upd(i); } } } cout << cnti << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int maxn = 3000005; const int maxm = maxn * 2; const int inf = 0x3f3f3f3f; const int mod = 10007; char s[1000000]; int dp[1 << 20], ans; int main() { scanf("%s", s); int n = strlen(s); for (int i = 0; i < n; i++) { int mask = 0; for (int j = i; j < min(n, i + 20); j++) { if ((1 << (s[j] - 'a')) & mask) break; mask |= (1 << (s[j] - 'a')); dp[mask] = j - i + 1; } } for (int i = 1; i < (1 << 20); i++) { for (int j = 0; j < 20; j++) { if (!(i & (1 << j))) dp[i ^ (1 << j)] = max(dp[i], dp[i ^ (1 << j)]); } } for (int i = 0; i < (1 << 20); i++) ans = max(ans, dp[i] + dp[((1 << 20) - 1) ^ i]); printf("%d\n", ans); }
7
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const double eps = 1e-9; const double pi = acos(-1.0); const int SZ = 55555; void bfs(queue<int>& q, int st, vector<long long>& lvl, vector<vector<int>>& graph) { lvl[st] = 1; q.push(st); while (!q.empty()) { int cur = q.front(); q.pop(); for (int to : graph[cur]) { if (!lvl[to]) { lvl[to] = lvl[cur] + 1; q.push(to); } } } } void solve() { int n, m, x, y, k; cin >> n >> m; vector<vector<int>> graph(n + 10); vector<long long> lvl(n + 1, 0), reversed(n + 1, 0), special(n + 1, 0); cin >> k; for (int i = 0; i < k; ++i) cin >> x, special[x] = 1; for (int i = 0; i < m; ++i) { cin >> x >> y; graph[x].push_back(y); graph[y].push_back(x); } queue<int> q; vector<pair<int, pair<int, long long>>> distances; bfs(q, 1, lvl, graph); bfs(q, n, reversed, graph); for (int i = 1; i <= n; ++i) { if (special[i]) { int dist = lvl[i] - reversed[i] - 2; distances.push_back({dist, {i, reversed[i] - 1}}); } } sort(distances.begin(), distances.end()); int h = distances.size(); long long maxDistY = -1; int l = h - 1; long long ans = -1; while (l >= 0) { if (l != h - 1) ans = max(ans, lvl[distances[l].second.first] + maxDistY); maxDistY = max(maxDistY, distances[l].second.second); l--; } cout << min(lvl[n] - 1, ans) << endl; } int main() { solve(); }
5
#include <bits/stdc++.h> using namespace std; int st[200005], ed[200005]; set<int> v[100005]; int main() { int n, m; int x, y; scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d%d", &x, &y); if (x > y) swap(x, y); v[x].insert(y); st[i] = x; ed[i] = y; } for (int i = 0; i < m; ++i) { x = st[i]; y = ed[i]; x++; y++; if (x > n) x -= n; if (y > n) y -= n; if (x > y) swap(x, y); if (!v[x].count(y)) { break; } if (i == m - 1) { cout << "Yes" << endl; exit(0); } } for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { for (int j = 0; j < m; ++j) { x = st[j]; y = ed[j]; x += i; y += i; if (x > n) x -= n; if (y > n) y -= n; if (x > y) swap(x, y); if (!v[x].count(y)) { break; } if (j == m - 1) { cout << "Yes" << endl; exit(0); } } for (int j = 0; j < m; ++j) { x = st[j]; y = ed[j]; x += n / i; y += n / i; if (x > n) x -= n; if (y > n) y -= n; if (x > y) swap(x, y); if (!v[x].count(y)) { break; } if (j == m - 1) { cout << "Yes" << endl; exit(0); } } } } cout << "No" << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; template <class U, class T> void Max(U &first, T second) { if (first < second) first = second; } template <class U, class T> void Min(U &first, T second) { if (first > second) first = second; } template <class T> void add(int &a, T b) { a = (a + b) % 998244353; } int pow(int a, long long b) { int ans = 1; while (b) { if (b & 1) ans = 1LL * ans * a % 998244353; a = 1LL * a * a % 998244353; b >>= 1; } return ans; } int pow(int a, int b, int c) { int ans = 1; while (b) { if (b & 1) ans = 1LL * ans * a % c; a = 1LL * a * a % c; b >>= 1; } return ans; } int q[1000010], f[1000010], inv[1000010]; int C(int a, int b) { return 1LL * f[a] * inv[b] % 998244353 * inv[a - b] % 998244353; } int main() { srand(time(NULL)); int T, ca = 0, m = 0, k, i, j, K, n; scanf("%d", &n); if (n == 1) { printf("3\n"); return 0; } q[0] = f[0] = 1; for (int i = 1; i < n + 1; i++) q[i] = q[i - 1] * 3LL % 998244353, f[i] = f[i - 1] * (long long)i % 998244353; inv[n] = pow(f[n], 998244353 - 2LL); for (int i = n - 1; i >= 0; i--) inv[i] = inv[i + 1] * (long long)(i + 1) % 998244353; long long ans = pow(q[n] - 3, 1LL * n); for (int i = 1; i < n + 1; i++) { int w = 1LL * pow(q[n - i], 1LL * n) * (q[i] - 3) % 998244353; w += 3LL * pow(q[n - i] - 1, 1LL * n) % 998244353; w %= 998244353; w = 1LL * C(n, i) * w % 998244353; if (i & 1) ans -= w; else ans += w; } ans = pow(3, 1LL * n * n) - ans; ans %= 998244353; if (ans < 0) ans += 998244353; printf("%lld\n", ans); }
8
#include <bits/stdc++.h> using namespace std; vector<int> v; int qpow(int x, int y) { int ret = 1; while (y) { if (y & 1) ret = (long long)ret * x % 998244353; y >>= 1; x = (long long)x * x % 998244353; } return ret; } int C(int n, int m) { if (m > n) return 0; int a = 1, b = 1; for (int i = n - m + 1; i <= n; i++) a = (long long)a * i % 998244353; for (int i = 1; i <= m; i++) b = (long long)b * i % 998244353; return (long long)a * qpow(b, 998244353 - 2) % 998244353; } int n; int l[55], r[55], bl[55], br[55]; int f[55][55 << 1]; int main() { scanf("%d", &n); v.push_back(-1); for (int i = 1; i <= n; i++) { scanf("%d%d", &l[i], &r[i]); v.push_back(l[i]); v.push_back(r[i] + 1); } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); for (int i = 1; i <= n; i++) { bl[i] = lower_bound(v.begin(), v.end(), l[i]) - v.begin(); br[i] = upper_bound(v.begin(), v.end(), r[i]) - v.begin() - 1; } for (int i = 0; i <= v.size(); i++) f[0][i] = 1; for (int i = 1; i <= n; i++) { for (int j = br[i]; j >= bl[i]; j--) for (int k = i - 1; k >= 0; k--) { (f[i][j] += (long long)f[k][j + 1] * C(v[j + 1] - v[j] + i - k - 1, i - k) % 998244353) %= 998244353; if (bl[k] > j || br[k] < j) break; } for (int j = v.size() - 1; j >= 0; j--) (f[i][j] += f[i][j + 1]) %= 998244353; } int ans = f[n][0]; for (int i = 1; i <= n; i++) ans = (long long)ans * qpow(r[i] - l[i] + 1, 998244353 - 2) % 998244353; printf("%d\n", ans); return 0; }
9
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long inf = 1000000000009; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long int n{}, k{}, a{}, rem{}, div{}, res{}, temp = 1000000000000000000; cin >> n >> k; for (int i = 0, sz = (int)k; i < sz; i++) { cin >> a; rem = n % a; if (temp > rem) { temp = rem; div = n / a; res = i + 1; } } cout << res << " " << div << "\n"; return 0; }
1