solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; long long unsigned solve() { int n; cin >> n; vector<int> even, odd; even.reserve((n + 1) / 2); odd.reserve(n / 2); for (int i = 0; i < n; i++) { int x; cin >> x; if (i & 1) odd.push_back(x); else even.push_back(x); } long long unsigned not_inverted = 0, inverting = 0, inverted = 0; for (int i = 0; i < odd.size(); i++) { inverted = max(inverted + even[i], max(not_inverted + even[i], inverting + even[i])); inverting = max(inverting + odd[i], not_inverted + odd[i]); not_inverted += even[i]; } long long unsigned ans = max(not_inverted, max(inverting, inverted)); if (odd.size() != even.size()) { ans += even.back(); } not_inverted = even[0], inverting = even[0], inverted = even[0]; for (int i = 1; i < even.size(); i++) { inverted = max(inverted + even[i], max(not_inverted + even[i], inverting + even[i])); inverting = max(inverting + odd[i - 1], not_inverted + odd[i - 1]); not_inverted += even[i]; } return max(ans, max(not_inverted, max(inverting, inverted))); } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int t; cin >> t; for (int test_case = 0; test_case < t; test_case++) { cout << solve() << "\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int i, numberOfPages, pagesReadable[7]; cin >> numberOfPages; for (int i = 0; i < 7; i++) { cin >> pagesReadable[i]; } for (i = 0; numberOfPages > 0; ++i) { numberOfPages -= pagesReadable[i % 7]; } cout << (--i % 7) + 1; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, i, cnt = 0; string s; cin >> n; while (n--) { cin >> s; if (s == "0" || s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" || s == "8" || s == "9" || s == "10" || s == "11" || s == "12" || s == "13" || s == "14" || s == "15" || s == "16" || s == "17" || s == "ABSINTH" || s == "BEER" || s == "BRANDY" || s == "CHAMPAGNE" || s == "GIN" || s == "RUM" || s == "SAKE" || s == "TEQUILA" || s == "VODKA" || s == "WHISKEY" || s == "WINE") cnt++; } cout << cnt; }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int b, c; cin >> b >> b >> c; int n; cin >> n; vector<int> a(n); int ans = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { if (b < a[i] && a[i] < c) { ++ans; } } cout << ans << '\n'; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int x, y, a, b; cin >> x >> y >> a >> b; int sign = 1; if (x > y) { sign *= -1; a *= -1; b *= -1; swap(a, b); swap(x, y); } if (a <= 0 && b <= 0) { cout << "DRAW\n"; return 0; } if (x + b >= y && y >= x + a) { cout << "FIRST\n"; if (sign == 1) cout << y << "\n"; else cout << x << "\n"; return 0; } if (a <= 0 && b >= 0) { cout << "DRAW\n"; return 0; } int n = y - x; n %= (a + b); if (n == 0) { cout << "SECOND\n"; return 0; } if (n <= a - 1) { cout << "DRAW\n"; return 0; } if (n <= b) { cout << "FIRST\n"; if (sign == 1) cout << x + sign * n << "\n"; else cout << y + sign * n << "\n"; return 0; } cout << "DRAW\n"; return 0; }
8
#include <bits/stdc++.h> using namespace std; constexpr static int MAXN = 1e6; int p[MAXN]; bool vis[MAXN]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &p[i]); p[i]--; } int bsum = 0; for (int i = 0; i < n; i++) { int b; scanf("%d", &b); bsum += b; } int cycles = 0; for (int i = 0; i < n; i++) { if (vis[i]) continue; int j = i; while (p[j] != i) { vis[j] = true; j = p[j]; } vis[j] = true; cycles++; } int total = (cycles == 1 ? 0 : cycles); if (bsum % 2 == 0) total++; printf("%d\n", total); }
4
#include <bits/stdc++.h> const int maxn = 1e5 + 10; const int N = 2e5 + 10; using namespace std; long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); } long long qmul(long long p, long long q) { long long f = 0; while (q) { if (q & 1) f = f + p; if (f > 5e18) return -1; p = p + p; q >>= 1; } return f; } long long qpow(long long p, long long q, long long mo) { long long f = 1; while (q) { if (q & 1) f = f * p % mo; p = p * p % mo; q >>= 1; } return f; } int n, m, t, du[maxn], x[maxn], k[maxn]; long long a[maxn], b[maxn]; int main() { int i, j; scanf("%d", &n); for (i = 1; i <= (int)n; i++) scanf("%lld", &b[i]); for (i = 1; i <= (int)n; i++) scanf("%lld", &a[i]); for (i = 2; i <= (int)n; i++) scanf("%d%d", &x[i], &k[i]), du[x[i]]++; queue<int> pq; for (i = 1; i <= (int)n; i++) if (du[i] == 0) pq.push(i); while (!pq.empty()) { int p = pq.front(); pq.pop(); if (p == 1) return 0 * puts(a[p] <= b[p] ? "YES" : "NO"); if (a[p] <= b[p]) { b[x[p]] += b[p] - a[p]; } else { if (b[x[p]] - 1.0 * (a[p] - b[p]) * k[p] < -(1LL << 60)) b[x[p]] = -(1LL << 60); else b[x[p]] -= (a[p] - b[p]) * k[p], a[p] = b[p]; if (b[x[p]] < -(1LL << 60)) b[x[p]] = -(1LL << 60); } if (--du[x[p]] == 0) pq.push(x[p]); } return 0; }
7
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); ll t; cin >> t; while(t--){ ll n; cin >> n; ll d[2 * n]; for(ll i=0; i<2*n; i++){ cin >> d[i]; } sort(d, d+2*n); bool ans=true; vector<ll> v; for(ll i=0; i<2*n; i+=2){ if(d[i] != d[i+1]){ ans = false; break; } else{ v.push_back(d[i]); } } if(ans){ ll a[n]; a[n-1] = v[n-1] / (n * 2); if(v[n-1] % (n * 2) != 0) ans = false; ll cur = 2 * a[n-1]; for(ll i=n-2; i>=0; i--){ if(v[i]-cur > 0 && (v[i] - cur) % ((i+1) * 2) == 0){ a[i] = (v[i] - cur) / ((i+1) * 2); cur += 2 * a[i]; if(a[i] == a[i+1]){ ans = false; break; } } else{ ans = false; break; } } // for(ll i=0; i<n; i++){ // cout << a[i] << " "; // } // cout << "\n"; if(ans) cout << "YES" << "\n"; else cout << "NO" << "\n"; } else{ cout << "NO" << "\n"; } } }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; string a, b; cin >> a >> b; if (b == "week") { if (n == 6 || n == 5) { cout << "53" << endl; } else { cout << "52" << endl; } } else if (b == "month") { if (n == 30) { cout << "11" << endl; } else if (n == 31) { cout << "7" << endl; } else { cout << "12" << endl; } return 0; } cin.ignore(); cin.ignore(); return 0; }
0
#include <bits/stdc++.h> int main() { int t; scanf("%d", &t); while (t--) { long long int n, i, j, k, f = 0; scanf("%lld", &n); long long int a[n]; for (i = 0; i < n; i++) scanf("%lld", &a[i]); for (i = 2; i < n; i++) { if (a[i] >= a[0] + a[1]) { f = 1; break; } } if (f == 1) printf("1 2 %lld\n", i + 1); else printf("-1\n"); } return 0; }
0
#include <bits/stdc++.h> inline int min(const int &first, const int &second) { return (((second - first) >> (32 - 1)) & (first ^ second)) ^ first; } inline int max(const int &first, const int &second) { return (((second - first) >> (32 - 1)) & (first ^ second)) ^ second; } inline long long min(const long long &first, const long long &second) { return (((second - first) >> (64 - 1)) & (first ^ second)) ^ first; } inline long long max(const long long &first, const long long &second) { return (((second - first) >> (64 - 1)) & (first ^ second)) ^ second; } using namespace std; char string_in_buffer[(int)260]; void fast_scan(int &first) { scanf("%d", &first); } void fast_scan(long long &first) { scanf("%lld", &first); } void fast_scan(unsigned long long &first) { scanf("%llu", &first); } void fast_scan(double &first) { scanf("%lf", &first); } void fast_scan(long double &first) { scanf("%Lf", &first); } void fast_scan(char &first) { scanf("%c", &first); if (first == '\n') { fast_scan(first); } } void fast_scan(string &first) { scanf("%s", string_in_buffer); first = string(string_in_buffer); } template <class TFirst, class TSecond> void fast_scan(pair<TFirst, TSecond> &p) { fast_scan(p.first); fast_scan(p.second); } template <class T> void fast_scan(vector<T> &v) { for (auto &first : v) fast_scan(first); } void fast_print(const int &first) { printf("%d", first); } void fast_print(const unsigned int &first) { printf("%u", first); } void fast_print(const long long &first) { printf("%lld", first); } void fast_print(const unsigned long long &first) { printf("%llu", first); } void fast_print(const char &first) { printf("%c", first); }; void fast_print(const double &first) { printf("%.15lf", first); } void fast_print(const long double &first) { printf("%.15Lf", first); } void fast_print(const string &first) { printf("%s", first.c_str()); } void fast_print(const char v[]) { fast_print((string)v); } template <class TFirst, class TSecond> void fast_print(const pair<TFirst, TSecond> &p) { fast_print(p.first); fast_print(' '); fast_print(p.second); } template <class T> void fast_print(const vector<T> &v) { if (v.empty()) return; fast_print(v[0]); for (int i = 1; i < v.size(); i++) { fast_print(' '); fast_print(v[i]); } } template <class T> void fast_print(const vector<vector<T>> &v) { if (v.empty()) return; fast_print(v[0]); for (int i = 1; i < v.size(); i++) { fast_print('\n'); fast_print(v[i]); } } template <class T> void fast_print(const T &v) { for (const auto &first : v) { fast_print(first); fast_print(' '); } } using namespace std; namespace smart_io { string print_start = ""; string sep = " "; bool first_print = false; void precall_print() { fast_print(print_start); print_start = "\n"; first_print = true; } void _print(deque<string>) {} template <class T, class... Args> void _print(deque<string> names, T elem, Args... args) { if (!first_print) { fast_print("\n"); } else { first_print = false; } fast_print(names.front()); fast_print(" = "); fast_print(elem); names.pop_front(); _print(names, args...); } } // namespace smart_io template <class T> ostream &operator,(ostream &os, const T &object) { if (!smart_io::first_print) { fast_print(smart_io::sep); } else { smart_io::first_print = false; } fast_print(object); return os; } template <class T> istream &operator,(istream &is, T &object) { fast_scan(object); return is; } namespace escape__from__random__aetuhoetnuhshe { using namespace std::chrono; mt19937 rng(duration_cast<milliseconds>(system_clock::now().time_since_epoch()) .count()); uniform_real_distribution<> prob_dist(0.0, 1.0); }; // namespace escape__from__random__aetuhoetnuhshe namespace typedefs {} namespace numbers_operation { template <class T> inline T floor_mod(T a, const T &b) { a %= b; if (a < 0) a += b; return a; } } // namespace numbers_operation using namespace numbers_operation; using namespace typedefs; using namespace escape__from__random__aetuhoetnuhshe; int n, m; vector<vector<pair<int, int>>> g; vector<bool> used; vector<int> st; vector<int> type; bool dfs(int v, int cur, int req) { st.push_back(v); used[v] = true; type[v] = cur; for (pair<int, int> sub : g[v]) { if (used[sub.first]) { if ((type[sub.first] ^ type[v] ^ sub.second) != req) { return false; } } else { if (!dfs(sub.first, type[v] ^ sub.second ^ req, req)) return false; } } return true; } signed main(signed argc, char *argv[]) { cin, n, m; g.resize(n); type.resize(n); for (int i = 0; i < (m); i++) { int a, b; string c; cin, a, b; cin, c; a--; b--; g[a].emplace_back(b, c == "R"); g[b].emplace_back(a, c == "R"); } vector<int> final(n + 1, -1); for (int req = 0; req < 2; req++) { vector<int> full; used = vector<bool>(n, false); for (int i = 0; i < (n); i++) { if (used[i]) continue; vector<int> best{-1}; for (int d = 0; d < (2); d++) { st.clear(); if (dfs(i, d, req)) { vector<int> acts; for (int first : st) { if (type[first]) acts.push_back(first); } if (best == vector<int>{-1} || ((int)best.size()) > ((int)acts.size())) { best = acts; } } if (d == 0) for (int first : st) { used[first] = false; } } if (best == vector<int>{-1}) best = vector<int>(n + 1, -1); full.insert(full.end(), best.begin(), best.end()); if (((int)full.size()) > n) break; } if (((int) final.size()) > ((int)full.size())) final = full; } if (((int) final.size()) <= n) { smart_io::precall_print(); cout, ((int) final.size()); for (auto &first : final) { first++; }; smart_io::precall_print(); cout, final; } else { smart_io::precall_print(); cout, -1; } }
7
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 5e3 + 2; long long n, cap, reqd; long long tank[N], dp[N][N]; set<long long> ok; bool solve(long long id, long long now) { if (now < 0) now += cap; if (now == 0) return 1; if (id == n) return 0; if (dp[id][now] != -1) return dp[id][now]; if (!solve(id + 1, now)) { if (!solve(id + 1, (now - tank[id]) % cap)) return dp[id][now] = 0; else ok.insert(id); } return dp[id][now] = 1; } signed main() { memset(dp, -1, sizeof dp); long long sum = 0; cin >> n >> cap >> reqd; for (long long i = 0; i < n; i++) { cin >> tank[i]; sum += tank[i]; } if (sum < reqd || !solve(0, reqd % cap)) { cout << "NO"; return 0; } if (reqd % cap == 0) { cout << "YES\n"; for (long long i = 1; i < n; i++) if (tank[i] > 0) cout << ceil((double)tank[i] / cap) << " " << i + 1 << " " << 1 << endl; if (reqd != 0) cout << reqd / cap << " " << 1 << " " << 2 << endl; return 0; } if (ok.size() == n) { bool flag = 1; for (auto i : ok) if (tank[i] > cap) { flag = 0; break; } if (!flag) { cout << "NO\n"; return 0; } } cout << "YES\n"; long long cur = reqd - (reqd % cap), collect; long long other = 1; if (ok.size() == 1) { collect = *ok.begin(); ok.erase(ok.begin()); for (long long i = 0; i < n; i++) { if (i != collect) { other = i; if (tank[collect] >= cap) cout << floor((double)tank[collect] / cap) << " " << collect + 1 << " " << i + 1 << endl; tank[i] += floor((double)tank[collect] / cap) * cap; break; } } tank[collect] %= cap; } else { collect = *ok.begin(); ok.erase(ok.begin()); for (auto i : ok) { if (tank[i] > 0) cout << ceil((double)tank[i] / cap) << " " << i + 1 << " " << collect + 1 << endl; tank[collect] += tank[i]; } other = *ok.begin(); ok.erase(ok.begin()); if (tank[collect] >= cap) cout << floor((double)tank[collect] / cap) << " " << collect + 1 << " " << other + 1 << endl; tank[collect] %= cap; tank[other] += floor((double)tank[collect] / cap) * cap; } for (long long i = 0; i < n && tank[collect] < reqd; i++) { if (!ok.count(i) && i != other && i != collect) { tank[other] += tank[i]; if (tank[i] > 0) cout << ceil((double)tank[i] / cap) << " " << i + 1 << " " << other + 1 << endl; } } if (reqd > tank[collect]) cout << (reqd - tank[collect]) / cap << " " << other + 1 << " " << collect + 1 << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; int p[256]; int main() { for (int i = 0; i <= 60; i++) { p[i] = i; } p[int('T')] = 66; p[int('J')] = 67; p[int('Q')] = 68; p[int('K')] = 69; p[int('A')] = 70; char a[2], b[2]; char q; cin >> q; cin >> a >> b; swap(a[0], a[1]); swap(b[0], b[1]); if (a[0] == b[0]) { if (p[(int)a[1]] > p[(int)b[1]]) cout << "YES"; else cout << "NO"; return 0; } if (a[0] == q) cout << "YES"; else cout << "NO"; }
1
#include <bits/stdc++.h> using namespace std; long freq[1 << 19 + 1]; int main() { long long n; char c; long long t, temp, p, index; cin >> t; while (t--) { p = 1, index = 0; cin >> c; cin >> n; temp = n; while (temp > 0) { if (temp % 2) index += p; p *= 2; temp /= 10; } switch (c) { case '+': freq[index]++; break; case '-': freq[index]--; break; case '?': cout << freq[index] << endl; break; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long N = 1234; double dp[N][N * 10]; int main() { long long types, q, i, j; cin >> types >> q; for (i = 1; i <= types; i++) for (j = i; j < N * 10; j++) { if (j == 1) { dp[i][j] = 1; continue; } dp[i][j] = (i / (double)types) * dp[i][j - 1] + dp[i - 1][j - 1] * ((types - i + 1) / (double)types); } while (q--) { double p; cin >> p; p /= 2000; for (i = 1;; i++) { if (dp[types][i] >= p) { cout << i << endl; break; } } } }
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x[n], y[n], cnt = 0; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; for (int i = 0; i < n; i++) { int flag = 0, a = 0; for (int j = 0; j < n; j++) { if ((x[i] == x[j]) && (y[i] > y[j])) { for (int k = 0; k < n; k++) { if ((x[i] == x[k]) && (y[i] < y[k])) { flag = 1; goto token; } } } } token: if (flag) { for (int l = 0; l < n; l++) { if ((y[i] == y[l]) && (x[i] > x[l])) { for (int k = 0; k < n; k++) { if ((y[i] == y[k]) && (x[i] < x[k])) { a = 1; goto t; } } } else flag = 0; } } t: if (a == 1) cnt++; } cout << cnt << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long int dd[n - 1]; long long int dpp[n - 1]; long long int d[n - 1]; long long int dp[n - 1]; for (int i = 0; i < n - 1; i++) { if (i % 2 == 0) { dd[i] = abs(a[i + 1] - a[i]); d[i] = -1 * abs(a[i + 1] - a[i]); } else { dd[i] = -1 * abs(a[i + 1] - a[i]); d[i] = abs(a[i + 1] - a[i]); } } dp[0] = dd[0]; for (int i = 1; i < n - 1; i++) { dp[i] = max(dd[i], dp[i - 1] + dd[i]); } dpp[0] = 0; dpp[1] = d[1]; for (int i = 2; i < n - 1; i++) { dpp[i] = max(d[i], dpp[i - 1] + d[i]); } sort(dp, dp + n - 1); sort(dpp, dpp + n - 1); cout << max(dp[n - 2], dpp[n - 2]); }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(false); cin.tie(0); cout.tie(0); int test = 1; while (test--) { int n; cin >> n; int a[n]; long long int sum = 0; for (int i = 0; i < n; i++) cin >> a[i], sum += a[i]; if (sum % n) cout << n - 1 << '\n'; else cout << n << '\n'; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n; const int maxn = 1000000 + 5; const long long mod = 1e9 + 7; int h[maxn]; long long dp[maxn]; int main(int argc, const char* argv[]) { cin >> n; for (int i = 1; i <= n; i++) { scanf("%d", h + i); } dp[2] = min(h[2] - 1, h[1] - 1) % mod; ; for (int i = 2; i < n; i++) { dp[i + 1] = (dp[i] * (long long)min(h[i - 1] - 1, min(h[i] - 1, h[i + 1] - 1)) + (long long)min(h[i] - 1, h[i + 1] - 1)) % mod; } long long ans = 0; for (int i = 1; i <= n; i++) { ans += (long long)(h[i] - 1); ans %= mod; } for (int i = 2; i <= n; i++) { ans = (ans + (long long)min(h[i - 1] - 1, h[i] - 1) * dp[i]) % mod; } cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, k, l, i, j; vector<pair<long long, string>> a; cin >> n >> m; string s, s1; getline(cin, s1); vector<string> b; while (cin) { getline(cin, s); b.push_back(s); } for (i = 0; i < b.size() - 1; i++) { s = b[i]; s1 = ""; k = 0; k = (s[8] - '0') * 10 + s[9] - '0'; l = (s[5] - '0') * 10 + s[6] - '0'; switch (l) { case 1: k = k; break; case 2: k = k + 31; break; case 3: k = k + 60; break; case 4: k = k + 91; break; case 5: k = k + 121; break; case 6: k = k + 152; break; case 7: k = k + 182; break; case 8: k = k + 213; break; case 9: k = k + 244; break; case 10: k = k + 274; break; case 11: k = k + 305; break; case 12: k = k + 335; break; } l = ((s[11] - '0') * 10 + s[12] - '0') * 60 * 60 + ((s[14] - '0') * 10 + s[15] - '0') * 60 + (s[17] - '0') * 10 + s[18] - '0'; for (j = 0; j < 19; j++) s1 = s1 + s[j]; a.push_back(make_pair((k - 1) * 24 * 60 * 60 + l, s1)); } for (i = 0; i < a.size(); i++) { if (i + m - 1 < a.size() && a[i + m - 1].first - a[i].first < n) { cout << a[i + m - 1].second; return 0; } } cout << -1; return 0; }
6
#include <bits/stdc++.h> using namespace std; int N, M, i, j; pair<int, int> A[13], B[13]; set<int> ANS, SOL; set<int>::iterator it; int main() { cin >> N >> M; for (i = 1; i <= N; i++) { cin >> A[i].first >> A[i].second; if (A[i].first > A[i].second) swap(A[i].first, A[i].second); } for (i = 1; i <= M; i++) { cin >> B[i].first >> B[i].second; if (B[i].first > B[i].second) swap(B[i].first, B[i].second); } for (i = 1; i <= N; i++) { SOL.clear(); for (j = 1; j <= M; j++) { if (A[i].first == B[j].first && A[i].second == B[j].second) continue; if (A[i].first != B[j].first && A[i].first != B[j].second && A[i].second != B[j].first && A[i].second != B[j].second) continue; if (A[i].first == B[j].first) SOL.insert(A[i].first); if (A[i].first == B[j].second) SOL.insert(A[i].first); if (A[i].second == B[j].first) SOL.insert(A[i].second); if (A[i].second == B[j].second) SOL.insert(A[i].second); } if (SOL.size() == 1) { it = SOL.begin(); ANS.insert(*it); } if (SOL.size() > 1) { cout << -1; return 0; } } for (j = 1; j <= M; j++) { SOL.clear(); for (i = 1; i <= N; i++) { if (A[i].first == B[j].first && A[i].second == B[j].second) continue; if (A[i].first != B[j].first && A[i].first != B[j].second && A[i].second != B[j].first && A[i].second != B[j].second) continue; if (A[i].first == B[j].first) SOL.insert(A[i].first); if (A[i].first == B[j].second) SOL.insert(A[i].first); if (A[i].second == B[j].first) SOL.insert(A[i].second); if (A[i].second == B[j].second) SOL.insert(A[i].second); } if (SOL.size() == 1) { it = SOL.begin(); ANS.insert(*it); } if (SOL.size() > 1) { cout << -1; return 0; } } if (ANS.size() == 0) cout << -1; if (ANS.size() == 1) { it = ANS.begin(); cout << *it; } if (ANS.size() > 1) cout << 0; return 0; }
5
#include <bits/stdc++.h> using namespace std; template <class T> inline void in(T &x) { x = 0; short 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(); x *= f; } template <class T, class... Args> inline void in(T &x, Args &...args) { in(x); in(args...); } template <class T> inline void out(T x, const char c = '\n') { static short st[30]; short m = 0; if (x < 0) putchar('-'), x = -x; do st[++m] = x % 10, x /= 10; while (x); while (m) putchar(st[m--] | '0'); putchar(c); } template <class T, class... Args> inline void out(const T &x, const Args &...args) { out(x, ' '); out(args...); } template <class T> inline void print(T a[], int n) { if (n <= 0) return; for (register int i = 0; i < n; ++i) out(a[i], ' '); putchar('\n'); } template <class T> inline void clr(T a[], int n) { if (n <= 0) return; memset(a, 0, sizeof(T) * n); } template <class T> inline void clr(T *a, T *b) { memset(a, 0, sizeof(T) * (b - a)); } namespace i207M { const int md = 1e9 + 7; inline int qpow(int a, int b) { int res = 1; for (; b; b >>= 1, a = (long long)a * a % md) if (b & 1) res = (long long)res * a % md; return res; } int p, a; inline int calcS(int n, int up, int ex) { if (!up) return ((((long long)(n + 2) * (n + 1) / 2 % md) - (ex * (n + 1)) + md) % md); else return ( (((long long)(p - 1 + p - 1 - n) * (n + 1) / 2 % md) + (ex * (n + 1))) % md); } inline int calcP(int n, int up, int ex) { if (!up) return n + 1 - ex; else return p - 1 - n + ex; } int len, num[1005]; int Div(int m) { long long tmp = 0; for (register int i = len; i >= 1; --i) { tmp = tmp * 10 + num[i]; num[i] = tmp / m; tmp %= m; } while (len && !num[len]) --len; return tmp; } int n, A[4005]; int f[4005][4005][2][2]; signed main() { in(p, a); static char tc[1005]; scanf("%s", tc + 1); len = strlen(tc + 1); for (register int i = 1; i <= len; ++i) num[len - i + 1] = tc[i] - '0'; while (len) A[++n] = Div(p); reverse(A + 1, A + 1 + n); if (a > n) { puts("0"); return 0; } f[0][0][0][1] = 1; for (register int i = 1; i <= n; ++i) for (register int j = 0; j <= a; ++j) for (register int k = 0; k <= 1; ++k) for (register int eq = 0; eq <= 1; ++eq) { int t = f[i - 1][j][k][eq]; if (!t) continue; for (register int up = 0; up <= 1; ++up) { int nj = min(j + up, a); if (!eq) (((f[i][nj][up][0]) += (((long long)(t) * (calcS(p - 1, k, up)) % md))) >= md ? (f[i][nj][up][0]) -= md : 0); else { (((f[i][nj][up][1]) += (((long long)(t) * (calcP(A[i], k, up)) % md))) >= md ? (f[i][nj][up][1]) -= md : 0); if (A[i]) (((f[i][nj][up][0]) += (((long long)(t) * (calcS(A[i] - 1, k, up)) % md))) >= md ? (f[i][nj][up][0]) -= md : 0); } } } int ans = (f[n][a][0][0] + f[n][a][0][1]) % md; out(ans); return 0; } } // namespace i207M signed main() { i207M::main(); return 0; }
12
#include <bits/stdc++.h> using namespace std; long long n, m, ans, v[1000001], a[1000001], sz[1000001], f[1000001], nt[1000001]; vector<long long> zh[200001]; long long gcd(long long x, long long y) { if (!y) return x; return gcd(y, x % y); } long long exgcd(long long a, long long b, long long &x, long long &y) { if (!b) { x = 1; y = 0; return a; } long long t, hh = exgcd(b, a % b, x, y); t = x; x = y; y = t - a / b * y; return hh; } long long work(long long aa, long long bb) { long long x, y, hh = exgcd(aa, m, x, y); x = ((x + m) % m * bb / hh) % m; return x; } void get(long long x, long long la) { long long i; for (i = 0; i < zh[x].size(); i++) { ans = work(la, zh[x][i]); printf("%lld ", ans); la = zh[x][i]; } if (nt[x]) get(nt[x], la); } int main() { long long i, j, x; scanf("%lld%lld", &n, &m); for (i = 1; i <= n; i++) { scanf("%lld", &x); v[x] = 1; } for (i = 1; i < m; i++) if (!v[i]) { x = gcd(i, m); sz[x]++; zh[x].push_back(i); } for (i = m / 2; i; i--) if (sz[i]) { for (j = i * 2; j <= m; j += i) if (f[j] && f[j] > f[i]) { f[i] = f[j]; nt[i] = j; } f[i] += sz[i]; } ans = f[1]; if (!v[0]) ans++; printf("%lld\n", ans); get(1, 1); if (!v[0]) puts("0"); }
7
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string str; cin >> str; map<int, int> tmp; int mn = INT_MAX; int pre = 0; for (int i = 0; i < str.size(); i++) { if (str[i] == '+') pre++; else pre--; if (tmp.find(pre) == tmp.end()) tmp.insert({pre, i + 1}); mn = min(mn, pre); } long long ans = str.size(); for (int i = 0; i < abs(min(mn, 0)); i++) { ans += (long long)(tmp[-1 - i]); } cout << ans << endl; } }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 2000; int c[MAXN][MAXN]; const int MOD = 1e9 + 7; int two[MAXN]; int main() { for (int i = 0; i < MAXN; i++) { c[i][i] = 1; c[i][0] = 1; } for (int i = 1; i < MAXN; i++) { for (int j = 1; j < i; j++) { c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % MOD; } } two[0] = 1; for (int i = 1; i < MAXN; i++) { two[i] = (two[i - 1] * 2) % MOD; } int n, m; cin >> n >> m; vector<bool> used(n + 2); for (int i = 0; i < m; i++) { int x; cin >> x; used[x] = true; } used[n + 1] = true; int start = 0; bool ok = true; vector<pair<int, int>> segments; for (int i = 1; i <= n + 1; i++) { if (used[i]) { if (ok == false) { segments.emplace_back(start, i - 1); ok = true; } } if (!used[i]) { if (ok == true) { start = i; ok = false; } } } int total = 0; int ans = 1; for (auto segment : segments) { total += segment.second - segment.first + 1; } for (auto segment : segments) { int length = segment.second - segment.first + 1; if (segment.first == 1 || segment.second == n) { ans = (long long)ans * c[total][length] % MOD; } else { ans = (long long)ans * c[total][length] % MOD * (long long)two[length - 1] % MOD; } total -= length; } cout << ans; return 0; }
5
#include <bits/stdc++.h> using namespace std; bool comp(pair<int, int> x, pair<int, int> y) { return abs(x.first) + abs(x.second) < abs(y.first) + abs(y.second); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int points; vector<pair<int, int> > p; long long moves; while (cin >> points) { moves = 0; p.clear(); p.resize(points); for (int i = 0; i < points; i++) { cin >> p[i].first >> p[i].second; if (!p[i].first || !p[i].second) moves += 4; else moves += 6; } cout << moves << "\n"; sort(p.begin(), p.end(), comp); for (int i = 0; i < p.size(); i++) { if (p[i].first) { cout << 1 << " " << abs(p[i].first) << " "; if (p[i].first > 0) cout << "R" << "\n"; else cout << "L" << "\n"; } if (p[i].second) { cout << 1 << " " << abs(p[i].second) << " "; if (p[i].second > 0) cout << "U" << "\n"; else cout << "D" << "\n"; } cout << 2 << "\n"; if (p[i].first) { cout << 1 << " " << abs(p[i].first) << " "; if (p[i].first > 0) cout << "L" << "\n"; else cout << "R" << "\n"; } if (p[i].second) { cout << 1 << " " << abs(p[i].second) << " "; if (p[i].second > 0) cout << "D" << "\n"; else cout << "U" << "\n"; } cout << 3 << "\n"; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(20); long long int n, m; cin >> n >> m; vector<vector<long long int> > a(n, vector<long long int>(m, 0)); vector<vector<long long int> > b(n, vector<long long int>(m, 0)); vector<pair<long long int, long long int> > ans; for (long long int i = 0; i < n; i++) for (long long int j = 0; j < m; j++) cin >> a[i][j]; for (long long int i = 0; i < n - 1; i++) { for (long long int j = 0; j < m - 1; j++) { if (a[i][j] && a[i + 1][j] && a[i][j + 1] && a[i + 1][j + 1]) { b[i][j] = 1; b[i + 1][j] = 1; b[i][j + 1] = 1; b[i + 1][j + 1] = 1; ans.push_back({i + 1, j + 1}); } } } for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < m; j++) { if (a[i][j] != b[i][j]) { cout << -1 << endl; return 0; } } } cout << (long long int)((ans).size()) << endl; for (long long int i = 0; i < (long long int)((ans).size()); i++) cout << ans[i].first << ' ' << ans[i].second << endl; ; return 0; }
2
#include <bits/stdc++.h> int main() { int t; while (scanf("%d", &t) != EOF) { for (int i = 1; i <= t; i++) { if (i % 4 == 1) printf("a"); if (i % 4 == 2) printf("b"); if (i % 4 == 3) printf("c"); if (i % 4 == 0) printf("d"); } printf("\n"); } }
1
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e18; long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long fpow(long long n, long long k, int p = 1000000007) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } int inv(int a, int p = 1000000007) { return fpow(a, p - 2, p); } long long spf[1000001 + 1]; void sieve() { for (int i = 1; i < 1000001; i++) { spf[i] = i; } for (long long i = 2; i < 1000001; i += 2) { spf[i] = 2; } for (long long i = 3; i < 1000001; i += 2) { if (spf[i] == i) { for (long long j = i * i; j < 1000001; j += i) { spf[j] = min(i, (long long)spf[j]); } } } } int main() { long long n, m; cin >> n >> m; set<long long> st; set<pair<long long, long long> > st1; map<long long, long long> pp; vector<long long> ans; for (int i = 0; i < n; i++) { long long x; cin >> x; st.insert(x); pp[x] = i; } for (int i = 0; i < m; i++) { long long x; cin >> x; if (st.find(x) != st.end()) { st1.insert(make_pair(pp[x], x)); } } for (auto it : st1) { cout << it.second << " "; } cout << endl; }
0
#include <bits/stdc++.h> using namespace std; int n, k, y, sayac; string a; int main() { scanf("%d", &n); cin >> a; for (int i = 0; i <= n - 1; i++) { if (a[i] == '>' && a[i + 1] == '<' && i <= n - 2) { k = i - 1; break; } } for (int i = n - 1; i >= 0; i--) { if (a[i] == '<' && a[i - 1] == '>' && i >= 1) { y = i + 1; break; } } for (int i = 0; i <= k; i++) { if (a[i] == '<') { sayac++; } } for (int i = y; i <= n - 1; i++) { if (a[i] == '>') { sayac++; } } if (k == 0 && y == 0) { cout << n; return 0; } cout << sayac; return 0; }
1
#include <bits/stdc++.h> using namespace std; int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + (ch ^ 48), ch = getchar(); return s * w; } int n, a[10010], sc; signed main() { int T = read(); while (T--) { sc = 0; n = read(); for (int i = 1; i <= n; i++) a[i] = read(), sc += a[i]; if (sc % n) printf("-1\n"); else { printf("%d\n", (n - 1) * 3); for (int i = 2; i <= n; i++) printf("1 %d %d\n%d 1 %d\n", i, (i - a[i] % i) % i, i, (a[i] + (i - a[i] % i) % i) / i); for (int i = 2; i <= n; i++) printf("1 %d %d\n", i, sc / n); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long total = 0; for (int i = 1; i <= n; ++i) { long long num; cin >> num; total += ((num - 1) * i) + 1; } cout << total << '\n'; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T a) { return ((a < 0) ? -a : a); } template <typename T> inline T sqr(T a) { return a * a; } const int INF = (int)1E9 + 7; const long double EPS = 1E-9; const long double PI = 3.1415926535897932384626433832795; int n, x[300000]; long double ans[50]; bool can(long double d) { long double rg = -1E100; int it = 0; for (int i = 0; i < int(n); ++i) { if (fabs(x[i] - rg) < EPS) continue; if (x[i] <= rg) continue; ans[it++] = x[i] + d; rg = max(x[i] + 2 * d, rg); if (it > 3) break; } return it <= 3; } int main() { scanf("%d", &n); for (int i = 0; i < int(n); ++i) scanf("%d", &x[i]); sort(x, x + n); n = unique(x, x + n) - x; long double lf = 0.0, rg = 1E9 + 7; for (int i = 0; i < int(100); ++i) { long double mid = (lf + rg) / 2; if (can(mid)) rg = mid; else lf = mid; } can(rg); cout.precision(6); cout << fixed; cout << (lf + rg) / 2 << endl; for (int i = 0; i < int(3); ++i) cout << min(max(0.0L, ans[i]), 1E9L + 100) << " "; cout << endl; return 0; }
5
#include <bits/stdc++.h> int main() { int ch[1000000]; char kh[1000000], arr[1000000]; int a = 0, b = 0, c, d, e, i = 0, j = 0, k = 0, l = 0, tep, temp, m, n, x, y; scanf("%d", &e); while (e--) { scanf("%d%d%d%d", &a, &b, &c, &d); i = 0; j = 0; k = 0; l = 0; if (a > b) { i = a; a = b; b = i; } k = c - d; l = c + d; if (k >= b || l <= a) j = b - a; if (k <= b && k >= a && l >= b) j = k - a; if (l >= a && l <= b && k <= a) j = b - l; if (k >= a && l <= b) j = k - a + b - l; if (k <= a && l >= b) j = 0; printf("%d\n", j); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int a, b, k, t, suan, d[1100002], f[1110000]; int main() { cin >> a >> b >> k >> t; int shu = k * 2 * t; d[shu] = 1; int y = d[0]; for (int i = 1; i <= t * 2; i++) { f[0] = y; for (int j = 1; j <= shu * 2 + k; j++) { f[j] = (f[j - 1] + d[j]) % 1000000007; } for (int j = 0; j <= k; j++) { d[j] = f[j + k]; } for (int j = k + 1; j <= shu * 2; j++) { d[j] = (f[j + k] - f[j - 1 - k] + 1000000007) % 1000000007; } } for (int i = shu - a + b + 1; i <= shu * 2; i++) { suan = (suan + d[i]) % 1000000007; } cout << suan; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n; if (n % 2 == 1) { cout << "0\n"; } else { m = n / 2; cout << (m - 1) / 2 << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; void fun() {} long long int __gcda(long long int a, long long int b) { if (b == 0) { return a; } else if (a == 0) { return b; } else if (a == b) { return a; } else if (b > a) { return __gcda(a, b % a); } else if (a > b) { return __gcda(a % b, b); } return 0; } long long int sear(long long int arr[], long long int val, long long int n) { for (long long int i = 0; i < n; i++) { if (arr[i] == val) { return i; } } return -1; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); fun(); long long int t; cin >> t; while (t--) { long long int n; cin >> n; char arr[n]; long long int dis = 0; long long int flag = 0; long long int ans = 0; vector<long long int> v; long long int ind = 1; for (long long int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] == 'A' && flag == 0) { flag = 1; ind = i; } } if (flag == 0) { cout << "0\n"; } else { dis = 0; for (long long int i = n - 1; i >= ind; i--) { dis++; if (arr[i] == 'A') { ans = max(dis - 1, ans); dis = 0; } } cout << ans << "\n"; } } }
0
#include <bits/stdc++.h> using namespace std; vector<char> temp; int n, m, allGood, ans; char matrix[50][51]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) scanf("%s", matrix[i]); for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < m - 1; ++j) { allGood = 1; temp.clear(); temp.push_back(matrix[i][j]); temp.push_back(matrix[i][j + 1]); temp.push_back(matrix[i + 1][j]); temp.push_back(matrix[i + 1][j + 1]); sort(temp.begin(), temp.end()); if (temp[0] != 'a') allGood = 0; if (temp[1] != 'c') allGood = 0; if (temp[2] != 'e') allGood = 0; if (temp[3] != 'f') allGood = 0; if (allGood) ++ans; } } printf("%d", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long read() { long long a = 0, b = getchar(), c = 1; while (!isdigit(b)) c = b == '-' ? -1 : 1, b = getchar(); while (isdigit(b)) a = a * 10 + b - '0', b = getchar(); return a * c; } const int N = 3e5 + 5; int n, ab, ac, ans, a[N], b[N], v[N], d[N], c[N]; int cre(int k); void add(int x, int y); void jj(int x); void gao(int x, int k, int &aa, int &bb); int gg(int x, int y); int sum(int x, int y); void down(int x); int main() { n = read(); while (n--) { int l = read(), r = read(), x, y, z; gao(ac, r, x, z); gao(x, l, x, y); add(y, 1); if (z) { int w = z; while (a[w]) { jj(w); w = a[w]; } gao(z, v[w] + 1, w, z); } else ans++; ac = gg(gg(x, cre(l)), gg(y, z)); } printf("%d", ans); return 0; } int cre(int k) { v[++ab] = k; c[ab] = rand(); return ab; } void add(int x, int y) { v[x] += y; d[x] += y; } void jj(int x) { add(a[x], d[x]); add(b[x], d[x]); d[x] = 0; } int sum(int x, int y) { if (!x or !y) return x | y; jj(x), jj(y); if (c[x] < c[y]) return b[x] = gg(b[x], y), x; else return a[y] = gg(x, a[y]), y; return -1; } void gao(int x, int k, int &aa, int &bb) { if (!x) { aa = bb = 0; return; } jj(x); if (v[x] < k) aa = x, gao(b[x], k, b[aa], bb); else bb = x, gao(a[x], k, aa, a[bb]); } void down(int x) { ans++; while (x) x = a[x]; jj(x); if (!x) down(a[x]); } int gg(int x, int y) { if (!x or !y) return x | y; jj(x), jj(y); if (c[x] < c[y]) return b[x] = gg(b[x], y), x; else return a[y] = gg(x, a[y]), y; return -1; }
10
#include <bits/stdc++.h> using namespace std; bool within(int x1, int x2, int y1, int y2, int px, int py) { if (x1 <= px and px <= x2 and y1 <= py and py <= y2) { return true; } return false; } int main() { ios::sync_with_stdio(false); int y, x, v, n; cin >> y >> x >> v >> n; int py[v], px[v]; for (int i = 0; i < v; i++) { cin >> px[i] >> py[i]; py[i]--, px[i]--; } int res = 0; for (int x1 = 0; x1 < x; x1++) { for (int x2 = x1; x2 < x; x2++) { for (int y1 = 0; y1 < y; y1++) { for (int y2 = y1; y2 < y; y2++) { int found = 0; for (int i = 0; i < v; i++) { if (within(x1, x2, y1, y2, py[i], px[i])) { found++; } } if (found >= n) { res++; } } } } } cout << res; return 0; }
1
#include <bits/stdc++.h> using namespace std; void c_p() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int vis[5000], a[5000], b[5000]; int main() { c_p(); int m, n, i = 0, j = 0, ans; cin >> n >> m; int a[n], b[m]; ans = n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> b[i]; while (i < n) { if (j >= m) break; while (j < m) { if (b[j] >= a[i]) ans -= 1, i += 1; j += 1; } i++; } cout << max(0, ans) << endl; }
2
#include <bits/stdc++.h> using namespace std; vector<long long> p, ant; long long par(long long u) { if (u == -1) return -1; return p[u] = (p[u] == u ? u : par(p[u])); } void un(long long x, long long y) { if (x == -1 || y == -1) return; long long px = par(x), py = par(y); p[py] = px; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, m, q; cin >> n >> m >> q; map<string, long long> foo; for (long long i = 0; i < n; i++) { string s; cin >> s; foo[s] = i; p.push_back(i); ant.push_back(-1); } while (m--) { long long t; cin >> t; string a, b; cin >> a >> b; long long x = par(foo[a]), y = par(foo[b]); ant[x] = par(ant[x]); ant[y] = par(ant[y]); if (t == 1) { if (ant[x] != y && ant[y] != x) { cout << "YES\n"; un(x, y); un(ant[x], ant[y]); if (ant[x] == -1) ant[x] = ant[y]; else ant[y] = ant[x]; } else { cout << "NO\n"; } } else { if (x != y) { un(x, ant[y]); un(ant[x], y); cout << "YES\n"; ant[x] = par(y); ant[y] = par(x); } else { cout << "NO\n"; } } } while (q--) { string a, b; cin >> a >> b; long long x = par(foo[a]), y = par(foo[b]); ant[x] = par(ant[x]); ant[y] = par(ant[y]); if (x == y) { cout << "1\n"; } else if (x == ant[y]) { cout << "2\n"; } else cout << "3\n"; } }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int da[maxn] = {0}; map<int, int> cnt; int main() { int n, m; cin >> n; int inp; int num = 0; int mmax = 1; map<int, int>::iterator idx; for (int i = 1; i <= n; i++) { cin >> inp; if (da[inp] != 0) { cnt[da[inp]]--; if (cnt[da[inp]] == 0) cnt.erase(da[inp]); } da[inp]++; cnt[da[inp]]++; if (cnt.size() > 2) continue; if (cnt.size() == 1) { if (cnt.begin()->first == 1 || cnt.begin()->second == 1) mmax = i; continue; } idx = cnt.begin(); int cnt1 = idx->second, num1 = idx->first; idx++; int cnt2 = idx->second, num2 = idx->first; if (cnt1 == 1 && num1 == 1) mmax = i; if (cnt2 == 1 && num2 == num1 + 1) mmax = i; } cout << mmax << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { vector<int> X = {-1, 0, 0, 1}; vector<int> Y = {0, -1, 1, 0}; int n, m, t; cin >> n >> m >> t; vector<vector<char>> a(n, vector<char>(m)); vector<vector<int>> dist(n, vector<int>(m, -1)); queue<pair<int, int>> q; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> a[i][j]; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { for (int k = 0; k < 4; ++k) { int x = i + X[k], y = j + Y[k]; if (x >= 0 && y >= 0 && x < n && y < m && a[x][y] == a[i][j]) { q.emplace(i, j); dist[i][j] = 0; } } } } while (!q.empty()) { auto cur = q.front(); q.pop(); for (int k = 0; k < 4; ++k) { int x = cur.first + X[k], y = cur.second + Y[k]; if (x >= 0 && y >= 0 && x < n && y < m && dist[x][y] == -1) { dist[x][y] = dist[cur.first][cur.second] + 1; q.emplace(x, y); } } } while (t--) { int i, j; long long p; cin >> i >> j >> p; i--, j--; if (dist[i][j] == -1) { cout << a[i][j] << '\n'; } else if (dist[i][j] > p) { cout << a[i][j] << '\n'; } else { p -= dist[i][j]; char ans; if (p % 2 == 0) { ans = a[i][j]; } else { if (a[i][j] == '1') { ans = '0'; } else { ans = '1'; } } cout << ans << '\n'; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; inline void wait(double seconds) { double endtime = clock() + (seconds * CLOCKS_PER_SEC); while (clock() < endtime) { ; } } inline int fastIn() { register char c = 0; register int a = 0; while (c < 33) c = getchar(); while (c > 33) { a = (a * 10) + (c - '0'); c = getchar(); } return a; } const int MX = 5005; int R, C; char buff[MX]; string grid[MX]; inline void read() { scanf("%d %d\n", &R, &C); for (int i = 0, j1 = R; i < j1; ++i) scanf(" %s", buff), grid[i] = (string)buff; } inline int encodeRC(int r, int c) { return (r * C + c); } inline int decodeR(int val) { return val / C; } inline int decodeC(int val) { return val % C; } struct info { int U[MX]; int D[MX]; int L[MX]; int R[MX]; inline void clear() { memset(U, 0, sizeof(U)), memset(D, 0, sizeof(D)), memset(L, 0, sizeof(L)), memset(R, 0, sizeof(R)); } }; info A, B; inline void mycopy() { memcpy(B.U, A.U, sizeof(A.U)); memcpy(B.D, A.D, sizeof(A.D)); memcpy(B.L, A.L, sizeof(A.L)); memcpy(B.R, A.R, sizeof(A.R)); } const int inf = 1023456789; inline void preprocess() { A.clear(); for (int i = 0; i < R; ++i) { int cnt = -1, ccnt = -1; for (int j = 0; j < C; ++j) { if (grid[i][j] != '.') A.L[encodeRC(i, j)] = cnt, cnt = encodeRC(i, j); if (grid[i][(C - 1) - j] != '.') A.R[encodeRC(i, (C - 1) - j)] = ccnt, ccnt = encodeRC(i, (C - 1) - j); } } for (int j = 0; j < C; ++j) { int cnt = -1, ccnt = -1; for (int i = 0; i < R; ++i) { if (grid[i][j] != '.') A.U[encodeRC(i, j)] = cnt, cnt = encodeRC(i, j); if (grid[(R - 1) - i][j] != '.') A.D[encodeRC((R - 1) - i, j)] = ccnt, ccnt = encodeRC((R - 1) - i, j); } } } inline bool move(char ch, int &tmp) { int li = B.L[tmp], ri = B.R[tmp], ui = B.U[tmp], di = B.D[tmp]; if (li != -1) B.R[li] = ri; if (ri != -1) B.L[ri] = li; if (ui != -1) B.D[ui] = di; if (di != -1) B.U[di] = ui; switch (ch) { case 'L': if (li == -1) return false; tmp = li; break; case 'R': if (ri == -1) return false; tmp = ri; break; case 'U': if (ui == -1) return false; tmp = ui; break; case 'D': if (di == -1) return false; tmp = di; break; case '.': return false; } return true; } inline int solve(int i, int j) { int tmp = encodeRC(i, j); mycopy(); int res = 0; while (true) { char ch = grid[decodeR(tmp)][decodeC(tmp)]; bool flag = move(ch, tmp); if (flag == false) break; ++res; } return res; } inline void proc() { preprocess(); int bst = 0; int cnt = 0; for (int i = 0, j1 = R; i < j1; ++i) { for (int j = 0, j1 = C; j < j1; ++j) { if (grid[i][j] != '.') { int local_bst = solve(i, j); if (local_bst >= bst) { if (local_bst == bst) { ++cnt; } else { cnt = 1; bst = local_bst; } } } } } cout << bst + 1 << ' ' << cnt << '\n'; } int main() { int kase = 1; for (int i = 0, j1 = kase; i < j1; ++i) { read(); proc(); } return 0; }
7
#include <bits/stdc++.h> using namespace std; double f[2005][2005]; double p; int n, T; int main() { cin >> n >> p >> T; for (int i = 0; i < n; i++) f[0][i] = 0.0; f[0][n] = 1; for (int i = 1; i <= T; i++) for (int j = 0; j <= n; j++) { f[i][j] = f[i - 1][j] * (1.0 - p); if (j < n) f[i][j] += f[i - 1][j + 1] * p; } double res = 0; for (int i = 0; i <= n; i++) res += i * f[T][i]; printf("%.9lf\n", (double)n - res); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int sz = 101, sz1 = 6; char brd[sz1][sz][sz], tmp[sz1][sz][sz]; int used[10], res = 1e9, n; void chng() { for (int i = 1; i <= 4; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) brd[i][j][k] = tmp[i][j][k]; } } } int calc() { int a = used[1], b = used[2], c = used[3], d = used[4], op = 0; for (int i = 1; i <= n; i++) { if (i % 2 && brd[a][i][1] == '0') { brd[a][i][1] = '1'; op++; } else if (i % 2 == 0 && brd[a][i][1] == '1') { brd[a][i][1] = '0'; op++; } for (int j = 1; j <= n; j++) { if (j > 1 && brd[a][i][j - 1] == brd[a][i][j]) { if (brd[a][i][j] == '1') brd[a][i][j] = '0'; else brd[a][i][j] = '1'; op++; } if (brd[a][i][j] == brd[b][i][j]) { if (brd[a][i][j] == '1') brd[b][i][j] = '0'; else brd[b][i][j] = '1'; op++; } } } for (int i = 1; i <= n; i++) { if (i % 2 && brd[c][i][1] == '1') { brd[c][i][1] = '0'; op++; } else if (i % 2 == 0 && brd[c][i][1] == '0') { brd[c][i][1] = '1'; op++; } for (int j = 1; j <= n; j++) { if (j > 1 && brd[c][i][j - 1] == brd[c][i][j]) { if (brd[c][i][j] == '1') brd[c][i][j] = '0'; else brd[c][i][j] = '1'; op++; } if (brd[c][i][j] == brd[d][i][j]) { if (brd[c][i][j] == '1') brd[d][i][j] = '0'; else brd[d][i][j] = '1'; op++; } } } return op; } void solve(int pos) { int i; if (pos == 5) { chng(); res = min(res, calc()); return; } for (i = 1; i <= 4; i++) if (!used[i]) { used[i] = pos; solve(pos + 1); used[i] = 0; } } int main() { cin >> n; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { cin >> brd[i][j][k]; tmp[i][j][k] = brd[i][j][k]; } } } solve(1); cout << res << endl; }
3
#include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(char s) { return string(1, s); } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool f = 1; string r = "{"; for (const auto& x : v) { if (!f) r += ", "; f = 0; r += to_string(x); } return r + "}"; } void debug_out() { cout << endl; } void show() { cout << endl; } void pret() { cout << endl; exit(0); } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); } template <typename Head, typename... Tail> void show(Head H, Tail... T) { cout << H << " "; show(T...); } template <typename Head, typename... Tail> void pret(Head H, Tail... T) { cout << H << " "; pret(T...); } const long long MOD = 1e9 + 7; const long long maxn = 300000 + 10; const string yes = "Ya"; const string no = "Tidak"; void solve() { long long a, b, c, d; cin >> a >> b >> c >> d; long long sign = -1; if ((a + b) % 2 == 0) sign = 1; if (sign == -1 and (a + d)) { cout << yes << " "; ; } else { cout << no << " "; ; } if (sign == -1 and (b + c)) { cout << yes << " "; ; } else { cout << no << " "; ; } if (sign == 1 and (b + c)) { cout << yes << " "; ; } else { cout << no << " "; ; } if (sign == 1 and (a + d)) { cout << yes << " "; ; } else { cout << no << " "; ; } cout << '\n'; } int32_t main() { cin.tie(0); ios_base::sync_with_stdio(0); ; long long t; cin >> t; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int p, x, ans = 0; bool b; cin >> p; for (int i = 1; i < p; i++) { b = 1; x = 1; for (int j = 1; j < p - 1; j++) { x = (x * i) % p; if ((x - 1) % p == 0) b = 0; } if (((x * i) - 1) % p != 0) b = 0; if (b) ans++; } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int cond = 1; const int N = 100000; vector<int> edges[N]; bool visited[N]; int t[N]; void dfs(int v, int prev, long long& minC, long long& pluC) { minC = max(0, t[v]); pluC = max(0, -t[v]); for (int i = 0; i < (edges[v].size()); i++) { if (edges[v][i] != prev) { int u = edges[v][i]; long long tmpMinC, tmpPluC; dfs(u, v, tmpMinC, tmpPluC); minC = max(minC, tmpMinC + max(t[v] - t[u], 0)); pluC = max(pluC, tmpPluC + max(t[u] - t[v], 0)); } } } int main() { int n; scanf("%d", &n); for (int i = 0; i < (n - 1); i++) { int u, v; scanf("%d %d", &u, &v); --u; --v; edges[u].push_back(v); edges[v].push_back(u); } for (int i = 0; i < (n); i++) { scanf("%d", &t[i]); visited[i] = false; } long long minR, pluR; dfs(0, -1, minR, pluR); printf("%I64d\n", minR + pluR); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7, MAX = 1005; int N, zero[MAX][MAX][8], dy[8] = {0, 0, 1, -1, 1, 1, -1, -1}, dx[8] = {1, -1, 0, 0, 1, -1, 1, -1}; char A[MAX][MAX]; double B[MAX][MAX], C[MAX][MAX][4]; void print(double ***x, int d) { for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) printf("%.2lf%c", x[i][j][d], j == N ? '\n' : ' '); } int main() { scanf("%d", &N); for (int i = 1; i <= N; ++i) scanf("%s", &A[i][1]); for (int i = 0; i <= N + 1; ++i) A[i][0] = A[i][N + 1] = A[0][i] = A[N + 1][i] = '0'; bool flag = false; for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) { flag = flag || (A[i][j] != '0'); B[i][j] = (A[i][j] == '0' || A[i][j] == '1') ? 0.0 : log(A[i][j] - '0'); for (int d = 0; d < 8; ++d) if (d == 0 || d == 4 || d == 2) { int ii = i - dy[d], jj = j - dx[d]; zero[i][j][d] = A[i][j] == '0' ? 0 : zero[ii][jj][d] + 1; } } if (!flag) return 0 * printf("0\n"); for (int i = 1; i <= N; ++i) for (int j = N; j >= 0; --j) { for (int d = 0; d < 8; ++d) if (d == 5 || d == 1) { int ii = i - dy[d], jj = j - dx[d]; zero[i][j][d] = A[i][j] == '0' ? 0 : zero[ii][jj][d] + 1; } } for (int i = N; i >= 1; --i) for (int j = N; j >= 0; --j) { for (int d = 0; d < 8; ++d) if (d == 7 || d == 3) { int ii = i - dy[d], jj = j - dx[d]; zero[i][j][d] = A[i][j] == '0' ? 0 : zero[ii][jj][d] + 1; } } for (int i = N; i >= 1; --i) for (int j = 0; j <= N; ++j) { int ii = i - dy[6], jj = j - dx[6]; zero[i][j][6] = A[i][j] == '0' ? 0 : zero[ii][jj][6] + 1; } for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) { C[i][j][0] = C[i][j - 1][0] + B[i][j]; C[i][j][1] = C[i - 1][j][1] + B[i][j]; C[i][j][2] = C[i - 1][j - 1][2] + B[i][j]; } for (int i = 1; i <= N; ++i) for (int j = N; j >= 1; --j) C[i][j][3] = C[i - 1][j + 1][3] + B[i][j]; double best = -1; int ti, tj, tr, ts; for (int i = 1; i <= N; ++i) for (int j = 1; j <= N; ++j) if (A[i][j] != '0') { int r[2] = {}; double cand[2] = {}; for (int t = 0; t < 2; ++t) { r[t] = 1e9; for (int d = t * 4; d < t * 4 + 4; ++d) r[t] = min(r[t], zero[i][j][d]); if (t == 0) cand[t] = (C[i][j + (r[t] - 1)][0] - C[i][j - r[t]][0]) + (C[i + (r[t] - 1)][j][1] - C[i - r[t]][j][1]); else cand[t] = (C[i + (r[t] - 1)][j + (r[t] - 1)][2] - C[i - r[t]][j - r[t]][2]) + (C[i + (r[t] - 1)][j - (r[t] - 1)][3] - C[i - r[t]][j + r[t]][3]); cand[t] -= B[i][j]; if (cand[t] > best) { ti = i, tj = j, tr = r[t], ts = t; best = cand[t]; } } } long long ans = 1; if (ts == 0) { for (int x = -tr + 1; x <= tr - 1; ++x) { ans = (ans * (A[ti + x][tj] - '0')) % MOD; if (x) ans = (ans * (A[ti][tj + x] - '0')) % MOD; } } else { for (int x = -tr + 1; x <= tr - 1; ++x) { ans = (ans * (A[ti + x][tj + x] - '0')) % MOD; if (x) ans = (ans * (A[ti + x][tj - x] - '0')) % MOD; } } printf("%lld\n", ans); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main(void) { vector<int> arr[1000]; int num; scanf("%d", &num); int ans = 2; while ((ans) * (ans - 1) / 2 <= num) ans++; ans--; printf("%d\n", ans); int count = 1; for (int i = 1; i < ans; i++) for (int j = i + 1; j <= ans; j++) { arr[i].push_back(count); arr[j].push_back(count); count++; } for (int i = 1; i <= ans; i++) { int len = arr[i].size(); for (int j = 0; j < len; j++) printf(j == 0 ? "%d" : " %d", arr[i][j]); printf("\n"); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int ar[200010] = {0}; int main() { int n, m; scanf("%d%d", &n, &m); int numz = 0; int numf = 0; for (int i = 0; i < n; i++) { scanf("%d", &ar[i]); if (ar[i] == 1) numz++; else numf++; } while (m--) { int a, b; scanf("%d%d", &a, &b); int mid = (b - a + 1) / 2; if (b - a == 0 || (b - a) % 2 == 0) printf("0\n"); else { if ((numz >= mid) && (numf >= mid)) printf("1\n"); else printf("0\n"); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a, b; cin >> a >> b; bool solve = false; for (int i = 1; i < 100; ++i) { for (int j = i; j < 100; ++j) { string s = to_string(i); string t = to_string(j); if (s[0] == a + '0' && t[0] == b + '0' && i + 1 == j) { cout << i << ' ' << j << endl; solve = true; break; } } if (solve) break; } if (!solve) cout << -1; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int t[n], mini = 1e9 + 1, pos; for (int i = 0; i < n; i++) { cin >> t[i]; mini = min(t[i], mini); if (mini == t[i]) pos = i + 1; } int c = 0; for (int i = 0; i < n; i++) c += (t[i] == mini); if (c > 1) cout << "Still Rozdil"; else cout << pos; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 1e5 + 5; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { string x, y; cin >> x >> y; int lx = x.size(), ly = y.size(); int py = 1; while (y[ly - py] == '0') ++py; int px = py; while (lx >= px && x[lx - px] == '0') ++px; cout << px - py << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<vector<pair<int, long long int> > > G(n); int x, y, z; for (int i = 0; i < m; ++i) { cin >> x >> y >> z; G[x - 1].push_back(pair<int, long long int>(y - 1, z)); G[y - 1].push_back(pair<int, long long int>(x - 1, z)); } int k; cin >> k; vector<int> portals(k); for (int i = 0; i < k; ++i) { cin >> portals[i]; portals[i]--; } map<int, int> portal_map; for (int i = 0; i < k; ++i) { portal_map[portals[i]] = i; } vector<long long int> cp(n, -1), dist(n, 100000000000); set<pair<long long int, int> > dijk; for (int i = 0; i < k; ++i) { cp[portals[i]] = portals[i]; dist[portals[i]] = 0; dijk.insert(pair<long long int, int>(0, portals[i])); } while (!dijk.empty()) { pair<long long int, int> top = *dijk.begin(); dijk.erase(dijk.begin()); long long int d = top.first; int v = top.second; for (int i = 0; i < G[v].size(); ++i) { if (d + G[v][i].second < dist[G[v][i].first]) { if (dist[G[v][i].first] != 100000000000) { dijk.erase(dijk.find( pair<long long int, int>(dist[G[v][i].first], G[v][i].first))); } dist[G[v][i].first] = d + G[v][i].second; cp[G[v][i].first] = cp[v]; dijk.insert( pair<long long int, int>(dist[G[v][i].first], G[v][i].first)); } } } vector<vector<pair<int, long long int> > > portal_graph(k); for (int i = 0; i < n; ++i) { for (int j = 0; j < G[i].size(); ++j) { if (cp[i] != cp[G[i][j].first]) { portal_graph[portal_map[cp[i]]].push_back(pair<int, long long int>( portal_map[cp[G[i][j].first]], dist[i] + G[i][j].second + dist[G[i][j].first])); } } } long long int fans = dist[0]; vector<bool> visited(k, false); vector<long long int> prim_dist(k, 100000000000000000); dijk.insert(pair<long long int, int>(0, portal_map[cp[0]])); prim_dist[portal_map[cp[0]]] = 0; while (!dijk.empty()) { pair<long long int, int> top = *dijk.begin(); dijk.erase(dijk.begin()); fans += top.first; int v = top.second; visited[v] = true; for (int i = 0; i < portal_graph[v].size(); ++i) { if (!visited[portal_graph[v][i].first] && portal_graph[v][i].second < prim_dist[portal_graph[v][i].first]) { if (prim_dist[portal_graph[v][i].first] != 100000000000000000) { dijk.erase(dijk.find(pair<long long int, int>( prim_dist[portal_graph[v][i].first], portal_graph[v][i].first))); } prim_dist[portal_graph[v][i].first] = portal_graph[v][i].second; dijk.insert(pair<long long int, int>( prim_dist[portal_graph[v][i].first], portal_graph[v][i].first)); } } } cout << fans; return 0; }
9
#include <bits/stdc++.h> using namespace std; vector<int> parts[100005], nums[2]; int main() { int i, n, k, p, oc, num; cin >> n >> k >> p; for (i = 0; i < n; i++) { cin >> num; nums[num % 2].push_back(num); } oc = k - p; if (nums[1].size() < oc) { cout << "NO"; return 0; } for (i = 0; i < oc; i++) parts[i].push_back(nums[1].back()), nums[1].pop_back(); if (nums[1].size() % 2 || (nums[1].size() / 2 + nums[0].size()) < p) { cout << "NO"; return 0; } for (i = oc; i < k; i++) { if (nums[1].size()) { parts[i].push_back(nums[1].back()), nums[1].pop_back(); parts[i].push_back(nums[1].back()), nums[1].pop_back(); } else parts[i].push_back(nums[0].back()), nums[0].pop_back(); } while (nums[0].size()) parts[k - 1].push_back(nums[0].back()), nums[0].pop_back(); while (nums[1].size()) parts[k - 1].push_back(nums[1].back()), nums[1].pop_back(); cout << "YES" << endl; for (i = 0; i < k; i++) { cout << parts[i].size(); for (p = 0; p < parts[i].size(); p++) cout << " " << parts[i][p]; cout << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 123; const int mod = 1e9 + 7; long long P, K, w[MAXN], cnt, used[MAXN]; void dfs(int n) { if (used[n]) return; cnt++; used[n] = 1; dfs(w[n]); } int main() { cin >> P >> K; for (long long i = 0; i < P; i++) w[i] = K * i % P; long long ans = 1; if (!K) { for (int i = 1; i < P; i++) ans = ans * P % mod; cout << ans << endl; return 0; } for (long long i = 0; i < P; i++) if (!used[i]) { cnt = 0; dfs(i); if (cnt != 1 || (cnt == 1 && K == 1)) ans *= P; ans %= mod; } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a[n], b[m], c[n - 1], d[m]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> b[i]; if (m == 1) cout << n; else { for (int i = 0; i < n - 1; i++) c[i] = a[i + 1] - a[i]; for (int i = 0; i < m - 1; i++) d[i] = b[i + 1] - b[i]; d[m - 1] = 1000000000; int pi[m]; pi[0] = 0; int i = 1, j = 0; for (; i < m; i++) { j = pi[i - 1]; while (j && d[i] != d[j]) j = pi[j - 1]; if (d[i] == d[j]) pi[i] = j + 1; else pi[i] = 0; } i = 0, j = 0; int count = 0; for (; i < n - 1; i++) { while (j && c[i] != d[j]) j = pi[j - 1]; if (c[i] == d[j]) j++; if (j == m - 1) ++count; } cout << count; } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long arr[100010]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long num, k, x, minn = 1e18, res = 1; cin >> num >> k; for (int i = 0; i < num; i++) { cin >> x; arr[i % k] += x; } for (int i = 0; i < 100010; i++) { if (arr[i] < minn && arr[i] != 0) { minn = arr[i]; res = i + 1; } } cout << res << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, a, b, c, s, x; int main() { cin >> n >> a >> b; while (n--) { cin >> x; if (x == 1) { if (a) a--; else if (b) b--, c++; else if (c) c--; else s++; } else if (b) b--; else s += 2; } cout << s << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9 + 7; const int N = 65; const long double eps = 1e-7; struct Task { int k, q; vector<pair<int, int>> queries; vector<int> result; Task(int k, int q) : k(k), q(q), queries(q), result(q) { int i = 0; for (auto& item : queries) { cin >> item.first; item.second = i++; } sort(queries.begin(), queries.end()); } void run() { vector<long double> f(k + 1, 0), g(k + 1, 0); f[0] = 1.0; int pos = 0; for (int day = 1; pos < q; ++day) { for (int c = 0; c <= k; ++c) { g[c] += ((long double)c / k) * f[c]; if (c < k) { g[c + 1] += ((long double)(k - c) / k) * f[c]; } } for (int c = 0; c <= k; ++c) { f[c] = g[c]; g[c] = 0.0; } while (pos < q && f[k] > (queries[pos].first - eps) / 2000.0) { result[queries[pos].second] = day; ++pos; } } for (auto& item : result) { cout << item << '\n'; } } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int k, q; cin >> k >> q; Task a(k, q); a.run(); return 0; }
7
#include <bits/stdc++.h> using namespace std; char a[200005]; int main() { int n, t, z; cin >> n >> t; a[0] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] == '.') z = i + 1; } int i; for (i = z; i <= n; i++) { if (a[i] >= '5' && i == z && t > 0) { for (int j = i - 2; j >= 1; j--) if (a[j] == '9') a[j] = '0'; else { a[j]++; break; } if (a[1] == '0') cout << 1; for (int j = 1; j <= i - 2; j++) cout << a[j]; cout << endl; return 0; } if (a[i] >= '5' && i - 1 >= z && t > 0) { a[i - 1] = (int(a[i - 1] - '0' + 1)) % 10 + '0'; n = i - 1; i = i - 2; t--; } } for (int i = 1; i <= n; i++) cout << a[i]; cout << endl; }
4
#include <bits/stdc++.h> const long long inf = 0x3f3f3f3f3f3f3f3LL; const long long mod = (long long)1e9 + 7; using namespace std; template <class T> void smin(T& a, T val) { if (a > val) a = val; } template <class T> void smax(T& a, T val) { if (a < val) a = val; } const long long N = 4 * (long long)1e3 + 10; long long n, v[N], d[N], p[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; vector<long long> a(n); for (long long i = 0; i < n; ++i) { cin >> v[i] >> d[i] >> p[i]; } vector<long long> vals; for (long long i = 0; i < n; ++i) { if (p[i] < 0) continue; vals.push_back(i + 1); long long ptr = v[i], inc2 = 0; for (long long j = i + 1; j < n; ++j) { if (p[j] < 0) continue; p[j] -= ptr; p[j] -= inc2; --ptr; smax(ptr, 0LL); if (p[j] < 0) inc2 += d[j]; } } cout << (long long)(vals.size()) << '\n'; for (auto it : vals) cout << it << ' '; return 0; }
5
#include <bits/stdc++.h> using namespace std; long long n, m, i, j, b[103], k, x; bool z; struct info { double k; double f; } a[103]; int main() { cin >> n >> m; for (i = 0; i < m; i++) cin >> a[i].k >> a[i].f; if (n == 1) { cout << 1; return 0; } for (i = 1; i <= 100; i++) { z = true; for (j = 0; j < m; j++) if (ceil(1.0 * a[j].k / i) != a[j].f) { z = false; break; } if (z) b[x] = ceil(1.0 * n / i), x++; } for (i = 1; i < x; i++) if (b[i] != b[i - 1]) { cout << -1; return 0; } cout << b[0]; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; while (cin >> n >> m) { vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } vector<int> c(m), d(m); for (int i = 0; i < m; i++) { cin >> c[i] >> d[i]; } const int maxn = 1e6; vector<int> r(maxn + 2); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i] <= c[j]) { r[c[j] - a[i]] = max(r[c[j] - a[i]], d[j] - b[i] + 1); } } } int ans = maxn * 2; for (int x = maxn; x >= 0; x--) { r[x] = max(r[x], r[x + 1]); ans = min(ans, x + r[x]); } cout << ans << '\n'; } return 0; }
6
/* Worldwide Combos, free and competitive block-stacking game! https://www.worldwide-combos.com */ #include<iostream> #include<vector> #include<array> #include<string> #include<string.h> #include<sstream> #include<map> #include<unordered_map> #include<chrono> #include<set> #include<unordered_set> #include<ctime> #include<bitset> #include<queue> #include<deque> #include<cmath> #include<algorithm> #include<iomanip> #include<stack> #include<random> #include<cassert> #include<complex> #include<fstream> //#include<pthread.h> #pragma GCC optimize "-O3" #pragma GCC target "tune=native" #define fin(i,n) for (int i = 0; i < n; i++) #define fin2(i,a,b) for (int i = a; i < b; i++) #define ford(i,n) for (int i = n-1; i >= 0; i--) #define ford2(i,a,b) for (int i = b-1; i >= a; i--) #define mp make_pair #define mt make_tuple #define pb push_back #define mod 998244353 //#define PI 3.1415926535897932384626 #define EXP1 2.718281828459045 #define si(n) scanf("%d", &n) #define sii(n,m) scanf("%d %d", &n, &m) #define siii(n,m,k) scanf("%d %d %d", &n, &m, &k) #define sl(n) scanf("%lld", &n) #define sll(n,m) scanf("%lld %lld", &n, &m) #define slll(n,m,k) scanf("%lld %lld %lld", &n, &m, &k) #define sd(n) scanf("%lf", &n) #define sdd(n,m) scanf("%lf %lf", &n, &m) #define sddd(n,m,k) scanf("%lf %lf %lf", &n, &m, &k) #define sc(c) scanf("%c", &c) #define ss(s) scanf("%s", s) #define sai(t,n) fin(iz,n) { scanf("%d", &t[iz]); } #define sal(t,n) fin(iz,n) { scanf("%lld", &t[iz]); } #define sad(t,n) fin(iz,n) { scanf("%lf", &t[iz]); } #define sald(t,n) fin(iz,n) { scanf("%Lf", &t[iz]); } #define pc(n) printf("%c\n", n) #define ps(s) printf("%s\n", s) #define pi(n) printf("%d\n", n) #define pii(n,m) printf("%d %d\n", n, m) #define piii(n,m,k) printf("%d %d %d\n", n, m, k) #define pl(n) printf("%lld\n", n) #define pll(n,m) printf("%lld %lld\n", n, m) #define plll(n,m,k) printf("%lld %lld %lld\n", n, m, k) #define pd(n) printf("%.9lf\n", n) #define pdd(n,m) printf("%.9lf %.9lf\n", n, m) #define pddd(n,m,k) printf("%.9lf %.9lf %.9lf\n", n, m, k) #define pai(t,n) fin(rz,n) { printf("%d ", t[rz]); } printf("\n") #define pal(t,n) fin(rz,n) { printf("%lld ", t[rz]); } printf("\n") #define pad(t,n) fin(rz,n) { printf("%.9lf ", t[rz]); } printf("\n") #define OK ps("OK") #define OK1 ps("OK1") #define OK2 ps("OK2") #define L long long int #define D double #define LD long double #define PII pair<int, int> #define MII map<int, int> #define VPII vector<PII> #define VVPII vector<VPII> #define VL vector<L> #define VI vector<int> #define VVI vector<VI> #define VD vector<D> #define VLD vector<LD> #define VVD vector<VD> #define VVPII vector<VPII> #define VC vector<char> #define INF 0x7fffffff int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; using namespace std; VI g[200200]; int d[200200] = {0}; int main() { //freopen ("input.txt","r",stdin); //freopen ("output.txt","w",stdout); int tt = 1; si(tt); fin2(zz, 1, tt+1) { int n, u,v; si(n); L a[200200]; sal(a, n); L s = 0; fin(i, n) { s += a[i]; d[i] = 0; } fin(i, n-1) { sii(u, v); u--; v--; g[u].pb(v); g[v].pb(u); d[u]++; d[v]++; } VL l; fin(i, n) { fin(j, d[i]-1) { l.pb(a[i]); } } sort(l.begin(), l.end()); VL r; r.pb(s); fin(i, n-2) { r.pb(r[i]+l[l.size()-1-i]); } pal(r, n-1); //sii(n, m); //siii(n, m, k); //ss(s); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x * f; } int n, d[1005], num[1005]; int tot, id[1005], cur[1005]; int nedge, head[1005]; struct Edge { int to, nxt; } edge[200005]; void add(int x, int y) { edge[++nedge].to = y; edge[nedge].nxt = head[x]; head[x] = nedge; } bool dfs(int k) { id[k] = tot; bool f = 0; for (int i = head[k]; i; i = edge[i].nxt) { int u = edge[i].to; if (!id[u]) cur[u] = cur[k] ^ 1, f |= dfs(u); else if (cur[u] == cur[k]) return 1; } return f; } int bfs(int k) { queue<int> q; memset(d, -1, sizeof(d)); q.push(k); d[k] = 0; while (!q.empty()) { int now = q.front(); q.pop(); for (int i = head[now]; i; i = edge[i].nxt) { int u = edge[i].to; if (d[u] < 0) { d[u] = d[now] + 1; q.push(u); } } } int res = 0; for (int i = 1; i <= n; i++) res = max(res, d[i]); return res; } int main() { n = read(); for (int m = read(); m; m--) { int x = read(), y = read(); add(x, y); add(y, x); } for (int i = 1; i <= n; i++) if (!id[i]) { tot++; if (dfs(i)) return puts("-1"), 0; } for (int i = 1; i <= n; i++) num[id[i]] = max(num[id[i]], bfs(i)); int ans = 0; for (int i = 1; i <= tot; i++) ans += num[i]; printf("%d", ans); }
9
#include <bits/stdc++.h> using namespace std; int mod = 998244353; long long f_pow(long long a, long long b) { long long res = 1, temp = a; while (b) { if (b & 1) res = res * temp % mod; temp = temp * temp % mod; b >>= 1; } return res; } long long fra[1000005]; long long inv[1000005]; void build() { fra[0] = 1; for (int i = 1; i <= 1000000; i++) fra[i] = fra[i - 1] * i % mod; inv[1000000] = f_pow(fra[1000000], mod - 2); for (int i = 999999; i >= 0; i--) inv[i] = inv[i + 1] * (i + 1) % mod; } long long C(int a, int b) { return fra[a] * inv[b] % mod * inv[a - b] % mod; } int pool[2][1000005]; int* cal[2]; int l[1000005]; int r[1000005]; int q[1000005]; int main() { build(); cal[0] = pool[0] + 1; cal[1] = pool[1] + 1; char c[1000005]; scanf("%s", c + 1); int len = strlen(c + 1); for (int i = 1; c[i] != 0; i++) { l[i] = l[i - 1]; r[i] = r[i - 1]; q[i] = q[i - 1]; if (c[i] == '(') l[i]++; if (c[i] == ')') r[i]++; if (c[i] == '?') q[i]++; } int cnt = 0; for (int i = 1; c[i] != 0; i++) { if (c[i] == '?') cnt++; } for (int i = 0; i <= cnt; i++) { cal[0][i] = (cal[0][i - 1] + C(cnt, i)) % mod; } for (int i = 0; i < cnt; i++) { cal[1][i] = (cal[1][i - 1] + C(cnt - 1, i)) % mod; } long long ans = 0; for (int i = 1; c[i] != 0; i++) { if (c[i] == ')') continue; int ll = l[i - 1]; int rr = r[len] - r[i]; int lq = q[i - 1], rq = q[len] - q[i]; if (c[i] == '?') { if (rq + rr - ll >= 0) ans += cal[1][min(rq + rr - ll - 1, cnt - 1)]; } else { if (rq + rr - ll >= 0) ans += cal[0][min(rq + rr - ll - 1, cnt)]; } } printf("%lld\n", ans % mod); }
10
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int fx[] = {-1, +0, +1, +0, +1, +1, -1, -1, +0}; int fy[] = {+0, -1, +0, +1, +1, -1, +1, -1, +0}; int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; template <typename T> inline bool isLeap(T y) { return (y % 400 == 0) || (y % 100 ? y % 4 == 0 : false); } template <typename T> inline T GCD(T a, T b) { a = abs(a); b = abs(b); if (a < b) swap(a, b); while (b) { a = a % b; swap(a, b); } return a; } template <typename T> inline T EGCD(T a, T b, T &x, T &y) { if (a == 0) { x = 0; y = 1; return b; } T x1, y1; T d = EGCD(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } template <typename T> inline T LCM(T x, T y) { T tp = GCD(x, y); if ((x / tp) * 1. * y > 9e18) return 9e18; return (x / tp) * y; } template <typename T> inline T BigMod(T A, T B, T M = MOD) { T ret = 1; while (B) { if (B & 1) ret = (ret * A) % M; A = (A * A) % M; B = B >> 1; } return ret; } template <typename T> inline T InvMod(T A, T M = MOD) { return BigMod(A, M - 2, M); } template <typename T> T scani(T &n) { n = 0; bool negative = false; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') negative = true; c = getchar(); } while (c >= '0' && c <= '9') { n = n * 10 + c - 48; c = getchar(); } if (negative) n = ~(n - 1); return n; } template <typename T> void write(T n, int type = true) { if (n < 0) { putchar('-'); n = -n; } if (!n) { putchar('0'); if (type == 32) putchar(' '); else if (type) putchar('\n'); return; } char buff[22]; int len = 0; while (n) buff[len++] = n % 10 + 48, n /= 10; for (int i = len - 1; i >= 0; i--) putchar(buff[i]); if (type == 32) putchar(' '); else if (type) putchar('\n'); } int scans(char *a) { int i = 0; char c = 0; while (c < 33) c = getchar(); while (c > 33) { a[i++] = c; c = getchar(); } a[i] = 0; return i; } template <typename T> inline T SqrDis(T x1, T y1, T x2, T y2) { return ((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)); } template <typename T> inline T Area2(T Ax, T Ay, T Bx, T By, T Cx, T Cy) { T ret = Ax * (By - Cy) + Bx * (Cy - Ay) + Cx * (Ay - By); if (ret < 0) return ret = -ret; return ret; } const int N = 2000006; const int M = 44444; const unsigned long long hs = 3797; char arr[1111][1111]; int p0[1111][1111]; int p1[1111][1111]; int n, m; int sx, sy, ex, ey; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", &arr[i]); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (arr[i][j] == 'S') sx = i, sy = j; if (arr[i][j] == 'T') ex = i, ey = j; } for (int j = sy; j >= 0; j--) { if (arr[sx][j] == '*') break; if (arr[sx][j] == 'T') { puts("YES"); return 0; } p0[sx][j] = 1; } for (int j = sy; j < m; j++) { if (arr[sx][j] == '*') break; if (arr[sx][j] == 'T') { puts("YES"); return 0; } p0[sx][j] = 1; } for (int j = sx; j >= 0; j--) { if (arr[j][sy] == '*') break; if (arr[j][sy] == 'T') { puts("YES"); return 0; } p0[j][sy] = 1; } for (int j = sx; j < n; j++) { if (arr[j][sy] == '*') break; if (arr[j][sy] == 'T') { puts("YES"); return 0; } p0[j][sy] = 1; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (p0[i][j]) { p1[i][j] = 1; for (int k = 0; k < 4; k++) { int x = i; int y = j; while (true) { x = x + fx[k]; y = y + fy[k]; if (x < 0 || y < 0 || x >= n || y >= m || arr[x][y] == '*') break; if (arr[x][y] == 'T') { puts("YES"); return 0; } p1[x][y] = 1; } } } } } for (int k = 0; k < 4; k++) { int x = ex; int y = ey; while (true) { x = x + fx[k]; y = y + fy[k]; if (x < 0 || y < 0 || x >= n || y >= m || arr[x][y] == '*') break; if (p1[x][y]) { puts("YES"); return 0; } } } puts("NO"); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, t; cin >> n >> t; pair<double, double> r[2000] = {}; for (int i = 0; i < n; i++) { cin >> r[i].first >> r[i].second; } sort(r, r + n); int ans = 2; for (int i = 0; i < n - 1; i++) { if (t > r[i + 1].first - r[i + 1].second / 2 - (r[i].first + r[i].second / 2)) continue; if (t == r[i + 1].first - r[i + 1].second / 2 - (r[i].first + r[i].second / 2)) ans++; if (t < r[i + 1].first - r[i + 1].second / 2 - (r[i].first + r[i].second / 2)) ans += 2; } cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; char s[200100]; char t[200100]; int mxr[200100]; int lst[30]; int mxGL[200100][30]; int main() { scanf("%s%s", s, t); int ns = strlen(s); int nt = strlen(t); mxr[ns] = 0; for (int i = ns - 1; i >= 0; i--) { if (mxr[i + 1] == nt) mxr[i] = nt; else if (s[i] == t[nt - mxr[i + 1] - 1]) mxr[i] = mxr[i + 1] + 1; else mxr[i] = mxr[i + 1]; } for (int i = 0; i < 26; i++) lst[i] = -2; for (int i = 0; i < nt; i++) { lst[t[i] - 'a'] = i; for (int j = 0; j < 26; j++) mxGL[i][j] = lst[j] + 1; } int mx = 0; for (int i = 0; i < ns; i++) { if (mx < nt && t[mx] == s[i]) mx++; if (!mx) { cout << "No"; return 0; } int tt = mxGL[mx - 1][s[i] - 'a']; if (tt == -1 || tt + mxr[i + 1] < nt) { cout << "No"; return 0; } } cout << "Yes"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int t; long long n, m; long long aggr; long long pos; long long dir; bool rslt; long long getpos(long long s) { if (s == 1) return 1; return s / 2 - getpos(s / 2) + 1; } long long paggr; long long ppos; long long pdir; long long pm; int main() { ios_base::sync_with_stdio(0); cin >> t; for (int tcount = (0); tcount < (t); ++tcount) { cin >> n; m = n; aggr = 0; dir = 1; pos = getpos(n); cout << "? " << pos << endl; cin >> rslt; pm = -1; while (m > 1) { pos += dir * ((m + 1) / 2 + aggr); dir *= -1; cout << "? " << pos << endl; cin >> rslt; if (rslt) { if (m % 2 == 1) { paggr = aggr; ppos = pos; pdir = dir; pm = m; } else pm = -1; m /= 2; } else { aggr += (m + 1) / 2; m /= 2; } } if (pm != -1) { if (pm != 3) { pos = ppos + pdir * (getpos(pm)); cout << "? " << pos << endl; cin >> rslt; } pos += pdir * (pm / 2 + paggr); cout << "? " << pos << endl; cin >> rslt; if (!rslt) aggr = pm / 2 + paggr; } cout << "= " << aggr + 1 << endl; } }
9
#include <bits/stdc++.h> using namespace std; int main() { int n, arr[101] = {0, 0}, x; cin >> n; for (int i = 0; i < n; i++) { cin >> x; for (int j = 1; j <= 100; j++) { if (j == x) arr[j]++; } } sort(arr, arr + 101); cout << arr[100] << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long M = 1e9 + 7; double pi = acos(-1.0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long powerm(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x) % M; y = y >> 1; x = (x * x) % M; } return res % M; } long long power(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long long gcd(long long a, long long b) { if (b > a) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } const int N = 7e5 + 500; vector<pair<int, int> > adj[N]; vector<int> col(N); vector<bool> vis(N); vector<int> cnt(2); bool ok; void dfs(int u, int n) { vis[u] = true; cnt[col[u]]++; for (pair<int, int> it : adj[u]) { if (vis[it.first]) { if ((col[it.first] ^ it.second) != col[u]) ok = 0; } else { col[it.first] = col[u] ^ it.second; dfs(it.first, n); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { adj[i].clear(); col[i] = -1; vis[i] = false; } for (int i = 0; i < m; i++) { int u, v; string s; cin >> u >> v >> s; if (s == "imposter") adj[u].push_back({v, 1}), adj[v].push_back({u, 1}); else { adj[u].push_back({v, 0}); adj[v].push_back({u, 0}); } } int ans = 0; ok = 1; for (int i = 1; i <= n; i++) { if (vis[i]) continue; col[i] = 0; cnt[0] = cnt[1] = 0; dfs(i, n); ans += max(cnt[0], cnt[1]); } if (!ok) ans = -1; cout << ans << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; bool prime(long long n) { for (long long d = 2; d * d <= n; d++) if (n % d == 0) return false; return true; } int main() { long long n; cin >> n; if (prime(n)) { cout << 1 << endl << 0; return 0; } for (long long d = 2; d * d <= n; d++) if (n % d == 0) { for (long long j = 2; j * j <= d; j++) if (d % j == 0 && prime(j) && prime(d / j)) { cout << 1 << endl << d; return 0; } for (long long j = 2; j * j <= n / d; j++) if ((n / d) % j == 0 && prime(j) && prime((n / d) / j)) { cout << 1 << endl << n / d; return 0; } } cout << 2; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 5500; long long int f[maxn], n; long long int solve(int l, int r) { long long int mx = 9999999999; vector<pair<int, int> > pi; for (int i = l; i <= r; i++) mx = min(mx, f[i]); bool flag = false; int duan[2]; for (int i = l; i <= r; i++) { f[i] -= mx; if (f[i]) { if (flag == false) { flag = true; duan[0] = i; } } else if (f[i] == 0) { if (flag == true) { flag = false; duan[1] = i - 1; pi.push_back((make_pair(duan[0], duan[1]))); } } } if (flag == true) { flag = false; duan[1] = r; pi.push_back((make_pair(duan[0], duan[1]))); } long long int digui = 0; int sz = pi.size(); for (int i = 0; i < sz; i++) { digui += solve(pi[i].first, pi[i].second); ; } return min((long long int)(r - l + 1), digui + mx); } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> f[i]; cout << solve(1, n) << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const double PI = 4 * atan(1); int N, M; int A[300013]; int pre[300013]; int dfib[300013]; vector<pair<int, int> > updates; int f[300013], fp[300013]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> M; for (int i = 0; i < N; i++) cin >> A[i]; f[1] = 1; for (int i = 2; i <= N + 2; i++) { f[i] = (f[i - 1] + f[i - 2]) % 1000000009; fp[i] = (fp[i - 1] + f[i - 1]) % 1000000009; } for (int q = 0; q < M; q++) { if (q % 500 == 0) { for (int i = 1; i < N; i++) { dfib[i] = (dfib[i] + dfib[i - 1]) % 1000000009; if (i >= 2) dfib[i] = (dfib[i] + dfib[i - 2]) % 1000000009; } for (int i = 0; i < N; i++) { A[i] = (A[i] + dfib[i]) % 1000000009; pre[i + 1] = (pre[i] + A[i]) % 1000000009; } memset(dfib, 0, sizeof dfib); updates.clear(); } int t, l, r; cin >> t >> l >> r; --l; if (t == 1) { updates.emplace_back(l, r); dfib[l]++; dfib[r] = (dfib[r] + 1000000009 - f[r - l + 1]) % 1000000009; dfib[r + 1] = (dfib[r + 1] + 1000000009 - f[r - l]) % 1000000009; } else { int ans = (pre[r] - pre[l] + 1000000009) % 1000000009; for (const auto& p : updates) { if (p.first < r && p.second > l) { int lo = 1 + max(0, l - p.first); int hi = 1 + min(p.second, r) - p.first; ans = (ans + (fp[hi] - fp[lo] + 1000000009) % 1000000009) % 1000000009; } } cout << ans << '\n'; } } cout.flush(); return 0; }
8
#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, k, x; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; while (n--) { cin >> k >> x; cout << 9 * (k - 1) + x << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, t1, t2, k; pair<int, int> res[100001]; bool cmp(pair<int, int> a, pair<int, int> b) { if (a.second == b.second) { return a.first < b.first; } return a.second > b.second; } int cal(int a, int b) { return a * t1 * (100 - k) + b * t2 * 100; } int main() { ios_base ::sync_with_stdio(false); int i; cin >> n >> t1 >> t2 >> k; for (i = 0; i < n; i++) { int a, b; cin >> a >> b; res[i].first = i + 1; res[i].second = max(cal(a, b), cal(b, a)); } sort(res, res + n, cmp); for (i = 0; i < n; i++) { printf("%d %.2f\n", res[i].first, (double)res[i].second / 100.0); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n; int p[200005], ind_of[200005], l_max[200005], r_max[200005]; int solve(int l, int r) { if (l == r) return 0; int ans = 0; int m = l + (r - l) / 2; ans += solve(l, m); ans += solve(m + 1, r); for (int i = l; i <= m; ++i) l_max[i] = p[i]; for (int i = r; i > m; --i) r_max[i] = p[i]; for (int i = m - 1; i >= l; --i) l_max[i] = std::max(l_max[i], l_max[i + 1]); for (int i = m + 2; i <= r; ++i) r_max[i] = std::max(r_max[i], r_max[i - 1]); for (int a = l; a <= m; ++a) { if (l_max[a] - p[a] < 0) continue; int b = ind_of[l_max[a] - p[a]]; if (b > m && b <= r && r_max[b] <= l_max[a]) ++ans; } for (int b = m + 1; b <= r; ++b) { if (r_max[b] - p[b] < 0) continue; int a = ind_of[r_max[b] - p[b]]; if (a >= l && a <= m && l_max[a] <= r_max[b]) ++ans; } return ans; } int main() { cin >> n; ind_of[0] = -1; for (int i = 0; i < n; ++i) { scanf("%d", p + i); ind_of[p[i]] = i; } cout << solve(0, n - 1) << endl; char I; cin >> I; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> m(n); for (int i = 0; i < n; ++i) { cin >> m[i]; } vector<int> r(n); for (int i = 0, t; i < n; ++i) { cin >> r[i]; } int used = 0; for (int i = 0; i < 1e6; ++i) { for (int k = 0; k < n; ++k) { if (i % m[k] == r[k]) { used += 1; break; } } } cout << fixed << setprecision(6) << (used * 1.0 / 1e6); return 0; }
4
#include <bits/stdc++.h> using std::cin; using std::cout; using std::endl; using std::getline; using std::string; using std::vector; using std::ws; size_t graph[5001][5001]; int dfs_num[5001]; size_t n; void dfs(size_t p, size_t u); int main(void) { std::ios_base::sync_with_stdio(false); cin >> n >> ws; string line; for (size_t line_cnt = 0; line_cnt < n; line_cnt++) { getline(cin, line); for (size_t char_cnt = 0; char_cnt < n; char_cnt++) graph[line_cnt][char_cnt] = (line.at(char_cnt) == '1'); } for (size_t counter = 0; counter < n; counter++) if (!dfs_num[counter]) { dfs_num[counter] = 1; dfs(-1, counter); } cout << -1 << endl; return 0; } void dfs(size_t p, size_t u) { for (size_t counter = 0; counter < n; counter++) if (graph[u][counter]) { if (!dfs_num[counter]) { dfs_num[counter] = 1; dfs(u, counter); } else if (p >= 0 && graph[counter][p]) { cout << counter + 1 << " " << p + 1 << " " << u + 1 << endl; exit(0); } } }
6
#include <bits/stdc++.h> using namespace std; long long dpf[110][100][60]; long long dpg[110][100][60]; int diff; int likes_init = 0; const long long mod = 998244353; long long fast_exp(long long b, int e) { b = b % mod; if (e == 0) return 1; if (e == 1) return b; if (e % 2 == 1) return (b * fast_exp(b, e - 1)) % mod; return fast_exp((b * b) % mod, e / 2); } long long modinv(long long x) { assert(x % mod != 0); return fast_exp(x, mod - 2); } long long f(int curr, int likes, int runs) { if (dpf[curr][likes - likes_init][runs] == 1LL << 40) { int dislike = likes + runs - diff; if (curr == 0) return 0; if (runs == 0) { return curr; } if (dislike < 0) return 0; long long tmp1, tmp2, tmp3; tmp1 = (curr == 0 ? 0 : curr * f(curr + 1, likes + 1, runs - 1)); tmp2 = (likes - curr == 0 ? 0 : (likes - curr) * f(curr, likes + 1, runs - 1)); tmp3 = (dislike == 0 ? 0 : dislike * f(curr, likes, runs - 1)); long long tmp = (tmp1 + tmp2 + tmp3) % mod; long long ans = (tmp * modinv(likes + dislike)) % mod; return dpf[curr][likes - likes_init][runs] = ans; } else { return dpf[curr][likes - likes_init][runs]; } } long long g(int curr, int likes, int runs) { if (dpg[curr][likes - likes_init][runs] == 1LL << 40) { int dislike = likes + runs - diff; if (curr == 0) return 0; if (runs == 0) { return curr; } if (dislike < 0) return 0; long long tmp1 = (curr == 0 ? 0 : curr * g(curr - 1, likes, runs - 1)); long long tmp2 = (likes == 0 ? 0 : likes * g(curr, likes + 1, runs - 1)); long long tmp3 = (dislike - curr == 0 ? 0 : (dislike - curr) * g(curr, likes, runs - 1)); long long tmp = (tmp1 + tmp2 + tmp3) % mod; long long ans = (tmp * modinv(likes + dislike)) % mod; return dpg[curr][likes - likes_init][runs] = ans; } else { return dpg[curr][likes - likes_init][runs]; } } int main() { int n, m; cin >> n >> m; diff = m; int a[n]; for (int i = 0; i < 110; i++) { for (int j = 0; j < 100; j++) { for (int k = 0; k < 60; k++) { dpf[i][j][k] = 1LL << 40; dpg[i][j][k] = 1LL << 40; } } } bool good[n]; int tl = 0; for (int i = 0; i < n; i++) { cin >> good[i]; } for (int i = 0; i < n; i++) { cin >> a[i]; if (good[i]) { diff += a[i]; tl += a[i]; } else diff -= a[i]; } likes_init = tl; for (int i = 0; i < n; i++) { if (good[i]) { cout << (f(a[i], tl, m) % mod + mod) % mod << "\n"; } else { cout << (g(a[i], tl, m) % mod + mod) % mod << "\n"; } } return 0; }
7
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 4e2 + 10; const int inf = 0x3f3f3f3f; int f[maxn][maxn]; string s, t; bool check(string t1, string t2) { int n = s.length(), n1 = t1.length(), n2 = t2.length(); memset(f, -1, sizeof(f)); f[0][0] = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j <= n1; ++j) { if (f[i][j] == -1) continue; f[i + 1][j] = max(f[i + 1][j], f[i][j]); if (j < n1 && s[i] == t1[j]) f[i + 1][j + 1] = max(f[i + 1][j + 1], f[i][j]); if (f[i][j] < n2 && s[i] == t2[f[i][j]]) f[i + 1][j] = max(f[i + 1][j], f[i][j] + 1); } } return f[n][n1] >= n2; } int main() { int T; scanf("%d", &T); while (T--) { cin >> s >> t; int flag = 0; for (int i = 0; i < t.size(); ++i) { if (check(t.substr(0, i), t.substr(i, t.size() - i))) { flag = 1; break; } } if (flag) printf("YES\n"); else printf("NO\n"); } return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { bool occu[4][4]; for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) occu[i][j] = false; string s; cin >> s; int ss = s.size(); int v = 0; int h = 0; for (int i = 0; i < ss; i++) { if (v % 2 == 0 && s[i] == 48) { cout << "1 1" << endl; v++; } else if (v % 2 && s[i] == 48) { cout << "3 1" << endl; v++; } else if (h % 4 == 0 && s[i] == 49) { cout << "1 3" << endl; h++; } else if (h % 4 == 1 && s[i] == 49) { cout << "2 3" << endl; h++; } else if (h % 4 == 2 && s[i] == 49) { cout << "3 3" << endl; h++; } else if (h % 4 == 3 && s[i] == 49) { cout << "4 3" << endl; h++; } } }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; int p[maxn], p2[maxn]; set<int> A, B; int main() { int n, a, b; scanf("%d%d%d", &n, &a, &b); for (int i = 0; i < n; ++i) { scanf("%d", &p[i]); A.insert(p[i]); } for (int i = 0; i < n; ++i) { { int x = a - p[i]; if (A.find(x) == A.end()) { B.insert(p[i]); A.erase(p[i]); } } } int tail = 0; for (set<int>::iterator it = B.begin(); it != B.end(); ++it) p2[tail++] = *it; bool flag = true; for (int i = 0; i < tail; ++i) { int x = b - p2[i]; if (B.find(x) == B.end()) { if (A.find(x) != A.end()) { B.insert(x); p2[tail++] = x; A.erase(x); A.erase(a - x); { p2[tail++] = a - x; B.insert(a - x); } } else { flag = false; break; } } } if (!flag) puts("NO"); else { puts("YES"); for (int i = 0; i < n; ++i) { if (i) printf(" "); if (A.find(p[i]) != A.end()) printf("0"); else printf("1"); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios ::sync_with_stdio(false); int n, m, c; cin >> n >> m >> c; vector<long long int> a(n), b(m); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { cin >> b[i]; for (int j = i, k = 0; k <= n - m; j++, k++) { a[j] += b[i]; } } for (int i = 0; i < n; i++) { cout << a[i] % c << " "; } }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; const int inf = 1e9 + 7; int head[maxn], to[maxn << 1], nxt[maxn << 1]; int Vertex; int qu[maxn], qf, qb, dist[2][maxn], fa[maxn], i, v, ok; int dijk(int u, int *Dist) { for (i = 1; i <= Vertex; i++) Dist[i] = inf; Dist[u] = 0; qf = qb = 0; qu[qb++] = u; while (qf < qb) { u = qu[qf++]; for (i = head[u]; i; i = nxt[i]) { v = to[i]; if (Dist[v] > Dist[u] + 1) { if (ok) fa[v] = u; Dist[v] = Dist[u] + 1; qu[qb++] = v; } } } return qu[qf - 1]; } int u, U, V, E; int vis[maxn]; long long answer; vector<pair<pair<int, int>, int> > res; int main() { scanf("%d", &Vertex); for (i = 1; i < Vertex; i++) { scanf("%d%d", &u, &v); nxt[++E] = head[u]; to[E] = v; head[u] = E; nxt[++E] = head[v]; to[E] = u; head[v] = E; } U = dijk(1, dist[0]); V = dijk(U, dist[0]); ok = 1; dijk(V, dist[1]); u = U; while (u) { vis[u] = 1; u = fa[u]; } for (i = qf - 1; i >= 0; i--) { u = qu[i]; if (!vis[u]) { if (dist[0][u] > dist[1][u]) { answer += dist[0][u]; res.push_back(make_pair(make_pair(u, U), u)); } else { answer += dist[1][u]; res.push_back(make_pair(make_pair(u, V), u)); } } } u = U; while (u != V) { answer += dist[1][u]; res.push_back(make_pair(make_pair(u, V), u)); u = fa[u]; } cout << answer << endl; for (i = 0; i < ((int)((res).size())); i++) { printf("%d %d %d\n", res[i].first.first, res[i].first.second, res[i].second); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; while (TESTS--) { long long int n, l, a; cin >> n >> l >> a; vector<long long int> v; v.push_back(0); v.push_back(l); while (n--) { long long int t, li; cin >> t >> li; v.push_back(t); v.push_back(t + li); } sort((v).begin(), (v).end()); long long int ans = 0; for (long long int i = 1; i < v.size(); i += 2) { long long int tmp = (v[i] - v[i - 1]) / a; ans += tmp; } cout << ans; } return 0; }
1
#include <bits/stdc++.h> using namespace std; using ll = long double; const int MAXN = 200001; ll cumul[MAXN], arr[MAXN]; map<ll, vector<int>> adjList; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { long long t; cin >> t; arr[i] = t; } cumul[0] = arr[0]; for (int i = 1; i < n; i++) cumul[i] = cumul[i - 1] + arr[i]; for (int i = 0; i < n; i++) adjList[arr[i]].push_back(i); ll sum = 0; for (auto& l : adjList) { for (auto i : l.second) { ll smaller = 0; ll larger = 0; ll equal = 0; if (adjList.find(l.first - 1) != adjList.end()) { smaller = adjList[l.first - 1].size() - (lower_bound(adjList[l.first - 1].begin(), adjList[l.first - 1].end(), i) - adjList[l.first - 1].begin()); } if (adjList.find(l.first + 1) != adjList.end()) { larger = adjList[l.first + 1].size() - (lower_bound(adjList[l.first + 1].begin(), adjList[l.first + 1].end(), i) - adjList[l.first + 1].begin()); } ll temp = (smaller + larger) * l.first; temp += (cumul[n - 1] - ((i - 1 >= 0) ? cumul[i - 1] : 0)) - smaller * (l.first - 1) - larger * (l.first + 1) - (n - i) * l.first; sum += temp; } } cout << setprecision(0) << std::fixed << sum << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; template <typename Tp> inline void rd(Tp &x) { x = 0; int fh = 1; char ch = 1; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') fh = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= fh; } template <typename Tp> inline void bird(Tp &x) { x = 0; int fh = 1; char ch = 1; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') fh = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 2 + ch - '0', ch = getchar(); x *= fh; } template <typename Tp> inline void smax(Tp &x, Tp y) { if (x < y) x = y; } const int maxn = 100 + 7; const int maxm = 20000 + 7; int n, m; int u[maxn], v[maxn]; inline void Init(void) { rd(n); rd(m); for (int i = 1; i <= m; i++) { rd(u[i]); rd(v[i]); } } int Head[maxn], Next[maxm], tot, to[maxm]; void add(int x, int y) { to[++tot] = y, Next[tot] = Head[x], Head[x] = tot; } int c[maxn]; void dfs(int x, int f, int col) { c[x] = col; for (int i = Head[x]; i; i = Next[i]) { int y = to[i]; if (y == f) continue; if (!c[y]) { dfs(y, x, 3 - col); } else if (c[y] == col) { puts("Impossible"); exit(0); } } } inline void Work(void) { for (int i = 1; i <= m; i++) { for (int j = 1; j < i; j++) { int x1 = u[i], y1 = v[i], x2 = u[j], y2 = v[j]; if (x1 > y1) swap(x1, y1); if ((x1 < x2 && x2 < y1 && (y2 > y1 && y2 <= n || y2 >= 1 && y2 < x1)) || (x1 < y2 && y2 < y1 && (x2 > y1 && x2 <= n || x2 >= 1 && x2 < x1))) { add(i, j); add(j, i); } } } for (int i = 1; i <= m; i++) { if (!c[i]) dfs(i, 0, 1); } for (int i = 1; i <= m; i++) { if (c[i] == 1) putchar('i'); else putchar('o'); } puts(""); } signed main(void) { Init(); Work(); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { long long w, h, k; cin >> w >> h >> k; long long ans = 0; for (long long i = 1; i <= k; i++) { ans += ((w - 4 * (i - 1) - 1) + (h - 4 * (i - 1) - 1)) * 2; } cout << ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; const unsigned long long INF = 2e18; int n, r; long long k; long long a[N], new_a[N]; void change_a() { int i; long long prefix = 0; for (i = 1; i <= r + 1; i++) { prefix += a[i]; new_a[i] += prefix; } for (i = r + 2; i <= n; i++) { prefix -= a[i - (r + 1)]; prefix += a[i]; new_a[i] += prefix; } long long suffix = 0; for (i = n; i >= n - r; i--) { suffix += a[i]; new_a[i] += suffix; } for (i = n - (r + 1); i >= 1; i--) { suffix -= a[i + (r + 1)]; suffix += a[i]; new_a[i] += suffix; } for (i = 1; i <= n; i++) { a[i] = new_a[i] - a[i]; } } long long delta[N]; bool check(long long x) { memset(delta, 0, sizeof(delta)); int i; long long need = 0; for (i = 1; i <= n; i++) { delta[i] += delta[i - 1]; if (a[i] + delta[i] < x) { need += x - (a[i] + delta[i]); if (i + r + r + 1 <= n) delta[i + r + r + 1] -= x - (a[i] + delta[i]); delta[i] += x - (a[i] + delta[i]); } if (need > k) return false; } return need <= k; } long long ans; int main() { int i; scanf("%d%d", &n, &r); scanf("%I64d", &k); for (i = 1; i <= n; i++) { scanf("%I64d", &a[i]); } change_a(); unsigned long long low, high, mid; check(6); low = 0; high = INF; while (low <= high) { mid = (low + high) / 2; if (check(mid)) { ans = mid; low = mid + 1; } else { high = mid - 1; } } printf("%I64d\n", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; int c[31 + 1][2][2][2], s[31 + 1][2][2][2], T[2][2][2]; void Initialise() { T[0][0][0] = 0; T[0][0][1] = 1; T[0][1][0] = 0; T[0][1][1] = 0; T[1][0][0] = 1; T[1][0][1] = 1; T[1][1][0] = 0; T[1][1][1] = 1; } int Query(int n, int m, int k) { if (n < 0 || m < 0) return 0; for (int x = 0; x <= 31; x++) for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) c[x][i][j][k] = s[x][i][j][k] = 0; c[0][0][0][0] = 1; for (int x = 0; x < 31; x++) { int t1 = ((n & (1 << x)) > 0), t2 = ((m & (1 << x)) > 0), t3 = ((k & (1 << x)) > 0); for (int p1 = 0; p1 < 2; p1++) { for (int p2 = 0; p2 < 2; p2++) { for (int p3 = 0; p3 < 2; p3++) { for (int c1 = 0; c1 < 2; c1++) { for (int c2 = 0; c2 < 2; c2++) { int c3 = (c1 ^ c2); int n1 = T[p1][t1][c1], n2 = T[p2][t2][c2], n3 = T[p3][t3][c3]; c[x + 1][n1][n2][n3] = (c[x + 1][n1][n2][n3] + c[x][p1][p2][p3]) % 1000000007; s[x + 1][n1][n2][n3] = (s[x + 1][n1][n2][n3] + s[x][p1][p2][p3]) % 1000000007; if (c3) s[x + 1][n1][n2][n3] = (s[x + 1][n1][n2][n3] + (1ll * (1 << x) * c[x][p1][p2][p3]) % 1000000007) % 1000000007; } } } } } } return (s[31][0][0][0] + c[31][0][0][0]) % 1000000007; } int FindCars(int x1, int y1, int x2, int y2, int k) { x1--, y1--, x2--, y2--, k--; int ans = (Query(x2, y2, k) + Query(x1 - 1, y1 - 1, k)) % 1000000007 - (Query(x1 - 1, y2, k) + Query(x2, y1 - 1, k)) % 1000000007; ans = (1000000007 + ans) % 1000000007; return ans; } int main() { int q; Initialise(); scanf("%d", &q); while (q--) { int x1, x2, y1, y2, k; scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k); printf("%d\n", FindCars(x1, y1, x2, y2, k)); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int TC; cin >> TC; for (int tc = 0; tc < TC; ++tc) { long long N, M; cin >> N >> M; if (N == 1) cout << 0 << '\n'; else if (N == 2) cout << M << '\n'; else cout << M * 2 << '\n'; } return 0; }
0
#include <bits/stdc++.h> int main() { int i, n, t, busy[100]; scanf("%d %d ", &n, &t); for (i = 0; i < n; i++) { scanf("%d", &busy[i]); busy[i] = 86400 - busy[i]; if (i == 0) { if (busy[0] >= t) { break; } } else { busy[0] += busy[i]; if (busy[0] >= t) { break; } } } printf("%d\n", i + 1); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t, n, j; string s; cin >> t; for (int i = 0; i < t; i++) { cin >> n >> s; for (j = 0; j < n && s[j] != '8'; j++) ; if (j < (n - 10)) cout << "YES" << endl; else cout << "No" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a, b; cin >> n >> m >> a >> b; if (m >= n) cout << "Second" << endl; else cout << "First" << endl; return 0; }
0