func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; int n, x, trie[2][31 * maxn], cnt[31 * maxn], a[maxn], tot = 1; inline void insert(int x) { int p = 1, t; for (int i = 31; i >= 0; i--) { t = (1 << i) & x ? 1 : 0; if (!trie[t][p]) trie[t][p] = ++tot; p = trie[t][p]; cnt[p]++; } } inline int get_max(int x) { int p = 1, t, res = 0; for (int i = 31; i >= 0; i--) { t = (1 << i) & x ? 1 : 0; if (trie[t][p] && cnt[trie[t][p]]) { p = trie[t][p]; } else { p = trie[t ^ 1][p]; res |= (1 << i); } cnt[p]--; } return res; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int i = 1; i <= n; i++) { scanf( %d , &x); insert(x); } for (int i = 1; i <= n; i++) { printf( %d%c , get_max(a[i]), i == n ? n : ); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string a, b, s; cin >> s; vector<string> g; for (long long i = 0; i < s.length(); i++) { a += s[i]; if (s[i] != a ) { b += s[i]; } if (a.length() + b.length() == s.length()) { break; } } if (s == a + b) { cout << a; } else { cout << :( << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; if (n % 2 == 0) { cout << 2; } else cout << 1; } |
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; inline int read() { int X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == - ; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } int n, m; int dx[4][5] = { {2, 1, 0, 0, 0}, {1, 0, 0, 0, -1}, {0, 0, 0, -1, -2}, {1, 0, 0, 0, -1}}; int dy[4][5] = { {0, 0, -1, 0, 1}, {0, 0, 1, 2, 0}, {-1, 0, 1, 0, 0}, {0, -2, -1, 0, 0}}; char maze[9][9]; int ans = -1; char as[9][9]; bool check(int x, int y, int dir, char c) { for (int i = 0; i < 5; i++) { int nx = x + dx[dir][i]; int ny = y + dy[dir][i]; if (nx < 0 or nx >= n or ny < 0 or ny >= m) { return 0; } if (!(c == . or maze[nx][ny] == . )) { return 0; } } for (int i = 0; i < 5; i++) { int nx = x + dx[dir][i]; int ny = y + dy[dir][i]; maze[nx][ny] = c; } return 1; } void dfs(int x, int y, char c) { if (x == n) { if (c - A > ans) { ans = c - A ; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { as[i][j] = maze[i][j]; } } } return; } if ((n - x) * m / 5 + c - A <= ans) { return; } if (y == m) { dfs(x + 1, 0, c); return; } for (int i = 0; i < 4; i++) { if (check(x, y, i, c)) { dfs(x, y + 1, c + 1); check(x, y, i, . ); } } dfs(x, y + 1, c); } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { maze[i][j] = . ; } } dfs(0, 0, A ); printf( %d n , ans); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { printf( %c , as[i][j]); } printf( n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; const double EPS = 1e-4; void solve() { int n; cin >> n; vector<long long> a(n); vector<pair<long long, long long>> diff(n); for (long long i = 0; i < int(n); i++) cin >> a[i]; for (long long i = 0; i < int(n); i++) diff[i] = {a[i] - i, i}; sort(diff.begin(), diff.end()); long long curr_sum = 0, res = 0; for (long long i = 1; i < n; ++i) { if (curr_sum == 0LL && diff[i].first == diff[i - 1].first) { curr_sum += (a[diff[i].second] + a[diff[i - 1].second]); } else if (diff[i].first == diff[i - 1].first) { curr_sum += a[diff[i].second]; } else { curr_sum = 0; } res = max(res, curr_sum); } for (long long i = 0; i < int(n); i++) res = max(res, a[i]); cout << res; debug() << [ << diff : << (diff) << ] ; } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); } |
#include <bits/stdc++.h> using namespace std; const long double pi = 3.14159265359; template <typename T> T abs(T x) { return x > 0 ? x : -x; } template <typename T> T sqr(T x) { return x * x; } const long long mod = 1000000007; struct matr { int maxn; int n; long long a[1 << 7][1 << 7]; matr(int _n) { maxn = 1 << _n; n = _n; for (int i = 0; i < maxn; i++) for (int j = 0; j < maxn; j++) a[i][j] = 0; } matr operator*(const matr &other) const { matr res(n); long long mod2 = mod * mod; for (int i = 0; i < maxn; i++) for (int j = 0; j < maxn; j++) { for (int h = 0; h < maxn; h++) { res.a[i][j] += (a[i][h] * other.a[h][j]); if (res.a[i][j] >= mod2) res.a[i][j] -= mod2; } res.a[i][j] %= mod; } return res; } vector<long long> operator*(const vector<long long> &row) { vector<long long> res(maxn); for (int i = 0; i < maxn; i++) { for (int j = 0; j < maxn; j++) { res[i] += (a[i][j] * row[j]) % mod; } res[i] %= mod; } return res; } }; matr gen(int n) { if (n == 1) { matr res(n); res.a[0][1] = 1; res.a[0][0] = 1; res.a[1][0] = 1; return res; } matr res(7); for (int _left = 0; _left < (1 << n); _left++) { for (int mid = 0; mid < (1 << (n - 1)); mid++) { int cmid = (mid << 1) | 1 | (1 << n); for (int _right = 0; _right < (1 << n); _right++) { bool bad = false; for (int j = 0; j < n && !bad; j++) { bad |= bool(_left & (1 << j)) && bool(_right & (1 << j)) && bool(cmid & (1 << j)) && bool(cmid & (1 << (j + 1))); } if (!bad) { res.a[_right][_left]++; } } } } return res; } int main() { vector<long long> ans(1 << 7); ans[1] = 1; for (int i = 1; i <= 7; i++) { int n; cin >> n; matr x(7), res(7); x = gen(i); for (int j = 0; j < (1 << i); j++) for (int h = 0; h < (1 << i); h++) res.a[j][h] = j == h; while (n) { if (n & 1) res = res * x; x = x * x; n >>= 1; } ans = res * ans; if (i == 7) break; for (int j = 0; j < (1 << i); j++) { ans[j | (1 << i)] = ans[j]; ans[j] = 0; } } cout << ans[(1 << 7) - 1] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long Inf = 1000000000000000000ll; const int inf = 1000000000; vector<long long> pri, fac; long long m, x, ans; void getpri(long long t, vector<long long>& v) { v.clear(); for (long long i = 2; i * i <= t; ++i) { if (t % i) continue; v.push_back(i); while (t % i == 0) t /= i; } if (t != 1) v.push_back(t); sort(v.begin(), v.end()); } long long Euler(long long t) { long long res = t; for (int i = 0; i < pri.size(); ++i) if (t % pri[i] == 0) res = res / pri[i] * (pri[i] - 1); return res; } long long mul(long long x, long long y, long long mod) { long long res = 0; while (y) { if (y & 1) (res += x) >= mod && (res -= mod); (x <<= 1) >= mod && (x -= mod); y >>= 1ll; } return res; } long long qpow(long long b, long long p, long long mod) { long long res = 1ll; b %= mod; while (p) { if (p & 1ll) res = mul(b, res, mod); b = mul(b, b, mod); p >>= 1ll; } return res; } void work(long long md) { long long f_d = Euler(md); long long l_d = f_d; if (l_d > 1) { for (int j = 0; j < fac.size(); ++j) while (l_d % fac[j] == 0 && qpow(x, l_d / fac[j], md) == 1) l_d /= fac[j]; } ans += f_d / l_d; } int main() { scanf( %lld%lld , &m, &x); getpri(m, pri); long long eul = Euler(m); if (eul > 1) getpri(eul, fac); else fac.push_back(1); for (long long i = 1; i * i <= m; ++i) { if (m % i) continue; work(i); if (i * i != m) work(m / i); } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int main(int argc, char *argv[]) { long long n, k; cin >> n >> k; long long altura = ceil((long double)k / n); cout << altura; return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T, class V> ostream& operator<<(ostream& s, pair<T, V> a) { s << a.first << << a.second; return s; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); long long int T = 1; cin >> T; for (long long int qq = 1; qq <= T; qq++) { long long int n; string a, b; cin >> n >> a >> b; vector<long long int> ans; long long int f = 0, l = n - 1, r = 0; for (int i = n - 1; i >= 0; i--) { if (b[i] - 0 != ((a[l] - 0 ) ^ f)) { if (a[l] != a[r]) a[r] = (a[r] - 0 ) ^ 1 + 0 , ans.push_back(1); f ^= 1; ans.push_back(i + 1); swap(l, r); } if (l > r) l--; else l++; } cout << ans.size(); for (auto x : ans) cout << << x; cout << endl; } } |
#include<bits/stdc++.h> using namespace std; #define int long long #define f(i,n) for(int i=0;i<(n);++i) #define fa(i,a,n) for(int i=a;i<=(n);++i) #define fd(i,a,n) for(int i=a;i>=(n);--i) #define tc int t;cin>>t;f(testcase,t) #define ld long double #define pii pair<int,int> #define mem(a,i) memset(a,i,sizeof(a)) #define all(x) x.begin(),x.end() #define tr(c,i) for(auto i = c.begin(); i != c.end(); i++) #define present(c,x) (c.find(x) != c.end()) #define cpresent(c,x) (find(allc,x) != c.end()) #define PI 3.1415926535897932 void c_p_c() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } void solve() { int n; cin >> n; int a[n]; map<int, int> m; f(i, n) { cin >> a[i]; m[a[i]]++; } cout << n - m.begin()->second << n ; } int32_t main() { c_p_c(); tc solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; void c() { int n; cin >> n; vector<long long> a(n); for (int(i) = (0); (i) < (n); ++(i)) cin >> a[i]; vector<long long> pf(n, a[0]); for (int(i) = (1); (i) < (n); ++(i)) pf[i] = pf[i - 1] ^ a[i]; map<long long, vector<pair<long long, long long>>> mp; } void b() { int n; cin >> n; vector<int> a(n); for (int(i) = (0); (i) < (n); ++(i)) cin >> a[i]; int sum = accumulate((a).begin(), (a).end(), 0); int best = sum; sort((a).begin(), (a).end()); for (int(r) = (1); (r) < (n); ++(r)) { sum -= a[r]; sum -= a[0]; for (int d = 1; d * d <= a[r]; d++) { if (a[r] % d == 0) { best = min(best, sum + a[r] / d + a[0] * d); best = min(best, sum + a[r] / (a[r] / d) + a[0] * (a[r] / d)); } } sum += a[r]; sum += a[0]; } cout << best; } int main() { b(); return 0; } |
#include <bits/stdc++.h> using namespace std; double mar[100000]; int main() { double ml, rl; cin >> ml >> rl; double m = ml, r = rl; double save = 0; if (ml >= 3) save += 2 * m * (m - 3) + 2 * (m - 2) + 6; else if (ml == 2) save += 2; save *= (sqrt(2) - 2); double ans = m * (m + 1) + m * (m + 1) * (2 * m + 1) / 3 - 2 * (m); ans += save; ans *= r; ans /= (m * m); printf( %.10f n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); int n, x; cin >> n; vector<vector<int>> adj(n); for (int i = 1; i < n; i++) cin >> x, x--, adj[x].push_back(i), adj[i].push_back(x); queue<int> q; q.push(0); map<int, int> depth, count; depth[0] = 0; count[0]++; while (!q.empty()) { int cur = q.front(); q.pop(); for (auto nxt : adj[cur]) if (depth.find(nxt) == depth.end()) depth[nxt] = depth[cur] + 1, count[depth[nxt]]++, q.push(nxt); } int ret = 0; for (auto it : count) ret += it.second % 2; cout << ret << endl; return 0; } |
#include <bits/stdc++.h> int main() { int i, j, n, k; while (scanf( %d %d , &n, &k) == 2) { int f, t, max = -2000000000; for (i = 0; i < n; i++) { scanf( %d %d , &f, &t); if (t > k) { if ((f - t + k) > max) max = f - t + k; } else { if (f > max) max = f; } } printf( %d n , max); } } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; for (int i = 1; i <= t; i++) { int n, k, d; cin >> n >> k >> d; int mn = n; int a[n + 10]; for (int j = 1; j <= n; j++) cin >> a[j]; for (int j = 1; j <= n - d + 1; j++) { set<int> s; for (int q = j; q <= j + d - 1; q++) s.insert(a[q]); int z = s.size(); mn = min(mn, z); } cout << mn << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, numa, numb, ans; bool f = 0; cin >> n >> a >> b; for (int i = 2, cnt = 1; i <= n; i *= 2) { numa = a / i; if (a % i != 0) numa++; numb = b / i; if (b % i != 0) numb++; if (numa == numb) { if (i == n) f = 1; ans = cnt; break; } cnt++; } if (f == 1) cout << Final! << endl; else cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long x[100005], y[112345]; set<long long> stx, sty; int main() { long long n, m, a, b; scanf( %lld %lld , &n, &m); long long total = n * n; while (m--) { scanf( %lld %lld , &a, &b); if (x[a] == 0 && y[b] == 0) { total = total - 2 * n + stx.size() + sty.size() + 1; } else if (x[a] && y[b] == 0) { total = total - n + stx.size(); } else if (y[b] && x[a] == 0) total = total - n + sty.size(); x[a] = y[b] = 1; stx.insert(a); sty.insert(b); printf( %lld , total); } } |
#include <bits/stdc++.h> using namespace std; int n; double a[22][22], p[20][(1 << 18) + 10], pc[(1 << 18) + 10], sm[20][(1 << 18) + 10]; int main() { while (scanf( %d , &n) != EOF) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { scanf( %lf , &a[i][j]); } memset(sm, 0, sizeof(sm)); for (int mask = 0; mask < (1 << n); mask++) { pc[mask] = 0.0; for (int bit = 0; bit < n; bit++) { if (mask & (1 << bit)) { pc[mask]++; for (int cara = 0; cara < n; cara++) { sm[cara][mask] += a[bit][cara]; } } } } for (int niv = 0; niv < n; niv++) { for (int mask = 0; mask < (1 << n); mask++) { if (niv == 0) { if (mask == ((1 << n) - 1)) { p[niv][mask] = 1.0; } else { p[niv][mask] = 0.0; } } else { double sum = 0.0; for (int bit = 0; bit < n; bit++) { if ((mask & (1 << bit)) == 0) { sum += p[niv - 1][mask | (1 << bit)] * sm[bit][mask]; } } p[niv][mask] = 2.0 * sum / (pc[mask] * (pc[mask] + 1)); } } } for (int i = 0; i < n; i++) { printf( %lf%c , p[n - 1][1 << i], n [i == (n - 1)]); } } return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << : << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, , ); cerr.write(names, comma - names) << : << arg1 << | ; __f(comma + 1, args...); } int main(void) { int n; scanf( %d , &n); int d; scanf( %d , &d); int h; scanf( %d , &h); if (d > 2 * h || d < h || (n > 2 && (d <= 1)) || (h < 1)) { cout << -1 n ; return 0; } else { int it = 1; int c = 0; while (it <= h) { cout << it << << it + 1 << endl; it++; c++; } if (d != h) { if (c < n - 1) { cout << 1 << << it + 1 << endl; c++; it += 1; d = d - h - 1; while (d > 0 && c < n - 1) { cout << it << << it + 1 << endl; it++; d--; c++; } while (c < n - 1) { cout << 1 << << it + 1 << endl; it++; c++; } } } else { if (c < n - 1) { cout << 2 << << it + 1 << endl; c++; it += 1; d = d - h - 1; while (d > 0 && c < n - 1) { cout << it << << it + 1 << endl; it++; d--; c++; } while (c < n - 1) { cout << 2 << << it + 1 << endl; it++; c++; } } } } return (0); } |
#include <bits/stdc++.h> using namespace std; const int N = 5005; const long long INF = 1e18; struct node { int u, v, w; node(int u = 0, int v = 0, int w = 0) : u(u), v(v), w(w) {} } a[2 * N]; int fst[N], nxt[2 * N], tot, n, size[N]; bool vis[N]; long long ans, sum[N], ans0, mn; void build(int x, int y, int z) { a[tot] = node(x, y, z); nxt[tot] = fst[x]; fst[x] = tot++; } void calc(int x) { int y; size[x] = 1; for (int p = fst[x]; ~p; p = nxt[p]) { if (vis[p / 2]) continue; vis[p / 2] = 1; y = a[p].v; calc(y); size[x] += size[y]; ans += (long long)a[p].w * size[y] * (n - size[y]); } } void dfs1(int x) { int y; sum[x] = 0; size[x] = 1; for (int p = fst[x]; ~p; p = nxt[p]) { if (vis[p / 2]) continue; vis[p / 2] = 1; y = a[p].v; dfs1(y); sum[x] = (long long)sum[y] + (long long)size[y] * a[p].w; size[x] += size[y]; } } void dfs2(int x, int all) { int y; for (int p = fst[x]; ~p; p = nxt[p]) { if (vis[p / 2]) continue; vis[p / 2] = 1; y = a[p].v; sum[y] = sum[x] - (long long)size[y] * a[p].w + (long long)(all - size[y]) * a[p].w; mn = min(mn, sum[y]); dfs2(y, all); } } long long work(int x, int i) { for (int j = 0; j < n - 1; j++) vis[j] = i == j ? 1 : 0; dfs1(x); for (int j = 0; j < n - 1; j++) vis[j] = i == j ? 1 : 0; mn = sum[x]; dfs2(x, size[x]); return (long long)(mn - sum[x]) * (n - size[x]); } int main() { int x, y, z; scanf( %d , &n); tot = 0; memset(fst, -1, sizeof(fst)); for (int i = 0; i < n - 1; i++) { scanf( %d%d%d , &x, &y, &z); x--; y--; build(x, y, z); build(y, x, z); } ans = 0; calc(0); ans0 = ans; long long tmp; for (int i = 0; i < n - 1; i++) { tmp = ans0 + work(a[i * 2].u, i) + work(a[i * 2].v, i); ans = min(ans, tmp); } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; class DSU { public: vector<long long> p; DSU() {} DSU(long long n) { p.resize(n + 1); for (long long i = 1; i <= n; i++) p[i] = i; } void join(long long a, long long b) { p[find(b)] = find(a); } long long find(long long a) { return a == p[a] ? a : p[a] = find(p[a]); } bool check(long long a, long long b) { return find(a) == find(b); } }; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m; cin >> n >> m; DSU d(n); vector<DSU> D; for (long long i = 0; i <= m; i++) D.push_back(d); for (long long i = 0; i < m; i++) { long long u, v, c; cin >> u >> v >> c; D[c].join(u, v); } long long q; cin >> q; while (q--) { long long u, v; cin >> u >> v; long long ans = 0; for (long long i = 1; i <= m; i++) if (D[i].check(u, v)) ans++; cout << ans << endl; } } |
#include <bits/stdc++.h> using namespace std; const int N = 500004; int a[N], m, k, s, t, n, sz = 0; map<int, int> mp, mq; void add(int n, int val) { if (n == -1) return; if (val == 1) { ++mq[n]; if (mq[n] == mp[n]) ++sz; } else { if (mq[n] == mp[n]) --sz; --mq[n]; } } int main() { scanf( %d %d %d %d , &m, &k, &n, &s); for (int i = 1; i <= m; i++) scanf( %d , &a[i]); for (int i = 1; i <= s; i++) { scanf( %d , &t); ++mp[t]; } for (int i = 1; i <= m; i++) if (!mp.count(a[i])) a[i] = -1; bool ok = 0; vector<int> v; for (int l = 1, r = 1; l <= m && !ok; l++) { for (; r <= m && sz < mp.size(); r++) add(a[r], 1); if (sz >= mp.size()) { if ((l - 1) / k + (m - r + 1) / k >= n - 1) { ok = 1; for (int i = l; i < r; i++) { if (a[i] != -1 && mp[a[i]] > 0) { --mp[a[i]]; a[i] = -2; } } for (int i = l; i < r; i++) { if (a[i] != -2 && (int)v.size() < r - l - k) v.push_back(i); } int cnt = (l - 1) % k; for (int i = 1; i <= cnt; i++) v.push_back(i); } } add(a[l], -1); } if (!ok) puts( -1 ); else { printf( %d n , v.size()); for (int x : v) printf( %d , x); } } |
#include <bits/stdc++.h> using namespace std; string s[] = { ABSINTH , BEER , BRANDY , CHAMPAGNE , GIN , RUM , SAKE , TEQUILA , VODKA , WHISKEY , WINE }; int main() { int n; scanf( %d , &n); string ss; int sol = 0; for (int i = 0; i < n; i++) { cin >> ss; if (isalpha(ss[0])) { for (int j = 0; j < 11; j++) if (ss == s[j]) { sol++; break; } } else { int age = 0; for (int j = 0; j < ss.length(); j++) { age *= 10; age += (ss[j] - 0 ); } if (age < 18) sol++; } } printf( %d n , sol); return 0; } |
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, /STACK:102400000,102400000 ) const int N = 100005; long long ans; int n, a[N], pi; vector<int> G[N]; int s[2], flag, cnt[2]; map<int, int> mp[2]; void upd(int u, int dep) { if ((dep % 2) != (flag % 2)) { s[flag ^ 1] ^= a[u]; mp[flag ^ 1][a[u]]++; ++cnt[flag ^ 1]; } else { s[flag] ^= a[u]; mp[flag][a[u]]++; ++cnt[flag]; } } void dfs(int u, int fa, int dep) { if (G[u].size() == 1 && u != 1) { if ((dep & 1) && flag == 0) flag ^= 1; } for (auto to : G[u]) if (to != fa) dfs(to, u, dep + 1); upd(u, dep); } int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); for (int i = 2; i <= n; ++i) { scanf( %d , &pi); G[pi].push_back(i); G[i].push_back(pi); } dfs(1, 0, 0); if (s[flag] == 0) { if (cnt[flag] > 1) ans += 1LL * cnt[flag] * (cnt[flag] - 1) / 2; if (cnt[flag ^ 1] > 1) ans += 1LL * cnt[flag ^ 1] * (cnt[flag ^ 1] - 1) / 2; for (auto it = mp[flag].begin(); it != mp[flag].end(); ++it) { int tmp1 = it->first, tmp2 = it->second, tmp3 = mp[flag ^ 1][tmp1]; ans += 1LL * tmp2 * tmp3; } } else { for (auto it = mp[flag].begin(); it != mp[flag].end(); ++it) { int tmp1 = it->first, tmp2 = it->second, tmp3 = mp[flag ^ 1][s[flag] ^ tmp1]; ans += 1LL * tmp2 * tmp3; } } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; class data { public: long long k, b; bool operator<(const data a) const { if (k * a.k < 0) return (b * a.k < a.b * k); else return (b * a.k > a.b * k); } bool operator==(const data a) const { return b * a.k == a.b * k; } } d[100010]; long long n, tot = 0; int main() { long long x, y, cnt = 0; scanf( %I64d , &n); for (long long i = 1; i <= n; ++i) { ++tot; scanf( %I64d%I64d , &d[tot].k, &d[tot].b); if (d[tot].k == 0) { --tot; continue; } } n = tot; sort(d + 1, d + tot + 1); long long nowk = 0; for (long long i = 1; i <= n; ++i) if ((d[i - 1] == d[i]) && (i > 1)) { nowk += abs(d[i].k); } else { if (nowk > 0) cnt++; nowk = abs(d[i].k); } if (nowk) cnt++; printf( %I64d n , cnt); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 2 == 1) { cout << -1 << endl; } else { cout << 2 1 ; for (int i = 3; i < n; i += 2) { cout << << i + 1 << << i; } cout << endl; } return 0; } |
#include <bits/stdc++.h> int a[15][200000][15]; int b[15] = {0}; int main() { a[1][0][0] = 0; b[1] = 1; int n; scanf( %d , &n); for (int i = 0; i < n; i++) { for (int j = 0; j < b[i]; j++) { int m = 0; for (int k = 0; k <= i; k++) if (a[i][j][k] > m) m = a[i][j][k]; m++; if (j % 2 == 0) { for (int k = 0; k <= i; k++) a[i + 1][b[i + 1]][k] = a[i][j][k]; a[i + 1][b[i + 1]][i + 1] = 0; b[i + 1]++; for (int r = m; r > 0; r--) { for (int k = 0; k <= i; k++) a[i + 1][b[i + 1]][k] = a[i][j][k]; a[i + 1][b[i + 1]][i + 1] = r; b[i + 1]++; } } else { for (int r = 1; r <= m; r++) { for (int k = 0; k <= i; k++) a[i + 1][b[i + 1]][k] = a[i][j][k]; a[i + 1][b[i + 1]][i + 1] = r; b[i + 1]++; } for (int k = 0; k <= i; k++) a[i + 1][b[i + 1]][k] = a[i][j][k]; a[i + 1][b[i + 1]][i + 1] = 0; b[i + 1]++; } } } printf( %d n , b[n]); for (int i = 0; i < b[n]; i++) { int m = 0; for (int j = 1; j <= n; j++) if (a[n][i][j] > m) m = a[n][i][j]; for (int j = 0; j <= m; j++) { if (j > 0) printf( , ); printf( { ); bool first = true; for (int k = 1; k <= n; k++) if (a[n][i][k] == j) { if (first) first = false; else printf( , ); printf( %d , k); } printf( } ); } printf( n ); } } |
#include <bits/stdc++.h> using namespace std; template <typename T> inline T qmin(const T &a, const T &b) { return a < b ? a : b; } template <typename T> inline T qmax(const T &a, const T &b) { return a > b ? a : b; } template <typename T> inline void getmin(T &a, const T &b) { if (a > b) a = b; } template <typename T> inline void getmax(T &a, const T &b) { if (a < b) a = b; } inline void fileio(string s) { freopen((s + .in ).c_str(), r , stdin); freopen((s + .out ).c_str(), w , stdout); } const int inf = 1e9 + 7; const int N = 51, M = 2953; const int go[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; char g[N][N]; int tp[N][N]; map<int, int> id; int mask[M], cnt; int dis[N][N][M]; bool vis[N][N][M], can[N][N][M]; int ans[N][N][M]; vector<pair<pair<int, int>, int> > to[N][N][M]; int n, m, k, sti, stj, edi, edj; inline void bfs() { static queue<pair<pair<int, int>, int> > q; static int st, ed; dis[sti][stj][1] = 1; q.push(make_pair(make_pair(sti, stj), 1)); while (!q.empty()) { pair<pair<int, int>, int> p = q.front(); q.pop(); int i = p.first.first, j = p.first.second, comb = mask[p.second]; if (dis[i][j][p.second] == dis[edi][edj][1]) break; comb |= (~tp[i][j] ? 1 << tp[i][j] : 0); for (register int d = 0; d < (4); ++d) { int ni = i + go[d][0], nj = j + go[d][1]; if (ni < 1 || ni > n || nj < 1 || nj > m || (ni == sti && nj == stj)) continue; int ncomb = comb; if (~tp[ni][nj]) { ncomb &= ~(1 << tp[ni][nj]); if (__builtin_popcount(ncomb) >= k) continue; } else { ncomb = 0; } int cid = id[ncomb]; if (dis[ni][nj][cid]) { if (dis[ni][nj][cid] == dis[i][j][p.second] + 1) to[i][j][p.second].push_back(make_pair(make_pair(ni, nj), cid)); continue; } dis[ni][nj][cid] = dis[i][j][p.second] + 1; q.push(make_pair(make_pair(ni, nj), cid)); to[i][j][p.second].push_back(make_pair(make_pair(ni, nj), cid)); } } } inline bool cmp(const pair<pair<int, int>, int> &a, const pair<pair<int, int>, int> &b) { pair<int, int> p = a.first, q = b.first; if (g[p.first][p.second] != g[q.first][q.second]) return g[p.first][p.second] < g[q.first][q.second]; if (p.first == edi && p.second == edj) return false; return cmp(to[p.first][p.second][a.second][ans[p.first][p.second][a.second]], to[q.first][q.second][b.second][ans[q.first][q.second][b.second]]); } inline void dfs(int i, int j, int k) { if (vis[i][j][k]) return; vis[i][j][k] = true; if (i == edi && j == edj) { can[i][j][k] = true; return; } vector<pair<int, int> > nxt; for (register int d = 0; d < (to[i][j][k].size()); ++d) { pair<pair<int, int>, int> p = to[i][j][k][d]; int ni = p.first.first, nj = p.first.second, nk = p.second; nxt.push_back(make_pair(tp[ni][nj], d)); } sort(nxt.begin(), nxt.end()); for (int id1 = 0, id2; id1 < nxt.size(); id1 = id2) { int bst = -1; for (id2 = id1; id2 < nxt.size() && nxt[id2].first == nxt[id1].first; ++id2) { pair<pair<int, int>, int> p = to[i][j][k][nxt[id2].second]; dfs(p.first.first, p.first.second, p.second); if (can[p.first.first][p.first.second][p.second]) { if (bst == -1 || cmp(p, to[i][j][k][bst])) bst = nxt[id2].second; } } if (~bst) { can[i][j][k] = true; ans[i][j][k] = bst; return; } } } inline void getans(pair<pair<int, int>, int> a) { pair<int, int> p = a.first; if (p.first == edi && p.second == edj) return; if (p.first != sti || p.second != stj) putchar(g[p.first][p.second]); getans(to[p.first][p.second][a.second][ans[p.first][p.second][a.second]]); } int main() { scanf( %d%d%d , &n, &m, &k); for (register int i = (1); i <= (n); ++i) { scanf( %s , g[i] + 1); for (register int j = (1); j <= (m); ++j) { if (g[i][j] == S ) { tp[i][j] = -1; sti = i, stj = j; } else if (g[i][j] == T ) { tp[i][j] = -1; edi = i, edj = j; } else { tp[i][j] = g[i][j] - a ; } } } id[0] = ++cnt; mask[cnt] = 0; for (register int i = (1); i <= (k - 1); ++i) { for (int comb = (1 << i) - 1, x, y; comb < (1 << 26); x = comb & -comb, y = comb + x, comb = ((comb & ~y) / x >> 1) | y) { id[comb] = ++cnt; mask[cnt] = comb; } } bfs(); if (!dis[edi][edj][1]) return puts( -1 ), 0; dfs(sti, stj, 1); getans(make_pair(make_pair(sti, stj), 1)); puts( ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = (1 << 29); const int maxn = 300005; const int mod = 1000007; vector<pair<int, int> > color[maxn]; int q1[maxn], q2[maxn], ans[maxn], qq; int n, m, num[maxn], top = 0, q[maxn], tot = 0, stk[maxn]; vector<int> edge[maxn]; bool vis[maxn]; long long V[maxn]; int N[maxn], C[maxn]; int F[mod]; void ins(int a, int b) { long long hsh = 1ll * a * (n + 2) + b; int h = hsh % mod; for (int p = F[h]; p > 0; p = N[p]) { if (V[p] == hsh) { return; } } V[++tot] = hsh, C[tot] = 0, N[tot] = F[h], F[h] = tot; } void upd(int a, int b) { long long hsh = 1ll * a * (n + 2) + b; int h = hsh % mod; for (int p = F[h]; p > 0; p = N[p]) { if (V[p] == hsh) { C[p]++; return; } } } int calc(int a, int b) { long long hsh = 1ll * a * (n + 2) + b; int h = hsh % mod; for (int p = F[h]; p > 0; p = N[p]) { if (V[p] == hsh) { return C[p]; } } return 0; } void DFS(int x) { if (vis[x]) { return; } vis[x] = 1; stk[++top] = x; for (int i = 0; i < edge[x].size(); ++i) { DFS(edge[x][i]); } } void solve1(int c) { if (color[c].empty()) { return; } int all = 0; for (int i = 0; i < color[c].size(); ++i) { num[++all] = color[c][i].first; num[++all] = color[c][i].second; } sort(num + 1, num + 1 + all); all = unique(num + 1, num + 1 + all) - num - 1; for (int i = 1; i <= all; ++i) { edge[num[i]].clear(); vis[num[i]] = 0; } for (int i = 0; i < color[c].size(); ++i) { int a = color[c][i].first, b = color[c][i].second; edge[a].push_back(b), edge[b].push_back(a); } for (int i = 1; i <= all; ++i) { if (!vis[num[i]]) { top = 0; DFS(num[i]); for (int j = 1; j <= top; ++j) { for (int k = j + 1; k <= top; ++k) { int a = stk[j], b = stk[k]; if (a > b) { swap(a, b); } upd(a, b); } } } } } int fa[maxn]; int getf(int x) { return x == fa[x] ? x : fa[x] = getf(fa[x]); } void solve2(int c) { for (int i = 1; i <= n; ++i) { fa[i] = i; } for (int i = 0; i < color[c].size(); ++i) { int a = color[c][i].first, b = color[c][i].second; a = getf(a); b = getf(b); if (a != b) { fa[a] = b; } } for (int i = 1; i <= qq; ++i) { if (getf(q1[i]) == getf(q2[i])) { ans[i]++; } } } int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= m; ++i) { color[i].clear(); } for (int i = 1, a, b, c; i <= m; ++i) { scanf( %d%d%d , &a, &b, &c); color[c].push_back(make_pair(a, b)); } tot = 0; memset(F, 0, sizeof(F)); memset(N, 0, sizeof(N)); memset(C, 0, sizeof(C)); memset(V, 0, sizeof(V)); scanf( %d , &qq); for (int i = 1; i <= qq; ++i) { scanf( %d%d , q1 + i, q2 + i); ans[i] = 0; if (q1[i] > q2[i]) { swap(q1[i], q2[i]); } ins(q1[i], q2[i]); } int L = (int)sqrt(m); if (1ll * L * L < m) { L++; } for (int i = 1; i <= m; ++i) { if (color[i].size() <= L) { solve1(i); } else { solve2(i); } } for (int i = 1; i <= qq; ++i) { ans[i] += calc(q1[i], q2[i]); } for (int i = 1; i <= qq; ++i) { if (q1[i] == q2[i]) { ans[i] = m; } } for (int i = 1; i <= qq; ++i) { printf( %d n , ans[i]); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long t, n, k; cin >> t; while (t--) { cin >> k >> n; cout << n + (9 * (k - 1)) << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; map<string, int> name, name2; const int maxn = 1e3 + 10; string s[maxn]; int A[maxn]; int main() { int n; cin >> n; int maxx = -999999999; for (int i = 0; i < n; i++) { cin >> s[i] >> A[i]; name[s[i]] += A[i]; } for (int i = 0; i < n; i++) maxx = max(maxx, name[s[i]]); for (int i = 0; i < n; i++) { name2[s[i]] += A[i]; if (name2[s[i]] >= maxx && name[s[i]] == maxx) { cout << s[i]; return 0; } } return 0; } |
#include <bits/stdc++.h> const double EPS = 1e-9; const int INT_INF = 1 << 31 - 1; const long long I64_INF = 1ll << 63 - 1ll; const double PI = acos(-1.0); using namespace std; struct person { int c, first; person(int _c = 0, int _f = 0) { c = _c; first = _f; } friend bool operator<(const person& a, const person& b) { return a.first < b.first; } }; int n, d, S; vector<person> a; vector<person> b; vector<int> sumb; int maxCNT = 0; int minS = 0; void upd(int a, int b) { if (a > maxCNT || a == maxCNT && b < minS) { maxCNT = a; minS = b; } } pair<int, int> go(int free, int second) { if (free >= (int)b.size()) return make_pair((int)b.size(), 0); if (b[0].first > second) return make_pair(free, 0); int r = (int)b.size() - free; int l = 0; while (l != r - 1) { int m = (l + r) >> 1; if (sumb[m] <= second) l = m; else r = m; } return make_pair(free + l + 1, sumb[l]); } int main() { scanf( %d%d%d , &n, &d, &S); for (int i = 0; i < (int)n; i++) { int c, first, l; scanf( %d%d%d , &c, &first, &l); if (l < d) first = S + 1; if (!c) b.push_back(person(c, first)); else a.push_back(person(c, first)); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); if (!b.empty()) sumb.push_back(b[0].first); for (int i = 1; i < (int)b.size(); i++) if (*sumb.rbegin() > S) sumb.push_back(S + 1); else sumb.push_back(*sumb.rbegin() + b[i].first); pair<int, int> r = go(0, S); upd(r.first, r.second); int k = 0; int sum = 0; int Sum = 0; for (int i = 0; i < (int)a.size(); i++) { Sum += a[i].c; if (Sum > n + n) break; } for (int i = 0; i < (int)a.size(); i++) { k++; sum += a[i].first; if (sum > S) break; int x = Sum - (int)a.size() + k; pair<int, int> r = go(x, S - sum); upd((int)a.size() + r.first, sum + r.second); } printf( %d %d n , maxCNT, minS); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k = 1; scanf( %d%d , &n, &m); while (1) { if (m < k) break; m -= k; k = k % n + 1; } printf( %d n , m); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int main() { int n; scanf( %d , &n); vector<int> a(n), cum(n + 1); for (int i = 0; i < n; i++) { scanf( %d , &a[i]); cum[i + 1] = a[i] + cum[i]; } int bits = 32 - __builtin_clz(n); vector<vector<int>> dp(bits + 1, vector<int>(n)); auto sum = [&](int l, int r) { return (cum[r] - cum[l]) % 10; }; for (int bit = 0; bit < bits; bit++) { int len = 1 << bit; for (int i = 0; i + 2 * len <= n; i++) { dp[bit + 1][i] = dp[bit][i] + dp[bit][i + len] + (sum(i, i + len) + sum(i + len, i + 2 * len)) / 10; } } int q; scanf( %d , &q); for (int qq = 0; qq < q; qq++) { int l, r; scanf( %d %d , &l, &r); --l; int bit = __builtin_ctz(r - l); printf( %d n , dp[bit][l]); } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e5 + 5, MOD = 1e9 + 7; long long n; long long par[MAXN]; long long cnt1[MAXN]; long long cnt2[MAXN]; long long dp1[MAXN]; long long dp2[MAXN]; long long color[MAXN]; bool subtree[MAXN]; vector<long long> G[MAXN]; void dfs_up1(long long v = 0) { for (long long i = 0; i < G[v].size(); i++) { dfs_up1(G[v][i]); subtree[v] |= subtree[G[v][i]]; } subtree[v] |= color[v]; } void dfs_up2(long long v = 0) { for (long long i = 0; i < G[v].size(); i++) dfs_up2(G[v][i]); if (!subtree[v]) { dp1[v] = 0; dp2[v] = 1; return; } if (color[v]) { dp1[v] = 1; for (long long i = 0; i < G[v].size(); i++) if (subtree[G[v][i]]) { if (color[G[v][i]]) dp1[v] = (dp1[v] * dp1[G[v][i]]) % MOD; if (!color[G[v][i]]) dp1[v] = (dp1[v] * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD; } dp2[v] = 0; return; } long long bu = 1, num = 0; for (long long i = 0; i < G[v].size(); i++) if (subtree[G[v][i]]) { if (color[G[v][i]]) { num++; bu = (bu * dp1[G[v][i]]) % MOD; } if (!color[G[v][i]]) bu = (bu * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD; } dp1[v] = num * bu % MOD; bu = 1; num = 0; for (long long i = 0; i < G[v].size(); i++) if (subtree[G[v][i]]) { if (color[G[v][i]]) bu = (bu * dp1[G[v][i]]) % MOD; if (!color[G[v][i]]) { cnt1[num] = ((num ? cnt1[num - 1] : 1) * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD; num++; } } long long ind = num - 1; for (long long i = G[v].size() - 1; ~i; i--) if (subtree[G[v][i]]) { if (!color[G[v][i]]) { cnt2[ind] = ((ind != num - 1 ? cnt2[ind + 1] : 1) * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD; ind--; } } ind = 0; for (long long i = 0; i < G[v].size(); i++) if (subtree[G[v][i]]) { if (!color[G[v][i]]) { dp1[v] = (dp1[v] + bu * (ind ? cnt1[ind - 1] : 1) % MOD * (ind + 1 != num ? cnt2[ind + 1] : 1) % MOD * dp1[G[v][i]]) % MOD; ind++; } } dp2[v] = 1; for (long long i = 0; i < G[v].size(); i++) if (subtree[G[v][i]]) { if (color[G[v][i]]) dp2[v] = (dp2[v] * dp1[G[v][i]]) % MOD; if (!color[G[v][i]]) dp2[v] = (dp2[v] * (dp1[G[v][i]] + dp2[G[v][i]])) % MOD; } } int main() { scanf( %lld , &n); for (long long i = 1; i < n; i++) { scanf( %lld , &par[i]); G[par[i]].push_back(i); } for (long long i = 0; i < n; i++) scanf( %lld , &color[i]); dfs_up1(); dfs_up2(); printf( %lld n , dp1[0]); return 0; } |
#include <bits/stdc++.h> using namespace std; long long s, x, a, ans = 1; void setup() { cin >> s >> x; a = s - x; if (a & 1) { cout << 0; exit(0); } a >>= 1; } void xuly() { long long k = a; while (x) { if (x & 1) { if (a & 1) { cout << 0; return; } ans <<= 1; } x >>= 1; a >>= 1; } if (k == 0) ans -= 2; cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); setup(); xuly(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string a, b; cin >> a >> b; int n = a.size(); int f = 0, s = 0; for (int i = 0; i < n; ++i) if (a[i] != b[i]) { if (a[i] == 4 ) f++; else s++; } int ans = min(f, s); ans += (f - ans) + (s - ans); cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1000000 + 5; int n, par[N], mx1[N], mx2[N], ans[N]; int main() { scanf( %d , &n); par[1] = mx1[1] = mx2[1] = ans[1] = 0; for (int i = 2; i <= n + 1; i++) { scanf( %d , &par[i]); int v = i, val; ans[i] = mx1[i] = mx2[i] = 0; while (v > 0) { val = max(mx1[v], mx2[v] + 1); if (val <= ans[v]) break; ans[v] = val; if (mx1[par[v]] < val) mx1[par[v]] = val; else if (mx2[par[v]] < val) mx2[par[v]] = val; v = par[v]; } printf( %d , max(mx1[1], mx2[1])); } return 0; } |
#include <bits/stdc++.h> using namespace std; int N, a, b, Q; long long W[(500000)], S[(500000)], O[(500000)]; map<pair<int, int>, vector<pair<int, int> > > M; void pro(int t, int b, long long* g, int& l) { *g = 0, l = 1; while (b < N) { long long S = W[b] + g[l - 1]; g[l++] = S, b += t; } } int main() { scanf( %d , &N); for (int i(0); i < N; i++) scanf( %lld , W + i); scanf( %d , &Q); for (int i(0); i < Q; i++) scanf( %d%d , &a, &b), --a, M[{b, a % b}].push_back({a, i}); for (auto& h : M) { pro(h.first.first, h.first.second, S, a); for (auto& w : h.second) O[w.second] = S[a - 1] - S[w.first / h.first.first]; } for (int i(0); i < Q; i++) printf( %lld n , O[i]); } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 256; const char nxt = n ; inline void in(string Q) { Q += .in ; freopen(Q.c_str(), r , stdin); } inline void out(string Q) { Q += .out ; freopen(Q.c_str(), w , stdout); } inline void open_file(string Q) { in(Q); out(Q); } inline bool Pal(const string& s) { return equal(s.begin(), s.end(), s.rbegin()); } vector<string> taxi, pizza, frienD; string change(string& X) { string ret = ; for (auto to : X) if (to != - ) ret.push_back(to); return ret; } int check(string x) { x = change(x); if (int(x.size()) != 6) return 2; string cur = ; cur += x[0]; for (auto to : x) { if (to != cur.back()) cur.push_back(to); } if (int(cur.size()) == 1) return 1; cur = x; sort(cur.begin(), cur.end()); reverse(cur.begin(), cur.end()); char last = . ; bool ok = 1; for (auto to : cur) { if (to == last) { ok = 0; break; } last = to; } if (cur == x && ok) return 0; return 2; } map<string, int> M[10], tax, piz, fr; vector<string> nm; int main() { ios_base::sync_with_stdio(0); cin.tie(0); if (0) open_file( ); int n; cin >> n; while (n--) { int kol; string name, phone; cin >> kol >> name; nm.push_back(name); M[0][name] = M[1][name] = M[2][name] = 0; while (kol--) { cin >> phone; int D = check(phone); M[D][name]++; } } for (int i = 0; i < 3; ++i) { int mx = 0; for (auto to : M[i]) { mx = max(mx, to.second); } if (!mx) { for (auto it : nm) { if (!tax[it] && i == 1) { tax[it] = 1; taxi.push_back(it); } if (!piz[it] && !i) { pizza.push_back(it); piz[it] = 1; } if (!fr[it] && i == 2) { fr[it] = 1; frienD.push_back(it); } } continue; } if (i == 1) { for (auto it : nm) { for (auto to : M[i]) { if (to.second == mx && it == to.first && !tax[it]) { taxi.push_back(it); tax[it] = 1; } } } } if (i == 0) { for (auto it : nm) { for (auto to : M[i]) { if (to.second == mx && it == to.first && !piz[it]) { pizza.push_back(it); piz[it] = 1; } } } } if (i == 2) { for (auto it : nm) { for (auto to : M[i]) { if (to.second == mx && it == to.first && !fr[it]) { frienD.push_back(it); fr[it] = 1; } } } } } cout << If you want to call a taxi, you should call: ; string ans = ; for (auto to : taxi) ans += + to + , ; ans.pop_back(); ans += . ; cout << ans; ans = ; cout << nxt << If you want to order a pizza, you should call: ; for (auto to : pizza) ans += + to + , ; ans.pop_back(); ans += . ; cout << ans; ans = ; cout << nxt << If you want to go to a cafe with a wonderful girl, you should call: ; for (auto to : frienD) ans += + to + , ; ans.pop_back(); ans += . ; cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; struct rec { int w, h; }; vector<rec> v; int main() { int n; scanf( %d , &n); for (int i = 0; i < n; i++) { int w, h; scanf( %d %d , &w, &h); v.push_back({w, h}); } int last = -0x3f3f3f3f; for (int i = n; i > 0; i--) { int prev = max(v[i - 1].w, v[i - 1].h); int mini = min(v[i].h, v[i].w); int maxi = max(v[i].h, v[i].w); if (mini >= last && mini <= prev) { last = mini; } else if (maxi >= last && maxi <= prev) { last = maxi; } else { printf( NO n ); return 0; } } printf( YES n ); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, a[100001], v[100001], posa[100001], posv[100001], x, y; int inx[100001]; inline void kspxa(int s, int t) { if (s < t) { int i = s, j = t, k = (i + j) >> 1; int l = a[i]; a[i] = a[k]; a[k] = l; l = posa[i]; posa[i] = posa[k]; posa[k] = l; k = a[i]; l = posa[i]; while (i != j) { while (i < j && a[j] > k) --j; if (i < j) a[i] = a[j], posa[i++] = posa[j]; while (i < j && a[i] < k) ++i; if (i < j) a[j] = a[i], posa[j--] = posa[i]; } a[i] = k; posa[i] = l; kspxa(s, i - 1); kspxa(i + 1, t); } } inline void kspxv(int s, int t) { if (s < t) { int i = s, j = t, k = (i + j) >> 1; int l = v[i]; v[i] = v[k]; v[k] = l; l = posv[i]; posv[i] = posv[k]; posv[k] = l; k = v[i]; l = posv[i]; while (i != j) { while (i < j && v[j] > k) --j; if (i < j) v[i] = v[j], posv[i++] = posv[j]; while (i < j && v[i] < k) ++i; if (i < j) v[j] = v[i], posv[j--] = posv[i]; } v[i] = k; posv[i] = l; kspxv(s, i - 1); kspxv(i + 1, t); } } int main() { scanf( %d%d%d%d , &n, &m, &x, &y); for (int i = 1; i <= n; i++) scanf( %d , &a[i]), posa[i] = i; for (int i = 1; i <= m; i++) scanf( %d , &v[i]), posv[i] = i; kspxa(1, n); kspxv(1, m); int left = 1, ans = 0; memset(inx, 0, sizeof(inx)); for (int i = 1; i <= n; i++) { if (left > m) break; while (left <= m && v[left] < a[i] - x) ++left; if (left > m) break; if (v[left] >= a[i] - x && v[left] <= a[i] + y) inx[posa[i]] = posv[left], ++left, ++ans; } printf( %d n , ans); for (int i = 1; i <= n; i++) if (inx[i]) printf( %d %d n , i, inx[i]); } |
#include <bits/stdc++.h> using namespace std; template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a) { return out << ( << a.first << , << a.second << ) ; } template <class A> ostream &operator<<(ostream &out, const vector<A> &a) { for (const A &it : a) out << it << ; return out; } template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) { return in >> a.first >> a.second; } template <class A> istream &operator>>(istream &in, vector<A> &a) { for (A &i : a) in >> i; return in; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, x, y; cin >> n >> x >> y; string s; cin >> s; reverse((s).begin(), (s).end()); long long cnt = 0; for (long long i = 0; i < x; i++) { if ((i == x || i == y)) { cnt += s[i] == 0 ; } else cnt += s[i] == 1 ; } cout << cnt; } |
#include <bits/stdc++.h> using namespace std; long long a[2005]; vector<long long> Z; vector<long long> F; int main() { long long n, i, S = 0; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; S += a[i]; } sort(a, a + n); reverse(a, a + n); for (i = 0; i < n; i++) { if (a[i] % 2 == 0) Z.push_back(a[i]); else F.push_back(a[i]); } i = 0; long long j = 0, k = 1, sum1 = 0, sum2 = 0; while (1) { if (k == 0) { if (i >= Z.size()) break; sum1 += Z[i]; i++; k = 1; } else { if (j >= F.size()) break; sum1 += F[j]; j++; k = 0; } } i = 0, j = 0, k = 0; while (1) { if (k == 0) { if (i >= Z.size()) break; sum2 += Z[i]; i++; k = 1; } else { if (j >= F.size()) break; sum2 += F[j]; j++; k = 0; } } long long maxi = max(sum1, sum2); cout << S - maxi; return 0; } |
#include <bits/stdc++.h> long long t[105], s[105]; long long a[105]; int getNumber(long long add) { if (add == 0) return 1; else { int res = -1; for (int i = 2; i <= 101; i++) { long long last = i * (i - 1) * (i - 2) / 6, next = i * (i - 1) * (i + 1) / 6; if ((next - last) == add) { res = i; break; } } return res; } } int main() { int n; scanf( %d%lld%lld , &n, &t[0], &s[0]); for (int i = 1; i <= n; i++) { if (i <= n - 3) printf( + %d n , n - i); else { if (i == n || i == n - 2) printf( + 1 n ); else printf( + 2 n ); } fflush(stdout); scanf( %lld%lld , &t[i], &s[i]); } long long add = t[n] - t[n - 1]; a[1] = getNumber(add) - 1; long long add0 = s[n] - s[n - 1], add1 = s[n - 2] - s[n - 3]; a[3] = add0 - add1 - 1; a[2] = add1 / (a[3] + 1); add = s[n - 1] - s[n - 2]; a[4] = (add - (a[3] + 1) * (a[1] + 1)) / (a[3] + 1) - 1; for (int i = 5; i <= n; i++) { long long add = s[n - i + 2] - s[n - i + 1]; a[i] = (add - a[i - 3] * a[i - 4] - a[i - 3] * (a[i - 1] + 1)) / (a[i - 1] + 1) - 1; } a[n]++; printf( ! ); for (int i = 1; i <= n; i++) printf( %lld , a[i]); printf( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; long long n; long long ans[100100 << 1]; long long ksm(long long p, long long q) { long long res = 1; while (q) { if (q & 1) res = res * p % 1000000007; p = p * p % 1000000007; q >>= 1; } return res; } signed main() { cin >> n; ans[0] = 1; for (long long i = 1; i <= (n << 1); i++) { ans[i] = ans[i - 1] * i % 1000000007; } cout << (ans[n << 1] * ksm(ans[n] * ans[n] % 1000000007, 1000000007 - 2)) % 1000000007 - n; return 0; } |
#include <bits/stdc++.h> using namespace std; long long n, a, b, t, p, i; int main() { cin >> n >> a >> b; p = b * n; while (n > 1) { t += 2 * a + 1; n--; } cout << t << << p; return 0; } |
#include <bits/stdc++.h> using namespace std; string s1, s2; bool eq(int a1, int b1, int a2, int b2) { if ((b1 - a1) & 1) { while (a1 < b1) { if (s1[a1] != s2[a2]) return 0; ++a1; ++a2; } return 1; } int m1 = (a1 + b1) / 2; int m2 = (a2 + b2) / 2; return (eq(a1, m1, a2, m2) && eq(m1, b1, m2, b2)) || (eq(a1, m1, m2, b2) && eq(m1, b1, a2, m2)); } bool func() { if (s1.length() != s2.length()) return 0; if (eq(0, s1.length(), 0, s2.length())) { return 1; } } int main() { cin >> s1 >> s2; if (func()) cout << YES ; else cout << NO ; return 0; } |
#include <bits/stdc++.h> using namespace std; void solve() { int n, m, k; cin >> n >> m >> k; vector<int> dp1(m, 0); vector<int> pref_max(m, 0); vector<int> suf_max(m, 0); vector<vector<int> > a(n); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int x; cin >> x; a[i].push_back(x); } } for (int i = 0; i < n; i++) { vector<vector<int> > sa1(m); vector<vector<int> > sa2(m); for (int j = 0; j < m; j++) { sa1[j].push_back(0); sa2[j].push_back(0); for (int ii = 0; ii < k; ii++) { if (j + ii < m) { sa1[j].push_back(sa1[j][sa1[j].size() - 1] + a[i][j + ii]); if (i < n - 1) { sa2[j].push_back(sa2[j][sa2[j].size() - 1] + a[i + 1][j + ii]); } else { sa2[j].push_back(0); } } else { sa1[j].push_back(sa1[j][sa1[j].size() - 1]); sa2[j].push_back(sa2[j][sa2[j].size() - 1]); } } } vector<int> dp2(m, -1e9); for (int j = 0; j < m; j++) { if (j - k >= 0) { dp2[j] = pref_max[j - k]; } if (j + k < m) { dp2[j] = max(dp2[j], suf_max[j + k]); } for (int ii = max(0, j - k + 1); ii < min(m, j + k); ii++) { if (ii < j) { dp2[j] = max(dp2[j], dp1[ii] - sa1[j][k - (j - ii)]); } else { dp2[j] = max(dp2[j], dp1[ii] - sa1[ii][k - (ii - j)]); } } if (i == 0) { dp2[j] = sa1[j][k] + sa2[j][k]; } else { dp2[j] += sa1[j][k] + sa2[j][k]; } } dp1 = dp2; pref_max[0] = dp1[0]; for (int j = 1; j < m; j++) { pref_max[j] = max(pref_max[j - 1], dp1[j]); } suf_max[m - 1] = dp1[m - 1]; for (int j = m - 2; j >= 0; j--) { suf_max[j] = max(suf_max[j + 1], dp1[j]); } } cout << pref_max[m - 1]; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); solve(); cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; s = s + x ; int dol = 0; int cent = 0; int td = 0; int tc = 0; for (int i = 0; i < s.length(); ++i) { if (s[i] >= a && s[i] <= z ) { dol += td; cent += tc; td = 0; tc = 0; continue; } if (s[i] >= 0 && s[i] <= 9 ) td = td * 10 + (int)(s[i] - 0 ); if (s[i] == . ) { if (s[i + 3] >= 0 && s[i + 3] <= 9 ) continue; else for (int j = 0; j < 2; ++j) { ++i; tc = tc * 10 + (int)(s[i] - 0 ); } } } dol = dol + cent / 100; cent = cent % 100; int storecent = cent; int dig = 0; string sd; string sc; if (dol == 0 && cent > 0) { sd = . + sd; sd = 0 + sd; } while (dol > 0) { if (dig == 0) sd = . + sd; sd = (char)((dol % 10) + 0 ) + sd; dol /= 10; dig = (dig + 1) % 3; } while (cent > 0) { sc = (char)((cent % 10) + 0 ) + sc; cent /= 10; } if (sc.length() == 1) sc = 0 + sc; for (int i = 0; i < sd.length() - 1; ++i) cout << sd[i]; if (storecent != 0) { cout << . ; cout << sc[0] << sc[1]; } cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; bool v[N]; int n, m, x, y, k, q[N], t, d[N], A[N], c; vector<int> a[N]; void D(int x) { q[++t] = x, d[x] = t; for (int i = 0; i < a[x].size(); i++) if (!d[a[x][i]]) D(a[x][i]); else if (d[x] - d[a[x][i]] + 1 >= k) { cout << 2 << n << d[x] - d[a[x][i]] + 1 << n ; for (int j = d[a[x][i]]; j <= d[x]; j++) cout << q[j] << ; exit(0); } if (!v[x]) { A[++c] = x; for (int i = 0; i < a[x].size(); i++) v[a[x][i]] = true; } t--; } int main() { cin >> n >> m; while (m-- && cin >> x >> y) a[x].push_back(y), a[y].push_back(x); k = sqrt(n - 1) + 1, D(1); cout << 1 << n ; for (int i = 1; i <= k; i++) cout << A[i] << ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long n, t, cur = 1, pre, s; cin >> n >> pre; s = n; n--; for (int i = 0; i < (int)(n); ++i) { cin >> t; if (t == pre) cur++; else if (t != pre && cur > 1) { s += (cur * (cur - 1) / 2); cur = 1; } pre = t; } if (cur > 1) s += (cur * (cur - 1) / 2); cout << s; } |
#include <bits/stdc++.h> using namespace std; int main() { int k; cin >> k; string s; cin >> s; int n = s.size(); int ps[n + 5]; ps[0] = s[0] - 0 ; for (int i = 1; i < n; i++) ps[i] = ps[i - 1] + s[i] - 0 ; long long ans = 0; for (int i = 0; i < n; i++) { int it1 = lower_bound(ps + i, ps + n, (i ? ps[i - 1] : 0) + k) - ps; if (it1 == n) continue; int it2 = lower_bound(ps + i, ps + n, (i ? ps[i - 1] : 0) + k + 1) - 1 - ps; ans += it2 - it1 + 1; } cout << ans << n ; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long int x; long long int k; long long int res; long long int curr; long long int exponentiation(long long int base, long long int power) { long long int ans = 1; while (power > 0) { if (power % 2) ans = (ans * base) % mod; base = (base * base) % mod; power /= 2; } return ans % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> x >> k; if (x == 0) { res = 0; } else if (k == 0) { res = (2 * x) % mod; } else { curr = (2 * x - 1 - x + mod) % mod; res = (x + ((((curr + x) % mod) * exponentiation(2, k)) % mod - curr) + mod) % mod; } cout << res << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; signed main() { long long N; cin >> N; long long frend[N + 1][N + 1]; long long i, j, k; for (i = 1; i <= N; i++) { for (j = 1; j <= N; j++) { cin >> frend[i][j]; } } long long alex[N + 1]; long long liking[N + 1]; long long c = N + 9; for (i = 1; i <= N; i++) { cin >> alex[i]; liking[alex[i]] = c--; } for (i = 1; i <= N; i++) { for (j = 1; j <= N; j++) { if (frend[i][j] == i) continue; long long mx = 0; for (k = 1; k <= frend[i][j]; k++) { if (k == i) continue; mx = max(mx, liking[k]); } if (mx == liking[frend[i][j]]) { cout << frend[i][j] << ; break; } } } cout << n ; } |
#include <bits/stdc++.h> using namespace std; int count_inversions(vector<int> v) { int t = 0; for (int i = 0; i < (int)v.size(); ++i) for (int j = i + 1; j < (int)v.size(); ++j) if (v[i] > v[j]) t++; return t; } int main() { int n; scanf( %d , &n); vector<int> v(n); for (int i = 0; i < n; ++i) scanf( %d , &v[i]); int inversions = count_inversions(v); cout << (inversions / 2) * 4 + (inversions % 2) << endl; } |
#include <bits/stdc++.h> long long mod; long long solve(int dep, long long idx, long long left, long long right, long long l, long long r, long long u, long long v) { if (right < l) return 0; if (left > r) return 0; if (left >= l && right <= r) { long long delta = (1LL << dep); long long begin = idx, end = idx + (right - left) * delta; if (end < u) return 0; if (begin > v) return 0; if (begin < u) begin = begin + (u - begin) / delta * delta; if (begin < u) begin += delta; if (end > v) end = end - (end - v) / delta * delta; if (end > v) end -= delta; if (begin > end) return 0; long long total = (end - begin) / delta + 1; return ((total % mod) * ((begin + end) % mod)) % mod; } long long delta = (1LL << dep); long long mid = (left + right) / 2; return (solve(dep + 1, idx, left, mid, l, r, u, v) + solve(dep + 1, idx + delta, mid + 1, right, l, r, u, v)) % mod; } int main() { long long n; int m; while (scanf( %I64d%d%I64d , &n, &m, &mod) == 3) { mod *= 2; for (int i = 0; i < m; i++) { long long l, r, u, v; scanf( %I64d%I64d%I64d%I64d , &l, &r, &u, &v); printf( %I64d n , solve(0, 1, 1, n, l, r, u, v) / 2); } } return 0; } |
#include <bits/stdc++.h> using namespace std; struct _ { ios_base::Init i; _() { cin.sync_with_stdio(0); cin.tie(0); } } _; const int MAXN = -1; int n, m; int main() { int a, b; cin >> a >> b; int n = a + b + 1; for (int i = n - a; i <= n; ++i) cout << i << ; for (int i = n - a - 1; i >= 1; --i) cout << i << ; cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, s = 0; scanf( %I64d%I64d , &a, &b); while (b != 0) { s = s + a / b; c = b; b = a % b; a = c; } printf( %I64d n , s); return 0; } |
#include<bits/stdc++.h> #define X first #define Y second #define sz(a) (int)a.size() #define ll long long //#define int long long using namespace std; const int mod = 1e9 + 7; const int N = 3e5; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; string s; cin >> s; vector <int> vec; for(int i = 0; i < n; i++) { if(s[i] == ? ) { vec.push_back(-1); } else { vec.push_back(s[i] - a ); } } vector <vector <int> > right(k, vector <int> (n, 0)); for(int i = 0; i < k; i++) { for(int j = n - 1; j >= 0; j--) { if(vec[j] == i || vec[j] == -1) { if(j == n - 1) { right[i][j] = 1; } else { right[i][j] = right[i][j + 1] + 1; } } // cout << right[i][j] << ; } // cout << n ; } int l = 0, r = n + 1; while(r - l > 1) { int midd = (r + l) / 2; vector <vector <int> > mass(26); for(int i = 0; i < k; i++) { for(int j = 0; j < n; j++) { if(right[i][j] >= midd) { mass[i].push_back(j); } } } vector <int> dp(1 << k, 1e9); dp[0] = -1; vector <int> iter(k, 0); vector <vector <int> > mass1(n + 1); mass1[0].push_back(0); for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { while(iter[j] < sz(mass[j]) && mass[j][iter[j]] < i) { iter[j]++; } } for(auto p : mass1[i]) { if(dp[p] != i - 1) { continue; } for(int j = 0; j < k; j++) { if((1 << j) & p) { continue; } if(iter[j] < sz(mass[j])) { int u = mass[j][iter[j]]; if(dp[(1 << j) | p] > u + midd - 1) { dp[(1 << j) | p] = u + midd - 1; mass1[u + midd].push_back((1 << j) | p); } } } } } if(dp[(1 << k) - 1] < n) { l = midd; } else { r = midd; } } cout << l; return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; int n, k, tot, root; int ak[30]; int son[20000005][2], g[20000005]; long long ans; void calc(int v, int *a) { memset(a, 0, 120); int i = 0; while (v) { a[i++] = v & 1; v >>= 1; } return; } int make_node() { ++tot; return tot; } int get_ans(int x, int d) { if (!son[x][d]) return 0; return g[son[x][d]]; } int ax[30]; void update(int x) { int nd = root; g[root]++; for (int i = 30 - 1; i >= 0; i--) { if (!son[nd][ax[i]]) son[nd][ax[i]] = make_node(); nd = son[nd][ax[i]]; g[nd]++; } return; } void query(int x) { int nd = root; for (int i = 30 - 1; i >= 0; i--) { if (!ak[i]) { ans += get_ans(nd, ax[i] ^ 1); nd = son[nd][ax[i]]; } else nd = son[nd][ax[i] ^ 1]; if (!nd) return; } ans += g[nd]; return; } int main() { int x, y = 0; scanf( %d %d , &n, &k); root = make_node(); update(0); calc(k, ak); for (int i = 1; i <= n; i++) { scanf( %d , &x); y ^= x; calc(y, ax); query(y); update(y); } printf( %I64d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; vector<int> T; int l, size; void Stworz(int n) { int p = 1; while (p < n) p *= 2; l = p; size = 2 * p - 1; for (int i = 0; i <= size; ++i) T.push_back(INF); } void Ustaw(int a, int x) { a += l; T[a] = x; a /= 2; while (a != 0) { T[a] = min(T[2 * a], T[2 * a + 1]); a /= 2; } } int Mini(int a, int b) { a += l; b += l; int m = min(T[a], T[b]); while (a / 2 != b / 2) { if (a % 2 == 0) m = min(m, T[a + 1]); if (b % 2 == 1) m = min(m, T[b - 1]); a /= 2; b /= 2; } return m; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> a(n); map<int, int> wyst, pier; for (int i = 0; i < n; ++i) { cin >> a[i]; wyst[a[i]] = 0; pier[a[i]] = INF; } Stworz(n); vector<int> odp; for (int L = 0; L < n;) { int P = L; while (P < n) { ++wyst[a[P]]; if (pier[a[P]] == INF) pier[a[P]] = P; Ustaw(P, pier[a[P]]); if (pier[a[P]] + 1 <= P - 1) { int ind = Mini(pier[a[P]] + 1, P - 1); if (ind < pier[a[P]]) { odp.push_back(a[ind]); odp.push_back(a[P]); odp.push_back(a[ind]); odp.push_back(a[P]); break; } } if (wyst[a[P]] >= 4) { for (int i = 0; i < 4; ++i) odp.push_back(a[P]); break; } ++P; } for (int i = L; i <= min(P, n); ++i) { --wyst[a[i]]; pier[a[i]] = INF; } L = P + 1; } cout << odp.size() << endl; for (int i = 0; i < odp.size(); ++i) cout << odp[i] << ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, y, x, h, n; cin >> a >> b >> c >> d; y = (a * 3) / 10, x = (a - ((a / 250) * c)), h = (b * 3) / 10, n = (b - ((b / 250) * d)); if (max(y, x) > max(n, h)) cout << Misha ; else if (max(y, x) < max(n, h)) cout << Vasya ; else cout << Tie ; return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; using Pl = pair<ll, int>; const int mod = 1e9 + 7, INF = 1 << 30; const double EPS = 1e-12, PI = 3.1415926535897932384626; const ll LINF = 1LL << 60, lmod = 1e9 + 7; const int MAX_N = 102; vector<P> graph[MAX_N]; int memo[2][MAX_N][MAX_N][30]; int solve(bool is_first, int a, int b, int score) { if (memo[int(is_first)][a][b][score] != -1) return memo[int(is_first)][a][b][score]; if (is_first) { bool win = false; for (auto p : graph[a]) { int u, c; tie(u, c) = p; if (c >= score) { win |= (solve(false, u, b, c) == 1); } } if (!win) return memo[int(is_first)][a][b][score] = 0; else return memo[int(is_first)][a][b][score] = 1; } else { bool win = true; for (auto p : graph[b]) { int u, c; tie(u, c) = p; if (c >= score) { win &= (solve(true, a, u, c) == 1); } } if (win) return memo[int(is_first)][a][b][score] = 1; else return memo[int(is_first)][a][b][score] = 0; } } int main() { int N, M; cin >> N >> M; for (int i = 0; i < M; i++) { int a, b; char c; cin >> a >> b >> c; a--; b--; graph[a].push_back(P(b, int(c - a ))); } for (int i = 0; i < 2; i++) for (int j = 0; j < MAX_N; j++) for (int k = 0; k < MAX_N; k++) for (int l = 0; l < 30; l++) memo[i][j][k][l] = -1; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) cout << (solve(true, i, j, 0) == 1 ? A : B ); cout << << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; vector<pair<int, int>> v; for (int i = 0; i < n; i++) { long long a, b; cin >> a >> b; v.push_back(make_pair(a, b)); } int c = 0; for (int i = 0; i < n; i++) { int up = 0, lo = 0, r = 0, l = 0; for (int j = 0; j < n; j++) { if (v[i].first > v[j].first && v[i].second == v[j].second) up = 1; if (v[i].first < v[j].first && v[i].second == v[j].second) lo = 1; if (v[i].first == v[j].first && v[i].second < v[j].second) l = 1; if (v[i].first == v[j].first && v[i].second > v[j].second) r = 1; } if (up + lo + l + r == 4) c++; } cout << c; } |
#include <bits/stdc++.h> using namespace std; const short int N = 1000; short int n, m, ax[] = {0, 0, 1, -1}, ay[] = {1, -1, 0, 0}, man_dis[N][N]; bool vis[N][N], v[N][N], g[N][N]; void good() { for (short int i = 0; i < n; i++) { for (short int j = 0; j < m; j++) { bool f = 0; if (i and v[i][j] == v[i - 1][j]) f = 1; if (j and v[i][j] == v[i][j - 1]) f = 1; if (i < n - 1 and v[i][j] == v[i + 1][j]) f = 1; if (j < m - 1 and v[i][j] == v[i][j + 1]) f = 1; g[i][j] = f; } } } void man_dist() { queue<pair<short int, short int>> q; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (g[i][j]) man_dis[i][j] = 0, q.push({i, j}), vis[i][j] = true; while (q.size()) { short int y = q.front().first, x = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { int nx = x + ax[i], ny = y + ay[i]; if (nx < 0 || ny < 0 || nx >= m || ny >= n || vis[ny][nx]) continue; man_dis[ny][nx] = man_dis[y][x] + 1; vis[ny][nx] = true; q.push({ny, nx}); } } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tt, ii = 0; short int k = 0; cin >> n >> m >> tt; for (short int i = 0; i < n; i++) for (short int j = 0; j < m; j++) { char x; cin >> x; v[i][j] = (x == 1 ); } good(); man_dist(); while (ii++ < tt) { short int i, j; long long p; cin >> i >> j >> p; i--, j--; if (n + m == 2) { cout << v[i][j] << endl; continue; } short int f = man_dis[i][j]; if (p <= f || !vis[i][j]) cout << v[i][j] << endl; else cout << (p - f + v[i][j]) % 2 << endl; } cerr << [Execution : << (1.0 * clock()) / CLOCKS_PER_SEC << s] n ; return 0; } |
#include <bits/stdc++.h> using namespace std; bool oppSign(double a, double b) { if (a == 0 || b == 0) return false; if (a > 0 && b < 0) return true; if (a < 0 && b > 0) return true; return false; } void parr(vector<double>& a) { for (int i = 0; i < a.size(); ++i) { cout << a[i] << ; } cout << endl; } int main() { int n; cin >> n; double a, b; cin >> a >> b; vector<pair<double, double> > v; for (int i = 0; i < (n); ++i) { double x, y; scanf( %lf%lf , &x, &y); double y1, y2; y1 = x * a + y; y2 = x * b + y; v.push_back(make_pair((y1), (y2))); } sort(v.begin(), v.end()); vector<double> ar, br; for (int i = 0; i < v.size(); ++i) { double x = v[i].first; double y = v[i].second; ar.push_back(x); br.push_back(y); } vector<double> bkr(br.size()); bkr[br.size() - 1] = br[br.size() - 1]; for (int i = br.size() - 2; i >= 0; --i) { if (br[i] > bkr[i + 1]) bkr[i] = bkr[i + 1]; else bkr[i] = br[i]; } bool flag = false; for (int i = 0; i < ar.size() - 1; ++i) { if (ar[i] != ar[i + 1] && br[i] != bkr[i + 1] && oppSign((ar[i] - ar[i + 1]), (br[i] - bkr[i + 1]))) { flag = true; break; } } if (flag == false) cout << NO n ; else cout << YES n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int n; long long ans; int a[150000 + 1]; int main() { scanf( %d , &n); for (register int i = 1; i <= n; ++i) { scanf( %d , &a[i]); ans += 1LL * a[i] * (n - a[i] + 1); } for (register int i = 1; i <= n; ++i) ans -= 1LL * min(a[i], a[i + 1]) * (n - max(a[i], a[i + 1]) + 1); printf( %lld , ans); } |
#include <bits/stdc++.h> using namespace std; const int inf = ~0u >> 1, N = 1000005; int n, r, x, ans, fr; char o[N]; void output(int x, int y) { for (int i = n; i; --i) if (x >= y) o[i - 1] = T , x -= y; else o[i - 1] = B , y -= x; } int evaluate(int x, int y) { int sta = 0, stb = 0, t; while (x != 0 && y != 0 && sta <= n) { if (x > y) { if (x % y != 0) { t = x / y; sta += t; stb += t - 1; x %= y; } else { t = x / y - 1; sta += t; stb += t - 1; x = y; ++sta; y = 0; } } else { if (y % x != 0) { t = y / x; sta += t; stb += t - 1; y %= x; } else { t = y / x - 1; sta += t; stb += t - 1; y = x; ++sta; x = 0; } } } if (x == 0 && y == 1 && sta == n) return stb; else return -1; } void nrun() { ans = n; for (int i = 1; i < r; ++i) { x = evaluate(i, r); if (x != -1 && x <= ans) ans = x, fr = i; x = evaluate(r, i); if (x != -1 && x <= ans) ans = x, fr = -i; } if (ans == n) printf( IMPOSSIBLE n ); else { printf( %d n , ans); if (fr > 0) output(fr, r); else output(r, -fr); puts(o); } } int main() { scanf( %d%d , &n, &r); if (r == 1) { printf(n == 1 ? 0 nT n : IMPOSSIBLE n ); } else nrun(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e6; int n, k, a, b; vector<int> v; int ret(int lt, int rt) { int id2 = upper_bound(v.begin(), v.end(), rt) - v.begin(); int id1 = lower_bound(v.begin(), v.end(), lt) - v.begin(); return id2 - id1; } long long dfs(int lt, int rt) { int nm = ret(lt, rt); if (!nm) return a; long long on = b * 1LL; on *= nm; on *= (rt - lt + 1); if (lt == rt) return on; int md = (lt + rt) / 2; long long tw = dfs(lt, md) + dfs(md + 1, rt); return min(on, tw); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> a >> b; for (int i = 1; i <= k; i++) { int x; cin >> x; v.push_back(x); } sort(v.begin(), v.end()); cout << dfs(1, 1 << n) << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n; cin >> n; int a[2 * n], ans = 0; for (int i = 0; i <= 2 * n - 1; i++) cin >> a[i]; for (int i = 0; i < 2 * n; i += 2) { int j = i + 1; while (a[j] != a[i]) j++; ans += j - i - 1; for (int k = j; k >= i + 2; k--) a[k] = a[k - 1]; } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int inf = 1e9 + 9; const long long MOD = 1e9 + 696969; const long long INF = 1e18; int n, m, k, a, b, c, w, h; char s[1000100]; int dp[1000100][26], last[1000100][26]; pair<int, int> SECOND, MIN; inline pair<int, int> minexc(int x) { if (x != MIN.second) return MIN; return SECOND; } char ret[1000100]; int main() { scanf( %d%d , &n, &k) ?: 0; scanf( %s , s + 1) ?: 0; for (int i = 1; i <= n; ++i) for (int j = 0; j < k; ++j) dp[i][j] = inf; MIN = {0, -1}; SECOND = {0, 0}; for (int i = 1; i <= n; ++i) { for (int j = 0; j < k; ++j) if (s[i] == (char)j + A ) { dp[i][j] = minexc(j).first; last[i][j] = minexc(j).second; } else dp[i][j] = minexc(j).first + 1, last[i][j] = minexc(j).second; MIN = {inf, inf}; SECOND = {inf, inf}; for (int j = 0; j < k; ++j) if (dp[i][j] < MIN.first) { SECOND = MIN; MIN = make_pair(dp[i][j], j); } else if (dp[i][j] < SECOND.first) SECOND = make_pair(dp[i][j], j); } int res = inf, poz = inf; for (int i = 0; i < k; ++i) if (dp[n][i] < res) res = dp[n][i], poz = i; cout << res << endl; if (res == 0) { for (int i = 1; i <= n; ++i) printf( %c , s[i]); exit(0); } for (int i = n; i > 0; --i) { ret[i] = poz + A ; poz = last[i][poz]; } for (int i = 1; i <= n; ++i) printf( %c , ret[i]); } |
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9; const long long INF64 = (long long)1e18; const long double eps = 1e-9; const long double pi = 3.14159265358979323846; int dp[20][50]; int frmP[20][50], frmQ[20][50], frmE[20][50]; long long ten[20]; char str[60]; vector<long long> arr; pair<long long, long long> rev[60]; long long num[20]; void update(int p1, int q1, int p2, int q2, int val) { if (dp[p1][q1] == 0) { dp[p1][q1] = 1; frmP[p1][q1] = p2; frmQ[p1][q1] = q2; frmE[p1][q1] = val; } } void re(int p1, int q1, int dep) { if (p1) { re(frmP[p1][q1], frmQ[p1][q1], dep + 1); int xx, yy; int i; long long val; val = arr[frmE[p1][q1]]; xx = rev[val].first; yy = rev[val].second; for (i = 0; i < xx; i++) num[i] += (4LL * ten[dep]); for (i = 0; i < yy; i++) num[i + xx] += (7LL * ten[dep]); } } int main() { long long i, j, k; for (i = 0; i <= 6; i++) { for (j = 0; j + i <= 6; j++) { arr.push_back(i * 4 + 7 * j); rev[i * 4 + 7 * j] = (make_pair(i, j)); } } sort((arr).begin(), (arr).end()); int tn; int len; long long t; t = 1; for (i = 0; i <= 18; i++) { ten[i] = t; t *= 10LL; } cin >> tn; while (tn--) { cin >> str; len = strlen(str); memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for (i = 1; i <= len; i++) { for (j = 0; j < 50; j++) { if (dp[i - 1][j]) { t = j * 10LL + (str[i - 1] - 0 ); for (k = 0; k < (int((arr).size())) && arr[k] <= t; k++) { if (t - arr[k] < 50LL) { update(i, t - arr[k], i - 1, j, k); } } } } } if (!dp[len][0]) printf( -1 n ); else { memset(num, 0, sizeof(num)); re(len, 0, 0); for (i = 0; i < 6; i++) { if (i) printf( ); cout << num[i]; } puts( ); } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; const long long llinf = 1e18 + 7; const double eps = 1e-15; const int N = 2e5 + 5; int cnt0[N], cnt1[N]; long long sum(int cnt[N], int l, int r) { long long ret = cnt[r]; if (l) ret -= cnt[l - 1]; return ret; } int main() { string a, b; cin >> a >> b; memset(cnt0, 0, sizeof cnt0); memset(cnt1, 0, sizeof cnt1); for (int i = 0; i < b.size(); ++i) { cnt0[i] += b[i] == 0 ; if (i) cnt0[i] += cnt0[i - 1]; cnt1[i] += b[i] == 1 ; if (i) cnt1[i] += cnt1[i - 1]; } long long ans = 0; for (int i = 0; i < a.size(); ++i) { int lastloc = int(b.size()) - int(a.size()) + i; if (a[i] == 1 ) { ans += sum(cnt0, i, lastloc); } else ans += sum(cnt1, i, lastloc); } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, r1, r2; cin >> n >> r1 >> r2; int a[n + 2]; for (int i = 1; i <= n; i++) if (i != r1) cin >> a[i]; else a[i] = 0; int v = r2, x = a[r2]; do { int y = a[x]; a[x] = v; v = x; x = y; } while (x != 0); for (int i = 1; i <= n; i++) if (i != r2) cout << a[i] << ; } |
#include<bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int,int>; using pll = pair<ll,ll>; #ifdef eclipse #define debug(...) do{ fprintf(stderr, LINE %d: (%s) = ,__LINE__, #__VA_ARGS__ ); _do(__VA_ARGS__); }while(0) template<typename T> void _do(T &&_x) {cerr << _x << endl;} template<typename T, typename ...S> void _do(T &&_x, S &&..._t) {cerr << _x << , ; _do(_t...);} #else #define debug(...) #endif const int MAXN = 300005; int n, in[MAXN], in2[MAXN]; int calc() { int ans = 0; for (int i = 1; i < n - 1; ++i) { if (in[i] < min(in[i-1], in[i+1])) ans++; else if (in[i] > max(in[i-1], in[i+1])) ans++; } return ans; } int calc2(int idx) { int x, y; x = y = 0; for (int i = idx - 1; i <= idx + 1; ++i) { if (i == 0 || i == n - 1) continue; if (in[i] < min(in[i-1], in[i+1])) y++; else if (in[i] > max(in[i-1], in[i+1])) y++; } for (int i = idx - 1; i <= idx + 1; ++i) { if (i == 0 || i == n - 1) continue; if (in2[i] < min(in2[i-1], in2[i+1])) x++; else if (in2[i] > max(in2[i-1], in2[i+1])) x++; } return x - y; } signed main (){ ios::sync_with_stdio(0),cin.tie(0); int t; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; ++i) cin >> in[i]; for (int i = 0; i < n; ++i) in2[i] = in[i]; int ans, now = calc(); ans = now; for (int i = 1; i < n - 1; ++i) { int tmp = in[i]; in[i] = in[i-1]; ans = min(ans, now - calc2(i)); in[i] = in[i+1]; ans = min(ans, now - calc2(i)); in[i] = tmp; } cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; long long n; int resp; int ask(long long x) { printf( ? %lld n , x); fflush(stdout); scanf( %d , &resp); return resp; } void ans(long long x) { printf( = %lld n , x); fflush(stdout); } void solve() { scanf( %lld , &n); if (n == 2ll) { resp = ask(1ll); resp = ask(2ll); if (resp) ans(1ll); else ans(2ll); return; } else if (n == 3ll) { resp = ask(2ll); resp = ask(1ll); if (resp) ans(1ll); else { resp = ask(3ll); if (resp) ans(2ll); else ans(3ll); } return; } vector<long long> path_mid_to_hi; long long lf = 1ll, rg = n - 1ll; for (long long md; lf <= rg;) { md = (lf + rg) / 2ll; path_mid_to_hi.push_back(md); if (lf == rg && md == n - 1ll) break; lf = md + 1ll; } long long st = n, bf = n; reverse(path_mid_to_hi.begin(), path_mid_to_hi.end()); bool go_left = 1; for (long long i : path_mid_to_hi) { bf = st; if (go_left) st -= i; else st += i; go_left ^= 1; } if (bf < st) go_left = 1; else go_left = 0; lf = 1ll, rg = n - 1ll; long long res = n; resp = ask(st); for (long long md; lf <= rg;) { md = (lf + rg) / 2; if (go_left) st -= md; else st += md; resp = ask(st); if (resp) { res = md; rg = md - 1; } else lf = md + 1; go_left ^= 1; } ans(res); return; } int main() { int tc; scanf( %d , &tc); for (; tc--;) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n, m; cin >> n >> m; vector<long long int> v; long long int s = (m + 1) / 2; v.push_back(s); long long int i = 1; if (m % 2) { while (1) { if (s + i == m + 1) break; if (s - i > 0) v.push_back(s - i); v.push_back(s + i); i++; } } else { while (1) { if (s + i == m + 1) break; v.push_back(s + i); if (s - i > 0) v.push_back(s - i); i++; } } i = 0; while (i < n) { cout << v[i] << n ; v.push_back(v[i]); i++; } return 0; } |
#include <bits/stdc++.h> const int INF = 1 << 29; int cmp(const void* a, const void* b); long long maxv(long long a, long long b); long long minv(long long a, long long b); long long gcd(long long u, long long v); int main() { long long t = 1, i = 0; while (t--) { long long r, d, n; scanf( %lld%lld%lld , &r, &d, &n); long long ans = 0, x, y, cr; for (long long i = 0; i < (n); i++) { scanf( %lld%lld%lld , &x, &y, &cr); if (x * x + y * y <= (r - cr) * (r - cr) && 2 * cr <= d && x * x + y * y >= (r - d + cr) * (r - d + cr)) ans++; } printf( %lld n , ans); } } int cmp(const void* a, const void* b) { if (*(long long*)a - *(long long*)b < 0) return -1; if (*(long long*)a - *(long long*)b > 0) return 1; return 0; } long long maxv(long long a, long long b) { if (a > b) return a; return b; } long long minv(long long a, long long b) { if (a < b) return a; return b; } long long gcd(long long u, long long v) { if (v == 0) return u; return gcd(v, u % v); } |
#include <bits/stdc++.h> using namespace std; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, -1, 1}; void arquivo() { freopen( , r , stdin); freopen( , w , stdout); } const int N = 505050; struct no { int l, r; int op; no(){}; no(int a, int b, int c) { op = a; l = b; r = c; } }; no vet[N]; int n, m, p; char s[N]; inline void print() { p = vet[0].r; while (p != n + 1) { printf( %c , vet[p].op == 1 ? ( : ) ); p = vet[p].r; } printf( n ); } inline void goRemove() { int p1 = p; int p2 = p; int qtd = vet[p1].op; while (qtd > 0) { p2 = vet[p2].r; qtd += vet[p2].op; } while (qtd < 0) { p2 = vet[p2].l; qtd += vet[p2].op; } int esq = min(p1, p2); int dir = max(p1, p2); esq = vet[esq].l; dir = vet[dir].r; vet[esq].r = dir; vet[dir].l = esq; if (dir != n + 1) p = dir; else p = esq; } int main() { scanf( %d %d %d , &n, &m, &p); vet[0].r = 1; vet[0].l = 0; for (int i = 1; i <= n; ++i) { char c; scanf( %c , &c); if (c == ( ) vet[i] = no(1, i - 1, i + 1); else vet[i] = no(-1, i - 1, i + 1); } vet[n + 1].l = n; vet[n + 1].r = n + 1; scanf( %s , s); int len = strlen(s); for (int i = 0; i < len; ++i) { if (s[i] == L ) p = vet[p].l; else if (s[i] == R ) p = vet[p].r; else goRemove(); } print(); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m; long long f[305][305][5]; struct point { int x, y; point(int x = 0, int y = 0) : x(x), y(y) {} } a[305]; struct edge { int u, v; double w; edge(int u = 0, int v = 0, double w = 0) : u(u), v(v), w(w) {} bool operator<(const edge b) const { return w < b.w; } } s[305 * 305]; int read() { char c = getchar(); int x = 0, s = 1; while (!isdigit(c)) { if (c == - ) s = -1; c = getchar(); } while (isdigit(c)) { x = (x << 3) + (x << 1) + c - 0 ; c = getchar(); } return x * s; } int main() { n = read(); for (int i = 1; i <= n; ++i) { a[i].x = read(); a[i].y = read(); } for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (i != j) s[++m] = edge(i, j, atan2(a[i].y - a[j].y, a[i].x - a[j].x)); sort(s + 1, s + m + 1); for (int i = 1; i <= m; ++i) { int u = s[i].u, v = s[i].v; f[u][v][0] = 1; for (int k = 1; k <= 4; ++k) for (int j = 1; j <= n; ++j) f[j][v][k] += f[j][u][k - 1]; } long long ans = 0; for (int i = 1; i <= n; ++i) ans += f[i][i][4]; printf( %I64d n , ans); } |
#include <bits/stdc++.h> using namespace std; int in[30]; int out[1050000][2]; int oc; int n; int pre[30]; int cnt[30][2]; inline int cntf(int np, int b) { if (cnt[np][b] != -1) return cnt[np][b]; if (np == n) return cnt[np][b] = 0; if (!b) return cnt[np][b] = cntf(pre[np] + 1, b) * 2 + (pre[np] - np + 1); else { int w1 = cntf(np + 1, 0) * 2 + 1; int w2 = cntf(pre[np] + 1, 0) * 2 + cntf(pre[np] + 1, 1) + (pre[np] - np + 1) * 2; return cnt[np][b] = min(w1, w2); } } inline void move(int a, int b, int c, bool base, int np) { if (np == n) return; if (base) { int d1 = cntf(np, 1); int d2 = cntf(np + 1, 0) * 2 + 1; if (d1 == d2) { move(a, c, b, false, np + 1); out[oc][0] = a; out[oc][1] = b; oc++; move(c, b, a, false, np + 1); } else { move(a, b, c, false, pre[np] + 1); for (int i = np; i <= pre[np]; i++) { out[oc][0] = a; out[oc][1] = c; oc++; } move(b, a, c, false, pre[np] + 1); for (int i = np; i <= pre[np]; i++) { out[oc][0] = c; out[oc][1] = b; oc++; } move(a, b, c, true, pre[np] + 1); } } else { move(a, c, b, false, pre[np] + 1); for (int i = np; i <= pre[np]; i++) { out[oc][0] = a; out[oc][1] = b; oc++; } move(c, b, a, false, pre[np] + 1); } } int main() { int i; scanf( %d , &n); for (i = 0; i < n; i++) { scanf( %d , &in[i]); } pre[n - 1] = n - 1; for (i = n - 2; i >= 0; i--) { if (in[i] == in[i + 1]) pre[i] = pre[i + 1]; else pre[i] = i; } memset(cnt, -1, sizeof(cnt)); oc = 0; move(1, 3, 2, true, 0); printf( %d n , oc); for (i = 0; i < oc; i++) printf( %d %d n , out[i][0], out[i][1]); } |
#include <bits/stdc++.h> using namespace std; using std::bitset; const double Pi = acos(-1); inline long long read() { long long x = 0; bool fu = 0; char ch = 0; while (ch > 9 || ch < 0 ) { ch = getchar(); if (ch == - ) fu = 1; } while (ch <= 9 && ch >= 0 ) x = (x * 10 - 48 + ch), ch = getchar(); return fu ? -x : x; } long long n, mod, w[100002], q, l, r; map<long long, long long> phi; inline long long go(long long num) { long long rt = num; for (long long i = 2; i * i <= num; i++) { if (num % i == 0) rt = rt * (i - 1) / i; while (num % i == 0) num /= i; } if (num > 1) rt = rt * (num - 1) / num; return rt; } inline long long pw(long long x, long long p, long long mod) { bool flag = 0; long long rt = 1; while (p) { if (p & 1) { if (rt * x >= mod) flag = 1; rt = rt * x % mod; } if (x * x >= mod) flag = 1; x = x * x % mod, p >>= 1; } return rt + flag * mod; } inline long long dfs(long long pos, long long mod) { if (pos > r || mod == 1) return 1; return pw(w[pos], dfs(pos + 1, phi[mod]), mod); } signed main() { cin >> n >> mod; l = mod; phi[1] = 1; while (l > 1) phi[l] = go(l), l = phi[l]; for (register long long i = 1, iend = n; i <= iend; ++i) w[i] = read(); cin >> q; while (q--) { l = read(), r = read(); cout << dfs(l, mod) % mod << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int maxn = 200; long long x, y, l, r; void solve() { set<long long> a, b, S; a.insert(1); b.insert(1); long long base = x; while (base <= INF) { a.insert(base); if (INF / base < x || x == 1) break; base *= x; } base = y; while (base <= INF) { b.insert(base); if (INF / base < y || y == 1) break; base *= y; } for (auto i : a) for (auto j : b) if (i + j <= r && i + j >= l) S.insert(i + j); S.insert(r + 1); long long pre = l - 1; long long ans = 0; for (auto z : S) { ans = max(ans, z - pre - 1); pre = z; } cout << ans << endl; } int main() { ios::sync_with_stdio(false); while (cin >> x >> y >> l >> r) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string s; int m; int a[100005] = {0}; int b[100005] = {0}; cin >> s; s = + s; cin >> m; for (int i = 1; i <= s.length(); i++) { if (s[i] == s[i + 1]) a[i] = 1; } for (int i = 1; i <= s.length(); i++) { b[i] = b[i - 1] + a[i]; } while (m--) { int l, r, flag = 0; cin >> l >> r; flag = b[r - 1] - b[l - 1]; cout << flag << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; void test_case() { int n, i, cnt; cin >> n; string str; cin >> str; cnt = 0; vector<int> c(n + 5, 0); vector<int> flag(n + 5, 0); int fl = 0; for (i = n - 1; i >= 0; i--) { if (str[i] == ( ) cnt++; else cnt--; c[i] = cnt; if (cnt > 0) fl = 1; flag[i] = fl; } cnt = 0; int ans = 0; fl = 0; for (i = 0; i < n; i++) { if (fl == 0 && str[i] == ) && cnt + 1 == -c[i + 1] && cnt + 1 >= 0 && flag[i + 1] == 0) { ans++; } else if (fl == 0 && str[i] == ( && cnt - 1 == -c[i + 1] && cnt - 1 >= 0 && flag[i + 1] == 0) { ans++; } if (str[i] == ( ) cnt++; else if (str[i] == ) ) cnt--; if (cnt < 0) fl = 1; } cout << ans << n ; return; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int t; t = 1; while (t--) { test_case(); } } |
#include <bits/stdc++.h> using namespace std; const int maxn = 2000 + 4; char s[maxn]; int a[maxn]; void get(bitset<maxn> &B) { scanf( %s , s); int m = strlen(s); int x = 1, y = -1; for (int i = 0; i < m; i++) a[m - 1 - i] = s[i] - 0 ; m--; x += m + y; ++x; for (int i = 0; i < maxn && x >= 0; i++) { int rem = 0; for (int j = m; j >= 0; j--) a[j] += rem * 10, rem = (a[j] & 1), a[j] >>= 1; B[i] = rem; while (m >= 0 && !a[m]) m--; x = m; } } int pos[maxn]; bitset<maxn> b[maxn], st[maxn]; int main() { if (fopen( in , r )) { freopen( in , r , stdin); freopen( out , w , stdout); } memset(pos, -1, sizeof(pos)); int n; scanf( %d , &n); for (int i = 0; i < n; i++) { get(b[i]); st[i][i] = 1; bool ok = 1; for (int j = 0; j < maxn; j++) if (b[i][j]) { if (pos[j] == -1) { ok = 0; pos[j] = i; break; } b[i] ^= b[pos[j]]; st[i] ^= st[pos[j]]; } if (!ok) { printf( 0 n ); continue; } st[i][i] = 0; printf( %d , st[i].count()); for (int j = 0; j < maxn; j++) if (st[i][j]) printf( %d , j); printf( n ); } } |
#include <bits/stdc++.h> using namespace std; ifstream fin; ofstream fout; FILE *outt, *inn; const int N = 100009, M = 70; int k, b[M], d[M], k2, y, yy; unsigned long long m, c[M][M], l, r, cur; void find() { for (int i = 0; i <= 64; i++) { for (int j = 0; j <= 64; j++) { if (i == 0) { c[i][j] = 1; continue; } if (i > j) continue; c[i][j] = c[i - 1][j - 1] + c[i][j - 1]; } } } int cnt; bool is; void binary(unsigned long long x) { memset(b, 0, sizeof(b)); is = 0; cnt = 69; while (x > 0) { b[--cnt] = x % 2; x /= 2; } b[0] = cnt; for (int i = b[0] + 1; i <= 68; i++) { if (b[i]) { return; } } is = 1; } unsigned long long solve(unsigned long long x) { cur = 0; binary(2 * x); k2 = k - 1; for (int i = b[0] + 1; i <= 68; i++) { if (b[i] == 1) { y = 68 - i; cur += c[k2][y]; k2--; } if (k2 == 0) { for (int j = i + 1; j <= 68; j++) { if (b[j]) { cur++; break; } } break; } } k2 = 0; for (int i = b[0]; i <= 68; i++) { if (b[i]) k2++; } if (k2 == k) cur++; binary(x + 1); if (!is) { k2 = k - 1; for (int i = b[0] + 1; i <= 68; i++) { if (b[i] == 0) { cur += c[k2 - 1][68 - i]; } if (b[i] == 1) { k2--; } if (k2 == 0) { break; } } k2 = 0; for (int i = b[0]; i <= 68; i++) { if (b[i]) k2++; } if (k2 == k) cur++; } return cur; } int main() { memset(c, 0, sizeof(c)); find(); cin >> m >> k; r = (unsigned long long)1; while (solve(r) < m) { r *= 2; } l = r / 2 + 1; while (l < r) { unsigned long long mid = (l + r) / 2; unsigned long long res = solve(mid); if (res < m) { l = mid + 1; } else if (res > m) { r = mid - 1; } else { cout << mid; return 0; } } cout << l; return 0; } |
#include <bits/stdc++.h> using namespace std; map<pair<int, int>, int> ord; vector<pair<int, int> > edges; vector<int> adj[200000]; int n; int main() { cin >> n; cout << n - 1 << endl; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; edges.push_back(make_pair(a, b)); ord[make_pair(a, b)] = ord[make_pair(b, a)] = i; adj[a].push_back(b); adj[b].push_back(a); cout << 2 << a << << b << endl; } for (int i = 1; i <= n; i++) for (int j = 0; j < adj[i].size() - 1; j++) cout << ord[make_pair(i, adj[i][j])] << << ord[make_pair(i, adj[i][j + 1])] << endl; } |
#include <bits/stdc++.h> using namespace std; int Max[100010 * 4], Min[100010 * 4]; int dp_Min[100010 * 4]; int a[100010]; int dp[100010]; int ql, qr, val; void build(int l, int r, int o) { if (l == r) { Max[o] = Min[o] = a[l]; return; } int mid = l + (r - l) / 2; build(l, mid, o * 2); build(mid + 1, r, o * 2 + 1); Max[o] = max(Max[o * 2], Max[o * 2 + 1]); Min[o] = min(Min[o * 2], Min[o * 2 + 1]); } int query_max(int l, int r, int o) { if (ql <= l && qr >= r) { return Max[o]; } int mid = l + (r - l) / 2; int ans = -0x3f3f3f3f; if (ql <= mid) ans = max(ans, query_max(l, mid, o * 2)); if (qr > mid) ans = max(ans, query_max(mid + 1, r, o * 2 + 1)); return ans; } int query_min(int l, int r, int o) { if (ql <= l && qr >= r) { return Min[o]; } int mid = l + (r - l) / 2; int ans = 0x3f3f3f3f; if (ql <= mid) ans = min(ans, query_min(l, mid, o * 2)); if (qr > mid) ans = min(ans, query_min(mid + 1, r, o * 2 + 1)); return ans; } int query2(int l, int r, int o) { if (ql <= l && qr >= r) { return dp_Min[o]; } int mid = l + (r - l) / 2; int ans = 0x3f3f3f3f; if (ql <= mid) ans = min(ans, query2(l, mid, o * 2)); if (qr > mid) ans = min(ans, query2(mid + 1, r, o * 2 + 1)); return ans; } void update(int l, int r, int o) { if (l == r) { dp_Min[o] = val; return; } int mid = l + (r - l) / 2; if (ql <= mid) update(l, mid, o * 2); else update(mid + 1, r, o * 2 + 1); dp_Min[o] = min(dp_Min[o * 2], dp_Min[o * 2 + 1]); } int main() { int n, s, l; scanf( %d%d%d , &n, &s, &l); for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); } build(1, n, 1); memset(dp_Min, 0x3f3f3f3f, sizeof(dp_Min)); for (int i = 1; i < l; i++) dp[i] = 0x3f3f3f3f; for (int i = l; i <= n; i++) { int x = 1, y = i - l + 2; while (x < y) { int mid = x + (y - x) / 2; ql = mid, qr = i; int val = query_max(1, n, 1) - query_min(1, n, 1); if (val > s) x = mid + 1; else y = mid; } if (x == i - l + 2) { dp[i] = 0x3f3f3f3f; continue; } ql = x - 1, qr = i - l; if (ql <= qr && qr) dp[i] = query2(1, n, 1) + 1; else dp[i] = 0x3f3f3f3f; if (x - 1 == 0) dp[i] = 1; val = dp[i]; ql = i; update(1, n, 1); } printf( %d n , dp[n] >= 0x3f3f3f3f ? -1 : dp[n]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int a[100010]; int b[100010]; while (scanf( %d , &a[0]) != EOF) { getchar(); int n = 1; while (scanf( %d , &a[n]) != EOF) { n++; getchar(); } sort(a, a + n); a[n] = 9999999; int h = 0; for (int i = 0; i < n; i++) { if (i == 0) { b[h++] = a[i]; } else if (a[i] != a[i - 1]) { b[h++] = a[i]; } } b[h] = 99999; int p = b[0]; int q = b[0]; int flag = 0; int ff = 0; for (int i = 1; i <= h; i++) { if (b[i] == b[i - 1] + 1) { p = b[i]; } else if (b[i] != b[i - 1] + 1) { ff = 1; if (flag == 1) { printf( , ); } flag = 1; if (p == q) { printf( %d , p); } else { printf( %d-%d , q, p); } p = b[i]; q = b[i]; } } printf( n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long a[1010]; int main() { long long n, k, p; int q; scanf( %I64d%I64d%d , &n, &k, &q); p = n - k; for (int i = 0; i < q; i++) scanf( %I64d , a + i); if (k == 0) { for (int i = 0; i < q; i++) printf( . ); return 0; } for (int i = 0; i < q; i++) { if (n % 2 == 0) { if (k <= p) { if (a[i] <= n - 2 * k) printf( . ); else if (a[i] % 2 == 1) printf( . ); else printf( X ); } else { if (a[i] > 2 * p) printf( X ); else if (a[i] % 2 == 1) printf( . ); else printf( X ); } } else { if (p < k) { if (a[i] <= 2 * p) { if (a[i] % 2 == 1) printf( . ); else printf( X ); } else printf( X ); } else { if (a[i] == n) printf( X ); else if (a[i] <= p - k + 1) printf( . ); else if (a[i] % 2 == 1) printf( . ); else printf( X ); } } } return 0; } |
#include <bits/stdc++.h> using namespace std; const long double PI = 3.14159265358979323846; const int MAXN = 1e5 + 3; long long n, a, b, p, q; bool isLeap(int a) { if (a % 400 == 0) return true; if (a % 4 == 0 && a % 100) return true; return false; } void solve() { int y; cin >> y; int cnt = 0; bool f = 0; if (isLeap(y)) f = 1; y++; bool isl = 0; while (1) { cnt += 365; isl = isLeap(y); if (isl) cnt++; if (cnt % 7 == 0 && isl == f) break; y++; } cout << y; } int main() { solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod1 = 1e9 + 7, mod = 998244353; const long long N = 3e5 + 7; long long ncr[N]; void fact(long long A) { auto binexp = [&](long long a, long long b) { long long res = 1; while (b) { if (b & 1) res *= a; a *= a; b >>= 1; a %= mod; res %= mod; } return res; }; ncr[0] = 1; for (long long i = 1; i <= A; i++) ncr[i] = ncr[i - 1] * (A - i + 1) % mod * binexp(i, mod - 2) % mod; } void a_b_l() { long long n; cin >> n; n <<= 1; long long ar[n]; for (long long i = 0; i < n; i++) cin >> ar[i]; sort(ar, ar + n); long long k = 0; for (long long i = 0; i < n / 2; i++) { k += ar[n - i - 1] - ar[i]; k %= mod; } fact(n); long long ans = k * ncr[n / 2] % mod; cout << ans << n ; } signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); ; long long t = 1; while (t--) a_b_l(); } |
#include <bits/stdc++.h> using namespace std; const double PAI = atan(1.0) * 4; const double EPS = 1e-5; int n; double theta; struct Point { double x, y; Point() {} Point(double _x, double _y) : x(_x), y(_y) {} Point operator+(const Point &r) const { return Point(x + r.x, y + r.y); } Point operator-(const Point &r) const { return Point(x - r.x, y - r.y); } Point operator*(double r) const { return Point(x * r, y * r); } Point operator/(double r) const { return Point(x / r, y / r); } Point rotate(double alpha) const { double s = sin(alpha), c = cos(alpha); return Point(x * c - y * s, x * s + y * c); } double lenght(const Point &r) const { return sqrt((x - r.x) * (x - r.x) + (y - r.y) * (y - r.y)); } Point cross_X(const Point &r) const { double l = x - r.x; l = l * r.y / (r.y - y); return Point((x * r.y - y * r.x) / (r.y - y), 0); } } p[105]; bool same(double x, double y) { return ((x - y) <= EPS && (y - x) <= EPS); } bool check() { double l[105]; l[1] = p[1].lenght(p[n]); for (int i = 2; i <= n; i++) l[i] = p[i].lenght(p[i - 1]); for (int i = 1; i <= n; i++) { if (p[i].x > 1000000 || p[i].x < -1000000) return false; if (p[i].y > 1000000 || p[i].y < -1000000) return false; if (l[i] > 1000 || l[i] < 1) return false; for (int j = 1; j < i; j++) if (same(l[i], l[j])) return false; } return true; } int main() { ios::sync_with_stdio(false); cin >> n; if (n <= 4) { cout << No solution << endl; return 0; } theta = 2 * PAI / n; while (true) { p[1] = Point(0, 0); for (int i = 2; i <= n / 2; i++) { p[i] = Point(2 * i, 0); p[i] = p[i].rotate(theta * (i - 1)) + p[i - 1]; } p[n / 2 + 1] = Point(900, 0); p[n / 2 + 1] = p[n / 2 + 1].rotate(theta * (n / 2)) + p[n / 2]; for (int i = n / 2 + 2; i <= n; i++) { p[i] = Point(2 * (i - n / 2) - 1, 0); p[i] = p[i].rotate(theta * (i - 1)) + p[i - 1]; } p[n] = p[n - 1].cross_X(p[n]); if (check()) break; } for (int i = 1; i <= n; i++) printf( %lf %lf n , p[i].x, p[i].y); return 0; } |
#include <bits/stdc++.h> using namespace std; class point { public: int x, ind; point(int dx = 0, int _ind = 0) { x = dx; ind = _ind; } }; int n, k, f[210][210]; point a[210], c[210]; bool cmpx(point A, point B) { return A.x < B.x; } bool cmpi(point A, point B) { return A.ind < B.ind; } int main() { cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> a[i].x; a[i].ind = i; c[i] = a[i]; } sort(c, c + n, cmpx); for (int i = 0; i < n; ++i) for (int j = i; j < n; ++j) for (int q = i; q <= j; ++q) f[i][j] += a[q].x; for (int i = 0; i < n; ++i) for (int j = i; j < n; ++j) { sort(a + i, a + j + 1, cmpx); int t = n - 1; int cnt = k; for (int q = i; q <= j; ++q) { while ((c[t].ind >= i) && (c[t].ind <= j) && (t >= 0)) --t; if ((t >= 0) && (cnt > 0) && (c[t].x > a[q].x)) { f[i][j] = f[i][j] - a[q].x + c[t].x; --t; --cnt; } else break; } sort(a + i, a + j + 1, cmpi); } int mx = f[0][0]; for (int i = 0; i < n; ++i) for (int j = i; j < n; ++j) if (mx < f[i][j]) mx = f[i][j]; cout << mx; return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) using namespace std; double h, d, c, n, mx; int hh, mm; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> hh >> mm; hh = hh * 60 + mm; cin >> h >> d >> c >> n; double nec = ceil(h / n) * c; if (hh >= 1200 && hh < 1440) return cout << setprecision(5) << fixed << nec * 0.8, 0; mm = 20 * 60; int diff = mm - hh; double nec2 = ceil((diff * d + h) / n) * c; cout << setprecision(5) << fixed << min(nec2 * 0.8, nec); return 0; } |
#include <bits/stdc++.h> using namespace std; static int i = 0; int fun(long long n) { i++; if (n % 2 == 1) return 0; else fun(n / 2); } int main() { int n; long long k; scanf( %d %lld , &n, &k); fun(k); printf( %d , i); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, id[105], m, i, j; unsigned long long cant[105], c[105], T[105], total, sol, factor, x, llevo; bool cmp(const int i, const int j) { return c[i] < c[j]; } int main() { scanf( %d , &n); for (i = 0; i < n; i++) { cin >> cant[i] >> c[i]; total += cant[i]; id[i] = i; } sort(id, id + n, cmp); scanf( %d , &m); for (i = 0; i < m; i++) cin >> T[i]; factor = 1; llevo = 0; i = 0; while (total > 0 && i < m) { if (llevo == T[i]) factor++, i++; j = 0; while (total > 0 && i < m && llevo < T[i]) { x = min(cant[id[j]], T[i] - llevo); cant[id[j]] -= x; total -= x; sol += x * factor * c[id[j]]; llevo += x; j++; } } for (i = 0; i < n; i++) sol += cant[i] * factor * c[i]; cout << sol << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; struct segment { int l, r, p; } seg[300005]; struct PS { int l, r, val; } P[300005 * 20]; int n, m, k, a, b, l, r, tot; int suf[300005]; int get(int node, int bas, int son, int x, int y) { if (bas >= x && son <= y) return P[node].val; bool l = (((bas + son) >> 1) >= x); bool r = (((bas + son) >> 1) + 1 <= y); if (l & r) return max(get(P[node].l, bas, ((bas + son) >> 1), x, y), get(P[node].r, ((bas + son) >> 1) + 1, son, x, y)); if (l) return get(P[node].l, bas, ((bas + son) >> 1), x, y); return get(P[node].r, ((bas + son) >> 1) + 1, son, x, y); } void create(int& node) { node = ++tot; P[node].val = 1005000000; } void up(int& node, int prv, int bas, int son, int x, int val) { if (!node) create(node); if (bas == x && son == x) { P[node].val = min(val, P[prv].val); return; } if (x <= ((bas + son) >> 1)) { P[node].r = P[prv].r; up(P[node].l, P[prv].l, bas, ((bas + son) >> 1), x, val); } else { P[node].l = P[prv].l; up(P[node].r, P[prv].r, ((bas + son) >> 1) + 1, son, x, val); } P[node].val = max(P[P[node].l].val, P[P[node].r].val); } int main() { P[0].val = 1005000000; scanf( %d %d %d , &n, &m, &k); for (int i = 1; i <= k; i++) { scanf( %d %d %d , &seg[i].l, &seg[i].r, &seg[i].p); } sort(seg + 1, seg + 1 + k, [](segment a, segment b) { return a.l < b.l; }); for (int i = k; i >= 1; i--) { up(suf[i], suf[i + 1], 1, n, seg[i].p, seg[i].r); } while (m--) { scanf( %d %d %d %d , &a, &b, &l, &r); int bas = 1, son = k; while (bas <= son) { if (seg[((bas + son) >> 1)].l < l) bas = ((bas + son) >> 1) + 1; else son = ((bas + son) >> 1) - 1; } printf(get(suf[bas], 1, n, a, b) > r ? no : yes ); puts( ); fflush(stdout); } } |