solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> #pragma comment(linker, "/stack:100000000") using namespace std; const int MOD = 777777777; const int N = 77777; bool w[3][3]; int t[4 * N][3][3]; void add(int &a, int b) { a += b; if (a >= MOD) a -= MOD; } void build(int v, int tl, int tr) { if (tl == tr) { for (int(i) = 0; (i) < (3); (i)++) t[v][i][i] = 1; } else { int tm = (tl + tr) / 2; build(2 * v, tl, tm); build(2 * v + 1, tm + 1, tr); for (int(i) = 0; (i) < (3); (i)++) for (int(j) = 0; (j) < (3); (j)++) { t[v][i][j] = 0; for (int(x) = 0; (x) < (3); (x)++) for (int(y) = 0; (y) < (3); (y)++) { if (w[x][y]) { add(t[v][i][j], (long long)t[2 * v][i][x] * t[2 * v + 1][y][j] % MOD); } } } } } void update(int v, int tl, int tr, int pos, int val) { if (tl == tr) { for (int(i) = 0; (i) < (3); (i)++) for (int(j) = 0; (j) < (3); (j)++) t[v][i][j] = 0; if (val >= 0) { t[v][val][val] = 1; } else { for (int(i) = 0; (i) < (3); (i)++) t[v][i][i] = 1; } } else { int tm = (tl + tr) / 2; if (pos <= tm) { update(2 * v, tl, tm, pos, val); } else { update(2 * v + 1, tm + 1, tr, pos, val); } for (int(i) = 0; (i) < (3); (i)++) for (int(j) = 0; (j) < (3); (j)++) { t[v][i][j] = 0; for (int(x) = 0; (x) < (3); (x)++) for (int(y) = 0; (y) < (3); (y)++) { if (w[x][y]) { add(t[v][i][j], (long long)t[2 * v][i][x] * t[2 * v + 1][y][j] % MOD); } } } } } int main() { int n, m, id, x; scanf("%d %d", &n, &m); for (int(i) = 0; (i) < (3); (i)++) for (int(j) = 0; (j) < (3); (j)++) { scanf("%d", &x); w[i][j] = x; } build(1, 0, n - 1); for (int(i) = 0; (i) < (m); (i)++) { scanf("%d %d", &id, &x); --id, --x; update(1, 0, n - 1, id, x); int res = 0; for (int(j) = 0; (j) < (3); (j)++) for (int(k) = 0; (k) < (3); (k)++) add(res, t[1][j][k]); printf("%d\n", res); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; bool isPrime[1005 + 1]; for (int i = 0; i <= 1005; i++) isPrime[i] = true; vector<int> primes; for (int i = 2; i <= 1005; i++) { if (isPrime[i]) { primes.push_back(i); for (int j = i * i; j <= 1005; j += i) { isPrime[j] = false; } } } int c = 0; for (int i = 1; i <= primes.size(); i++) { int s = primes[i] + primes[i - 1] + 1; if (s <= 1005 && isPrime[s] && s <= n) c++; } cout << (c >= k ? "YES" : "NO") << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; long long inf = 1e18; long long power(long long a, long long b, long long mod) { long long c = 1; a = a % mod; while (b > 0) { if (b % 2) c *= a; c %= mod; b /= 2; a *= a; a %= mod; } return c; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, q; cin >> n >> q; long long a[3][n + 2]; for (long long i = 0; i < 3; i++) { for (long long j = 0; j < n + 2; j++) a[i][j] = 0; } set<pair<long long, long long> > s, s1, s2, s3, s4; while (q--) { long long x, y; cin >> x >> y; if (a[x][y] == 0) a[x][y] = 1; else { if (x == 1) { if (a[1][y] == 1 && a[2][y] == 1) s.erase({y, y}); if (a[1][y] == 1 && a[2][y - 1] == 1) { s1.erase({y, y - 1}); s4.erase({y - 1, y}); } if (a[1][y] == 1 && a[2][y + 1] == 1) { s2.erase({y, y + 1}); s3.erase({y + 1, y}); } } if (x == 2) { if (a[2][y] == 1 && a[1][y] == 1) s.erase({y, y}); if (a[2][y] == 1 && a[1][y - 1] == 1) { s3.erase({y, y - 1}); s2.erase({y - 1, y}); } if (a[2][y] == 1 && a[1][y + 1] == 1) { s4.erase({y, y + 1}); s1.erase({y + 1, y}); } } a[x][y] = 0; } if (x == 1) { if (a[1][y] == 1 && a[2][y] == 1) s.insert({y, y}); if (a[1][y] == 1 && a[2][y - 1] == 1) { s1.insert({y, y - 1}); s4.insert({y - 1, y}); } if (a[1][y] == 1 && a[2][y + 1] == 1) { s2.insert({y, y + 1}); s3.insert({y + 1, y}); } } if (x == 2) { if (a[2][y] == 1 && a[1][y] == 1) s.insert({y, y}); if (a[2][y] == 1 && a[1][y - 1] == 1) { s3.insert({y, y - 1}); s2.insert({y - 1, y}); } if (a[2][y] == 1 && a[1][y + 1] == 1) { s4.insert({y, y + 1}); s1.insert({y + 1, y}); } } if (a[1][1] == 0 && a[2][n] == 0 && s.size() == 0 && s1.size() == 0 && s2.size() == 0 && s3.size() == 0 && s4.size() == 0) cout << "Yes" << "\n"; else cout << "No" << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; long long int Pow(long long int a, long long int b, long long int md, long long int ans = 1) { for (; b; b >>= 1, a = a * a % md) if (b & 1) ans = ans * a % md; return ans % md; } const long long int MAXN = 1e6 + 10; const long long int INF = 8e18; const long long int MOD = 1e9 + 7; long long int seg[MAXN << 2], A[MAXN], mark[MAXN], n, f = 1; vector<long long int> pos[MAXN], vec[MAXN]; void update(long long int ptr, long long int x, long long int id = 1, long long int l = 1, long long int r = MAXN) { if (r - l == 1) { seg[id] = x; return; } long long int mid = (l + r) >> 1; if (ptr < mid) update(ptr, x, id << 1, l, mid); else update(ptr, x, id << 1 | 1, mid, r); seg[id] = min(seg[id << 1], seg[id << 1 | 1]); } long long int get(long long int x, long long int id = 1, long long int l = 1, long long int r = MAXN) { if (seg[id] >= x) return -1; if (r - l == 1) return l; long long int mid = (l + r) >> 1; long long int res = get(x, id << 1, l, mid); if (res == -1) res = get(x, id << 1 | 1, mid, r); return res; } int main() { scanf("%lld", &n); for (long long int i = 1; i <= n; i++) { scanf("%lld", &A[i]); pos[A[i]].push_back(i); f &= (A[i] == 1); } if (f) return !printf("1\n"); for (long long int i = 1; i <= n; i++) { long long int lst = 1; pos[i].push_back(n + 1); for (long long int j : pos[i]) { vec[j].push_back(lst); lst = j + 1; } } for (long long int i = 1; i <= n; i++) { update(A[i], i); for (long long int j : vec[i + 1]) { mark[get(j)] = 1; } } long long int ans = 1; mark[1] = 1; mark[get(1)] = 1; while (mark[ans]) ans++; printf("%lld\n", ans); return 0; }
8
#include<cstdio> #include<algorithm> using namespace std; int n,k; long long a[1005][1005],sum[1005],sumsqr[1005],dif[1005],difsqr[1005]; int DeltaX,WrongTime; int main() { scanf("%d%d",&k,&n); for(int i=1;i<=n;i++) { for(int j=1;j<=k;j++) { scanf("%lld",&a[i][j]); sum[i]+=a[i][j]; sumsqr[i]+=a[i][j]*a[i][j]; } dif[i-1]=sum[i]-sum[i-1]; difsqr[i-1]=sumsqr[i]-sumsqr[i-1]; } // for(int i=1;i<=n;i++) // { // printf("i=%d sum=%lld sqr=%lld dif1=%lld dif2=%lld\n",i-1,sum[i-1],sumsqr[i],dif[i],difsqr[i]); // } if(dif[1]!=dif[2] && dif[1]!=dif[5]) { if(dif[2]==dif[5]) WrongTime=1; else WrongTime=2; DeltaX=dif[5]; } else { for(int i=2;i<=n-1;i++) { if(dif[i]!=dif[i-1]) { WrongTime=i+1; break; } } DeltaX=dif[1]; } long long Sum,SumSqr; if(WrongTime>=4) { Sum=sum[WrongTime-1]+DeltaX; SumSqr=sumsqr[WrongTime-1]+(2*difsqr[WrongTime-2]-difsqr[WrongTime-3]); } else { Sum=sum[WrongTime+1]-DeltaX; SumSqr=sumsqr[WrongTime+1]-(2*difsqr[WrongTime+1]-difsqr[WrongTime+2]); } // printf("Sum=%lld SumSqr=%lld\n",Sum,SumSqr); long long XplusY,XminusY; XminusY=Sum-sum[WrongTime]; XplusY=(SumSqr-sumsqr[WrongTime])/XminusY; printf("%d %lld\n",WrongTime-1,(XplusY+XminusY)/2); }
11
#include <bits/stdc++.h> using namespace std; int memo[105][105][30][2]; int N, M; vector<int> ady[105]; vector<char> alpha[105]; bool dp(int a, int b, int c, bool max) { if (memo[a][b][c][max] != -1) return memo[a][b][c][max]; int win = 0; int u = max ? a : b; for (int i = 0; i < ady[u].size(); i++) { int v = ady[u][i]; if (alpha[u][i] - 'a' < c) continue; if (max && !dp(v, b, alpha[u][i] - 'a', 0)) win = true; if (!max && !dp(a, v, alpha[u][i] - 'a', 1)) win = true; } return memo[a][b][c][max] = win; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> N >> M; while (M--) { int x, y; char a; cin >> x >> y >> a; ady[x].push_back(y); alpha[x].push_back(a); } memset(memo, -1, sizeof memo); for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (dp(i, j, 0, 1)) cout << "A"; else cout << "B"; } cout << "\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int DEBUG = 0; namespace defs { template <class Type> Type read() { Type aa; int bb = 1, ch; while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-') ; ch == '-' ? aa = 0, bb = -1 : aa = ch - '0'; while (ch = getchar(), ch >= '0' && ch <= '9') aa = aa * 10 + ch - '0'; return bb == 1 ? aa : -aa; } const unsigned int rand_x = 11309; const unsigned int rand_y = 1000000019; unsigned int rand_cur; unsigned int rand_int() { return rand_cur = rand_cur * rand_x + rand_y; } const int inf = 1071026353; const int mod = 1000000007; const int modx = 998244353; } // namespace defs namespace geo { struct P { long long x, y; P operator-(const P &p) const { return (P){x - p.x, y - p.y}; } P operator+(const P &p) const { return (P){x + p.x, y + p.y}; } long long operator*(const P &p) const { return x * p.y - y * p.x; } bool operator<(const P &p) const { return x < p.x || (x == p.x && y < p.y); } }; P tmp[300010]; void gethull(int &n, P *p) { sort(p + 1, p + n + 1); int top = 0; for (int i = 1; i <= n; ++i) { while (top > 1 && (tmp[top] - tmp[top - 1]) * (p[i] - tmp[top]) >= 0) --top; tmp[++top] = p[i]; } for (int i = 1; i <= top; ++i) { p[i] = tmp[i]; } n = top; } void query(int n, P *p, int q, long long &ans) { int l = 1, r = n; while (l + 2 < r) { int mid = l + r >> 1; long long z = p[mid].y + q * p[mid].x; long long z2 = p[mid + 1].y + q * p[mid + 1].x; if (z < z2) l = mid + 1; else r = mid; } for (int i = l; i <= r; ++i) { long long z = p[i].y + q * p[i].x; (ans < (z) ? ans = z : 0); } } } // namespace geo namespace st { geo::P p[300010], tmp[300010]; vector<int> node[1200000]; struct cque { int l, r, x, id; } que[300010]; long long ans[300010]; void insert(int i, int l, int r, int p, int id) { node[i].push_back(id); if (l < r) { int mid = l + r >> 1; if (p <= mid) insert(i << 1, l, mid, p, id); else insert(i << 1 | 1, mid + 1, r, p, id); } } void solve(int i, int l, int r, int tot) { int siz = node[i].size(); for (int j = 0; j < siz; ++j) tmp[j + 1] = p[node[i][j]]; geo::gethull(siz, tmp); for (int j = 1; j <= tot; ++j) { if (que[j].l <= l && que[j].r >= r) { geo::query(siz, tmp, que[j].x, ans[que[j].id]); } } if (l == r) return; int mid = l + r >> 1; int tot2 = 0; for (int j = 1; j <= tot; ++j) { if (que[j].l <= l && que[j].r >= r) continue; if (que[j].l <= mid) swap(que[++tot2], que[j]); } solve(i << 1, l, mid, tot2); tot2 = 0; for (int j = 1; j <= tot; ++j) { if (que[j].l <= l && que[j].r >= r) continue; if (que[j].r > mid) swap(que[++tot2], que[j]); } solve(i << 1 | 1, mid + 1, r, tot2); } } // namespace st namespace lightningwork { int (*read)() = defs::read<int>; struct cq { int l, r, id; bool operator<(const cq &q) const { return r < q.r; } } q[100010]; int n, m, num[100010], sum[100010]; int r[100010]; void work() { memset(st::ans, 180, sizeof st::ans); n = read(); for (int i = 1; i <= n; ++i) { num[i] = read(); sum[i] = sum[i - 1] + num[i]; st::p[i].x = num[i]; st::p[i].y = -i * num[i] + sum[i - 1]; st::insert(1, 1, n, i, i); } m = read(); for (int i = 1; i <= m; ++i) { int x = read(); int y = read(); st::que[i].l = st::que[i].x = y - x + 1; st::que[i].r = y; st::que[i].id = i; r[i] = y; } st::solve(1, 1, n, m); for (int i = 1; i <= m; ++i) { printf("%lld\n", sum[r[i]] - st::ans[i]); } } } // namespace lightningwork int main() { if (0) freopen( "work" ".in", "r", stdin), freopen( "work" ".out", "w", stdout); lightningwork::work(); if (0) fclose(stdin), fclose(stdout); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 212345; vector<pair<int, int> > v; int main() { ios::sync_with_stdio(false); int n; long long k, tsum = 0; cin >> n >> k; for (int i = 0; i < (n); ++i) { int x; cin >> x; if (x == 1 && !v.empty() && v.back().first == 1) ++v.back().second; else v.push_back({x, 1}); tsum += x; } n = (int)((v).size()); long long ans = 0; for (int i = 0; i < (n); ++i) { while (v[i].second >= 1) { long long cp = 1, csum = 0; for (int j = i; j < (n); ++j) { long long p = v[j].first; long long qtd = v[j].second; if (p != 1) { long long limp = k * 10000000000000 / cp; if (p > limp) break; cp *= p; csum += p; if (cp == csum * k) ++ans; } else { long long num = cp - k * csum; if (num % k == 0) { long long x = num / k; if (x >= 1 && x <= qtd) ++ans; } csum += qtd; } } --v[i].second; } } cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; cin >> a >> b >> c >> d; a = max(3 * a / 10, a - (a * c / 250)); b = max(3 * b / 10, b - (b * d / 250)); if (a > b) cout << "Misha"; else if (a < b) { cout << "Vasya"; } else { cout << "Tie"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 200 + 5, LG = 40, inf = 1e9; pair<int, int> dp[2][N][LG * N]; int n, m, a[N], b[N]; pair<int, int> mx(pair<int, int> x, pair<int, int> y) { if (x.first == y.first) return ((x.second > y.second) ? x : y); return ((x.first > y.first) ? x : y); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { long long x; cin >> x; while (x % 2 == 0) a[i]++, x /= 2; while (x % 5 == 0) b[i]++, x /= 5; } for (int i = 0; i <= 1; i++) for (int j = 0; j <= m; j++) for (int k = 0; k < N * LG; k++) dp[i][j][k] = {-inf, -inf}; dp[0][0][0] = {0, 0}; for (int i = 0; i < n; i++) { for (int j = 0; j <= min(i + 1, m); j++) { for (int k = 0; k <= (i + 1) * LG; k++) { dp[(i + 1) & 1][j][k] = dp[i & 1][j][k]; if (j && k >= b[i]) { int mn = min(k, dp[i & 1][j - 1][k - b[i]].second + a[i]); dp[(i + 1) & 1][j][k] = mx(dp[(i + 1) & 1][j][k], make_pair(mn, dp[i & 1][j - 1][k - b[i]].second + a[i])); } } } } pair<int, int> ans = {0, 0}; for (int i = 0; i <= n * LG; i++) ans = mx(ans, dp[n & 1][m][i]); cout << ans.first << "\n"; }
6
#include <bits/stdc++.h> using namespace std; int w[101000], n; int main() { int i, j; scanf("%d", &n); for (i = 2; i <= n + 1; i++) { for (j = i + i; j <= n + 1; j += i) { w[j] = 1; } } if (n <= 2) { printf("1\n"); for (i = 1; i <= n; i++) printf("%d ", 1); return 0; } printf("2\n"); for (i = 1; i <= n; i++) { if (!w[i + 1]) printf("1 "); else printf("2 "); } }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 200005; vector<int> g2[MAXN]; vector<pair<int, int> > g[MAXN]; long long int dist[MAXN]; void dijkstra(int x) { memset(dist, -1, sizeof(dist)); priority_queue<pair<long long int, int> > q; dist[x] = 0; q.push({0, x}); while (!q.empty()) { x = q.top().second; long long int c = -q.top().first; q.pop(); if (dist[x] != c) continue; for (int i = 0, colchin = g[x].size(); i < colchin; ++i) { int y = g[x][i].first; long long int c = g[x][i].second; if (dist[y] < 0 || dist[x] + c < dist[y]) dist[y] = dist[x] + c, q.push({-dist[y], y}); } } } int n, m, k; int path[MAXN]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0, colchin = m; i < colchin; ++i) { int u, v; cin >> u >> v; u--; v--; g[v].push_back(pair<int, int>(u, 1)); g2[u].push_back(v); } cin >> k; for (int i = 0, colchin = k; i < colchin; ++i) { int p; cin >> p; p--; path[i] = p; } dijkstra(path[k - 1]); int minimum = 0; int maximum = 0; for (int i = 1, colchin = k; i < colchin; ++i) if (dist[path[i - 1]] <= dist[path[i]]) minimum++; for (int i = 0, colchin = k; i < colchin; ++i) { int cnt = 0; int minimumNode = -1; for (int u : g2[path[i]]) { if (dist[path[i]] - 1 == dist[u]) { cnt++; minimumNode = u; } } if (cnt > 1) { maximum++; } else if (cnt == 1) { if (i + 1 < k && path[i + 1] != minimumNode) { maximum++; } } } cout << minimum << " " << maximum << "\n"; return 0; }
4
#include <bits/stdc++.h> const int MAXD = 25; int n, x, noans; int d[MAXD]; int dn; void init() { scanf("%d%d", &n, &x); ++n; int m = n; for (int i = 2; i * i <= m; ++i) if (m % i == 0) { noans = 1; return; } m = n - 1; for (int i = 2; i * i <= m; ++i) if (m % i == 0) { d[dn++] = i; while (m % i == 0) m /= i; } if (m > 1) d[dn++] = m; } int powermod(int x, int y, int z) { int s = 1; while (y) { if (y & 1) s = (long long)s * x % z; x = (long long)x * x % z; y >>= 1; } return s; } void solve() { if (noans) { printf("-1\n"); return; } for (int i = x - 1; i > 1; --i) if (i % n) { int fl = 1; for (int j = 0; j < dn; ++j) if (powermod(i, (n - 1) / d[j], n) == 1) { fl = 0; break; } if (fl) { printf("%d\n", i); return; } } printf("-1\n"); } int main() { init(); solve(); return 0; }
9
#include <bits/stdc++.h> using namespace std; vector<int64_t> prime; int64_t ar[(int64_t)1e6 + 100]; void sieve() { for (int64_t i = 2; i <= 1e6; i++) { if (ar[i] == 0) { prime.push_back(i); for (int64_t j = i; j <= 1e6; j += i) { ar[j] = 1; } } } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int64_t n; cin >> n; sieve(); multiset<int64_t> mm; for (int64_t i = 0; i < prime.size(); i++) { while (n % prime[i] == 0) { n /= prime[i]; mm.insert(prime[i]); } } vector<int64_t> ans; while (mm.size() > 0) { int64_t t1 = 1; for (int64_t i : mm) t1 *= i; ans.push_back(t1); mm.erase(mm.begin()); } ans.push_back(1); for (int64_t i : ans) cout << i << " "; cout << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int i, i0, n, m, w[10], b[10], A, B, visw[10]; char a[10][10]; int main() { while (scanf("%s", &a[0]) != EOF) { A = B = 10; memset(visw, 0, sizeof(visw)); for (i = 0; i < 8; i++) w[i] = 10, b[i] = 10; for (i = 1; i < 8; i++) scanf("%s", &a[i]); for (i = 0; i < 8; i++) { for (i0 = 0; i0 < 8; i0++) { if (a[i][i0] == 'W') { if (b[i0] != 10) { b[i0] = 10; visw[i0] = 1; } else { if (!visw[i0]) w[i0] = min(w[i0], i); } } if (a[i][i0] == 'B') { if (w[i0] == -1) { continue; } else { b[i0] = min(b[i0], 7 - i); } } } } for (i0 = 0; i0 < 8; i0++) { if (w[i0] != -1 && w[i0] != 10) A = min(A, w[i0]); if (b[i0] != -1 && b[i0] != 10) B = min(B, b[i0]); } if (A <= B) printf("A\n"); else printf("B\n"); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long MAXN = +100500; const long long INF = 1e18; pair<long long, long long> lt[MAXN]; int main() { long long n, L, a; cin >> n >> L >> a; for (int i = 1; i <= n; i++) { cin >> lt[i].first; cin >> lt[i].second; } sort(lt + 1, lt + n + 1); long long ans = 0; for (int i = 1; i < n; i++) { ans += (lt[i + 1].first - (lt[i].first + lt[i].second)) / a; } ans += lt[1].first / a; ans += (L - (lt[n].second + lt[n].first)) / a; cout << ans; }
1
#include <bits/stdc++.h> using namespace ::std; class wast { public: int x; int y; wast(int x1, int y1) { x = x1; y = y1; } bool operator<(wast const &obj) const { if (x < obj.x) return true; else if (x > obj.x) return false; else if (y < obj.y) { return true; } else { return false; } } }; int main() { int n, m, k, t; cin >> n >> m >> k >> t; set<wast> w; vector<int> wn(n, 0); for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; x--; y--; wast temp(x, y); w.insert(temp); wn[x]++; } for (int i = 0; i < t; i++) { int x, y; cin >> x >> y; x--; y--; wast temp(x, y); if (w.find(temp) != w.end()) { cout << "Waste" << endl; } else { int c = 0; int d = 0; for (int i = 0; i < x; i++) { d = d + wn[i]; c = c + m; } c = c - d; c = c % 3; for (int i = 0; i < y; i++) { wast temp2(x, i); if (w.find(temp2) != w.end()) { ; } else { c++; c = c % 3; } } if (c == 0) { cout << "Carrots" << endl; } else if (c == 1) { cout << "Kiwis" << endl; } else if (c == 2) { cout << "Grapes" << endl; } } } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long int sum[5001]; long long int seq[5002][5001]; int m, n; long long int fun_seq(int st, int k) { if (seq[st][k] == -1) { long long int res1 = (long long)0, prev_sum = (long long)-1; for (int i = st + m - 1; i <= n - (k - 1) * m; i++) { if (prev_sum < sum[i] - sum[i - m]) { long long int temp = sum[i] - sum[i - m] + fun_seq(i + 1, k - 1); if (res1 < temp) res1 = temp; prev_sum = sum[i] - sum[i - m]; } } seq[st][k] = res1; } return seq[st][k]; } int main() { for (int i = 1; i <= 5001; i++) for (int j = 1; j <= 5000; j++) seq[i][j] = -1; for (int i = 1; i <= 5001; i++) seq[i][0] = 0; int k; scanf("%d%d%d", &n, &m, &k); int a[n + 1]; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sum[0] = 0; sum[1] = a[1]; for (int i = 2; i <= n; i++) { sum[i] = sum[i - 1] + a[i]; } long long int res1 = (long long)0, prev_sum = (long long)-1; for (int i = m; i <= n - (k - 1) * m; i++) { if (prev_sum < sum[i] - sum[i - m]) { long long int temp = sum[i] - sum[i - m] + fun_seq(i + 1, k - 1); if (res1 < temp) res1 = temp; prev_sum = sum[i] - sum[i - m]; } } printf("%lld ", res1); return 0; }
4
#include <bits/stdc++.h> using namespace std; struct ot { int l, r, i; bool f; } a[200005]; int ans[200005]; bool cmp(ot a, ot b) { if (a.l == b.l) return a.r > b.r; else return a.l < b.l; } int main() { int n, m, n1 = 0; cin >> n >> m; for (int i = 0; i < m; i++) { int x; cin >> a[i].l >> a[i].r; a[i].r += a[i].l; a[i].i = i; a[i].f = 0; } sort(a, a + m, cmp); a[0].f = 1; int r = a[0].r; for (int i = 1; i < m;) { int k = i; while (a[i].l <= r && i < m) { if (a[i].r > a[k].r) k = i; i++; } if (a[k].r > r) { a[k].f = 1; r = a[k].r; } } for (int i = 0; i < m; i++) if (!a[i].f) { ans[n1] = a[i].i; n1++; } cout << n1 << endl; for (int i = 0; i < n1; i++) cout << ans[i] + 1 << " "; return 0; }
6
#include<bits/stdc++.h> #define F first #define S second #define PB push_back #define sz(s) (int(s.size())) #define bit(n, k) (((n)>>(k))&1) using namespace std; typedef pair<int, int> pii; typedef long long ll; const int maxn = 1e6 + 10; int f[maxn]; int pr[maxn]; int main(){ ios_base::sync_with_stdio(0), cin.tie(), cout.tie(); for(int i = 2; i < maxn; i++){ if(pr[i] == 0){ for(int j = i; j < maxn; j+= i) pr[j]= i; } } f[1] = 1; for(int i = 2; i < maxn; i++){ int p = pr[i], cnt = 0, x = i; while(x % p == 0) x/=p, cnt++; f[i] = f[x] * (cnt & 1 ? p : 1); } int q; cin >> q; while(q--){ int n; cin >> n; map<int, int> mp; for(int i = 0; i < n; i++){ int x; cin >> x; mp[f[x]]++; } int ans0 = 0, ans1 = 0; for(auto it : mp){ ans0 = max(ans0, it.S); if(it.F == 1 || (it.S&1) == 0) ans1+= it.S; } ans1 = max(ans1, ans0); int qq; cin >> qq; while(qq--){ ll x; cin >> x; if(x == 0) cout << ans0 << "\n"; else cout << ans1 << "\n"; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; int lim = 1000000; int mod = 1000000000 + 7; int inf = 1000000000; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; string p; cin >> p; vector<int> y(m + 2); y[m + 1] = -1; for (int i = 1; i <= m; i++) { cin >> y[i]; } int pl = (int)p.length(); vector<int> z(pl); z[0] = 0; for (int i = 1, l = 0, r = 0; i < pl; ++i) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < pl && p[z[i]] == p[i + z[i]]) ++z[i]; if (i + z[i] - 1 > r) { l = i; r = i + z[i] - 1; } } int pos = 1; for (int i = 2; i <= m; i++) { if (y[i] - y[i - 1] < pl) { if (z[y[i] - y[i - 1]] < pl - (y[i] - y[i - 1])) { pos = 0; break; } } } if (!pos) cout << 0; else { int empty = 0; int counter = -pl - 100; int j = 1; for (int i = 1; i <= n; i++) { if (y[j] == i) { counter = y[j]; j++; } else if (i - counter >= pl) empty++; } long long int res = 1; while (empty--) { res *= 26; res %= mod; } cout << res; } }
5
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } long long N, M; bitset<2000> A[2000]; bitset<2000> B[2000]; long long X[555555], Y[555555]; signed main() { scanf("%lld%lld", &N, &M); for (long long i = 0; i < (M); i++) { long long a, b; scanf("%lld%lld", &a, &b); a--; b--; A[a][b] = 1; X[i] = a; Y[i] = b; } for (long long i = 0; i < (N); i++) B[i][i] = 1; for (long long i = 0; i < (N); i++) { long long t = -1; for (long long j = i; j < N; j++) if (A[j][i]) t = j; assert(t != -1); if (t != i) { swap(A[i], A[t]); swap(B[i], B[t]); } for (long long j = 0; j < N; j++) { if (i == j) continue; if (A[j][i] == 0) continue; A[j] ^= A[i]; B[j] ^= B[i]; } } for (long long i = 0; i < (M); i++) { if (B[Y[i]][X[i]]) puts("NO"); else puts("YES"); } return 0; }
10
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long fpow(long long x, long long y, long long mod) { long long ans = 1ll; for (; y; y >>= 1) { if (y & 1) ans = ans * x % mod; x = x * x % mod; } return ans; } const long long maxd = 4e5 + 10; long long a[maxd], b[maxd], ans[maxd]; int main() { long long n; scanf("%lld", &n); for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]); for (long long i = 1; i <= n; i++) b[i] = i; clock_t st = clock(); while (clock() - st < 0.4 * CLOCKS_PER_SEC) { random_shuffle(b + 1, b + 1 + n); long long x = 0, y = 0; for (long long i = 1; i <= n; i++) { long long t = gcd(x, a[b[i]]); if (t == x) y = gcd(a[b[i]], y), ans[b[i]] = 2; else x = t, ans[b[i]] = 1; } if (x == 1 && y == 1) { printf("%s\n", "YES"); for (long long i = 1; i <= n; i++) printf("%lld ", ans[i]); return 0; } } printf("%s\n", "NO"); return 0; }
10
#include <bits/stdc++.h> using namespace std; long double g[55], f[55][55], C[55][55]; long long sz[55], n; vector<long long> G[55]; void dfs(long long x, long long fa) { f[x][1] = 1; sz[x] = 1; for (long long v : G[x]) { if (v == fa) continue; dfs(v, x); for (long long i = 0; i <= sz[x] + sz[v]; i++) g[i] = 0; for (long long i = 1; i <= sz[x]; i++) for (long long j = 1; j <= sz[v]; j++) for (long long k = 0; k <= sz[v]; k++) g[i + k] += f[x][i] * f[v][min(j, k + 1)] * (j <= k ? 0.5 : 1) * C[i + k - 1][k] * C[sz[x] - i + sz[v] - k][sz[v] - k]; for (long long i = 0; i <= sz[x] + sz[v]; i++) f[x][i] = g[i]; sz[x] += sz[v]; } } signed main() { scanf("%lld", &n); for (long long i = 1; i < n; i++) { long long u, v; scanf("%lld%lld", &u, &v); G[u].push_back(v); G[v].push_back(u); } C[0][0] = 1; for (long long i = 1; i <= n * 2; i++) { C[i][0] = 1; for (long long j = 1; j <= i; j++) C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; } long double jc = 1; for (long long i = 1; i < n; i++) jc = jc * i; for (long long i = 1; i <= n; i++) { dfs(i, 0); printf("%.9Lf\n", f[i][n] / jc); } return 0; }
10
#include <bits/stdc++.h> using namespace std; int n, m, k, h; long long pref[2 * 100000 + 5]; unordered_map<int, int> map_val; vector<int> value; vector<int> doNeed; int modulo = 1000000007; int arr[1000000 + 5]; int t[1000000]; void build(int a[], int v, int tl, int tr) { if (tl == tr) t[v] = a[tl]; else { int tm = (tl + tr) / 2; build(a, v * 2, tl, tm); build(a, v * 2 + 1, tm + 1, tr); } } void update(int v, int tl, int tr, int l, int r, int add) { if (l > r) return; if (l == tl && tr == r) t[v] += add; else { int tm = (tl + tr) / 2; update(v * 2, tl, tm, l, min(r, tm), add); update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, add); } } int get(int v, int tl, int tr, int pos) { if (tl == tr) return t[v]; int tm = (tl + tr) / 2; if (pos <= tm) return t[v] + get(v * 2, tl, tm, pos); else return t[v] + get(v * 2 + 1, tm + 1, tr, pos); } vector<pair<int, bool>> all_nodes; int main() { cin >> n >> k; vector<pair<pair<int, int>, int>> all_vert(n); for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; a--; b--; all_vert[i] = make_pair(make_pair(a, b), i); update(1, 0, 210000, a, b, 1); } sort(all_vert.begin(), all_vert.end()); vector<int> res; priority_queue<pair<int, pair<int, int>>> q; int vertInd = 0; for (int i = 0; i < 2100000; i++) { int cur = get(1, 0, 210000, i); if (cur > k) { pair<int, pair<int, int>> cur_p; for (int j = vertInd; j < n; j++) { if (all_vert[j].first.first <= i) { q.push(make_pair( all_vert[j].first.second, make_pair(all_vert[j].first.first, all_vert[j].second))); if (j == n - 1) { vertInd = n; } } else { vertInd = j; break; } } cur_p = q.top(); q.pop(); update(1, 0, 210000, cur_p.second.first, cur_p.first, -1); i--; res.push_back(cur_p.second.second + 1); } } cout << res.size() << endl; for (int i = 0; i < res.size(); i++) { cout << res[i] << " "; } }
5
#include <bits/stdc++.h> using namespace std; long long mod = (long long)1e9 + 7; long long fast_expo(long long base, long long pw) { long long ans = 1; while (pw > 0) { if (pw % 2 == 1) ans = ans * base % mod; base = base * base % mod; pw = pw / 2; } return ans; } int main() { long long a, b, n, x; cin >> a >> b >> n >> x; long long inv1 = fast_expo(a - 1, mod - 2); long long an = fast_expo(a, n); long long bolu = (an - 1) * inv1 % mod; if (a == 1) bolu = n % mod; long long ilk = b * bolu % mod; long long iki = an * x % mod; long long res = ilk + iki; if (n == 1) { res = a * x % mod + b % mod; } cout << res % mod << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end()); vector<long long int>::iterator ip; ip = unique(v.begin(), v.end()); v.resize(distance(v.begin(), ip)); long long int count = 0; for (int i = 0; i < v.size(); i++) if (v[i] != 0) count++; cout << count << "\n"; }
0
#include <bits/stdc++.h> using namespace std; const long long int INF = LLONG_MAX; vector<long long int> v1, v2; string s1, s2, s3; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int a, b, c, d, e, f, i, j, k, cnt1 = 0, cnt2 = 0, sum1 = 0, sum2 = 0, chk1 = 0, chk2 = 0, T; cin >> a >> b; long long int lagbe = 2 * a; long long int ans = (lagbe / b); if (lagbe % b) ans++; lagbe = 5 * a; ans += (lagbe / b); if (lagbe % b) ans++; lagbe = 8 * a; ans += (lagbe / b); if (lagbe % b) ans++; cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k, t, x, y; cin >> n >> m >> k >> t; vector<pair<int, int>> waste_coords; while (k--) { cin >> x >> y; waste_coords.push_back(make_pair(x, y)); } sort(waste_coords.begin(), waste_coords.end()); vector<string> crops = {"Carrots", "Kiwis", "Grapes"}; while (t--) { cin >> x >> y; int total = (x - 1) * m + y; int wastes = 0; bool is_waste = false; for (int i = 0; i < waste_coords.size(); i++) { if (waste_coords[i].first == x && waste_coords[i].second == y) { is_waste = true; break; } else if (waste_coords[i].first < x || (waste_coords[i].first == x && waste_coords[i].second < y)) { wastes++; } else { break; } } if (is_waste) { cout << "Waste\n"; } else cout << crops[(total - wastes - 1) % 3] << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { double a, b, n, mn = 999999999; int m; cin >> m >> n; while (m--) { cin >> a >> b; if (a / b < mn) mn = a / b; } printf("%.8lf", mn * n); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> Vec; if (n < 10) cout << n << endl; else { long long ts3a = 9, sum = 0; while (true) { if (n > sum) Vec.push_back(ts3a); sum += ts3a; ts3a *= 10; if (sum + ts3a > n) break; } Vec.push_back(n - sum); long long out = 0; for (int i = 0; i < Vec.size(); i++) { out += (i + 1) * Vec[i]; } cout << out << endl; } }
2
#include <bits/stdc++.h> using namespace std; int main() { long long int m, n, i, j, k, z = 0, x, y; scanf("%lld %lld", &(n), &(m)); pair<long long int, pair<long long int, long long int>> ar[n]; for (i = 0; i < n; i++) { scanf("%lld", &(ar[i].second.first)); ar[i].second.second = i; ar[i].first = ar[i].second.first % m; } z = n / m; long long int make_pair[m]; memset(make_pair, 0, sizeof(make_pair)); std::vector<pair<long long int, pair<long long int, long long int>>> v; for (i = 0; i < n; i++) { make_pair[ar[i].first]++; if (make_pair[ar[i].first] > z) { v.push_back(ar[i]); } } sort(v.begin(), v.end()); j = 0; long long int ans = 0; for (i = 0; i < m; ++i) { if (make_pair[i] >= z) continue; k = z - make_pair[i]; for (; j < v.size();) { if (i > v[j].first) { ar[v[j].second.second].second.first += (i - v[j].first); ans += (i - v[j].first); j++; } else { ar[v.back().second.second].second.first += (m + i - v.back().first); ans += (m + i - v.back().first); v.pop_back(); } k--; if (k == 0) break; } } printf("%lld\n", (ans)); for (i = 0; i < n; i++) cout << ar[i].second.first << ' '; return 0; }
5
#include <bits/stdc++.h> template <class T> inline void gmax(T &a, T b) { if (b > a) a = b; } template <class T> inline void gmin(T &a, T b) { if (b < a) a = b; } using namespace std; const int N = 1e5 + 10, M = 1e6 + 10, Z = 1e9 + 7; const double PI = acos(-1.0), eps = 1e-8; const int INF = 1e9; int casenum, casei; int r, d, n, rr, x, y; int main() { scanf("%d%d%d", &r, &d, &n); int num = 0; d = r - d; for (int i = 1; i <= n; i++) { scanf("%d%d%d", &x, &y, &rr); int dis = x * x + y * y; if (dis <= (r - rr) * (r - rr) && dis >= (rr + d) * (rr + d)) num++; } printf("%d\n", num); return 0; }
1
#include <bits/stdc++.h> using namespace std; struct star { int y, x, r; bool operator<(const star& o) const { if (r == o.r) { if (y == o.y) return x < o.x; return y < o.y; } return r < o.r; } }; int main() { int n, m, k; cin >> n >> m >> k; vector<string> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<star> s; for (int y = 0; y < n; y++) { for (int x = 0; x < m; x++) { if (a[y][x] != '*') continue; int mr = min(min(x, y), min(m - x - 1, n - y - 1)); for (int r = 1; r <= mr; r++) { if (a[y - r][x] == '*' && a[y + r][x] == '*' && a[y][x - r] == '*' && a[y][x + r] == '*') s.push_back({y + 1, x + 1, r}); } } } sort(s.begin(), s.end()); if (k <= s.size()) { star t = s[k - 1]; cout << t.y << " " << t.x << endl; cout << (t.y - t.r) << " " << t.x << endl; cout << (t.y + t.r) << " " << t.x << endl; cout << t.y << " " << (t.x - t.r) << endl; cout << t.y << " " << (t.x + t.r) << endl; } else cout << -1 << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int DaysOfMonth[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; deque<int> que; int N, M; string sLog; int conv(const string &s, int l, int r) { int ans = 0; for (int i = l; i <= r; i++) ans = ans * 10 + s[i] - '0'; return ans; } char buffer[100]; int main() { que.clear(); for (int i = 1; i <= 12; i++) DaysOfMonth[i] += DaysOfMonth[i - 1]; scanf("%d %d\n", &N, &M); bool flag = false; while (getline(cin, sLog)) { int month = conv(sLog, 5, 6); int day = conv(sLog, 8, 9); int hour = conv(sLog, 11, 12); int minute = conv(sLog, 14, 15); int second = conv(sLog, 17, 18); int now = second + minute * 60 + hour * 60 * 60 + day * 60 * 60 * 24 + DaysOfMonth[month - 1] * 60 * 60 * 24; while (!que.empty() && now - que.front() + 1 > N) { que.pop_front(); } que.push_back(now); if ((int)que.size() >= M) { int len = sLog.copy(buffer, 19, 0); buffer[len] = '\0'; cout << buffer << endl; flag = true; break; } } if (!flag) cout << -1 << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int n, x, a[maxn], b[maxn], p, t, r, mx, res; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &x); t = x; r = 0; p = 0; for (int j = 2; 1ll * j * j <= x; ++j) { if (t % j == 0) { while (t % j == 0) { t /= j; } b[r++] = j; p = p > a[j] ? p : a[j]; } } if (t > 1) { b[r++] = t; p = p > a[t] ? p : a[t]; } for (int j = 0; j < r; ++j) { a[b[j]] = p + 1; } mx = p + 1; res = res > mx ? res : mx; } printf("%d", res); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; bool a[26][26]; string s; for (int i = 0; i < n; i++) { cin >> s; for (int j = 0; j < m; j++) a[i][j] = (s[j] - '0' == 0 ? false : true); } int ans = 0; for (int i = 0; i < n; i++) { bool temp[26] = {false}; for (int j = i; j < n; j++) { for (int k = 0; k < m; k++) temp[k] |= a[j][k]; int ml = 0, l = 0; for (int k = 0; k < m; k++) { if (temp[k]) ml = max(ml, l), l = 0; else l++; } ml = max(ml, l); if (ml) ans = max(ans, (j - i + 1) * 2 + ml * 2); } } cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<long long> graph[200001]; bool visited[200001] = {false}; long long dp[200001] = {0}; long long c[200001] = {0}; long long a[200001] = {0}; long long zero = 0; long long dfs(long long s) { long long result = 0; if (a[s] == 1) result++; else result--; visited[s] = true; for (long long i = 0; i < graph[s].size(); i++) { long long d; if (!visited[graph[s][i]]) { d = dfs(graph[s][i]); if (d > 0) { result += d; } } } c[s] = result; return result; } void deepee(long long s, long long m) { visited[s] = true; long long result = 0; if (a[s] == 1) { result++; } else { result--; } result += max(zero, m); for (long long i = 0; i < graph[s].size(); i++) { if (!visited[graph[s][i]]) { result += max(c[graph[s][i]], zero); } } for (long long i = 0; i < graph[s].size(); i++) { if (!visited[graph[s][i]]) { if (c[graph[s][i]] > 0) { deepee(graph[s][i], result - c[graph[s][i]]); } else { deepee(graph[s][i], result); } } } dp[s] = result; } int main() { long long n, x, y; cin >> n; for (long long i = 1; i <= n; i++) { cin >> a[i]; } for (long long i = 0; i < n - 1; i++) { cin >> x >> y; graph[x].push_back(y); graph[y].push_back(x); } dfs(1); memset(visited, false, sizeof(visited)); deepee(1, 0); for (long long i = 1; i <= n; i++) { cout << dp[i] << " "; } cout << endl; }
5
#include <bits/stdc++.h> const long long N = 2 * 1e2 + 9; using namespace std; long long t, n, m; bool a[N][N]; int main() { cin >> t; while (t--) { cin >> n >> m; long long b[N] = {}, c[N] = {}; long long ans1 = n, ans2 = m; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> a[i][j]; if (a[i][j]) { if (b[i] == 0) { ans1--; b[i]++; } if (c[j] == 0) { ans2--; c[j]++; } } } } long long ans = min(ans1, ans2); string answer = (ans % 2 == 0) ? "Vivek" : "Ashish"; cout << answer << endl; } }
1
#include <bits/stdc++.h> int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); long long i = 0; int n; std::cin >> n; while (i * i < n) i++; int whole_part = (i * i - n) / i; std::cout << i + i - whole_part; return 0; }
1
#pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using db = double; using str = string; using pi = pair<int,int>; using pl = pair<ll,ll>; using pd = pair<double,double>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<double>; using vs = vector<string>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; #define tcT template<class T #define tcTUU tcT, class ...U #define tcTU tcT, class U #define tcTU3 tcTU, class C #define tcTU4 tcTU3, class D #define ts to_string #define rsz resize #define ins insert #define all(x) (x).begin(), (x).end() #define sz(v) ((int)(v).size()) #define rall(x) (x).rbegin(), (x).rend() #define pb push_back #define mt make_tuple #define eb emplace_back #define mp make_pair #define lb lower_bound #define ub upper_bound #define fe first #define se second #define bs binary_search #define ft front() #define bk back() #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define trav(a,x) for (auto& a: x) const int di[4]={-1, 0, 1, 0}, dj[4]={0, 1, 0, -1}; const int di8[8]={-1, -1, 0, 1, 1, 1, 0, -1}, dj8[8]={0, 1, 1, 1, 0, -1, -1, -1}; const ld PI = acos((ld)-1); const ll INF = 1e18; const double eps=1e-11;//NOTES:eps mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll randint(ll a, ll b) { return uniform_int_distribution<ll>(a, b)(rng);} using ul = unsigned long long; ul modMul(ul a, ul b, const ul mod) { ll ret = a*b-mod*(ul)((ld)a*b/mod); return ret+((ret<0)-(ret>=(ll)mod))*mod; } ul modPow(ul a, ul b, const ul mod) { if (b == 0) return 1; ul res = modPow(a,b/2,mod); res = modMul(res,res,mod); return b&1 ? modMul(res,a,mod) : res; } bool prime(ul n) { // not ll! if (n < 2 || n % 6 % 4 != 1) return n-2 < 2; ul A[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}, s = __builtin_ctzll(n-1), d = n>>s; trav(a,A) { // ^ count trailing zeroes ul p = modPow(a,d,n), i = s; while (p != 1 && p != n-1 && a%n && i--) p = modMul(p,p,n); if (p != n-1 && i != s) return 0; } return 1; } #define X fe #define Y se template<typename A, typename B> pair<A, B> operator+(const pair<A, B>& a, const pair<A, B>& b) { return mp(a.X + b.X, a.Y + b.Y); } template<typename A, typename B> pair<A, B> operator+=(pair<A, B>& a, const pair<A, B>& b) { a.X += b.X; a.Y += b.Y; return a; } template<typename A, typename B> pair<A, B> operator-(const pair<A, B>& a, const pair<A, B>& b) { return mp(a.X - b.X, a.Y - b.Y); } template<typename A, typename B> pair<A, B> operator-(const pair<A, B>& a) { return mp(-a.X, -a.Y); } template<typename A, typename B> pair<A, B>& operator++(pair<A, B>& a) { ++a.X; ++a.Y; return a; } template<typename A, typename B> pair<A, B>& operator--(pair<A, B>& a) { --a.X; --a.Y; return a; } template<typename A> vector<A>& operator++(vector<A>& a) { for (auto it = a.begin(); it != a.end(); ++it) ++* it; return a; } template<typename A> vector<A>& operator--(vector<A>& a) { for (auto it = a.begin(); it != a.end(); ++it) --* it; return a; } template<typename A, typename B> pair<A, B> operator++(pair<A, B>& a, int) { pair<A, B> b = a; ++a; return b; } template<typename A, typename B> pair<A, B> operator--(pair<A, B>& a, int) { pair<A, B> b = a; --a; return b; } template<typename A> vector<A>& operator++(vector<A>& a, int) { vector<A> b = a; ++a; return b; } template<typename A> vector<A>& operator--(vector<A>& a, int) { vector<A> b = a; --a; return b; } template<typename A, typename B> pair<A, B> operator-=(pair<A, B>& a, const pair<A, B>& b) { a.X -= b.X; a.Y -= b.Y; return a; } bool pow2(int i){ return i&&(i&-i)==i;} constexpr int pct(int x) { return __builtin_popcount(x); } constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down ll half(ll x) { return fdiv(x,2); } bool inc(ll a,ll b,ll c){ return a<=b&&b<=c; } template<class t> int lwb(const vector<t>&v,const t&a){ return lower_bound(all(v),a)-v.begin(); } template<class T> inline T lowbit(T n){return (n^(n-1))&n;}//NOTES:lowbit( template <typename I> struct _reversed_struct { I &v_; explicit _reversed_struct(I &v) : v_{v} {} typename I::reverse_iterator begin() const { return v_.rbegin(); } typename I::reverse_iterator end() const { return v_.rend(); } }; template <typename I> _reversed_struct<I> reversed(I &v) { return _reversed_struct<I>(v); } template <typename T1, typename T2> typename vector<pair<T1, T2>>::iterator lower_bound(const vector<pair<T1, T2>> &v, const T1 &x) { return lower_bound(all(v), x, [](pair<T1, T2> a, pair<T1, T2> b) { return a.fe < b.fe; }); } template <typename T1, typename T2> typename vector<pair<T1, T2>>::iterator upper_bound(const vector<pair<T1, T2>> &v, const T1 &x) { return upper_bound(all(v), x, [](pair<T1, T2> a, pair<T1, T2> b) { return a.fe < b.fe; }); } #define L1(u, ...) [&](auto &&u) { return __VA_ARGS__; } #define L2(u, v, ...) [&](auto &&u, auto &&v) { return __VA_ARGS__; } #define sort_by(x, y) sort(all(x), [&](const auto &l, const auto &r) { return y; }) bool isUpperCase(char c){return c>='A' && c<='Z';}//NOTES:isUpperCase( bool isLowerCase(char c){return c>='a' && c<='z';}//NOTES:isLowerCase( bool isLetter(char c){return c>='A' && c<='Z' || c>='a' && c<='z';}//NOTES:isLetter( bool isDigit(char c){return c>='0' && c<='9';}//NOTES:isDigit( char toLowerCase(char c){return (isUpperCase(c))?(c+32):c;}//NOTES:toLowerCase( char toUpperCase(char c){return (isLowerCase(c))?(c-32):c;}//NOTES:toUpperCase( int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}//NOTES:toInt( ll toInt64(string s){ll r=0;istringstream sin(s);sin>>r;return r;}//NOTES:toInt64( double toDouble(string s){double r=0;istringstream sin(s);sin>>r;return r;}//NOTES:toDouble( template<class T> inline T sqr(T x){return x*x;}//NOTES:sqr double dist(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));}//NOTES:dist( double distR(double x1,double y1,double x2,double y2){return sqr(x1-x2)+sqr(y1-y2);}//NOTES:distR( template<class T> T cross(T x0,T y0,T x1,T y1,T x2,T y2){return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);}//NOTES:cross( int crossOper(double x0,double y0,double x1,double y1,double x2,double y2)//NOTES:crossOper( {double t=(x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);if (fabs(t)<=eps) return 0;return (t<0)?-1:1;} bool isIntersect(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)//NOTES:isIntersect( {return crossOper(x1,y1,x2,y2,x3,y3)*crossOper(x1,y1,x2,y2,x4,y4)<0 && crossOper(x3,y3,x4,y4,x1,y1)*crossOper(x3,y3,x4,y4,x2,y2)<0;} bool isMiddle(double s,double m,double t){return fabs(s-m)<=eps || fabs(t-m)<=eps || (s<m)!=(t<m);}//NOTES:isMiddle( #define removebit(n, b) ((n) & ~(static_cast<std::decay<decltype(n)>::type>(1) << (b))) constexpr ll power(ll x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); } #define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower); #define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper); bool isPower(ll x, ll y) { ll res1 = log(y) / log(x); double res2 = log(y) / log(x); return (res1 == res2); } tcT> bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } tcT> bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } void TIME(){ #ifdef __APPLE__ cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif } tcT> void remDup(vector<T>& v) { sort(all(v)); v.erase(unique(all(v)),end(v)); } tcTU> void remAll(vector<T>& v, U a){ v.erase(remove(all(v),a),v.end()); } tcTU> T fstTrue(T lo, T hi, U f) { //smallest while (lo < hi) { T mid = half(lo+hi); f(mid) ? hi = mid : lo = mid+1; } return lo; } tcTU> T lstTrue(T lo, T hi, U f) { //largest while (lo < hi) { T mid = half(lo+hi+1); f(mid) ? lo = mid : hi = mid-1; } return lo; } // TO_STRING tcTU3> string ts(tuple<T,U,C> p); tcTU4> string ts(tuple<T,U,C,D> p); string ts(char c) { return string(1,c); } string ts(bool b) { #ifdef __APPLE__ return b ? "true" : "false"; #else return ts((int)b); #endif } string ts(const char* s) { return (string)s; } string ts(string s) { return s; } tcT> string ts(complex<T> c) { stringstream ss; ss << c; return ss.str(); } string ts(vb v) { string res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); res += "}"; return res; } template<size_t SZ> string ts(bitset<SZ> b) { string res = ""; F0R(i,sz(b)) res += char('0'+b[i]); return res; } tcTU> string ts(pair<T,U> p); tcT> string ts(T v) { // containers with begin(), end() #ifdef __APPLE__ bool fst = 1; string res = "{"; for (const auto& x: v) { if (!fst) res += ", "; fst = 0; res += ts(x); } res += "}"; return res; #else bool fst = 1; string res = ""; for (const auto& x: v) { if (!fst) res += " "; fst = 0; res += ts(x); } return res; #endif } tcTU> string ts(pair<T,U> p) { #ifdef __APPLE__ return "("+ts(p.fe)+", "+ts(p.se)+")"; #else return ts(p.fe)+" "+ts(p.se); #endif } tcTU3> string ts(tuple<T,U,C> p) { #ifdef __APPLE__ return "("+ts(get<0>(p))+","+ts(get<1>(p))+","+ts(get<2>(p))+")"; #else return ts(get<0>(p))+" "+ts(get<1>(p))+" "+ts(get<2>(p)); #endif } tcTU4> string ts(tuple<T,U,C,D> p) { #ifdef __APPLE__ return "("+ts(get<0>(p))+","+ts(get<1>(p))+","+ts(get<2>(p))+","+ts(get<3>(p))+")"; #else return ts(get<0>(p))+" "+ts(get<1>(p))+" "+ts(get<2>(p))+" "+ts(get<3>(p)); #endif } //INPUT void re(string& d){getline(cin,d) ;getline(cin,d); } tcTU3> void re(tuple<T,U,C>& p); tcTU4> void re(tuple<T,U,C,D>& p); tcT> void re(complex<T>& c); tcTU> void re(pair<T,U>& p); tcT> void re(vector<T>& v); tcT> void rv(int n, vector<T>& x) { x.rsz(n); re(x); } tcT, size_t SZ> void re(array<T,SZ>& a); tcT> void re(T& x) { cin >> x; } void re(double& d) { string t; re(t); d = stod(t); } void re(ld& d) { string t; re(t); d = stold(t); } tcTUU> void re(T& t, U&... u) { re(t); re(u...); } tcT> void re(complex<T>& c) { T a,b; re(a,b); c = {a,b}; } tcTU> void re(pair<T,U>& p) { re(p.fe,p.se); } tcT> void re(vector<T>& x) { trav(a,x) re(a); } tcT, size_t SZ> void re(array<T,SZ>& x) { trav(a,x) re(a); } tcTU3> void re(tuple<T,U,C>& p){re(get<0>(p),get<1>(p),get<2>(p));} tcTU4> void re(tuple<T,U,C,D>& p){re(get<0>(p),get<1>(p),get<2>(p),get<3>(p));} // OUTPUT tcT> void pr(T x) { cout << ts(x); } tcTUU> void pr(const T& t, const U&... u) { pr(t); pr(u...); } void ps() { pr("\n"); } // print w/ spaces tcTUU> void ps(const T& t, const U&... u) { pr(t); if (sizeof...(u)) pr(" "); ps(u...); } struct chash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(pair<int,int> x)const{ return operator()(uint64_t(x.first)<<32|x.second); } }; tcT> using V = vector<T>; tcT, size_t SZ> using AR = array<T,SZ>; tcT> using mpq = priority_queue<T, vector<T>, greater<T>>; tcT> using pq = priority_queue<T>; tcTU> using um = unordered_map<T,U,chash>; tcT> using us = unordered_set<T, chash>; tcT> using PR = pair<T,T>; #define one(x) memset(x,-1,sizeof(x)) #define zero(x) memset(x,0,sizeof(x)) // const int MOD = 1e9+7; const int MOD =998244353; // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/rope> // using namespace __gnu_pbds; // using namespace __gnu_cxx; // #define fbo find_by_order // #define ook order_of_key // tcT> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // tcTU> using ht = gp_hash_table<T,U,chash>; // template<class key, class value, class cmp = std::less<key>> using omap = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>; // using cd = complex<ld>; // using vcd = vector<cd>; // int atMost(oset<pi>& T, int r) { // return T.ook({r,MOD}); } // int getSum(oset<pi>& T, int l, int r) { // return atMost(T,r)-atMost(T,l-1); } // fbo(k) returns iterator to kth element starting from 0; // ook(k) returns count of elements strictly smaller than k; template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } // primitive root for FFT int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int mint() { v = 0; } mint(ll _v) { v = (-MOD < _v && _v < MOD) ? _v : _v % MOD; if (v < 0) v += MOD; } friend bool operator==(const mint& a, const mint& b) { return a.v == b.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } friend void re(mint& a) { ll x; re(x); a = mint(x); } friend string ts(mint a) { return ts(a.v); } mint& operator+=(const mint& m) { if ((v += m.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& m) { if ((v -= m.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& m) { v = (ll)v*m.v%MOD; return *this; } mint& operator/=(const mint& m) { return (*this) *= inv(m); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a,MOD-2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } }; typedef mint<MOD,5> mi; // 5 is primitive root for both common mods typedef vector<mi> vmi; typedef pair<mi,mi> pmi; typedef vector<pmi> vpmi; vector<vmi> scmb; // small combinations void genComb(int SZ) { scmb.assign(SZ,vmi(SZ)); scmb[0][0] = 1; FOR(i,1,SZ) F0R(j,i+1) scmb[i][j] = scmb[i-1][j]+(j?scmb[i-1][j-1]:0); } vi invs, fac, ifac; // make sure to convert to LL before doing any multiplications ... void genFac(int SZ) { invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ); invs[1] = fac[0] = ifac[0] = 1; FOR(i,2,SZ) invs[i] = MOD-(ll)MOD/i*invs[MOD%i]%MOD; FOR(i,1,SZ) { fac[i] = (ll)fac[i-1]*i%MOD; ifac[i] = (ll)ifac[i-1]*invs[i]%MOD; } } mi comb(int a, int b) { if (a < b || b < 0) return 0; return (ll)fac[a]*ifac[b]%MOD*ifac[a-b]%MOD; } ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} ll lcm(ll a,ll b) { return (a / gcd(a, b)) * b;} ll ncr(ll n, ll r){ ll p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r) { p *= n; k *= r; ll m = gcd(p, k); p /= m; k /= m; n--; r--; } } else p = 1; return p;} // DEBUG void DBG() { cerr << "]" << endl; } tcTUU> void DBG(const T& t, const U&... u) { cerr << ts(t); if (sizeof...(u)) cerr << ", "; DBG(u...); } #ifdef __APPLE__ // chk -> fake assert #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); #else #define dbg(...) 0 #define chk(...) 0 #endif V<vi> readGraph(int n,int m){ V<vi>g(n); F0R(i,m){ int a,b; re(a,b); a--;b--; g[a].pb(b); g[b].pb(a); } return g; } V<vi> readTree(int n){ return readGraph(n,n-1); } void setPrec() { cout << fixed << setprecision(15); } void setIn(str s) { freopen(s.c_str(),"r",stdin); } void setOut(str s) { freopen(s.c_str(),"w",stdout); } void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } void setIO(str s = "") { unsyncIO(); setPrec(); // cin.exceptions(cin.failbit); // throws exception when do smth illegal // ex. try to read letter into int if (sz(s)) { setIn(s+".in");setOut(s+".out"); } // for USACO } const int mxN = 1e5 + 5; void solve(){ int n; re(n); ll ans=0; map<pi,int>m; F0R(i,n){ int x,y,u,v; re(x,y,u,v); int a=u-x,b=v-y,g=gcd(abs(a),abs(b)); a/=g; b/=g; pi z={a,b}; ans+=m[z]; m[-z]++; } ps(ans); } int main() { setIO(); int T = 1; re(T); FOR(_,1,T+1) { //pr("Case #",_,": "); solve(); } // you should actually read the stuff at the bottom } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */
4
#include <bits/stdc++.h> using namespace std; bool Check(long long suml, long long sumr, long long fl, long long fr) { long long M = suml + fl * 9 - sumr; long long m = suml - (sumr + fr * 9); if ((M + m) / 2 == 0) return true; else return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; string s; cin >> s; long long fl = 0; long long fr = 0; long long suml = 0; long long sumr = 0; for (long i = 0; i < n / 2; i++) { if (s[i] == '?') fl += 1; else suml += s[i] - '0'; } for (long i = n / 2; i < n; i++) { if (s[i] == '?') fr += 1; else sumr += s[i] - '0'; } if (Check(suml, sumr, fl, fr)) cout << "Bicarp"; else cout << "Monocarp"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int o, e, t, n, temp, p; cin >> t; for (int i = 0; i < t; i++) { cin >> n; p = 0; e = 0; o = 0; for (int j = 0; j < n; j++) { cin >> temp; if (p) { if (!(temp % 2)) { e++; } } else { if (temp % 2) { o++; } } p = !p; } if (e == o) { cout << e << "\n"; } else cout << -1 << "\n"; } }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, x, y, s; cin >> n >> x >> y; s = sqrt(x); n--; if (n + s <= y && pow(s, 2) + n >= x) { cout << s << endl; for (int i = 1; i <= n; i++) cout << "1" << endl; } else if (n + s + 1 <= y && pow(s + 1, 2) + n >= x) { cout << s + 1 << endl; for (int i = 1; i <= n; i++) cout << "1" << endl; } else cout << "-1"; return 0; }
3
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:36777216") template <class T> inline void RD(T &); template <class T> inline void OT(const T &); inline long long RD() { long long x; RD(x); return x; } template <class T> inline T &_RD(T &x) { RD(x); return x; } inline void RC(char &c) { scanf(" %c", &c); } inline char RC() { char c; RC(c); return c; } inline char _RC(char &c) { RC(c); return c; } inline void RF(double &x) { scanf("%lf", &x); }; inline double RF() { double x; RF(x); return x; } inline double _RF(double &x) { RD(x); return x; } inline void RS(char *s) { scanf("%s", s); } inline char *_RS(char *s) { scanf("%s", s); return s; } template <class T0, class T1> inline void RD(T0 &x0, T1 &x1) { RD(x0), RD(x1); } template <class T0, class T1, class T2> inline void RD(T0 &x0, T1 &x1, T2 &x2) { RD(x0), RD(x1), RD(x2); } template <class T0, class T1, class T2, class T3> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { RD(x0), RD(x1), RD(x2), RD(x3); } template <class T0, class T1, class T2, class T3, class T4> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); } template <class T0, class T1> inline void OT(T0 &x0, T1 &x1) { OT(x0), OT(x1); } template <class T0, class T1, class T2> inline void OT(T0 &x0, T1 &x1, T2 &x2) { OT(x0), OT(x1), OT(x2); } template <class T0, class T1, class T2, class T3> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3) { OT(x0), OT(x1), OT(x2), OT(x3); } template <class T0, class T1, class T2, class T3, class T4> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6) { OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6); } inline void RF(double &a, double &b) { RF(a), RF(b); } inline void RF(double &a, double &b, double &c) { RF(a), RF(b), RF(c); } inline void RS(char *s1, char *s2) { RS(s1), RS(s2); } inline void RS(char *s1, char *s2, char *s3) { RS(s1), RS(s2), RS(s3); } template <class T> inline void RST(T &A) { memset(A, 0, sizeof(A)); } template <class T0, class T1> inline void RST(T0 &A0, T1 &A1) { RST(A0), RST(A1); } template <class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2) { RST(A0), RST(A1), RST(A2); } template <class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { RST(A0), RST(A1), RST(A2), RST(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6); } template <class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q) { while (!Q.empty()) Q.pop(); } template <class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q) { while (!Q.empty()) Q.pop(); } template <class T> inline void CLR(T &A) { A.clear(); } template <class T0, class T1> inline void CLR(T0 &A0, T1 &A1) { CLR(A0), CLR(A1); } template <class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2) { CLR(A0), CLR(A1), CLR(A2); } template <class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { CLR(A0), CLR(A1), CLR(A2), CLR(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6); } template <class T> inline void CLR(T &A, int n) { for (int i = 0; i < int(n); ++i) CLR(A[i]); } template <class T> inline void FLC(T &A, int x) { memset(A, x, sizeof(A)); } template <class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x) { FLC(A0, x), FLC(A1, x); } template <class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2) { FLC(A0), FLC(A1), FLC(A2); } template <class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3) { FLC(A0), FLC(A1), FLC(A2), FLC(A3); } template <class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4) { FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4); } template <class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5) { FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5); } template <class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6) { FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6); } template <class T> inline void SRT(T &A) { sort(A.begin(), A.end()); } template <class T, class C> inline void SRT(T &A, C B) { sort(A.begin(), A.end(), B); } const int MOD = 1000000007; const int INF = 0x3f3f3f3f; const double EPS = 1e-9; const double OO = 1e15; const double PI = acos(-1.0); template <class T> inline void checkMin(T &a, const T b) { if (b < a) a = b; } template <class T> inline void checkMax(T &a, const T b) { if (b > a) a = b; } template <class T, class C> inline void checkMin(T &a, const T b, C c) { if (c(b, a)) a = b; } template <class T, class C> inline void checkMax(T &a, const T b, C c) { if (c(a, b)) a = b; } template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T min(T a, T b, T c, T d) { return min(min(a, b), min(c, d)); } template <class T> inline T max(T a, T b, T c, T d) { return max(max(a, b), max(c, d)); } template <class T> inline T sqr(T a) { return a * a; } template <class T> inline T cub(T a) { return a * a * a; } int Ceil(int x, int y) { return (x - 1) / y + 1; } inline bool _1(int x, int i) { return x & 1 << i; } inline bool _1(long long x, int i) { return x & 1LL << i; } inline long long _1(int i) { return 1LL << i; } inline long long _U(int i) { return _1(i) - 1; }; template <class T> inline T low_bit(T x) { return x & -x; } template <class T> inline T high_bit(T x) { T p = low_bit(x); while (p != x) x -= p, p = low_bit(x); return p; } template <class T> inline T cover_bit(T x) { T p = 1; while (p < x) p <<= 1; return p; } inline int count_bits(int x) { x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1); x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2); x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4); x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8); x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16); return x; } inline int count_bits(long long x) { x = (x & 0x5555555555555555LL) + ((x & 0xaaaaaaaaaaaaaaaaLL) >> 1); x = (x & 0x3333333333333333LL) + ((x & 0xccccccccccccccccLL) >> 2); x = (x & 0x0f0f0f0f0f0f0f0fLL) + ((x & 0xf0f0f0f0f0f0f0f0LL) >> 4); x = (x & 0x00ff00ff00ff00ffLL) + ((x & 0xff00ff00ff00ff00LL) >> 8); x = (x & 0x0000ffff0000ffffLL) + ((x & 0xffff0000ffff0000LL) >> 16); x = (x & 0x00000000ffffffffLL) + ((x & 0xffffffff00000000LL) >> 32); return x; } int reverse_bits(int x) { x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa); x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc); x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0); x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00); x = ((x >> 16) & 0x0000ffff) | ((x << 16) & 0xffff0000); return x; } long long reverse_bits(long long x) { x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL); x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL); x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL); x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL); x = ((x >> 16) & 0x0000ffff0000ffffLL) | ((x << 16) & 0xffff0000ffff0000LL); x = ((x >> 32) & 0x00000000ffffffffLL) | ((x << 32) & 0xffffffff00000000LL); return x; } inline void INC(int &a, int b) { a += b; if (a >= MOD) a -= MOD; } inline int sum(int a, int b) { a += b; if (a >= MOD) a -= MOD; return a; } inline void DEC(int &a, int b) { a -= b; if (a < 0) a += MOD; } inline int dff(int a, int b) { a -= b; if (a < 0) a += MOD; return a; } inline void MUL(int &a, int b) { a = (long long)a * b % MOD; } inline int pdt(int a, int b) { return (long long)a * b % MOD; } inline int sum(int a, int b, int c) { return sum(sum(a, b), c); } inline int sum(int a, int b, int c, int d) { return sum(sum(a, b), sum(c, d)); } inline int pow(int a, long long b) { int c(1); while (b) { if (b & 1) MUL(c, a); MUL(a, a), b >>= 1; } return c; } template <class T> inline T pow(T a, long long b) { T c(1); while (b) { if (b & 1) c *= a; a *= a, b >>= 1; } return c; } inline int _I(int b) { int a = MOD, x1 = 0, x2 = 1, q; while (true) { q = a / b, a %= b; if (!a) return (x2 + MOD) % MOD; DEC(x1, pdt(q, x2)); q = b / a, b %= a; if (!b) return (x1 + MOD) % MOD; DEC(x2, pdt(q, x1)); } } inline void DIA(int &a, int b) { MUL(a, _I(b)); } inline int qtt(int a, int b) { return pdt(a, _I(b)); } inline int phi(int n) { int res = n; for (int i = 2; sqr(i) <= n; ++i) if (!(n % i)) { DEC(res, qtt(res, i)); do { n /= i; } while (!(n % i)); } if (n != 1) DEC(res, qtt(res, n)); return res; } inline int rand32() { return (bool(rand() & 1) << 30) | (rand() << 15) + rand(); } inline int random32(int l, int r) { return rand32() % (r - l + 1) + l; } inline int random(int l, int r) { return rand() % (r - l + 1) + l; } int dice() { return rand() % 6; } bool coin() { return rand() % 2; } template <class T> inline void RD(T &x) { scanf("%d", &x); } int ____Case; template <class T> inline void OT(const T &x) { printf("%I64d ", x); } const int N = 100009; long long a[N], z[N], s[N]; int n, q, x; int main() { for (int n____ = int(_RD(n)), i = 0; i < n____; ++i) RD(a[i]); sort(a, a + n, greater<int>()); for (int i = 1; i <= int(n); ++i) s[i] = s[i - 1] + a[i]; for (int i = 1; i * i <= n; i++) { int c = 0, t = 0, w = 1; for (int j = 0; j < n; j++) { z[i] += a[j] * t; c++; if (c == w) { c = 0; w *= i; t++; } } } int T____; RD(T____); while (T____--) { RD(x); if (x >= n) x = n - 1; if ((long long)x * x <= n) OT(z[x]); else OT(s[n - 1] * 2 - s[x]); } }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 11; long long ten[maxn]; void init() { ten[0] = 1; for (int i = 1; i < maxn; i++) ten[i] = ten[i - 1] * 10; } int getlen(long long x) { int res = 0; while (x) { x /= 10; res++; } if (res == 0) res = 1; return res; } long long cnt_F(long long x) { if (x == 0) return 0; int len = getlen(x); long long tmp = x - (ten[len - 1] - 1); long long ans = cnt_F(ten[len - 1] - 1) + len * tmp; return ans; } long long lower_F(long long x) { long long l = 1, r = x, res = 0; while (l <= r) { long long mid = (l + r) >> 1; long long t = cnt_F(mid); if (t <= x) l = mid + 1, res = mid; else r = mid - 1; } return res; } long long sum(long long l, long long r) { long long ans = (l + r) % 2 == 0 ? (l + r) / 2 * (r - l + 1) : (r - l + 1) / 2 * (l + r); return ans; } long long cnt_G(long long x) { if (x == 0) return 0; int len = getlen(x); long long tmp = x - (ten[len - 1] - 1); long long ini = cnt_F(ten[len - 1] - 1); long long ans = cnt_G(ten[len - 1] - 1) + tmp * ini + sum(1, tmp) * len; return ans; } long long lower_G(long long x) { long long l = 1, r = min(x, 500000000ll), res; while (l <= r) { long long mid = (l + r) >> 1; long long t = cnt_G(mid); if (t <= x) res = mid, l = mid + 1; else r = mid - 1; } return res; } int main() { std::ios::sync_with_stdio(false); init(); int q; cin >> q; while (q--) { long long x; cin >> x; long long fir = lower_G(x); long long hav = cnt_G(fir); if (hav == x) { cout << fir % 10 << "\n"; continue; } x -= hav; fir = lower_F(x); hav = cnt_F(fir); if (hav == x) { cout << fir % 10 << "\n"; continue; } x -= hav; fir++; int b[20], k = 0; while (fir) { b[++k] = fir % 10; fir /= 10; } cout << b[k - x + 1] << "\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; if (n == 1) cout << "1"; else if (n == 2) cout << "2"; else if (n == 3) cout << "6"; else if (n % 2 == 1) { cout << n * (n - 1) * (n - 2); } else { if (n % 3 == 0) cout << (n - 1) * (n - 2) * (n - 3); else cout << n * (n - 1) * (n - 3); } }
4
#include <bits/stdc++.h> using namespace std; bool check1(long long start, long long a[], long long w, long long n) { if (start > w) return false; for (long long i = 0; i < n; i++) { start = start + a[i]; if (start > w) return false; } return true; } bool check2(long long start, long long a[], long long n) { for (long long i = 0; i < n; i++) { start = start + a[i]; if (start < 0) return false; } return true; } inline void solve() { long long n, w; cin >> n >> w; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; long long maxi, mini; long long low = 0, high = 1000000000000000000LL; long long mid; while (low < high) { mid = low + (high - low) / 2; if (!check1(mid, a, w, n)) high = mid; else low = mid + 1; } maxi = low - 1; low = 0, high = 1000000000000000000LL; while (low < high) { mid = low + (high - low) / 2; if (check2(mid, a, n)) high = mid; else low = mid + 1; } mini = low; if ((maxi - mini + 1) < 0) cout << 0; else cout << maxi - mini + 1; } int main() { ios::sync_with_stdio(0); cin.tie(0); ; long long t = 1; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const double pi = acos(0.0) * 2.0; const long double eps = 1e-10; const int step[8][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, 1}, {1, 1}, {1, -1}, {-1, -1}}; template <class T> inline T abs1(T a) { return a < 0 ? -a : a; } template <typename t, typename t1> t min1(t a, t1 b) { return a < b ? a : b; } template <typename t, typename... arg> t min1(t a, arg... arr) { return min1(a, min1(arr...)); } template <typename t, typename t1> t max1(t a, t1 b) { return a > b ? a : b; } template <typename t, typename... arg> t max1(t a, arg... arr) { return max1(a, max1(arr...)); } inline int jud(double a, double b) { if (abs(a) < eps && abs(b) < eps) return 0; else if (abs1(a - b) / max(abs1(a), abs1(b)) < eps) return 0; if (a < b) return -1; return 1; } template <typename t> inline int jud(t a, t b) { if (a < b) return -1; if (a == b) return 0; return 1; } template <typename it, typename t1> inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) { if (na == 0) return 0; int be = 0, en = na - 1; if (*a <= *(a + na - 1)) { if (f_lb == 0) while (be < en) { int mid = (be + en + 1) / 2; if (jud(*(a + mid), val) != 1) be = mid; else en = mid - 1; } else while (be < en) { int mid = (be + en) / 2; if (jud(*(a + mid), val) != -1) en = mid; else be = mid + 1; } if (f_small && jud(*(a + be), val) == 1) be--; if (!f_small && jud(*(a + be), val) == -1) be++; } else { if (f_lb) while (be < en) { int mid = (be + en + 1) / 2; if (jud(*(a + mid), val) != -1) be = mid; else en = mid - 1; } else while (be < en) { int mid = (be + en) / 2; if (jud(*(a + mid), val) != 1) en = mid; else be = mid + 1; } if (!f_small && jud(*(a + be), val) == -1) be--; if (f_small && jud(*(a + be), val) == 1) be++; } return be; } template <class T> inline T lowb(T num) { return num & (-num); } inline int bitnum(unsigned int nValue) { return __builtin_popcount(nValue); } inline int bitnum(int nValue) { return __builtin_popcount(nValue); } inline int bitnum(unsigned long long nValue) { return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32); } inline int bitnum(long long nValue) { return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32); } inline int bitmaxl(unsigned int a) { if (a == 0) return 0; return 32 - __builtin_clz(a); } inline int bitmaxl(int a) { if (a == 0) return 0; return 32 - __builtin_clz(a); } inline int bitmaxl(unsigned long long a) { int temp = a >> 32; if (temp) return 32 - __builtin_clz(temp) + 32; return bitmaxl(int(a)); } inline int bitmaxl(long long a) { int temp = a >> 32; if (temp) return 32 - __builtin_clz(temp) + 32; return bitmaxl(int(a)); } long long pow(long long n, long long m, long long mod = 0) { if (m < 0) return 0; long long ans = 1; long long k = n; while (m) { if (m & 1) { ans *= k; if (mod) ans %= mod; } k *= k; if (mod) k %= mod; m >>= 1; } return ans; } template <class t1, class t2> inline void add(t1 &a, t2 b, int mod = -1) { if (mod == -1) mod = 1000000007; a += b; while (a >= mod) a -= mod; while (a < 0) a += mod; } template <class t> void output1(t arr) { for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' '; cerr << endl; } template <class t> void output2(t arr) { for (int i = 0; i < (int)arr.size(); i++) output1(arr[i]); } const int maxn = 505000; bool flag = 0; int dp[maxn], nxtdp[maxn][3], tnxt[maxn][3]; int n, m; struct link { int nxt, bef; bool exist; } e[maxn * 2]; int head[maxn]; struct edge { int be, en, a, b; bool operator<(const edge &re) const { if (be == re.be) { return en < re.en; } return be < re.be; } } alle[maxn]; priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >, greater<pair<int, pair<int, int> > > > pq; int findnxt(int no, int t, int cate) { for (int rit = head[no]; rit != -1; rit = e[rit].nxt) { int it = rit / 2; if (alle[it].be <= t) { t = max(alle[it].en - (alle[it].en % 2 != 2 - cate) * (cate != 3), t); } else break; } return t; } void addedge(int no, int i) { e[i].bef = -1; e[i].exist = 1; e[i].nxt = head[no]; if (head[no] != -1) { e[head[no]].bef = i; } head[no] = i; e[i].exist = 1; } void deledge(int i) { if (e[i].bef != -1) { e[e[i].bef].nxt = e[i].nxt; } if (e[i].nxt != -1) { e[e[i].nxt].bef = e[i].bef; } e[i].exist = 0; } int getrcate(int cate) { if (cate == 3) return 3; else if (cate == 2) return 1; else if (cate == 1) return 2; else return 0; } void updrcate(int rcate, int nxtno, int t, int add) { if (((rcate | dp[nxtno]) ^ dp[nxtno]) >= 3) { cout << dp[nxtno] << ' ' << rcate << endl; exit(0); } if ((dp[nxtno] | rcate) > (dp[nxtno] | nxtdp[nxtno][add])) { if (((rcate | dp[nxtno]) ^ dp[nxtno]) != t % 2 + 1) { cout << dp[nxtno] << ' ' << rcate << ' ' << t; exit(0); } pq.push(make_pair(t, make_pair(4 + rcate, nxtno))); nxtdp[nxtno][add] = rcate; pq.push(make_pair(t + 1, make_pair(0, nxtno))); } } void add(int no1, int eno, int no, int t) { int more = getrcate(dp[no1]) | dp[no]; more ^= dp[no]; if ((more & (t % 2 + 1)) && alle[eno].en > t) updrcate(more & (t % 2 + 1), no, t, 2 - t % 2); if ((more & (2 - t % 2)) && alle[eno].en > t + 1 && tnxt[no1][more & (2 - t % 2)] > t) updrcate(more & (2 - t % 2), no, t + 1, t % 2 + 1); } int main() { ios_base::sync_with_stdio(0); scanf("%d%d", &n, &m); if (n == 1) { cout << 0 << endl; return 0; } for (int i = 0; i < m; i++) { int a, b, l, r; scanf("%d%d%d%d", &a, &b, &l, &r); a--; b--; alle[i].a = a; alle[i].b = b; alle[i].be = l; alle[i].en = r; } auto ju = [&]() { return n == 100000 && m == 149999; }; flag = ju(); sort(alle, alle + m); memset(head, -1, sizeof(head)); for (int i = m - 1; i >= 0; i--) { addedge(alle[i].a, i * 2); addedge(alle[i].b, i * 2 + 1); } dp[0] = 2; pq.push(make_pair(findnxt(0, 0, 2), make_pair(3, 0))); tnxt[0][2] = findnxt(0, 0, 2); for (int i = 0; i < m; i++) { pq.push(make_pair(alle[i].be, make_pair(-1, i))); pq.push(make_pair(alle[i].en, make_pair(1, i))); } if (pq.size() && pq.top().first != 0) { puts("-1"); return 0; } for (; pq.size();) { int t = pq.top().first, cate = pq.top().second.first, no = pq.top().second.second; pq.pop(); if (no == n - 1 && cate < 2 && cate != -1 && cate != 1) { printf("%d\n", t); return 0; } if (cate > 4) { dp[no] |= nxtdp[no][2 - t % 2]; nxtdp[no][2 - t % 2] = 0; } else if (cate > 1) { if (!(dp[no] & (cate - 1))) { cout << t << ' ' << no << ' ' << dp[no] << ' ' << cate - 1 << endl; tnxt[no][cate - 1]; exit(0); } dp[no] &= 3 ^ (cate - 1); } else if (cate == 1) { deledge(no * 2); deledge(no * 2 + 1); } else if (cate == 0) { if (dp[no] != 3 && 2 - dp[no] != t % 2) { pq.push(make_pair(t + 1, make_pair(0, no))); continue; } assert(dp[no] & (2 - (t % 2))); int rt = findnxt(no, t, (2 - (t % 2))); tnxt[no][2 - t % 2] = rt; pq.push(make_pair(rt, make_pair(1 + (2 - t % 2), no))); for (int rit = head[no]; rit != -1; rit = e[rit].nxt) { int it = rit / 2; if (alle[it].be <= t) { int nxtno = alle[it].a + alle[it].b - no; add(no, it, nxtno, t); } else break; } } else { int a = alle[no].a, b = alle[no].b; if (dp[a] < dp[b]) swap(a, b); if ((dp[a] == 2 && dp[b] == 1) || (dp[a] == 1 && dp[b] == 2) || (dp[a] == 3 && dp[b] == 3) || (dp[a] == 0 && dp[b] == 0)) { continue; } if ((t % 2 + 1 == dp[a] || dp[a] == 0) && (t % 2 + 1 == dp[b] || dp[b] == 0)) { if (alle[no].en > t) pq.push(make_pair(t + 1, make_pair(-1, no))); continue; } add(a, no, b, t); add(b, no, a, t); } } printf("-1\n"); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; pair<int, int> b[N]; int a[N]; int n, m; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; b[i] = make_pair(a[i], i); } sort(b + 1, b + 1 + n); cin >> m; while (m--) { int k, pos; cin >> k >> pos; multiset<int> arr; for (int i = n; i > n - k; i--) { arr.insert(b[i].first); } vector<int> ans; int pt = 0; for (int i = 1; i <= n; i++) { if (arr.find(a[i]) != arr.end()) { ans.push_back(a[i]); arr.erase(arr.lower_bound(a[i])); } if (0 == (int)arr.size()) break; } cout << ans[pos - 1] << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<vector<int>> G, H; vector<int> ord, comp; vector<bool> V; struct SCC { int n, c; SCC(int n) : n(n) { G.resize(n); H.resize(n); for (int i = 0; i < n; i++) { G[i].reserve(n); H[i].reserve(n); G[i].clear(); H[i].clear(); } ord.reserve(n); ord.clear(); }; void add_edge(int u, int v) { G[u].push_back(v); H[v].push_back(u); } void dfs1(int v) { V[v] = true; for (auto& u : G[v]) if (!V[u]) dfs1(u); ord.push_back(v); } void dfs2(int v) { comp[v] = c; for (auto& u : H[v]) if (comp[u] == -1) dfs2(u); } vector<int> scc() { V.assign(n, 0); for (int i = 0; i < n; i++) if (!V[i]) dfs1(i); comp.assign(n, -1); c = 0; for (int i = 0; i < n; i++) { int v = ord[n - 1 - i]; if (comp[v] == -1) dfs2(v), c++; } return comp; } vector<vector<int>> dag() { set<pair<int, int>> S; vector<vector<int>> dag(c); for (int a = 0; a < n; a++) { for (auto& b : G[a]) { if (comp[a] == comp[b]) continue; if (!S.count({comp[a], comp[b]})) { dag[comp[a]].push_back(comp[b]); S.insert({comp[a], comp[b]}); } } } return dag; } }; int VAR(int i) { return 2 * i; } int NOT(int i) { return i ^ 1; } struct SAT { int n; SCC scc; SAT(int n) : n(n), scc(2 * n) {} void add_or(int a, int b) { if (a == NOT(b)) return; scc.add_edge(NOT(a), b); scc.add_edge(NOT(b), a); } void add_true(int a) { add_or(a, a); } void add_false(int a) { add_or(NOT(a), NOT(a)); } void add_xor(int a, int b) { add_or(a, b); add_or(NOT(a), NOT(b)); } void add_imply(int a, int b) { add_or(NOT(a), b); } bool solve() { auto comp = scc.scc(); for (int i = 0; i < 2 * n; i += 2) { if (comp[i] == comp[i + 1]) return false; } return true; } }; struct Rule { int pos1, pos2; char t1, t2; void read() { cin >> pos1 >> t1 >> pos2 >> t2; pos1 = VAR(pos1 - 1); pos2 = VAR(pos2 - 1); if (t1 == 'C') pos1 = NOT(pos1); if (t2 == 'C') pos2 = NOT(pos2); } }; string alpha, s; int l, n, m; bool vowel[256]; Rule rule[4 * 200 * 199]; char next_vowel[256], next_cons[256]; char min_v, min_c; bool check_str(int pl, bool next_greater = true) { SAT sat(2 * n); if (!min_c) { for (int i = 0; i < n; i++) { sat.add_true(VAR(i)); } } else if (!min_v) { for (int i = 0; i < n; i++) { sat.add_false(VAR(i)); } } for (int i = 0; i < m; i++) { sat.add_imply(rule[i].pos1, rule[i].pos2); } for (int i = 0; i < pl; i++) { if (vowel[s[i]]) { sat.add_true(VAR(i)); } else { sat.add_false(VAR(i)); } } if (pl < n && next_greater) { char ch = s[pl]; if (!next_vowel[ch] && !next_cons[ch]) { return false; } if (next_vowel[ch] && !next_cons[ch]) { sat.add_true(VAR(pl)); } if (!next_vowel[ch] && next_cons[ch]) { sat.add_false(VAR(pl)); } } return sat.solve(); } string build_soln(int pl) { if (pl >= n) return s; string old_s = s; char ch = 127; char old = s[pl]; s = old_s; for (int i = pl + 1; i < n; i++) { s[i] = 'a'; } if (next_vowel[old]) { s[pl] = next_vowel[old]; if (check_str(pl + 1, false)) { ch = min(ch, s[pl]); } } if (next_cons[old]) { s[pl] = next_cons[old]; if (check_str(pl + 1, false)) { ch = min(ch, s[pl]); } } old_s[pl] = ch; for (int i = pl + 1; i < n; i++) { s = old_s; for (int j = i + 1; j < n; j++) { s[j] = 'a'; } ch = 127; if (min_v) { s[i] = min_v; if (check_str(i + 1, false)) { ch = min(ch, s[i]); } } if (min_c) { s[i] = min_c; if (check_str(i + 1, false)) { ch = min(ch, s[i]); } } old_s[i] = ch; } return old_s; } int main() { cin >> alpha; l = alpha.length(); for (int i = 0; i < l; i++) { vowel[i + 'a'] = alpha[i] == 'V'; } min_v = min_c = 0; for (int i = 'a' + l - 1; i >= 'a'; i--) { next_vowel[i] = min_v; next_cons[i] = min_c; if (vowel[i]) { min_v = i; } else { min_c = i; } } cin >> n >> m; for (int i = 0; i < m; i++) { rule[i].read(); } cin >> s; int prefix_len = n; while (prefix_len >= 0 && !check_str(prefix_len)) { prefix_len--; } if (prefix_len < 0) { cout << -1 << endl; return 0; } cout << build_soln(prefix_len) << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; template <typename F, typename S> ostream &operator<<(ostream &os, const pair<F, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << "{"; typename vector<T>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } return os << "}"; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << "["; typename set<T>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } return os << "]"; } template <typename F, typename S> ostream &operator<<(ostream &os, const map<F, S> &v) { os << "["; typename map<F, S>::const_iterator it; for (it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << it->first << " = " << it->second; } return os << "]"; } const long long int mod = 1000000007; const long long int inf = (long long int)1e15; long long int power(long long int a, long long int b, long long int m = mod) { if (b == 0) return 1; if (b == 1) return (a % m); long long int x = power(a, b / 2, m); x = (x * x) % m; if (b % 2) x = (x * a) % m; return x; } long long int max(long long int a, long long int b) { return (a > b ? a : b); } long long int min(long long int a, long long int b) { return (a < b ? a : b); } const int N = 3e5 + 5; int n; string s; int a[N]; int f[4]; int tot; void balance(int torem, int toput) { if (torem > toput) { for (int i = 0; i < s.size() && f[toput] < tot && f[torem] > tot; i++) { if (a[i] == torem) { f[toput]++; f[torem]--; a[i] = toput; } } } else { for (int i = s.size() - 1; i >= 0 && f[toput] < tot && f[torem] > tot; i--) { if (a[i] == torem) { f[toput]++; f[torem]--; a[i] = toput; } } } } void solve() { cin >> n >> s; tot = n / 3; for (int i = 0; i < s.size(); i++) { a[i] = s[i] - '0'; f[s[i] - '0']++; } if (f[0] > tot && f[1] > tot) { balance(1, 2); balance(0, 2); } if (f[0] > tot && f[2] > tot) { balance(2, 1); balance(0, 1); } if (f[1] > tot && f[2] > tot) { balance(2, 0); balance(1, 0); } if (f[0] > tot) { balance(0, 2); balance(0, 1); } if (f[1] > tot) { balance(1, 0); balance(1, 2); } if (f[2] > tot) { balance(2, 0); balance(2, 1); } for (int i = 0; i < n; i++) { cout << a[i]; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t = 1, num = 1; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const int oo = 0x3f3f3f3f; const long long OO = 0x3f3f3f3f3f3f3f3f; const int maxn = 2000100; char s[maxn + 5]; int N; vector<int> chd[maxn + 5]; int ty[maxn + 5]; int id[maxn + 5]; long long f[maxn + 5]; long long ans[maxn + 5]; void calc(int first) { if (ty[first] == 2) { f[first] = 1; return; } if (ty[first] == 0) f[first] = OO; else f[first] = 0; for (auto second : chd[first]) { calc(second); if (ty[first] == 0) chkmin(f[first], f[second]); else f[first] += f[second]; } } void dfs(int first, long long w) { if (ty[first] == 2) { ans[id[first]] = w; return; } if (ty[first] == 0) { int Miny = -1; for (auto second : chd[first]) if (!~Miny || f[second] < f[Miny]) Miny = second; dfs(Miny, w); } else { for (auto second : chd[first]) dfs(second, w); } } int main() { int T; scanf("%d", &T); while (T--) { int r; scanf("%d", &r); char c; int n = 0; while (isprint(c = getchar())) if (c != ' ') s[n++] = c; N = 0; vector<int> stk; ty[N] = 0; chd[N].clear(); stk.push_back(N++); int m = 0; for (int i = (0), i_end_ = (n); i < i_end_; ++i) { if (s[i] == '(') { ty[N] = 0; chd[N].clear(); chd[stk.back()].push_back(N); stk.push_back(N++); } else if (s[i] == '*') { ty[N] = 2; id[N] = m++; chd[N].clear(); chd[stk.back()].push_back(N); N++; } else if (s[i] == ')') stk.pop_back(); else if (s[i] == 'S') ty[stk.back()] = 0; else if (s[i] == 'P') ty[stk.back()] = 1; } calc(0); for (int i = (0), i_end_ = (m); i < i_end_; ++i) ans[i] = 0; dfs(0, r * f[0]); printf("REVOLTING "); for (int i = (0), i_end_ = (m); i < i_end_; ++i) printf("%lld ", ans[i]); printf("\n"); } return 0; }
10
#include <bits/stdc++.h> using namespace std; const int N = 1e2 + 20; const int oo = 1e9 + 20; const int MOD = 1e9 + 9; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { long long n; cin >> n; long long three = 2; while (three * (three - 1) / 2 <= n) { three++; } three--; long long rem = n - three * (three - 1) / 2; string ans = "1" + string(three - 2, '3') + string(rem, '1') + "33" + "7"; assert(ans.size() <= 1e5); cout << ans << '\n'; } }
5
#include <bits/stdc++.h> using namespace std; int dadsadasda; const int INF = 0x3f3f3f3f; const long long LLINF = 1e18; const int MAXN = 5010; int p, n, m; int cnt[MAXN], pre[MAXN]; int tmp; int main() { dadsadasda = scanf("%d %d", &n, &p); for (int i = 0; i < n; i++) { dadsadasda = scanf("%d", &tmp); cnt[tmp]++; m = max(m, tmp); } for (int i = 1; i < m + 1; i++) { pre[i] = cnt[i] + pre[i - 1]; } vector<int> ans; for (int k = max(m - n + 1, 1); k < m; k++) { long long ansa = 1; long long v = pre[k - 1]; for (int i = k; i < k + n; i++) { v = v + cnt[i]; ansa *= v; v--; v %= p; ansa %= p; } if (ansa % p != 0) ans.push_back(k); } printf("%d\n", (int)ans.size()); for (auto x : ans) printf("%d ", x); printf("\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n; int lcs(string s, string t) { int L[n + 1][n + 1]; int i, j; for (i = 0; i < n + 1; i++) for (j = 0; j < n + 1; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (s[i - 1] == t[j - 1]) L[i][j] = L[i - 1][j - 1] + 1; else L[i][j] = max(L[i - 1][j], L[i][j - 1]); } return L[n][n]; } void boost() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int main() { boost(); long long n, m, i, j, k, l; cin >> n; long long a[n]; for (i = 0; i < n; i++) cin >> a[i]; long long mx = 0; for (i = 0; i < n; i++) mx = max(mx, a[i]); long long sum = 0; for (i = 0; i < n; i++) sum += mx - a[i]; if (sum >= mx) { cout << mx; return 0; } long long temp, dif = mx - sum; long long extra = 0; while (1) { temp = (dif + extra + n - 1) / n; if (temp > extra) extra = temp; else break; } cout << extra + mx; }
4
#include <bits/stdc++.h> using namespace std; int ind[100005], dp = -1, flag1; vector<int> g[100005]; map<int, int> mp; void dfs(int u, int dep, int fa) { for (int i = 0; i < g[u].size(); i++) { int j = g[u][i]; if (j == fa) continue; dfs(j, dep + 1, u); } if (g[u].size() == 1) { mp[fa]++; if (dp == -1) { if (dep % 2) dp = 1; else dp = 0; } if (dp != -1) { if ((dep % 2) && !dp) flag1 = 1; else if (((dep % 2) == 0) && (dp == 1)) flag1 = 1; } } } signed main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 1, a, b; i < n; i++) { cin >> a >> b; g[a].push_back(b); g[b].push_back(a); ind[a]++; ind[b]++; } int rt; for (int i = 1; i <= n; i++) if (ind[i] != 1) { rt = i; break; } dfs(rt, 0, -1); int res = n - 1; for (int i = 1; i <= n; i++) if (mp[i]) res -= (mp[i] - 1); cout << (flag1 == 1 ? 3 : 1) << " " << res << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 222; int ori[maxn]; int len[maxn]; int getlen(int x) { int res = 0; while (x) { x /= 2; res++; } return res; } vector<pair<int, int> > ans; vector<int> savelen[maxn]; void solve() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &ori[i]); len[i] = getlen(ori[i]); savelen[len[i]].push_back(i); } for (int len = 1; len <= 30; len++) { int mx = 1 << len; mx--; for (int pos : savelen[len]) { int sum = 0; for (int p = pos - 2; p >= 1; p--) { sum += ori[p + 1]; if ((ori[p] ^ ori[pos]) == sum) ans.push_back(make_pair(p, pos)); if (sum > mx) break; } sum = 0; for (int p = pos + 2; p <= n; p++) { sum += ori[p - 1]; if ((ori[p] ^ ori[pos]) == sum) ans.push_back(make_pair(pos, p)); if (sum > mx) break; } } } sort(ans.begin(), ans.end()); ans.erase(unique(ans.begin(), ans.end()), ans.end()); int A = int(ans.size()); cout << A << endl; } signed main() { solve(); }
8
#include <bits/stdc++.h> using namespace std; int main() { int testcase; int counter[1000]; int modulo[2] = {}; int checker; cin >> testcase; for (int i = 0; i < testcase; i++) { int x; cin >> x; counter[i] = x % 2; modulo[counter[i]]++; } if (modulo[0] < modulo[1]) checker = 0; else checker = 1; for (int i = 0; i < testcase; i++) { if (counter[i] == checker) { cout << i + 1; return 0; } } }
2
#include <bits/stdc++.h> using namespace std; int n, m; priority_queue<long long> p1; priority_queue<long long> p2; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cerr.tie(0); cin >> n >> m; vector<vector<long long>> mat; mat.resize(n + 5, vector<long long>(m + 5)); long long red[max(n, m) + 5]; long long kol[max(n, m) + 5]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> mat[i][j]; if (n <= 4 || m <= 4) { long long s = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) s += mat[i][j]; cout << s; return 0; } if (m < n) { vector<vector<long long>> mat2; mat2.resize(m + 5, vector<long long>(n + 5)); for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) mat2[i][j] = mat[j][i]; mat = mat2; int t = m; m = n; n = t; } for (int i = 1; i <= n; i++) { red[i] = 0; for (int j = 1; j <= m; j++) red[i] += mat[i][j]; p1.push(red[i]); } for (int j = 1; j <= m; j++) { kol[j] = 0; for (int i = 1; i <= n; i++) kol[j] += mat[i][j]; p2.push(kol[j]); } priority_queue<long long> temp1; priority_queue<long long> temp2; long long sol = -1; long long tes; pair<long long, int> p; temp1 = p1; tes = 0; for (int i = 0; i < 4; i++) { tes += temp1.top(); temp1.pop(); } sol = max(tes, sol); tes = 0; for (int i = 1; i <= n; i++) { tes = red[i]; temp2 = priority_queue<long long>(); for (int j = 1; j <= m; j++) temp2.push(kol[j] - mat[i][j]); for (int j = 0; j < 3; j++) { tes += temp2.top(); temp2.pop(); } sol = max(tes, sol); } for (int i = 1; i < n; i++) { for (int j = i + 1; j <= n; j++) { tes = red[i] + red[j]; temp2 = priority_queue<long long>(); for (int k = 1; k <= m; k++) temp2.push(kol[k] - mat[i][k] - mat[j][k]); for (int k = 0; k < 2; k++) { tes += temp2.top(); temp2.pop(); } sol = max(tes, sol); } } for (int j = 1; j <= m; j++) { tes = kol[j]; temp1 = priority_queue<long long>(); for (int i = 1; i <= n; i++) temp1.push(red[i] - mat[i][j]); for (int i = 0; i < 3; i++) { tes += temp1.top(); temp1.pop(); } sol = max(sol, tes); } tes = 0; for (int i = 0; i < 4; i++) { tes += p2.top(); p2.pop(); } sol = max(tes, sol); cout << sol; return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, u, v, c; int matrix[101][101]; bool seen[101][101]; int find(int from, int res) { int max = res; for (int i = 0; i < n; i++) { if (!matrix[from][i] || seen[from][i]) continue; for (int j = 0; j < n; j++) seen[j][i] = true; int ff = find(i, res + matrix[from][i]); if (ff > max) max = ff; for (int j = 0; j < n; j++) seen[j][i] = false; } return max; } int main() { cin >> n; int k = n; while (--k) { cin >> u >> v >> c; matrix[u][v] = c; matrix[v][u] = c; } for (int i = 0; i < n; i++) { seen[i][0] = true; } cout << find(0, 0) << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long fact[100100]; long long invFact[100100]; long long a[101000][110]; long long ex(long long a, long long n) { if (n == 0) return 1; long long res = ex(a, n / 2); res *= res; res %= mod; if (n % 2 == 1) res *= a; res %= mod; return res; } long long inv(long long a) { return ex(a, mod - 2); } long long C(int n, int k) { long long res = fact[n]; res *= invFact[k]; res %= mod; res *= invFact[n - k]; res %= mod; return res; } void add(int l, int r, int k) { a[l + 200 - k][k]++; a[l + 200 - k][k] %= mod; for (int i = 0; i <= k; i++) { a[r + 1 + 200][i] -= C(r - l + k + 1, k - i); a[r + 1 + 200][i] %= mod; } } void get() { for (int i = 0; i < 101000 - 1; i++) { for (int j = 0; j < 105; j++) { a[i + 1][j] = (a[i + 1][j] + a[i][j]) % mod; if (j != 0) a[i + 1][j - 1] = (a[i + 1][j - 1] + a[i][j]) % mod; } } } int ini[100100]; int main() { int N, M; scanf("%d%d", &N, &M); fact[0] = 1; invFact[0] = 1; for (int i = 1; i < 100100; i++) { fact[i] = fact[i - 1] * i; fact[i] %= mod; invFact[i] = inv(fact[i]); } for (int i = 0; i < N; i++) scanf("%d", ini + i); for (int i = 0; i < M; i++) { int l, r, k; scanf("%d%d%d", &l, &r, &k); l--; r--; add(l, r, k); } get(); for (int i = 0; i < N; i++) { long long ans = a[i + 200][0]; ans += ini[i]; ans %= mod; ans += mod; ans %= mod; printf("%d%c", (int)ans, i == N - 1 ? '\n' : ' '); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; char a[705]; int X[705]; int N; long long DP[705][705][10][2]; int main() { scanf("%s", a + 1); N = strlen(a + 1); for (int i = 1; i <= N; ++i) X[i] = a[i] - '0'; for (int i = 0; i <= 9; ++i) DP[0][0][i][1] = 1; for (int d = 0; d <= 9; ++d) { for (int i = 0; i <= N - 1; ++i) { for (int j = 0; j <= i; ++j) { for (int k = 0; k <= 1; ++k) { long long v = DP[i][j][d][k]; if (!v) continue; for (int nd = 0; nd <= 9; ++nd) { if (k && nd > X[i + 1]) continue; int nk = k && nd == X[i + 1]; long long& nv = DP[i + 1][j + (nd >= d)][d][nk]; nv += v; if (nv >= MOD) nv -= MOD; } } } } } long long ret = 0; for (int d = 1; d <= 9; ++d) { long long ones = 1; for (int i = 1; i <= N; ++i) { long long v = (DP[N][i][d][0] + DP[N][i][d][1]) % MOD; ret += v * ones % MOD; if (ret >= MOD) ret -= MOD; ones = (ones * 10 % MOD + 1) % MOD; } } printf("%lld\n", ret); }
10
#include <bits/stdc++.h> using namespace std; using LL = long long; const LL hot = (1 << ('z' - 'a' + 1)) - 1; LL id(const set<char>& s) { LL p = 0; for (char c : s) { p += (1 << (c - 'a')); } return p; } int main(void) { int N; cin >> N; LL filt = hot; int cnt = -1; for (int i = 0; i < N - 1; ++i) { char q; cin >> q; string S; cin >> S; set<char> s; for (char c : S) s.insert(c); LL val = id(s); if ('!' != q) { val ^= hot; } if (cnt >= 0 && (q == '!' || q == '?')) { cnt++; } filt &= val; if (cnt >= 0) continue; for (int h = 0; h < 26; ++h) { if (filt == (1 << h)) { cnt = 0; } } } cout << max(cnt, 0) << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long int mod = 1000000007; bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long int powe(long long int x, long long int y) { long long int res = 1; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; void solve() { long long int i, x, y, j, k, h, n, w, k1, k2, q; cin >> n; vector<long long int> v(n); map<long long int, long long int> m; for (i = 0; i < n; i++) { cin >> v[i]; m[v[i]]++; if (m[v[i]] > 2) { cout << "NO" << endl; return; } } cout << "YES" << endl; vector<long long int> v1, v2; for (i = 0; i < n; i++) { if (!m[v[i]]) continue; if (m[v[i]] == 2) v2.push_back(v[i]); m[v[i]] = 0; v1.push_back(v[i]); } sort(v1.begin(), v1.end()); ; if (v2.size()) sort(v2.rbegin(), v2.rend()); cout << v1.size() << endl; for (i = 0; i < v1.size(); i++) cout << v1[i] << " "; cout << endl; if (!v2.size()) { cout << 0 << endl << endl; return; } cout << v2.size() << endl; for (i = 0; i < v2.size(); i++) cout << v2[i] << " "; cout << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int i, j, l, k, t, n; t = 1; for (i = 1; i <= t; i++) { solve(); } }
1
#include <bits/stdc++.h> const int INF = (int)1e9 + 7; using namespace std; int n; map<string, int> id; int a[10][10], x[10]; int aa, bb, cc, resa, resb; int max1(int a, int b, int c) { return (max(trunc(aa / a), max(trunc(bb / b), trunc(cc / c)))); } int min1(int a, int b, int c) { return (min(trunc(aa / a), min(trunc(bb / b), trunc(cc / c)))); } int countLike() { int gr1 = 0, gr2 = 0, gr3 = 0; for (int i = 1; i <= 7; i++) for (int j = 1; j <= 7; j++) if (x[i] == x[j] && a[i][j]) { if (x[i] == 1) gr1++; if (x[i] == 2) gr2++; if (x[i] == 3) gr3++; } return gr1 + gr2 + gr3; } void Update() { int g1 = 0, g2 = 0, g3 = 0; for (int i = 1; i <= 7; i++) { g1 += (x[i] == 1); g2 += (x[i] == 2); g3 += (x[i] == 3); } if (g1 == 0 || g2 == 0 || g3 == 0) return; if (max1(g1, g2, g3) - min1(g1, g2, g3) < resa) { resa = max1(g1, g2, g3) - min1(g1, g2, g3); resb = countLike(); } else if (max1(g1, g2, g3) - min1(g1, g2, g3) == resa) { resb = max(resb, countLike()); } } void Back(int u) { if (u == 8) { Update(); return; } for (int i = 1; i <= 3; i++) { x[u] = i; Back(u + 1); } } int main() { cin >> n; id["Anka"] = 1; id["Chapay"] = 2; id["Cleo"] = 3; id["Troll"] = 4; id["Dracul"] = 5; id["Snowy"] = 6; id["Hexadecimal"] = 7; memset(a, 0, sizeof(a)); for (int i = 1; i <= n; i++) { string u, temp, v; cin >> u >> temp >> v; a[id[u]][id[v]] = 1; } cin >> aa >> bb >> cc; resa = INF; resb = 0; Back(1); cout << resa << " " << resb << endl; }
3
#include <bits/stdc++.h> using namespace std; long long int inv(long long int a) { int n = 1000000005; int p = 1000000007; long long int ret = 1; while (n != 0) { if (n % 2 == 1) ret = (ret * a) % p; n /= 2; a = a * a % p; } return ret; } int main() { int n; cin >> n; long long int dp[n][2]; int i, j; for (i = 0; i < n; i++) { dp[i][0] = 0; dp[i][1] = 0; } vector<int> child[n]; long long int temp; for (i = 1; i < n; i++) { cin >> temp; child[temp].push_back(i); } int wb[n]; for (i = 0; i < n; i++) { cin >> wb[i]; } for (i = n - 1; i >= 0; i--) { if (child[i].size() == 0) { dp[i][wb[i]] = 1; } else { temp = 1; for (j = 0; j < child[i].size(); j++) { temp *= ((dp[child[i][j]][0] + dp[child[i][j]][1]) % 1000000007); temp %= 1000000007; } if (wb[i] == 1) { dp[i][1] = temp; dp[i][0] = 0; } else { dp[i][0] = temp; dp[i][1] = 0; for (j = 0; j < child[i].size(); j++) { dp[i][1] += temp * dp[child[i][j]][1] % 1000000007 * inv((dp[child[i][j]][0] + dp[child[i][j]][1]) % 1000000007) % 1000000007; dp[i][1] %= 1000000007; } } } } cout << dp[0][1] << "\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; string s[8]; int main() { for (int i = 0; i < 8; i++) cin >> s[i]; int x = 8, y = 0; for (int j = 0; j < 8; j++) { for (int i = 0; i < 8; i++) if (s[i][j] != '.') { if (s[i][j] == 'W') x = min(x, i); break; } for (int i = 7; i >= 0; i--) if (s[i][j] != '.') { if (s[i][j] == 'B') y = max(y, i); break; } } if (x <= 7 - y) cout << "A\n"; else cout << "B\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int m, n; cin >> m >> n; cout << m * n / 2; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, i, count = 0, ans = 0; cin >> n; int vi[n]; int a[n]; memset(vi, 0, sizeof(vi)); for (i = 0; i < n; i++) { cin >> a[i]; } int flag = 0; while (count != n) { ans++; if (flag == 1) { for (i = n - 1; i >= 0; i--) { if (count >= a[i] && vi[i] == 0) { vi[i] = 1; count++; } } flag = 0; } else { for (i = 0; i < n; i++) { if (count >= a[i] && vi[i] == 0) { count++; vi[i] = 1; } } flag = 1; } } cout << (ans - 1) << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; const long long inf = 1000000000; vector<int> G[maxn], M[maxn]; int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt, outd[maxn], val[maxn], imin, gg[maxn], n; stack<int> S; long long ans; void dfs(int u) { pre[u] = lowlink[u] = ++dfs_clock; S.push(u); for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (!pre[v]) { dfs(v); lowlink[u] = min(lowlink[u], lowlink[v]); } else if (!sccno[v]) { lowlink[u] = min(lowlink[u], pre[v]); } } if (lowlink[u] == pre[u]) { scc_cnt++; imin = inf; for (;;) { int x = S.top(); S.pop(); imin = min(imin, val[x]); sccno[x] = scc_cnt; if (x == u) break; } gg[scc_cnt] = min(gg[scc_cnt], imin); } } void solve(int n) { dfs_clock = scc_cnt = 0; memset(sccno, 0, sizeof sccno); memset(pre, 0, sizeof pre); for (int i = 1; i <= n; ++i) { if (!pre[i]) dfs(i); } } int main() { scanf("%d", &n); ans = 0; for (int i = 1; i <= n; ++i) { scanf("%d", val + i); } for (int i = 1, v; i <= n; ++i) { scanf("%d", &v); if (v == i) continue; G[i].push_back(v); } for (int i = 0; i < maxn; ++i) { gg[i] = inf; } solve(n); for (int i = 1; i <= n; ++i) { for (int j = 0; j < G[i].size(); ++j) { if (sccno[i] != sccno[G[i][j]]) { M[sccno[i]].push_back(sccno[G[i][j]]); outd[sccno[i]]++; } } } ans = 0; for (int i = 1; i <= scc_cnt; ++i) { if (outd[i] == 0) { ans += gg[i]; } } printf("%lld\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long g[444444], n; long long a[444444], val[444444]; long long used[444444]; void dfs(long long i) { used[i] = 1; if (g[i] == -1) { used[i] = 2; return; } if (used[g[i]] == 1 || !g[i]) { val[i] = -1; used[i] = 2; return; } if (!used[g[i]]) dfs(g[i]); if (val[g[i]] == -1) val[i] = -1; else val[i] = val[g[i]] + (i >= n ? a[i - n + 1] : a[i]); used[i] = 2; } int main() { scanf("%lld", &n); for (long long i = 1; i < n; i++) scanf("%lld", &a[i]); for (long long i = 1; i < n; i++) { if (i + a[i] < n) g[i] = i + a[i] + n - 1; else { g[i] = -1; val[i] = a[i]; } if (i - a[i] >= 0) g[i + n - 1] = i - a[i]; else { g[i + n - 1] = -1; val[i + n - 1] = a[i]; } } for (long long i = 1; i <= n * 2 - 2; i++) { if (!used[i]) dfs(i); } for (long long i = 1; i < n; i++) { if (val[i + n - 1] == -1) printf("-1\n"); else printf("%lld\n", i + val[i + n - 1]); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 200009; vector<int> neigh[maxn]; int col[maxn]; bool vis[maxn]; void dfs(int v, int c) { vis[v] = true; col[v] = c; for (int u : neigh[v]) { if (vis[u]) continue; dfs(u, 1 - c); } } int pos[maxn]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int u, v; scanf("%d %d", &u, &v); pos[2 * i] = u; pos[2 * i + 1] = v; neigh[u].push_back(v); neigh[v].push_back(u); neigh[2 * i + 1].push_back(2 * i + 2); neigh[2 * i + 2].push_back(2 * i + 1); } for (int i = 1; i <= 2 * n; i++) { if (!vis[i]) { dfs(i, 1); } } for (int i = 0; i < n; i++) { printf("%d %d\n", col[pos[2 * i]] + 1, col[pos[2 * i + 1]] + 1); } }
9
#include <bits/stdc++.h> using namespace std; template <class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> T sqr(T x) { return x * x; } template <class T> T cube(T x) { return x * x * x; } template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template <class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); } const long double PI = acos(-1.0); const long double eps = 1e-9; const int dr[] = {-1, 0, +1, 0}; const int dc[] = {0, +1, 0, -1}; const int inf = (int)1e9 + 5; const long long linf = (long long)1e16 + 5; const long long mod = (long long)1e9 + 7; bool h[400]; int main() { std::ios::sync_with_stdio(false); int n, i, p, a; scanf("%d", &p); scanf("%d", &n); i = 0; memset(h, false, sizeof(h)); while (i < n) { scanf("%d", &a); int k = a % p; if (h[k]) { cout << i + 1 << endl; break; } else { h[k] = true; } i++; } if (i >= n) cout << "-1" << endl; }
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:60777216") using namespace std; int main() { int n; cin >> n; vector<pair<int, int> > p(n); for (int i = (0); i < (n); i++) scanf("%d %d", &p[i].first, &p[i].second); sort((p).begin(), (p).end()); int lst = -1e9; for (int i = (0); i < (n); i++) { if (lst <= p[i].second) lst = p[i].second; else lst = p[i].first; } cout << lst << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; void GO() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); } const long long INF = 1e9 + 7; const long long mod = (1e9); const int maxn = (int)4000; const long double PI = acos(-1.0); const long double eps = 0.000000001; mt19937 rnd(time(0)); vector<vector<int> > g; vector<int> u; vector<int> mas; void dfs(int v) { u[v] = 1; mas.push_back(v); for (auto to : g[v]) { if (u[to]) continue; dfs(to), mas.push_back(v); } } void solve() { int n, m, k; cin >> n >> m >> k; long long mx = (2 * n + k - 1) / k; g.resize(n), u.resize(n); for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; a--, b--; g[a].push_back(b), g[b].push_back(a); } dfs(0); vector<vector<int> > ans(k); int ost = mx, timer = 0; for (int i = 0; i < int(mas.size()); ++i) { if (ost) { ost--; ans[timer].push_back(mas[i]); } else { ost = mx - 1, timer++; ans[timer].push_back(mas[i]); } } timer++; while (timer < k) ans[timer++].push_back(mas.back()); for (int i = 0; i < k; ++i) { cout << int(ans[i].size()) << ' '; for (auto &j : ans[i]) cout << j + 1 << ' '; cout << '\n'; } } signed main() { GO(); int Q = 1; while (Q--) { solve(); } }
6
#include <bits/stdc++.h> inline long long ceil_div(long long a, long long b) { return (a + b - 1) / b; } using namespace std; inline int qr() { int f = 0, fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fu = -1; c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } return f * fu; } const int N = 1e6 + 10, M = N * 4; const long long INF = 2e18; struct DINIC { int head[N << 1], ver[M << 1], Next[M << 1], d[N << 1]; int s, t, tot = 1, n; queue<int> q; long long mxf, edge[M << 1]; inline void init() { memset(head, 0, sizeof(int) * (n + 1)); tot = 1, mxf = n = 0; } inline void add(int x, int y, int z) { n = max({n, x, y}); ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot; ver[++tot] = x, edge[tot] = 0, Next[tot] = head[y], head[y] = tot; } inline bool bfs() { memset(d, 0, sizeof(int) * (n + 1)); while (!q.empty()) q.pop(); q.push(s), d[s] = 1; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = head[x]; i; i = Next[i]) if (edge[i] && !d[ver[i]]) { q.push(ver[i]); d[ver[i]] = d[x] + 1; if (ver[i] == t) return true; } } return false; } long long dinic(int x, long long flow) { if (x == t) return flow; long long rest = flow, k; for (int i = head[x]; i && rest; i = Next[i]) if (edge[i] && d[ver[i]] == d[x] + 1) { k = dinic(ver[i], min(rest, 1ll * edge[i])); if (!k) d[ver[i]] = 0; edge[i] -= k, edge[i ^ 1] += k, rest -= k; } return flow - rest; } inline long long solve() { long long flow; while (bfs()) while ((flow = dinic(s, INF))) mxf += flow; return mxf; } } D; int a[N], b[N], n, T; int main() { T = qr(); while (T--) { n = qr(); for (register int i = 1; i <= n; ++i) a[i] = qr(); for (register int i = 1; i <= n; ++i) b[i] = qr(); D.init(); D.s = 0, D.t = 2 * n + 1; for (int i = 1; i <= n; i++) D.add(0, i, b[i]); for (int i = 1; i <= n; i++) D.add(n + i, 2 * n + 1, a[i]); for (int i = 1; i <= n; i++) { D.add(i, n + i, b[i]); if (i < n) D.add(i, n + i + 1, b[i]); else D.add(i, n + 1, b[i]); } long long sum = 0; for (int i = 1; i <= n; i++) sum += a[i]; puts(D.solve() == sum ? "YES" : "NO"); } return 0; }
8
#include <bits/stdc++.h> using namespace std; long long int mod = 1000000007; long long int powmod(long long int a, long long int b) { long long int res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long int gcd(long long int a, long long int b) { return b ? gcd(b, a % b) : a; } const int MAXN = 200005; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { long long int n, m, flag = 0, k, ans = 0, x = 0, y = 0, a, b, A = 0, B = 0; cin >> n >> b >> a; int s[n]; for (int i = (0); i < (n); i++) cin >> s[i]; A = a; B = b; while (ans < n) { if (s[ans] == 0) { if (A > 0) { ans++; A--; } else if (B > 0) { ans++; B--; } else break; } else { if (B > 0 && A < a) { ans++; B--; A = min(A + 1, a); } else if (A > 0) { ans++; A--; } else break; } } cout << ans << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; int const nmax = 100000; int const sigma = 26; int v[1 + nmax], frec[1 + sigma], pos[1 + sigma][1 + 2 * nmax]; vector<pair<int, int>> constraint[1 + sigma]; int dp[1 + nmax]; bool satisfy(int x, int l, int r) { return l <= x && x <= r; } int change(int x, int val, int l, int r) { int result = -satisfy(dp[x], l, r) + satisfy(dp[x] + val, l, r); dp[x] += val; return result; } int updateinterval(int x, int y, int val, int l, int r) { int result = 0; for (int i = x; i <= y; i++) result += change(i, val, l, r); return result; } int main() { string s; cin >> s; int n = s.size(); for (int i = 1; i <= n; i++) v[i] = s[i - 1] - 'a' + 1; int q, l, r; cin >> q >> l >> r; int zerop = 0; for (int i = 1; i <= q; i++) { char op; int x, y; cin >> op >> x >> y; constraint[op - 'a' + 1].push_back({x, y}); if (x == 0) zerop++; } for (int i = 1; i <= sigma; i++) for (int j = 1; j <= 2 * nmax; j++) pos[i][j] = n + 1; for (int i = 1; i <= n; i++) { frec[v[i]]++; pos[v[i]][frec[v[i]]] = i; } ll result = 0, acc = 0; for (int i = n; 1 <= i; i--) { dp[i] = zerop; if (satisfy(dp[i], l, r) == 1) acc++; for (int h = 0; h < constraint[v[i]].size(); h++) { int x = constraint[v[i]][h].first; int y = constraint[v[i]][h].second; if (0 < x && pos[v[i]][frec[v[i]] + x - 1] != n + 1) acc += updateinterval(pos[v[i]][frec[v[i]] + x - 1], pos[v[i]][frec[v[i]] + x] - 1, 1, l, r); if (pos[v[i]][frec[v[i]] + y] != n + 1) acc += updateinterval(pos[v[i]][frec[v[i]] + y], pos[v[i]][frec[v[i]] + y + 1] - 1, -1, l, r); } frec[v[i]]--; result += acc; } cout << result; return 0; }
10
#include <bits/stdc++.h> using namespace std; template <typename... T> void read(T&... args) { ((cin >> args), ...); } void solve() { int n, m; cin >> n >> m; string second[n]; for (int i = 0; i < n; i++) { cin >> second[i]; } int first[m], x; for (int i = 0; i < m; i++) { vector<int> v(5, 0); int max = 0; for (int j = 0; j < n; j++) { x = second[j][i] - 65; v[x]++; if (max < v[x]) max = v[x]; } first[i] = max; } int A[m]; long long r = 0; for (int i = 0; i < m; i++) { cin >> A[i]; r += A[i] * first[i]; } cout << r; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T = 1; while (T--) { solve(); } return 0; }
0
#include <bits/stdc++.h> int main(void) { uint32_t n, m, x; uint8_t divAmount = 0, primeCount = 0, lastPrime = 0; scanf("%d %d", &n, &m); x = m - n; for (register size_t i = 1; i <= x; i++) { divAmount = 0; n++; for (register size_t j = 1; j <= n; j++) { if (n % j == 0) divAmount++; } if (divAmount == 2) { primeCount++; lastPrime = n; } } if ((primeCount == 1) && (lastPrime == m)) printf("YES"); else printf("NO"); return 0; }
0
#include <bits/stdc++.h> using namespace std; ifstream in("input.txt"); ofstream out("output.txt"); vector<int> v[100001]; int vis[100001] = {0}; vector<int> arr; int main() { string str; cin >> str; int min = INT_MAX; int a[str.length()]; for (int i = 0; i < str.length(); i++) { int x = int(str[i]); if (min >= x) { a[i] = 0; min = x; } else a[i] = 1; } for (int i = 0; i < str.length(); i++) { if (a[i] == 1) cout << "Ann \n"; else cout << "Mike \n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int n, m, T; int A[maxn], dp[maxn][2]; int f(int x, int q) { if (x == 0 && q == 0) return 0; else if (x <= 0) return 1 << 29; if (dp[x][q] != -1) return dp[x][q]; if (q == 0) { if (x >= 2) dp[x][0] = min(f(x - 2, 1), f(x - 1, 1)); else dp[x][0] = f(x - 1, 1); return dp[x][0]; } else { if (x >= 2) dp[x][1] = min(f(x - 2, 0) + A[x] + A[x - 1], f(x - 1, 0) + A[x]); else dp[x][1] = f(x - 1, 0) + A[x]; return dp[x][1]; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> T; while (T--) { cin >> n; for (int i = 1; i <= n; ++i) { cin >> A[i]; } for (int i = 0; i <= n; ++i) { dp[i][0] = dp[i][1] = -1; } cout << min(f(n, 0), f(n, 1)) << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; long long modexpo(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % 1000000007; a = (a * a) % 1000000007; b >>= 1; } return res; } long long expo(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a); a = (a * a); b >>= 1; } return res; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i += 6) { if ((n % i == 0) || (n % (i + 2) == 0)) return false; } return true; } long double dist(long long x1, long long y1, long long x2, long long y2) { return (long double)sqrt( (long double)((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))); } long long squaredist(long long x1, long long y1, long long x2, long long y2) { return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } long long nCr(long long n, long long r) { if (r == 0) return 1; return (n * nCr(n - 1, r - 1)) / r; } int main() { int n, m; cin >> n >> m; string a, b; cin >> a; cin >> b; int j = 0, i1 = 0, i = 0, k; while (j < n && i1 < m) { if (a[j] == b[i1]) { j++; i1++; } else break; } a.erase(a.begin(), a.begin() + j); b.erase(b.begin(), b.begin() + i1); j = b.length() - 1; i = a.length() - 1; while (j >= 0 && i >= 0) { if (b[j] == a[i]) { j--; i--; } else break; } a.erase(a.begin() + i + 1, a.end()); b.erase(b.begin() + j + 1, b.end()); if (a == b || a == "*") cout << "YES\n"; else cout << "NO\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int inf = 1000 * 1000 * 1000; int n; double L; double ar[1000], br[1000]; int main() { cin >> n >> L; for (int i = 0; i < n; i++) cin >> ar[i]; for (int i = 0; i < n; i++) cin >> br[i]; double res = 0; double x = inf; for (int i = 0; i < n; i++) x = min(x, br[i] / ar[i]); for (int i = 0; i < n; i++) res += ar[i]; res *= x; res = min(res, L); printf("%.5lf", res); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { if (1) { int k, p; long long int a, b, c, d; long long int count = 0; cin >> a >> b >> c >> d; long long int temp[3] = {0}, key; temp[0] = a, temp[1] = b; temp[2] = c; for (k = 1; k < 3; k++) { key = temp[k]; for (p = k - 1; p >= 0 && temp[p] > key; p--) temp[p + 1] = temp[p]; temp[p + 1] = key; } count += temp[2] - temp[1]; count += temp[2] - temp[0]; if (count <= d) { d = d - count; if ((a + b + c + count + d) % 3 == 0) cout << "YES" << endl; else { cout << "NO" << endl; } } else cout << "NO" << endl; } } }
0
#include <bits/stdc++.h> using namespace std; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } int main() { int a, b, c, d; cin >> a >> b >> c >> d; int ans[51][51]; cout << "50 50" << endl; for (long long i = (long long)(1); i <= (long long)(25); ++i) { for (long long j = (long long)(1); j <= (long long)(25); ++j) { ans[i][j] = 'a'; } } a--; for (long long i = (long long)(1); i <= (long long)(25); ++i) { for (long long j = (long long)(26); j <= (long long)(50); ++j) ans[i][j] = 'b'; } b--; for (long long i = (long long)(26); i <= (long long)(50); ++i) { for (long long j = (long long)(1); j <= (long long)(25); ++j) { ans[i][j] = 'c'; } } c--; for (long long i = (long long)(26); i <= (long long)(50); ++i) { for (long long j = (long long)(26); j <= (long long)(50); ++j) { ans[i][j] = 'd'; } } d--; for (int i = 1; i <= 25 && b; i += 2) { for (int j = 2; j <= 25 && b; j += 2) { ans[i][j] = 'b'; b--; } } for (int i = 1; i <= 25 && c; i += 2) { for (int j = 27; j <= 50 && c; j += 2) { ans[i][j] = 'c'; c--; } } for (int i = 26; i <= 50 && d; i += 2) { for (int j = 2; j <= 25 && d; j += 2) { ans[i][j] = 'd'; d--; } } for (int i = 26; i <= 50 && a; i += 2) { for (int j = 26; j <= 50 && a; j += 2) { ans[i][j] = 'a'; a--; } } for (long long i = (long long)(1); i <= (long long)(50); ++i) { for (long long j = (long long)(1); j <= (long long)(50); ++j) { cout << (char)ans[i][j]; } cout << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { char d; cin >> d; string k1, k2, k3, f; cin >> f; k1 = "qwertyuiop"; k2 = "asdfghjkl;"; k3 = "zxcvbnm,./"; int n = f.size(); if (d == 'R') { for (int i = 0; i < n; i++) { char now = f[i]; for (int j = 0; j < k1.size(); j++) { if (now == k1[j]) f[i] = k1[j - 1]; } for (int j = 0; j < k2.size(); j++) { if (now == k2[j]) f[i] = k2[j - 1]; } for (int j = 0; j < k3.size(); j++) { if (now == k3[j]) f[i] = k3[j - 1]; } } } else if (d == 'L') { for (int i = 0; i < n; i++) { char now = f[i]; for (int j = 0; j < k1.size(); j++) { if (now == k1[j]) f[i] = k1[j + 1]; } for (int j = 0; j < k2.size(); j++) { if (now == k2[j]) f[i] = k2[j + 1]; } for (int j = 0; j < k3.size(); j++) { if (now == k3[j]) f[i] = k3[j + 1]; } } } cout << f << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, l1, l2, r1, r2; cin >> l1 >> r1 >> l2 >> r2; if (l1 != r2) { cout << l1 << " " << r2 << endl; } else if (l1 != l2) { cout << l1 << " " << l2 << endl; } else if (r1 != l2) { cout << r1 << " " << l2 << endl; } else if (r1 != r2) { cout << r1 << " " << r2 << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long p[15], b[15]; for (long long i = 0; i < 14; ++i) cin >> p[i]; long long q = 0; for (long long i = 0; i < 14; ++i) { for (long long k = 0; k < 14; ++k) b[k] = p[k]; long long c = p[i] / 14; b[i] = 0; int cnt = 1; for (int j = i + 1;; ++j) { if (j == 14) j = 0; b[j] += c; cnt++; if (j == i) break; } c = p[i] % 14; for (int j = i + 1; c; ++j) { if (j == 14) j = 0; b[j]++; c--; } long long ans = 0; for (long long k = 0; k < 14; ++k) if (b[k] % 2 == 0 && b[k]) ans += b[k]; q = max(q, ans); } cout << q << endl; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 3; int n, a[N], ans = 0; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", a + i); } sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { if ((i == 1 || a[i] != a[i - 1]) && a[i]) ans++; } cout << ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; int a, b; char a1[33][33] = { 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1}; int main() { ios::sync_with_stdio(false); cin >> a >> b; cout << int(a1[a][b]); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 600100; int N, W; vector<pair<long long, int> > os; vector<pair<pair<long long, long long>, int> > t2; long long bval[MAXN]; long long mval[MAXN]; int msource[MAXN]; int source[MAXN]; int ans[MAXN]; inline bool cmp(pair<long long, long long> left, pair<long long, long long> right) { return left.first + left.second < right.first + right.second; } inline bool cmp2(pair<pair<long long, long long>, int> left, pair<pair<long long, long long>, int> right) { return cmp(left.first, right.first); } int main() { cin >> N >> W; for (int i = 0; i < N; i++) { long long a, b; cin >> a >> b; b -= a; if (a <= b) { os.push_back(make_pair(a, i)); os.push_back(make_pair(b, i)); } else { t2.push_back(make_pair(make_pair(a, b), i)); } } sort(os.begin(), os.end()); sort(t2.begin(), t2.end(), cmp2); int M = t2.size(); for (int i = 0; i <= W; i++) bval[i] = 1e15; if (M > 0) { mval[M - 1] = t2[M - 1].first.first; msource[M - 1] = M - 1; } for (int i = M - 2; i >= 0; i--) { if (t2[i].first.first < mval[i + 1]) msource[i] = i; else msource[i] = msource[i + 1]; mval[i] = min(mval[i + 1], t2[i].first.first); } long long tot = 0, mhi = -1e9; int mloc = -1; bval[0] = 0; for (int i = 0; i < M; i++) { if (tot + mval[i] < tot + t2[i].first.first + t2[i].first.second - mhi) source[2 * i + 1] = msource[i]; else source[2 * i + 1] = mloc; bval[2 * i + 1] = min(tot + mval[i], tot + t2[i].first.first + t2[i].first.second - mhi); tot += t2[i].first.first + t2[i].first.second; if (t2[i].first.second > mhi) mloc = i; mhi = max(mhi, t2[i].first.second); bval[2 * i + 2] = tot; } int res = max(0, W - (int)os.size()); for (int i = max(0, W - (int)os.size()); i < W; i++) { if (bval[i + 1] < bval[i] + os[W - 1 - i].first) res = i + 1; bval[i + 1] = min(bval[i + 1], bval[i] + os[W - i - 1].first); } for (int i = 0; i < MAXN; i++) ans[i] = 0; for (int i = 0; i < W - res; i++) ans[os[i].second]++; for (int i = 0; i < res / 2; i++) ans[t2[i].second] += 2; if (res % 2 == 1) { int c = source[res]; if (c >= res / 2) ans[t2[c].second]++; else { ans[t2[c].second]--; ans[t2[res / 2].second] += 2; } } cout << bval[W] << "\n"; for (int i = 0; i < N; i++) cout << ans[i]; cout << "\n"; return 0; }
9
#include <bits/stdc++.h> using namespace std; int mod = 1e9 + 7; int power(int p, int k) { int a = 1; while (k) { if (k & 1) a = (a * p) % mod; p = (p * p) % mod; k >>= 1; } return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tc = 1; int i, j; cin >> tc; while (tc--) { int n; cin >> n; if (n == 1 || n == 2) cout << 1; else if (n == 3) cout << 2; else if (n % 2 == 0) cout << n / 2; else if (n % 2 == 1) cout << n / 2 + 1; cout << "\n"; } }
0
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int N = 2e5; int a[N], b[N]; int dp[N][2]; int main() { int n, c; scanf("%d%d", &n, &c); for (int i = 0; i < n - 1; i++) scanf("%d", &a[i]); for (int i = 0; i < n - 1; i++) scanf("%d", &b[i]); dp[0][0] = 0; dp[0][1] = c; for (int i = 1; i < n; i++) dp[i][0] = dp[i][1] = INF; for (int i = 0; i < n - 1; i++) { dp[i + 1][0] = min(dp[i + 1][0], min(dp[i][0] + a[i], dp[i][1] + a[i])); dp[i + 1][1] = min(dp[i + 1][1], min(dp[i][1] + b[i], dp[i][0] + c + b[i])); } for (int i = 0; i < n; i++) printf("%d ", min(dp[i][0], dp[i][1])); puts(""); }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, *a, max_d = 0; ifstream in("in.txt"); cin >> n; bool fl = 1; int x; a = new int[n]; cin >> a[0]; x = a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; if (a[i] == x) continue; else fl = 0; } if (fl) { for (int i = 0; i < n; i++) { cout << a[i] << ' '; } return 0; } for (int i = 0; i < n - 1; i++) if (a[i + 1] - a[i] > max_d) max_d = a[i + 1] - a[i]; int ib = 0, j = n - 1, ia = 0; sort(&a[0], &a[n]); int *b = new int[n]; int k = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { b[ib] = a[i]; ib++; } else { b[j] = a[i]; j--; } } for (int i = 0; i < n; i++) cout << b[i] << ' '; delete[] a; delete[] b; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 2009; int n; int A; int B; int op[N][N]; double a[N]; double b[N]; double ab[N]; double dp[N][N]; int solve(double X) { for (int i = 1; i <= n; i++) { for (int j = 0; j <= A; j++) { op[i][j] = 0; dp[i][j] = dp[i - 1][j]; if (j > 0 && dp[i][j] < dp[i - 1][j - 1] + a[i]) { dp[i][j] = dp[i - 1][j - 1] + a[i]; op[i][j] = 1; } if (dp[i][j] < dp[i - 1][j] + b[i] - X) { dp[i][j] = dp[i - 1][j] + b[i] - X; op[i][j] = 2; } if (j > 0 && dp[i][j] < dp[i - 1][j - 1] + ab[i] - X) { dp[i][j] = dp[i - 1][j - 1] + ab[i] - X; op[i][j] = 3; } } } int ans = 0; int j = A; for (int i = n; i >= 1; i--) { if (op[i][j] >= 2) { ans++; } if (op[i][j] & 1) { j--; } } return ans; } int main() { cin >> n >> A >> B; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> b[i]; } for (int i = 1; i <= n; i++) { ab[i] = a[i] + b[i] - a[i] * b[i]; } double l = 0; double r = 1; for (int it = 0; it < 50; it++) { double m = (l + r) / 2.0; if (solve(m) > B) { l = m; } else { r = m; } } cout << dp[n][A] + r * B; return 0; }
11
#include <bits/stdc++.h> using namespace std; const int MAXL = 40000; pair<int, int> p[MAXL]; int Init() { for (int i = 0; i * i < MAXL; i++) { for (int j = i; j * j + i * i < MAXL; j++) { int now = i * i + j * j; p[now].first = i; p[now].second = j; } } return 0; } inline bool cmp(pair<int, int> a, pair<int, int> b) { return a.first * a.first + a.second * a.second > b.first * b.first + b.second * b.second; } inline bool cmp2(pair<int, int> a, pair<int, int> b) { return atan2(a.second, a.first) < atan2(b.second, b.first) - (1e-8); } int L(int a, int b) { return a * a + b * b; } int n; int Solve() { vector<pair<int, int> > e; int sum = 0; scanf("%d", &n); for (int i = 0; i < MAXL && e.size() <= n; i++) { if (p[i].first == 0 && p[i].second == 0) continue; sum ^= p[i].first ^ p[i].second; e.push_back(p[i]); } reverse(e.begin(), e.end()); for (vector<pair<int, int> >::iterator it = e.begin(); it != e.end(); it++) { if (((sum ^ it->first ^ it->second) & 1) == 0) { e.erase(it); break; } } sort(e.begin(), e.end(), cmp); int ix = 0, iy = 0; for (vector<pair<int, int> >::iterator it = e.begin(); it != e.end(); it++) { int fx = 1, fy = 1; int xx = it->first, yy = it->second; if (ix > 0) fx = -1; if (iy > 0) fy = -1; if (L(fx * xx + ix, fy * yy + iy) < L(fx * yy + ix, fy * xx + iy)) *it = make_pair(fx * xx, fy * yy); else *it = make_pair(fx * yy, fy * xx); ix = it->first + ix; iy = it->second + iy; } sort(e.begin(), e.end(), cmp2); puts("YES"); for (vector<pair<int, int> >::iterator it = e.begin(); it != e.end(); it++) { printf("%d %d\n", ix, iy); ix += it->first; iy += it->second; } return 0; } int main() { Init(); Solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; int find_res(char s[], char c) { int i, k = 0; if (c == 'r') k = 1; else k = 0; int x = 0, y = 0; for (i = 0; i < strlen(s); i++) { if (k == 1 && s[i] != 'r') x++; else if (k == 0 && s[i] != 'b') y++; k = 1 - k; } return (x > y ? x : y); } int main() { char str[100010]; int n; while (scanf("%d", &n) != EOF) { scanf("%s", str); int x, y; x = find_res(str, 'r'); y = find_res(str, 'b'); printf("%d\n", (x < y ? x : y)); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int inf = 0x7fffffff; const double eps = 1e-10; const double pi = acos(-1.0); inline int read() { int x = 0, f = 1; char ch; ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = 0; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch & 15); ch = getchar(); } if (f) return x; else return -x; } int n, K, cnt, num; int mp[5][110]; struct Answer { int id, x, y; void out() { printf("%d %d %d\n", id, x, y); } }; vector<Answer> ans; int main() { n = read(); K = read(); for (int i = 1; i <= 4; i++) for (int j = 1; j <= n; j++) mp[i][j] = read(); int sx = 2, sy = 1, fx, fy, tx, ty; while (cnt <= 10000 && ans.size() <= 20000 && num != K) { fx = sx; fy = (fx == 2 ? sy + 1 : sy - 1); if (fy > n) ++fx, --fy; if (fy < 1) --fx, ++fy; ty = fy; tx = (fx == 2 ? 1 : 4); if (mp[fx][fy]) { if (mp[tx][ty] == mp[fx][fy]) ans.push_back((Answer){mp[fx][fy], tx, ty}), ++num, mp[fx][fy] = 0; else if (!mp[sx][sy]) ans.push_back((Answer){mp[fx][fy], sx, sy}), swap(mp[fx][fy], mp[sx][sy]); } sx = fx; sy = fy; ++cnt; } if (num != K || ans.size() > 20000) puts("-1"); else { printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); i++) ans[i].out(); } return 0; }
6