solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; template <class T> struct segtree { int _segR; vector<T> _segtree; function<T(T, T)> _combine; T _defaultValue; public: segtree(vector<T>& initArr, function<T(T, T)> combine, T defaultValue) { assert(initArr.size() > 1 && initArr[0] == 0); _segR = initArr.size() - 1; _segtree = vector<T>(initArr.size() * 4); _combine = combine; _defaultValue = defaultValue; init(1, 1, _segR, initArr); } T query(int start, int end, int node = 1, int l = 0, int r = 0) { if (node == 1) { l = 1; r = _segR; } if (start <= l && r <= end) return _segtree[node]; if (r < start || end < l) return _defaultValue; int m = (l + r) / 2; T lVal = query(start, end, 2 * node, l, m); T rVal = query(start, end, 2 * node + 1, m + 1, r); return _combine(lVal, rVal); } void update(int index, T delta, int node = 1, int l = 0, int r = 0) { if (node == 1) { l = 1; r = _segR; } if (l <= index && index <= r) { _segtree[node] = _combine(_segtree[node], delta); } if (index < l || r < index || l == r) return; int m = (l + r) / 2; update(index, delta, 2 * node, l, m); update(index, delta, 2 * node + 1, m + 1, r); } void fixedUpdate(int index, T newValue, int node = 1, int l = 0, int r = 0) { if (node == 1) { l = 1; r = _segR; } if (index < l || r < index) return; if (l == r) { _segtree[node] = newValue; return; } int m = (l + r) / 2; fixedUpdate(index, newValue, 2 * node, l, m); fixedUpdate(index, newValue, 2 * node + 1, m + 1, r); if (l <= index && index <= r) { T lVal = query(l, r, 2 * node, l, m); T rVal = query(l, r, 2 * node + 1, m + 1, r); _segtree[node] = _combine(lVal, rVal); } } private: T init(int node, int l, int r, vector<T>& initArr) { if (l == r) { return _segtree[node] = initArr[l]; } int m = (l + r) / 2; T lVal = init(2 * node, l, m, initArr); T rVal = init(2 * node + 1, m + 1, r, initArr); return _segtree[node] = _combine(lVal, rVal); } }; long long PosMod(long long a) { return a >= 0 ? a : a + 1000000007; } long long AddMod(long long a, long long b) { return PosMod((a + b) % 1000000007); } long long MultMod(long long a, long long b) { return PosMod((a * b) % 1000000007); } long long value[200100]; int sortedIdx_y[200100]; int sortedIdx_x[200100]; std::pair<int, int> range[200100]; int main() { cin.tie(0); ios::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; range[i] = make_pair(a, b); sortedIdx_y[i] = i; sortedIdx_x[i] = i; } function<bool(int, int)> cmpY = [](int a, int b) { return range[a].second < range[b].second; }; sort(sortedIdx_y, sortedIdx_y + n, cmpY); function<bool(int, int)> cmpX = [](int a, int b) { return range[a].first < range[b].first; }; sort(sortedIdx_x, sortedIdx_x + n, cmpX); vector<long long> initArr(2 * n + 10); segtree<long long> seg( initArr, [](long long l, long long r) { return AddMod(l, r); }, 0); for (int i = 0; i < n; i++) { std::pair<int, int> r = range[sortedIdx_y[i]]; int x = r.first; long long val = seg.query(x + 1, 2 * n); value[sortedIdx_y[i]] = AddMod(val, 1); seg.update(x, AddMod(val, 1)); } set<int> lastPos; int t; cin >> t; vector<bool> isReq(n + 1); for (int i = 0; i < t; i++) { int s; cin >> s; isReq[s - 1] = true; lastPos.insert(range[s - 1].second); } long long ans = 0; int reqCnt = 0; for (int i = 0; i < n; i++) { int curT = sortedIdx_x[i]; if (isReq[curT]) { reqCnt++; ans = AddMod(ans, value[curT]); lastPos.erase(range[curT].second); } else if (range[curT].second < *lastPos.rbegin()) { ans = AddMod(ans, value[curT]); } if (reqCnt == t) break; } cout << ans; }
11
#include <bits/stdc++.h> using namespace std; void input(long long int n, long long int a[]) { for (long long int i = 0; i < n; ++i) cin >> a[i]; } long long int bin_expo(long long int A, long long int B, long long int M) { long long int res = 1LL; while (B > 0) { if (B % 2 == 1) { res = (res * A) % M; } A = (A * A) % M; B = B / 2; } return res; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int lcm(long long int a, long long int b) { return (a * b) / gcd(a, b); } double distance(long long int x1, long long int y1, long long int x2, long long int y2) { return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0); } long long int a[2005][2005], flag; int main() { ios_base::sync_with_stdio(0); int n; scanf("%d", &n); for (long long int i = 0; i < n; ++i) { for (int j = 0; j < n; j++) { scanf("%d", &a[i][j]); } if (a[i][i]) flag = 1; } for (long long int i = 0; i < n; ++i) { for (int j = i + 1; j < n; j++) { if (a[i][j] != a[j][i] || (a[i][j] == 0 && j != i)) { flag = 1; } } } for (long long int i = 0; i < n; ++i) { int r = 1; for (int j = 0; j < n; j++) { if (a[i][j] < a[i][r] && i != j) { r = j; } } for (int k = 0; k < n; k++) { if (abs(a[i][k] - a[r][k]) != a[r][i]) { flag = 1; } } } if (!flag) cout << "YES"; else cout << "NO"; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int INPUT_MAXCHARS = 1 << 16; char buffer[INPUT_MAXCHARS]; struct FastReader { char *p; FastReader() : p(buffer) { fread(buffer, 1, sizeof buffer, stdin); } int next_int() { return int(next_ll()); } long long next_ll() { upd(); long long sgn = 1; while (*p < '0') { if (*p++ == '-') { sgn = -1; break; } } long long val = 0; while (*p >= '0') { val = val * 10 + (*p - '0'); ++p; } return sgn * val; } private: void upd() { int remchars = INPUT_MAXCHARS - (p - buffer); if (remchars < 25) { memcpy(buffer, p, remchars); size_t cnt = fread(buffer + remchars, 1, sizeof buffer - remchars, stdin); if (remchars + cnt < sizeof buffer) { buffer[remchars + cnt] = 0; } p = buffer; } } }; const int MAXK = 80; long long N[MAXK + 1]; long long l_to_r[MAXK + 1]; long long inf = 100000000000000000LL; pair<long long, pair<long long, long long> > memo[MAXK + 1][5]; int memo_cnt[MAXK + 1]; pair<long long, long long> go_lr(long long a, int k) { for (int i = 0; i < memo_cnt[k]; ++i) { if (memo[k][i].first == a) { return memo[k][i].second; } } pair<long long, long long> ret; if (k == 0) { ret = make_pair(0ll, 0ll); } else if (k == 1) { ret = make_pair(a == 0 ? 0ll : 1ll, a == 0 ? 1ll : 0ll); } else { long long n = N[k - 1]; if (a >= n) { ret = go_lr(a - n, k - 2); ++ret.first; } else { ret = go_lr(a, k - 1); ret.first = min(ret.first, ret.second + 2); ret.second = min(ret.first, ret.second) + 1 + l_to_r[k - 2]; } } memo[k][memo_cnt[k]++] = make_pair(a, ret); return ret; } inline long long go_left(long long a, int k) { return go_lr(a, k).first; } inline long long go_right(long long a, int k) { return go_lr(a, k).second; } inline long long minlr(long long a, int k) { pair<long long, long long> tmp = go_lr(a, k); return min(tmp.first, tmp.second); } long long dist(long long a, long long b, int k) { if (a == b) { return 0; } assert(k >= 1); assert(a < b); if (k == 1) { assert(a == 0 && b == 1); return 1; } if (a == 0) { return go_left(b, k); } if (b == N[k] - 1) { return go_right(a, k); } long long ret = inf; long long n = N[k - 1]; if (n <= a) { ret = dist(a - n, b - n, k - 2); } else if (b < n) { ret = min(dist(a, b, k - 1), minlr(a, k - 1) + minlr(b, k - 1) + 2); } else { ret = 1 + go_left(b - n, k - 2) + minlr(a, k - 1); } return ret; } int main() { FastReader reader; int Q = reader.next_int(); int K = reader.next_int(); K = min(K, MAXK); N[0] = 1; N[1] = 2; l_to_r[0] = 0; l_to_r[1] = 1; for (int i = 2; i <= MAXK; ++i) { N[i] = min(inf, N[i - 1] + N[i - 2]); l_to_r[i] = l_to_r[i - 2] + 1; } while (Q--) { long long a = reader.next_ll() - 1; long long b = reader.next_ll() - 1; if (a > b) { swap(a, b); } memset(memo_cnt, 0, sizeof memo_cnt); printf("%I64d\n", dist(a, b, K)); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int n, k; string line[4]; bool rhymes = true; vector<string> scheme; string s = "aaaa"; bool isVowel(char c) { return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); } string getSubstr(string str) { int count = 0; string ans = "!"; int curr = str.size() - 1; while (count < k && curr >= 0) count += isVowel(str[curr--]) ? 1 : 0; if (curr < 0 && count != k) rhymes = false; return (ans = str.substr(curr + 1)); } void getScheme() { string temp[4]; for (int i = 0; i < 4; i++) { temp[i] = getSubstr(line[i]); } if (temp[0] == temp[1]) { if (temp[2] == temp[3]) { if (temp[1] == temp[2]) scheme.push_back("aaaa"); else scheme.push_back("aabb"); return; } else rhymes = false; } else { if (temp[1] == temp[3] && temp[0] == temp[2]) scheme.push_back("abab"); else if (temp[1] == temp[2] && temp[0] == temp[3]) scheme.push_back("abba"); else rhymes = false; } } int main() { cin >> n >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) cin >> line[j]; getScheme(); } for (int i = 0; i < scheme.size(); i++) { if (scheme[i] != "aaaa") { if (s == "aaaa") s = scheme[i]; else rhymes = (rhymes && (s == scheme[i])); } } rhymes ? cout << s << endl : cout << "NO\n"; }
4
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const long long MAX = 2e9 + 7; const long long mod = 1e9 + 7; const long long maxn = 1e6 + 7; int n, tim; long long s[maxn], num[maxn]; long long pow_(long long x, long long n) { long long res = 1; while (n) { if (n & 1) res = (res * x) % mod; x = (x * x) % mod; n >>= 1; } return res; } int main() { scanf("%d", &n); memset(s, 0, sizeof(s)); int mx = -1; for (int i = 0; i < n; i++) { scanf("%d", &tim); s[tim]++; mx = max(mx, tim); } long long sum = 0; for (int i = mx; i > 1; i--) { long long t = 0; for (int j = i; j <= mx; j += i) t += s[j]; if (!t) continue; num[i] = (t * pow_(2, t - 1)) % mod; for (int j = i * 2; j <= mx; j += i) { num[i] = (num[i] - num[j] + mod) % mod; } sum = (sum + num[i] * i) % mod; } printf("%lld\n", sum); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<pair<int, int> > w; for (int i = 0; i < n; i++) { int temp; cin >> temp; w.push_back(make_pair(temp, i)); } sort(w.begin(), w.end()); stack<int> introvert; string s; cin >> s; int t = 0; for (int i = 0; i < 2 * n; i++) { if (s[i] == '0') { introvert.push(w[t].second); cout << w[t].second + 1 << " "; t++; } else { cout << introvert.top() + 1 << " "; introvert.pop(); } } return 0; }
2
#include <bits/stdc++.h> using namespace std; string a[101]; bool comp(string a, string b) { if (a.size() < b.size()) return 1; else return 0; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1, comp); for (int i = 1; i < n; i++) { if (a[i + 1].find(a[i]) == -1) return cout << "NO", 0; } cout << "YES" << endl; for (int i = 1; i <= n; i++) cout << a[i] << endl; }
1
#include <bits/stdc++.h> int n, m; int main() { scanf("%d%d", &n, &m); printf(n == m ? "Yes" : "No"); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { long long r; while (b != 0) { r = a % b; a = b; b = r; } return a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } int const nmax = 200010; int n, m; vector<int> g[nmax]; map<pair<int, int>, int> mp; vector<int> ans; void euler(int u) { while (!g[u].empty()) { int v = g[u].back(); g[u].pop_back(); if (mp[pair<int, int>(u, v)] > 0) { mp[pair<int, int>(u, v)]--; mp[pair<int, int>(v, u)]--; euler(v); } } ans.push_back(u); } void solve() { cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; g[x].push_back(y); mp[pair<int, int>(x, y)]++; { g[y].push_back(x); mp[pair<int, int>(y, x)]++; } } vector<int> d; for (int i = 0; i < n; i++) { if (int((g[i]).size()) % 2 == 1) d.push_back(i); } if (int((d).size()) % 2 == 1) { int i = d.back(); d.pop_back(); g[i].push_back(i); mp[pair<int, int>(i, i)]++; } while (int((d).size())) { int x = d.back(); d.pop_back(); int y = d.back(); d.pop_back(); g[x].push_back(y); g[y].push_back(x); mp[pair<int, int>(x, y)]++; mp[pair<int, int>(y, x)]++; } euler(0); if (int((ans).size()) % 2 == 0) ans.push_back(0); cout << ans.size() - 1 << "\n"; bool st = true; for (int i = 1; i < int((ans).size()); i++) { if (st) cout << ans[i - 1] + 1 << " " << ans[i] + 1 << "\n"; else cout << ans[i] + 1 << " " << ans[i - 1] + 1 << "\n"; st = !st; } } int main() { solve(); return 0; }
9
#include <bits/stdc++.h> int main() { int i, n, a[1001], cnt = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] == 1) { cnt++; } } if (n == 1 && cnt == 1) { printf("YES"); } else if (n != 1 && cnt == (n - 1)) { printf("YES"); } else { printf("NO"); } return 0; }
1
#include <bits/stdc++.h> long long int dpc[1000001]; long long int dp[1000001]; bool level[1000001]; using namespace std; int main() { long long int n, q; cin >> n >> q; long long int v[n + 1]; long long int c[n + 1]; for (int i = 1; i <= n; ++i) { cin >> v[i]; } for (int i = 1; i <= n; ++i) { cin >> c[i]; } while (q--) { for (int i = 1; i <= n; ++i) { dpc[c[i]] = -1234567890123456; dp[i] = -1234567890123456; level[c[i]] = false; } int a, b; cin >> a >> b; long long int ma = 0; long long int mindex = 0, mindex1 = 0; long long int ma1 = 0; for (int i = 1; i <= n; ++i) { long long int g = a * v[i]; long long int f = b * v[i]; if (mindex != c[i]) { if (dpc[c[i]] == -1234567890123456) { dp[i] = max(ma + f, f); if (dpc[c[i]] < dp[i]) dpc[c[i]] = dp[i]; if (ma < dpc[c[i]]) { long long int t = ma; ma = dpc[c[i]]; ma1 = t; mindex1 = mindex; mindex = c[i]; } else if (ma1 < dpc[c[i]] && mindex != c[i]) { ma1 = dpc[c[i]]; mindex1 = c[i]; } } else { dp[i] = max(dpc[c[i]] + a * v[i], max(ma + f, f)); if (dpc[c[i]] < dp[i]) dpc[c[i]] = dp[i]; if (ma < dpc[c[i]]) { long long int t = ma; ma = dpc[c[i]]; ma1 = t; mindex1 = mindex; mindex = c[i]; } else if (ma1 < dpc[c[i]] && mindex != c[i]) { ma1 = dpc[c[i]]; mindex1 = c[i]; } } } else { dp[i] = max(dpc[c[i]] + a * v[i], max(ma1 + f, f)); if (dpc[c[i]] < dp[i]) dpc[c[i]] = dp[i]; if (ma < dpc[c[i]]) { ma = dpc[c[i]]; } } } long long int ans = 0; for (int i = 1; i <= n; ++i) { ans = max(ans, dp[i]); } cout << ans << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template <class T> int cntbit(T s) { return __builtin_popcount(s); } template <class T> inline T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } long long n, a[100005], d, res; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> d; for (long long i = 1, _b = n; i <= _b; i++) cin >> a[i]; for (long long i = 1, _b = n; i <= _b; i++) { long long tmp = upper_bound(a + i, a + n + 1, a[i] + d) - a; tmp -= i + 1; if (tmp >= 2) { res += tmp * (tmp - 1) / 2; } } cout << res; return 0; }
2
#include <bits/stdc++.h> using namespace std; const double eps = 1e-7; const int N = 1e7 + 4; const int M = 13; const long long mod = 1e9 + 7; const long long phi = 1e9 + 6; const int oo = 214748364; const int INF = 0x7fffffff; const double DOF = 999999999.99999; inline long long read() { long long zzl = 0, ioi = 1; char ch = ' '; while (!isdigit(ch)) { ch = getchar(); if (ch == '-') ioi = -1; } while (isdigit(ch)) { zzl = zzl * 10 + ch - '0'; ch = getchar(); } return zzl * ioi; } long long n, f1, f2, f3, c, ans; struct mat { long long size, m[10][10]; mat(long long x) { size = x; memset(m, 0, sizeof(m)); } void init() { for (register int i = 1; i <= size; i++) m[i][i] = 1; } friend mat operator*(mat x, mat y) { mat p(x.size); for (register int i = 1; i <= p.size; i++) for (register int l = 1; l <= p.size; l++) for (register int j = 1; j <= p.size; j++) p.m[i][j] = (p.m[i][j] + x.m[i][l] * y.m[l][j]) % phi; return p; } void out() { for (register int i = 1; i <= size; i++) { for (register int j = 1; j <= size; j++) printf("%d ", m[i][j]); puts(""); } } } bsa(3), bsb(5), x(3), y(3), z(3), w(5); mat mul(mat a, long long k) { mat p(a.size); p.init(); for (; k; k >>= 1, a = a * a) if (k & 1) p = p * a; return p; } long long ksm(long long x, long long k) { long long p = 1; for (; k; k >>= 1, x = x * x % mod) if (k & 1) p = p * x % mod; return p; } void init() { n = read(), f1 = read(), f2 = read(), f3 = read(), c = read(); w.m[5][1] = 2; x.m[3][1] = y.m[2][1] = z.m[1][1] = 1; bsa.m[1][1] = bsa.m[1][2] = bsa.m[1][3] = bsa.m[2][1] = bsa.m[3][2] = 1; bsb.m[1][1] = bsb.m[1][2] = bsb.m[1][3] = bsb.m[1][4] = bsb.m[1][5] = bsb.m[2][1] = bsb.m[3][2] = bsb.m[4][4] = bsb.m[4][5] = bsb.m[5][5] = 1; } int main() { init(); bsa = mul(bsa, n - 3); bsb = mul(bsb, n - 3); w = bsb * w; x = bsa * x; y = bsa * y; z = bsa * z; ans = ksm(c, w.m[1][1]) * ksm(f1, x.m[1][1]) % mod * ksm(f2, y.m[1][1]) % mod * ksm(f3, z.m[1][1]) % mod; printf("%lld", ans); return 0; }
7
#include <bits/stdc++.h> using namespace std; void solve() { int a, b; string s; cin >> a >> b >> s; int n = s.size(); int cntSecondType = 0, cntThirdType = 0, cntFourthType = 0; int lenFourthType = 0; int length = 0; for (int i = 0; i <= n; i++) { if (i == n || (i < n && (s[i] == 'X'))) { if (b <= length && length < a) cntSecondType++; else if (a <= length && length < 2 * b) cntThirdType++; else if (2 * b <= length) cntFourthType++, lenFourthType = length; length = 0; } else { length++; } } if (cntSecondType || cntFourthType >= 2) { cout << "NO\n"; return; } int canWin = 0; if (cntFourthType == 1) { for (int pos = 1; pos + a - 1 <= lenFourthType; pos++) { int len1 = pos - 1, len2 = lenFourthType - (pos + a) + 1; if (b <= len1 && len1 < a) continue; if (b <= len2 && len2 < a) continue; if (2 * b <= len1) continue; if (2 * b <= len2) continue; int newCnt = cntThirdType; if (a <= len1 && len1 < 2 * b) newCnt++; if (a <= len2 && len2 < 2 * b) newCnt++; if (newCnt % 2 == 0) canWin = 1; } } else { canWin = cntThirdType % 2; } if (canWin) cout << "YES\n"; else cout << "NO\n"; } int main() { ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0); cout.setf(ios::fixed), cout.precision(20); int q; cin >> q; for (int i = 0; i < q; i++) solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int MAX = 200002; int n, s; pair<int, int> res[MAX]; struct tmp { int id, val; } tem[MAX]; bool operator<(const tmp& a, const tmp& b) { return a.val < b.val; } int main() { scanf("%d %d", &n, &s); priority_queue<tmp> col; bool f = true; for (int i = 0, num; i < n; i++) { scanf("%d", &num); if (num == s) f = false; if (!num) continue; col.push((tmp){i, num}); } if (!f) { printf("No\n"); return 0; } int r = 0; tmp to, aux; while (!col.empty()) { to = col.top(); col.pop(); int key = to.val, k = 0; if (!key) { printf("No\n"); return 0; } for (int i = 0; i < to.val; i++) { if (col.empty()) { printf("No\n"); return 0; } aux = col.top(); col.pop(); if (!aux.val) { printf("No\n"); return 0; } if (aux.val > 0) { res[r++] = pair<int, int>(to.id, aux.id); if (aux.val - 1) tem[k++] = (tmp){aux.id, aux.val - 1}; } } for (int i = 0; i < k; i++) col.push(tem[i]); } printf("Yes\n"); printf("%d\n", r); for (int i = 0; i < r; i++) printf("%d %d\n", res[i].first + 1, res[i].second + 1); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; if (n == 2) cout << 2 << endl; else cout << n % 2 << endl; } }
0
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > vt; char a[1005][1005]; int row[1005]; int col[1005]; int n, m; long long solve(int n, int m, int row[]) { int i, j, k, p, l, r; long long ans = 0; for (i = 1; i <= n; ++i) if (row[i] != -1) { p = 0, l = i; while (i <= n && row[i] != -1 && row[i] > p) { p = row[i]; ++i; } r = --i; for (j = l; j <= r; ++j) for (k = j; k <= r; ++k) if (j != k) ans += 4 * (row[j] - 1) * (m - row[k]); else ans += 2 * (row[j] - 1) * (m - row[k]); } for (i = 1; i <= n; ++i) if (row[i] != -1) { p = m + 1, l = i; while (i <= n && row[i] != -1 && row[i] < p) { p = row[i]; ++i; } r = --i; for (j = l; j <= r; ++j) for (k = j; k <= r; ++k) if (j != k) ans += 4 * (m - row[j]) * (row[k] - 1); else ans += 2 * (m - row[j]) * (row[k] - 1); } return ans; } long long work() { vector<pair<int, int> >::iterator x, y; long long ans = 0; int i, j; ans = (long long)m * m * (n - 1) * n * (n + 1) / 3; ans += (long long)n * n * (m - 1) * m * (m + 1) / 3; for (i = 1; i <= n; ++i) for (x = vt.begin(); x != vt.end(); ++x) ans -= m * abs(i - x->first) * 2; for (j = 1; j <= m; ++j) for (x = vt.begin(); x != vt.end(); ++x) ans -= n * abs(j - x->second) * 2; for (x = vt.begin(); x != vt.end(); ++x) for (y = vt.begin(); y != vt.end(); ++y) ans += abs(x->first - y->first) + abs(x->second - y->second); ans += solve(n, m, row); for (i = 1; i <= n; ++i) for (j = i + 1; j <= m; ++j) swap(a[i][j], a[j][i]); ans += solve(m, n, col); return ans; } int main() { int i, j, num; memset(row, -1, sizeof(row)); memset(col, -1, sizeof(col)); scanf("%d%d", &n, &m); num = n * m; for (i = 1; i <= n; ++i) scanf("%s", a[i] + 1); for (i = 1; i <= n; ++i) for (j = 1; j <= m; ++j) if (a[i][j] == 'X') { row[i] = j; col[j] = i; vt.push_back(make_pair(i, j)); --num; } printf("%.10f\n", (double)work() / num / num); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int mod(1e9 + 7); const int maxn(5005); inline void Inc(int &x, int y) { if ((x += y) >= mod) x -= mod; } inline int Pow(long long x, int y) { register long long ret = 1; for (; y; y >>= 1, x = x * x % mod) if (y & 1) ret = ret * x % mod; return ret; } int f[maxn], n, k, ans; int main() { register int i, j, a, fac, cur; scanf("%d%d", &n, &k), f[0] = 1; for (i = 1; i <= n; ++i) { scanf("%d", &a); for (j = i; j; --j) f[j] = 1LL * f[j] * a % mod, Inc(f[j], mod - f[j - 1]); f[0] = 1LL * f[0] * a % mod; } a = Pow(n, 1LL * k * (mod - 2) % (mod - 1)), cur = Pow(n, k - min(n, k)); for (fac = 1, i = k - min(n, k) + 1; i <= k; ++i) fac = 1LL * fac * i % mod; for (i = min(n, k); ~i; --i) { Inc(ans, 1LL * f[i] * fac % mod * cur % mod); cur = 1LL * cur * n % mod, fac = 1LL * fac * Pow(k - i + 1, mod - 2) % mod; } ans = 1LL * ans * a % mod, ans = (f[0] - ans + mod) % mod; printf("%d\n", ans); return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { long long int n, t1, t2; cin >> n; vector<pair<long long int, long long int>> num; for (long long int i = 0; i < n; i++) { cin >> t1 >> t2; num.push_back(make_pair(t1, t2)); } sort(num.begin(), num.end()); long long int count = 0; long long int maxend = 0; for (long long int i = 0; i < n; i++) { if (num[i].second < maxend) count++; maxend = max(maxend, num[i].second); } cout << count << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<string> vec_splitter(string second) { second += ','; vector<string> res; while (!second.empty()) { res.push_back(second.substr(0, second.find(','))); second = second.substr(second.find(',') + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } long long powermod(long long base, long long exp, long long mod) { long long result = 1; while (exp != 0) { if ((exp % 2) == 1) { result = (result * base) % mod; } base = (base * base) % mod; exp /= 2; } return result; } vector<int> v[100001]; vector<int> in(100005 + 1), low(100005 + 1), vis(100005 + 1); vector<pair<int, int>> p; set<pair<int, int>> second; int timer; void dfs(int node, int parent) { vis[node] = 1; in[node] = low[node] = timer; timer++; for (auto child : v[node]) { if (child == parent) continue; if (vis[child]) { low[node] = min(low[node], in[child]); if (second.count({node, child}) or second.count({child, node})) { } else { p.push_back(make_pair(node, child)); second.insert({node, child}); } } else { dfs(child, node); low[node] = min(low[child], low[node]); if (low[child] > in[node]) { return; } else { if (second.count({node, child}) or second.count({child, node})) { } else { p.push_back(make_pair(node, child)); second.insert({node, child}); } } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tt = clock(); int n, m; cin >> n >> m; timer = 0; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } dfs(1, -1); if (p.size() == m) { for (auto second : p) cout << second.first << " " << second.second << endl; } else cout << 0 << endl; cerr << "TIME = " << (double)1.0 * (clock() - tt) / CLOCKS_PER_SEC << " seconds" << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int p[n + 1], s[n + 1], invs[n + 1], invp[n + 1], k = 0, cost = 0; vector<pair<int, int> > shift; for (int i = 1; i <= n; i++) { scanf("%d", &p[i]); invp[p[i]] = i; } for (int i = 1; i <= n; i++) { scanf("%d", &s[i]); invs[s[i]] = i; } for (int i = 1; i <= n; i++) { int x = s[i]; for (int j = invp[x] - 1; j >= i; j--) { int y = p[j]; if (invs[y] >= invp[x]) { swap(p[invp[x]], p[invp[y]]); swap(invp[x], invp[y]); cost += abs(invp[x] - invp[y]); k++; shift.push_back(make_pair(invp[x], invp[y])); } } } printf("%d\n", cost); printf("%d\n", k); for (int i = 0; i < k; i++) printf("%d %d\n", shift[i].first, shift[i].second); return 0; }
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 400000; int n, m; long long t; int d[MAXN]; vector<pair<int, int> > vv; vector<pair<int, int> > aa; int ans[MAXN]; long long get(long long s, long long e, int d, long long t) { if (d == -1) swap(e, s); e -= s; e = (e + m) % m; t *= 2; if (e > t) return 0; t -= e; long long ans = 1 + t / m; return ans; } int main() { scanf("%d%d%lld", &n, &m, &t); for (int i = 0; i < n; ++i) { int p; char c; scanf("%d %c", &p, &c); if (c == 'R') d[i] = 1; else d[i] = -1; --p; aa.push_back(make_pair(((p + t * d[i]) % m + m) % m, d[i])); vv.push_back(make_pair(p, i)); } sort(vv.begin(), vv.end()); sort(aa.begin(), aa.end()); int dd = d[vv[0].second]; int st = vv[0].first; pair<int, int> gg = make_pair( ((vv[0].first + t * d[vv[0].second]) % m + m) % m, d[vv[0].second]); for (int i = 0; i < n; ++i) if (aa[i] == gg) { rotate(aa.begin(), aa.begin() + i, aa.end()); break; } long long sum = 0; for (int i = 1; i < (int)vv.size(); ++i) { if (d[vv[i].second] == dd) continue; sum += get(st, vv[i].first, dd, t); } sum %= n; if (dd == -1) sum = (n - sum) % n; rotate(vv.begin(), vv.begin() + sum, vv.end()); for (int i = 0; i < n; ++i) ans[vv[i].second] = aa[i].first; for (int i = 0; i < n; ++i) printf("%d ", ans[i] + 1); printf("\n"); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int maxn = 100055; const int mod = 1e9 + 7; int root, sum, num; int pos[maxn]; int top[maxn]; int Size[maxn]; int p[maxn]; int id[maxn]; int son[maxn]; int d[maxn]; long long val[maxn]; int la[maxn]; vector<int> G[maxn]; struct node { int l, r; long long val; long long lazy; } rmq[maxn << 2]; void init() { memset(son, -1, sizeof(son)); memset(p, -1, sizeof(p)); root = 1; } void dfs1(int u, int fa, int step) { d[u] = step; p[u] = fa; Size[u] = 1; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (v == fa) continue; dfs1(v, u, step + 1); Size[u] += Size[v]; if (son[u] == -1 || Size[v] > Size[son[u]]) son[u] = v; } } void dfs2(int u, int tp) { top[u] = tp; id[u] = la[u] = ++num; pos[id[u]] = u; if (son[u] == -1) return; dfs2(son[u], tp); for (int i = 0; i < G[u].size(); i++) { int v = G[u][i]; if (v != son[u] && v != p[u]) { dfs2(v, v); } } la[u] = num; } int lca(int u, int v) { while (top[u] != top[v]) { if (d[top[u]] < d[top[v]]) swap(u, v); u = p[top[u]]; } return d[u] < d[v] ? u : v; } int LCA(int u, int v) { int a = lca(u, v); int b = lca(root, u); int c = lca(root, v); if (d[a] < d[b]) swap(a, b); if (d[a] < d[c]) swap(a, c); return a; } int nearlca(int u, int v) { if (d[u] > d[v]) swap(u, v); if (top[u] == top[v]) return pos[id[u] + 1]; while (top[u] != top[v]) { if (d[top[u]] > d[top[v]]) swap(u, v); if (p[top[v]] == u) break; else v = p[top[v]]; } if (top[u] == top[v]) return pos[id[u] + 1]; else return top[v]; } void Pushup(int rt) { rmq[rt].val = rmq[rt << 1].val + rmq[rt << 1 | 1].val; } void Pushdown(int rt) { int l = rmq[rt].l; int r = rmq[rt].r; int m = (l + r) >> 1; if (rmq[rt].lazy != 0) { long long c = rmq[rt].lazy; rmq[rt << 1].lazy += c; rmq[rt << 1 | 1].lazy += c; rmq[rt << 1].val += (long long)(m - l + 1) * c; rmq[rt << 1 | 1].val += (long long)(r - m) * c; rmq[rt].lazy = 0; } } void build(int l, int r, int rt) { rmq[rt].l = l; rmq[rt].r = r; rmq[rt].lazy = 0; if (l == r) { rmq[rt].val = val[pos[l]]; return; } int m = (l + r) >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); Pushup(rt); } void update(int rt, int L, int R, int c) { int l = rmq[rt].l; int r = rmq[rt].r; if (L <= l && r <= R) { rmq[rt].val += (long long)(r - l + 1) * c; rmq[rt].lazy += c; return; } Pushdown(rt); int m = (l + r) >> 1; if (L <= m) update(rt << 1, L, R, c); if (m < R) update(rt << 1 | 1, L, R, c); Pushup(rt); } long long query(int rt, int L, int R) { int l = rmq[rt].l; int r = rmq[rt].r; if (L <= l && r <= R) return rmq[rt].val; Pushdown(rt); int m = (l + r) >> 1; long long ret = 0; if (L <= m) ret += query(rt << 1, L, R); if (m < R) ret += query(rt << 1 | 1, L, R); return ret; } void add_edge(int u, int v) { G[u].push_back(v); G[v].push_back(u); } long long Query(int x) { return query(1, id[x], la[x]); } void Update(int x, int c) { update(1, id[x], la[x], c); } bool ini(int x, int y) { if (id[x] >= id[y] && id[x] <= la[y]) return true; return false; } int main() { init(); int n, q; scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) { cin >> val[i]; } for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); add_edge(u, v); } dfs1(1, -1, 0); dfs2(1, 1); build(1, num, 1); while (q--) { int k; scanf("%d", &k); if (k == 1) { int v; scanf("%d", &v); root = v; } else if (k == 2) { int u, v, x; scanf("%d%d%d", &u, &v, &x); int in_cnt = 0; int rt; if (ini(u, root)) in_cnt++; if (ini(v, root)) in_cnt++; if (in_cnt == 1) rt = root; else if (in_cnt == 2) rt = lca(u, v); else rt = LCA(u, v); if (rt == root) { Update(1, x); } else if (ini(rt, root)) { Update(rt, x); } else if (ini(root, rt)) { Update(1, x); int y = nearlca(root, rt); Update(y, -x); } else { Update(rt, x); } } else if (k == 3) { int v; scanf("%d", &v); long long ans; if (v == root) { ans = Query(1); } else if (ini(v, root)) { ans = Query(v); } else if (ini(root, v)) { ans = Query(1); int x = nearlca(root, v); ans -= Query(x); } else { ans = Query(v); } cout << ans << endl; } } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 11; int n, size = -1, dl[N << 1], cnt, st; int cha[N]; bool mark[N], book[N << 1]; long long res, q1[N], q2[N], h1[N], h2[N], chn[N], ans = 1e18, tmp; int read() { int x = 0; char ch = getchar(); while (ch > '9' || ch < '0') ch = getchar(); while (ch >= '0' && ch <= '9') x = 10 * x + ch - 48, ch = getchar(); return x; } struct ddd { int head[N], nex[N << 1], to[N << 1], wei[N << 1]; void clear() { memset(head, -1, sizeof(head)); } void add(int x, int y, int z) { nex[++size] = head[x]; to[size] = y; head[x] = size; wei[size] = z; } } E; void dfs(int u, int fa) { mark[u] = 1; for (int i = E.head[u]; i != -1; i = E.nex[i]) { int v = E.to[i]; if ((i ^ 1) == fa) continue; if (mark[v] && !book[i]) { cha[v] = -1; cha[u] = 1; dl[++cnt] = i; book[i] = book[i ^ 1] = 1; continue; } else { if (mark[v]) continue; dfs(v, i); cha[u] += cha[v]; if (cha[v]) { dl[++cnt] = i; book[i] = book[i ^ 1] = 1; } } } } long long dfs2(int u, int fa) { long long &fs = chn[u], sc = 0, now; for (int i = E.head[u]; i != -1; i = E.nex[i]) { if (book[i]) continue; int v = E.to[i]; if (fa == v) continue; now = dfs2(v, u) + E.wei[i]; if (now >= fs) sc = fs, fs = now; else if (now > sc) sc = now; } tmp = max(tmp, fs + sc); return fs; } void debug() { for (int i = 1; i <= cnt; i++) { int u = E.to[dl[i]]; printf("i=%d u=%d h1=%lld h2=%lld\n", i, u, h1[u], h2[u]); } } int main() { E.clear(); n = read(); int u, v, w; for (int i = 1; i <= n; i++) { u = read(), v = read(), w = read(); if (u == v) continue; E.add(u, v, w); E.add(v, u, w); } dfs(2, -1); for (int i = 1; i <= cnt; i++) { int u = E.to[dl[i]]; dfs2(u, u); } q1[E.to[dl[1] ^ 1]] = q2[E.to[dl[1] ^ 1]] = chn[E.to[dl[1] ^ 1]]; long long now = q1[E.to[dl[1] ^ 1]], sum = 0; for (int i = 2; i <= cnt; i++) { u = E.to[dl[i] ^ 1], v = E.to[dl[i]]; now += E.wei[dl[i]]; sum += E.wei[dl[i]]; q2[u] = max(q2[v], now + chn[u]); now = max(now, chn[u]); q1[u] = max(q1[v], sum + chn[u]); } u = E.to[dl[1]], v = E.to[dl[1] ^ 1]; h2[u] = chn[u]; h1[u] = chn[u]; sum = 0; now = chn[u]; for (int i = cnt; i >= 2; i--) { u = E.to[dl[i]], v = E.to[dl[i] ^ 1]; sum += E.wei[dl[i]]; now += E.wei[dl[i]]; h2[u] = max(h2[v], now + chn[u]); h1[u] = max(h1[v], sum + chn[u]); now = max(now, chn[u]); } long long res = 0; res = max(res, h2[E.to[dl[1] ^ 1]]); res = max(res, h1[E.to[dl[1] ^ 1]]); res = max(tmp, res); ans = min(ans, res); for (int i = 2; i <= cnt; i++) { u = E.to[dl[i]], v = E.to[dl[i] ^ 1]; res = q1[u] + h1[v] + E.wei[dl[1]]; res = max(res, max(q2[u], h2[v])); res = max(res, tmp); ans = min(ans, res); } cout << ans << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; template <typename T> vector<T> &operator+=(vector<T> &v, T x) { v.push_back(x); return v; } void solve() { int n; cin >> n; int a[n], b[n]; int cash = 1, t = 0; for (int i = (0), _b_ = (n); i < _b_; i++) { cin >> a[i] >> b[i]; if (i > 0) { if (a[i] == a[i - 1] && b[i] == b[i - 1]) { t++; } else { cash = max(cash, t + 1); t = 0; } } } cout << max(cash, t + 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); }
1
#include <bits/stdc++.h> using namespace std; vector<int> g[200001]; bool del[200001]; bool vis[200001]; int cnt[200001]; int evam[200001]; int n; int dfs(int v) { int ans = 1, t; vis[v] = 1; for (int i = 0; i < g[v].size(); i++) { if (!vis[g[v][i]]) { t = dfs(g[v][i]); ans += t; if (!(t & 1)) { evam[v]++; } } } if (ans != n && !((n - ans) & 1)) { evam[v]++; } return ans; } struct cmp { bool operator()(const int &a, const int &b) { if (evam[a] == evam[b]) { if (!(cnt[a] & 1) && (cnt[b] & 1)) { return true; } else if (!(cnt[b] & 1) && (cnt[a] & 1)) { return false; } else { return a < b; } } else { return evam[a] < evam[b]; } } }; int main() { ios::sync_with_stdio(false); int t; set<int, cmp> st; cin >> n; if (n & 1) { for (int i = 1; i <= n; i++) { cin >> t; if (t) { g[t].push_back(i); cnt[t]++; g[i].push_back(t); cnt[i]++; } } cout << "YES" << endl; t = dfs(1); for (int i = 1; i <= n; i++) { if (!evam[i]) { st.insert(i); } } while (!st.empty()) { t = *st.begin(); st.erase(st.begin()); cout << t << endl; del[t] = 1; for (int i = 0; i < g[t].size(); i++) { if (!del[g[t][i]]) { cnt[g[t][i]]--; evam[g[t][i]]--; if (!evam[g[t][i]]) { st.insert(g[t][i]); } } } } } else { cout << "NO" << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; long long const INF = 1e18, p = 1e9 + 7; void speedup() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } void solve() { long long n; cin >> n; vector<pair<long long, long long>> pr; for (long long i = 0; i < n; ++i) { long long x; cin >> x; pr.push_back({x, -i}); } sort(pr.rbegin(), pr.rend()); long long m; cin >> m; vector<vector<pair<long long, long long>>> g(n + 1); for (long long i = 0; i < m; ++i) { vector<pair<long long, long long>> a; a.clear(); long long k, pz; cin >> k >> pz; if (g[k].size() == 0) { for (long long i = 0; i < k; ++i) { a.push_back({-pr[i].second, pr[i].first}); } sort(a.begin(), a.end()); swap(a, g[k]); } cout << g[k][pz - 1].second << '\n'; } } signed main() { speedup(); solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, m, k; int a[200005]; int main() { while (scanf("%d%d%d", &n, &m, &k) != EOF) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + 1 + n); k++; long long t = m / k, tt = m % k; long long ans = t * (k - 1) * a[n] + t * a[n - 1] + tt * a[n]; printf("%lld\n", ans); } }
1
#include <bits/stdc++.h> using namespace std; int main() { int k, n, t; bool fl, f; string s; vector<int> a; vector<int> b(5); vector<int> d(10); for (int i = 0; i < 6; i++) { cin >> k; a.push_back(k); } cin >> n; vector<int> c(n); for (int i = 0; i < n; i++) { cin >> s; t = 0; fl = false; for (int j = 0; j < s.size(); j++) { if (s[j] == ',') { fl = true; if (t == 'S') { b[0]++; c[i] = 7; break; } if (t == 'M') { b[1]++; c[i] = 8; break; } if (t == 'L') { b[2]++; c[i] = 9; break; } if (t == 'X' + 'L') { b[3]++; c[i] = 10; break; } b[4]++; c[i] = 11; break; } else { t += s[j]; } } if (!fl) { if (t == 'S') { a[0]--; c[i] = 1; } else { if (t == 'M') { a[1]--; c[i] = 2; } else { if (t == 'L') { a[2]--; c[i] = 3; } else { if (t == 'X' + 'L') { a[3]--; c[i] = 4; } else { if (t == 'X' + 'X' + 'L') { a[4]--; c[i] = 5; } else { a[5]--; c[i] = 6; } } } } } } } f = true; for (int i = 0; i < 6; i++) { if (a[i] < 0) { f = false; break; } else { if (i == 0) { b[0] -= a[0]; d[0] = a[0]; } else { if (i >= 1 && i < 5) { a[i] -= max(b[i - 1], 0); if (a[i] < 0) { f = false; break; } else { b[i] -= a[i]; d[2 * i] = a[i]; } } else { if (b[4] > a[5]) { f = false; break; } else { d[9] = a[5]; } } } } } if (!f) { cout << "NO"; } else { cout << "YES\n"; for (int i = 0; i < n; i++) { if (c[i] == 1) { cout << "S\n"; continue; } if (c[i] == 2) { cout << "M\n"; continue; } if (c[i] == 3) { cout << "L\n"; continue; } if (c[i] == 4) { cout << "XL\n"; continue; } if (c[i] == 5) { cout << "XXL\n"; continue; } if (c[i] == 6) { cout << "XXXL\n"; continue; } if (c[i] == 7) { if (d[0] > 0) { cout << "S\n"; d[0]--; } else { cout << "M\n"; } continue; } if (c[i] == 8) { if (d[2] > 0) { cout << "M\n"; d[2]--; } else { cout << "L\n"; } continue; } if (c[i] == 9) { if (d[4] > 0) { cout << "L\n"; d[4]--; } else { cout << "XL\n"; } continue; } if (c[i] == 10) { if (d[6] > 0) { cout << "XL\n"; d[6]--; } else { cout << "XXL\n"; } continue; } else { if (d[8] > 0) { cout << "XXL\n"; d[8]--; } else { cout << "XXXL\n"; } } } } return 0; }
5
/****************************************************************************** Online C++ Compiler. Code, Compile, Run and Debug C++ program online. Write your code in this editor and press "Run" button to compile and execute it. *******************************************************************************/ #include <bits/stdc++.h> using namespace std; #define lli int #define ll long long int main() { lli tc=1; cin>>tc; while(tc--){ lli n,i; cin>>n; string s; cin>>s; if(s[0]=='1' and s[n-1]=='1'){ lli c1=0,c0=0; for(lli i=0;i<n;i++){ if(s[i]=='1')c1++; else c0++; } if(c1%2==0 and c0%2==0){ cout<<"YES\n"; lli c11=0,c10=0; for(i=0;i<n;i++){ if(s[i]=='1' and c11<c1/2)cout<<"(",c11++; else if(s[i]=='1' and c11>=c1/2)cout<<")",c11++; else if(s[i]=='0' and c10%2==1)cout<<")",c10++; else if(s[i]=='0' and c10%2==0)cout<<"(",c10++; } cout<<"\n"; c11=0,c10=0; for(i=0;i<n;i++){ if(s[i]=='1' and c11<c1/2)cout<<"(",c11++; else if(s[i]=='1' and c11>=c1/2)cout<<")",c11++; else if(s[i]=='0' and c10%2==1)cout<<"(",c10++; else if(s[i]=='0' and c10%2==0)cout<<")",c10++; } cout<<"\n"; } else goto label1; } else label1:cout<<"NO\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<bool> sieve(long long n) { vector<bool> prime(n + 1, true); prime[0] = prime[1] = false; for (long long i = 2; i <= n; ++i) { if (prime[i] && (i * i) <= n) for (long long j = i * i; j <= n; j += i) prime[j] = false; } return prime; } long long power(long long a, long long b, long long m = 1000000007) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long r, g, b; cin >> r >> g >> b; if (r * g * b == 0) { cout << r / 3 + b / 3 + g / 3; return 0; } long long val1 = r / 3 + b / 3 + g / 3; long long val2 = 1 + (r - 1) / 3 + (b - 1) / 3 + (g - 1) / 3; long long val3 = 2 + (r - 2) / 3 + (b - 2) / 3 + (g - 2) / 3; cout << max({val1, val2, val3}); return 0; }
4
#include <queue> #include <vector> #include<iostream> #include<map> #include<math.h> #include<string.h> #include<algorithm> #include<bits/stdc++.h> #define rep(i,a,b) for( int i(a); i <= b; ++i) #define ll long long #define inf 1e15 #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define PI acos(-1) #define fi first #define se second #define pb push_back #define debug(x) printf("x=%d\n",x); #define ls(x) arr[x].child[0] #define rs(x) arr[x].child[1] #define PI acos(-1) using namespace std; const int N=1e6+7; const ll mod=1e9+7; inline ll read() { ll X=0; bool flag=1; char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();} while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();} if(flag) return X; return ~(X-1); } ll n,k,m; ll ar[N],br[N]; ll ans; int bc[N],ac[N]; vector<int>vt[5]; void solvve() { ll a=read(),b=read(); // if(a>b) swap(a,b); if(a>b) {cout<<"NO"<<endl; return;} int cnt1=0,cnt2=0; rep(i,0,32) { if(a&(1<<i)) cnt1++; if(b&(1<<i)) cnt2++; if(cnt2) { if(!cnt1){cout<<"NO"<<endl; return;} else { cnt1--; cnt2--; } } } cout<<"YES"<<endl; //else cout<<"NO"<<endl; } int main() { // freopen("input","r",stdin); // while(scanf("%lld",&n)!=EOF) int T; T=read();while(T--) { //printf("cas=%d ",++cc); solvve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int IINF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; const double DINF = numeric_limits<double>::infinity(); const int MOD = 1000000007; const double EPS = 1e-9; const double PI = acos(-1.0); const int DX[] = {1, 0, -1, 0, 1, -1, 1, -1}; const int DY[] = {0, 1, 0, -1, 1, -1, -1, 1}; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long sqr(long long x) { return x * x; } long long sqr(int x) { return (long long)x * x; } double sqr(double x) { return x * x; } long double sqr(long double x) { return x * x; } mt19937 mmtw(960172); long long rnd(long long x, long long y) { static uniform_int_distribution<long long> d; return d(mmtw) % (y - x + 1) + x; } const int N = 100179; struct Step { int x1, y1, x2, y2; }; vector<Step> solve(vector<vector<pair<string, int>>> &f) { int sx = ((int)((f).size())); int sy = ((int)((f[0]).size())); vector<Step> ans; auto step = [&](int x1, int y1, int x2, int y2) { ans.push_back({x1, y1, x2, y2}); f[x2][y2].first.push_back(f[x1][y1].first[f[x1][y1].second++]); }; auto top = [&](int x, int y) { return f[x][y].first[f[x][y].second] - '0'; }; auto empty = [&](int x, int y) { return f[x][y].second == ((int)((f[x][y].first).size())); }; int sz0 = ((int)((f[0][0].first).size())); int sz1 = ((int)((f[0][1].first).size())); for (int i = 0; i < sz0; ++i) if (top(0, 0) == 0) step(0, 0, 1, 0); else step(0, 0, 0, 1); for (int i = 0; i < sz1; ++i) if (top(0, 1) == 1) step(0, 1, 1, 1); else step(0, 1, 0, 0); for (int x = 0; x < sx; ++x) for (int y = 2; y < sy; ++y) while (!empty(x, y)) step(x, y, x, top(x, y)); for (int x = 1; x < sx; ++x) { while (true) { if (!empty(x, 0)) { if (top(x, 0) == 0) step(x, 0, 0, 0); else step(x, 0, x, 1); } else if (!empty(x, 1)) { if (top(x, 1) == 1) step(x, 1, 0, 1); else step(x, 1, x, 0); } else break; } } return ans; } int main() { ios::sync_with_stdio(false); int n, m; scanf("%d%d", &n, &m); vector<vector<pair<string, int>>> f1(n, vector<pair<string, int>>(m)); vector<vector<pair<string, int>>> f2(n, vector<pair<string, int>>(m)); static char ss[N]; for (auto &f : f1) for (auto &s : f) { scanf("%s", ss); s.first = ss; s.second = 0; reverse((s.first).begin(), (s.first).end()); } for (auto &f : f2) for (auto &s : f) { scanf("%s", ss); s.first = ss; s.second = 0; } auto ans1 = solve(f1); auto ans2 = solve(f2); reverse((ans2).begin(), (ans2).end()); cout << ((int)((ans1).size())) + ((int)((ans2).size())) << "\n"; for (auto const &s : ans1) cout << s.x1 + 1 << " " << s.y1 + 1 << " " << s.x2 + 1 << " " << s.y2 + 1 << "\n"; for (auto const &s : ans2) cout << s.x2 + 1 << " " << s.y2 + 1 << " " << s.x1 + 1 << " " << s.y1 + 1 << "\n"; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int hea[maxn], nxt[maxn << 1], to[maxn << 1], maxd, tot; vector<int> pos[maxn]; void add(int a, int b) { nxt[++tot] = hea[a]; hea[a] = tot; to[tot] = b; } void dfs(int x, int fa, int d) { pos[d].push_back(x); maxd = max(maxd, d); for (int i = hea[x]; i; i = nxt[i]) if (to[i] != fa) dfs(to[i], x, d + 1); } int main() { int T; string s; scanf("%d", &T); while (T--) { memset(hea, tot = 0, sizeof(hea)); int n, a, b, x, d; maxd = 0; scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d%d", &a, &b); add(a, b); add(b, a); } printf("? %d", n); for (int i = 1; i <= n; i++) { printf(" %d", i); pos[i].clear(); } pos[0].clear(); cout << endl; cin >> x >> d; dfs(x, 0, 0); int l = 0, r = maxd, D = d, X = -1; while (l <= r) { int mid = (l + r) >> 1; printf("? %llu", pos[mid].size()); for (int i : pos[mid]) printf(" %d", i); cout << endl; cin >> x >> d; if (d == D) l = mid + 1, X = x; else r = mid - 1; } pos[D].clear(); dfs(X, 0, 0); printf("? %llu", pos[D].size()); for (int i : pos[D]) printf(" %d", i); cout << endl; cin >> x >> d; cout << "! " << x << ' ' << X << endl; cin >> s; if (s == "Incorrect") break; } return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long N = (long long)(1e6) + 322; const long long inf = (long long)1e13; const long long mod = 1000000007; const double eps = 1e-9; long long ans, n, a[N], w; void calc(long long id, long long sum) { if (id == 0) { ans = max(ans, sum); return; } long long mx = min(a[id], (w - sum) / id); for (long long i = mx; i >= 0 && i >= mx - 10; i--) { calc(id - 1, sum + id * i); } } signed main() { ios_base ::sync_with_stdio(false); cin.tie(0); cin >> w; for (long long i = 1; i <= 8; ++i) cin >> a[i]; calc(8, 0); cout << ans; return 0; }
7
#include <bits/stdc++.h> using namespace std; int cnt[150005], sz[150005], p[150005]; void init(int n) { for (int i = int(1); i <= int(n); i++) { p[i] = i; sz[i] = 1; cnt[i] = 0; } } int fp(int x) { if (x != p[x]) p[x] = fp(p[x]); return p[x]; } void link(int x, int y) { p[x] = y; cnt[y] += cnt[x]; sz[y] += sz[x]; } int main() { int n, m, x, y, px, py; scanf("%d", &n); scanf("%d", &m); init(n); for (int i = int(1); i <= int(m); i++) { scanf("%d", &x); scanf("%d", &y); px = fp(x); py = fp(y); if (px != py) link(px, py); cnt[py]++; } bool fl = 1; for (int i = int(1); i <= int(n); i++) { if (i != fp(i)) continue; if (cnt[i] != (sz[i] * 1LL * (sz[i] - 1)) / 2) { fl = 0; break; } } if (fl) cout << "YES\n"; else cout << "NO\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; unordered_map<long long, int> M; int T[200009]; int I[200009]; int N[200009]; int f[200009]; int w[29]; int n[29]; vector<int> v[29]; long long su[29]; vector<int> Y[200009]; stack<int> S; void dfs(int x) { f[x] = 1; S.push(x); int k = N[x], u = I[k]; if (!k || f[k] == 2) { f[x] = 2; return; } if (f[k] == 1) { vector<int> z = {k}; int B = (1 << u); while (S.top() != k) { z.push_back(S.top()); B |= (1 << I[S.top()]); S.pop(); } if (__builtin_popcount(B) == z.size()) Y[B] = z; f[x] = 2; return; } dfs(k); f[x] = 2; } int main() { int k; int c = 0; long long SU = 0; cin >> k; for (int i = 0; i < k; i++) { int x; cin >> x; for (int j = 0; j < x; j++) { int a; cin >> a; v[i].push_back(a); T[++c] = a; M[a] = c; I[c] = i; su[i] += a; SU += a; } } if (SU % k != 0) { cout << "No" << endl; return 0; } long long s = SU / k; for (int i = 0; i < k; i++) { for (int x : v[i]) { N[M[x]] = M[s - (su[i] - x)]; } } for (int i = 1; i <= c; i++) { if (f[i] == 0) { while (S.size()) S.pop(); dfs(i); } } for (int i = 1; i < (1 << k); i++) { if (Y[i].size()) continue; for (int x = (i - 1) & i; x; x = (x - 1) & i) { if (Y[x].size() && Y[i - x].size()) { Y[i] = Y[x]; for (int u : Y[i - x]) Y[i].push_back(u); break; } } } if (!Y[(1 << k) - 1].size()) { cout << "No" << endl; return 0; } cout << "Yes" << endl; vector<int> v = Y[(1 << k) - 1]; for (int x : v) { w[I[x]] = T[x]; n[I[N[x]]] = I[x]; } for (int i = 0; i < k; i++) cout << w[i] << " " << n[i] + 1 << endl; }
8
#include <bits/stdc++.h> using namespace std; vector<int> *e; vector<int> mn; vector<int> ord; int *used; int dfs(int v) { used[v] = 1; for (int w : e[v]) { if (used[w] == 0) dfs(w); if (used[w] == 1) return -1; } used[v] = 2; ord.push_back(v + 1); return 1; } int main() { int n, k, a, b; cin >> n >> k; for (int i = 0; i < k; i++) { cin >> a; mn.push_back(a - 1); } used = new int[n]; e = new vector<int>[n]; for (int i = 0; i < n; i++) { used[i] = 0; cin >> a; for (int j = 0; j < a; j++) { cin >> b; e[i].push_back(b - 1); } } int fr = 0; for (int w : mn) { if (!used[w]) { int res = dfs(w); if (res == -1) fr = -1; } } if (fr == -1) cout << -1; else { cout << ord.size() << endl; for (auto i = ord.begin(); i != ord.end(); i++) cout << *i << ' '; } }
3
#include <bits/stdc++.h> using namespace std; template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template <class T> int cntbit(T s) { return __builtin_popcount(s); } template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } long long q, l, r; long long f[16][1 << 16]; string str = "0123456789abcdef"; long long n, m; int a[15], num; long long g[20][20][2][2][2]; void init() { memset(f, 0, sizeof(f)); for (int mask = 0; mask < (1 << 16); ++mask) { int temp = mask; int A = 15; int Max = 0; for (int run = 0; run < (4); ++run) { int u = temp & A; Max = max(Max, u); temp >>= 4; } for (int i = 0; i < (16); ++i) { if (mask == 0) f[i][mask] = 0; else f[i][mask] = f[i][mask - 1]; } for (int i = 0; i < (16); ++i) if ((i <= Max && getbit(mask, Max)) || (getbit(mask, i) && i > Max)) { f[i][mask]++; } } } int TT(char ch) { for (int i = 0; i < (16); ++i) if (str[i] == ch) return i; return -1; } long long remind(string s) { long long res = 0; for (int i = 0; i < (((long long)(s).size())); ++i) { res = res * 16 + TT(s[i]); } return res; } long long go(int id, int so, int cochua, int behon, int can) { if (id == 0) { return (cochua & (behon >= can)); } long long &res = g[id][so][cochua][behon][can]; if (res != -1) return res; res = 0; int lo = 0, hi = so; if (!behon) { hi = min(hi, a[id]); } for (int i = lo; i <= hi; i++) { res += go(id - 1, so, cochua | (i == so), behon | (i < a[id]), can); } return res; } long long cal(long long x) { if (x <= 0) return 0; long long A = (1ll << 16) - 1; long long B = (1ll << 4) - 1; m = (A & x); n = (x >> 16); num = 0; while (n) { a[++num] = (n & B); n >>= 4; } memset(g, -1, sizeof(g)); long long res = 0; for (int i = 0; i < (16); ++i) { int cochua = 0; if (i == 0) cochua = 1; long long T = go(num, i, cochua, 0, 0); res += T * f[i][m]; T = go(num, i, cochua, 0, 1); res += T * (f[i][A] - f[i][m]); } return res; } void solve() { ios::sync_with_stdio(0); cin.tie(0); init(); cin >> q; for (int run = 0; run < (q); ++run) { string s; cin >> s; l = remind(s); cin >> s; r = remind(s); cout << cal(r) - cal(l - 1) << endl; } } int main() { solve(); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, m; vector<int> g[N]; int main() { std ::ios ::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { cin >> n >> m; if (n % m == 0) { cout << "YES\n"; continue; } else cout << "NO\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; string s; cin >> s; long long cnt = 0; vector<long long> v; for (int i = 0; i < s.length() - 10; i++) { if (s[i] == '8') { cnt++; v.push_back(i); } } if (cnt == 0) puts("NO"); else { long long mid = ((n - 11)) / 2; if (cnt <= mid) puts("NO"); else { long long sum = 0; for (int i = 1; i < v.size(); i++) { sum += ((v[i] - v[i - 1]) - 1); } if (sum > mid) puts("NO"); else puts("YES"); } } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { long long ans = max(a, b), minn = min(a, b); while (true) { if (ans % minn == 0) return minn; ans = (ans % minn); if (ans < minn) swap(ans, minn); } } long long bs(long long x) { long long f = 0, l = x, m = (f + l) / 2; while (true) { m = (f + l) / 2; if (true) return m; else if (true) f = m + 1; else l = m - 1; } } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, x, z, d = 0, i = 0, k; cin >> n >> x; z = x / 2; if (x % 2 != 0) { d = 1; z++; } k = z; if (d == 1) { i = k - 1; int j = z, l; l = j; n--; while (true) { i++; if (k == i) { cout << k << endl; z = k; i = 0; if (!n--) break; } else { z = k - i; cout << z << endl; if (!n--) break; z = k + i; cout << z << endl; if (!n--) break; } } } else { i = k - 1; int j = z + 1, l; l = j; n--; while (true) { i++; if (k == i) { cout << k << endl; z = k; i = 0; if (!n--) break; cout << j << endl; l = j; i = 0; if (!n--) break; } else { z = k - i; cout << z << endl; if (!n--) break; l = j + i; cout << l << endl; if (!n--) break; } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 400100; const long long mod = 1000000007; const long long alpha = 300; long long c[22][MAXN], p[MAXN], cn[MAXN], pn[MAXN], cnt[MAXN]; void suffix_ini(string s) { long long n = s.size(); for (long long i = 0; i < n; i++) cnt[s[i]]++; for (long long i = 1; i < alpha; i++) cnt[i] += cnt[i - 1]; for (long long i = n - 1; i >= 0; i--) p[--cnt[s[i]]] = i; long long cla = 0; c[0][p[0]] = 0; for (long long i = 1; i < n; i++) { if (s[p[i]] != s[p[i - 1]]) ++cla; c[0][p[i]] = cla; } } void suffix(string s) { long long n = s.size(); for (long long h = 0; ((long long)1 << h) < n; h++) { for (long long i = 0; i < n; i++) { pn[i] = p[i] - ((long long)1 << h); if (pn[i] < 0) pn[i] += n; } memset(cnt, 0, sizeof cnt); for (long long i = 0; i < n; i++) cnt[c[h][pn[i]]]++; for (long long i = 1; i < n; i++) cnt[i] += cnt[i - 1]; for (long long i = n - 1; i >= 0; i--) p[--cnt[c[h][pn[i]]]] = pn[i]; long long cla = 0; cn[p[0]] = 0; for (long long i = 1; i < n; i++) { pair<long long, long long> cur = {c[h][p[i]], c[h][(p[i] + ((long long)1 << h)) % n]}; pair<long long, long long> pre = { c[h][p[i - 1]], c[h][(p[i - 1] + ((long long)1 << h)) % n]}; if (cur.first != pre.first || cur.second != pre.second) cla++; cn[p[i]] = cla; } for (long long i = 0; i < n; i++) c[h + 1][i] = cn[i]; } } struct str { long long a = 0, b = 0, c = 0; }; long long v[MAXN], n, k[MAXN], pai[MAXN]; str t[MAXN]; vector<long long> vec[MAXN]; long long res = 0; long long A, B, C; long long find(long long x) { if (pai[x] == x) return x; return pai[x] = find(pai[x]); } void join(long long a, long long b) { b = find(b); res -= t[b].a * t[b].b * t[b].c; long long h; if (a > b) h = a - 1; else h = a; if (p[h] < A) t[b].a--; else if (p[h] < A + B) t[b].b--; else t[b].c--; a = find(a); res -= t[a].a * t[a].b * t[a].c; t[b].a += t[a].a; t[b].b += t[a].b; t[b].c += t[a].c; res += t[b].a * t[b].b * t[b].c; res %= mod; pai[a] = b; } long long comp(long long x, long long y) { long long r = 0; for (long long i = 20; i >= 0; i--) { if (c[i][x] != c[i][y] || c[i][x] == -1) continue; r += ((long long)1 << i); x += ((long long)1 << i); y += ((long long)1 << i); if (x > n) x -= n; if (y > n) y -= n; } return r; } signed main() { memset(k, 0, sizeof k); memset(c, -1, sizeof c); string a, b, c; cin >> a >> b >> c; a += "#"; b += "&"; c += "$"; A = a.size(), B = b.size(), C = c.size(); string s = a + b + c; n = s.size(); suffix_ini(s); suffix(s); for (long long i = 1; i < s.size(); i++) { v[i] = comp(p[i - 1], p[i]); pai[i] = i; if (p[i] < A) t[i].a++; else if (p[i] < A + B) t[i].b++; else t[i].c++; if (p[i - 1] < A) t[i].a++; else if (p[i - 1] < A + B) t[i].b++; else t[i].c++; vec[v[i]].push_back(i); } vector<long long> r; for (long long i = (long long)s.size(); i >= 1; i--) { for (auto j : vec[i]) { if (k[j + 1] == 1) join(j, j + 1); if (k[j - 1] == 1) join(j, j - 1); k[j] = 1; } if (i <= min({A, B, C}) - 1) r.push_back(res); } for (long long i = (long long)r.size() - 1; i >= 0; i--) cout << r[i] << " "; }
8
#include <bits/stdc++.h> using namespace std; const int MX = 505; int a[MX], b[MX]; long long tot[MX]; bool can[MX][MX]; long long dp[MX][MX]; int main() { int i, j, k; int n, c; cin >> n >> c; for (i = 1; i <= n; ++i) cin >> a[i] >> b[i]; for (i = 1; i <= n; ++i) tot[i] = tot[i - 1] + a[i] + b[i]; can[0][0] = 1; for (i = 1; i <= n; ++i) { for (j = 0; j < c; ++j) { if (!can[i - 1][j]) continue; int prev = (tot[i - 1] - j) % c; for (k = 0; k < c; ++k) { int tj = j, ta = a[i], tb = b[i], cnt = 0; if (tj > k) { tj -= k; ta -= (c - tj); cnt++; if (ta < 0) continue; } else { ta -= (k - tj); if (ta < 0) continue; } cnt += ta / c; ta %= c; if (ta) { tb -= (c - ta); cnt++; if (tb < 0) continue; } cnt += (prev + tb) / c; can[i][k] = 1; dp[i][k] = dp[i - 1][j] + cnt; } } } long long mx = 0; for (i = 0; i < c; ++i) mx = max(mx, dp[n][i]); cout << mx << endl; }
8
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> A, pair<int, int> B) { return A.first > B.first; } int main() { int n; cin >> n; long long sum = 0; int a[1005]; vector<pair<int, int>> v_in, v_io; vector<pair<int, pair<int, int>>> b; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } sum /= n; if (sum * 1.0 / n != (int)sum * 1.0 / n) { cout << "Unrecoverable configuration." << endl; exit(0); } long long num = sum; bool flag = true; for (int i = 0; i < n; i++) { if (a[i] > num) v_in.push_back(make_pair(a[i] - num, i)), flag = false; else if (a[i] < num) v_io.push_back(make_pair(num - a[i], i)), flag = false; } if (flag) { cout << "Exemplary pages." << endl; exit(0); } sort(v_in.begin(), v_in.end(), cmp); sort(v_io.begin(), v_io.end(), cmp); if (v_in.size() != v_io.size() || v_in.size() >= 2) { cout << "Unrecoverable configuration." << endl; exit(0); } flag = true; for (int i = 0; i < v_in.size(); i++) { if (v_in[i].first != v_io[i].first) { flag = false; break; } b.push_back( make_pair(v_in[i].first, make_pair(v_in[i].second, v_io[i].second))); } if (!flag) { cout << "Unrecoverable configuration." << endl; } else { for (int i = 0; i < b.size(); i++) printf("%d ml. from cup #%d to cup #%d.\n", b[i].first, b[i].second.second + 1, b[i].second.first + 1); } }
2
#include <bits/stdc++.h> using namespace std; inline int read() { int res = 0, f = 1; char c = getchar(); while (!(c >= '0' && c <= '9') && c != '-') c = getchar(); if (c == '-') f = -1, c = getchar(); while ((c >= '0' && c <= '9')) res = (res << 3) + (res << 1) + (c ^ 48), c = getchar(); return res * f; } const int maxn = 1e4 + 10; int n, e[maxn << 1][2], h[maxn]; void add_edge(int u, int v, int i) { e[i][0] = v, e[i][1] = h[u]; h[u] = i; return; } int siz[maxn], dp[maxn], y = 1e9, cen; void dfs(int u, int fa) { siz[u] = 1; for (int i = h[u]; i; i = e[i][1]) { int v = e[i][0]; if (v != fa) { dfs(v, u); siz[u] += siz[v]; dp[u] = max(dp[u], siz[v]); } } dp[u] = max(dp[u], n - siz[u]); if (dp[u] < y) cen = u, y = dp[u]; return; } pair<int, int> a[maxn]; int cnt; int num, fa[maxn], ans[maxn]; void work(int u, int sta, int w) { siz[u] = 1; ans[u] = sta * w; sta = 1; for (int i = h[u]; i; i = e[i][1]) { int v = e[i][0]; if (v != fa[u]) { fa[v] = u; work(v, sta, w); siz[u] += siz[v]; sta += siz[v]; } } } int main() { n = read(); for (int i = 1, u, v; i < n; i++) { u = read(), v = read(); add_edge(u, v, i * 2); add_edge(v, u, i * 2 + 1); } dfs(1, 0); for (int i = h[cen]; i; i = e[i][1]) { int v = e[i][0]; if (siz[v] < siz[cen]) a[++cnt] = make_pair(siz[v], v); else a[++cnt] = make_pair(n - siz[cen], v); } sort(a + 1, a + cnt + 1); int mx = -1, bd = 0, tmp = 0, t; for (int i = 1; i <= cnt; i++) { tmp += a[i].first; if (tmp * (n - tmp - 1) > mx) mx = tmp * (n - tmp - 1), t = tmp, bd = i; } tmp = 1; for (int i = 1; i <= bd; i++) { fa[a[i].second] = cen; work(a[i].second, tmp, 1); tmp += a[i].first; } tmp = 1; for (int i = bd + 1; i <= cnt; i++) { fa[a[i].second] = cen; work(a[i].second, tmp, t + 1); tmp += a[i].first; } for (int i = 1; i <= n; i++) if (i != cen) printf("%d %d %d\n", i, fa[i], ans[i]); return 0; }
9
#include <bits/stdc++.h> using namespace std; const long long N = (long long)1e6 + 113; const double eps = 1e-7; long long n, k, ans; char p; long long used[30]; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> p; used[int(p - 'A' + 1)]++; } sort(used + 1, used + 28); reverse(used + 1, used + 28); for (int i = 1; i <= 27; i++) { long long num = min(k, used[i]); ans += num * num; k -= num; } cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m; int grid[250][250]; int deltax[] = {0, 1, 1, 1, 0, -1, -1, -1}; int deltay[] = {1, 1, 0, -1, -1, -1, 0, 1}; bool valid(int x, int y) { if (x >= n || y >= m || x < 0 || y < 0) return false; return true; } int consider[][8] = {{0, 1, 0, 1, 1, 1, 1, 0}, {0, 0, 1, 1, 0, 1, 1, 0}, {0, 0, 0, 0, 1, 1, 1, 0}, {0, 0, 1, 0, 0, 0, 1, 0}}; bool isGood(int x, int y, int diag, int type, int corner) { if (!valid(x, y)) return false; if (grid[x][y] == 0) return false; int forward = 2 * type + diag; int row = (1 - diag) * 2 + (1 - corner); for (int i = 0; i < 8; i++) { if (consider[row][i]) { int dir = (forward + i) % 8; int u = x + deltax[dir]; int v = y + deltay[dir]; if (valid(u, v) && grid[u][v] == 1) return false; } } return true; } bool isSquare(int x, int y, int diag) { int xstart = x, ystart = y; int s = 0; int type = 0; if (!isGood(x, y, diag, type, 1)) return false; do { x += deltax[2 * type + diag]; y += deltay[2 * type + diag]; s++; } while (isGood(x, y, diag, type, 0)); type = 1; if (!isGood(x, y, diag, type, 1)) return false; for (int k = 1; k < 3 * s; k++) { x += deltax[2 * type + diag]; y += deltay[2 * type + diag]; if (k % s == 0) type++; if (!isGood(x, y, diag, type, (k % s == 0))) { return false; } } return true; } int main() { int T; cin >> T; for (int t = 0; t < T; t++) { cin >> n >> m; for (int i = 0; i < n; i++) { string line; cin >> line; for (int j = 0; j < m; j++) grid[i][j] = line[j] - '0'; } int nsquares = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (grid[i][j] == 1) { if (isSquare(i, j, 0) || isSquare(i, j, 1)) nsquares++; } cout << nsquares << endl; } return 0; }
7
#include <bits/stdc++.h> using namespace std; int main(void) { int n; long long v[100100]; cin >> n; for (int i = 0; i < n; i++) cin >> v[i]; if (n <= 2) goto end; for (int i = 1; i < n - 1; i++) { if ((v[i + 1] - v[i]) * (long long)(v[i] - v[0]) < 0L) { printf("3\n%d %d %d\n", 1, i + 1, i + 2); goto end2; } } end:; printf("0"); end2:; return 0; }
5
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v); template <typename A, typename B> ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.first << ", " << p.second << ")"; } template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) cout << ", "; cout << v[i]; } return cout << "]"; } template <typename A, typename B> istream &operator>>(istream &cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; } mt19937 rng(steady_clock::now().time_since_epoch().count()); void usaco(string filename) { freopen((filename + ".in").c_str(), "r", stdin); freopen((filename + ".out").c_str(), "w", stdout); } const long double pi = 3.14159265358979323846; const long long mod = 1000000007; long long n, m, k, q, Q, T, l, r, x, y, z; long long a[1000005]; long long b[1000005]; long long c[1000005]; string second, t; long long ans = 0; long long dp[52][20005]; long long mat[52][20005]; long long pre[52][20005]; long long pmax[52][20005]; long long smax[52][20005]; pair<long long, long long> isect(pair<long long, long long> a, pair<long long, long long> b) { return make_pair(max(a.first, b.first), min(a.second, b.second)); } long long gsum(int r, int c1, int c2) { long long v = pre[r][c2]; if (c1 > 0) v -= pre[r][c1 - 1]; return v; } long long rec(int r, int c) { if (r >= n || c >= m - k + 1) return 0; if (dp[r][c] != -1) return dp[r][c]; long long second = gsum(r, c, c + k - 1); long long s2; if (r == n - 1) s2 = 0; else s2 = gsum(r + 1, c, c + k - 1); if (r == 0) { return dp[r][c] = second + s2; } long long lpos = max(c - k + 1, 0LL); long long rpos = min(c + k - 1, m - k); if (lpos != 0) dp[r][c] = max(dp[r][c], second + s2 + pmax[r - 1][lpos - 1]); if (rpos != m - k) dp[r][c] = max(dp[r][c], second + s2 + smax[r - 1][rpos + 1]); for (long long i = (lpos); i < (rpos + 1); i++) { pair<long long, long long> iv = isect(make_pair(i, i + k - 1), make_pair(c, c + k - 1)); long long loss = 0; if (iv.first <= iv.second) loss = gsum(r, iv.first, iv.second); dp[r][c] = max(dp[r][c], second + s2 - loss + dp[r - 1][i]); } return dp[r][c]; } int main() { { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }; cin >> n >> m >> k; for (long long i = 0; i < (n); i++) for (int ele = 0; ele < m; ele++) cin >> mat[i][ele]; ; for (long long i = 0; i < (n); i++) { long long run = 0; for (long long j = 0; j < (m); j++) { run += mat[i][j]; pre[i][j] = run; } } for (long long i = 0; i < (n); i++) for (long long j = 0; j < (m); j++) dp[i][j] = -1; for (long long i = 0; i < (n); i++) { for (long long j = 0; j < (m - k + 1); j++) ans = max(ans, rec(i, j)); long long run = 0; for (long long j = 0; j < (m - k + 1); j++) { run = max(run, dp[i][j]); pmax[i][j] = run; } run = 0; for (long long j = (m - k); j >= 0; j--) { run = max(run, dp[i][j]); smax[i][j] = run; } } cout << ans << endl; }
7
#include <bits/stdc++.h> #pragma comment(linker, "/STACK: 1024000000,1024000000") using namespace std; const int MOD = 1e9 + 7; const int N = 55; int n, T; int f[N][N][N][3], f0[N][N * N], f12[N][N][N * N], t0, t12, ct[3]; void add(int& x, int y) { x += y; if (x >= MOD) x -= MOD; } int main() { scanf("%d%d", &n, &T); f0[0][0] = f12[0][0][0] = 1; t0 = t12 = 0; for (int o = 0, x, y; o < n; o++) { scanf("%d%d", &x, &y); --y; if (!y) { for (int i = t0; i >= 0; i--) for (int j = ct[0]; j >= 0; j--) add(f0[j + 1][i + x], f0[j][i]); t0 += x; } else { for (int i = t12; i >= 0; i--) for (int j = ct[1]; j >= 0; j--) for (int k = ct[2]; k >= 0; k--) add(f12[j + (y == 1)][k + (y == 2)][i + x], f12[j][k][i]); t12 += x; } ct[y]++; } f[1][0][0][0] = f[0][1][0][1] = f[0][0][1][2] = 1; for (int i = 0; i <= ct[0]; i++) for (int j = 0; j <= ct[1]; j++) for (int k = 0; k <= ct[2]; k++) { if (i + j + k <= 1) continue; if (i) f[i][j][k][0] = 1ll * i * (f[i - 1][j][k][1] + f[i - 1][j][k][2]) % MOD; if (j) f[i][j][k][1] = 1ll * j * (f[i][j - 1][k][0] + f[i][j - 1][k][2]) % MOD; if (k) f[i][j][k][2] = 1ll * k * (f[i][j][k - 1][0] + f[i][j][k - 1][1]) % MOD; } int ans = 0; for (int t = 0; t <= T && t <= t0; t++) for (int i = 0; i <= ct[0]; i++) { if (!f0[i][t]) continue; for (int j = 0; j <= ct[1]; j++) for (int k = 0; k <= ct[2]; k++) { if (!f12[j][k][T - t]) continue; add(ans, (1ll * f[i][j][k][0] + f[i][j][k][1] + f[i][j][k][2]) % MOD * f0[i][t] % MOD * f12[j][k][T - t] % MOD); } } printf("%d\n", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; int n; int get(int i) { cout << "? " << i << endl; int ans; cin >> ans; return ans; } int pa(int i) { int l = get(i), r = get(i + n); if (l == r) { cout << "! " << i << endl; exit(0); } if (l > r) return -1; return 1; } int main() { cin >> n; n /= 2; if (n % 2 == 1) { cout << "! -1" << endl; return 0; } int prev = pa(1); int lo = 1, hi = 1 + n; while (lo + 1 < hi) { int mi = (lo + hi) / 2; int nx = pa(mi); if (nx == prev) lo = mi; else hi = mi; } assert(false); }
6
#include <bits/stdc++.h> using namespace std; int Num, Head[300010], Vet[700010], Next[700010]; int n, m, q; int f[300010], M[300010][2]; int a[300010][2], b[300010][2]; int h, Re[300010]; bool vis[300010]; void Add(int u, int v) { Num++; Vet[Num] = v; Next[Num] = Head[u]; Head[u] = Num; } int Find(int u) { if (f[u] != u) f[u] = Find(f[u]); return f[u]; } void Merge(int u, int v) { int f1 = Find(u), f2 = Find(v); if (f1 == f2) return; if (M[f1][0] < M[f2][0]) swap(f1, f2); f[f2] = f1; if (M[f1][0] > M[f2][0]) { M[f1][1] = max(M[f1][1], M[f2][0] + 1); return; } M[f1][1] = M[f1][0]; M[f1][0] = M[f2][0] + 1; } void dfs(int u) { vis[u] = true; for (int e = Head[u]; e != -1; e = Next[e]) { int v = Vet[e]; if (vis[v]) continue; dfs(v); if (a[u][0] < a[v][0]) { a[u][1] = a[u][0]; b[u][1] = b[u][1]; a[u][0] = a[v][0]; b[u][0] = v; } else if (a[u][1] < a[v][0]) { a[u][1] = a[v][0]; b[u][1] = v; } } a[u][0]++; a[u][1]++; } void dfs2(int u) { h++; Re[h] = u; vis[u] = true; for (int e = Head[u]; e != -1; e = Next[e]) { int v = Vet[e]; if (vis[v]) continue; if (v != b[u][0]) { if (a[v][0] <= a[u][0] + 1) { a[v][1] = a[v][0]; b[v][1] = b[v][0]; a[v][0] = a[u][0] + 1; b[v][0] = u; } else if (a[v][1] <= a[u][0] + 1) { a[v][1] = a[u][0] + 1; b[v][1] = u; } } else { if (a[v][0] <= a[u][1] + 1) { a[v][1] = a[v][0]; b[v][1] = b[v][0]; a[v][0] = a[u][1] + 1; b[v][0] = u; } else if (a[v][1] <= a[u][1] + 1) { a[v][1] = a[u][1] + 1; b[v][1] = u; } } } for (int e = Head[u]; e != -1; e = Next[e]) { int v = Vet[e]; if (!vis[v]) dfs2(v); } } int main() { ios_base::sync_with_stdio(false); Num = -1; memset(Head, -1, sizeof Head); scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); Add(u, v); Add(v, u); } memset(vis, false, sizeof vis); for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i); memset(vis, false, sizeof vis); for (int i = 1; i <= n; i++) if (!vis[i]) { h = 0; dfs2(i); int k = Re[1]; for (int j = 2; j <= h; j++) if (a[k][0] > a[Re[j]][0]) k = Re[j]; for (int j = 1; j <= h; j++) f[Re[j]] = k; M[k][0] = a[k][0] - 1, M[k][1] = a[k][1] - 1; } for (int i = 1; i <= q; i++) { int k; scanf("%d", &k); if (k == 1) { int u, fu; scanf("%d", &u); fu = Find(u); printf("%d\n", M[fu][0] + M[fu][1]); } else { int u, v; scanf("%d%d", &u, &v); Merge(u, v); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; long long c[N]; char a[N]; void add(int x, int y) { for (; x < N; x += x & -x) c[x] += y; } long long ask(int x) { long long ans = 0; for (; x > 0; x -= x & -x) ans += c[x]; return ans; } queue<int> pos[30]; int main() { int n; cin >> n; cin >> a + 1; long long ans = 0; for (int i = 1; i <= n; i++) { pos[a[i] - 'a'].push(i); add(i, 1); } for (int i = 1; i <= n; i++) { ans += ask(pos[a[n - i + 1] - 'a'].front()) - 1; add(pos[a[n - i + 1] - 'a'].front(), -1); pos[a[n - i + 1] - 'a'].pop(); } cout << ans; }
5
#include <bits/stdc++.h> using namespace std; template <class T> inline T Max(T a, T b) { return a > b ? a : b; } template <class T> inline T Min(T a, T b) { return a < b ? a : b; } inline long long LAbs(long long x) { return x > 0 ? x : -x; } inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } const int N = 3e5 + 10; int st[26]; int way[9][26] = {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, {0, 1, 23, 3, 21, 5, 2, 7, 4, 9, 6, 11, 8, 13, 14, 15, 16, 18, 20, 17, 19, 12, 22, 10, 24}, {0, 1, 6, 3, 8, 5, 10, 7, 12, 9, 23, 11, 21, 13, 14, 15, 16, 19, 17, 20, 18, 4, 22, 2, 24}, {0, 1, 2, 17, 19, 6, 8, 5, 7, 14, 16, 11, 12, 13, 4, 15, 3, 10, 18, 9, 20, 21, 22, 23, 24}, {0, 1, 2, 16, 14, 7, 5, 8, 6, 19, 17, 11, 12, 13, 9, 15, 10, 3, 18, 4, 20, 21, 22, 23, 24}, {0, 1, 2, 3, 4, 5, 6, 15, 16, 9, 10, 11, 12, 13, 14, 23, 24, 17, 18, 7, 8, 21, 22, 19, 20}, {0, 1, 2, 3, 4, 5, 6, 19, 20, 9, 10, 11, 12, 13, 14, 7, 8, 17, 18, 23, 24, 21, 22, 15, 16}}; int vis[26]; int main() { for (int i = 1; i <= 24; i++) cin >> st[i]; for (int w = 1; w <= 6; w++) { int flag = 0; for (int i = 1; i <= 6; i++) { for (int j = 1; j <= 4; j++) { int tj = (i - 1) * 4 + j, ti = (i - 1) * 4 + 1; if (st[way[w][tj]] != st[way[w][ti]]) { flag = 1; break; } } if (flag) break; } if (!flag) { cout << "YES\n"; return 0; } } cout << "NO\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <class X, class Y> void amin(X& x, const Y& y) { if (x > y) { x = y; } } template <class X, class Y> void amax(X& x, const Y& y) { if (x < y) { x = y; } } const int INF = 0x3f3f3f3f; const long long INFL = 0x3f3f3f3f3f3f3f3fLL; void process() { long long a, b; cin >> a >> b; if (b - a >= 10) cout << 0; else { long long res = 1; for (long long i = a + 1; i <= b; i++) { res = (res * (i % 10)) % 10; } cout << res; } } void init() {} int main() { ios_base::sync_with_stdio(false); init(); process(); }
1
#include <bits/stdc++.h> template <typename Elem> int quick_power(int a, Elem b, int Mod) { int ans = 1; while (b) { if (b & 1) { ans = 1ll * ans * a % Mod; } b >>= 1; a = 1ll * a * a % Mod; } return ans; } const int Maxn = 100000; const int Mod = 998244353; int n; long long sum_0[Maxn + 5]; int sum_1[Maxn + 5], sum_2[Maxn + 5], sum_3[Maxn + 5]; int f[Maxn + 5]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { int x, a; scanf("%d%d", &x, &a); int s_1 = 1ll * x * a % Mod, s_2 = 1ll * x * x % Mod * a % Mod, s_3 = 1ll * x * x % Mod * a % Mod * (a - 1) % Mod; for (int j = 1; j * j <= x; j++) { if (x % j == 0) { sum_3[j] = (sum_3[j] + s_3 + 2ll * s_1 * sum_1[j]) % Mod; sum_2[j] = (sum_2[j] + s_2) % Mod; sum_1[j] = (sum_1[j] + s_1) % Mod; sum_0[j] = (sum_0[j] + a); if (j * j != x) { sum_3[x / j] = (sum_3[x / j] + s_3 + 2ll * s_1 * sum_1[x / j]) % Mod; sum_2[x / j] = (sum_2[x / j] + s_2) % Mod; sum_1[x / j] = (sum_1[x / j] + s_1) % Mod; sum_0[x / j] = (sum_0[x / j] + a); } } } } for (int i = Maxn; i > 0; i--) { if (sum_0[i] == 0) { continue; } int p_2 = 0, p_3 = 0; if (sum_0[i] > 2) { p_3 = quick_power(2, sum_0[i] - 3, Mod); p_2 = (p_3 << 1) % Mod; } else if (sum_0[i] > 1) { p_2 = quick_power(2, sum_0[i] - 2, Mod); } if (sum_0[i] > 1) { f[i] = (f[i] + 1ll * p_2 * ((sum_0[i] - 1) % Mod) % Mod * sum_2[i]) % Mod; f[i] = (f[i] + 1ll * p_2 * sum_3[i]) % Mod; } if (sum_0[i] > 2) { f[i] = (f[i] + 1ll * p_3 * ((sum_0[i] - 2) % Mod) % Mod * sum_3[i]) % Mod; } for (int j = i + i; j <= Maxn; j += i) { f[i] = (f[i] - f[j] + Mod) % Mod; } } printf("%d\n", f[1]); return 0; }
10
#include <bits/stdc++.h> using namespace std; int n, p[1005], dp[1005], sol; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> p[i]; dp[0] = 2; for (int i = 1; i < n; i++) { int ans = 2; for (int j = p[i] - 1; j < i; j++) ans = (ans + dp[j]) % 1000000007; dp[i] = ans; } for (int i = 0; i < n; i++) sol = (sol + dp[i]) % 1000000007; cout << sol; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { long long x, y; cin >> x >> y; if (x > y) { cout << x + y << endl; } else if (x == y) { cout << x << endl; } else { cout << y - ((x + y) / 2) % x << endl; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s; cin >> t; int i, n; n = s.length(); for (i = 0; i < n / 2; i++) { swap(s[i], s[n - i - 1]); } if (s == t) cout << "YES"; else cout << "NO"; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, p; scanf("%d %d", &n, &p); long long sum = 0; long long ans = 0; int a[100005]; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum += a[i]; } long long temp = 0; for (int j = 0; j < n - 1; j++) { temp += a[j]; ans = max(ans, temp % p + (sum - temp) % p); } printf("%lld\n", ans); }
2
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 10; const int SZ = 1e6 + 10; const int mod = 1e9 + 7; const double PI = acos(-1); const double eps = 1e-7; long long read() { long long n = 0; char a = getchar(); bool flag = 0; while (a > '9' || a < '0') { if (a == '-') flag = 1; a = getchar(); } while (a <= '9' && a >= '0') { n = n * 10 + a - '0', a = getchar(); } if (flag) n = -n; return n; } int edges1[15][15][17010]; vector<int> nodes[17010]; vector<pair<int, int> > G[22]; bool vis[17010]; int f[17010], g[15][15][17010]; int to[15][17010], n, m, trans[17010]; void print(int S) { for (int i = 0; i < n; i++) if (S >> i & 1) printf("%d,", i + 1); } void dfs(int S) { if (vis[S]) return; vis[S] = 1; int ans = 1e9, e1 = 0, e2 = 0; for (int S1 = (S - 1) & S; S1 > 0; S1 = (S1 - 1) & S) { int S2 = S ^ S1; dfs(S1); for (int i = 0; i < n; i++) { if ((S2 >> i & 1) == 0) continue; if (!to[i][S1]) continue; for (int j = 0; j < n; j++) { if ((S2 >> j & 1) == 0) continue; if (!to[j][S1]) continue; if (!g[i][j][S2]) continue; int tmp = f[S1] + g[i][j][S2] + 2; if (tmp < ans) { ans = tmp; trans[S] = S1; e1 = i; e2 = j; } } } } for (int i = 0; i < n; i++) { if (S >> i & 1) { int S1 = S ^ (1 << i); int t1 = 0, t2 = 0; for (pair<int, int> p : G[i]) { int v = p.first; if (S1 >> v & 1) { if (!t1) t1 = p.second; else t2 = p.second; } } if (t2 == 0) continue; int tmp = f[S1] + 2; if (tmp < ans) ans = tmp, e1 = i, e2 = i, trans[S] = S1; } } f[S] = ans; nodes[S].push_back(e1); nodes[S].push_back(e2); } int a[SZ], b[SZ]; int pre[15][15][17010]; int main() { n = read(), m = read(); for (int i = 1; i <= m; i++) { int x = read() - 1, y = read() - 1; g[x][y][(1 << x) | (1 << y)] = 1; g[y][x][(1 << x) | (1 << y)] = 1; edges1[x][y][(1 << x) | (1 << y)] = i; edges1[y][x][(1 << x) | (1 << y)] = i; G[x].push_back(make_pair(y, i)); G[y].push_back(make_pair(x, i)); a[i] = x; b[i] = y; } vis[0] = 1; for (int i = 0; i < n; i++) { f[1 << i] = 0, vis[1 << i] = 1; for (int S = 0; S < (1 << n); S++) { for (pair<int, int> p : G[i]) { if (S >> p.first & 1) { to[i][S] = p.second; break; } } } } for (int S = 0; S < (1 << n); S++) { for (int i = 0; i < n; i++) { if ((S >> i & 1) == 0) continue; for (int j = 0; j < n; j++) { if ((S >> j & 1) == 0) continue; if (!g[i][j][S]) continue; for (pair<int, int> p : G[j]) { int x = p.first; if (S >> x & 1) continue; if (x == i) continue; if (!g[i][x][S ^ (1 << x)]) { g[i][x][S ^ (1 << x)] = g[i][j][S] + 1; pre[i][x][S ^ (1 << x)] = j; edges1[i][x][S ^ (1 << x)] = p.second; } } } } } dfs((1 << n) - 1); printf("%d\n", f[(1 << n) - 1]); int S = (1 << n) - 1; while (__builtin_popcount(S) > 1) { int S2 = S ^ trans[S], S1 = trans[S]; int x = nodes[S][0]; int y = nodes[S][1]; if (x != y) { printf("%d %d\n", a[to[x][S1]] + 1, b[to[x][S1]] + 1); printf("%d %d\n", a[to[y][S1]] + 1, b[to[y][S1]] + 1); while (__builtin_popcount(S2) >= 2) { int v = edges1[x][y][S2]; printf("%d %d\n", a[v] + 1, b[v] + 1); int p = pre[x][y][S2]; S2 ^= 1 << y; y = p; } S = S1; } else { int t = 0; for (pair<int, int> p : G[x]) { if (S1 >> p.first & 1) { printf("%d %d\n", a[p.second] + 1, b[p.second] + 1); if (++t == 2) break; } } S = S1; } } }
10
#include <bits/stdc++.h> using namespace std; int n, c[210], p[210], d[210]; bool b[210], a[210][210]; int find(int k) { int i; for (i = 1; i <= n; i++) { if (c[i] == k) { if (d[i] == 0 && b[i]) { return i; } } } return n + 1; } int calc(int k) { memset(b, true, sizeof(b)); for (int i = 1; i <= n; i++) d[i] = p[i]; int ret = 0; for (int z = 1; z <= n; z++) { int x = find(k); while (x > n) { k++; if (k == 4) k = 1; ret++; x = find(k); } b[x] = false; ret++; for (int i = 1; i <= n; i++) if (a[i][x]) d[i]--; } return ret; } int main() { int i, j; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &c[i]); memset(a, false, sizeof(a)); for (i = 1; i <= n; i++) { scanf("%d", &p[i]); for (j = 1; j <= p[i]; j++) { int y; scanf("%d", &y); a[i][y] = true; } } int ans = calc(1); int temp; temp = calc(2); if (temp < ans) ans = temp; temp = calc(3); if (temp < ans) ans = temp; printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, counter = 0, i, m; cin >> n; char ch1[200000], ch2[200000]; scanf("%s", ch1); for (i = 0; i < n; i += 2) { int counta = 0, countb = 0; if (ch1[i] == 'a' || ch1[i + 1] == 'a') counta++; if (ch1[i] == 'b' || ch1[i + 1] == 'b') countb++; if (counta != countb) { counter++; if (counta > 1) { ch2[i] = 'b'; ch2[i + 1] = 'a'; } else { ch2[i] = 'a'; ch2[i + 1] = 'b'; } } else { ch2[i] = ch1[i]; ch2[i + 1] = ch1[i + 1]; } } printf("%d\n", counter); for (i = 0; i < n; i++) { printf("%c", ch2[i]); } printf("\n"); return 0; }
0
#include <bits/stdc++.h> const int MAX_N = 300000; const int MOD = 998244353; int v[1 + MAX_N], sp[1 + MAX_N]; int lgput(int b, int e) { int ac = 1; while (e > 0) { if (e % 2 == 1) ac = (long long)ac * b % MOD; b = (long long)b * b % MOD; e /= 2; } return ac; } int main() { int N; scanf("%d", &N); for (int i = 1; i <= N; ++i) scanf("%d", &v[i]); std::sort(v + 1, v + 1 + N); for (int i = 1; i <= N; ++i) sp[i] = (sp[i - 1] + v[i]) % MOD; int invn = lgput(N, MOD - 2); for (int k = 1; k <= N; ++k) { int rem = (N - k) % k; int sizecomplete = (N - k) / k; int last = 0, coef = 0; if (rem > 0) ++coef; coef += sizecomplete; int res = 0; if (rem > 0) { res = ((long long)sp[rem] * coef + res) % MOD; last += rem; coef--; } while (coef > 0) { res = ((long long)(sp[last + k] + MOD - sp[last]) * coef + res) % MOD; last += k; --coef; } printf("%d ", (long long)res * invn % MOD); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int n, l; cin >> n >> l; int *mas = new int[n]; for (int i = 0; i < n; i++) { cin >> mas[i]; } double r = 0; sort(mas, mas + n); for (int i = 0; i < n - 1; i++) { if (mas[i + 1] - mas[i] > r) r = mas[i + 1] - mas[i]; } r = (double)r / 2; if (l - mas[n - 1] > r) r = l - mas[n - 1]; if (mas[0] > r) r = mas[0]; cout << fixed << setprecision(10); cout << r; }
2
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x) { char c; bool nega = 0; while ((!isdigit(c = getchar())) && (c != '-')) ; if (c == '-') { nega = 1; c = getchar(); } x = c - 48; while (isdigit(c = getchar())) x = x * 10 + c - 48; if (nega) x = -x; } template <typename T> inline void writep(T x) { if (x > 9) writep(x / 10); putchar(x % 10 + 48); } template <typename T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } writep(x); putchar(' '); } template <typename T> inline void writeln(T x) { write(x); putchar('\n'); } template <typename T> inline void Min(T &a, T b) { if (a > b) a = b; } template <typename T> inline void Max(T &a, T b) { if (a < b) a = b; } int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); int x = 0; bool y = 1; int ans = 0; char xx; while (cin >> xx) { if (xx == '+' || xx == '-') { if (y) ans += x; else ans -= x; x = 0; y = (xx == '+'); } x = x * 10 + (xx - '0'); } if (y) ans += x; else ans -= x; cout << ans; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<int>> g(n); vector<pair<int, int>> weight(n); vector<int> w(n); for (int i = 0; i < n; ++i) { cin >> weight[i].first; w[i] = weight[i].first; weight[i].second = i; } for (int i = 0; i < m; ++i) { int x, y; cin >> x >> y; x--; y--; g[x].push_back(y); g[y].push_back(x); } sort(weight.begin(), weight.end()); reverse(weight.begin(), weight.end()); long sum = 0; for (int i = 0; i < n; ++i) { int v = weight[i].second; for (size_t j = 0; j < g[v].size(); ++j) { int to = g[v][j]; sum += w[to]; g[to].erase(find(g[to].begin(), g[to].end(), v)); } } cout << sum; return 0; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll maxn = 1e6; const ll mod = 1e9 + 7; ll fact[3 * maxn + 10]; ll inv_fact[3 * maxn + 10]; ll qpow(ll a, ll p) { ll res = 1; while (p) { if (p & 1) res = res * a % mod; a = a * a % mod; p >>= 1; } return res; } ll binom(int n, int m) { if (m > n) return 0; return fact[n] * inv_fact[m] % mod * inv_fact[n - m] % mod; } void init() { fact[0] = 1; for (int i = 1; i <= 3 * maxn; i++) fact[i] = (fact[i - 1] * i) % mod; inv_fact[3 * maxn] = qpow(fact[3 * maxn], mod - 2); for (int i = 3 * maxn - 1; i >= 0; i--) inv_fact[i] = inv_fact[i + 1] * (i + 1) % mod; } ll n; ll dp[3 * maxn + 5][3]; int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); init(); ll inv3 = qpow(3, mod - 2); cin >> n; dp[0][0] = dp[0][1] = dp[0][2] = n; for (ll i = 1; i <= 3 * n; i++) { dp[i][0] = (binom(3 * n, i + 1) + mod - (2 * dp[i - 1][0] % mod + dp[i - 1][1]) % mod) % mod * inv3 % mod; dp[i][1] = (dp[i][0] + dp[i - 1][0]) % mod; dp[i][2] = (dp[i][1] + dp[i - 1][1]) % mod; } ll q; cin >> q; while (q--) { ll x; cin >> x; cout << (dp[x][0] + binom(3 * n, x)) % mod << "\n"; } return 0; }
8
#include <bits/stdc++.h> using namespace std; inline int read() { register int res = 0, c; while (c = getchar(), c < '0' || c > '9') ; do { res = (res * 10) + (c ^ 48); } while (c = getchar(), c >= '0' && c <= '9'); return res; } int s[300010], dp[300010]; map<int, int> R[300010]; int main() { int Q = read(); while (Q--) { long long ans = 0; int N = read(); for (int i = 1; i <= N + 2; i++) { if (i <= N) s[i] = read(); dp[i] = 0; R[i].clear(); } for (int i = N; i; i--) { if (R[i + 1][s[i]]) { int pos = R[i + 1][s[i]] + 1; R[i].swap(R[pos]); if (pos < N) { R[i][s[pos]] = pos; } dp[i] = dp[pos] + 1; ans += dp[i]; } R[i][s[i]] = i; } if (ans < 2e9) printf("%d\n", (int)ans); else printf("%lld\n", ans); } }
9
#include <bits/stdc++.h> using namespace std; bool primes[1000000 + 1]; void sieve(long long int n) { primes[0] = false; primes[1] = false; for (long long int i{2}; i <= n; i++) { primes[i] = true; } for (long long int i{2}; i <= sqrt(n); i++) { if (primes[i] == true) { for (long long int j{2}; j * i <= n; j++) { primes[i * j] = false; } } } } bool comp(pair<int, int> &a, pair<int, int> &b) { return (a.first < b.first); } long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } long long int lcm(long long int a, long long int b) { return a * b / gcd(a, b); } long long int fastpow(long long base, long long n) { if (n == 0) return 1; if (n == 1) return base; long long halfn = fastpow(base, n / 2); if (n % 2 == 0) return (halfn * halfn); else return (((halfn * halfn)) * base); } void bfs(int s, vector<int> v[], bool vis[]) { queue<int> q; q.push(s); vis[s] = true; while (!q.empty()) { int a = q.front(); q.pop(); for (auto x : v[a]) { if (!vis[x]) { q.push(x); vis[x] = true; } } } } void dfs(int s, vector<int> v[], bool vis[]) { vis[s] = true; for (auto x : v[s]) { if (!vis[x]) { dfs(x, v, vis); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m, k, t; cin >> n >> m >> k >> t; vector<pair<long long int, long long int>> v; for (long long int i = 0; i < k; i++) { long long int x, y; cin >> x >> y; v.push_back(make_pair(x, y)); } sort(v.begin(), v.end()); while (t--) { long long int x, y; cin >> x >> y; long long int i = 0; bool flag = false; while (v[i] <= make_pair(x, y) && i < k) { if (v[i].first == x && v[i].second == y) { cout << "Waste" << "\n"; flag = true; break; } i++; } if (!flag) { long long int ans = ((x - 1) * m) + (y - 1) - i; ans = ans % 3; if (ans == 0) { cout << "Carrots" << "\n"; } else if (ans == 1) { cout << "Kiwis" << "\n"; } else { cout << "Grapes" << "\n"; } } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long int dp[1000100]; int main() { int n; scanf("%d", &n); int i, j, k, curr; long long int ans = 0; dp[0] = 1; for (i = 0; i < n; i++) { scanf("%d", &curr); vector<int> vi; for (j = 1; j * j <= curr; j++) { if (curr % j == 0) { vi.push_back(j); if (j * j == curr) continue; vi.push_back(curr / j); } } sort(vi.begin(), vi.end(), greater<int>()); for (j = 0; j < vi.size(); j++) { dp[vi[j]] += dp[vi[j] - 1]; dp[vi[j]] = dp[vi[j]] % mod; } } for (i = 1; i <= n; i++) { ans += dp[i]; ans = ans % mod; } printf("%lld\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t-- > 0) { long long int a, b, c; cin >> a >> b >> c; long long int dif = abs(a - b); long long int total = 2 * dif; if (total < c || total < a || total < b) { cout << -1 << endl; continue; } if (c + dif > total) { cout << abs(c - dif) << endl; } else { cout << c + dif << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long arrAns[36] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8092, 16184, 32368, 64736, 129472, 258944, 517888, 1035776, 2071552, 4143104, 8286208, 16572416, 33144832, 66289664, 132579328, 265158656, 530317312, 1060634624, 2121269248, 4242538496, 8485076992, 16970153984, 33940307968}; int main() { int n; cin >> n; cout << arrAns[n] << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAXn = 1000 * 100; int h[MAXn + 5], Max[MAXn + 5]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> h[i]; Max[n - 1] = h[n - 1]; for (int i = n - 2; i >= 0; i--) Max[i] = max(Max[i + 1], h[i]); for (int i = 0; i < n - 1; i++) cout << max(Max[i + 1] + 1 - h[i], 0) << " "; cout << 0 << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int a[600010]; const int N = 6e5 + 9; struct stree { int l, r, sum, lazy; } t[N << 2]; inline void build(int now, int l, int r) { t[now].l = l, t[now].r = r; if (l == r) { t[now].sum = a[l], t[now].lazy = 0; return; } int mid = (l + r) / 2; build(now * 2, l, mid), build(now * 2 + 1, mid + 1, r); t[now].sum = min(t[now * 2].sum, t[now * 2 + 1].sum); } inline void pushdown(int now) { int lazy = t[now].lazy; if (lazy == 0) return; t[now].sum += lazy; if (t[now].l != t[now].r) t[now * 2].lazy += lazy, t[now * 2 + 1].lazy += lazy; t[now].lazy = 0; } inline void change(int now, int l, int r, int d) { if (t[now].l == l && t[now].r == r) { t[now].lazy += d; return; } pushdown(now); if (t[now * 2].r >= r) change(now * 2, l, r, d); else if (t[now * 2 + 1].l <= l) change(now * 2 + 1, l, r, d); else change(now * 2, l, t[now * 2].r, d), change(now * 2 + 1, t[now * 2 + 1].l, r, d); if (t[now].l != t[now].r) pushdown(now * 2), pushdown(now * 2 + 1), t[now].sum = min(t[now * 2].sum, t[now * 2 + 1].sum); } inline int query(int now, int l, int r) { pushdown(now); if (t[now].l == l && t[now].r == r) return t[now].sum; if (t[now * 2].r >= r) return query(now * 2, l, r); else if (t[now * 2 + 1].l <= l) return query(now * 2 + 1, l, r); else return min(query(now * 2, l, t[now * 2].r), query(now * 2 + 1, t[now * 2 + 1].l, r)); } multiset<int> s; set<pair<int, int> > s2; int main() { int n, k, m; cin >> n >> k >> m; for (int i = 1; i <= N; i++) a[i] = n - i; build(1, 1, N); while (m--) { int x, y; cin >> x >> y; if (s2.find({x, y}) != s2.end()) { s2.erase({x, y}); x = max(x, k) - min(x, k) + y; change(1, 1, x, 1); s.erase(s.find(x)); } else { s2.insert({x, y}); x = max(x, k) - min(x, k) + y; change(1, 1, x, -1); s.insert(x); } if (s.empty()) puts("0"); else cout << max(1, -query(1, 1, *(--s.end()))) - 1 << "\n"; } return 0; }
9
#include <bits/stdc++.h> using namespace std; inline void proc_status() { ifstream t("/proc/self/status"); cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl; } template <typename T> inline int chkmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <typename T> inline int chkmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; } template <typename T> inline T sqr(const T &val) { return val * val; } namespace fastIO { const int MAX_BUFFER_SIZE = 1 << 16; char buffer[MAX_BUFFER_SIZE], *cur = buffer, *ed = buffer; inline char getc() { return *(cur == ed ? ed = buffer + fread(buffer, 1, MAX_BUFFER_SIZE, stdin), cur = buffer : cur)++; } } // namespace fastIO using fastIO::getc; template <typename T> inline T read() { register T sum(0), fg(1); register char ch(getc()); for (; !isdigit(ch); ch = getc()) if (ch == '-') fg = -1; for (; isdigit(ch); ch = getc()) sum = sum * 10 - '0' + ch; return sum * fg; } inline char *read_str(char *s) { register char ch(getc()); while (!isgraph(ch)) ch = getc(); for (; isgraph(ch); ch = getc()) *s++ = ch; *s = '\0'; return s; } const int MAXN = (int)1e5; int n, Q; int *s[MAXN + 5]; int len[MAXN + 5]; inline void input() { n = read<int>(), Q = read<int>(); for (int i = 1; i <= n; ++i) { static char s0[MAXN + 5]; len[i] = read_str(s0) - s0; s[i] = new int[len[i]]; for (int j = 0; j < len[i]; ++j) s[i][j] = s0[j] - 'a'; } } namespace DFA { const int MAX_NODE = MAXN, alpha = 26; int trans[MAX_NODE + 5][alpha], fail[MAX_NODE + 5], cnt = 0; int rt = ++cnt; inline int insert(int *s0, int N) { int u = rt; for (int i = 0; i < N; ++i) { int &v = trans[u][s0[i]]; if (!v) v = ++cnt; u = v; } return u; } inline void build_fail() { static queue<int> q; fail[rt] = 0; q.push(rt); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < alpha; ++i) { int &v = trans[u][i]; if (!v) v = fail[u] ? trans[fail[u]][i] : rt; else fail[v] = fail[u] ? trans[fail[u]][i] : rt, q.push(v); } } } vector<int> adj[MAX_NODE + 5]; int st[MAX_NODE + 5], ed[MAX_NODE + 5], dfn_cur = 0; inline void dfs(int u) { st[u] = ++dfn_cur; for (auto v : adj[u]) dfs(v); ed[u] = dfn_cur; } inline void build() { for (int i = 1; i <= cnt; ++i) if (fail[i]) adj[fail[i]].push_back(i); dfs(rt); } } // namespace DFA int nd[MAXN + 5]; struct query0 { int k, c, id; query0() {} query0(int _k, int _c, int _id) : k(_k), c(_c), id(_id) {} }; vector<query0> qry0[MAXN + 5]; struct query1 { int l, r, id; query1() {} query1(int _l, int _r, int _id) : l(_l), r(_r), id(_id) {} }; vector<query1> qry1[MAXN + 5]; long long ans[MAXN + 5]; struct BIT { int *S, N; inline void build(int _N) { N = _N, S = new int[N + 1]; for (int i = 1; i <= N; ++i) S[i] = 0; } inline void add(int p, int val) { for (; p <= N; p += p & -p) S[p] += val; } inline int prefix(int p) { int res = 0; for (; p > 0; p -= p & -p) res += S[p]; return res; } }; inline void sub0_solve(int LIM) { static BIT t; t.build(DFA::cnt); auto insert = [](int k) { using namespace DFA; int u = rt; for (int i = 0; i < len[k]; ++i) u = trans[u][s[k][i]]; t.add(st[u], +1); t.add(ed[u] + 1, -1); }; auto query = [](int k) { using namespace DFA; int u = rt; long long res = 0; for (int i = 0; i < len[k]; ++i) { u = trans[u][s[k][i]]; res += t.prefix(st[u]); } return res; }; for (int i = 1; i <= n; ++i) { if (len[i] <= LIM) insert(i); for (auto it : qry0[i]) ans[it.id] += it.c * query(it.k); } } inline void sub1_solve(int LIM) { static int sum[MAXN + 5]; function<void(int u)> dfs; dfs = [&dfs](int u) { using namespace DFA; for (auto v : adj[u]) { dfs(v); sum[u] += sum[v]; } }; auto build = [](int k) { using namespace DFA; for (int i = 1; i <= cnt; ++i) sum[i] = 0; int u = rt; for (int i = 0; i < len[k]; ++i) ++sum[u = trans[u][s[k][i]]]; }; for (int k = 1; k <= n; ++k) if (len[k] > LIM) { build(k); dfs(DFA::rt); static vector<pair<int, int> > qrys[MAXN + 5]; for (int i = 1; i <= n; ++i) qrys[i].clear(); for (auto it : qry1[k]) { qrys[it.l - 1].emplace_back(-1, it.id); qrys[it.r].emplace_back(+1, it.id); } long long S = 0; for (int i = 1; i <= n; ++i) { S += sum[nd[i]]; for (auto it : qrys[i]) ans[it.second] += it.first * S; } } } inline void solve() { for (int i = 1; i <= n; ++i) nd[i] = DFA::insert(s[i], len[i]); DFA::build_fail(); DFA::build(); int LIM = 0; for (int i = 1; i <= n; ++i) LIM += len[i]; LIM = (int)sqrt(LIM); for (int i = 1; i <= Q; ++i) { int l = read<int>(), r = read<int>(), k = read<int>(); if (len[k] <= LIM) { qry0[l - 1].emplace_back(k, -1, i); qry0[r].emplace_back(k, +1, i); } else qry1[k].emplace_back(l, r, i); } sub0_solve(LIM); sub1_solve(LIM); for (int i = 1; i <= Q; ++i) printf("%lld\n", ans[i]); } int main() { input(); solve(); return 0; }
11
#include <bits/stdc++.h> using namespace std; using namespace std; long long gcd(long long a, long long b) { if (a < b) return gcd(b, a); else if (b == 0) return a; else return gcd(b, a % b); } long long pow(long long b, long long e) { if (e == 0) return 1; else if (e % 2 == 0) { long long a = pow(b, e / 2); return a * a; } else { long long a = pow(b, e / 2); return b * a * a; } } long long pow_m(long long x, long long y, long long m = 1000000007) { x = x % m; long long res = 1; while (y) { if (y & 1) res = res * x; res %= m; y = y >> 1; x = x * x; x %= m; } return res; } long long modInverse(long long a, long long m) { return pow_m(a, m - 2, m); } long long max(long long a, long long b) { if (a >= b) return a; else return b; } long long min(long long a, long long b) { if (a <= b) return a; else return b; } long long bin_coff(long long n, long long k, long long m) { vector<vector<long long>> ans(n + 1, vector<long long>(k, 0)); for (long long i = 0; 0 < n + 1 ? i < n + 1 : i > n + 1; 0 < n + 1 ? i += 1 : i -= 1) { for (long long j = 0; 0 < min(i, k) + 1 ? j < min(i, k) + 1 : j > min(i, k) + 1; 0 < min(i, k) + 1 ? j += 1 : j -= 1) { if (j == 0 || j == i) ans[i][j] = 1; else ans[i][j] = ans[i - 1][j - 1] % m + ans[i - 1][j] % m; } } return ans[n][k] % m; } long long inverse(long long i) { if (i == 1) return 1; return (1000000007 - ((1000000007 / i) * inverse(1000000007 % i)) % 1000000007 + 1000000007) % 1000000007; } bool prime(long long n) { if (n == 1) return false; if (n <= 3) return true; if (n % 2 == 0) return false; for (long long i = 3; i <= sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } bool cmp(pair<char, long long> &a, pair<char, long long> &b) { return a.second > b.second; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long tt = 1; for (long long i = 0; 0 < tt ? i < tt : i > tt; 0 < tt ? i += 1 : i -= 1) { long long n, m, res = 0; cin >> n >> m; long long arr[m], b[m]; for (long long i = 0; i < m; i++) { cin >> arr[i]; b[i] = arr[i]; } long long maxx = 0, minn = 0, j = 0; sort(arr, arr + m); while (n--) { sort(b, b + m); minn += arr[j]; maxx += b[m - 1]; arr[j]--; b[m - 1]--; if (arr[j] == 0) j++; } cout << maxx << " " << minn << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 100005, maxs = (1 << 17) + 5, mod = 998244353, inv2 = (mod + 1) / 2; inline int gi() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int sum = 0; while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar(); return sum; } inline int add(int x, int y) { return x + y < mod ? x + y : x + y - mod; } inline int sub(int x, int y) { return x < y ? x - y + mod : x - y; } inline int fpow(int x, int k) { int res = 1; while (k) { if (k & 1) res = (long long)res * x % mod; k >>= 1; x = (long long)x * x % mod; } return res; } int m, n; long long x, y, z, f[maxs], g[maxs], h[maxs], a[maxn], b[maxn], c[maxn]; void fwt(long long *a) { for (int d = 0; d < n; ++d) for (int i = 0; i < (1 << n); i += 1 << (d + 1)) for (int j = 0; j < 1 << d; ++j) { long long x = a[i + j], y = a[i + (1 << d) + j]; a[i + j] = x + y; a[i + (1 << d) + j] = x - y; } } void ifwt(long long *a) { for (int d = 0; d < n; ++d) for (int i = 0; i < (1 << n); i += 1 << (d + 1)) for (int j = 0; j < 1 << d; ++j) { long long x = a[i + j], y = a[i + (1 << d) + j]; a[i + j] = (long long)(x + y) * inv2 % mod; a[i + (1 << d) + j] = (long long)(x - y + mod) * inv2 % mod; } } int main() { m = gi(); n = gi(); x = gi(); y = gi(); z = gi(); int sum = 0; for (int i = 1; i <= m; ++i) a[i] = gi(), b[i] = gi(), c[i] = gi(), ++f[a[i] ^ b[i]], ++g[a[i] ^ c[i]], ++h[b[i] ^ c[i]], sum ^= a[i]; fwt(f); fwt(g); fwt(h); int c1 = add(x, add(y, z)), c2 = add(x, sub(y, z)), c3 = add(x, sub(z, y)), c4 = sub(x, add(y, z)); int t1, t2, t3, t4; for (int i = 0; i < (1 << n); ++i) { t1 = (m + f[i] + g[i] + h[i]) / 4 % (mod - 1); t2 = (m + f[i] - t1 * 2) / 2 % (mod - 1); t3 = (m + g[i] - t1 * 2) / 2 % (mod - 1); t4 = (m + h[i] - t1 * 2) / 2 % (mod - 1); f[i] = (long long)fpow(c1, t1) * fpow(c2, t2) % mod * fpow(c3, t3) % mod * fpow(c4, t4) % mod; } ifwt(f); for (int i = 0; i < (1 << n); ++i) printf("%lld ", f[i ^ sum]); return 0; }
12
#include <bits/stdc++.h> using namespace std; struct edge { long long a, b, w; }; long long n, m; vector<vector<edge>> adjList; deque<long long> path; vector<bool> inPath; long long dfs(long long x, long long c = 0, long long p = -1) { if (c == x) { inPath[c] = true; path.push_front(c); return 0; } else { for (auto e : adjList[c]) { if (e.b != p) { long long d = dfs(x, e.b, c); if (d >= 0) { inPath[c] = true; path.push_front(c); return d + e.w; } } } return -1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; adjList = vector<vector<edge>>(n); for (long long i = 0; i < n - 1; i++) { long long a, b, w; cin >> a >> b >> w; a--; b--; adjList[a].push_back({a, b, w}); adjList[b].push_back({b, a, w}); } inPath = vector<bool>(n); long long dist = dfs(n - 1); bool ok = true; for (long long i = 0; i < n && ok; i++) { if (i == path[0] || i == path.back()) { ok &= adjList[i].size() <= 2; } else if (inPath[i]) { ok &= adjList[i].size() <= 3; } else { ok &= adjList[i].size() <= 1; } } long long offset; if (ok) { offset = 0; vector<long long> spike(path.size()); vector<long long> next(path.size() - 1); for (long long i = 0; i < path.size(); i++) { for (auto n : adjList[path[i]]) { if (!inPath[n.b]) { spike[i] = n.w; } else if (i + 1 < path.size() && n.b == path[i + 1]) { next[i] = n.w; } } } for (long long i = 0; i + 1 < path.size(); i++) { if (spike[i] > 0 || spike[i + 1] > 0) { offset = max(offset, dist - next[i] + spike[i] + spike[i + 1]); } } for (long long i = 0; i + 1 < next.size(); i++) { offset = max(offset, dist - next[i] - next[i + 1]); } } else { offset = dist; } for (long long i = 0; i < m; i++) { long long x; cin >> x; cout << min(dist, offset + x) << endl; } }
9
#include <bits/stdc++.h> using namespace std; int a[200010], n, S, T; vector<int> A; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", a + i), S += a[i]; for (int i = 0; i < n; i++) if (S - a[i] == (long long)a[i] * (n - 1)) A.push_back(i + 1); printf("%d\n", (int)A.size()); for (int i = 0; i < (int)A.size(); i++) printf("%d%c", A[i], i == (int)A.size() - 1 ? '\n' : ' '); return 0; }
2
#include <bits/stdc++.h> using namespace std; struct node { long long int cur; long long int lr; long long int e; long long int f; long long int rl; node() {} node(long long int cur, long long int lr, long long int e, long long int f, long long int rl) : cur(cur), lr(lr), e(e), f(f), rl(rl) {} void update(node &elem) { lr += elem.lr + cur - elem.cur; e += elem.e + cur - elem.cur; f += elem.f + cur - elem.cur; rl += elem.rl + cur - elem.cur; cur = elem.cur; return; } long long int get_maxi(long long int x) { long long int v1 = max(lr, rl); long long int v2 = max(-2 * x + e, 2 * x + f); return max(v1, v2); } }; const int MAXN = (int)1e5 + 5; const long long int INF = (long long int)1e16; int v[MAXN]; node tree[4 * MAXN]; long long int delta[MAXN]; node max(node &x, node &y) { return node(0, max(x.lr, y.lr), max(x.e, y.e), max(x.f, y.f), max(x.rl, y.rl)); } void init(int idx, int a, int b) { if (a == b) { long long int cur = abs(delta[a - 1]) + abs(delta[a]); long long int lr = v[a - 1] - v[a + 1]; long long int e = v[a - 1] + v[a + 1] - 2 * v[a]; long long int f = 2 * v[a] - v[a - 1] - v[a + 1]; long long int rl = v[a + 1] - v[a - 1]; tree[idx] = node(cur, lr - cur, e - cur, f - cur, rl - cur); return; } int mid = (a + b) / 2; init(idx * 2, a, mid); init(idx * 2 + 1, mid + 1, b); tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]); return; } node query(int idx, int a, int b, int l, int r) { if (b < l || a > r) { return node(0, -INF, -INF, -INF, -INF); } if (l <= a && b <= r) { return tree[idx]; } int mid = (a + b) / 2; node q1 = query(idx * 2, a, mid, l, r); node q2 = query(idx * 2 + 1, mid + 1, b, l, r); return max(q1, q2); } void update(int idx, int a, int b, int l, int r, node elem) { if (b < l || a > r) { return; } if (l <= a && b <= r) { tree[idx].update(elem); return; } int mid = (a + b) / 2; update(idx * 2, a, mid, l, r, elem); update(idx * 2 + 1, mid + 1, b, l, r, elem); tree[idx] = max(tree[idx * 2], tree[idx * 2 + 1]); return; } int main(void) { int n, q; int t, l, r, x; long long int sum = 0; scanf(" %d", &n); v[0] = v[n + 1] = 0; for (int i = 1; i <= n; i++) { scanf(" %d", &v[i]); } for (int i = 1; i < n; i++) { sum += abs(v[i] - v[i + 1]); delta[i] = v[i] - v[i + 1]; } init(1, 1, n); scanf(" %d", &q); while (q--) { scanf(" %d %d %d %d", &t, &l, &r, &x); if (t == 1) { long long int res = query(1, 1, n, l, r).get_maxi(x); printf("%lld\n", sum + res); } else { sum -= abs(delta[l - 1]); sum -= abs(delta[r]); delta[l - 1] -= x; delta[r] += x; sum += abs(delta[l - 1]); sum += abs(delta[r]); if (l == r) { update(1, 1, n, l - 1, l - 1, node(abs(delta[l - 2]) + abs(delta[l - 1]), -x, +x, -x, +x)); update(1, 1, n, l, l, node(abs(delta[l - 1]) + abs(delta[l]), 0, -2 * x, +2 * x, 0)); update(1, 1, n, r + 1, r + 1, node(abs(delta[r]) + abs(delta[r + 1]), +x, +x, -x, -x)); } else { update(1, 1, n, l - 1, l - 1, node(abs(delta[l - 2]) + abs(delta[l - 1]), -x, +x, -x, +x)); update(1, 1, n, l, l, node(abs(delta[l - 1]) + abs(delta[l]), -x, -x, +x, +x)); update(1, 1, n, r, r, node(abs(delta[r - 1]) + abs(delta[r]), +x, -x, +x, -x)); update(1, 1, n, r + 1, r + 1, node(abs(delta[r]) + abs(delta[r + 1]), +x, +x, -x, -x)); } } } return 0; }
9
#include <bits/stdc++.h> using namespace std; struct spt { long double p; int add; spt(long double pp, int addd) : p(pp), add(addd) {} bool operator<(const spt& other) const { return p < other.p; } }; vector<spt> S; long long A[100010]; long double prob[100010]; int main() { long long N, L, v1, v2; cin >> N >> L >> v1 >> v2; long double r = ((long double)v2) / ((long double)(v1 + v2)); long double rL = r * L; for (int i = 0; i < N; i++) { cin >> A[i]; S.push_back(spt(A[i], false)); S.push_back(spt(A[i] - rL, true)); S.push_back(spt(A[i] + 2 * L, false)); S.push_back(spt(A[i] - rL + 2 * L, true)); } S.push_back(spt(2. * L, true)); sort(S.begin(), S.end()); long double p = 0.l; int cnt = 0; for (int i = 0; i < N; i++) if (A[i] < rL) cnt++; int X = S.size(); for (int j = 0; j < X; j++) { long double npt = S[j].p; if (npt < 0.) continue; prob[cnt] += (npt - p); if (npt >= 2.0l * L) break; if (S[j].add) { cnt++; } else { cnt--; } p = npt; } cout << fixed << setprecision(20); for (int i = (0); i <= (N); i++) cout << prob[i] / ((long double)(2.0l * L)) << endl; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<pair<int, int>> v; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; v.push_back(make_pair(a, b)); } sort(v.begin(), v.end(), [](pair<int, int> a, pair<int, int> b) { return a.first - a.second > b.first - b.second; }); long long ans = 0; for (int i = 0; i < n; i++) { long long a = v[i].first; long long b = v[i].second; long long j = i + 1; ans += a * (j - 1) + b * (n - j); } cout << ans << '\n'; return 0; }
4
#include <bits/stdc++.h> int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int test; std::cin >> test; while (test--) { long long n; std::cin >> n; std::vector<double> a(n); std::map<double, int> fre; double sum = 0; for (int i = 0; i < n; i++) { std::cin >> a[i]; fre[a[i]]++; sum += a[i]; } double x = sum - sum / n * (n - 2); long long res = 0; for (int i = 0; i < n; i++) { if (x - a[i] == a[i]) { res += fre[x - a[i]] - 1; } else { res += fre[x - a[i]]; } } std::cout << res / 2 << '\n'; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 111111; int n, m, p, q; struct MFS { int fa[N], c; long long sum[N]; void init(int n) { for (int i = 1; i <= n; i++) fa[i] = i, sum[i] = 0; c = n; } int find(int x) { return fa[x] = fa[x] == x ? x : find(fa[x]); } bool same(int x, int y) { return find(x) == find(y); } void merge(int x, int y, int d) { int fx = find(x), fy = find(y); if (fx == fy) sum[fx] += d; else { fa[fx] = fy; sum[fy] += sum[fx] + d; c--; } } } mfs; int X[N], Y[N]; const int UB = 1000000000; bool work() { if (mfs.c < q) return 0; if (q == n && p) return 0; if (mfs.c - p > q) return 0; set<pair<long long, int> > region; for (int i = 1; i <= n; i++) { int fi = mfs.find(i); region.insert(pair<long long, int>(mfs.sum[fi], fi)); } int tt = 0; while (region.size() > q) { pair<long long, int> t1 = *region.begin(); region.erase(t1); pair<long long, int> t2 = *region.begin(); region.erase(t2); X[tt] = t1.second, Y[tt++] = t2.second; region.insert(pair<long long, int>( min((long long)UB, t1.first + t2.first + 1) + t1.first + t2.first, t1.second)); mfs.merge(t1.second, t2.second, (int)min((long long)UB, t1.first + t2.first + 1)); } if (tt < p) for (int i = 1; i <= n; i++) if (mfs.fa[i] != i) { while (tt < p) X[tt] = i, Y[tt++] = mfs.fa[i]; break; } return 1; } int main() { while (cin >> n >> m >> p >> q) { mfs.init(n); int first, second, d; for (int i = 0; i < m; i++) { cin >> first >> second >> d; mfs.merge(first, second, d); } if (work()) { cout << "YES" << endl; for (int i = 0; i < p; i++) cout << X[i] << ' ' << Y[i] << endl; } else cout << "NO" << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 50; const int iinf = INT_MAX / 2; const long long linf = LONG_MAX / 2; const int MOD = 1e9 + 7; inline int read() { int X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } void write(int x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } int n, m, k, ans; int mp[N]; int a[N][N], res[N][N], bad[N][N][N]; void dfs(int x, int y, int mc) { if (x == n) { for (int i = 1; i <= mc; i++) for (int qi = i + 1; qi <= mc; qi++) if (mp[i] != 0 && mp[qi] != 0 && mp[i] == mp[qi]) return; int free = 0, clos = k; for (int i = 1; i <= mc; i++) if (mp[i] == 0) free++; else clos--; int cur = 1; for (int i = 1; i <= free; i++) { cur *= clos; clos--; } ans = (ans + cur) % MOD; return; } for (int c = 1; c <= min(mc + 1, k); c++) { if (bad[x][y][c]) continue; for (int X = x; X < n; X++) for (int Y = y; Y < m; Y++) bad[X][Y][c]++; res[x][y] = c; int ok = 1; if (a[x][y] != 0 && mp[c] != 0 && mp[c] != a[x][y]) ok = 0; if (ok) { bool flag = 0; if (!mp[c]) { mp[c] = a[x][y]; flag = 1; } int nmc = c > mc ? c : mc; if (y < m - 1) dfs(x, y + 1, nmc); else dfs(x + 1, 0, nmc); if (flag) mp[c] = 0; } for (int X = x; X < n; X++) for (int Y = y; Y < m; Y++) bad[X][Y][c]--; } } signed main() { scanf("%d%d%d", &n, &m, &k); if (n + m - 1 > k) { cout << 0 << '\n'; return 0; } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf("%d", &a[i][j]); memset(bad, 0, sizeof(bad)); memset(mp, 0, sizeof(mp)); dfs(0, 0, 0); printf("%d", ans); return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { string s; string s2; cin >> s >> s2; transform(s.begin(), s.end(), s.begin(), ::tolower); transform(s2.begin(), s2.end(), s2.begin(), ::tolower); if (s == s2) { printf("0\n"); } else if (s < s2) { printf("-1\n"); } else if (s > s2) { printf("1\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 1000; const long long base = 1e9 + 7; const int N = 2e5 + 2; int n; bool k[N]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; cin >> n; if (s.size() < n) cout << "impossible"; else { int d = 0; for (int i = (0), b_ = (s.size() - 1); i <= b_; ++i) if (!k[s[i]]) { k[s[i]] = true; d++; } if (d >= n) cout << 0; else cout << n - d; } }
1
#include <bits/stdc++.h> using namespace std; int rain(int n, int *T, int i) { int r = 1; int d = i + 1, g = i - 1, ex = i; while ((g >= 0) && (T[g] <= T[i])) { r++; i = g; g--; } i = ex; while ((d < n) && (T[d] <= T[i])) { r++; i = d; d++; } return r; } int max(int n, int *T) { int x; x = rain(n, T, 0); for (int i = 1; i < n; i++) { if (rain(n, T, i) > x) x = rain(n, T, i); } return x; } int main() { int *T; int n; cin >> n; T = new int[n]; for (int i = 0; i < n; i++) cin >> T[i]; cout << max(n, T); return 0; }
1
#include <bits/stdc++.h> using namespace std; void solve(long long n, long long k, string s, string t) { long long res = 1; long long ans = 0; long long local; long long i; for (i = 0; i < n; i++) { local = 0; if (s[i] == 'b') local += 1; if (t[i] == 'a') local += 1; if (res * 2 - local > k) break; else { res = min(res * 2 - local, k); ans += res; } } if (i < n) ans += k * (n - i); cout << ans << endl; } int main() { long long n, k; string s, t; cin >> n >> k; cin >> s; cin >> t; solve(n, k, s, t); return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, m; int t[110], l[110], h[110]; void solve() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d%d%d", &t[i], &l[i], &h[i]); } int L = m, R = m; for (int i = 1, T = 0; i <= n; ++i) { L -= (t[i] - T); R += (t[i] - T); if (R >= l[i] && L <= h[i]) { L = max(l[i], L); R = min(R, h[i]); } else { puts("NO"); return; } T = t[i]; } puts("YES"); } int main() { int T; scanf("%d", &T); for (int i = 1; i <= T; ++i) solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int max_n = 4222, inf = 1000111222; int n, a[max_n], p[max_n], q[max_n]; int posp[max_n]; void upd(int p1, int p2, int x) { if (p1 > p2) { swap(p1, p2); } if (!x) { return; } int val = p[p1] ^ x; int t = posp[val]; if (t == p2) { swap(posp[p[p1]], posp[p[p2]]); swap(p[p1], p[p2]); return; } swap(q[p2], q[t]); swap(posp[p[p1]], posp[p[t]]); swap(p[p1], p[t]); upd(p2, t, q[p2] ^ x ^ q[t]); } int main() { scanf("%d", &n); int x = 0; for (int i = 0; i < (1 << n); ++i) { scanf("%d", &a[i]); x ^= a[i]; p[i] = q[i] = i; posp[p[i]] = i; } if (x) { puts("Fou"); return 0; } for (int i = 0; i + 1 < (1 << n); ++i) { upd(i, i + 1, a[i] ^ p[i] ^ q[i]); } puts("Shi"); for (int i = 0; i < (1 << n); ++i) { printf("%d ", p[i]); } puts(""); for (int i = 0; i < (1 << n); ++i) { printf("%d ", q[i]); } puts(""); return 0; }
11
#include <bits/stdc++.h> using namespace std; int n, a[1005], par[1005]; vector<pair<int, int> > v; vector<int> v1; void init() { for (int i = 0; i <= n; ++i) par[i] = i; } int parent(int x) { if (par[x] == x) return x; return par[x] = parent(par[x]); } bool join(int x, int y) { x = parent(x); y = parent(y); if (x == y) return 0; par[x] = y; return 1; } int main() { cin >> n; init(); for (int i = 0; i < n - 1; ++i) { int x, y; scanf("%d%d", &x, &y); if (!join(x, y)) { v.push_back({x, y}); } } for (int i = 1; i <= n; ++i) { if (par[i] == i) { v1.push_back(i); } } cout << v.size() << endl; for (int i = 0; i < v.size(); ++i) { printf("%d %d %d %d\n", v[i].first, v[i].second, v1[i], v1[i + 1]); } }
5
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x) { x = 0; int fu = 1; char c = getchar(); while (c > 57 || c < 48) { if (c == 45) fu = -1; c = getchar(); } while (c <= 57 && c >= 48) { x = (x << 3) + (x << 1) + c - 48; c = getchar(); } x *= fu; } template <typename T> inline void fprint(T x) { if (x < 0) putchar(45), x = -x; if (x > 9) fprint(x / 10); putchar(x % 10 + 48); } template <typename T> inline void fprint(T x, char ch) { fprint(x); putchar(ch); } int n, sz[100010], fa[100010]; int head[100010], cnt, nxt[200010], e[200010]; inline void add(int u, int v) { nxt[++cnt] = head[u]; head[u] = cnt; e[cnt] = v; } double ans = 0, pin[100010], pout[100010], totin, totout; void dfs(int x) { sz[x] = 1; for (register int i = head[x]; i; i = nxt[i]) { if (fa[x] == e[i]) continue; fa[e[i]] = x; dfs(e[i]); sz[x] += sz[e[i]]; pin[x] += pin[e[i]]; ans = ans + pin[e[i]] * sz[e[i]] * pout[x]; } ans = ans + (1 - pin[x]) * (n - sz[x]) * pout[x]; } int main() { read(n); for (register int i = 1; i < n; i++) { int u, v; read(u); read(v); add(u, v); add(v, u); } for (register int i = 1; i <= n; i++) { scanf("%lf%lf", &pin[i], &pout[i]); totin += pin[i]; totout += pout[i]; } for (register int i = 1; i <= n; i++) { pin[i] /= totin; pout[i] /= totout; } dfs(1); printf("%.12lf\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; using std::map; const int CMAX = 100005; const char Cfd[] = "", Cfr[] = ""; vector<pair<int, int> > P; long long sums[CMAX]; int main() { int n, a, b; cin >> n >> a; for (int l = 0; l < n; l++) for (int r = l + 1; r < n; r++) { cin >> b; if (b != a) { P.push_back(make_pair(l, r - 1)); a = b; if (r == n - 1) P.push_back(make_pair(r, r)); l = r - 1; break; } if (a == b && r == n - 1) { P.push_back(make_pair(l, r)); l = r; break; } } for (int i = 1; i <= n; i++) sums[i] = sums[i - 1] + (long long)i; long long res = 0; for (int i = 0; i < P.size(); i++) res += (long long)sums[P[i].second - P[i].first + 1]; cout << (n == 1 ? 1 : res); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; while (t--) { cin >> n; int p = -1, q = -1, a, d = 0; vector<int> h(101, 0); for (int i = 0; i < n; i++) { cin >> a; h[a]++; } for (int i = 0; i <= n; i++) { if (h[i] == 0) { cout << p + q + 2 << endl; break; } else if (h[i] == 1) { p++; d = 1; } else { p++; if (d == 0) q++; } } } }
0
#include <bits/stdc++.h> using namespace std; const long long Z = 1000000007; struct M { long long a[2][2]; }; M operator*(const M& a, const M& b) { M r; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { r.a[i][j] = 0; for (int k = 0; k < 2; ++k) { r.a[i][j] += a.a[i][k] * b.a[k][j] % Z; } r.a[i][j] %= Z; } } return r; } M pow(M m, long long e) { if (e == 1) return m; M x = pow(m, e / 2); return e & 1 ? m * x * x : x * x; } int main() { long long x; cin >> x; if (x == 0) { cout << 1 << '\n'; return 0; } M m = {{{3, 1}, {1, 3}}}; M r = pow(m, x); cout << r.a[1][1] % Z << '\n'; }
2
#include <bits/stdc++.h> #pragma GCC optimize("O3") const int INF = 1e9; const int MOD = 1e9 + 7; const int N = 1e5; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q; cin >> q; while (q--) { int h, n, m; cin >> h >> n >> m; if (h - m * 10 <= 0) { cout << "YES\n"; } else { bool isOk = false; while (n--) { h /= 2; h += 10; if (h <= m * 10) { cout << "YES\n"; isOk = true; break; } } if (!isOk) { cout << "NO\n"; } } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int arr[3002], brr[3002], crr[3002], dp[3002][2]; int n; int call(int idx, int isleft) { if (idx == n - 1) { if (isleft) { return brr[idx]; } else return arr[idx]; } if (dp[idx][isleft] != -1) return dp[idx][isleft]; int ret; if (isleft) ret = max(brr[idx] + call(idx + 1, 1), crr[idx] + call(idx + 1, 0)); else ret = max(arr[idx] + call(idx + 1, 1), brr[idx] + call(idx + 1, 0)); return dp[idx][isleft] = ret; } int main() { while (cin >> n) { memset(dp, -1, sizeof(dp)); for (int i = 0; i < n; ++i) cin >> arr[i]; for (int i = 0; i < n; ++i) cin >> brr[i]; for (int i = 0; i < n; ++i) cin >> crr[i]; int res = call(0, 0); cout << res << endl; } }
5