func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; const int N = 200000; long long x[N], y[N]; vector<int> ids[N]; int order[N]; long long cross(int a, int b, int c) { return x[c] * y[c] * (x[b] * y[a] - x[a] * y[b]); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> x[i] >> y[i]; } iota(order, order + n, 0); sort(order, order + n, [&](int a, int b) { return x[a] == x[b] ? y[a] > y[b] : x[a] > x[b]; }); vector<int> hull; for (int i = 0, j = 0; i < n; i = j) { int a = order[i]; while (hull.size() >= 2) { int b = hull[hull.size() - 1], c = hull[hull.size() - 2]; if (cross(a, c, b) + cross(b, a, c) + cross(c, b, a) >= 0) { break; } hull.pop_back(); } hull.push_back(a); while (j < n && x[order[j]] == x[a] && y[order[j]] == y[a]) { ids[a].push_back(order[j++]); } } while (hull.size() >= 2) { int a = hull[hull.size() - 1], b = hull[hull.size() - 2]; if (y[b] < y[a]) { break; } hull.pop_back(); } vector<int> ans; for (auto i : hull) { ans.insert(ans.begin(), ids[i].begin(), ids[i].end()); } sort(ans.begin(), ans.end()); for (auto i : ans) { cout << i + 1 << ; } cout << n ; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1000005; class fenwick { int n; vector<int> v; public: fenwick(int _n) : n(_n) { v.resize(n); } void update(int at, int by) { while (at < v.size()) { v[at] += by; at |= (at + 1); } } int query(int at) { int res = 0; while (at >= 0) { res += v[at]; at = (at & (at + 1)) - 1; } return res; } }; map<int, int> indexes; int get_index(int x) { if (indexes.count(x) == 0) { int sz = indexes.size(); indexes[x] = sz; } return indexes[x]; } vector<int> f(vector<int> &v) { int n = v.size(); vector<int> cnt(maxn, 0); vector<int> ret; for (auto &x : v) { cnt[x]++; ret.push_back(cnt[x]); } return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; ++i) { int x; cin >> x; v[i] = get_index(x); } auto left = f(v); reverse(v.begin(), v.end()); auto right = f(v); reverse(right.begin(), right.end()); fenwick ft = fenwick(maxn); for (int i = 0; i < n; ++i) { ft.update(right[i], 1); } long long ans = 0; for (int i = 0; i < n; ++i) { ft.update(right[i], -1); int cnt = ft.query(left[i] - 1); ans += cnt; } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long int mod = 998244353; vector<long long int> prime; long long int n = 1000001; vector<bool> vis(n, true); void sieve() { vis[0] = vis[1] = false; for (long long int i = 2; i * i <= n; i++) if (vis[i]) for (long long int j = i * i; j <= n; j += i) vis[j] = false; for (long long int i = 2; i <= n; i++) if (vis[i]) prime.push_back(i); } bool check(long long int n, long long int k) { if (n < 2 * k) return false; if (k == 1) return vis[n]; if (k == 2) { if (n % 2) return vis[n - 2]; return true; } return true; } int32_t main() { sieve(); long long int n; cin >> n; for (auto it = lower_bound(prime.begin(), prime.end(), n); it != prime.end(); it++) { if (*it > ((n * (n - 1)) >> 1)) break; if (!check((*it) << 1, n)) continue; long long int m = *it; cout << m << endl; m -= n; for (long long int i = 1; i <= n; i++) { long long int z = i + 1; if (z > n) z = 1; cout << i << << z << endl; } long long int diff = 2, init = (n >> 1) + (n & 1); while (m--) { cout << init << << init + diff << endl; diff += 2; init--; } return 0; } cout << -1 ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAXN = 1e5 + 3; const int inf = 2e9; int max(int a, int b) { if (a > b) return a; else return b; } int min(int a, int b) { if (a < b) return a; else return b; } bool cmp(pair<int, int> p1, pair<int, int> p2) { if (p1.first < p2.first) return true; else if (p1.first == p2.first) { if (p1.second < p2.second) return true; return false; } return false; } void solve() { int n, m, d; cin >> n >> m >> d; int a[n][m]; for (int i = (int)0; i < (int)n; ++i) { for (int j = (int)0; j < (int)m; ++j) { cin >> a[i][j]; } } int check = a[0][0] % d; vector<int> v; bool pr = false; for (int i = (int)0; i < (int)n; ++i) { for (int j = (int)0; j < (int)m; ++j) { if (a[i][j] % d == check) { v.push_back(a[i][j]); } else { pr = true; break; } } } if (pr) cout << -1 << n ; else { sort((v).begin(), (v).end()); int cc = (n * m); int bi = (cc / 2); int ans = 0; for (int i = (int)0; i < (int)cc; ++i) { ans += abs(v[i] - v[bi]); } cout << ans / d << n ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if ((a == 0) || (b == 0)) return a + b; return gcd(b, a % b); } long long pow_mod(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % 1000000007; a = (a * a) % 1000000007; b >>= 1; } return (res % 1000000007); } long long pow_2(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a); a = (a * a); b >>= 1; } return res; } bool isPrime(long long a) { for (int i = 3; (i * i) <= a; i += 2) { if ((a % i) == 0) return false; } if ((a != 2) && ((a % 2) == 0)) return false; if (a == 1) return false; return true; } string con_ll_to_str(int a) { stringstream mystr; mystr << a; return mystr.str(); } long long con_str_to_ll(string st) { long long numb = 0; int len = st.size(), i, j = 0; for (i = len - 1; i >= 0; i--) { numb += (pow_2(10, j) * (st[i] - 0 )); j++; } return numb; } long long baseno_to_decimal(string st, long long basee) { long long i = 0, j, anss = 0; for (j = (int)st.length() - 1; j >= 0; j--) { anss += (st[j] - 0 ) * pow_2(basee, i); i++; } return anss; } string decimal_to_string(long long num, long long basee) { long long i = 0, j, opop; string anss = ; vector<string> stri; stri.push_back( 0 ); stri.push_back( 1 ); stri.push_back( 2 ); stri.push_back( 3 ); stri.push_back( 4 ); stri.push_back( 5 ); stri.push_back( 6 ); stri.push_back( 7 ); stri.push_back( 8 ); stri.push_back( 9 ); stri.push_back( A ); stri.push_back( B ); stri.push_back( C ); stri.push_back( D ); stri.push_back( E ); stri.push_back( F ); if (num == 0) { return 0 ; } while (num) { opop = num % basee; anss += stri[opop]; num /= basee; } reverse(anss.begin(), anss.end()); return anss; } const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}; long long arr[100010]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long T, N, K, i, j; string st = ; cin >> N >> K; if (K > N) { cout << -1 ; return 0; } if (K == 1 && N > 1) { cout << -1 ; return 0; } if (N == 1) { cout << a ; return 0; } long long opop = (N - K + 2); for (i = 1; i <= opop; i++) { if (i % 2) { st.push_back( a ); } else { st.push_back( b ); } } long long po = st.length(), pu = 0; for (i = po + 1; i <= N; i++) { st.push_back( c + pu); pu++; } cout << st; return 0; } |
#include <bits/stdc++.h> using namespace std; double mas[10000][3]; void optim(int cnt, double Px, double Py, double Pz, double vs, double vp) { double l = 0, r = 0; double x1 = mas[cnt][0]; double y1 = mas[cnt][1]; double z1 = mas[cnt][2]; double x2 = mas[cnt + 1][0]; double y2 = mas[cnt + 1][1]; double z2 = mas[cnt + 1][2]; for (int i = 0; i < cnt; i++) { l += sqrt((mas[i][0] - mas[i + 1][0]) * (mas[i][0] - mas[i + 1][0]) + (mas[i][1] - mas[i + 1][1]) * (mas[i][1] - mas[i + 1][1]) + (mas[i][2] - mas[i + 1][2]) * (mas[i][2] - mas[i + 1][2])); r += sqrt((mas[i][0] - mas[i + 1][0]) * (mas[i][0] - mas[i + 1][0]) + (mas[i][1] - mas[i + 1][1]) * (mas[i][1] - mas[i + 1][1]) + (mas[i][2] - mas[i + 1][2]) * (mas[i][2] - mas[i + 1][2])); } int j = cnt; r += sqrt((mas[j][0] - mas[j + 1][0]) * (mas[j][0] - mas[j + 1][0]) + (mas[j][1] - mas[j + 1][1]) * (mas[j][1] - mas[j + 1][1]) + (mas[j][2] - mas[j + 1][2]) * (mas[j][2] - mas[j + 1][2])); l /= vs; r /= vs; while (r - l > 1E-12) { double m = (r + l) / 2; double xx = (x1 + x2) / 2, yy = (y1 + y2) / 2, zz = (z1 + z2) / 2; double dist2 = sqrt((xx - Px) * (xx - Px) + (yy - Py) * (yy - Py) + (zz - Pz) * (zz - Pz)); double t2 = dist2 / vp; if (t2 - m > 1E-10) { l = m; x1 = xx; y1 = yy; z1 = zz; } else if (t2 - m < -1E-10) { r = m; x2 = xx; y2 = yy; z2 = zz; } else { l = m; x1 = xx; y1 = yy; z1 = zz; break; } } printf( %.6lf n , l); printf( %.6lf %.6lf %.6lf n , x1, y1, z1); } int main() { int n; cin >> n; double vs, vp; for (int i = 0; i <= n; i++) cin >> mas[i][0] >> mas[i][1] >> mas[i][2]; cin >> vp >> vs; double Px, Py, Pz; cin >> Px >> Py >> Pz; bool f = false; int cnt = 0; double dist = 0; if (fabs(Px - mas[0][0]) < 1E-7 && fabs(Py - mas[0][1]) < 1E-7 && fabs(Pz - mas[0][2]) < 1E-7) { cout << YES << endl; printf( %.6lf n , 0.0); printf( %.6lf %.6lf %.6lf n , Px, Py, Pz); return 0; } for (int i = 0; i < n; i++) { dist += sqrt((mas[i][0] - mas[i + 1][0]) * (mas[i][0] - mas[i + 1][0]) + (mas[i][1] - mas[i + 1][1]) * (mas[i][1] - mas[i + 1][1]) + (mas[i][2] - mas[i + 1][2]) * (mas[i][2] - mas[i + 1][2])); double dist2 = sqrt((mas[i + 1][0] - Px) * (mas[i + 1][0] - Px) + (mas[i + 1][1] - Py) * (mas[i + 1][1] - Py) + (mas[i + 1][2] - Pz) * (mas[i + 1][2] - Pz)); double t1 = dist / vs; double t2 = dist2 / vp; if (t1 - t2 > -1E-14) { cnt = i; f = true; break; } } if (!f) { cout << NO ; return 0; } else cout << YES << endl; optim(cnt, Px, Py, Pz, vs, vp); return 0; } |
#include <bits/stdc++.h> using namespace std; constexpr long long MOD = 1e9 + 7; struct { int val, lazy; } tree[4 * 1000010]; int bal(int p) { return tree[p].val + tree[p].lazy; } void ins(int S, int val, int L = 1, int R = 1000000, int p = 1) { if (S < L || R < 1) return; if (1 <= L && R <= S) { tree[p].lazy += val; return; } tree[2 * p].lazy += tree[p].lazy; tree[2 * p + 1].lazy += tree[p].lazy; tree[p].lazy = 0; int M = (L + R) / 2; ins(S, val, L, M, 2 * p); ins(S, val, M + 1, R, 2 * p + 1); tree[p].val = max(bal(2 * p), bal(2 * p + 1)); } int query(int L = 1, int R = 1000000, int p = 1) { if (L == R) { return bal(p) > 0 ? L : -1; } int M = (L + R) / 2; tree[2 * p].lazy += tree[p].lazy; tree[2 * p + 1].lazy += tree[p].lazy; tree[p].val += tree[p].lazy; tree[p].lazy = 0; if (bal(2 * p + 1) > 0) return query(M + 1, R, 2 * p + 1); return query(L, M, 2 * p); } int main() { int n, m; scanf( %d%d , &n, &m); vector<int> a[2]; for (int i = 0; i < n; ++i) { int x; scanf( %d , &x); a[0].push_back(x); ins(x, 1); } for (int i = 0; i < m; ++i) { int x; scanf( %d , &x); a[1].push_back(x); ins(x, -1); } int q; scanf( %d , &q); while (q--) { int idx, i, x; scanf( %d%d%d , &idx, &i, &x); --i; --idx; if (idx == 0) { ins(a[idx][i], -1); ins(x, 1); } else { ins(a[idx][i], 1); ins(x, -1); } a[idx][i] = x; printf( %d n , query()); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int main() { long long k; long long now = 9; long long last = 1; cin >> k; if (k <= 9) { cout << k; return 0; } long long finalk = k; long long pow = 1; for (int step = 1; step <= 15; step++) { long long tmplast = last - 1; if (k <= now) { long long this_num = tmplast + (k / step); bool OK = 1; long long tmpnum = this_num; for (int i = 1; i <= step; i++) { if (tmpnum % 10 != 9) OK = false; tmpnum /= 10; } if (OK) step++; if (k % step) this_num += 1; long long yiwei = (step - k % step); if (yiwei == step) yiwei = 0; for (int i = 1; i <= yiwei; i++) { this_num /= 10; } printf( %d n , this_num % 10); return 0; } k -= now; pow *= 10; now = 9 * pow; now *= (step + 1); last *= 10; } return 0; } |
#include <bits/stdc++.h> using namespace std; int n; int m; vector<vector<int>> row; vector<vector<int>> col; vector<string> g; vector<vector<bool>> vis; struct node { int d; int i; int j; }; struct NODE { vector<int> e; bool vis = false; }; vector<NODE> G; bool operator<(const node a, const node b) { return a.d > b.d; } int dijk() { priority_queue<pair<int, int>> q; q.push({-1, n - 1}); pair<int, int> cur; while (!q.empty()) { cur = q.top(); q.pop(); if (!G[cur.second].vis) { G[cur.second].vis = true; if (cur.second == 0) { return -cur.first - 1; } for (int i = 0; i < G[cur.second].e.size(); ++i) { int nex = G[cur.second].e[i]; q.push({cur.first - 1, nex}); } } } return -1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; g.resize(n); row.resize(n); col.resize(m); string T1(n, . ); string T2(m, # ); vis.resize(n, (vector<bool>(m, false))); G.resize(n + m); for (int i = 0; i < n; ++i) { cin >> g[i]; } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (g[i][j] == # ) { G[i].e.push_back(n + j); G[n + j].e.push_back(i); } } } cout << dijk() << n ; } |
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> istream &operator>>(istream &in, pair<T1, T2> &a) { in >> a.first >> a.second; return in; } template <typename T1, typename T2> ostream &operator<<(ostream &out, pair<T1, T2> a) { out << a.first << << a.second; return out; } template <typename T, typename T1> T amax(T &a, T1 b) { if (b > a) a = b; return a; } template <typename T, typename T1> T amin(T &a, T1 b) { if (b < a) a = b; return a; } const long long INF = 1e18; const int32_t M = 1e9 + 7; const int32_t MM = 998244353; const int N = 5e5 + 5; const int32_t MOD = MM; struct mod_int { int32_t val; mod_int(long long v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = v; } static int32_t mod_inv(int32_t a, int32_t m = MOD) { int32_t g = m, r = a, x = 0, y = 1; while (r != 0) { int32_t q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return x < 0 ? x + m : x; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return x % m; unsigned x_high = x >> 32, x_low = (unsigned)x; unsigned quot, rem; asm( divl %4 n : =a (quot), =d (rem) : d (x_high), a (x_low), r (m)); return rem; } explicit operator int() const { return val; } mod_int &operator+=(const mod_int &other) { val += other.val; if (val >= MOD) val -= MOD; return *this; } mod_int &operator-=(const mod_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } mod_int &operator*=(const mod_int &other) { val = fast_mod((uint64_t)val * other.val); return *this; } mod_int &operator/=(const mod_int &other) { return *this *= other.inv(); } friend mod_int operator+(const mod_int &a, const mod_int &b) { return mod_int(a) += b; } friend mod_int operator-(const mod_int &a, const mod_int &b) { return mod_int(a) -= b; } friend mod_int operator*(const mod_int &a, const mod_int &b) { return mod_int(a) *= b; } friend mod_int operator/(const mod_int &a, const mod_int &b) { return mod_int(a) /= b; } mod_int &operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } mod_int &operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } mod_int operator++(int32_t) { mod_int before = *this; ++*this; return before; } mod_int operator--(int32_t) { mod_int before = *this; --*this; return before; } mod_int operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const mod_int &other) const { return val == other.val; } bool operator!=(const mod_int &other) const { return val != other.val; } mod_int inv() const { return mod_inv(val); } friend ostream &operator<<(ostream &stream, const mod_int &m) { return stream << m.val; } friend istream &operator>>(istream &stream, mod_int &m) { return stream >> m.val; } mod_int pow(long long p) const { assert(p >= 0); mod_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; a *= a; p >>= 1; } return result; } }; int n, k, m; pair<pair<int, int>, int> x[N]; vector<int> v[N]; bool is1[N][32]; int cnt[32]; int last[32]; int f[N][32]; mod_int dp[N][32]; mod_int ps[N][32]; void solve() { cin >> n >> k >> m; for (int i = 1; i < m + 1; i++) { cin >> x[i]; } sort(x + 1, x + m + 1); for (int i = 1; i < m + 1; i++) { v[x[i].first.first].push_back(i); v[x[i].first.second].push_back(-i); } for (int i = 1; i < n + 2; i++) { for (int y : v[i]) { if (y < 0) { } else { for (int j = 0; j < k; j++) { if (((x[y].second >> j) & 1)) cnt[j]++; } } } for (int j = 0; j < k; j++) { if (cnt[j]) is1[i][j] = 1; } for (int j = 0; j < k; j++) { f[i][j] = last[j]; } for (int y : v[i]) { if (y < 0) { y *= -1; for (int j = 0; j < k; j++) { if (((x[y].second >> j) & 1)) cnt[j]--; else amax(last[j], x[y].first.first); } } } } for (int j = 0; j < k; j++) { dp[0][j] = 1; ps[0][j] = 1; } for (int i = 1; i < n + 2; i++) { for (int j = 0; j < k; j++) { if (!is1[i][j]) { dp[i][j] = ps[i - 1][j]; if (f[i][j]) dp[i][j] -= ps[f[i][j] - 1][j]; } ps[i][j] = dp[i][j] + ps[i - 1][j]; } } mod_int ans = 1; for (int i = 0; i < k; i++) { ans *= dp[n + 1][i]; } cout << ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int n; int f[200005][3], ans, dep[200005]; int head[200005], cnt; struct data { int next, to; data(int next = 0, int to = 0) : next(next), to(to) {} } edge[200005 << 1]; int read() { char c = getchar(); int x = 0; while (!isdigit(c)) c = getchar(); while (isdigit(c)) { x = (x << 3) + (x << 1) + c - 0 ; c = getchar(); } return x; } void connect(int u, int v) { edge[++cnt] = data(head[u], v); head[u] = cnt; } void DFS(int u, int pre) { f[u][0] = 1; dep[u] = dep[pre] + 1; int mini = 0x3f3f3f3f; for (int i = head[u]; i; i = edge[i].next) { int v = edge[i].to; if (v == pre) continue; DFS(v, u); f[u][0] = f[u][0] + min(min(f[v][0], f[v][1]), f[v][2]); f[u][2] = f[u][2] + min(f[v][0], f[v][1]); mini = min(mini, f[v][0] - min(f[v][0], f[v][1])); } f[u][1] = f[u][2] + mini; if (dep[u] == 3) ans = ans + min(f[u][0], min(f[u][1], f[u][2])); } int main() { n = read(); for (int i = 1; i < n; ++i) { int u = read(), v = read(); connect(u, v); connect(v, u); } DFS(1, 0); printf( %d n , ans); } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int q; cin >> q; vector<int> powers; powers.reserve(10000); for (int i = 0; i < (1 << 10); i++) { long long tot = 0; long long pow = 1; for (int j = 0; j < 10; j++) { if (i & (1 << j)) tot += pow; pow *= 3; } powers.push_back(tot); } sort(powers.begin(), powers.end()); while (q--) { int n; cin >> n; if (find(powers.begin(), powers.end(), n) == powers.end()) cout << *upper_bound(powers.begin(), powers.end(), n) << n ; else cout << n << n ; } } |
#include <bits/stdc++.h> void dfs(int x); struct node { int n, h; } h[100001]; bool operator<(const node &A, const node &B) { return A.h < B.h; } std::vector<int> map[100001]; int range[100001][2]; void init(int n); int getmax(int f, int r); void set(int x, int v); int imax[100001][3]; int dmax[100001][3]; int main() { int n; int i, j; int t, a, b; scanf( %d , &n); for (i = 1; i <= n; i++) { scanf( %d , &h[i].h); h[i].n = i; } std::sort(h + 1, h + n + 1); for (i = 1; i < n; i++) { scanf( %d%d , &a, &b); map[a].push_back(b); map[b].push_back(a); } dfs(1); init(n); for (i = 1; i <= n; i = j) { for (j = i; j <= n && h[j].h == h[i].h; j++) { int x = h[j].n; for (int k = 0; k < map[x].size(); k++) { int xx = map[x][k]; if (range[xx][0] < range[x][0]) continue; int t = getmax(range[xx][0], range[xx][1]); if (t > imax[x][0]) { imax[x][2] = imax[x][0]; imax[x][1] = xx; imax[x][0] = t; } else if (t > imax[x][2]) { imax[x][2] = t; } } } for (j = i; j <= n && h[j].h == h[i].h; j++) { int x = h[j].n; set(range[x][0], imax[x][0] + 1); } } init(n); for (i = n; i >= 1; i = j) { for (j = i; j >= 1 && h[j].h == h[i].h; j--) { int x = h[j].n; for (int k = 0; k < map[x].size(); k++) { int xx = map[x][k]; if (range[xx][0] < range[x][0]) continue; int t = getmax(range[xx][0], range[xx][1]); if (t > dmax[x][0]) { dmax[x][2] = dmax[x][0]; dmax[x][1] = xx; dmax[x][0] = t; } else if (t > dmax[x][2]) { dmax[x][2] = t; } } } for (j = i; j >= 1 && h[j].h == h[i].h; j--) { int x = h[j].n; set(range[x][0], dmax[x][0] + 1); } } int ans = 0; for (i = 1; i <= n; i++) { if (imax[i][1] == dmax[i][1]) { if (ans < imax[i][0] + dmax[i][2] + 1) ans = imax[i][0] + dmax[i][2] + 1; if (ans < imax[i][2] + dmax[i][0] + 1) ans = imax[i][2] + dmax[i][0] + 1; } else { if (ans < imax[i][0] + dmax[i][0] + 1) ans = imax[i][0] + dmax[i][0] + 1; } } init(n); for (i = 1; i <= n; i = j) { for (j = i; j <= n && h[j].h == h[i].h; j++) { int x = h[j].n; int t = getmax(range[x][1] + 1, n); if (ans < t + dmax[x][0] + 1) ans = t + dmax[x][0] + 1; } for (j = i; j <= n && h[j].h == h[i].h; j++) { int x = h[j].n; set(range[x][0], imax[x][0] + 1); } } init(n); for (i = n; i >= 1; i = j) { for (j = i; j >= 1 && h[j].h == h[i].h; j--) { int x = h[j].n; int t = getmax(range[x][1] + 1, n); if (ans < t + imax[x][0] + 1) ans = t + imax[x][0] + 1; } for (j = i; j >= 1 && h[j].h == h[i].h; j--) { int x = h[j].n; set(range[x][0], dmax[x][0] + 1); } } printf( %d , ans); return 0; } int dT = 0; void dfs(int x) { range[x][0] = ++dT; for (int i = 0; i < map[x].size(); i++) { int xx = map[x][i]; if (range[xx][0] != 0) continue; dfs(xx); } range[x][1] = dT; } int IT[400001]; int b; void init(int n) { for (b = 1; b <= n; b *= 2) ; for (int i = b * 2; i >= 1; i--) IT[i] = 0; } int max(int a, int b) { return a > b ? a : b; } int getmax(int f, int r) { f += b; r += b; int m = 0; while (f < r) { if (f % 2 == 1) m = max(m, IT[f++]); if (r % 2 == 0) m = max(m, IT[r--]); f /= 2; r /= 2; } if (f == r) m = max(m, IT[f]); return m; } void set(int x, int v) { IT[x += b] = v; while (x /= 2) IT[x] = max(IT[x * 2], IT[x * 2 + 1]); } |
#include<bits/stdc++.h> #define ll long long #define L(i, j, k) for(int i = (j); i <= (k); ++i) #define R(i, j, k) for(int i = (j); i >= (k); --i) #define vi vector < int > #define sz(a) ((int) a.size()) using namespace std; const int N = 1e6 + 7; int T; int n, m, c[N], a[N], b[N]; void Main () { n = m = 0; int x; cin >> x; L(i, 1, x) { int w; cin >> w; c[i] = w; if(i & 1) a[++n] = w; else b[++m] = w; } sort(a + 1, a + n + 1); sort(b + 1, b + m + 1); sort(c + 1, c + x + 1); n = 0, m = 0; L(i, 1, x) { if(i & 1) { if(a[++n] != c[i]) return cout << NO n , void(); } else { if(b[++m] != c[i]) return cout << NO n , void(); } } cout << YES n ; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> T; while (T--) Main(); return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> inline T BMOD(T p, T e, T m) { T ret = 1; while (e) { if (e & 1) ret = (ret * p) % m; p = (p * p) % m; e >>= 1; } return (T)ret; } template <class T> inline T MODINV(T a, T m) { return BMOD(a, m - 2, m); } template <class T> inline T isPrime(T a) { for (T i = 2; i <= sqrt(double(a)); i++) { if (a % i == 0) { return 0; } } return 1; } template <class T> inline T lcm(T a, T b) { return (a / __gcd(a, b)) * b; } template <class T> inline T power(T a, T b) { return (b == 0) ? 1 : a * power(a, b - 1); } template <class T> inline string to_str(T t) { stringstream ss; ss << t; return ss.str(); } int main() { int h, m; scanf( %d:%d , &h, &m); h %= 12; double a = h * 30.0 + (m / 2.0); double b = m * 6.0; printf( %.10lf %.10lf n , a, b); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m; long long f[102][102]; int main() { scanf( %d%d , &n, &m); for (int i = 0; i <= n; i++) f[0][i] = 1; for (int j = 1; j <= n; j++) for (int i = 1; i <= n; i++) for (int k = 0; k < i; k++) f[i][j] += f[k][j - 1] * f[i - k - 1][j - 1]; printf( %lld , f[n][n] - f[n][m - 1]); } |
#include <bits/stdc++.h> using namespace std; int cnt[26]; void sol() { int n; cin >> n; for (int i = 0; i < n; i++) { long long k, x; cin >> k >> x; cout << (x + (k - 1) * 9) << n ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) sol(); } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int ans = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) { int inp; cin >> inp; if (i == j || i + j == n - 1 || i == n / 2 || j == n / 2) ans += inp; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MXN = 1 << 19; const int MOD = 1000000007; int arr[MXN]; int suf[2 * MXN]; int pre[2 * MXN]; int lb[2 * MXN]; int rb[2 * MXN]; void build(int c, int l, int r) { lb[c] = l; rb[c] = r; if (l == r) return; int k = (l + r) / 2; build(2 * c, l, k); build(2 * c + 1, k + 1, r); } void updatesuf(int c, int idx, int amt) { if (lb[c] == rb[c]) { suf[c] += amt; return; } int k = (lb[c] + rb[c]) / 2; if (idx <= k) updatesuf(2 * c, idx, amt); else updatesuf(2 * c + 1, idx, amt); suf[c] = suf[2 * c] + suf[2 * c + 1]; suf[c] %= MOD; } void updatepre(int c, int idx, int amt) { if (lb[c] == rb[c]) { pre[c] += amt; return; } int k = (lb[c] + rb[c]) / 2; if (idx <= k) updatepre(2 * c, idx, amt); else updatepre(2 * c + 1, idx, amt); pre[c] = pre[2 * c] + pre[2 * c + 1]; pre[c] %= MOD; } int querysuf(int c, int l, int r) { if (lb[c] == l && rb[c] == r) return suf[c]; int k = (lb[c] + rb[c]) / 2; if (l <= k && k < r) return (querysuf(2 * c, l, k) + querysuf(2 * c + 1, k + 1, r)) % MOD; else if (r <= k) return querysuf(2 * c, l, r); else return querysuf(2 * c + 1, l, r); } int querypre(int c, int l, int r) { if (lb[c] == l && rb[c] == r) return pre[c]; int k = (lb[c] + rb[c]) / 2; if (l <= k && k < r) return (querypre(2 * c, l, k) + querypre(2 * c + 1, k + 1, r)) % MOD; else if (r <= k) return querypre(2 * c, l, r); else return querypre(2 * c + 1, l, r); } int main() { int n; scanf( %d , &n); vector<pair<int, int> > proc; for (int i = 1; i <= n; i++) { scanf( %d , arr + i); proc.emplace_back(arr[i], i); } build(1, 0, MXN - 1); sort(proc.begin(), proc.end()); int ans = 0; for (auto p : proc) { int x = p.first; int idx = p.second; int l = idx - 1; int r = n - l - 1; int a = querysuf(1, idx, n); int b = querypre(1, 1, idx); long long cur = 1ll * a * (l + 1) + 1ll * b * (r + 1) + 1ll * (l + 1) * (r + 1); cur %= (long long)MOD; cur *= (long long)x; cur %= (long long)MOD; ans += cur; ans %= MOD; updatesuf(1, idx, r + 1); updatepre(1, idx, l + 1); } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &x, T y) { return x > y ? x = y, 1 : 0; } template <typename T> inline bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; } inline void read(int &x) { char ch = getchar(); int f = 1; x = 0; while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) x = x * 10 + ch - 0 , ch = getchar(); x *= f; } int n, m, p[100050], v[100050], nx[100050], pr[100050], id[100050]; set<pair<int, int> > se; int get(int x, int y) { if (x == y) return 1000000005; int d = (p[y] - p[x] + m) % m; if (x > y) d += v[y]; if (d <= v[x]) return 1; if (v[x] <= v[y]) return 1000000005; return (d - v[y] - 1) / (v[x] - v[y]) + 1; } bool cmp(int x, int y) { return p[x] < p[y]; } int main() { read(n), read(m); for (register int i = 1; i <= n; ++i) read(p[i]), --p[i], read(v[i]), id[i] = i; sort(id + 1, id + n + 1, cmp); for (register int i = 1; i <= n; ++i) pr[id[i]] = id[i - 1], nx[id[i]] = id[i + 1]; nx[pr[id[1]] = id[n]] = id[1]; for (register int i = 1; i <= n; ++i) se.insert(make_pair(get(i, nx[i]), i)); while (1) { set<pair<int, int> >::iterator it = se.begin(); if (it->first >= 1000000005) break; int u = it->second, t = it->first; se.erase(it); se.erase(make_pair(get(pr[u], u), pr[u])), se.erase(make_pair(get(nx[u], nx[nx[u]]), nx[u])); pr[nx[u] = nx[nx[u]]] = u; p[u] = (p[u] + t) % m, --v[u]; se.insert(make_pair(get(pr[u], u), pr[u])), se.insert(make_pair(get(u, nx[u]), u)); } printf( %d n , se.size()); for (auto v : se) printf( %d , v.second); puts( ); return 0; } |
#include bits/stdc++.h using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; #define int ll #define sz(a) int(a.size()) #define dbg(a) cout << #a << = << a << endl #define dbg1(a) cout << #a << = [ ; for (auto& _ : a) cout << _ << ; cout << ] << endl #define dbg2(a) cout << #a << = [ ; for (auto& _ : a) cout << ( << _.first << << _.second << ) ; cout << ] << endl const int inf = int(1e9) + 7; void solve() { int n; cin >> n; bool ok = false; for (int i = 0; i < n; ++i) { int q; cin >> q; ok |= q != int(sqrt(q)) * int(sqrt(q)); } cout << (ok ? YES n : NO n ); } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); int tests = 1; cin >> tests; while (tests--) { solve(); } } |
#include <bits/stdc++.h> using namespace std; int main(void) { int n, a[12][12]; cin >> n; memset(a, 0, sizeof(a)); for (int i = 1; i <= n; i++) { a[1][i] = 1; a[i][1] = 1; } for (int i = 2; i <= n; i++) { for (int j = 2; j <= n; j++) { a[i][j] = a[i - 1][j] + a[i][j - 1]; } } cout << a[n][n]; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; vector<pair<int, int>> v(n); vector<int> v2(k); for (int i = 0; i < n; i++) { cin >> v[i].first; v[i].second = i + 1; } sort(v.begin(), v.end()); int sum = 0; for (int i = 0; i < k; i++) { sum += v[n - 1 - i].first; v2[i] = v[n - 1 - i].second; } cout << sum << endl; sort(v2.begin(), v2.end()); for (int i = k - 1; i > 0; i--) { v2[i] = v2[i] - v2[i - 1]; } int cnt = 0; for (int i = 0; i < k - 1; i++) { cout << v2[i] << ; cnt += v2[i]; } cout << n - cnt; return 0; } |
#pragma GCC optimize ( Ofast ) #include<bits/stdc++.h> using namespace std; void*wmem; char memarr[96000000]; template<class S, class T> inline S min_L(S a,T b){ return a<=b?a:b; } template<class T> inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16] = {0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } template<class T> inline void walloc1d(T **arr, int x1, int x2, void **mem = &wmem){ walloc1d(arr, x2-x1, mem); (*arr) -= x1; } template<class T1> void sortA_L(int N, T1 a[], void *mem = wmem){ sort(a, a+N); } template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){ int i; pair<T1, T2>*arr; walloc1d(&arr, N, &mem); for(i=(0);i<(N);i++){ arr[i].first = a[i]; arr[i].second = b[i]; } sort(arr, arr+N); for(i=(0);i<(N);i++){ a[i] = arr[i].first; b[i] = arr[i].second; } } inline int my_getchar(){ static char buf[1048576]; static int s = 1048576; static int e = 1048576; if(s == e && e == 1048576){ e = fread(buf, 1, 1048576, stdin); s = 0; } if(s == e){ return EOF; } return buf[s++]; } inline void rd(int &x){ int k; int m=0; x=0; for(;;){ k = my_getchar(); if(k== - ){ m=1; break; } if( 0 <=k&&k<= 9 ){ x=k- 0 ; break; } } for(;;){ k = my_getchar(); if(k< 0 ||k> 9 ){ break; } x=x*10+k- 0 ; } if(m){ x=-x; } } inline void rd(long long &x){ int k; int m=0; x=0; for(;;){ k = my_getchar(); if(k== - ){ m=1; break; } if( 0 <=k&&k<= 9 ){ x=k- 0 ; break; } } for(;;){ k = my_getchar(); if(k< 0 ||k> 9 ){ break; } x=x*10+k- 0 ; } if(m){ x=-x; } } inline int rd_int(void){ int x; rd(x); return x; } struct MY_WRITER{ char buf[1048576]; int s; int e; MY_WRITER(){ s = 0; e = 1048576; } ~MY_WRITER(){ if(s){ fwrite(buf, 1, s, stdout); } } } ; MY_WRITER MY_WRITER_VAR; void my_putchar(int a){ if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){ fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout); MY_WRITER_VAR.s = 0; } MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a; } inline void wt_L(char a){ my_putchar(a); } inline void wt_L(int x){ int s=0; int m=0; char f[10]; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ my_putchar( - ); } while(s--){ my_putchar(f[s]+ 0 ); } } template<class S> inline void arrInsert(const int k, int &sz, S a[], const S aval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } a[k] = aval; } template<class S, class T> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } a[k] = aval; b[k] = bval; } template<class S, class T, class U> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; } template<class S, class T, class U, class V> inline void arrInsert(const int k, int &sz, S a[], const S aval, T b[], const T bval, U c[], const U cval, V d[], const V dval){ int i; sz++; for(i=sz-1;i>k;i--){ a[i] = a[i-1]; } for(i=sz-1;i>k;i--){ b[i] = b[i-1]; } for(i=sz-1;i>k;i--){ c[i] = c[i-1]; } for(i=sz-1;i>k;i--){ d[i] = d[i-1]; } a[k] = aval; b[k] = bval; c[k] = cval; d[k] = dval; } template<class S, class T> inline S chmin(S &a, T b){ if(a>b){ a=b; } return a; } template<class S, class T> inline S chmax(S &a, T b){ if(a<b){ a=b; } return a; } int N; int C; int Q; int A[30000]; int X; long long Y; long long cnt[5][30000+2]; int sz; int lis[5]; int ind[5]; int usz[30000+2]; int ulis[30000+2][5]; int dsz[30000+2]; int dlis[30000+2][5]; long long skipL[5][30000+2]; long long skipR[5][30000+2]; long long skipV[5][30000+2]; int skip = 150; long long skip2L[5][30000+2]; long long skip2R[5][30000+2]; long long skip2V[5][30000+2]; int skip2 = 30; int main(){ int t_ynMSdg; wmem = memarr; int i; int j; int k; int c; for(i=(0);i<(5);i++){ cnt[i][0] = 1; } for(i=(0);i<(5);i++){ for(k=(0);k<(30000);k++){ for(j=(0);j<(min_L(k, i)+1);j++){ cnt[i][k+1] += cnt[i-j][k-j]; if(cnt[i][k+1] > 2000000000000000000LL){ cnt[i][k+1] = 2000000000000000000LL; } } } } int KrdatlYV = rd_int(); for(t_ynMSdg=(0);t_ynMSdg<(KrdatlYV);t_ynMSdg++){ int dtiCQK_a; rd(N); rd(C); rd(Q); { int a2conNHc; for(a2conNHc=(0);a2conNHc<(N);a2conNHc++){ rd(A[a2conNHc]); } } for(k=(0);k<(N);k++){ sz = 0; for(i=(0);i<(C+1);i++){ if(k+i < N){ arrInsert(sz, sz, ind, i, lis, A[k+i]); } } sortA_L(sz, lis, ind); j = usz[k] = dsz[k] = 0; for(i=(0);i<(sz);i++){ if(ind[i] == 0){ j++; continue; } if(j==0){ ulis[k][usz[k]++] = ind[i]; } if(j==1){ dlis[k][dsz[k]++] = ind[i]; } } } for(k=(0);k<(N);k++){ for(c=(0);c<(C+1);c++){ long long sm = 0; skipL[c][k] = -4611686016279904256LL; skipR[c][k] = 4611686016279904256LL; if(k+skip+5 >= N){ swap(skipL[c][k], skipR[c][k]); continue; } for(j=(k);j<(k+skip);j++){ for(i=(0);i<(usz[j]);i++){ if(c >= ulis[j][i]){ sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1]; } } if(sm > 4611686016279904256LL){ sm = 2000000000000000000LL; } chmax(skipL[c][k], sm); chmin(skipR[c][k], sm + cnt[c][N-j-1]); } skipV[c][k] = sm; } } for(k=(0);k<(N);k++){ for(c=(0);c<(C+1);c++){ long long sm = 0; skip2L[c][k] = -4611686016279904256LL; skip2R[c][k] = 4611686016279904256LL; if(k+skip2+5 >= N){ swap(skip2L[c][k], skip2R[c][k]); continue; } for(j=(k);j<(k+skip2);j++){ for(i=(0);i<(usz[j]);i++){ if(c >= ulis[j][i]){ sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1]; } } if(sm > 4611686016279904256LL){ sm = 2000000000000000000LL; } chmax(skip2L[c][k], sm); chmin(skip2R[c][k], sm + cnt[c][N-j-1]); } skip2V[c][k] = sm; } } for(dtiCQK_a=(0);dtiCQK_a<(Q);dtiCQK_a++){ rd(X);X += (-1); rd(Y);Y += (-1); if(Y >= cnt[C][N]){ wt_L(-1); wt_L( n ); continue; } c = C; for(k=(0);k<(N);k++){ if(skipL[c][k] <= Y && Y < skipR[c][k]){ if(X < k + skip){ wt_L(A[X]); wt_L( n ); break; } Y -= skipV[c][k]; k += skip - 1; continue; } if(skip2L[c][k] <= Y && Y < skip2R[c][k]){ if(X < k + skip2){ wt_L(A[X]); wt_L( n ); break; } Y -= skip2V[c][k]; k += skip2 - 1; continue; } for(i=(0);i<(usz[k]);i++){ if(c >= ulis[k][i]){ if(Y < cnt[c-ulis[k][i]][N-k-ulis[k][i]-1]){ if(X <= k + ulis[k][i]){ wt_L(A[k+ulis[k][i]-(X-k)]); wt_L( n ); goto qE8LMwYZ; } c -= ulis[k][i]; k += ulis[k][i]; goto lQU550vz; } else{ Y -= cnt[c-ulis[k][i]][N-k-ulis[k][i]-1]; } } } if(Y < cnt[c][N-k-1]){ if(X == k){ wt_L(A[k]); wt_L( n ); break; } continue; } else{ Y -= cnt[c][N-k-1]; } for(i=(0);i<(dsz[k]);i++){ if(c >= dlis[k][i]){ if(Y < cnt[c-dlis[k][i]][N-k-dlis[k][i]-1]){ if(X <= k + dlis[k][i]){ wt_L(A[k+dlis[k][i]-(X-k)]); wt_L( n ); goto qE8LMwYZ; } c -= dlis[k][i]; k += dlis[k][i]; goto lQU550vz; } else{ Y -= cnt[c-dlis[k][i]][N-k-dlis[k][i]-1]; } } } lQU550vz:; } qE8LMwYZ:; } } return 0; } // cLay version 20210103-1 // --- original code --- // //no-unlocked // int N, C, Q, A[3d4], X; ll Y; // ll cnt[5][3d4+2]; // int sz, lis[5], ind[5]; // int usz[3d4+2], ulis[3d4+2][5]; // int dsz[3d4+2], dlis[3d4+2][5]; // ll skipL[5][3d4+2], skipR[5][3d4+2], skipV[5][3d4+2]; int skip = 150; // ll skip2L[5][3d4+2], skip2R[5][3d4+2], skip2V[5][3d4+2]; int skip2 = 30; // { // int i, j, k, c; // rep(i,5) cnt[i][0] = 1; // rep(i,5) rep(k,3d4) rep(j,min(k,i)+1){ // cnt[i][k+1] += cnt[i-j][k-j]; // if(cnt[i][k+1] > 2d18) cnt[i][k+1] = 2d18; // } // REP(rd_int()){ // rd(N,C,Q,A(N)); // // rep(k,N){ // sz = 0; // rep(i,C+1) if(k+i < N) arrInsert(sz, sz, ind, i, lis, A[k+i]); // sortA(sz, lis, ind); // j = usz[k] = dsz[k] = 0; // rep(i,sz){ // if(ind[i] == 0) j++, continue; // if(j==0) ulis[k][usz[k]++] = ind[i]; // if(j==1) dlis[k][dsz[k]++] = ind[i]; // } // } // // rep(k,N) rep(c,C+1){ // ll sm = 0; // skipL[c][k] = -ll_inf; // skipR[c][k] = ll_inf; // if(k+skip+5 >= N) swap(skipL[c][k], skipR[c][k]), continue; // rep(j,k,k+skip){ // rep(i,usz[j]) if(c >= ulis[j][i]) sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1]; // if(sm > ll_inf) sm = 2d18; // skipL[c][k] >?= sm; // skipR[c][k] <?= sm + cnt[c][N-j-1]; // } // skipV[c][k] = sm; // } // // rep(k,N) rep(c,C+1){ // ll sm = 0; // skip2L[c][k] = -ll_inf; // skip2R[c][k] = ll_inf; // if(k+skip2+5 >= N) swap(skip2L[c][k], skip2R[c][k]), continue; // rep(j,k,k+skip2){ // rep(i,usz[j]) if(c >= ulis[j][i]) sm += cnt[c-ulis[j][i]][N-j-ulis[j][i]-1]; // if(sm > ll_inf) sm = 2d18; // skip2L[c][k] >?= sm; // skip2R[c][k] <?= sm + cnt[c][N-j-1]; // } // skip2V[c][k] = sm; // } // // rep(Q){ // rd(X--, Y--); // if(Y >= cnt[C][N]) wt(-1), continue; // c = C; // rep(k,N){ // if(skipL[c][k] <= Y < skipR[c][k]){ // if(X < k + skip) wt(A[X]), break; // Y -= skipV[c][k]; // k += skip - 1; // continue; // } // if(skip2L[c][k] <= Y < skip2R[c][k]){ // if(X < k + skip2) wt(A[X]), break; // Y -= skip2V[c][k]; // k += skip2 - 1; // continue; // } // rep(i,usz[k]) if(c >= ulis[k][i]){ // if(Y < cnt[c-ulis[k][i]][N-k-ulis[k][i]-1]){ // if(X <= k + ulis[k][i]) wt(A[k+ulis[k][i]-(X-k)]), break_break; // c -= ulis[k][i]; // k += ulis[k][i]; // break_continue; // } else { // Y -= cnt[c-ulis[k][i]][N-k-ulis[k][i]-1]; // } // } // if(Y < cnt[c][N-k-1]){ // if(X == k) wt(A[k]), break; // continue; // } else { // Y -= cnt[c][N-k-1]; // } // rep(i,dsz[k]) if(c >= dlis[k][i]){ // if(Y < cnt[c-dlis[k][i]][N-k-dlis[k][i]-1]){ // if(X <= k + dlis[k][i]) wt(A[k+dlis[k][i]-(X-k)]), break_break; // c -= dlis[k][i]; // k += dlis[k][i]; // break_continue; // } else { // Y -= cnt[c-dlis[k][i]][N-k-dlis[k][i]-1]; // } // } // } // } // } // } |
#include <bits/stdc++.h> using namespace std; int n; struct Trie { int trie[6000005][2]; int cnt[6000005]; int root = 0; long long val[6000005]; long long xorsum = 0; long long ans[6000005 / 10]; int C = 0; int newroot() { trie[C][0] = trie[C][1] = -1; cnt[C] = 0; return C++; } void Insert(long long x) { long long p = root; for (int i = 60; i >= 0; i--) { long long id = (x >> i) & 1; if (trie[p][id] == -1) trie[p][id] = newroot(); p = trie[p][id]; cnt[p]++; } val[p] = x; } bool check(long long xorsum, long long &res, int index, bool limit, int dep) { if (index != root && (cnt[index] == 0 || index == -1)) return false; if (dep == -1) { res = val[index]; cnt[index]--; return true; } int id = (xorsum >> dep) & 1; if (id == 0) { bool ret = (check(xorsum, res, trie[index][0], limit, dep - 1)) || (!limit && check(xorsum, res, trie[index][1], limit, dep - 1)); if (ret == 0) return false; cnt[index]--; return true; } else { bool ret = (check(xorsum, res, trie[index][0], limit, dep - 1)) || (check(xorsum, res, trie[index][1], false, dep - 1)); if (ret == 0) return false; cnt[index]--; return true; } } void solve() { for (int i = n - 1; i >= 0; i--) { long long res; bool ret = check(xorsum, res, root, true, 60); if (ret == 0) { puts( No ); return; } ans[i] = res; assert(xorsum > (xorsum ^ res)); xorsum ^= res; } puts( Yes ); for (int i = 0; i < n; i++) { cout << ans[i] << ; } puts( ); } } Tree; int main() { scanf( %d , &n); Tree.root = Tree.newroot(); for (int i = 0; i < n; i++) { long long x; scanf( %I64d , &x); Tree.Insert(x); Tree.xorsum ^= x; } Tree.solve(); } |
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << = << a << endl; err(++it, args...); } long long powm(long long a, long long b, long long mod) { long long res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; } const long long mod = 1e9 + 7; const long long N = 1000005; const long long inf = 1e9; void solve() { int n; cin >> n; deque<long long> dq; long long ans = 0; while (n--) { long long x; cin >> x; if (dq.size() == 0) { dq.push_back(x); continue; } if (dq.size() > 0) ans = max(ans, (dq.back() ^ x)); while (dq.size() > 0 and dq.back() < x) { ans = max(ans, (x ^ dq.back())); dq.pop_back(); } if (dq.size() > 0) ans = max(ans, (dq.back() ^ x)); dq.push_back(x); } long long curr = dq.back(); dq.pop_back(); while (!dq.empty()) { ans = max(ans, (curr ^ dq.back())); curr = dq.back(); dq.pop_back(); } cout << ans << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); int tc = 1; while (tc--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; const double g = 9.8; const double eps = 1e-9; int n, m, v; struct Ques { double angle; int id; } ques[10010]; struct Wall { double x, y; } wall[100010]; double ans_x[10010], ans_y[10010]; bool cmp(const Ques &a, const Ques &b) { return a.angle < b.angle; } bool cmp2(const Wall &a, const Wall &b) { return a.x < b.x; } int main() { scanf( %d%d , &n, &v); for (int i = 1; i <= n; i++) { scanf( %lf , &ques[i].angle); ques[i].id = i; } scanf( %d , &m); for (int i = 1; i <= m; i++) scanf( %lf%lf , &wall[i].x, &wall[i].y); sort(ques + 1, ques + n + 1, cmp); sort(wall + 1, wall + m + 1, cmp2); int h = 1; for (int i = 1; i <= n; i++) { double v_y = v * sin(ques[i].angle), v_x = v * cos(ques[i].angle); while (h <= m) { double time = wall[h].x / v_x; double y = v_y * time - 0.5 * g * time * time; if (y < eps) { ans_x[ques[i].id] = v_x * (v_y / (0.5 * g)); ans_y[ques[i].id] = 0; break; } else if (y < wall[h].y + eps) { ans_x[ques[i].id] = wall[h].x; ans_y[ques[i].id] = y; break; } else h++; } } for (int i = 1; i <= n; i++) if (ans_x[ques[i].id] < eps) { double v_y = v * sin(ques[i].angle), v_x = v * cos(ques[i].angle); ans_x[ques[i].id] = v_x * (v_y / (0.5 * g)); ans_y[ques[i].id] = 0; } for (int i = 1; i <= n; i++) printf( %.9lf %.9lf n , ans_x[i], ans_y[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; k--; int a[n]; int sum = 0; for (int i = 0; i < n; ++i) { cin >> a[i]; } if (a[k] == 1) sum++; for (int i = 1; i < n; ++i) { if (k - i < 0) { for (int j = k + i; j < n; ++j) { sum += a[j]; } break; } if (k + i >= n) { for (int j = k - i; j >= 0; --j) { sum += a[j]; } break; } if (a[k - i] == 1 && a[k + i] == 1) sum += 2; } cout << sum; return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-6; const int mod = 1000000007; inline string getstr(string &s, int l, int r) { string ret = ; for (int i = l; i <= r; i++) ret.push_back(s[i]); return ret; } int modpow(int x, int y, int md = mod) { if (y == 0) return 1; int ret = modpow(x, y >> 1, md); ret = (long long)ret * ret % md; if (y & 1) ret = (long long)ret * x % md; return ret; } string s, t; int n; int main() { cin >> n >> s >> t; int l, r; for (l = 0;; l++) { if (s[l] != t[l]) { break; } } for (r = n - 1;; r--) { if (s[r] != t[r]) { break; } } int lft = 1, rgt = 1; for (int i = l; i < r; i++) { if (s[i] != t[i + 1]) lft = 0; if (s[i + 1] != t[i]) rgt = 0; } cout << lft + rgt << endl; return 0; } |
#include <bits/stdc++.h> const int nax = 1e5 + 5; using namespace std; int gcd(int x, int y) { if (x % y == 0) return y; return gcd(y, x % y); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, m; cin >> n >> m; vector<int> a(n + 1), b(m + 1); for (int i = 0; i <= n; i++) { cin >> a[i]; } for (int i = 0; i <= m; i++) { cin >> b[i]; } if (n != m) { if (n > m) { if ((a[0] > 0 && b[0] > 0) || (a[0] < 0 && b[0] < 0)) { puts( Infinity ); } else { puts( -Infinity ); } } else { cout << 0/1 << n ; } return 0; } else { int ans = gcd(a[0], b[0]); if ((a[0] > 0 && b[0] > 0) || (a[0] < 0 && b[0] < 0)) cout << a[0] / ans << / << b[0] / ans << n ; else cout << -abs(a[0] / ans) << / << abs(b[0] / ans) << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; bool check(int p) { if (p == 2) return 1; if (p % 2 == 0) return 0; for (long long int i = 3; i * i <= p; i += 2) { if (p % i == 0) return 0; } if (p > 2) return 1; return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { int n; cin >> n; if (check(n)) { cout << 1 << n ; cout << n << n ; continue; } if (check(n - 2)) { cout << 2 << n ; cout << 2 << << n - 2 << n ; continue; } cout << 3 << n ; int a; for (int i = n; i > 3; i--) { if (check(i)) { a = i; n -= a; break; } } cout << a << ; for (int i = 2; i <= n; i++) { if (check(i) && check(n - i)) { cout << i << << n - i << n ; break; } } } return 0; } |
#include <bits/stdc++.h> using namespace std; map<int, int> m; bool fd(int x, int y) { if (x == y) return m[y] >= 2; return m.find(y) != m.end(); } int main() { int n; cin >> n; for (int i = 0; i < (n); i++) { int x; cin >> x; m[x]++; } int cnt = 0; for (auto& xp : m) { int x = xp.first; for (int s = 1; s <= 30; s++) { if ((1 << s) <= x) continue; if (fd(x, (1 << s) - x)) goto cont; } cnt += xp.second; cont:; } cout << cnt << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, k, l, a, v, b, n, m; string s; while (cin >> s) { k = 0; j = 0; l = s.size(); for (i = 0; i < l; i++) { if (s[i] == W && s[i + 1] == U && s[i + 2] == B ) { i = i + 2; if (k > 0) { if (j == 1) cout << ; j = 0; } } else { cout << s[i]; j = 1; } k = 1; } } } |
#include <bits/stdc++.h> using namespace std; inline int read(); int a[14250]; unsigned long long ans; bool cmp(int a, int b) { return a < b; } int main() { int n = read(), k = read(); for (int i = 1; i <= n; i++) { a[i] = read(); } sort(a + 1, a + n + 1, cmp); for (int i = 1; i <= k; i++) { ans += a[i]; } cout << ans; return 0; } inline int read() { int x = 0, w = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) w = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * w; } |
#include <bits/stdc++.h> using namespace std; vector<int> adj[1000009]; int in[1000009], out[1000009]; bool vs[22][1000009]; void dfs(int x, int n) { vs[x][n] = 1; for (auto& it : adj[n]) if (!vs[x][it]) dfs(x, it); } int main() { int N, M; scanf( %d%d , &N, &M); while (M--) { int u, v; scanf( %d%d , &u, &v); adj[u].push_back(v); in[v]++; out[u]++; } vector<int> src, snk; for (int i = 1; i <= N; i++) { if (in[i] == 0) src.push_back(i); if (out[i] == 0) snk.push_back(i); } int s = src.size(); for (int i = 0; i < s; i++) { dfs(i, src[i]); } for (int i = 1; i < (1 << s) - 1; i++) { set<int> st; int c = 0; for (int j = 0; j < s; j++) { if (i & (1 << j)) { ++c; for (auto& it : snk) if (vs[j][it]) st.insert(it); } } if (c >= (int)st.size()) return !printf( NO ); } puts( YES ); return 0; } |
#include <bits/stdc++.h> using namespace std; char z[500005]; int Qs1[500005], b, Qs2[500005]; int f(char a) { if (a == w ) return b + 1; return 1; } int main() { int i, j, n, a, t, Max = 0; int *qs2 = Qs2 + 1, *qs1 = Qs1 + 1; scanf( %d %d %d %d , &n, &a, &b, &t); gets(z); qs1[0] = f(z[0]); for (i = 1; i < n; i++) { qs1[i] = a + f(z[i]) + qs1[i - 1]; } for (i = n - 1; i >= 0; i--) { qs2[n - 1 - i] = a + f(z[i]) + qs2[n - 2 - i]; } for (i = 0; i < n; i++) { if (qs1[i] <= t) Max = max(min(i + 1, n), Max); if (qs2[i] + qs1[0] <= t) Max = max(min(i + 2, n), Max); } for (i = 0; i < n; i++) { int tmp = lower_bound(qs2, qs2 + n, t - qs1[i] - a * i) - qs2; if (t - qs1[i] - a * i < qs2[tmp]) tmp--; if (tmp == -1) continue; Max = max(min(tmp + i + 2, n), Max); } for (i = 0; i < n; i++) { int tmp = lower_bound(qs1, qs1 + n, t - qs2[i] - a * i - a) - qs1; if (t - qs2[i] - a * i - a < qs1[tmp]) tmp--; if (tmp == -1) continue; Max = max(min(tmp + i + 2, n), Max); } printf( %d , Max); return 0; } |
#include <bits/stdc++.h> using namespace std; int a[105][105], c[105], r[105]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { c[j] += a[i][j]; r[i] += a[i][j]; } int co = 0; set<int> sc, sr; set<int>::iterator it; while (co <= 10) { for (int i = 1; i <= n; i++) if (r[i] < 0) { if (sr.find(i) != sr.end()) sr.erase(sr.find(i)); else sr.insert(i); r[i] = -r[i]; for (int j = 1; j <= m; j++) c[j] -= 2 * a[i][j]; for (int j = 1; j <= m; j++) a[i][j] = -a[i][j]; } for (int j = 1; j <= m; j++) if (c[j] < 0) { if (sc.find(j) != sc.end()) sc.erase(sc.find(j)); else sc.insert(j); c[j] = -c[j]; for (int i = 1; i <= n; i++) r[i] -= 2 * a[i][j]; for (int i = 1; i <= n; i++) a[i][j] = -a[i][j]; } co++; } cout << sr.size() << ; for (it = sr.begin(); it != sr.end(); it++) cout << *it << ; cout << endl; cout << sc.size() << ; for (it = sc.begin(); it != sc.end(); it++) cout << *it << ; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long N = 2e5 + 5; const double pi = acos(-1.0); long long addM(long long a, long long b) { return (a + b) % MOD; } long long multM(long long a, long long b) { return (a * b) % MOD; } long long binpow(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; } long long ModInverse(long long n) { return binpow(n, MOD - 2); } vector<long long> findFact() { vector<long long> fact(N); fact[0] = 1; for (long long i = 1; i <= N; i++) { fact[i] = multM(i, fact[i - 1]); } return fact; } long long ncr(long long n, long long r) { vector<long long> fact = findFact(); return ((fact[n] * ModInverse(fact[n - r])) % MOD * ModInverse(fact[r])) % MOD; } vector<bool> findPrime() { vector<bool> is_prime(N + 1, true); is_prime[0] = false; is_prime[1] = false; for (long long i = 2; i <= N; i++) { if (is_prime[i] && i * i <= N) { for (long long j = i * i; j <= N; j += i) is_prime[j] = false; } } return is_prime; } vector<bool> segmentedSieveNoPreGen(long long L, long long R) { vector<bool> isPrime(R - L + 1, true); long long lim = sqrt(R); for (long long i = 2; i <= lim; ++i) for (long long j = max(i * i, (L + i - 1) / i * i); j <= R; j += i) isPrime[j - L] = false; if (L == 1) isPrime[0] = false; return isPrime; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long tt; cin >> tt; while (tt--) { long long n; cin >> n; string x, y; cin >> x >> y; bool check = true; for (long long i = 0; i < n; i++) { if (x[i] > y[i]) { check = false; } } if (check) { long long ans = 0; for (char c = a ; c <= z ; c++) { char mn = z ; bool check = false; for (long long i = 0; i < n; i++) { if (x[i] == c && x[i] != y[i]) { check = true; mn = min(mn, y[i]); } } for (long long i = 0; i < n; i++) { if (x[i] == c && x[i] != y[i]) { x[i] = mn; } } if (check) ans++; } cout << ans << n ; } else { cout << -1 n ; } } return 0; } |
#include <bits/stdc++.h> #pragma GCC target( avx2 ) #pragma GCC optimization( O3 ) #pragma GCC optimization( unrolled-loops ) using namespace std; const long long mod = 1e9 + 7; int test = 1; const int N = 100100; long long n; int k = 1; long long x = 1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; if (n > 9) { n -= 9; k++; x *= 10; } if (n > 180) { n -= 180; k++; x *= 10; } if (n > 2700) { n -= 2700; k++; x *= 10; } if (n > 36000) { n -= 36000; k++; x *= 10; } if (n > 450000) { n -= 450000; k++; x *= 10; } if (n > 5400000) { n -= 5400000; k++; x *= 10; } if (n > 63000000) { n -= 63000000; k++; x *= 10; } if (n > 720000000) { n -= 720000000; k++; x *= 10; } if (n > 8100000000) { n -= 8100000000; k++; x *= 10; } if (n > 90000000000) { n -= 90000000000; k++; x *= 10; } if (n > 990000000000) { n -= 990000000000; k++; x *= 10; } long long m, l; m = n / k; l = n % k; x += m - 1; if (l == 0) cout << x % 10; else { x++; while (k > l) { k--; x /= 10; } cout << x % 10; } } |
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 10; int a[N], nx[N]; long long pr[N]; int main() { int n, q; scanf( %d %d , &n, &q); for (int i = 1; i <= n; i++) scanf( %d , &a[i]), a[i + n] = a[i]; for (int i = 1; i <= n + n; i++) pr[i] = pr[i - 1] + a[i]; while (q--) { long long b; scanf( %lld , &b); int now = 1; int mx = n + n + 1, id; for (int i = 1; i <= n + n; i++) { while (now <= n + n && pr[i - 1] + b >= pr[now]) now++; nx[i] = now; if (nx[i] - i >= n) nx[i] = i + n; if (nx[i] <= n + n && mx > nx[i] - i) { mx = nx[i] - i; id = i; } } int ans = n + n; for (int i = id; i < nx[id]; i++) { int p = i; int res = 0; while (p < i + n) { res++; if (nx[p] > i + n && res > 1) { if (pr[i + n - 1] - pr[p - 1] + pr[nx[i] - 1] - pr[i - 1] <= b) res--; } p = nx[p]; } ans = min(ans, res); } printf( %d n , ans); } } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, q; char s[N]; struct node { int s, x, z, xz, zy, d; } tr[N << 2]; void upd(int o) { int S = tr[(o << 1)].s, lx = tr[(o << 1)].x, lz = tr[(o << 1)].z, lxz = tr[(o << 1)].xz, lzy = tr[(o << 1)].zy; int rx = tr[(o << 1 | 1)].x + S, rz = tr[(o << 1 | 1)].z - 2 * S, rxz = tr[(o << 1 | 1)].xz - S, rzy = tr[(o << 1 | 1)].zy - S; tr[o].s = S + tr[(o << 1 | 1)].s; tr[o].x = max(lx, rx), tr[o].z = max(lz, rz); tr[o].xz = max(max(lxz, rxz), lx + rz); tr[o].zy = max(max(lzy, rzy), lz + rx); tr[o].d = max(max(tr[(o << 1)].d, tr[(o << 1 | 1)].d), max(lxz + rx, lx + rzy)); } void mdf(int o, int l, int r, int p, int c) { if (l == r) { tr[o] = {c, c, -2 * c, -c, -c, 0}; return; } int mid = (l + r) >> 1; if (p <= mid) mdf((o << 1), l, mid, p, c); else mdf((o << 1 | 1), mid + 1, r, p, c); upd(o); } int main() { scanf( %d%d%s , &n, &q, s + 1); for (int i = (1); i <= (2 * n - 2); ++i) mdf(1, 1, 2 * n - 2, i, s[i] == ( ? 1 : -1); printf( %d n , tr[1].d); while (q--) { int x, y; scanf( %d%d , &x, &y); swap(s[x], s[y]); mdf(1, 1, 2 * n - 2, x, s[x] == ( ? 1 : -1); mdf(1, 1, 2 * n - 2, y, s[y] == ( ? 1 : -1); printf( %d n , tr[1].d); } return 0; } |
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-11; long long n, k; bool solveIt(long long d1, long long d2) { if ((k - d1 + d2) % 3 != 0) return false; long long Y = (k - d1 + d2) / 3; long long X = d1 + Y; long long Z = Y - d2; if (X >= 0 && Y >= 0 && Z >= 0 && X <= k && Y <= k && Z <= k && X <= (n / 3) && Y <= (n / 3) && Z <= (n / 3) && abs(X - Y) == abs(d1) && abs(Y - Z) == abs(d2)) return true; return false; } int main() { std::ios_base::sync_with_stdio(0); long long t, d2, d1; cin >> t; while (t--) { bool flg = false; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << no << endl; continue; } if (solveIt(d1, d2)) flg = true; if (solveIt(d1, -d2)) flg = true; if (solveIt(-d1, d2)) flg = true; if (solveIt(-d1, -d2)) flg = true; if (flg) cout << yes << endl; else cout << no << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; vector<pair<long long, long long> > v; for (int i = 0; i < m; i++) { long long a, b; cin >> b >> a; v.push_back(make_pair(a, b)); } sort(v.rbegin(), v.rend()); long long total = 0, i = 0, count = 0; while (i < m && (count + v[i].second) <= n) { total += v[i].first * v[i].second; count += v[i].second; i++; } if (i != m) total += v[i].first * (n - count); cout << total << endl; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int MAX = 1000005; int SetBit(int n, int x) { return n | (1 << x); } int ClearBit(int n, int x) { return n & ~(1 << x); } int ToggleBit(int n, int x) { return n ^ (1 << x); } bool CheckBit(int n, int x) { return (bool)(n & (1 << x)); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int w, y, max; cin >> w >> y; int a; if (w > y) { max = w; } else if (w == y) { max = w; } else { max = y; } if ((w == 6 && y == 6)) { cout << 1 << / << 6 << endl; } else if ((w == 6 && (y == 1 || y == 2 || y == 3 || y == 4 || y == 5))) { cout << 1 << / << 6 << endl; } else if ((y == 6 && (w == 1 || w == 2 || w == 3 || w == 4 || w == 5))) { cout << 1 << / << 6 << endl; } else { a = 6 - max + 1; if (a == 2) { cout << 1 << / << 3 << endl; } if (a == 3) { cout << 1 << / << 2 << endl; } if (a == 4) { cout << 2 << / << 3 << endl; } if (a == 5 || a == 1) { cout << a << / << 6 << endl; } if (a == 6) { cout << 1 << / << 1 << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int max(int &a, int &b) { return (a > b ? a : b); } unsigned long long max(unsigned long long &a, unsigned long long &b) { return (a > b ? a : b); } int min(int &a, int &b) { return (a < b ? a : b); } long long int bits_needed(long long int val) { long long int count = 0; while (val) count++, val >>= 1; return count; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 0, n = 0, i = 0; long long int val = 0, prev = 0, ans = 0; cin >> t; while (t-- > 0) { ans = 0; cin >> n; cin >> prev; for (i = 1; i < n; i++) { cin >> val; if (val < prev) { ans = max(ans, bits_needed(prev - val)); } else prev = val; } cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, d, sum, ans; char s[105]; int main() { scanf( %d %d , &n, &d); for (int i = 0; i < d; i++) { scanf( %s , s); bool ok = true; for (int j = 0; j < n; j++) if (s[j] == 0 ) ok = false; if (ok) sum = 0; else sum++, ans = max(ans, sum); } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 5000005; using ll = long long; ll f[N], pre[N]; int n, a[N]; vector<int> ans; void move(int x) { if (x == n || a[x] == 0 || a[x + 1] == 0) return; ans.push_back(x); int t = min(a[x], a[x + 1]); a[x] -= t; a[x + 1] -= t; } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; f[2] = a[1]; for (int i = 3; i <= n + 1; i++) if (f[i - 3] + max(a[i - 1], a[i - 2]) < f[i - 2] + a[i - 1]) f[i] = f[pre[i] = i - 3] + max(a[i - 1], a[i - 2]); else f[i] = f[pre[i] = i - 2] + a[i - 1]; for (int i = f[n] < f[n + 1] ? n : n + 1; i > 1; i = pre[i]) if (pre[i] == i - 3) { move(i - 2), move(i - 1); if (i > 3) move(i - 3); } else { move(i - 1); if (i > 2) move(i - 2); } cout << ans.size() << endl; for (auto &i : ans) cout << i << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; inline int ReadInt() { int i = 0; char c = 0; char flag = 1; while ((c < 0 || c > 9 ) && c != - ) c = getchar(); if (c == - ) { flag = 0; c = getchar(); } for (; c >= 0 && c <= 9 ; c = getchar()) i = i * 10 + c - 0 ; if (!flag) i = -i; return i; } inline void WriteInt(int n) { if (n < 0) { putchar( - ); WriteInt(-n); return; } if (n < 10) putchar(n + 0 ); else { WriteInt(n / 10); putchar(n % 10 + 0 ); } } int n, m; int top; long long ans, sum, last; int A[100100]; int tmp; int main() { scanf( %i%i , &n, &m); for (int i = 0; i < n; i++) scanf( %i , &A[i]), sum += A[i]; sort(A, A + n); int top = 0; for (int i = 0; i < n; i++) if (A[i] > top) ans++, top++; else ans++; ans += A[n - 1] - top; printf( %I64d , sum - ans); putc( n , stdout); } |
#include <bits/stdc++.h> using namespace std; int Matrix[1002][2002]; int max(int a, int b) { if (a > b) return a; else return b; } int min(int a, int b) { if (a < b) return a; else return b; } int distance(int a, int b, int c) { int first, second; first = a < b ? b - a : a - b; second = b < c ? c - b : b - c; return first + second; } int main() { int n, k, p; int person[1005]; int key[2002]; cin >> n >> k >> p; for (int i = 1; i <= n; i++) cin >> person[i]; for (int i = 1; i <= k; i++) cin >> key[i]; sort(person + 1, person + n + 1); sort(key + 1, key + k + 1); for (int i = 1; i <= n; i++) { for (int j = i; j <= k; j++) { if (i == j) { Matrix[i][j] = max(Matrix[i - 1][j - 1], distance(person[i], key[i], p)); } else { Matrix[i][j] = min(Matrix[i][j - 1], max(Matrix[i - 1][j - 1], distance(person[i], key[j], p))); } } } int ans = 2e9; for (int i = n; i <= k; i++) ans = min(ans, Matrix[n][i]); cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; long long max(long long a, long long b) { return a > b ? a : b; } long long min(long long a, long long b) { return a < b ? a : b; } void fun(int arr[], int n) { stack<int> s; int left[n + 1]; int right[n + 1]; for (int i = 0; i < n; i++) { left[i] = -1; right[i] = n; } for (int i = 0; i < n; i++) { while (!s.empty() && arr[s.top()] >= arr[i]) s.pop(); if (!s.empty()) left[i] = s.top(); s.push(i); } while (!s.empty()) s.pop(); for (int i = n - 1; i >= 0; i--) { while (!s.empty() && arr[s.top()] >= arr[i]) s.pop(); if (!s.empty()) right[i] = s.top(); s.push(i); } int ans[n + 1]; for (int i = 0; i <= n; i++) ans[i] = 0; for (int i = 0; i < n; i++) { int len = right[i] - left[i] - 1; ans[len] = max(ans[len], arr[i]); } for (int i = n - 1; i >= 1; i--) ans[i] = max(ans[i], ans[i + 1]); for (int i = 1; i <= n; i++) cout << ans[i] << ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t, n, k; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; fun(arr, n); cout << n ; return 0; } |
#include <bits/stdc++.h> int K; int main() { scanf( %d , &K); if (K % 2) { puts( YES ); if (K == 1) { puts( 2 1 n1 2 ); return 0; } int i, j, N = (K << 1) + 4, M = (3 * K - 2 + (K - 1) * (K - 2) / 2 - K / 2) * 2 + 1, tmp1 = K + 1, tmp2 = K + 2; printf( %d %d n , N, M); for (i = 1; i < K; i++) printf( %d %d n%d %d n%d %d n%d %d n%d %d n%d %d n , K, i, K + tmp2, i + tmp2, tmp1, i, tmp1 + tmp2, i + tmp2, tmp2, i, tmp2 << 1, i + tmp2); printf( %d %d n%d %d n , tmp1, tmp2, tmp1 + tmp2, tmp2 << 1); int lim = K - 1; for (i = 1, tmp1 = K >> 1; i < lim; i++) for (j = i + 1; j < K; j++) if (j != i + tmp1) printf( %d %d n%d %d n , i, j, i + tmp2, j + tmp2); printf( %d %d n , K, K + tmp2); } else puts( NO ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 3050; int F[N][N]; char buf[N]; vector<int> row[N]; short S[N][N]; inline short get(int y1, int x1, int y2, int x2) { return S[y2][x2] + S[y1][x1] - S[y2][x1] - S[y1][x2]; } int A[N]; int n, m, k; short to_L[N], to_R[N]; short path[N]; int pt = 0; inline short getL(short x) { if (x == -1) return -1; if (to_L[x] == -1 || A[to_L[x]]) return to_L[x]; pt = 0; path[pt++] = x; while (true) { x = to_L[x]; if (x == -1 || A[x] != 0) break; else path[pt++] = x; } for (int i = 0; i < pt - 1; i++) to_L[path[i]] = x; return x; } short getR(short x) { if (x == m) return m; if (to_R[x] == m || A[to_R[x]]) return to_R[x]; pt = 0; path[pt++] = x; while (true) { x = to_R[x]; if (x == m || A[x] != 0) break; else path[pt++] = x; } for (int i = 0; i < pt - 1; i++) to_R[path[i]] = x; return x; } vector<short> visited; short Rpos[N]; int used[N]; int curver = 0; vector<short> interesting; void calcLpos(short x) { visited.clear(); short cur = 0; while (true) { if (x == -1) return; if (used[x] == curver) return; used[x] = curver; visited.push_back(x); cur += A[x]; if (cur >= k) { x = getL(x); if (x != -1 && used[x] != curver) visited.push_back(x), used[x] = curver; return; } x = getL(x); } assert(false); } long long start(short r) { for (short j = 0; j < m; j++) { A[j] = get(r, j, n, j + 1); } short lst = m; for (short j = m - 1; j >= 0; j--) { to_R[j] = lst; if (A[j]) lst = j; } lst = -1; for (short j = 0; j < m; j++) { to_L[j] = lst; if (A[j]) lst = j; } lst = -1; for (short j = 0; j < m; j++) { if (A[j]) { Rpos[j] = max(j, lst); while (true) { short nrpos = getR(Rpos[j]); if (Rpos[j] == m || get(r, j, n, Rpos[j] + 1) >= k) { break; } else { Rpos[j] = nrpos; } } lst = Rpos[j]; } } int curval = 0; for (short j = 0; j < m; j++) { if (A[j]) { short pj = getL(j); short from = Rpos[j]; curval += (int)(j - pj) * (m - from); } } long long ans = 0; ans += curval; for (int i = n - 1; i > r; i--) { interesting.clear(); ++curver; for (short x : row[i]) { calcLpos(x); interesting.insert(interesting.end(), visited.rbegin(), visited.rend()); short r = getR(x); if (r != m && used[r] != curver) { used[r] = curver; interesting.push_back(r); } } for (short j : interesting) { short pj = getL(j); short from = Rpos[j]; curval -= (int)(j - pj) * (m - from); } for (short x : row[i]) { A[x]--; } short lst = -1; for (short j : interesting) { if (!A[j]) continue; short pj = getL(j); Rpos[j] = max(Rpos[j], lst); while (true) { short nrpos = getR(Rpos[j]); if (Rpos[j] == m || get(r, j, i, Rpos[j] + 1) >= k) { break; } else { Rpos[j] = nrpos; } } lst = Rpos[j]; short from = Rpos[j]; curval += (int)(j - pj) * (m - from); } ans += curval; } return ans; } int main() { int tmp; scanf( %d %d %d %d , &n, &m, &tmp, &k); for (int i = 0; i < tmp; i++) { int y, x; scanf( %d %d , &y, &x); --y, --x; F[y][x] = 1; row[y].push_back(x); } for (int i = 0; i < n; i++) { sort(row[i].begin(), row[i].end()); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { S[i + 1][j + 1] = S[i + 1][j] + S[i][j + 1] - S[i][j] + F[i][j]; } } long long ans = 0; long long last = start(n - 1); ans = last; for (int i = n - 2; i >= 0; i--) { if (i % 10 == 0) { } if (row[i].empty()) ans += last; else { ans += (last = start(i)); } } printf( %lld n , ans); } |
#include <bits/stdc++.h> using namespace std; long n, tim; string st; long lpos, calc1, calc2; long need, ans, calc; long l, r, has, bad; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> tim; cin >> st; lpos = -1; for (int i = 0; i < st.size(); i++) { if (st[i] == H ) ++calc1; if (st[i] == H ) lpos = i; if (st[i] == S ) ++calc2; } if (tim <= lpos) { cout << -1 << endl; return 0; } l = 0; r = 1e6; if (calc1 > calc2) l = calc1 - calc2; while (l < r) { long mid = l + r; mid /= 2; calc = 0; has = mid; bad = 1e9; need = 0; long oscore = 1e9; long tt = mid; long epos = 0; for (int i = 0; i < st.size(); i++) { if (tt < calc1) epos = i; if (st[i] == S ) ++tt; } if (epos < lpos) epos = lpos; for (long i = 0; i < st.size(); i++) { ++calc; if (bad < 1e7) oscore = min(oscore, epos - i + calc + (epos - bad) * (bad < 1e7)); if (st[i] == H ) { if (has == 0) { bad = min(bad, i); need++; } else { --has; } } if (st[i] == S ) { ++has; if (has >= need && i >= lpos) { if (need) calc += i - bad; break; } if (has >= need && need > 0) { calc += 2 * (i - bad); bad = 1e9; has -= need; need = 0; } } if (i >= lpos && has >= need) { if (need) calc += i - bad; break; } } calc = min(calc, oscore); if (calc > tim) l = mid + 1; else r = mid; } cout << l << endl; cin.get(); cin.get(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 2 == 0) cout << NO << endl; else { cout << YES << endl; cout << 1 << ; for (int i = 4; i < 2 * n; i += 4) cout << i << << i + 1 << ; for (int i = 2; i < 2 * n; i += 4) cout << i << << i + 1 << ; cout << 2 * n << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, SF, FS; SF = FS = 0; string flights; cin >> n >> flights; for (int i = 0; i < flights.size() - 1; i++) { if (flights[i] == S && flights[i + 1] == F ) { SF++; } else if (flights[i] == F && flights[i + 1] == S ) { FS++; } } if (SF > FS) { cout << YES ; } else { cout << NO ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 100; struct Node { int id, w; }; vector<int> g[maxn]; vector<Node> gg; bool vis[maxn]; int dfs(int u) { vis[u] = 1; for (int i : g[u]) { if (!vis[i]) { return dfs(i); } } return u; } int main(int argc, const char* argv[]) { ios::sync_with_stdio(false); int n, a, b; Node tmp; cin >> n; for (int i = 0; i < n - 1; ++i) { cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } int t = 0; for (int i = 1; i <= n; ++i) { int tt = g[i].size(); tmp.id = i; tmp.w = tt; gg.push_back(tmp); if (tt > 2) { ++t; } if (t >= 2) { break; } } if (t >= 2) { cout << No << endl; } else { cout << Yes << endl; sort(gg.begin(), gg.end(), [&](Node a, Node b) { return a.w > b.w; }); if (t) tmp = gg[0]; else tmp = gg[n - 1]; vis[tmp.id] = 1; cout << tmp.w << endl; for (int i : g[tmp.id]) { cout << tmp.id << << dfs(i) << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; long long n, k; int a[200007]; long long fac[200007]; long long inv[200007]; long long pw2[200007]; long long free_pw[200007]; long long fastpow(long long x, long long st) { long long ret = 1; while (st > 0) { if ((st & 1) != 0) { ret = (ret * x) % 998244353; --st; } else { x = (x * x) % 998244353; st >>= 1; } } return ret; } long long get_comb(long long x, long long y) { if (x < y) { return 0; } long long ret = fac[x]; ret = (ret * inv[y]) % 998244353; ret = (ret * inv[x - y]) % 998244353; return ret; } void input() { scanf( %I64d%I64d , &n, &k); for (int i = 1; i <= n; ++i) { scanf( %d , &a[i]); } a[n + 1] = a[1]; fac[0] = pw2[0] = free_pw[0] = 1; for (int i = 1; i <= n; ++i) { fac[i] = (fac[i - 1] * i) % 998244353; pw2[i] = (pw2[i - 1] * 2) % 998244353; free_pw[i] = (free_pw[i - 1] * (k - 2)) % 998244353; } inv[n] = fastpow(fac[n], 998244353 - 2); for (int i = n - 1; i >= 0; --i) { inv[i] = (inv[i + 1] * (i + 1)) % 998244353; } } void solve() { int free = 0; long long aux = 1; for (int i = 1; i <= n; ++i) { if (a[i] == a[i + 1]) { ++free, aux = (aux * k) % 998244353; } } long long ans = 0; for (int i = 0; i < n - free; ++i) { long long w = (get_comb(n - free, i) * free_pw[i]) % 998244353; int lft = n - free - i; long long coef = pw2[lft]; if ((lft & 1) == 0) { coef = (coef - get_comb(lft, lft / 2) + 998244353) % 998244353; } coef = (coef * inv[2]) % 998244353; ans = (ans + (w * coef) % 998244353) % 998244353; } printf( %I64d n , (aux * ans) % 998244353); } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); input(); solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0; cin >> n; n -= 10; if (n == 11) { ans += 4; n -= 11; } if (n == 10) { ans = (4 * 4) - 1; n -= 10; } for (int i = 9; i >= 1; --i) { if (n == i) { ans += 4; n -= i; } } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; double dist[5001][5001]; double x[5001], y[5001], z[5001]; int n; double sqr(double x) { return x * x; } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> x[i] >> y[i] >> z[i]; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { dist[i][j] = sqrt(sqr(x[i] - x[j]) + sqr(y[i] - y[j]) + sqr(z[i] - z[j])); dist[j][i] = dist[i][j]; } } double distance = 100000; for (int i = 2; i <= n; i++) { double minn = 100000; for (int j = 2; j <= n; j++) { if (i != j) { if (dist[1][j] + dist[j][i] < minn) minn = dist[1][j] + dist[j][i]; } } if (dist[1][i] + (minn - dist[1][i]) / 2 < distance) distance = dist[1][i] + (minn - dist[1][i]) / 2; } cout.precision(20); cout << distance; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, m; cin >> n >> m; string s, t; cin >> s >> t; int mincnt = n + 1; int nin = -1; for (int i = 0; i <= m - n; i++) { int cnt = 0; for (int j = 0; j < n; j++) { if (s[j] != t[i + j]) cnt++; } if (mincnt > cnt) { mincnt = cnt; nin = i; } } vector<long long> a; int i = nin; for (int k = i; k <= n + i - 1; k++) if (s[k - i] != t[k]) a.push_back(k - i); cout << a.size() << n ; for (int i = 0; i < mincnt; i++) cout << a[i] + 1 << ; } |
#include <bits/stdc++.h> int days(int y) { if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)) { return 366; } else { return 365; } } int main() { int year, i, d, di, dii; unsigned long long int count = 0; scanf( %d , &year); d = days(year); di = d; for (i = year;; i++) { dii = days(i + 1); count += di; if (count % 7 == 0 && d == dii) { printf( %d n , i + 1); return 0; } di = dii; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int ar[n], a, b, sum = 0; for (int i = 0; i < n - 1; i++) cin >> ar[i]; cin >> a >> b; a--; b--; for (int i = a; i < b; i++) sum += ar[i]; cout << sum << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 28; const double INF = 1e12, EPS = 1e-9; int n, m, c[101]; void run() { cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; for (; a <= b; a++) c[a]++; } for (int i = 1; i <= n; i++) if (c[i] != 1) { cout << i << << c[i] << endl; return; } cout << OK << endl; } int main() { run(); } |
#include <bits/stdc++.h> using namespace std; int n; int a[1501]; int calcinv() { int j; int rez = 0; for (int i = 1; i <= n; i++) for (j = i + 1; j <= n; j++) if (a[i] > a[j]) rez++; return rez; } int main() { int i, nri, j, m, l, r, lg; cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; nri = calcinv() % 2; cin >> m; for (i = 1; i <= m; i++) { cin >> l >> r; lg = r - l; lg = lg * (lg + 1) / 2; nri = (nri + lg) % 2; if (nri == 0) cout << even ; else cout << odd ; cout << n ; } } |
#include <bits/stdc++.h> using namespace std; void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int main() { int n; cin >> n; int x[n], y[n], min_x = INT_MAX, max_y = INT_MIN; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; if (x[i] < min_x) min_x = x[i]; if (y[i] > max_y) max_y = y[i]; } int k = 0, res = -1; while (k < n) { if (x[k] == min_x) { if (y[k] == max_y) { res = k + 1; break; } } k++; } cout << res << endl; } |
#include <bits/stdc++.h> using namespace std; char s[1000010], t[1000010]; int ls, lt; int sum[1000010][3][3]; int main() { scanf( %s%s , s + 1, t + 1); ls = strlen(s + 1); lt = strlen(t + 1); if (ls > lt) { swap(ls, lt); swap(s, t); } for (int i = 1; i <= ls; ++i) { if (s[i] == R ) s[i] = 0; if (s[i] == G ) s[i] = 1; if (s[i] == B ) s[i] = 2; } for (int i = 1; i <= lt; ++i) { if (t[i] == R ) t[i] = 0; if (t[i] == G ) t[i] = 1; if (t[i] == B ) t[i] = 2; } long long ans = 0; int l = 1, r = 1; while (r < lt && t[r] != s[1]) { ++r; for (int j1 = 0; j1 < 3; ++j1) for (int j2 = 0; j2 < 3; ++j2) sum[r][j1][j2] = sum[r - 1][j1][j2]; ++sum[r][t[r - 1]][t[r]]; } for (int i = 1; i <= ls; ++i) { while (r < lt && t[r] != s[i]) { ++r; for (int j1 = 0; j1 < 3; ++j1) for (int j2 = 0; j2 < 3; ++j2) sum[r][j1][j2] = sum[r - 1][j1][j2]; ++sum[r][t[r - 1]][t[r]]; } ans += r - l + 1; if (s[i] != s[i - 1]) ans -= sum[r][s[i]][s[i - 1]] - sum[l - 1][s[i]][s[i - 1]]; if (s[i] == t[l]) ++l; if (r < lt) { ++r; for (int j1 = 0; j1 < 3; ++j1) for (int j2 = 0; j2 < 3; ++j2) sum[r][j1][j2] = sum[r - 1][j1][j2]; ++sum[r][t[r - 1]][t[r]]; } } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; void no() { cout << NO n ; exit(0); } int main() { int n; cin >> n; vector<int> arr(n); for (int i = 0; i < n; ++i) cin >> arr[i]; sort(arr.begin(), arr.end()); for (int i = n - 1; i >= 0; --i) { arr[i] -= arr[0]; if (arr[i] >= n) no(); } vector<int> cnt(n); for (int i = 0; i < n; ++i) cnt[arr[i]]++; int mx = arr[n - 1]; for (int i = 1; i < mx; ++i) { if (cnt[i] < 2) no(); cnt[i] -= 2; } cnt[0]--; cnt[mx]--; for (int i = 0; i <= mx; ++i) { while (cnt[i] > 0) { if (cnt[i + 1] == 0) no(); cnt[i]--; cnt[i + 1]--; } } cout << YES n ; } |
#include <bits/stdc++.h> using namespace std; const int N = 1000 * 100 + 10; const long long INF = 1000 * 1000 * 1000LL * 1000 * 10000; int n, q, pt = 1, cnt; vector<long long> ans; set<pair<long long, long long> > s; set<pair<long long, long long> >::iterator it1, it2; long long po[100], a[N], sum[N * 4][2]; pair<long long, long long> chng[N * 4][2], minn[N * 4][2]; void shift(int ind, bool o, int b, int mid) { if (chng[ind][o].first) { chng[ind * 2][o] = chng[ind * 2 + 1][o] = chng[ind][o]; minn[ind * 2][o] = make_pair(chng[ind][o].second, b); minn[ind * 2 + 1][o] = make_pair(chng[ind][o].second, mid); sum[ind * 2][o] = sum[ind * 2 + 1][o] = 0; chng[ind][o] = make_pair(0, 0); } long long t = sum[ind][o]; sum[ind * 2][o] += t; sum[ind * 2 + 1][o] += t; minn[ind * 2][o].first += t; minn[ind * 2 + 1][o].first += t; sum[ind][o] = 0; return; } void add(int l, int r, pair<long long, long long> val, bool o, int b = 0, int e = n, int ind = 1) { if (l <= b && e <= r) { minn[ind][o].first += val.second; sum[ind][o] += val.second; return; } int mid = (b + e) / 2; shift(ind, o, b, mid); if (l < mid) add(l, r, val, o, b, mid, ind * 2); if (r > mid) add(l, r, val, o, mid, e, ind * 2 + 1); minn[ind][o] = min(minn[ind * 2][o], minn[ind * 2 + 1][o]); return; } void sett(int l, int r, pair<long long, long long> val, bool o, int b = 0, int e = n, int ind = 1) { if (l <= b && e <= r) { chng[ind][o] = val; sum[ind][o] = 0; minn[ind][o] = make_pair(val.second, b); return; } int mid = (b + e) / 2; shift(ind, o, b, mid); if (l < mid) sett(l, r, val, o, b, mid, ind * 2); if (r > mid) sett(l, r, val, o, mid, e, ind * 2 + 1); minn[ind][o] = min(minn[ind * 2][o], minn[ind * 2 + 1][o]); } pair<long long, long long> getmin(int l, int r, int b = 0, int e = n, int ind = 1) { pair<long long, long long> ret = make_pair(INF, INF); if (l <= b && e <= r) return minn[ind][0]; int mid = (b + e) / 2; shift(ind, 0, b, mid); if (l < mid) ret = min(ret, getmin(l, r, b, mid, ind * 2)); if (r > mid) ret = min(ret, getmin(l, r, mid, e, ind * 2 + 1)); return ret; } long long get(int p, bool o, int b = 0, int e = n, int ind = 1) { if (b + 1 == e) return sum[ind][o] + chng[ind][o].second; int mid = (b + e) / 2; shift(ind, o, b, mid); if (p < mid) return get(p, o, b, mid, ind * 2); return get(p, o, mid, e, ind * 2 + 1); } long long type1(int x) { x--; return po[get(x, 1)] - get(x, 0); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> q; po[0] = 1; for (int i = 1; i <= 12; i++) po[i] = po[i - 1] * 42; for (int i = 0; i < n; i++) { cin >> a[i]; a[i] = 1 - a[i]; sett(i, i + 1, make_pair(pt++, a[i]), 0); s.insert(make_pair(i, i + 1)); } long long tp, x, l, r; for (int i = 0; i < q; i++) { cin >> tp; if (tp == 1) { cin >> x; ans.push_back(type1(x)); } else { cin >> l >> r >> x; l--; if (tp == 2) { sett(l, r, make_pair(pt++, 1 - x), 0); sett(l, r, make_pair(pt++, 0), 1); s.insert(make_pair(l, r)); it1 = it2 = s.find(make_pair(l, r)); if (it1 != s.begin() && (*--it1).second > l) { pair<long long, long long> temp = *it1; s.erase(it1); it1 = s.find(make_pair(l, r)); if (temp.first < l) s.insert(make_pair(temp.first, l)); if (temp.second > r) s.insert(make_pair(r, temp.second)); } it2++; while (it2 != s.end() && (*it2).first < r) { pair<long long, long long> temp = *it2; s.erase(it2); it2 = ++s.find(make_pair(l, r)); if (temp.second > r) s.insert(make_pair(r, temp.second)); } } else { bool flag = 1; pair<long long, long long> t1 = *--s.upper_bound(make_pair(l, n + 1)); if (t1.first != l) { s.erase(t1); s.insert(make_pair(t1.first, l)); s.insert(make_pair(l, t1.second)); } pair<long long, long long> t2 = *--s.upper_bound(make_pair(r, n + 1)); if (t2.first != r) { s.erase(t2); s.insert(make_pair(t2.first, r)); if (t2.second != r) s.insert(make_pair(r, t2.second)); } while (flag) { add(l, r, make_pair(pt++, -x), 0); pair<long long, long long> temp = getmin(l, r); while (temp.first < 0) { pair<long long, long long> seg = *--s.upper_bound(make_pair(temp.second, n + 1)); int pnt = get(temp.second, 1); add(seg.first, seg.second, make_pair(pt++, po[pnt + 1] - po[pnt]), 0); sett(seg.first, seg.second, make_pair(pt++, pnt + 1), 1); temp = getmin(l, r); } if (temp.first != 0) flag = 0; } } } } for (long long u : ans) cout << u << endl; } |
#include <bits/stdc++.h> using namespace std; const int B = 12; const int D = 64; const int MASK = 1030; int q, b; long long f[B][D][MASK]; long long sum[B][D]; long long call(int base, int rem, int mask) { if (rem <= 0) return mask == 0; long long &ret = f[base][rem][mask]; if (ret != -1) return ret; ret = 0; for (int i = 0; i < base; ++i) { ret += call(base, rem - 1, mask ^ 1 << i); } return ret; } long long solve(long long x) { if (x == 0) return 0; vector<int> v; while (x) { v.push_back(x % b); x /= b; } reverse(v.begin(), v.end()); int mask = 0, sz = v.size(); long long ans = 0; for (int i = 0; i < sz; ++i) { mask ^= 1 << v[i]; if (i + 1 < sz) { for (int j = 0; j < v[i + 1]; ++j) { ans += call(b, sz - 2 - i, mask ^ 1 << j); } } else { ans += (mask == 0); } } for (int i = 1; i < v[0]; ++i) { ans += call(b, sz - 1, 1 << i); } for (int i = 1; i < sz; ++i) { for (int j = 1; j < b; ++j) { ans += call(b, sz - 1 - i, 1 << j); } } return ans; } int main() { memset(f, -1, sizeof f); memset(sum, -1, sizeof sum); scanf( %d , &q); while (q--) { long long l, r; scanf( %d %lld %lld , &b, &l, &r); printf( %lld n , solve(r) - solve(l - 1)); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int n, a, b, c, result = 0, i, x, y, j = 0; cin >> n >> a >> b >> c; for (i = 0; i <= min(n, a / 2); i++) { x = min(c, (n - i) / 2); y = min(i + b, n); if (y - (n - x * 2) / 2 >= 0) { if (y >= n - x * 2) result += (y - (n - x * 2)) / 2 + 1; } } cout << result; return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> inline T BM(T p, T e, T M) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T mdINV(T a, T M) { return BM(a, M - 2, M); } template <class T> inline T PW(T p, T e) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p); p = (p * p); } return (T)ret; } template <class T> string NTS(T Number) { stringstream ss; ss << Number; return ss.str(); } template <class T> T stringtonumber(const string &Text) { istringstream ss(Text); T result; return ss >> result ? result : 0; } template <class T> bool ISLEFT(T a, T b, T c) { if (((a.first.first - b.first.first) * (b.second.second - c.second.second) - (b.first.first - c.first.first) * (a.second.second - b.second.second)) < 0.0) return 1; else return 0; } int n, k; vector<int> v[300ll + 2]; long long int dp[102][44]; void go(int u, int p) { dp[u][0] = 1; dp[u][k + 1] = 1; for (int i = 0; i < v[u].size(); i++) { int first = v[u][i]; if (first == p) continue; go(first, u); long long int kop[44]; memset(kop, 0, sizeof kop); for (int j = 0; j <= k * 2; j++) { for (int l = 0; l <= k * 2; l++) { if (j + l <= k * 2) { kop[min(j, l + 1)] = (kop[min(j, l + 1)] + dp[u][j] * dp[first][l]) % 1000000007; } else { kop[max(j, l + 1)] = (kop[max(j, l + 1)] + dp[u][j] * dp[first][l]) % 1000000007; } } } for (int j = 0; j <= k * 2; j++) dp[u][j] = kop[j]; } } int main() { scanf( %d%d , &n, &k); for (int i = 1; i <= n - 1; i++) { int first, second; scanf( %d%d , &first, &second); v[first].push_back(second); v[second].push_back(first); } go(1, -1); int ans = 0; for (int i = 0; i <= k; i++) { ans += dp[1][i]; if (ans >= 1000000007) ans -= 1000000007; } printf( %d , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int b, g, n; scanf( %d%d%d , &b, &g, &n); printf( %d n , min(b, n) - (n - min(g, n)) + 1); return 0; } |
#include <bits/stdc++.h> using namespace std; int cf[45], nc[45]; vector<int> ppf[45]; bool canadd(int i, int m) { for (int p : ppf[m]) { if (nc[p] && cf[p] != i % p) return false; } return true; } void add(int i, int m) { for (int p : ppf[m]) { if (nc[p] == 0) cf[p] = i % p; nc[p]++; } } void rem(int m) { for (int p : ppf[m]) { nc[p]--; } } vector<pair<int, int> > loc[100005]; int k[100005]; int n, m; int main() { for (int i = (2); i < (45); i++) { int x = i; for (int j = (2); j < (45); j++) { int fct = 1; while (x % j == 0) { fct *= j; ppf[i].push_back(fct); x /= j; } } } scanf( %d %d , &n, &m); for (int i = (0); i < (n); i++) { scanf( %d , k + i); for (int j = (0); j < (k[i]); j++) { int x; scanf( %d , &x); loc[x].push_back(make_pair(i, j)); } } for (int x = (1); x < (m + 1); x++) { queue<pair<int, int> > cq; int prv = -1, ans = 0; for (auto l : loc[x]) { int i, j; tie(i, j) = l; if (i != prv + 1) { while (int(cq.size())) { int fi, fj; tie(fi, fj) = cq.front(); cq.pop(); rem(k[fi]); } } while (!canadd(j, k[i])) { int fi, fj; tie(fi, fj) = cq.front(); cq.pop(); rem(k[fi]); } add(j, k[i]); cq.emplace(i, j); prv = i; ans = max(ans, int(cq.size())); } while (int(cq.size())) { int fi, fj; tie(fi, fj) = cq.front(); cq.pop(); rem(k[fi]); } printf( %d n , ans); } } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t, n, num; cin >> t; while (t--) { cin >> n; bool flag = false; unordered_map<int, vector<int>> m; for (int i = 0; i < n; ++i) { cin >> num; m[num].push_back(i); } for (auto& k : m) { if (k.second.back() - k.second.front() >= 2) { flag = true; break; } } if (flag) cout << YES n ; else cout << NO n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; struct arr { int i; double s; } e[1506]; int n, m, x[1506], y[1506], w[1506], f[1506][1506], v[1506][2]; double ans, s[1506]; inline bool cmp(arr x, arr y) { return x.s > y.s; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d%d%d , &x[i], &y[i], &w[i]), e[i].s = 1.0 * w[i] * w[i] * acos(-1.0), e[i].i = i; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { double t = sqrt(1.0 * (x[i] - x[j]) * (x[i] - x[j]) + 1.0 * (y[i] - y[j]) * (y[i] - y[j])); if (1.0 * w[i] >= 1.0 * w[j] + t) f[i][j] = 1; } sort(e + 1, e + n + 1, cmp); for (int i = 1; i <= n; i++) { if (!v[e[i].i][0]) { ans += e[i].s; for (int j = 1; j <= n; j++) if (f[e[i].i][j]) v[j][0] = 1; } else if (!v[e[i].i][1]) { ans += e[i].s; for (int j = 1; j <= n; j++) if (f[e[i].i][j]) v[j][1] = 1; } else { ans -= e[i].s; for (int j = 1; j <= n; j++) if (f[e[i].i][j]) v[j][0] = 0; } } printf( %.12lf n , ans); } |
#include <bits/stdc++.h> using namespace std; const int maxn = 61 * 30 + 5; pair<int, int> C[62]; int M, A[32], cc; bool dp[32][62][maxn], D[62][62]; short int fr[32][62][maxn]; int main() { scanf( %d , &M); for (int i = 1; i <= M; ++i) scanf( %d , A + i); sort(A + 1, A + M + 1); dp[0][0][0] = true; for (int i = 1; i <= M; ++i) for (int j = i; j <= 61; ++j) for (int k = (j * (j - 1)) >> 1; k < maxn; ++k) { for (int m = 1; m <= j; ++m) if (k >= m * A[i] && dp[i - 1][j - m][k - m * A[i]]) { dp[i][j][k] = true; fr[i][j][k] = m; break; } } int N = M; while (!dp[M][N][(N * (N - 1)) >> 1]) ++N; printf( %d n , N); for (int i = M, j = N, k = (N * (N - 1)) >> 1; i; --i) { int c = fr[i][j][k]; j -= c; k -= c * A[i]; for (int j = 0; j < c; ++j) { ++cc; C[cc] = make_pair(A[i], cc); } } for (int i = 1; i <= N; ++i) { sort(C + 1, C + cc + 1); for (int j = i + 1; j <= i + C[i].first; ++j) D[C[i].second][C[j].second] = true; for (int j = i + C[i].first + 1; j <= N; ++j) D[C[j].second][C[i].second] = true, --C[j].first; C[i].first = -1; } for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) printf( %d , D[i][j] ? 1 : 0); printf( n ); } } |
#include <bits/stdc++.h> using namespace std; const char *par = () ; const int pm[] = {1, -1}; int n; int mn, res, fir; string s; vector<int> v, w; map<int, int> mp; int init(string s) { v = vector<int>(n); for (int i = 0, pre = 0; i < n; ++i) { v[i] = (pre += pm[strchr(par, s[i]) - par]); mp[v[i]]++; } if (v.back() != 0) return 0; res = mp.begin()->second; mn = mp.begin()->first; for (int i = 0; i < n; ++i) if (v[i] == mn) { fir = (i + 1) % n; return res; } } int count(int l, int r) { int cur = v[(l - 1 + n) % n]; s[l] = ) ; for (int i = l; i != r; i = (i + 1) % n) { cur += pm[strchr(par, s[i]) - par]; if (!--mp[v[i]]) mp.erase(v[i]); mp[cur]++; } int ret = mp.begin()->second; cur = v[(l - 1 + n) % n]; for (int i = l; i != r; i = (i + 1) % n) { cur += pm[strchr(par, s[i]) - par]; if (!--mp[cur]) mp.erase(cur); mp[v[i]]++; } s[l] = ( ; return ret; } int main() { cin >> n >> s; if (!init(s)) { cout << 0 n1 1 n ; return 0; } stack<int> st; int i = fir; w = vector<int>(n); do { if (s[i] == ( ) st.push(i); if (s[i] == ) ) { w[i] = st.top(); w[w[i]] = i; st.pop(); } } while ((i = (i + 1) % n) != fir); int ans[3] = {res}; for (int i = 0; i < n; ++i) if (s[i] == ( && v[i] <= mn + 2) { int ret = count(i, w[i]); if (ret > ans[0]) ans[0] = ret, ans[1] = i, ans[2] = w[i]; } cout << ans[0] << endl << ans[1] + 1 << << ans[2] + 1 << endl; } |
#include <bits/stdc++.h> using namespace std; inline bool cmp(const pair<int, int> &a, const pair<int, int> &b) { if (a.first != b.first) return a.first > b.first; return a.second < b.second; } int a[1000005], b[1000005]; char s[1000005], r[1000005]; int test; int main() { int t, n, m, k, l, x, y, z, flag = 0, count, d, mx, mn; long long int sum, prod; scanf( %s%s , s, r); count = sum = 0; n = strlen(s), m = strlen(r); int j = 0; for (int i = 0; i < m; i++) { if (r[i] == s[j]) j++; if (j == n) break; } printf( %d , j + 1); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, a[200005]; void slove() { int temp, u, v, w, m; for (int i = 1; i <= n; i++) cin >> temp, a[i] = 0x3f3f3f3f; cin >> m; for (int i = 1; i <= m; i++) { cin >> u >> v >> w; if (a[v] > w) a[v] = w; } int flag = 1, k = 1; int ans = 0; for (int i = 1; i <= n; i++) { if (a[i] == 0x3f3f3f3f && k) { ans -= 0x3f3f3f3f; k = 0; } else if (a[i] == 0x3f3f3f3f) { flag = 0; break; } ans += a[i]; } if (flag) cout << ans << n ; else cout << -1 << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; slove(); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long INF = (1ll << 60); const int N = 205; int n, m, _len; int nxt[N]; long long k; long long ADD(long long x, long long y) { return min(INF, x + y); } struct node { char pref[N]; char suff[N]; long long len; long long sum; } t[N * 4], eddd; char s[N * 4]; char ans[N]; node operator+(node a, node b) { node ans; ans.len = ADD(a.len, b.len); ans.sum = ADD(a.sum, b.sum); int l1 = min(a.len, 2ll * _len); int l2 = min(b.len, 2ll * _len); for (int i = (int)(1); i <= (int)(min(l1, _len)); i++) s[i] = a.pref[i]; for (int i = (int)(1); i <= (int)(min(l1, _len)); i++) s[l1 - i + 1] = a.suff[i]; for (int i = (int)(1); i <= (int)(min(l2, _len)); i++) s[l1 + i] = b.pref[i]; for (int i = (int)(1); i <= (int)(min(l2, _len)); i++) s[l1 + l2 - i + 1] = b.suff[i]; int p = 0; s[l1 + l2 + 1] = 0; for (int i = (int)(l1 - min(l1, _len - 1) + 1); i <= (int)(l1 + 1 + min(l2, _len - 1) - 1); i++) { for (; p && s[i] != ::ans[p + 1]; p = nxt[p]) ; if (s[i] == ::ans[p + 1]) ++p; if (p == _len) ++ans.sum, p = nxt[p]; } for (int i = (int)(1); i <= (int)(min(_len, l1 + l2)); i++) ans.pref[i] = s[i]; for (int i = (int)(1); i <= (int)(min(_len, l1 + l2)); i++) ans.suff[i] = s[l1 + l2 - i + 1]; return ans; } long long count(int l) { _len = l; nxt[1] = 0; for (int i = (int)(2); i <= (int)(l); i++) { int j = nxt[i - 1]; for (; j && ans[j + 1] != ans[i]; j = nxt[j]) ; nxt[i] = j + (ans[j + 1] == ans[i]); } t[0].pref[1] = t[0].suff[1] = 0 ; t[1].pref[1] = t[1].suff[1] = 1 ; eddd.pref[1] = eddd.suff[1] = 2 ; t[0].len = t[1].len = eddd.len = 1; t[0].sum = (l == 1 && ans[1] == 0 ); t[1].sum = (l == 1 && ans[1] == 1 ); eddd.sum = (l == 1 && ans[1] == 2 ); for (int i = (int)(2); i <= (int)(n); i++) { t[i] = t[i - 2] + t[i - 1]; } return (t[n] + eddd).sum; } int main() { scanf( %d%lld%d , &n, &k, &m); k++; for (int i = (int)(1); i <= (int)(m); i++) { ans[i] = 2 ; long long val = count(i); if (val >= k) break; k -= val; ans[i] = 0 ; val = count(i); if (val >= k) { putchar( 0 ); continue; } k -= val; ans[i] = 1 ; val = count(i); if (val >= k) { putchar( 1 ); continue; } assert(0); } } |
#include <bits/stdc++.h> using namespace std; const int B = 27397, MOD = 1e9 + 7; const int B1 = 33941, MOD1 = 1e9 + 9; int t; inline void solve() { int r, n; scanf( %d%d , &n, &r); --r; int sum = 0; for (int i = 0; i < n - 1; ++i) { int x; scanf( %d , &x); --x; sum += x; } for (int i = 0; i < n; ++i) if ((sum + i) % n == r) printf( %d n , i + 1); } int main(void) { scanf( %d , &t); while (t-- > 0) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; vector<vector<pair<int, long long> > > g; int n, a[100005]; bool erased[100005]; void dfs(int cur, long long cost, long long minCost) { if (cost - minCost > 1ll * a[cur]) { erased[cur] = 1; return; } for (int i = 0; i < g[cur].size(); i++) { dfs(g[cur][i].first, cost + g[cur][i].second, min(minCost, cost)); } } int count(int u) { if (erased[u]) return 0; int ans = 1; for (int i = 0; i < g[u].size(); i++) ans += count(g[u][i].first); return ans; } int main() { scanf( %d , &n); g.assign(n, vector<pair<int, long long> >()); for (int i = 0; i < n; i++) scanf( %d , &a[i]); for (int i = 1; i < n; i++) { int v, c; scanf( %d %d , &v, &c); v--; g[v].push_back(pair<int, long long>(i, c)); } memset(erased, 0, sizeof erased); dfs(0, 0ll, 0ll); printf( %d n , n - count(0)); } |
#include <bits/stdc++.h> using namespace std; int n, m; int fibo[200005], t[20][200005], f[20][200005], a[1000006], g[200005], h[200005], s[200005]; void FWT_or(int*, int, int); void FWT_and(int*, int, int); void FWT_xor(int*, int, int); void init(); int main() { init(); scanf( %d , &n); for (int i = 1; i <= n; ++i) { scanf( %d , a + i); int x = a[i], cnt = 0; m = max(m, x); for (int j = 0; j <= 17; ++j) if (x & (1 << j)) ++cnt; ++f[cnt][x]; ++h[x]; ++s[x]; } int bit = 1, l = 1; while (bit <= m) bit <<= 1, ++l; for (int i = 0; i <= l; ++i) FWT_or(f[i], 1, bit); for (int i = 1; i <= l; ++i) for (int j = 0; j <= i; ++j) for (int k = 0; k < bit; ++k) t[i][k] = (1ll * t[i][k] + 1ll * f[j][k] * f[i - j][k] % 1000000007) % 1000000007; for (int i = 0; i <= l; ++i) { FWT_or(t[i], -1, bit); for (int j = 0; j < bit; ++j) { int cnt = 0; for (int k = 0; k <= l; ++k) if (j & (1 << k)) ++cnt; g[j] = (1ll * g[j] + t[i][j] * (cnt == i)) % 1000000007; } } FWT_xor(h, 1, bit); for (int i = 0; i < bit; ++i) h[i] = 1ll * h[i] * h[i] % 1000000007; FWT_xor(h, -1, bit); for (int i = 0; i < bit; ++i) s[i] = 1ll * fibo[i] * s[i] % 1000000007, g[i] = 1ll * fibo[i] * g[i] % 1000000007, h[i] = 1ll * fibo[i] * h[i] % 1000000007; FWT_and(h, 1, bit); FWT_and(g, 1, bit); FWT_and(s, 1, bit); for (int i = 0; i < bit; ++i) s[i] = 1ll * s[i] * g[i] % 1000000007 * h[i] % 1000000007; FWT_and(s, -1, bit); int ans = 0; for (int i = 1; i < bit; i <<= 1) ans = (1ll * ans + s[i]) % 1000000007; cout << ans << endl; return 0; } void FWT_or(int* x, int f, int len) { for (int i = 1; i < len; i <<= 1) for (int j = 0; j < len; j += i << 1) for (int k = 0; k < i; ++k) x[i + j + k] = (1ll * x[i + j + k] + x[j + k] * f + 1000000007) % 1000000007; return; } void FWT_and(int* x, int f, int len) { for (int i = 1; i < len; i <<= 1) for (int j = 0; j < len; j += i << 1) for (int k = 0; k < i; ++k) x[j + k] = (1ll * x[j + k] + x[i + j + k] * f + 1000000007) % 1000000007; return; } void FWT_xor(int* x, int f, int len) { for (int i = 1; i < len; i <<= 1) for (int j = 0; j < len; j += i << 1) for (int k = 0; k < i; ++k) { int p = x[j + k], q = x[i + j + k]; x[j + k] = (1ll * p + q) % 1000000007; x[i + j + k] = (1ll * p - q + 1000000007) % 1000000007; if (f == -1) x[j + k] = 1ll * x[j + k] * 500000004 % 1000000007, x[i + j + k] = 1ll * x[i + j + k] * 500000004 % 1000000007; } return; } void init() { fibo[1] = fibo[2] = 1; for (int i = 3; i <= (1 << 17); ++i) fibo[i] = (1ll * fibo[i - 1] + fibo[i - 2]) % 1000000007; return; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long N = 1e5 + 10; long long power(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret *= a; a *= a; if (ret >= MOD) ret %= MOD; if (a >= MOD) a %= MOD; b >>= 1; } return ret; } long long invmod(long long x) { return power(x, MOD - 2); } long long dp[55][2]; long long a[55]; long long n; long long go(long long pos, long long check) { if (pos == n + 1) return 0; if (dp[pos][check] != -1) return dp[pos][check]; long long ans = 0; if (check == 0) { ans += max(a[pos] + go(pos + 1, check ^ 1), go(pos + 1, check)); } else { ans += min(go(pos + 1, check ^ 1), a[pos] + go(pos + 1, check)); } dp[pos][check] = ans; return ans; } int32_t main() { cin >> n; long long sum = 0; for (long long i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } memset(dp, -1, sizeof(dp)); long long k = go(1, 0); cout << sum - k << << k << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << n << ; for (int i = 1; i < n; i++) { cout << i << ; } } |
#include <bits/stdc++.h> template <typename T> inline bool MIN(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool MAX(T &a, const T &b) { return a < b ? a = b, 1 : 0; } using namespace std; int n, m, K, p; int exgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1, y = 0; return a; } int res = exgcd(b, a % b, x, y); int t = x; x = y; y = t - a / b * y; return res; } int get_inv(int a) { int x, y; exgcd(a, 1000000007, x, y); return (x % 1000000007 + 1000000007) % 1000000007; } int P[100010], sum_P[100010]; int g[1510][1510], sum[1510][1510]; int main() { scanf( %d%d , &n, &m); int a, b; scanf( %d%d , &a, &b); p = (long long)a * get_inv(b) % 1000000007; scanf( %d , &K); int pow2 = 1; for (int i = 1; i <= K; ++i) pow2 = (long long)pow2 * (1 - p + 1000000007) % 1000000007; for (int i = 0, pow = 1, c = 1; i <= K; ++i) { if (i == K && p == 1) pow2 = 1; P[i] = (long long)c * pow % 1000000007 * pow2 % 1000000007; pow2 = (long long)pow2 * get_inv((1 - p + 1000000007) % 1000000007) % 1000000007; pow = (long long)pow * p % 1000000007; c = (long long)c * (K - i) % 1000000007 * get_inv(i + 1) % 1000000007; } sum_P[0] = P[0]; for (int i = 1; i <= m; ++i) sum_P[i] = (sum_P[i - 1] + P[i]) % 1000000007; g[0][m] = sum[0][m] = 1; for (int i = 1; i <= n; ++i) { int tmp = 0; for (int j = 1; j <= m; ++j) { tmp = (tmp + (long long)P[j - 1] * sum[i - 1][j - 1]) % 1000000007; g[i][j] = ((long long)sum_P[j - 1] * (sum[i - 1][m] - sum[i - 1][m - j] + 1000000007) - tmp + 1000000007) % 1000000007; g[i][j] = (long long)g[i][j] * P[m - j] % 1000000007; sum[i][j] = (sum[i][j - 1] + g[i][j]) % 1000000007; } } printf( %d n , sum[n][m]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long m, n, b[12345], w[12345], u, v, d = 0, c = 0; int mx = INT_MIN, mn = INT_MAX; string p[123]; char ch; bool sorted; cin >> n >> m; vector<int> a(n); for (int row = 0; row < n; row++) { cin >> p[row]; for (int col = 0; col < m; col++) { if (p[row][col] == * ) { c = c ^ row; d = d ^ col; } } } cout << c + 1 << << d + 1; return 0; } |
#include <bits/stdc++.h> using namespace std; int top = 0; int N, Q, NN; int K, M, R; vector<int> Adj[100005]; int dep[100005]; int anc[100005][21]; int pre[100005], pst[100005], tt; void dfs(int u) { pre[u] = ++tt; for (int j = 1; j <= 20; j++) { if (dep[u] <= (1 << j)) break; anc[u][j] = anc[anc[u][j - 1]][j - 1]; } int v; for (int k = 0; k < Adj[u].size(); k++) { v = Adj[u][k]; if (v == anc[u][0]) continue; anc[v][0] = u; dep[v] = dep[u] + 1; dfs(v); } pst[u] = ++tt; } int lca(int u, int v) { if (dep[u] < dep[v]) swap(u, v); for (int j = 20; j >= 0; j--) { if (dep[anc[u][j]] >= dep[v]) { u = anc[u][j]; } } if (u == v) return u; for (int j = 20; j >= 0; j--) { if (anc[u][j] != anc[v][j]) { u = anc[u][j]; v = anc[v][j]; } } return anc[u][0]; } int h[100005], c[2 * 100005]; int lbt(int x) { return x & (-x); } void update(int x, int dv) { for (; x <= NN; x += lbt(x)) { c[x] += dv; } } int getsum(int x) { int ans = 0; for (; x > 0; x -= lbt(x)) { ans += c[x]; } return ans; } bool cmp(const int& a, const int& b) { return dep[a] < dep[b]; } int query(int u, int lca) { return getsum(pre[u] - 1) - getsum(pre[lca] - 1); } int a[100005]; bool vis[100005]; long long dp[100005][305]; int work() { int u, z; for (int i = 1; i <= K; i++) { u = a[i]; update(pre[u], 1); update(pst[u], -1); vis[a[i]] = 1; } int x; for (int i = 1; i <= K; i++) { u = a[i]; z = lca(u, R); if (u == R) h[i] = 0; else if (u == z) h[i] = query(R, u) - (vis[u] == true) + (vis[R] == true); else if (z == R) h[i] = query(u, R); else h[i] = query(u, z) + query(R, z) - (vis[z] == true) + (vis[R] == true); } sort(h + 1, h + 1 + K); dp[1][1] = 1; for (int i = 2; i <= K; i++) { for (int j = 1; j <= M; j++) { if (j > h[i]) dp[i][j] = ((long long)dp[i - 1][j] * (j - h[i]) % int(1e9 + 7) + dp[i - 1][j - 1]) % int(1e9 + 7); else dp[i][j] = 0; } } long long ans = 0; for (int j = 1; j <= M; j++) { ans = (ans + dp[K][j]) % int(1e9 + 7); } for (int i = 1; i <= K; i++) { u = a[i]; update(pre[u], -1); update(pst[u], +1); vis[a[i]] = 0; } return ans; } int main() { scanf( %d%d , &N, &Q); int u, v; for (int i = 1; i < N; i++) { scanf( %d%d , &u, &v); Adj[u].push_back(v); Adj[v].push_back(u); } dep[1] = 1; dfs(1); NN = 2 * N; while (Q--) { scanf( %d%d%d , &K, &M, &R); for (int i = 1; i <= K; i++) { scanf( %d , &a[i]); } printf( %d n , work()); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 255; const int M = 16; const int mod = (int)1e9 + 7; char grid[N][N], ngrid[N][N]; long long dp[2][M][1 << M][2][2]; void add(long long& a, long long b) { a += b; while (a >= mod) { a -= mod; } } int main() { int n, m; scanf( %d %d , &n, &m); for (int i = 0; i < n; i++) { scanf( %s , grid[i]); } if (n < m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ngrid[j][i] = grid[i][j]; } } swap(n, m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { grid[i][j] = ngrid[i][j]; } } } if (grid[0][0] == x ) { dp[0][0][0][0][0] = 1; } else { dp[0][0][0][0][1] = 1; dp[0][0][1][1][0] = 1; } for (int i = 0; i < n; i++) { if (i > 0) { for (int j = 0; j < m; j++) { for (int mask = 0; mask < 1 << m; mask++) { for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { dp[i & 1][j][mask][a][b] = 0; } } } } } for (int j = 0; j < m; j++) { if (i == 0 && j == 0) continue; for (int mask = 0; mask < 1 << m; mask++) { for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { if (grid[i][j] != x ) { if (a == 1) { if (mask & (1 << j)) { if (j == 0) { add(dp[i & 1][j][mask][0][b], dp[1 - (i & 1)][m - 1][mask][a][b]); add(dp[i & 1][j][mask][1][b], dp[1 - (i & 1)][m - 1][mask][a][b]); } else { add(dp[i & 1][j][mask][a][b], 2 * dp[i & 1][j - 1][mask][a][b]); } } else { if (j == 0) { if (b == 0) { add(dp[i & 1][j][mask][0][b + 1], dp[1 - (i & 1)][m - 1][mask][a][b]); } add(dp[i & 1][j][mask | (1 << j)][a][b], dp[1 - (i & 1)][m - 1][mask][a][b]); } else { add(dp[i & 1][j][mask | (1 << j)][a][b], dp[i & 1][j - 1][mask][a][b]); add(dp[i & 1][j][mask][a][b], dp[i & 1][j - 1][mask][a][b]); } } } else { if (mask & (1 << j)) { if (j == 0) { add(dp[i & 1][j][mask][1][b], dp[1 - (i & 1)][m - 1][mask][a][b]); add(dp[i & 1][j][mask][a][b], dp[1 - (i & 1)][m - 1][mask][a][b]); } else { add(dp[i & 1][j][mask][1][b], dp[i & 1][j - 1][mask][a][b]); add(dp[i & 1][j][mask][a][b], dp[i & 1][j - 1][mask][a][b]); } } else { if (j == 0) { if (b == 0) { add(dp[i & 1][j][mask][a][b + 1], dp[1 - (i & 1)][m - 1][mask][a][b]); } add(dp[i & 1][j][mask | (1 << j)][1][b], dp[1 - (i & 1)][m - 1][mask][a][b]); } else { if (b == 0) { add(dp[i & 1][j][mask][a][b + 1], dp[i & 1][j - 1][mask][a][b]); } add(dp[i & 1][j][mask | (1 << j)][1][b], dp[i & 1][j - 1][mask][a][b]); } } } } else { if (j == 0) { add(dp[i & 1][j][mask & (~(1 << j))][0][b], dp[1 - (i & 1)][m - 1][mask][a][b]); } else { add(dp[i & 1][j][mask & (~(1 << j))][0][b], dp[i & 1][j - 1][mask][a][b]); } } } } } } } int mask = 0; for (int j = 0; j < m; j++) { if (grid[n - 1][j] != x ) { mask |= 1 << j; } } long long ans = 0; for (int nmask = 0; nmask < 1 << m; nmask++) { if (nmask | mask == mask) { for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { add(ans, dp[(n - 1) & 1][m - 1][nmask][a][b]); } } } } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; long long int n, k, a[100007]; pair<long long int, long long int> cur[1007]; bool res[200001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (long long int i = 1; i <= n; ++i) cin >> a[i]; long long int last = 0, cnt = 0; for (long long int t = 0; t <= 150000; ++t) { for (long long int i = 1; i <= k; ++i) { if (a[cur[i].second] == cur[i].first) { if (cur[i].second <= n && cur[i].second >= 1) cnt++; cur[i].second = ++last; cur[i].first = 0; } } long long int d = round((100.0 * cnt) / (1.0 * n)); for (long long int i = 1; i <= k; ++i) { if (cur[i].second <= n && cur[i].second >= 1 && cur[i].first + 1 == d) res[cur[i].second] = 1; cur[i].first++; } } long long int sum = 0; for (long long int i = 1; i <= n; ++i) sum += res[i]; cout << sum << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int s, x1, x2; cin >> s >> x1 >> x2; int t1, t2; cin >> t1 >> t2; int time_for_igor = abs(x1 - x2) * t2; int p, d; cin >> p >> d; int time_for_tram = -1; if (x2 > x1) { if (d == 1) { if (p > x1) { time_for_tram = s - p; time_for_tram += s; time_for_tram += x1; time_for_tram += (x2 - x1); time_for_tram *= t1; } else { time_for_tram = abs(x1 - p) * t1; time_for_tram += abs(x1 - x2) * t1; } } else { time_for_tram = p; time_for_tram += x1; time_for_tram += (x2 - x1); time_for_tram *= t1; } } else { if (d == -1) { if (p < x1) { time_for_tram = p; time_for_tram += s; time_for_tram += (s - x1); time_for_tram += (x1 - x2); time_for_tram *= t1; } else { time_for_tram = p - x1; time_for_tram += x1 - x2; time_for_tram *= t1; } } else { time_for_tram = s - p; time_for_tram += s - x1; time_for_tram += x1 - x2; time_for_tram *= t1; } } cout << min(time_for_tram, time_for_igor) << endl; } |
#include <bits/stdc++.h> using namespace std; const int N = 2005; bool f[N][N]; int x[3005], y[3005]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> x[i] >> y[i]; f[x[i] + 1000][y[i] + 1000] = true; } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if ((x[i] + x[j]) % 2 != 0 || (y[i] + y[j]) % 2 != 0) continue; int x_md = (x[i] + x[j]) / 2; int y_md = (y[i] + y[j]) / 2; if (f[x_md + 1000][y_md + 1000] == 1) ans++; } } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { float num; int t, n, d; cin >> num >> t >> n >> d; int a = (num / n); if ((num / n) > a) a += 1; int y = d, x = 0; for (int i = 0; i < a; i++) { if (x <= y) x += t; else y += t; } if (max(x, y) < (t * a)) cout << YES ; else cout << NO ; return 0; } |
#include <bits/stdc++.h> using namespace std; void _print(long long t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(unsigned long long t) { cerr << t; } template <class T, class V> void _print(T a[], V sz); template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << { ; _print(p.first); cerr << -> ; _print(p.second); cerr << } << endl; } template <class T, class V> void _print(T a[], V sz) { for (V i = 0; i < sz; i++) { _print(a[i]); cerr << ; } cerr << endl; } template <class T> void _print(vector<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] << endl; } template <class T> void _print(set<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] << endl; } template <class T> void _print(multiset<T> v) { cerr << [ ; for (T i : v) { _print(i); cerr << ; } cerr << ] ; } template <class T, class V> void _print(map<T, V> v) { cerr << [ ; for (auto i : v) { _print(i); cerr << ; } cerr << ] << endl; } void _printall(long long t) { cout << t; } void _printall(int t) { cout << t; } void _printall(string t) { cout << t; } void _printall(char t) { cout << t; } void _printall(long double t) { cout << t; } void _printall(double t) { cout << t; } void _printall(unsigned long long t) { cout << t; } template <class T, class V> void _printall(T a[], V sz); template <class T, class V> void _printall(pair<T, V> p); template <class T> void _printall(vector<T> v); template <class T> void _printall(set<T> v); template <class T, class V> void _printall(map<T, V> v); template <class T> void _printall(multiset<T> v); template <class T, class V> void _printall(T a[], V sz) { for (V i = 0; i < sz; i++) { _printall(a[i]); cout << ; } cout << endl; } template <class T, class V> void _printall(pair<T, V> p) { _printall(p.ff); cout << , ; _printall(p.ss); } template <class T> void _printall(vector<T> v) { for (T i : v) { _printall(i); cout << ; } cout << endl; } template <class T> void _printall(set<T> v) { for (T i : v) { _printall(i); cout << ; } cout << endl; } template <class T> void _printall(multiset<T> v) { for (T i : v) { _printall(i); cout << ; } cout << endl; } template <class T, class V> void _printall(map<T, V> v) { for (auto i : v) { _printall(i); cout << ; } cout << endl; } long long MAXN = 200001; long long spf[200001]; void sieve() { spf[1] = 1; for (int i = 2; i < MAXN; i++) { spf[i] = i; } for (long long i = 4; i < MAXN; i += 2) { spf[i] = 2; } for (long long i = 3; i * i < MAXN; i++) { if (spf[i] == i) { for (long long j = i * i; j < MAXN; j += i) { if (spf[j] == j) spf[j] = i; } } } } vector<long long> getFactorization(long long x) { vector<long long> ret; while (x != 1) { ret.push_back(spf[x]); x = x / spf[x]; } return ret; } void solve() { long long n; cin >> n; string s; cin >> s; vector<int> pre; int cnt = 0; for (int i = 0; i < n; i++) { pre.push_back(1); } for (int i = n - 2; i >= 0; i--) { if (s[i] == s[i + 1]) { pre[i] = pre[i + 1] + 1; } } reverse(pre.begin(), pre.end()); vector<int> fin; for (int i = 0; i < pre.size() - 1; i++) { if (pre[i] == 1 && pre[i + 1] == 1) { fin.push_back(pre[i]); } else if (pre[i] >= 1 && pre[i + 1] == 1) { fin.push_back(pre[i]); } } if (pre[n - 1] >= 1) { fin.push_back(pre[n - 1]); } int ind1 = fin.size() - 1; int ind2 = fin.size() - 1; while (pre.size() != 0 && ind1 >= 0 && ind2 >= 0) { if (fin[ind1] > 1) { cnt++; fin.pop_back(); if (ind2 == ind1) { ind2--; } ind1--; } else { while (fin[ind2] == 1) { ind2--; } if (ind2 < 0) { break; } else { cnt++; fin[ind2]--; fin.pop_back(); ind1--; } } } if (pre.size() != 0) { cnt += (fin.size() + 1) / 2; } cout << cnt << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long t = 1; cin >> t; while (t--) { solve(); } cerr << time taken : << (float)clock() / CLOCKS_PER_SEC << secs << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX_N = 14; int a[MAX_N]; void add(int st, int x) { for (int i = 0; i < MAX_N; i++) a[i] += x / MAX_N; for (int i = 1; i <= abs(x % MAX_N); i++) a[(st + i) % MAX_N] += (x < 0 ? -1 : 1); } long long point() { long long ans = 0; for (int i = 0; i < MAX_N; i++) if (a[i] % 2 == 0) ans += a[i]; return ans; } long long move(int st) { int x = a[st]; a[st] = 0, add(st, x); long long ans = point(); add(st, -x), a[st] = x; return ans; } int main() { ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); for (int i = 0; i < MAX_N; i++) cin >> a[i]; long long ans = 0; for (int i = 0; i < MAX_N; i++) ans = max(ans, move(i)); cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; bool isNonDegenerateTriangle(int a, int b, int c) { return (a + b > c && b + c > a && c + a > b); } bool isDegenerateTriangle(int a, int b, int c) { return (a + b == c || b + c == a || c + a == b); } int main() { int n; cin >> n; long long int a[n], s = 0; for (int i = 0; i < n; i++) { cin >> a[i]; s += a[i]; } sort(a, a + n); cout << 2 * a[n - 1] - s + 1; } |
#include <bits/stdc++.h> using namespace std; const int N = 260; const int mod = 777777777; int n, m, C[N][N], f[N][N][N]; int ans[9][1 << 8] = { {0}, {2, 1}, {32, 30, 80, 109}, {6824, 59808, 147224, 415870, 1757896, 1897056, 4898872, 7593125}, {776830421, 290516100, 746623577, 293783147, 33900006, 735127505, 565460332, 428982705, 472062098, 161873957, 117354594, 515619293, 578944191, 312106242, 569389279, 391464593}, {261086313, 584837659, 683961846, 468868529, 211593382, 736955478, 229471758, 157617135, 398169441, 360252438, 629394768, 264125799, 647490480, 342079395, 391579767, 225200475, 486011304, 513156108, 628771752, 132906648, 142138221, 20119449, 444199674, 195188679, 387329805, 44684703, 651912135, 737154512, 612549793, 519860281, 186175544, 212568440}, {240805271, 239509872, 581127897, 6511239, 156126222, 509425833, 672407328, 366667722, 459185405, 509737025, 554790222, 165216555, 703150560, 74806569, 398730015, 383350905, 506108358, 51326142, 298053147, 104256117, 391428765, 374020479, 206607807, 87664059, 275899176, 56407680, 551553401, 448939463, 582889860, 129676638, 226078251, 135769095, 61292868, 578972226, 190181628, 390739055, 184587732, 446575689, 732674124, 232198470, 676760679, 352474101, 611444862, 575661807, 628905585, 320813094, 522840969, 469781928, 156006018, 554473341, 239654268, 643714911, 433540170, 199307003, 496385218, 291740751, 67309914, 370826673, 202356819, 279421821, 421203111, 63744786, 520987612, 550671827}, {482164403, 768209115, 462063756, 154906374, 36099042, 341766705, 678182556, 621882744, 478771358, 231881111, 175889805, 243630450, 168908523, 671961765, 55761813, 652682670, 773939082, 517628076, 756201264, 124604900, 750976272, 498253248, 676047609, 137170026, 705610017, 495032139, 561797418, 703097347, 500815609, 95984586, 739707108, 265613565, 387099846, 777331779, 594676173, 591219559, 407997044, 208947235, 93337440, 478908360, 685013007, 487033953, 671903001, 39521181, 738490312, 33785059, 465470131, 310453920, 54648783, 346831137, 427694175, 474743430, 705296781, 435828036, 429824745, 663532359, 261388683, 244690731, 533997135, 596108961, 506813013, 371892402, 590145264, 104733162, 143420103, 654339672, 700348950, 685038942, 578826927, 286484229, 501639192, 434962491, 299270097, 27702486, 335375775, 111746817, 565603164, 294926121, 676063665, 735862995, 710035809, 437011960, 668528077, 138765186, 508213986, 615036450, 353784942, 624827616, 343900011, 241289776, 52410890, 72018835, 352406796, 415705878, 4802637, 376367145, 65589678, 333633477, 341834527, 303717460, 282387700, 42951006, 254706039, 423048528, 526429710, 68131467, 669954708, 12787348, 500636381, 317959019, 479433192, 657133515, 416259390, 610216692, 340129188, 44594256, 257373347, 138718678, 530767740, 292922628, 37220268, 605295159, 480722613, 458170419, 30540300, 487159055, 232966794, 149150650}, {412133651, 386543325, 139952108, 289303402, 102404925, 317067177, 396414708, 80515854, 663739304, 317300809, 228877044, 493725043, 715317967, 490300965, 315527373, 743539734, 488329191, 553627998, 533025234, 242583957, 706116537, 614109258, 645447222, 523195911, 492109128, 722623041, 111085128, 766395126, 654378921, 691964847, 496688157, 399056049, 654363234, 102052314, 191720088, 473910948, 259736526, 332840025, 388047555, 665791056, 627111387, 139696515, 441456687, 443032569, 283264821, 771641703, 452641455, 511306362, 117572859, 127701891, 721298331, 176520078, 357242229, 611296308, 696994956, 405628839, 429224274, 465336054, 695091546, 689828796, 574648641, 351220905, 507964023, 675326610, 517248963, 453528621, 220301928, 494463186, 681789969, 339589656, 44524053, 417125457, 339589404, 747135963, 341780733, 734158215, 396817281, 21997836, 5728464, 147611205, 456248898, 714128667, 377654949, 3862068, 128418948, 589390074, 304947090, 11703825, 228266073, 127304142, 429215724, 361541124, 521572968, 468358191, 341231688, 65323503, 613778508, 15985323, 291661029, 410970006, 591638112, 349541550, 89967528, 224922159, 361094166, 584206074, 640051812, 324264456, 652625388, 693768537, 11740617, 309238398, 211085469, 194905872, 639416484, 110110707, 296645895, 748118511, 131177718, 511142751, 775975599, 421403409, 475528473, 434685258, 1768977, 80301375, 708023862, 569195676, 56238084, 632887668, 88089750, 631539342, 396695565, 38780154, 695798271, 469819224, 439587099, 69045921, 682966116, 112310856, 64943298, 534475872, 40215357, 389728458, 286368453, 736006257, 501181650, 54829908, 603489402, 338032656, 512182818, 627500097, 462674016, 3103092, 157324491, 43978329, 596818971, 259025598, 9088632, 91991781, 577291428, 211245489, 429471231, 142626330, 172560633, 510907446, 444609585, 758102058, 375112647, 744786693, 276174402, 19259856, 233672418, 745389414, 225772848, 23385663, 324290610, 519804558, 120337812, 402578568, 360676008, 450089262, 551043738, 337388940, 512108856, 28879011, 690040638, 106017282, 558262341, 99972432, 608226003, 612152037, 42414435, 776201013, 39580443, 518796945, 494437752, 583194366, 723936555, 415359657, 309569589, 751104774, 166684527, 249229170, 353120823, 130668327, 753823584, 580966092, 561963717, 543672234, 393846327, 586278000, 327398400, 278403867, 156455586, 363920382, 190245195, 290039148, 547014447, 466218648, 146037150, 585462906, 666008595, 691786683, 374707494, 622498779, 231158277, 685740951, 115612245, 681825249, 545555745, 551718468, 277206615, 640171035, 757727334, 195193908, 658656684, 457760646, 225925875, 505761984, 18685233, 506832921, 112511021, 396846646, 290147622, 113924623, 669986155, 336008070, 63611061, 238586775, 119956662, 616557739, 772784623, 334527774, 410403148, 51933421}}; void inc(int& a, int b) { if (a += b, a >= mod) a -= mod; } void solve(int n) { memset(f, 0, sizeof(f)); for (int i = 0; i <= n; ++i) for (int j = 0; j <= i; ++j) { C[i][j] = !j ? 1 : (C[i - 1][j - 1] + C[i - 1][j]) % mod; } f[0][0][0] = 1; int tot = 0; for (int i = 1; i <= n; ++i) for (int l = 0; l <= n; ++l) for (int g = 0; g <= l; ++g) { int tmp = f[i - 1][l][g]; if (tmp) { for (int c = 0; l + c <= n; ++c) { inc(f[i][l + c][g + (c == i ? c : 0)], 1LL * tmp * C[l + c][c] % mod); } } } for (int k = 1; k <= n; ++k) { fprintf(stderr, %d %d n , n, k); if (k != 1) printf( , ); printf( %d , f[n][n][n - k]); } } void make_table() { printf( int ans[9][1 << 8] = { n ); for (int n = 0; n <= 8; ++n) { printf( { ); solve(1 << n); printf( }, n ); } printf( } ); } int main() { scanf( %d %d , &n, &m); int t = 0; while ((1 << t) != n) ++t; printf( %d n , ans[t][m - 1]); } |
#include <bits/stdc++.h> using namespace std; int n, m, tmp; long long A[1005]; long long B[1005]; long long Acost(int q) { q *= 4; long long ret = 0; for (int i = 1; i <= n; i++) { long long tmp2 = i * 4 - 2 - q; ret += A[i] * tmp2 * tmp2; } return ret; } long long Bcost(int q) { q *= 4; long long ret = 0; for (int i = 1; i <= m; i++) { long long tmp2 = i * 4 - 2 - q; ret += B[i] * tmp2 * tmp2; } return ret; } int ansx, ansy; long long sumx = LLONG_MAX, sumy = LLONG_MAX; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> tmp; A[i] += tmp; B[j] += tmp; } } for (int i = 0; i <= n; i++) { long long tmp3 = Acost(i); if (tmp3 < sumx) { ansx = i; sumx = tmp3; } } for (int i = 0; i <= m; i++) { long long tmp3 = Bcost(i); if (tmp3 < sumy) { ansy = i; sumy = tmp3; } } cout << sumx + sumy << endl; cout << ansx << << ansy << endl; } |
#include <bits/stdc++.h> using namespace std; const int MAX = 1005; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long rand(long long a, long long b) { return uniform_int_distribution<long long>(a, b)(rng); } const int K = 3; struct Hash { long long a[K]; Hash() : a() {} Hash operator^(const Hash &A) const { Hash ans = Hash(); for (int i = 0; i < K; i++) ans.a[i] = a[i] ^ A.a[i]; return ans; } bool operator==(const Hash &A) const { for (int i = 0; i < K; i++) if (a[i] != A.a[i]) return false; return true; } bool operator<(const Hash &A) const { for (int i = 0; i < K; i++) if (a[i] != A.a[i]) return a[i] < A.a[i]; return false; } }; Hash genHash() { Hash ret = Hash(); for (int i = 0; i < K; i++) ret.a[i] = rand(1, 1LL << 60); return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, s, d; cin >> n >> s >> d; Hash hash[n], sw[s]; for (int i = 0; i < n; i++) hash[i] = genHash(); for (int i = 0; i < s; i++) { int k; cin >> k; Hash cur = Hash(); while (k--) { int x; cin >> x; cur = cur ^ hash[x - 1]; } sw[i] = cur; } map<Hash, int> left_sz; int left = min(s / 2, 13); for (int mask = 0; mask < (1 << left); mask++) { Hash cur = Hash(); for (int bit = 0; bit < left; bit++) { if (!((mask >> bit) & 1)) continue; cur = cur ^ sw[bit]; } if (!left_sz.count(cur)) left_sz[cur] = __builtin_popcountll(mask); else left_sz[cur] = min(left_sz[cur], __builtin_popcountll(mask)); } map<Hash, int> right_sz; int right = s - left; for (int mask = 0; mask < (1 << right); mask++) { Hash cur = Hash(); for (int bit = 0; bit < right; bit++) { if (!((mask >> bit) & 1)) continue; cur = cur ^ sw[bit + left]; } if (!right_sz.count(cur)) right_sz[cur] = __builtin_popcountll(mask); else right_sz[cur] = min(right_sz[cur], __builtin_popcountll(mask)); } while (d--) { Hash cur = Hash(); int sz; cin >> sz; for (int i = 0; i < sz; i++) { int x; cin >> x; cur = cur ^ hash[x - 1]; } int ans = 100; for (auto p : left_sz) { Hash tmp = cur ^ p.first; if (right_sz.count(tmp)) ans = min(ans, p.second + right_sz[tmp]); if (left_sz.count(tmp)) { if (tmp == p.first) ans = 0; else ans = min(ans, p.second + left_sz[tmp]); } } cout << (ans == 100 ? -1 : ans) << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 210; int n; int a[N]; bool vis[N]; int lp[N]; int dis[N]; int inloop(int v) { vis[v] = 1; int u = a[v]; int cnt = 1; while (u != v && !vis[u]) { vis[u] = 1; ++cnt; u = a[u]; } if (u == v) return cnt; return 0; } int dfsandsetlp(int v) { if (lp[v] > 0) return dis[v]; int t = dfsandsetlp(a[v]); lp[v] = lp[a[v]]; return dis[v] = t + 1; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; return a / gcd(a, b) * b; } int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) { scanf( %d , a + i); } for (int i = 1; i <= n; ++i) { memset(vis, 0, sizeof(vis)); int t = inloop(i); lp[i] = t; } for (int i = 1; i <= n; ++i) { if (!lp[i]) { dis[i] = dfsandsetlp(i); } } long long res = 1; for (int i = 1; i <= n; ++i) { res = lcm(res, lp[i]); } int mx = 0; for (int i = 1; i <= n; ++i) { mx = max(mx, dis[i]); } if (res < mx) { int t = mx / res; if (res * t == mx) { res = mx; } else { res = res * (t + 1); } } printf( %I64d n , res); return 0; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.