solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; long long int x; cin >> x; long long int mn = (s[0] - '0') * 10 + (s[1] - '0'); mn *= 60; mn += (s[3] - '0') * 10 + (s[4] - '0'); mn += x; long long int minute = mn % 60; long long int hour = mn / 60; if (hour >= 24) { hour %= 24; } if (hour < 10) cout << "0"; cout << hour << ":"; if (minute < 10) cout << "0"; cout << minute << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int, int>; using LP = pair<ll, ll>; template <class T> istream& operator>>(istream& is, vector<T>& v) { for (T& t : v) { is >> t; } return is; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { for (int i = 0; i < (v.size()); ++i) os << v[i] << (i == v.size() - 1 ? "\n" : " "); return os; } void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; } void YES(bool b) { cout << (b ? "YES" : "NO") << endl; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } const int inf = 1001001001; const ll linf = 1001001001001001001 * 4; template <typename T, typename MERGE, typename UPDATE> class segtree { int n; vector<T> val; T identity; MERGE merge; UPDATE _update; public: segtree(int _n, vector<T> init, T identity, MERGE merge, UPDATE update) : identity(identity), merge(merge), _update(update) { n = 1; while (n < _n) n *= 2; val = vector<T>(2 * n - 1, identity); for (int i = 0; i < (_n); ++i) val[i + n - 1] = init[i]; for (int i = (n - 1) - 1; i >= 0; i--) val[i] = merge(val[i * 2 + 1], val[i * 2 + 2]); } void update(int i, T x) { i += n - 1; val[i] = _update(val[i], x); while (i > 0) { i = (i - 1) / 2; val[i] = merge(val[i * 2 + 1], val[i * 2 + 2]); } } T query(int a, int b, int k = 0, int l = 0, int r = -1) { if (r == -1) r = n; if (b <= l || r <= a) return identity; if (a <= l && r <= b) return val[k]; T t1 = query(a, b, 2 * k + 1, l, (l + r) / 2); T t2 = query(a, b, 2 * k + 2, (l + r) / 2, r); return merge(t1, t2); } T operator[](int i) const { return val[i]; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; vector<ll> a((n + 1) / 2); cin >> a; ll x; cin >> x; for (int i = 0; i < (n / 2); ++i) a.push_back(x); vector<ll> sum(n + 1, 0); for (int i = 0; i < (n); ++i) sum[i + 1] = sum[i] + a[i]; if (sum[n] > 0) { cout << n << endl; return 0; } int sz = (n + 1) / 2; auto f = [](ll a, ll b) { return min(a, b); }; auto g = [](ll a, ll b) { return a; }; vector<ll> init(sz + 1); for (int i = 0; i < (sz + 1); ++i) init[i] = x * i - sum[i]; segtree<ll, decltype(f), decltype(g)> st(sz + 1, init, linf, f, g); for (int k = (sz); k < (n); ++k) { if (n - k > sz && x <= 0) continue; ll mn = st.query(0, n - k + 1); mn += sum[k]; if (mn > 0) { cout << k << endl; return 0; } } cout << -1 << endl; }
8
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { long long r; while (b != 0) { r = a % b; a = b; b = r; } return a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long fpow(long long n, long long k, int p = (int)1e9 + 7) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } const int MOD = (int)1e9 + 7; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = 2 * acos((long double)0); const int maxn = 110; int n, m; int a[maxn]; void solve() { scanf("%d%d", &n, &m); for (int i = (0); i < (n); i++) { int k; scanf("%d", &k); for (int i = (0); i < (k); i++) { int d; scanf("%d", &d); d--; a[d] = 1; } } for (int i = (0); i < (m); i++) if (!a[i]) { printf("NO"); return; } printf("YES"); } int main() { solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<long long> a; const long long inf = 1e18; long long n, start, x; const long long MAX_QUERY = 1999, BRUTE = 0; vector<bool> used(100500, false); int main() { ios_base ::sync_with_stdio(0); cin.tie(0); long long i; cin >> n >> start >> x; for (i = 1; i <= n; i++) a.push_back(i); random_shuffle(a.begin(), a.end()); for (i = 1; i < n; i++) { long long t = rand(); long long w = rand(); long long q = rand(); long long e = rand(); long long ind1 = ((t + 173) * (w + 59)) % n; long long ind2 = ((t + 227) * (w + 313)) % n; swap(a[ind1], a[ind2]); } for (i = 0; i < n; i++) { if (a[i] == start) { swap(a[0], a[i]); break; } } long long rez = -inf; long long connect = -1; long long query = 0; long long val, Next; for (i = 0; i < min(500LL, n); i++) { long long wish = a[i]; if (!used[wish]) { query++; cout << "? " << wish << endl; fflush(stdout); cin >> val >> Next; if (wish == start && val >= x) { cout << "! " << val << endl; fflush(stdout); return 0; } if (val <= x && val > rez) { rez = val; connect = Next; } if (val > x) { if (Next != -1) used[Next] = false; } } } if (rez == x) { cout << "! " << rez << endl; fflush(stdout); return 0; } for (i = 0; i < 5000; i++) { if (rez >= x) { cout << "! " << rez << endl; fflush(stdout); return 0; } if (connect == -1 || query == MAX_QUERY - 1) { if (rez < x) { cout << "! -1" << endl; fflush(stdout); } else { cout << "! " << rez << endl; fflush(stdout); } return 0; } cout << "? " << connect << endl; fflush(stdout); query++; cin >> val >> Next; if (val >= x) { cout << "! " << val << endl; fflush(stdout); return 0; } rez = val; connect = Next; } if (rez < x) { cout << "! -1" << endl; fflush(stdout); } else { cout << "! " << rez << endl; fflush(stdout); } return 0; }
6
#include <bits/stdc++.h> using namespace std; struct MaxFlow { const long long INF = 1e18; struct Edge { int u, v; long long c, rc, cost; shared_ptr<long long> flow; Edge(int _u, int _v, long long _c, long long _cost = 0) : u(_u), v(_v), c(_c), cost(_cost) { rc = 0; } }; struct FlowTracker { shared_ptr<long long> flow; long long cap, rcap; bool dir; FlowTracker(long long _cap, long long _rcap, shared_ptr<long long> _flow, int _dir) : cap(_cap), rcap(_rcap), flow(_flow), dir(_dir) {} long long rem() const { if (dir == 0) { return cap - *flow; } else { return rcap + *flow; } } void add_flow(long long f) { if (dir == 0) *flow += f; else *flow -= f; assert(*flow <= cap); assert(-*flow <= rcap); } operator long long() const { return rem(); } void operator-=(long long x) { add_flow(x); } void operator+=(long long x) { add_flow(-x); } }; int source, sink; vector<vector<int> > adj, cost; vector<vector<FlowTracker> > cap; vector<Edge> edges; MaxFlow(int _source, int _sink) : source(_source), sink(_sink) { assert(source != sink); } int add_edge(int u, int v, long long c, long long cost = 0) { edges.push_back(Edge(u, v, c, cost)); return edges.size() - 1; } vector<int> now, lvl; vector<long long> pot; void prep() { int max_id = max(source, sink); for (auto edge : edges) max_id = max(max_id, max(edge.u, edge.v)); adj.resize(max_id + 1); cost.resize(max_id + 1); cap.resize(max_id + 1); now.resize(max_id + 1); lvl.resize(max_id + 1); pot.resize(max_id + 1); for (auto &edge : edges) { auto flow = make_shared<long long>(0); adj[edge.u].push_back(edge.v); cost[edge.u].push_back(edge.cost); cap[edge.u].push_back(FlowTracker(edge.c, edge.rc, flow, 0)); if (edge.u != edge.v) { adj[edge.v].push_back(edge.u); cost[edge.v].push_back(-edge.cost); cap[edge.v].push_back(FlowTracker(edge.c, edge.rc, flow, 1)); } assert(cap[edge.u].back() == edge.c); edge.flow = flow; } } bool dinic_bfs() { fill(now.begin(), now.end(), 0); fill(lvl.begin(), lvl.end(), 0); lvl[source] = 1; vector<int> bfs(1, source); for (int i = 0; i < bfs.size(); ++i) { int u = bfs[i]; for (int j = 0; j < adj[u].size(); ++j) { int v = adj[u][j]; if (cap[u][j] > 0 && lvl[v] == 0) { lvl[v] = lvl[u] + 1; bfs.push_back(v); } } } return lvl[sink] > 0; } long long dinic_dfs(int u, long long flow) { if (u == sink) return flow; while (now[u] < adj[u].size()) { int v = adj[u][now[u]]; if (lvl[v] == lvl[u] + 1 && cap[u][now[u]] != 0) { long long res = dinic_dfs(v, min(flow, (long long)cap[u][now[u]])); if (res > 0) { cap[u][now[u]] -= res; return res; } } ++now[u]; } return 0; } long long max_flow() { prep(); long long ans = 0; while (dinic_bfs()) { long long cur = 0; do { cur = dinic_dfs(source, INF); ans += cur; } while (cur > 0); } return ans; } void bellman_ford() { vector<long long> dist(adj.size(), INF); dist[source] = 0; for (int times = 0; times < dist.size(); ++times) for (int u = 0; u < dist.size(); ++u) if (dist[u] < INF) for (int i = 0; i < adj[u].size(); ++i) if (cap[u][i]) dist[adj[u][i]] = min(dist[adj[u][i]], dist[u] + cost[u][i]); for (int u = 0; u < adj.size(); ++u) for (int i = 0; i < adj[u].size(); ++i) cost[u][i] += dist[u] - dist[adj[u][i]]; pot = dist; } long long dijkstra(vector<long long> &dist, vector<int> &last_node, vector<int> &last_index) { dist[source] = 0; vector<long long> flow(dist.size()); flow[source] = INF; vector<bool> visited(dist.size()); priority_queue<pair<long long, int> > pq; pq.push({0, source}); while (!pq.empty()) { int u = pq.top().second; pq.pop(); if (visited[u]) continue; visited[u] = true; for (int i = 0; i < adj[u].size(); ++i) { int v = adj[u][i]; if (!visited[v] && cap[u][i]) if (dist[u] + cost[u][i] + pot[u] - pot[v] < dist[v]) { dist[v] = dist[u] + cost[u][i] + pot[u] - pot[v]; last_node[v] = u; last_index[v] = i; flow[v] = min(flow[u], (long long)cap[u][i]); pq.push({(long long)-dist[v], v}); } } } return flow[sink]; } pair<long long, long long> min_cost_flow() { prep(); long long total_flow = 0, total_cost = 0; while (true) { vector<long long> dist(adj.size(), INF); vector<int> last_node(dist.size()), last_index(dist.size()); long long flow = dijkstra(dist, last_node, last_index); if (flow == 0) break; for (int u = sink; u != source; u = last_node[u]) cap[last_node[u]][last_index[u]] -= flow; for (int i = 0; i < adj.size(); ++i) pot[i] += dist[i]; total_flow += flow; total_cost += pot[sink] * flow; } return {total_flow, total_cost}; } long long flow_on_edge(int edge_index) { assert(edge_index < edges.size()); return *edges[edge_index].flow; } }; vector<vector<int> > arr; int n, m; bool valid(int r, int c) { return r >= 0 && c >= 0 && r < n && c < m; } int main() { cin >> n >> m; arr = vector<vector<int> >(n, vector<int>(m)); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) scanf("%d", &arr[i][j]); int dr[] = {1, 0, -1, 0}; int dc[] = {0, 1, 0, -1}; auto mf = MaxFlow(n * m, n * m + 1); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) if ((i + j) % 2 == 0) { mf.add_edge(mf.source, i * m + j, 1); for (int d = 0; d < 4; ++d) if (valid(i + dr[d], j + dc[d])) { mf.add_edge(i * m + j, (i + dr[d]) * m + j + dc[d], 1, (arr[i][j] != arr[i + dr[d]][j + dc[d]])); } } else { mf.add_edge(i * m + j, mf.sink, 1); } auto res = mf.min_cost_flow(); assert(res.first == n * m / 2); cout << res.second << '\n'; }
7
#include <bits/stdc++.h> const int32_t M = 1e9 + 7; const int32_t mod = 998244353; using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 0; i < n; i++) cout << 1 << " "; cout << '\n'; } }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long sum = 0; for (int i = n, j = 0; i >= j; i--, j++) { sum += i - j; } cout << sum; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int LIM = 20, SIZE = 4, INF = 1e+9, MAX_LIM = 30000, M = 3; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int n, m, body, len = 0, max_seg = -1, dist[LIM][LIM][MAX_LIM], ans = INF; bool used[LIM][LIM][MAX_LIM], g[LIM][LIM]; pair<int, int> apple, head, body_pos[LIM]; class state { public: int x, y, body; state(int x, int y, int body) : x(x), y(y), body(body) {} }; state make_state(int x, int y, int body) { return state(x, y, body); } void read() { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { static char c; cin >> c; if (c == '.') { g[i][j] = true; } else if (c == '#') { g[i][j] = false; } else if (c == '@') { g[i][j] = true; apple = make_pair(i, j); } else { g[i][j] = true; max_seg = max(max_seg, int(c - '0')); body_pos[int(c - '0') - 1] = make_pair(i, j); } } } for (int i = 0; i < 2 * (max_seg - 1); i++) { len <<= 1; len |= 1; } } int get_num(int x, int y, int x1, int y1) { x = x1 - x; y = y1 - y; if (x == 1 && y == 0) return 0; if (x == 0 && y == 1) return 1; if (x == -1 && y == 0) return 2; return 3; } void build_body() { for (int i = max_seg - 1; i > 0; i--) { body <<= 2; int key = get_num(body_pos[i - 1].first, body_pos[i - 1].second, body_pos[i].first, body_pos[i].second); body |= key; body &= len; } } void paint_body(int x, int y, int body, bool b = false) { g[x][y] = b; for (int i = 0; i < max_seg - 2; i++) { int key = (body & M) % SIZE; x += dx[key]; y += dy[key]; g[x][y] = b; body >>= 2; } } void bfs() { int x = body_pos[0].first, y = body_pos[0].second; dist[x][y][body] = 0; used[x][y][body] = true; queue<state> q; q.push(make_state(x, y, body)); while (!q.empty()) { state base = q.front(); q.pop(); x = base.x; y = base.y; body = base.body; if (x == apple.first && y == apple.second) ans = min(ans, dist[x][y][body]); paint_body(x, y, body); for (int i = 0; i < SIZE; i++) { int x1 = x + dx[i], y1 = y + dy[i]; if (x1 < 0 || x1 >= n || y1 < 0 || y1 >= m || !g[x1][y1]) continue; int body1 = ((body << 2) | (i + 2) % SIZE) & len; if (!used[x1][y1][body1]) { used[x1][y1][body1] = true; dist[x1][y1][body1] = dist[x][y][body] + 1; q.push(make_state(x1, y1, body1)); } } paint_body(x, y, body, true); } } int main() { read(); build_body(); bfs(); cout << (ans != INF ? ans : -1); return 0; }
7
#include <bits/stdc++.h> using namespace std; inline int ___INT() { int ret; scanf("%d", &ret); return ret; } struct Edge { int u, v, w; int dir; Edge() { dir = -1; w = 0; } Edge(int _u, int _v) { u = _u; v = _v; w = 0; dir = -1; } }; int nodeCount, edgeCount; vector<int> adj[200005]; Edge E[200005]; int totalW[200005], totalF[200005]; void updateDir(int fromNode, int e) { if (E[e].u == fromNode) E[e].dir = 0; else E[e].dir = 1; } int getAdjNode(int u, int e) { if (E[e].u == u) return E[e].v; return E[e].u; } int calcDiff(int w, int f) { return abs(w - f - f); } void updateNodeInPQ(int u, int newF, set<pair<int, int> >& PQ) { int diff = calcDiff(totalW[u], totalF[u]); set<pair<int, int> >::iterator it = PQ.find(pair<int, int>(diff, u)); if (it != PQ.end()) PQ.erase(it); PQ.insert(pair<int, int>(calcDiff(totalW[u], newF), u)); } void solve() { set<pair<int, int> > PQ; for (int i = 0; i < (adj[1].size()); ++i) { int e = adj[1][i]; updateDir(1, e); int v = getAdjNode(1, e); totalF[v] += E[e].w; PQ.insert(pair<int, int>(calcDiff(totalW[v], totalF[v]), v)); } while (PQ.size() > 0) { pair<int, int> node = *(PQ.begin()); PQ.erase(PQ.begin()); int u = node.second; assert(node.first == 0 || u == nodeCount); for (int i = 0; i < (adj[u].size()); ++i) { int e = adj[u][i]; if (E[e].dir != -1) continue; updateDir(u, e); int v = getAdjNode(u, e); updateNodeInPQ(v, totalF[v] + E[e].w, PQ); totalF[v] += E[e].w; } } } int main() { nodeCount = ___INT(); edgeCount = ___INT(); memset(totalW, (0), sizeof(totalW)); memset(totalF, (0), sizeof(totalF)); for (int e = 0; e < (edgeCount); ++e) { int u = ___INT(); int v = ___INT(); int w = ___INT(); E[e].u = u; E[e].v = v; E[e].w = w; E[e].dir = -1; adj[u].push_back(e); adj[v].push_back(e); totalW[u] += E[e].w; totalW[v] += E[e].w; } solve(); for (int e = 0; e < (edgeCount); ++e) { printf("%d\n", E[e].dir); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main(void) { int a, b, ans = 0, cnt = 0; cin >> a >> b; while (a > 0) { ans += a; a += cnt; cnt = a % b; a /= b; } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int cal(int a, int b) { int res = 0; while (a) { res += b / a; b %= a; if (a > b) swap(a, b); } if (b == 1) return res; else return 1000000000; } int calMistake(int a, int b) { int res = 0; while (a) { b -= a; if (a > b) swap(a, b); else res++; } return res - 1; } int in[1000005][2]; int main() { long long N, R; long long minn = 1000000000; int X, Y; cin >> N >> R; for (int i = 0; i <= R; i++) { int a = i; int b = R - i; if (a > b) swap(a, b); if (cal(a, b) + 1 == N) { int temp = calMistake(a, b); if (temp < minn) { minn = temp; X = a; Y = b; } } } if (minn != 1000000000) { cout << max(0LL, minn) << endl; in[N - 1][0] = X; in[N - 1][1] = Y; for (int i = N - 2; i > 0; i--) { in[i][0] = in[i + 1][0]; in[i][1] = in[i + 1][1] - in[i + 1][0]; if (in[i][0] > in[i][1]) swap(in[i][0], in[i][1]); } int top = 0; int bot = 1; bool up = true; for (int i = 1; i <= N; i++) { if (i == 1) { printf("T"); top = 1; } else { if (i == N) { if (up) printf("B"); else printf("T"); } else { if (up) { int nt = top; int nb = top + bot; if (min(nt, nb) == in[i][0] && max(nt, nb) == in[i][1]) { printf("B"); up = false; top = nt; bot = nb; } else { printf("T"); nt = top + bot; nb = bot; top = nt; bot = nb; } } else { int nt = top + bot; int nb = bot; if (min(nt, nb) == in[i][0] && max(nt, nb) == in[i][1]) { printf("T"); up = true; top = nt; bot = nb; } else { printf("B"); nt = top; nb = top + bot; top = nt; bot = nb; } } } } } printf("\n"); } else { printf("IMPOSSIBLE\n"); } return 0; }
6
#include <bits/stdc++.h> int main() { int n; while (~scanf("%d", &n)) { int x, k, m; k = 0, m = n; while (n) { k++; x = n % 10; n /= 10; } int sum = (x + 1); for (int i = 1; i < k; i++) sum *= 10; printf("%d\n", sum - m); } }
0
#include <bits/stdc++.h> using namespace std; int main() { char input1[3], input2[3]; string h, v; int dif1, dif2, i; cin >> input1; cin >> input2; dif1 = (int)input1[0] - (int)input2[0]; dif2 = input1[1] - input2[1]; if (dif1 > 0) h = "L"; else h = "R"; if (dif2 > 0) v = "D"; else v = "U"; dif1 = abs(dif1); dif2 = abs(dif2); if (dif1 > dif2) { cout << dif1 << endl; for (i = 0; i < dif2; i++) cout << h << v << endl; while (i++ < dif1) cout << h << endl; } else { cout << dif2 << endl; for (i = 0; i < dif1; i++) cout << h + v << endl; while (i++ < dif2) cout << v << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long INF = 2000000005; const long long MOD = 1000000007; struct Edge { int a, b, d; Edge(int p1, int p2, int p3) : a(p1), b(p2), d(p3){}; friend bool operator<(Edge e1, Edge e2) { return e1.d < e2.d; } }; int N, M, K, W; int Cost, Par[1005]; string B[1005][15]; vector<Edge> E; vector<int> G[1005]; int find(int a) { if (Par[a] == a) return a; return Par[a] = find(Par[a]); } void join(int a, int b, int d) { if (find(a) == find(b)) return; Cost += d; G[a].push_back(b); G[b].push_back(a); Par[find(a)] = find(b); } void dfs(int n, int p) { cout << n << " " << p << "\n"; for (int v : G[n]) if (v != p) dfs(v, n); } int main() { cin >> N >> M >> K >> W; for (int k = (1); k <= (K); k++) for (int i = (0); i <= (N - 1); i++) cin >> B[k][i]; for (int p = (1); p <= (K); p++) for (int q = (1); q <= (K); q++) { int d = 0; for (int i = (0); i <= (N - 1); i++) for (int j = (0); j <= (M - 1); j++) if (B[p][i][j] != B[q][i][j]) d++; E.push_back(Edge(p, q, d * W)); } for (int i = (1); i <= (K); i++) E.push_back(Edge(i, 0, N * M)); sort(E.begin(), E.end()); for (int i = (1); i <= (K); i++) Par[i] = i; for (Edge e : E) join(e.a, e.b, e.d); cout << Cost << "\n"; for (int v : G[0]) dfs(v, 0); }
5
#include <bits/stdc++.h> int t, n, ans = 0, a[150005]; int main() { int i, Min; scanf("%d", &t); while (t--) { scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]); ans = 0; Min = a[n]; for (i = n - 1; i; i--) { if (a[i] > Min) ans++; if (a[i] < Min) Min = a[i]; } printf("%d\n", ans); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int a[1000020]; int n; int main() { scanf("%d", &n); int m = 0, cur = 0; for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); a[x]++; if (a[x] > m) m = a[x], cur = x; } printf("%d\n", cur); return 0; }
1
#include <bits/stdc++.h> int comparetor(const void* a, const void* b); int main(void) { int n; scanf("%d", &n); char str[n]; scanf("%s", str); int count_n = 0, count_z = 0; for (int i = 0; i < n; i++) { if (str[i] == 'n') count_n++; if (str[i] == 'z') count_z++; } while (count_n--) printf("%d ", 1); while (count_z--) printf("%d ", 0); return 0; }
0
#include <bits/stdc++.h> using namespace std; struct node { double x, y; } p[3007]; int n; int cmp(node a, node b) { if (a.x != b.x) return a.x < b.x; else return a.y < b.y; } bool bsearch(node t) { int l = 0, r = n; while (l < r) { int m = (l + r) / 2; if (p[m].x == t.x && p[m].y == t.y) return true; else if (p[m].x > t.x) r = m - 1; else if (p[m].x == t.x && p[m].y > t.y) r = m - 1; else if (p[m].x == t.x && p[m].y < t.y) l = m + 1; else if (p[m].x < t.x) l = m + 1; } return false; } int main() { int i, j; node t; while (scanf("%d", &n) != EOF) { int ct = 0; for (i = 0; i < n; ++i) scanf("%lf%lf", &p[i].x, &p[i].y); sort(p, p + n, cmp); for (i = 0; i < n - 2; ++i) { int pos = i + 2; for (j = i + 1; j < n; ++j) { t.x = 2 * p[j].x - p[i].x; t.y = 2 * p[j].y - p[i].y; for (int k = pos; k < n; ++k) { if (t.x >= p[k].x) { if (t.x == p[k].x) { if (t.y == p[k].y) { ct++; pos = k; break; } else if (t.y < p[k].y) { pos = k - 1; break; } } } else { pos = k; break; } } } } printf("%d\n", ct); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { char a[100001]; char m[100001]; cin >> a; cin >> m; int len = strlen(a); int len1 = strlen(m); sort(m, m + len1); int i = 0; int j = len - 1; i = 0; j = len1 - 1; while (i < j) { char temp = m[i]; m[i] = m[j]; m[j] = temp; i++; j--; } j = 0; for (int i = 0; a[i] != '\0'; i++) { if (a[i] < m[j] && j < len1) { a[i] = m[j]; j++; } } cout << a; }
1
#include <bits/stdc++.h> using namespace std; int a[56789][5] = {0}; int main() { int m, n, x, sum(0); cin >> m >> n; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) scanf("%d", &a[i][j]); for (int i = 0; i < m; i++) { sum += a[i][0]; a[i][0] = sum; } int s = 0; for (int i = 0; i < n; i++) { s += a[0][i]; a[0][i] = s; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (a[i - 1][j] <= a[i][j - 1]) a[i][j] += a[i][j - 1]; else a[i][j] += a[i - 1][j]; } } for (int i = 0; i < m; i++) printf("%d ", a[i][n - 1]); return 0; }
2
#include <bits/stdc++.h> using namespace std; template <typename T> struct Pt { T x, y; void scan() { cin >> x >> y; } }; template <typename T> inline T sqr(const T& a) { return a * a; } template <typename T> inline int sign(const T& a) { return a < 0 ? -1 : a > 0; } void task(); int main() { ios_base::sync_with_stdio(0); int tests; cin >> tests; for (int i = 0; i < tests; ++i) task(); return 0; } int m, k; int a[int(1e5) + 10]; int t[int(1e5) + 10]; int r[int(1e5) + 10]; int ans[int(1e5) + 10]; int is_bad[int(1e5) + 10]; int eat_before_first[int(1e5) + 10]; int use[int(1e5) + 10]; void calc_eat_before_first(int id) { for (int i = 0; i < id; ++i) ++eat_before_first[t[i]]; } void task() { cin >> m >> k; --m; for (int i = 1; i <= k; ++i) cin >> a[i]; for (int i = 0; i < m; ++i) cin >> t[i] >> r[i]; fill(ans, ans + k + 5, 0); fill(is_bad, is_bad + k + 5, 0); fill(eat_before_first, eat_before_first + k + 5, 0); fill(use, use + k + 5, 0); int was_dissatisfied = 0; int first_dessatisfied_id = -1; for (int i = 0; i < m; ++i) { was_dissatisfied |= r[i]; if (was_dissatisfied) is_bad[t[i]] = 1; if (r[i] && first_dessatisfied_id == -1) first_dessatisfied_id = i; } if (first_dessatisfied_id == -1) { calc_eat_before_first(m); int sum = 0; for (int i = 1; i <= k; ++i) sum += eat_before_first[i]; for (int i = 1; i <= k; ++i) if (a[i] <= m - sum + eat_before_first[i]) ans[i] = 1; } else { calc_eat_before_first(first_dessatisfied_id); int the_best_id = -1; int sum = 0; for (int i = 1; i <= k; ++i) sum += eat_before_first[i]; for (int i = 1; i <= k; ++i) if (!is_bad[i]) { (void(1)); if (the_best_id == -1 || a[i] - eat_before_first[i] < a[the_best_id] - eat_before_first[the_best_id]) { the_best_id = i; } if (a[i] - eat_before_first[i] + sum <= first_dessatisfied_id) ans[i] = 1; } a[the_best_id] -= eat_before_first[the_best_id]; for (int i = 0; a[the_best_id]; ++i) if (t[i] == 0) { t[i] = the_best_id; --a[the_best_id]; } for (int i = 0; i < m; ++i) ++use[t[i]]; for (int i = 1; i <= k; ++i) { if (a[i] - use[i] - use[0] <= 0) ans[i] = 1; } (void(1)); } for (int i = 1; i <= k; ++i) cout << (ans[i] ? 'Y' : 'N'); cout << "\n"; }
6
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; class Result { public: map<int, long long int> s_path; map<int, int> parent; void print() { cout << "\n"; cout << "shortest path" << "\n"; for (auto ele : s_path) { cout << ele.first << ":" << ele.second << ", "; } cout << "\n"; cout << "\n"; cout << "parent" << "\n"; for (auto ele : parent) { cout << ele.first << ":" << ele.second << ", "; } cout << "\n"; } }; class WeightedGraph { public: map<pair<int, int>, int> weight; vector<vector<int>> adj = vector<vector<int>>(10000); void add_edge(int u, int v, int w) { adj[u].push_back(v); adj[v].push_back(u); weight[make_pair(min(u, v), max(u, v))] = w; } }; void dijkstra(WeightedGraph &g, Result &r, int s) { set<pair<int, int>> pq; set<pair<int, int>>::iterator set_iter; r.s_path[s] = 0; r.parent[s] = 0; pq.insert(make_pair(0, s)); while (!pq.empty()) { int u = pq.begin()->second; int weight = pq.begin()->first; pq.erase(pq.begin()); for (auto v : g.adj[u]) { int new_weight; if (r.s_path.find(v) == r.s_path.end()) { new_weight = weight + g.weight[make_pair(min(u, v), max(u, v))]; r.s_path[v] = new_weight; r.parent[v] = u; pq.insert(make_pair(new_weight, v)); } else { new_weight = weight + g.weight[make_pair(min(u, v), max(u, v))]; if (new_weight < r.s_path[v]) { pq.erase(pq.find(make_pair(r.s_path[v], v))); pq.insert(make_pair(new_weight, v)); r.s_path[v] = new_weight; r.parent[v] = u; } } } } } int main() { long long int n, m, k; int u, v, w; WeightedGraph g; cin >> n >> m >> k; vector<pair<int, int>> quary; vector<vector<int>> paths(n + 1, vector<int>(n + 1, INF)); while (m--) { cin >> u >> v >> w; g.add_edge(u, v, w); } for (int u = 1; u <= n; u++) { Result r; dijkstra(g, r, u); for (auto ele : r.s_path) { paths[u][ele.first] = ele.second; paths[ele.first][u] = ele.second; } } while (k--) { cin >> u >> v; quary.push_back(make_pair(u, v)); } int ans = INF; for (int v = 1; v <= n; v++) { for (auto to : g.adj[v]) { int cur = 0; for (auto ele : quary) { auto a = ele.first; auto b = ele.second; cur += min({paths[a][b], paths[a][v] + paths[to][b], paths[a][to] + paths[v][b]}); } ans = min(ans, cur); } } cout << ans << "\n"; return 0; }
6
#include <bits/stdc++.h> char S[222222]; unsigned Q[222222], Qi; int main() { unsigned n, a, b, i, j, k; scanf("%u%u%u%u%s", &n, &a, &b, &k, S); for (i = -1, j = 0; ++i < n;) { if (S[i] == '1') j = 0; else if (++j == b) { Q[Qi++] = i; j = 0; } } printf("%u\n", Qi - (--a)); for (i = a - 1; ++i < Qi;) printf("%u%c", Q[i] + 1, i + 1 != Qi ? ' ' : '\n'); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int size = 101; vector<int> g[size]; struct compare { bool operator()(const int& l, const int& r) { return l > r; } }; class Solution { public: void solve(std::istream& in, std::ostream& out) { int m, t, r; in >> m >> t >> r; vector<int> nums; for (int i = 0; i < m; i++) { int c; in >> c; nums.push_back(c); } priority_queue<int, std::vector<int>, compare> d; set<int> mmt; int ans = 0; for (int i = 0; i < m; i++) { int cs = nums[i] - 1; while (!d.empty()) { if (d.top() < nums[i]) { d.pop(); } else break; } while (d.size() != r) { int s = cs + t; if (s < nums[i]) { out << -1; return; } d.push(s); cs--; ans++; } } out << ans; } }; void solve(std::istream& in, std::ostream& out) { out << std::setprecision(12); Solution solution; solution.solve(in, out); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); istream& in = cin; ostream& out = cout; solve(in, out); int end; cin >> end; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int inf = 2 * 1e9; int TLeft, TRight; struct node_t; typedef node_t* node; struct node_t { int mini, add; node l, r; node_t(int a = 0) : mini(a), add(0), l(NULL), r(NULL) {} }; int get_mini(node v) { return v ? v->mini : 0; } int get_add(node v) { return v ? v->add : 0; } void add(node v, int tl, int tr, int l, int r, int c = 1) { if (l > r) return; if (tl == l && tr == r) { v->add += c; v->mini += c; return; } int tm = (tl + tr) / 2; if (!v->l) v->l = new node_t(); if (!v->r) v->r = new node_t(); add(v->l, tl, tm, l, min(tm, r), c); add(v->r, tm + 1, tr, max(tm + 1, l), r, c); v->mini = min(v->l->mini, v->r->mini) + v->add; } int FindFirstZero(node v, int tl, int tr) { if (v->mini != 0) return -1; int tm = (tl + tr) / 2; if (!v->l) return tl; else { if (v->l->mini == 0) return FindFirstZero(v->l, tl, tm); } if (!v->r) return tm + 1; else { return FindFirstZero(v->r, tm + 1, tr); } } node CreateTree(int l, int r) { TLeft = l, TRight = r; return new node_t(); } void plus1(node v, int l, int r) { add(v, TLeft, TRight, l, r, 1); } void minus1(node v, int l, int r) { add(v, TLeft, TRight, l, r, -1); } struct item { int main_coord; int minor_l, minor_r; int type; item(int a, int b, int c, int d) : main_coord(a), minor_l(b), minor_r(c), type(d) {} }; bool compare(item a, item b) { if (a.main_coord != b.main_coord) return a.main_coord < b.main_coord; else return a.type < b.type; } int n, m, k; vector<pair<int, int> > input; int find_min_x_coord(int t) { node root = CreateTree(0, m - 1); vector<item> scanline; for (int i = 0; i < input.size(); i++) { scanline.push_back(item(max(-1, input.at(i).first - t - 1), max(0, input.at(i).second - t), min(m - 1, input.at(i).second + t), -1)); scanline.push_back(item(min(n, input.at(i).first + t + 1), max(0, input.at(i).second - t), min(m - 1, input.at(i).second + t), 1)); } sort(scanline.begin(), scanline.end(), compare); int j = 0; while (scanline.at(j).main_coord == -1 && scanline.at(j).type == -1) { int l_ = scanline.at(j).minor_l; int r_ = scanline.at(j).minor_r; plus1(root, l_, r_); j++; } if (FindFirstZero(root, TLeft, TRight) != -1) return 0; for (int i = j; i < scanline.size(); i++) { int l_ = scanline.at(i).minor_l; int r_ = scanline.at(i).minor_r; if (scanline.at(i).type == -1) plus1(root, l_, r_); else minus1(root, l_, r_); if (FindFirstZero(root, TLeft, TRight) != -1) { if (scanline.at(i).main_coord != n) return scanline.at(i).main_coord; else return -1; } } return -1; } int find_min_y_coord(int t) { node root = CreateTree(0, n - 1); vector<item> scanline; for (int i = 0; i < input.size(); i++) { scanline.push_back(item(max(-1, input.at(i).second - t - 1), max(0, input.at(i).first - t), min(n - 1, input.at(i).first + t), -1)); scanline.push_back(item(min(m, input.at(i).second + t + 1), max(0, input.at(i).first - t), min(n - 1, input.at(i).first + t), 1)); } sort(scanline.begin(), scanline.end(), compare); int j = 0; while (scanline.at(j).main_coord == -1 && scanline.at(j).type == -1) { int l_ = scanline.at(j).minor_l; int r_ = scanline.at(j).minor_r; plus1(root, l_, r_); j++; } if (FindFirstZero(root, TLeft, TRight) != -1) return 0; for (int i = j; i < scanline.size(); i++) { int l_ = scanline.at(i).minor_l; int r_ = scanline.at(i).minor_r; if (scanline.at(i).type == -1) plus1(root, l_, r_); else minus1(root, l_, r_); if (FindFirstZero(root, TLeft, TRight) != -1) { if (scanline.at(i).main_coord != m) return scanline.at(i).main_coord; else return -1; } } return -1; } bool check(int t) { int x0 = find_min_x_coord(t); int y0 = find_min_y_coord(t); if (x0 == -1) return 1; int put_x, put_y; put_x = min(x0 + t, n - 1); put_y = min(y0 + t, m - 1); input.push_back(make_pair(put_x, put_y)); x0 = find_min_x_coord(t); y0 = find_min_y_coord(t); input.pop_back(); if (x0 == -1) return 1; else return 0; } int main() { scanf("%d%d%d", &n, &m, &k); int a, b; for (int i = 0; i < k; i++) { scanf("%d%d", &a, &b); a--; b--; input.push_back(make_pair(a, b)); } int l = 0, r = max(n, m); while (r - l > 1) { int m = (l + r) / 2; if (check(m)) r = m; else l = m + 1; } if (check(l)) printf("%d", l); else printf("%d", r); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int a1, a2, k1, k2, n; int min1, minp1, max1, maxp1; cin >> a1 >> a2 >> k1 >> k2 >> n; if (min(k1, k2) == k1) { min1 = k1; minp1 = a1; max1 = k2; maxp1 = a2; } else { min1 = k2; minp1 = a2; max1 = k1; maxp1 = a1; } if (n <= (max1 - 1) * maxp1 + (min1 - 1) * (minp1)) { if (n > min1 * minp1) cout << "0 " << (n - minp1 * min1) / max1 + minp1; else cout << "0 " << n / min1; } else { cout << n - ((max1 - 1) * maxp1) - ((min1 - 1) * minp1) << " " << (n - minp1 * min1) / max1 + minp1; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, ans = 1; cin >> a >> b; if ((b - a) >= 10) cout << 0 << endl; else { long long int i; for (i = a + 1; i <= b; i++) { ans = (1LL * ans * (i % 10)) % 10; } cout << ans << endl; } }
1
#include <bits/stdc++.h> int main() { int t, j; char s[1001]; scanf("%d", &t); for (int i = 0; i < t; i++) { scanf("%s", s); for (j = 0; s[j]; j++) ; if (s[j - 1] == 'o' && s[j - 2] == 'p') printf("FILIPINO\n"); else if (s[j - 1] == 'a' && s[j - 2] == 'd' && s[j - 3] == 'i' && s[j - 4] == 'n' && s[j - 5] == 'm') printf("KOREAN\n"); else printf("JAPANESE\n"); } }
0
#include <bits/stdc++.h> using namespace std; long long int a[101][101]; long long int n, m, q; void display() { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) cout << a[i][j] << " "; cout << endl; } } void solve(int j) { if (j >= q) return; int t, r, c, x, i; cin >> t; if (t == 1) { cin >> r; x = a[r - 1][0]; for (i = 0; i < m - 1; i++) a[r - 1][i] = a[r - 1][i + 1]; a[r - 1][m - 1] = x; solve(j + 1); x = a[r - 1][m - 1]; for (i = m - 1; i > 0; i--) a[r - 1][i] = a[r - 1][i - 1]; a[r - 1][0] = x; } else if (t == 2) { cin >> c; x = a[0][c - 1]; for (i = 0; i < n - 1; i++) a[i][c - 1] = a[i + 1][c - 1]; a[n - 1][c - 1] = x; solve(j + 1); x = a[n - 1][c - 1]; for (i = n - 1; i > 0; i--) a[i][c - 1] = a[i - 1][c - 1]; a[0][c - 1] = x; } else if (t == 3) { cin >> r >> c >> x; a[r - 1][c - 1] = x; solve(j + 1); } } int main() { ios::sync_with_stdio(false); int i, j; cin >> n >> m >> q; memset(a, 0, sizeof(a)); solve(0); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) cout << a[i][j] << " "; cout << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, t, arr[100000]; cin >> n >> t; for (int f = 0; f + 1 < n; f++) cin >> arr[f]; int i = 0; while (i + 1 < t) { i += arr[i]; } if (i + 1 == t) cout << "YES\n"; else cout << "NO\n"; }
1
#include <bits/stdc++.h> using namespace std; int n, tot; char f[26], g[27], x; multiset<string> s, h[20]; int chk(string &u) { auto p = s.lower_bound(u); if (p == s.end()) return 0; auto w = *p; w.resize(u.size()); return w == u; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { string u; cin >> u; s.insert(u); h[u.size()].insert(u); tot += u.size(); } for (int i = 0; i <= 25; ++i) f[i] = g[i] = i + 'a'; scanf(" %c", &x); g[26] = x; sort(g, g + 27); tot /= n / 2; ++tot; for (int i = 0; i < n / 2; ++i) { string u; u.push_back('a'); for (auto &c : f) { u[0] = c; if (chk(u)) break; } while (u.size() != tot) { u.push_back('a'); for (auto &c : g) { u.back() = c; if (c == x) { auto w = u; w.pop_back(); auto p = *s.lower_bound(w); if (p == w) { s.erase(s.find(w)); h[w.size()].erase(h[w.size()].find(w)); int len = tot - u.size(); w = *h[len].begin(); u += w; s.erase(s.find(w)); h[len].erase(h[len].begin()); break; } } else if (chk(u)) break; } } cout << u << endl; } }
6
#include <bits/stdc++.h> using namespace std; struct O { int id, t, d, p; }; struct R { int p; int id; R *prev; bool operator<(const R &b) const { return p < b.p; } }; int main(int argc, char **argv) { int n; cin >> n; vector<O> oo(n); for (int i = 0; i < n; i++) { O &o = oo[i]; o.id = i; cin >> o.t >> o.d >> o.p; } sort(oo.begin(), oo.end(), [](const O &a, const O &b) { return a.d < b.d; }); int mxt = oo[n - 1].d; vector<vector<R>> dp(mxt + 1, vector<R>(n + 1, R{0, -1, nullptr})); for (int t = 1; t <= mxt; t++) { for (int oi = 1; oi <= n; oi++) { const O &o = oo[oi - 1]; dp[t][oi] = max(dp[t][oi - 1], dp[t - 1][oi]); if (t < o.d && t >= o.t && dp[t - o.t][oi - 1].p + o.p > dp[t][oi - 1].p) dp[t][oi] = R{dp[t - o.t][oi - 1].p + o.p, o.id, &dp[t - o.t][oi - 1]}; } } cout << dp[mxt][n].p << endl; vector<int> ids; if (dp[mxt][n].id >= 0) { ids.push_back(dp[mxt][n].id); for (R *nxt = dp[mxt][n].prev; nxt->id >= 0; nxt = nxt->prev) ids.push_back(nxt->id); } reverse(ids.begin(), ids.end()); cout << ids.size() << endl; for (int i : ids) cout << i + 1 << ' '; cout << endl; return 0; }
6
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <typename T, typename TT> inline ostream &operator<<(ostream &os, const pair<T, TT> &t) { return os << t.first << " " << t.second; } template <typename T> inline ostream &operator<<(ostream &os, const vector<T> &t) { for (auto i : t) os << i << " "; return os; } template <typename T> inline istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; } template <typename T1, typename T2> inline istream &operator>>(istream &is, pair<T1, T2> &t) { is >> t.first >> t.second; return is; } const long long mod = 1e9 + 7; void solve() { int n; cin >> n; vector<string> s(n); vector<long long> col(n), row(n); cin >> s; long long ans = 0; for (int i = 0; i < n; i++) { int j, use = 0; for (j = n - 1; j > i; j--) { int cur = s[i][j] - '0' + use + col[j]; if (cur & 1) { use++; ans++; col[j]++; } } int use1 = 0; for (j = n - 1; j > i; j--) { int cur = s[j][i] - '0' + row[j] + use1; if (cur & 1) { use1++; ans++; row[j]++; } } ans += (use + use1 + s[i][i] - '0' + row[i] + col[i]) % 2; } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ; int T = 1; while (T--) { solve(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int inf = 2e9; int n, a[10], f[5][1010][4100], bit[5][5]; char s[5][1010]; void upd(int &x, int v) { x = min(x, v); } int id(int i, int j) { int now = (i - 1) * 4 + (j - 1); return 1 << now; } int main() { scanf("%d", &n); for (int i = 1; i <= 4; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= 4; i++) { scanf("%s", s[i] + 1); } for (int i = 1; i < 4; i++) { for (int j = 1; j + i - 1 <= 4; j++) { for (int x = 1; x <= i; x++) { for (int y = 0; y < i; y++) { bit[i][j] += id(x, y + j); } } bit[i][j] ^= ((1 << 12) - 1); } } for (int i = 0; i <= 4; i++) { for (int j = 0; j <= n; j++) { for (int mask = 0; mask < (1 << 12); mask++) { f[i][j][mask] = inf; } } } f[4][0][0] = 0; for (int i = 1; i <= n; i++) { int now = 0; for (int j = 1; j <= 4; j++) { if (s[j][i] == '*') { now |= 1 << (j - 1); } } for (int mask = 0; mask < (1 << 8); mask++) { upd(f[0][i][(mask << 4) | now], f[4][i - 1][mask]); } for (int j = 1; j <= 4; j++) { for (int mask = 0; mask < (1 << 12); mask++) { int v = f[j - 1][i][mask]; if (v >= inf) { continue; } upd(f[j][i][mask], v); for (int k = 1; k < 4 && k <= j; k++) { if (mask == 8) { } upd(f[j][i][mask & bit[k][j - k + 1]], v + a[k]); } } } for (int j = 1; j <= 4 && j <= i; j++) { upd(f[4][i][0], f[4][i - j][0] + a[4]); } } cout << f[4][n][0] << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int a, b; int conv(int a, int x) { int ouch = 0; vector<int> A; while (a) { A.push_back(a % x); a /= x; } reverse(A.begin(), A.end()); for (int i = 0; i < (int)A.size() - 1; i++) { ouch += A[i]; ouch *= 10; } ouch += A[A.size() - 1]; return ouch; } int unconv(int a, int x) { int ouch = 0, y = 1; while (a) { ouch += (a % 10) * y; a /= 10; y *= x; } return ouch; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> a >> b; int maxi = 0, sa = a, sb = b, ans = 0; while (sa) { maxi = max(maxi, sa % 10); sa /= 10; } while (sb) { maxi = max(maxi, sb % 10); sb /= 10; } for (int i = maxi + 1; i <= 1000; i++) { int x = conv(unconv(a, i) + unconv(b, i), i), c = 0; while (x) { x /= 10; c++; } ans = max(ans, c); } cout << ans << "\n"; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(2 * n); for (int& i : a) cin >> i; vector<int> lf(n + 1); for (int i = 1; i <= n; i++) lf[i] = lf[i - 1] + (a[i - 1] == 1 ? +1 : -1); vector<int> rt(n + 1); map<int, int> mp; mp[0] = n; for (int i = n - 1; ~i; i--) { rt[i] = rt[i + 1] + (a[n + i] == 1 ? +1 : -1); mp[rt[i]] = i; } int ans = INT_MAX; for (int i = 0; i <= n; i++) { if (mp.find(-lf[i]) != mp.end()) ans = min(ans, mp[-lf[i]] + n - i); } cout << (ans == INT_MAX ? 2 * n : ans) << endl; } }
4
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = (int)2e5 + 5; const double pi = 3.14159265359; int cnt[26], res[26]; void solve() { string s; cin >> s; int n; cin >> n; bool ok = 0; for (int i = 0; i < n; ++i) { string t; cin >> t; if (t == s) { ok = 1; } cnt[t[0] - 'a']++, res[t[1] - 'a']++; } if (ok) { cout << "YES"; return; } if ((cnt[s[1] - 'a'] && res[s[0] - 'a'])) cout << "YES"; else cout << "NO"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 105; const int MAXM = 2005; int oldpath[MAXM], newpath[MAXM]; bool mp[MAXN][MAXN], newmp[MAXN][MAXN]; int n, m; void AddEdge(bool mp[MAXN][MAXN], int a, int b) { mp[a][b] = mp[b][a] = true; return; } void DelEdge(bool mp[MAXN][MAXN], int a, int b) { mp[a][b] = mp[b][a] = false; return; } bool DFS(int nowp, int k, int rest) { newpath[k] = nowp; if (rest == 0) return true; for (int i = 1; i <= n; i++) if (newmp[nowp][i]) { DelEdge(newmp, nowp, i); if (DFS(i, k + 1, rest - 1)) return true; AddEdge(newmp, nowp, i); } return false; } int main() { bool found = false; scanf("%d%d", &n, &m); m++; for (int i = 0; i < m; i++) { scanf("%d", &oldpath[i]); newpath[i] = oldpath[i]; if (i > 0) AddEdge(mp, oldpath[i], oldpath[i - 1]); } for (int i = m - 2; i >= 0; i--) { int index; AddEdge(newmp, oldpath[i], oldpath[i + 1]); for (int j = oldpath[i + 1] + 1; j <= n; j++) if (newmp[oldpath[i]][j]) { DelEdge(newmp, oldpath[i], j); if (DFS(j, i + 1, m - i - 2)) { found = true; break; } AddEdge(newmp, oldpath[i], j); } if (found) break; } if (found) { for (int i = 0; i < m; i++) printf("%d ", newpath[i]); } else printf("No solution\n"); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; if (x1 == x2) { int x = x1 + abs(y1 - y2); cout << x << ' ' << y1 << ' ' << x << ' ' << y2; return 0; } if (y1 == y2) { int y = y1 + abs(x1 - x2); cout << x1 << ' ' << y << ' ' << x2 << ' ' << y; return 0; } if (abs(x1 - x2) == abs(y1 - y2)) { cout << x1 << ' ' << y2 << ' ' << x2 << ' ' << y1; return 0; } cout << -1; }
2
#include <bits/stdc++.h> using namespace std; const int inf = ~0U >> 1; const long long INF = ~0ULL >> 1; template <class T> inline void read(T &n) { char c; for (c = getchar(); !(c >= '0' && c <= '9'); c = getchar()) ; n = c - '0'; for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - '0'; } int Pow(int base, int n, int mo) { if (n == 0) return 1; if (n == 1) return base % mo; int tmp = Pow(base, n >> 1, mo); tmp = (long long)tmp * tmp % mo; if (n & 1) tmp = (long long)tmp * base % mo; return tmp; } const int dx[4] = {-1, 1, 0, 0}; const int dy[4] = {0, 0, -1, 1}; int n, cntx, cnty, dir[2000], step[2000], v[2500][2500], vis[2500][2500], invalid[2500][2500]; long long ans, hx[3000], hy[3000]; queue<pair<int, int> > Q; void add(long long x, long long y) { hx[++cntx] = x; hy[++cnty] = y; } int main() { scanf("%d", &n); long long x = (10000000001ll + 1) / 2; long long y = x; add(x, y); for (int i = (1); i <= (n); ++i) { char buf[5]; scanf("%s", buf); if (buf[0] == 'L') dir[i] = 0; else if (buf[0] == 'R') dir[i] = 1; else if (buf[0] == 'U') dir[i] = 2; else dir[i] = 3; scanf("%d", &step[i]); x += step[i] * dx[dir[i]]; y += step[i] * dy[dir[i]]; add(x, y); } sort(hx + 1, hx + cntx + 1); sort(hy + 1, hy + cnty + 1); cntx = unique(hx + 1, hx + cntx + 1) - hx - 1; cnty = unique(hy + 1, hy + cnty + 1) - hy - 1; x = (10000000001ll + 1) / 2; y = x; int curx = lower_bound(hx + 1, hx + cntx + 1, x) - hx; int cury = lower_bound(hy + 1, hy + cnty + 1, y) - hy; v[curx * 2 - 1][cury * 2 - 1] = 1; for (int i = (1); i <= (n); ++i) { x += step[i] * dx[dir[i]]; y += step[i] * dy[dir[i]]; while (hx[curx] != x || hy[cury] != y) { if (dir[i] == 0 && hx[curx] - hx[curx - 1] > 1) v[(curx - 1) * 2][cury * 2 - 1] = 1; else if (dir[i] == 1 && hx[curx + 1] - hx[curx] > 1) v[curx * 2][cury * 2 - 1] = 1; else if (dir[i] == 2 && hy[cury] - hy[cury - 1] > 1) v[curx * 2 - 1][(cury - 1) * 2] = 1; else if (dir[i] == 3 && hy[cury + 1] - hy[cury] > 1) v[curx * 2 - 1][cury * 2] = 1; curx += dx[dir[i]]; cury += dy[dir[i]]; v[curx * 2 - 1][cury * 2 - 1] = 1; } } for (int i = (1); i <= (cntx - 1); ++i) if (hx[i + 1] - hx[i] <= 1) { for (int j = (1); j <= (2 * cnty); ++j) invalid[i * 2][j] = 1; } for (int i = (1); i <= (cnty - 1); ++i) if (hy[i + 1] - hy[i] <= 1) { for (int j = (1); j <= (2 * cntx); ++j) invalid[j][i * 2] = 1; } vis[0][0] = 1; Q.push((make_pair(0, 0))); while (Q.size()) { pair<int, int> u = Q.front(); Q.pop(); if (invalid[u.first][u.second]) continue; for (int k = (0); k <= (3); ++k) { int nx = u.first + dx[k]; int ny = u.second + dy[k]; if (!(nx <= 2 * cntx && ny <= 2 * cnty && nx >= 0 && ny >= 0)) continue; if (invalid[nx][ny]) nx += dx[k], ny += dy[k]; if (nx <= 2 * cntx && ny <= 2 * cnty && nx >= 0 && ny >= 0 && !vis[nx][ny] && !v[nx][ny]) { vis[nx][ny] = 1; Q.push((make_pair(nx, ny))); } } } for (int i = (1); i <= (cntx); ++i) for (int j = (1); j <= (cnty); ++j) { if (!vis[i * 2 - 1][j * 2 - 1]) ans++; if (!vis[i * 2 - 1][j * 2] && j < cnty && !invalid[i * 2 - 1][j * 2]) ans += hy[j + 1] - hy[j] - 1; if (!vis[i * 2][j * 2 - 1] && i < cntx && !invalid[i * 2][j * 2 - 1]) ans += hx[i + 1] - hx[i] - 1; if (!vis[i * 2][j * 2] && i < cntx && j < cnty && !invalid[i * 2][j * 2]) ans += (hy[j + 1] - hy[j] - 1) * (hx[i + 1] - hx[i] - 1); } printf("%lld\n", ans); return 0; }
7
#include <bits/stdc++.h> using namespace std; const long double pi = 3.14159265358979323846; const int inf = (int)1e9; const long double eps = 1e-9; const int N = 2e5 + 5; int n, m, s, d; int a[N]; int main() { scanf("%d %d %d %d", &n, &m, &s, &d); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } sort(a, a + n); if (a[0] - 1 < s) { cout << "IMPOSSIBLE"; return 0; } int d1 = d - 1; for (int i = d1; i < n; ++i) { if (a[i - d1] == a[i] - d1) { cout << "IMPOSSIBLE"; return 0; } } vector<int> res; vector<char> moves; int lastCan = a[0] - 1; res.push_back(lastCan); moves.push_back('R'); for (int i = 1; i < n; ++i) { if (a[i] - a[i - 1] - 2 < s) { continue; } int jump = a[i - 1] + 1 - lastCan; if (jump > d) { cout << "IMPOSSIBLE"; return 0; } res.push_back(jump); moves.push_back('J'); lastCan = a[i] - 1; res.push_back((a[i] - 1) - (a[i - 1] + 1)); moves.push_back('R'); } int jump = a[n - 1] + 1 - lastCan; if (jump > d) { cout << "IMPOSSIBLE"; return 0; } res.push_back(jump); moves.push_back('J'); if (m - (a[n - 1] + 1) > 0) { res.push_back(m - (a[n - 1] + 1)); moves.push_back('R'); } for (int i = 0; i < res.size(); ++i) { if (moves[i] == 'R') { cout << "RUN " << res[i] << "\n"; } else { cout << "JUMP " << res[i] << "\n"; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int dr[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; const int dc[8] = {0, 1, 1, 1, 0, -1, -1, -1}; const double PI = acos(-1); const double EPS = 10e-9; const int e5 = 1e5 + 5; const int e6 = 1e6 + 5; map<string, string> mp; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { string a, b; cin >> a >> b; mp[b] = a; } for (int i = 0; i < m; i++) { string a, b, cmd = ""; cin >> a >> b; for (int j = 0; j < b.size() - 1; j++) cmd += b[j]; cout << a + " " + b + " #" + mp[cmd] << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, a, b; cin >> n >> a >> b; int ar[n]; ar[0] = 1; int pre = 1; for (int i = 1; i < n; i++) { if (i == 1 && b == 0) { ar[i] = 1; continue; } if (b > 0) { ar[i] = pre + 1; pre += ar[i]; b--; } else if (a > 0) { ar[i] = ar[i - 1] + 1; a--; } else { ar[i] = 1; } if (ar[i] > 50000) { cout << "-1"; return 0; } } if (a > 0 || b > 0) { cout << "-1"; return 0; } for (int i = 0; i < n; i++) { cout << ar[i] << " "; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MX = 1e5 + 5; int n; int M[9][2] = {{0, 0}, {-1, -1}, {1, 1}, {0, 1}, {1, 0}, {0, -1}, {-1, 0}, {1, -1}, {-1, 1}}; int compute(vector<int> Arr, int A0, int A1, int val) { Arr[0] = A0; Arr[1] = A1; int d = Arr[1] - Arr[0]; int ans = val; for (int i = 2; i < n; i++) { if (d + Arr[i - 1] != Arr[i]) { if (d + Arr[i - 1] != Arr[i] + 1) { if (d + Arr[i - 1] != Arr[i] - 1) { return -1; } else { Arr[i]--; ans++; } } else { Arr[i]++; ans++; } } } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int val; cin >> n; vector<int> A; for (int i = 0; i < n; cin >> val, A.push_back(val), i++) ; int ans = -1; for (int i = 0; i < 9; i++) { int num = compute(A, A[0] + M[i][0], A[1] + M[i][1], abs(M[i][0]) + abs(M[i][1])); if (num != -1) { if (ans == -1) ans = num; else ans = min(ans, num); } } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> namespace pb_ds { namespace io { const int MaxBuff = 1 << 15; const int Output = 1 << 23; char B[MaxBuff], *S = B, *T = B; char Out[Output], *iter = Out; inline void flush() { fwrite(Out, 1, iter - Out, stdout); iter = Out; } } // namespace io template <class Type> inline Type Read() { using namespace io; register char ch; register Type ans = 0; register bool neg = 0; while (ch = ((S == T) && (T = (S = B) + fread(B, 1, MaxBuff, stdin), S == T) ? 0 : *S++), (ch < '0' || ch > '9') && ch != '-') ; ch == '-' ? neg = 1 : ans = ch - '0'; while (ch = ((S == T) && (T = (S = B) + fread(B, 1, MaxBuff, stdin), S == T) ? 0 : *S++), '0' <= ch && ch <= '9') ans = ans * 10 + ch - '0'; return neg ? -ans : ans; } template <class Type> inline void Print(register Type x, register char ch = '\n') { using namespace io; if (!x) *iter++ = '0'; else { if (x < 0) *iter++ = '-', x = -x; static int s[100]; register int t = 0; while (x) s[++t] = x % 10, x /= 10; while (t) *iter++ = '0' + s[t--]; } *iter++ = ch; } } // namespace pb_ds using namespace pb_ds; using namespace std; const int maxn = 5150 + 10; const int maxg = 100 + 10; const int INF = 1000000000; int n, mm, s, t; int rel[maxg][maxg]; int wedge[maxg][maxg]; int indeg[maxn]; int edgeidx(int x, int y) { return (2 * n - x) * (x - 1) / 2 + (y - x) + n; } struct Edge { int from, to, cap, flow, cost; Edge(int from, int to, int cap, int flow, int cost) : from(from), to(to), cap(cap), flow(flow), cost(cost) {} }; template <int maxn> struct MCMF { int n, m, s, t; vector<Edge> edges; vector<int> G[maxn]; int inq[maxn]; int d[maxn]; int p[maxn]; int a[maxn]; void init(int n) { this->n = n; for (int i = 0; i < n; i++) G[i].clear(); edges.clear(); } void AddEdge(int from, int to, int cap, int cost) { edges.push_back(Edge(from, to, cap, 0, cost)); edges.push_back(Edge(to, from, 0, 0, -cost)); m = edges.size(); G[from].push_back(m - 2); G[to].push_back(m - 1); } bool BellmanFord(int s, int t, int& cost) { for (int i = 0; i < n; i++) d[i] = INF; memset(inq, 0, sizeof(inq)); d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF; queue<int> Q; Q.push(s); while (!Q.empty()) { int u = Q.front(); Q.pop(); inq[u] = 0; for (int i = 0; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; if (e.cap > e.flow && d[e.to] > d[u] + e.cost) { d[e.to] = d[u] + e.cost; p[e.to] = G[u][i]; a[e.to] = min(a[u], e.cap - e.flow); if (!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; } } } } if (d[t] == INF) return false; cost += d[t] * a[t]; int u = t; while (u != s) { edges[p[u]].flow += a[t]; edges[p[u] ^ 1].flow -= a[t]; u = edges[p[u]].from; } return true; } int Mincost(int s, int t) { int cost = 0; while (BellmanFord(s, t, cost)) ; return cost; } }; MCMF<maxn> solver; int main() { n = Read<int>(), mm = Read<int>(); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (i ^ j) rel[i][j] = 2; for (int i = 1; i <= mm; ++i) { int u = Read<int>(), v = Read<int>(); rel[u][v] = 1, rel[v][u] = 0; } s = 0; t = (n + 1) * n / 2 + n + 1; solver.init(t + 1); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (rel[i][j] == 1) indeg[j]++; } for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) { if (rel[i][j] < 2) continue; solver.AddEdge(s, edgeidx(i, j), 1, 0); solver.AddEdge(edgeidx(i, j), i, 1, 0); wedge[j][i] = solver.m - 2; solver.AddEdge(edgeidx(i, j), j, 1, 0); wedge[i][j] = solver.m - 2; } int down = 0; for (int i = 1; i <= n; i++) { down += indeg[i] * (indeg[i] - 1) / 2; for (int j = indeg[i] + 1; j < n; j++) solver.AddEdge(i, t, 1, j - 1); } down += solver.Mincost(s, t); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (rel[i][j] < 2) printf("%d", rel[i][j]); else printf("%d", solver.edges[wedge[i][j]].flow); } puts(""); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int c; int arr[100000]; int main() { int n; int k; cin >> n >> k; if (k > n) { cout << -1; return 0; } for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 0; i < n; i++) for (int j = i; j > 0; j--) if (arr[j] < arr[j - 1]) { c = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = c; } cout << arr[n - k] << " " << 0; }
0
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9; long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } long long power(long long a, long long n) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; } n >>= 1; a *= a; } return p; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; cout << n; for (int(i) = 1; (i) <= (n - 1); (i)++) { cout << ' ' << i; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 200200; const int mod = 1e9 + 7; const long long INF = 1e18; int n; long long x[N]; long long y[N]; double get_dist(double xa, double ya, double xb, double yb) { return (xa - xb) * (xa - xb) + (ya - yb) * (ya - yb); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> x[i - 1] >> y[i - 1]; double ans = 1e15; for (int i = 0; i < n; ++i) { int pre = (i - 1 + n) % n; int nxt = (i + 1) % n; double L = min(x[pre], x[nxt]), R = max(x[pre], x[nxt]); if (y[pre] == y[nxt]) { double dist = abs(y[i] - y[pre]); ans = min(ans, dist / 2.0); continue; } if (x[pre] == x[nxt]) { double dist = abs(x[i] - x[nxt]); ans = min(ans, dist / 2.0); continue; } double A = y[pre] - y[nxt]; double B = x[nxt] - x[pre]; double C = (x[pre] * y[nxt] - x[nxt] * y[pre]); double dist = (A * x[i] + B * y[i] + C) / sqrt(A * A + B * B); ans = min(ans, dist / 2.0); } cout.precision(15); cout << fixed << ans; return 0; }
5
#include <bits/stdc++.h> using namespace std; int a, b, sum, ans = 0; int main() { cin >> a >> b; sum = a + b; for (register int f = 1, t, mnp, mxp, l = 1, r; l <= sum; l = r + 1, f = 1) { t = sum / l, r = sum / t; if ((a + t) / (t + 1) > a / t || (b + t) / (t + 1) > b / t) continue; mnp = (a + t) / (t + 1) + (b + t) / (t + 1), mxp = a / t + b / t; mnp = max(mnp, l), mxp = min(mxp, r); ans += mxp - mnp + 1; } cout << ans; return 0; }
9
#include <bits/stdc++.h> using namespace std; struct info { int a, b; }; bool cmp(const info &A, const info &B) { return A.b < B.b; } int main() { int n, m; cin >> n >> m; info *A = new info[m]; for (int i = 0; i < m; i++) cin >> A[i].a >> A[i].b; sort(A, A + m, cmp); int result = 0; for (int i = m - 1; i >= 0 && n > 0; i--) { if (n >= A[i].a) { result += A[i].a * A[i].b; n -= A[i].a; } else { result += n * A[i].b; n = 0; } } cout << result; delete[] A; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m, top; int stk[207]; int a[107], b[107]; bool vis[207]; vector<int> G[207]; bool check(int f, int s) { if (b[f] <= a[s]) return true; if (a[f] >= b[s]) return true; if ((a[f] >= a[s]) && (b[f] <= b[s])) return true; if ((a[f] <= a[s]) && (b[f] >= b[s])) return true; return false; } void addedge(int e1, int e2) { int x = e1 * 2; int y = e2 * 2; G[x].push_back(y ^ 1); G[y].push_back(x ^ 1); G[x ^ 1].push_back(y); G[y ^ 1].push_back(x); } bool dfs(int x) { if (vis[x]) return 1; if (vis[x ^ 1]) return 0; vis[x] = 1; int sz = G[x].size(); stk[++top] = x; for (int i = 0; i < sz; i++) { if (!dfs(G[x][i])) return false; } return true; } int main() { scanf("%d%d", &n, &m); memset(vis, 0, sizeof(vis)); for (int i = 0; i < m; i++) { scanf("%d%d", &a[i], &b[i]); if (a[i] > b[i]) { int t = a[i]; a[i] = b[i]; b[i] = t; } } for (int i = 0; i < m; i++) { for (int j = i + 1; j < m; j++) { if (!check(i, j)) { addedge(i, j); } } } for (int i = 0; i < 2 * m; i += 2) { if ((!vis[i]) && (!vis[i ^ 1])) { top = 0; if (!dfs(i)) { while (top > 0) vis[--top] = 0; if (!dfs(i + 1)) { printf("Impossible\n"); return 0; } } } } for (int i = 0; i < 2 * m; i += 2) { if (vis[i]) printf("%c", 'o'); else printf("%c", 'i'); } printf("\n"); return 0; }
7
#include <bits/stdc++.h> using namespace std; double Fib(double x, double z); string BinDec(unsigned long long x); string StringInt(long long x); unsigned long long StringInt(string x); unsigned long long BinDec(string x); unsigned long long POWMOD(unsigned long long x, unsigned long long y, unsigned long long mod); unsigned long long POWE(long long, long long); int main() { ios::sync_with_stdio(false); cin.tie(0); int x, y, i, a[600] = {}, b, c, s = 0; cin >> x >> y; for (i = 0; i < x; i++) { cin >> a[i]; } for (i = 1; i < x; i++) { b = a[i - 1] + a[i]; if (b >= y) continue; c = a[i] + a[i + 1]; if (c < y && y > b) { a[i] += min(y - c, y - b); s += min(y - c, y - b); } b = a[i - 1] + a[i]; if (b >= y) continue; else { a[i - 1] += (y - b); s += (y - b); } } cout << s << endl; for (i = 0; i < x; i++) cout << a[i] << ' '; return 0; } double Fib(double x, double z) { int i = 0; double f = x, r = x, t; for (i = 2; i < z; i++) { t = f; f += r; r = t; } return f; } string BinDec(unsigned long long x) { string z; while (1) { z += ((x % 3) + '0'); x /= 3; if (x == 0) break; } while (1) { if (z.size() == 30) break; z += '0'; } reverse(z.begin(), z.end()); return z; } unsigned long long BinDec(string x) { long long i; unsigned long long z = 1, a = 0; for (i = x.size() - 1; i >= 0; i--) { if (x[i] == '1') a += z; if (x[i] == '2') a += (2 * z); z *= 3; } return a; } unsigned long long StringInt(string x) { unsigned long long z = 0, i; for (i = 0; i < x.size(); i++) { z *= 10; z += (x[i] - '0'); } return z; } string StringInt(long long x) { string z; while (x != 0) { z += (x % 10 + '0'); x /= 10; } reverse(z.begin(), z.end()); return z; } unsigned long long POWMOD(unsigned long long base, unsigned long long exp, unsigned long long mod) { unsigned long long res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base); res %= mod; exp /= 2; base = (base * base); base %= mod; } return res % mod; } unsigned long long POWE(long long base, long long exp) { unsigned long long res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base); exp /= 2; base = (base * base); } return res; }
1
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } int ar[5]; int res[1001][1001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); for (int i = (0); i < (3); ++i) ar[i] = i; int x1, y1, x2, y2, x3, y3; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; int area = x1 * y1 + x2 * y2 + x3 * y3; int sq = sqrt(area); if (sq * sq != area) { cout << -1 << endl; return 0; } if (max(x1, max(x2, max(x3, max(y1, max(y2, y3))))) > sq) { cout << -1 << endl; return 0; } if (x1 != sq && y1 != sq) { if (x2 != sq && y2 != sq) { if (x3 != sq && y3 != sq) { cout << -1 << endl; return 0; } swap(x1, x3); swap(y1, y3); swap(ar[0], ar[2]); } else { swap(x1, x2); swap(y1, y2); swap(ar[0], ar[1]); } } if (x1 < y1) swap(x1, y1); if (x2 > y2) swap(x2, y2); if (x3 > y3) swap(x3, y3); if (y2 == sq && y3 == sq && x2 + x3 == sq - y1) { for (int i = (0); i < (sq); ++i) for (int j = (0); j < (y1); ++j) res[i][j] = ar[0]; for (int j = (y1); j < (y1 + x2); ++j) for (int i = (0); i < (sq); ++i) res[i][j] = ar[1]; for (int j = (y1 + x2); j < (sq); ++j) for (int i = (0); i < (sq); ++i) res[i][j] = ar[2]; cout << sq << endl; for (int i = (0); i < (sq); ++i) { for (int j = (0); j < (sq); ++j) cout << (char)(res[i][j] + 'A'); cout << endl; } return 0; } if (x1 != sq) swap(x1, y1); if (x2 != sq - y1 && y2 != sq - y1) { cout << -1 << endl; return 0; } if (x3 != sq - y1 && y3 != sq - y1) { cout << -1 << endl; return 0; } if (y2 != sq - y1) swap(x2, y2); if (y3 != sq - y1) swap(x3, y3); if (x2 + x3 != sq) { cout << -1 << endl; return 0; } for (int i = (0); i < (sq); ++i) for (int j = (0); j < (y1); ++j) res[i][j] = ar[0]; for (int j = (y1); j < (sq); ++j) for (int i = (0); i < (x2); ++i) res[i][j] = ar[1]; for (int j = (y1); j < (sq); ++j) for (int i = (x2); i < (sq); ++i) res[i][j] = ar[2]; cout << sq << endl; for (int i = (0); i < (sq); ++i) { for (int j = (0); j < (sq); ++j) cout << (char)(res[i][j] + 'A'); cout << endl; } }
4
#include <bits/stdc++.h> using namespace std; map<string, int> m; char s1[25], s2[25]; int main() { int t; string a; m["void"] = 0; cin >> t; while (t--) { cin >> a; if (a == "typedef") { scanf("%s%s", s1, s2); int _y = 0; int i = 0; while (s1[i] == '&') { _y++; i++; } int len = strlen(s1); int _x = 0; int j = len - 1; while (s1[j] == '*') { _x++; j--; } s1[j + 1] = 0; string s3(&s1[i]); if (m.count(s3) != 0) { int k = m[s3]; if (k + _x - _y >= 0) { m[s2] = k + _x - _y; } else m.erase(s2); } else m.erase(s2); } else { cin >> s1; int _y = 0; int i = 0; while (s1[i] == '&') { _y++; i++; } int len = strlen(s1); int _x = 0; int j = len - 1; while (s1[j] == '*') { _x++; j--; } s1[j + 1] = 0; string s3(&s1[i]); if (m.count(s3) != 0) { int k = m[s3]; len = k + _x - _y; if (len >= 0) { printf("void"); for (int i = 0; i < len; i++) printf("*"); printf("\n"); } else printf("errtype\n"); } else printf("errtype\n"); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int numerator, denominator, number; cin >> number; if (number % 2 == 0) { numerator = number / 2; denominator = number - numerator; numerator--; denominator++; if (numerator % 2 == 0 && denominator % 2 == 0) { numerator--; denominator++; } } else { numerator = number / 2; denominator = number - numerator; } cout << numerator << " " << denominator << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; bool check(long long n, long long pos) { return (n & (1ll << pos)) >> pos; } const long long M = 1000010; struct data { long long first, second, t; data(long long a = 0, long long b = 0, long long c = 0) { first = a; second = b; t = c; } }; long long L[M], R[M]; vector<data> A; long long limit; long long mindif; vector<long long> lucky; bool compleft(data &a, data &b) { if (a.second == b.second) return a.t > b.t; else return a.second < b.second; } bool compright(data &a, data &b) { if (a.first == b.first) return a.t < b.t; else return a.first < b.first; } long long evaluate(long long a, long long b, long long c) { if (a == 1000000000000000011) return 1000000000000000011; double costcost = a, bb = b, cc = c, infinf = 1000000000000000011; if ((costcost + bb * cc) >= infinf) return 1000000000000000011; return a + b * c; } bool ispossible(long long len) { if (len == 0) return true; for (long long i = 0, j = i + len - 1; j < lucky.size(); i++, j++) { if (L[j] + R[i] <= limit && lucky[j] - lucky[i] <= mindif) return true; } return false; } int main() { ios_base::sync_with_stdio(false); long long i, j, k, n; long long DMAX = 18; for (i = 1; i <= DMAX; i++) { for (j = 0; j < (1ll << i); j++) { char a[22]; for (k = 0; k < i; k++) a[k] = check(j, k) ? '4' : '7'; a[i] = '\0'; A.push_back(data(atoll(a), atoll(a), 0)); } } mindif = 1000000000000000011; scanf("%lld%lld", &n, &limit); for (i = 0; i < n; i++) { scanf("%lld%lld", &j, &k); A.push_back(data(j, k, 1)); mindif = min(mindif, k - j); } sort((A).begin(), (A).end(), compleft); long long cost = 0, seg = A[0].t; n = 0; if (A[0].t == 0) L[n++] = 0, lucky.push_back(A[0].first); for (i = 1; i < A.size(); i++) { cost = evaluate(cost, (A[i].second - A[i - 1].second), seg); seg += A[i].t; if (A[i].t == 0) L[n++] = cost, lucky.push_back(A[i].first); } sort((A).begin(), (A).end(), compright); cost = 0, seg = A.back().t; j = n - 1; if (A.back().t == 0) R[j--] = 0; for (i = A.size() - 2; i >= 0; i--) { cost = evaluate(cost, (A[i + 1].first - A[i].first), seg); seg += A[i].t; if (A[i].t == 0) R[j--] = cost; } long long first = 0, h = n; while (first < h) { long long m = (first + h + 1) / 2; if (ispossible(m)) first = m; else h = m - 1; } printf("%lld\n", first); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> x, cost; for (int i = 0; i < n; i++) { int no; cin >> no; x.push_back(no); } sort(x.begin(), x.end()); int q; cin >> q; while (q--) { int cost, j; cin >> cost; j = lower_bound(x.begin(), x.end(), cost) - x.begin(); if (binary_search(x.begin(), x.end(), cost)) cout << upper_bound(x.begin(), x.end(), cost) - x.begin() << endl; else cout << j << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; int arr[] = {1, 5, 10, 20, 100}; long long int res = 0; for (int i = 4; i >= 0; i--) { res = res + (n / arr[i]); n = n % arr[i]; } cout << res; }
0
#include <bits/stdc++.h> using namespace std; int du[100010]; int h, t; int p1[200010]; int q1; int p2[200010]; int q2; int pp[200010]; int qq; vector<int> g[100010]; bool judge(int x, int y) { if (du[x] - 1 < h || du[y] - 1 < t) return false; int i, j; i = j = 0; qq = q1 = q2 = 0; while (i < du[x] || j < du[y]) { if (i < du[x] && g[x][i] == y) { i++; continue; } if (j < du[y] && g[y][j] == x) { j++; continue; } if (j == du[y]) p1[q1++] = g[x][i++]; else if (i == du[x]) p2[q2++] = g[y][j++]; else if (g[x][i] == g[y][j]) { pp[qq++] = g[x][i]; i++; j++; } else { if (g[x][i] < g[y][j]) p1[q1++] = g[x][i++]; else p2[q2++] = g[y][j++]; } } int sum = qq; if (q1 < h) sum -= h - q1; if (q2 < t) sum -= t - q2; if (sum < 0) return false; printf("YES\n"); printf("%d %d\n", x, y); int num = 0; for (int i = 0; i < q1; i++) { printf("%d", p1[i]); num++; if (num < h) printf(" "); else { printf("\n"); break; } } while (qq >= 1 && num < h) { printf("%d", pp[--qq]); num++; if (num < h) printf(" "); else { printf("\n"); break; } } num = 0; for (int i = 0; i < q2; i++) { printf("%d", p2[i]); num++; if (num < t) printf(" "); else { printf("\n"); break; } } while (qq >= 1 && num < t) { printf("%d", pp[--qq]); num++; if (num < t) printf(" "); else { printf("\n"); break; } } return true; } int x[100010]; int y[100010]; int main() { int n, m; scanf("%d %d %d %d", &n, &m, &h, &t); memset(du, 0, sizeof(du)); for (int i = 0; i < m; i++) { scanf("%d %d", &x[i], &y[i]); g[x[i]].push_back(y[i]); g[y[i]].push_back(x[i]); du[x[i]]++; du[y[i]]++; } for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end()); bool flag = false; for (int i = 0; i < m; i++) { if (du[x[i]] >= h + t + 2) { if (du[y[i]] >= t + 1) { judge(x[i], y[i]); return 0; } } else if (du[y[i]] >= h + t + 2) { if (du[x[i]] >= h + 1) { judge(x[i], y[i]); return 0; } } else { if (judge(x[i], y[i])) return 0; if (judge(y[i], x[i])) return 0; } } if (!flag) printf("NO\n"); return 0; }
6
#include <bits/stdc++.h> using namespace std; int m; int n; int a[100000]; struct st { int t, l, c; long long s, e; }; st stage[100000]; int main() { cin >> m; long long c = 0; for (int i = 0; i < m; i++) { cin >> stage[i].t >> stage[i].l; if (stage[i].t == 2) { cin >> stage[i].c; stage[i].s = c; c += stage[i].l * stage[i].c; stage[i].e = c - 1; } else { stage[i].c = 0; stage[i].s = c; c++; stage[i].e = c - 1; } if (stage[i].s < 100000) { if (stage[i].t == 1) a[stage[i].s] = stage[i].l; else { for (int j = stage[i].s; j <= ((stage[i].e < 100000 - 1) ? stage[i].e : 100000 - 1); j++) a[j] = a[(j - stage[i].s) % stage[i].l]; } } } cin >> n; for (int i = 0; i < n; i++) { int rez; long long x; cin >> x; x--; int l = 0; int r = m - 1; while (l < r) { int tmp = (l + r) / 2; if (stage[tmp].e < x) l = tmp + 1; else if (stage[tmp].s > x) r = tmp - 1; else { l = tmp; r = tmp; } } if (stage[l].t == 1) rez = stage[l].l; else { long long tt; tt = x - stage[l].s; tt = tt % stage[l].l; rez = a[tt]; } cout << rez << ' '; } return 0; }
4
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } map<long long, long long> ma; signed main() { long long n, t, a, b, i, j, k, x; cin >> t; while (t--) { cin >> n; a = (n - 1) / 3; if (a == 0) { cout << n << "\n"; continue; } i = 0; b = 1; long long y = i; while (true) { b *= 4; if (i + b < a) { i += b; y = i; } else { i = b - 1 + a - i; break; } } if (n % 3 == 1) { cout << i << "\n"; continue; } j = b * 2; long long len = b; b = y + 1; while (len >= 4) { x = len / 4; if (a <= b + x - 1) { } else if (a <= b + 2 * x - 1) { j += 2 * x; a -= x; } else if (a <= b + 3 * x - 1) { j += 3 * x; a -= 2 * x; } else { j += x; a -= 3 * x; } len /= 4; } if (n % 3 == 2) { cout << j << "\n"; } else { cout << (i ^ j) << "\n"; } } return 0; }
7
#include <bits/stdc++.h> using namespace std; long long int cross(pair<long long int, long long int> o, pair<long long int, long long int> a, pair<long long int, long long int> b) { return ((a.first - o.first) * (b.second - o.second) - (a.second - o.second) * (b.first - o.first)); } int n, a, b; int as = 0; void ans(int f, int s) { if (s == 0) return; as += f / s; f = f % s; swap(f, s); ans(f, s); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; while (n--) { cin >> a >> b; if (a > b) ans(a, b); else ans(b, a); cout << as << "\n"; as = 0; } return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef struct node1 { int x; int y; long long int cap; long long int flow; } edge; vector<edge> e; vector<int> Adj[210]; vector<int> Adj1[201]; vector<int> g1[210]; int isprime[100001]; int curr[210]; int a[210]; int d[210]; bool visited[210]; int source; int sink; int id = 0; stack<int> s1; void DFS1(int u, int p) { visited[u] = true; s1.push(u); for (vector<int>::iterator it = Adj1[u].begin(); it != Adj1[u].end(); it++) { if (!visited[(*it)] && (*it) != p) { DFS1((*it), u); } else if (visited[(*it)] && (*it) != p) { while (!s1.empty()) { g1[id].push_back(s1.top()); s1.pop(); } } } return; } void AddEdge(int source, int sink, long long int cap) { edge e1 = {source, sink, cap, 0LL}; edge e2 = {sink, source, 0LL, 0LL}; Adj[source].push_back(e.size()); e.push_back(e1); Adj[sink].push_back(e.size()); e.push_back(e2); } int BFS(int source, int sink, int n) { queue<int> Q1; for (int i = 0; i <= (n + 1); i++) { d[i] = -1; } Q1.push(source); d[source] = 0; while (!Q1.empty() && d[sink] < 0) { int x = Q1.front(); Q1.pop(); for (vector<int>::iterator it = Adj[x].begin(); it != Adj[x].end(); it++) { int id = (*it); int y = e[id].y; if (d[y] < 0 && e[id].flow < e[id].cap) { Q1.push(y); d[y] = d[x] + 1; } } } if (d[sink] != -1) { return 1; } else { return 0; } } long long int DFS(int x, long long int flow) { if (!flow) { return 0LL; } if (x == sink) { return flow; } for (; curr[x] < (int)Adj[x].size(); curr[x]++) { int id = Adj[x][curr[x]]; int y = e[id].y; if (d[y] != (d[x] + 1)) { continue; } long long int pushed = DFS(y, min(flow, e[id].cap - e[id].flow)); if (pushed) { e[id].flow = e[id].flow + pushed; e[id ^ 1].flow = e[id ^ 1].flow - pushed; return pushed; } } return 0LL; } long long int max_flow(int source, int sink, int n) { long long int flow = 0; long long int pushed; while (BFS(source, sink, n)) { for (int i = 0; i <= (n + 1); i++) { curr[i] = 0; } while (pushed = DFS(source, INT_MAX)) { flow = flow + pushed; } } return flow; } void seive() { for (int i = 2; i <= 100000; i++) { isprime[i] = true; } for (int i = 2; i <= 100000; i++) { if (isprime[i]) { for (int j = (i + i); j <= 100000; j = j + i) { isprime[j] = false; } } } } int main() { seive(); int n; scanf("%d", &n); long long int count_e = 0; long long int count_o = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] % 2 == 0) { count_e++; } else { count_o++; } } source = 0; sink = 201; for (int i = 1; i <= n; i++) { for (int j = (i + 1); j <= n; j++) { if (isprime[a[i] + a[j]]) { if (a[i] % 2 == 1 && a[j] % 2 == 0) { AddEdge(i, j, 1); } else if (a[i] % 2 == 0 && a[j] % 2 == 1) { AddEdge(j, i, 1); } } } } for (int i = 1; i <= n; i++) { if (a[i] % 2 == 0) { AddEdge(i, sink, 2); } else { AddEdge(source, i, 2); } } long long int flow = max_flow(source, sink, 203); bool poss = true; if (flow != 2 * count_e) { poss = false; } if (poss) { for (vector<edge>::iterator it = e.begin(); it != e.end(); it++) { edge t1 = (*it); if (t1.x != source && t1.x != sink && t1.y != source && t1.y != sink) { if (a[t1.x] % 2 == 1 && t1.flow == t1.cap) { Adj1[t1.x].push_back(t1.y); Adj1[t1.y].push_back(t1.x); } } } for (int i = 1; i <= n; i++) { if (!visited[i]) { DFS1(i, i); while (!s1.empty()) { s1.pop(); } id++; } } printf("%d \n", id); for (int i = 0; i < id; i++) { printf("%d ", g1[i].size()); for (vector<int>::iterator it = g1[i].begin(); it != g1[i].end(); it++) { printf("%d ", (*it)); } printf("\n"); } } else { printf("Impossible\n"); } return 0; }
7
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 502; int n, m, q; int r[502][502], b[502][502], y[502][502], g[502][502]; char c[502][502]; int val[502][502]; int f[502][11][502][11]; void init() { memset(f, 0, sizeof(f)); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) f[i][0][j][0] = val[i][j]; for (int k = 1; k <= 10; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int a = i + (1 << k - 1), b = j + (1 << k - 1); f[i][k][j][0] = max(f[i][k - 1][j][0], a >= maxn ? 0 : f[a][k - 1][j][0]); f[i][0][j][k] = max(f[i][0][j][k - 1], b >= maxn ? 0 : f[i][0][b][k - 1]); } for (int i = 1; i <= 10; i++) for (int j = 1; j <= 10; j++) for (int k = 1; k <= n; k++) for (int l = 1; l <= m; l++) { int a = k + (1 << i - 1), b = l + (1 << j - 1); if (a >= maxn || b >= maxn) continue; f[k][i][l][j] = max(max(f[k][i - 1][l][j], f[k][i][l][j - 1]), f[a][i - 1][b][j - 1]); } return; } int query(int a, int b, int c, int d) { if (a > b || c > d) return 0; int k1 = 0, k2 = 0; while (a + (1 << k1) - 1 < b - (1 << k1)) k1++; while (c + (1 << k2) - 1 < d - (1 << k2)) k2++; int A = b - (1 << k1) + 1, C = d - (1 << k2) + 1; return max(max(f[a][k1][c][k2], f[A][k1][C][k2]), max(f[a][k1][C][k2], f[A][k1][c][k2])); } int main() { ios::sync_with_stdio(false); scanf("%d%d%d", &n, &m, &q); getchar(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { c[i][j] = getchar(); } getchar(); } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (c[i][j] != 'R') { r[i][j] = 0; continue; } r[i][j] = min(r[i - 1][j - 1], min(r[i - 1][j], r[i][j - 1])) + 1; } for (int i = 1; i <= n; i++) for (int j = m; j >= 1; j--) { if (c[i][j] != 'G') { g[i][j] = 0; continue; } g[i][j] = min(g[i - 1][j + 1], min(g[i - 1][j], g[i][j + 1])) + 1; } for (int i = n; i >= 1; i--) for (int j = 1; j <= m; j++) { if (c[i][j] != 'Y') { y[i][j] = 0; continue; } y[i][j] = min(y[i + 1][j - 1], min(y[i + 1][j], y[i][j - 1])) + 1; } for (int i = n; i >= 1; i--) for (int j = m; j >= 1; j--) { if (c[i][j] != 'B') { b[i][j] = 0; continue; } b[i][j] = min(b[i + 1][j + 1], min(b[i + 1][j], b[i][j + 1])) + 1; } n--; m--; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) val[i][j] = min(min(r[i][j], g[i][j + 1]), min(y[i + 1][j], b[i + 1][j + 1])); init(); for (int i = 1; i <= q; i++) { int r1, c1, r2, c2; scanf("%d%d%d%d", &r1, &c1, &r2, &c2); r2--; c2--; int l = 0, r = 250; while (l != r) { int m = (l + r) >> 1; m++; int t = m - 1; if (query(r1 + t, r2 - t, c1 + t, c2 - t) >= m) l = m; else r = m - 1; } printf("%d\n", l * l * 4); } }
8
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; cin >> t; while (t--) { string s, h; cin >> s >> h; long long hash[26] = {0}; long long l1 = s.length(), l2 = h.length(); if (l2 < l1) { cout << "NO"; } else { long long flag = 0; for (long long i = 0; i < l1; i++) { hash[s[i] - 'a']++; } for (long long i = 0; i + l1 - 1 < l2; i++) { long long hash2[26] = {0}; string temp; temp = h.substr(i, l1); for (long long j = 0; j < l1; j++) { hash2[temp[j] - 'a']++; } long long f = 1; for (long long k = 0; k < 26; k++) { if (hash[k] != hash2[k]) { f = 0; break; } } if (f) { flag = 1; break; } } if (flag) { cout << "YES"; } else { cout << "NO"; } } cout << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, i, a[11000]; while (scanf("%d", &n) != EOF) { int sum = 0; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); sum += a[i]; } if (n == 1 && sum == 1) printf("YES\n"); else if (n == 1 && sum == 0) printf("NO\n"); else if (sum == n - 1) printf("YES\n"); else printf("NO\n"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; bool divide(long long sum, long long k, long long c) { long long t = 0; if (sum * 2 / k / (k + 1) == 0) return false; for (long long i = 1; i <= k - 1; i++) { cout << i * c << ' '; t += i; } cout << (sum - t) * c << endl; return true; } int main() { long long n, k; bool flag = false; cin >> n >> k; long long up = sqrt(n) + 1; for (long long i = 1; i <= up; i++) { if (n % i == 0) { if (divide(n / (n / i), k, n / i)) { flag = true; break; } } } if (!flag) { for (long long i = up; i >= 1; i--) { if (n % i == 0) { if (divide(n / i, k, i)) { flag = true; break; } } } } if (!flag) cout << -1 << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<complex<long double> > moles, home; complex<long double> rotate_90(const complex<long double>& p, const complex<long double>& about) { return complex<long double>(-(p.imag() - about.imag()) + about.real(), (p.real() - about.real()) + about.imag()); } bool square(complex<long double> p1, complex<long double> p2, complex<long double> p3, complex<long double> p4) { map<long double, int> lengths; lengths[hypot(p1.real() - p2.real(), p1.imag() - p2.imag())]++; lengths[hypot(p1.real() - p3.real(), p1.imag() - p3.imag())]++; lengths[hypot(p1.real() - p4.real(), p1.imag() - p4.imag())]++; lengths[hypot(p3.real() - p2.real(), p3.imag() - p2.imag())]++; lengths[hypot(p4.real() - p2.real(), p4.imag() - p2.imag())]++; lengths[hypot(p4.real() - p3.real(), p4.imag() - p3.imag())]++; bool digonals = 0, slide = 0; for (auto it : lengths) { if (it.second == 2 && it.first) digonals = 1; else if (it.second == 4 && it.first) slide = 1; } return (lengths.size() == 2 && slide && digonals); } int solve() { int c = 0; int mini = INT_MAX; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { for (int k = 0; k < 4; ++k) { for (int f = 0; f < 4; ++f) { if (square(moles[0], moles[1], moles[2], moles[3])) mini = min(mini, i + j + k + f); moles[3] = rotate_90(moles[3], home[3]); } moles[2] = rotate_90(moles[2], home[2]); } moles[1] = rotate_90(moles[1], home[1]); } moles[0] = rotate_90(moles[0], home[0]); } return mini; } int main() { int n; scanf("%d", &n); while (n--) { home.clear(); moles.clear(); for (int i = 0; i < 4; ++i) { long double x1, y1, hx, hy; cin >> x1 >> y1 >> hx >> hy; complex<long double> p1(x1, y1); complex<long double> p2(hx, hy); moles.push_back(p1); home.push_back(p2); } int ans = solve(); if (ans == INT_MAX) cout << -1 << endl; else cout << ans << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; string s; int main() { long long int a, b, j, k, l, m, i; cin >> k >> b; m = ((b - 1) * b) / 2; m = m % 1000000007; long long int ans = 0; for (i = 1; i <= k; i++) { long long int x = (i * b + 1) % 1000000007; ans = (ans + (x * m) % 1000000007) % 1000000007; } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int nmax = 1e6; const long long mod = 500001269; int n; bool ok; long long h[nmax + 1]; long long pb[nmax + 1]; string s, a, b, c; inline long long hsh(int x, int y) { long long ans = (h[y] - h[x - 1] * pb[y - x + 1]) % mod; if (ans < 0) ans += mod; return ans; } void check(int lg, int x) { if (lg > 0 && x > 0 && lg + x < n) { if ((s[n - lg + 1] == '0' && lg > 1) || (s[n - lg - x + 1] == '0' && x > 1) || (s[1] == '0' && n - lg - x > 1)) return; if (hsh(n - lg + 1, n) == (hsh(1, n - lg - x) + hsh(n - lg - x + 1, n - lg)) % mod) { a.resize(n - lg - x); b.resize(x); c.resize(lg); for (int i = 1; i <= n - lg - x; ++i) { a[i - 1] = s[i]; } for (int i = n - lg - x + 1; i <= n - lg; ++i) { b[i - (n - lg - x + 1)] = s[i]; } for (int i = n - lg + 1; i <= n; ++i) { c[i - (n - lg + 1)] = s[i]; } ok = 1; } } } int main() { cin >> s; n = (int)s.size(); s = '#' + s; pb[0] = 1; for (int i = 1; i <= n; ++i) pb[i] = pb[i - 1] * 10 % mod; for (int i = 1; i <= n; ++i) h[i] = (h[i - 1] * 10 + s[i] - '0') % mod; ok = 0; for (int i = 1; i <= n / 2 && ok == 0; ++i) { if (!ok) check(i, i); if (!ok) check(i, i - 1); if (!ok) check(i, (n - i - i)); if (!ok) check(i, (n - i - (i - 1))); } cout << a << "+" << b << "=" << c << "\n"; return 0; }
7
#include <bits/stdc++.h> using namespace std; char num[150]; int main() { while (cin >> num) { int len = strlen(num); int de = 150; bool flag = 0; int tem; for (int i = 0; i < len; ++i) { if (num[i] == '.') { de = i; } } if (de == 150) { de = len; num[len] = '.'; len++; } if (num[0] == '-') { cout << "("; flag = 1; for (int i = 1; i < len; ++i) num[i - 1] = num[i]; de--; len--; } cout << "$"; while (de > 3) { de -= 3; } for (int i = 0; i < len; ++i) { if (num[i] == '.') { cout << "."; if (len > i + 1) cout << num[i + 1]; else cout << 0; if (len > i + 2) cout << num[i + 2]; else cout << 0; break; } if (de == i) { cout << ","; de += 3; } cout << num[i]; } if (flag) cout << ")"; cout << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const unsigned MAXN = (1 << 18); struct basis { vector<unsigned> base; void add(unsigned val) { for (unsigned &x : base) if (x & -x & val) val ^= x; if (val == 0) return; for (unsigned &x : base) if (x & -x & val) x ^= val; base.push_back(val); } void xor_all(unsigned val) { for (unsigned &x : base) if (x & 1) x ^= val; } bool query(unsigned val) { for (unsigned x : base) if (x & -x & val) val ^= x; return val == 0; } unsigned size() { return base.size(); } void clear() { base.clear(); } basis() {} basis(vector<unsigned> li) { for (unsigned x : li) add(x); } basis operator+(basis oth) { for (unsigned x : base) oth.add(x); return oth; } }; unsigned a[MAXN]; struct node { basis base; unsigned lazy; node() { lazy = 0; } node(unsigned val) { base.add(val); lazy = 0; } }; node temp, broken; node merge(node l, node r) { temp.base = l.base + r.base; temp.lazy = 0; return temp; } struct segment_tree { node tr[4 * MAXN]; void push(unsigned l, unsigned r, unsigned idx) { if (tr[idx].lazy) { tr[idx].base.xor_all(tr[idx].lazy); if (l != r) { tr[2 * idx + 1].lazy ^= tr[idx].lazy; tr[2 * idx + 2].lazy ^= tr[idx].lazy; } tr[idx].lazy = 0; } } void init(unsigned l, unsigned r, unsigned idx) { if (l == r) { tr[idx] = node(a[l]); return; } unsigned mid = (l + r) >> 1; init(l, mid, 2 * idx + 1); init(mid + 1, r, 2 * idx + 2); tr[idx] = merge(tr[2 * idx + 1], tr[2 * idx + 2]); } void update(unsigned qL, unsigned qR, unsigned val, unsigned l, unsigned r, unsigned idx) { push(l, r, idx); if (qL > r || l > qR) return; if (qL <= l && r <= qR) { tr[idx].lazy ^= val; push(l, r, idx); return; } unsigned mid = (l + r) >> 1; update(qL, qR, val, l, mid, 2 * idx + 1); update(qL, qR, val, mid + 1, r, 2 * idx + 2); tr[idx] = merge(tr[2 * idx + 1], tr[2 * idx + 2]); } node query(unsigned qL, unsigned qR, unsigned l, unsigned r, unsigned idx) { push(l, r, idx); if (l > qR || r < qL) return broken; if (qL <= l && r <= qR) return tr[idx]; unsigned mid = (l + r) >> 1; return merge(query(qL, qR, l, mid, 2 * idx + 1), query(qL, qR, mid + 1, r, 2 * idx + 2)); } }; unsigned n, m; void read() { cin >> n >> m; for (unsigned i = 1; i <= n; i++) cin >> a[i]; } segment_tree t; void solve() { for (unsigned i = 1; i <= n; i++) a[i] = (a[i] << 1) | 1; t.init(1, n, 0); while (m--) { unsigned type, l, r; cin >> type >> l >> r; if (type == 1) { unsigned val; cin >> val; t.update(l, r, val << 1, 1, n, 0); } else { basis ans, ret = t.query(l, r, 1, n, 0).base; for (unsigned x : ret.base) ans.add(x >> 1); cout << (1 << ans.size()) << '\n'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); solve(); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int n = 5e5 + 10; const long long MOD = 998244353; int N, K, M; int L[n], R[n], X[n]; vector<tuple<int, int, int> > A; int cnt[n]; bool vis[n]; long long dp[n]; long long to[n]; long long pw[n]; long long calc() { sort(A.begin(), A.end()); stack<int> S; fill_n(vis, M + 1, true); for (auto t : A) { int l, r, ind; tie(l, r, ind) = t; r *= -1; while (S.size() && R[S.top()] >= R[ind]) { vis[S.top()] = false; S.pop(); } S.push(ind); } vector<int> X; X.push_back(M); L[M] = R[M] = 0; for (int i = 0; i < ((int)(A).size()); i++) { int ind = get<2>(A[i]); if (vis[ind]) { X.push_back(ind); } } X.push_back(M + 1); L[M + 1] = R[M + 1] = N + 1; fill_n(dp, M + 2, 0); fill_n(to, M + 2, 0); dp[0] = 1; deque<tuple<int, int, int> > Q; for (int i = 1; i < ((int)(X).size()); i++) { if (i != 1) dp[i] = dp[i - 1]; Q.push_back({R[X[i - 1]] + 1, R[X[i]], i - 1}); to[i - 1] = pw[cnt[R[X[i]]] - cnt[R[X[i - 1]]]] - 1 + MOD; to[i - 1] %= MOD; dp[i] += to[i - 1] * dp[i - 1]; dp[i] %= MOD; while (get<0>(Q[0]) < L[X[i]]) { int l, r, ind; tie(l, r, ind) = Q[0]; if (r < L[X[i]]) { Q.pop_front(); dp[i] -= to[ind] * dp[ind] % MOD; dp[i] = (dp[i] + MOD) % MOD; to[ind] = 0; } else { dp[i] -= dp[ind] * to[ind] % MOD; dp[i] = (dp[i] + MOD) % MOD; l = L[X[i]]; get<0>(Q[0]) = l; to[ind] = (pw[cnt[r] - cnt[l - 1]] - 1) * pw[cnt[l - 1] - cnt[R[X[ind]]]] % MOD; dp[i] += dp[ind] * to[ind]; dp[i] = (dp[i] + MOD) % MOD; } } } return dp[((int)(X).size()) - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); pw[0] = 1; for (int i = 1; i < n; i++) { pw[i] = 2 * pw[i - 1] % MOD; } cin >> N >> K >> M; for (int i = 0; i < M; i++) { cin >> L[i] >> R[i] >> X[i]; } long long res = 1; for (int b = 0; b < K; b++) { A.clear(); fill_n(cnt, N + 10, 0); for (int i = 0; i < M; i++) { if (X[i] & (1 << b)) { cnt[L[i]]++; cnt[R[i] + 1]--; } else { A.push_back({L[i], -R[i], i}); } } for (int i = 1; i < N + 5; i++) { cnt[i] += cnt[i - 1]; } for (int i = 0; i < N + 5; i++) { if (!cnt[i]) cnt[i] = 1; else cnt[i] = 0; } for (int i = 1; i < N + 5; i++) { cnt[i] += cnt[i - 1]; } res *= calc(); res %= MOD; } cout << res << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, i, x, a, b, result = 0, t, w; cin >> a >> b >> n; for (t = 1; n >= 0; t++) { for (i = n; i >= 1; i--) { if (n % i == 0 && a % i == 0) { x = i; break; } } n = n - x; if (n == 0) { result = 0; } for (w = n; w >= 1; w--) { if (n % w == 0 && b % w == 0) { x = w; break; } } n = n - x; if (n == 0) { result = 1; } } cout << result; }
0
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int cnt = 0; for (int i = 0; i < s.length(); ++i) { if (s[i] == '4' || s[i] == '7') ++cnt; } if (cnt == 4 || cnt == 7) cout << "YES"; else cout << "NO"; return 0; }
0
#include <bits/stdc++.h> int main() { char arr[10000]; int ln, cnt = 0, val = 0, i, j; scanf("%s", arr); scanf("%d", &cnt); for (ln = 0; arr[ln]; ln++) ; if (ln % cnt != 0) { puts("NO"); return 0; } cnt = ln / cnt; while (val < ln) { for (i = val, j = (val + cnt - 1); i < j;) { if (arr[i++] != arr[j--]) { printf("NO\n"); return 0; } } val += cnt; } printf("YES\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 25; const int MAXV = 50000; bitset<MAXN * MAXV + 1> dp[2 * MAXN][MAXN + 1]; int main() { int i, j, n; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n; vector<int> arr(2 * n); for (i = 0; i < n; i++) { cin >> arr[i]; } for (i = n; i < 2 * n; i++) { cin >> arr[i]; } sort(arr.rbegin(), arr.rend()); vector<vector<int> > sol(2, vector<int>(n)); sol[0][0] = arr[2 * n - 1]; sol[1][n - 1] = arr[2 * n - 2]; arr.resize(2 * n - 2); int sz = arr.size(), tot = 0; for (auto it : arr) { tot += it; } dp[0][0][0] = 1; dp[0][1][arr[0]] = 1; for (i = 0; i + 1 < sz; i++) { for (j = 0; j <= min(i + 1, n); j++) { dp[i + 1][j] = (dp[i + 1][j] | dp[i][j]); if (j < n) { dp[i + 1][j + 1] = (dp[i + 1][j + 1] | (dp[i][j] << arr[i + 1])); } } } int s, mn = 1e9; for (int sum = 0; sum <= tot; sum++) { int cur = max(sum, tot - sum); if (dp[sz - 1][n - 1][sum] && cur <= mn) { mn = cur; s = sum; } } vector<bool> vis(sz); i = sz - 1, j = n - 1; while (i >= 0) { if (i == 0) { vis[i] = 1 - j; } else { if (dp[i - 1][j][s]) { vis[i] = 1; } else { vis[i] = 0; j--; s -= arr[i]; } } i--; } int id = 1; for (i = 0; i < sz; i++) { if (vis[i] == 0) { sol[0][id++] = arr[i]; } } id = n - 1; for (i = 0; i < sz; i++) { if (vis[i]) { sol[1][--id] = arr[i]; } } sort(sol[0].begin(), sol[0].end()); sort(sol[1].rbegin(), sol[1].rend()); for (i = 0; i < 2; i++) { for (j = 0; j < n; j++) { cout << sol[i][j] << " "; } cout << "\n"; } return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a; cin >> b; cin >> c; vector<int> vec; vec.push_back(a + b + c); vec.push_back(a + b * c); vec.push_back(a * b + c); vec.push_back(a * b * c); vec.push_back((a + b) * c); vec.push_back((a * b) + c); vec.push_back(a + (b * c)); vec.push_back(a * (b + c)); int max = -1; for (int i = 0; i < vec.size(); i++) { if (vec[i] > max) max = vec[i]; } cout << max; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; const int MOD = 1e9 + 7; int n; long long int a[MAXN], minn; bool have[MAXN], flag; int main() { while (scanf("%d", &n) != EOF) { a[1] = 0; minn = 0; memset(have, false, sizeof(have)); flag = true; for (int i = 1; i < n; ++i) { int num; scanf("%I64d", &num); a[i + 1] = a[i] + num; minn = min(minn, a[i + 1]); } for (int i = 1; i <= n; ++i) { a[i] += -minn + 1; if (a[i] > n || have[a[i]]) { printf("-1\n"); flag = false; break; } have[a[i]] = true; } if (flag) { for (int i = 1; i <= n; ++i) { i == n ? printf("%I64d\n", a[i]) : printf("%I64d ", a[i]); } } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, k; long double v1, v2, l; bool can(long double t, long double l) { int N = n; while (N > 0) { N -= k; if (t == 0) return false; if (N > 0) { long double t1 = (l - v1 * t) / (v2 - v1); long double t2 = (2 * v2 * t1) / (v1 + v2); t -= t2; l -= v1 * t2; } else { t -= l / v2; } if (t < 0) return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << setprecision(10) << fixed; cin >> n >> l >> v1 >> v2 >> k; long double L = 0, R = l + 0.1; while (L + 0.0000000001 < R) { long double m = (L + R) / 2; if (can(m, l)) R = m; else L = m; } cout << R; }
5
#include <bits/stdc++.h> using namespace std; template <class T> inline void gmin(T &a, T b) { if (a > b) a = b; } template <class T> inline void gmax(T &a, T b) { if (a < b) a = b; } inline int sign(const double &a) { return a > 1e-9 ? 1 : (a < -1e-9 ? -1 : 0); } struct Initializer { Initializer() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } ~Initializer() {} } initializer; int to[1024], din[1024], X, flag = 0, len = 0; int dfs(int first) { int res; if (to[first] == 0) res = 1; else res = dfs(to[first]) + 1; if (first == X) { flag = 1; len = res; } return res; } int main() { int n; scanf("%d%d", &n, &X); for (int i = 1; i <= n; i++) { int k; scanf("%d", &k); if (!k) continue; to[k] = i; din[i]++; } vector<int> a; for (int i = 1; i <= n; i++) if (!din[i]) { flag = 0; a.push_back(dfs(i)); if (flag) { len = a.back() - len + 1; if (int(a.size()) > 1) swap(a.back(), a.front()); } } vector<int> f(1024 + 1); f[0] = 1; for (int i = 1; i <= int(a.size()) - 1; i++) for (int j = 1024 - 1; j >= a[i]; j--) { f[j] |= f[j - a[i]]; } for (int i = 0; i <= 1024 - 1; i++) if (f[i]) printf("%d\n", i + len); return 0; }
3
#include <bits/stdc++.h> using namespace std; int query(int l, int r) { int s; cout << "? " << l + 1 << " " << r + 1 << endl; cin >> s; return s; } void solve() { int n, i, s, p; cin >> n; int a[n]; p = query(0, n - 1); for (i = 1; i < n - 1; i++) { s = query(i, n - 1); a[i - 1] = p - s; p = s; } s = query(n - 3, n - 2); a[n - 2] = s - a[n - 3]; a[n - 1] = p - a[n - 2]; cout << "!"; for (i = 0; i < n; i++) cout << " " << a[i]; cout << endl; } int main() { ios_base::sync_with_stdio(false); int t = 1; while (t--) solve(); }
3
#include <bits/stdc++.h> using namespace std; double const eps = 1e-6; double const pi = 3.1415926535; long long const mod = 1e9 + 7; int a[105]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> m >> n; for (int i = 1; i <= n; i++) { long long t = -1, p = -1; for (int j = 1; j <= m; j++) { int x; cin >> x; if (x > t) { t = x; p = j; } } a[p]++; } long long t = -1, p = -1; for (int i = 1; i <= m; i++) { if (a[i] > t) { t = a[i]; p = i; } } cout << p << endl; return 0; }
1
#include <bits/stdc++.h> int main() { char str[100], i; int l, j, f = 0, c = 0; gets(str); l = strlen(str); for (i = 'a'; i <= 'z'; i++) { f = 0; for (j = 0; j < l; j++) { if (i == str[j]) f++; } if (f != 0) { c++; } } if (c % 2 == 0) printf("CHAT WITH HER!\n"); else if (c % 2 != 0) printf("IGNORE HIM!\n"); }
0
#include <bits/stdc++.h> using namespace std; const int NMAX = 1001000; const long double INF = 1e50; long long x[NMAX], y[NMAX], a[NMAX]; int N; long long MM = 1.1e9; const long double EPS = 1e-5; namespace n0 { int sgn = -1; struct line { long double m, b, left; line() : left(-INF) {} line(long double m, long double b) : m(m), b(b), left(-INF) {} long double eval(long double x) const { return m * x + b; } bool operator<(const line& o) const { if (m == o.m) return sgn * b < sgn * o.b; return sgn * m > sgn * o.m; } long double intersect(const line& o) const { if (abs(m - o.m) < EPS) return INF; return (o.b - b) / (m - o.m); } }; struct comp_left { bool operator()(const line& l, const line& r) const { if (l.left == r.left) { if (l.m == r.m) return l.b < r.b; return l.m < r.m; } return l.left < r.left; } }; struct envelope { set<line> lines; set<line, comp_left> top; long double query(long double x) { line l; l.left = x; set<line, comp_left>::iterator it = --top.lower_bound(l); return it->eval(x); } void recompute(line nl, line l, long double xl) { top.erase(nl); lines.erase(nl); if (nl.m != l.m) { nl.left = xl; top.insert(nl); lines.insert(nl); } } void insert(line l) { if (lines.size() == 1) { if (l < *lines.begin()) { recompute(*lines.begin(), l, l.intersect(*lines.begin())); } else { if (l.m == lines.begin()->m) return; l.left = l.intersect(*lines.begin()); } } else if (lines.size() > 1) { auto i = lines.lower_bound(l); if (i != lines.begin() && i != lines.end()) { auto j = i; j--; long double x = i->intersect(*j); if (sgn * l.eval(x) >= sgn * (i->eval(x))) return; } for (int k = 0; k < 2; k++) { int s = (k ? -1 : 1); i = lines.lower_bound(l); bool bcond = (k ? i != lines.end() : i != lines.begin()); if (!bcond) continue; auto j = i; if (k) j++; else { j = --i; j--; } while (true) { bool econd = (k ? j == lines.end() : i == lines.begin()); long double x12 = i->intersect(l), x13 = j->intersect(l); if (!econd && s * x13 > s * x12 - EPS) { auto it = i; if (k) i++, j++; else i--, j--; top.erase(*it); lines.erase(it); } else { if (k) recompute(*i, l, i->intersect(l)); else l.left = l.intersect(*i); break; } } } } lines.insert(l); top.insert(l); } }; } // namespace n0 long long ans; int main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> N; vector<vector<long long>> v(N, vector<long long>(3)); for (auto& vv : v) { for (auto& vvv : vv) { cin >> vvv; } } sort(v.begin(), v.end()); for (int i = 1; i <= N; ++i) { x[i] = v[i - 1][0]; y[i] = v[i - 1][1]; a[i] = v[i - 1][2]; } n0::envelope env; env.insert(n0::line(0, 0)); for (int i = N; i >= 0; --i) { long long f = env.query(MM - x[i]) + .4; ans = max(f, ans); long double b = f + (long double)(x[i] - MM) * y[i] - a[i]; long double m = y[i]; if (i != 0) { env.insert(n0::line(m, b)); } } cout << ans << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const int INF = 2e9; const long long INF64 = 3e18; const long double EPS = 1e-12; const long double PI = 3.1415926535897932384626433832795028841; const long long MD = 925262732; const long long T = 634521; const int N = 200100; const int M = 2001; long long n, a[N]; vector<pair<long long, long long> > p; int main() { cin >> n; for (int i = 0; i < int(n); i++) { scanf("%I64d", &a[i]); } long long ans = 0; for (int i = 0; i < n; i++) { p.clear(); long long j = i; long long q = 1; while (j < n) { if (a[j] != -1) { if (int((p).size()) == 0) p.push_back(make_pair(a[j], j)); else { if (int((p).size()) == 1) { p.push_back(make_pair(a[j], j)); q = p[1].first - p[0].first; long long z = p[1].second - p[0].second; if (abs(q) % z != 0) break; q /= z; if (p[0].first - q * (p[0].second - i) <= 0) break; } else { p.push_back(make_pair(a[j], j)); long long qq = p[int((p).size()) - 1].first - p[int((p).size()) - 2].first; long long z = p[int((p).size()) - 1].second - p[int((p).size()) - 2].second; if (abs(qq) % z != 0 || qq / z != q) break; } } } else { if (int((p).size()) > 1) { long long z = int((p).size()) - 1; if (p[z].first + q * (j - p[z].second) <= 0) break; } } j++; } ans++; i = j - 1; } cout << ans; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const double eps = 1e-9; const double pi = acos(-1.0); const int m1 = (int)1e9 + 7; const int m2 = (int)1e9 + 9; int n, m, k; string s; int a[100100]; long long pp1[100100], pp2[100100]; long long sp1[100100], sp2[100100]; struct Hash { int v1, v2, len; Hash(int _v1, int _v2, int _len) { v1 = _v1; v2 = _v2; len = _len; } bool operator==(const Hash& o) { return v1 == o.v1 && v2 == o.v2; } Hash operator+(const Hash& b) { Hash a = *this; if (a.len == 0) return b; if (b.len == 0) return a; int v1 = (a.v1 * pp1[b.len] + b.v1) % m1; int v2 = (a.v2 * pp2[b.len] + b.v2) % m2; return Hash(v1, v2, a.len + b.len); } Hash() { v1 = v2 = len = 0; } }; struct Tree { int len, color; Hash h; Tree(int _len, int ch) { len = _len; color = -1; h = Hash(ch, ch, len); } Tree(Tree t1, Tree t2) { len = t1.len + t2.len; color = -1; h = t1.h + t2.h; } Tree() {} }; Tree T[400400]; void push(int v, int tl, int tr) { if (T[v].color == -1) return; T[v].h = Hash(sp1[tr - tl] * T[v].color % m1, sp2[tr - tl] * T[v].color % m2, T[v].len); if (tl != tr) { T[2 * v].color = T[2 * v + 1].color = T[v].color; } T[v].color = -1; } void build(int v, int tl, int tr) { if (tl == tr) { T[v] = Tree(tr - tl + 1, a[tl]); return; } int mid = (tl + tr) / 2; build(2 * v, tl, mid); build(2 * v + 1, mid + 1, tr); T[v] = Tree(T[2 * v], T[2 * v + 1]); } void update(int v, int tl, int tr, int l, int r, int val) { push(v, tl, tr); if (l > tr || r < tl) return; if (l <= tl && tr <= r) { T[v].color = val; push(v, tl, tr); return; } int mid = (tl + tr) / 2; update(2 * v, tl, mid, l, r, val); update(2 * v + 1, mid + 1, tr, l, r, val); T[v] = Tree(T[2 * v], T[2 * v + 1]); } Tree get(int v, int tl, int tr, int l, int r) { push(v, tl, tr); if (l > tr || r < tl) return Tree(0, 0); if (l <= tl && tr <= r) return T[v]; int mid = (tl + tr) / 2; return Tree(get(2 * v, tl, mid, l, r), get(2 * v + 1, mid + 1, tr, l, r)); } int main() { scanf("%d%d%d\n", &n, &m, &k); srand(time(NULL)); int p1 = (rand() * 10000 + 1000) * 2 + 1; int p2 = (rand() * 10000 + 1000) * 2 + 1; pp1[0] = pp2[0] = 1; sp1[0] = sp2[0] = 1; for (int i = 1; i < 100100; i++) { pp1[i] = pp1[i - 1] * p1 % m1; pp2[i] = pp2[i - 1] * p2 % m2; sp1[i] = (sp1[i - 1] + pp1[i]) % m1; sp2[i] = (sp2[i - 1] + pp2[i]) % m2; } cin >> s; for (int i = 0; i < s.length(); i++) a[i + 1] = s[i]; build(1, 1, n); for (int i = 0; i < m + k; i++) { int id, l, r, cd; scanf("%d%d%d%d", &id, &l, &r, &cd); if (id == 1) { update(1, 1, n, l, r, cd + '0'); } else { Tree t1 = get(1, 1, n, l, r - cd); Tree t2 = get(1, 1, n, l + cd, r); if (t1.h == t2.h) { printf("YES\n"); } else printf("NO\n"); } } return 0; }
8
#include <bits/stdc++.h> using namespace std; int n, m, cn[1234567], a[1234567]; int u[1234567][3][3]; inline void cmax(int& a, int b) { if (a < b) a = b; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", a + i), ++cn[a[i]]; memset(u, -127 / 3, sizeof u); u[0][0][0] = 0; for (int i = 0; i < m + 5; ++i) for (int j = 0; j <= 2; ++j) for (int k = 0; k <= 2; ++k) if (u[i][j][k] >= 0) { int w = cn[i + 1] - j - k; for (int s = 0; s <= 2 && s <= w; ++s) cmax(u[i + 1][k][s], u[i][j][k] + (w - s) / 3 + s); } cout << u[m + 5][0][0] << "\n"; }
7
#include <bits/stdc++.h> using namespace std; const int inf_int = 1e9 + 100; const long long inf_ll = 1e18; const double pi = 3.1415926535898; template <class T1, class T2> std::ostream &operator<<(std::ostream &out, const std::pair<T1, T2> &rhs) { out << "( " << rhs.first << " , " << rhs.second << " )"; return out; } template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return '"' + s + '"'; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A> string to_string(vector<vector<A>> v) { bool first = true; string res = "\n{"; for (const auto &x : v) { if (!first) { res += ",\n "; } first = false; res += to_string(x); } res += "}\n"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } bool debug = 0; const int MAXN = 3000 + 100; const int LOG = 20; const int mod = 998244353; const int MX = (1e7 + 100); const unsigned long long one = 1; unsigned long long multiply(unsigned long long a, unsigned long long b); unsigned long long multiply_powers_of_2(unsigned long long a, unsigned long long b) { if (a == 1 || b == 1) return a * b; unsigned long long n = 5; while ((one << (one << n)) > max(a, b)) { --n; } unsigned long long power = (one << (one << n)); if (a >= power && b >= power) { return multiply(power * 3 / 2, multiply_powers_of_2(a / power, b / power)); } else if (a >= power) { return multiply_powers_of_2(a / power, b) * power; } else { return multiply_powers_of_2(a, b / power) * power; } } unsigned long long multiply(unsigned long long a, unsigned long long b) { unsigned long long ans = 0; for (int i = 0; i < 64; i++) { if (a & (one << i)) { for (int j = 0; j < 64; j++) { if (b & (one << j)) { ans ^= multiply_powers_of_2(one << i, one << j); } } } } return ans; } const unsigned long long M = ((one << 63) - 1) * 2 + 1; vector<int> factors = {5, 641, 6700417, 65537, 257, 17, 3}; unsigned long long save[64][64]; unsigned long long my_mul(unsigned long long a, unsigned long long b) { unsigned long long res = 0; for (int i = 0; i < 64; ++i) { if ((a >> i) & 1) { for (int j = 0; j < 64; ++j) { if ((b >> j) & 1) { res ^= save[i][j]; } } } } return res; } unsigned long long bin_pow(unsigned long long a, unsigned long long n) { unsigned long long res = 1; while (n) { if (n & 1) res = my_mul(a, res); a = my_mul(a, a); n >>= 1; } return res; } struct super_mul { unsigned long long mem[64]; void init(unsigned long long a) { for (int i = 0; i < 64; ++i) { mem[i] = 0; for (int j = 0; j < 64; ++j) { if ((a >> j) & 1) { mem[i] ^= save[i][j]; } } } } unsigned long long mult(unsigned long long a) { unsigned long long res = 0; for (int i = 0; i < 64; ++i) { if ((a >> i) & 1) res ^= mem[i]; } return res; } }; template <typename T> void crt(const vector<int> &p, const vector<int> &a, T &res) { assert(p.size() == a.size()); auto inverse = [&](int q, int m) { q %= m; if (q < 0) q += m; int b = m, u = 0, v = 1; while (q) { int t = b / q; b -= t * q; swap(q, b); u -= t * v; swap(u, v); } assert(b == 1); if (u < 0) u += m; return u; }; vector<int> x(p.size()); for (int i = 0; i < (int)p.size(); i++) { assert(0 <= a[i] && a[i] < p[i]); x[i] = a[i]; for (int j = 0; j < i; j++) { x[i] = (int)((long long)(x[i] - x[j]) * inverse(p[j], p[i]) % p[i]); if (x[i] < 0) x[i] += p[i]; } } res = 0; for (int i = (int)p.size() - 1; i >= 0; i--) { res = res * p[i] + x[i]; } } void solve() { unsigned long long a, b; cin >> a >> b; vector<int> f, v; for (int i = 0; i < (int(factors.size())); ++i) { unsigned long long A = bin_pow(a, M / factors[i]); unsigned long long B = bin_pow(b, M / factors[i]); 42; if (A == B && A == 1) { continue; } if (false && A == B) { f.push_back(factors[i]); v.push_back(1); continue; } int n = sqrt(factors[i]) + 1; super_mul mul_step; mul_step.init(A); unsigned long long big_step = 1; for (int i = 0; i < n; ++i) { big_step = mul_step.mult(big_step); } 42; super_mul mul_big; mul_big.init(big_step); unsigned long long val = big_step; map<unsigned long long, int> mp; for (int i = 1; i <= n; ++i) { if (!mp.count(val)) { mp[val] = i; } val = mul_big.mult(val); } 42; int res = inf_int; if (false && A == B) { res = 1; } else { unsigned long long val = B; for (int i = 0; i <= n; ++i) { if (mp.count(val)) { res = min(mp[val] * n - i, res); } val = mul_step.mult(val); } } if (res == inf_int) { cout << -1 << "\n"; return; } f.push_back(factors[i]); v.push_back(res); 42; } if ((int(f.size())) == 0) { cout << 1 << "\n"; return; } unsigned long long ans = 0; crt(f, v, ans); cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.setf(ios::fixed); cout.precision(15); for (int i = 0; i < 64; ++i) { for (int j = 0; j < 64; ++j) { save[i][j] = multiply_powers_of_2(one << i, one << j); } } int t = 1; cin >> t; while (t--) solve(); 42; }
13
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long maxn = 1e6 + 7; inline long long read() { long long res = 0, tmp = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') tmp = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') res = (res << 1) + (res << 3) + (ch ^ 48), ch = getchar(); return res * tmp; } inline void write(long long x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } void fre() { freopen(".in", "r", stdin); freopen(".out", "w", stdout); } long long max(long long a, long long b) { return a > b ? a : b; } long long min(long long a, long long b) { return a < b ? a : b; } void hour() { cerr << "运行时间:" << 1.0 * clock() / CLOCKS_PER_SEC << "s" << endl; } long long a[maxn][4], l[4], n, m, x, y, t, r, ans = 1; string s; int main() { n = read(); m = read(); x = read(); y = read(); if (n * m * x * y % 2 == 1) return puts("-1"); cin >> s; t = (s == "UL" ? 1 : (s == "UR" ? 2 : (s == "DL" ? 3 : 4))); if (x == n) a[y][0] = 1, l[0]++; if (y == m) a[x][1] = 1, l[1]++; if (x == 1) a[y][2] = 1, l[2]++; if (y == 1) a[x][3] = 1, l[3]++; while (++r < maxn) { if (l[0] + l[1] + l[2] + l[3] == n + m - (n * m % 2) * 2) { write(ans); return 0; } if (t == 1) { if (x - 1 > y - 1) { ans += y - 1; x -= (y - 1); y = 1; } else { ans += x - 1; y -= (x - 1); x = 1; } if (x == 1 && y == 1) { if (a[y][2] == 0) a[y][2] = 1, l[2]++; if (a[x][3] == 0) a[x][3] = 1, l[3]++; if (a[y + 1][2] == a[y][2] || a[y - 1][2] == a[y][2] || a[x + 1][3] == a[x][3] || a[x - 1][3] == a[x][3]) return puts("-1"); t = 4; } else if (x == 1) { if (a[y][2] == 0) a[y][2] = 1, l[2]++; if (a[y + 1][2] == a[y][2] || a[y - 1][2] == a[y][2]) return puts("-1"); t = 3; } else { if (a[x][3] == 0) a[x][3] = 1, l[3]++; if (a[x + 1][3] == a[x][3] || a[x - 1][3] == a[x][3]) return puts("-1"); t = 2; } } else if (t == 2) { if (x - 1 > m - y) { ans += m - y; x -= (m - y); y = m; } else { ans += x - 1; y += (x - 1); x = 1; } if (x == 1 && y == m) { if (a[y][2] == 0) a[y][2] = 1, l[2]++; if (a[x][1] == 0) a[x][1] = 1, l[1]++; if (a[y + 1][2] == a[y][2] || a[y - 1][2] == a[y][2] || a[x + 1][1] == a[x][1] || a[x - 1][1] == a[x][1]) return puts("-1"); t = 3; } else if (x == 1) { if (a[y][2] == 0) a[y][2] = 1, l[2]++; if (a[y + 1][2] == a[y][2] || a[y - 1][2] == a[y][2]) return puts("-1"); t = 4; } else { if (a[x][1] == 0) a[x][1] = 1, l[1]++; if (a[x + 1][1] == a[x][1] || a[x - 1][1] == a[x][1]) return puts("-1"); t = 1; } } else if (t == 3) { if (n - x > y - 1) { ans += y - 1; x += (y - 1); y = 1; } else { ans += n - x; y -= (n - x); x = n; } if (x == n && y == 1) { if (a[y][0] == 0) a[y][0] = 1, l[0]++; if (a[x][3] == 0) a[x][3] = 1, l[3]++; if (a[y + 1][0] == a[y][0] || a[y - 1][0] == a[y][0] || a[x + 1][3] == a[x][3] || a[x - 1][3] == a[x][3]) return puts("-1"); t = 2; } else if (x == n) { if (a[y][0] == 0) a[y][0] = 1, l[0]++; if (a[y + 1][0] == a[y][0] || a[y - 1][0] == a[y][0]) return puts("-1"); t = 1; } else { if (a[x][3] == 0) a[x][3] = 1, l[3]++; if (a[x + 1][3] == a[x][3] || a[x - 1][3] == a[x][3]) return puts("-1"); t = 4; } } else if (t == 4) { if (n - x > m - y) { ans += m - y; x += (m - y); y = m; } else { ans += n - x; y += (n - x); x = n; } if (x == n && y == m) { if (a[y][0] == 0) a[y][0] = 1, l[0]++; if (a[x][1] == 0) a[x][1] = 1, l[1]++; if (a[y + 1][0] == a[y][0] || a[y - 1][0] == a[y][0] || a[x + 1][1] == a[x][1] || a[x - 1][1] == a[x][1]) return puts("-1"); t = 1; } else if (x == n) { if (a[y][0] == 0) a[y][0] = 1, l[0]++; if (a[y + 1][0] == a[y][0] || a[y - 1][0] == a[y][0]) return puts("-1"); t = 2; } else { if (a[x][1] == 0) a[x][1] = 1, l[1]++; if (a[x + 1][1] == a[x][1] || a[x - 1][1] == a[x][1]) return puts("-1"); t = 3; } } } puts("-1"); return 0; }
8
#include <bits/stdc++.h> using namespace std; long long int power_mod(long long int A, long long int B, long long int M) { long long int res = 1; while (B > 0) { if (B % 2 == 1) { res = (res * A) % M; } A = (A * A) % M; B = B / 2; } return res; } long long int maxx(long long int a, long long int b) { if (a > b) return a; else return b; } long long int minn(long long int a, long long int b) { if (a < b) return a; else return b; } map<long long int, long long int> dp; vector<int> v; long long int f(int x) { long long int ans = power_mod(2, x - 1, 1000000007); if (x == 1) return 1; if (x == 2) return 1; if (x == 3) return 3; if (dp[x] != 0) return dp[x]; for (int i = v.size() - 1; i >= 0; i--) { if (v[i] == 1) continue; if (x % v[i] == 0) ans = (ans - f(x / v[i]) + 1000000007) % 1000000007; } return dp[x] = ans; } int main() { int n, i, x, y; long long int ans; ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); cin >> x >> y; if (y % x != 0) { cout << "0" << endl; } else { n = y / x; for (i = 1; i * i <= n; i++) { if (n % i == 0) { v.push_back(i); if (i * i != n) { v.push_back(n / i); } } } sort(v.begin(), v.end()); ans = f(n); cout << ans << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long logN = 20; const long long N = 100005; const long long M = 1000000007; const long long INF = 1e12; const long long dx[4] = {1, 0, -1, 0}; const long long dy[4] = {0, 1, 0, -1}; long long A[2005]; long long dp[2005][2005]; long long beg[2005], la[2005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; for (long long i = 1; i < n + 1; i++) cin >> A[i]; for (long long i = 1; i < n + 1; i++) { long long l1 = 0, l2 = 0; for (long long j = i; j < n + 1; j++) { if (A[j] == 1) { l1 = max(l1, l2) + 1; } else { l2++; } dp[i][j] = max(l1, l2); } } long long cnt = 0; for (long long i = 1; i < n + 1; i++) { if (A[i] == 1) cnt++; beg[i] = cnt; } cnt = 0; for (long long i = n; i >= 1; i--) { if (A[i] == 2) cnt++; la[i] = cnt; } long long ans = 0; for (long long i = 1; i < n + 1; i++) for (long long j = i; j < n + 1; j++) { ans = max(ans, beg[i - 1] + dp[i][j] + la[j + 1]); } cout << ans << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<int> even; vector<int> odd; vector<int> ev; vector<int> od; map<int, int> ma; map<int, int> mm; int arr[200005]; int brr[200005]; int main() { int m, n; scanf("%d %d", &m, &n); int i, j; for (i = 0; i < m; i++) { scanf("%d", &arr[i]); brr[i] = arr[i]; } int re = m / 2; for (i = 0; i < m; i++) { int x = arr[i]; if (ma[x]) continue; ma[x]++; if (x % 2 == 0) even.push_back(i); else odd.push_back(i); mm[i]++; } int cou = 0; if (even.size() > re) { int xx = even.size(); for (i = 0; i < (xx - re); i++) { int ind = even[even.size() - 1]; od.push_back(ind); even.pop_back(); } } if (odd.size() > re) { int xx = odd.size(); for (i = 0; i < (xx - re); i++) { int ind = odd[odd.size() - 1]; ev.push_back(ind); odd.pop_back(); } } if (even.size() + ev.size() < re) { for (i = 0; i < m; i++) { if (mm[i] == 0) { ev.push_back(i); mm[i]++; if (ev.size() + even.size() == re) break; } } } if (odd.size() + od.size() < re) { for (i = 0; i < m; i++) { if (mm[i] == 0) { od.push_back(i); mm[i]++; if (od.size() + odd.size() == re) break; } } } for (i = 1; i <= n; i++) { if (ma[i]) continue; if (ev.size() == 0 && od.size() == 0) break; if (i % 2 == 0) { if (ev.size() > 0) { int ind = ev[ev.size() - 1]; brr[ind] = i; ev.pop_back(); } } else if (i % 2 == 1) { if (od.size() > 0) { int ind = od[od.size() - 1]; brr[ind] = i; od.pop_back(); } } } if (od.size() > 0 || ev.size() > 0) { printf("-1\n"); return 0; } for (i = 0; i < m; i++) { if (brr[i] != arr[i]) cou++; } printf("%d\n", cou); for (i = 0; i < m; i++) printf("%d ", brr[i]); }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, k; vector<int> primes; cin >> n >> k; primes.push_back(2); for (int i = 3; i <= n; ++i) { bool prime = true; for (int j = 2; j < i; ++j) if (i % j == 0) { prime = false; break; } if (prime) primes.push_back(i); } int s = 0; for (int i = 0; i < primes.size(); ++i) { for (int j = 0; j < primes.size() - 1; ++j) if (primes[i] == primes[j] + primes[j + 1] + 1) { s++; break; } } cout << (s >= k ? "YES" : "NO"); return 0; }
1
#include <bits/stdc++.h> using namespace std; double l, p, q; int main() { while (scanf("%lf%lf%lf", &l, &p, &q) != EOF) { double k = p; double ans = l / (p + q) * k; printf("%.5lf\n", ans); } }
0
#include <bits/stdc++.h> char arr[290000]; int in[290000]; int main() { int a, b, c, d; scanf("%d", &a); getchar(); int i; gets(arr); int s; int len = strlen(arr); int p = 0; for (i = 0; i < a; i++) { scanf("%d", in + i); } int min = INT_MAX; for (i = 0; i < a; i++) { if (arr[i] == 'R' && arr[i + 1] == 'L') { int res = (in[i + 1] + in[i]) / 2; if (in[i + 1] - res < min) { min = in[i + 1] - res; } } } if (min == INT_MAX) { printf("-1"); } else printf("%d", min); }
1
#include <bits/stdc++.h> int l, r, x, y, s; int main() { scanf("%d%d%d%d%d", &l, &r, &x, &y, &s); for (int i = x; i <= y; i++) if (1ll * l <= 1ll * s * i && 1ll * s * i <= 1ll * r) puts("YES"), exit(0); puts("NO"); return 0; }
2
#include <bits/stdc++.h> using namespace std; struct Graph { int vertex, edge, rmax; int head[1001]; bool visited[1001]; struct Node { int next; int v; int l, r; Node(){}; Node(int a, int b, int lv, int rv) : next(a), v(b), l(lv), r(rv){}; } adjlist[6001]; void init(int n) { memset(head, -1, sizeof(head)); memset(visited, 0, sizeof(visited)); vertex = n, edge = rmax = 0; } void insert(int u, int v, int la, int ra) { rmax = max(rmax, ra); adjlist[edge] = Node(head[u], v, la, ra); head[u] = edge++; adjlist[edge] = Node(head[v], u, la, ra); head[v] = edge++; } bool DFS(int v, int l, int r) { if (v == vertex) return true; visited[v] = 1; for (int i = head[v]; i != -1; i = adjlist[i].next) if (!visited[adjlist[i].v]) { int vl = adjlist[i].l; int vr = adjlist[i].r; if (l >= vl && l <= vr && r >= vl && r <= vr) { int rt = min(r, vr); if (DFS(adjlist[i].v, l, rt)) return true; } } return false; } void check() { int lmax = 0; for (int i = 0; i < edge; i++) { int low, mid, high; low = adjlist[i].l; high = rmax; while (low <= high && low >= adjlist[i].l) { mid = (low + high) / 2; memset(visited, 0, sizeof(visited)); if (DFS(1, adjlist[i].l, mid)) { low = mid + 1; lmax = max(lmax, mid - adjlist[i].l + 1); } else high = mid - 1; } } if (lmax) printf("%d\n", lmax); else puts("Nice work, Dima!"); } } g; int main() { int n, m; int u, v, l, r; while (~scanf("%d %d", &n, &m)) { g.init(n); while (m--) { scanf("%d %d %d %d", &u, &v, &l, &r); g.insert(u, v, l, r); } g.check(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; template <typename T> void Read(T &cn) { char c; int sig = 1; while (!isdigit(c = getchar())) if (c == '-') sig = -1; cn = c - 48; while (isdigit(c = getchar())) cn = cn * 10 + c - 48; cn *= sig; } template <typename T> void Write(T cn) { if (cn < 0) { putchar('-'); cn = 0 - cn; } int wei = 0; T cm = 0; int cx = cn % 10; cn /= 10; while (cn) wei++, cm = cm * 10 + cn % 10, cn /= 10; while (wei--) putchar(cm % 10 + 48), cm /= 10; putchar(cx + 48); } template <typename T> void Max(T &cn, T cm) { cn = cn < cm ? cm : cn; } template <typename T> void Min(T &cn, T cm) { cn = cn < cm ? cn : cm; } struct dian2 { long double x, y; inline friend dian2 operator+(dian2 a, dian2 b) { dian2 guo; guo.x = a.x + b.x; guo.y = a.y + b.y; return guo; } inline friend dian2 operator-(dian2 a, dian2 b) { dian2 guo; guo.x = a.x - b.x; guo.y = a.y - b.y; return guo; } }; struct dian { int x, y; void mk(int cn, int cm) { x = cn; y = cm; } inline friend bool operator<(dian a, dian b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } inline friend dian operator+(dian a, dian b) { dian guo; guo.x = a.x + b.x; guo.y = a.y + b.y; return guo; } inline friend dian operator-(dian a, dian b) { dian guo; guo.x = a.x - b.x; guo.y = a.y - b.y; return guo; } void outit() { printf("%d %d\n", x, y); } }; struct xian { dian2 A, B; }; dian a[100000 * 16 + 1]; int alen; int n; int zhan[100000 * 16 + 1]; int zlen1, zlen2; long double ansr; int ansA, ansB, ansC; dian mk(int cn, int cm) { dian guo; guo.mk(cn, cm); return guo; } void zeng(int cn, int cm) { if (0 <= cn && cn <= 100000 && 0 <= cm && cm <= 100000) a[++alen].mk(cn, cm); } long long chaji(dian A, dian B, dian C) { long long dx1 = B.x - A.x, dx2 = C.x - A.x; long long dy1 = B.y - A.y, dy2 = C.y - A.y; return dx1 * dy2 - dx2 * dy1; } dian2 zh(dian A, dian B) { dian2 guo; guo.x = (A.x + B.x) / 2.0; guo.y = (A.y + B.y) / 2.0; return guo; } dian2 zhuan(dian A) { dian2 guo; guo.x = -A.y; guo.y = A.x; return guo; } dian2 jiao(xian L1, xian L2) { long double kB = (L1.A.x * L1.B.y - L1.B.x * L1.A.y + L2.A.y * L1.B.x - L2.A.x * L1.B.y) / (L2.B.x * L1.B.y - L2.B.y * L1.B.x); dian2 guo; guo.x = L2.A.x + kB * L2.B.x; guo.y = L2.A.y + kB * L2.B.y; return guo; } dian2 suan_O(dian A, dian B, dian C) { xian L1, L2; L1.A = zh(A, B); L1.B = zhuan(A - B); L2.A = zh(B, C); L2.B = zhuan(B - C); dian2 guo = jiao(L1, L2); return guo; } long double suan_jlf(dian2 A, dian B) { return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); } int pan(dian2 A, long double R) { if (zlen2 >= 4000) return 1; for (int i = 1; i <= zlen2; i++) if (suan_jlf(A, a[zhan[i]]) - R >= 0.00000000001) return 0; return 1; } void jian(int cn, int cm, int cx) { if (chaji(a[cn], a[cm], a[cx]) == 0) return; dian2 linO = suan_O(a[cn], a[cm], a[cx]); long double linr = suan_jlf(linO, a[cn]); if (linr <= ansr) return; if (pan(linO, linr)) ansr = linr, ansA = cn, ansB = cm, ansC = cx; } int main() { Read(n); alen = 0; for (int i = 1; i <= n; i++) { int bx, by, bz; Read(bx); Read(by); Read(bz); zeng(bx - bz, by); zeng(bx + bz, by); zeng(bx, by - bz); zeng(bx, by + bz); if (bx + by <= bz) zeng(0, 0); if (bx + 100000 - by <= bz) zeng(0, 100000); if (100000 - bx + by <= bz) zeng(100000, 0); if (100000 - bx + 100000 - by <= bz) zeng(100000, 100000); if (bx < bz) zeng(0, by - (bz - bx)), zeng(0, by + (bz - bx)); if (by < bz) zeng(bx - (bz - by), 0), zeng(bx + (bz - by), 0); if (100000 - bx < bz) zeng(100000, by + (bz + bx - 100000)), zeng(100000, by - (bz + bx - 100000)); if (100000 - by < bz) zeng(bx + (bz + by - 100000), 100000), zeng(bx - (bz + by - 100000), 100000); } sort(a + 1, a + alen + 1); zlen1 = 2; zhan[1] = 1; zhan[2] = 2; for (int i = 3; i <= alen; i++) { while (zlen1 >= 2 && chaji(a[zhan[zlen1 - 1]], a[zhan[zlen1]], a[i]) <= 0) zlen1--; zhan[++zlen1] = i; } zlen2 = zlen1 + 1; zhan[zlen2] = alen - 1; for (int i = alen - 2; i >= 1; i--) { while (zlen2 - zlen1 >= 1 && chaji(a[zhan[zlen2 - 1]], a[zhan[zlen2]], a[i]) <= 0) zlen2--; zhan[++zlen2] = i; } ansr = 0; jian(zhan[zlen2 - 1], zhan[1], zhan[2]); for (int i = 1; i <= zlen2 - 2; i++) jian(zhan[i], zhan[i + 1], zhan[i + 2]); a[ansA].outit(); a[ansB].outit(); a[ansC].outit(); return 0; }
10
#include <bits/stdc++.h> using namespace std; long long m; int k; long long l, r; long long dp[100][100]; long long DP(int x, int z) { if (z == 0) return 1; if (x == 0) return z == 0 ? 1 : 0; if (dp[x][z] != -1) return dp[x][z]; long long ret = 0; ret += DP(x - 1, z) + DP(x - 1, z - 1); return dp[x][z] = ret; } long long cal(long long x) { int s[70], ct = 0; while (x) s[ct++] = x % 2, x >>= 1; reverse(s, s + ct); long long ret = 0; memset(dp, -1, sizeof dp); int nowk = 0; for (int i = 0; i < ct; i++) { if (s[i] == 1) { ret += DP(ct - 1 - i, k - nowk), nowk++; } } return ret; } int main() { while (cin >> m >> k) { l = 1, r = (long long)1e18; while (l < r) { long long mid = (l + r) / 2; if (cal(2 * mid + 1) - cal(mid + 1) >= m) r = mid; else l = mid + 1; } if (cal(2 * l + 1) - cal(l + 1) == m) cout << l << endl; else cout << l - 1 << endl; } return 0; }
6