func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 1e5 + 10; int n; ll d, m; ll a[N], sum[N]; int main() { cin >> n >> d >> m; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); ll tmp = 0; int ptr = -1; for (int i = 0; i < n; i++) { if (a[i] <= m) tmp += a[i]; else { reverse(a, a + i); ptr = i - 1; break; } } sum[0] = a[0]; for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + a[i]; ll ans = tmp + (a[n - 1] > m ? a[n - 1] : 0), here = 0; for (int i = n - 1; i > ptr; i--) { here += a[i]; int need = (n - i) * (d + 1) - d; int p = min(n - 1 - need, ptr); if (need > n) break; ans = max(ans, here + (p >= 0 ? sum[p] : 0)); } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> vec; double ans = 0; int n; cin >> n; vec.resize(n); for (int i = 0; i < n; cin >> vec[i++]) ; sort(vec.rbegin(), vec.rend()); for (int i = 0; i < n; i += 2) ans += vec[i] * vec[i]; for (int i = 1; i < n; i += 2) ans -= vec[i] * vec[i]; ans *= 3.1415926536; cout << fixed << setprecision(11) << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, i, j, ans, k; int main() { scanf( %I64d%I64d , &n, &k); ans = 1; for (i = 2; i <= n; i++) { if (i == 1) ans = (ans * (k - 1)) % 1000000007; else if (i <= k) ans = (ans * (k)) % 1000000007; else ans = (ans * (n - k)) % 1000000007; } printf( %I64d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const long long mxn = 1e6 + 7; const long long mod = 1e9 + 7; const long long inf = 1e18 + 7; int dx[] = {+1, -1, 0, 0}; int dy[] = {0, 0, -1, +1}; pair<int, int> p[mxn]; map<long long, long long> f, s; int n; int a[mxn], ls[mxn]; long long ans; void upd1(int x, int val) { while (x <= (int)1e8) { f[x] += val; x = (x | (x + 1)); } } void upd2(int x) { while (x <= (int)1e8) { s[x]++; x = (x | (x + 1)); } } long long sum1(int x) { long long res = 0; while (x > 0) { res += f[x]; x = (x & (x + 1)) - 1; } return res; } int sum2(int x) { int res = 0; while (x > 0) { res += s[x]; x = (x & (x + 1)) - 1; } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> p[i].first; } for (int i = 1; i <= n; i++) { cin >> p[i].second; ls[i] = p[i].second; } sort(p + 1, p + n + 1); sort(ls + 1, ls + n + 1); for (int i = 1; i <= n; i++) { p[i].second = lower_bound(ls + 1, ls + n + 1, p[i].second) - ls; } for (int i = 1; i <= n; i++) { ans += (1LL * p[i].first * sum2(p[i].second)) - sum1(p[i].second); upd1(p[i].second, p[i].first); upd2(p[i].second); } cout << ans; return 0; }
#include <bits/stdc++.h> const int mod = 1000000007; const int gmod = 3; const int inf = 1039074182; const double eps = 1e-9; const long long llinf = 2LL * inf * inf; template <typename T1, typename T2> inline void chmin(T1 &x, T2 b) { if (b < x) x = b; } template <typename T1, typename T2> inline void chmax(T1 &x, T2 b) { if (b > x) x = b; } inline void chadd(int &x, int b) { x += b - mod; x += (x >> 31 & mod); } template <typename T1, typename T2> inline void chadd(T1 &x, T2 b) { x += b; if (x >= mod) x -= mod; } template <typename T1, typename T2> inline void chmul(T1 &x, T2 b) { x = 1LL * x * b % mod; } template <typename T1, typename T2> inline void chmod(T1 &x, T2 b) { x %= b, x += b; if (x >= b) x -= b; } template <typename T> inline T mabs(T x) { return (x < 0 ? -x : x); } using namespace std; using namespace std; template <typename T> ostream &operator<<(ostream &cout, vector<T> vec) { cout << { ; for (int i = 0; i < (int)vec.size(); i++) { cout << vec[i]; if (i != (int)vec.size() - 1) cout << , ; } cout << } ; return cout; } template <typename T> void operator*=(vector<T> &vec, int k) { for (auto &x : vec) x *= k; } template <typename T> void operator-=(vector<T> &a, const vector<T> &b) { assert(a.size() == b.size()); for (size_t it = 0; it < a.size(); it++) { a[it] -= b[it]; } } template <typename T> vector<T> operator*(const vector<T> &vec, int k) { vector<T> res(vec); res *= k; return res; } template <typename T1, typename T2> ostream &operator<<(ostream &cout, pair<T1, T2> p) { cout << ( << p.first << , << p.second << ) ; return cout; } template <typename T, typename T2> ostream &operator<<(ostream &cout, set<T, T2> s) { vector<T> t; for (auto x : s) t.push_back(x); cout << t; return cout; } template <typename T, typename T2> ostream &operator<<(ostream &cout, multiset<T, T2> s) { vector<T> t; for (auto x : s) t.push_back(x); cout << t; return cout; } template <typename T> ostream &operator<<(ostream &cout, queue<T> q) { vector<T> t; while (q.size()) { t.push_back(q.front()); q.pop(); } cout << t; return cout; } template <typename T1, typename T2, typename T3> ostream &operator<<(ostream &cout, map<T1, T2, T3> m) { for (auto &x : m) { cout << Key: << x.first << << Value: << x.second << endl; } return cout; } template <typename T> T operator*(vector<T> v1, vector<T> v2) { assert(v1.size() == v2.size()); int n = v1.size(); T res = 0; for (int i = 0; i < n; i++) { res += v1[i] * v2[i]; } return res; } template <typename T1, typename T2> void operator+=(pair<T1, T2> &x, const pair<T1, T2> y) { x.first += y.first; x.second += y.second; } template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &x, const pair<T1, T2> &y) { return make_pair(x.first + y.first, x.second + y.second); } template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &x, const pair<T1, T2> &y) { return make_pair(x.first - y.first, x.second - y.second); } template <typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> x) { return make_pair(-x.first, -x.second); } template <typename T> vector<vector<T>> operator~(vector<vector<T>> vec) { vector<vector<T>> v; int n = vec.size(), m = vec[0].size(); v.resize(m); for (int i = 0; i < m; i++) { v[i].resize(n); } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { v[i][j] = vec[j][i]; } } return v; } void print0x(int x) { std::vector<int> vec; while (x) { vec.push_back(x & 1); x >>= 1; } std::reverse(vec.begin(), vec.end()); for (int i = 0; i < (int)vec.size(); i++) { std::cout << vec[i]; } std::cout << ; } template <typename T> void print0x(T x, int len) { std::vector<int> vec; while (x) { vec.push_back(x & 1); x >>= 1; } reverse(vec.begin(), vec.end()); for (int i = (int)vec.size(); i < len; i++) { putchar( 0 ); } for (size_t i = 0; i < vec.size(); i++) { std::cout << vec[i]; } std::cout << ; } template <int mod> struct ModInt { int x; ModInt() { x = 0; } ModInt(int _x) { x = _x % mod; if (x < 0) x += mod; } ModInt(long long _x) { x = _x % mod; if (x < 0) x += mod; } ModInt<mod> &operator++() { ++x; if (x == mod) x = 0; return *this; } ModInt<mod> operator++(int) { int t = x; x++; if (x == mod) x = 0; return t; } ModInt<mod> &operator--() { --x; if (x == -1) x += mod; return *this; } ModInt<mod> operator--(int) { int t = x; x--; if (x == -1) x += mod; return t; } }; template <int mod> inline int mabs(const ModInt<mod> &a) { return a.x; } template <int mod> bool operator==(const ModInt<mod> &a, const ModInt<mod> &b) { return a.x == b.x; } template <int mod> bool operator==(ModInt<mod> a, int x) { return (a.x == x); } template <int mod> bool operator!=(const ModInt<mod> &a, const ModInt<mod> &b) { return (a.x != b.x); } template <int mod> bool operator!=(ModInt<mod> a, int x) { return (a.x != x); } template <int mod> ModInt<mod> operator+(ModInt<mod> a, int b) { a.x += b; if (a.x >= mod) a.x -= mod; return a; } template <int mod> ModInt<mod> operator+(ModInt<mod> a, ModInt<mod> b) { int tmp = a.x + b.x; if (tmp >= mod) tmp -= mod; return tmp; } template <int mod> ModInt<mod> operator-(ModInt<mod> a, ModInt<mod> b) { int tmp = a.x - b.x; if (tmp < 0) tmp += mod; return tmp; } template <int mod> ModInt<mod> operator-(ModInt<mod> a, int b) { return a - (ModInt<mod>)b; } template <int mod> void operator-=(ModInt<mod> &a, ModInt<mod> b) { a.x -= b.x; a.x += (a.x >> 31 & mod); } template <int mod, typename T> void operator-=(ModInt<mod> &a, const T &b) { a.x -= b; a.x += (a.x >> 31 & mod); } template <int mod> ModInt<mod> operator*(ModInt<mod> a, ModInt<mod> b) { return 1LL * a.x * b.x % mod; } template <int mod> ModInt<mod> operator*(ModInt<mod> a, int b) { return 1LL * a.x * b % mod; } template <int mod> void operator*=(ModInt<mod> &a, ModInt<mod> b) { a = a * b; } template <int mod, typename T> void operator*=(ModInt<mod> &a, const T &b) { a = a * b; } template <int mod> void operator+=(ModInt<mod> &a, ModInt<mod> b) { a.x += b.x; if (a.x >= mod) a.x -= mod; } template <int mod, typename T> void operator+=(ModInt<mod> &a, const T &b) { a.x += b; if (a.x >= mod) a.x -= mod; } template <int mod> inline ModInt<mod> inv(ModInt<mod> x) { int m = mod - 2; ModInt<mod> basic = x; x = 1; while (m) { if (m & 1) x *= basic; m >>= 1; basic *= basic; } return x; } template <int mod> ModInt<mod> operator/(ModInt<mod> a, ModInt<mod> b) { return a * inv(b); } template <int mod> ModInt<mod> operator/(ModInt<mod> a, int b) { return a * inv((ModInt<mod>)b); } template <int mod> void operator/=(ModInt<mod> &a, int b) { a *= inv(ModInt<mod>(b)); } template <int mod> void operator/=(ModInt<mod> &a, ModInt<mod> b) { a = a / b; } template <int mod> ModInt<mod> operator^(ModInt<mod> basic, int x) { ModInt<mod> res = 1; while (x) { if (x & 1) res *= basic; basic *= basic; x >>= 1; } return res; } template <int mod> istream &operator>>(istream &cin, ModInt<mod> &x) { cin >> x.x; x.x %= mod; if (x.x < 0) x.x += mod; return cin; } template <int mod> ostream &operator<<(ostream &cout, ModInt<mod> x) { cout << x.x; return cout; } namespace combinatorics { int *fac; int *ifac; int __Tmod; inline int add(int a, int b) { return (a + b) % __Tmod; } inline int sub(int a, int b) { return (a - b + __Tmod) % __Tmod; } inline int mult(int a, int b) { return (1LL * a * b) % __Tmod; } inline int fastpow(int basic, int x) { chmod(x, __Tmod - 1); if (x == 0) return 1; int res = 1; while (x) { if (x & 1) res = mult(res, basic); basic = mult(basic, basic); x >>= 1; } return res; } inline int inv(int x) { return fastpow(x, __Tmod - 2); } inline int div(int a, int b) { return mult(a, inv(b)); } void init(int n, int tmod) { __Tmod = tmod; fac = new int[n + 5]; ifac = new int[n + 5]; fac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = mult(fac[i - 1], i); } ifac[n] = inv(fac[n]); for (int i = n - 1; i >= 0; i--) { ifac[i] = mult(ifac[i + 1], i + 1); } } inline int C(int n, int m) { if (n < m || n < 0 || m < 0) return 0; return mult(mult(fac[n], ifac[m]), ifac[n - m]); } inline int Cat(int x) { return mult(C(x * 2, x), inv(x + 1)); } } // namespace combinatorics using namespace std; using Int = ModInt<mod>; int n, k; char c[100005]; Int sum[100005]; Int a[100005]; int main() { scanf( %d%d , &n, &k); scanf( %s , c); for (int i = 0; i < n; i++) c[i] -= 0 ; combinatorics::init(n, mod); for (int i = 0; i < n; i++) { a[i] = c[i]; sum[i] = a[i] + (i ? sum[i - 1] : 0); } Int res = 0; for (int j = 0; j <= n - 2; j++) { Int b = Int(combinatorics::C(n - j - 2, k - 1)) * (Int(10) ^ j); res += b * sum[n - j - 2]; } for (int i = 0; i < n; i++) { res += a[i] * combinatorics::C(i, k) * (Int(10) ^ (n - 1 - i)); } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void warp() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); } long long get(vector<long long> &a, vector<long long> &b) { long long ans = 0; long long all = 0; long long n = a.size(); for (long long i = 0; i < n; i++) all += b[i]; long long cur = b[0], tar = 0; while (cur < all - cur) { tar++; cur += b[tar]; } for (long long i = 0; i < n; i++) { ans += b[i] * abs(a[i] - a[tar]); } return ans; } long long check(long long need, vector<long long> a) { long long ans = 0; long long n = a.size(); vector<long long> cur; vector<long long> b; long long s = 0; for (long long i = 0; i < n; i++) { a[i] %= need; if (a[i]) { cur.push_back(i); b.push_back(a[i]); } s += a[i]; if (s >= need && b.size()) { b.back() -= (s - need); ans += get(cur, b); cur.clear(); b.clear(); s -= need; if (s == 0) continue; cur.push_back(i); b.push_back(s); } } return ans; } long long solve(long long n, vector<long long> a) { long long cnt = 0; for (long long i = 0; i < n; i++) cnt += a[i]; if (cnt == 1) { return -1; } long long ans = check(cnt, a); vector<bool> prime(1e6 + 1, true); for (long long i = 2; i * i <= cnt; i++) { if (cnt % i) continue; while (cnt % i == 0) cnt /= i; ans = min(ans, check(i, a)); } if (cnt > 1) ans = min(ans, check(cnt, a)); return ans; } const long long maxn = 1e6; void gen(vector<long long> &a) { long long n = a.size(); for (long long i = 0; i < n; i++) a[i] = rand() % 20; long long s = 0; for (long long i = 0; i < n; i++) s += a[i]; if (s == 0) a[0] = 1; } signed main() { warp(); long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } cout << solve(n, a) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct Person { int num; string n1, n2; string final; Person(int num = -1) { this->num = num; } void read(int num = -1) { this->num = num; cin >> n1 >> n2; } }; const int N = 200000; Person b[N], a[N]; int p[N]; int n; bool smaller(Person f, Person s) { return p[f.num] < p[s.num]; } void fail() { printf( NO n ); exit(0); } const bool D = 0; int main() { if (D) { freopen( test.in , r , stdin); freopen( test.out , w , stdout); } else { } scanf( %d , &n); for (int i = 0; i < n; ++i) { b[i].read(i); } for (int i = 0; i < n; ++i) { scanf( %d , &p[i]); --p[i]; } for (int i = 0; i < n; ++i) { a[i] = b[p[i]]; } a[0].final = min(a[0].n1, a[0].n2); for (int i = 1; i < n; ++i) { a[i].final = min(a[i].n1, a[i].n2); if (a[i].final <= a[i - 1].final) { a[i].final = max(a[i].n1, a[i].n2); if (a[i].final <= a[i - 1].final) fail(); } } printf( YES n ); return 0; }
#include <bits/stdc++.h> int main() { int n, i, j, temp; scanf( %d , &n); char s[n]; scanf( %s , &s); for (i = 0; i < n; i++) { if (s[i] > s[i + 1] && i + 1 < n) { printf( YES n ); printf( %d %d , i + 1, i + 2); return 0; } } printf( NO ); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> void pr(vector<T> &v) { for (int i = 0; i < (int)(v).size(); i++) cout << v[i] << ; cout << n ; ; } template <typename T> void pr(vector<vector<T>> &v) { for (int i = 0; i < (int)(v).size(); i++) { pr(v[i]); } } template <typename T> void re(T &x) { cin >> x; } template <typename T> void re(vector<T> &a) { for (int i = 0; i < (int)(a).size(); i++) re(a[i]); } template <class Arg, class... Args> void re(Arg &first, Args &...rest) { re(first); re(rest...); } template <typename T> void pr(T x) { cout << x << n ; ; } template <class Arg, class... Args> void pr(const Arg &first, const Args &...rest) { cout << first << ; pr(rest...); cout << n ; ; } void ps() { cout << n ; ; } template <class T, class... Ts> void ps(const T &t, const Ts &...ts) { cout << t; if (sizeof...(ts)) cout << ; ps(ts...); } const long long MOD = 1000000007; long double PI = 4 * atan(1); long double eps = 1e-12; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k; cin >> n >> k; long long powk = k; long long exp = 1; long long nexp = n - 1; if (k == 1) { nexp = n; } if (k != 0 && k != 1) { while (powk % n != 1) { powk *= k; powk %= n; exp++; } nexp = (n - 1) / exp; } long long powp = 1; for (int i = 0; i < nexp; i++) { powp *= n; powp %= MOD; } cout << powp << n ; ; }
#include <bits/stdc++.h> using namespace std; int s[10000], g[10000]; set<int> v; int main() { int k, n; cin >> k >> n; for (int i = 0; i < n; i++) { cin >> s[i] >> g[i]; } for (int i = 1; i <= 100; i++) { bool f = 1; for (int j = 0; j < n; j++) { if ((s[j] + i - 1) / i != g[j]) { f = 0; break; } } if (f) { v.insert((k + i - 1) / i); } } if (v.size() != 1) { cout << -1; } else { cout << *v.begin(); } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n1 = 0; int n2 = 0; int n4 = 0; int n8 = 0; int n16 = 0; int n32 = 0; int n64 = 0; int n128 = 0; int n256 = 0; int n512 = 0; int n1024 = 0; int n2048 = 0; bool flag = false; int n; cin >> n; for (int i = 0; i < n; i++) { int temp; cin >> temp; if (temp == 2048) { n2048++; flag = true; } if (flag == false) { if (temp == 1) n1++; else if (temp == 2) n2++; else if (temp == 4) n4++; else if (temp == 8) n8++; else if (temp == 16) n16++; else if (temp == 32) n32++; else if (temp == 64) n64++; else if (temp == 128) n128++; else if (temp == 256) n256++; else if (temp == 512) n512++; else if (temp == 1024) n1024++; } } if (n2048 >= 1) { cout << YES << endl; continue; } int temp; temp = n1 / 2; n2 += temp; temp = n2 / 2; n4 += temp; temp = n4 / 2; n8 += temp; temp = n8 / 2; n16 += temp; temp = n16 / 2; n32 += temp; temp = n32 / 2; n64 += temp; temp = n64 / 2; n128 += temp; temp = n128 / 2; n256 += temp; temp = n256 / 2; n512 += temp; temp = n512 / 2; n1024 += temp; temp = n1024 / 2; n2048 += temp; if (n2048 >= 1) { cout << YES << endl; } else { cout << NO << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a; int A[6]; int main() { scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d , &A[i]); } sort(A, A + n); printf( %d , A[n / 2]); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9 + 7; class Compare { public: bool operator()(const string &s, const string &t) const { if (s.size() != t.size()) return (s.size() < t.size()); for (int i = 0, _n = (s.size()); i < _n; i = i + 1) { if (s[i] < t[i]) return (true); if (s[i] > t[i]) return (false); } return (false); } }; map<string, int, Compare> wid; pair<int, int> val[300300], vcp[300300], f[300300]; vector<int> g[300300], h[300300]; int low[300300], num[300300], cpid[300300]; vector<int> tid; bool fre[300300]; int nw, nc, cnt; stack<int> st; template <class T> inline void minimize(T &x, const T &y) { if (x > y) x = y; } inline int nextint(void) { int x; cin >> x; return (x); } inline string nextstring(void) { string s; cin >> s; for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) if ( a <= *it && *it <= z ) *it -= a - A ; return (s); } inline pair<int, int> value(const string &s) { int res = 0; for (__typeof((s).begin()) it = (s).begin(); it != (s).end(); it++) if (*it == R ) res++; return (pair<int, int>(res, s.size())); } inline int strid(const string &s) { __typeof(wid.begin()) it = wid.find(s); if (it == wid.end()) { wid[s] = ++nw; val[nw] = value(s); return (nw); } return (it->second); } void init(void) { for (int zz = 0, _n = (nextint()); zz < _n; zz = zz + 1) tid.push_back(strid(nextstring())); for (int zz = 0, _n = (nextint()); zz < _n; zz = zz + 1) { int u = strid(nextstring()); int v = strid(nextstring()); g[u].push_back(v); } for (int i = (1), _b = (nw); i <= _b; i = i + 1) fre[i] = true; } void visit(int u) { low[u] = num[u] = ++cnt; st.push(u); for (__typeof((g[u]).begin()) it = (g[u]).begin(); it != (g[u]).end(); it++) if (fre[*it]) { int v = *it; if (num[v] == 0) { visit(v); minimize(low[u], low[v]); } else minimize(low[u], num[v]); } if (low[u] == num[u]) { int v; nc++; do { v = st.top(); st.pop(); cpid[v] = nc; fre[v] = false; } while (v != u); } } void tarjan(void) { for (int i = (1), _b = (nw); i <= _b; i = i + 1) if (num[i] == 0) visit(i); for (int i = (1), _b = (nc); i <= _b; i = i + 1) vcp[i] = pair<int, int>(INF, INF); for (int i = (1), _b = (nw); i <= _b; i = i + 1) minimize(vcp[cpid[i]], val[i]); for (int i = (1), _b = (nc); i <= _b; i = i + 1) f[i] = pair<int, int>(-1, -1); for (int i = (1), _b = (nw); i <= _b; i = i + 1) for (__typeof((g[i]).begin()) it = (g[i]).begin(); it != (g[i]).end(); it++) { int j = *it; if (cpid[i] != cpid[j]) h[cpid[i]].push_back(cpid[j]); } } pair<int, int> dp(int u) { if (f[u].first >= 0) return (f[u]); f[u] = vcp[u]; for (__typeof((h[u]).begin()) it = (h[u]).begin(); it != (h[u]).end(); it++) minimize(f[u], dp(*it)); return (f[u]); } void process(void) { long long cnt = 0; long long len = 0; for (__typeof((tid).begin()) it = (tid).begin(); it != (tid).end(); it++) { pair<int, int> tmp = dp(cpid[*it]); cnt += tmp.first; len += tmp.second; } cout << cnt << << len; } int main(void) { ios::sync_with_stdio(false); init(); tarjan(); process(); return 0; }
#include <bits/stdc++.h> using namespace std; int c[200010]; int v[200010]; set<int> not_full; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, q, p, x, k; cin >> n; for (int i = 1; i <= n; ++i) { cin >> c[i]; not_full.insert(i); } cin >> m; while (m--) { cin >> q; if (q == 1) { cin >> p >> x; while (x > 0) { if (not_full.find(p) == not_full.end()) { set<int>::iterator it = not_full.upper_bound(p); if (it == not_full.end()) { break; } else { p = *it; } } else if (v[p] + x < c[p]) { v[p] += x; x = 0; } else { x = x - (c[p] - v[p]); v[p] = c[p]; not_full.erase(p); } } } else { cin >> k; cout << v[k] << n ; } } return 0; }
#include <bits/stdc++.h> #define taskname #define pb push_back #define eb emplace_back #define fi first #define se second #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define for0(i, n) for (int i = 0; i < (int)(n); ++i) #define for1(i, n) for (int i = 1; i <= (int)(n); ++i) #define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i) #define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i) using namespace std; typedef long long ll; typedef long double ld; typedef vector <int> vi; template<class T> using v2d = vector <vector <T> >; template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); const int maxN = 2e5 + 10; const ll mod = 998244353; int n; ll a[maxN], pre[maxN]; void solve() { cin >> n; for1(i, n) { cin >> a[i]; } //case works if (n == 1) { cout << 1 << n ; return; } ll ans = 0, sum = 0; for1(i, n) { sum += a[i]; } pre[1] = a[1]; pre[2] = a[2]; fore(i, 3, n) { pre[i] = pre[i - 2] + a[i]; } ll cur = 0; for1(i, n - 1) { int l = 0, r = (n - i + 1) / 2; cur += a[i]; if (cur * 2 >= sum) { break; } while (r - l > 1) { int mid = (l + r) / 2; if ((pre[i + mid * 2] - pre[i] + cur) * 2 < sum) { l = mid; } else { r = mid; } } ans = (ans + l + 1) % mod; } cur = a[n]; for1(i, n - 2) { int l = 0, r = (n - i) / 2; cur += a[i]; if (cur * 2 >= sum) { break; } while (r - l > 1) { int mid = (l + r) / 2; if ((pre[i + mid * 2] - pre[i] + cur) * 2 < sum) { l = mid; } else { r = mid; } } ans = (ans + l + 1) % mod; } cur = 0; fore(i, 2, n - 1) { int l = 0, r = (n - i + 1) / 2; cur += a[i]; if (cur * 2 >= sum) { break; } while (r - l > 1) { int mid = (l + r) / 2; if ((pre[i + mid * 2] - pre[i] + cur) * 2 < sum) { l = mid; } else { r = mid; } } ans = (ans + l + 1) % mod; } cur = a[n]; fore(i, 2, n - 2) { int l = 0, r = (n - i) / 2; cur += a[i]; if (cur * 2 >= sum) { break; } while (r - l > 1) { int mid = (l + r) / 2; if ((pre[i + mid * 2] - pre[i] + cur) * 2 < sum) { l = mid; } else { r = mid; } } ans = (ans + l + 1) % mod; } cur = 0; for1(i, n) { cur += a[i]; if (cur * 2 > sum) { ans = (ans + 1) % mod; } } cout << ans << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int T = 1; cin >> T; while (T--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> p(n, 0); for (int i = 0; i < n; i++) { int aux; cin >> aux; p[i] = aux - 1; } vector<long long int> dp(n + 1, 0); for (int i = 1; i < n + 1; i++) { dp[i] = (long long int)(dp[i - 1] * (long long int)2 - dp[p[i - 1]] + 2 + 1000000007) % 1000000007; } cout << dp[n] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; pair<int, int> V[300300][30]; int Lar[300300]; vector<int> Map[300300]; struct tree { tree *L[27]; tree() { for (int i = 0; i < 27; i++) L[i] = NULL; } } root, *point, *point2; int mm[300300], C[300300], depht[300300]; int MAX = 1; tree *Pos[300300]; void dfs(int x) { tree *ant = point; C[x] = 1; mm[x] = -1; Pos[x] = point; pair<int, int> *p; for (int i = 0; i < Lar[x]; i++) { p = &V[x][i]; if (point->L[p->second] == NULL) { point->L[p->second] = new tree; point = point->L[p->second]; } else { point = point->L[p->second]; } depht[p->first] = depht[x] + 1; if (depht[p->first] > MAX) MAX = depht[p->first]; dfs(p->first); C[x] += C[p->first]; if (mm[x] == -1) { mm[x] = p->first; } else if (C[p->first] > C[mm[x]]) { mm[x] = p->first; } point = ant; } Map[depht[x]].push_back(x); } bool flag; int Nodo; void dfs2(int y) { bool aflag = flag; tree *ap2 = point2; tree *ap1 = point; pair<int, int> *p; for (int i = 0; i < Lar[y]; i++) { p = &V[y][i]; if (flag) { if (point2->L[p->second] != NULL) { point2 = point2->L[p->second]; } else { flag = false; } } if (point->L[p->second] != NULL) { point = point->L[p->second]; } else { point->L[p->second] = new tree; point = point->L[p->second]; if (!flag) Nodo++; } dfs2(p->first); point2 = ap2; point = ap1; flag = aflag; } flag = aflag; } int Sol[300300]; int main() { int N, x, y; char l[5]; scanf( %d , &N); for (int i = 1; i < N; i++) { scanf( %d%d%s , &x, &y, l); V[x][Lar[x]] = pair<int, int>(y, l[0] - a ); Lar[x]++; } depht[1] = 1; point = &root; Pos[1] = point; dfs(1); int X, Y; for (int i = 1; i < MAX; i++) { Sol[i] = 0; for (vector<int>::iterator p = Map[i].begin(); p != Map[i].end(); p++) { X = *p; if (Lar[X] == 1) { Sol[i]++; continue; } else if (Lar[X] == 0) { continue; } tree *ante; point = ante = new tree; Nodo = C[mm[X]]; pair<int, int> *q; for (int i = 0; i < Lar[X]; i++) { q = &V[X][i]; Y = q->first; if (Y != mm[X]) { point2 = Pos[mm[X]]; flag = true; dfs2(Y); point = ante; } } Sol[i] += (C[X]) - Nodo; } } int XSol = 0; for (int i = 1; i < MAX; i++) { if (Sol[XSol] < Sol[i]) { XSol = i; } } printf( %d n%d n , N - Sol[XSol], XSol); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 1; const long long LOG = 20; vector<pair<long long, long long> > edg[MAXN]; long long sz[MAXN]; long long link[MAXN]; long long ans[MAXN]; bool used[MAXN]; vector<pair<long long, long long> > ost[MAXN]; long long dp[MAXN][LOG]; long long mx[MAXN][LOG]; long long timein[MAXN]; long long timeout[MAXN]; long long timer = 0; long long first[MAXN]; vector<long long> h; long long pos = 0; bool cmp(tuple<long long, long long, long long, long long> a, tuple<long long, long long, long long, long long> b) { return get<2>(a) < get<2>(b); } long long fin(long long u) { if (u == link[u]) return u; return link[u] = fin(link[u]); } bool same(long long u, long long v) { return fin(u) == fin(v); } void unite(long long u, long long v) { u = fin(u); v = fin(v); if (sz[u] > sz[v]) swap(u, v); sz[v] += sz[u]; link[u] = link[v]; } void dfs(long long v, long long pred, long long w, long long ha) { timein[v] = timer++; mx[v][0] = w; dp[v][0] = pred; first[v] = pos; h.push_back(ha); pos++; for (long long i = 1; i < LOG; i++) { dp[v][i] = dp[dp[v][i - 1]][i - 1]; mx[v][i] = max(mx[v][i - 1], mx[dp[v][i - 1]][i - 1]); } for (auto u : ost[v]) if (u.first != pred) dfs(u.first, v, u.second, ha + 1); timeout[v] = timer++; } bool upper(long long u, long long v) { return timein[u] <= timein[v] && timeout[u] >= timeout[v]; } long long lca(long long u, long long v) { if (upper(u, v)) return u; if (upper(v, u)) return v; for (long long i = LOG - 1; i >= 0; i--) if (!upper(dp[u][i], v)) u = dp[u][i]; return dp[u][0]; } long long vis(long long u) { return h[first[u]]; } long long getmax(long long u, long long v) { long long ans = -1; if (vis(u) < vis(v)) swap(u, v); for (long long i = LOG - 1; i >= 0; i--) if (vis(dp[u][i]) >= vis(v)) { ans = max(ans, mx[u][i]); u = dp[u][i]; } return ans; } signed main() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long ver, a, b, w, n, m, i, j, sum = 0, num, c, ves = -1; cin >> n >> m; vector<tuple<long long, long long, long long, long long> > reb(m); for (i = 0; i < m; i++) { cin >> a >> b >> w; edg[a].push_back({b, w}); edg[b].push_back({a, w}); reb[i] = make_tuple(a, b, w, i); } sort(reb.begin(), reb.end(), cmp); for (i = 1; i <= n; i++) { link[i] = i; sz[i] = 1; } vector<long long> made; for (i = 0; i < m; i++) { tie(a, b, w, num) = reb[i]; if (!same(a, b)) { unite(a, b); sum += w; made.push_back(num); used[num] = true; ost[a].push_back({b, w}); ost[b].push_back({a, w}); } } for (auto c : made) { ans[c] = sum; used[c] = true; } for (i = 0; i < LOG; i++) dp[1][i] = 1; dfs(1, 1, 0, 0); for (i = 0; i < m; i++) { tie(a, b, w, num) = reb[i]; if (used[num]) continue; c = lca(a, b); ves = max(getmax(a, c), getmax(b, c)); ans[num] = sum - ves + w; } for (i = 0; i < m; i++) cout << ans[i] << n ; }
#include <bits/stdc++.h> using namespace std; map<string, int> mp; int sum[1111], cnt, po[1111], Sum[1111]; char s[1111][1111], ss[1111][1111]; int a[1111]; int find(char *s) { if (mp[s]) return mp[s]; mp[s] = ++cnt; strcpy(ss[cnt], s); return cnt; } int main() { int n; while (scanf( %d , &n) == 1) { int mx = -1000000000; for (int i = 1; i <= n; i++) { scanf( %s%d , s[i], &a[i]); int id = find(s[i]); sum[id] += a[i]; } for (int i = 1; i <= cnt; i++) mx = max(mx, sum[i]); for (int i = 1; i <= n; i++) { int id = find(s[i]); Sum[id] += a[i]; if (Sum[id] >= mx && sum[id] >= mx) { puts(s[i]); break; } } } }
#include <bits/stdc++.h> using namespace std; const int limit = 320; int v[100000]; int dp[limit][100000]; int n; static int bruteforce(int p, int k) { int cnt = 0; while (p < n) { cnt++; p = p + v[p] + k; } return cnt; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> v[i]; for (int j = 1; j < limit; j++) { for (int i = n - 1; i >= 0; i--) { if (i + j + v[i] >= n) dp[j][i] = 1; else dp[j][i] = 1 + dp[j][i + j + v[i]]; } } int q; cin >> q; for (int i = 0, p, k; i < q; ++i) { cin >> p >> k; --p; if (k < limit) cout << dp[k][p] << n ; else cout << bruteforce(p, k) << n ; } }
#include <bits/stdc++.h> const int inf = 0x3f3f3f; const long long INF = 1ll << 61; using namespace std; int n, x; typedef struct Node { int t, h, m; }; Node aa[2000 + 55], bb[2000 + 55]; void init() { memset(aa, 0, sizeof(aa)); memset(bb, 0, sizeof(bb)); } int cnt1, cnt2; bool input() { while (cin >> n >> x) { cnt1 = cnt2 = 0; for (int i = 0; i < n; i++) { int t; scanf( %d , &aa[i].t); scanf( %d %d , &aa[i].h, &aa[i].m); bb[i].t = aa[i].t, bb[i].h = aa[i].h, bb[i].m = aa[i].m; } return false; } return true; } bool cmp1(Node x, Node y) { return x.m > y.m; } void cal() { sort(aa, aa + n, cmp1); sort(bb, bb + n, cmp1); int ans1 = 0; int now = 0; int tx = x; for (int i = 0; i < n; i++) { if (aa[i].t == now && tx >= aa[i].h) { now = 1 - now; tx += aa[i].m; ans1++; aa[i].t = -1; i = -1; } } int ans2 = 0; now = 1; for (int i = 0; i < n; i++) { if (bb[i].t == now && x >= bb[i].h) { now = 1 - now; x += bb[i].m; ans2++; bb[i].t = -1; i = -1; } } int ans = max(ans1, ans2); cout << ans << endl; } void output() {} int main() { while (true) { init(); if (input()) return 0; cal(); output(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { map<long long int, long long int> m; long long int a, x, n, o, i, j; long long int sum = 0; cin >> n; for (i = 0; i < n; i++) { cin >> a >> x; m[a] = max(m[a], x); } cin >> o; for (i = 0; i < o; i++) { cin >> a >> x; m[a] = max(m[a], x); } map<long long int, long long int>::iterator it = m.begin(); for (it; it != m.end(); it++) { sum += it->second; } cout << sum; return 0; }
#include <bits/stdc++.h> using namespace std; const int PI = 3.141592653589793; const int inf = 1000111222; const int mod = 1000000007; const int N = 200007; bool comp(const pair<int, int> &a, const pair<int, int> &b) { int len = a.second - a.first + 1; int len2 = b.second - b.first + 1; if (len == len2) return a.first < b.first; return (len > len2); } void solve() { int n; cin >> n; int ar[n + 7]; set<pair<int, int>, bool (*)(const pair<int, int> &, const pair<int, int> &)> S(&comp); S.insert({1, n}); int cnt = 0; while (!S.empty()) { auto it = S.begin(); int l = it->first, r = it->second; S.erase(it); int len = r - l + 1; int mid = 0; if (len & 1) { mid = (l + r) / 2; } else { mid = (l + r - 1) / 2; } ++cnt; ar[mid] = cnt; if (len == 1) continue; if (mid > l) S.insert({l, mid - 1}); if (mid < r) S.insert({mid + 1, r}); } for (int i = 1; i <= n; i++) { cout << ar[i] << ; } cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int cnt = 1, T[N * 31][2], n; long long t, a[N * 31]; void insert(long long x) { int u = 0; for (int i = 30; i >= 0; i--) { int v = (x >> i) & 1; if (!T[u][v]) { memset(T[cnt], 0, sizeof(T[cnt])); a[cnt] = 0; T[u][v] = cnt++; } u = T[u][v]; } a[u] = x; } long long dfs(int p, int k) { if (!T[p][0] && !T[p][1]) return 0; if (!T[p][0]) return dfs(T[p][1], k - 1); if (!T[p][1]) return dfs(T[p][0], k - 1); return (1LL << k) + min(dfs(T[p][0], k - 1), dfs(T[p][1], k - 1)); } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %lld , &t); insert(t); } printf( %lld n , dfs(0, 30)); return 0; }
#include <bits/stdc++.h> long long gcdfun(long long x, long long y) { if (y == 0) return x; else return gcdfun(y, x % y); } using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n, k = 1, flag = 0, ans = 0; cin >> n; vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); int p = *max_element(a.begin(), a.end()); while (k <= 1024) { flag = 0; for (int i = 0; i < n; i++) { b[i] = (a[i] ^ k); } sort(b.begin(), b.end()); for (int i = 0; i < n; i++) { if (a[i] != b[i]) { flag = 1; break; } } if (flag == 1) k++; else { cout << k << n ; ans = 1; break; } } if (ans == 0) cout << -1 << n ; } }
#include <bits/stdc++.h> using namespace std; double tick() { static clock_t oldt, newt = clock(); double diff = 1.0 * (newt - oldt) / CLOCKS_PER_SEC; oldt = newt; return diff; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long a[4][4] = {0}; long long k; cin >> k; char c; std::map<long long, long long> mp; for (long long i = 0LL; i < (4); i++) for (long long j = 0LL; j < (4); j++) { cin >> c; if (c != . ) mp[c - 0 ]++; } bool flag = true; for (auto a : mp) if (a.second > (2 * k)) { flag = false; break; } if (flag) cout << YES ; else cout << NO ; return 0; }
#include <bits/stdc++.h> using namespace std; int main(int argc, char *argv[]) { string d; cin >> d; bool f = true; for (int i = 0; i < d.size(); i++) { if (d[i] == 0 ) { d.erase(i, 1); f = false; break; } } if (f) { d.erase(0, 1); } cout << d << endl; return EXIT_SUCCESS; }
#include <bits/stdc++.h> using namespace std; struct node { int x, y; } p[111]; int g[1111][1111]; int vis[1111]; int n; void dfs(int u) { vis[u] = 1; int i; for (i = 0; i < n; i++) { if (g[u][i] && !vis[i]) { dfs(i); } } } int main() { cin >> n; int i, j; memset(g, 0, sizeof(g)); for (i = 0; i < n; i++) { cin >> p[i].x >> p[i].y; } int ans = 0; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (p[i].x == p[j].x || p[i].y == p[j].y) { g[i][j] = g[j][i] = 1; } } } for (i = 0; i < n; i++) { if (!vis[i]) { dfs(i); ans++; } } cout << ans - 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, R, RP[70]; int T, nm = 1, o; int GCD(int a, int b) { return !a ? b : GCD(b % a, a); } void BT(int a) { if (a == 62) { if (o == 0) return; if (o % 2 == 0) RP[nm]--; else RP[nm]++; return; } BT(a + 1); int ls = nm; nm = (nm * a) / GCD(nm, a); o++; if (nm <= 62) BT(a + 1); nm = ls; o--; } long long powe(long long a, int b) { long long res = 1; for (; b; b /= 2) { if (b % 2) res *= a; a *= a; } return res; } long long gen(long long k) { long long t = powl(n, 1. / k) - 0.5; return t + (powl(t + 1, k) - 0.5 <= n); } int main() { cin >> T; BT(2); for (int i = 0; i < T; i++) { cin >> n; R = 0; for (int j = 2; j <= 62; j++) R += RP[j] * (gen(j) - 1LL); cout << n - R - 1 << endl; } }
#include <bits/stdc++.h> #pragma GCC optimize( 03 ) using namespace std; int n, m, q, x[200100], y[200100], dad[400100]; bool viz[400100]; int find(int p) { if (dad[p] == p) return p; return dad[p] = find(dad[p]); } void join(int a, int b) { dad[find(a)] = find(b); } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n >> m >> q; for (int i = 1; i <= n + m; i++) dad[i] = i; for (int i = 1; i <= q; i++) cin >> x[i] >> y[i]; for (int i = 1; i <= q; i++) join(x[i], y[i] + n); int rs = 0; for (int i = 1; i <= n + m; i++) { int p = find(i); if (!viz[p]) rs++; viz[p]++; } cout << rs - 1; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int a[110], n, sum = 0, i; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 0; i < n - 1; i++) { if (a[i] == a[i + 1]) { sum++; i++; } } printf( %d n , sum / 2); return 0; }
#include <bits/stdc++.h> const int N = 1e3 + 5; int n, m, q, sum[N][N]; int opt(int x, int y) { return (__builtin_popcount(x) + __builtin_popcount(y)) & 1; } int calc(int x, int y, int opt) { return opt == 0 ? sum[x][y] : x * y - sum[x][y]; } long long I(int x, int y, int r, int c) { long long ans = 1LL * r * c / 2 * n * m; if ((r & 1) && (c & 1)) ans += calc(n, m, opt(r - 1, c - 1)); return ans; } long long D(int x, int y, int r, int c) { int xx = r * n; long long ans = 1LL * c / 2 * (x - xx) * m; if (c & 1) ans += calc(x - xx, m, opt(r, c - 1)); return ans; } long long R(int x, int y, int r, int c) { int yy = c * m; long long ans = 1LL * r / 2 * n * (y - yy); if (r & 1) ans += calc(n, y - yy, opt(r - 1, c)); return ans; } long long O(int x, int y, int r, int c) { int xx = r * n, yy = c * m; return calc(x - xx, y - yy, opt(r, c)); } long long query(int x, int y) { if (x == 0 || y == 0) return 0; int r = (x - 1) / n, c = (y - 1) / m; if (r == 0 && c == 0) return O(x, y, r, c); if (r == 0) return D(x, y, r, c) + O(x, y, r, c); if (c == 0) return R(x, y, r, c) + O(x, y, r, c); return I(x, y, r, c) + D(x, y, r, c) + R(x, y, r, c) + O(x, y, r, c); } long long query(int x1, int y1, int x2, int y2) { return query(x2, y2) - query(x1 - 1, y2) - query(x2, y1 - 1) + query(x1 - 1, y1 - 1); } int main() { scanf( %d%d%d , &n, &m, &q); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int x; scanf( %1d , &x); sum[i][j] = x + sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; } } for (int i = 1; i <= q; i++) { int x1, y1, x2, y2; scanf( %d%d%d%d , &x1, &y1, &x2, &y2); printf( %lld n , query(x1, y1, x2, y2)); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int c = 0, r, d, a, s; a = n; while (n) { if (n >= 10) { r = n / 10; n -= r * 10; n += r; c += r * 10; } else { c += n; n /= 10; } } cout << c << endl; } }
#include <bits/stdc++.h> using namespace std; string tolower(string str) { for (int i = 0; i < str.size(); i++) str[i] = tolower(str[i]); return str; } int main() { int n; cin >> n; vector<string> store; for (int i = 0; i < n; i++) { string str; cin >> str; store.push_back(tolower(str)); } string str, temp, ans = ; cin >> temp; vector<int> color(temp.size()); str = tolower(temp); char letter; cin >> letter; for (int i = 0; i < str.size(); i++) { for (int j = 0; j < store.size(); j++) { string& here = store[j]; if (i + here.size() <= str.size()) { string substr = str.substr(i, here.size()); if (substr == here) { for (int k = i; k < i + here.size(); k++) color[k] = 1; } } } } char second = (letter == a ) ? b : a ; for (int i = 0; i < str.size(); i++) { if (color[i]) ans += (str[i] == letter) ? second : letter; else ans += str[i]; if (isupper(temp[i])) ans[i] = toupper(ans[i]); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> int inp() { char c = getchar(); while (c < 0 || c > 9 ) c = getchar(); int sum = 0; while (c >= 0 && c <= 9 ) { sum = sum * 10 + c - 0 ; c = getchar(); } return sum; } int x[200010], s1, s2, n; bool check(int lim) { int l = -2147483647; int r = 2147483647; for (int i = n; i >= 1; i--) { if (x[i] >= l && x[i] <= r) { l = x[i] - lim; r = x[i] + lim; } else { l = std::max(l, x[i] - lim); r = std::min(r, x[i] + lim); } } return (l <= s1 && s1 <= r) || (l <= s2 && s2 <= r); } int main() { n = inp(); s1 = inp(); s2 = inp(); int l = abs(s1 - s2); int r = 1e9; for (int i = 1; i <= n; i++) { x[i] = inp(); if (x[i] == x[i - 1]) { i--; n--; } } while (l < r) { int mid = (l + r) >> 1; if (check(mid)) r = mid; else l = mid + 1; } printf( %d n , l); }
#include <bits/stdc++.h> using namespace std; int dp[55][35555], n, m, a[35555], las[35555], pre[35555], now; struct LT { int mx, lazy; } tr[517171]; void U(int x) { tr[x].mx = max(tr[x << 1].mx, tr[x << 1 | 1].mx); } void D(int x) { tr[x << 1].lazy += tr[x].lazy; tr[x << 1].mx += tr[x].lazy; tr[x << 1 | 1].lazy += tr[x].lazy; tr[x << 1 | 1].mx += tr[x].lazy; tr[x].lazy = 0; U(x); } void rebuild(int x, int l, int r) { tr[x].lazy = 0; if (l == r) { tr[x].mx = dp[now][l - 1]; return; } int mid = (l + r) >> 1; rebuild(x << 1, l, mid); rebuild(x << 1 | 1, mid + 1, r); U(x); } void C(int x, int l, int r, int st, int en) { if (l == st && r == en) { tr[x].mx++; tr[x].lazy++; return; } D(x); int mid = (l + r) >> 1; if (st <= mid) C(x << 1, l, mid, st, min(en, mid)); if (en > mid) C(x << 1 | 1, mid + 1, r, max(st, mid + 1), en); U(x); } int Q(int x, int l, int r, int st, int en) { if (l == st && r == en) { return tr[x].mx; } D(x); int t = 0, mid = (l + r) >> 1; if (st <= mid) t = max(t, Q(x << 1, l, mid, st, min(en, mid))); if (en > mid) t = max(t, Q(x << 1 | 1, mid + 1, r, max(st, mid + 1), en)); return t; } int main() { scanf( %d%d , &n, &m); memset(las, 0, sizeof(las)); for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); pre[i] = las[a[i]]; las[a[i]] = i; } memset(dp, 0, sizeof(dp)); for (int i = 1; i <= m; i++) { now = i - 1; rebuild(1, 1, n); for (int j = 1; j <= n; j++) { C(1, 1, n, pre[j] + 1, j); dp[i][j] = Q(1, 1, n, 1, j); } } printf( %d , dp[m][n]); }
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, f = 1, ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) x = (x << 1) + (x << 3) + ch - 0 , ch = getchar(); return f * x; } const long long maxn = 1000100, inf = 1000000000000000LL, mod = 998244353; long long n, m, a[maxn]; long long x, y, c; long long f[maxn], d[maxn], g[maxn], del[maxn], Max[maxn], mi[20], flag[maxn], level; pair<long long, long long> ch(long long x) { for (long long i = (0); i <= (11); i++) if (x < mi[i]) return make_pair(i, x - mi[i]); } void upp(long long o) { while (f[o] > 0) { g[o]++; Max[o] = f[o] = Max[o] + mi[g[o] - 1] - mi[g[o]]; } } void pushdown(long long x) { if (del[x]) { Max[x << 1] += del[x], Max[x << 1 | 1] += del[x]; if (d[x << 1]) f[x << 1] += del[x], upp(x << 1); else del[x << 1] += del[x]; if (d[x << 1 | 1]) f[x << 1 | 1] += del[x], upp(x << 1 | 1); else del[x << 1 | 1] += del[x]; del[x] = 0; } if (d[x]) { Max[x << 1] = Max[x << 1 | 1] = f[x]; del[x << 1] = del[x << 1 | 1] = 0; g[x << 1] = g[x << 1 | 1] = g[x]; d[x << 1] = d[x << 1 | 1] = 1; f[x << 1] = f[x << 1 | 1] = f[x]; d[x] = 0; f[x] = 0; } } void updata(long long x) { Max[x] = max(Max[x << 1], Max[x << 1 | 1]); g[x] = (Max[x << 1] > Max[x << 1 | 1]) ? g[x << 1] : g[x << 1 | 1]; if (flag[x << 1] && flag[x << 1 | 1] && Max[x << 1] == Max[x << 1 | 1] && g[x << 1] == g[x << 1 | 1]) flag[x] = 1; else flag[x] = 0; } long long query(long long l, long long r, long long o) { if (l > y || r < x) return -inf; if (l >= x && r <= y) { level = g[o]; return Max[o]; } pushdown(o); return max(query(l, ((l + r) >> 1), o << 1), query(((l + r) >> 1) + 1, r, o << 1 | 1)); } void change(long long l, long long r, long long o) { if (l > y || r < x) return; if (l >= x && r <= y) { d[o] = 1; flag[o] = 1; del[o] = 0; pair<long long, long long> x = ch(c); f[o] = x.second; Max[o] = x.second; g[o] = x.first; return; } pushdown(o); change(l, ((l + r) >> 1), o << 1); change(((l + r) >> 1) + 1, r, o << 1 | 1); updata(o); } void Change(long long l, long long r, long long o) { if (l > y || r < x) return; if (l >= x && r <= y) { Max[o] += c; if (d[o]) { f[o] += c; if (f[o] > 0) { while (Max[o] > 0) { g[o]++; Max[o] = f[o] = Max[o] + mi[g[o] - 1] - mi[g[o]]; } } } else del[o] += c; return; } pushdown(o); Change(l, ((l + r) >> 1), o << 1); Change(((l + r) >> 1) + 1, r, o << 1 | 1); updata(o); } void upgrade(long long l, long long r, long long o) { if (flag[o]) { while (Max[o] > 0) { g[o]++; Max[o] = Max[o] + mi[g[o] - 1] - mi[g[o]]; } return; } pushdown(o); if (Max[o << 1] > 0) upgrade(l, ((l + r) >> 1), o << 1); if (Max[o << 1 | 1] > 0) upgrade(((l + r) >> 1) + 1, r, o << 1 | 1); updata(o); } void build(long long l, long long r, long long o) { if (l == r) { flag[o] = 1; pair<long long, long long> x = ch(a[l]); Max[o] = x.second; g[o] = x.first; flag[o] = 1; return; } build(l, ((l + r) >> 1), o << 1); build(((l + r) >> 1) + 1, r, o << 1 | 1); updata(o); } int main() { n = read(); m = read(); mi[0] = 1; for (long long i = (1); i <= (11); i++) mi[i] = mi[i - 1] * 42; for (long long i = (1); i <= (n); i++) a[i] = read(); build(1, n, 1); while (m--) { long long ch = read(); if (ch == 1) { y = x = read(); printf( %I64d n , query(1, n, 1) + mi[level]); } if (ch == 2) { x = read(), y = read(), c = read(); change(1, n, 1); } if (ch == 3) { x = read(), y = read(), c = read(); do { Change(1, n, 1); upgrade(1, n, 1); if (Max[1] < 0) break; } while (1); } } return 0; }
// zukonit14 - Kunal Raut #include<bits/stdc++.h> using namespace std; // #include ext/pb_ds/assoc_container.hpp // #include ext/pb_ds/tree_policy.hpp // using namespace __gnu_pbds; // template<class T> // using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update> ; // template<class key, class value, class cmp = std::less<key>> // using omap = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>; // find_by_order(k) returns iterator to kth element starting from 0; // order_of_key(k) returns count of elements strictly smaller than k; /*/-----------------------------DEFINES----------------------------------/*/ #define ll long long #define ld long double #define pb push_back #define mp make_pair #define fi first #define se second #define pi 3.1415926535 #define all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() #define ms(s, n) memset(s, n, sizeof(s)) #define prec(n) fixed<<setprecision(n) #define forci(p,n) for(ll i=p;i<(ll)n;i++) #define forcj(p,n) for(ll j=p;j<(ll)n;j++) #define forc(i,p,n) for(ll i=p;i<(ll)n;i++) #define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define bits(a) __builtin_popcountll(a) #define djokovic freopen( input00.txt , r , stdin);freopen( output00.txt , w , stdout); #define inrange(i,a,b) ((i>=min(a,b)) && (i<=max(a,b))) #define sz(a) (ll)a.size() #define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll)) #define mine(a) (*min_element((a).begin(), (a).end())) #define maxe(a) (*max_element((a).begin(), (a).end())) #define mini(a) ( min_element((a).begin(), (a).end()) - (a).begin()) #define maxi(a) ( max_element((a).begin(), (a).end()) - (a).begin()) #define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin()) #define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin()) clock_t time_p = clock(); void rtime() {time_p = clock() - time_p; cout << nTime Taken : << fixed << (float)(time_p) / CLOCKS_PER_SEC << s n ;} /*/-----------------------------INLINE FUNCTIONS----------------------------------/*/ inline ll gcd(ll a, ll b) {if (b == 0) return a; return gcd(b, a % b);} inline ll lcm(ll a, ll b) {return (a / gcd(a, b) * b);} inline bool isprime(ll n) {ll i; for (i = 2; i <= sqrt(n); i++) {if (n % i == 0)return false;} return true;} inline ll ceil(ll num, ll den) {return ((num + den - 1) / den);} /*/-----------------------------TRACE FUNCTIONS----------------------------------/*/ template<class T> ostream& operator<<(ostream &os, string V) { os << [ ; for (auto v : V) os << v << ; return os << ] ; } template<class T> ostream& operator<<(ostream &os, vector<T> V) { os << [ ; for (auto v : V) os << v << ; return os << ] ; } template<class T> ostream& operator<<(ostream &os, set<T> S) { os << { ; for (auto s : S) os << s << ; return os << } ; } template<class T> ostream& operator<<(ostream &os, multiset<T> S) { os << { ; for (auto s : S) os << s << ; return os << } ; } template<class T, class T1> ostream& operator<<(ostream &os, map<T, T1> S) { os << { ; for (auto s : S) os << ( << s.first << , << s.second << ) ; return os << } ; } template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) { return os << ( << P.first << , << P.second << ) ; } #define tracearr(a,n) {cout << #a<< : ;cout<< [ ;for (ll i = 0; i < n; i++) cout << a[i] << ;cout << ] n ;} #define TRACE #ifdef TRACE #define trace(...) __f(#__VA_ARGS__, __VA_ARGS__) template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << : << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, , ); cout.write(names, comma - names) << : << arg1 << | ; __f(comma + 1, args...); } #else #define trace(...) #endif /*/-----------------------------MODULAR ARITHMETIC----------------------------------/*/ // const ll modx = 1e9 + 7; const ll modx = 998244353; const ll mod = modx; inline ll add(ll x, ll y, ll mod = modx) { x += y; if (x >= mod) return x - mod; return x % mod; } inline ll sub(ll x, ll y, ll mod = modx) { x -= y; if (x < 0) return x + mod; return x; } inline ll mul(ll x, ll y, ll mod = modx) { return (x * 1ll * y) % mod; } inline ll expo(ll x, ll y, ll mod = modx) { ll ans = 1; while (y) { if (y & 1) ans = mul(ans, x, mod); x = mul(x, x, mod); y >>= 1; } return ans % mod; } inline ll inv(ll x, ll mod = modx) { return expo(x, mod - 2, mod); } /*/-----------------------------CODE BEGINS----------------------------------/*/ #define int long long ll rr[] = {0, 1, 1, 1, 0, -1, -1, -1}; ll cc[] = {1, 1, 0, -1, -1, -1, 0, 1}; //x-y -> URDL , row-column -> RDLU; 0-based; const ll MAXN = 2e5 + 100, MAX2N = 2e3 + 10, LG = 20, INF = 2e18, base = 33; const int N = 1e5 + 5; ll n, m, k, q, x, y, z, a[MAXN], d[MAXN], p[MAXN], vis[MAXN]; vector<ll>g[MAXN]; ll dp[MAXN]; void fill() { for (ll i = 0; i <= n; i++) d[i] = -1, p[i] = -1, dp[i] = 0, g[i].clear(), vis[i] = 0; } void dijkstra(ll s) { d[s] = 0; queue<ll>q; q.push(s); while (!q.empty()) { ll v = q.front(); q.pop(); for (auto u : g[v]) { if (d[u] == -1) { d[u] = d[v] + 1; q.push(u); } } } } void dfs(ll s) { vis[s] = 1; dp[s] = d[s]; for (auto to : g[s]) { if (!vis[to] && d[s] < d[to]) { dfs(to); } if (d[s] < d[to]) dp[s] = min(dp[s], dp[to]); else dp[s] = min(dp[s], d[to]); } } void solve() { cin >> n >> m; fill(); for (ll i = 0; i < m; i++) { ll u, v; cin >> u >> v; --u, --v; g[u].pb(v); } dijkstra(0); dfs(0); for (ll i = 0; i < n; i++) cout << dp[i] << ; cout << n ; } signed main() { bolt; #ifndef ONLINE_JUDGE djokovic; #endif ll t; t = 1; cin >> t; for (ll i = 1; i <= t; i++) { //cout << Case # << i << : ; solve(); } //rtime(); }
#include <bits/stdc++.h> using namespace std; string s; map<string, bool> vis; void ReadCube(string cube) { int _4D[4][6] = {{0, 1, 2, 3, 4, 5}, {0, 3, 2, 1, 5, 4}, {0, 5, 2, 4, 1, 3}, {0, 4, 2, 5, 3, 1}}; for (int i = 0; i < 4; ++i) { string x(6, ); for (int j = 0; j < 6; ++j) { x[j] = cube[_4D[i][j]]; } vis[x] = true; } } void rotate90() { swap(s[0], s[1]); swap(s[1], s[2]); swap(s[2], s[3]); ReadCube(s); } void Left(string c) { int p[] = {5, 1, 4, 3, 0, 2}; string x(6, ); for (int i = 0; i < 6; ++i) { x[i] = c[p[i]]; } ReadCube(x); } void Right(string c) { int p[] = {4, 1, 5, 3, 2, 0}; string x(6, ); for (int i = 0; i < 6; ++i) { x[i] = c[p[i]]; } ReadCube(x); } int main() { cin >> s; int ans = 0; sort(s.begin(), s.end()); do { if (vis[s]) continue; for (int i = 0; i < 4; ++i) { rotate90(); } Left(s); Right(s); ans++; } while (next_permutation(s.begin(), s.end())); cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; #define MAXN (int)(1e5 + 5) #define MAXL 20 #define F first #define S second #define endl n #define MOD (lli)(1e9 + 9) #define MOD2 (lli)(1e9 + 7) #define lli long long int #define sz(a) int(a.size()) #define DEBUG if (0) cout << aqui << endl; #define PI 2 * acos(0.0) typedef pair<int, lli> ii; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int dddx[] = {1, -1, 0, 0, 1, 1, -1, -1}; int dddy[] = {0, 0, 1, -1, 1, -1, 1, -1}; int t; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> t; while(t--) { int k, n, m; cin >> k >> n >> m; vector<int> vec, vec2; for(int i=0;i<n;i++) { int a; cin >> a; vec.push_back(a); } for(int i=0;i<m;i++) { int a; cin >> a; vec2.push_back(a); } vector<int> ans; int a = 0, b = 0; bool nono = false; while(a < sz(vec) or b < sz(vec2)) { bool at = false; if(b < sz(vec2)) { if(!vec2[b] or (a < sz(vec) && vec2[b] < vec[a])) at = true; } if(!at && a < sz(vec)) { ans.push_back(vec[a]); a++; } else if(b < sz(vec2)) { ans.push_back(vec2[b]); b++; } } int atual = k; for(auto i : ans) { if(!i) atual++; else if(i > atual) nono = true; } if(nono) { cout << -1 << endl; continue; } for(auto i : ans) cout << i << ; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-8; const int mod = 1e9 + 7; const int N = 1e6 + 10; const long long INF = 1e18; long long power(long long x, long long y) { long long t = 1; while (y > 0) { if (y % 2) y -= 1, t = t * x % mod; else y /= 2, x = x * x % mod; } return t; } long long I[100][100], temp[100][100], M[100][100], store[100][100]; void copyM(long long M1[][100], long long M2[][100], int m) { for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { M2[i][j] = M1[i][j]; } } } void matmul(long long M1[][100], long long M2[][100], int m) { for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { temp[i][j] = 0; for (int k = 0; k < m; ++k) temp[i][j] = (temp[i][j] + (M2[i][k] * M1[k][j]) % mod) % mod; } } } void setI(int m) { for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { if (i != j) I[i][j] = 0; else I[i][j] = 1; } } } void powerM(long long M1[][100], int m, long long n) { setI(m); while (n > 0) { if (n % 2 == 0) { matmul(M1, M1, m); copyM(temp, M1, m); n /= 2; } else { matmul(M1, I, m); copyM(temp, I, m); n -= 1; } } } long long t[105]; int main() { int n, b, k, x; scanf( %d%d%d%d , &n, &b, &k, &x); memset(M, 0, sizeof(M)); for (int i = 0; i < n; ++i) { int p; scanf( %d , &p); p %= x; t[p]++; } for (int i = 0; i < x; ++i) for (int j = 0; j < x; ++j) { M[i][(i * 10 + j) % x] += t[j]; } powerM(M, x, b); printf( %lld n , I[0][k]); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; int n; cin >> n; vector<long long> l(n + 1), t(n + 1); l[0] = 0; t[0] = 0; for (int i = 1; i <= n; i++) { char c; cin >> c; l[i] = l[i - 1]; t[i] = t[i - 1]; if (c == L ) l[i]--; else if (c == R ) l[i]++; else if (c == U ) t[i]++; else t[i]--; } long long low = 0; long long high = 1000000000LL * 1000000000LL * 4; long long mid; while (low < high) { mid = (low + high) / 2; long long tempx = (mid / n) * l[n] + l[mid % n]; long long tempy = (mid / n) * t[n] + t[mid % n]; if (abs(x1 + tempx - x2) + abs(y1 + tempy - y2) <= mid) { high = mid; } else { low = mid + 1; } } mid = low; long long tempx = (mid / n) * l[n] + l[mid % n]; long long tempy = (mid / n) * t[n] + t[mid % n]; if (abs(x1 + tempx - x2) + abs(y1 + tempy - y2) <= mid) cout << mid << endl; else cout << -1 << endl; }
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) using namespace std; const int N = 1200031; int n, ar[N], w[N]; int gcd(int a, int b) { while (a && b) a > b ? a %= b : b %= a; return a + b; } vector<pair<int, int> > edges; vector<int> entries[N]; int sz[N]; long long ANS[N]; vector<int> interesting; long long cur_pairs; void run_cleanup() { for (int i = 0; i < interesting.size(); i++) { int id = interesting[i]; sz[id] = 1; w[id] = id; } } int get(int x) { interesting.push_back(x); if (x == w[x]) return x; return w[x] = get(w[x]); } long long F(int x) { return 1ll * x * (x - 1) / 2; } void merge(int a, int b) { a = get(a); b = get(b); if (a == b) return; cur_pairs -= F(sz[a]); cur_pairs -= F(sz[b]); w[a] = b; sz[b] += sz[a]; cur_pairs += F(sz[b]); } vector<int> divs[N]; int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> ar[i]; } for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; edges.push_back(make_pair(a, b)); } for (int i = 0; i < edges.size(); i++) { int here = gcd(ar[edges[i].first], ar[edges[i].second]); entries[here].push_back(i); } for (int i = 1; i <= n; i++) { w[i] = i; sz[i] = 1; } for (int i = 1; i <= 200000; i++) { interesting.clear(); cur_pairs = 0; for (int j = i; j <= 200000; j += i) { for (int q = 0; q < entries[j].size(); q++) { int id = entries[j][q]; merge(edges[id].first, edges[id].second); } } ANS[i] = cur_pairs; run_cleanup(); } for (int i = 1; i <= 200000; i++) { for (int j = i * 2; j <= 200000; j += i) divs[j].push_back(i); } for (int i = 200000; i; --i) { for (int j = 0; j < divs[i].size(); j++) { int p = divs[i][j]; ANS[p] -= ANS[i]; } } for (int i = 1; i <= n; i++) { ANS[ar[i]]++; } for (int i = 1; i <= 200000; i++) { if (ANS[i]) cout << i << << ANS[i] << endl; } return 0; }
#include <bits/stdc++.h> int a, n; int main() { scanf( %d %d , &n, &a); int res = 0; if (a % 2 == 1) printf( %d n , a / 2 + a % 2); else printf( %d n , n / 2 - a / 2 + 1); return 0; }
#include <bits/stdc++.h> using namespace std; int compare(const void* a, const void* b) { return (*(int*)a - *(int*)b); } int main() { int n; long long k, sum2, sum3; int a[100005], c[100005]; long long b[100005]; int i, j, count; cin >> n; cin >> k; for (i = 0; i < n; i++) scanf( %d , &a[i]); sort(a, a + n); count = 0; for (i = 0; i < n; i++) { j = i; while (a[j] == a[i] && j < n) j++; b[count] = j - i; c[count] = a[i]; count++; i = j - 1; } sum2 = 0; for (i = 0; i < count; i++) { if (sum2 + b[i] * n >= k) { sum3 = 0; for (j = 0; j < count; j++) { sum3 += (b[i] * b[j]); if (sum2 + sum3 >= k) { printf( %d %d , c[i], c[j]); return 0; } } } else sum2 += (b[i] * n); } return 0; }
#include <bits/stdc++.h> using namespace std; long long x, y, z, t1, t2, t3; int main() { scanf( %lld%lld%lld%lld%lld%lld , &x, &y, &z, &t1, &t2, &t3); puts(abs(x - y) * t1 >= abs(x - y) * t2 + abs(x - z) * t2 + 3 * t3 ? YES : NO ); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, l, r, k, c, i; string s; int main() { ios_base::sync_with_stdio(0); cin >> n; if (n == 0) { cout << a << n ; return 0; } while (n > 0) { l = 1; r = 100000; k++; while (l < r) { c = (l + r) / 2; if (c * (c - 1) / 2 > n) r = c; else l = c + 1; } if (l * (l - 1) / 2 > n) l--; n -= (l * (l - 1) / 2); for (i = 1; i <= l; i++) s += char(96 + k); } cout << s << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const long long MAX = 100005; const long double PI = 3.14159265359; const long double G = 9.807; const long long INF = 1e18; const long double EPS = 1e-6; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } bool isprime(long long a) { if (a == 2) return 1; if (!(a & 1)) return 0; for (long long i = 3; i * i <= a; i += 2) if (a % i == 0) return 0; return 1; } long long mpow(long long base, long long exponent, long long modulus) { if (modulus == 1) return 0; long long result = 1; base = base % modulus; while (exponent) { if (exponent % 2 == 1) result = (result * base) % modulus; exponent = exponent >> 1; base = (base * base) % modulus; } return result; } long long minv(long long a, long long mod) { long long _gcd = gcd(a, mod); assert(_gcd == 1); return mpow(a, mod - 2, mod); } long long t; long long dp[35]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> t; while (t--) { long long d, mod; cin >> d >> mod; long long temp = d; long long mxb = 0; memset(dp, 0, sizeof dp); for (long long i = 0; i <= 32; ++i) { long long p = (1LL << i); if (p > temp) { mxb = i; break; } } long long ans = 1 % mod; dp[1] = 1 % mod; for (long long i = 2; i <= mxb; ++i) { long long nums = mpow(2LL, i - 1, mod); if (i == mxb) { nums = d - mpow(2LL, i - 1, mod) + 1; nums += mod; nums %= mod; } dp[i] += nums; dp[i] %= mod; for (long long j = i - 1; j >= 1; --j) { dp[i] += dp[j] * nums % mod; dp[i] %= mod; } ans += dp[i]; ans %= mod; } cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline T smin(T &a, const T &b) { return a > b ? a = b : a; } template <typename T> inline T smax(T &a, const T &b) { return a < b ? a = b : a; } inline void add(long long &l, const long long &r) { l = (l + r) % (long long)10139; } long long gcd(long long v, long long u) { return u ? gcd(u, v % u) : v; } long long po(long long v, long long u) { return u ? (po(v * v % (long long)10139, u >> 1) * (u & 1 ? v : 1) % (long long)10139) : 1; } int n, m; int p[200010]; pair<long long, pair<int, int>> ed[200010]; vector<int> adj[200010]; const int lca_LG = 20; int lca_par[lca_LG][200010], lca_level[200010], lca_cost[lca_LG][200010]; inline int oo(int v, int u) { return ed[v].second.second + ed[v].second.first - u; } void lca_dfs(int lca_v, int lca_papa = 0) { lca_par[0][lca_v] = lca_papa; lca_level[lca_v] = lca_level[lca_papa] + 1; for (auto lca_e : adj[lca_v]) { if (oo(lca_e, lca_v) != lca_papa) { lca_cost[0][oo(lca_e, lca_v)] = ed[lca_e].first; lca_dfs(oo(lca_e, lca_v), lca_v); } } } int get_lca(int lca_v, int lca_u) { int res = 0; if (lca_level[lca_v] < lca_level[lca_u]) { swap(lca_v, lca_u); } for (int i = lca_LG - 1; ~i; i--) { if (lca_level[lca_par[i][lca_v]] >= lca_level[lca_u]) { smax(res, lca_cost[i][lca_v]); lca_v = lca_par[i][lca_v]; } } if (lca_v == lca_u) { return res; } for (int i = lca_LG - 1; ~i; i--) { if (lca_par[i][lca_v] != lca_par[i][lca_u]) { smax(res, lca_cost[i][lca_v]); smax(res, lca_cost[i][lca_u]); lca_v = lca_par[i][lca_v]; lca_u = lca_par[i][lca_u]; } } smax(res, lca_cost[0][lca_v]); smax(res, lca_cost[0][lca_u]); return res; } int get_p(int v) { if (p[v]) return p[v] = get_p(p[v]); return v; } bool merge(int v, int u) { v = get_p(v); u = get_p(u); if (v == u) return false; p[v] = u; return true; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) cin >> ed[i].second.first >> ed[i].second.second >> ed[i].first; sort(ed, ed + m); long long nc = n, cost = 0; for (int i = 0; i < m; i++) { if (merge(ed[i].second.first, ed[i].second.second)) { cost += ed[i].first; nc--; adj[ed[i].second.first].push_back(i); adj[ed[i].second.second].push_back(i); } } if (nc == 1) { lca_dfs(1); for (int i = 1; i < lca_LG; i++) { for (int j = 1; j <= n; j++) { lca_par[i][j] = lca_par[i - 1][lca_par[i - 1][j]]; lca_cost[i][j] = max(lca_cost[i - 1][j], lca_cost[i - 1][lca_par[i - 1][j]]); } } } int q; cin >> q; while (q--) { int v, u; cin >> v >> u; if (nc > 2) { cout << -1 << n ; continue; } if (nc == 2) { if (get_p(v) == get_p(u)) { cout << -1 << n ; continue; } cout << cost << n ; continue; } cout << cost - get_lca(v, u) << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); std::cin.tie(NULL); std::cout.tie(NULL); ; string s; getline(cin, s); long long int ans = 0, cur = 0; int sg = +1; for (int i = (0); i < (int((s).size())); i += (1)) { if (s[i] == + || s[i] == - ) { ans += sg * cur; cur = 0; } if (s[i] == + ) sg = +1; if (s[i] == - ) sg = -1; cur = (cur * 10) + (s[i] - 0 ); if (i == int((s).size()) - 1) { ans += sg * cur; } } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 5; int mark[2][M]; int h[2][M]; int32_t main() { ios_base::sync_with_stdio(false); ; int n, k; cin >> n >> k; for (int i = 0; i < 2; i++) { string s; cin >> s; for (int j = 0; j < n; j++) if (s[j] == X ) mark[i][j] = 1; } mark[0][0] = 1; queue<pair<int, int> > q; q.push(make_pair(0, 0)); while (q.size()) { int x = (q.front()).first; int y = (q.front()).second; if (y >= n) return cout << YES << endl, 0; int t = h[x][y]; for (int i = 0; i < 2; i++) if (!mark[i][t]) mark[i][t] = 1; if (!mark[x][y + 1]) { h[x][y + 1] = h[x][y] + 1; mark[x][y + 1] = 1; q.push(make_pair(x, y + 1)); } if (y > 0 && !mark[x][y - 1]) { h[x][y - 1] = h[x][y] + 1; mark[x][y - 1] = h[x][y] + 1; q.push(make_pair(x, y - 1)); } if (!mark[1 - x][y + k]) { h[1 - x][y + k] = h[x][y] + 1; mark[1 - x][y + k] = 1; q.push(make_pair(1 - x, y + k)); } q.pop(); } return cout << NO << endl, 0; }
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; int n; vector<int> a[200]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed; cin >> n; int k = (1 + sqrt(1 + 8 * n)) / 2, x = 1; cout << k << endl; for (int i = 1; i <= k; i++) { for (int j = i + 1; j <= k; j++) { a[i].push_back(x); a[j].push_back(x); x++; } } for (int i = 1; i <= k; i++) { int m = a[i].size(); for (int j = 0; j < m; j++) { cout << a[i][j] << (j == m - 1 ? n : ); } } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> adj[100000]; vector<int> hs; bool possible(int wantvalue, int maxcost) { const int nhs = hs.size(); long long cost = 0; int i = 0; for (int j = 0; j <= nhs; j++) { while (j - i > wantvalue) { cost -= (hs[j - 1] - hs[i]); i++; } if (j - i >= wantvalue && cost <= (long long)maxcost) return true; if (j - i > 0 && j < nhs) { cost += (long long)(hs[j] - hs[j - 1]) * (j - i); } } return false; } void dfs(int v, int parent, int height) { if (parent != -1) hs.push_back(height); for (int i = 0; i < (int)adj[v].size(); i++) { int v2 = adj[v][i]; if (v2 == parent) continue; dfs(v2, v, height + 1); } } int main(int argc, char** argv) { int m, k, p; scanf( %d %d %d , &m, &k, &p); for (int i = 0; i < m - 1; i++) { int a, b; scanf( %d %d , &a, &b); a--, b--; adj[a].push_back(b); adj[b].push_back(a); } dfs(0, -1, 0); sort(hs.begin(), hs.end()); int l = 0, r = k; while (l < r) { int mid = (l + r + 1) / 2; if (possible(mid, p)) l = mid; else r = mid - 1; } printf( %d n , l); return 0; }
#include <bits/stdc++.h> using namespace std; long double Ax, Ay, Bx, By, Cx, Cy, a11, a12, a21, a22, det; bool flag; void f(double x, double y) { double tx, ty; tx = a11 * x + a12 * y; ty = a21 * x + a22 * y; cerr << tx << << ty << n ; tx = fabs(tx); ty = fabs(ty); tx += 1e-11; ty += 1e-11; tx -= (long long)tx; ty -= (long long)ty; if (flag) { tx = x; ty = y; } if (fabs(tx) < 1e-10 && fabs(ty) < 1e-10) { cout << YES n ; exit(0); } } int main() { cin >> Ax >> Ay >> Bx >> By >> Cx >> Cy; det = Cx * Cx + Cy * Cy; flag = fabs(det) < 1e-10; a11 = Cx / det; a12 = Cy / det; a21 = -Cy / det; a22 = Cx / det; f(Bx - Ax, By - Ay); f(Bx - Ay, By + Ax); f(Bx + Ax, By + Ay); f(Bx + Ay, By - Ax); cout << NO n ; }
#include <bits/stdc++.h> using namespace std; vector<int> a, b, d1, d2; int main() { int n, l; cin >> n >> l; a.resize(n); b.resize(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 0; i < n; ++i) { cin >> b[i]; } a.push_back(l + a[0]); b.push_back(l + b[0]); for (int i = 1; i <= n; ++i) { d1.push_back(a[i] - a[i - 1]); d2.push_back(b[i] - b[i - 1]); } for (int i = 0; i < n; i++) { bool flag = 1; for (int j = 0; j < n; j++) { if (d1[j] != d2[(j + i) % n]) { flag = 0; break; } } if (flag) { cout << YES ; return 0; } } cout << NO ; return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long dp[11][2][1010][1010]; string s, t; long long bt(int K, bool f, int a, int b) { if (K == 0) return 0; long long& ref = dp[K][f][a][b]; if (ref != -1) return ref; ref = INT_MIN; if (f) { ref = bt(K - 1, 0, a, b); if (a < s.size() && b < t.size() && s[a] == t[b]) ref = max(ref, 1 + bt(K, 1, a + 1, b + 1)); } else { if (a < s.size()) ref = bt(K, 0, a + 1, b); if (b < t.size()) ref = max(ref, bt(K, 0, a, b + 1)); if (a < s.size() && b < t.size() && s[a] == t[b]) ref = max(ref, 1 + bt(K, 1, a + 1, b + 1)); } return ref; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N, M, K; cin >> N >> M >> K; cin >> s >> t; memset(dp, -1, sizeof dp); assert(bt(K, 0, 0, 0) >= K); cout << bt(K, 0, 0, 0) << n ; return 0; }
#include <bits/stdc++.h> using namespace std; void model(vector<int>& a, const vector<pair<int, int> >& v) { for (pair<int, int> p : v) swap(a[p.first], a[p.second]); } bool check(int n, const vector<pair<int, int> >& v) { vector<int> a(n); for (int i = 0; i < n; ++i) a[i] = i; model(a, v); for (int i = 0; i < n; ++i) if (a[i] != i) return false; return true; } vector<pair<int, int> > sol0, sol1; void solve(int n, vector<pair<int, int> >& res) { if (n < 4) return; solve(n - 4, res); for (int i = 0; i + 1 < n - 4; i += 2) { res.push_back(pair<int, int>(i, n - 4)); res.push_back(pair<int, int>(i + 1, n - 3)); res.push_back(pair<int, int>(i + 1, n - 4)); res.push_back(pair<int, int>(i, n - 3)); res.push_back(pair<int, int>(i, n - 2)); res.push_back(pair<int, int>(i + 1, n - 1)); res.push_back(pair<int, int>(i + 1, n - 2)); res.push_back(pair<int, int>(i, n - 1)); } if (n % 2) { res.push_back(pair<int, int>(n - 5, n - 4)); res.push_back(pair<int, int>(n - 5, n - 3)); res.push_back(pair<int, int>(n - 5, n - 2)); res.push_back(pair<int, int>(n - 4, n - 1)); res.push_back(pair<int, int>(n - 5, n - 1)); for (pair<int, int> p : sol1) { res.push_back(pair<int, int>(p.first + n - 4, p.second + n - 4)); } } else { for (pair<int, int> p : sol0) { res.push_back(pair<int, int>(p.first + n - 4, p.second + n - 4)); } } assert(res.size() == n * (n - 1) / 2); } void solve0() { vector<pair<int, int> > v; for (int i = 0; i < 4; ++i) for (int j = i + 1; j < 4; ++j) v.push_back(pair<int, int>(i, j)); do { if (check(4, v)) { sol0 = v; break; } } while (next_permutation(v.begin(), v.end())); assert(!sol0.empty()); } void solve1() { vector<pair<int, int> > v; for (int i = 0; i < 4; ++i) for (int j = i + 1; j < 4; ++j) if (pair<int, int>(i, j) != pair<int, int>(0, 3)) v.push_back(pair<int, int>(i, j)); do { vector<int> st = {3, 0, 1, 2}; vector<int> en = {0, 1, 2, 3}; model(st, v); if (st == en) { sol1 = v; break; } } while (next_permutation(v.begin(), v.end())); assert(!sol1.empty()); } int main() { std::ios::sync_with_stdio(false); solve0(); solve1(); int n; cin >> n; if (n * (n - 1) / 2 % 2) { cout << NO n ; return 0; } cout << YES n ; vector<pair<int, int> > res; solve(n, res); for (int i = 0; i < res.size(); ++i) { cout << min(res[i].first, res[i].second) + 1 << << max(res[i].first, res[i].second) + 1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int t; long long x, y; int main() { cin >> t; while (t--) { cin >> x >> y; if (x == y) cout << x << endl; else if (x > y) cout << x + y << endl; else cout << y - y % x / 2 << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { long long int q; cin >> q; for (long long int l = 0; l < q; l++) { long long int n; cin >> n; long long int ans = 0; while (n != 1) { long long int track = ans; while (n % 2 == 0) { n = n / 2; ans++; } while (n % 3 == 0) { n = 2 * n / 3; ans++; } while (n % 5 == 0) { n = 4 * n / 5; ans++; } if (track == ans) break; } if (n != 1) cout << -1 << endl; else cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n, i, j; cin >> t; while (t--) { cin >> n; if (n % 2 == 0) { cout << n / 2 << << n / 2; } else { for (i = 3, j = 0; i * i <= n; i++) { if (n % i == 0) { cout << n / i << << n - (n / i); j = 1; break; } } if (j == 0) cout << 1 << << n - 1; } printf( n ); } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 2E5 + 10; const int P = 1E9 + 7; int n, q; struct node { int l, r, id; } qu[MAXN]; int a[MAXN], pri[1100], cnt; int num[1000001]; long long ans[MAXN]; long long bin[MAXN]; vector<int> dir[1000001]; vector<int> rec[MAXN]; int pow(int a, int n) { long long b = a; long long ret = 1; while (n) { if (n & 1) ret = ret * b % P; b = b * b % P; n >>= 1; } return ret; } bool cmp(node n1, node n2) { if (n1.l != n2.l) return n1.l < n2.l; else return n1.r < n2.r; } bool prime(int p) { for (int i = 0; pri[i] * pri[i] <= p; i++) if (!(p % pri[i])) return false; return true; } void init() { pri[cnt = 0] = 2; for (int i = 3; i < 100000; i++) if (prime(i)) pri[++cnt] = i; cnt++; for (int i = 0; i < MAXN; i++) rec[i].clear(); for (int i = 0; i < MAXN; i++) bin[i] = 1; for (int i = 0; i < 1000001; i++) dir[i].clear(); memset(num, 0, sizeof(num)); } int exgcd(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return b; } else { long long tx, ty; long long d = exgcd(b % a, a, tx, ty); x = ty - (b / a) * tx; y = tx; return d; } } int inverse(int k) { long long a = k, b = P, x, y; exgcd(a, b, x, y); return (x % P + P) % P; } int main() { int temp, l, u, v; init(); scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); scanf( %d , &q); for (int i = 0; i < q; i++) { scanf( %d%d , &qu[i].l, &qu[i].r); qu[i].id = i; } sort(qu, qu + q, cmp); for (int i = 1; i <= n; i++) { for (int j = 0; pri[j] * pri[j] <= a[i]; j++) { while (a[i] % pri[j] == 0) { a[i] /= pri[j]; if (dir[pri[j]].empty()) temp = pri[j] - 1; else temp = pri[j]; dir[pri[j]].push_back(i); rec[i].push_back(pri[j]); for (int k = i; k <= n; k += k & -k) bin[k] = bin[k] * temp % P; } } if (a[i] != 1) { if (dir[a[i]].empty()) temp = a[i] - 1; else temp = a[i]; dir[a[i]].push_back(i); rec[i].push_back(a[i]); for (int k = i; k <= n; k += k & -k) bin[k] = bin[k] * temp % P; } } l = 1; for (int i = 0; i < q; i++) { for (; l < qu[i].l; l++) { for (int j = 0; j < rec[l].size(); j++) { u = rec[l][j]; if (num[u] < dir[u].size() - 1) v = dir[u][++num[u]]; else v = n + 1; temp = (long long)(u - 1) * inverse(u) % P; for (int k = v; k <= n; k += k & -k) bin[k] = bin[k] * temp % P; } } temp = qu[i].id; ans[temp] = 1; for (int k = qu[i].l - 1; k > 0; k -= k & -k) ans[temp] = ans[temp] * bin[k] % P; ans[temp] = inverse(ans[temp]); for (int k = qu[i].r; k > 0; k -= k & -k) { ans[temp] = ans[temp] * bin[k] % P; } } for (int i = 0; i < q; i++) printf( %lld n , ans[i]); }
#include <bits/stdc++.h> using namespace std; int main() { int i; string a[3]; cin >> a[0] >> a[1] >> a[2]; for (i = 0; i < 3; i++) { if ((a[i] == rock ) && (a[(i + 1) % 3] == scissors ) && (a[(i + 2) % 3] == scissors )) { break; } if ((a[i] == scissors ) && (a[(i + 1) % 3] == paper ) && (a[(i + 2) % 3] == paper )) { break; } if ((a[i] == paper ) && (a[(i + 1) % 3] == rock ) && (a[(i + 2) % 3] == rock )) { break; } } if (i == 0) { cout << F << endl; } else if (i == 1) { cout << M << endl; } else if (i == 2) { cout << S << endl; } else { cout << ? << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, pos, lo, hi; while (cin >> n >> pos >> lo >> hi) { int cnt = 0; if (lo == 1 && hi == n) { cout << 0 << endl; continue; } int xlo = abs(pos - lo); int xhi = abs(pos - hi); if (lo - 1 >= 1 && hi + 1 <= n) { cnt += 2; if (xlo < xhi) { cnt += xlo; } else cnt += xhi; cnt += hi - lo; } else if (lo - 1 >= 1) { cnt += xlo; cnt++; } else { cnt += xhi; cnt++; } cout << cnt << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int M = 1e5 + 5; int main() { ios_base::sync_with_stdio(false); int tt; cin >> tt; while (tt--) { int n, i, j; cin >> n; vector<long long> v[N]; for (i = 0; i < n; i++) { int m; long long p; cin >> m >> p; v[m].push_back(p); } int want[N]; int cnt = 0; for (i = 0; i < n; i++) { int lu = v[i].size(); if (lu == 0) continue; want[i] = max(0, i - cnt); cnt += lu; } multiset<long long> st; long long ans = 0; int taken = 0; for (i = n - 1; i >= 0; i--) { int lu = v[i].size(); if (lu == 0) continue; for (j = 0; j < lu; j++) { st.insert(v[i][j]); } int fu = st.size(); int req = min(max(0, want[i] - taken), fu); for (j = 0; j < req; j++) { ans += *(st.begin()); taken++; st.erase(st.begin()); } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 300010; const int MOD = 1000000007; int n, m; int a[N], b[N]; char mm[10010]; vector<int> f[N]; long long w[2010]; int RM(int a) { if (!m) return 0; int t; for (int i = m - 1; i >= 0; i--) { t = w[i] % a; w[i] /= a; if (i) w[i - 1] += t * 1000000000ll; } while (m && !w[m - 1]) m--; return t; } int main() { scanf( %d , &n); for (int i = 2; i <= n; i++) scanf( %d , &a[i]); a[1] = 1; a[n + 1] = 1000000000; for (int i = 1; i <= n; i++) scanf( %d , &b[i]); scanf( %s , mm); for (int i = strlen(mm) - 1; i >= 0; i -= 9, m++) for (int j = max(0, i - 8); j <= i; w[m] = w[m] * 10 + mm[j++] - 0 ) ; f[1].push_back(1); for (int i = 1; i <= n; i++) { int s = f[i].size(), sum = 0, md = 0; if (a[i + 1] != 1) md = RM(a[i + 1]); for (int j = 0; j < s + b[i]; j++) { if (j < s) (sum += f[i][j]) %= MOD; if (j > b[i]) (sum += MOD - f[i][j - b[i] - 1]) %= MOD; if (j % a[i + 1] == md) f[i + 1].push_back(sum); } } printf( %d n , m ? 0 : f[n + 1].size() == 0 ? 0 : f[n + 1][0]); }
#include <bits/stdc++.h> using namespace std; char s[500005], t[500005]; long long ans; int n, k, now, tmp; int main() { scanf( %d %d n , &n, &k); fread(s + 1, 1, n, stdin); getchar(); fread(t + 1, 1, n, stdin); s[n + 1] = # ; if (k == 1) return printf( %d n , n), 0; for (now = 1; s[now] == t[now]; now++) ; if (now > n) return printf( %d n , n), 0; ans = now + 1; for (int i = now + 1; i <= n; i++) { tmp = tmp << 1; if (s[i] == a ) tmp++; if (t[i] == b ) tmp++; tmp = min(k - 2, tmp); ans += tmp + 2; } printf( %lld n , ans); }
#include <bits/stdc++.h> using namespace std; template <typename TF> void write_debug_output(ostream &out, TF const &f) { out << f << std::endl; } template <typename TF, typename... TR> void write_debug_output(ostream &out, TF const &f, TR const &...rest) { out << f << ; write_debug_output(out, rest...); } using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using vi = vector<int>; using vii = vector<pii>; using vll = vector<ll>; using vs = vector<string>; const int DR[] = {-1, 0, 1, 0, -1, 1, 1, -1}; const int DC[] = {0, -1, 0, 1, -1, -1, 1, 1}; const double PI = acos(-1.0); const double EPS = 1e-9; const int MOD = 1e9 + 7; const int INF = 1073741823; const ll INFLL = 4e18; const int MAX = 100; int N, M, aa[MAX + 5], bb[MAX + 5]; void read() { cin >> N >> M; for (int i = 0; i < (int)N; i++) cin >> aa[i]; for (int i = 0; i < (int)M; i++) cin >> bb[i]; } void solve() { int xorA = 0, xorB = 0; for (int i = 0; i < (int)N; i++) xorA ^= aa[i]; for (int i = 0; i < (int)M; i++) xorB ^= bb[i]; if (xorA != xorB) { cout << NO n ; return; } cout << YES n ; int b = bb[1]; int c = aa[1]; int a = aa[0] ^ bb[1] ^ (xorB ^ bb[0] ^ bb[1]); for (int i = 0; i < (int)N; i++) { for (int j = 0; j < (int)M; j++) { if (i == 0 && j == 0) cout << a << ; else if (i == 0 && j == 1) cout << b << ; else if (i == 1 && j == 0) cout << c << ; else if (i == 0) cout << bb[j] << ; else if (j == 0) cout << aa[i] << ; else cout << 0 ; } cout << n ; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int TC = 1; for (int tc = (int)1; tc <= (int)TC; tc++) { read(); solve(); } }
#include <bits/stdc++.h> using namespace std; const int N = 1000007, M = 5000007; const long long INF = 1e14; inline void read(int &first) { char c = getchar(); while ((c < 48) || (c > 57)) c = getchar(); first = c ^ 48; c = getchar(); while ((c >= 48) && (c <= 57)) { first = first * 10 + (c ^ 48); c = getchar(); } } struct node { int l, r; int maxx; } tr[N << 2]; int n, m, t, k; int first[N], second; int a[N]; int num[N]; int s[N]; int pre[N]; void build(int p, int l, int r) { tr[p] = {l, r, 0}; if (l == r) { tr[p].maxx = s[r]; return; } int mid = l + r >> 1; build(p << 1, l, mid); build(p << 1 | 1, mid + 1, r); tr[p].maxx = max(tr[p << 1].maxx, tr[p << 1 | 1].maxx); } int query(int p, int l, int r) { if (tr[p].l >= l && tr[p].r <= r) { return tr[p].maxx; } int mid = tr[p].l + tr[p].r >> 1; int res = 0; if (l <= mid) res = max(res, query(p << 1, l, r)); if (r > mid) res = max(res, query(p << 1 | 1, l, r)); return res; } int main() { scanf( %d , &t); while (t--) { for (int i = 0; i <= n; ++i) s[i] = pre[i] = num[i] = 0; scanf( %d%d , &n, &m); for (int i = 1; i <= n; ++i) scanf( %d , &first[i]), a[i] = first[i]; for (int i = 1; i <= n; ++i) scanf( %d , &second); sort(a + 1, a + 1 + n); int tot = unique(a + 1, a + 1 + n) - a - 1; for (int i = 1; i <= n; ++i) num[lower_bound(a + 1, a + 1 + tot, first[i]) - a]++; for (int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + num[i]; for (int i = 1; i <= n; ++i) s[i] = pre[upper_bound(a + i + 1, a + 1 + tot, a[i] + m) - a - 1] - pre[i - 1]; build(1, 1, tot); int ans = 0; for (int i = 1; i <= tot; ++i) { ans = max(ans, s[i] + query(1, upper_bound(a + i + 1, a + 1 + tot, a[i] + m) - a, tot)); } printf( %d n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << : << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, , ); cerr.write(names, comma - names) << : << arg1 << | ; __f(comma + 1, args...); } int main() { int k; scanf( %d , &k); int x[10]; memset(x, 0, sizeof(x)); char s[5]; for (auto i = 0; i < 4; i++) { scanf( %s , s); for (auto j = 0; j < 4; j++) { if (s[j] == . ) { continue; } x[s[j] - 0 ]++; } } for (auto i = 1; i < 10; i++) { if (x[i] > 2 * k) { printf( NO n ); return 0; } } printf( YES n ); return 0; }
#include <bits/stdc++.h> using namespace std; int n; int ara[200010]; string res; string str; void func(int le, int ri, int chk, int past) { if (res.size() < str.size()) { res = str; } if (le > ri) { return; } if (chk == 0) { if ((past < ara[le]) && (ara[le] < ara[ri])) { str += L ; func(le + 1, ri, chk, ara[le]); str.pop_back(); } else if ((past < ara[ri]) && (ara[ri] < ara[le])) { str += R ; func(le, ri - 1, chk, ara[ri]); str.pop_back(); } else if ((past < ara[ri]) && (ara[ri] == ara[le])) { str += L ; func(le + 1, ri, 1, ara[le]); str.pop_back(); str += R ; func(le, ri - 1, 2, ara[ri]); str.pop_back(); } else if (past < ara[le]) { str += L ; func(le + 1, ri, chk, ara[le]); str.pop_back(); } else if (past < ara[ri]) { str += R ; func(le, ri - 1, chk, ara[ri]); str.pop_back(); } } else if (chk == 1) { if (past < ara[le]) { str += L ; func(le + 1, ri, chk, ara[le]); str.pop_back(); } } else { if (past < ara[ri]) { str += R ; func(le, ri - 1, chk, ara[ri]); str.pop_back(); } } } int main() { cin >> n; for (int i = 0; i <= n - 1; i++) { cin >> ara[i]; } str = ; res = ; func(0, n - 1, 0, 0); cout << res.size() << n ; cout << res << n ; return 0; }
#include <bits/stdc++.h> const int N = 300300; using namespace std; struct edge { int ul, ur, dl, dr, a, al, ar; long long xl, xr; }; edge comb(edge a, edge b, int sa, int sb) { edge g; g.a = max(a.a, b.a); g.al = a.al; g.xl = a.xl; g.ul = a.ul; g.dl = a.dl; g.ar = b.ar; g.xr = b.xr; g.ur = b.ur; g.dr = b.dr; if (a.xr == b.xl) { return g; } else if (a.xr > b.xl) { g.a = max(g.a, a.ar + b.dl); if (sb == b.dl) g.ar = a.ar + b.dl; if (sa == a.a) g.al = a.a + b.dl; if (sa == a.dl) g.dl += b.dl; if (sb == b.ur) g.ur += a.ur; g.a = max(g.a, max(g.al, g.ar)); return g; } else { g.a = max(g.a, a.dr + b.al); if (sb == b.a) g.ar = a.dr + b.a; if (sa == a.dr) g.al = a.dr + b.al; if (sb == b.dr) g.dr += a.dr; if (sa == a.ul) g.ul += b.ul; g.a = max(g.a, max(g.al, g.ar)); return g; } } edge t[4 * N]; int n; int a[N]; long long lz[4 * N]; void push(int x) { if (lz[x] == 0) return; t[x * 2].xl += lz[x]; t[x * 2].xr += lz[x]; t[x * 2 + 1].xl += lz[x]; t[x * 2 + 1].xr += lz[x]; lz[x * 2] += lz[x]; lz[x * 2 + 1] += lz[x]; lz[x] = 0; } void build(int x, int l, int r) { if (l == r) { t[x].xl = t[x].xr = a[l]; t[x].a = t[x].al = t[x].ar = t[x].dl = t[x].dr = t[x].ul = t[x].ur = 1; return; } int m = (l + r) / 2; build(x * 2, l, m); build(x * 2 + 1, m + 1, r); t[x] = comb(t[x * 2], t[x * 2 + 1], m - l + 1, r - m); } void upd(int x, int l, int r, int tl, int tr, long long d) { if (tl > tr) return; if (l == tl && r == tr) { lz[x] += d; t[x].xl += d; t[x].xr += d; return; } push(x); int m = (l + r) / 2; upd(x * 2, l, m, tl, min(m, tr), d); upd(x * 2 + 1, m + 1, r, max(m + 1, tl), tr, d); t[x] = comb(t[x * 2], t[x * 2 + 1], m - l + 1, r - m); } int main() { ios_base::sync_with_stdio(0); scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); build(1, 1, n); int m; scanf( %d , &m); for (int i = 1; i <= m; i++) { int l, r, d; scanf( %d%d%d , &l, &r, &d); upd(1, 1, n, l, r, d); printf( %d n , t[1].a); } }
#include <bits/stdc++.h> using namespace std; int c[100010]; long long ans; int main() { int n, x; scanf( %d%d , &n, &x); for (int i = 1; i <= n; i++) scanf( %d , &c[i]); sort(c + 1, c + n + 1); if (n < x) { ans = 0; for (int i = 1; i <= n; i++) ans += (long long)c[i] * (x - i + 1); printf( %I64d , ans); return 0; } if (n >= x) { ans = 0; for (int i = 1; i <= x; i++) ans += (long long)i * c[x + 1 - i]; for (int i = x + 1; i <= n; i++) ans += c[i]; } printf( %I64d , ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0) #define INP(arr,n) for(int a=0;a<n;a++)cin>>arr[a] #define OTP(arr,n) for(int a=0;a<n;a++)cout<<arr[a]<< #define ll long long int #define pb(v,a) v.push_back(a) int main() { IOS;int t = 0;cin >> t; while (t--) { ll n = 0;cin >> n; ll arr[n];INP(arr,n); cout << 0 ; for (ll a = 1;a < n;a++) { ll x = 0,y = 1; bitset <64> bs(arr[a - 1]); bitset <64> bs2(arr[a]); for (int b = 0;b < 64;b++,y *= 2) bs2[b] == 0 && bs[b] == 1 ? x += y,arr[a] += y: x = x; cout << x << ; } cout << n ; } }
#include <bits/stdc++.h> using namespace std; string p[3][2] = {{ lios , liala }, { etr , etra }, { initis , inites }}, s; pair<int, int> kind(string q) { int l = q.length(); if (l >= 4 && q.substr(l - 4, 4) == p[0][0]) return pair<int, int>(0, 0); if (l >= 5 && q.substr(l - 5, 5) == p[0][1]) return pair<int, int>(0, 1); if (l >= 3 && q.substr(l - 3, 3) == p[1][0]) return pair<int, int>(1, 0); if (l >= 4 && q.substr(l - 4, 4) == p[1][1]) return pair<int, int>(1, 1); if (l >= 6 && q.substr(l - 6, 6) == p[2][0]) return pair<int, int>(2, 0); if (l >= 6 && q.substr(l - 6, 6) == p[2][1]) return pair<int, int>(2, 1); return pair<int, int>(-1, -1); } int tran[2][3] = {{0, 1, -1}, {-1, -1, 1}}; int main() { int g = -1, st = -1, ok = true, tow = false; cin >> s; g = kind(s).second; if (g != -1) st = tran[0][kind(s).first]; while (cin >> s && ok) { tow = true; pair<int, int> t = kind(s); if (t.second != g) ok = false; if (st == -1) break; st = tran[st][t.first]; } if ((tow && ok && st == 1) || (!tow && g != -1)) cout << YES << endl; else cout << NO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, sb, a[4], data1[100001], num[100001]; void merge(int l, int r) { int mid = (l + r) / 2; int i = l, j = mid + 1, k = 0; while (i <= mid && j <= r) { if (data1[i] < data1[j]) num[k++] = data1[i++]; else num[k++] = data1[j++]; } while (i <= mid) num[k++] = data1[i++]; while (j <= r) num[k++] = data1[j++]; for (int p = 0; p < k; p++) data1[p + l] = num[p]; } void sort(int l, int r) { if (l >= r) return; int mid = (l + r) / 2; sort(l, mid); sort(mid + 1, r); merge(l, r); } int main() { cin >> n; for (int j = 0; j < n; j++) cin >> data1[j]; sort(0, n - 1); for (int i = 1; i <= 3; i++) { a[i] = 1; while (data1[sb] == data1[sb + 1] && sb < n - 1) { a[i]++; sb++; } sb++; } if (data1[0] == data1[1]) { if (data1[1] == data1[2]) cout << a[1] * (a[1] - 1) * (a[1] - 2) / 6; else cout << a[2]; } else { if (data1[1] == data1[2]) cout << a[2] * (a[2] - 1) / 2; else cout << a[3]; } }
#include <bits/stdc++.h> using namespace std; #pragma warning(disable : 4996) #pragma comment(linker, /STACK:336777216 ) const int sqrtn = 300; const int inf = 987654321; const int sz = 1e5 + 5; const int mod = 1e9 + 7; int n, vis[sz]; string str; int main() { scanf( %d , &n); cin >> str; int ret = 0, x = 0, y = 0, last = 0; if (str[0] == U ) { y++; last = 1; } else x++, last = 0; for (int i = 1; i < n; i++) { if (str[i] == U ) y++; else x++; if (vis[i - 1]) { if (y > x && last == 0) { ret++, last = 1; } else if (x > y && last == 1) { ret++, last = 0; } } if (y == x) vis[i] = 1; } printf( %d , ret); }
#include <bits/stdc++.h> using namespace std; namespace IO { const int maxn(1 << 21 | 1); char *iS, *iT, ibuf[maxn], obuf[maxn], *oS = obuf, *oT = obuf + maxn - 1, c, st[20]; int f, tp, len; inline char getc() { return iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, maxn, stdin), iS == iT ? EOF : *iS++) : *iS++; } template <class T> inline void read(T &x) { for (c = getc(), f = 1; c < 0 || c > 9 ; c = getc()) f = c == - ? -1 : 1; for (x = 0; c >= 0 && c <= 9 ; c = getc()) x = x * 10 + (c ^ 48); x *= f; } template <class T, class... Other> inline void read(T &x, Other &...y) { read(x), read(y...); } inline void flush() { fwrite(obuf, 1, oS - obuf, stdout), oS = obuf; } inline void putc(char ch) { *oS++ = ch; if (oS == oT) flush(); } template <class T> inline void print(T x, char ch = n ) { if (!x) putc( 0 ); if (x < 0) putc( - ), x = -x; while (x) st[++tp] = x % 10 + 0 , x /= 10; while (tp) putc(st[tp--]); putc(ch); } template <class T, class... Other> inline void print(T x, Other... y) { print(x), print(y...); } inline void print_str(char *s) { for (len = strlen(s), f = 0; f < len; ++f) putc(s[f]); } inline void read_str(char *s, int &l) { for (l = 0, c = getc(); (c < a || c > z ) && (c < A || c > Z ); c = getc()) ; for (; (c >= a && c <= z ) || (c >= A && c <= Z ); c = getc()) s[l++] = c; s[l] = 0 ; } } // namespace IO using IO ::flush; using IO ::print; using IO ::print_str; using IO ::read; using IO ::read_str; template <class T> inline void cmax(T &x, T y) { x = x >= y ? x : y; } template <class T, class... Other> inline void cmax(T &x, T y, Other... z) { Cmax(x, y), Cmax(x, z...); } template <class T> inline void cmin(T &x, T y) { x = x <= y ? x : y; } template <class T, class... Other> inline void cmin(T &x, T y, Other... z) { Cmin(x, y), Cmin(x, z...); } inline int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } const int mod(1e9 + 7); inline void inc(int &x, const int y) { x = x + y >= mod ? x + y - mod : x + y; } inline void dec(int &x, const int y) { x = x - y < 0 ? x - y + mod : x - y; } inline int add(const int x, const int y) { return x + y >= mod ? x + y - mod : x + y; } inline int sub(const int x, const int y) { return x - y < 0 ? x - y + mod : x - y; } inline int fpow(long long x, int y) { long long ret = 1; for (; y; y >>= 1, x = x * x % mod) if (y & 1) ret = ret * x % mod; return ret; } const int maxn(1e5 + 5); int k, lg[23], ans = 1, f[55][10005], num, inv[55]; long long n, d[23]; inline int solve(int p, int lim) { int i, j, l, ret = 0; memset(f, 0, sizeof(f)), f[lim][0] = 1; for (i = 1; i <= k; ++i) for (j = 0; j <= lim; ++j) for (l = j; l <= lim; ++l) inc(f[j][i], (long long)f[l][i - 1] * inv[l + 1] % mod); for (ret = 0, i = 0; i <= lim; ++i) inc(ret, (long long)f[i][k] * fpow(p, i) % mod); return ret; } int main() { long long i; read(n, k), ans = 1; for (i = 1; i <= 50; ++i) inv[i] = fpow(i, mod - 2); for (i = 2; i * i <= n; ++i) if (n % i == 0) { d[++num] = i; while (n % i == 0) n /= i, ++lg[num]; } if (n > 1) d[++num] = n, lg[num] = 1; for (i = 1; i <= num; ++i) ans = (long long)ans * solve(d[i] % mod, lg[i]) % mod; print(ans); return flush(), 0; }
#include <bits/stdc++.h> using namespace std; int ly1[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int ly2[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int ly3[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int nly[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int main() { int n; int i, j; cin >> n; int ara[n + 3]; for (i = 0; i < n; i++) cin >> ara[i]; int flg = 0; int cnt = 0; for (i = 0, cnt = 0; i < 36; i++, cnt++) { int pos = i % 36; for (j = 0; j < n; j++) { if (ara[j] != ly1[(i + j) % 36]) { flg = 1; break; } if (j == n - 1) { cout << YES ; return 0; } } } for (i = 0, cnt = 0; i < 36; i++, cnt++) { int pos = i % 36; for (j = 0; j < n; j++) { if (ara[j] != ly2[(i + j) % 36]) { flg = 1; break; } if (j == n - 1) { cout << YES ; return 0; } } } for (i = 0, cnt = 0; i < 36; i++, cnt++) { int pos = i % 36; for (j = 0; j < n; j++) { if (ara[j] != ly3[(i + j) % 36]) { flg = 1; break; } if (j == n - 1) { cout << YES ; return 0; } } } for (i = 0, cnt = 0; i < 36; i++, cnt++) { int pos = i % 36; for (j = 0; j < n; j++) { if (ara[j] != nly[(i + j) % 36]) { flg = 1; break; } if (j == n - 1) { cout << YES ; return 0; } } } cout << NO ; }
#include <bits/stdc++.h> using namespace std; struct Query { int p, s, id; bool operator<(const Query &b) const { return (s < b.s); } }; int main() { int n; scanf( %d , &n); vector<int> a(n); for (int i = 0; i < n; ++i) scanf( %d , &a[i]); int m; scanf( %d , &m); vector<Query> q(m); for (int i = 0; i < m; ++i) { scanf( %d%d , &q[i].p, &q[i].s); --q[i].p; q[i].id = i; } sort(q.begin(), q.end()); vector<long long> ans(m, 0); int lim = sqrt(n); for (int i = 0; i < q.size(); ++i) { if (q[i].s > lim) { for (int j = q[i].p; j < n; j += q[i].s) ans[q[i].id] += a[j]; } else { int cs = q[i].s; vector<long long> sums(n, 0); for (int j = n - 1; j >= 0; --j) sums[j] = a[j] + ((j + cs < n) ? sums[j + cs] : 0); int j; for (j = i; j < q.size() && q[j].s == cs; ++j) ans[q[j].id] = sums[q[j].p]; i = j - 1; } } for (int i = 0; i < ans.size(); ++i) printf( %I64d n , ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const int mod = 1e9 + 7; const int N = 4e5 + 10; const int inf = 1e9; int n, k, m; map<pair<int, int>, int> vis; int mx[N << 2], tag[N << 2]; int a[N]; void pd(int p, int k) { mx[p << 1] += k; tag[p << 1] += k; mx[p << 1 | 1] += k; tag[p << 1 | 1] += k; tag[p] = 0; } void up(int dl, int dr, int l, int r, int p, int k) { if (l == dl && r == dr) { mx[p] += k; tag[p] += k; return; } int mid = l + r >> 1; pd(p, tag[p]); if (dr <= mid) up(dl, dr, l, mid, p << 1, k); else if (dl > mid) up(dl, dr, mid + 1, r, p << 1 | 1, k); else up(dl, mid, l, mid, p << 1, k), up(mid + 1, dr, mid + 1, r, p << 1 | 1, k); mx[p] = max(mx[p << 1], mx[p << 1 | 1]); } int main() { ios::sync_with_stdio(false); cin >> n >> k >> m; for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; int p = abs(x - k) + y; if (vis[make_pair(x, y)]) { up(1, p, 1, 2 * n, 1, -1); a[p]--; if (a[p] == 0) up(p, p, 1, 2 * n, 1, -p + 1); vis[make_pair(x, y)] = 0; } else { up(1, p, 1, 2 * n, 1, 1); if (a[p] == 0) up(p, p, 1, 2 * n, 1, p - 1); ++a[p]; vis[make_pair(x, y)] = 1; } cout << max(0, mx[1] - n) << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const long long linf = (long long)1e18; const double eps = (double)1e-8; const int mod = (int)1e9 + 7; const int maxn = (int)1e5 + 5; const int MX = (int)1e5; const int PRIME = 31; string s, t; int n, m, first; long long hs[maxn], ht[maxn], st[maxn]; int d[maxn][33]; int subs(int l, int r) { return 1ll * (hs[r] - hs[l - 1] + mod) % mod * st[max(n, m) - r] % mod; } int subt(int l, int r) { return 1ll * (ht[r] - ht[l - 1] + mod) % mod * st[max(n, m) - r] % mod; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; cin >> s; s = _ + s; cin >> m; cin >> t; t = _ + t; cin >> first; st[0] = 1; for (int i = (1); i <= (int)(MX); ++i) { st[i] = 1ll * st[i - 1] * PRIME % mod; } hs[0] = 0; for (int i = (1); i <= (int)(n); ++i) { hs[i] = (hs[i - 1] + st[i - 1] * (s[i] - a + 1)) % mod; } ht[0] = 0; for (int i = (1); i <= (int)(m); ++i) { ht[i] = (ht[i - 1] + st[i - 1] * (t[i] - a + 1)) % mod; } memset((d), 0, sizeof(d)); for (int i = (0); i <= (int)(n - 1); ++i) { for (int j = (0); j <= (int)(first); ++j) { d[i + 1][j] = max(d[i + 1][j], d[i][j]); if (j == first) continue; int l = 0; int r = min(n - i, m - d[i][j]); while (l < r) { int mid = (l + r + 1) / 2; if (subs(i + 1, i + mid) == subt(d[i][j] + 1, d[i][j] + mid)) l = mid; else r = mid - 1; } d[i + l][j + 1] = max(d[i + l][j + 1], d[i][j] + l); } } for (int i = (1); i <= (int)(first); ++i) { if (d[n][i] == m) { cout << YES << n ; return 0; } } cout << NO << n ; return 0; }
#include <bits/stdc++.h> using namespace std; long long int a[123456], b[123456]; vector<long long int> v1, v2, v3; map<long long int, long long int> mp; map<string, long long int> mps; map<char, long long int> mpc; int main() { long long int f = 0, x = 0, l = 0, r = 0, n, i; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i]; } sort(b, b + i); for (i = 0; i < n; i++) { if (a[i] != b[i]) { l = i + 1; break; } } for (i = n - 1; i >= 0; i--) { if (a[i] != b[i]) { r = i + 1; break; } } if (l == 0 && r == 0) { l = 1; r = 1; cout << yes << endl; cout << l << << r << endl; return 0; } long long int k = r - 1; for (i = l - 1; i <= r - 1; i++) { long long int aa = i; long long int bb = k; if (mp[bb] == 0 && mp[aa] == 0) { swap(a[i], a[k]); mp[aa] = 1; mp[bb] = 1; k--; } } for (i = 0; i < n; i++) { if (a[i] != b[i]) { cout << no << endl; return 0; } } cout << yes << endl; cout << l << << r << endl; }
#include <bits/stdc++.h> using namespace std; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); int main() { ios::sync_with_stdio(0); cin.tie(0); map<string, vector<int> > mda; int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { string s; cin >> s; int cost; cin >> cost; mda[s].push_back(cost); } for (auto &c : mda) { sort(c.second.rbegin(), c.second.rend()); } long long ans = 0; long long odd = 0; for (auto &c : mda) { string t = c.first; reverse(t.begin(), t.end()); if (t < c.first) { long long best = 0; long long s = 0; for (int i = 0; i < (int)c.second.size() && i < mda[t].size(); i++) { s += c.second[i]; s += mda[t][i]; best = max(best, s); } ans += best; } else if (t == c.first) { long long s = 0; int mda = 0; long long last = 0; long long fir = -1e18; for (int i = 0; i < (int)c.second.size(); i++) { if (c.second[i] > 0) { last = c.second[i]; s += c.second[i]; mda++; } else { fir = c.second[i]; break; } } if (mda % 2 == 0) { ans += s; } else { long long me = max(s - last, s + fir); long long delta_odd = s - me; ans += me; odd = max(odd, delta_odd); } } } cout << ans + odd << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long tc, n, arr1[100005], m, arr2[100005], count = 0, even = 0, even2 = 0, odd = 0, odd2 = 0; cin >> tc; while (tc--) { cin >> n; for (int i = 0; i < n; i++) { cin >> arr1[i]; if (arr1[i] % 2 == 0) even++; else odd++; } cin >> m; for (int i = 0; i < m; i++) { cin >> arr2[i]; if (arr2[i] % 2 == 0) even2++; else odd2++; } count += even * even2; count += odd * odd2; cout << count << endl; count = 0; even = 0; even2 = 0; odd = 0; odd2 = 0; } }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; bool adj[4005][4005]; int edges[4005]; int main() { int n, m, res = INF; cin >> n >> m; while (m--) { int a, b; cin >> a >> b; adj[a][b] = adj[b][a] = 1; edges[a]++; edges[b]++; } for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) { if (adj[i][j] == 1) { for (int k = j + 1; k <= n; k++) if (adj[j][k] == 1 && adj[k][i] == 1) res = min(res, edges[i] + edges[j] + edges[k] - 6); } } if (res == INF) puts( -1 ); else cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long maxn = -1e18; long long minn = 1e18; long long mod = 1000000007; long long ans = 0; long long cur = 0; long long tong = 0, tong2 = 0; long long cnt = 0; long long k, l; string s; long long n, m, x, y; long long b[100005]; long long was[200005]; bool flag = true; vector<long long> node[200005]; long long dp[105][105]; long long a[55]; long long c[55]; map<string, int> ok; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> a[2] >> a[3] >> a[5] >> a[6]; cout << min(a[2], min(a[5], a[6])) * 256 + min(a[2] - min(a[2], min(a[5], a[6])), a[3]) * 32; }
#include <bits/stdc++.h> using namespace std; namespace scanfuck { const int L = (1 << 21) + 1; struct io { char ibuf[L], *iS, *iT, obuf[L], *oS, *oT, c, st[55]; int f, tp; void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } void putc(char x) { *oS++ = x; if (oS == oT) flush(); } template <class T> T rd() { register T x = 0; for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++); !isdigit(c); c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) if (c == - ) f = -1; for (x = 0; isdigit(c); c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) x = x * 10 + (c & 15); return x * f; } template <class T> void print(T x) { if (!x) putc( 0 ); else { if (x < 0) putc( - ), x = -x; for (tp = 0; x; st[tp++] = x % 10 + 48, x /= 10) ; for (tp--; ~tp; putc(st[tp--])) ; } } void gs(char* s, int& l) { for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++); !isalpha(c); c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) ; for (l = 0; isalpha(c); c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) s[l++] = c; } void ps(const char* s) { for (tp = 0; tp < strlen(s); ++tp) putc(s[tp]); } io() : oS(obuf), oT(obuf + L - 1) {} ~io() { flush(); } } ip; } // namespace scanfuck int n = scanfuck::ip.rd<int>(), m = scanfuck::ip.rd<int>(), k = scanfuck::ip.rd<int>(); inline bool judge() { if (!(n & 1)) return 1; int t = sqrt(m); for (register int i = 1; i <= t; ++i) if (m % i == 0 && (i >= k && m / i > 1 || i > 1 && m / i >= k)) return 0; return 1; } int main() { scanfuck::ip.ps(judge() ? Marsel : Timur ); }
#include <bits/stdc++.h> using namespace std; const int maxn = 1000006; char s[maxn]; int ans[8] = {1869, 8196, 1986, 8691, 6198, 1689, 1968}; int main() { while (scanf( %s , s) != EOF) { int len = strlen(s); int A[10] = {0}; for (int i = 0; i < len; i++) { A[s[i] - 0 ]++; } int m = 0; for (int i = 1; i < A[1]; i++) { printf( 1 ); m = (m * 10 + 1) % 7; } for (int i = 1; i <= A[2]; i++) { printf( 2 ); m = (m * 10 + 2) % 7; } for (int i = 1; i <= A[3]; i++) { printf( 3 ); m = (m * 10 + 3) % 7; } for (int i = 1; i <= A[4]; i++) { printf( 4 ); m = (m * 10 + 4) % 7; } for (int i = 1; i <= A[5]; i++) { printf( 5 ); m = (m * 10 + 5) % 7; } for (int i = 1; i < A[6]; i++) { printf( 6 ); m = (m * 10 + 6) % 7; } for (int i = 1; i <= A[7]; i++) { printf( 7 ); m = (m * 10 + 7) % 7; } for (int i = 1; i < A[8]; i++) { printf( 8 ); m = (m * 10 + 8) % 7; } for (int i = 1; i < A[9]; i++) { printf( 9 ); m = (m * 10 + 9) % 7; } m = (m * 10000) % 7; printf( %d , ans[m]); for (int i = 1; i <= A[0]; i++) printf( 0 ); printf( n ); } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> vector<T>& operator--(vector<T>& v) { for (auto& i : v) --i; return v; } template <typename T> vector<T>& operator++(vector<T>& v) { for (auto& i : v) ++i; return v; } template <typename T> istream& operator>>(istream& is, vector<T>& v) { for (auto& i : v) is >> i; return is; } template <typename T> ostream& operator<<(ostream& os, vector<T>& v) { for (auto& i : v) os << i << ; return os; } template <typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template <typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p) { os << p.first << << p.second; return os; } template <typename T, typename U> pair<T, U> operator-(pair<T, U> a, pair<T, U> b) { return make_pair(a.first - b.first, a.second - b.second); } template <typename T, typename U> pair<T, U> operator+(pair<T, U> a, pair<T, U> b) { return make_pair(a.first + b.first, a.second + b.second); } template <typename T, typename U> void umin(T& a, U b) { if (a > b) a = b; } template <typename T, typename U> void umax(T& a, U b) { if (a < b) a = b; } const int K = 15; array<array<bool, 1 << K>, K> dp; int main() { int k; cin >> k; vector<vector<int>> v(k); map<int, int> where; vector<long long> sms(k, 0); long long need = 0; for (int i = 0; i < k; ++i) { int n; cin >> n; v[i].resize(n); cin >> v[i]; for (auto u : v[i]) { where[u] = i; sms[i] += u; need += u; } } if (need % k != 0) { cout << No n ; return 0; } need /= k; vector<map<int, pair<int, int>>> from(k); for (int i = 0; i < k; ++i) { for (int j = 0; j < v[i].size(); ++j) { int next = need - (sms[i] - v[i][j]); if (where.count(next)) { from[i][v[i][j]] = make_pair(next, where[next]); } else { from[i][v[i][j]] = make_pair(0, -1); } } } set<int> used; vector<pair<int, int>> sth; for (int i = 0; i < k; ++i) { for (int j = 0; j < v[i].size(); ++j) { int start = v[i][j]; if (used.count(start)) continue; int start_i = i; int now = start; int visited = 1 << i; bool ok = true; vector<int> was = {start}; while (true) { auto [next, next_i] = from[start_i][start]; if (next_i == -1) { ok = false; break; } if (visited & (1 << next_i)) { if (next != now) { ok = false; } break; } visited |= (1 << next_i); start = next; start_i = next_i; if (used.count(start)) { ok = false; break; } was.push_back(start); } if (ok) { for (auto y : was) { used.insert(y); } sth.emplace_back(visited, now); } } } dp[0].fill(false); for (auto [mask, s] : sth) { dp[0][mask] = true; } dp[0][0] = true; for (int i = 1; i < k; ++i) { for (int j = 0; j < (1 << k); ++j) { int mask = j; int submask = mask; if (j == 0) { dp[i][mask] = true; } else { dp[i][mask] = false; } while (submask != 0) { if (dp[i - 1][submask] && dp[i - 1][mask ^ submask]) { dp[i][mask] = true; break; } submask = (submask - 1) & mask; } } } if (!dp[k - 1][(1 << k) - 1]) { cout << No n ; return 0; } cout << Yes n ; vector<int> masks = {(1 << k) - 1}; for (int i = k - 1; i > 0; --i) { vector<int> new_masks; for (auto mask : masks) { int submask = mask; while (submask != 0) { if (dp[i - 1][submask] && dp[i - 1][mask ^ submask]) { if (submask) new_masks.push_back(submask); if (mask ^ submask) new_masks.push_back(mask ^ submask); break; } submask = (submask - 1) & mask; } } swap(new_masks, masks); } sort(sth.begin(), sth.end()); 42; ; vector<int> starts; vector<pair<pair<int, int>, int>> ans; for (auto mask : masks) { auto it = lower_bound(sth.begin(), sth.end(), make_pair(mask, (int)(-1e9 - 10))); assert(it->first == mask); int start = it->second; int now = start; int now_i = where[start]; int visited = 1 << now_i; vector<pair<int, int>> path; path.emplace_back(now_i, now); while (true) { auto [next, next_i] = from[now_i][now]; if (visited & (1 << next_i)) { break; } now = next; now_i = next_i; path.emplace_back(now_i, now); visited |= 1 << next_i; } for (int i = 0; i < path.size(); ++i) { ans.emplace_back(path[i], path[(i - 1 + path.size()) % path.size()].first); } } sort(ans.begin(), ans.end()); for (auto [p, y] : ans) { cout << p.second << << y + 1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double inf = 1e20; const double pi = acos(-1.0); const int maxp = 10001; int dcmp(double d) { if (fabs(d) < eps) return 0; return d > eps ? 1 : -1; } inline double sqr(double x) { return x * x; } struct point { double x, y; point() {} point(double _x, double _y) : x(_x), y(_y){}; void input() { scanf( %lf%lf , &x, &y); } void output() { printf( %.2f %.2f n , x, y); } bool operator==(point a) const { return dcmp(a.x - x) == 0 && dcmp(a.y - y) == 0; } bool operator<(point a) const { return dcmp(a.x - x) == 0 ? dcmp(y - a.y) < 0 : x < a.x; } double len() { return hypot(x, y); } double len2() { return x * x + y * y; } double distance(point p) { return hypot(x - p.x, y - p.y); } point add(point p) { return point(x + p.x, y + p.y); } point sub(point p) { return point(x - p.x, y - p.y); } point mul(double b) { return point(x * b, y * b); } point div(double b) { return point(x / b, y / b); } double dot(point p) { return x * p.x + y * p.y; } double det(point p) { return x * p.y - y * p.x; } double rad(point a, point b) { point p = *this; return fabs(atan2(fabs(a.sub(p).det(b.sub(p))), a.sub(p).dot(b.sub(p)))); } point trunc(double r) { double l = len(); if (!dcmp(l)) return *this; r /= l; return point(x * r, y * r); } point rotleft() { return point(-y, x); } point rotright() { return point(y, -x); } point rotate(point p, double angle) { point v = this->sub(p); double c = cos(angle), s = sin(angle); return point(p.x + v.x * c - v.y * s, p.y + v.x * s + v.y * c); } }; struct line { point a, b; line() {} line(point _a, point _b) { a = _a; b = _b; } bool operator==(line v) { return (a == v.a) && (b == v.b); } line(point p, double angle) { a = p; if (dcmp(angle - pi / 2) == 0) { b = a.add(point(0, 1)); } else { b = a.add(point(1, tan(angle))); } } line(double _a, double _b, double _c) { if (dcmp(_a) == 0) { a = point(0, -_c / _b); b = point(1, -_c / _b); } else if (dcmp(_b) == 0) { a = point(-_c / _a, 0); b = point(-_c / _a, 1); } else { a = point(0, -_c / _b); b = point(1, (-_c - _a) / _b); } } void input() { a.input(); b.input(); } void adjust() { if (b < a) swap(a, b); } double length() { return a.distance(b); } double angle() { double k = atan2(b.y - a.y, b.x - a.x); if (dcmp(k) < 0) k += pi; if (dcmp(k - pi) == 0) k -= pi; return k; } int relation(point p) { int c = dcmp(p.sub(a).det(b.sub(a))); if (c < 0) return 1; if (c > 0) return 2; return 3; } bool pointonseg(point p) { return dcmp(p.sub(a).det(b.sub(a))) == 0 && dcmp(p.sub(a).dot(p.sub(b))) <= 0; } bool parallel(line v) { return dcmp(b.sub(a).det(v.b.sub(v.a))) == 0; } int segcrossseg(line v) { int d1 = dcmp(b.sub(a).det(v.a.sub(a))); int d2 = dcmp(b.sub(a).det(v.b.sub(a))); int d3 = dcmp(v.b.sub(v.a).det(a.sub(v.a))); int d4 = dcmp(v.b.sub(v.a).det(b.sub(v.a))); if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2) return 2; return ((d1 == 0 && dcmp(v.a.sub(a).dot(v.a.sub(b))) <= 0) || (d2 == 0 && dcmp(v.b.sub(a).dot(v.b.sub(b))) <= 0) || (d3 == 0 && dcmp(a.sub(v.a).dot(a.sub(v.b))) <= 0) || (d4 == 0 && dcmp(b.sub(v.a).dot(b.sub(v.b))) <= 0)); } int linecrossseg(line v) { int d1 = dcmp(b.sub(a).det(v.a.sub(a))); int d2 = dcmp(b.sub(a).det(v.b.sub(a))); if ((d1 ^ d2) == -2) return 2; return (d1 == 0 || d2 == 0); } int linecrossline(line v) { if ((*this).parallel(v)) return v.relation(a) == 3; return 2; } point crosspoint(line v) { double a1 = v.b.sub(v.a).det(a.sub(v.a)); double a2 = v.b.sub(v.a).det(b.sub(v.a)); return point((a.x * a2 - b.x * a1) / (a2 - a1), (a.y * a2 - b.y * a1) / (a2 - a1)); } double dispointtoline(point p) { return fabs(p.sub(a).det(b.sub(a))) / length(); } double dispointtoseg(point p) { if (dcmp(p.sub(b).dot(a.sub(b))) < 0 || dcmp(p.sub(a).dot(b.sub(a))) < 0) return min(p.distance(a), p.distance(b)); return dispointtoline(p); } point lineprog(point p) { return a.add(b.sub(a).mul(b.sub(a).dot(p.sub(a)) / b.sub(a).len2())); } point symmetrypoint(point p) { point q = lineprog(p); return point(2 * q.x - p.x, 2 * q.y - p.y); } }; struct circle { point p; double r; circle() {} circle(point _p, double _r) : p(_p), r(_r){}; circle(double x, double y, double _r) : p(point(x, y)), r(_r){}; circle(point a, point b, point c) { p = line(a.add(b).div(2), a.add(b).div(2).add(b.sub(a).rotleft())) .crosspoint( line(c.add(b).div(2), c.add(b).div(2).add(b.sub(c).rotleft()))); r = p.distance(a); } circle(point a, point b, point c, bool t) { line u, v; double m = atan2(b.y - a.y, b.x - a.x), n = atan2(c.y - a.y, c.x - a.x); u.a = a; u.b = u.a.add(point(cos((n + m) / 2), sin((n + m) / 2))); v.a = b; m = atan2(a.y - b.y, a.x - b.x), n = atan2(c.y - b.y, c.x - b.x); v.b = v.a.add(point(cos((n + m) / 2), sin((n + m) / 2))); p = u.crosspoint(v); r = line(a, b).dispointtoseg(p); } void input() { p.input(); scanf( %lf , &r); } void output() { printf( %.2lf %.2lf %.2lf n , p.x, p.y, r); } bool operator==(circle v) { return ((p == v.p) && dcmp(r - v.r) == 0); } bool operator<(circle v) const { return ((p < v.p) || ((p == v.p) && dcmp(r - v.r) < 0)); } double area() { return pi * sqr(r); } double circumference() { return 2 * pi * r; } int relation(point b) { double dst = b.distance(p); if (dcmp(dst - r) < 0) return 2; if (dcmp(dst - r) == 0) return 1; return 0; } int relationseg(line v) { double dst = v.dispointtoseg(p); if (dcmp(dst - r) < 0) return 2; if (dcmp(dst - r) == 0) return 1; return 0; } int relationline(line v) { double dst = v.dispointtoline(p); if (dcmp(dst - r) < 0) return 2; if (dcmp(dst - r) == 0) return 1; return 0; } int getcircle(point a, point b, double r, circle &c1, circle &c2) { circle x(a, r), y(b, r); int t = x.pointcrosscircle(y, c1.p, c2.p); if (!t) return 0; c1.r = c2.r = r; return t; } int getcircle(line u, point q, double r1, circle &c1, circle &c2) { double dis = u.dispointtoline(q); if (dcmp(dis - r1 * 2) > 0) return 0; if (dcmp(dis) == 0) { c1.p = q.add(u.b.sub(u.a).rotleft().trunc(r1)); c2.p = q.add(u.b.sub(u.a).rotright().trunc(r1)); c1.r = c2.r = r1; return 2; } line u1 = line(u.a.add(u.b.sub(u.a).rotleft().trunc(r1)), u.b.add(u.b.sub(u.a).rotleft().trunc(r1))); line u2 = line(u.a.add(u.b.sub(u.a).rotright().trunc(r1)), u.b.add(u.b.sub(u.a).rotright().trunc(r1))); circle cc = circle(q, r1); point p1, p2; if (!cc.pointcrossline(u1, p1, p2)) cc.pointcrossline(u2, p1, p2); c1 = circle(p1, r1); if (p1 == p2) { c2 = c1; return 1; } c2 = circle(p2, r1); return 2; } int getcircle(line u, line v, double r1, circle &c1, circle &c2, circle &c3, circle &c4) { if (u.parallel(v)) return 0; line u1 = line(u.a.add(u.b.sub(u.a).rotleft().trunc(r1)), u.b.add(u.b.sub(u.a).rotleft().trunc(r1))); line u2 = line(u.a.add(u.b.sub(u.a).rotright().trunc(r1)), u.b.add(u.b.sub(u.a).rotright().trunc(r1))); line v1 = line(v.a.add(v.b.sub(v.a).rotleft().trunc(r1)), v.b.add(v.b.sub(v.a).rotleft().trunc(r1))); line v2 = line(v.a.add(v.b.sub(v.a).rotright().trunc(r1)), v.b.add(v.b.sub(v.a).rotright().trunc(r1))); c1.r = c2.r = c3.r = c4.r = r1; c1.p = u1.crosspoint(v1); c2.p = u1.crosspoint(v2); c3.p = u2.crosspoint(v1); c4.p = u2.crosspoint(v2); return 4; } int getcircle(circle cx, circle cy, double r1, circle &c1, circle &c2) { circle x(cx.p, r1 + cx.r), y(cy.p, r1 + cy.r); int t = x.pointcrosscircle(y, c1.p, c2.p); if (!t) return 0; c1.r = c2.r = r1; return t; } int pointcrossline(line v, point &p1, point &p2) { if (!(*this).relationline(v)) return 0; point a = v.lineprog(p); double d = v.dispointtoline(p); d = sqrt(r * r - d * d); if (dcmp(d) == 0) { p1 = a; p2 = a; return 1; } p1 = a.sub(v.b.sub(v.a).trunc(d)); p2 = a.add(v.b.sub(v.a).trunc(d)); return 2; } int relationcircle(circle v) { double d = p.distance(v.p); if (dcmp(d - r - v.r) > 0) return 5; if (dcmp(d - r - v.r) == 0) return 4; double l = fabs(r - v.r); if (dcmp(d - r - v.r) < 0 && dcmp(d - l) > 0) return 3; if (dcmp(d - l) == 0) return 2; if (dcmp(d - l) < 0) return 1; } int pointcrosscircle(circle v, point &p1, point &p2) { int rel = relationcircle(v); if (rel == 1 || rel == 5) return 0; double d = p.distance(v.p); double l = (d + (sqr(r) - sqr(v.r)) / d) / 2; double h = sqrt(sqr(r) - sqr(l)); p1 = p.add(v.p.sub(p).trunc(l).add(v.p.sub(p).rotleft().trunc(h))); p2 = p.add(v.p.sub(p).trunc(l).add(v.p.sub(p).rotright().trunc(h))); if (rel == 2 || rel == 4) return 1; return 2; } int tangentline(point q, line &u, line &v) { int x = relation(q); if (x == 2) return 0; if (x == 1) { u = line(q, q.add(q.sub(p).rotleft())); v = u; return 1; } double d = p.distance(q); double l = sqr(r) / d; double h = sqrt(sqr(r) - sqr(l)); u = line(q, p.add(q.sub(p).trunc(l).add(q.sub(p).rotleft().trunc(h)))); v = line(q, p.add(q.sub(p).trunc(l).add(q.sub(p).rotright().trunc(h)))); return 2; } double areacircle(circle v) { int rel = relationcircle(v); if (rel >= 4) return 0.0; if (rel <= 2) return min(area(), v.area()); double d = p.distance(v.p); double hf = (r + v.r + d) / 2.0; double ss = 2 * sqrt(hf * (hf - r) * (hf - v.r) * (hf - d)); double a1 = acos((r * r + d * d - v.r * v.r) / (2.0 * r * d)); a1 = a1 * r * r; double a2 = acos((v.r * v.r + d * d - r * r) / (2.0 * v.r * d)); a2 = a2 * v.r * v.r; return a1 + a2 - ss; } double areatriangle(point a, point b) { if (dcmp(p.sub(a).det(p.sub(b)) == 0)) return 0.0; point q[5]; int len = 0; q[len++] = a; line l(a, b); point p1, p2; if (pointcrossline(l, q[1], q[2]) == 2) { if (dcmp(a.sub(q[1]).dot(b.sub(q[1]))) < 0) q[len++] = q[1]; if (dcmp(a.sub(q[2]).dot(b.sub(q[2]))) < 0) q[len++] = q[2]; } q[len++] = b; if (len == 4 && (dcmp(q[0].sub(q[1]).dot(q[2].sub(q[1]))) > 0)) swap(q[1], q[2]); double res = 0; int i; for (i = 0; i < len - 1; i++) { if (relation(q[i]) == 0 || relation(q[i + 1]) == 0) { double arg = p.rad(q[i], q[i + 1]); res += r * r * arg / 2.0; } else res += fabs(q[i].sub(p).det(q[i + 1].sub(p)) / 2.0); } return res; } }; struct polygon { int n; point p[maxp]; line l[maxp]; void input() { for (int i = 0; i < n; i++) p[i].input(); } void add(point q) { p[n++] = q; } void getline() { for (int i = 0; i < n; i++) l[i] = line(p[i], p[(i + 1) % n]); } struct cmp { point p; cmp(const point &p0) { p = p0; } bool operator()(const point &aa, const point &bb) { point a = aa, b = bb; int d = dcmp(a.sub(p).det(b.sub(p))); if (d == 0) return dcmp(a.distance(p) - b.distance(p)) < 0; return d > 0; } }; void norm() { point mi = p[0]; for (int i = 1; i < n; i++) mi = min(mi, p[i]); sort(p, p + n, cmp(mi)); } void getconvex(polygon &convex) { int i; sort(p, p + n); convex.n = n; for (i = 0; i < min(n, 2); i++) convex.p[i] = p[i]; if (n <= 2) return; int &top = convex.n; top = 1; for (i = 2; i < n; i++) { while (top && convex.p[top].sub(p[i]).det(convex.p[top - 1].sub(p[i])) <= 0) top--; convex.p[++top] = p[i]; } int temp = top; convex.p[++top] = p[n - 2]; for (i = n - 3; i >= 0; i--) { while (top != temp && convex.p[top].sub(p[i]).det(convex.p[top - 1].sub(p[i])) <= 0) top--; convex.p[++top] = p[i]; } } bool isconvex() { bool s[3]; memset(s, 0, sizeof(s)); int i, j, k; for (i = 0; i < n; i++) { j = (i + 1) % n; k = (j + 1) % n; s[dcmp(p[j].sub(p[i]).det(p[k].sub(p[i]))) + 1] = 1; if (s[0] && s[2]) return 0; } return 1; } int relationpoint(point q) { int i, j; for (i = 0; i < n; i++) if (p[i] == q) return 3; getline(); for (i = 0; i < n; i++) if (l[i].pointonseg(q)) return 2; int cnt = 0; for (i = 0; i < n; i++) { j = (i + 1) % n; int k = dcmp(q.sub(p[j]).det(p[i].sub(p[j]))); int u = dcmp(p[i].y - q.y); int v = dcmp(p[j].y - q.y); if (k > 0 && u < 0 && v >= 0) cnt++; if (k < 0 && v < 0 && u >= 0) cnt--; } return cnt != 0; } int relationline(line u) { int i, k = 0; getline(); for (i = 0; i < n; i++) { if (l[i].segcrossseg(u) == 2) return 1; if (l[i].segcrossseg(u) == 1) k = 1; } if (!k) return 0; vector<point> vp; for (i = 0; i < n; i++) { if (l[i].segcrossseg(u)) { if (l[i].parallel(u)) { vp.push_back(u.a); vp.push_back(u.b); vp.push_back(l[i].a); vp.push_back(l[i].b); continue; } vp.push_back(l[i].crosspoint(u)); } } sort(vp.begin(), vp.end()); int sz = vp.size(); for (i = 0; i < sz - 1; i++) { point mid = vp[i].add(vp[i + 1]).div(2); if (relationpoint(mid) == 1) return 1; } return 2; } void convexcut(line u, polygon &po) { int i; int &top = po.n; top = 0; for (i = 0; i < n; i++) { int d1 = dcmp(p[i].sub(u.a).det(u.b.sub(u.a))); int d2 = dcmp(p[(i + 1) % n].sub(u.a).det(u.b.sub(u.a))); if (d1 >= 0) po.p[top++] = p[i]; if (d1 * d2 < 0) po.p[top++] = u.crosspoint(line(p[i], p[(i + 1) % n])); } } double getcircumference() { double sum = 0; int i; for (i = 0; i < n; i++) sum += p[i].distance(p[(i + 1) % n]); return sum; } double getarea() { double sum = 0; for (int i = 0; i < n; i++) sum += p[i].det(p[(i + 1) % n]); return fabs(sum) / 2; } bool getdir() { double sum = 0; int i; for (i = 0; i < n; i++) sum += p[i].det(p[(i + 1) % n]); if (dcmp(sum) > 0) return 1; return 0; } point getbarycentre() { point ret(0, 0); double area = 0; int i; for (i = 1; i < n - 1; i++) { double tmp = p[i].sub(p[0]).det(p[i + 1].sub(p[0])); if (dcmp(tmp) == 0) continue; area += tmp; ret.x += (p[0].x + p[i].x + p[i + 1].x) / 3 * tmp; ret.y += (p[0].y + p[i].y + p[i + 1].y) / 3 * tmp; } if (dcmp(area)) ret = ret.div(area); return ret; } double areacircle(circle c) { int i; double ans = 0; for (i = 0; i < n; i++) { int j = (i + 1) % n; if (dcmp(p[j].sub(c.p).det(p[i].sub(c.p))) >= 0) ans += c.areatriangle(p[i], p[j]); else ans -= c.areatriangle(p[i], p[j]); } return fabs(ans); } int relationcircle(circle c) { getline(); int i, x = 2; if (relationpoint(c.p) != 1) return 0; for (i = 0; i < n; i++) { if (c.relationseg(l[i]) == 2) return 0; if (c.relationseg(l[i]) == 1) x = 1; } return x; } void find(int st, point tri[], circle &c) { if (!st) c = circle(point(0, 0), -2); if (st == 1) c = circle(tri[0], 0); if (st == 2) c = circle(tri[0].add(tri[1]).div(2), tri[0].distance(tri[1]) / 2.0); if (st == 3) c = circle(tri[0], tri[1], tri[2]); } void solve(int cur, int st, point tri[], circle &c) { find(st, tri, c); if (st == 3) return; int i; for (i = 0; i < cur; i++) if (dcmp(p[i].distance(c.p) - c.r) > 0) { tri[st] = p[i]; solve(i, st + 1, tri, c); } } circle mincircle() { random_shuffle(p, p + n); point tri[4]; circle c; solve(n, 0, tri, c); return c; } int circlecover(double r) { int ans = 0, i, j; vector<pair<double, int> > v; for (i = 0; i < n; i++) { v.clear(); for (j = 0; j < n; j++) if (i != j) { point q = p[i].sub(p[j]); double d = q.len(); if (dcmp(d - 2 * r) <= 0) { double arg = atan2(q.y, q.x); if (dcmp(arg) < 0) arg += 2 * pi; double t = acos(d / (2 * r)); v.push_back(make_pair(arg - t + 2 * pi, -1)); v.push_back(make_pair(arg + t + 2 * pi, 1)); } } sort(v.begin(), v.end()); int cur = 0; for (j = 0; j < int(v.size()); j++) { if (v[j].second == -1) ++cur; else --cur; ans = max(ans, cur); } } return ans + 1; } int pointinpolygon(point q) { if (getdir()) reverse(p, p + n); if (dcmp(q.sub(p[0]).det(p[n - 1].sub(p[0]))) == 0) { if (line(p[n - 1], p[0]).pointonseg(q)) return n - 1; return -1; } int low = 1, high = n - 2, mid; while (low <= high) { mid = (low + high) >> 1; if (dcmp(q.sub(p[0]).det(p[mid].sub(p[0]))) >= 0 && dcmp(q.sub(p[0]).det(p[mid + 1].sub(p[0]))) < 0) { polygon c; c.p[0] = p[mid]; c.p[1] = p[mid + 1]; c.p[2] = p[0]; c.n = 3; if (c.relationpoint(q)) return mid; return -1; } if (dcmp(q.sub(p[0]).det(p[mid].sub(p[0]))) > 0) low = mid + 1; else high = mid - 1; } return -1; } }; struct polygons { vector<polygon> p; polygons() { p.clear(); } void clear() { p.clear(); } void push(polygon q) { if (dcmp(q.getarea())) p.push_back(q); } vector<pair<double, int> > e; void ins(point s, point t, point X, int i) { double r = fabs(t.x - s.x) > eps ? (X.x - s.x) / (t.x - s.x) : (X.y - s.y) / (t.y - s.y); r = min(r, 1.0); r = max(r, 0.0); e.push_back(make_pair(r, i)); } double polyareaunion() { double ans = 0.0; int c0, c1, c2, i, j, k, w; for (i = 0; i < int(p.size()); i++) if (p[i].getdir() == 0) reverse(p[i].p, p[i].p + p[i].n); for (i = 0; i < int(p.size()); i++) { for (k = 0; k < p[i].n; k++) { point &s = p[i].p[k], &t = p[i].p[(k + 1) % p[i].n]; if (!dcmp(s.det(t))) continue; e.clear(); e.push_back(make_pair(0.0, 1)); e.push_back(make_pair(1.0, -1)); for (j = 0; j < int(p.size()); j++) if (i != j) { for (w = 0; w < p[j].n; w++) { point a = p[j].p[w], b = p[j].p[(w + 1) % p[j].n], c = p[j].p[(w - 1 + p[j].n) % p[j].n]; c0 = dcmp(t.sub(s).det(c.sub(s))); c1 = dcmp(t.sub(s).det(a.sub(s))); c2 = dcmp(t.sub(s).det(b.sub(s))); if (c1 * c2 < 0) ins(s, t, line(s, t).crosspoint(line(a, b)), -c2); else if (!c1 && c0 * c2 < 0) ins(s, t, a, -c2); else if (!c1 && !c2) { int c3 = dcmp(t.sub(s).det(p[j].p[(w + 2) % p[j].n].sub(s))); int dp = dcmp(t.sub(s).dot(b.sub(a))); if (dp && c0) ins(s, t, a, dp > 0 ? c0 * ((j > i) ^ (c0 < 0)) : -(c0 < 0)); if (dp && c3) ins(s, t, b, dp > 0 ? -c3 * ((j > i) ^ (c3 < 0)) : c3 < 0); } } } sort(e.begin(), e.end()); int ct = 0; double tot = 0.0, last; for (j = 0; j < int(e.size()); j++) { if (ct == 2) tot += e[j].first - last; ct += e[j].second; last = e[j].first; } ans += s.det(t) * tot; } } return fabs(ans) * 0.5; } }; const int maxn = 500; struct circles { circle c[maxn]; double ans[maxn]; double pre[maxn]; int n; circles() {} void add(circle cc) { c[n++] = cc; } bool inner(circle x, circle y) { if (x.relationcircle(y) != 1) return 0; return dcmp(x.r - y.r) <= 0 ? 1 : 0; } void init_or() { int i, j, k = 0; bool mark[maxn] = {0}; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) if (i != j && !mark[j]) if ((c[i] == c[j]) || inner(c[i], c[j])) break; if (j < n) mark[i] = 1; } for (i = 0; i < n; i++) if (!mark[i]) c[k++] = c[i]; n = k; } void init_and() { int i, j, k = 0; bool mark[maxn] = {0}; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) if (i != j && !mark[j]) if ((c[i] == c[j]) || inner(c[j], c[i])) break; if (j < n) mark[i] = 1; } for (i = 0; i < n; i++) if (!mark[i]) c[k++] = c[i]; n = k; } double areaarc(double th, double r) { return 0.5 * sqr(r) * (th - sin(th)); } void getarea() { int i, j; memset(ans, 0, sizeof(ans)); vector<pair<double, int> > v; for (i = 0; i < n; i++) { v.clear(); v.push_back(make_pair(-pi, 1)); v.push_back(make_pair(pi, -1)); for (j = 0; j < n; j++) if (i != j) { point q = c[j].p.sub(c[i].p); double ab = q.len(), ac = c[i].r, bc = c[j].r; if (dcmp(ab + ac - bc) <= 0) { v.push_back(make_pair(-pi, 1)); v.push_back(make_pair(pi, -1)); continue; } if (dcmp(ab + bc - ac) <= 0) continue; if (dcmp(ab - ac - bc) > 0) continue; double th = atan2(q.y, q.x), fai = acos((ac * ac + ab * ab - bc * bc) / (2.0 * ac * ab)); double a0 = th - fai; if (dcmp(a0 + pi) < 0) a0 += 2 * pi; double a1 = th + fai; if (dcmp(a1 - pi) > 0) a1 -= 2 * pi; if (dcmp(a0 - a1) > 0) { v.push_back(make_pair(a0, 1)); v.push_back(make_pair(pi, -1)); v.push_back(make_pair(-pi, 1)); v.push_back(make_pair(a1, -1)); } else { v.push_back(make_pair(a0, 1)); v.push_back(make_pair(a1, -1)); } } sort(v.begin(), v.end()); int cur = 0; for (j = 0; j < int(v.size()); j++) { if (cur && dcmp(v[j].first - pre[cur])) { ans[cur] += areaarc(v[j].first - pre[cur], c[i].r); ans[cur] += 0.5 * point(c[i].p.x + c[i].r * cos(pre[cur]), c[i].p.y + c[i].r * sin(pre[cur])) .det(point(c[i].p.x + c[i].r * cos(v[j].first), c[i].p.y + c[i].r * sin(v[j].first))); } cur += v[j].second; pre[cur] = v[j].first; } } for (i = 1; i <= n; i++) { ans[i] -= ans[i + 1]; } } }; struct halfplane : public line { double angle; halfplane() {} halfplane(point _a, point _b) { a = _a; b = _b; } halfplane(line v) { a = v.a; b = v.b; } void calcangle() { angle = atan2(b.y - a.y, b.x - a.x); } bool operator<(const halfplane &b) const { return angle < b.angle; } }; struct halfplanes { int n; halfplane hp[maxp]; point p[maxp]; int que[maxp]; int st, ed; void push(halfplane tmp) { hp[n++] = tmp; } void unique() { int m = 1, i; for (i = 1; i < n; i++) { if (dcmp(hp[i].angle - hp[i - 1].angle)) hp[m++] = hp[i]; else if (dcmp(hp[m - 1].b.sub(hp[m - 1].a).det(hp[i].a.sub(hp[m - 1].a)) > 0)) hp[m - 1] = hp[i]; } n = m; } bool halfplaneinsert() { int i; for (i = 0; i < n; i++) hp[i].calcangle(); sort(hp, hp + n); unique(); que[st = 0] = 0; que[ed = 1] = 1; p[1] = hp[0].crosspoint(hp[1]); for (i = 2; i < n; i++) { while (st < ed && dcmp((hp[i].b.sub(hp[i].a).det(p[ed].sub(hp[i].a)))) < 0) ed--; while (st < ed && dcmp((hp[i].b.sub(hp[i].a).det(p[st + 1].sub(hp[i].a)))) < 0) st++; que[++ed] = i; if (hp[i].parallel(hp[que[ed - 1]])) return false; p[ed] = hp[i].crosspoint(hp[que[ed - 1]]); } while ( st < ed && dcmp(hp[que[st]].b.sub(hp[que[st]].a).det(p[ed].sub(hp[que[st]].a))) < 0) ed--; while (st < ed && dcmp(hp[que[ed]] .b.sub(hp[que[ed]].a) .det(p[st + 1].sub(hp[que[ed]].a))) < 0) st++; if (st + 1 >= ed) return false; return true; } void getconvex(polygon &con) { p[st] = hp[que[st]].crosspoint(hp[que[ed]]); con.n = ed - st + 1; int j = st, i = 0; for (; j <= ed; i++, j++) con.p[i] = p[j]; } }; struct point3 { double x, y, z; point3() {} point3(double _x, double _y, double _z) : x(_x), y(_y), z(_z){}; void input() { scanf( %lf%lf%lf , &x, &y, &z); } void output() { printf( %.2lf %.2lf %.2lf , x, y, z); } bool operator==(point3 a) { return dcmp(a.x - x) == 0 && dcmp(a.y - y) == 0 && dcmp(a.z - z) == 0; } bool operator<(point3 a) const { return dcmp(a.x - x) == 0 ? dcmp(y - a.y) == 0 ? dcmp(z - a.z) < 0 : y < a.y : x < a.x; } double len() { return sqrt(len2()); } double len2() { return x * x + y * y + z * z; } double distance(point3 p) { return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y) + (p.z - z) * (p.z - z)); } point3 add(point3 p) { return point3(x + p.x, y + p.y, z + p.z); } point3 sub(point3 p) { return point3(x - p.x, y - p.y, z - p.z); } point3 mul(double d) { return point3(x * d, y * d, z * d); } point3 div(double d) { return point3(x / d, y / d, z / d); } double dot(point3 p) { return x * p.x + y * p.y + z * p.z; } point3 det(point3 p) { return point3(y * p.z - p.y * z, p.x * z - x * p.z, x * p.y - p.x * y); } double rad(point3 a, point3 b) { point3 p = (*this); return acos(a.sub(p).dot(b.sub(p)) / (a.distance(p) * b.distance(p))); } point3 trunc(double r) { r /= len(); return point3(x * r, y * r, z * r); } }; struct line3 { point3 a, b; line3() {} line3(point3 _a, point3 _b) { a = _a; b = _b; } bool operator==(line3 v) { return (a == v.a) && (b == v.b); } void input() { a.input(); b.input(); } double length() { return a.distance(b); } double dispointtoline(point3 p) { return b.sub(a).det(p.sub(a)).len() / a.distance(b); } double dispointtoseg(point3 p) { if (dcmp(p.sub(b).dot(a.sub(b))) < 0 || dcmp(p.sub(a).dot(b.sub(a))) < 0) return min(p.distance(a), p.distance(b)); return dispointtoline(p); } point3 lineprog(point3 p) { return a.add(b.sub(a).trunc(b.sub(a).dot(p.sub(a)) / b.distance(a))); } point3 rotate(point3 p, double ang) { if (dcmp((p.sub(a).det(p.sub(b)).len())) == 0) return p; point3 f1 = b.sub(a).det(p.sub(a)); point3 f2 = b.sub(a).det(f1); double len = fabs(a.sub(p).det(b.sub(p)).len() / a.distance(b)); f1 = f1.trunc(len); f2 = f2.trunc(len); point3 h = p.add(f2); point3 pp = h.add(f1); return h.add((p.sub(h)).mul(cos(ang * 1.0))) .add((pp.sub(h)).mul(sin(ang * 1.0))); } }; struct plane { point3 a, b, c, o; plane() {} plane(point3 _a, point3 _b, point3 _c) { a = _a; b = _b; c = _c; o = pvec(); } plane(double _a, double _b, double _c, double _d) { o = point3(_a, _b, _c); if (dcmp(_a) != 0) a = point3((-_d - _c - _b) / _a, 1, 1); else if (dcmp(_b) != 0) a = point3(1, (-_d - _c - _a) / _b, 1); else if (dcmp(_c) != 0) a = point3(1, 1, (-_d - _a - _b) / _c); } void input() { a.input(); b.input(); c.input(); o = pvec(); } point3 pvec() { return b.sub(a).det(c.sub(a)); } bool pointonplane(point3 p) { return dcmp(p.sub(a).dot(o)) == 0; } double angleplane(plane f) { return acos(o.dot(f.o) / (o.len() * f.o.len())); } double dispoint(point3 p) { return fabs(p.sub(a).dot(o) / o.len()); } point3 pttoplane(point3 p) { line3 u = line3(p, p.add(o)); crossline(u, p); return p; } int crossline(line3 u, point3 &p) { double x = o.dot(u.b.sub(a)); double y = o.dot(u.a.sub(a)); double d = x - y; if (dcmp(fabs(d)) == 0) return 0; p = u.a.mul(x).sub(u.b.mul(y)).div(d); return 1; } int crossplane(plane f, line3 &u) { point3 oo = o.det(f.o); point3 v = o.det(oo); double d = fabs(f.o.dot(v)); if (dcmp(d) == 0) return 0; point3 q = a.add(v.mul(f.o.dot(f.a.sub(a)) / d)); u = line3(q, q.add(oo)); return 1; } }; const int MAXN = 1005; struct node { int x, y, t; double p; friend bool operator<(node a, node b) { return a.t < b.t; } void input() { scanf( %d%d%d%lf , &x, &y, &t, &p); } } p[MAXN]; double dp[MAXN]; int sqr(int x) { return x * x; } double dis(int i, int j) { return sqrt(sqr(p[i].x - p[j].x) + sqr(p[i].y - p[j].y)); } int main() { int n; while (cin >> n) { for (int i = 1; i <= n; i++) p[i].input(); sort(p + 1, p + n + 1); memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) { dp[i] = p[i].p; for (int j = 1; j < i; j++) if (dis(i, j) + p[j].t <= p[i].t) dp[i] = max(dp[i], dp[j] + p[i].p); } printf( %.10lf n , *max_element(dp + 1, dp + n + 1)); } return 0; }
#include <bits/stdc++.h> const int a[250] = {0, 1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51, 1, 2, 1, 14, 1, 2, 2, 14, 1, 6, 1, 4, 2, 2, 1, 52, 2, 5, 1, 5, 1, 15, 2, 13, 2, 2, 1, 13, 1, 2, 4, 267, 1, 4, 1, 5, 1, 4, 1, 50, 1, 2, 3, 4, 1, 6, 1, 52, 15, 2, 1, 15, 1, 2, 1, 12, 1, 10, 1, 4, 2}; int main() { int n; scanf( %d , &n); printf( %d , a[n]); return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-9; int n, m, k; long long dp[35][35][100]; long long dfs(int l, int r, int t) { if (l * r < t) return 999999999; if (l > r) swap(l, r); if (dp[l][r][t] != -1) return dp[l][r][t]; if (t == 0) { dp[l][r][t] = 0; return 0; } if (l * r == t) return 0; long long ans1 = 9999999999999LL; for (int i = 1; i < l; i++) for (int j = 0; j <= t; j++) ans1 = min(ans1, dfs(i, r, j) + dfs(l - i, r, t - j) + r * r); for (int i = 1; i < r; i++) for (int j = 0; j <= t; j++) ans1 = min(ans1, dfs(l, i, j) + dfs(l, r - i, t - j) + l * l); if (dp[l][r][t] != -1) dp[l][r][t] = min(dp[l][r][t], ans1); else dp[l][r][t] = ans1; return dp[l][r][t]; } int main() { int t; scanf( %d , &t); memset(dp, -1, sizeof(dp)); while (t--) { scanf( %d%d%d , &n, &m, &k); printf( %I64d n , dfs(n, m, k)); } }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; int main() { ios_base::sync_with_stdio(0); map<long long, bool> mapa; long long in[200009]; long long n, len, x, y; cin >> n >> len >> x >> y; for (int i = 0; i < n; i++) { cin >> in[i]; mapa[in[i]] = true; } bool x_mila = false, y_mila = false; for (int i = 0; i < n; i++) { if (mapa[in[i] - x] == true) x_mila = true; if (mapa[in[i] + x] == true) x_mila = true; if (mapa[in[i] - y] == true) y_mila = true; if (mapa[in[i] + y] == true) y_mila = true; } if (x_mila && y_mila) { cout << 0 << endl; return 0; } vector<int> opt; for (int i = 0; i < n; i++) { if (in[i] - x >= 0) opt.push_back(in[i] - x); if (in[i] + x <= len) opt.push_back(in[i] + x); if (in[i] - y >= 0) opt.push_back(in[i] - y); if (in[i] + y <= len) opt.push_back(in[i] + y); } for (int i = 0; i < ((int)(opt.size())); i++) { bool xm = false, ym = false; int curr = opt[i]; if (mapa[curr - x] || mapa[curr + x]) xm = true; if (mapa[curr - y] || mapa[curr + y]) ym = true; if ((xm && ym) || (x_mila && ym) || (xm && y_mila)) { cout << 1 << endl; cout << curr << endl; return 0; } } cout << 2 << endl; cout << x << << y << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); long long l, r; long long g(long long x) { long long t = 1; while (t <= x) t *= 10; return (t - 1 - x) * x; } int n, m; const char let[] = ACGT ; char buf[1600]; int len[16]; int a[16][16]; vector<int> st; struct Node { Node* ch[4]; Node* next[4]; int end; Node() { memset(a, 0, sizeof a); ; } void mark(Node* root) { end = 0; for (int i = 0, _n = (m); i < _n; i++) if (len[i] <= (int)(st).size()) { bool eq = true; for (int k = 0, _n = (len[i]); k < _n; k++) if (st[(int)(st).size() - len[i] + k] != a[i][k]) { eq = false; break; } if (eq) { end = max(end, len[i]); } } for (int i = 0, _n = (4); i < _n; i++) if (ch[i]) { st.push_back(i); ch[i]->mark(root); st.pop_back(); next[i] = ch[i]; } else { st.push_back(i); next[i] = 0; for (int j = 0, _n = ((int)(st).size() + 1); j < _n; j++) if (j > 0) { Node* v = root; for (int jj = j; jj < (int)(st).size() && v; jj++) v = v->ch[st[jj]]; if (v) { next[i] = v; break; } } st.pop_back(); } } }; Node nodes[1024]; int w; int d[1024][128][11]; int main() { gets(buf); sscanf(buf, %d%d , &n, &m); for (int i = 0, _n = (m); i < _n; i++) { gets(buf); len[i] = strlen(buf); for (int j = 0, _n = (len[i]); j < _n; j++) a[i][j] = strchr(let, buf[j]) - let; } w = 1; for (int i = 0, _n = (m); i < _n; i++) { Node* v = &nodes[0]; for (int j = 0, _n = (len[i]); j < _n; j++) { if (v->ch[a[i][j]] == 0) v->ch[a[i][j]] = &nodes[w++]; v = v->ch[a[i][j]]; } } nodes[0].mark(&nodes[0]); memset(d, 0, sizeof d); ; d[0][0][0] = 1; for (int i = 0, _n = (n); i < _n; i++) for (int j = 0, _n = (w); j < _n; j++) for (int c = 0, _n = (11); c < _n; c++) if (d[i][j][c]) for (int add = 0, _n = (4); add < _n; add++) { int nc = c + 1; Node* v = nodes[j].next[add]; if (v->end >= nc) nc = 0; if (nc == 10) continue; d[i + 1][v - nodes][nc] = (d[i + 1][v - nodes][nc] + d[i][j][c]) % 1000000009; } long long res = 0; for (int j = 0, _n = (w); j < _n; j++) res = (res + d[n][j][0]) % 1000000009; cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long int mul(long long int a, long long int b) { return ((a % int(1e9 + 7)) * (b % int(1e9 + 7))) % int(1e9 + 7); } long long int add(long long int a, long long int b) { return ((a % int(1e9 + 7)) + (b % int(1e9 + 7))) % int(1e9 + 7); } long long int sub(long long int a, long long int b) { long long int ans = ((a % int(1e9 + 7)) - (b % int(1e9 + 7))) % int(1e9 + 7); ans %= int(1e9 + 7); ans = (ans + int(1e9 + 7)) % int(1e9 + 7); return ans; } long long int mpow(long long int a, long long int b) { long long int ans = 1; long long int po = a; while (b != 0) { if (b % 2) { ans = ((ans % int(1e9 + 7)) * (po % int(1e9 + 7))) % int(1e9 + 7); } po = ((po % int(1e9 + 7)) * (po % int(1e9 + 7))) % int(1e9 + 7); b /= 2; } return ans; } void solve() { long long int h, w; cin >> h >> w; long long int ans = 0, ffh, ffw; for (int i = 0; i < 2; i++) { if (i) swap(w, h); if (i) swap(ffw, ffh); long long int fh = 1LL << (long long int)floor(log2(h) + 1e-6); while (w < fh / 1.25) { fh /= 2; } long long int fw = min(w, (long long int)(fh / .8 + 1e-6)); if (ans < fh * fw) { ans = fh * fw; ffh = fh; ffw = fw; } else if (ans == fh * fw) { if (i == 0 and fh > ffh) { ffh = fh; ffw = fw; } else if (i == 1 and fw > ffw) { ffh = fh; ffw = fw; } } if (i) { swap(w, h); swap(ffw, ffh); } } cout << ffh << << ffw << n ; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t; solve(); }
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, F = 1; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) F = -1; c = getchar(); } while (c >= 0 && c <= 9 ) { x = x * 10 + c - 0 ; c = getchar(); } return x * F; } int n, s, m, k; int a[2000 + 5], b[2000 + 5], Nxt[2000 + 5]; int dp[2000 + 5][2000 + 5], sum[2000 + 5]; bool check(int val) { for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + (a[i] <= val); for (int i = 0; i <= m; i++) for (int j = 0; j <= n; j++) dp[i][j] = 0; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) dp[i][j] = max(dp[i][j], dp[i - 1][j]); for (int j = 1; j <= n; j++) if (Nxt[j]) dp[i][Nxt[j]] = max(dp[i][Nxt[j]], dp[i - 1][j - 1] + sum[Nxt[j]] - sum[j - 1]); for (int j = 1; j <= n; j++) dp[i][j] = max(dp[i][j], dp[i][j - 1]); } return dp[m][n] >= k; } int main() { n = read(), s = read(), m = read(), k = read(); for (int i = 1; i <= n; i++) b[i] = a[i] = read(); for (int i = 1; i <= s; i++) { int l = read(), r = read(); for (int j = l; j <= r; j++) Nxt[j] = max(Nxt[j], r); } sort(b + 1, b + n + 1); int l = 1, r = n, ans = -1; while (l <= r) { int mid = (l + r) >> 1; if (check(b[mid])) ans = b[mid], r = mid - 1; else l = mid + 1; } printf( %d , ans); }
#include <bits/stdc++.h> using namespace std; const int maxn = 300005; int N, K, val[maxn], prime[30000], cnt, limit, ans; bool del[maxn]; void pre() { for (int i = 2; i <= N; i++) { if (!val[i]) prime[++cnt] = i; val[i]++; for (int j = i + i; j <= N; j += i) val[j]++; } } int main() { scanf( %d%d , &N, &K); pre(); long long tot = 0; for (int i = 1; i <= N; i++) { tot += val[i]; if (tot >= K) { ans = limit = i; break; } } if (tot > K) for (int i = 1; i <= cnt; i++) { if (tot == K) break; if (tot - K >= limit / prime[i]) { del[prime[i]] = 1; tot -= limit / prime[i]; ans--; } } if (tot != K) printf( No n ); else { printf( Yes n ); printf( %d n , ans); for (int i = 1; i <= limit; i++) if (!del[i]) printf( %d , i); puts( ); } return 0; }
#include <bits/stdc++.h> using namespace std; int get(int l, int n) { int v1, v2; cout << ? << l << endl; fflush(stdout); cin >> v1; cout << ? << (l + n / 2 - 1) % n + 1 << endl; fflush(stdout); cin >> v2; return v2 - v1; } int main() { int n; cin >> n; int l, r; r = (n / 2 + 2) / 2; l = 1; int v_r, v_l; v_r = get(r, n); v_l = get(l, n); if (v_r == 0) { cout << ! << r << endl; return 0; } if (v_l == 0) { cout << ! << l << endl; return 0; } if (((v_r > 0) - (v_r < 0)) * ((v_l > 0) - (v_l < 0)) == 1) { int tmp = r; r = l + n / 2; l = tmp; } int m, v_m; while (r - l > 1) { m = (r + l) / 2; v_m = get(m, n); if (v_m == 0) { cout << ! << m << endl; return 0; } if (((v_m > 0) - (v_m < 0)) * ((v_l > 0) - (v_l < 0)) == 1) { l = m; v_l = v_m; } else { r = m; v_r = v_m; } } cout << ! << -1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int dp[2010][2010]; int main() { int n, h, i; scanf( %d%d , &n, &h); dp[0][0] = 1; for (i = 1; i <= n; i++) { int x; scanf( %d , &x); x = h - x; if (x < 0) { printf( 0 n ); return 0; } if (x >= 1) { dp[i][x] = (dp[i - 1][x] + dp[i - 1][x - 1]) % 1000000007; dp[i][x - 1] = 1LL * dp[i][x] * x % 1000000007; } else { dp[i][0] = dp[i - 1][0]; } } printf( %d n , dp[n][0]); return 0; }
#include <bits/stdc++.h> using namespace std; unordered_map<char, long long> s, p; bool check() { long long any = s[ ? ]; long long sub = 0; for (auto it = p.begin(); it != p.end(); it++) { if (s[it->first] > p[it->first]) return false; else if (s[it->first] < p[it->first]) sub += (p[it->first] - s[it->first]); } if (any >= sub) return true; return false; } long long solve(string s1, string s2) { long long ans = 0; for (long long i = 0; i < s2.size(); i++) { s[s1[i]]++; } if (check()) ans++; long long start = 0; for (long long i = s2.size(); i < s1.size(); i++) { s[s1[start]]--; s[s1[i]]++; if (check()) ans++; start++; } return ans; } int main() { string s1, s2; cin >> s1 >> s2; for (long long i = 0; i < s2.length(); i++) p[s2[i]]++; if (s2.length() > s1.length()) cout << 0 n ; else cout << solve(s1, s2) << endl; return 0; }