func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> #pragma comment(linker, /STACK:100000000 ) using namespace std; int ri() { int x; scanf( %d , &x); return x; } int cnt[1000100]; int main() { int n, m; scanf( %d %d , &n, &m); long long ans = (long long)n * (long long)(n - 1) * (long long)(n - 2) / 6LL; for (int i = 0; i < m; i++) { int a, b; scanf( %d %d , &a, &b); cnt[a]++; cnt[b]++; } long long sub = 0; for (int i = 1; i <= n; i++) { if (cnt[i] != 0) { sub += (long long)cnt[i] * (n - 1 - (long long)cnt[i]); } } ans -= sub / 2LL; printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; long long solve() { long long n, T, a, b; cin >> n >> T >> a >> b; vector<long long> hard(n); long long x; long long hardcount = 0, easycount = 0; for (int i = 0; i < n; ++i) { cin >> x; if (x) { hard[i] = b; ++hardcount; } else { hard[i] = a; ++easycount; } } vector<long long> mand(n); for (int i = 0; i < n; ++i) { cin >> mand[i]; } vector<pair<long long, long long>> prob(n); for (int i = 0; i < n; ++i) { prob[i] = make_pair(mand[i], hard[i]); } sort(prob.begin(), prob.end()); int m_easy = 0, m_hard = 0; long long res = 0; long long musttime = 0; for (int i = 0; i < n; ++i) { long long time = prob[i].first - musttime; if (time > 0) { long long solved = m_easy + m_hard; --time; solved += min(time / a, easycount - m_easy); time -= min(time / a, easycount - m_easy) * a; solved += min(time / b, hardcount - m_hard); time -= min(time / b, hardcount - m_hard) * b; res = max(res, solved); } if (prob[i].second == b) { ++m_hard; musttime += b; } else { ++m_easy; musttime += a; } } long long time = T - musttime; if (time >= 0) res = n; return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(30); cout << fixed; int t; cin >> t; while (t--) { cout << solve() << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> void read(T& x) { cin >> x; } template <class H, class T> void read(pair<H, T>& x) { read(x.first); read(x.second); } template <class A> void read(vector<A>& v) { for (auto& x : v) read(x); } template <class H, class... T> void read(H& h, T&... t) { read(h); read(t...); } string to_string(const char* c) { return (string)c; } string to_string(char c) { return (string) + c; } string to_string(string second) { return second; } string to_string(bool b) { return b ? True : False ; } template <class A> string to_string(vector<A> v) { string second; for (auto& x : v) second += to_string(x) + ; return second; } template <class H, class... T> string to_string(H& h, T&... t) { string second; second += to_string(h); second += ; second += to_string(t...); return second; } void print(string second) { cout << second << n ; } template <class... A> void print(A... a) { print(to_string(a...)); } int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); } string tb(int x) { string ans; int c = x; for (int i = log2(x); i >= 0; --i) { if (pow(2, i) <= c) { ans += 1 ; c -= pow(2, i); } else { ans += 0 ; } } return ans; } bool ispr(int x) { if (x <= 1) { return false; } for (int i = 2; i <= sqrt(x); ++i) { if (x % i == 0) { return false; } } return true; } struct point { int x; int y; }; const int MOD = 1e9 + 7; const int MX = 2e5 + 5; const long long INF = 1e18; void solve() { double n, k; cin >> n >> k; double dist = 0; point prev; read(prev.x, prev.y); for (int i = 1; i < n; ++i) { point now; read(now.x, now.y); dist += sqrt((now.x - prev.x) * (now.x - prev.x) + (now.y - prev.y) * (now.y - prev.y)); prev = now; } printf( %.9f , (dist * k) / 50); } int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); bool isT = 0; if (isT) { int t; cin >> t; while (t--) { solve(); } return 0; } solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int A[100010]; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1, n, i, j; cin >> n; int cnt = 0; for (i = 0; i < n; i++) { cin >> A[i]; if (A[i] == 1) cnt++; } if (cnt) { cout << n - cnt << n ; } else { int cur = 0; for (i = 0; i < n; i++) cur = gcd(cur, A[i]); if (cur != 1) { cout << -1 n ; } else { int mn = n; for (i = 0; i < n; i++) { cur = A[i]; for (int j = i + 1; j < n; j++) { cur = gcd(cur, A[j]); if (cur == 1) { mn = min(mn, j - i); break; } } } cout << mn + n - 1 << n ; } } return 0; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, lft, curr = 1; scanf( %d , &n); lft = n; while (lft) { if (lft == 3) break; for (int j = 1; j <= (lft + 1) / 2; j++) { printf( %d , curr); } curr *= 2; lft -= (lft + 1) / 2; } if (lft == 3) { printf( %d %d %d n , curr, curr, curr * 3); } return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) using namespace std; const long long INF = 1e18; const int MOD = 1e9 + 7; const long long dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; const long long Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; long long in() { long long x; cin >> x; return x; } void print() { cout << n ; } template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) { cout << head; if (sizeof...(tail) != 0) cout << ; print(forward<Tail>(tail)...); } template <class T> void print(vector<T> &vec) { for (auto &a : vec) { cout << a; if (&a != &vec.back()) cout << ; } cout << n ; } template <class T> void print(set<T> &set) { for (auto &a : set) { cout << a << ; } cout << n ; } template <class T> void print(vector<vector<T>> &df) { for (auto &vec : df) { print(vec); } } long long power(long long x, long long n) { long long ret = 1; while (n > 0) { if (n & 1) ret *= x; x *= x; n >>= 1; } return ret; } long long comb(long long n, long long k) { vector<vector<long long>> v(n + 1, vector<long long>(n + 1, 0)); for (long long i = 0; i < (long long)((v).size()); i++) { v[i][0] = 1; v[i][i] = 1; } for (long long k = 1; k < (long long)((v).size()); k++) { for (long long j = 1; j < k; j++) { v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]); } } return v[n][k]; } void add(long long &a, long long b) { a += b; if (a >= MOD) a -= MOD; } template <class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } long long ceildiv(long long a, long long b) { if (b < 0) a = -a, b = -b; if (a >= 0) return (a + b - 1) / b; else return a / b; } long long floordiv(long long a, long long b) { if (b < 0) a = -a, b = -b; if (a >= 0) return a / b; else return (a - b + 1) / b; } long long floorsqrt(long long x) { assert(x >= 0); long long ok = 0; long long ng = 1; while (ng * ng <= x) ng <<= 1; while (ng - ok > 1) { long long mid = (ng + ok) >> 1; if (mid * mid <= x) ok = mid; else ng = mid; } return ok; } template <class T> bool contain(const std::string &s, const T &v) { return s.find(v) != std::string::npos; } __attribute__((constructor)) void faster_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } long long n, m, p; vector<string> fld; vector<long long> s; vector<vector<long long>> vis; vector<queue<pair<pair<long long, long long>, long long>>> q(9); void bfs() { bool changed = true; while (changed) { changed = false; for (long long ni = 0; ni < (long long)(9); ni++) { while ((!q[ni].empty()) and (q[ni].front().second < s[ni])) { auto current_pos = q[ni].front(); long long x = current_pos.first.first; long long y = current_pos.first.second; long long step = current_pos.second; q[ni].pop(); for (long long dir = 0; dir < 4; ++dir) { long long nx = x + dx[dir]; long long ny = y + dy[dir]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue; if (fld[nx][ny] == # ) continue; if (vis[nx][ny] == -1) { q[ni].push(make_pair(make_pair(nx, ny), step + 1)); vis[nx][ny] = ni; changed = true; } } } long long sz = (long long)((q[ni]).size()); vector<pair<long long, long long>> tmp; for (long long si = 0; si < (long long)(sz); si++) { auto current_pos = q[ni].front(); long long x = current_pos.first.first; long long y = current_pos.first.second; long long step = current_pos.second; q[ni].pop(); tmp.push_back(make_pair(x, y)); } for (auto i : tmp) { q[ni].push(make_pair(i, 0LL)); } } } } signed main() { cin >> n >> m >> p; s.assign(p, 0); vis.assign(n, vector<long long>(m, -1)); for (long long pi = 0; pi < (long long)(p); pi++) { cin >> s[pi]; } for (long long ni = 0; ni < (long long)(n); ni++) { string ss; cin >> ss; fld.push_back(ss); } for (long long ni = 0; ni < (long long)(n); ni++) { for (long long mi = 0; mi < (long long)(m); mi++) { if (fld[ni][mi] != . and fld[ni][mi] != # ) { q[fld[ni][mi] - 1 ].push(make_pair(make_pair(ni, mi), 0LL)); vis[ni][mi] = fld[ni][mi] - 1 ; } } } bfs(); vector<long long> ans(p); for (long long ni = 0; ni < (long long)(n); ni++) { for (long long mi = 0; mi < (long long)(m); mi++) { if (vis[ni][mi] != -1) ans[vis[ni][mi]]++; } } print(ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int l[105], r[105]; int dp[105][100005]; int temp[100005]; int q[100005][2], cntl, cntr; int main() { int n, k; scanf( %d%d , &n, &k); for (int i = 1; i <= k; i++) scanf( %d%d , &l[i], &r[i]); k++; l[k] = r[k] = 2 * n; for (int i = 1; i <= 100005; i++) dp[0][i] = 1005; dp[0][0] = 0; for (int i = 1; i <= k; i++) { for (int j = 0; j <= n; j++) { dp[i][j] = 1005; if (j >= l[i] - r[i - 1]) dp[i][j] = min(dp[i][j], dp[i - 1][j - (l[i] - r[i - 1])]); if (r[i - 1] >= j) dp[i][j] = min(dp[i][j], dp[i - 1][r[i - 1] - j] + 1); } if (r[i] != l[i]) { for (int j = 0; j <= n; j++) { temp[j] = 1005; if (j >= r[i] - l[i]) temp[j] = min(temp[j], dp[i][j - (r[i] - l[i])]); if (l[i] >= j) temp[j] = min(temp[j], dp[i][l[i] - j] + 1); } cntl = 0; cntr = 0; for (int j = 0; j <= n; j++) { if (j <= l[i]) { while (cntl != cntr && q[cntr][1] >= dp[i][l[i] - j] + 1) cntr--; cntr++; q[cntr][0] = j; q[cntr][1] = dp[i][l[i] - j] + 1; } if (cntl != cntr && q[cntl + 1][0] + r[i] - l[i] == j) cntl++; if (cntl != cntr) temp[j] = min(q[cntl + 1][1], temp[j]); } for (int j = 0; j <= n; j++) dp[i][j] = temp[j]; } } if (dp[k][n] > 3 * k) printf( Hungry n ); else printf( Full n%d n , dp[k][n]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k, idx = 0; cin >> n >> k; pair<int, int> inst[n]; vector<int> v; for (int i = 1; i <= n; i++) { int t; cin >> t; inst[i - 1] = make_pair(t, i); } sort(inst, inst + n); while (k >= inst[idx].first && idx < n) { v.push_back(inst[idx].second); k -= inst[idx].first; idx++; } cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long i, j, n, a[100001], b[100001], c[100001], d[100001]; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; b[i] = c[i] = a[i]; } for (i = 0; i < n - 1; i++) { if (b[i] >= b[i + 1]) b[i + 1] = b[i] + 1; } for (i = n - 1; i > 0; i--) { if (c[i] >= c[i - 1]) c[i - 1] = c[i] + 1; } for (i = 0; i < n; i++) { if (b[i] >= c[i]) { j = i; break; } } for (i = 0; i <= j; i++) d[i] = b[i]; for (i = j + 1; i < n; i++) d[i] = c[i]; long long sum = 0; for (i = 0; i < n; i++) { sum += (d[i] - a[i]); } cout << sum << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int dx[] = {-1, 0, 0, 1}; const int dy[] = {0, -1, 1, 0}; int cnt[2][10]; int main() { int a, b; cin >> a >> b; while (a % 2 == 0 || a % 3 == 0 || a % 5 == 0) { if (a % 2 == 0) { a /= 2; cnt[0][2]++; } if (a % 3 == 0) { a /= 3; cnt[0][3]++; } if (a % 5 == 0) { a /= 5; cnt[0][5]++; } } while (b % 2 == 0 || b % 3 == 0 || b % 5 == 0) { if (b % 2 == 0) { b /= 2; cnt[1][2]++; } if (b % 3 == 0) { b /= 3; cnt[1][3]++; } if (b % 5 == 0) { b /= 5; cnt[1][5]++; } } if (a != b) puts( -1 ); else { cout << abs(cnt[0][2] - cnt[1][2]) + abs(cnt[0][3] - cnt[1][3]) + abs(cnt[0][5] - cnt[1][5]) << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll my_exp(ll a, ll b) { if (b == 0) { return 1; } else { ll temp = my_exp(a, b / 2); temp *= temp; if (b % 2 == 1) { temp *= a; } return temp; } } class mycomparison { bool reverse; public: mycomparison(const bool& revparam = false) { reverse = revparam; } bool operator()(const ll& lhs, const ll& rhs) const { if (reverse) return (lhs > rhs); else return (lhs < rhs); } }; int main() { std::priority_queue<ll, std::vector<ll>, greater<ll> > fourth; ll n, m; vector<ll> insert, getMin, removeMin; vector<pair<ll, ll> > order; std::cin >> n; string a; for (ll i = 0; i < n; ++i) { std::cin >> a; if (a != removeMin ) scanf( %lld n , &m); if (a == insert ) { order.push_back(make_pair(0, m)); fourth.push(m); } if (a == removeMin ) { if (fourth.empty()) { order.push_back(make_pair(0, 1)); fourth.push(1); } order.push_back(make_pair(1, 1)); fourth.pop(); } if (a == getMin ) { if (fourth.empty()) { order.push_back(make_pair(0, m)); fourth.push(m); } while (fourth.top() < m) { fourth.pop(); order.push_back(make_pair(1, m)); if (fourth.empty()) { order.push_back(make_pair(0, m)); fourth.push(m); } } if (fourth.top() == m) { order.push_back(make_pair(3, m)); } else { fourth.push(m); order.push_back(make_pair(0, m)); order.push_back(make_pair(3, m)); } } } std::cout << (ll)order.size() << std::endl; for (typeof(order.begin()) it = order.begin(); it != order.end(); it++) { if ((*it).first == 0) { std::cout << insert << (*it).second << std::endl; } if ((*it).first == 1) { std::cout << removeMin << std::endl; } if ((*it).first == 3) { std::cout << getMin << (*it).second << std::endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int d[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; int a, b, res; int main() { scanf( %d%d , &a, &b); for (int i = a; i <= b; i++) { int t = i; while (t) { res += d[t % 10]; t /= 10; } } printf( %d n , res); return 0; } |
#include <bits/stdc++.h> using namespace std; void read() { freopen( in.txt , r , stdin); freopen( out.txt , w , stdout); } bool voi(char c) { return (c == a || c == e || c == i || c == y || c == o || c == u ); } long long log_pow(long long b, long long p) { if (p == 0) return 1; if (p == 1) return b; if (p % 2) return b * log_pow(b, p - 1); long long r = log_pow(b, p / 2); return r * r; } int d5l_i() { int x; scanf( %d , &x); return x; } long long d5l_l() { long long x; scanf( %lld , &x); return x; } char d5l_c() { char x; scanf( %c , &x); return x; } long long fact(int n) { long long fc = 1; for (int i = 2; i <= n; i++) fc *= i; return fc; } int len(long long n) { int c = 0; while (n) { n /= 2; c++; } return c; } bool why(int a, int b, int c) { return ((long long)a + b > c && (long long)c + b > a && (long long)c + a > b); } int a[100005]; long long ans; int main() { int n = d5l_i(), m = d5l_i(); for (int i = 0; i < n; i++) a[i] = d5l_i(); sort(a, a + n); vector<int> v; bool flag = false, ops = false; for (int i = 1; i < a[0]; i++) { if (ans + i > m) { ops = true; break; } ans += i; v.push_back(i); } if (!ops) for (int i = 0; i < n - 1; i++) { for (int j = a[i] + 1; j < a[i + 1]; j++) { if (ans + j > m) { flag = true; break; } ans += j; v.push_back(j); } if (flag) break; } if (!flag && !ops) for (int i = a[n - 1] + 1; i <= 1e9; i++) { if (ans + i > m) break; ans += i; v.push_back(i); } printf( %d n , v.size()); for (int i = 0; i < v.size(); i++) printf( %d , v[i]); } |
#include <bits/stdc++.h> const long long N = 50050; const long long Q = 2e18; const long long mod = 1e9 + 7; const long long MAGIC = 30; using namespace std; int a, b, c, n; void solve() { cin >> a >> b >> c >> n; if (a + b - c >= n || c > a || c > b) { cout << -1 << n ; } else { cout << n - (a + b - c) << n ; } } bool mtest = false; int main() { ios_base::sync_with_stdio(0); int TE = 1; if (mtest) cin >> TE; while (TE--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; vector<long long int> vec; map<long long int, long long int> mp; int main() { long long int x = 0, y = 0, z = 0, sum = 0; cin >> x; for (int i = 0; i < x; i++) { cin >> y; vec.push_back(y); mp[y]++; } sort(vec.begin(), vec.end()); for (int i = 0; i < vec.size(); i++) { for (int j = 0; j <= 30; j++) { z = powl(2, j) - vec[i]; if (binary_search(vec.begin() + i + 1, vec.end(), z) == true) { if (z == vec[i]) { sum += --mp[z]; } else { sum += mp[z]; } } } } cout << sum << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { printf( YES nNO nNO nYES nNO nNO nNO nNO nNO nYES nYES nYES nYES nYES nYES nYES nNO nYES nNO nYES nYES nNO nYES nNO nNO nYES nNO nNO nYES nYES nNO nYES nNO nYES nNO nYES nYES nYES nNO nYES nYES nNO nYES nYES nNO nYES nYES n YES nNO nYES nNO nNO nYES nYES nYES nYES nYES nYES nNO nNO nYES nNO nNO nNO nNO nNO nNO nNO nNO nNO nYES nNO nYES nNO nNO nNO nYES nYES nNO nYE S nYES nNO nNO nNO nNO nNO nNO nYES nYES nNO nYES nNO nYES nYES nNO nNO nNO nYES nYES nYES nYES nYES nNO nNO nNO nYES nNO nYES nNO nYES nNO nYE S nNO nNO nYES nYES nYES nYES nNO nNO nNO nYES nNO nYES nYES nYES nNO nY ES nNO nNO nNO nYES nNO nNO nNO nYES nYES nYES nYES nYES nYES nYES nYES nYES nYES nYES nNO nYES nNO nNO nNO nNO nNO nYES nNO nNO nYES nNO nYES nNO nYES nNO nYES nNO nYES nYES nYES nNO nNO nYES nNO nNO nNO nNO nYES nNO nYES nNO nNO nYES nNO nYES nYES nYES nNO nNO nNO nYES nYES nNO nYES nNO nYES nYES nYES nNO nYES nYES nNO nYES nNO nYES nYES nNO nYES nYES n NO nNO nYES nNO nNO nNO nYES nYES nNO nYES n ); return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; struct State { int bk; string fr; int pos; int cnt; }; bool operator<(State lhs, State rhs) { if (lhs.bk != rhs.bk) return lhs.bk < rhs.bk; if (lhs.fr != rhs.fr) return lhs.fr < rhs.fr; if (lhs.pos != rhs.pos) return lhs.pos < rhs.pos; if (lhs.cnt != rhs.cnt) return lhs.cnt < rhs.cnt; return false; } int main() { int n; cin >> n; string s; cin >> s; vector<int> jump(s.size() + 1, 0); for (int i = 0; i <= s.size(); i++) { int to = 0; for (int j = 0; j < i; j++) { bool ok = true; for (int k = 0; k < j; k++) { ok &= (s[k] == s[i - j + k]); } if (i != s.size()) ok &= (s[j] != s[i]); if (ok) to = j + 1 - (int)(i == s.size()); } jump[i] = to; } set<string> subs; for (int i = 0; i < s.size(); i++) { for (int j = i + 1; j <= s.size(); j++) { string ss; for (int k = i; k < j; k++) { ss += s[k]; } subs.insert(ss); } } set<string> lasts; string ss; for (int i = s.size() - 1; i >= 0; i--) { ss = s[i] + ss; lasts.insert(ss); } map<State, ll> dp; dp[State{-1, , 0, 0}] = 1; for (int i = 0; i < n; i++) { map<State, ll> next; for (auto &e : dp) { int bk = e.first.bk; string str = e.first.fr; int pos = e.first.pos; int cnt = e.first.cnt; if (str != @ ) { str += s[pos]; if (subs.count(str)) { if (lasts.count(str)) { bk = i; } } else { str = @ ; } } if (++pos == s.size()) { pos = jump[pos]; cnt = true; } next[State{bk, str, pos, cnt}] += e.second; bk = e.first.bk; str = e.first.fr; pos = e.first.pos; cnt = e.first.cnt; if (str != @ ) { str += (s[pos] == 0 ? 1 : 0 ); if (subs.count(str)) { if (lasts.count(str)) { bk = i; } } else { str = @ ; } } pos = jump[pos]; next[State{bk, str, pos, cnt}] += e.second; } dp = next; } ll res = 0; for (auto &e : dp) { int bk = e.first.bk; string strs = e.first.fr; int pos = e.first.pos; int cnt = e.first.cnt; string str; for (int i = 0; i < e.first.pos; i++) { str += s[i]; } for (int i = s.size() - e.first.bk - 1; i < s.size(); i++) { str += s[i]; } for (int i = 0; i <= (int)str.size() - (int)s.size(); i++) { if (s == str.substr(i, s.size())) { cnt = true; } } if (cnt >= 1) { res += e.second; } } cout << res << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; using lli = long long int; using pii = pair<int, int>; using vi = vector<int>; using vb = vector<bool>; using vvi = vector<vector<int>>; using vlli = vector<long long int>; using vpii = vector<pair<int, int>>; const int N = 200005; const int blockSize = sqrt(N); lli n, t, l, r, x, idx, L, R, res; vlli a, ans, cnt(1000005); vector<tuple<lli, lli, lli, lli>> q; int get(int idx) { return idx / blockSize; } void add(lli x) { res += x * (2 * (cnt[x]++) + 1); } void del(lli x) { res += x * (-2 * (cnt[x]--) + 1); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> t; a.resize(n); ans.resize(t); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < t; i++) { cin >> l >> r; l--; r--; q.push_back(make_tuple(get(l), r, l, i)); } sort((q).begin(), (q).end()); L = R = 0; add(a[0]); for (int i = 0; i < t; i++) { tie(ignore, r, l, idx) = q[i]; while (L < l) del(a[L++]); while (L > l) add(a[--L]); while (R < r) add(a[++R]); while (R > r) del(a[R--]); ans[idx] = res; } for (int i = 0; i < t; i++) cout << ans[i] << n ; } |
#include <bits/stdc++.h> using namespace std; long long bigmod(long long a, int p, long long int m) { if (p == 0) return 1; if (p & 1) { return ((a % m) * (bigmod(a, p - 1, m))) % m; } else { long long tmp = bigmod(a, p / 2, m); return (tmp * tmp) % m; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; pair<long long int, long long int> p[n]; for (long long int i = 0; i < n; i++) { long long int x; cin >> x; p[i].first = -x; p[i].second = i; } sort(p, p + n); vector<long long int> v[n]; vector<pair<long long int, long long int> > p1; for (long long int i = 0; i < n; i++) { p1.push_back(make_pair(p[i].second, p[i].first)); sort(p1.begin(), p1.end()); for (long long int t = 0; t < p1.size(); t++) { v[i].push_back(-p1[t].second); } } long long int m; cin >> m; while (m--) { long long int a; cin >> a; long long int b; cin >> b; cout << v[a - 1][b - 1]; cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const double eps = 3e-5; double x, h, l; int main() { scanf( %lf , &x); for (double h = 1; h <= 10 + eps; h++) { if (h + eps < x) continue; l = h / (sqrt((h * h) / (x * x) - 1)) * 2.0; for (double q = 1; q <= 10 + eps; q++) if (l - q < eps && q - l < eps) { printf( %d %d , (int)q, (int)h); return 0; } } } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a[10], b[10]; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i] == b[j]) { printf( %d , a[i]); if (i != n - 1) printf( ); } } } return 0; } |
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a < b) return gcd(b, a); else if (b == 0) return a; else return gcd(b, a % b); } long long pow(long long b, long long e) { if (e == 0) return 1; else if (e % 2 == 0) { long long a = pow(b, e / 2); return a * a; } else { long long a = pow(b, e / 2); return b * a * a; } } long long powm(long long x, long long y, long long m = 1000000007) { x = x % m; long long res = 1; while (y) { if (y & 1) res = res * x; res %= m; y = y >> 1; x = x * x; x %= m; } return res; } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } long long modInverse(long long a, long long m) { return powm(a, m - 2, m); } bool issq(long long n) { long long p = sqrt(n); return p * p == n; } long long MIN(long long a = 2e18, long long b = 2e18, long long c = 2e18, long long d = 2e18, long long e = 2e18) { return min(a, min(b, min(c, min(d, e)))); } long long MAX(long long a = -2e18, long long b = -2e18, long long c = -2e18, long long d = -2e18, long long e = -2e18) { return max(a, max(b, max(c, max(d, e)))); } vector<int> prime; void seive(long long n) { prime.resize(n + 5, 0); long long j; for (long long i = 2; i <= n; i++) { if (!prime[i]) { prime[i] = i; for (j = i * i; j <= n; j += i) if (!prime[j]) prime[j] = i; } } } long long extended_GCD(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long gcd = extended_GCD(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; } long long mulInv(long long a, long long mod) { long long x, y; extended_GCD(a, mod, x, y); if (x < 0) x += mod; return x; } void __print(int x) { cout << x; } void __print(long x) { cout << x; } void __print(long long x) { cout << x; } void __print(unsigned x) { cout << x; } void __print(unsigned long x) { cout << x; } void __print(unsigned long long x) { cout << x; } void __print(float x) { cout << x; } void __print(double x) { cout << x; } void __print(long double x) { cout << x; } void __print(char x) { cout << << x << ; } void __print(const char *x) { cout << << x << ; } void __print(const string &x) { cout << << x << ; } void __print(bool x) { cout << (x ? true : false ); } template <typename T, typename V> void __print(const pair<T, V> &x) { cout << { ; __print(x.first); cout << , ; __print(x.second); cout << } ; } template <typename T> void __print(const T &x) { int f = 0; cout << { ; for (auto &i : x) cout << (f++ ? , : ), __print(i); cout << } ; } void _print() { cout << ] n ; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << , ; _print(v...); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long a[n + 1]; long long f = 0; for (long long i = 1; i <= n; i++) { cin >> a[i]; if (a[1] != a[i]) f = 1; } if (f == 0) cout << -1; else { long long d = 15e6; seive(d); long long hcf = a[1]; for (long long i = 2; i <= n; i++) hcf = gcd(hcf, a[i]); map<long long, long long> mp; for (long long i = 1; i <= n; i++) { a[i] = a[i] / hcf; long long x = a[i]; while (x != 1) { long long p = prime[x]; mp[p]++; while (x % p == 0) x /= p; } } long long ans = 0; for (auto x : mp) ans = max(ans, x.second); cout << n - ans; cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MID = 1e5; const int MAX = 1e9; int main() { int n; cin >> n; if (n % 2) { cout << n - 9 << << 9; } else { cout << n - 8 << << 8; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7, M = 2e5 + 7; int n, m, k, t; vector<int> b; struct Node { int in, out; long long h; Node() {} Node(int i, int o, long long _h) : in(i), out(o), h(_h) {} } a[N]; bool operator<(const Node &a, const Node &b) { if (a.out == b.out) return a.in < b.in; return a.out < b.out; } long long fen[M]; void add(int i, long long v) { for (; i <= (int)b.size(); i += i & -i) fen[i] = max(fen[i], v); } long long get(int i) { long long res = 0; for (; i > 0; i -= i & -i) res = max(res, fen[i]); return res; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d%d%lld , &a[i].in, &a[i].out, &a[i].h); b.push_back(a[i].in), b.push_back(a[i].out); } sort(a + 1, a + n + 1); sort(b.begin(), b.end()), b.resize((int)(unique(b.begin(), b.end()) - b.begin())); for (int i = n; i > 0; i--) { k = (lower_bound(b.begin(), b.end(), a[i].out) - b.begin()) + 1; t = (lower_bound(b.begin(), b.end(), a[i].in) - b.begin()) + 1; add(t, get(k - 1) + a[i].h); } printf( %lld , get((int)b.size())); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; if (n % 3 == 0) { cout << n / 3 * 2; } if (n % 3 > 0) { cout << (n - 1) / 3 * 2 + 1; } return 0; } |
#include <bits/stdc++.h> using namespace std; void solve() { int n, m; cin >> n >> m; vector<long long> vect; vect.assign(n, 0); long long ans = 0; for (int i = 0; i < n; i++) cin >> vect[i]; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; ans += min(vect[a - 1], vect[b - 1]); } cout << ans << endl; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); } |
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { T 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 modinverse(T a, T M) { return bigmod(a, M - 2, M); } template <class T> inline T lcm(T a, T b) { a = abs(a); b = abs(b); return (a / gcd(a, b)) * b; } template <class T, class X> inline bool getbit(T a, X i) { T t = 1; return ((a & (t << i)) > 0); } template <class T, class X> inline T setbit(T a, X i) { T t = 1; return (a | (t << i)); } template <class T, class X> inline T resetbit(T a, X i) { T t = 1; return (a & (~(t << i))); } inline long long power(long long a, long long b) { long long multiply = 1; for (int i = (1); i <= (b); i++) { multiply *= a; } return multiply; } inline long long ABS(long long a) { return (a >= 0) ? a : -a; } long long n, in, m; map<long long, vector<long long> > mp; pair<long long, long long> fact[200003]; int main() { scanf( %lld , &n); for (int i = (1); i <= (n); i++) { scanf( %lld , &in); mp[in].push_back(i); } for (int i = (1); i <= (n); i++) { scanf( %lld , &in); mp[in].push_back(i); } scanf( %lld , &m); fact[0] = make_pair(1, 0); long long ans = 1, cnt, temp; for (long long i = (1); i <= (200000); i++) { temp = i; cnt = 0; while (temp % 2 == 0) { temp /= 2; cnt++; } fact[i].first = (fact[i - 1].first * temp) % m; fact[i].second = cnt + fact[i - 1].second; } vector<long long> v; for (__typeof((mp).begin()) it = (mp).begin(); it != (mp).end(); it++) { v = it->second; if (v.size() == 1) continue; sort(v.begin(), v.end()); ans *= fact[v.size()].first; ans %= m; cnt = 0; for (int i = (1); i <= (v.size() - 1); i++) { if (v[i] == v[i - 1]) cnt++; } ans *= bigmod(2LL, fact[v.size()].second - cnt, m); ans %= m; } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; struct node { int u, s; node() { u = s = 0; } bool operator<(const node &temp) const { if (this->s < temp.s) return 1; return 0; } }; node black[100005]; node white[100005]; vector<vector<node> > adj; int main() { int n; scanf( %d , &n); adj.resize(n); bool allzero = true; int w, b; w = b = 0; for (int i = 0; i < n; ++i) { int c; scanf( %d , &c); if (c == 0) { white[w].u = i; scanf( %d , &white[w].s); if (white[w].s) allzero = false; ++w; } else { black[b].u = i; scanf( %d , &black[b].s); if (black[b].s) allzero = false; ++b; } } if (allzero) { if (w < b) { for (int i = 0; i < b; ++i) cout << white[0].u + 1 << << black[i].u + 1 << << 0 << endl; for (int i = 1; i < w; ++i) cout << white[i].u + 1 << << black[i].u + 1 << << 0 << endl; } else { for (int i = 0; i < w; ++i) cout << black[0].u + 1 << << white[i].u + 1 << << 0 << endl; for (int i = 1; i < b; ++i) cout << black[i].u + 1 << << white[i].u + 1 << << 0 << endl; } return 0; } sort(black, black + b); sort(white, white + w); for (int indb = 0, indw = 0; indb < b && indw < w;) { if (white[indw].s < black[indb].s) { black[indb].s -= white[indw].s; adj[black[indb].u].push_back(white[indw]); ++indw; } else { white[indw].s -= black[indb].s; adj[white[indw].u].push_back(black[indb]); ++indb; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < adj[i].size(); ++j) { printf( %d %d %d n , i + 1, adj[i][j].u + 1, adj[i][j].s); } } return 0; } |
#include <bits/stdc++.h> using namespace std; vector<string> vs, vs2; set<string> st; map<string, int> mp, mp1; long long i, j, k, x, y, m, n, a, b, c, d, e, f, g, h, l, o, p, q, t, u, v, w, z, r, ln, neg, pos, sm, ar[500000 + 5], br[500000 + 5], fr[60], cnt; string s, s1, s2, s3; int main() { while (getline(cin, s)) { ln = s.size(); long long in = 0; for (int i = z ; i >= a ; i--) { for (j = in; j < ln; j++) { if (i == (int)s[j]) { in = max(in, j); cout << s[j]; } } } cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 22; const int INF = 1e9 + 2; long long n, ans, mark[MAXN]; char str[MAXN]; int main() { cin >> n; cin >> str; for (int i = 0; i < n; i++) { if (mark[str[i]] == 0) { mark[str[i]] = 1; ans++; } } if (n > 26) cout << -1; else cout << n - ans; return 0; } |
#include <bits/stdc++.h> using namespace std; using LL = long long; using DD = long double; using pii = pair<int, int>; const LL INFLL = 0x7f7f7f7f7f7f7f7fLL; const int INFint = 0x7f7f7f7f; LL exactMatchingSubranges(vector<int> &cumulative, int match) { LL rr = 0; for (int i = 0; i < int((int((cumulative).size()))); ++i) { int looking_for = match ^ cumulative[i]; int p = lower_bound((cumulative).begin(), (cumulative).end(), looking_for) - cumulative.begin(); int q = upper_bound((cumulative).begin(), (cumulative).end(), looking_for) - cumulative.begin(); rr += q - p; } return rr; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.precision(16); int n, k; cin >> n >> k; vector<int> aa(n, 0); for (int i = 0; i < int(n); ++i) cin >> aa[i]; vector<int> cumulative((int((aa).size())), 0); cumulative[0] = aa[0]; int amax = 0; for (int i = int(1); i < int((int((aa).size()))); ++i) { cumulative[i] = cumulative[i - 1] ^ aa[i]; amax = max(amax, aa[i]); } cumulative.push_back(0); sort((cumulative).begin(), (cumulative).end()); LL rr = exactMatchingSubranges(cumulative, k); while (true) { if ((k & 1) == 0) { k |= 1; rr += exactMatchingSubranges(cumulative, k); } k >>= 1; amax >>= 1; if (amax == 0) break; for (int i = 0; i < int((int((cumulative).size()))); ++i) cumulative[i] >>= 1; } cout << rr / 2 << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5, maxm = 4e5 + 5; int head[maxn], nume; struct edge { int v, w, c, next; } e[maxm]; inline void init_edge() { memset(head, -1, sizeof head); nume = 0; } inline void add_edge(int u, int v, int w = 0, int c = 0) { e[nume].v = v; e[nume].w = w; e[nume].c = c; e[nume].next = head[u]; head[u] = nume++; } int dep[maxn], dp[maxn][20], numpow = 19; void dfs(int u, int f) { dep[u] = dep[f] + 1; dp[u][0] = f; for (int i = 1; i <= numpow; ++i) dp[u][i] = dp[dp[u][i - 1]][i - 1]; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].v; if (v == f) continue; dfs(v, u); } } int lca(int u, int v) { if (dep[u] > dep[v]) swap(u, v); if (dep[u] < dep[v]) { int tmp = dep[v] - dep[u]; for (int i = 0; i <= numpow; ++i) if (tmp & (1 << i)) v = dp[v][i]; } if (u != v) { for (int i = numpow; ~i; --i) if (dp[u][i] != dp[v][i]) u = dp[u][i], v = dp[v][i]; u = v = dp[u][0]; } return u; } int ff(int u, int num) { for (int i = 0; (1 << i) <= num; i++) { if (num & (1 << i)) u = dp[u][i]; } return u; } int f[maxn]; int find(int x) { if (x == f[x]) return x; return f[x] = find(f[x]); } int he(int u, int v) { u = find(u); v = find(v); if (u != v) f[u] = v; } queue<int> que; int n, k, r, q; int vis[maxn]; int main() { scanf( %d%d%d , &n, &k, &r); init_edge(); for (int i = 1; i < n; i++) { int u, v; scanf( %d%d , &u, &v); add_edge(u, v); add_edge(v, u); } memset(vis, -1, sizeof vis); for (int i = 1; i <= r; i++) { int u; scanf( %d , &u); vis[u] = 0; que.push(u); } while (!que.empty()) { int u = que.front(); que.pop(); for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].v; if (vis[v] == -1) { vis[v] = vis[u] + 1; que.push(v); } } } for (int i = 1; i <= n; i++) f[i] = i; for (int u = 1; u <= n; u++) { for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].v; if (vis[u] + vis[v] < k) { he(u, v); } } } dfs(1, 0); scanf( %d , &q); while (q--) { int u, v, Lca, U, V; scanf( %d%d , &u, &v); Lca = lca(u, v); if (dep[u] + dep[v] - 2 * dep[Lca] <= k) printf( YES n ); else { if (dep[u] - dep[Lca] >= (k + 1) / 2) U = ff(u, (k + 1) / 2); else U = ff(v, dep[u] + dep[v] - 2 * dep[Lca] - (k + 1) / 2); if (dep[v] - dep[Lca] >= (k + 1) / 2) V = ff(v, (k + 1) / 2); else V = ff(u, dep[u] + dep[v] - 2 * dep[Lca] - (k + 1) / 2); if (find(U) == find(V) && vis[U] <= k / 2 && vis[V] <= k / 2) printf( YES n ); else printf( NO n ); } } } |
#include <bits/stdc++.h> using namespace std; int main() { int k, n, w, q, cem = 0; cin >> k >> n >> w; for (int i = 1; i <= w; i++) { cem = cem + i * k; } if (n <= cem) { cout << cem - n; } else cout << 0; return 0; } |
#include <bits/stdc++.h> using namespace std; int cnt_occur(string s, string t) { int cnt = 0; for (int i = 0; i < s.size(); ++i) { if (s.substr(i, 3) == t) cnt++; } return cnt; } long max(int a, int b) { return a >= b ? a : b; } void swap(int &a, int &b) { int temp = a; a = b; b = temp; } int power(int x, unsigned int y) { int temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else return x * temp * temp; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } void soE(int n, bool f[]) { for (int i = 0; i < n; ++i) f[i] = true; f[0] = f[1] = false; for (int p = 2; p * p <= n; p++) { if (f[p] == true) { for (int i = p * p; i <= n; i += p) f[i] = false; } } } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; ; while (t--) { long long a; cin >> a; long long cnt = 0; long long temp = a; while (a) { if (temp % 2 == 0) { if (a % 2 == 0) { if ((a / 2) % 2 != 0 or a == 4) { cnt += a / 2; if (a == 4) { a = a / 2; } else a = a / 2 - 1; } else { cnt += 1; a -= 2; } } } else { if (a == 5) { cnt += 2; break; } cnt += 1; a = a - 1; if (a == 0) break; if ((a / 2) % 2 == 0) a -= 1; else a = a / 2; } } cout << cnt << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; void RI() {} template <typename... T> void RI(int& head, T&... tail) { scanf( %d , &head); RI(tail...); } void RLL() {} template <typename... T> void RLL(long long& head, T&... tail) { scanf( %lld , &head); RLL(tail...); } void print() { putchar( n ); } template <typename T, typename... U> void print(T head, U... tail) { cout << head << ; print(tail...); } const int MOD = 1000000007; int main() { int k, ans = 0; RI(k); for (int i = 0; i < (int)(k); i++) { int r; RI(r); ans = max(ans, r - 25); } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int K, N, M, Q; map<string, int> Basic, F[111]; string st, Comp_St[60]; vector<pair<string, int> > Comp[60]; int main() { cin >> K >> N >> M >> Q; for (int i = 1; i <= N; i++) { cin >> st; Basic[st] = i; } for (int i = 0, k; i < M; i++) { cin >> st; st = st.substr(0, st.size() - 1); Comp_St[i] = st; for (string stt; cin >> stt;) { cin >> k; Comp[i].push_back(make_pair(stt, k)); char ch; scanf( %c , &ch); if (ch != , ) break; } } for (int i = 0, p; i < Q; i++) { cin >> p >> st; F[p][st]++; for (int j = 0; j < M; j++) { st = Comp_St[j]; bool flag = true; for (int k = 0; k < Comp[j].size(); k++) if (F[p][Comp[j][k].first] < Comp[j][k].second) flag = false; if (flag) { F[p][st]++; for (int k = 0; k < Comp[j].size(); k++) F[p][Comp[j][k].first] -= Comp[j][k].second; } } } for (int i = 1; i <= K; i++) { int TOT = 0; for (map<string, int>::iterator it = F[i].begin(); it != F[i].end(); it++) if (it->second > 0) TOT++; printf( %d n , TOT); for (map<string, int>::iterator it = F[i].begin(); it != F[i].end(); it++) if (it->second > 0) cout << it->first << << it->second << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; struct team { string name; int score; int goals_for; int goals_against; int goal_diff() const { return goals_for - goals_against; } }; bool operator<(const team &A, const team &B) { if (A.score != B.score) return A.score > B.score; if (A.goal_diff() != B.goal_diff()) return A.goal_diff() > B.goal_diff(); return A.goals_for > B.goals_for; } map<string, team> m; int main(void) { int n; scanf( %d , &n); for (int i = 0; i < n; ++i) { static char buff[128]; scanf( %s , buff); team T; T.name = string(buff); T.score = T.goals_against = T.goals_for = 0; m[T.name] = T; } for (int i = 0; i < n * (n - 1) / 2; ++i) { static char buff[128]; int ga, gb; scanf( %s %d:%d , buff, &ga, &gb); string s(buff); int f = s.find( - ); team &a = m[s.substr(0, f)]; team &b = m[s.substr(f + 1)]; a.goals_for += ga; a.goals_against += gb; b.goals_for += gb; b.goals_against += ga; if (ga > gb) a.score += 3; else if (ga < gb) b.score += 3; else { a.score += 1; b.score += 1; } } vector<team> teams; for (map<string, team>::iterator it = m.begin(); it != m.end(); ++it) teams.push_back(it->second); sort(teams.begin(), teams.end()); vector<string> ret; for (int i = 0; i < n / 2; ++i) ret.push_back(teams[i].name); sort(ret.begin(), ret.end()); for (int i = 0; i < n / 2; ++i) printf( %s n , ret[i].c_str()); return 0; } |
#include <bits/stdc++.h> using namespace std; int read() { int tot = 0, fh = 1; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) fh = -1; c = getchar(); } while (c >= 0 && c <= 9 ) { tot = tot * 10 + c - 0 ; c = getchar(); } return tot * fh; } void getstr(char *a) { char c = getchar(); int len = 0; while (!((c >= A && c <= Z ) || (c >= a && c <= z ) || (c >= 0 && c <= 9 ))) c = getchar(); while ((c >= A && c <= Z ) || (c >= a && c <= z ) || (c >= 0 && c <= 9 )) { a[++len] = c; c = getchar(); } } const int maxn = 200100; int n, t, q, ans, block, num, belong[maxn]; long long a[maxn], L[2000], R[2000], sum[2000], tag[2000]; void build() { block = sqrt(n); num = n / block; if (num * block != n) num++; for (int i = 1; i <= num; i++) { L[i] = block * (i - 1) + 1; R[i] = block * i; R[num] = n; for (int j = L[i]; j <= R[i]; j++) { belong[j] = i; sum[i] += a[j]; } } } int fd(int l, int r, int lim) {} int main() { n = read(); q = read(); for (int i = 1; i <= n; i++) a[i] = read(); build(); while (q--) { t = read(); if (t == 1) { int x = read(); long long y = read(); for (int i = 1; i < belong[x]; i++) if (tag[i] == 0) { if (a[R[i]] < y) { if (a[L[i]] <= y) { tag[i] = y; sum[i] = (R[i] - L[i] + 1ll) * tag[i]; } else { sum[i] = 0; for (int j = L[i]; j <= R[i]; j++) { a[j] = max(a[j], y); sum[i] += a[j]; } } } } else { if (tag[i] < y) { tag[i] = y; sum[i] = (R[i] - L[i] + 1ll) * tag[i]; } } int cur = belong[x]; sum[cur] = 0; for (int i = L[cur]; i <= R[cur]; i++) if (i <= x) { a[i] = max(a[i], max(y, tag[cur])); sum[cur] += a[i]; } else { a[i] = max(a[i], tag[cur]); sum[cur] += a[i]; } tag[cur] = 0; } else { int x = read(); long long y = read(); ans = 0; int cur = belong[x]; for (int i = x; i <= R[cur]; i++) { long long tmp = max(tag[cur], a[i]); if (y >= tmp) { y -= tmp; ans++; tmp = max(tag[cur], a[R[cur]]); if (tmp > y) break; } } for (int i = belong[x] + 1; i <= num; i++) { long long tmp = sum[i]; if (y >= tmp) { y -= tmp; ans += R[i] - L[i] + 1; } else { tmp = max(tag[i], a[R[i]]); if (tmp > y) continue; for (int j = L[i]; j <= R[i]; j++) { tmp = max(tag[i], a[j]); if (y >= tmp) { y -= tmp; ans++; tmp = max(tag[i], a[R[i]]); if (tmp > y) break; } } } } cout << ans << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; struct BIT { static const int N = 200006; long long a[N]; int n; void init(int _n) { n = _n; memset(a, 0, sizeof(a)); } void update(int pos, long long val) { for (int i = pos; n >= i; i += i & (-i)) { a[i] += val; } } long long query(int pos) { long long ret = 0; for (int i = pos; i > 0; i -= i & (-i)) { ret += a[i]; } return ret; } } bit; const int N = 200006; const int P = 32; set<pair<long long, long long> > st[P]; long long a[N]; void addd(long long val, int pos) { if (!val) st[0].insert(make_pair(pos, val)); else { for (int i = P - 1; i >= 1; i--) { if (val >= (1LL << (i - 1))) { st[i].insert(make_pair(pos, val)); break; } } } } void dell(long long val, int pos) { if (!val) st[0].erase(make_pair(pos, val)); else { for (int i = P - 1; i >= 1; i--) { if (val >= (1LL << (i - 1))) { st[i].erase(make_pair(pos, val)); break; } } } } int main() { int n, q; scanf( %d %d , &n, &q); bit.init(n); for (int i = 1; n >= i; i++) { scanf( %lld , &a[i]); bit.update(i, a[i]); addd(a[i], i); } while (q--) { int p, x; scanf( %d %d , &p, &x); dell(a[p], p); bit.update(p, -a[p]); a[p] = x; addd(a[p], p); bit.update(p, a[p]); int ans = -1; for (int i = 0; P > i; i++) { auto iter = st[i].begin(); int cnt = 2; while (cnt--) { if (iter == st[i].end()) break; pair<long long, long long> p = (*iter); if (bit.query(p.first - 1) == p.second) { ans = p.first; break; } ++iter; } } printf( %d n , ans); } } |
#include <bits/stdc++.h> using namespace std; struct P { int x, y; }; P me; P other; bool white; int N, M; P operator+(P a, P b) { return {a.x + b.x, a.y + b.y}; } P operator-(P a, P b) { return {a.x - b.x, a.y - b.y}; } bool operator==(P a, P b) { return a.x == b.x && a.y == b.y; } bool isItAStep(P a, P b) { a = a - b; int dx = abs(a.x); int dy = abs(a.y); return (dx == 1 && dy == 2) || (dx == 2 && dy == 1); } P winWhite, winBlack; void step(P newP) { if (white && isItAStep(winWhite, me)) { cout << winWhite.x << << winWhite.y << endl << flush; exit(0); } if (!white && isItAStep(winBlack, me)) { cout << winBlack.x << << winBlack.y << endl << flush; exit(0); } if (isItAStep(other, me)) { cout << other.x << << other.y << endl << flush; exit(0); } me = newP; cout << me.x << << me.y << endl << flush; if (white && me.x == N / 2 && me.y == M / 2) exit(0); if (!white && me.x == N / 2 + 1 && me.y == M / 2) exit(0); } void getStep() { cin >> other.x >> other.y; if (other == me) exit(1); if (other.x == -1) exit(0); } const vector<P> ize = { {+1, +2}, {-1, +2}, {+1, -2}, {-1, -2}, {+2, +1}, {-2, +1}, {+2, -1}, {-2, -1}, }; vector<P> dfs(P from, P to, int K) { if (from == to) return vector<P>(1, to); if (K == 0) return vector<P>(); vector<P> best; for (const P& x : ize) { P to2 = to + x; if (to2.x < 1 || to2.y < 1) continue; if (to2.x > N || to2.y > M) continue; vector<P> c = dfs(from, to2, K - 1); if (best.size() == 0 || (!c.empty() && c.size() < best.size())) best = c; } if (best.empty()) return best; best.push_back(to); return best; } vector<P> closestDistance_(P from, P to) { int dx = abs(to.x - from.x); int dy = abs(to.y - from.y); int ix = (to.x > from.x ? 1 : -1); int iy = (to.y > from.y ? 1 : -1); vector<P> S; while (dx > 5 || dy > 5) { int wx = 2, wy = 1; if (dx < dy) { wx = 1; wy = 2; } ix = (to.x > from.x ? 1 : -1); iy = (to.y > from.y ? 1 : -1); from.x += wx * ix; from.y += wy * iy; S.push_back(from); dx = abs(to.x - from.x); dy = abs(to.y - from.y); } vector<P> ize = dfs(from, to, 6); assert(!ize.empty()); auto it = ize.begin(), end = ize.end(); it++; while (it != end) { S.push_back(*it); it++; } return S; } void check(vector<P>& S, P from, P to) { if (S.empty()) return; P last = from; assert(to == S.back()); for (const P& p : S) { assert(isItAStep(last, p)); last = p; } } int main() { int xw, yw, xb, yb; cin >> N >> M; cin >> xw >> yw >> xb >> yb; winWhite = {N / 2, M / 2}; winBlack = {N / 2 + 1, M / 2}; P whitee = {xw, yw}; P blackk = {xb, yb}; vector<P> cdww = closestDistance_(whitee, winWhite); vector<P> cdbw = closestDistance_(blackk, winWhite); vector<P> cdwb = closestDistance_(whitee, winBlack); vector<P> cdbb = closestDistance_(blackk, winBlack); vector<P> cd; white = true; bool white_kerget = ((xw ^ xb ^ yw ^ yb) & 1) == 1; cerr << W->w: << cdww.size() << endl; cerr << W->b: << cdwb.size() << endl; cerr << B->w: << cdbw.size() << endl; cerr << B->b: << cdbb.size() << endl; cerr << (white_kerget ? White kerget : Black kerget ) << endl; if (white_kerget) { cerr << white kerget << endl; if (cdwb.size() <= cdbb.size() + 1) { cerr << white bevarja << endl; white = true; cd = move(cdwb); } else if (cdww.size() <= cdbb.size()) { cerr << white megelozi << endl; white = true; cd = move(cdww); } else { white = false; cd = move(cdbb); } } else { cerr << black kerget << endl; if (cdbw.size() <= cdww.size()) { cerr << black bevarja << endl; white = false; cd = move(cdbw); } else if (cdbb.size() < cdww.size()) { cerr << black megelozi << endl; white = false; cd = move(cdbb); } else { white = true; cd = move(cdww); } } cdww.clear(); cdwb.clear(); cdbw.clear(); cdbb.clear(); if (white) { me = whitee; other = blackk; cout << WHITE << endl; } else { me = blackk; other = whitee; cout << BLACK << endl; getStep(); } if (!white) { cd.push_back(winWhite + P{2, 1}); cd.push_back(winWhite + P{0, 2}); cd.push_back(winWhite + P{1, 0}); } else if (white) { cd.push_back(winBlack - P{2, 1}); cd.push_back(winBlack - P{0, 2}); cd.push_back(winBlack - P{1, 0}); } int i = 0; while (i < cd.size()) { step(cd[i]); getStep(); i++; } return 0; } |
#include <bits/stdc++.h> using namespace std; char s[200005]; bool vis[200005]; int a[200005]; int n, m; double p; void doit() { memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; ++i) { scanf( %s , s); if (s[0] != ? ) { a[i] = atoi(s); vis[i] = 1; } } for (int i = 1; i <= m; ++i) { a[n + i] = 1001000000; vis[n + i] = 1; } n += m; long long ans = 0; for (int i = 1; i <= m; ++i) { int last = -1001000000, ff, t = 0; for (int j = i; j <= n; j += m) if (vis[j]) { if (a[j] - last <= t) { printf( Incorrect sequence n ); return; } if (!t) { last = a[j]; continue; } if (last <= 0 && a[j] >= 0) { if (last < 0 && a[j] > 0) t--; if (!t) { for (int k = ff; k < j; k += m) a[k] = 0; last = a[j]; continue; } int k1 = max(0, last + 1), k2 = 0; while (t) { if (k2 + 1 < a[j]) k2++, t--; if (!t) break; if (k1 - 1 > last) k1--, t--; } last = k1 - 1; for (int k = ff; k < j; k += m) a[k] = ++last; last = a[j]; } else if (last > 0) { for (int k = ff; k < j; k += m) a[k] = ++last; last = a[j]; t = 0; } else { last = a[j]; for (int k = j - m; k >= ff; k -= m) a[k] = --last; last = a[j]; t = 0; } } else { t++; if (t == 1) ff = j; } } n -= m; for (int i = 1; i <= n - 1; ++i) printf( %d , a[i]); printf( %d n , a[n]); } int main() { while (scanf( %d%d , &n, &m) != EOF) doit(); } |
#include <bits/stdc++.h> using namespace std; bool comp(int a, int b) { return (a < b); } int max(int a, int b) { if (a > b) return a; return b; } int min(int a, int b) { if (a < b) return a; return b; } long long power(long long a, int x) { while (x--) { a *= a; } return a; } int main() { int n, i = 0, x; cin >> n; string s, c; cin >> s; while (i < n) { x = i; if (s[i] == o ) while (s[i + 1] == g && s[i + 2] == o ) i += 2; if (x < i) c += *** ; else c += s[i]; i++; } cout << c; } |
#include <bits/stdc++.h> using namespace std; long long int arr[378999]; long long int arr2[234597]; long long int ab[10]; vector<long long int> adj[22222]; map<long long int, long long int> mp, mp2; map<long long int, long long int>::iterator it; vector<long long int> vec; string s; long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t, i, loss, j, diff; cin >> t; while (t--) { long long int n, x, y, k, maxi = INT_MIN, mini = INT_MAX, two = 0, one1 = 0, one2 = 0, zero = 0, p; long long int cs, cw, w; mp.clear(); mp2.clear(); string s, ans = ; cin >> s; cin >> x; long long int len = s.length(); for (i = 0; i < len; i++) { ans += 1 ; } long long int f = 0; for (i = 0; i < len; i++) { if (s[i] == 1 ) { continue; } else if (s[i] == 0 ) { if (i - x >= 0) { ans[i - x] = 0 ; } if (i + x < len) { ans[i + x] = 0 ; } } } for (i = 0; i < len; i++) { if (s[i] == 1 ) { if ((i - x >= 0 && ans[i - x] == 1 ) || (i + x < len && ans[i + x] == 1 )) continue; else { f = 1; break; } } } if (f == 1) cout << -1 n ; else cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int a[1505]; int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); int ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= i - 1; j++) if (a[j] > a[i]) ans ^= 1; int q; scanf( %d , &q); while (q--) { int a, b; scanf( %d%d , &a, &b); int sz = b - a + 1; sz = sz * (sz - 1) / 2; if (sz % 2 == 1) ans ^= 1; if (ans == 0) printf( even n ); else printf( odd n ); } } |
#include <bits/stdc++.h> using namespace std; int n; string str; int main() { cin >> str; n = str.length(); string str1 = str + str; reverse(str.begin(), str.end()); string str2 = str + str; int ans = 0; int dp[2][2 * n]; for (int i = 0; i < 2 * n - 1; i++) { dp[0][i] = 1; if (i > 0 && str1[i] != str1[i - 1]) { dp[0][i] += min(n - 1, dp[0][i - 1]); } ans = max(ans, dp[0][i]); } for (int i = 0; i < 2 * n - 1; i++) { dp[1][i] = 1; if (i > 0 && str2[i] != str2[i - 1]) { dp[1][i] += min(n - 1, dp[1][i - 1]); } ans = max(ans, dp[1][i]); } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int n; int a[1111111]; bool curr_state[22]; long long sum_inversions[22]; long long sum_non_inversions[22]; int tmp_array1[1111111]; int tmp_array2[1111111]; int binary_search(int i, int sz) { if (tmp_array2[0] >= tmp_array1[i]) return -1; int low = 0; int high = sz - 1; while (low < high) { int mid = (low + high + 1) / 2; if (tmp_array2[mid] >= tmp_array1[i]) high = mid - 1; else low = mid; } return low; } long long find_num_inversions(int sz) { sort(tmp_array2, tmp_array2 + sz); long long answer = 0; for (int i = 0; i < sz; i++) { int j = binary_search(i, sz); answer += j + 1; } return answer; } int main() { scanf( %d , &n); for (int i = 0; i < (1 << n); i++) scanf( %d , &a[i]); for (int level = 1; level <= n; level++) { sum_inversions[level - 1] = 0; sum_non_inversions[level - 1] = 0; for (int j = 0; j < (1 << n); j += (1 << (n - level + 1))) { int l1 = j; int u1 = j + (1 << (n - level)) - 1; int l2 = j + (1 << (n - level)); int u2 = j + (1 << (n - level + 1)) - 1; for (int i = l1; i <= u1; i++) { tmp_array1[i - l1] = a[i]; } for (int j = l2; j <= u2; j++) { tmp_array2[j - l2] = a[j]; } sum_inversions[level - 1] += find_num_inversions(u1 - l1 + 1); for (int i = l1; i <= u1; i++) { tmp_array2[i - l1] = a[i]; } for (int j = l2; j <= u2; j++) { tmp_array1[j - l2] = a[j]; } sum_non_inversions[level - 1] += find_num_inversions(u1 - l1 + 1); } } for (int i = 0; i <= n - 1; i++) { curr_state[i] = false; } int m; scanf( %d , &m); for (int i = 1; i <= m; i++) { int q; scanf( %d , &q); for (int j = n - q; j <= n - 1; j++) curr_state[j] = (!curr_state[j]); long long answer = 0; for (int level = 0; level <= n - 1; level++) answer += (curr_state[level] ? sum_non_inversions[level] : sum_inversions[level]); cout << answer << endl; } } |
#include <bits/stdc++.h> using namespace std; vector<int> t; int rec(int k, int sz, int n = 0) { if (t[n] <= k) return k - t[n]; if (n >= sz && t[n]) return n; int a = rec(k, sz, 2 * n + 1); if (a >= sz) return a; else return rec(a, sz, 2 * n + 2); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m, q1 = 0, q2 = 0, k = 1; cin >> n >> m; vector<int> a(m), el; for (int i = 0; i < m; ++i) cin >> a[i]; while (k < n) k *= 2; k *= 2; k--; t.resize(k); for (int i = 0; i < n; ++i) { int p; cin >> p; if (p == 0 || p == 1) { el.push_back(p); int index = k / 2 + q1; t[index] = 1; while (index != 0) { index = index / 2 - (index % 2 == 0); t[index] = t[2 * index + 1] + t[2 * index + 2]; } q1++; q2++; } else { vector<int> rm; int l = -1, r = m; while (l + 1 < r) { int mid = (l + r) / 2; if (a[mid] > q2) r = mid; else l = mid; } q2 -= l + 1; for (int j = 0; j <= l; ++j) rm.push_back(rec(a[j] - 1, k / 2)); for (int x : rm) { t[x] = 0; while (x != 0) { x = x / 2 - (x % 2 == 0); t[x] = t[2 * x + 1] + t[2 * x + 2]; } } } } bool empty = true; for (int i = 0; i < el.size(); ++i) if (t[k / 2 + i]) empty = false, cout << el[i]; if (empty) cout << Poor stack! ; } |
#include <bits/stdc++.h> using namespace std; inline long long read() { char ch = getchar(); long long nega = 1; while (!isdigit(ch)) { if (ch == - ) nega = -1; ch = getchar(); } long long ans = 0; while (isdigit(ch)) { ans = ans * 10 + ch - 48; ch = getchar(); } if (nega == -1) return -ans; return ans; } long long a[40005], b[40005]; long long u[40005], v[40005], n, m; signed main() { cin >> n >> m; for (long long i = 1; i <= n; i++) a[i] = read(); for (long long j = 1; j <= m; j++) b[j] = read(); vector<long long> s; for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= m; j++) { s.push_back(a[i] + b[j]); u[a[i] + b[j] + 20000] |= (1LL << (i - 1)); v[a[i] + b[j] + 20000] |= (1LL << (j - 1)); } } sort(s.begin(), s.end()); s.erase(unique(s.begin(), s.end()), s.end()); long long ans = 0; for (long long i : s) { for (long long j : s) { long long r = u[i + 20000] | u[j + 20000], s = v[i + 20000] | v[j + 20000]; ans = max(ans, 0LL + __builtin_popcountll(r) + __builtin_popcountll(s)); } } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> inline bool uin(T& a, T b) { return a > b ? (a = b, true) : false; } template <class T> inline bool uax(T& a, T b) { return a < b ? (a = b, true) : false; } const int nax = 2e5 + 10; vector<int> offer[nax]; int K[nax]; int N, M; bool can(int day) { vector<int> last(N, -1); for (int i = 0; i <= min(day, nax - 1); ++i) { for (int x : offer[i]) { last[x] = i; } } vector<int> need(K, K + N); vector<int> temp_offer[nax]; for (int i = 0; i < N; ++i) if (last[i] != -1) temp_offer[last[i]].push_back(i); int money = 0; for (int d = 0; d <= day; ++d) { ++money; if (d > 2e5) continue; for (int x : temp_offer[d]) { if (money >= need[x]) { money -= need[x]; need[x] = 0; } else { need[x] -= money; money = 0; break; } } } for (int i = 0; i < N; ++i) ; ; return money >= 2 * accumulate(need.begin(), need.end(), 0); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M; for (int i = 0; i < N; ++i) cin >> K[i]; for (int d, t, i = 0; i < M; ++i) { cin >> d >> t; --d, --t; offer[d].push_back(t); } for (int i = 0; i < 10; ++i) ; int l = 0, r = 4e5; while (l < r) { int m = (l + r) / 2; ; if (can(m)) r = m; else l = m + 1; } cout << r + 1 << n ; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; struct modint { int n; modint() : n(0) {} modint(int n) : n(n) {} }; modint operator+(modint a, modint b) { return modint((a.n += b.n) >= mod ? a.n - mod : a.n); } modint operator*(modint a, modint b) { return modint(1LL * a.n * b.n % mod); } bool operator<(modint a, modint b) { return a.n < b.n; } bool operator!=(modint a, modint b) { return a.n != b.n; } const int S = 4; using modints = array<modint, S>; modints operator+(modints a, modints b) { modints ret; for (int i = 0; i < S; i++) { ret[i] = a[i] + b[i]; } return ret; } modints operator*(modints a, modints b) { modints ret; for (int i = 0; i < S; i++) { ret[i] = a[i] * b[i]; } return ret; } bool operator<(modints a, modints b) { for (int i = 0; i < S; i++) { if (a[i] != b[i]) return a[i] < b[i]; } return false; } const int N = 1e5; int n; vector<int> g[N]; vector<int> gg[N]; modints rnd[N]; int height[N]; vector<int> h; modints val[N]; modints one; map<modints, int> mp; pair<int, int> ans; int cnt = 0; void add(modints a) { if (mp[a]++ == 0) { cnt++; } } void del(modints a) { if (mp[a]-- == 1) { cnt--; } assert(mp[a] >= 0); } void dfs(int u, int p) { for (int v : g[u]) if (v != p) { dfs(v, u); height[u] = max(height[u], height[v] + 1); } val[u] = one; for (int v : g[u]) if (v != p) { val[u] = val[u] * (rnd[height[u]] + val[v]); } add(val[u]); } void dfs2(int u, int p) { auto tmp = val[u]; del(tmp); int n = g[u].size(); h.clear(); h.push_back(0); for (int v : g[u]) { h.push_back(height[v] + 1); } sort(h.rbegin(), h.rend()); int h0 = h[0]; int h1 = h[1]; modints L0 = one; modints L1 = one; vector<modints> R0(n); vector<modints> R1(n); R0[n - 1] = one; R1[n - 1] = one; for (int i = n - 2; i >= 0; i--) { R0[i] = R0[i + 1] * (rnd[h0] + val[g[u][i + 1]]); R1[i] = R1[i + 1] * (rnd[h1] + val[g[u][i + 1]]); } ans = max(ans, make_pair(cnt, u)); for (int i = 0; i < n; i++) { int v = g[u][i]; if (height[v] + 1 != h0) { height[u] = h0; val[u] = L0 * R0[i]; } else { height[u] = h1; val[u] = L1 * R1[i]; } L0 = L0 * (rnd[h0] + val[v]); L1 = L1 * (rnd[h1] + val[v]); if (v == p) continue; add(val[u]); dfs2(v, u); del(val[u]); } add(tmp); } int main() { mt19937 mt(time(NULL) ^ 1234567); uniform_int_distribution<int> uni(0, mod - 1); for (int i = 0; i < N; i++) { for (int j = 0; j < S; j++) { rnd[i][j] = uni(mt); } } for (int i = 0; i < S; i++) { one[i] = 1; } cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; scanf( %d %d , &u, &v); u--; v--; g[u].push_back(v); g[v].push_back(u); } if (n == 1) { cout << 1 << endl; return 0; } dfs(0, -1); dfs2(0, -1); cout << ans.second + 1 << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int a, ta; cin >> a >> ta; int b, tb, h = 0, m = 0, num = 0; int totmin; cin >> b >> tb; string s; cin >> s; for (int i = 0; i < 2; i++) { h = h * 10 + (s[i] - 0 ); } for (int i = 3; i < 5; i++) { m = m * 10 + (s[i] - 0 ); } totmin = h * 60 + m; for (int i = 5 * 60; i <= 1439; i += b) { if ((i <= totmin && i + tb > totmin) || (i >= totmin && i < totmin + ta)) num++; } cout << num; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = (1ll << 32); const int LOGN = 14; const long long INF = (1ll << 62); long long __gcd(long long a, long long b) { if (b == 0) return a; return __gcd(b, a % b); } bool comp(pair<int, unordered_set<int> >& a, pair<int, unordered_set<int> >& b) { return a.second.size() > b.second.size(); } int main() { std ::ios_base ::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); int n, m; cin >> n >> m; vector<pair<int, unordered_set<int> > > a(n); int sz; int ct = 0; int ct_p1 = 0; for (int i = 0; i < n; ++i) { cin >> sz; ct += sz; a[i].first = i; int x; for (int j = 0; j < sz; ++j) { cin >> x; a[i].second.insert(x); } } ct_p1 = ct % n; ct /= n; sort(a.begin(), a.end(), comp); int l = 0; int r = n - 1; vector<vector<int> > ans; while (l < r) { int req_l_size = ct; int req_r_size = ct; if (l < ct_p1) req_l_size++; if (r < ct_p1) req_r_size++; vector<int> temp; for (auto it : a[l].second) { if ((int)a[l].second.size() - (int)temp.size() == req_l_size) break; if (a[r].second.size() == req_r_size) break; if (a[r].second.find(it) == a[r].second.end()) { temp.push_back(it); a[r].second.insert(it); vector<int> x; x.push_back(a[l].first); x.push_back(a[r].first); x.push_back(it); ans.push_back(x); } } for (int i = 0; i < temp.size(); ++i) a[l].second.erase(temp[i]); if (a[r].second.size() == req_r_size) r--; if (a[l].second.size() == req_l_size) l++; } cout << ans.size() << endl; for (int i = 0; i < ans.size(); ++i) cout << ans[i][0] + 1 << << ans[i][1] + 1 << << ans[i][2] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf( %d , &t); string s1, s2; cin >> s1 >> s2; int temp; vector<int> index; for (int i = 0; i < t; i++) { temp = -1; if (s1[i] == s2[i]) { continue; } for (int j = i + 1; j < t; j++) { if (s1[j] == s2[i]) { temp = j; break; } } if (temp == -1) { printf( %d , temp); return 0; } for (int j = temp - 1; j >= i; j--) { swap(s1[j], s1[j + 1]); index.push_back(j + 1); } } printf( %d n , index.size()); for (int i = 0; i < index.size(); i++) { printf( %d , index[i]); } } |
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << << x << ; } void __print(const char *x) { cerr << << x << ; } void __print(const string &x) { cerr << << x << ; } void __print(bool x) { cerr << (x ? true : false ); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << { ; __print(x.first); cerr << , ; __print(x.second); cerr << } ; } template <typename T> void __print(const T &x) { int f = 0; cerr << { ; for (auto &i : x) cerr << (f++ ? , : ), __print(i); cerr << } ; } void _print() { cerr << ] n ; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << , ; _print(v...); } const long long inf = 9223372036854775807; const long long mod = 1000000007; const double pi = acos(-1); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); long long n, k; cin >> n >> k; long long val = n - (n % 2); if (n == 1) { if (k == 0) { cout << 1; } else { cout << -1; } return 0; } if (val / 2 > k) { cout << -1; } else { long long x = (val / 2 - 1); cout << k - x << << (k - x) * 2 << ; set<long long> s; s.insert(k - x); s.insert((k - x) * 2); long long t = 2; for (int i = 0; i < x; i++) { if (s.find(t) == s.end() && s.find(t + 1) == s.end()) { cout << t << << t + 1 << ; s.insert(t); s.insert(t + 1); t += 2; } else { t += 2; i--; } } t += 2; if (n % 2 != 0) { while (t == (k - x) || t == (k - x) * 2) { t++; } cout << t; } } return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } void solve() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < (int)(n); ++i) cin >> a[i]; set<int> st; vector<int> ans; for (int i = 0; i < (int)(n); ++i) { if (st.find(a[i]) == st.end()) { ans.push_back(i + 1); } st.insert(a[i]); } if (ans.size() < k) cout << NO ; else { cout << YES << n ; for (int i = 0; i < (int)(k); ++i) cout << ans[i] << ; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); int T = 1; while (T--) { solve(); cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 100; const int MX = 200503; int N, M, k; int arr[MX]; int cnt[MX]; vector<int> G[MX]; int vis[MX]; vector<int> sr; void dfs(int x) { vis[x] = 1; for (int i = 0; i < G[x].size(); i++) { int ch = G[x][i]; if (vis[ch] == 1) { sr.push_back(ch); } else if (!vis[ch]) dfs(ch); } vis[x] = 2; } int get(int x, int p) { int ret = arr[x]; vis[x] = 1; for (int i = 0; i < G[x].size(); i++) { int ch = G[x][i]; if (vis[ch]) continue; ret = min(ret, get(ch, x)); } return ret; } int main() { scanf( %d , &N); for (int i = 1; i <= N; i++) { scanf( %d , &arr[i]); } for (int i = 1; i <= N; i++) { int x; scanf( %d , &x); G[i].push_back(x); } long long an = 0; for (int i = 1; i <= N; i++) if (vis[i] == 0) dfs(i); memset(vis, 0, sizeof vis); for (int i = 0; i < sr.size(); i++) an += get(sr[i], -1); cout << an << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; long long nChooseR(int n, int k); long long gcd(long long a, long long b); vector<string> split(string target, char c); bool isPrime(long long g); long long n, k, r, l, t, x, y, m, a[100003]; long long ans; int main() { string s; cin >> s; reverse(((s).begin()), ((s).end())); ans = s[0] == 1 ; n = ((int)((s).size())); x = 1; for (int i = 0; i < (int)(n - 1); i++) { x *= 4; x %= 1000000007; if (s[i + 1] == 1 ) ans = ans * 2 + x; else ans *= 2; ans %= 1000000007; } cout << ans; return 0; } long long nChooseR(int n, int k) { if (k > n) return 0; if (k * 2 > n) k = n - k; if (k == 0) return 1; long long result = n; for (int i = 2; i <= k; ++i) { result *= (n - i + 1); result /= i; } return result; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } vector<string> split(string target, char c) { string d = ; vector<string> arr; for (auto n : target) { if (n != c) d += n; else if (d != ) arr.push_back(d), d = ; } if (d != ) arr.push_back(d), d = ; return arr; } bool isPrime(long long g) { if ((g % 2 == 0 && g > 2) || g == 1) return 0; for (int i = 3; i * i <= g; i += 2) if (g % i == 0) return 0; return 1; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; vector<int> v; vector<vector<int>> g; int in[N], out[N], col[N], is[N], dp[18][N], depth[N], z; struct node { int l, r, id; node(int a = 0, int b = 0, int c = 0) { l = a; r = b; id = c; } bool operator<(const node& e) const { int a = l / z, b = e.l / z; if (a != b) return a < b; if (a & 1) return r > e.r; return r < e.r; } }; vector<node> q; void dfs(int u, int p) { dp[0][u] = p; in[u] = v.size(); v.push_back(u); for (int i = 1; i < 18; i++) dp[i][u] = dp[i - 1][u] + 1 ? dp[i - 1][dp[i - 1][u]] : -1; for (auto i : g[u]) if (i != p) depth[i] = depth[u] + 1, dfs(i, u); out[u] = v.size(); v.push_back(u); } int lca(int a, int b) { if (depth[a] < depth[b]) swap(a, b); for (int i = 17; i + 1; i--) if (depth[a] - (1 << i) >= depth[b]) a = dp[i][a]; if (a == b) return a; for (int i = 17; i + 1; i--) if (dp[i][a] != dp[i][b]) a = dp[i][a], b = dp[i][b]; return dp[0][a]; } int main() { int n; scanf( %d , &n); g.resize(n); for (int i = 0; i < n; i++) scanf( %d , is + i); for (int i = 0; i < n; i++) scanf( %d , col + i); for (int i = 1; i < n; i++) { int a, b; scanf( %d%d , &a, &b); g[--a].push_back(--b); g[b].push_back(a); } dfs(0, -1); scanf( %d , &n); z = sqrt(v.size()) + 1; for (int i = 0; i < n; i++) { int a, b; scanf( %d%d , &a, &b); --a; --b; if (in[a] > in[b]) swap(a, b); q.push_back(node(in[a] + 1, in[b], i)); } sort(q.begin(), q.end()); long long ans[N] = {}, temp = 0, freq[N] = {}; int l = 0, r = -1; unordered_map<int, int> mp[2]; for (auto i : q) { if (i.l - 1 == i.r) continue; while (r < i.r) { r++; int a = is[v[r]]; int b = col[v[r]]; if (freq[v[r]]++) { temp -= mp[!a][b]; mp[a][b]--; } else { temp += mp[!a][b]; mp[a][b]++; } } while (r > i.r) { int a = is[v[r]]; int b = col[v[r]]; if (--freq[v[r]]) { temp += mp[!a][b]; mp[a][b]++; } else { temp -= mp[!a][b]; mp[a][b]--; } r--; } while (l < i.l) { int a = is[v[l]]; int b = col[v[l]]; if (--freq[v[l]]) { temp += mp[!a][b]; mp[a][b]++; } else { temp -= mp[!a][b]; mp[a][b]--; } l++; } while (l > i.l) { l--; int a = is[v[l]]; int b = col[v[l]]; if (freq[v[l]]++) { temp -= mp[!a][b]; mp[a][b]--; } else { temp += mp[!a][b]; mp[a][b]++; } } int LCA = lca(v[i.l - 1], v[i.r]); int lol = mp[!is[LCA]][col[LCA]]; ans[i.id] = temp + lol; } for (int i = 0; i < q.size(); i++) printf( %lld n , ans[i]); } |
#include <bits/stdc++.h> using namespace std; int n, ls, tp, cx, yy, qq; char s[64]; string ans, cur, st[55555][2], tt; bool quot, found; int main() { scanf( %d n , &n); tp = -1; found = false; qq = 0; for (int _ = 0; _ < n; ++_) { gets(s); ls = strlen(s); quot = false; cur = ; for (int i = 0; i < ls; ++i) if (quot) { if (s[i] == ) quot = false; else cur += s[i]; } else { if (s[i] == ) quot = true; else if (s[i] != ) cur += s[i]; } if (!cur.find( try )) { tp += !found; qq += found; } else if (!cur.find( catch )) { if (qq) { --qq; continue; } if (found) { st[tp][0] = ; cx = 6; while (cur[cx] != , ) st[tp][0] += cur[cx++]; ++cx; st[tp][1] = ; while (cur[cx] != ) ) st[tp][1] += cur[cx++]; if (!tp) break; } --tp; } else if (!cur.find( throw )) { found = true; yy = tp; cx = 6; tt = ; while (cur[cx] != ) ) tt += cur[cx++]; } } ans = Unhandled Exception ; for (int i = yy; i >= 0; --i) if (st[i][0] == tt) { ans = st[i][1]; break; } printf( %s , ans.c_str()); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxL = 70; int mod = 1e9 + 7; long long mod1 = 1e9 + 7; const int inf = 1.2e18; const int maxn = 6e5 + 100; const int baze = 330; const int base = 3; int from[maxn]; int to[maxn]; int w[maxn]; int dis[maxn]; int head[maxn]; int sdfgf[maxn]; int backup[maxn]; int chiz[maxn]; int eof, source = maxn - 1, sink = maxn - 2; void add_edge(int u, int v, int W) { from[eof] = u, to[eof] = v, w[eof] = W, sdfgf[eof] = head[u], head[u] = eof++; swap(u, v); W = 0; from[eof] = u, to[eof] = v, w[eof] = W, sdfgf[eof] = head[u], head[u] = eof++; } bool bfs() { int st = 0, en = 1; chiz[0] = source; memset(dis, 63, sizeof dis); dis[source] = 0; while (st < en) { int v = chiz[st++]; for (int e = head[v]; ~e; e = sdfgf[e]) if (w[e] && dis[to[e]] > dis[v] + 1) { dis[to[e]] = dis[v] + 1; chiz[en++] = to[e]; if (to[e] == sink) return true; } } return false; } int dfs(int v, int mn = 1000000) { if (!mn || v == sink) return mn; int ret = 0; for (int &e = head[v]; ~e; e = sdfgf[e]) if (dis[to[e]] == dis[v] + 1) { int x = dfs(to[e], min(mn, w[e])); ret += x; w[e] -= x; w[e ^ 1] += x; mn -= x; if (mn == 0) break; } return ret; } int flow() { memcpy(backup, head, sizeof head); int ret = 0; while (bfs()) { ret += dfs(source); memcpy(head, backup, sizeof head); } return ret; } int ar[60]; string t[maxn]; int pos[maxn]; int n; int32_t main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); memset(head, -1, sizeof head); map<string, int> mp; mp[ S ] = 0; mp[ M ] = 1; mp[ L ] = 2; mp[ XL ] = 3; mp[ XXL ] = 4; mp[ XXXL ] = 5; for (int i = 0; i < 6; i++) cin >> ar[i], add_edge(source, i, ar[i]); cin >> n; vector<int> vc; for (int i = 0; i < n; i++) { string s; cin >> s; t[i] = s; pos[i] = ((int)((s).size())); if (s.find( , ) < ((int)((s).size()))) { int x = s.find( , ); pos[i] = x; vc.push_back(eof); add_edge(mp[s.substr(0, x)], i + 10, 1); add_edge(mp[s.substr(x + 1, ((int)((s).size())))], i + 10, 1); add_edge(i + 10, sink, 1); } else vc.push_back(eof), add_edge(mp[s], sink, 1); } int ans = flow(); if (ans != n) return cout << NO << endl, 0; cout << YES << endl; int hlp = 0; for (int x : vc) { if (w[x] == 0) cout << t[hlp].substr(0, pos[hlp]) << n ; else cout << t[hlp].substr(pos[hlp] + 1, ((int)((t[hlp]).size()))) << n ; hlp++; } } |
#include <bits/stdc++.h> using namespace std; long long mod1(long long x) { if (x >= 0) { return x; } return -x; } vector<long long> countDigit(long long n) { vector<long long> ret; while (n != 0) { ret.push_back(n % 10); n = n / 10; } return ret; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm1(long long a, long long b) { return (a / gcd(a, b)) * b; } bool prime[1000000]; void SieveOfEratosthenes(long long n) { memset(prime, true, sizeof(prime)); prime[1] = false; for (long long p = 2; p * p <= n; p++) { if (prime[p] == true) { for (long long i = p * p; i <= n; i += p) prime[i] = false; } } } bool isPowerOfTwo(long long n) { if (n == 0) return false; return (ceil(log2(n)) == floor(log2(n))); } bool PowerOfFour(long long num) { if (num <= 0) return false; while (num != 1) { if (num % 4 != 0) return false; num = num / 4; } return true; } bool compare(pair<long long, long long> p1, pair<long long, long long> p2) { if (p1.first != p2.first) { return p1.first > p2.first; } else { p1.second > p2.second; } } bool compare1(pair<long long, long long> p1, pair<long long, long long> p2) { if (p1.second != p2.second) { return p1.second < p2.second; } else { p1.first < p2.first; } } bool isPalindrome(string str) { long long l = 0; long long h = str.size() - 1; while (h > l) { if (str[l++] != str[h--]) { return false; } } return true; } vector<long long> primef(long long n) { vector<long long> v; if (n == 1) { return v; } while (n % 2 == 0) { v.push_back(2); n = n / 2; } for (long long i = 3; i <= sqrt(n); i = i + 2) { while (n % i == 0) { v.push_back(i); n = n / i; } } if (n > 2) v.push_back(n); return v; } long long mod = 1000000007; long long fact1(long long x) { if (x < 2) { return 1; } return (x * fact1(x - 1)) % mod; } long long power1(long long x, long long y) { long long r = 1; while (y > 0) { r = (r % mod * x % mod) % mod; y--; } return r; } bool isp(long long x) { if (x >= 0) { long long sr = sqrt(x); return (sr * sr == x); } return false; } bool isPri(long long n) { if (n <= 1) return false; for (long long i = 2; i < n; i++) if (n % i == 0) return false; return true; } long long power2(long long x, long long y, long long p) { long long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long gcdExtended(long long a, long long b, long long *x, long long *y) { if (a == 0) { *x = 0, *y = 1; return b; } long long x1, y1; long long gcd = gcdExtended(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } long long modInverse(long long b, long long m) { long long x, y; long long g = gcdExtended(b, m, &x, &y); if (g != 1) return -1; return (x % m + m) % m; } long long ask(long long val) { cout << val << endl; fflush(stdout); long long x; cin >> x; return x; } void solve() { long long n; cin >> n; string s; cin >> s; long long ret = INT_MAX; for (long long i = 0; i < 26; i++) { long long l = 0; long long r = n - 1; long long c = 0; while (l <= r) { if (s[l] == s[r]) { l++; r--; continue; } if (s[l] == i + a ) { l++; c++; continue; } if (s[r] == i + a ) { r--; c++; continue; } c = INT_MAX; break; } ret = min(ret, c); } if (ret == INT_MAX) { cout << -1 << endl; } else { cout << ret << endl; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { solve(); } } |
#include <bits/stdc++.h> using namespace std; long long power(long long base, long long pow, long long mo) { long long res = 1; while (pow) { if (pow % 2 != 0) res = (res * base) % mo; base = (base * base) % mo; pow /= 2; } return res; } long long inv(long long i) { return power(i, 1000000007 - 2, 1000000007); } std::string str = All Good Till Here ; void solve() { long long i, j, x, n; cin >> n; long long a[n], p[n]; for (long long c1 = 0; c1 < n; c1++) cin >> a[c1]; ; for (long long i = 0; i < n; i++) { p[a[i] - 1] = i; } long long ans = 0, max2 = 1; for (long long i = 1; i < n; i++) { if (p[i] > p[i - 1]) { max2++; } else { ans = max(max2, ans); max2 = 1; } } ans = max(max2, ans); cout << n - ans << n ; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long tt; tt = 1; while (tt--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 100010; const int N = 100010; struct e { int v; e *nxt; } es[N << 1], *fir[N]; map<pair<int, int>, int> mp; struct node { int ls, rs; int l, r; int sum; int mid() { return (l + r) >> 1; } } nodes[N << 2]; int n, en, val[N]; int que[N], par[N], dep[N], root[N], seg[N], st[N], ed[N], top[N], sons[N], id[N]; int ln, cnt, tr; inline void init() { en = 0; memset(fir, 0, sizeof(fir)); } inline void add_e(int u, int v) { es[en].v = v; es[en].nxt = fir[u]; fir[u] = &es[en++]; } inline void newNode(int &id, int l, int r) { nodes[cnt].ls = nodes[cnt].rs = -1; nodes[cnt].l = l; nodes[cnt].r = r; id = cnt++; } void build(int &id, int l, int r) { newNode(id, l, r); if (l + 1 >= r) { nodes[id].sum = 1; return; } int mid = (l + r) >> 1; build(nodes[id].ls, l, mid); build(nodes[id].rs, mid, r); nodes[id].sum = nodes[nodes[id].ls].sum + nodes[nodes[id].rs].sum; } void initTree() { int l, r, u, v, i; e *cur; l = r = 0; tr = 1; que[r++] = tr; par[tr] = -1; dep[tr] = 0; while (l != r) { u = que[l++]; for (cur = fir[u]; cur; cur = cur->nxt) { if ((v = cur->v) != par[u]) { que[r++] = v; par[v] = u; dep[v] = dep[u] + 1; } } } for (i = 1; i <= n; i++) { sons[i] = 1; id[i] = -1; } for (i = r - 1; i >= 0; i--) { u = que[i]; if (par[u] >= 0) { sons[par[u]] += sons[u]; } } l = r = 0; que[r++] = tr; ln = cnt = 0; while (l != r) { u = que[l++]; st[ln] = dep[u]; top[ln] = u; while (u >= 0) { id[u] = ln; ed[ln] = dep[u]; seg[dep[u]] = u; int best; for (cur = fir[u], best = -1; cur; cur = cur->nxt) { if (id[v = cur->v] == -1) { if (best == -1 || (best >= 0 && sons[v] > sons[best])) { best = v; } } } if (best >= 0) { for (cur = fir[u]; cur; cur = cur->nxt) { if (id[v = cur->v] == -1 && best != v) { que[r++] = v; } } } u = best; } root[ln] = -1; build(root[ln], st[ln], ed[ln]); ln++; } } int S; void lqry(int &id, int ql, int qr) { if (id == -1) return; if (ql == qr) return; if (ql <= nodes[id].l && nodes[id].r <= qr) { S += nodes[id].sum; if (S > inf) S = inf; return; } if (nodes[id].l + 1 == nodes[id].r) { S += nodes[id].sum; if (S > inf) S = inf; return; } int mid = (nodes[id].l + nodes[id].r) >> 1; if (ql < mid) { lqry(nodes[id].ls, ql, qr); } if (qr > mid) { lqry(nodes[id].rs, ql, qr); } } void update(int id, int x, int y, int val) { if (id == -1) return; if (nodes[id].l + 1 == nodes[id].r) { nodes[id].sum = val; return; } int mid = (nodes[id].l + nodes[id].r) >> 1; if (mid <= x) update(nodes[id].rs, x, y, val); else if (mid >= y) update(nodes[id].ls, x, y, val); nodes[id].sum = nodes[nodes[id].ls].sum + nodes[nodes[id].rs].sum; if (nodes[id].sum > inf) nodes[id].sum = inf; } void qry(int u, int v) { while (id[u] != id[v]) { if (id[u] > id[v]) { swap(u, v); } int b = id[v]; lqry(root[b], st[b], dep[v]); v = par[top[b]]; if (mp.find(make_pair(top[b], par[top[b]])) != mp.end()) S += val[mp[make_pair(top[b], par[top[b]])]]; else S += val[mp[make_pair(par[top[b]], top[b])]]; if (S > inf) S = inf; } if (dep[u] > dep[v]) { swap(u, v); } lqry(root[id[u]], dep[u], dep[v]); } struct ee { int x, y; } edge[100010]; int main() { int m, x, y, ty; scanf( %d , &n); init(); for (int i = 1; i < n; i++) { scanf( %d%d , &x, &y); mp[make_pair(x, y)] = i; val[i] = 1; edge[i].x = x; edge[i].y = y; add_e(x, y); add_e(y, x); } initTree(); scanf( %d , &m); for (int i = 0; i < m; i++) { scanf( %d , &ty); if (ty == 3) { scanf( %d%d , &x, &y); S = 0; qry(x, y); if (S >= inf) printf( -1 n ); else printf( %d n , S); } else if (ty == 1) { scanf( %d , &x); int u = edge[x].x; int v = edge[x].y; val[x] = 1; if (dep[u] > dep[v]) { swap(u, v); } if (id[u] == id[v]) update(root[id[u]], dep[u], dep[u], 1); } else { scanf( %d , &x); int u = edge[x].x; int v = edge[x].y; val[x] = inf; if (dep[u] > dep[v]) { swap(u, v); } if (id[u] == id[v]) update(root[id[u]], dep[u], dep[v], inf); } } } |
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int n; cin >> n; bitset<6> a(n); string s = a.to_string(); swap(s[1], s[5]); swap(s[2], s[3]); bitset<6> b(s); cout << static_cast<int>(b.to_ulong()) << n ; } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:256000000 ) using namespace std; int main() { int n; scanf( %d , &n); printf( %d , n); for (int i = 1; i < n; i++) printf( %d , i); return 0; } |
#include <bits/stdc++.h> const int N = 105; int n, m; char a[N][N]; struct Node { int ax, ay, bx, by, cx, cy; }; void work(int i, int j, int mask) { for (int x = 0; x < 2; ++x) { for (int y = 0; y < 2; ++y) { if (mask >> (x << 1 | y) & 1) { a[i][j] ^= 1, a[i][j + 1] ^= 1, a[i + 1][j] ^= 1, a[i + 1][j + 1] ^= 1; a[i + x][j + y] ^= 1; } } } } void solve() { std::cin >> n >> m; for (int i = 0; i < n; ++i) { std::cin >> a[i]; } std::vector<Node> ans; if ((n & 1) && (m & 1) && a[n - 1][m - 1] == 1 ) { ans.push_back({n - 1, m - 1, n - 2, m - 1, n - 1, m - 2}); a[n - 1][m - 1] = 0 ; a[n - 2][m - 1] ^= 1; a[n - 1][m - 2] ^= 1; } if (m & 1) { for (int i = 0; i < n - 1; i += 2) { if (a[i][m - 1] == 1 && a[i + 1][m - 1] == 1 ) { ans.push_back({i, m - 2, i, m - 1, i + 1, m - 1}); a[i][m - 2] ^= 1; } else if (a[i][m - 1] == 1 ) { ans.push_back({i, m - 2, i + 1, m - 2, i + 1, m - 1}); ans.push_back({i, m - 2, i, m - 1, i + 1, m - 1}); a[i + 1][m - 2] ^= 1; } else if (a[i + 1][m - 1] == 1 ) { ans.push_back({i, m - 2, i + 1, m - 2, i, m - 1}); ans.push_back({i, m - 2, i, m - 1, i + 1, m - 1}); a[i + 1][m - 2] ^= 1; } } --m; } if (n & 1) { for (int i = 0; i < m - 1; i += 2) { if (a[n - 1][i] == 1 && a[n - 1][i + 1] == 1 ) { ans.push_back({n - 2, i, n - 1, i, n - 1, i + 1}); a[n - 2][i] ^= 1; } else if (a[n - 1][i] == 1 ) { ans.push_back({n - 2, i, n - 2, i + 1, n - 1, i + 1}); ans.push_back({n - 2, i, n - 1, i, n - 1, i + 1}); a[n - 2][i + 1] ^= 1; } else if (a[n - 1][i + 1] == 1 ) { ans.push_back({n - 2, i, n - 2, i + 1, n - 1, i}); ans.push_back({n - 2, i, n - 1, i, n - 1, i + 1}); a[n - 2][i + 1] ^= 1; } } --n; } for (int i = 0; i < n; i += 2) { for (int j = 0; j < m; j += 2) { for (int k = 0; k < 16; ++k) { work(i, j, k); if (a[i][j] == 0 && a[i][j + 1] == 0 && a[i + 1][j] == 0 && a[i + 1][j + 1] == 0 ) { for (int x = 0; x < 2; ++x) { for (int y = 0; y < 2; ++y) { if (k >> (x << 1 | y) & 1) { std::vector<int> p; for (int dx = 0; dx < 2; ++dx) { for (int dy = 0; dy < 2; ++dy) { if (x != dx || y != dy) { p.push_back(i + dx); p.push_back(j + dy); } } } ans.push_back({p[0], p[1], p[2], p[3], p[4], p[5]}); } } } break; } work(i, j, k); } } } std::cout << ans.size() << n ; for (auto v : ans) { std::cout << v.ax + 1 << << v.ay + 1 << ; std::cout << v.bx + 1 << << v.by + 1 << ; std::cout << v.cx + 1 << << v.cy + 1 << n ; } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int T = 1; std::cin >> T; while (T--) { solve(); } } |
#include <bits/stdc++.h> using namespace std; int ans[51][6] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 4, 3, 0, 0, 0, 0, 5, 3, 0, 0, 0, 0, 6, 5, 0, 0, 0, 0, 7, 6, 0, 0, 0, 0, 8, 6, 5, 4, 0, 0, 9, 5, 0, 0, 0, 0, 10, 7, 0, 0, 0, 0, 11, 9, 0, 0, 0, 0, 12, 6, 4, 1, 0, 0, 13, 4, 3, 1, 0, 0, 14, 5, 3, 1, 0, 0, 15, 14, 0, 0, 0, 0, 16, 15, 13, 4, 0, 0, 17, 14, 0, 0, 0, 0, 18, 11, 0, 0, 0, 0, 19, 6, 2, 1, 0, 0, 20, 17, 0, 0, 0, 0, 21, 19, 0, 0, 0, 0, 22, 21, 0, 0, 0, 0, 23, 18, 0, 0, 0, 0, 24, 23, 22, 17, 0, 0, 25, 22, 0, 0, 0, 0, 26, 6, 2, 1, 0, 0, 27, 5, 2, 1, 0, 0, 28, 25, 0, 0, 0, 0, 29, 27, 0, 0, 0, 0, 30, 6, 4, 1, 0, 0, 31, 28, 0, 0, 0, 0, 32, 22, 2, 1, 0, 0, 33, 20, 0, 0, 0, 0, 34, 27, 2, 1, 0, 0, 35, 33, 0, 0, 0, 0, 36, 25, 0, 0, 0, 0, 37, 5, 4, 3, 2, 1, 38, 6, 5, 1, 0, 0, 39, 35, 0, 0, 0, 0, 40, 38, 21, 19, 0, 0, 41, 38, 0, 0, 0, 0, 42, 41, 20, 19, 0, 0, 43, 42, 38, 37, 0, 0, 44, 43, 18, 17, 0, 0, 45, 44, 42, 41, 0, 0, 46, 45, 26, 25, 0, 0, 47, 42, 0, 0, 0, 0, 48, 47, 21, 20, 0, 0, 49, 40, 0, 0, 0, 0, 50, 49, 24, 23, 0, 0, }; int main() { int k; cin >> k; for (int i = 0; i < k; i++) { int found = 0; for (int j = 0; j < 6; j++) if (ans[k][j] == i + 1) found = 1; cout << found << ; } cout << endl; for (int i = 0; i < k; i++) cout << 1 ; cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int tux; cin >> tux; int foo, bar, baz, quz, pur, oo; foo = bar = baz = 0; quz = 1; while (tux) { cin >> pur; foo += pur; bar++; if (foo * quz > bar * baz) { baz = foo; quz = bar; } tux--; } cout.precision(5); cout << fixed << (baz * 1. / quz); return 0; } |
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vii; typedef vector<vi> vvi; typedef vector<pll> vll; typedef vector<vl> vvl; #define fori(i, n) for (int i = 0; i < n; i++) #define ford(i, n) for (int i = n - 1; i >= 0; i--) #define rep(i, a, b) for (int i = a; i <= b; i++) #define repd(i, a, b) for (int i = a; i >= b; i--) #define trav(x, a) for (auto &x : a) #define all(x) (x).begin(), (x).end() #define pb push_back #define eb emplace_back #define endl n #define sz(a) (int)(a).size() #define fi first #define se second clock_t time_p = clock(); void time_taken() { time_p = clock() - time_p; cerr << Time Taken : << (float)(time_p) / CLOCKS_PER_SEC << n ; } const ll mod = 1e9 + 7; const ll INF = 1e18; int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); int n, d; cin >> n >> d; vi a(n); fori(i, n) cin >> a[i]; vector<vector<ld>> dp(n + 1, vector<ld>(10, -1)); vvi prv(n + 1, vi(10)); vvi taken(n + 1, vi(10)); dp[0][1] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < 10; j++) { if (dp[i][j] == -1) continue; int to = (j * a[i]) % 10; ld maybe = dp[i][j] + log2l(a[i]); if (maybe > dp[i + 1][to]) { dp[i + 1][to] = maybe; taken[i + 1][to] = 1; prv[i + 1][to] = j; } to = j; maybe = dp[i][j]; if (maybe > dp[i + 1][to]) { dp[i + 1][to] = maybe; taken[i + 1][to] = 0; prv[i + 1][to] = j; } } } if (dp[n][d] == -1) { cout << -1 << endl; return 0; } int i = n, j = d; vi ans; while (i) { if (taken[i][j]) { ans.pb(a[i - 1]); } j = prv[i][j]; i--; } if(sz(ans) == 0){ cout << -1 << endl; return 0; } cout << sz(ans) << endl; for (int x : ans) { cout << x << ; } cout << endl; time_taken(); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, sz, timer; int p[1000005]; vector<int> v[1000005]; int tin[1000005], tout[1000005]; int t[4000005], tt[4000005]; int depth[1000005]; int a[1000005]; pair<int, int> q[1000005]; bool used[1000005]; void dfs(int x, int y = 0) { used[x] = true; tin[x] = ++timer; depth[x] = depth[y] + 1; for (int i = 0; i < v[x].size(); ++i) { int to = v[x][i]; dfs(to, x); } tout[x] = timer; return; } void update(int x, int y) { x += sz - 1; t[x] = y; x >>= 1; while (x) { t[x] = max(t[x + x], t[x + x + 1]); x >>= 1; } return; } int get(int l, int r, int ll, int rr, int x) { if (l > r || ll > r || l > rr || ll > rr) return 0; if (l >= ll && r <= rr) return t[x]; int mid = (l + r) >> 1; return max(get(l, mid, ll, rr, x + x), get(mid + 1, r, ll, rr, x + x + 1)); } void update2(int x, int y) { x += sz - 1; tt[x] = y; x >>= 1; while (x) { tt[x] = max(tt[x + x], tt[x + x + 1]); x >>= 1; } return; } int get_pred(int x) { if (x == p[x]) return x; return p[x] = get_pred(p[x]); } void update_val(int x) { int val = get(1, sz, tin[x], tout[x], 1) - depth[x] + 1; update2(x, val); return; } void add_vertex(int x) { for (int i = 0; i < v[x].size(); ++i) { int to = v[x][i]; update2(to, 0); to = get_pred(to); p[to] = x; } update(tin[x], depth[x]); update_val(x); return; } void del_vertex(int x) { update(tin[x], 0); x = get_pred(x); update_val(x); return; } int main(int argc, const char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i]; } q[0] = {1e9, n + 1}; for (int i = n; i > 0; --i) { while (sz > 0 && q[sz].first <= a[i]) --sz; v[q[sz].second].push_back(i); q[++sz] = {a[i], i}; } for (int i = n + 1; i > 0; --i) if (!used[i]) dfs(i); for (int i = 1; i <= n + 1; ++i) p[i] = i; sz = 1; while (sz < n + 1) sz += sz; for (int i = 1; i <= m; ++i) { add_vertex(i); } cout << tt[1] << ; for (int i = m + 1; i <= n; ++i) { add_vertex(i); del_vertex(i - m); cout << tt[1] << ; } cout << n ; } |
#include <bits/stdc++.h> int sta[1000001], head, ans[1000001]; char s[1000001]; int main() { int ans1 = 0, ans2 = 1; scanf( %s , s); for (int i = 0; s[i]; ++i) { if (s[i] == ) ) { if (!head) continue; head--; ans[i] = i - sta[head] + 1 + ans[sta[head] - 1]; if (ans1 == ans[i]) ans2++; else if (ans1 < ans[i]) { ans1 = ans[i]; ans2 = 1; } } else sta[head++] = i; } printf( %d %d , ans1, ans2); return 0; } |
#include <bits/stdc++.h> using namespace std; int t, n; int a[200004]; int main() { cin >> t; while (t--) { cin >> n; for (int i = 0; i < 2 * n; i++) cin >> a[i]; sort(a, a + 2 * n); cout << a[n] - a[n - 1] << n ; } cin >> t; return 0; } |
#include <bits/stdc++.h> using namespace std; int T, n, a[30005], b[30005]; long long sum[2]; int main() { scanf( %d , &T); while (T--) { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); a[n + 1] = 0; for (int i = 1; i <= n + 1; i++) b[i] = a[i] - a[i - 1]; sum[0] = sum[1] = 0; for (int i = 2; i <= n; i++) { if (b[i] > 0) sum[0] += b[i]; else sum[1] -= b[i]; } if (b[1] >= sum[1] && -b[n + 1] >= sum[0]) { printf( YES n ); } else printf( NO n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; bool g[205][205]; struct coor { int x, y; }; vector<int> ans; int a[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1}; coor Max, Min; int w, h; void dfs(int x, int y) { g[x][y] = 1; for (int i = 0; i < 8; i++) { coor c; c.x = x + a[i][0]; c.y = y + a[i][1]; if (c.x >= 0 && c.x <= w && c.y >= 0 && c.y <= h) { if (c.x >= Max.x && c.y >= Max.y) Max = c; if (c.x <= Min.x && c.y <= Min.y) Min = c; if (g[c.x][c.y] == 0) dfs(c.x, c.y); } } } void out() { int i, j; for (i = 0; i <= w; i++) { for (j = 0; j <= h; j++) { printf( %d , g[i][j]); } printf( n ); } printf( n ); } int main() { int i, j, n, x1, x2, y1, y2; scanf( %d%d%d , &w, &h, &n); w = w + w; h = h + h; ans.clear(); memset(g, 0, sizeof(g)); for (i = 0; i <= w; i++) g[i][0] = g[i][h] = 1; for (i = 0; i <= h; i++) g[0][i] = g[w][i] = 1; for (j = 0; j < n; j++) { scanf( %d%d%d%d , &x1, &y1, &x2, &y2); x1 += x1; x2 += x2; y1 += y1; y2 += y2; if (x1 == x2) for (i = y1; i <= y2; i++) g[x1][i] = 1; else for (i = x1; i <= x2; i++) g[i][y1] = 1; } for (i = 0; i <= w; i++) for (j = 0; j <= h; j++) { if (g[i][j] == 0) { Max.x = Max.y = -1; Min.y = Min.x = 1000; dfs(i, j); ans.push_back((Max.x - Min.x) * (Max.y - Min.y) / 4); } } sort(ans.begin(), ans.end()); for (i = 0; i < ans.size() - 1; i++) printf( %d , ans[i]); printf( %d n , ans[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1000000, M = 1000000007, D3 = (M + 1) / 3; int T, n, m; int pd[N + 5], pr[N], pn, phi[N + 5]; int f[N + 5], s0[N + 5], s1[N + 5], s2[N + 5]; void init() { phi[1] = 1; for (int i = 2; i <= N; i++) { if (!pd[i]) phi[i] = i - 1, pr[++pn] = i; for (int j = 1; j <= pn && i * pr[j] <= N; j++) { pd[i * pr[j]] = pr[j]; if (i % pr[j] == 0) { phi[i * pr[j]] = phi[i] * pr[j]; break; } else phi[i * pr[j]] = phi[i] * phi[pr[j]]; } } for (int d = 1; d <= N; d++) for (int k = d; k <= N; k += d) f[k] += d * phi[k / d]; for (int L = 1; L <= N; L++) { f[L] = (((long long)L * L - L - L) % M * L % M - (long long)(L + 1) * L % M * L % M + (long long)L * (L + 1) % M * (L + L + 1) % M * D3 % M + 2LL * f[L]) % M; if (f[L] < 0) f[L] += M; } for (int i = 1; i <= N; i++) { s0[i] = (s0[i - 1] + (long long)f[i]) % M; s1[i] = (s1[i - 1] + (long long)i * f[i]) % M; s2[i] = (s2[i - 1] + (long long)i * i % M * f[i]) % M; } } int main() { init(); for (scanf( %d , &T); T--;) { scanf( %d%d , &n, &m); if (n > m) swap(n, m); long long ans = (long long)(n + 1) * (m + 1) % M * s0[n] % M; ans -= (long long)(n + m + 2) * s1[n] % M; ans += s2[n]; ans = (ans % M + M) % M; printf( %d n , ans); } return 0; } |
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int n, m; vector<int> tr[1000005]; vector<int> vr[1000005]; pair<int, int> pr[1000005]; int high; long long team[1000005], ir[1000005]; int main() { scanf( %d %d , &n, &m); int x, y; for (int i = 0; i < m; i++) { scanf( %d %d , &x, &y); if (x < y) swap(x, y); vr[x].push_back(y); } for (int i = 1; i < n + 1; i++) { scanf( %d , &x); for (int j = 0; j < x; j++) { scanf( %d , &y); tr[i].push_back(y); high = max(high, y); } } while (1) { int tot = 0; for (int i = 1; i < high + 1; i++) team[i] = rng() % 2 + 1; for (int i = 1; i < n + 1; i++) { ir[i] = tr[i][rng() % tr[i].size()]; for (int j = 0; j < vr[i].size(); j++) { if (team[ir[vr[i][j]]] != team[ir[i]]) tot++; } } if (tot >= m / 2 + (m % 2)) break; } for (int i = 1; i < n + 1; i++) printf( %d , ir[i]); printf( n ); for (int i = 1; i < high + 1; i++) printf( %d , team[i]); } |
#include <bits/stdc++.h> using namespace std; int a[200001]; int b[200000]; int c[200001]; int main() { int n, i, j; cin >> n; map<int, int> m; for (i = 0; i < n; i++) { cin >> b[i]; m[b[i]]++; a[b[i]] = 1; } vector<int> v; for (i = 1; i <= n; i++) { if (a[i] == 0) { v.push_back(i); } } sort(v.begin(), v.end()); cout << v.size() << endl; int p = 0; for (i = 0; i < n; i++) { if (c[b[i]] == 1) { cout << v[p] << ; p++; c[b[i]] = 1; m[b[i]]--; } else { if (m[b[i]] == 1) { cout << b[i] << ; } else if (m[b[i]] > 1) { if (v[p] < b[i]) { cout << v[p] << ; m[b[i]]--; p++; } else { cout << b[i] << ; c[b[i]] = 1; m[b[i]]--; } } } } return 0; } |
#include <bits/stdc++.h> using namespace std; class segtree { public: int val[(1 << 20) * 2]; int le[(1 << 20) * 2]; int ri[(1 << 20) * 2]; void update(int pos, int x) { val[pos + (1 << 20) - 1] = 0; le[pos + (1 << 20) - 1] = 1 - x; ri[pos + (1 << 20) - 1] = x; } void up() { for (int i = (1 << 20) - 2; i >= 0; i--) { int add = min(le[i * 2 + 1], ri[i * 2 + 2]); val[i] = val[i * 2 + 1] + val[i * 2 + 2] + add; le[i] = le[i * 2 + 1] + le[i * 2 + 2] - add; ri[i] = ri[i * 2 + 1] + ri[i * 2 + 2] - add; } } pair<int, pair<int, int> > query(int a, int b, int k, int l, int r) { pair<int, pair<int, int> > p = make_pair(0, make_pair(0, 0)); if (r < a || b < l) return p; if (a <= l && r <= b) { return make_pair(val[k], make_pair(le[k], ri[k])); } pair<int, pair<int, int> > x = query(a, b, k * 2 + 1, l, (l + r) / 2); pair<int, pair<int, int> > y = query(a, b, k * 2 + 2, (l + r) / 2 + 1, r); int z = min(x.second.first, y.second.second); return make_pair(x.first + y.first + z, make_pair(x.second.first + y.second.first - z, x.second.second + y.second.second - z)); } } seg; char ss[1000005]; int main() { scanf( %s , &ss); int n = strlen(ss); for (int i = 0; i < n; i++) { seg.update(i, (ss[i] == ) )); } seg.up(); int q; scanf( %d , &q); for (int i = 0; i < q; i++) { int a, b; scanf( %d %d , &a, &b); a--; b--; printf( %d n , seg.query(a, b, 0, 0, (1 << 20) - 1).first * 2); } } |
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b, c, d; cin >> a >> b >> c >> d; if ((a + b) % 2 == 0) { cout << Tidak Tidak ; if (b || c) cout << Ya ; else cout << Tidak ; if (a || d) cout << Ya ; else cout << Tidak ; } else { if (a || d) cout << Ya ; else cout << Tidak ; if (b || c) cout << Ya ; else cout << Tidak ; cout << Tidak Tidak ; } cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int M = 1e6 + 10; int n, ans; void swap(int &x, int &y) { int t = x; x = y; y = t; } int main() { cin >> n; vector<char> x(n), y(n); for (auto &i : x) cin >> i; for (auto &i : y) cin >> i; for (int i = 0; i < n - 1; ++i) { if (x[i] == y[i]) continue; if (x[i + 1] == y[i + 1]) { ans++; continue; } if (x[i] == y[i + 1] && x[i + 1] == y[i]) swap(x[i], x[i + 1]), ans++; else ans++; } if (x[n - 1] != y[n - 1]) ans++; cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int const N = 202000; set<int> s; set<int>::iterator it; int edg[N]; vector<int> g[N]; int ans[N]; int cur = 1; int vis[N]; void dfs(int node) { vis[node] = 1; ans[cur]++; vector<int> send; for (it = s.begin(); it != s.end();) { if (!binary_search(g[node].begin(), g[node].end(), *it)) { send.push_back(*it); s.erase(it++); } else it++; } for (int el : send) dfs(el); } int main() { int n, e; scanf( %d%d , &n, &e); for (int i = 1; i <= n; ++i) s.insert(i); for (int i = 0; i < e; ++i) { int temp, temp1; scanf( %d%d , &temp, &temp1); edg[temp]++; edg[temp1]++; g[temp].push_back(temp1); g[temp1].push_back(temp); } vector<pair<int, int>> go; for (int i = 1; i <= n; i++) { go.push_back(make_pair(edg[i], i)); sort(g[i].begin(), g[i].end()); } sort(go.begin(), go.end()); for (auto el : go) { if (vis[el.second]) continue; s.erase(el.second); ans[0]++; dfs(el.second); cur++; } cout << ans[0] << n ; sort(ans + 1, ans + ans[0] + 1); for (int i = 1; i <= ans[0]; i++) { cout << ans[i] << ; } return 0; } |
#include <bits/stdc++.h> int main() { int n, x, m, count; scanf( %d %d , &n, &m); for (x = 1, count = 0; x <= n; x++) { if (m % x == 0 && (m / x) <= n) { count = count + 1; } } printf( %d , count); } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; while (tt--) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } int ans = 0; sort(a.begin(), a.end()); for (int i = 0, j = 0; j < n; j++) { if (j - i + 1 >= a[j]) { ++ans; i = j + 1; } } cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(NULL); long long i, t, n, com, midc; cin >> n; map<int, int> m; set<int> s; vector<int> x(n); vector<int> a(3); for (i = 0; i < n; i++) { cin >> x[i]; m[x[i]]++; s.insert(x[i]); } t = s.size(); if (t == 1) { cout << n << endl; for (i = 0; i < n; i++) cout << x[i] << ; cout << endl; } else if (t == 2) { i = 0; for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) { a[i] = *it; i++; } if (abs(a[0] - a[1]) == 1) { cout << n << endl; for (i = 0; i < n; i++) cout << x[i] << ; cout << endl; } else { com = min(m[a[0]], m[a[1]]); cout << n - (2 * com) << endl; ; for (i = 0; i < (2 * com); i++) cout << a[0] + 1 << ; for (i = 0; i < m[a[0]] - com; i++) cout << a[0] << ; for (i = 0; i < m[a[1]] - com; i++) cout << a[1] << ; cout << endl; } } else { i = 0; for (set<int>::const_iterator it = s.begin(); it != s.end(); it++) { a[i] = *it; i++; } midc = m[a[1]] / 2; com = min(m[a[0]], m[a[2]]); if (midc > com) { cout << n - (midc * 2) << endl; for (i = 0; i < midc; i++) { cout << a[0] << ; cout << a[2] << ; } for (i = 0; i < m[a[0]]; i++) cout << a[0] << ; for (i = 0; i < m[a[2]]; i++) cout << a[2] << ; if (m[a[1]] % 2) cout << a[1]; cout << endl; } else { cout << n - (2 * com) << endl; for (i = 0; i < m[a[1]]; i++) cout << a[1] << ; for (i = 0; i < (2 * com); i++) cout << a[1] << ; for (i = 0; i < m[a[0]] - com; i++) cout << a[0] << ; for (i = 0; i < m[a[2]] - com; i++) cout << a[2] << ; cout << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const long long int INF = 1e12; int n, k; int memo[101][101][101]; int solve(string &str, int i, int N, int _max) { _max = max(N, _max); int &a = memo[i][N][_max]; if (a != -1) return a; if (i == n) return a = (_max == k); if (str[i] != N ) { bool temp1 = solve(str, i + 1, 0, _max); if (str[i] == Y || temp1) return a = temp1; } return a = solve(str, i + 1, N + 1, _max); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); memset(memo, -1, sizeof(memo)); cin >> n >> k; string str; cin >> str; cout << (solve(str, 0, 0, 0) ? YES : NO ); return 0; } |
#include <bits/stdc++.h> using namespace std; double EPS = 1e-9; int INF = 1000000000; long long INFF = 8000000000000000000LL; double PI = acos(-1); int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; inline string IntToString(int a) { char x[100]; sprintf(x, %d , a); string s = x; return s; } inline int StringToInt(string a) { char x[100]; int res; strcpy(x, a.c_str()); sscanf(x, %d , &res); return res; } inline string GetString(void) { char x[1000005]; scanf( %s , x); string s = x; return s; } inline string uppercase(string s) { int n = (int)s.size(); for (int(i) = (0); (i) < (n); ++(i)) if (s[i] >= a && s[i] <= z ) s[i] = s[i] - a + A ; return s; } inline string lowercase(string s) { int n = (int)s.size(); for (int(i) = (0); (i) < (n); ++(i)) if (s[i] >= A && s[i] <= Z ) s[i] = s[i] - A + a ; return s; } inline void OPEN(string s) { freopen((s + .in ).c_str(), r , stdin); freopen((s + .out ).c_str(), w , stdout); } int k, n1, n2, n3, t1, t2, t3; priority_queue<int, vector<int>, greater<int> > satu, dua, tiga; int t[100005]; bool can(int T) { if ((int)satu.size() == n1 && satu.top() + t1 > T) return false; if ((int)dua.size() == n2 && dua.top() + t2 > T + t1) return false; if ((int)tiga.size() == n3 && tiga.top() + t3 > T + t1 + t2) return false; return true; } int main() { scanf( %d %d %d %d %d %d %d , &k, &n1, &n2, &n3, &t1, &t2, &t3); int bef = 0; for (int(i) = (0); (i) < (k); ++(i)) { int ambil = bef; int L = t[i - 1]; int R = INF; while (L <= R) { int M = (L + R) >> 1; if (can(M)) { ambil = M; R = M - 1; } else L = M + 1; } if ((int)satu.size() == n1) { satu.pop(); } satu.push(ambil); if ((int)dua.size() == n2) { dua.pop(); } dua.push(ambil + t1); if ((int)tiga.size() == n3) { tiga.pop(); } tiga.push(ambil + t1 + t2); if (i == k - 1) { printf( %d n , ambil + t1 + t2 + t3); } bef = ambil; } return 0; } |
#include <bits/stdc++.h> using namespace std; void solve(int x, int y, int n) { vector<int> ans(n + 1); ans[x] = 1; ans[y] = 2; for (int i = 1, j = 3; i <= n; i++) { if (ans[i] == 0) { ans[i] = j++; } } for (int i = 1; i <= n; i++) { cout << ans[i] << ; } cout << n ; ans[y] = 1; for (int i = 1; i <= n; i++) { cout << ans[i] << ; } cout << n ; exit(0); } signed main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; if (n == 0 || n * (n - 1) / 2 == m) { cout << NO n ; } else { cout << YES n ; set<pair<int, int>> st; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; if (x > y) swap(x, y); st.insert({x, y}); } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (st.find({i, j}) == st.end()) { solve(i, j, n); } } } } return 0; } |
#include <bits/stdc++.h> using namespace std; map<long long, long long> mp, vis; vector<long double> vv; vector<pair<long long, long long> > vp; string s, q; char c; int main() { long double t = 0, n, m, l, r, mn = 0, mx = 0, ans = 0, nm = 0, cnt = 0, x, y, z, d, v, g; cin >> l >> d >> v >> g >> r; l -= d; t += d / v; long long p = (long long)t / (long long)(g + r); x = t - (p * (g + r)); if (x >= g) t += r - (x - g); t += l / v; cout << setprecision(10) << fixed << t; } |
#include <bits/stdc++.h> using namespace std; long long int max(long long int a, long long int b) { if (a > b) return a; else return b; } long long int min(long long int a, long long int b) { if (a < b) return a; else return b; } int main() { long long int t, a, b, x, y; cin >> t; while (t--) { long long int k1 = 0, k2 = 0, k3 = 0, k4 = 0, k5 = 0, k6 = 0, k7 = 0, c1, c2, c3, c4; cin >> a >> b >> x >> y; c1 = max(x, y); c2 = min(x, y); c3 = max(a, b); c4 = min(a, b); if (min(a, b) >= max(x, y)) { k1 = (a - x - 1) * b; k2 = x * b; k3 = (b - y - 1) * a; k4 = y * a; k5 = max(k1, k2); k6 = max(k3, k4); k7 = max(k5, k6); } else { k1 = (c3 - c1 - 1) * c4; k2 = c1 * c4; k3 = (c4 - c2 - 1) * c3; k4 = c2 * c3; k5 = max(k1, k2); k6 = max(k3, k4); k7 = max(k5, k6); } cout << k7; if (t != 0) cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int a[26][26] = {0}, n, mini = -1; cin >> n; while (n--) { char c[11]; scanf( %s , c); int k = strlen(c), i = c[0] - a , j = c[k - 1] - a ; for (int m = 0; m < 26; m++) if (a[m][i]) a[m][j] = max(a[m][j], a[m][i] + k); a[i][j] = max(a[i][j], k); mini = max(mini, a[j][j]); } cout << mini; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 500; int dist[maxn][maxn]; long long ans; void modify(int u, int v, int w) { if (dist[u][v] > w) { ans -= (dist[v][u] - w); dist[u][v] = w; } return; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> dist[i][j]; if (j > i) { ans += dist[i][j]; } } } int k; cin >> k; for (int i = 1; i <= k; i++) { int u, v, w; cin >> u >> v >> w; modify(u, v, w); if (dist[u][v] != dist[v][u]) { dist[v][u] = dist[u][v]; } for (int p = 1; p <= n; p++) { for (int q = 1; q <= n; q++) { modify(p, q, dist[p][u] + dist[v][q] + w); if (dist[p][q] != dist[q][p]) { dist[q][p] = dist[p][u] + dist[v][q] + w; } } } cout << ans << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1000; const int inf = (int)1e9 + 1; const long long big = (long long)1e18 + 1; const int P = 239; const int MOD = (int)1e9 + 7; const int MOD1 = (int)1e9 + 9; const double eps = 1e-9; const double pi = atan2(0, -1); const int ABC = 26; int cnt = 0, len, s, f; vector<int> g[N]; void solve(int k) { if (k == 1) { g[0].push_back(1); g[1].push_back(0); s = 0; f = 1; len = 1; cnt += 2; } else if (k % 2 == 0) { solve(k / 2); g[cnt].push_back(s); g[s].push_back(cnt); g[cnt + 1].push_back(s); g[s].push_back(cnt + 1); g[cnt + 2].push_back(cnt); g[cnt].push_back(cnt + 2); g[cnt + 2].push_back(cnt + 1); g[cnt + 1].push_back(cnt + 2); s = cnt + 2; len += 2; cnt += 3; } else { solve(k - 1); for (int i = 0; i < len; i++) { if (i == len - 1) { g[cnt - 1].push_back(f); g[f].push_back(cnt - 1); } else { g[cnt - 1].push_back(cnt); g[cnt].push_back(cnt - 1); cnt++; } } } } char ans[N][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(20); int k; cin >> k; solve(k); int id[cnt]; int curr = 2; for (int i = 0; i < cnt; i++) if (i == s) id[i] = 0; else if (i == f) id[i] = 1; else id[i] = curr++; for (int i = 0; i < cnt; i++) for (int j = 0; j < cnt; j++) ans[i][j] = N ; for (int i = 0; i < cnt; i++) for (int j : g[i]) ans[id[i]][id[j]] = Y ; cout << cnt << n ; for (int i = 0; i < cnt; i++) { for (int j = 0; j < cnt; j++) cout << ans[i][j]; cout << n ; } return 0; } |
#include <bits/stdc++.h> inline int Get() { char ch; while ((ch = getchar()) < 0 || ch > 9 ) ; int Num = ch - 0 ; while ((ch = getchar()) >= 0 && ch <= 9 ) Num = (Num << 3) + (Num << 1) + ch - 0 ; return Num; } const int N = 1e5 + 5; int n, m, a[N], count[N << 2], sum[N << 2], rev[N << 2]; std ::vector<int> vec0[N], vec1[N]; void build(int k, int l, int r) { sum[k] = r - l + 1; if (l == r) return; int mid = l + r >> 1; build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r); } void down(int k) { if (rev[k]) { count[k << 1] = sum[k << 1] - count[k << 1], rev[k << 1] ^= 1; count[k << 1 | 1] = sum[k << 1 | 1] - count[k << 1 | 1], rev[k << 1 | 1] ^= 1; rev[k] = 0; } } void modify(int k, int l, int r, int x, int y) { if (x <= l && r <= y) { rev[k] ^= 1, count[k] = sum[k] - count[k]; return; } int mid = l + r >> 1; down(k); if (x <= mid) modify(k << 1, l, mid, x, y); if (y > mid) modify(k << 1 | 1, mid + 1, r, x, y); count[k] = count[k << 1] + count[k << 1 | 1]; } int query(int k, int l, int r, int x, int y) { if (x <= l && r <= y) return count[k]; int mid = l + r >> 1; down(k); if (y <= mid) return query(k << 1, l, mid, x, y); if (x > mid) return query(k << 1 | 1, mid + 1, r, x, y); return query(k << 1, l, mid, x, y) + query(k << 1 | 1, mid + 1, r, x, y); } int main() { n = Get(), m = Get(); for (int i = 1; i <= n; ++i) a[i] = Get(); std ::sort(a + 1, a + n + 1); for (int i = 1; i <= m; ++i) { int l = std ::lower_bound(a + 1, a + n + 1, Get()) - a; int r = std ::upper_bound(a + 1, a + n + 1, Get()) - a - 1; if (1 <= l && l <= n && 1 <= r && r <= n && l <= r) vec0[l].push_back(r), vec1[r].push_back(l); } long long ans = (long long)n * (n - 1) * (n - 2) / 6; build(1, 1, n); for (int i = 1; i <= n; ++i) { for (int k = 0, sze = vec0[i].size(); k < sze; ++k) modify(1, 1, n, i, vec0[i][k]); int res = (i > 1 ? i - 1 - query(1, 1, n, 1, i - 1) : 0) + (i < n ? query(1, 1, n, i + 1, n) : 0); ans -= (long long)res * (res - 1) / 2; for (int k = 0, sze = vec1[i].size(); k < sze; ++k) modify(1, 1, n, vec1[i][k], i); } printf( %I64d n , ans); } |
#include <bits/stdc++.h> using namespace std; void upd(int& a, int b) { a = max(a, b); } int main() { int n, m; cin >> n >> m; vector<int> a(n), b(m); for (int i = 0; i < (int)n; ++i) cin >> a[i]; for (int i = 0; i < (int)m; ++i) cin >> b[i]; sort(a.begin(), a.end()); sort(b.begin(), b.end()); vector<int> to_seg(n); int seg = 0; vector<int> start, end; for (int i = 0; i < (int)a.size(); ++i) { if (i == 0 || a[i] != a[i - 1] + 1) { seg++; if (i != 0) end.push_back(i - 1); start.push_back(i); } to_seg[i] = seg - 1; } end.push_back(n - 1); vector<int> z(start.size()), d(start.size()); for (int segment = 0; segment < (int)start.size(); ++segment) { int first = -1; int cnt = 0; for (int i = 0; i < (int)m; ++i) { if (b[i] < a[start[segment]]) first = i; else if (b[i] <= a[end[segment]]) cnt++; } int cnt_orig = cnt; upd(d[segment], cnt); if (segment) upd(d[segment], cnt + z[segment - 1]); if (first != -1) { for (int i = first; i >= 0; --i) { ++cnt; int r = a[start[segment]]; int l = b[i]; if (start[segment] - (r - l) >= 0) { int Q = 0; int X = to_seg[start[segment] - (r - l)] - 1; if (X >= 0) Q = z[X]; upd(d[segment], cnt + Q); } } } cnt = 0; for (int i = first + cnt_orig + 1; i < m; ++i) { ++cnt; int l = a[end[segment]]; int r = b[i]; if (end[segment] + (r - l) < n) { upd(z[to_seg[end[segment] + (r - l)]], d[segment] + cnt); } } upd(z[segment], d[segment]); } cout << *max_element(z.begin(), z.end()) << endl; } |
#include <bits/stdc++.h> using namespace std; long long calc2(long long a, long long b, long long c, long long ab, long long ac, long long bc, long long abc) { long long parts[] = {a - ab - ac + abc, b - ab - bc + abc, c - ac - bc + abc, ab - abc, ac - abc, bc - abc, abc}; int masks[3] = {1 + 8 + 16 + 64, 2 + 8 + 32 + 64, 4 + 16 + 32 + 64}; static int mark[64 * 8] = {0}; static int id = 0; ++id; long long res = 0; for (int i = 0; i < 7; ++i) if ((masks[0] >> i) & 1) for (int j = 0; j < 7; ++j) if ((masks[1] >> j) & 1) for (int k = 0; k < 7; ++k) if ((masks[2] >> k) & 1) { int a1 = min(i, min(j, k)); int a3 = max(i, max(j, k)); int a2 = i + j + k - a1 - a3; if (mark[64 * a1 + 8 * a2 + a3] == id) continue; mark[64 * a1 + 8 * a2 + a3] = id; if (i == j && j == k) { long long x = parts[i]; res += +x * (x - 1) * (x - 2) / 6 + x * (x - 1) + x; } else if (i == j || j == k || i == k) { long long x = parts[i]; long long y = parts[j]; if (i == j) y = parts[k]; else if (j == k) swap(x, y); res += x * (x + 1) / 2 * y; } else { res += parts[i] * parts[j] * parts[k]; } } return res; } int get_common_num(vector<int> &a, vector<int> &b) { int ia = 0, ib = 0; int res = 0; while (ia < a.size() && ib < b.size()) { if (a[ia] == b[ib]) { ++res; ++ia, ++ib; } else if (a[ia] < b[ib]) ++ia; else ++ib; } return res; } int get_common_num(vector<int> &a, vector<int> &b, vector<int> &c) { int ia = 0, ib = 0, ic = 0; int res = 0; while (ia < a.size() && ib < b.size() && ic < c.size()) { if (a[ia] == b[ib] && b[ib] == c[ic]) { ++res; ++ia, ++ib, ++ic; } else if (a[ia] <= min(b[ib], c[ic])) ++ia; else if (b[ib] <= min(a[ia], c[ic])) ++ib; else ++ic; } return res; } vector<int> dividers[100111]; int main() { ios::sync_with_stdio(false); dividers[1].push_back(1); vector<int> smallest_fact(100111 + 1, 0); for (int i = 2; i < 100111; ++i) { if (!smallest_fact[i]) { smallest_fact[i] = i; for (long long j = i * (long long)i; j < 100111; j += i) { if (!smallest_fact[j]) smallest_fact[j] = i; } } int p = smallest_fact[i]; int x = i; while (x % p == 0) x /= p; int y = i; int pp = p; dividers[i] = dividers[x]; while (y % p == 0) { for (int d : dividers[x]) dividers[i].push_back(pp * d); pp *= p; y /= p; } sort(dividers[i].begin(), dividers[i].end()); } int t; cin >> t; while (t--) { int a, b, c; cin >> a >> b >> c; int x = dividers[a].size(); int y = dividers[b].size(); int z = dividers[c].size(); int xy = get_common_num(dividers[a], dividers[b]); int xz = get_common_num(dividers[a], dividers[c]); int yz = get_common_num(dividers[b], dividers[c]); int xyz = get_common_num(dividers[a], dividers[b], dividers[c]); long long ans = calc2(x, y, z, xy, xz, yz, xyz); cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; map<string, int> my; int n, m; string var[5006]; string bit[5006]; int op[5006][2]; int opera[5006]; int ans[5005][1003]; int print1[1005], print2[1005]; int main() { scanf( %d , &n); scanf( %d , &m); for (int i = 1; i <= n; i++) { string z; cin >> z; my[z] = i; var[i] = z; cin >> z; cin >> z; if (z[0] == ? ) { op[i][0] = 0; cin >> z; if (z[0] == A ) opera[i] == 1; else if (z[0] == O ) opera[i] = 2; else opera[i] = 3; cin >> z; if (z[0] == ? ) op[i][1] = 0; else op[i][1] = my[z]; } else if (z[0] == 0 || z[0] == 1 ) bit[i] = z; else { op[i][0] = my[z]; cin >> z; if (z[0] == A ) opera[i] = 1; else if (z[0] == O ) opera[i] = 2; else opera[i] = 3; cin >> z; if (z[0] == ? ) op[i][1] = 0; else op[i][1] = my[z]; } } for (int i = 0; i < m; i++) { int cnt[2]; cnt[0] = cnt[1] = 0; for (int j = 0; j <= 1; j++) { for (int k = 1; k <= n; k++) { if (opera[k] == 0) { ans[k][i] = bit[k][i] - 0 ; cnt[j] += ans[k][i]; } else { int v1 = op[k][0], v2 = op[k][1]; if (v1 == 0) ans[v1][i] = j; if (v2 == 0) ans[v2][i] = j; if (opera[k] == 1) ans[k][i] = ans[v1][i] & ans[v2][i]; else if (opera[k] == 2) ans[k][i] = ans[v1][i] | ans[v2][i]; else ans[k][i] = ans[v1][i] ^ ans[v2][i]; cnt[j] += ans[k][i]; } } } if (cnt[0] > cnt[1]) { print1[i] = 0; print2[i] = 1; } else if (cnt[0] < cnt[1]) { print1[i] = 1; print2[i] = 0; } else { print1[i] = 0; print2[i] = 0; } } for (int i = 0; i < m; i++) cout << print2[i]; cout << endl; for (int i = 0; i < m; i++) cout << print1[i]; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; long long power(long long a, long long b) { long long ans = 1; while (b > 0) { if (b & 1) ans *= a; a = a * a; b >>= 1; } return ans; } long long powm(long long a, long long b) { a %= MOD; long long ans = 1; while (b > 0) { if (b & 1) ans = (ans * a) % MOD; a = (a * a) % MOD; b >>= 1; } return ans; } void IO() {} int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); IO(); ; long long n, k; cin >> n >> k; long long A[k]; for (long long &x : A) cin >> x; long long pos = 0, res = LLONG_MAX; for (long long i = 0; i < k; i++) { if (n % A[i] < res) { pos = i + 1; res = n % A[i]; } } cout << pos << << n / A[pos - 1] << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize( Ofast ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,avx,avx2 ) const long long maxn = 64, mod = 1e9 + 7, INF = 1e9, lg = 15 + 1, SQRT = 400; int n, a[maxn], par[maxn], SZ_T[maxn], FAC[maxn], INV[maxn]; vector<int> adj1[maxn], adj2[maxn], cmp; long long dp[(1 << lg)][maxn], ans[maxn]; bool mark[maxn]; inline long long power(long long a, long long b) { long long res = 1; while (b) { if (b & 1) { res = (res * a) % mod; } b /= 2; a = (a * a) % mod; } return res; } inline void make_graph() { sort(a, a + n); for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (a[j] % a[i] == 0) { adj1[i].push_back(j); adj1[j].push_back(i); } } } } void dfs(int v) { mark[v] = 1; cmp.push_back(a[v]); for (auto u : adj1[v]) { if (!mark[u]) { dfs(u); } } } inline void get(int cnt) { memset(dp, 0, sizeof dp); sort(cmp.begin(), cmp.end()); vector<int> s, t; for (int i = 0; i < cmp.size(); ++i) { bool is = 0; for (int j = 0; j < i; ++j) { if (cmp[i] % cmp[j] == 0) is = 1; } if (!is) { s.push_back(cmp[i]); } else { t.push_back(cmp[i]); } } for (int i = 0; i < t.size(); ++i) { par[i] = 0; for (int j = 0; j < s.size(); ++j) { if (t[i] % s[j] == 0) { par[i] |= (1 << j); } } } SZ_T[cnt] = t.size(); if (!t.size()) { ans[cnt] = 1; return; } for (int i = 0; i < t.size(); ++i) { dp[par[i]][1]++; } for (int mask = 0; mask < (1 << s.size()); ++mask) { for (int i = 0; i < t.size(); ++i) { int counter = 0; for (int j = 0; j < t.size(); ++j) { if ((mask | par[j]) == mask) { counter++; } else if (mask & par[j]) { dp[mask | par[j]][i + 1] = (dp[mask][i] + dp[mask | par[j]][i + 1]) % mod; } } if (counter > i) dp[mask][i + 1] = (dp[mask][i + 1] + (dp[mask][i] * (counter - i)) % mod) % mod; } } ans[cnt] = dp[(1 << s.size()) - 1][t.size()]; } int main() { ios::sync_with_stdio(false); cin.tie(0); FAC[0] = INV[0] = 1; for (int i = 1; i < maxn; ++i) { FAC[i] = (FAC[i - 1] * i) % mod; INV[i] = power(FAC[i], mod - 2); } cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } make_graph(); int cnt = 0; for (int i = 0; i < n; ++i) { if (!mark[i]) { dfs(i); get(cnt); cnt++; cmp.clear(); } } long long res = 1, SZ = 0; for (int i = 0; i < cnt; ++i) { res = (res * ans[i]) % mod; SZ += max(SZ_T[i] - 1, 0); } long long h = FAC[SZ]; for (int i = 0; i < cnt; ++i) { h = (h * INV[max(SZ_T[i] - 1, 0)]) % mod; } res = (res * h) % mod; cout << res << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, s = 0, c = 0; cin >> n; for (long long int i = 0; i < n; i++) { long long x; cin >> x; s += x; if (x % 2 != 0) c = 1; } if (s % 2 != 0) { cout << First ; } else { if (c) cout << First ; else cout << Second ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, d, moves = 0; int* b; cin >> n >> d; b = new int[n]; cin >> b[0]; for (int i = 1; i < n; i++) { cin >> b[i]; if (b[i] <= b[i - 1]) { int time = (b[i - 1] - b[i]) / d + 1; b[i] += time * d; moves += time; } } delete[] b; cout << moves; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.