func_code_string stringlengths 59 71.4k |
|---|
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pll = pair<ll, ll>; template <class T> using vv = vector<vector<T>>; template <class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; } template <class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; } const ll MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(0); int n; cin >> n; string opes(n, . ); vector<pii> clds(n); for (int(i) = 0; (i) < (n); ++(i)) { string s; cin >> s >> clds[i].first; if (s == IN ) { opes[i] = > ; continue; } --clds[i].first; if (s == NOT ) { opes[i] = ~ ; continue; } cin >> clds[i].second; --clds[i].second; if (s == AND ) { opes[i] = & ; } else if (s == OR ) { opes[i] = | ; } else if (s == XOR ) { opes[i] = ^ ; } else return 1; }; vector<int> vals(n); { function<int(int)> dfs = [&](int v) { if (opes[v] == & ) { vals[v] = dfs(clds[v].first) & dfs(clds[v].second); } else if (opes[v] == | ) { vals[v] = dfs(clds[v].first) | dfs(clds[v].second); } else if (opes[v] == ^ ) { vals[v] = dfs(clds[v].first) ^ dfs(clds[v].second); } else if (opes[v] == ~ ) { vals[v] = 1 - dfs(clds[v].first); } else vals[v] = clds[v].first; return vals[v]; }; dfs(0); }; string re(n, vals[0] + 0 ); ; { function<void(int)> dfs = [&](int v) { if (opes[v] == & ) { if (vals[clds[v].first]) dfs(clds[v].second); if (vals[clds[v].second]) dfs(clds[v].first); } else if (opes[v] == | ) { if (!vals[clds[v].first]) dfs(clds[v].second); if (!vals[clds[v].second]) dfs(clds[v].first); } else if (opes[v] == ^ ) { dfs(clds[v].first); dfs(clds[v].second); } else if (opes[v] == ~ ) { dfs(clds[v].first); } else re[v] ^= 1; }; dfs(0); } for (int(i) = 0; (i) < (n); ++(i)) if (opes[i] == > ) cout << re[i]; cout << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int oo = 1000000009; const double eps = 1e-9; const int mod = 1000000007; int nos[100001], n, pos = 0; int main() { cin >> n; for (int i = 0; i < n; ++i) scanf( %d , &nos[i]); sort(nos, nos + n); for (int i = 0; i < n; ++i) if (pos + 1 <= nos[i]) ++pos; printf( %d n , pos + 1); } |
#include <bits/stdc++.h> using namespace std; int n, m, qu; int a[100011]; int st[100011], en[100011]; int pos[100011]; int rt[100011], nxt[100011]; int step[100011]; bool vis[100011]; vector<int> v[100011], vv; queue<int> q; void bunhae_interval() { int r = n % m, mm = n / m; st[0] = 1; for (int i = 0; i < m; i++) { if (i) st[i] = en[i - 1] + 1; en[i] = st[i] + mm - 1; if (i < r) en[i]++; for (int j = st[i]; j <= en[i]; j++) pos[j] = i; } } void update(int x) { int p = pos[x]; for (int i = st[p]; i <= x; i++) vis[i] = 0; vis[x] = 1; q.push(x); while (!q.empty()) { int y = q.front(); q.pop(); for (int i = 0; i < v[y].size(); i++) { int z = v[y][i]; if (vis[z]) continue; vis[z] = 1; step[z] = step[y] + 1; rt[z] = rt[x]; q.push(z); } } } int main() { scanf( %d %d , &n, &qu); m = sqrt(n); bunhae_interval(); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int i = n; i >= 1; i--) { int j = i + a[i]; if (j > n || pos[j] != pos[i]) nxt[i] = j, rt[i] = i, step[i] = 0; else rt[i] = rt[j], step[i] = step[j] + 1, v[j].push_back(i); } while (qu--) { int tp, x, y; scanf( %d %d , &tp, &x); if (tp == 1) { int cnt = 1, en = x; while (1) { if (rt[en] == en) { if (nxt[en] > n) break; else cnt++, en = nxt[en]; } else { cnt += step[en], en = rt[en]; } } printf( %d %d n , en, cnt); } else { int xx = x + a[x]; if (xx > n) goto la; vv.clear(); for (int i = 0; i < v[xx].size(); i++) if (v[xx][i] != x) vv.push_back(v[xx][i]); v[xx].clear(); for (int i = 0; i < vv.size(); i++) v[xx].push_back(vv[i]); la:; scanf( %d , &y); int z = x + y; if (z > n || pos[z] != pos[x]) nxt[x] = z, rt[x] = x, step[x] = 0; else rt[x] = rt[z], step[x] = step[z] + 1, v[z].push_back(x); a[x] = y; update(x); } } } |
#include <bits/stdc++.h> using namespace std; long long int powers[1000100]; vector<pair<int, int> > edges; unsigned long long hashes[1000100] = {0}; unsigned long long ts[1000100] = {0}; int main(void) { powers[0] = 1; for (int i = 1; i <= 1000000; i++) powers[i] = (powers[i - 1] * (unsigned long long)41) % 291598227841757; int n, m; scanf( %d %d , &n, &m); for (int i = 0; i < m; i++) { int u, v; scanf( %d %d , &u, &v); hashes[u] = (hashes[u] + powers[v]) % 291598227841757; hashes[v] = (hashes[v] + powers[u]) % 291598227841757; ts[u - 1] = hashes[u]; ts[v - 1] = hashes[v]; edges.push_back(make_pair(u, v)); } sort(ts, ts + n); int cur = 0; long long int ans = 0; for (int i = 1; i < n; i++) { if (ts[i] != ts[cur]) { long long int n = i - cur; ans += n * (n - 1) / 2; cur = i; } } long long int a = n - cur; ans += a * (a - 1) / 2; for (int i = 0; i < m; i++) { int u = edges[i].first, v = edges[i].second; unsigned long long hu = (hashes[u] + powers[u]) % 291598227841757; unsigned long long hv = (hashes[v] + powers[v]) % 291598227841757; if (hu == hv) ans++; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 1e5 + 1; int n, m; vector<int> adj[N]; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int rand(int x) { return uniform_int_distribution<int>(0, x - 1)(rng); } int d[N]; bool inq[N]; bool vis3[N]; bool ok = true; void dfs3(int id) { inq[id] = true; vis3[id] = true; for (auto x : adj[id]) { if (!vis3[x]) { dfs3(x); } else if (!inq[x]) ok = false; } inq[id] = false; } bool arin(int x) { for (int i = 1; i <= n; i++) vis3[i] = inq[i] = false; ok = true; dfs3(x); return ok; } bool vis[N]; vector<int> ch[N]; int par[N]; int dx[N], dy[N]; int cs[N]; int up[N]; void dfs(int id) { vis[id] = true; up[id] = id; for (auto x : adj[id]) { if (!vis[x]) { d[x] = d[id] + 1; par[x] = id; ch[id].push_back(x); cs[id] = x; dfs(x); } else { dx[x]--; dx[id]++; if (d[up[id]] > d[x]) up[id] = x; } } for (auto x : ch[id]) { if (d[up[x]] < d[up[id]]) up[id] = up[x]; } } int dfs4(int id) { int duh = dx[id]; for (auto x : ch[id]) duh += dfs4(x); dy[id] = duh; return duh; } bool good[N]; int rt; void dfs2(int id) { if (dy[id] <= 1) good[id] = good[up[id]]; if (id == rt) good[id] = true; for (auto x : ch[id]) { dfs2(x); } } void solve() { cin >> n >> m; for (int i = 1; i <= n; i++) { adj[i].clear(); ch[i].clear(); dx[i] = dy[i] = 0; good[i] = false; } for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); } int x = 0; for (int i = 1; i <= 100; i++) { x = rand(n) + 1; if (arin(x)) break; x = 0; } if (x == 0) { cout << -1 n ; return; } for (int i = 1; i <= n; i++) vis[i] = false; d[x] = 0; dfs(x); rt = x; dfs4(x); dfs2(x); int cnt = 0; for (int i = 1; i <= n; i++) if (good[i]) cnt++; if (cnt * 5 < n) { cout << -1 n ; return; } for (int i = 1; i <= n; i++) if (good[i]) cout << i << ; cout << n ; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) solve(); } |
#include <bits/stdc++.h> const int maxN = 3e5 + 5; int main() { if (fopen( iput.txt , r )) { freopen( iput.txt , r , stdin); freopen( oput.txt , w , stdout); } int n; scanf( %d , &n); printf( %d n , n / 2 * 2 + n - n / 2); printf( 2 ); for (int i = 4; i <= n; i += 2) printf( %d , i); for (int i = 1; i <= n; i += 2) printf( %d , i); for (int i = 2; i <= n; i += 2) printf( %d , i); return 0; } |
#include <bits/stdc++.h> using namespace std; set<int> st[200000 + 7]; set<int>::iterator it; vector<int> graph[200000 + 7], edge[200000 + 7]; pair<int, int> edges[200000 + 7], path[200000 + 7]; int sid[200000 + 7], prio[200000 + 7], pr_id, can[200000 + 7], neg[200000 + 7], rev[200000 + 7]; long long weg[200000 + 7], uwg[200000 + 7]; void dfs(int u, int edg) { int v, ed; long long wg = 0; neg[u] = edg; for (int i = 0; i < graph[u].size(); i++) { v = graph[u][i]; ed = edge[u][i]; dfs(v, ed); wg += weg[v]; } if (u == 1) return; prio[u] = ++pr_id; rev[pr_id] = edg; sid[u] = u; st[u].insert(pr_id); can[edg] = min(1LL * edges[edg].first - 1, edges[edg].second - wg); if (can[edg] < 0) { puts( -1 ); exit(0); } weg[u] = wg + edges[edg].first - can[edg]; } void solve(int u) { int v, id1, id2; for (int i = 0; i < graph[u].size(); i++) { v = graph[u][i]; solve(v); uwg[u] += uwg[v]; id1 = sid[u]; id2 = sid[v]; if (st[id2].size() > st[id1].size()) swap(id1, id2); for (it = st[id2].begin(); it != st[id2].end(); it++) st[id1].insert(int(*it)); sid[u] = id1; } if (u == 1) return; int temp; while (uwg[u] > edges[neg[u]].second) { temp = *st[sid[u]].begin(); temp = rev[temp]; if (uwg[u] - can[temp] >= edges[neg[u]].second) { edges[temp].first -= can[temp]; edges[temp].second -= can[temp]; uwg[u] -= can[temp]; can[temp] = 0; st[sid[u]].erase(st[sid[u]].begin()); } else { int tpv = uwg[u] - edges[neg[u]].second; edges[temp].first -= tpv; edges[temp].second -= tpv; uwg[u] -= tpv; can[temp] -= tpv; } } uwg[u] += edges[neg[u]].first; } int main() { int n, u, v, w, p; scanf( %d , &n); for (int i = 1; i < n; i++) { scanf( %d %d , &u, &v); scanf( %d %d , &edges[i].first, &edges[i].second); edge[u].push_back(i); graph[u].push_back(v); path[i] = make_pair(u, v); } dfs(1, 0); solve(1); printf( %d n , n); for (int i = 1; i < n; i++) printf( %d %d %d %d n , path[i].first, path[i].second, edges[i].first, edges[i].second); return 0; } |
#include <bits/stdc++.h> using namespace std; inline long long gcd(long long x, long long y) { return x ? gcd(y % x, x) : y; } const long long mod = 1e9 + 7; inline signed rd() { signed ans = 0; char last = , ch = getchar(); while (!(ch >= 0 && ch <= 9 )) last = ch, ch = getchar(); while (ch >= 0 && ch <= 9 ) ans = ans * 10 + ch - 0 , ch = getchar(); if (last == - ) ans = -ans; return ans; } inline long long QPow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans; } inline long long QPow(long long a, long long b, const long long &mod) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; a = a * a % mod; b >>= 1; } return ans; } long long exgcd(long long a, long long b, long long &x, long long &y) { long long d = a; if (b == 0) x = 1, y = 0; else { d = exgcd(b, a % b, y, x), y -= a / b * x; } return d; } const long double PI = 3.141592653589793115997963468544185161590576171875; const long long maxn = 100005; struct Req { long long s, a, b; bool friend operator<(const Req &a, const Req &b) { return (a.b - a.a) > (b.b - b.a); } } reqs[maxn]; long long arr[maxn]; long long rarr[maxn]; long long cnt[maxn]; signed main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long t, m, n, R, s; long long k; cin >> n >> s; for (long long i = 1; i <= n; i++) cin >> reqs[i].s >> reqs[i].a >> reqs[i].b; long long ans = 0; long long sum = 0; for (long long i = 1; i <= n; i++) ans += reqs[i].a * reqs[i].s, sum += reqs[i].s; sort(reqs + 1, reqs + 1 + n); for (long long i = 1; i <= n; i++) arr[i] = reqs[i].b - reqs[i].a, cnt[i] = reqs[i].s; for (long long i = 1; i <= n; i++) rarr[i] = arr[n - i + 1]; signed id = 1; while (arr[id] > 0) id++; id--; long long tot = 0; long long tmp = ans; for (long long i = 1; i <= id; i++) { tot += cnt[i]; tmp += arr[i] * cnt[i]; } long long num = (tot + s - 1) / s; num *= s; long long tmp3 = tmp; long long re = sum % s; num -= tot; num -= (s - re) % s; for (long long i = id + 1; i <= n; i++) { if (num <= 0) break; long long tmp1 = min(num, cnt[i]); num -= tmp1; tmp3 += tmp1 * arr[i]; if (num <= 0) break; } num = (tot + s - 1) / s; num *= s; num -= s; long long tmp4 = ans; for (long long i = 1; i <= n; i++) { if (num <= 0) break; long long tmp2 = min(cnt[i], num); num -= tmp2; tmp4 += tmp2 * arr[i]; if (num == 0) break; } ans = max(ans, max(tmp4, tmp3)); cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, k, a, b, ii; int ans; char ch[1500][1500]; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; int dp[1500][1500]; int p[100007]; void dfs(int x, int y) { if (x > n || x < 1 || y > m || y < 1) return; if (ch[x][y] == * ) { ans++; return; } if (dp[x][y] != 0) return; dp[x][y] = ii; for (int i = 0; i < 4; i++) { int temp_x = x + dx[i]; int temp_y = y + dy[i]; dfs(temp_x, temp_y); } return; } int main() { cin >> n >> m >> k; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> ch[i][j]; } } for (ii = 1; ii <= k; ii++) { cin >> a >> b; ans = 0; if (!dp[a][b]) dfs(a, b); else ans = p[dp[a][b]]; p[ii] = ans; cout << ans << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a, b; cin >> n >> m; double mn = 101.00, x = 0.00; for (int i = 1; i <= n; i++) { cin >> a >> b; x = a / (b * 1.00); if (mn > x) mn = x; } cout << fixed << setprecision(8); cout << mn * m << endl; return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long int myRand(long long int B) { return (unsigned long long)rng() % B; } inline double time() { return static_cast<double>(chrono::duration_cast<chrono::nanoseconds>( chrono::steady_clock::now().time_since_epoch()) .count()) * 1e-9; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, m; cin >> n >> m; vector<string> s(n); for (int i = 0; i < n; i++) { cin >> s[i]; } vector<int> sum(m + 1); for (int i = 0; i + 1 < m; i++) { int u = 0; for (int j = 1; j < n; j++) { if (s[j][i] == X and s[j - 1][i + 1] == X ) u = 1; } sum[i + 1] = sum[i] + u; } int q; cin >> q; while (q--) { int x, y; cin >> x >> y; if (x == y) { printf( YES n ); continue; } if (sum[y - 1] - sum[x - 1] > 0) { printf( NO n ); continue; } else { printf( YES n ); continue; } } } |
#include <bits/stdc++.h> using namespace std; long long t, n, S, s, up, down, mid, mx, mn, a, b, k, l, r, ans, raod; pair<long long, long long> p[300005]; int main() { cin >> t; while (t--) { cin >> n >> S; mn = 1e10; mx = 0; for (k = 1; k <= n; k++) { cin >> a >> b; p[k].first = a; mn = min(mn, a); p[k].second = b; mx = max(mx, b); } ans = 0; sort(p + 1, p + n + 1); l = mn; r = mx; while (l <= r) { long long mid = (l + r + 1) / 2; up = 0; down = 0; s = 0; for (k = 1; k <= n; k++) if (p[k].first > mid) up++; else if (p[k].second < mid) down++; if (up >= n / 2 + 1) { l = mid + 1; } else if (down >= n / 2 + 1) r = mid - 1; else { raod = 0; for (k = n; k >= 1; k--) { if (raod <= n / 2 & p[k].second >= mid) { raod++; s += max(mid, p[k].first); } else s += p[k].first; } if (s <= S) { ans = mid; l = mid + 1; } else r = mid - 1; } } cout << ans << endl; } } |
#include <bits/stdc++.h> using namespace std; int const maxn = 100 * 1000 + 5; int a[maxn]; int b[maxn]; int main() { int t; cin >> t; int n, f1, fm1; for (int i = 0; i < t; i++) { f1 = maxn; fm1 = maxn; cin >> n; for (int j = 0; j < n; j++) { cin >> a[j]; if (a[j] == 1 && f1 == maxn) f1 = j; if (a[j] == -1 && fm1 == maxn) fm1 = j; } bool check = true; for (int j = 0; j < n; j++) { cin >> b[j]; } for (int j = n - 1; j >= 0; j--) { if (j > 0) { if (a[j] < b[j] && f1 >= j) check = false; if (a[j] > b[j] && fm1 >= j) check = false; } else { if (a[j] != b[j]) check = false; } if (!check) break; } if (check) cout << YES << endl; else cout << NO << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; void solve() { long long k, i, j, b = 1; string s = codeforces ; vector<int> a(10, 1); cin >> k; for (; b < k;) for (i = 0; i < 10; i++) { b /= a[i]; ++a[i]; b = b * a[i]; if (b > k - 1) break; } for (i = 0; i < 10; i++) for (j = 0; j < a[i]; j++) cout << s[i]; cout << n ; return; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; while (t--) solve(); } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; int dp[502][502][2], n, m; char str[502][502]; int dis; int check(int x, int y) { if (x < 0 or x >= n and y < 0 or y >= m) return 0; else return 1; } int call(int x, int y, int a, int idx) { int ham = x + y; int paisi = n - 1 - a; int baki = ham - paisi; int b = m - 1 - baki; if (check(x, y) == 0 or check(a, b) == 0) return dp[x][y][a & 1] = 0; if (str[x][y] != str[a][b]) return dp[x][y][a & 1] = 0; if (idx == 0) return dp[x][y][a & 1]; int dx = ((x - a) < 0 ? -(x - a) : (x - a)); int dy = ((y - b) < 0 ? -(y - b) : (y - b)); if (dis % 2 == 0) { if (ham + 1 > dis / 2) return dp[x][y][a & 1] = 0; if (ham + 1 == dis / 2) return dp[x][y][a & 1] = (dx + dy <= 1); } else { if (ham > dis / 2) return dp[x][y][a & 1] = 0; if (ham == dis / 2) return dp[x][y][a & 1] = (x == a and y == b); } long long ret = 0; ret += call(x + 1, y, a, 1 - idx); ret += call(x + 1, y, a - 1, 1 - idx); ret += call(x, y + 1, a, 1 - idx); ret += call(x, y + 1, a - 1, 1 - idx); return dp[x][y][a & 1] = ret % MOD; } int main() { scanf( %d %d , &n, &m); dis = n + m - 1; for (int i = 0; i < n; i++) scanf( %s , str[i]); for (int a = 0; a < n; a++) { for (int y = m - 1; y >= 0; y--) { int tempx = n - 1; int tempy = y; while (tempx >= 0 and tempy < m) { call(tempx, tempy, a, 1); tempy++; tempx--; } } for (int x = n - 2; x >= 0; x--) { int tempx = x; int tempy = 0; while (tempx >= 0 and tempy < m) { call(tempx, tempy, a, 1); tempy++; tempx--; } } } printf( %d n , dp[0][0][(n - 1) & 1]); } |
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e18; const long long mod = (long long)1e9 + 7; const double eps = (double)1e-9; const double pi = acos(-1.0); const int dx[] = {0, 0, 1, 0, -1}; const int dy[] = {0, 1, 0, -1, 0}; const int N = 500500; int n, k; vector<pair<int, int> > g[N]; long long dp[N], W[N]; void dfs(int v, int pr = -1) { dp[v] = 0; vector<long long> u; for (auto to : g[v]) { if (to.first == pr) continue; dfs(to.first, v); dp[v] += dp[to.first] + W[to.first]; if (-W[to.first] + to.second > 0) u.push_back(-W[to.first] + to.second); } sort(u.begin(), u.end()); reverse(u.begin(), u.end()); for (int i = 0; i < min(k - 1, (int)u.size()); ++i) dp[v] += u[i]; if (k <= (int)u.size()) W[v] = u[k - 1]; else W[v] = 0; } void solve() { cin >> n >> k; for (int i = 1; i <= n; ++i) g[i].clear(); for (int i = 1; i < n; ++i) { int x, y, w; cin >> x >> y >> w; g[x].push_back(make_pair(y, w)); g[y].push_back(make_pair(x, w)); } dfs(1); cout << dp[1] + W[1] << endl; } int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int T; cin >> T; while (T--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n; cin >> n; int count = 0; for (int a = 1; a <= n; a++) { for (int b = a; b <= n; b++) { int k = a * a + b * b; int c = sqrt(k); if (c * c == k && c <= n) count++; } } cout << count; return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) using namespace std; const int N = 500031; int tests, n, k; vector<pair<int, int> > g[N]; int used[N]; long long dp[N][2]; void dfs(int v) { used[v] = 1; long long guaranteed = 0; vector<long long> benefits; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i].first; int cost = g[v][i].second; if (used[to] == 1) continue; dfs(to); long long without_edge = max(dp[to][0], dp[to][1]); long long with_edge = dp[to][0] + cost; guaranteed += without_edge; long long benefit_here = with_edge - without_edge; if (benefit_here < 0) benefit_here = 0; benefits.push_back(benefit_here); } sort(benefits.begin(), benefits.end()); reverse(benefits.begin(), benefits.end()); long long S = 0; for (int i = 0; i < k - 1 && i < benefits.size(); i++) { S += benefits[i]; } dp[v][0] = guaranteed + S; S = 0; for (int i = 0; i < k && i < benefits.size(); i++) { S += benefits[i]; } dp[v][1] = guaranteed + S; } int main() { ios_base::sync_with_stdio(0); cin >> tests; for (; tests; --tests) { cin >> n >> k; for (int i = 1; i <= n; i++) { g[i].clear(); used[i] = 0; } for (int i = 1; i < n; i++) { int a, b, c; cin >> a >> b >> c; g[a].push_back(make_pair(b, c)); g[b].push_back(make_pair(a, c)); } dfs(1); cout << max(dp[1][0], dp[1][1]) << endl; } return 0; } |
#include <bits/stdc++.h> int map[525][525]; int note[10] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512}; int main() { int time = 0, size = 1, i, j, head, tail; map[1][1] = 1; while (time != 9) { time++; head = size + 1; tail = 2 * size; for (i = 1; i <= size; i++) for (j = head; j <= tail; j++) map[i][j] = map[i][j - size]; for (i = head; i <= tail; i++) for (j = 1; j <= size; j++) map[i][j] = map[i - size][j]; for (i = head; i <= tail; i++) for (j = head; j <= tail; j++) map[i][j] = -map[i - size][j - size]; size *= 2; } int k; scanf( %d , &k); for (i = 1; i <= note[k]; i++) { for (j = 1; j <= note[k]; j++) if (map[i][j] == 1) printf( + ); else printf( * ); puts( ); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tichnas; cin >> tichnas; while (tichnas--) { long long n, c, m, maxs = 1e9, temp, ans; map<long long, long long> type; bool possible; cin >> n; for (int i = 0; i < n; i++) { cin >> c; type[c]++; } for (auto it = type.begin(); it != type.end(); it++) maxs = min(maxs, it->second + 1); ans = n; for (int i = 2; i <= maxs; i++) { possible = true; m = 0; for (auto it = type.begin(); it != type.end() && possible; it++) { temp = it->second; if (temp % i == 0) m += temp / i; else if (i - 1 - temp % i <= temp / i) m += temp / i + 1; else possible = false; } if (possible) ans = m; } cout << ans << n ; } return 0; } |
#include<bits/stdc++.h> using namespace std; #define ll long long #define imin INT_MIN #define imax INT_MAX #define br n #define sp #define pie 3.141592653589793238462643383279 #define lower(sl) transform(sl.begin(), sl.end(), sl.begin(), ::tolower); #define upper(sl) transform(sl.begin(), sl.end(), sl.begin(), ::toupper); #define fo(i, n) for(int (i)=0;(i)<n;(i)++) #define rfo(i, n) for(int (i)=n-1;(i)>=0;(i)--) #define forl(i,a,n) for(int (i)=a;(i)<n;(i)++) #define forr(i,a,n) for(int (i)=n-1;(i)>=a;(i)--) #define tr(it, x) for(auto it = x.begin(); it != x.end(); it++) #define trr(it, x) for(auto it = x.rbegin(); it != x.rend(); it++) #define popcount(x) __builtin_popcount(x) // count the number of one’s(set bits) in an integer. #define popcountll(x) __builtin_popcountll(x) #define clz(x) __builtin_clz(x) // count the leading zeros(binary representation) of the integer #define clzll(x) __builtin_clzll(x) #define ctz(x) __builtin_ctz(x) // count the trailing zeros(binary representation) of the integer #define ctzll(x) __builtin_ctzll(x) #define pb push_back #define mp make_pair #define F first #define S second #define gcd __gcd #define lcm(a,b) ((a)*(b))/gcd(a,b) #define deci(n) fixed << setprecision(n) #define all(x) x.begin(),x.end() #define max3(a,b,c) max(max(a,b),c) #define min3(a,b,c) min(min(a,b),c) #define max4(a,b,c,d) max(a,max3(b,c,d)) #define min4(a,b,c,d) min(a,min3(b,c,d)) #define max5(a,b,c,d,e) max(max4(a,b,c,d),e) #define min5(a,b,c,d,e) min(min4(a,b,c,d),e) typedef pair<int, int> pii; typedef pair<ll, ll> pl; typedef pair<double, double> pd; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<pii> vpii; typedef vector<pl> vpl; typedef vector<vi> vvi; typedef vector<vl> vvl; typedef priority_queue<int, vector<int>, greater<int>> minpq; typedef priority_queue<int> pq; typedef unordered_map<int,int> umii; typedef map<int,int> mii; typedef set<int> si; typedef deque<int> di; typedef queue<int> qi; const int max_n = 1e6 + 1; const int mod = 1e9 + 7; const int inf = 1e9; const ll linf = 1e18; const int wmod = 998244353; int midofthree(int a,int b,int c){ if((a<=b and a>=c) or (a<=c and a>=b)) return a; else if((b>=a and b<=c) or (b<=a and b>=c)) return b; return c; } bool isPowerTwo(int x){ return (x && !(x & (x-1))); } bool ispalindrome(string s){ ll len=s.length(); fo(i, len){ if(s[i]!=s[len-i-1]) return false; } return true; } bool isSubstring(string s1, string s2){ if(s1.find(s2) != string::npos) return true; return false; } int sum_of_digits(int x){ int sum = 0; while (x > 0) { sum += x % 10; x /= 10; } return sum; } int num_of_digits(int x){ return floor(log10(x) + 1); } int binaryToDecimal(int n){ int num = n; int dec_value = 0; int base = 1; int temp = num; while (temp){ int last_digit = temp % 10; temp = temp / 10; dec_value += last_digit * base; base = base * 2; } return dec_value; } int isvowel(char x){ if (x == a || x == e || x == i || x == o || x == u ) return 1; return 0; } int isconsonant(char x){ if (x != a && x != e && x != i && x != o && x != u ) return 1; return 0; } // Check if n is a prime in O(sqrt(n/6)) bool isPrime(int n){ if(n<=1)return 0; if(n<=3)return 1; if(n%2==0 or n%3==0)return 0; for(int i=5;i*i<=n;i+=6){ if(n%i==0 or n%(i+2)==0)return 0; } return 1; } // Generate the all primes <= n in O(nlog(logn)) vi sieve(int n){ vi primes, isprime(n + 1, 1); for (int i = 2; i * i <= n; i++) if (isprime[i]) for (int j = i + i; j <= n; j += i) isprime[j] = 0; for (int i = 2; i <= n; i++) if (isprime[i]) primes.push_back(i); return primes; } vi factorize(int n){ vi res; for (int i = 2; i * i <= n; ++i){ while (n % i == 0){ res.push_back(i); n /= i; } } if (n != 1){ res.push_back(n); } return res; } // Iteratively finding (x^n)(mod M) (slightly faster) int modularExponentiation(int x,int n,int M){ int result=1; while(n>0){ if(n % 2 ==1){ result=(result * x)%M;} x=(x*x)%M; n = n>>1; } return result; } // calculationg exponential in logn int binaryExponentiation(int x,int n){ int result=1; while(n>0){ if(n % 2 ==1){ result=result * x;} x=x*x; n=n/2; } return result; } int modInverse(int a, int m){ int m0 = m,y = 0, x = 1; if (m == 1) return 0; while (a > 1) { int q = a / m; int t = m; m = a % m, a = t; t = y; y = x - q * y; x = t;} if (x < 0) x += m0; return x; } ll factorial(int n){ if(n==1 || n==0){ return 1; } else{ return n*factorial(n-1); } } #define MAX 1000 // Function to find the factorial of large number // and return them in string format string longfactorial(int n){ long long counter; long double sum = 0; if (n == 0) return 1 ; for (counter = 1; counter <= n; counter++) { sum = sum + log(counter); } string result = to_string(round(exp(sum))); return result; } ll NcR(int n,int m){ ll z=1; for(int i=0;i<m;i++) z=z*(n-i)/(i+1); return z; } void fillPrefixSum(int arr[], int n, int prefixSum[]){ prefixSum[0] = arr[0]; for (int i = 1; i < n; i++) prefixSum[i] = prefixSum[i - 1] + arr[i]; } /* ========== YOUR CODE HERE ========= */ void solution(){ // 0xAAAAAAAA=even bits set to 1 ; 0x55555555 = odd bits set to 1 // int prefixSuma[n]; // fillPrefixSum(a, n, prefixSuma); // int n; // cin>>n; // int a[n]; // fo(i,n){ // cin>>a[i]; // } int w,h,n; cin>>w>>h>>n; if(n==1){ cout<< YES <<br; } else{ int c=1,a=0; int t1=1, t2=1; while(w%2==0){ c = 2*c; w/=2; } while(h%2==0){ c*=2; h/=2; } if(c>=n){ cout<< YES <<br; } else{ cout<< NO <<br; } } } /* ========== YOUR CODE HERE ========= */ int main(){ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); int testcase = 1; cin>>testcase; while(testcase--) { solution(); } return 0; } // By Ashwani Kumar, Indian Institute Of Technology(IIT), Guwahati. |
#include <bits/stdc++.h> using namespace std; int pgcd(int a, int b) { if (a == 0) return b; return pgcd(b % a, a); } int nbNombres; int vals[300 * 1000]; int minRange[20][300 * 1000]; int pgcdRange[20][300 * 1000]; vector<int> possibles(int taille) { vector<int> sol; int etage = __builtin_log2(taille); int pseudo = (1 << etage); for (int iDebut = 0; iDebut + taille <= nbNombres; iDebut++) { int mini = min(minRange[etage][iDebut], minRange[etage][iDebut + taille - pseudo]); int pg = pgcd(pgcdRange[etage][iDebut], pgcdRange[etage][iDebut + taille - pseudo]); if (mini == pg) sol.push_back(iDebut); } return sol; } int main() { cin >> nbNombres; for (int iNombre = 0; iNombre < nbNombres; iNombre++) { cin >> vals[iNombre]; minRange[0][iNombre] = vals[iNombre]; pgcdRange[0][iNombre] = vals[iNombre]; } for (int iPuis = 1; iPuis < 20; iPuis++) { int curPuis = (1 << iPuis), derPuis = (1 << (iPuis - 1)); for (int iPos = 0; iPos + curPuis <= nbNombres; iPos++) { minRange[iPuis][iPos] = min(minRange[iPuis - 1][iPos], minRange[iPuis - 1][iPos + derPuis]); pgcdRange[iPuis][iPos] = pgcd(pgcdRange[iPuis - 1][iPos], pgcdRange[iPuis - 1][iPos + derPuis]); } } int debut = 0, fin = nbNombres + 1; while (debut + 1 < fin) { int mil = (debut + fin) / 2; if (!possibles(mil).empty()) { debut = mil; } else { fin = mil; } } vector<int> sol = possibles(debut); cout << sol.size() << << debut - 1 << endl; for (int val : sol) { cout << val + 1 << ; } cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, s, e; int x[5013]; int a[5013], b[5013], c[5013], d[5013]; long long dp[5013][5013]; int main() { scanf( %d%d%d , &n, &s, &e); for (int i = 1; i <= n; i++) scanf( %d , &x[i]); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int i = 1; i <= n; i++) scanf( %d , &b[i]); for (int i = 1; i <= n; i++) scanf( %d , &c[i]); for (int i = 1; i <= n; i++) scanf( %d , &d[i]); memset(dp, 0x7F, sizeof dp); dp[0][0] = 0; for (int i = 1; i <= n; i++) { int diff = 0; if (s < i && e >= i) diff = 1; if (e < i && s >= i) diff = -1; for (int j = 0; j <= n; j++) { int k = j - diff; if (k < 0) continue; if (i == s) { if (k) dp[i][j] = min(dp[i][j], dp[i - 1][j] + c[i] + x[i]); dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j] + d[i] - x[i]); } else if (i == e) { if (j) dp[i][j - 1] = min(dp[i][j - 1], dp[i - 1][j] + a[i] + x[i]); dp[i][j] = min(dp[i][j], dp[i - 1][j] + b[i] - x[i]); } else { if (j && k) dp[i][j - 1] = min(dp[i][j - 1], dp[i - 1][j] + c[i] + a[i] + 2 * x[i]); if (k) dp[i][j] = min(dp[i][j], dp[i - 1][j] + c[i] + b[i]); if (j) dp[i][j] = min(dp[i][j], dp[i - 1][j] + d[i] + a[i]); dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j] + d[i] + b[i] - 2 * x[i]); } } if (i == n) continue; diff = 0; if (s <= i && e > i) diff = 1; if (e <= i && s > i) diff = -1; for (int j = 0; j <= n; j++) { int k = j - diff; if (j <= 0 && k <= 0) dp[i][j] = 1e18; } } printf( %lld n , dp[n][0]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, f, g = 0, h = 0, i = 0, j, k, l, p = 0; cin >> a >> b >> c >> d >> e >> f; g = a - d; h = b - e; i = c - f; if (g > 0) { j = g / 2; } else { j = 0; } if (h > 0) { k = h / 2; } else { k = 0; } if (i > 0) { l = i / 2; } else { l = 0; } if (g >= 0) { p = p + j; } else { p = p + g; }; if (h >= 0) { p = p + k; } else { p = p + h; } if (i >= 0) { p = p + l; } else { p = p + i; } if (p >= 0) { cout << Yes ; } else { cout << No ; } return 0; } |
#include <bits/stdc++.h> int main() { int n, i, sum = 0; scanf( %d , &n); if (n == 1) sum = 1; if (n == 2) sum = 5; if (n == 3) sum = 13; if (n >= 4) { for (i = 4; i <= n; i++) { sum = sum + i * i - (i - 2) * (i - 2); } sum = sum + 13; } printf( %d , sum); } |
#include <bits/stdc++.h> typedef long long int ll; typedef long double ld; typedef unsigned long long ull; #define RESET(a, b) memset(a, b, sizeof(a)) int INF = 1000000005; long long INFF = 1000000000000000005LL; using namespace std; struct monster { ll health, attack_power; }; int main() { ll t; cin>>t; while(t--) { ll a,b; cin>>a>>b; ll ans=INFF; if(a==1) { if(b!=1) { cout<<1<<endl; } else { cout<< 2 n ; } continue; } if(b>a) { cout<< 1 n ; continue; } for(int i=0;i<=100000;i++) { ll x=b+i; ll y=a; ll cnt=i; bool ok=0; while(y and x!=1) { ok=1; y=y/x; cnt++; } //cout<<x<<endl; if(ok)ans=min(ans,cnt); } cout<<ans<<endl; } //ctime; return 0; } |
#include <bits/stdc++.h> using namespace std; int upd[400000][10]; long long val[400000][10]; bool dirty[400000]; void clean(int n, int l, int r) { long long buf[10]; memset(buf, 0, sizeof buf); for (int i = 0; i < 10; i++) { buf[upd[n][i]] += val[n][i]; } dirty[n] = false; for (int i = 0; i < 10; i++) { val[n][i] = buf[i]; } if (l < r) { dirty[n * 2] = dirty[n * 2 + 1] = true; for (int i = 0; i < 10; i++) { upd[n * 2][i] = upd[n][upd[n * 2][i]]; upd[n * 2 + 1][i] = upd[n][upd[n * 2 + 1][i]]; } } for (int i = 0; i < 10; i++) { upd[n][i] = i; } } long long get(int rl, int rr, int n = 1, int l = 0, int r = 100000) { if (l > rr || r < rl) return 0; if (dirty[n]) clean(n, l, r); if (l >= rl && r <= rr) { long long ans = 0; for (int i = 0; i < 10; i++) { ans += val[n][i] * i; } return ans; } long long ans = get(rl, rr, n * 2, l, (l + r) / 2); ans += get(rl, rr, n * 2 + 1, (l + r) / 2 + 1, r); return ans; } void update(int rl, int rr, int from, int to, int n = 1, int l = 0, int r = 100000) { if (l > rr || r < rl) { clean(n, l, r); return; } if (dirty[n]) clean(n, l, r); if (l >= rl && r <= rr) { for (int i = 0; i < 10; i++) { if (upd[n][i] == from) upd[n][i] = to; } dirty[n] = true; clean(n, l, r); return; } update(rl, rr, from, to, n * 2, l, (l + r) / 2); update(rl, rr, from, to, n * 2 + 1, (l + r) / 2 + 1, r); for (int i = 0; i < 10; i++) { val[n][i] = val[n * 2][i] + val[n * 2 + 1][i]; } } int n, q; int arr[100000]; void build(int n = 1, int l = 0, int r = 100000) { for (int i = 0; i < 10; i++) upd[n][i] = i; if (l == r) { if (l < n) { int mul = 1; int c = arr[l]; while (c) { val[n][c % 10] += mul; c /= 10; mul *= 10; } } return; } build(n * 2, l, (l + r) / 2); build(n * 2 + 1, (l + r) / 2 + 1, r); for (int i = 0; i < 10; i++) { val[n][i] = val[n * 2][i] + val[n * 2 + 1][i]; } } void printtree() { for (int i = 0; i < n; i++) { cout << get(i, i) << ; } cout << endl; } int main() { ios::sync_with_stdio(false); cin >> n >> q; for (int i = 0; i < n; i++) { cin >> arr[i]; } build(); for (int i = 0; i < q; i++) { int t, l, r, x, y; cin >> t; if (t == 1) { cin >> l >> r >> x >> y; update(l - 1, r - 1, x, y); } else { cin >> l >> r; cout << get(l - 1, r - 1) << endl; } } } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; string ans; string t; for (int i = 0; i < n; i++) { cin >> t; if (i == 0) { ans = t; continue; } for (int j = 0; j < t.size(); j++) if (ans[j] == ? ) ans[j] = t[j]; else if (ans[j] == t[j] || t[j] == ? ) ans[j] = ans[j]; else ans[j] = . ; } for (int i = 0; i < ans.size(); i++) if (ans[i] == . ) ans[i] = ? ; else if (ans[i] == ? ) ans[i] = a ; ios_base::sync_with_stdio(false); printf( %s , ans.c_str()); return 0; } |
#include <bits/stdc++.h> using namespace std; const int dr[] = {-1, 0, 1, 0, -1, -1, 1, 1}; const int dc[] = {0, -1, 0, 1, -1, 1, -1, 1}; const int N = 212345; int n, m, q, p[N], a[N]; int f[N], g[N], last[N]; vector<int> loc[N]; int maxm[N]; int my_find(int id, int val) { int l = 0, r = loc[id].size(); while (l < r) { int mid = l + (r - l) / 2; int x = loc[id][mid]; if (f[x] > val) r = mid; else l = mid + 1; } if (l == loc[id].size()) return m + 1; else return loc[id][l]; } int tmin[N * 4]; void build(int o, int l, int r) { if (l == r) tmin[o] = maxm[l]; else { int mid = l + (r - l) / 2; build(o * 2, l, mid); build(o * 2 + 1, mid + 1, r); tmin[o] = min(tmin[o * 2], tmin[o * 2 + 1]); } } const int INF = int(1e9); int query(int o, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) { return tmin[o]; } else { int mid = l + (r - l) / 2; int lmax = INF, rmax = INF; if (ql <= mid) lmax = query(o * 2, l, mid, ql, qr); if (qr > mid) rmax = query(o * 2 + 1, mid + 1, r, ql, qr); return min(lmax, rmax); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m >> q; for (int i = 1; i <= n; i++) { int x; cin >> x; p[x] = i; } for (int i = 1; i <= m; i++) { cin >> a[i]; a[i] = p[a[i]]; loc[a[i]].push_back(i); } for (int i = 1; i <= n; i++) sort((loc[i]).begin(), (loc[i]).end()); for (int i = 1; i <= m; i++) { last[a[i]] = i; if (a[i] == 1) f[i] = i; else f[i] = f[last[a[i] - 1]]; } for (int i = 1; i <= n; i++) last[i] = m + 1; g[m + 1] = m + 1; for (int i = m; i >= 1; i--) { last[a[i]] = i; if (a[i] == n) g[i] = i; else g[i] = g[last[a[i] + 1]]; } for (int i = 1; i <= m; i++) { if (a[i] == 1) maxm[i] = g[i]; else maxm[i] = my_find(a[i] - 1, g[i]); } build(1, 1, m); string ans; while (q--) { int l, r; cin >> l >> r; if (query(1, 1, m, l, r) <= r) ans.push_back( 1 ); else ans.push_back( 0 ); } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 2050; int p[N], id[N], rmd[N], P[N], n; int qry(vector<int> vec) { cout << ? << vec.size() << ; for (auto t : vec) cout << t << ; cout << endl, fflush(stdout); int ok; cin >> ok; return ok; } int query(int x) { vector<int> vec; for (int i = 1; i <= n; ++i) if (!p[i] && x != i) vec.push_back(i); return qry(vec); } void query(int x, int y, int xx, int yy) { for (int i = 1; i <= n; ++i) if (!id[x] && !p[i] && rmd[i] == y) { if (query(i)) id[x] = i; } else if (!id[xx] && !p[i] && rmd[i] == yy) if (query(i)) id[xx] = i; p[id[x]] = x, p[id[xx]] = xx; } int main() { cin >> n; query(1, 0, n, 0); for (int t = 2, l = 2, r = n - 1; l <= r; t <<= 1) { for (int i = 1; i <= n; ++i) if (!p[i]) { vector<int> vec; for (int j = 1; j <= t; ++j) if (j % t != rmd[i]) vec.push_back(id[j]); vec.push_back(i); if (qry(vec)) rmd[i] += t >> 1; } while (l <= 2 * t && l <= r) query(l, l % t, r, r % t), ++l, --r; } if (p[1] * 2 > n) for (int i = 1; i <= n; ++i) p[i] = n - p[i] + 1; cout << ! ; for (int i = 1; i <= n; ++i) cout << p[i] << ; cout << endl, fflush(stdout); return 0; } |
#include <bits/stdc++.h> const bool TEST = 0; const int inf = 0x7fffffff; using namespace std; template <typename T> void pA(T *begin, int n) { for (int i = 0; i < n; i++) { cout << *(begin++); } printf( n ); } const char cho[4] = { R , B , Y , G }; vector<unsigned int> fAns; bool flag = 0; bool check(string s, vector<unsigned int> ans) { if (flag) return 0; if (TEST) cout << s << endl; ; for (int c = 0; c < 4; c++) { int pre = -4; for (unsigned int i = 0; i < s.size(); i++) { if (s[i] == ! ) { if (c == 3) return 1; else continue; } if (s[i] == cho[c]) { if (i - pre >= 4) { pre = i; } else return false; } } } fAns = ans; flag = 1; return 0; } int cor(char x) { switch (x) { case R : return 0; case B : return 1; case Y : return 2; case G : return 3; } return 30; } void count(string s, vector<unsigned int> ans) { if (TEST) cout << int count << s << endl; int sum = 0; for (unsigned int i = 0; i < 4; i++) { sum += cor(s[i]); } if (sum != 6) return; for (unsigned int i = 4; i < s.size(); i++) { if (s[i] == ! ) { ans[cor(s[i - 4])]++; } } fAns = ans; } void dfs(unsigned int st, unsigned int curTol, const string s, vector<unsigned int> ans) { bool val = 0; val = check(s, ans); if (!val) return; if (st == 4) { count(s, ans); } if (TEST) cout << val << endl; for (unsigned int i = st; i < 4; i++) { if (s[i] == ! ) { for (int c = 0; c < 4; c++) { string s2 = s; s2[i] = cho[c]; ans[c]++; dfs(i + 1, curTol + 1, s2, ans); ans[c]--; } return; } } count(s, ans); } void sol() { string s; cin >> s; for (int i = 0; i < 4; i++) { fAns.push_back(0); } dfs(0, 0, s, fAns); for (int i = 0; i < 4; i++) { if (0 != i) cout << ; cout << fAns[i]; } cout << endl; } void sol2() { string s; cin >> s; for (int i = 0; i < 4; i++) { fAns.push_back(0); } for (int c = 0; c < 4; c++) { char x = cho[c]; unsigned int pos = s.find_first_of(x); pos %= 4; for (unsigned int i = pos; i < s.size(); i += 4) { if ( ! == s[i]) fAns[cor(x)]++; } } for (int i = 0; i < 4; i++) { if (0 != i) cout << ; cout << fAns[i]; } cout << endl; } int main() { sol2(); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mx = 1e5 + 10; long long n, m, a[mx], cnt[mx], r[mx], l[mx], p[mx], ans[mx]; int main() { cin >> n >> m; for (long long i = 1; i <= n; i++) { cin >> a[i]; if (a[i] < mx) cnt[a[i]]++; } for (long long i = 0; i < m; i++) cin >> l[i] >> r[i]; for (long long in = 1; in < mx; in++) { if (cnt[in] >= in) { p[0] = 0; for (long long i = 1; i <= n; i++) p[i] = p[i - 1] + (a[i] == in); for (long long i = 0; i < m; i++) ans[i] += ((p[r[i]] - p[l[i] - 1]) == in); } } for (long long i = 0; i < m; i++) cout << ans[i] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; char a[1000010]; char b[1000010]; int main() { cin >> a; int ind = 0; int ans = 0; int n = strlen(a); for (int i = 0; i < n; i++) { if (a[i] == [ || a[i] == < || a[i] == ( || a[i] == { ) { b[ind] = a[i]; ind++; } else { ind--; if (ind < 0) { cout << Impossible ; return 0; } if (b[ind] == [ && a[i] != ] ) { ans++; } else if (b[ind] == < && a[i] != > ) ans++; else if (b[ind] == { && a[i] != } ) ans++; else if (b[ind] == ( && a[i] != ) ) ans++; } } if (ind > 0) cout << Impossible ; else cout << ans; } |
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { x = 0; int f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == - ) f = -f; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); x *= f; } inline void print(int x) { if (x < 0) putchar( - ), x = -x; if (!x) return; print(x / 10), putchar((x % 10) ^ 48); } inline void write(int x) { if (!x) putchar( 0 ); else print(x); putchar( n ); } int n, ans[5], head[1000050], tot, mx[1000050], mx1[1000050], p, dep[1000050], d, vis[1000050], f[1000050]; struct edge { int to, nxt; } e[1000050 << 1]; void ins(int u, int v) { e[++tot] = (edge){v, head[u]}, head[u] = tot; } void dfs(int x, int fa) { dep[x] = dep[fa] + 1; int cnt = 0; f[x] = fa; for (int i = head[x]; i; i = e[i].nxt) if (e[i].to != fa && !vis[e[i].to]) { dfs(e[i].to, x), cnt++; if (!mx[x] || dep[mx[x]] < dep[mx[e[i].to]]) mx1[x] = mx[x], mx[x] = mx[e[i].to]; else if (dep[mx[e[i].to]] > dep[mx1[x]]) mx1[x] = mx[e[i].to]; } if (!cnt) mx[x] = x; if (cnt >= 2) { if (dep[x] > d) d = dep[x], p = x; else if (dep[x] == d && dep[mx1[x]] + dep[mx[x]] > dep[mx1[p]] + dep[mx[p]]) p = x; } } int main() { read(n); for (int i = 1, x, y; i < n; i++) read(x), read(y), ins(x, y), ins(y, x); dfs(1, 0), ans[1] = mx[p], ans[3] = mx1[p]; for (int i = head[p]; i; i = e[i].nxt) if (e[i].to != f[p] && (mx[e[i].to] == mx[p] || mx[e[i].to] == mx1[p])) vis[e[i].to] = 1; memset(mx, 0, sizeof mx), memset(mx1, 0, sizeof mx1); d = 0; dfs(p, 0), ans[2] = mx[p], ans[4] = mx1[p]; printf( %d %d n%d %d n , ans[1], ans[2], ans[3], ans[4]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string st[n]; int a[m]; for (int i = 0; i < n; i++) { cin >> st[i]; } for (int i = 0; i < m; i++) { cin >> a[i]; } int maxa = 0; int ca, cb, cc, cd, ce; for (int i = 0; i < m; i++) { ca = 0; cb = 0; cc = 0; cd = 0; ce = 0; for (int j = 0; j < n; j++) { if (st[j][i] == A ) ca++; if (st[j][i] == B ) cb++; if (st[j][i] == C ) cc++; if (st[j][i] == D ) cd++; if (st[j][i] == E ) ce++; } int x1 = max(ca, cb); int x2 = max(cc, cd); int x3 = max(max(x1, x2), ce); maxa += x3 * a[i]; } cout << maxa << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const double PI = 2.0 * acos(0.0); inline int getInt() { int x; scanf( %d , &x); return x; } inline long long getLL() { long long x; scanf( %lld , &x); return x; }; inline int NUM(char c) { return (int)c - 48; } inline char CHR(int n) { return (char)(n + 48); } template <class T> inline T MAX(T a, T b) { if (a > b) return a; return b; } template <class T> inline T MIN(T a, T b) { if (a < b) return a; return b; } inline void OPEN(string s) { freopen((s + .in ).c_str(), r , stdin); freopen((s + .out ).c_str(), w , stdout); } int N; int A[100000 + 5]; int sorted[100000 + 5]; map<int, stack<int> > MAP; int pos[100000 + 5]; int idx = -1; inline bool islucky(int x) { if (x == 0) return false; while (x) { if (x % 10 != 4 && x % 10 != 7) return false; x /= 10; } return true; } vector<pair<int, int> > sol; stack<int> tumpuk; bool found[100000 + 5]; inline int jelajah(int i) { found[i] = true; int root = i; while (pos[i] != root) { sol.push_back(make_pair(i, pos[i])); i = pos[i]; found[i] = true; } return i; } int main() { scanf( %d , &N); for (int i = 0, _n = (N); i < _n; i++) { scanf( %d , &A[i]); if (islucky(A[i])) idx = i; sorted[i] = A[i]; } sort(sorted, sorted + N); bool so = true; for (int i = 0, _n = (N); i < _n; i++) { if (A[i] != sorted[i]) { so = false; } } if (idx == -1 && !so) { puts( -1 ); return 0; } else if (idx == -1) { puts( 0 ); return 0; } for (int i = (N)-1; i >= 0; i--) { MAP[sorted[i]].push(i); } for (int i = 0, _n = (N); i < _n; i++) { int x = A[i]; A[i] = MAP[x].top(); MAP[x].pop(); pos[A[i]] = i; } idx = jelajah(idx); for (int i = 0, _n = (N); i < _n; i++) { if (!found[i]) { tumpuk.push(idx); sol.push_back(make_pair(idx, i)); idx = jelajah(i); } } while ((tumpuk).size() > 0) { sol.push_back(make_pair(idx, tumpuk.top())); idx = tumpuk.top(); tumpuk.pop(); } printf( %d n , (sol).size()); for (int i = 0, _n = ((sol).size()); i < _n; i++) { printf( %d %d n , sol[i].first + 1, sol[i].second + 1); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int TAILLE_MAX = 1000; int main() { int n, l, r; cin >> n >> l; int p[TAILLE_MAX]; for (int i = 0; i < n; i++) cin >> p[i]; sort(p, p + n); r = max(p[0], l - p[n - 1]) * 2; for (int i = 0; i < n - 1; i++) r = max(r, p[i + 1] - p[i]); cout.precision(20); cout << fixed << r / 2.0 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long n; long long k; long long a[200005]; long long s1; long long s2; long long inv[200005]; long long p; long long ans; long long quick_pow(long long x, long long n) { long long res = 1; while (n) { if (n & 1) res = res * x % 1000000007; x = x * x % 1000000007; n >>= 1; } return res; } int main() { scanf( %lld %lld , &n, &k); inv[1] = inv[0] = 1LL; for (long long i = 2; i <= n; i++) inv[i] = (1000000007 - 1000000007 / i) * inv[1000000007 % i] % 1000000007; for (long long i = 1; i <= n; ++i) inv[i] = inv[i - 1] * inv[i] % 1000000007; for (long long i = 1; i <= n; ++i) scanf( %lld , a + i); for (long long i = 0; i <= k; ++i) { long long fg = 1; if (i & 1) fg = -1; (s1 += fg * inv[i] * inv[k - i] % 1000000007 * quick_pow(k - i, n) % 1000000007) %= 1000000007; (s2 += fg * inv[i] * inv[k - i] % 1000000007 * quick_pow(k - i, n - 1) % 1000000007) %= 1000000007; } p = (s1 + s2 * (n - 1) % 1000000007) % 1000000007; p = (p + 1000000007) % 1000000007; for (long long i = 1; i <= n; ++i) ans = (ans + a[i] * p % 1000000007) % 1000000007; printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxN = (int)2E5 + 5; const long long mdls = (long long)1E9 + 7; vector<int> Adj[maxN]; vector<long long> suff[maxN], pref[maxN]; long long f[maxN], g[maxN], h[maxN]; int n; void DFS(int u) { f[u] = 1; for (int i = 0, sz = Adj[u].size(); i < sz; ++i) { int v = Adj[u][i]; DFS(v); f[u] = (f[u] * (f[v] + 1)) % mdls; } int sz = Adj[u].size(); suff[u].resize(sz); pref[u].resize(sz); long long temp = 1; for (int i = 0; i < sz; ++i) { temp = (temp * (f[Adj[u][i]] + 1)) % mdls; pref[u][i] = temp; } temp = 1; for (int i = sz - 1; i >= 0; --i) { temp = (temp * (f[Adj[u][i]] + 1)) % mdls; suff[u][i] = temp; } } void DFS2(int u) { for (int i = 0, sz = Adj[u].size(); i < sz; ++i) { int v = Adj[u][i]; h[v] = h[u]; if (i > 0) h[v] = (h[v] * pref[u][i - 1]) % mdls; if (i < sz - 1) h[v] = (h[v] * suff[u][i + 1]) % mdls; h[v] = (h[v] + 1) % mdls; g[v] = (f[v] * h[v]) % mdls; DFS2(v); } } int main() { scanf( %d , &n); for (int u = 1; u <= n; ++u) Adj[u].clear(); for (int v = 2; v <= n; ++v) { int u; scanf( %d , &u); Adj[u].push_back(v); } DFS(1); h[1] = 1; g[1] = f[1]; DFS2(1); for (int u = 1; u <= n; ++u) printf( %I64d , g[u]); printf( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; using LL = long long; template <class T> int size(T &&x) { return int(x.size()); } template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &p) { return out << ( << p.first << , << p.second << ) ; } template <class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << { ; for (auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? : , ); return out << } ; } void dump() {} template <class T, class... Args> void dump(T &&x, Args... args) { cerr << x << ; ; dump(args...); } mt19937_64 rng(0); int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int _degsize; cin >> _degsize; vector<int> d(_degsize); for (int &di : d) cin >> di; vector<pair<int, int>> edges; auto add_edge = [&](int v, int u) { assert(v != u); edges.emplace_back(v, u); }; while (size(d)) { 0 && cerr; int n = d.back() + 1; if (size(d) == 1) { for (int v = (0); v <= ((n)-1); ++v) for (int u = (0); u <= ((v)-1); ++u) add_edge(v, u); d.clear(); break; } int max_cnt = d.front(); for (int &di : d) di -= max_cnt; for (int v = (n - max_cnt); v <= (n - 1); ++v) for (int u = (0); u <= ((v)-1); ++u) add_edge(v, u); d.erase(d.begin()); d.erase(prev(d.end())); } cout << size(edges) << n ; for (auto [v, u] : edges) cout << v + 1 << << u + 1 << n ; } |
#include <bits/stdc++.h> using namespace std; const int mx = 5e5 + 10; const double eps = 1e-9; const int inf = 1e9; char a[mx]; multiset<char> ms; int main() { scanf( %s , a); int n = strlen(a); int mn = a[0] - a ; printf( Mike n ); for (int i = 1; i < n; i++) { if (a[i] - a > mn) { printf( Ann n ); } else printf( Mike n ); mn = min(mn, a[i] - a ); } } |
#include <bits/stdc++.h> using namespace std; int main() { int dem = 0; int n; int tong = 0; int s; cin >> n; for (int i = 9; i >= 1; i--) { if (n % i == 0) { s = i; break; } } int z; z = n / s; cout << z << endl; while (z) { cout << s << ; z--; } return 0; } |
#include <bits/stdc++.h> using namespace std; string removeZero(string s) { int i = 0, m = 0, j = s.size() - 1; if (s[i] == 0 ) { while (s[i] == 0 ) { i++; } } s.erase(0, i); if (s[j] == 0 ) { while (s[j] == 0 ) { m++; j--; } } s.erase(j + 1, s.size() - j); return s; } int main() { string s; cin >> s; s = removeZero(s); int x = 0, o = s.size() - 1, flag = 0; while (x <= s.size() - 1) { if (s[x] != s[o]) { flag = 1; cout << NO ; break; } x++; o--; } if (flag == 0) cout << YES ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const int maxm = 1e5 + 5; const long double eps = 1e-14; const long double eps2 = 1e-9; const long double INF = 1 << 30; inline int dcmp(const long double& x) { return fabs(x) < eps ? 0 : (x < 0 ? -1 : 1); } template <typename T> inline bool getmin(T& a, const T& b) { return dcmp(b - a) < 0 ? (a = b, true) : false; } template <typename T> inline bool getmax(T& a, const T& b) { return dcmp(b - a) > 0 ? (a = b, true) : false; } pair<long double, long double> operator+( const pair<long double, long double>& a, const pair<long double, long double>& b) { return {a.first + b.first, a.second + b.second}; } pair<long double, long double> operator-( const pair<long double, long double>& a, const pair<long double, long double>& b) { return {a.first - b.first, a.second - b.second}; } pair<long double, long double> operator*( const pair<long double, long double>& a, const long double& b) { return {a.first * b, a.second * b}; } long double dot(const pair<long double, long double>& a, const pair<long double, long double>& b) { return a.first * b.first + a.second * b.second; } long double cross(const pair<long double, long double>& a, const pair<long double, long double>& b) { return a.first * b.second - a.second * b.first; } long double abs(const pair<long double, long double>& a) { return sqrt(dot(a, a)); } struct Iron { int show, speed, st, ed; inline void read() { scanf( %d%d%d%d , &show, &speed, &st, &ed); st--, ed--; } } iron[maxm]; struct Iron2 { long double from, fromt, to, tot; }; struct Path { int id, from, to; }; struct Event { long double t; int segid; bool operator<(const Event& rhs) const { return dcmp(t - rhs.t) < 0; } }; int n, m; vector<int> G[maxn], chain[maxn]; int fa[maxn], dep[maxn], chainid[maxn], size[maxn]; int pre[maxn], dfs_clock; void dfs(int now) { vector<int>& vec = chain[chainid[now]]; for (int i = ((int)(vec.size())) - 1; i >= 0; i--) pre[vec[i]] = dfs_clock++; for (int i = 0; i < ((int)(vec.size())); i++) { int now = vec[i]; for (const int& to : G[now]) if ((!i || to != vec[i - 1]) && to != fa[now]) dfs(to); } } inline void make_HLD() { int first = 0, last = 0; int* q = new int[n]; q[last++] = 0; fa[0] = -1; dep[0] = 0; while (last - first > 0) { int now = q[first++]; for (const int& to : G[now]) if (to != fa[now]) { fa[to] = now; dep[to] = dep[now] + 1; q[last++] = to; } } for (int i = last - 1; i >= 0; i--) { int now = q[i], hs = -1; size[now] = 1; for (const int& to : G[now]) if (to != fa[now]) { size[now] += size[to]; if (hs < 0 || size[to] > size[hs]) hs = to; } if (hs < 0) chainid[now] = now; else chainid[now] = chainid[hs]; chain[chainid[now]].push_back(now); } dfs_clock = 0; delete[] q; dfs(0); } inline bool inrange(const long double& x, const long double& L, const long double& R) { return dcmp(x - L) >= 0 && dcmp(x - R) <= 0; } long double interPnt(pair<long double, long double> p1, pair<long double, long double> p2, pair<long double, long double> q1, pair<long double, long double> q2) { pair<long double, long double> d = p2 - p1, e = q2 - q1; long double crs = cross(d, e); if (abs(crs) < eps) { long double c2 = cross(d, q1 - p1); if (abs(c2) > eps) return INF; long double da2 = dot(d, d); long double f1 = dot(d, q1 - p1) / da2; long double f2 = dot(d, q2 - p1) / da2; if (f1 < -eps and f2 < -eps) return INF; if (f1 > 1 + eps and f2 > 1 + eps) return INF; long double f = max(0.l, min(f1, f2)); return (p1 + d * f).first; } long double f1 = cross(d, q1 - p1); long double f2 = cross(d, p1 - q2); long double f = f1 + f2; pair<long double, long double> x = q1 * (f2 / f) + q2 * (f1 / f); long double fp = f1 / f; long double gp = dot(x - p1, d) / dot(d, d); if (fp < -eps or fp > 1 + eps) return INF; if (gp < -eps or gp > 1 + eps) return INF; return x.first; } inline long double getIntersectime(const Iron2& A, const Iron2& B) { return interPnt(make_pair(A.fromt, A.from), make_pair(A.tot, A.to), make_pair(B.fromt, B.from), make_pair(B.tot, B.to)); const long double &x1 = A.from, &t1 = A.fromt, &x2 = A.to, &t2 = A.tot, &x3 = B.from, &t3 = B.fromt, &x4 = B.to, &t4 = B.tot; long double deno = (x2 - x1) * (t4 - t3) - (t2 - t1) * (x4 - x3); long double nume = (x3 - x1) * (t2 - t1) * (t4 - t3) - t3 * (t2 - t1) * (x4 - x3) + t1 * (t4 - t3) * (x2 - x1); long double ans; if (dcmp(deno) == 0) { if (dcmp((x4 - x3) / (t4 - t3) * (t1 - t3) + x3 - x1) == 0) ans = max(t1, t3); else ans = INF; } else ans = nume / deno; if (inrange(ans, max(t1, t3), min(t2, t4))) return ans; else return INF; } long double curtime; vector<Iron2> vec; struct cmp { inline long double getpos(const Iron2& I, const long double& t) const { return I.from + ((I.to - I.from) / (I.tot - I.fromt)) * (t - I.fromt); } bool operator()(const int& a, const int& b) const { long double d1 = dcmp(getpos(vec[a], curtime) - getpos(vec[b], curtime)), d2 = dcmp(getpos(vec[a], curtime + 1) - getpos(vec[b], curtime + 1)); return d1 < 0 || (d1 == 0 && d2 < 0) || (d1 == 0 && d2 == 0 && a < b); } }; vector<Iron2> Seg[maxn]; inline long double solve_path(const int& nowSeg) { vec = Seg[nowSeg]; set<int, cmp> second; vector<Event> eve; for (int i = 0; i < ((int)(vec.size())); i++) eve.push_back((Event){vec[i].fromt, i << 1}), eve.push_back((Event){vec[i].tot, i << 1 | 1}); sort(eve.begin(), eve.end()); long double ans = INF; for (const Event& e : eve) { curtime = e.t; if (dcmp(curtime - ans) >= 0) break; int id = e.segid; if (!(id & 1)) { id /= 2; set<int>::iterator it = second.lower_bound(id); assert(second.find(id) == second.end()); if (it != second.end()) getmin(ans, getIntersectime(vec[id], vec[*it])); if (it != second.begin()) it--, getmin(ans, getIntersectime(vec[id], vec[*it])); second.insert(id); } else { id /= 2; set<int>::iterator it = second.lower_bound(id), itL = it, itR = it; assert((*it) == id); itR++; if (itL != second.begin() && itR != second.end()) { itL--; getmin(ans, getIntersectime(vec[*itL], vec[*itR])); } second.erase(it); } } return ans; } vector<Path> path, L, R; inline void getPath(int a, int b) { L.clear(); R.clear(); while (chainid[a] != chainid[b]) { int atop = chain[chainid[a]].back(), btop = chain[chainid[b]].back(); if (dep[atop] > dep[btop]) L.push_back((Path){chainid[a], a, fa[atop]}), a = fa[atop]; else R.push_back((Path){chainid[b], fa[btop], b}), b = fa[btop]; } L.push_back((Path){chainid[a], a, b}); path.clear(); for (int i = 0; i < ((int)(L.size())); i++) path.push_back(L[i]); for (int i = ((int)(R.size())) - 1; i >= 0; i--) path.push_back(R[i]); } inline int PosOnPath(int id, int now) { if (chainid[now] != id) return 0; return pre[now] - pre[chain[id].back()] + 1; } inline long double solve_tree() { make_HLD(); for (int i = 0; i < m; i++) { getPath(iron[i].st, iron[i].ed); long double curt = iron[i].show, v = iron[i].speed; for (const Path& p : path) { int from = PosOnPath(p.id, p.from), to = PosOnPath(p.id, p.to); long double arrt = curt + abs(to - from) / v; long double cc = dcmp(arrt - curt) == 0 ? 0 : (to - from) / (arrt - curt); Seg[p.id].push_back( (Iron2){from - eps2 * cc, curt - eps2, to + eps2 * cc, arrt + eps2}); curt = arrt; } } long double res = INF; for (int i = 0; i < n; i++) getmin(res, solve_path(i)); return res; } int main() { scanf( %d%d , &n, &m); for (int i = 0, a, b; i < n - 1; i++) { scanf( %d%d , &a, &b); a--, b--; G[a].push_back(b); G[b].push_back(a); } for (int i = 0; i < m; i++) iron[i].read(); long double ans = solve_tree(); if (dcmp(ans - INF) >= 0) printf( -1 n ); else printf( %.13f n , (double)ans); } |
#include <bits/stdc++.h> using namespace std; const long long oo = 1e14; long long ab(long long x) { return x > 0 ? x : -x; } long long a[100005]; long long kn[66]; long long sum[100005]; map<long long, int> mp; int nxt[100005]; int main() { int n, k; scanf( %d%d , &n, &k); kn[0] = 1; int cnt = 0; for (int i = 1;; i++) { kn[i] = kn[i - 1] * k; if (kn[i] == 1) break; cnt++; if (ab(kn[i]) >= oo) break; } for (int i = 1; i <= n; i++) { scanf( %lld , &a[i]); sum[i] = sum[i - 1] + a[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j <= cnt; j++) { mp[sum[n] - sum[i] - kn[j]]++; } } long long ans = 0, step = 0; for (int i = n; i >= 1; i--) { ans += mp[step]; for (int j = 0; j <= cnt; j++) { mp[sum[n] - sum[i - 1] - kn[j]]--; } step += a[i]; } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e16; const long long int maxn = 1 << 20; double pie = 3.1415926535; string str[101]; bool done[101]; int main() { long long int n, i, j, cnt, offset; string s, s1, ans; cin >> n >> s; ans = ; for (i = 0; i < n; i++) ans += 9 ; for (i = 0; i < n; i++) { offset = 10 - (s[i] - 0 ); s1.clear(); for (j = i, cnt = 0; cnt < n; cnt++, j++) { long long int x = (s[j] - 0 + offset) % 10; s1 += (x + 0 ); if (j == n - 1) j = -1; } ans = min(ans, s1); } cout << ans << endl; } |
#include <bits/stdc++.h> using std::max; using std::min; const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; const long long INF = 0x3f3f3f3f3f3f3f3f; std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count()); template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) { return (!b) ? a : gcd(b, a % b); } template <typename _Tp> inline _Tp abs(const _Tp &a) { return a >= 0 ? a : -a; } template <typename _Tp> inline void chmax(_Tp &a, const _Tp &b) { (a < b) && (a = b); } template <typename _Tp> inline void chmin(_Tp &a, const _Tp &b) { (b < a) && (a = b); } template <typename _Tp> inline void read(_Tp &x) { char ch(getchar()); bool f(false); while (!isdigit(ch)) f |= ch == 45, ch = getchar(); x = ch & 15, ch = getchar(); while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); f && (x = -x); } template <typename _Tp, typename... Args> inline void read(_Tp &t, Args &...args) { read(t); read(args...); } inline int read_str(char *s) { char ch(getchar()); while (ch == || ch == r || ch == n ) ch = getchar(); char *tar = s; *tar = ch, ch = getchar(); while (ch != && ch != r && ch != n && ch != EOF) *(++tar) = ch, ch = getchar(); return tar - s + 1; } const int N = 3005; const int S = 12; const int mod = 998244353; template <typename _Tp1, typename _Tp2> inline void add(_Tp1 &a, _Tp2 b) { (a += b) >= mod && (a -= mod); } template <typename _Tp1, typename _Tp2> inline void sub(_Tp1 &a, _Tp2 b) { (a -= b) < 0 && (a += mod); } long long ksm(long long a, long long b = mod - 2) { long long res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod, b >>= 1; } return res; } int n, c; int a[N]; int A[N], B[N]; namespace sub1 { int dp[N][N / S + 5]; int f[N][N], cnt[N], ans[N]; void MAIN() { int maxx = n / c; for (int j = 1; j <= n; ++j) { memset(cnt, 0, sizeof(cnt)); int qwq = c - 1, coef = 1; if (!qwq) f[j][j] = 1; for (int i = j - 1; i >= 1; --i) { if (a[i] != a[j]) { if (!cnt[a[i]]) --qwq; else coef = 1ll * coef * B[cnt[a[i]]] % mod * A[cnt[a[i]] + 1] % mod; ++cnt[a[i]]; } if (!qwq) f[i][j] = coef; } } dp[0][0] = 1; for (int i = 1; i <= n; ++i) for (int j = 1, LIM = i / c; j <= LIM; ++j) for (int k = 1; k <= i; ++k) add(dp[i][j], 1ll * f[k][i] * dp[k - 1][j - 1] % mod); memset(cnt, 0, sizeof(cnt)); int qwq = c, coef = 1, qaq = 1; for (int i = n; i >= 0; --i) { for (int j = 0; j <= maxx; ++j) add(ans[j], 1ll * (qaq - (qwq ? 0 : coef) + mod) * dp[i][j] % mod); if (!cnt[a[i]]) --qwq; else coef = 1ll * coef * B[cnt[a[i]]] % mod * A[cnt[a[i]] + 1] % mod; ++cnt[a[i]]; add(qaq, qaq); } sub(ans[0], 1); for (int j = 0; j <= n; ++j) printf( %d%c , ans[j], n [j == n]); } } // namespace sub1 namespace sub2 { int dp[N][1 << S]; void MAIN() { int maxx = n / c; dp[0][0] = 1; for (int i = 0; i < n; ++i) { for (int j = i / c; j >= 0; --j) { for (int st = (1 << c) - 2; st >= 0; --st) { int to = st | 1 << (a[i + 1] - 1); if (to + 1 == (1 << c)) add(dp[j + 1][0], dp[j][st]); else add(dp[j][to], dp[j][st]); } } } for (int i = 0; i <= n; ++i) { int ans = 0; if (i <= maxx) for (int st = 0; st < 1 << c; ++st) add(ans, dp[i][st]); if (!i) sub(ans, 1); printf( %d%c , ans, n [i == n]); } } } // namespace sub2 int main() { for (int i = 1; i < N; ++i) A[i] = (A[i - 1] + A[i - 1] + 1) % mod, B[i] = ksm(A[i]); read(n, c); for (int i = 1; i <= n; ++i) read(a[i]); if (c > S) sub1::MAIN(); else sub2::MAIN(); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long inff = 0x3f3f3f3f3f3f3f3f; int main() { cin.tie(0); long long k, d, t, qw, e; scanf( %I64d , &k), scanf( %I64d , &d), scanf( %I64d , &t); qw = (k / d + 1) * d; if (k % d == 0) { cout << t << endl; return 0; } double s = 0.0, zz, po; po = (qw - k) * 0.5 + k; e = 2ll * t / (qw + k); e *= qw; s += e; e /= qw; zz = t - e * po; if (zz <= k) { s += zz; } else { s += k; zz -= k; s += zz * 2.0; } printf( %.10f n , s); return 0; } |
#include <bits/stdc++.h> using namespace std; void fff() { freopen( dravde.in , r , stdin); freopen( dravde.out , w , stdout); } const int MAXN = 100001; int n; map<int, int> m[MAXN]; int f[MAXN], s[MAXN], p[MAXN], cnt = 0, z; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { int v, c, l, r; scanf( %d%d%d%d , &v, &c, &l, &r); if (r + c < MAXN && (!l || m[l].find(r + c) != m[l].end())) { int u = m[l][r + c]; f[i] = f[u] + v, p[i] = u; if (!r && f[i] > f[z]) { z = i; } if (f[i] > f[m[l + c][r]]) { m[l + c][r] = i; } } } while (z) { s[cnt++] = z; z = p[z]; } printf( %d n , cnt); while (cnt--) { printf( %d , s[cnt]); } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, v; pair<int, int> p[10]; int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; ++i) { scanf( n ); for (int j = 1; j <= m; ++j) { char ch = getchar(); if (ch == * ) p[++v] = make_pair(i, j); } } if (p[1].first == p[2].first && p[1].second == p[3].second) printf( %d %d , p[3].first, p[2].second); else if (p[1].first == p[3].first && p[1].second == p[2].second) printf( %d %d , p[2].first, p[3].second); else if (p[2].first == p[1].first && p[2].second == p[3].second) printf( %d %d , p[3].first, p[1].second); else if (p[2].first == p[3].first && p[2].second == p[1].second) printf( %d %d , p[1].first, p[3].second); else if (p[3].first == p[1].first && p[3].second == p[2].second) printf( %d %d , p[2].first, p[1].second); else if (p[3].first == p[2].first && p[3].second == p[1].second) printf( %d %d , p[1].first, p[2].second); return 0; } |
#include <bits/stdc++.h> using namespace std; long long tests, t, qq, q, i, j, bb[200000], s1, ans[2100][2100]; long long a, b; long solve(long long a, long long b) { if (a < b) swap(a, b); if (a == 0 || b == 0) return 0; if (solve(a % b, b) == 0) return 1; t = a / b; if (b % 2 == 1) return 1 - t % 2; t %= (b + 1); if (t % 2 == 1) return 0; return 1; } int main() { long s = 0; cin >> tests; for (; tests; --tests) { cin >> a >> b; if (solve(a, b)) cout << First << endl; else cout << Second << endl; } cin.get(); cin.get(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 100500; using ll = long long; using point = pair<ll, ll>; ll& x(point& p) { return p.first; } ll& y(point& p) { return p.second; } int n; point p[N], p2[N]; set<point> s; point centerN, center2, invP; int no() { cout << NO << endl; return 0; } int main() { cin >> n; x(centerN) = 0; y(centerN) = 0; for (int i = 0; i < n; ++i) { cin >> x(p[i]) >> y(p[i]); x(p2[i]) = 2 * x(p[i]); y(p2[i]) = 2 * y(p[i]); x(centerN) += x(p[i]); y(centerN) += y(p[i]); } if ((2 * x(centerN)) % n != 0 || (2 * y(centerN)) % n != 0) { return no(); } x(center2) = 2 * x(centerN) / n; y(center2) = 2 * y(centerN) / n; for (int i = 0; i < n; ++i) { x(p2[i]) -= x(center2); y(p2[i]) -= y(center2); s.insert(p2[i]); } for (int i = 0; i < n; ++i) { x(invP) = -x(p2[i]); y(invP) = -y(p2[i]); auto it = s.find(invP); if (it == s.end()) { return no(); } } cout << YES << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, q; int kek[20]; long long a[696969]; void upd(int x, int v, int s = 0, int e = (1 << n) - 1, int d = n - 1, int p = 1) { if (s == e) { a[p] = v; return; } int m = (s + e) / 2; if (x <= m) { upd(x, v, s, m, d - 1, 2 * p + kek[d]); } else { upd(x, v, m + 1, e, d - 1, 2 * p + 1 - kek[d]); } a[p] = a[2 * p] + a[2 * p + 1]; } long long query(int s, int e, int ps = 0, int pe = (1 << n) - 1, int d = n - 1, int p = 1) { if (e < ps || pe < s) return 0; if (s <= ps && pe <= e) return a[p]; int pm = (ps + pe) / 2; return query(s, e, ps, pm, d - 1, 2 * p + kek[d]) + query(s, e, pm + 1, pe, d - 1, 2 * p + 1 - kek[d]); } int main() { scanf( %d %d , &n, &q); for (int i = 0; i < (1 << n); i++) { int x; scanf( %d , &x); upd(i, x); } while (q--) { int t; scanf( %d , &t); if (t == 1) { int x, v; scanf( %d %d , &x, &v); x--; upd(x, v); } if (t == 4) { int s, e; scanf( %d %d , &s, &e); printf( %lld n , query(s - 1, e - 1)); } if (t == 2) { int x; scanf( %d , &x); for (int i = 0; i < x; i++) kek[i] ^= 1; } if (t == 3) { int x; scanf( %d , &x); kek[x] ^= 1; } } } |
#include <bits/stdc++.h> using namespace std; struct event { long long int peakval; long long int pos; long long int time1; }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, m, d; cin >> n >> m >> d; struct event e1[m + 1]; e1[0].time1 = 0; for (long long int i = 1; i <= m; i++) { cin >> e1[i].pos >> e1[i].peakval >> e1[i].time1; } long long int dp[2][n + 1]; for (long long int i = 1; i <= n; i++) { dp[0][i] = 0; } deque<pair<long long int, long long int> > dq1; long long int delta, store; for (long long int i = 1; i <= m; i++) { delta = e1[i].time1 - (e1[i - 1].time1); delta *= d; for (long long int j = 1; j <= min(delta + 1, n); j++) { while (!dq1.empty() && dp[0][j] >= dq1.back().first) { dq1.pop_back(); if (dq1.empty()) { break; } } dq1.push_back({dp[0][j], j}); } for (long long int j = 1; j <= n; j++) { while (dq1.front().second < (j - delta)) { dq1.pop_front(); } store = e1[i].peakval - fabs(e1[i].pos - j); dp[1][j] = dq1.front().first + store; if ((j + delta + 1) <= n) { while (dp[0][j + delta + 1] >= dq1.back().first) { dq1.pop_back(); if (dq1.empty()) { break; } } dq1.push_back({dp[0][j + delta + 1], j + delta + 1}); } } for (long long int i = 1; i <= n; i++) { dp[0][i] = dp[1][i]; } dq1.clear(); } long long int ans = -1000000000000000007; for (long long int i = 1; i <= n; i++) { ans = max(ans, dp[1][i]); } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; class { public: int n, m; vector<pair<int, int> > V[5555]; bool vis[5555], in[5555]; int ans[5555]; void solve() { cin >> n >> m; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; V[u].push_back({v, i}); } for (int i = 1; i <= n; ++i) { if (!vis[i]) dfs(i); } if (two) cout << 2 << n ; else cout << 1 << n ; for (int i = 0; i < m; ++i) { cout << ans[i] + 1 << ; } } bool two = 0; void dfs(int u) { in[u] = 1; vis[u] = 1; for (auto v : V[u]) { if (!vis[v.first]) { dfs(v.first); } else if (in[v.first]) { ans[v.second] = 1; two = 1; } } in[u] = 0; } } NSPACE; int main() { ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); NSPACE.solve(); } |
#include <bits/stdc++.h> using namespace std; const int N = 1e9 + 7; void solve() { long long n, curr, val, ans = 0; cin >> n >> curr; vector<long long> a(n), cost(n); for (long long i = 0; i < n; i++) cin >> a[i]; cin >> val; for (long long i = 0; i < n; i++) cin >> cost[i]; multiset<long long> s; for (long long i = 0; i < n; i++) { s.insert(cost[i]); if (curr < a[i]) { long long x = (a[i] - curr + val - 1) / val; if (x > s.size()) { ans = -1; break; } while (x--) { ans += *s.begin(); curr += val; s.erase(s.begin()); } } } cout << ans; cout << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long int n; string s; vector<long long int> ss; inline bool check(long long int lim) { long long int sum = 0, cnt = 0; for (long long int i = 0; i < ss.size(); i++) { if (sum + ss[i] <= lim) { sum += ss[i]; } else { cnt++; sum = ss[i]; } } cnt++; if (cnt <= n) return true; return false; } int main() { cin >> n; cin.ignore(); getline(cin, s); long long int pos = 0; for (long long int i = 0; i < s.length(); i++) { if (s[i] == - || s[i] == ) { ss.push_back(i - pos + 1); pos = i + 1; } } ss.push_back(s.length() - pos); long long int low, high, mid; high = s.length(); low = -1; for (long long int i = 0; i < ss.size(); i++) low = max(low, ss[i]); long long int ans = high; while (low <= high) { mid = (low + high) / 2; if (check(mid)) { high = mid - 1; ans = min(ans, mid); } else low = mid + 1; } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int podch[1100]; int newp[1100]; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k, x; cin >> n >> k >> x; for (int i = 0; i < n; i++) { int tmp; cin >> tmp; podch[tmp]++; } for (int i = 0; i < k; i++) { int countwas = 0; for (int j = 0; j < 1100; j++) { newp[j] = 0; } for (int j = 0; j < 1100; j++) { int tmp = podch[j]; if (podch[j] % 2 != 0) { if (countwas % 2 == 0) { newp[(j ^ x)] += (podch[j] / 2 + 1); newp[j] -= (podch[j] / 2 + 1); } else { newp[(j ^ x)] += (podch[j] / 2); newp[j] -= (podch[j] / 2); } } else { newp[(j ^ x)] += (podch[j] / 2); newp[j] -= (podch[j] / 2); } countwas += tmp; } for (int j = 0; j < 1100; j++) { podch[j] += newp[j]; } } int kek = 0; for (int i = 0; i < 1100; i++) { if (podch[i]) { kek = i; break; } } int kek2 = 0; for (int i = 0; i < 1100; i++) { if (podch[i]) { kek2 = i; } } cout << kek2 << << kek << endl; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long add(long long x, long long y, long long CMOD = MOD) { return (x + y + CMOD + CMOD) % CMOD; } long long mult(long long x, long long y, long long CMOD = MOD) { return add(x, 0) * add(y, 0) % CMOD; } long long fast_expo(long long x, long long y, long long CMOD = MOD) { if (x == 0) return 0; if (y == 0) return 1; long long ans = fast_expo(x, y / 2, CMOD); ans = mult(ans, ans, CMOD); if (y & 1) ans = mult(ans, x, CMOD); return ans; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long gen(long long x, long long y) { if (x == y) return x; return x + rng() % (y - x + 1); } const int TAM = 1e5 + 100; const long long INF = LLONG_MAX / 4; int n; int main() { priority_queue<long long, vector<long long>, greater<long long> > pq; scanf( %d , &n); if (n % 2 == 0) pq.push(0); for (int i = 0; i < n; i++) { int x; scanf( %d , &x); pq.push(x); } long long ans = 0; while (pq.size() > 1) { long long x = 0; for (int i = 0; i < 3; i++) { x += pq.top(); pq.pop(); } ans += x; pq.push(x); } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; int b[n]; vector<int> c; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { if (!b[i]) c.push_back(a[i]); } sort(c.rbegin(), c.rend()); int j = 0; for (int i = 0; i < n; i++) { if (b[i]) cout << a[i] << ; else cout << c[j++] << ; } cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; char q[15][15], p[15][15], max_s[15], min_s[15], tmp[15], ans[15]; int np[15]; int main() { int i, j, k, n, carry, t, flag; while (scanf( %d %d , &n, &k) != EOF) { for (i = 0; i < n; ++i) scanf( %s , q + i); for (i = 0; i < k; ++i) { np[i] = i; ans[i] = 9 ; } ans[i] = 0 ; do { for (i = 0; i < k; ++i) { min_s[i] = 9 ; max_s[i] = 0 ; } min_s[i] = 0 ; max_s[i] = 0 ; for (i = 0; i < n; ++i) { for (j = 0; j < k; ++j) { p[i][j] = q[i][np[j]]; } p[i][j] = 0 ; if (strcmp(p[i], min_s) < 0) strcpy(min_s, p[i]); if (strcmp(p[i], max_s) > 0) strcpy(max_s, p[i]); } carry = 0; for (i = k - 1; i >= 0; --i) { t = max_s[i] - min_s[i] - carry; if (t < 0) { carry = 1; t += 10; } else carry = 0; tmp[i] = t + 0 ; } tmp[k] = 0 ; if (strcmp(tmp, ans) < 0) strcpy(ans, tmp); } while (next_permutation(np, np + k)); flag = 0; for (i = 0; i < k; ++i) { if (ans[i] == 0 ) { if (flag != 0) printf( %c , ans[i]); } else { if (flag == 0) flag = 1; printf( %c , ans[i]); } } if (flag == 0) printf( 0 n ); else printf( n ); } return 0; } |
#include <bits/stdc++.h> const int N = 100001; int i, n, m, g[N], v[N], b[N], res[N], c[N], a; int main() { scanf( %d%d , &n, &m); for (i = 1; i <= n; i++) scanf( %d , &g[i]); for (i = 1; i <= m; i++) scanf( %d , &v[i]); for (i = 1; i <= m; i++) scanf( %d , &b[i]); for (i = 1; i <= m; i++) { for (a = (v[i] + res[i - 1] - 1) % n + 1; b[i] > c[a]; a = g[a]) { if (c[a] == 0) res[i]++; c[a] = b[i]; b[i]--; } printf( %d n , res[i]); } } |
#include <bits/stdc++.h> using namespace std; struct var { char name[20], type[20]; } v[1010]; int vcnt; struct func { char name[20]; char para[50][20]; int paracnt; } f[1010]; int fcnt; int n; char str[110], name[110], para[110][110], cnt; int main() { scanf( %d , &fcnt); gets(str); for (int i = 1; i <= fcnt; ++i) { gets(str); int j = 0, l = 0, len = strlen(str); while (str[j] != d ) ++j; ++j; while (!isalpha(str[j]) && !isdigit(str[j])) ++j; while (isalpha(str[j]) || isdigit(str[j])) f[i].name[l++] = str[j++]; f[i].name[l] = 0 ; j++; while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j; while (j < len) { l = 0; f[i].paracnt++; while (isalpha(str[j]) || isdigit(str[j])) f[i].para[f[i].paracnt][l++] = str[j++]; f[i].para[f[i].paracnt][l] = 0 ; while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j; } } scanf( %d , &vcnt); gets(str); for (int i = 1; i <= vcnt; ++i) { gets(str); int j = 0, len = strlen(str), l = 0; while (!isalpha(str[j]) && !isdigit(str[j])) ++j; while (isdigit(str[j]) || isalpha(str[j])) v[i].type[l++] = str[j++]; v[i].type[l] = 0 ; while (!isalpha(str[j]) && !isdigit(str[j])) ++j; l = 0; while (isdigit(str[j]) || isalpha(str[j])) v[i].name[l++] = str[j++]; v[i].name[l] = 0 ; } scanf( %d , &n); gets(str); while (n--) { gets(str); int j = 0, l = 0, len = strlen(str); while (!isalpha(str[j]) && !isdigit(str[j])) ++j; while (isalpha(str[j]) || isdigit(str[j])) name[l++] = str[j++]; name[l] = 0 ; j++; cnt = 0; while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j; while (j < len) { l = 0; cnt++; while (isalpha(str[j]) || isdigit(str[j])) para[cnt][l++] = str[j++]; para[cnt][l] = 0 ; while (!isalpha(str[j]) && !isdigit(str[j]) && (j < len)) ++j; } bool flag = true; for (int i = 1; i <= cnt; ++i) { bool find = false; for (int j = 1; j <= vcnt; ++j) if (!strcmp(para[i], v[j].name)) { strcpy(para[i], v[j].type); find = true; break; } if (!find) { flag = false; break; } } if (!flag) { printf( 0 n ); continue; } int res = 0; for (int i = 1; i <= fcnt; ++i) if ((strcmp(f[i].name, name) == 0) && (cnt == f[i].paracnt)) { bool okay = true; for (int j = 1; j <= cnt; ++j) if (strcmp(f[i].para[j], T ) && strcmp(f[i].para[j], para[j])) { okay = false; break; } res += okay; } printf( %d n , res); } return 0; } |
#include <bits/stdc++.h> using namespace std; int nxt[500005], to[500005], head[500005], fa[500005], f[500005][22], dep[500005], ru[500005], tot; bool vis[500005], used[500005]; queue<int> q; vector<int> ei[500005]; void add(int u, int v) { nxt[++tot] = head[u]; head[u] = tot; to[tot] = v; nxt[++tot] = head[v]; head[v] = tot; to[tot] = u; } void dfs(int u, int p) { dep[u] = dep[p] + 1; f[u][0] = p; for (int i = 1; i <= 20; i++) f[u][i] = f[f[u][i - 1]][i - 1]; for (int e = head[u]; e; e = nxt[e]) { int v = to[e]; if (v == p) continue; dfs(v, u); } } int lca(int x, int y) { if (dep[x] < dep[y]) swap(x, y); for (int i = 20; i >= 0; i--) { if (dep[f[x][i]] >= dep[y]) { x = f[x][i]; } } if (x == y) return x; for (int i = 20; i >= 0; i--) { if (f[x][i] != f[y][i]) { x = f[x][i]; y = f[y][i]; } } return f[x][0]; } int find(int x) { if (x == fa[x]) return x; return fa[x] = find(fa[x]); } struct sad { int x, y; } a[500005]; int main() { int n, x, y, u, v; cin >> n; for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= n - 1; i++) { scanf( %d%d , &x, &y); add(x, y); } for (int i = 1; i <= n - 1; i++) { scanf( %d%d , &a[i].x, &a[i].y); ru[a[i].x]++; ru[a[i].y]++; ei[a[i].x].push_back(i); ei[a[i].y].push_back(i); } dfs(1, 0); for (int i = 1; i <= n - 1; i++) { if (ru[a[i].x] == 1 || ru[a[i].y] == 1) { used[i] = 1; q.push(i); } } int ans = 0; cout << n - 1 << endl; while (!q.empty()) { int eg = q.front(); q.pop(); u = a[eg].x; v = a[eg].y; if (ru[u] != 1) swap(u, v); int z = lca(u, v); int p = find(u); if (dep[p] > dep[z]) { ans++; fa[p] = find(f[p][0]); printf( %d %d %d %d n , p, f[p][0], a[eg].x, a[eg].y); } else { int t = v; for (int i = 20; i >= 0; i--) if (dep[f[t][i]] > dep[z] && find(f[t][i]) != p) t = f[t][i]; fa[t] = find(f[t][0]); printf( %d %d %d %d n , t, f[t][0], a[eg].x, a[eg].y); } ru[u]--; ru[v]--; if (ru[v] == 1) { int mm = ei[v].size(); for (int i = 0; i < mm; i++) { int ey = ei[v][i]; if (used[ey] == 0) q.push(ey), used[ey] = 1; } } } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long inf = 0x3f3f3f3f; const long long MAXN = 0x7fffffff; const long long INF = 0x3f3f3f3f3f3f3f3fLL; void file() {} void pr(bool num) { cout << (num == 1 ? YES : NO ) << n ; } signed main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); ; long long t, n; cin >> t; while (t--) { long long a, b, c; cin >> a >> b >> c; long long ans = min(a, b); ans = min(ans, c); a -= ans; b -= ans; c -= ans; long long num = min(a, b); a -= num; b -= num; long long sum = a + b + c; if (num <= sum) ans += num; else { ans += sum; num -= sum; ans += num / 3 * 2; if (num % 3 == 2) ans++; } cout << ans << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-9; const long double PI = 3.141592653589793238462643383279502884197; const int MOD = 666013; const int NMax = 350000; int N, C, D; int lastIndexForSearch; long long ans = 0LL; struct Punct; long long diferente[NMax], diferentePartiale[NMax]; long long sqr(const long long &x); long long Determinant(const Punct &A, const Punct &B, const Punct &C); long long Dist2(const Punct &A, const Punct &B); long double GetAngle(const Punct &A); int GetCadran(const Punct &A); long long CompareUnghiPolar(const Punct &A, const Punct &B); struct Punct { int x, y; Punct() { this->x = 0; this->y = 0; } Punct(const int x, const int y) { this->x = x; this->y = y; } inline bool operator<(const Punct &other) const { long long s = CompareUnghiPolar(*this, other); if (s == 0) return Dist2(Punct(0, 0), *this) < Dist2(Punct(0, 0), other); return s == -1; } }; Punct a[NMax]; enum InsertType { InsertCoord, InsertCntStrict, InsertCntNestrict, InsertIndexNestrict }; enum QueryType { QueryCoord, QueryCntStrict, QueryCntNestrict, QueryIndexNestrict }; struct HashNode { int x, y, indexNestrict; long long cntStrict, cntNestrict; HashNode() { x = y = indexNestrict = 0; cntStrict = cntNestrict = 0LL; } HashNode(const int &x, const int &y, const int &indexNestrict, const long long &cntStrict, const long long &cntNestrict) { this->x = x; this->y = y; this->indexNestrict = indexNestrict; this->cntStrict = cntStrict; this->cntNestrict = cntNestrict; } }; vector<HashNode> H[MOD]; struct CntEvidence { int x, y; long long cnt; CntEvidence() { x = y = 0; cnt = 0LL; } CntEvidence(const int &x, const int &y, const long long &cnt) { this->x = x; this->y = y; this->cnt = cnt; } inline bool operator<(const CntEvidence &other) const { long long s = CompareUnghiPolar(Punct(this->x, this->y), Punct(other.x, other.y)); if (s == 0) return Dist2(Punct(0, 0), Punct(this->x, this->y)) < Dist2(Punct(0, 0), Punct(other.x, other.y)); return s == -1; } }; CntEvidence maiMiciStrictVector[NMax]; CntEvidence maiMiciStrictVectorSumePartiale[NMax]; CntEvidence maiMiciSauEgaleVector[NMax]; CntEvidence maiMiciSauEgaleVectorSumePartiale[NMax]; int NmaiMiciStrictVector; int NmaiMiciSauEgaleVector; int gcd(int x, int y) { int r; while (y) { r = x % y; x = y; y = r; } return x; } void Insert(const int &x, const int &y, InsertType type, long long param = 0) { int codh = (abs(x) + abs(y)) % MOD; if (type == InsertCoord) { for (vector<HashNode>::iterator it = H[codh].begin(); it != H[codh].end(); ++it) { if (it->x == x && it->y == y) return; } H[codh].push_back(HashNode(x, y, 0, 0, 0)); return; } else if (type == InsertCntStrict) { for (vector<HashNode>::iterator it = H[codh].begin(); it != H[codh].end(); ++it) { if (it->x == x && it->y == y) { it->cntStrict = param; return; } } H[codh].push_back(HashNode(x, y, 0, param, 0)); return; } else if (type == InsertCntNestrict) { for (vector<HashNode>::iterator it = H[codh].begin(); it != H[codh].end(); ++it) { if (it->x == x && it->y == y) { it->cntNestrict = param; return; } } H[codh].push_back(HashNode(x, y, 0, 0, param)); return; } else if (type == InsertIndexNestrict) { for (vector<HashNode>::iterator it = H[codh].begin(); it != H[codh].end(); ++it) { if (it->x == x && it->y == y) { it->indexNestrict = param; return; } } H[codh].push_back(HashNode(x, y, param, 0, 0)); return; } } void FindMaiMiciStrict(int x, int y) { int st = 1, dr = lastIndexForSearch; int ret = 0; while (st <= dr) { int mij = (st + dr) / 2; if (CompareUnghiPolar(a[mij], Punct(x, y)) < 0) { ret = mij; st = mij + 1; } else dr = mij - 1; } Insert(x, y, InsertCntStrict, ret); maiMiciStrictVector[++NmaiMiciStrictVector] = CntEvidence(x, y, ret); } void FindMaiMiciSauEgale(int x, int y) { int st = 1, dr = lastIndexForSearch; int ret = 0; while (st <= dr) { int mij = (st + dr) / 2; if (CompareUnghiPolar(a[mij], Punct(x, y)) <= 0) { ret = mij; st = mij + 1; } else dr = mij - 1; } Insert(x, y, InsertCntNestrict, ret); maiMiciSauEgaleVector[++NmaiMiciSauEgaleVector] = CntEvidence(x, y, ret); } long long QueryHash(int x, int y, QueryType type) { x = -x; y = -y; if (y == 0) { if (x < 0) x = -1; else if (x > 0) x = 1; } else if (x == 0) { if (y < 0) y = -1; else if (y > 0) y = 1; } else { int semnx = 1; int semny = 1; if (x < 0) { x = -x; semnx = -1; } if (y < 0) { y = -y; semny = -1; } int cmmdc = gcd(x, y); x = x / cmmdc; y = y / cmmdc; x = x * semnx; y = y * semny; } int codh = (abs(x) + abs(y)) % MOD; for (vector<HashNode>::iterator it = H[codh].begin(); it != H[codh].end(); ++it) { if (it->x == x && it->y == y) { if (type == QueryCntStrict) return it->cntStrict; if (type == QueryCntNestrict) return it->cntNestrict; if (type == QueryIndexNestrict) return it->indexNestrict; } } return -1; } void Solve() { int loc; loc = 0; for (int i = 1; i <= N; ++i) { if (a[i].y > 0 || (a[i].y == 0 && a[i].x > 0)) { ++loc; swap(a[i], a[loc]); } } sort(a + 1, a + loc + 1); sort(a + loc + 1, a + N + 1); lastIndexForSearch = -1; NmaiMiciStrictVector = 0; NmaiMiciSauEgaleVector = 0; int specialIndex = -1; for (int i = 1; i <= N; ++i) { if (GetAngle(a[i]) >= PI) { if (a[i].y == 0) { Insert(1, 0, InsertCoord); FindMaiMiciStrict(1, 0); FindMaiMiciSauEgale(1, 0); continue; } if (a[i].x == 0) { Insert(0, 1, InsertCoord); FindMaiMiciStrict(0, 1); FindMaiMiciSauEgale(0, 1); continue; } int x, y; x = -a[i].x; y = -a[i].y; int semnx = 1, semny = 1; if (x < 0) { x = -x; semnx = -1; } if (y < 0) { y = -y; semny = -1; } int cmmdc = gcd(x, y); x = x / cmmdc; y = y / cmmdc; x = x * semnx; y = y * semny; Insert(x, y, InsertCoord); FindMaiMiciStrict(x, y); FindMaiMiciSauEgale(x, y); } else { lastIndexForSearch = i; } } bool foundOnRightOX = false; specialIndex = NmaiMiciSauEgaleVector; for (int i = 1; i <= N; ++i) { if (a[i].y == 0 && a[i].x > 0) { foundOnRightOX = true; Insert(-1, 0, InsertCoord); FindMaiMiciStrict(-1, 0); FindMaiMiciSauEgale(-1, 0); } } loc = 0; for (int i = 1; i <= NmaiMiciStrictVector; ++i) { if (maiMiciStrictVector[i].y > 0 || (maiMiciStrictVector[i].y == 0 && maiMiciStrictVector[i].x > 0)) { ++loc; swap(maiMiciStrictVector[i], maiMiciStrictVector[loc]); } } sort(maiMiciStrictVector + 1, maiMiciStrictVector + loc + 1); sort(maiMiciStrictVector + loc + 1, maiMiciStrictVector + NmaiMiciStrictVector + 1); for (int i = 1; i <= NmaiMiciStrictVector; ++i) { maiMiciStrictVectorSumePartiale[i].x = maiMiciStrictVector[i].x; maiMiciStrictVectorSumePartiale[i].y = maiMiciStrictVector[i].y; maiMiciStrictVectorSumePartiale[i].cnt = ((i > 1) ? (maiMiciStrictVectorSumePartiale[i - 1].cnt) : (0)) + maiMiciStrictVector[i].cnt; } loc = 0; for (int i = 1; i <= NmaiMiciSauEgaleVector; ++i) { if (maiMiciSauEgaleVector[i].y > 0 || (maiMiciSauEgaleVector[i].y == 0 && maiMiciSauEgaleVector[i].x > 0)) { ++loc; swap(maiMiciSauEgaleVector[i], maiMiciSauEgaleVector[loc]); } } sort(maiMiciSauEgaleVector + 1, maiMiciSauEgaleVector + loc + 1); sort(maiMiciSauEgaleVector + loc + 1, maiMiciSauEgaleVector + NmaiMiciSauEgaleVector + 1); for (int i = 1; i <= NmaiMiciSauEgaleVector; ++i) { maiMiciSauEgaleVectorSumePartiale[i].x = maiMiciSauEgaleVector[i].x; maiMiciSauEgaleVectorSumePartiale[i].y = maiMiciSauEgaleVector[i].y; maiMiciSauEgaleVectorSumePartiale[i].cnt = ((i > 1) ? (maiMiciSauEgaleVectorSumePartiale[i - 1].cnt) : (0)) + maiMiciSauEgaleVector[i].cnt; } for (int i = 1; i <= NmaiMiciSauEgaleVector; ++i) { int index = i; int x = maiMiciSauEgaleVectorSumePartiale[i].x; int y = maiMiciSauEgaleVectorSumePartiale[i].y; Insert(x, y, InsertIndexNestrict, index); } for (int i = 1; i <= NmaiMiciSauEgaleVector; ++i) diferente[i] = max(0LL, maiMiciSauEgaleVector[i].cnt - maiMiciStrictVector[i].cnt); for (int i = 1; i <= NmaiMiciSauEgaleVector; ++i) diferentePartiale[i] = diferentePartiale[i - 1] + diferente[i]; if (lastIndexForSearch != -1 && NmaiMiciSauEgaleVector != 0 && NmaiMiciStrictVector != 0) { for (int i = lastIndexForSearch + 1; i <= N; ++i) { if (a[i].y == 0 && a[i].x < 0) { int index; if (foundOnRightOX) { index = specialIndex; } else index = NmaiMiciSauEgaleVector; if (index <= 0) continue; long long total = maiMiciSauEgaleVectorSumePartiale[index].cnt; long long total2; int jndex = QueryHash(a[i].x, a[i].y, QueryIndexNestrict); if (jndex == -1) continue; total2 = maiMiciSauEgaleVectorSumePartiale[jndex].cnt + 1LL * (index - jndex) * maiMiciSauEgaleVector[jndex].cnt; long long inPlus = diferentePartiale[index]; if (foundOnRightOX) inPlus = inPlus - diferentePartiale[jndex]; ans += max(0LL, total - total2 - inPlus); } else { int index; index = NmaiMiciSauEgaleVector; if (index <= 0) continue; long long total = maiMiciSauEgaleVectorSumePartiale[index].cnt; long long total2; int jndex = QueryHash(a[i].x, a[i].y, QueryIndexNestrict); if (jndex == -1) continue; total2 = maiMiciSauEgaleVectorSumePartiale[jndex].cnt + 1LL * (index - jndex) * maiMiciSauEgaleVector[jndex].cnt; long long inPlus = diferentePartiale[index]; inPlus = inPlus - diferentePartiale[jndex]; ans += max(0LL, total - total2 - inPlus); } } } } void EliminaDuplicate() { int maiMici180, cnt; long long total; maiMici180 = 0; total = 0LL; for (int i = 1; i <= N; ++i) { int x = a[i].x, y = a[i].y; if (y > 0 || (y == 0 && x > 0)) ++maiMici180; } for (int i = 1; i <= N; ++i) { if (a[i].y < 0) { int x = -a[i].x; int y = -a[i].y; int st = 1, dr = maiMici180; int val = 0; while (st <= dr) { int mij = (st + dr) / 2; if (Determinant(Punct(0, 0), Punct(x, y), a[mij]) <= 0) { val = mij; st = mij + 1; } else dr = mij - 1; } total += max(0, maiMici180 - val); } } cnt = 0; for (int i = 1; i <= N; ++i) if (a[i].y == 0 && a[i].x > 0) ++cnt; ans = ans - 1LL * total * cnt; } int main() { std::ios_base::sync_with_stdio(false); cin >> N >> C >> D; for (int i = 1; i <= N; ++i) { int x, y; cin >> x >> y; a[i] = Punct(x - C, y - D); } Solve(); EliminaDuplicate(); for (int i = 0; i < MOD; ++i) H[i].clear(); for (int i = 1; i <= N; ++i) { a[i].x = -a[i].x; a[i].y = -a[i].y; } Solve(); EliminaDuplicate(); cout << ans << n ; return 0; } long long sqr(const long long &x) { return x * x; } long long Determinant(const Punct &A, const Punct &B, const Punct &C) { return 1LL * (A.x - C.x) * (B.y - C.y) - 1LL * (A.y - C.y) * (B.x - C.x); } long long Dist2(const Punct &A, const Punct &B) { return sqr(A.x - B.x) + sqr(A.y - B.y); } int GetCadran(const Punct &A) { if (A.x > 0 && A.y > 0) return 1; if (A.x < 0 && A.y > 0) return 2; if (A.x < 0 && A.y < 0) return 3; return 4; } long double GetAngle(const Punct &A) { long double ret = atan2(A.y, A.x); if (A.y < 0) ret = ret + PI * 2.0; return ret; } long long CompareUnghiPolar(const Punct &A, const Punct &B) { long long ret = Determinant(Punct(0, 0), A, B); if (ret > 0) return -1; if (ret < 0) return 1; if (A.y == 0 && B.y == 0) { if ((A.x > 0 && B.x > 0) || (A.x < 0 && B.x < 0)) return 0; if (A.x > 0 && B.x < 0) return -1; return 1; } if (A.x == 0 && B.x == 0) { if ((A.y > 0 && B.y > 0) || (A.y < 0 && B.y < 0)) return 0; if (A.y > 0 && B.y < 0) return -1; return 1; } int semnxA, semnxB, semnyA, semnyB; semnxA = semnxB = semnyA = semnyB = 1; if (A.x < 0) semnxA = -1; if (B.x < 0) semnxB = -1; if (A.y < 0) semnyA = -1; if (B.y < 0) semnyB = -1; if (semnxA == semnxB && semnyA == semnyB) return 0; if (semnyA == 1) return -1; return 1; } |
#include <bits/stdc++.h> using namespace std; const int N = 577, INF = 1e9, mod = 1e9 + 7; int n, m; long long h; int a[N][N], b[N][N]; int ans; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf( %d , &a[i][j]); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf( %d , &b[i][j]); if (b[i][j] != a[i][j]) { b[1][1] = !b[1][1]; b[i][1] = !b[i][1]; b[1][j] = !b[1][j]; b[i][j] = !b[i][j]; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (b[i][j] != a[i][j]) { printf( No n ); return 0; } } } printf( Yes n ); return 0; } |
#include <iostream> #define MAX 200010 using namespace std; long long int t, n, A[MAX], DP[MAX][105], res, a; int main() { cin >> t; for (int i = 0; i <= MAX - 3; i++) { DP[i][0] = 1; if (i <= 100) DP[i][i] = 1; } for (int i = 1; i <= MAX - 3; i++) { for (int j = 1; j <= min(i, 100); j++) { DP[i][j] = DP[i - 1][j] + DP[i - 1][j - 1]; } } while (t--) { cin >> n; res = 0; for (int i = 1; i <= n; i++) A[i] = 0; for (int i = 1; i <= n; i++) { cin >> a; A[a]++; } for (int i = 1; i <= n; i++) { res += A[i] * DP[A[i - 1] + A[i - 2]][2]; res += DP[A[i]][2] * DP[A[i - 1] + A[i - 2]][1]; res += DP[A[i]][3]; } cout << res << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; long long int mod = 1000000007; void dispvec(set<long long int> arr) { set<long long int>::iterator itr = arr.begin(); while (itr != arr.end()) { cout << *itr << ; itr++; } cout << endl; } struct CustomCompare { bool operator()(const pair<long long int, long long int> &i, const pair<long long int, long long int> &j) { if (i.first == j.first) return i.first < j.first; return i.second < j.second; } }; void solve() { int n; cin >> n; vector<long long int> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; long long int sum = arr[0] + 1; int curr = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] < arr[i - 1]) { sum += arr[i - 1] - arr[i]; } else if (arr[i] > arr[i - 1]) { sum += arr[i] - arr[i - 1]; } sum += 2; } cout << sum << endl; } int main() { int t = 1; int i = 1; while (i <= t) { solve(); i++; } return 0; } |
#include <bits/stdc++.h> using namespace std; int num[300005][2]; int jichu = 100000; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, x; cin >> n >> m; if (n == 1) { cout << 0 << endl; return 0; } long long cont = 0, sum = n; for (int i = 1; i <= n; i++) { num[jichu + i][0] = 1; num[jichu + i][1] = 1; } for (int i = 1; i <= m; i++) { cin >> x; int l = x - i, r = x + i; if (num[jichu + l][1] > 0) { cont += 1LL * num[jichu + l][1]; num[jichu + l - 1][1] += num[jichu + l][1]; num[jichu + l][1] = 0; } if (num[jichu + r][0] > 0) { cont += 1LL * num[jichu + r][0]; num[jichu + r + 1][0] += num[jichu + r][0]; num[jichu + r][0] = 0; } } for (int i = jichu + 1 - m; i < jichu + n; i++) { if (i + m + 1 > jichu + n) { sum += 1LL * num[i][1] * ((m + 1) - (i + m + 1 - jichu - n)); } else { sum += 1LL * num[i][1] * (m + 1); } } for (int i = jichu + 2; i <= jichu + n + m; i++) { if (i <= jichu + m + 1) { sum += 1LL * num[i][0] * (i - jichu - 1); } else { sum += 1LL * num[i][0] * (m + 1); } } sum -= cont; cout << sum << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long fpow(long long a, int b, int mod) { long long res = 1; for (; b > 0; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res % mod; } const int mod = 1e9 + 7; const int N = 2e5 + 100; inline int read() { static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; register bool sgn = false; while (ch != - && (ch < 0 || ch > 9 )) ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; if (ch == - ) sgn = true, ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; while (ch >= 0 && ch <= 9 ) x = (x << 1) + (x << 3) + (ch ^ 48), ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; return sgn ? -x : x; } int mon[N]; int d[N]; int main() { int t; cin >> t; while (t--) { int n, m; scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &mon[i]); scanf( %d , &m); for (int i = 1; i <= m; i++) { int a, b; scanf( %d%d , &a, &b); d[b] = max(a, d[b]); } for (int i = n; i >= 0; i--) d[i] = max(d[i + 1], d[i]); int ans = 0; for (int i = 1; i <= n;) { if (mon[i] > d[1]) { cout << -1 << endl; goto brk; } int j = 0; int MX = 0; for (; j <= n; j++) { MX = max(MX, mon[i + j]); if (d[j + 1] < MX) break; } ans++; i += j; } cout << ans << endl; brk:; for (int i = 0; i <= n; i++) d[i] = 0; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long t, n, max_gcd = 1; cin >> t; while (t--) { cin >> n; cout << n / 2 << endl; } return 0; } |
#include <bits/stdc++.h> const long long iinf = 1e18; const int inf = 1e9 + 10; const int MOD = (1e6 + 3); int dir4[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; int knight[8][2] = {{2, 1}, {2, -1}, {-2, 1}, {-2, -1}, {1, 2}, {-1, 2}, {1, -2}, {-1, -2}}; using namespace std; void solve() { int n; cin >> n; int ans = 1; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { ans = i; break; } } if (ans == 1) { cout << 1 << << n - 1 << n ; return; } int a = n / ans; cout << a << << (ans - 1) * a << n ; } int main() { int t = 1; cin >> t; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m; bool a = true; int main() { scanf( %d%d , &n, &m); int arr[m]; if (m % 2 == 0) { arr[0] = (m) / 2; for (int i = 1; i < m; i++) { if (!a) { arr[i] = arr[i - 1] - i; a = !a; } else { arr[i] = arr[i - 1] + i; a = !a; } } } else { arr[0] = (m + 1) / 2; for (int i = 1; i < m; i++) { if (!a) { arr[i] = arr[i - 1] + i; a = !a; } else { arr[i] = arr[i - 1] - i; a = !a; } } } for (int i = 0; i < n; i++) { printf( %d n , arr[i % m]); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; map<int, int> mp; map<int, int> np; for (int i = 1; i <= n; i++) { int x, y; cin >> x >> y; int p = y - x; int q = y + x; mp[p]++; np[q]++; } map<int, int>::iterator it; int box = 0; for (it = mp.begin(); it != mp.end(); it++) { int g = it->second; box += (g * (g - 1)) / 2; } for (it = np.begin(); it != np.end(); it++) { int g = it->second; box += (g * (g - 1)) / 2; } cout << box << endl; } |
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > vc; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long N, M, x, y, z, p, i, X, Y, j, lplp1, lplp2; cin >> N >> M >> x >> y >> z >> p; long long opop1 = x % 4; long long opop2 = y % 2; long long opop3 = z % 4; for (i = 1; i <= p; i++) { cin >> X >> Y; vc.push_back(make_pair(X, Y)); } for (i = 1; i <= opop1; i++) { for (j = 0; j <= (int)vc.size() - 1; j++) { lplp1 = vc[j].first; lplp2 = vc[j].second; vc[j].first = vc[j].second; vc[j].second = N - lplp1 + 1; } swap(N, M); } for (i = 1; i <= opop2; i++) { for (j = 0; j <= (int)vc.size() - 1; j++) { lplp1 = vc[j].first; lplp2 = vc[j].second; vc[j].second = M - lplp2 + 1; } } for (i = 1; i <= opop3; i++) { for (j = 0; j <= (int)vc.size() - 1; j++) { lplp1 = vc[j].first; lplp2 = vc[j].second; vc[j].first = M - lplp2 + 1; vc[j].second = lplp1; } swap(N, M); } for (i = 0; i <= (int)vc.size() - 1; i++) { cout << vc[i].first << << vc[i].second << n ; } return 0; } |
#include <bits/stdc++.h> int solve(int m, int n) { if (m == 1 || n == 1) return m * n; if (m == 2 || n == 2) { if (m < n) { m = n; n = 2; } if (m % 4 == 0) return m; else if (m % 4 == 1) return m + 1; else if (m % 4 == 2) return m + 2; else return m + 1; } if (m % 2 == 1 && n % 2 == 1) return m * n / 2 + 1; return m * n / 2; } int main() { int m, n; scanf( %d %d , &m, &n); printf( %d , solve(m, n)); } |
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << { ; string sep; for (const auto &x : v) os << sep << x, sep = , ; return os << } ; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << ( << p.first << , << p.second << ) ; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << << H; dbg_out(T...); } template <const unsigned &MOD> struct _m_uint { unsigned val; _m_uint(int64_t v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = v; } static unsigned inv_mod(unsigned a, unsigned m = MOD) { unsigned g = m, r = a, x = 0, y = 1; int sign = -1; while (r != 0) { unsigned q = g / r; g %= r; swap(g, r); x += q * y; swap(x, y); sign = -sign; } return sign > 0 ? x : m - x; } explicit operator unsigned() const { return val; } explicit operator uint64_t() const { return val; } _m_uint &operator+=(const _m_uint &other) { val = val < MOD - other.val ? val + other.val : val - (MOD - other.val); return *this; } _m_uint &operator-=(const _m_uint &other) { val = val < other.val ? val + (MOD - other.val) : val - other.val; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { return x % m; unsigned x_high = x >> 32, x_low = (unsigned)x; unsigned quot, rem; asm( divl %4 n : =a (quot), =d (rem) : d (x_high), a (x_low), r (m)); return rem; } _m_uint &operator*=(const _m_uint &other) { val = fast_mod((uint64_t)val * other.val); return *this; } _m_uint &operator/=(const _m_uint &other) { return *this *= other.inv(); } friend _m_uint operator+(const _m_uint &a, const _m_uint &b) { return _m_uint(a) += b; } friend _m_uint operator-(const _m_uint &a, const _m_uint &b) { return _m_uint(a) -= b; } friend _m_uint operator*(const _m_uint &a, const _m_uint &b) { return _m_uint(a) *= b; } friend _m_uint operator/(const _m_uint &a, const _m_uint &b) { return _m_uint(a) /= b; } _m_uint &operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } _m_uint &operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } _m_uint operator++(int) { _m_uint before = *this; ++*this; return before; } _m_uint operator--(int) { _m_uint before = *this; --*this; return before; } _m_uint operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const _m_uint &other) const { return val == other.val; } bool operator!=(const _m_uint &other) const { return val != other.val; } _m_uint inv() const { return inv_mod(val); } _m_uint pow(int64_t p) const { if (p < 0) return inv().pow(-p); _m_uint a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; a *= a; p >>= 1; } return result; } friend ostream &operator<<(ostream &os, const _m_uint &m) { return os << m.val; } }; auto random_address = [] { char *p = new char; delete p; return (uint64_t)p; }; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1)); extern const unsigned HASH_P = (unsigned)-13337; using hash_int = _m_uint<HASH_P>; const uint64_t HASH_P2 = (uint64_t)HASH_P * HASH_P; const int HASH_COUNT = 2; uniform_int_distribution<unsigned> MULT_DIST(0.1 * HASH_P, 0.9 * HASH_P); const hash_int HASH_MULT[HASH_COUNT] = {MULT_DIST(rng), MULT_DIST(rng)}; const hash_int HASH_INV[HASH_COUNT] = {1 / HASH_MULT[0], 1 / HASH_MULT[1]}; vector<hash_int> hash_pow[HASH_COUNT] = {{1}, {1}}; const int INF = 1e9 + 5; template <typename T_string = string> struct string_hash { static const bool BUILD_REVERSE = false; T_string str; vector<hash_int> _prefix_hash[HASH_COUNT]; vector<hash_int> _prefix_inv_hash[HASH_COUNT]; string_hash() { build({}); } string_hash(const T_string &_str) { build(_str); } int length() const { return (int)_prefix_hash[0].size() - 1; } template <typename T_char> void add_char(const T_char &c) { str.push_back(c); for (int h = 0; h < HASH_COUNT; h++) { _prefix_hash[h].push_back(HASH_MULT[h] * _prefix_hash[h].back() + c); if (hash_pow[h].size() < _prefix_hash[h].size()) hash_pow[h].push_back(HASH_MULT[h] * hash_pow[h].back()); if (BUILD_REVERSE) _prefix_inv_hash[h].push_back((_prefix_inv_hash[h].back() + c) * HASH_INV[h]); } } void pop_char() { str.pop_back(); for (int h = 0; h < HASH_COUNT; h++) { _prefix_hash[h].pop_back(); if (BUILD_REVERSE) _prefix_inv_hash[h].pop_back(); } } void build(const T_string &_str) { str = {}; for (int h = 0; h < HASH_COUNT; h++) { _prefix_hash[h] = {0}; if (BUILD_REVERSE) _prefix_inv_hash[h] = {0}; } for (auto &c : _str) add_char(c); } uint64_t _single_hash(int h, int start, int end) const { uint64_t power = (uint64_t)hash_pow[h][end - start]; return (power + (uint64_t)_prefix_hash[h][end] + HASH_P2 - (uint64_t)_prefix_hash[h][start] * power) % HASH_P; } uint64_t substring_hash(int start, int end) const { assert(0 <= start && start <= end && end <= length()); return _single_hash(0, start, end) + (HASH_COUNT > 1 ? _single_hash(1, start, end) << 32 : 0); } uint64_t _reverse_single_hash(int h, int start, int end) const { uint64_t power = (uint64_t)hash_pow[h][end - start]; return (power + (uint64_t)_prefix_inv_hash[h][end] * power + HASH_P - (uint64_t)_prefix_inv_hash[h][start]) % HASH_P; } uint64_t reverse_substring_hash(int start, int end) const { assert(0 <= start && start <= end && end <= length()); return _reverse_single_hash(0, start, end) + (HASH_COUNT > 1 ? _reverse_single_hash(1, start, end) << 32 : 0); } bool equal(int start1, int start2, int length) const { return substring_hash(start1, start1 + length) == substring_hash(start2, start2 + length); } bool is_palindrome(int start, int end) const { return substring_hash(start, end) == reverse_substring_hash(start, end); } int compare(int start1, int start2, int max_length = INF) const; }; template <typename T_string> int first_mismatch(const string_hash<T_string> &hash1, int start1, const string_hash<T_string> &hash2, int start2, int max_length = INF) { max_length = min({max_length, hash1.length() - start1, hash2.length() - start2}); static const int FIRST = 5; int first = min(max_length, FIRST); for (int i = 0; i < first; i++) if (hash1.str[start1 + i] != hash2.str[start2 + i]) return i; if (hash1.substring_hash(start1, start1 + max_length) == hash2.substring_hash(start2, start2 + max_length)) return max_length; static const int MANUAL = 15; int low = first, high = max_length - 1; while (high - low > MANUAL) { int mid = (low + high + 1) / 2; if (hash1.substring_hash(start1, start1 + mid) == hash2.substring_hash(start2, start2 + mid)) low = mid; else high = mid - 1; } for (int i = low; i < high; i++) if (hash1.str[start1 + i] != hash2.str[start2 + i]) return i; return high; } template <typename T_string> int hash_compare(const string_hash<T_string> &hash1, int start1, const string_hash<T_string> &hash2, int start2, int max_length = INF) { int mismatch = first_mismatch(hash1, start1, hash2, start2, max_length); int length1 = min(hash1.length() - start1, max_length); int length2 = min(hash2.length() - start2, max_length); if (mismatch == min(length1, length2)) return length1 == length2 ? 0 : (length1 < length2 ? -1 : +1); if (hash1.str[start1 + mismatch] == hash2.str[start2 + mismatch]) return 0; return hash1.str[start1 + mismatch] < hash2.str[start2 + mismatch] ? -1 : +1; } template <typename T_string> int string_hash<T_string>::compare(int start1, int start2, int max_length) const { return hash_compare(*this, start1, *this, start2, max_length); } using seq_hash = string_hash<vector<int>>; int N, bucket_size, n_buckets; vector<int> S; vector<bool> invalid_bucket; vector<seq_hash> bucket_prefix, bucket_suffix; int which_bucket(int index) { return index < N ? index / bucket_size : n_buckets; } int get_bucket_start(int b) { return bucket_size * b; } int get_bucket_end(int b) { return min(bucket_size * (b + 1), N); } vector<int> process_stack(int start, int end) { vector<int> stack; for (int i = start; i < end; i++) if (!stack.empty() && stack.back() > 0 && S[i] == -stack.back()) stack.pop_back(); else stack.push_back(S[i]); return stack; } void build_bucket(int b) { assert(0 <= b && b < n_buckets); int bucket_start = get_bucket_start(b); int bucket_end = get_bucket_end(b); vector<int> stack = process_stack(bucket_start, bucket_end); int size = stack.size(); int prefix = 0, suffix = size; while (prefix < size && stack[prefix] < 0) prefix++; while (suffix > 0 && stack[suffix - 1] > 0) suffix--; if (prefix != suffix) { invalid_bucket[b] = true; return; } invalid_bucket[b] = false; bucket_prefix[b] = {}; for (int i = prefix - 1; i >= 0; i--) bucket_prefix[b].add_char(abs(stack[i])); bucket_suffix[b] = {}; for (int i = suffix; i < size; i++) bucket_suffix[b].add_char(stack[i]); } void init_buckets() { bucket_size = sqrt(N) + 1; n_buckets = (N + bucket_size - 1) / bucket_size; invalid_bucket.assign(n_buckets, false); bucket_prefix.assign(n_buckets, {}); bucket_suffix.assign(n_buckets, {}); for (int b = 0; b < n_buckets; b++) build_bucket(b); } void update(int index, int change) { assert(0 <= index && index < N); S[index] = change; build_bucket(which_bucket(index)); } struct range { const seq_hash *hasher = nullptr; int start = 0, end = 0; range() {} range(const seq_hash &_hasher, int _start, int _end) : hasher(&_hasher), start(_start), end(_end) {} int length() const { return end - start; } }; bool query(int start, int end) { assert(0 <= start && start <= end && end <= N); int start_b = which_bucket(start), end_b = which_bucket(end); if (start_b == end_b) return process_stack(start, end).empty(); vector<range> ranges; vector<int> stack = process_stack(start, get_bucket_end(start_b)); ; for (int x : stack) if (x < 0) return false; seq_hash start_hasher(stack); ranges.emplace_back(start_hasher, 0, start_hasher.length()); for (int b = start_b + 1; b < end_b; b++) { if (invalid_bucket[b]) return false; ; int prefix_end = bucket_prefix[b].length(); while (prefix_end > 0) { if (ranges.empty()) return false; range &back = ranges.back(); int take = min(prefix_end, back.length()); uint64_t prefix_hash = bucket_prefix[b].substring_hash(prefix_end - take, prefix_end); uint64_t range_hash = back.hasher->substring_hash(back.end - take, back.end); if (prefix_hash != range_hash) return false; prefix_end -= take; back.end -= take; if (back.length() == 0) ranges.pop_back(); } assert(prefix_end == 0); ranges.emplace_back(bucket_suffix[b], 0, bucket_suffix[b].length()); } stack = process_stack(get_bucket_start(end_b), end); ; for (int x : stack) if (x > 0) return false; for (int x : stack) { while (!ranges.empty() && ranges.back().length() == 0) ranges.pop_back(); if (ranges.empty()) return false; range &back = ranges.back(); if (back.hasher->str[back.end - 1] != abs(x)) return false; back.end--; } while (!ranges.empty() && ranges.back().length() == 0) ranges.pop_back(); return ranges.empty(); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int _; cin >> N >> _; S.resize(N); for (auto &s : S) cin >> s; init_buckets(); int Q; cin >> Q; for (int q = 0; q < Q; q++) { int type; cin >> type; if (type == 1) { int index, t; cin >> index >> t; index--; update(index, t); } else if (type == 2) { int L, R; cin >> L >> R; L--; cout << (query(L, R) ? Yes : No ) << n ; } else { assert(false); } } } |
#include <bits/stdc++.h> using namespace std; int n, kov = 1; int t[502][502]; void ert(int a, int b) { t[a][b] = kov, kov++; } int main() { cin >> n; if (n <= 2) { cout << -1 << n ; return 0; } if (n == 3) { cout << 1 << << 3 << << 2 << n ; cout << 5 << << 4 << << 8 << n ; cout << 9 << << 6 << << 7 << n ; return 0; } for (int i = n - 3; i >= 1; i--) ert(i, n); for (int i = 1; i <= n - 1; i++) { for (int j = 1; j <= n - 1; j++) { if (i % 2 == n % 2) ert(i, n - j); else ert(i, j); } } t[n - 2][n] = n * n, t[n][n] = n * n - 1; ert(n, n - 2), ert(n, n - 1), ert(n - 1, n); for (int i = 1; i <= n - 3; i++) ert(n, i); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << t[i][j] << ; } cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long int len = s.length(); long long int cnt = 0; for (long long int i = 0; i < len; i++) { if (s[i] == Q ) { cnt++; } } long long int suffixes = cnt; long long int prefixes = 0; long long int ans = 0; for (long long int j = 0; j < len; j++) { if (s[j] == Q ) { prefixes++; suffixes--; } if (s[j] == A ) { ans += prefixes * suffixes; } } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, i, a, b, sum = 0; cin >> n >> m; long long arr[n + 1]; for (i = 1; i <= n; i++) { cin >> arr[i]; } for (i = 0; i < m; i++) { cin >> a >> b; sum += min(arr[a], arr[b]); } cout << sum << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; namespace perfectGroups { const int mx = 5001; int arr[mx], ans[mx], previous[mx], dp[5001][5001], zero[mx]; int transNumber(int num) { int temp = abs(num), rt = sqrt(temp), ret = 1; for (int i = 2; i <= rt; i++) { int cnt = 0; while (temp > 1 && temp % i == 0) { cnt++; temp = temp / i; } if (cnt & 1) ret = ret * i; } if (temp > 1 || temp == 0) ret = ret * temp; return ret * (num > 0 ? 1 : -1); } void mapTheArray(int *n) { for (int i = 1; i <= *n; i++) { zero[i] = zero[i - 1] + (arr[i] == 0); } map<int, int> maping; for (int i = 1, idx = 0; i <= *n; i++) { if (maping.find(arr[i]) == maping.end()) { arr[i] = maping[arr[i]] = ++idx; } else { arr[i] = maping[arr[i]]; } } for (int i = 1; i <= *n; i++) { int pre = previous[arr[i]]; previous[arr[i]] = i; arr[i] = pre; } } void dpTable(int *n) { for (int i = 1; i <= *n; i++) { for (int j = 1; j + i - 1 <= *n; j++) { int l = i, r = i + j - 1; if (arr[r] < l) dp[l][r] = dp[l][r - 1] + 1; else dp[l][r] = dp[l][r - 1]; } } for (int i = 1; i <= *n; i++) { for (int j = i; j <= *n; j++) { int id = zero[j] - zero[i - 1]; if (id >= 1 && dp[i][j] > 1) dp[i][j]--; ans[dp[i][j]]++; } } } void solve() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , &arr[i]); arr[i] = transNumber(arr[i]); } mapTheArray(&n); dpTable(&n); for (int i = 1; i <= n; i++) { printf( %d , ans[i]); if (i == n) puts( ); } } } // namespace perfectGroups int main() { perfectGroups::solve(); return 0; } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:60777216 ) using namespace std; int d[10]; int c[107][107]; long long dp[107][17]; long long solve(int len, int pos) { if (pos == 9) { if (len >= d[9]) { return 1; } return 0; } if (dp[len][pos] != -1) { return dp[len][pos]; } dp[len][pos] = 0; if (pos == 0) { for (int j = d[pos]; j <= len; ++j) { dp[len][pos] += solve(len - j, pos + 1) * c[len - 1][j] % 1000000007; if (dp[len][pos] >= 1000000007) dp[len][pos] -= 1000000007; } } else { for (int j = d[pos]; j <= len; ++j) { dp[len][pos] += solve(len - j, pos + 1) * c[len][j] % 1000000007; if (dp[len][pos] >= 1000000007) dp[len][pos] -= 1000000007; } } return dp[len][pos]; } int main() { c[0][0] = 1; c[1][1] = 1; c[1][0] = 1; for (int i = 2; i <= 100; ++i) { c[i][i] = 1; c[i][0] = 1; for (int j = 1; j < i; ++j) { c[i][j] = c[i - 1][j] + c[i - 1][j - 1]; if (c[i][j] >= 1000000007) c[i][j] -= 1000000007; } } long long ans = 0; memset(dp, -1, sizeof(dp)); int len; scanf( %d , &len); for (int i = 0; i < 10; ++i) { scanf( %d , d + i); } for (int i = 1; i <= len; ++i) { ans += solve(i, 0); if (ans >= 1000000007) ans -= 1000000007; } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 300005; using ll = long long; map<ll, int> dd; map<pair<int, int>, int> f; int n, pre[3][N]; ll a[3][N]; int dfs(int n, int m) { if (pre[1][n] == -1 && pre[2][m] == -1 && pre[0][min(n, m)] == -1) return 0; pair<int, int> p{n, m}; if (f.count(p)) return f[p]; int ans = 0; if (pre[1][n] > pre[2][m]) ans = max(ans, dfs(pre[1][n], m) + 1); else if (pre[2][m] != -1) ans = max(ans, dfs(n, pre[2][m]) + 1); if (pre[0][min(n, m)] != -1) ans = max(ans, dfs(pre[0][min(n, m)], pre[0][min(n, m)]) + 1); return f[p] = ans; } int main() { ios::sync_with_stdio(false); cin >> n; for (int j = 1; j <= 2; j++) for (int i = 1; i <= n; i++) cin >> a[j][i], a[0][i] += a[j][i]; for (int i = 0; i <= 2; i++) for (int j = 1; j <= n; j++) a[i][j] += a[i][j - 1]; for (int i = 0; i <= 2; i++) { dd.clear(); dd[0] = 0; pre[i][0] = -1; for (int j = 1; j <= n; j++) { pre[i][j] = max(pre[i][j - 1], dd.count(a[i][j]) ? dd[a[i][j]] : -1); dd[a[i][j]] = j; } } cout << dfs(n, n) << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, k; long long c[1010][1010]; const int mod = 1e9 + 7; void pre() { for (int i = 0; i <= 1005; i++) { c[i][0] = c[i][i] = 1; } for (int i = 1; i <= 1005; i++) { for (int j = 0; j <= i; j++) { c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; ; } } } int main(int argc, char const *argv[]) { scanf( %d%d%d , &n, &m, &k); pre(); if (2 * k > n - 1 || 2 * k > m - 1) { printf( 0 ); } else { printf( %lld n , c[n - 1][k * 2] * c[m - 1][k * 2] % mod); } return 0; } |
#include <bits/stdc++.h> using namespace std; constexpr long long MOD = 1e9 + 7; long long cnt[710][710]; int a[500100]; int main() { int n; scanf( %d , &n); for (int ii = 0; ii < n; ++ii) { int t, x, y; scanf( %d%d%d , &t, &x, &y); if (t == 1) { a[x] += y; for (int i = 1; i <= 700; ++i) { cnt[i][x % i] += y; } } else { long long ans = 0; if (x > 700) { for (int i = y % x; i <= 500000; i += x) { ans += a[i]; } } else { ans = cnt[x][y % x]; } printf( %I64lld n , ans); } } return 0; } |
#include <bits/stdc++.h> using namespace std; constexpr int MAXN = 3e5 + 5; constexpr int MAXW = 1e4 + 5; constexpr int MOD = 998244353; constexpr long long INF = 2e10; constexpr int LOG = 11; string s; vector<int> ans; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s; reverse(s.begin(), s.end()); while (!s.empty()) { if (s.back() == . ) { ans.push_back(0); s.pop_back(); } else { s.pop_back(); if (s.back() == - ) { ans.push_back(2); } else { ans.push_back(1); } s.pop_back(); } } for (auto p : ans) { cout << p; } return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const ld pi = acos(0) * 2; template <typename T> inline void read(T &x) { char c; bool nega = 0; while ((!isdigit(c = getchar())) && (c != - )) ; if (c == - ) { nega = 1; c = getchar(); } x = c - 48; while (isdigit(c = getchar())) x = x * 10 + c - 48; if (nega) x = -x; } template <typename T> inline void writep(T x) { if (x > 9) writep(x / 10); putchar(x % 10 + 48); } template <typename T> inline void write(T x) { if (x < 0) { putchar( - ); x = -x; } writep(x); } template <typename T> inline void writeln(T x) { write(x); putchar( n ); } template <typename CT, typename T> inline void reset_container(CT &c, int sz, T v) { c.resize(sz); for (auto &x : c) x = v; } int n, k; int a[101]; void solve() { read(n); read(k); for (int i = (1); i <= (n); i++) read(a[i]); sort(a + 1, a + n + 1); if (a[n] - k > a[1] + k) puts( -1 ); else writeln(a[1] + k); } int main() { int q; read(q); while (q--) solve(); } |
#include <bits/stdc++.h> using namespace std; vector<bool> trust; vector<int> massive(int** mas, int num) { vector<int> mase; int nume = num; int nume1; while (nume != 0) { nume1 = nume; nume = mas[nume - 1][0]; } mase.push_back(0); while (nume1 != 0) { mase.push_back(nume1); trust[nume1 - 1] = true; nume1 = mas[nume1 - 1][1]; } mase.push_back(0); return mase; } int main() { int n; cin >> n; int** mas = new int*[n]; for (int i = 0; i < n; i++) { mas[i] = new int[2]; trust.push_back(false); cin >> mas[i][0] >> mas[i][1]; } vector<vector<int>> comp; for (int i = 0; i < n; i++) { vector<int> vect2; if (!trust[i]) { vect2 = massive(mas, i + 1); comp.push_back(vect2); } } vector<int> ansver; ansver.push_back(0); for (auto i : comp) { for (auto j : i) { if (j != 0) { ansver.push_back(j); } } } ansver.push_back(0); for (int i = 1; i < n + 1; i++) { for (int j = 0; j < ansver.size(); j++) { if (i == ansver[j]) { cout << ansver[j - 1] << << ansver[j + 1]; } } cout << endl; } } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MX = 2e2 + 5; const long long INF = 1e18; const long double PI = 4 * atan((long double)1); const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; namespace io { void setIn(string second) { freopen(second.c_str(), r , stdin); } void setOut(string second) { freopen(second.c_str(), w , stdout); } void setIO(string second = ) { ios_base::sync_with_stdio(0); cin.tie(0); if ((int)second.size()) { setIn(second + .in ); setOut(second + .out ); } } } // namespace io using namespace io; namespace input { template <class T> void re(complex<T>& x); template <class T1, class T2> void re(pair<T1, T2>& p); template <class T> void re(vector<T>& a); template <class T, size_t SZ> void re(array<T, SZ>& a); template <class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } void re(long double& x) { string t; re(t); x = stold(t); } template <class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); } template <class T> void re(complex<T>& x) { T a, b; re(a, b); x = cd(a, b); } template <class T1, class T2> void re(pair<T1, T2>& p) { re(p.first, p.second); } template <class T> void re(vector<T>& a) { for (int i = (0); i < ((int)a.size()); ++i) re(a[i]); } template <class T, size_t SZ> void re(array<T, SZ>& a) { for (int i = (0); i < (SZ); ++i) re(a[i]); } } // namespace input namespace output { template <class T1, class T2> void pr(const pair<T1, T2>& x); template <class T, size_t SZ> void pr(const array<T, SZ>& x); template <class T> void pr(const vector<T>& x); template <class T> void pr(const set<T>& x); template <class T1, class T2> void pr(const map<T1, T2>& x); template <class T> void pr(const T& x) { cout << x; } template <class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { pr(first); pr(rest...); } template <class T1, class T2> void pr(const pair<T1, T2>& x) { pr( { , x.first, , , x.second, } ); } template <class T> void prContain(const T& x) { pr( { ); bool fst = 1; for (const auto& a : x) pr(!fst ? , : , a), fst = 0; pr( } ); } template <class T, size_t SZ> void pr(const array<T, SZ>& x) { prContain(x); } template <class T> void pr(const vector<T>& x) { prContain(x); } template <class T> void pr(const set<T>& x) { prContain(x); } template <class T1, class T2> void pr(const map<T1, T2>& x) { prContain(x); } void ps() { pr( n ); } template <class Arg> void ps(const Arg& first) { pr(first); ps(); } template <class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { pr(first, ); ps(rest...); } } // namespace output using namespace output; using namespace input; long long add(long long a, long long b) { a += b; if (a >= MOD) { a -= MOD; } return a; } long long sub(long long a, long long b) { a -= b; if (a < 0) { a += MOD; } return a; } long long mul(long long a, long long b) { return (a * b) % MOD; } void add_self(long long& a, long long b) { a = add(a, b); } void sub_self(long long& a, long long b) { a = sub(a, b); } void mul_self(long long& a, long long b) { a = mul(a, b); } int main() { setIO(); long long t; re(t); for (int ii = (0); ii < (t); ++ii) { long long N, W; re(N, W); vector<long long> v; for (int i = (0); i < (N); ++i) { long long a; re(a); v.push_back(a); } bool done = false; for (int i = (0); i < ((int)v.size()); ++i) { if (v[i] >= (W + 1) / 2 && v[i] <= W) { ps(1); ps(i + 1); done = true; break; } } if (done) { continue; } long long lind = -1; long long cs = 0; long long ignore = 0; for (int i = (0); i < ((int)v.size()); ++i) { if (v[i] > W) { ignore++; continue; } cs += v[i]; if (cs >= (W + 1) / 2 && cs <= W) { lind = i; break; } } if (lind == -1) { ps(-1); continue; } ps(lind + 1 - ignore); for (int i = (0); i < (lind + 1); ++i) { if (v[i] > W) { continue; } cout << i + 1 << ; } ps(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j; cin >> n >> m; for (i = 1; i <= n; i++) { for (j = 0; j < m; j++) { if (i % 4 == 0) { if (j == 0) cout << # ; else cout << . ; } else if (i % 4 == 2) { if (j == m - 1) cout << # ; else cout << . ; } else cout << # ; } cout << endl; } } |
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e6 + 5; long long n, m, i, t, j, k, l, x, y, z, s, e, p, q; long long dg(long long s, long long e) { if (e % 2) return 1 - s % 2; if (s > e / 2) return s % 2; if (s > e / 4) return 1; return dg(s, e / 4); } int main() { scanf( %lld , &n); p = 0; q = 1; for (int i = 1; i <= n; i++) { scanf( %lld%lld , &s, &e); x = dg(s, e); if (s > e / 2) y = 1; else y = dg(s, e / 2); if (p + q != 1) continue; else if (p) p = 1 - x, q = 1 - y; else p = x, q = y; } printf( %lld %lld n , p, q); } |
#include <bits/stdc++.h> using namespace std; char s[5005]; int n, dp[5005][5]; const int inf = 1e7 + 5; int go(int id, int need) { if (id == n) return 0; if (dp[id][need] != -1) return dp[id][need]; int ans = 0; if (need == 1) { if (s[id] == a ) ans = max(1 + go(id + 1, 1), 1 + go(id + 1, 2)); else ans = max(go(id, 2), go(id + 1, 1)); } else if (need == 2) { if (s[id] == a ) ans = max(go(id, 3), go(id + 1, 2)); else ans = max(1 + go(id + 1, 2), 1 + go(id + 1, 3)); } else { if (s[id] == a ) ans = 1 + go(id + 1, 3); else ans = go(id + 1, 3); } return dp[id][need] = ans; } int main() { scanf( %s , s); n = strlen(s); memset(dp, -1, sizeof(dp)); printf( %d n , go(0, 1)); } |
#include <bits/stdc++.h> using namespace std; const int N = 200; typedef long long matrix[N][N]; const long long MOD = 1000000007; const double EPS = 1e-8; const double PI = 2 * acos(0); const int maxn = 1e6 + 6; int main() { cin.sync_with_stdio(false); int n, ans = 0, a[100100], p[100100]; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i] >> p[i]; int i = 1; while (i <= n) { ans += a[i] * p[i]; for (int j = i + 1; j <= n; j++) { if (p[j] < p[i]) { i = j - 1; break; } ans += a[j] * p[i]; if (j == n) i = n; } i++; } cout << ans; } |
#include <bits/stdc++.h> using namespace std; long n, m; vector<string> pref; long mov[500][500]; string temp; string inp[500]; long er, td; long bst[2000]; string tst; long dp[4][500][500]; long nend, t, ncvrd, answ; void ad(long &a, long b) { a += b; if (a >= 1000000009) a -= 1000000009; } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> inp[i]; for (int j = 0; j < inp[i].size(); j++) inp[i][j] = inp[i][j] - A + a ; } pref.push_back( ); for (int i = 1; i <= m; i++) { temp = ; for (int j = 0; j < inp[i].size(); j++) { temp += inp[i][j]; pref.push_back(temp); } } for (int i = 0; i < pref.size(); i++) { bst[i] = -1; for (int j = 1; j <= m; j++) { if (inp[j].size() > pref[i].size()) continue; er = 0; if (bst[i] >= 0 && inp[j].size() <= inp[bst[i]].size()) continue; td = pref[i].size() - inp[j].size(); for (int q = 0; q < inp[j].size(); q++) if (pref[i][q + td] != inp[j][q]) { er = 1; break; } if (er == 0) bst[i] = j; } } for (int i = 0; i < pref.size(); i++) for (int ad = 0; ad < 26; ad++) { mov[i][ad] = -1; tst = pref[i]; tst += char( a + ad); for (int j = 0; j < pref.size(); j++) { if (pref[j].size() > tst.size()) continue; er = 0; td = tst.size() - pref[j].size(); if (mov[i][ad] != -1 && pref[mov[i][ad]].size() > pref[j].size()) continue; for (int q = 0; q < pref[j].size(); q++) if (pref[j][q] != tst[td + q]) { er = 1; break; } if (er == 0) mov[i][ad] = j; } } dp[0][0][0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= pref.size(); j++) for (int q = 0; q <= i; q++) dp[1 - i % 2][j][q] = 0; for (int end = 0; end < pref.size(); end++) for (int cvrd = 0; cvrd <= i; cvrd++) if (cvrd >= i - 25) if (dp[i % 2][end][cvrd]) for (int achar = 0; achar < 26; achar++) { nend = mov[end][achar]; t = bst[nend]; if (t > -1 && inp[t].size() + cvrd > i) ncvrd = i + 1; else ncvrd = cvrd; ad(dp[1 - i % 2][nend][ncvrd], dp[i % 2][end][cvrd]); } } answ = 0; for (int end = 0; end < pref.size(); end++) ad(answ, dp[n % 2][end][n]); cout << answ << endl; cin.get(); cin.get(); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a, b; for (int i = 0; i < n / 2; ++i) { a.push_back(i); } for (int i = 0; i < n - n / 2; ++i) { b.push_back(i + n / 2); } cout << a.size() * b.size() << endl; for (int i = 0; i < a.size(); ++i) { for (int j = 0; j < b.size(); ++j) { cout << a[i] + 1 << << b[j] + 1 << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; char s1[100005], s2[100005]; int main() { int l1, l2, i; char tmp; while (~scanf( %s , s1)) { l1 = strlen(s1); for (i = l1 - 1, l2 = 0, tmp = 0; i >= 0; i--) { if (s1[i] >= tmp) { tmp = s1[i]; s2[l2++] = s1[i]; } } for (i = l2 - 1; i >= 0; i--) { printf( %c , s2[i]); } printf( n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 2010; int n, m; long long f[N][N], g[N][N], h[N][N], o[N][N], p[N][N], ans; int main() { cin >> n >> m; memset(f, 0, sizeof(f)); memset(g, 0, sizeof(g)); memset(h, 0, sizeof(h)); memset(o, 0, sizeof(o)); for (int i = 1; i <= n; ++i) { for (int j = 2; j <= m; ++j) { (f[i][j] += 1 + g[i - 1][j] * j % mod + h[i - 1][j]) %= mod; o[i][j] = (f[i][j] - f[i - 1][j]) % mod; } for (int j = 2; j <= m; ++j) { g[i][j] = (g[i][j - 1] + f[i][j]) % mod; h[i][j] = (h[i][j - 1] + f[i][j] * (1 - j) % mod) % mod; } } memset(p, 0, sizeof(p)); for (int i = 1; i <= n; ++i) for (int j = 2; j <= m; ++j) p[i][j] = (p[i - 1][j] + o[i][j]) % mod; ans = 0; for (int i = 1; i <= n; ++i) for (int j = 2; j <= m; ++j) { (ans += (m - j + 1) * o[i][j] % mod * p[n - i + 1][j] % mod) %= mod; } cout << (ans + mod) % mod << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 35; const long long N = 1e7 + 35; const long long mod = 3e18; const double eps = 1e-6; string a[MAX]; map<string, int> mm; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; for (int j = 1; j <= 9; j++) { for (int k = 0; k + j < 10; k++) { string s = a[i].substr(k, j); if (mm[s] == 0) mm[s] = i + 1; else if (mm[s] != i + 1) mm[s] = -1; } } } string s; for (int i = 0; i < n; i++) { bool ok = 0; for (int j = 1; j <= 9; j++) { for (int k = 0; k + j < 10; k++) { s = a[i].substr(k, j); if (mm[s] != -1) { ok = 1; break; } } if (ok) break; } cout << s << n ; } } |
#include <bits/stdc++.h> const int kMaxN = 1e7 + 5; int N; int phi[kMaxN]; long long prime[kMaxN], primeList[kMaxN], count[kMaxN]; int k = 0; void ComputePhi() { for (int i = 1; i <= N; i++) { phi[i] = i; } for (int i = 2; i <= N; i++) { if (phi[i] == i) { for (int j = i; j <= N; j += i) { phi[j] = phi[j] / i * (i - 1); } } } } void ComputePrimes() { for (int i = 2; i <= N; i++) { if (prime[i] != 0) { count[prime[i]]++; continue; } for (int j = i; j <= N; j += i) { if (!prime[j]) { prime[j] = i; } } count[prime[i]]++; } for (int i = 2; i <= N; i++) { while (count[i] > 0) { primeList[++k] = i; count[i]--; } } } int main() { scanf( %d , &N); ComputePhi(); ComputePrimes(); long long sol = 0; for (int i = 2; i <= N; i++) { sol += i - 1 - phi[i]; } sol *= -1; int cursor = k; for (int i = 1; i <= k; i++) { while (cursor >= i && primeList[i] * primeList[cursor] > N) { cursor--; } if (cursor >= i) { sol += 2LL * (cursor - i); } } cursor = k; for (int i = 1; i <= k; i++) { while (cursor > 0 && primeList[i] * primeList[cursor] > N) { cursor--; } cursor++; if (2 * primeList[i] <= N && cursor < i && primeList[i] * primeList[cursor] > N) { sol += 3LL * (i - cursor); } } printf( %lld n , sol); return 0; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.