solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; int main() { uint64_t a, b, amount = 0, buff; cin >> a >> b; while (a != 0) { if (a > b) { amount += a / b; a %= b; } else { amount++; buff = b; b = a; a = buff - b; } } cout << amount << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { string a; int i, x; set<char> b; getline(cin, a); for (i = 1; i < a.size() - 1; i += 3) { b.insert(a[i]); } cout << b.size() << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; bool cmp(const int &a, const int &b) { return a < b; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int lcm(int a, int b) { return a * (b / gcd(a, b)); } int dp[505][505]; int graph[505][505]; int arr[505]; unsigned long long int ans[505]; bool alive[505]; int main() { int i, j, k; int n; scanf("%d", &n); for (i = 0; i < n; i++) for (j = 0; j < n; j++) scanf("%d", &graph[i][j]); for (i = 0; i < n; i++) scanf("%d", arr + i), --arr[i]; int z; for (i = 0; i < n; i++) for (j = 0; j < n; j++) dp[i][j] = i == j ? 0 : 1000000007; for (z = n - 1; z >= 0; z--) { int x = arr[z]; alive[x] = 1; for (i = 0; i < n; i++) if (alive[i]) dp[i][x] = graph[i][x], dp[x][i] = graph[x][i]; for (i = 0; i < n; i++) for (j = 0; j < n; j++) dp[j][x] = min(dp[j][x], (dp[j][i] + dp[i][x])), dp[x][j] = min(dp[x][j], (dp[x][i] + dp[i][j])); for (i = 0; i < n; i++) for (j = 0; j < n; j++) dp[i][j] = min(dp[i][j], (dp[i][x] + dp[x][j])); unsigned long long int sum = 0; for (i = 0; i < n; i++) for (j = 0; j < n; j++) if (alive[i] && alive[j]) sum += dp[i][j]; ans[z] = sum; } for (i = 0; i < n; i++) cout << ans[i] << ' '; return 0; }
9
#include <bits/stdc++.h> using namespace std; template <class T, class U> bool cmax(T &a, const U &b) { return a < b ? a = b, 1 : 0; } template <class T, class U> bool cmin(T &a, const U &b) { return b < a ? a = b, 1 : 0; } void _BG(const char *s) { cerr << s << endl; }; template <class T, class... TT> void _BG(const char *s, T a, TT... b) { for (int c = 0; *s && (c || *s != ','); ++s) { cerr << *s; for (char x : "([{") c += *s == x; for (char x : ")]}") c -= *s == x; } cerr << " = " << a; if (sizeof...(b)) { cerr << ", "; ++s; } _BG(s, b...); } bool RD() { return 1; } bool RD(char &a) { return scanf(" %c", &a) == 1; } bool RD(char *a) { return scanf("%s", a) == 1; } bool RD(double &a) { return scanf("%lf", &a) == 1; } bool RD(int &a) { return scanf("%d", &a) == 1; } bool RD(long long &a) { return scanf("%lld", &a) == 1; } template <class T, class... TT> bool RD(T &a, TT &...b) { return RD(a) && RD(b...); } void PT(const char &a) { putchar(a); } void PT(char const *const &a) { fputs(a, stdout); } void PT(const double &a) { printf("%.16f", a); } void PT(const int &a) { printf("%d", a); } void PT(const long long &a) { printf("%lld", a); } template <char s = ' ', char e = '\n'> void PL() { if (e) PT(e); } template <char s = ' ', char e = '\n', class T, class... TT> void PL(const T &a, const TT &...b) { PT(a); if (sizeof...(b) && s) PT(s); PL<s, e>(b...); } struct node { static node *nil; node *p, *c[2], *mn; int k, v; node(int a, int b) : p(nil), c{nil, nil}, mn(this), k(a), v(b) {} node *pull() { if (this == nil) return this; mn = this; for (int i(0); i < (2); ++i) if (c[i]->mn->v < mn->v) mn = c[i]->mn; return this; } int d() { return p->c[1] == this; } void lk(int i, node *x) { c[i] = x; x->p = this; } node *cc(int i) { node *x = c[i]; lk(i, nil); nil->lk(0, x); this->pull(); return x; } node *rot() { int i = d(); node *x = p; p->lk(i, c[i ^ 1]); p->pull(); p->p->lk(p->d(), this); lk(i ^ 1, x); return this; } node *spl() { for (; p != nil; rot()) if (p->p != nil) (p->d() == d() ? p : this)->rot(); return this->pull(); } node *la() { return c[1] != nil ? c[1]->la() : this->spl(); } node *fi() { return c[0] != nil ? c[0]->fi() : this->spl(); } }; node *node::nil = new node(-1, 1e9 + 987); int ans; node *meld(node *a, node *b) { if (a == node::nil) return b; if (b == node::nil) return a; a = a->la(); b = b->fi(); if (a->k == b->k) { a->v += b->v - 2 * ans; node *t = b->cc(1); delete b; b = t; } a->lk(1, b); return a->pull(); } node *rm(node *t) { if (t == node::nil) return t; t = t->mn->spl(); if (t->v <= 2 * ans) { node *x = meld(rm(t->cc(0)), rm(t->cc(1))); delete t; t = x; } return t; } int main() { node *b, *t, *e; b = t = e = node::nil; { vector<pair<int, int> > a; char c; while (RD(c)) { c -= 'a'; if (!((int)(a).size()) || ((a).back()).first != c) a.push_back({c, 1}); else ++((a).back()).second; } b = new node(a[0].first, a[0].second); int i = 1; for (; i < ((int)(a).size()) - 1; ++i) t = meld(t, new node(a[i].first, a[i].second)); if (i < ((int)(a).size())) e = new node(a[i].first, a[i].second); } while (b != node::nil && e != node::nil) { if (t != node::nil) { t = t->mn->spl(); int k = min((t->v - 2 * ans + 1) / 2, min(b->v, e->v)); ans += k; t = rm(t); if ((b->v -= k) == 0) { delete b; b = node::nil; if (t != node::nil) { b = t->fi(); t = b->cc(1); b->v -= 2 * ans; } } if ((e->v -= k) == 0) { delete e; e = node::nil; if (t != node::nil) { e = t->la(); t = e->cc(0); e->v -= 2 * ans; } } if (b != node::nil && t != node::nil && (t = t->fi())->k == b->k) { b->v += t->v - 2 * ans; node *x = t->cc(1); delete t; t = x; } if (e != node::nil && t != node::nil && (t = t->la())->k == e->k) { e->v += t->v - 2 * ans; node *x = t->cc(0); delete t; t = x; } } else { ans += min(b->v, e->v) * (b->k != e->k); e = node::nil; } } PL(ans); }
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int> > adj(n + 1); vector<int> indeg(n + 1, 0); vector<int> outdeg(n + 1, 0); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); indeg[y]++; indeg[x]++; outdeg[x]++; outdeg[y]++; } int cnt = -1; int ram = 1; while (ram) { cnt++; queue<int> q; for (int i = 1; i <= n; i++) { if (indeg[i] + outdeg[i] == 2) q.push(i); } ram = q.size(); while (!q.empty()) { int a = q.front(); q.pop(); indeg[a]--; outdeg[a]--; for (auto it : adj[a]) { indeg[it]--; outdeg[it]--; } } } cout << cnt; return 0; }
4
#include <bits/stdc++.h> using namespace std; int sq(int p) { if (p % 2 == 0) return (p / 2) * (p / 2); else return (p / 2) * (p + 1) / 2; } int main(void) { int n, curr = 0; cin >> n; while (sq(curr) < n) { curr++; } cout << curr; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int M = 1010; int n, m, flag; char ma[M][M]; bool vis[M][M][5][5]; void dfs(int x, int y, int c, int t) { if (x < 1 || y < 1 || x > n || y > m) return; if (t > 2 || ma[x][y] == '*' || vis[x][y][c][t] || flag) return; vis[x][y][c][t] = 1; if (ma[x][y] == 'T') { flag = 1; return; } if (c) { dfs(x, y - 1, c, t); dfs(x, y + 1, c, t); } else { dfs(x - 1, y, c, t); dfs(x + 1, y, c, t); } dfs(x, y, !c, t + 1); } int main() { int x, y; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> ma[i][j]; if (ma[i][j] == 'S') { x = i; y = j; } } } dfs(x, y, 0, 0); dfs(x, y, 1, 0); if (flag) printf("YES\n"); else printf("NO\n"); }
8
#include <bits/stdc++.h> using namespace std; long long arr[50009][9]; int main() { long long n, m, ans = 0; cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> arr[i][j]; for (int i = 2; i <= n; i++) arr[i][1] += arr[i - 1][1]; for (int i = 2; i <= m; i++) { for (int j = 1; j <= n; j++) { if (j != 1) { arr[j][i] += max(arr[j][i - 1], arr[j - 1][i]); } else arr[j][i] += arr[j][i - 1]; } } for (int i = 1; i <= n; i++) cout << arr[i][m] << " "; return 0; }
5
#include <bits/stdc++.h> using namespace std; char aa[5001]; int n, m; bool avail[5001]; int L[5001], R[5001], U[5001], D[5001]; int cnt[5001]; int encode(int i, int j) { if (i < 0 || i >= n || j < 0 || j >= m) return -1; return i * m + j; } void del(int cur) { int ll = L[cur], rr = R[cur], uu = U[cur], dd = D[cur]; if (ll != -1) R[ll] = rr; if (rr != -1) L[rr] = ll; if (uu != -1) D[uu] = dd; if (dd != -1) U[dd] = uu; avail[cur] = 0; } int main() { int i, j, ii, jj; while (scanf("%d%d", &n, &m) == 2) { for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { char c; scanf(" %c", &c); aa[i * m + j] = c; L[i * m + j] = encode(i, j - 1); R[i * m + j] = encode(i, j + 1); U[i * m + j] = encode(i - 1, j); D[i * m + j] = encode(i + 1, j); } } memset(cnt, 0, sizeof(cnt)); for (ii = 0; ii < n; ii++) { for (jj = 0; jj < m; jj++) { int now = encode(ii, jj); if (aa[now] == '.') continue; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { L[i * m + j] = encode(i, j - 1); R[i * m + j] = encode(i, j + 1); U[i * m + j] = encode(i - 1, j); D[i * m + j] = encode(i + 1, j); avail[i * m + j] = 1; } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { int cur = encode(i, j); if (aa[cur] == '.') del(cur); } } int xx = 0; while (now != -1) { int temp = now; if (aa[now] == 'L') { now = L[now]; } else if (aa[now] == 'R') { now = R[now]; } else if (aa[now] == 'U') { now = U[now]; } else if (aa[now] == 'D') { now = D[now]; } del(temp); xx++; } cnt[xx]++; } } for (i = 5000; i >= 0; i--) { if (cnt[i]) { printf("%d %d\n", i, cnt[i]); break; } } } }
15
#include <bits/stdc++.h> using namespace std; inline long long read() { long long num = 0, neg = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') neg = -1; c = getchar(); } while (isdigit(c)) { num = (num << 3) + (num << 1) + c - '0'; c = getchar(); } return num * neg; } int n, a[100010], ans = -1, res = 0; int main() { n = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) { int now = a[i], len = 1; while (a[i + 1] == a[i] && i < n) i++, len++; if (now > ans) ans = now, res = len; if (now == ans) res = max(res, len); } cout << res; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; map<int, vector<pair<int, int>>> mp; vector<pair<int, int>> shoes; long long memo[MAXN][4]; long long calcState(int ind, int mask) { if (memo[ind][mask] != -1) { return memo[ind][mask]; } if (ind == shoes.size()) { return 0; } auto it = mp.find(shoes[ind].first); long long answer = calcState(ind + 1, 0); if (it != mp.end()) { bool mode = false; if (ind != shoes.size() - 1 && shoes[ind].first + 1 == shoes[ind + 1].first) { mode = true; } for (int i = 0; i < it->second.size(); i++) { if (it->second[i].first < shoes[ind].second) continue; answer = max(answer, shoes[ind].second + calcState(ind + 1, (1 << i) * mode)); } } it = mp.find(shoes[ind].first - 1); if (it != mp.end()) { for (int i = 0; i < it->second.size(); i++) { if (((mask >> i) & 1) == 1) continue; if (it->second[i].first < shoes[ind].second) continue; answer = max(answer, shoes[ind].second + calcState(ind + 1, 0)); } } memo[ind][mask] = answer; return answer; } long long value; vector<pair<int, int>> answer; map<pair<int, int>, int> shoe2Index; void recover(int ind, int mask) { if (ind == shoes.size()) { return; } auto it = mp.find(shoes[ind].first); if (0 + calcState(ind + 1, 0) == value) { value -= 0; recover(ind + 1, 0); return; } if (it != mp.end()) { bool mode = false; if (ind != shoes.size() - 1 && shoes[ind].first + 1 == shoes[ind + 1].first) { mode = true; } for (int i = 0; i < it->second.size(); i++) { if (it->second[i].first < shoes[ind].second) continue; if (shoes[ind].second + calcState(ind + 1, (1 << i) * mode) == value) { answer.push_back({it->second[i].second, shoe2Index[shoes[ind]]}); value -= shoes[ind].second; recover(ind + 1, (1 << i) * mode); return; } } } it = mp.find(shoes[ind].first - 1); if (it != mp.end()) { for (int i = 0; i < it->second.size(); i++) { if (((mask >> i) & 1) == 1) continue; if (it->second[i].first < shoes[ind].second) continue; if (shoes[ind].second + calcState(ind + 1, 0) == value) { answer.push_back({it->second[i].second, shoe2Index[shoes[ind]]}); value -= shoes[ind].second; recover(ind + 1, 0); return; } } } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) { int c, s; cin >> c >> s; shoes.push_back({s, c}); shoe2Index[{s, c}] = i; } sort(shoes.begin(), shoes.end()); int m; cin >> m; for (int i = 0; i < m; i++) { int d, l; cin >> d >> l; mp[l].push_back({d, i}); } for (auto it = mp.begin(); it != mp.end(); it++) { sort(it->second.begin(), it->second.end(), greater<pair<int, int>>()); while (it->second.size() > 2) it->second.pop_back(); } memset(memo, -1, sizeof(memo)); value = calcState(0, 0); recover(0, 0); cout << calcState(0, 0) << '\n'; cout << answer.size() << '\n'; for (pair<int, int> item : answer) { cout << item.first + 1 << " " << item.second + 1 << '\n'; } }
17
#include <bits/stdc++.h> using namespace std; string s[505]; bool vis[505][505]; long long n, m, k; bool chck(long long a, long long b) { if (a > 0 and s[a - 1][b] == '.') return 1; if (a < m - 1 and s[a + 1][b] == '.') return 1; if (b > 0 and s[a][b - 1] == '.') return 1; if (b < m - 1 and s[a][b + 1] == '.') return 1; return 0; } void dfs(long long a, long long b) { if (a < 0 or a > n - 1 or b < 0 or b > m - 1 or s[a][b] != '.' or vis[a][b]) return; vis[a][b] = 1; dfs(a + 1, b); dfs(a - 1, b); dfs(a, b - 1); dfs(a, b + 1); if (k) { s[a][b] = 'X'; k--; } } int main(void) { ios_base::sync_with_stdio(0); long long a, b, c, d, e, f; long long abc[100005]; cin >> n >> m >> k; memset(vis, 0, sizeof(vis)); for (a = 0; a < n; a++) cin >> s[a]; for (a = 0; a < n; a++) { for (b = 0; b < m; b++) { if (s[a][b] == '.') break; } if (b < m) break; } dfs(a, b); for (a = 0; a < n; a++) cout << s[a] << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; int grid[10][10]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> grid[i][j]; } } for (int k = 0; k < n; ++k) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { grid[i][j] = min(grid[i][j], grid[i][k] + grid[k][j]); } } } int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { ans = max(ans, grid[i][j]); } } cout << ans << endl; }
12
#include <bits/stdc++.h> using namespace std; inline int setBit(int N, int pos) { return N = N | (1 << pos); } inline int resetBit(int N, int pos) { return N = N & ~(1 << pos); } inline bool checkBit(int N, int pos) { return (bool)(N & (1 << pos)); } struct Vector { long long int x, y; int id; long long int val() { return (x * x + y * y); } void print() { cerr << 38 << ": " << "x" << " = " << x << " " << "y" << " = " << y << '\n'; } }; inline double angleBetween(Vector A, Vector O, Vector B) { Vector t1, t2; t1.x = A.x - O.x; t1.y = A.y - O.y; t2.x = B.x - O.x; t2.y = B.y - O.y; double theta = (atan2(t2.y, t2.x) - atan2(t1.y, t1.x)); if (theta < 0) theta += 2 * acos(-1.00); if (fabs(theta - acos(-1.00)) < 1e-7) return acos(-1.00); if (theta > acos(-1.00) + 1e-7) theta = 2 * acos(-1.00) - theta; return theta; } vector<Vector> V; vector<pair<int, int> > E[1000000 + 10]; int sltn[1000000 + 10]; void dfs(int node, int state) { if (E[node].size() == 0) { sltn[node] = state; return; } for (auto v : E[node]) dfs(v.first, state * v.second); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x, y; scanf("%d %d", &x, &y); V.push_back({x, y, i}); } if (V.size() == 1) { puts("1"); return 0; } int cnt = n; while (V.size() >= 3) { vector<Vector> tmp; for (int i = 0; i < 3; i++) tmp.push_back(V.back()), V.pop_back(); bool done = 0; for (int i = 0; i < 3; i++) { for (int j = -1; j <= 1; j += 2) { for (int k = i + 1; k < 3; k++) { for (int q = -1; q <= 1; q += 2) { Vector A = tmp[i]; Vector B = tmp[k]; A.x *= j; A.y *= j; B.x *= q; B.y *= q; if (A.val() == 0 || B.val() == 0 || angleBetween(A, {0, 0}, B) + 1e-7 >= 2 * acos(-1.00) / 3) { done = true; int taken[3] = {0}; taken[i] = taken[k] = 1; for (int bla = 0; bla < 3; bla++) { if (!taken[bla]) V.push_back(tmp[bla]); } cnt++; Vector cur = {A.x + B.x, A.y + B.y, cnt}; V.push_back(cur); assert(cur.val() <= 1e12); E[cnt].push_back({A.id, j}); E[cnt].push_back({B.id, q}); break; } } if (done) break; } if (done) break; } if (done) break; } assert(done); } bool done = false; double lim = (1.5e6) * (1.5e6); for (int i = -1; i < 2; i += 2) { for (int j = -1; j < 2; j += 2) { Vector A = V[0]; Vector B = V[1]; A.x *= i; A.y *= i; B.x *= j; B.y *= j; Vector tmp = {A.x + B.x, A.y + B.y}; if ((tmp.x * tmp.x + tmp.y * tmp.y) <= (lim)) { cnt++; E[cnt].push_back({A.id, i}); E[cnt].push_back({B.id, j}); done = true; break; } } if (done) break; } dfs(cnt, 1); for (int i = 1; i <= n; i++) { if (i > 1) printf(" "); printf("%d", sltn[i]); } puts(""); return 0; }
15
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; long long int q = 0; for (int i = 0; i < n; i++) { cin >> a[i]; q += a[i]; } long long int e = 0; if (q % 2 != 0) q++; for (int i = 0; i < n; i++) { e += a[i]; if (e >= q / 2) { cout << i + 1; return 0; } } }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 2020; int dp[maxn][maxn]; string st[] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"}; char s[maxn][10]; char ans[maxn]; int cal(int a, int b) { int cnt = 0; for (int i = 0; i < 7; i++) { if (s[a][i] == '0') { if (st[b][i] == '1') cnt++; } else if (st[b][i] == '0') return -1; } return cnt; } int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%s", s[i]); memset(dp, 0, sizeof(dp)); for (int i = 0; i < 10; i++) { int id = cal(n, i); if (id == -1 || k - id < 0) continue; dp[n][k - id] = 1; } for (int i = n - 1; i >= 1; i--) { for (int j = 0; j <= k; j++) { for (int i0 = 0; i0 <= 9; i0++) { int id = cal(i, i0); if (id == -1 || dp[i][j] == 1) continue; dp[i][j] = dp[i + 1][j + id]; } } } if (dp[1][0] == 0) puts("-1"); else { dp[n + 1][k] = 1; int tot = 0, now = 0; for (int i = 1; i <= n; i++) { for (int i0 = 9; i0 >= 0; i0--) { int tmp = cal(i, i0); if (tmp == -1) continue; if (tmp + now <= k && dp[i + 1][now + tmp] == 1) { now += tmp; ans[tot++] = i0 + '0'; break; } } } puts(ans); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c, d; cin >> a >> b >> c >> d; if (a == d) { if (c != 0 && a == 0) cout << "0"; else cout << "1"; } else { cout << "0"; } }
3
#include <bits/stdc++.h> using namespace std; long long int buildstack(vector<long long int> adj[], stack<long long int>& s, bool visited[], long long int& f, long long int u, long long int p) { visited[u] = true; s.push(u); for (auto itr = adj[u].begin(); itr != adj[u].end(); itr++) { if (*itr == p) continue; if (visited[*itr] == true) { f = 1; return *itr; } long long int x = buildstack(adj, s, visited, f, *itr, u); if (f == 1) return x; } s.pop(); return -1; } void dfs(vector<long long int> adj[], bool visited[], long long int dist[], long long int x, long long int ct) { visited[x] = true; dist[x] = ct; for (auto itr = adj[x].begin(); itr != adj[x].end(); itr++) { if (visited[*itr] != true) dfs(adj, visited, dist, *itr, ct + 1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; vector<long long int> adj[n]; for (long long int i = 0; i < n; i++) { long long int x, y; cin >> x >> y; x--; y--; adj[x].push_back(y); adj[y].push_back(x); } bool visited[n]; for (long long int i = 0; i < n; i++) visited[i] = false; stack<long long int> s; long long int f = -1; long long int stop = buildstack(adj, s, visited, f, 0, -1); stack<long long int> final; long long int dist[n]; for (long long int i = 0; i < n; i++) dist[i] = -1; while (s.empty() == false) { long long int x = s.top(); dist[x] = 0; final.push(x); s.pop(); if (x == stop) break; } for (long long int i = 0; i < n; i++) { visited[i] = false; if (dist[i] == 0) visited[i] = true; } while (final.empty() == false) { long long int x = final.top(); final.pop(); dfs(adj, visited, dist, x, 0); } for (long long int i = 0; i < n; i++) cout << dist[i] << " "; cout << "\n"; return 0; }
8
#include <bits/stdc++.h> inline int query(int x) { printf("? %d\n", x); std::fflush(stdout); int res; scanf("%d", &res); return res; } int n; inline bool chk(int x) { --x; int t1 = query(x + 1), t2 = query((x + n / 2) % n + 1); if (t1 == t2) { printf("! %d\n", x + 1); std::fflush(stdout); exit(0); } return t1 - t2 < 0; } int main() { scanf("%d", &n); if (n % 4) { printf("! -1\n"); std::fflush(stdout); return 0; } bool t = chk(1); int l = 2, r = n / 2, ans = -1; while (l <= r) { int mid = (l + r) >> 1; if (chk(mid) == t) ans = mid, l = mid + 1; else r = mid - 1; } return 0; }
12
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:5000000000") const long long mod = 1000000007; long long Inf = (long long)2e9; long long LINF = (long long)1e18 + 1e17; using namespace std; int main() { ios::sync_with_stdio(false); long long n, h; cin >> n >> h; if (h >= 2e9) { long long l = 0, r = 2e9; while (r - l > 1) { long long m = (r + l) / 2; if (m * (m + 1) / 2 >= n) { r = m; } else { l = m; } } cout << r; return 0; } if (n <= h * (h + 1) / 2) { long long l = 0, r = h; while (r - l > 1) { long long m = (r + l) / 2; if (m * (m + 1) / 2 >= n) r = m; else l = m; } cout << r; return 0; } long long l = 0, r = 2e9; while (r - l > 1) { long long m = (r + l) / 2; long long cnt = (h + m) * (h + m + 1) / 2 - h * (h - 1) / 2; long long tmp = (h + m) * (h + m - 1) / 2; if (cnt + tmp <= n) l = m; else r = m; } long long cnt = (h + l) * (h + l + 1) / 2 - h * (h - 1) / 2; long long tmp = (h + l) * (h + l - 1) / 2; n = n - cnt - tmp; long long ans = l + 1 + h + l - 1; ans += (n + (h + l - 1)) / (h + l); cout << ans; return 0; }
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); ; long n, k; cin >> n >> k; vector<int> ar(n); for (int i = 0; i < n; i++) cin >> ar[i]; queue<int> q; for (int i = 1; i <= n; i++) q.push(i); int i = 0; while (!q.empty() && (k--)) { int range = ar[i] % q.size(); while (range != 0) q.push(q.front()), q.pop(), range--; printf("%d ", q.front()); q.pop(); i++; } return 0; }
5
#include <bits/stdc++.h> using std::min; int n, m, a[17][10005], dis[20][20], dp[16][1 << 16][16], ans; int c(int x, int y) { int t = 0x3f3f3f3f3f; for (int i = 1; i < m; i++) t = std::min(t, abs(a[x][i] - a[y][i + 1])); return t; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]); memset(dis, 0x3f, sizeof(dis)); for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) for (int k = 1; k <= m; k++) dis[i][j] = dis[j][i] = std::min(dis[i][j], abs(a[i][k] - a[j][k])); for (int i = 0; i < n; i++) dp[i][1 << i][i] = 0x3f3f3f3f3f; for (int i = 1; i < 1 << n; i++) for (int j = 0; j < n; j++) if (i & (1 << j)) { for (int k = 0; k < n; k++) if (i & (1 << k)) { for (int h = 0; h < n; h++) if ((i & (1 << h)) == 0) { dp[j][i | (1 << h)][h] = std::max(dp[j][i | (1 << h)][h], min(dp[j][i][k], dis[k][h])); } } } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { ans = std::max(ans, min(dp[i][(1 << n) - 1][j], c(j, i))); } printf("%d\n", ans); }
12
#include <bits/stdc++.h> using namespace std; void solve() { long long n, _x, _y; cin >> n; pair<pair<long long, long long>, pair<long long, long long> > pts[n]; for (long long i = 0; i < 2 * n; i++) { cin >> _x >> _y; if (i & 1) pts[i / 2].second = {_x, _y}; else pts[i / 2].first = {_x, _y}; } pair<pair<long long, long long>, pair<long long, long long> > a[4] = { {{INT_MAX, INT_MAX}, {INT_MAX, INT_MAX}}, {{INT_MAX, INT_MAX}, {INT_MAX, INT_MAX}}, {{INT_MIN, INT_MIN}, {INT_MIN, INT_MIN}}, {{INT_MIN, INT_MIN}, {INT_MIN, INT_MIN}}}; long long b[4]; for (long long i = 0; i < n; i++) { if (pts[i].first.first > a[2].first.first) { a[2] = pts[i]; b[2] = i; } if (pts[i].second.first < a[0].second.first) { a[0] = pts[i]; b[0] = i; } if (pts[i].first.second > a[3].first.second) { a[3] = pts[i]; b[3] = i; } if (pts[i].second.second < a[1].second.second) { a[1] = pts[i]; b[1] = i; } } for (long long j = 0; j < 4; j++) { pair<pair<long long, long long>, pair<long long, long long> > m = { {INT_MIN, INT_MIN}, {INT_MAX, INT_MAX}}; for (long long i = 0; i < n; i++) { if (b[j] == i) continue; m = {{max(m.first.first, pts[i].first.first), max(m.first.second, pts[i].first.second)}, {min(m.second.first, pts[i].second.first), min(m.second.second, pts[i].second.second)}}; } if (m.first.first <= m.second.first && m.first.second <= m.second.second) { cout << m.first.first << ' ' << m.first.second << endl; break; } } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int n; cin >> n; n = 1 << n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) cout << (__builtin_parity(i & j) ? '*' : '+'); cout << '\n'; } return 0; }
10
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma G++ optimize(2) using namespace std; int main() { long long n, a, b; scanf("%lld", &a), scanf("%lld", &b), n = a + b; long long ans = 0; for (long long l = 1, r; l <= n; l = r + 1) { r = n / (n / l); long long t = n / l, al = (a + t) / (1 + t), ar = a / t, bl = (b + t) / (1 + t), br = b / t; if (al <= ar && bl <= br) ans += max(0ll, min(r, ar + br) - max(l, al + bl) + 1); } printf("%lld\n", ans); return 0; }
19
#include <bits/stdc++.h> #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; template <class T, class S> ostream &operator<<(ostream &o, const pair<T, S> &p) { return o << '(' << p.first << ", " << p.second << ')'; } template <template <class, class...> class T, class... A> typename enable_if<!is_same<T<A...>, string>(), ostream &>::type operator<<( ostream &o, T<A...> V) { o << '['; for (auto a : V) o << a << ", "; return o << ']'; } long long int a[100010], l[100010], r[100010], pos[100010]; bool used[100010]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long int n; cin >> n; long long int k; cin >> k; for (long long int i = 1; i < (n * k + 1); ++i) cin >> a[i]; for (long long int t = 0; t < (n); ++t) { fill_n(pos, n + 1, -1); for (long long int i = 1; i < (n * k + 1); ++i) { if (~pos[a[i]] && !used[a[i]]) { l[a[i]] = pos[a[i]]; r[a[i]] = i; used[a[i]] = true; fill_n(pos, n + 1, -1); } else pos[a[i]] = i; } } for (long long int i = 1; i < (n + 1); ++i) cout << l[i] << ' ' << r[i] << '\n'; }
15
#include <bits/stdc++.h> using namespace std; int P[100005], c1, c2, g1[20], g2[20], pos[400], st1, mn, st2, e, n, i, j, k, from[170][1 << 9][1 << 9], p[100005], a[100005], cnt, u1[20], u2[20], is[100005]; inline int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } void check(int now, int f1, int f2) { if (now > n) { if (f1 == 1 && f2 == 1) { cout << "YES\n"; for (int i = 1; i <= n; ++i) cout << p[i] << " "; exit(0); } return; } p[now] = 1; check(now + 1, gcd(f1, a[now]), f2); p[now] = 2; check(now + 1, f1, gcd(f2, a[now])); } void solve(int x, int y) { int X = x, Y = y; x = a[x]; y = a[y]; memset(P, 0, sizeof(P)); P[X] = 1; P[Y] = 2; c1 = c2 = 0; for (i = 1; i <= 3432; ++i) { if (x % p[i] == 0) { u1[++c1] = p[i]; while (x % p[i] == 0) x /= p[i]; } } if (x > 1) u1[++c1] = x; for (i = 1; i <= 3432; ++i) { if (y % p[i] == 0) { u2[++c2] = p[i]; while (y % p[i] == 0) y /= p[i]; } } if (y > 1) u2[++c2] = y; memset(g1, 0, sizeof(g1)); memset(g2, 0, sizeof(g2)); memset(from, 0, sizeof(from)); from[0][(1 << c1) - 1][(1 << c2) - 1] = 1; for (cnt = 0, i = 1; i <= n; ++i) { if (i == X || i == Y) continue; mn = 666; st1 = st2 = 0; for (j = 1; j <= c1; ++j) { if (a[i] % u1[j] == 0) st1 |= (1 << j - 1); else mn = min(mn, g1[j]); } for (j = 1; j <= c2; ++j) { if (a[i] % u2[j] == 0) st2 |= (1 << j - 1); else mn = min(mn, g2[j]); } if (mn > 8) continue; pos[++cnt] = i; for (j = 1; j <= c1; ++j) if (a[i] % u1[j]) ++g1[j]; for (j = 1; j <= c2; ++j) if (a[i] % u2[j]) ++g2[j]; for (j = 0; j < (1 << c1); ++j) for (k = 0; k < (1 << c2); ++k) { if (from[cnt - 1][j][k]) { from[cnt][j & st1][k] = j << 9 | k; from[cnt][j][k & st2] = j << 9 | k; } } if (from[cnt][0][0]) { st1 = st2 = 0; for (; cnt; --cnt) { e = from[cnt][st1][st2]; if ((e >> 9) != st1) P[pos[cnt]] = 1; else P[pos[cnt]] = 2; st1 = e >> 9; st2 = e & (511); } cout << "YES\n"; for (i = 1; i <= n; ++i) { if (!P[i]) ++P[i]; cout << P[i] << " "; } exit(0); } } } int x, y; int main() { cin >> n; for (i = 2; i < 32000; ++i) { if (!is[i]) p[++cnt] = i; for (j = 1; j <= cnt && (i * p[j]) < 32000; ++j) { is[i * p[j]] = 1; if (i % p[j] == 0) break; } } for (i = 1; i <= n; ++i) cin >> a[i]; if (n <= 23) { check(1, 0, 0); cout << "NO\n"; } else { while (clock() <= 0.45 * CLOCKS_PER_SEC) { x = rand() % n + 1; y = rand() % n + 1; while (y == x) y = rand() % n + 1; solve(x, y); } cout << "NO\n"; } }
21
#include <bits/stdc++.h> using namespace std; string a[5205]; string x[505]; int main() { x['0'] = "0000"; x['1'] = "0001"; x['2'] = "0010"; x['3'] = "0011"; x['4'] = "0100"; x['5'] = "0101"; x['6'] = "0110"; x['7'] = "0111"; x['8'] = "1000"; x['9'] = "1001"; x['A'] = "1010"; x['B'] = "1011"; x['C'] = "1100"; x['D'] = "1101"; x['E'] = "1110"; x['F'] = "1111"; int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { string xx; cin >> xx; a[i] += "0"; for (int j = 0; j < xx.size(); j++) a[i] += x[xx[j]]; } for (int i = 1; i <= n; i++) { if (n % i != 0) continue; int f = 0; for (int j = 1; j <= n; j += n / i) { for (int k = 1; k <= n; k += n / i) { for (int jj = j; jj < j + n / i; jj++) { for (int kk = k; kk < k + n / i; kk++) { if (a[jj][kk] != a[j][k]) { f = 1; break; } } if (f) break; } if (f) break; } if (f) break; } if (!f) { printf("%d", n / i); return 0; } } return 0; }
10
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; const int N = 3000005; char A[N], B[N], C[N], S[N]; int la, lb, lc, ls; struct suffix_array { int sa[N], rk[N], hr[N], hs[N]; int sz, tmp[N], bin[N]; void build(char *S, int ls) { sz = max(255, ls); for (int i = (1); i <= (ls); ++i) rk[i] = S[i]; for (int i = (1); i <= (sz); ++i) bin[i] = 0; for (int i = (1); i <= (ls); ++i) bin[rk[i]]++; for (int i = (1); i <= (sz); ++i) bin[i] += bin[i - 1]; for (int i = (1); i <= (ls); ++i) sa[bin[rk[i]]--] = i; for (int j = 1; j <= ls; j <<= 1) { int tot = 0; for (int i = (ls - j + 1); i <= (ls); ++i) tmp[++tot] = i; for (int i = (1); i <= (ls); ++i) if (sa[i] - j > 0) tmp[++tot] = sa[i] - j; for (int i = (1); i <= (sz); ++i) bin[i] = 0; for (int i = (1); i <= (ls); ++i) bin[rk[i]]++; for (int i = (1); i <= (sz); ++i) bin[i] += bin[i - 1]; for (int i = (ls); i >= (1); --i) sa[bin[rk[tmp[i]]]--] = tmp[i]; tot = 1; tmp[sa[1]] = 1; for (int i = (2); i <= (ls); ++i) tmp[sa[i]] = rk[sa[i]] == rk[sa[i - 1]] && rk[sa[i] + j] == rk[sa[i - 1] + j] ? tot : ++tot; for (int i = (1); i <= (ls); ++i) rk[i] = tmp[i]; if (tot == ls) break; } for (int i = (1); i <= (ls); ++i) { hs[i] = max(0, hs[i - 1] - 1); while (S[i + hs[i]] == S[sa[rk[i] - 1] + hs[i]]) hs[i]++; hr[rk[i]] = hs[i]; } } } SA; struct node { int x, y, z; } s[N]; bool operator<(const node &a, const node &b) { return a.z < b.z; } long long ans[N]; struct bcj { int fa[N], a[N], b[N], c[N], len[N]; void init(int ls) { for (int i = (1); i <= (ls); ++i) { fa[i] = i; len[i] = ls; if (i <= la) a[i]++; if (i >= la + 1 + 1 && i <= la + 1 + lb) b[i]++; if (i >= la + 1 + lb + 1 + 1) c[i]++; } } int get(int x) { if (x == fa[x]) return x; return fa[x] = get(fa[x]); } void merge(int x, int y, int z) { int f1 = get(x); int f2 = get(y); ans[z] = (0ll + ans[z] + (1ll * a[f2] * b[f1] % mod * c[f1] % mod) + (1ll * a[f2] * b[f2] % mod * c[f1] % mod) + (1ll * a[f2] * b[f1] % mod * c[f2] % mod) + (1ll * a[f1] * b[f2] % mod * c[f2] % mod) + (1ll * a[f1] * b[f2] % mod * c[f1] % mod) + (1ll * a[f1] % mod * b[f1] * c[f2] % mod)) % mod; len[f2] = z; a[f2] += a[f1]; b[f2] += b[f1]; c[f2] += c[f1]; fa[f1] = f2; } } DSU; int main() { scanf("%s", A + 1); la = strlen(A + 1); scanf("%s", B + 1); lb = strlen(B + 1); scanf("%s", C + 1); lc = strlen(C + 1); memcpy(S + 1, A + 1, la); S[la + 1] = 'z' + 1; memcpy(S + 1 + la + 1, B + 1, lb); S[la + 1 + lb + 1] = 'z' + 2; memcpy(S + 1 + la + 1 + lb + 1, C + 1, lc); ls = strlen(S + 1); SA.build(S, ls); int x = min(min(la, lb), lc); DSU.init(ls); int tot = 0; for (int i = (2); i <= (ls); ++i) s[++tot].x = SA.sa[i - 1], s[tot].y = SA.sa[i], s[tot].z = SA.hr[i]; sort(s + 1, s + 1 + tot); reverse(s + 1, s + 1 + tot); for (int i = (1); i <= (tot); ++i) DSU.merge(s[i].x, s[i].y, s[i].z); for (int i = (x - 1); i >= (1); --i) ans[i] = (ans[i] + ans[i + 1]) % mod; for (int i = (1); i <= (x); ++i) printf("%d ", ans[i]); }
16
#include <bits/stdc++.h> const int INF = 0x3f3f3f3f; const int maxn = 2e5 + 50; const int Mod = 1e9 + 7; const double PI = acos(-1); using namespace std; char s[maxn]; int n, l; int main() { while (scanf("%d %d", &n, &l) != EOF) { memset(s, 0, sizeof(s)); scanf("%s", s); int pos = 0, t = l; for (; pos < n; pos++) if (s[pos] == '.') break; bool inc = false; for (pos++; pos < n; pos++) { if (s[pos] >= '5') { if (s[pos - 1] == '.') { inc = true; } else { s[pos] = '\0'; while (s[pos - 1] == '9') { s[pos - 1] = '\0'; pos--; } if (s[pos - 1] == '.') { inc = true; } else s[pos - 1]++; } t--; break; } } if (!inc) { pos--; while (t > 0) { if (s[pos] != '.' && s[pos] >= '5') { s[pos] = '\0'; pos--; if (s[pos] == '.') { inc = true; break; } s[pos]++; t--; } else { if (s[pos] == '.') inc = true; break; } } } string res = s; if (inc) { for (pos = 0; pos < n; pos++) if (s[pos] == '.') break; s[pos] = '\0'; res = s; for (pos--; pos >= 0; pos--) { if (res[pos] == '9') { res[pos] = '0'; if (pos == 0) { res = "1" + res; break; } } else { res[pos]++; break; } } } printf("%s\n", res.c_str()); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, h, m, k; cin >> n >> h >> m >> k; vector<int> hi(n); vector<int> mi(n); vector<pair<int, int> > trains; for (int i = 0; i < n; i++) { cin >> hi[i] >> mi[i]; if (mi[i] >= m / 2) { mi[i] -= m / 2; } trains.push_back({mi[i], i + 1}); trains.push_back({mi[i] - m / 2, i + 1}); } sort(trains.begin(), trains.end()); int cancel = n; int cancelat = 0; int time = 0; for (int i = 0; i < 2 * n; i++) { int t = trains[i].first + k; if (t < 0 && trains[i + 1].first >= 0) { t = 0; } if (0 <= t && t < m / 2 && (i == 2 * n - 1 || trains[i + 1].first > trains[i].first)) { pair<int, int> pr = {t, 0}; auto it = lower_bound(trains.begin(), trains.end(), pr); int x = it - trains.begin(); int currcanc = x - i - 1; if (currcanc < cancel) { cancel = currcanc; cancelat = i; time = t; } } } cout << cancel << " " << time << "\n"; for (int i = cancelat + 1; i <= cancelat + cancel; i++) { cout << trains[i].second << " "; } return 0; }
15
#include <bits/stdc++.h> using namespace std; const long long mxN = 1e6; char line[mxN]; int main() { ios_base::sync_with_stdio(false); cin.getline(line, 1e6); vector<string> w; vector<char> s; for (long long i = 0; i < 1e5 + 500; i++) { if (line[i] == '\0') { string t = ""; for (auto it : s) { t += it; } w.push_back(t); break; } if (line[i] == ' ') { string t = ""; for (auto it : s) { t += it; } w.push_back(t); s.erase(s.begin(), s.end()); continue; } s.push_back(line[i]); } long long cnt = w.size(); if (cnt == 1) { long long n = w[0].size(); string str1 = ".", str2 = ".", str3 = ".", str4 = "."; if (n >= 4) str1 = w[0].substr(n - 4, 4); if (n >= 5) str2 = w[0].substr(n - 5, 5); if (n >= 3) str3 = w[0].substr(n - 3, 3); if (n >= 6) str4 = w[0].substr(n - 6, 6); if (str1 == "etra" || str1 == "lios" || str2 == "liala" || str3 == "etr" || str4 == "inites" || str4 == "initis") cout << "YES"; else cout << "NO"; return 0; } bool masculine = false, feminine = false, ok = true; long long startadj = -1, endadj = -1, startnoun = -1, endnoun = -1, startverb = 1e6, endverb = 1e6; for (long long i = 0; i < cnt; i++) { long long n = w[i].length(); if (n < 3) { ok = false; break; } string str1 = ".", str2 = ".", str3 = ".", str4 = "."; bool done = false; if (n >= 4) str1 = w[i].substr(n - 4, 4); if (n >= 5) str2 = w[i].substr(n - 5, 5); if (n >= 3) str3 = w[i].substr(n - 3, 3); if (n >= 6) str4 = w[i].substr(n - 6, 6); if (str1 != "." && str1 == "lios") { if (startadj == -1) { startadj = i; } endadj = i; masculine = true; done = true; } if (str1 != "." && str1 == "etra") { if (startnoun == -1) { startnoun = i; } endnoun = i; feminine = true; done = true; } if (str2 != "." && str2 == "liala") { if (startadj == -1) { startadj = i; } endadj = i; feminine = true; done = true; } if (str4 != "." && str4 == "inites") { if (startverb == 1e6) { startverb = i; } endverb = i; done = true; feminine = true; } if (str3 != "." && str3 == "etr") { if (startnoun == -1) { startnoun = i; } endnoun = i; done = true; masculine = true; } if (str4 != "." && str4 == "initis") { if (startverb == 1e6) { startverb = i; } endverb = i; done = true; masculine = true; } if (!done) { ok = false; break; } } if (!ok) { cout << "NO"; } else { if ((masculine == true && feminine == true) || (masculine == false && feminine == false)) cout << "NO"; else { if (startnoun == endnoun && startnoun != -1 && startnoun > endadj && endnoun < startverb) { cout << "YES"; } else { cout << "NO"; } } } return 0; }
8
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; using pii = pair<int, int>; using pll = pair<ll, ll>; using ull = unsigned long long; using ld = long double; template <typename T> void _(const char* s, T h) { cerr << s << " = " << h << "\n"; } template <typename T, typename... Ts> void _(const char* s, T h, Ts... t) { int b = 0; while (((b += *s == '(') -= *s == ')') != 0 || *s != ',') cerr << *s++; cerr << " = " << h << ","; _(s + 1, t...); } struct init { init() { cin.tie(0); iostream::sync_with_stdio(0); cout << fixed << setprecision(10); cerr << fixed << setprecision(5); } ~init() {} } init; template <typename T, typename U> void upx(T& x, U y) { if (x < y) x = y; } template <typename T, typename U> void upn(T& x, U y) { if (x > y) x = y; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll D = 4, dx[] = {+1, 0, -1, 0}, dy[] = {0, +1, 0, -1}; ll ask(ll x, ll y) { cout << "? " << x << ' ' << y << endl; ll r; cin >> r; assert(r != -1); return r; } int32_t main() { const ll m = 1e9; ll a = ask(1, 1); ll b = ask(m, 1); ll sum = m + 1 - b + a; if (sum % 2 == 0) { ll xm = sum / 2; ll yl = ask(xm, 1) + 1; ll xl = a + 2 - yl; ll xr = xm + (xm - xl); ll yr = m - ask(xm, m); cout << "! " << xl << ' ' << yl << ' ' << xr << ' ' << yr << endl; } else { ll xm = sum / 2; ll yl = ask(xm, 1) + 1; ll xl = a + 2 - yl; ll xr = xm + (xm - xl) + 1; ll yr = m - ask(xm, m); cout << "! " << xl << ' ' << yl << ' ' << xr << ' ' << yr << endl; } return 0; }
13
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; map<int, int> mp; vector<int> x(n), l(m), r(m); for (int i = 0; i < n; i++) { cin >> x[i]; mp[x[i]] = i; } for (int i = 0; i < m; i++) cin >> l[i] >> r[i]; vector<int> c(n); int i = 0; map<int, int>::iterator it; for (it = mp.begin(); it != mp.end(); it++) { c[it->second] = (i % 2); i++; } cout << endl; for (int i = 0; i < n; i++) cout << (i ? " " : "") << c[i]; cout << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; using ll = long long int; ll n, k, A, B, x, tot = 0; int main() { cin >> n >> k >> A >> B; if (k == 1) { cout << (n - 1) * A << "\n"; return 0; } while (n != 1) { if (n % k == 0) { if ((n - (n / k)) * A <= B) { tot += (n - (n / k)) * A; n = n / k; } else { n = n / k; tot += B; } } else { tot += (n % k) * A; n = n - n % k; if (n <= 0) { tot -= abs(1 - n) * A; n = 1; } } } cout << tot << "\n"; }
6
#include <bits/stdc++.h> using namespace std; long long int ar[40]; int main() { long long int n, l; cin >> n >> l; long long int arr[40]; cin >> arr[0]; for (int i = 1; i < n; i++) { cin >> arr[i]; arr[i] = min(2 * arr[i - 1], arr[i]); } for (int i = n; i < 40; i++) arr[i] = 2 * arr[i - 1]; long long int a = l, k = 0; while (a > 0) { if (a % 2 == 1) ar[k] = 1; a = a / 2; k++; } long long int ans = 0; for (int i = 0; i < 40; i++) if (ar[i] == 1) ans += arr[i]; a = l; bool b = 0; long long int ch = ans; for (int i = 0; i < n; i++) { if (b) ans = min(ans, ch + arr[i]); if (ar[i] == 1) { b = 1; ch -= arr[i]; } } cout << ans; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; int n, a[maxn]; int main() { scanf("%d", &n); int cur = 0; a[0] = 1; for (int i = 1; i < n; ++i) { a[++cur] = 1; while (cur != 0 && a[cur] == a[cur - 1]) { a[cur - 1]++; --cur; } } for (int i = 0; i <= cur; ++i) { if (i) printf(" "); printf("%d", a[i]); } }
0
#include <bits/stdc++.h> using namespace std; bool prime(int n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } int main() { int a, b; cin >> a >> b; int pr = a + 1; while (!prime(pr)) { pr = pr + 1; } if (pr == b) { cout << "YES\n"; } else { cout << "NO\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k; cin >> n >> m; vector<vector<int>> g(n + 1), r(n + 1); vector<int> dist(n + 1, -1); queue<int> q; for (int i = 0; i < m; ++i) { int x1, x2; cin >> x1 >> x2; g[x1].push_back(x2); r[x2].push_back(x1); } cin >> k; vector<int> a(k); for (int i = 0; i < k; ++i) cin >> a[i]; q.push(a.back()); dist[a.back()] = 0; while (!q.empty()) { int v = q.front(); q.pop(); for (auto to : r[v]) { if (dist[to] == -1) { dist[to] = dist[v] + 1; q.push(to); } } } int ans1 = 0, ans2 = 0; for (int i = 0; i < k - 1; ++i) ans1 += (dist[a[i]] != dist[a[i + 1]] + 1); for (int i = 0; i < k - 1; ++i) for (auto v : g[a[i]]) if (v != a[i + 1] && dist[v] == dist[a[i]] - 1) { ++ans2; break; } cout << ans1 << ' ' << ans2; return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; int p = 0, e1 = -1; for (int i = 0; i < t.length(); i++) { if (t[i] == s[p]) p++; if (p == s.length()) { e1 = i; break; } } if (p != s.length()) { cout << 0 << endl; return 0; } p = s.length() - 1; for (int i = t.length() - 1; i >= 0; i--) { if (t[i] == s[p]) p--; if (p == -1) { cerr << e1 << " " << i << endl; if (i <= e1) cout << 0 << endl; else cout << i - e1 << endl; return 0; } } return 0; }
6
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const int maxn = 3e5 + 10, maxm = 11; int casn, n, m; long long k; struct node { int a, b, flag; }; set<unsigned long long> ck; unordered_map<unsigned long long, int> cnt; unordered_map<unsigned long long, node> dp; unsigned long long pw[40]; node dfs(unsigned long long now, int sum, int x, int len, string s = "") { if (dp.count(now)) return dp[now]; node mx = {0, 0, 0}; for (int i = 1; i <= 26; ++i) { unsigned long long to = now * 29ull + i; if (ck.count(to)) { node tmp = dfs(to, sum + i, max(i, x), len + 1, s + char('a' + i - 1)); swap(tmp.a, tmp.b); tmp.flag ^= 1; tmp.a += (sum + i) * max(i, x) + cnt[to]; if (mx.flag < tmp.flag) mx = tmp; else if (tmp.flag == mx.flag && mx.a < tmp.a) mx = tmp; else if (tmp.flag == mx.flag && tmp.a == mx.a && mx.b > tmp.b) mx = tmp; } to = i * pw[len] + now; if (ck.count(to)) { node tmp = dfs(to, sum + i, max(i, x), len + 1, char('a' + i - 1) + s); swap(tmp.a, tmp.b); tmp.flag ^= 1; tmp.a += (sum + i) * max(i, x) + cnt[to]; if (mx.flag < tmp.flag) mx = tmp; else if (tmp.flag == mx.flag && mx.a < tmp.a) mx = tmp; else if (tmp.flag == mx.flag && tmp.a == mx.a && mx.b > tmp.b) mx = tmp; } } return dp[now] = mx; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); pw[0] = 1; for (int i = 1; i <= 32; ++i) pw[i] = pw[i - 1] * 29; cin >> n; for (int i = 1; i <= n; ++i) { set<unsigned long long> vis; string s; cin >> s; int len = s.size(); for (int i = 0; i <= len - 1; ++i) { unsigned long long t = 0; for (int j = i; j <= len - 1; ++j) { t = t * 29 + s[j] - 'a' + 1; vis.insert(t); } } for (auto i : vis) ck.insert(i), ++cnt[i]; } auto ans = dfs(0, 0, 0, 0); if (ans.flag) cout << "First\n"; else cout << "Second\n"; cout << ans.a << ' ' << ans.b << endl; }
13
#include <bits/stdc++.h> using namespace std; int n; const int MAXN = 1e5 + 1; long long sum1toX[MAXN]; long long sortedSum1toX[MAXN]; int v[MAXN]; int m, l, r, type; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> v[i]; } sum1toX[0] = 0; for (int i = 1; i <= n; i++) { sum1toX[i] = sum1toX[i - 1] + v[i - 1]; } sort(v, v + n); sortedSum1toX[0] = 0; for (int i = 1; i <= n; i++) { sortedSum1toX[i] = sortedSum1toX[i - 1] + v[i - 1]; } cin >> m; for (int i = 0; i < m; i++) { cin >> type >> l >> r; if (type == 1) { cout << (long long)sum1toX[n] - (sum1toX[l - 1] + sum1toX[n] - sum1toX[r]) << '\n'; } else { cout << (long long)sortedSum1toX[n] - (sortedSum1toX[l - 1] + sortedSum1toX[n] - sortedSum1toX[r]) << '\n'; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; int long long pre[100005], suff[100005], a[100005]; struct point { int long long l, r; }; int main() { ios::sync_with_stdio(0), cin.tie(0); int long long n, flag = 0; cin >> n; map<int long long, struct point> m; for (int long long i = 0; i < n; i++) { cin >> a[i]; if (m.find(a[i]) == m.end()) { struct point p; p.l = i; p.r = i; m.insert(make_pair(a[i], p)); } else { m[a[i]].r = i; } } pre[0] = a[0]; suff[n - 1] = a[n - 1]; for (int long long i = 1; i < n; i++) pre[i] = pre[i - 1] + a[i]; for (int long long i = n - 2; i >= 0; i--) suff[i] = suff[i + 1] + a[i]; for (int long long i = 1; i < n; i++) { if (pre[i - 1] > suff[i]) { int long long d = pre[i - 1] - suff[i]; if (d % 2 == 1) continue; if (m.find(d / 2) != m.end()) { if (m[d / 2].l <= i - 1) { flag = 1; break; } } } else if (pre[i - 1] < suff[i]) { int long long d = -pre[i - 1] + suff[i]; if (d % 2 == 1) continue; if (m.find(d / 2) != m.end()) { if (m[d / 2].r >= i) { flag = 1; break; } } } else { flag = 1; break; } } if (pre[n - 1] % 2 == 0) { if (m.find(pre[n - 1] / 2) != m.end()) flag = 1; } if (flag == 1) cout << "YES" << endl; else cout << "NO" << endl; }
11
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } const double EPS = 1e-9; const double PI = acos(-1.0); int main() { int n; cin >> n; string s; cin >> s; int ans = 0; for (int i = (0); i < (s.size()); ++i) { if (i % n == 0) { if (i >= 3) { char c = s[i - 1]; bool flag = true; for (int j = (0); j < (3); ++j) { if (s[i - j - 1] != c) { flag = false; break; } } if (flag) { ans++; s[i] = c; } } } } cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; struct Point { int a, b, c, d, e; Point() { a = b = c = d = e = 0; } Point(int aa, int bb, int cc, int dd, int ee) { a = aa; b = bb; c = cc; d = dd; e = ee; } }; Point difference(Point s, Point t) { return Point(s.a - t.a, s.b - t.b, s.c - t.c, s.d - t.d, s.e - t.e); } int dot(Point s, Point t) { return (s.a * t.a + s.b * t.b + s.c * t.c + s.d * t.d + s.e * t.e); } Point x[34]; vector<Point> v; vector<int> ans; bool flag; int main() { int N; cin >> N; if (N >= 34) { cout << 0 << endl; return 0; } for (int i = 1; i <= N; i++) { cin >> x[i].a >> x[i].b >> x[i].c >> x[i].d >> x[i].e; } for (int i = 1; i <= N; i++) { v.clear(); flag = true; for (int j = 1; j <= N; j++) { if (j != i) { v.push_back(difference(x[j], x[i])); } } for (int j = 0; j < N - 1; j++) { for (int k = 0; k < N - 1; k++) { if (k != j && dot(v[j], v[k]) > 0) { flag = false; } } } if (flag) { ans.push_back(i); } } cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << endl; } }
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; long long k; cin >> n >> k; long long a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long ans = a[n / 2], med = a[n / 2]; long long numcross = 0; bool domore = true; for (int i = n / 2 + 1; i < n; i++) { ++numcross; long long req = numcross * (a[i] - med); if (req <= k) { k -= req; med = a[i]; } else { med += k / numcross; domore = false; break; } } if (domore) med += (k / ((n + 1) / 2)); cout << med << "\n"; }
6
#include <bits/stdc++.h> using namespace std; long long modexpo(long long x, long long p) { long long res = 1; x = x % 1000000007; while (p) { if (p % 2) res = res * x; p >>= 1; x = x * x % 1000000007; res %= 1000000007; } return res; } struct compare { bool operator()(const pair<long long, long long> a, const pair<long long, long long> b) const { return a.first < b.first; } }; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m, x; cin >> n >> m >> x; long long edges = n - 1; edges *= (n - 2); edges /= 2; edges++; if (m > edges || m < n - 1) { cout << "-1" << "\n"; exit(0); } set<long long> st; for (long long i = 1; i <= n; i++) { st.insert(i); } st.erase(x); long long nxt = *st.begin(); st.erase(st.begin()); vector<long long> v; for (long long i : st) { v.push_back(i); } vector<pair<long long, long long>> ans; long long rest = n - 2, l = 0; while (rest--) { ans.push_back({x, v[l]}); l++; } ans.push_back({x, nxt}); m -= n - 1; for (long long i = 0; i < v.size() && m > 0; i++) { for (long long j = i + 1; j < v.size() && m > 0; j++) { ans.push_back({v[i], v[j]}); m--; } } for (auto x : ans) { cout << x.first << " " << x.second << "\n"; } }
9
#include <bits/stdc++.h> using namespace std; template <class T> inline T min(T &a, T &b) { return a < b ? a : b; } template <class T> inline T max(T &a, T &b) { return a > b ? a : b; } template <class T> void read(T &x) { char ch; while ((ch = getchar()) && !isdigit(ch)) ; x = ch - '0'; while ((ch = getchar()) && isdigit(ch)) x = x * 10 + ch - '0'; } struct point { int x, y; point() {} point(int _x, int _y) : x(_x), y(_y) {} }; long long Pow(long long a, long long b, long long mod) { long long res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } const int N = 200; int n, m, K, dis[N][N], ti, ok[N][N], dp[N][N], vis[N], can[N], Vis[N][N], G[N][N]; vector<int> E[N], F[N], full[N], Road[N], Up[N]; int S, T; void bfs(int st) { queue<int> Q; for (int i = 1; i <= n; i++) dis[ti][i] = ((~0U >> 1) - 3); dis[ti][st] = 0; Q.push(st); while (Q.size()) { int x = Q.front(); Q.pop(); for (int i = 0; i < E[x].size(); i++) if (dis[ti][E[x][i]] > dis[ti][x] + 1) { dis[ti][E[x][i]] = dis[ti][x] + 1; Q.push(E[x][i]); } } } void dfs(int x) { Vis[ti][x] = 1; if (vis[x] != ti) { full[ti].push_back(x); Road[dis[ti][x]].push_back(x); } vis[x] = ti; for (int i = 0; i < F[x].size(); i++) if (dis[ti][F[x][i]] == dis[ti][x] - 1) dfs(F[x][i]); } int dfs(int x, int y) { int ans = 0; for (int i = 1; i <= n; i++) if (Vis[y][i] && G[x][i] && dis[y][i] == dis[y][x] + 1) ans = max(ans, dfs(i, y)); if (ans == 0) ans = ((~0U >> 1) - 3); ans = min(ans, dp[x][y]); ans = min(ans, can[x] + 1); return ans; } int main() { scanf("%d%d%d%d", &n, &m, &S, &T); int x, y; if (S == T) { puts("0"); return 0; } for (int i = 1; i <= m; i++) scanf("%d%d", &x, &y), E[x].push_back(y), F[y].push_back(x), G[x][y] = 1; scanf("%d", &K); for (int i = 1; i <= K; i++) { scanf("%d%d", &x, &y); ti = i; for (int j = 0; j <= n; j++) Road[j].clear(); bfs(x); dfs(y); for (int j = 0; j <= n; j++) if (Road[j].size() == 1) Up[Road[j][0]].push_back(i), ok[Road[j][0]][i] = 1; } for (int i = 1; i <= n; i++) can[i] = ((~0U >> 1) - 3); for (int i = 1; i <= n; i++) for (int j = 0; j <= K; j++) dp[i][j] = ((~0U >> 1) - 3); for (int i = 0; i < Up[T].size(); i++) dp[T][Up[T][i]] = 1, can[T] = 1; while (1) { int F = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= K; j++) if (ok[i][j] == 1) { int flag = 1, Max = 0, size = 0; Max = dfs(i, j); if (dp[i][j] > Max) { dp[i][j] = Max, can[i] = min(can[i], dp[i][j]), F = 1; } } if (!F) break; } int ans = ((~0U >> 1) - 3); for (int i = 1; i <= K; i++) ans = min(ans, dp[S][i]); if (ans == ((~0U >> 1) - 3)) puts("-1"); else printf("%d\n", ans); }
18
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t)) aux1 += m * x; long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; aux2 *= cost; ans += min(aux1, aux2); } cout << ans << endl; return 0; }
11
#include <bits/stdc++.h> using namespace std; const int MOD = int(1e9) + 7; int pom[2][2] = {{0, 1}, {1, 1}}; long long tmp; void mulMat(int *A, int *B, int n) { int res[2][2] = {{0}}; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) { tmp = res[i][j]; tmp += (*(A + i * n + k)) * 1LL * (*(B + k * n + j)); if (tmp >= MOD) res[i][j] = tmp % MOD; else res[i][j] = tmp; } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) *(A + i * n + j) = res[i][j]; } void fastExp(int pot, int *T) { while (pot) { if (pot & 1) { mulMat(T, pom[0], 2); } mulMat(pom[0], pom[0], 2); pot >>= 1; } pom[0][0] = 0; pom[0][1] = 1; pom[1][0] = 1; pom[1][1] = 1; } struct Node { int mat[2][2]; Node() { mat[0][0] = 1; mat[0][1] = 0; mat[1][0] = 0; mat[1][1] = 1; } Node(int pot) { mat[0][0] = 1; mat[0][1] = 0; mat[1][0] = 0; mat[1][1] = 1; fastExp(pot, mat[0]); } }; void zbrojiMat(int *resMat, int *op1, int *op2) { for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) { tmp = (*(op1 + i * 2 + j) + *(op2 + i * 2 + j)); if (tmp >= MOD) *(resMat + i * 2 + j) = tmp - MOD; else *(resMat + i * 2 + j) = tmp; } } int n, m; struct Node zeroe; struct segTree { struct Node arr[1 << 18]; struct Node lazy[1 << 18]; int offset; struct Node neutralElem; void normalInit(int n) { for (offset = 1; offset < n; offset *= 2) ; } struct Node mergeValues(struct Node l, struct Node r) { struct Node n; zbrojiMat(n.mat[0], l.mat[0], r.mat[0]); return n; } segTree(int n) { normalInit(n); } void fillTree(int *data) { for (int i = offset; i < offset + n; i++) arr[i] = Node(*(data + i - offset)); for (int i = offset - 1; i >= 1; i--) arr[i] = mergeValues(arr[i * 2], arr[i * 2 + 1]); } void updateRange(int lo, int hi, int *x) { update(1, 0, offset, lo, hi + 1, x); } struct Node query(int lo, int hi) { return q(1, 0, offset, lo, hi + 1); } private: void prop(int node) { mulMat(arr[node * 2].mat[0], lazy[node].mat[0], 2); mulMat(arr[node * 2 + 1].mat[0], lazy[node].mat[0], 2); mulMat(lazy[node * 2].mat[0], lazy[node].mat[0], 2); mulMat(lazy[node * 2 + 1].mat[0], lazy[node].mat[0], 2); lazy[node] = neutralElem; } void update(int node, int lo, int hi, int qlo, int qhi, int *T) { if (lo >= qhi or hi <= qlo) return; if (lo >= qlo && hi <= qhi) { mulMat(arr[node].mat[0], T, 2); mulMat(lazy[node].mat[0], T, 2); return; } prop(node); int mid = (lo + hi) / 2; if (node < offset) { update(node * 2, lo, mid, qlo, qhi, T); update(node * 2 + 1, mid, hi, qlo, qhi, T); arr[node] = mergeValues(arr[node * 2], arr[node * 2 + 1]); } } struct Node q(int node, int lo, int hi, int qlo, int qhi) { if (lo >= qhi || hi <= qlo) return zeroe; if (lo >= qlo && hi <= qhi) return arr[node]; prop(node); int mid = (lo + hi) / 2; return mergeValues(q(node * 2, lo, mid, qlo, qhi), q(node * 2 + 1, mid, hi, qlo, qhi)); } }; const int MAX = 100005; int data[MAX]; segTree T(1); int main() { int t; t = 1; while (t--) { zeroe.mat[0][0] = 0; zeroe.mat[1][1] = 0; cin >> n >> m; for (int i = 0; i < n; i++) { scanf("%d", &data[i]); data[i]; } T.normalInit(n); T.fillTree(data); for (int i = 0; i < m; i++) { int id; scanf("%d", &id); int l, r; scanf("%d%d", &l, &r); l--; r--; if (id == 1) { int x; scanf("%d", &x); int Ti[2][2] = {{1, 0}, {0, 1}}; fastExp(x, Ti[0]); T.updateRange(l, r, Ti[0]); } else { printf("%d\n", T.query(l, r).mat[0][1]); } } } return 0; }
15
#include <bits/stdc++.h> using namespace std; class Solution { public: long long solve(long long m, long long d, long long w) { auto gcd = [&](long long a, long long b) { while (a != 0) { b %= a; swap(a, b); } return b; }; long long res = 0; auto n = min(m, d); long long g = 0; if (d % w == 0) { g = w; } else { auto gg = gcd(d - 1, w); g = w / gg; } if (g == -1) { res = n * (n - 1); } else { long long p = n / g; res = n * p - p * (p + 1) / 2 * g; } return res; } }; int main(int argc, char** argv) { ios::sync_with_stdio(false); cin.tie(0); Solution sol; int t; cin >> t; while (t-- > 0) { int m, d, w; cin >> m >> d >> w; cout << sol.solve(m, d, w) << '\n'; } return 0; }
14
#include <bits/stdc++.h> using namespace std; const int Maxn = 100005; int sum[Maxn]; int need; string s, res; void tryGet() { if (s.length() % 2) { need = s.length() + 1; return; } for (int i = s.length() - 1; i >= 0; i--) if (s[i] > '7') sum[i] = -1; else if (s[i] == '7') if (sum[i + 1] == -1) sum[i] = -1; else sum[i] = 1 + sum[i + 1]; else if (s[i] > '4') sum[i] = 1; else if (s[i] == '4') if (sum[i + 1] == 0) sum[i] = 0; else sum[i] = 1; else sum[i] = 0; int left4 = s.length() / 2, left7 = s.length() / 2; if (sum[0] == -1 || sum[0] > left7) { need = s.length() + 2; return; } for (int i = 0; i < s.length(); i++) { if (left4 && (s[i] < '4' || s[i] == '4' && sum[i + 1] != -1 && sum[i + 1] <= left7)) { left4--; res += "4"; } else if (left7 && sum[i] != -1 && sum[i] <= left7) { left7--; res += "7"; } else { need = s.length() + 2; res = ""; return; } if (res[i] > s[i]) break; } while (left4--) res += "4"; while (left7--) res += "7"; } int main() { getline(cin, s); tryGet(); if (res == "") { for (int i = 0; i < need / 2; i++) res += "4"; for (int i = 0; i < need / 2; i++) res += "7"; } cout << res << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << "\n"; err(++it, args...); } const long long N = 1e6 + 5, M = 1e9 + 7, inf = 1e18; int t[N]; void up(int p, int v) { if (p <= 0) return; for (; p < N; p += (p & -p)) { t[p] += v; } } int qry(int p) { int res = 0; for (; p > 0; p -= (p & -p)) { res += t[p]; } return res; } int posof(long long prefsum) { int pos = 0; long long sum = 0; for (int i = 21; i >= 0; --i) { if ((pos + (1 << i) <= N) && (sum + t[pos + (1 << i)] < prefsum)) { pos += (1 << i); sum += t[pos]; } } return pos + 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int n, m; cin >> n >> m; vector<int> pos(n + 1), pre(n + 1, -1), ans; vector<pair<int, int> > q[m]; int a[m], id = 0; for (int i = 1; i < n + 1; i++) { up(i, 1); } set<int> s; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; a[i] = x; if (pre[x] != -1 and y > ((int)(s).size())) { return cout << -1, 0; } if (pre[x] == -1 and y <= ((int)(s).size())) { return cout << -1, 0; } if (y > ((int)(s).size())) { pos[x] = posof(y); ; up(1, 1); up(pos[x], -1); } else { int l = pre[x]; q[l + 1].push_back({i, id++}); ans.push_back(y); } pre[x] = i; s.insert(x); } memset(t, 0, sizeof t); vector<int> nxt(n + 1, -1); for (int i = m - 1; i >= 0; i--) { int x = a[i]; if (nxt[x] != -1) { up(nxt[x], -1); } up(i, 1); nxt[x] = i; for (auto p : q[i]) { int r = p.first, id = p.second; if (ans[id] != qry(r)) { return cout << -1, 0; } } } s.clear(); for (int i = 1; i < n + 1; i++) { s.insert(i); } for (int i = 1; i < n + 1; i++) if (pos[i]) { s.erase(pos[i]); } for (int i = 1; i < n + 1; i++) { if (not pos[i]) { pos[i] = *s.begin(); s.erase(s.begin()); } } vector<int> fans(n + 1); for (int i = 1; i < n + 1; i++) { fans[pos[i]] = i; } for (int i = 1; i < n + 1; i++) { cout << fans[i] << " "; } }
14
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long i, j, t; cin >> t; while (t--) { long long n; cin >> n; long long a[n + 1], b[n + 1], d[n + 1]; map<long long, long long> vis; for (i = 1; i <= n; i++) { cin >> a[i]; if (!vis[a[i]]) vis[a[i]] = i; } for (i = 1; i <= n; i++) { cin >> b[i]; d[i] = b[i] - a[i]; } if (a[1] != b[1]) { cout << "NO" << "\n"; continue; } for (i = 2; i <= n; i++) { if (d[i] < 0 && (vis[-1] == 0 || vis[-1] >= i)) break; if (d[i] > 0 && (vis[1] == 0 || vis[1] >= i)) break; } if (i == n + 1) cout << "YES" << "\n"; else cout << "NO" << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d %d", &n, &m); map<int, vector<pair<int, string> > > M; vector<pair<int, string> > aux; for (int i = 1; i <= m; i++) M[i] = aux; string s; int a, b; for (int i = 0; i < n; i++) { cin >> s >> a >> b; M[a].push_back(pair<int, string>(b, s)); } for (int i = 1; i <= m; i++) { vector<pair<int, string> > V = M[i]; sort(V.rbegin(), V.rend()); if (V.size() > 2 && V[1].first == V[2].first) printf("?\n"); else cout << V[0].second << " " << V[1].second << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { int n; string s, ans; cin >> n >> s; for (int i = 0; i < 2 * n - 1; i += 2) ans += s[i]; cout << ans << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int w, h, k, area, guild, result = 0; cin >> w >> h >> k; result = ((w * 2 + h * 2) - 4); if (k > 0) { for (int i = 1; i < k; i++) { result += (((w - 4 * i) * 2 + (h - 4 * i) * 2) - 4); } } cout << result << endl; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 2e3, inf = 1e9; int n, s, m, k, aa[maxn], dp[maxn][maxn], cnt[maxn], sl[maxn], a[maxn]; vector<pair<int, int>> apear[maxn]; void input() { cin >> n >> s >> m >> k; for (int i = 1; i <= n; i++) { cin >> aa[i]; a[i] = aa[i]; } a[n + 1] = aa[n + 1] = inf; sort(a + 1, a + n + 1); for (int i = 1, r; i <= s; i++) { cin >> sl[i] >> r; for (int j = sl[i]; j <= r; j++) apear[j].push_back({sl[i], i}); } for (int i = 1; i <= n; i++) sort(apear[i].begin(), apear[i].end()); } void clear() { for (int i = 0; i <= n; i++) memset(dp[i], 0, sizeof dp[i]); memset(cnt, 0, sizeof cnt); } bool check(int x) { clear(); for (int i = 1; i <= n; i++) { if (aa[i] <= x) for (auto [j, j2] : apear[i]) cnt[j2]++; for (int j = 1; j <= m; j++) dp[i][j] = max( max(dp[i][j - 1], dp[i - 1][j]), (apear[i].size() ? cnt[apear[i][0].second] + dp[sl[apear[i][0].second] - 1][j - 1] : 0)); } return dp[n][m] >= k; } void bs() { int l = 0, r = n + 1; while (l < r) { int mid = (l + r) / 2; check(a[mid]) ? r = mid : l = mid + 1; } cout << (r <= n ? a[r] : -1) << endl; } int main() { input(); bs(); return 0; }
17
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> q(3 * n + 1); for (int i = 0; i < 3 * n; i++) { int a; cin >> a; q[a] = i; } vector<vector<int>> v(n, vector<int>(3)); for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { cin >> v[i][j]; } } int is; cin >> is; int team; int ind; for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { if (v[i][j] == is) { team = i; ind = j; } } } int m = 1e9; for (int i = 0; i < 3; i++) { m = min(m, q[v[team][i]]); } if (q[is] == m) { vector<int> ans; vector<int> dop; int mi = 1e9; for (int i = 0; i < 3; i++) { if (v[team][i] < mi && i != ind) mi = v[team][i]; } ans.push_back(mi); mi = 0; for (int i = 0; i < 3; i++) { if (v[team][i] > mi && i != ind) mi = v[team][i]; } ans.push_back(mi); for (int i = 0; i < team; i++) { for (int j = 0; j < 3; j++) { if (v[i][j] < mi) ans.push_back(v[i][j]); else dop.push_back(v[i][j]); } } sort(ans.begin(), ans.end()); for (int i = team + 1; i < n; i++) { for (int j = 0; j < 3; j++) { dop.push_back(v[i][j]); } } sort(dop.begin(), dop.end()); for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } for (int i = 0; i < dop.size(); i++) { cout << dop[i] << " "; } } else { for (int i = 1; i <= 3 * n; i++) { if (i != is) { cout << i << " "; } } } }
12
#include <bits/stdc++.h> using namespace std; int count_zero(int n) { int result = 0; while (n) { result += n / 5; n = n / 5; } return result; } int solve(int m) { int flag = 0; for (int i = 0; i < 1000000; i++) { if (count_zero(i) == m) { cout << "5" << endl; cout << i << " " << i + 1 << " " << i + 2 << " " << i + 3 << " " << i + 4 << " "; flag = 1; break; } } if (flag == 1) { return 0; } else { return 1; } } int main() { int m; cin >> m; int flag = solve(m); if (flag == 1) { cout << "0"; } }
5
#include <bits/stdc++.h> using namespace std; long long fac(long long n) { long long p = 1; for (long long i = n; i > 0; i--) { p *= i; } return p; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long test_case = 1; while (test_case--) { string s; cin >> s; char minus = '-'; char dot = '.'; long long len = s.length(); long long doti = -1; for (int i = 0; i < len; i++) { if (s[i] == '.') { doti = i; break; } } long long cnt = 0; string ans; if (doti >= 0) { for (int i = doti - 1; i >= 0; i--) { if (s[i] == '-') continue; cnt++; if ((cnt - 1) % 3 == 0 && cnt != 1) { ans = ans + ','; } ans = ans + s[i]; } reverse(ans.begin(), ans.end()); if (len - doti >= 3) { ans = ans + '.' + s[doti + 1] + s[doti + 2]; } else if (len - doti == 2) { long long ok = doti + 1; ans = ans + '.' + s[ok] + '0'; } else if (len - doti == 1) { ans = ans + '.' + '0' + '0'; } } if (doti < 0) { for (int i = len - 1; i >= 0; i--) { if (s[i] == '-') continue; cnt++; if ((cnt - 1) % 3 == 0 && cnt != 1) { ans = ans + ','; } ans = ans + s[i]; } reverse(ans.begin(), ans.end()); ans = ans + '.' + '0' + '0'; } if (s[0] == '-') { cout << "(" << "$" << ans << ")" << "\n"; } if (s[0] != '-') { cout << "$" << ans << "\n"; } } }
4
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") int c[(500000 + 10)], a[(500000 + 10)], b[(500000 + 10)], id[(500000 + 10)]; int ll[(500000 + 10)], rr[(500000 + 10)]; int h[(500000 + 10)]; bool e[(500000 + 10)]; vector<int> vt[(500000 + 10)], Left[(500000 + 10)], Right[(500000 + 10)], fuck[(500000 + 10)]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%d", &h[i]); } for (int i = 0; i < m; ++i) { scanf("%d%d", &a[i], &b[i]); a[i]--; vt[a[i]].push_back(b[i]); } for (int i = 0; i < n; ++i) { sort(vt[i].begin(), vt[i].end()); unique(vt[i].begin(), vt[i].end()); } for (int i = 0; i <= n; ++i) c[i] = 0x3f3f3f3f; int len = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < vt[i].size(); ++j) { int k = lower_bound(c, c + len, vt[i][j]) - c; Left[i].push_back(k + 1); } int k = lower_bound(c, c + len, h[i]) - c; if (k == len) c[len++] = h[i], ll[i] = len; else c[k] = h[i], ll[i] = k; ll[i] = k + 1; } int ans = len; for (int i = 0; i <= n; ++i) c[i] = 0x3f3f3f3f; len = 0; for (int i = n - 1; i >= 0; --i) { for (int j = 0; j < vt[i].size(); ++j) { int k = lower_bound(c, c + len, -vt[i][j]) - c; Right[i].push_back(k + 1); } int k = lower_bound(c, c + len, -h[i]) - c; if (k == len) c[len++] = -h[i], rr[i] = len; else c[k] = -h[i], rr[i] = k; rr[i] = k + 1; } for (int i = 0; i < n; ++i) { if (ll[i] + rr[i] - 1 == ans) fuck[ll[i]].push_back(i); } for (int i = 0; i <= ans; ++i) if (fuck[i].size() == 1) e[fuck[i][0]] = 1; for (int i = 0; i < m; ++i) { int id = lower_bound(vt[a[i]].begin(), vt[a[i]].end(), b[i]) - vt[a[i]].begin(); int res = Left[a[i]][id] + Right[a[i]][id] - 1; if (!e[a[i]]) res = max(res, ans); else res = max(res, ans - 1); printf("%d\n", res); } }
18
#include <bits/stdc++.h> using namespace std; int a[2000200], b[2002000], c[2002000]; int main() { int n, m; scanf("%d%d", &n, &m); memset(b, 0, sizeof(b)); for (int i = 0; i < n; i++) { int x; scanf("%d", &a[i]); b[a[i]]++; } sort(a, a + n); int ans = 0, max0 = a[n - 1], max2 = a[n - 1]; int k = 0; for (int i = 1; i < 1000001; i++) { if (b[i]) c[k++] = i; } for (int i = 0; i < k - 1; i++) { if (c[i + 1] <= c[i] + m) ans += b[c[i]]; } printf("%d\n", n - ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { string s, r, p, s1, s2; int res = 0, i; cin >> s; p = s; reverse(s.begin(), s.end()); r = s; if (p != r) { res = p.length(); cout << res; } else { for (i = 1; i <= p.length(); i++) { p.erase(0, 1); s1 = p; reverse(s1.begin(), s1.end()); s2 = s1; if (p != s2) { res = s2.length(); break; } } cout << res << endl; } }
1
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; int n, m; vector<pair<long long, long long>> segments_on[123][2], decomp_a[123][2]; void dfs_a(int h, long long L, long long R, long long l, long long r, int w) { if (L >= l && R <= r) { decomp_a[h][w].push_back({L, R}); return; } if (l > R || r < L) return; long long mid = (L + R) >> 1; dfs_a(h - 1, L, mid, l, r, w); dfs_a(h - 1, mid + 1, R, l, r, w); } void dfs_b(int h, long long L, long long R, long long l, long long r, int w) { if (l > R || r < L) return; segments_on[h][w].push_back({L, R}); if (L >= l && R <= r) return; long long mid = (L + R) >> 1; dfs_b(h - 1, L, mid, l, r, w); dfs_b(h - 1, mid + 1, R, l, r, w); } long long calc(long long x) { x %= mod; return (x) * (x + 1) >> 1LL % mod; } int main() { cin >> n; for (int i = 1; i <= n; i++) { long long L, R; cin >> L >> R; dfs_a(60, 0, (1LL << 60) - 1, L, R, 0); dfs_b(60, 0, (1LL << 60) - 1, L, R, 1); } cin >> m; for (int i = 1; i <= m; i++) { long long L, R; cin >> L >> R; dfs_a(60, 0, (1LL << 60) - 1, L, R, 1); dfs_b(60, 0, (1LL << 60) - 1, L, R, 0); } vector<pair<long long, long long>> ans; for (int i = 0; i <= 60; i++) { for (int t = 0; t <= 1; t++) { for (auto p : segments_on[i][t]) { for (auto q : decomp_a[i][t]) { long long pref = (p.first ^ q.first) >> i; ans.emplace_back(pref << i, (pref << i) + (1LL << i) - 1); } } } } sort(ans.begin(), ans.end()); long long res = 0; long long last = -1; for (auto p : ans) { p.first = max(p.first, last); if (p.first > p.second) continue; res += calc(p.second) - calc(p.first - 1); res += mod; last = p.second + 1; res %= mod; } cout << res << endl; return 0; }
23
#include <bits/stdc++.h> using namespace std; const int MAXV = 1000006; const int MAXN = 150005; int A[MAXN], par[MAXV], pfact[MAXV], p_comp[MAXV]; vector<int> primes; set<int> ans[MAXV]; int parent(int pos) { if (par[pos] != pos) par[pos] = parent(par[pos]); return par[pos]; } int dsu_merge(int u, int v) { u = parent(u); v = parent(v); if (u > v) swap(u, v); par[v] = u; return u; } vector<int> get_factors(int num) { vector<int> res; int cp = -1; while (num > 1) { if (pfact[num] != cp) res.push_back(pfact[num]); cp = pfact[num]; num /= pfact[num]; } return res; } void precomp() { for (int i = 2; i < MAXV; ++i) { if (pfact[i]) continue; primes.push_back(i); for (int j = i; j < MAXV; j += i) { pfact[j] = i; } } } int main(int argc, char const *argv[]) { int n, q; scanf("%d %d", &n, &q); for (int i = 1; i <= n; ++i) { scanf("%d", &A[i]); par[A[i]] = A[i]; } precomp(); for (int i = 0; i < primes.size(); ++i) { int p = primes[i], cc = 0; for (int j = p; j < MAXV; j += p) { if (!par[j]) continue; int nc = parent(j); if (cc == 0) cc = nc; else cc = dsu_merge(cc, nc); } p_comp[p] = cc; } for (int i = 1; i <= n; ++i) { int ai = A[i]; int nxt = ai + 1; vector<int> factors = get_factors(nxt); for (int j = 0; j < factors.size(); ++j) { int f = factors[j]; if (p_comp[f]) factors[j] = parent(p_comp[f]); } int cc = parent(ai); for (int j = 0; j < factors.size(); ++j) { ans[cc].insert(factors[j]); for (int k = j + 1; k < factors.size(); ++k) { ans[factors[j]].insert(factors[k]); } } } while (q--) { int u, v; scanf("%d %d", &u, &v); u = parent(A[u]); v = parent(A[v]); if (u == v) printf("0\n"); else if ((ans[u].find(v) != ans[u].end()) || (ans[v].find(u) != ans[v].end())) printf("1\n"); else printf("2\n"); } return 0; }
19
#include <bits/stdc++.h> using namespace std; int a[300005], n, k; long long dp[300005], ans = -1e9; vector<int> v[300005]; void dfs(int nod, int p, bool f) { dp[nod] = a[nod]; for (int u : v[nod]) { if (u != p) { dfs(u, nod, f); dp[nod] += max(dp[u], 0LL); } } if (f) ans = max(ans, dp[nod]); else if (dp[nod] == ans) { dp[nod] = 0; k++; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } dfs(1, 0, 1); dfs(1, 0, 0); cout << ans * k << " " << k; return 0; }
16
#include <bits/stdc++.h> using namespace std; const int MAX = 500 + 9; int dp[MAX + 9][MAX + 9]; vector<int> v; bool check(int i, int j) { for (int i1 = 0; i1 < j - i; i1++) { if (v[i + i1] != v[j - i1]) return false; } return true; } int f(int i, int j) { if (i > j) return 0; if (check(i, j) == true) return 1; if (dp[i][j] != -1) return dp[i][j]; int value = 1e9; for (int i1 = i; i1 <= j; i1++) { if (v[i1] == v[i]) { value = min(value, max(1, f(i + 1, i1 - 1)) + f(i1 + 1, j)); } } return dp[i][j] = value; } int main(void) { memset(dp, -1, sizeof(dp)); int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int in; scanf("%d", &in); v.push_back(in); } printf("%d\n", f(0, n - 1)); return 0; }
11
#include <bits/stdc++.h> using namespace std; int n, m, a[66], b[66]; vector<pair<long long, long long> > v; int ans; int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); a[i] *= 2; } for (int i = 1; i <= m; i++) { scanf("%d", &b[i]); b[i] *= 2; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int p = (a[i] + b[j]) / 2; long long mask1 = 0, mask2 = 0; for (int k = 1; k <= n; k++) { for (int l = 1; l <= m; l++) { if (p + (p - a[k]) == b[l]) { mask1 |= 1ll * ((1ll) << (k - 1)); mask2 |= 1ll * ((1ll) << (l - 1)); } } } v.push_back({mask1, mask2}); } } for (int i = 0; i < (int)v.size(); i++) { for (int j = i; j >= 0; j--) { long long m1 = v[i].first | v[j].first; long long m2 = v[i].second | v[j].second; ans = max(ans, __builtin_popcountll(m1) + __builtin_popcountll(m2)); } } printf("%d", ans); }
13
#include <bits/stdc++.h> using namespace std; inline long long Phi(long long X) { register long long Return; Return = X; register long long i; for (i = 2; i * i <= X; i++) { if (X % i == 0) { while (X % i == 0) { X /= i; } Return = Return / i * (i - 1); } } if (X > 1) { Return = Return / X * (X - 1); } return Return; } int main(void) { register long long i; register long long N, K; cin >> N >> K; for (i = 1; i <= K; i += 2) { N = Phi(N); if (N == 1) { break; } } cout << N % 1000000007 << endl; return 0; }
13
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v; int i, x; for (i = 0; i < 4; i++) { cin >> x; v.push_back(x); } sort(v.begin(), v.end()); if (v[3] < v[2] + v[1] || v[2] < v[0] + v[1]) cout << "TRIANGLE"; else if (v[3] == v[2] + v[1] || v[2] == v[0] + v[1]) cout << "SEGMENT"; else cout << "IMPOSSIBLE"; }
1
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; long long n, a[N], c[N], p[N]; inline long long read() { long long ret = 0, f = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 1; c = getchar(); } while (isdigit(c)) { ret = ret * 10 + c - 48; c = getchar(); } if (f) return -ret; return ret; } long long lowbit(long long x) { return x & -x; } void add(long long x, long long y) { while (x <= n) { c[x] += y; x += lowbit(x); } } long long sum(long long x) { long long res = 0; while (x > 0) { res += c[x]; x -= lowbit(x); } return res; } signed main() { n = read(); for (long long i = 1; i <= n; i++) a[i] = read(); for (long long i = 1; i <= n; i++) add(i, i); for (long long i = n; i; i--) { long long l = 1, r = n; while (l < r) { long long mid = (l + r + 1) / 2; if (sum(mid - 1) <= a[i]) l = mid; else r = mid - 1; } p[i] = r; add(r, -r); } for (long long i = 1; i <= n; i++) printf("%d ", p[i]); }
11
#include <bits/stdc++.h> using namespace std; int main() { char str[10]; char str1[10]; scanf("%s%s", str, str1); int lenth = strlen(str); sort(str, str + lenth); int i = 1; while (str[0] == '0' && lenth != 1) { swap(str[0], str[i]); i++; } if (strcmp(str, str1) == 0) printf("OK\n"); else printf("WRONG_ANSWER\n"); return 0; }
3
#include <bits/stdc++.h> using namespace std; const double PI = 3.1415926535897; const long long INF_LL = 1e18 + 7; const double EPS = 1e-5; const long long INF_int = 1e9 + 7; const long long maxn = 2e5 + 10; const long long mod2 = 1e9 + 9; const long long mod3 = 1e9 + 33; long long p; const long long K = 47500; vector<vector<long long>> g; vector<vector<long long>> w; vector<vector<pair<long long, long long>>> d; void enter_d(long long n) { for (long long i = 0; i < n; ++i) { long long k; cin >> k; d[i].resize(k); for (long long j = 0; j < k; ++j) cin >> d[i][j].first; if (k != 0) d[i][k - 1].second = 1; for (long long j = k - 2; j >= 0; --j) { if (d[i][j + 1].first == d[i][j].first) { d[i][j].second = d[i][j + 1].second; continue; } if (d[i][j + 1].first - 1 == d[i][j].first) d[i][j].second = d[i][j + 1].second + 1; else d[i][j].second = 1; } } return; } void solve() { long long n, m; cin >> n >> m; g.assign(n, vector<long long>()); w.assign(n, vector<long long>()); d.assign(n, vector<pair<long long, long long>>()); for (long long i = 0; i < m; ++i) { long long v, u, c; cin >> v >> u >> c; v--, u--; g[v].push_back(u), w[v].push_back(c); g[u].push_back(v), w[u].push_back(c); } enter_d(n); set<pair<long long, long long>> s; vector<long long> len(n, INF_LL); s.insert({0, 0}); len[0] = 0; while (!s.empty()) { long long v; tie(ignore, v) = *s.begin(); s.erase(s.begin()); for (long long i = 0; i < (g[v]).size(); ++i) { long long to = g[v][i]; long long temp = len[v] + w[v][i]; long long l = -1, r = (d[v]).size(); while (l + 1 < r) { long long m = (l + r) / 2; if (d[v][m].first > len[v]) r = m; else l = m; } if (!(l == -1 || d[v][l].first != len[v])) temp += d[v][l].second; if (len[to] > temp) { if (len[to] != INF_LL) s.erase(s.find({len[to], to})); len[to] = temp; s.insert({len[to], to}); } } } if (len[n - 1] == INF_LL) len[n - 1] = -1; cout << len[n - 1]; } int32_t main() { cout << fixed << setprecision(15); ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); long long t = 1; while (t--) { solve(); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int k1, k2, k3; void Enter() { cin >> k1 >> k2 >> k3; if (k1 > k2) swap(k1, k2); if (k1 > k3) swap(k1, k3); if (k2 > k3) swap(k2, k3); } void Solve() { if ((k1 == 1) || (k1 == 2 && k2 == 2) || (k1 == 2 && k2 == 4 && k3 == 4) || (k1 == 3 && k2 == 3 && k3 == 3)) { cout << "YES"; return; } else cout << "NO"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); Enter(); Solve(); }
6
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e6, mod = 1e9 + 7; long long n, m, a, b, jc[2222222], injc[2222222], cur, ans; long long binpow(long long a, long long t) { if (t < 0) return binpow(a, (-t) * (mod - 2)); long long res = 1, p = a; for (long long i = t; i; i >>= 1) { if (i & 1) res = res * p % mod; p = p * p % mod; } return res; } void Init() { jc[0] = 1; for (long long i = 1; i <= maxn; i++) { jc[i] = jc[i - 1] * i % mod; } injc[maxn] = binpow(jc[maxn], mod - 2); for (long long i = maxn - 1; i >= 0; i--) injc[i] = injc[i + 1] * (i + 1) % mod; } long long C(long long n, long long k) { if (n < k) return 0; return (jc[n] * injc[k] % mod) * injc[n - k] % mod; } int main() { cin >> n >> m >> a >> b; Init(); for (long long i = 1; i < n && i <= m; i++) { cur = C(m - 1, i - 1) * binpow(m, n - i - 1) % mod; cur = cur * binpow(n, n - i - 2) % mod; cur = cur * (i + 1) % mod; cur = cur * C(n - 2, i - 1) % mod * jc[i - 1] % mod; ans = (ans + cur) % mod; } cout << ans << endl; return 0; }
16
#include <bits/stdc++.h> using namespace std; int n, m, pa[1005]; int getpa(int x) { if (pa[x] == x) return x; return pa[x] = getpa(pa[x]); } int main() { scanf("%d%d", &n, &m); int ans = 0, a, b, p1, p2; if (m != n - 1) ans = 1; for (int i = 1; i <= n; ++i) pa[i] = i; for (int i = 1; i <= m; ++i) { scanf("%d%d", &a, &b); if (ans) continue; p1 = getpa(a); p2 = getpa(b); if (p1 == p2) ans = 1; else pa[p1] = p2; } if (ans) puts("no"); else puts("yes"); return 0; }
5
#include <bits/stdc++.h> using namespace std; inline int in() { int res = 0; char c; int f = 1; while ((c = getchar()) < '0' || c > '9') if (c == '-') f = -1; while (c >= '0' && c <= '9') res = res * 10 + c - '0', c = getchar(); return res * f; } const int N = 100010; pair<int, int> a[N]; long long mn[N], mx[N]; long long cnt[N]; inline double base(int i) { return (1.0 * (a[i - 1].second - a[i - 1].first + 1) * (a[i].second - a[i].first + 1)); } inline int len(int i) { return a[i].second - a[i].first + 1; } int main() { int n = in(), p = in(); for (int i = 0; i < n; i++) { a[i].first = in(); a[i].second = in(); } a[n] = a[0]; n++; double total = 0.0; for (int i = 0; i < n; i++) { cnt[i] = a[i].second / p - (a[i].first - 1) / p; if (i) { total += 1.0 - 1. * (len(i) - cnt[i]) * (len(i - 1) - cnt[i - 1]) / len(i) / len(i - 1); } } printf("%.10lf\n", total * 2000); return 0; }
9
#include <bits/stdc++.h> struct eea_result { int g, x, y; }; eea_result eea(int a, int b) { int rx = a, ry = b; int sx = 1, sy = 0; int tx = 0, ty = 1; while (ry != 0) { int t; int q = rx / ry; t = rx - q * ry; rx = ry; ry = t; t = sx - q * sy; sx = sy; sy = t; t = tx - q * ty; tx = ty; ty = t; } eea_result ans = {rx, sx, tx}; return ans; } int main(void) { int m; int h1, a1; int x1, y1; int h2, a2; int x2, y2; scanf("%d %d %d %d %d %d %d %d %d", &m, &h1, &a1, &x1, &y1, &h2, &a2, &x2, &y2); int t1[1000000]; int t2[1000000]; for (int i = m; i--;) { t1[i] = t2[i] = -1; } int c; for (c = 0;; c++) { if (t1[h1] != -1) { break; } t1[h1] = c; h1 = (int)(((long long)x1 * h1 + y1) % m); } fputc('\n', stderr); int c1 = c; for (c = 0;; c++) { if (t2[h2] != -1) { break; } t2[h2] = c; h2 = (int)(((long long)x2 * h2 + y2) % m); } fputc('\n', stderr); int c2 = c; if (t1[h1] > t1[a1] && t2[h2] > t2[a2]) { printf("%d\n", (t1[a1] == t2[a2]) ? t1[a1] : -1); } else if (t1[h1] > t1[a1]) { if (t1[a1] == -1) { puts("-1"); } else { int a = c2 - t2[h2]; int b = t2[a2]; int c = t1[a1]; printf("%d\n", ((c - b) % a == 0 && c >= b) ? c : -1); } } else if (t2[h2] > t2[a2]) { if (t2[h2] == -1) { puts("-1"); } else { int a = c1 - t1[h1]; int b = t1[a1]; int c = t2[a2]; printf("%d\n", ((c - b) % a == 0 && c >= b) ? c : -1); } } else { int a = c1 - t1[h1]; int b = t1[a1]; int c = c2 - t2[h2]; int d = t2[a2]; auto e = eea(a, c); long long g = e.g; long long x = e.x; long long y = e.y; if ((d - b) % g != 0) { puts("-1"); } else { x *= (d - b) / g; y *= (d - b) / g; y *= -1; long long j; if (x < 0 || y < 0) { j = std::max((-x + c / g - 1) / (c / g), (-y + a / g - 1) / (a / g)); } else { j = -1 * std::min(x / (c / g), y / (a / g)); } x += j * (c / g); y += j * (a / g); printf("%lld\n", a * x + b); } } return 0; }
14
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 9; const int maxm = 3e3 + 9; const long long mod = 998244353; int add[maxn]; long long w[maxn]; long long dp[maxm][maxm]; long long Pow(long long a, long long b) { long long res = 1; a %= mod; while (b) { if (b & 1) res = res * a % mod; b >>= 1; a = a * a % mod; } return res; } inline long long inv(long long a) { return Pow(a, mod - 2); } long long INV[2 * maxm]; int main() { int n, m; scanf("%d%d", &n, &m); long long s0 = 0, s1 = 0, inv0, inv1; for (int i = 1; i <= n; i++) scanf("%d", add + i); for (int i = 1; i <= n; i++) { scanf("%lld", w + i); if (add[i]) s1 += w[i]; else s0 += w[i]; } inv0 = inv(s0); inv1 = inv(s1); for (int i = -m - 1; i <= m + 1; i++) { if (s0 + s1 + i <= 0) continue; INV[i + maxm] = inv(s0 + s1 + i); } dp[0][0] = 1; for (int i = 1; i <= m; i++) { for (int j = 0; j <= i; j++) { dp[j][i - j] = (j == 0) ? 0 : (dp[j - 1][i - j] * (s1 + j - 1) % mod * INV[j - 1 - (i - j) + maxm] % mod); dp[j][i - j] += (i - j == 0) ? 0 : (dp[j][i - j - 1] * (s0 - (i - j - 1)) % mod * INV[j - (i - j - 1) + maxm] % mod); dp[j][i - j] %= mod; } } long long ans[2] = {0, 0}; for (int i = 0; i <= m; i++) { ans[1] += dp[i][m - i] * (s1 + i) % mod; ans[0] += dp[i][m - i] * (s0 - m + i) % mod; } ans[0] %= mod, ans[1] %= mod; for (int i = 1; i <= n; i++) { if (add[i]) { printf("%d\n", w[i] * ans[1] % mod * inv1 % mod); } else { printf("%d\n", w[i] * ans[0] % mod * inv0 % mod); } } }
15
//#pragma GCC optimize("inline,Ofast,no-stack-protector,unroll-loops,fast-math,O3") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,avx,abm") #include <bits/stdc++.h> #define ll long long #define int ll typedef double ld; #define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const long long mod = 1000000007; const int N = 30000; int n, w; int a[101010]; int h[101010]; void solve() { cin >> n >> w; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int j = 1; j <= n; j++)h[j] = w; sort(a + 1, a + 1 + n, greater<int>()); int mx = 0; for (int i = 1; i <= n; i++) { int l = 0, r = n; while (l + 1 < r) { int mid = (l + r) / 2; if (h[mid] >= a[i])r = mid; else l = mid; } h[r] -= a[i]; mx = max(mx, r); } cout << mx << '\n'; } int32_t main() { fast int t; cin >> t; while (t--) { solve(); } } /** */
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } int minn, maxx, count = 0; if (n > 2) { minn = min(a[0], a[1]); maxx = max(a[0], a[1]); for (int i = 2; i < n; ++i) { if (a[i] > maxx) { maxx = a[i]; count++; } else if (a[i] < minn) { minn = a[i]; count++; } } if (a[1] != a[0]) { count++; } cout << count; } else { if (n == 2) { if (a[0] != a[1]) cout << "1"; else cout << "0"; } else { cout << "0"; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; struct S { int x, y; } a[3]; bool cmp(S a, S b) { if (a.x == b.x) return a.y < b.y; return a.x < b.x; } set<pair<int, int> > s; int main() { int i; for (i = 0; i < 3; i++) cin >> a[i].x >> a[i].y; sort(a, a + 3, cmp); if (a[0].x == a[1].x) { for (i = a[0].y; i <= a[1].y; i++) s.insert(make_pair(a[0].x, i)); if (a[2].y <= a[1].y && a[2].y >= a[0].y) for (i = a[0].x; i <= a[2].x; i++) s.insert(make_pair(i, a[2].y)); else { for (i = a[1].x; i <= a[2].x; i++) s.insert(make_pair(i, a[2].y)); if (a[2].y > a[1].y) for (i = a[1].y; i <= a[2].y; i++) s.insert(make_pair(a[1].x, i)); else for (i = a[2].y; i <= a[0].y; i++) s.insert(make_pair(a[1].x, i)); } } else if (a[1].x == a[2].x) { for (i = a[1].y; i <= a[2].y; i++) s.insert(make_pair(a[1].x, i)); if (a[0].y <= a[2].y && a[0].y >= a[1].y) for (i = a[0].x; i <= a[1].x; i++) s.insert(make_pair(i, a[0].y)); else { for (i = a[0].x; i <= a[1].x; i++) s.insert(make_pair(i, a[0].y)); if (a[0].y > a[2].y) for (i = a[2].y; i <= a[0].y; i++) s.insert(make_pair(a[1].x, i)); else for (i = a[0].y; i <= a[1].y; i++) s.insert(make_pair(a[1].x, i)); } } else { for (i = a[0].x; i <= a[1].x; i++) s.insert(make_pair(i, a[0].y)); if (a[0].y > a[1].y) for (i = a[1].y; i <= a[0].y; i++) s.insert(make_pair(a[1].x, i)); else for (i = a[0].y; i <= a[1].y; i++) s.insert(make_pair(a[1].x, i)); for (i = a[1].x; i <= a[2].x; i++) s.insert(make_pair(i, a[2].y)); if (a[2].y < min(a[1].y, a[0].y)) for (i = a[2].y; i <= min(a[1].y, a[0].y); i++) s.insert(make_pair(a[1].x, i)); else if (a[2].y > max(a[1].y, a[0].y)) for (i = max(a[1].y, a[0].y); i <= a[2].y; i++) s.insert(make_pair(a[1].x, i)); } cout << s.size() << endl; for (set<pair<int, int> >::iterator it = s.begin(); it != s.end(); it++) cout << it->first << " " << it->second << endl; }
8
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if ((a == 0) || (b == 0)) return a + b; return gcd(b, a % b); } long long pow_mod(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % 1000000007; a = (a * a) % 1000000007; b >>= 1; } return res; } vector<int> adj[100010]; long long col[100010], visited[100010], arr[100010]; long long c0, c1, cnt; void dfs(long long src, long long par) { visited[src] = 1; long long h = adj[src].size(), i, xx; cnt++; for (i = 0; i <= h - 1; i++) { xx = adj[src][i]; if ((xx == par) || (visited[xx] == 1)) { continue; } dfs(xx, src); } } long long comb3(long long N) { return (N * (N - 1) * (N - 2)) / 6; } long long comb2(long long N) { return (N * (N - 1)) / 2; } bool bipartile(long long src) { long long uu = 0, i, h, xx; queue<long long> qu; col[src] = 1; qu.push(src); c1++; while (!qu.empty()) { uu = qu.front(); qu.pop(); h = adj[uu].size(); for (i = 0; i <= h - 1; i++) { xx = adj[uu][i]; if (col[xx] == -1) { col[xx] = 1 - col[uu]; qu.push(xx); if (col[xx] == 1) { c1++; } else { c0++; } } else if (col[xx] == col[uu]) { return false; } } } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long N, i, j, A, B, maxx = -9223372036854775807, M; string st; cin >> N >> M; for (i = 0; i <= M - 1; i++) { cin >> A >> B; --A; --B; adj[A].push_back(B); adj[B].push_back(A); } memset(col, -1, sizeof(col)); for (i = 0; i <= N - 1; i++) { if (col[i] == -1) { if (bipartile(i) != 1) { cout << "0 1"; return 0; } } } for (i = 0; i <= N - 1; i++) { if (visited[i] == 0) { cnt = 0; dfs(i, -1); maxx = ((maxx) > (cnt) ? (maxx) : (cnt)); arr[cnt]++; } } if (maxx == 1) { cout << "3 "; cout << comb3(N); return 0; } else if (maxx == 2) { cout << "2 "; long long ans = (comb2(arr[2]) * 4) + (arr[2] * (N - 2 * arr[2])); cout << ans; return 0; } else { long long ans = 0; cout << "1 "; memset(col, -1, sizeof(col)); for (i = 0; i <= N - 1; i++) { if (col[i] == -1) { c0 = 0; c1 = 0; bipartile(i); ans += comb2(c0) + comb2(c1); } } cout << ans; } return 0; }
12
#include <bits/stdc++.h> using namespace std; long long calc(long long n) { long long i, res = n; for (i = 2; i <= sqrt(n); i++) if (n % i == 0) { while (n % i == 0) n /= i; res -= res / i; } if (n > 1) res -= res / n; return res; } int main() { long long n, k; cin >> n >> k; long long res = n; k = (k + 1) / 2; while (k--) { res = calc(res); if (res == 1) break; } cout << res % 1000000007; }
13
#include <bits/stdc++.h> using namespace std; static const int INF = 500000000; template <class T> void debug(T a, T b) { for (; a != b; ++a) cerr << *a << ' '; cerr << endl; } int n, k; long long int ans = 0; const long long int mod = 1000000007; int mig[10]; int state[10], vis[10]; bool rec(int v) { if (state[v] == 1) return true; if (vis[v]) return false; vis[v] = 1; if (!rec(mig[v])) return false; state[v] = 1; return true; } bool judge() { for (int i = 0; i < k; ++i) state[i] = -1, vis[i] = 0; state[0] = 1; for (int i = 0; i < k; ++i) if (state[i] == -1) { if (rec(i) == 0) return false; } return true; } void dfs(int dep) { if (dep == k) { if (judge()) ++ans; return; } for (int i = 0; i < k; ++i) { mig[dep] = i; dfs(dep + 1); } } int main() { cin >> n >> k; dfs(1); ans = (ans * k) % mod; for (int i = k; i < n; ++i) ans = (ans * (n - k)) % mod; cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int dp[101][1 << 18], id[66], a[111]; int ans[101][1 << 18], b[111], p[111], mask[111]; int main() { int n, cnt = 0; for (int i = 2; i <= 60; i++) { int flag = 0; for (int j = 2; j < i; j++) if (i % j == 0) flag = 1; if (!flag) p[cnt++] = i; } for (int i = 1; i <= 60; i++) { for (int j = 0; j < cnt; j++) { if (i % p[j] == 0) mask[i] |= (1 << j); } } cin >> n; memset(dp, 0x3f3f3f3f, sizeof(dp)); for (int i = 1; i <= n; i++) cin >> a[i]; dp[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < (1 << 17); j++) { for (int k = 1; k <= 60; k++) { if (mask[k] & j) continue; if (dp[i - 1][j] + abs(a[i] - k) < dp[i][j | mask[k]]) { dp[i][j | mask[k]] = dp[i - 1][j] + abs(a[i] - k); ans[i][j | mask[k]] = k; } } } } int minn = 0x3f3f3f3f; int mins = 0; for (int i = 0; i < (1 << 17); i++) { if (dp[n][i] < minn) { minn = dp[n][i]; mins = i; } } for (int i = n; i >= 1; i--) { b[i] = ans[i][mins]; mins -= mask[b[i]]; } for (int i = 1; i <= n; i++) cout << b[i] << ' '; cout << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 1000006; int n; int a[N]; long long s[N]; int x[N]; long long y[N]; int top = 0; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i]; x[0] = y[0] = 0; for (int i = 1; i <= n; ++i) { while (top && 1.0 * (s[i] - y[top]) / (i - x[top]) < 1.0 * (s[i] - y[top - 1]) / (i - x[top - 1])) --top; ++top; x[top] = i; y[top] = s[i]; } for (int i = 1; i <= top; ++i) { for (int j = x[i - 1] + 1; j <= x[i]; ++j) { printf("%.12lf\n", 1.0 * (y[i] - y[i - 1]) / (x[i] - x[i - 1])); } } return 0; }
13
#include <bits/stdc++.h> using namespace std; inline long long read() { char c = getchar(); long long x = 0, f = 1; while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f; } struct node { long long u, v, w; } e[200005 << 1]; long long n, m, tot, f[200005], a[200005], k, cnt, ans; inline long long getf(long long x) { return f[x] == x ? x : f[x] = getf(f[x]); } inline bool merge(long long x, long long y) { long long fx = getf(x), fy = getf(y); if (fx != fy) f[fx] = fy; else return 0; return 1; } inline bool cmp(node x, node y) { return x.w < y.w; } signed main() { n = read(), m = read(); k = 1; for (long long i = 1; i <= n; i++) f[i] = i, a[i] = read(); for (long long i = 1; i <= n; i++) if (a[k] > a[i]) k = i; for (long long i = 1; i <= m; i++) e[i].u = read(), e[i].v = read(), e[i].w = read(); for (long long i = 1; i <= n; i++) { if (k == i) continue; e[i + m].u = k; e[i + m].v = i; e[i + m].w = a[i] + a[k]; } sort(e + 1, e + m + n + 1, cmp); for (long long i = 2; i <= n + m; i++) { if (merge(e[i].u, e[i].v)) { cnt++; ans += e[i].w; if (cnt == n) break; } } cout << ans << endl; }
11
#include <bits/stdc++.h> using namespace std; int a[30][30]; char st[110][110]; int in[30]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%s", st[i]); memset(a, 0, sizeof(a)); for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { int l1 = strlen(st[i]); int l2 = strlen(st[j]); int flag = 0; for (int k = 0; k < min(l1, l2); ++k) if (st[i][k] != st[j][k]) { a[st[i][k] - 'a'][st[j][k] - 'a'] = 1; flag = 1; break; } if (!flag) { if (l1 > l2) { printf("Impossible\n"); return 0; } } } memset(in, 0, sizeof(in)); for (int i = 0; i < 26; ++i) for (int j = 0; j < 26; ++j) if (a[i][j]) ++in[j]; int ans[26]; for (int i = 0; i < 26; ++i) { int flag = 0; for (int j = 0; j < 26; ++j) if (in[j] == 0) { ans[i] = j; flag = 1; break; } if (!flag) { printf("Impossible\n"); return 0; } in[ans[i]] = -1; for (int j = 0; j < 26; ++j) if (a[ans[i]][j]) --in[j]; } for (int i = 0; i < 26; ++i) { char ch = 'a' + ans[i]; printf("%c", ch); } printf("\n"); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxn = 3e3 + 15; const double eps = 1e-7; const int INF = 0x3f3f3f3f; long long read() { long long x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f; } int n; string s[maxn], t[maxn]; char tmp[maxn]; int z[maxn << 1]; void getZ(const string &s) { for (int i = 1, l = 0, r = -1; i < ((int)(s).size()); ++i) { if (i + z[i - l] < r) { z[i] = z[i - l]; } else { l = i; r = max(r, i); while (r < ((int)(s).size()) && s[r] == s[r - l]) r++; z[i] = r - l; } } } int getPos(const string &s, const string &t) { getZ(t + '#' + s); for (int i = 0; i < ((int)(s).size()); ++i) { if (z[((int)(t).size()) + 1 + i] >= ((int)(t).size())) return i; } return -1; } int ls[maxn], rs[maxn]; void solve() { string from, to; for (int i = 0; i < n; i++) { if (s[i] == t[i]) { ls[i] = -1; rs[i] = -1; continue; } auto &l = ls[i], &r = rs[i]; l = 0; r = ((int)(s[i]).size()); while (s[i][l] == t[i][l]) l++; while (s[i][r - 1] == t[i][r - 1]) --r; string curs = s[i].substr(l, r - l), curt = t[i].substr(l, r - l); if (from.empty()) { from = curs; to = curt; } if (from != curs || to != curt) { printf("NO\n"); return; } } while (1) { char c = 0; bool bad = 0; for (int i = 0; i < n; ++i) { if (ls[i] == -1) continue; if (ls[i] == 0) { bad = 1; break; } if (!c) { c = s[i][ls[i] - 1]; } if (c != s[i][ls[i] - 1]) { bad = 1; break; } } if (bad) break; from.insert(from.begin(), c); to.insert(to.begin(), c); for (int i = 0; i < n; ++i) { if (ls[i] == -1) continue; ls[i]--; } } while (1) { char c = 0; bool bad = 0; for (int i = 0; i < n; ++i) { if (ls[i] == -1) continue; if (rs[i] >= ((int)(s[i]).size())) { bad = 1; break; } if (!c) { c = s[i][rs[i]]; } if (c != s[i][rs[i]]) { bad = 1; break; } } if (bad) break; from += c; to += c; for (int i = 0; i < n; ++i) { if (ls[i] == -1) continue; rs[i]++; } } for (int i = 0; i < n; i++) { int pos = getPos(s[i], from); if (ls[i] == -1) { if (pos != -1) { printf("NO\n"); return; } } else { if (pos != ls[i]) { printf("NO\n"); return; } } } printf("YES\n"); printf("%s\n%s\n", from.c_str(), to.c_str()); } int main() { {}; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%s", tmp); s[i] = tmp; } for (int i = 0; i < n; ++i) { scanf("%s", tmp); t[i] = tmp; } solve(); return 0; }
16
#include <bits/stdc++.h> using namespace std; int mod(int a, int b) { return a >= 0 ? a % b : (b + a % b) % b; } int dceil(int a, int b) { return (a + b - 1) / b; } struct Diff { int x; int y; long long v; }; long long calc(long long x, long long y, long long z) { long long xy = x - y; long long yz = y - z; long long zx = z - x; return xy * xy + yz * yz + zx * zx; } int closesti(long long z, long long a[], int na) { int s = 0; int e = na - 1; while (s < e - 1) { int m = (s + e) / 2; if (a[m] < z) { s = m; } else { e = m; } } if (abs(z - a[e]) > abs(z - a[s])) { return s; } return e; } Diff closest(long long z, long long a[], int na, long long b[], int nb) { Diff res; res.x = closesti(z, a, na); res.y = closesti(z, b, nb); return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long a[100010]; long long b[100010]; long long c[100010]; int tcase; cin >> tcase; while (tcase--) { int na, nb, nc; cin >> na >> nb >> nc; for (int vi = 0; vi < na; vi++) { cin >> a[vi]; } for (int vi = 0; vi < nb; vi++) { cin >> b[vi]; } for (int vi = 0; vi < nc; vi++) { cin >> c[vi]; } sort(a, a + na); sort(b, b + nb); sort(c, c + nc); long long res = LLONG_MAX; for (int i = 0; i < na; i++) { Diff cl = closest(a[i], b, nb, c, nc); long long v = calc(a[i], b[cl.x], c[cl.y]); res = min(res, v); } for (int i = 0; i < nb; i++) { Diff cl = closest(b[i], a, na, c, nc); long long v = calc(b[i], a[cl.x], c[cl.y]); res = min(res, v); } for (int i = 0; i < nc; i++) { Diff cl = closest(c[i], a, na, b, nb); long long v = calc(c[i], a[cl.x], b[cl.y]); res = min(res, v); } cout << res << "\n"; } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s1, s2, s3; cin >> s1 >> s2 >> s3; int k = 1, n = s1.size(); for (int i = 0; i < n; i++) { if (s1[i] == s2[i]) { if (s1[i] != s3[i] || s2[i] != s3[i]) { k = 0; break; } } else { if (s1[i] == s3[i]) swap(s1[i], s3[i]); else if (s2[i] == s3[i]) swap(s2[i], s3[i]); else { k = 0; break; } } } if (k == 0) cout << "NO\n"; else { cout << "YES\n"; } } }
0
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 2e5 + 5; inline int read() { int res = 0, ch, flag = 0; if ((ch = getchar()) == '-') flag = 1; else if (ch >= '0' && ch <= '9') res = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') res = res * 10 + ch - '0'; return flag ? -res : res; } int n, m, k; struct node { int x, y; }; node p[N]; vector<int> data; int bit[N], t[N]; void add(int i) { while (i <= m) { bit[i]++; i += i & -i; } } int sum(int i) { int res = 0; while (i > 0) { res += bit[i]; i -= i & -i; } return res; } bool cmp(node a, node b) { if (a.y != b.y) return a.y > b.y; return a.x < b.x; } void solve() {} int main() { ios::sync_with_stdio(false); cin.tie(0); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &p[i].x, &p[i].y); data.push_back(p[i].x); } sort(data.begin(), data.end()); data.erase(unique(data.begin(), data.end()), data.end()); m = data.size(); for (int i = 1; i <= n; i++) p[i].x = lower_bound(data.begin(), data.end(), p[i].x) - data.begin() + 1; sort(p + 1, p + n + 1, cmp); int cnt = 0; long long ans = 0; for (int i = 1; i <= n;) { int j = i; while (j < n && p[j].y == p[j + 1].y) j++; t[i - 1] = 0; for (int k = i; k <= j; k++) if (sum(p[k].x) == sum(p[k].x - 1)) { add(p[k].x); cnt++; } for (int k = i; k <= j; k++) t[k] = sum(p[k].x); for (int k = i; k <= j; k++) ans += 1LL * (t[k] - t[k - 1]) * (cnt - t[k] + 1); i = j + 1; } printf("%I64d\n", ans); return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { int n, m, voy, parado = 1; long long mov = 0; cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> voy; mov += voy - parado; if (parado > voy) mov += n; parado = voy; } cout << mov; return 0; }
2
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=2e5+10,M=1e9+7,OO=0x3f3f3f3f; int main(){ //freopen("haa.txt","r",stdin); //freopen("myfile.txt","w",stdout); int t; scanf("%d",&t); while(t--){ int n; scanf("%d",&n); vector<int> freq(N); for(int i=0;i<n;++i){ int v; scanf("%d",&v); freq[v]++; } vector<int> dp(N,0); for(int i=1;i<N;++i){ dp[i]+=freq[i]; for(int j=2*i;j<N;j+=i) dp[j]=max(dp[j],dp[i]); } printf("%d\n",n-*max_element(dp.begin(),dp.end())); } return 0; }
11
#include <bits/stdc++.h> using namespace std; double PI = 3.1415926536f; int main() { string a, b; cin >> a >> b; int i, j, n; i = 0; j = 0; n = b.size(); for (i = 0; i < n; i++) { if (b[i] == a[j]) { j++; } } cout << j + 1; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long sqr(long long x) { return x * x; } long long n, m, k, tot, ans, sum; long long a[1005][1005]; long long sumx[1005], sumy[1005], costx[1005], costy[1005]; string st; char ch; long long calc(long long x, long long y) { if (x < y) return (y - x - 1) * 4 + 2; else return (x - y) * 4 + 2; } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> a[i][j]; sumx[i] += a[i][j]; sumy[j] += a[i][j]; } } for (int i = 0; i <= n; i++) { costx[i] = 0; for (int j = 1; j <= n; j++) { costx[i] += sumx[j] * sqr(calc(i, j)); } } for (int i = 0; i <= m; i++) { costy[i] = 0; for (int j = 1; j <= m; j++) { costy[i] += sumy[j] * sqr(calc(i, j)); } } long long maxx = ~0U >> 2, x = 0, y = 0; maxx *= maxx; for (int i = 0; i <= n; i++) for (int j = 0; j <= m; j++) { long long now = costx[i] + costy[j]; if (now < maxx) { maxx = now; x = i; y = j; } } cout << maxx << endl; cout << x << ' ' << y << endl; }
10
#include <bits/stdc++.h> using namespace std; int main() { long long m, b; cin >> m >> b; long long c = b * m; long long ans = 0; for (long long i = 0; i <= c; i++) { long long y = b - ceil(i * 1.0 / m); long long cur = i * (i + 1) / 2 * (y + 1) + y * (y + 1) / 2 * (i + 1); ans = max(ans, cur); } cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; int mod = 1e9 + 7; long long Bpow(long long a, long long s) { if (s == 0) { return 1; } long long u = Bpow(a, s / 2); if (s & 1) return u * u % mod * a % mod; else return u * u % mod; } int main() { int n, m, q; cin >> n >> m >> q; vector<int> pos(n + 1); vector<int> p(n); for (int i = 0; i < n; i++) { int a; cin >> a; p[i] = a; pos[a] = i; } vector<map<int, int>> ma(2e5 + 7); vector<int> last(n + 1); vector<int> a(m); for (int i = 0; i < m; i++) { cin >> a[i]; } int uknow = 1; vector<int> ans(m); vector<vector<int>> par(m + 1, vector<int>(20)); for (int i = m - 1; i >= 0; i--) { int c1 = p[(pos[a[i]] + 1) % n]; int uk; if (last[c1]) par[i][0] = last[c1]; last[a[i]] = i; for (int j = 1; j < 20; j++) { par[i][j] = par[par[i][j - 1]][j - 1]; } int now = i; for (int j = 0; j < 20; j++) { if ((n - 1) & (1 << j)) now = par[now][j]; } int c2 = p[(pos[a[i]] - 1 + n) % n]; ans[i] = min((i < m - 1 ? ans[i + 1] : 1e9 + 7), (now ? now : 1e9 + 7)); } vector<int> res; while (q--) { int l, r; cin >> l >> r; --l; --r; if (ans[l] > r && n > 1) res.push_back(0); else res.push_back(1); } for (int i = 0; i < res.size(); i++) { cout << res[i]; } }
12