func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int MAXN = (int)1003; const int LOG = (int)11; const int infint = (int)1e9 + 3; const long long inf = (long long)1e12; string A, B[MAXN]; int cost[MAXN], dp[MAXN][MAXN], last[MAXN], n, mx, sza; pair<int, int> radix[MAXN]; void build() { memset(dp, -63, sizeof dp); for (auto &ch : A) ch -= 0 ; mx++; reverse(A.begin(), A.end()); sza = A.size(); while ((int)A.size() < mx) A += (char)(0); for (int i = 0; i < n; i++) { last[i] = B[i].size(); reverse(B[i].begin(), B[i].end()); while ((int)B[i].size() < mx) B[i] += 0 ; radix[i] = {B[i][0], i}; } sort(radix, radix + n); reverse(radix, radix + n); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> A >> n; mx = A.size(); for (int i = 0; i < n; i++) { cin >> B[i]; mx = max(mx, (int)B[i].size()); } for (int i = 0; i < 10; i++) cin >> cost[i]; build(); dp[0][0] = 0; for (int i = 0; i < mx; i++) { int l, r; if (A[i] == ? - 0 ) l = 0, r = 9; else l = r = A[i]; if (i == sza - 1 && A[i] == ? - 0 ) l = 1; for (int k = l; k <= r; k++) { int pt = 0, add = 0; for (int j = 0; j < n; j++) { int ind = radix[j].second; int nxt = B[ind][i] - 0 + k; if (nxt >= 10) pt++; if (max(last[ind], sza) > i || nxt > 0) add += cost[nxt % 10]; } for (int j = 0; j <= n; j++) { dp[i + 1][pt] = max(dp[i + 1][pt], dp[i][j] + add); if (j == n) continue; int ind = radix[j].second; int nxt = B[ind][i] - 0 + k; if (nxt == 9) pt++; if (max(last[ind], sza) > i || nxt > 0) add -= cost[nxt % 10]; add += cost[(nxt + 1) % 10]; } } reverse(radix, radix + n); for (int j = 0; j < n; j++) { int ind = radix[j].second; radix[j].first = n * B[ind][i] + j; } sort(radix, radix + n); reverse(radix, radix + n); } cout << dp[mx][0]; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int a, b, c, i, j, cnt = 0; cin >> a >> b >> c; for (i = 0; i <= c; i++) { for (j = c; j >= 0; j--) { if ((j * a + i * b) == c) { cout << Yes << endl; cnt = 1; break; } } if (cnt == 1) break; } if (cnt == 0) cout << No << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = (long long)(1e6) + 322; const long long MAXN = (long long)(1e7) + 100; const long long inf = (long long)(3e18) + 322; const long long mod = (long long)(1e9) + 7; const double eps = 1e-6; inline void upd(long long &mn, long long x, long long y, long long z) { mn = min(mn, (x - y) * (x - y) + (y - z) * (y - z) + (z - x) * (z - x)); } void solve() { long long mn = inf; long long n1, n2, n3; vector<long long> v[4]; cin >> n1 >> n2 >> n3; for (long long i = 1; i <= n1; ++i) { long long x; cin >> x; v[0].push_back(x); } sort((v[0]).begin(), (v[0]).end()); for (long long i = 1; i <= n2; ++i) { long long x; cin >> x; v[1].push_back(x); } sort((v[1]).begin(), (v[1]).end()); for (long long i = 1; i <= n3; ++i) { long long x; cin >> x; v[2].push_back(x); } sort((v[2]).begin(), (v[2]).end()); vector<long long> ids; for (long long i = 0; i < 3; ++i) { ids.push_back(i); } do { for (long long i = 0; i < (long long)(v[ids[0]].size()); ++i) { long long x = v[ids[0]][i]; auto z = lower_bound((v[ids[2]]).begin(), (v[ids[2]]).end(), x); if (z == v[ids[2]].end()) continue; auto y = lower_bound((v[ids[1]]).begin(), (v[ids[1]]).end(), (x + *z) / 2); if (y != v[ids[1]].end()) upd(mn, x, *y, *z); if (y != v[ids[1]].begin()) { auto y2 = --lower_bound((v[ids[1]]).begin(), (v[ids[1]]).end(), (x + *z) / 2); upd(mn, x, *y2, *z); } } } while (next_permutation((ids).begin(), (ids).end())); cout << mn << n ; } signed main() { ios_base ::sync_with_stdio(false); cin.tie(0); long long test; cin >> test; for (long long tt = 1; tt <= test; ++tt) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> int chkmax(T &a, T b) { if (b > a) { a = b; return 1; } return 0; } template <class T> int chkmin(T &a, T b) { if (b < a) { a = b; return 1; } return 0; } template <class iterator> void output(iterator begin, iterator end, ostream &out = cerr) { while (begin != end) { out << (*begin) << ; begin++; } out << endl; } template <class T> void output(T x, ostream &out = cerr) { output(x.begin(), x.end(), out); } void fast_io() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int MOD = 998244353; int mul(int a, int b) { return (1LL * a * b) % MOD; } int power(int a, int deg) { int res = 1; for (; deg; a = mul(a, a), deg >>= 1) { if (deg & 1) { res = mul(res, a); } } return res; } int inverse(int x) { return power(x, MOD - 2); } void add(int &a, int b) { a += b; if (a >= MOD) { a -= MOD; } } const int mx = 2e5 + 228; int n, fact[mx]; long long k; void calc_fact() { fact[0] = 1; for (int i = 1; i < mx; ++i) { fact[i] = mul(fact[i - 1], i); } } int C(int n, int k) { return (k > n ? 0 : mul(fact[n], inverse(mul(fact[k], fact[n - k])))); } signed main() { fast_io(); calc_fact(); cin >> n >> k; if (k >= n) { cout << 0 << endl; exit(0); } int x = n - k; int ans = 0; for (int i = 0; i <= x; ++i) { int sign = (i % 2 ? MOD - 1 : 1); add(ans, mul(sign, mul(C(x, i), power(x - i, n)))); } ans = mul(ans, C(n, x)); if (k) { ans = mul(ans, 2); } cout << ans << n ; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; const long long mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string str; cin >> str; long long n = str.length(); vector<pair<long long, long long> > arr; arr.push_back(make_pair(1, str[0] - a )); for (long long i = 1; i < n; i++) { if (str[i] == str[i - 1]) arr[arr.size() - 1].first++; else arr.push_back(make_pair(1, str[i] - a )); } long long ans = 0; while (arr.size() > 1) { ans++; vector<pair<long long, long long> > tmp; for (long long i = 0; i < arr.size(); i++) { if (i == 0 || i == arr.size() - 1) arr[i].first -= 1; else arr[i].first -= 2; if (tmp.size() == 0 || arr[i].second != tmp[tmp.size() - 1].second) { if (arr[i].first > 0) tmp.push_back(arr[i]); } else if (arr[i].first > 0) tmp[tmp.size() - 1].first += arr[i].first; } arr = tmp; } cout << ans; }
#include <bits/stdc++.h> using namespace std; class Graph { public: vector<vector<int> > adjList; vector<int> indegree; Graph(int v) { adjList.resize(v); indegree.resize(v, 0); } void add(int u, int v) { adjList[u].push_back(v); indegree[v]++; } }; struct subset { int rank; int parent; }; int find(subset subsets[], int i) { if (subsets[i].parent != i) subsets[i].parent = find(subsets, subsets[i].parent); return subsets[i].parent; } void Union(subset subsets[], int x, int y) { int xroot = find(subsets, x); int yroot = find(subsets, y); if (subsets[xroot].rank > subsets[yroot].rank) { subsets[yroot].parent = xroot; } else if (subsets[xroot].rank < subsets[yroot].rank) { subsets[xroot].parent = yroot; } else { subsets[yroot].parent = xroot; subsets[xroot].rank++; } } bool TopologicalSort(Graph const &graph, vector<int> &ans, int v, subset subsets[]) { vector<int> indegree = graph.indegree; queue<int> S; for (int i = 0; i < v; i++) { if (!indegree[i]) { S.push(i); ans[find(subsets, i)] = 1; } } while (!S.empty()) { int n = S.front(); S.pop(); for (int m : graph.adjList[n]) { indegree[m] -= 1; ans[find(subsets, m)] = ans[find(subsets, n)] + 1; if (!indegree[m]) { S.push(m); } } } for (int i = 0; i < v; i++) { if (indegree[i]) { return false; } } return true; } int main() { int n, m; cin >> n >> m; subset *subsets = new subset[n + m]; for (int v = 0; v < n + m; v++) { subsets[v].parent = v; subsets[v].rank = 0; } Graph graph(n + m); char input[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> input[i][j]; if (input[i][j] == = ) { Union(subsets, i, j + n); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int x = find(subsets, i); int y = find(subsets, j + n); if (input[i][j] == < ) { graph.add(x, y); } else if (input[i][j] == > ) { graph.add(y, x); } } } vector<int> ans(n + m); if (TopologicalSort(graph, ans, n + m, subsets)) { cout << YES << endl; for (int i = 0; i < n; i++) { cout << ans[find(subsets, i)] << ; } cout << endl; for (int i = n; i < m + n; i++) { cout << ans[find(subsets, i)] << ; } cout << endl; } else { cout << NO << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { char trash[100]; int n; cin >> n; cin.getline(trash, 10); for (int i = 0; i < n; ++i) { string s; getline(cin, s); bool fredas = (s.find( lala. , s.length() - 5) == s.length() - 5); bool rainbows = (s.find( miao. ) == 0); if (fredas ^ rainbows) { cout << (fredas ? Freda s : Rainbow s ); } else { cout << OMG>.< I don t know! ; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int SET(int N, int pos) { return N = N | (1 << pos); } int RESET(int N, int pos) { return N = N & ~(1 << pos); } bool CHECK(int N, int pos) { return (bool)(N & (1 << pos)); } template <typename T> inline T __lcm(T a, T b) { return (a * b) / __gcd(a, b); } struct node { long long x, y, cnt; }; int dx[] = {0, 1, 0, -1, -1, 1, -1, 1}; int dy[] = {1, 0, -1, 0, 1, 1, -1, -1}; long long a[1000000]; long long b[1000000]; long long c[1000000]; void solve() { long long n; cin >> n; if (n % 2 == 0) { cout << -1 << endl; return; } for (long long i = 0; i < n; i++) { c[i] = n - i - 1; } for (long long i = 0; i < n; i++) { a[i] = i; } for (long long i = 0; i < n; i++) { b[i] = c[i] - a[i]; if (b[i] < 0) { b[i] += n; } } for (long long i = 0; i < n; i++) { cout << a[i] << ; } cout << endl; for (long long i = 0; i < n; i++) { cout << b[i] << ; } cout << endl; for (long long i = 0; i < n; i++) { cout << c[i] << ; } cout << endl; } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const double long EPS = 1e-7; vector<vector<long long>> precost(long long n, vector<long long> &p, vector<long long> &id) { vector<vector<long long>> res(n, vector<long long>(n)); for (int i = 0; i < n; i++) { vector<long long> deg(p.size()); for (int j = 1; j < p.size(); j++) { deg[p[j]]++; } long long val = 0; for (int j = i; j < n; j++) { long long cur = id[j]; while (cur != 0 && deg[cur] == 0) { val++; deg[p[cur]]--; cur = p[cur]; } res[i][j] = val; } } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<vector<long long>> adj(2); vector<vector<long long>> id(2, vector<long long>(n)); for (int i = 0; i < 2; i++) { long long a; cin >> a; adj[i].resize(a); for (int j = 1; j <= a - 1; j++) { cin >> adj[i][j]; adj[i][j]--; } for (int j = 0; j < n; j++) { cin >> id[i][j]; id[i][j]--; } } vector<vector<vector<long long>>> cost(2); for (int i = 0; i < 2; i++) { cost[i] = precost(n, adj[i], id[i]); } vector<long long> dp(n + 1); for (int i = 0; i < n; i++) { for (int j = i + 1; j <= n; j++) { dp[j] = max(dp[j], dp[i] + max(cost[0][i][j - 1], cost[1][i][j - 1])); } } cout << dp[n]; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; long long mpow(long long x, long long y) { long long res = 1; x = x % 1000000007; while (y) { if (y & 1) { res *= x; res = res % 1000000007; } x *= x; x %= 1000000007; y >>= 1; } return res; } bool isprime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } void solves_1() { long long n; cin >> n; vector<long long> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.rbegin(), v.rend()); set<long long> st; long long tmp = 0; for (int i = 0; i < n; i++) { if (st.find(v[i]) != st.end()) continue; st.insert(v[i]); while (v[i] % 2 == 0) { v[i] /= 2; tmp++; st.insert(v[i]); } } cout << tmp << endl; return; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); ; long long t; cin >> t; while (t--) solves_1(); return 0; }
#include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0) return 1; if (b == 1) return mod(a, m); long long k = mod(exp(a, b / 2, m), m); if (b & 1) { return mod(a * mod(k * k, m), m); } else return mod(k * k, m); } map<vector<long long>, long long> mp; long long a[500100]; vector<long long> adj[500100]; void solve() { mp.clear(); long long n, m; cin >> n >> m; for (long long i = 1; i <= n; i++) { adj[i].clear(); cin >> a[i]; } for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; adj[b].push_back(a); } for (long long i = 1; i <= n; i++) { sort(adj[i].begin(), adj[i].end()); if (adj[i].size()) { mp[adj[i]] += a[i]; } } long long g = -1; for (map<vector<long long>, long long>::iterator it = mp.begin(); it != mp.end(); it++) { if (g == -1) g = it->second; else g = gcd(g, it->second); } if (g == -1) g = 0; cout << g << n ; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int n; int main() { cin >> n; n -= 10; if (n <= 0) cout << 0 << endl; else if (n <= 9) cout << 4 << endl; else if (n == 10) cout << 15 << endl; else if (n == 11) cout << 4 << endl; else cout << 0 << endl; }
#include <bits/stdc++.h> using namespace std; int p[100005], cnt, h[100005], cnth, l[100005], cntl, cntquery; int query(int x, int y) { printf( 0 %d %d n , x, y); fflush(stdout); int ret; scanf( %d , &ret); cntquery++; if (cntquery > 300000) while (1) ; return ret; } int main() { int t = 0, q, X; for (int x = -100000000; x <= 100000000;) { q = query(x, x); if (q == 0) { p[++cnt] = x; x++; } else if (q < 100 && x + 100 * 2 <= 100000000 && query(x + 100, x + 100) >= 100) { X = x; x += 100 * 2; } else { X = x; x += q; } } for (int i = 1; i <= cnt; i++) { q = query(p[i], X); if (q == 0) l[++cntl] = p[i]; q = query(X, p[i]); if (q == 0) h[++cnth] = p[i]; } printf( 1 %d %d n , cntl, cnth); for (int i = 1; i <= cntl; i++) printf( %d , l[i]); puts( ); for (int i = 1; i <= cnth; i++) printf( %d , h[i]); puts( ); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 150001; struct BIT { int bit[maxn]; BIT() { memset(bit, 0, sizeof(bit)); } void add(int x, int v) { for (x++; x < maxn; x += x & -x) bit[x] += v; } void add(int a, int b, int v) { add(a, 1), add(b + 1, -1); } int qry(int x) { int ret = 0; for (x++; x; x -= x & -x) ret += bit[x]; return ret; } }; struct segTree { int l, r; segTree *left = 0, *right = 0; int val = 0; segTree(int a, int b) : l(a), r(b){}; void add(int x, int v) { if (l == r) { val += v; return; } int mid = (l + r) / 2; if (x <= mid) { if (!left) left = new segTree(l, mid); left->add(x, v); } else { if (!right) right = new segTree(mid + 1, r); right->add(x, v); } val = (left ? left->val : 0) + (right ? right->val : 0); } void mrg(segTree *tre) { if (!tre) return; if (!left) left = tre->left; else if (tre->left) left->mrg(tre->left); if (!right) right = tre->right; else if (tre->right) right->mrg(tre->right); val += tre->val; delete tre; } int qry(int a, int b) { if (a <= l && r <= b) return val; int ret = 0, mid = (l + r) / 2; if (a <= mid && b >= l && left) ret += left->qry(a, b); if (b > mid && a <= r && right) ret += right->qry(a, b); return ret; } void clear() { if (left) left->clear(); if (right) right->clear(); delete this; } }; const int w = 18; int n, m, k; int q[maxn][2]; int p[w][maxn]; int d[maxn], sz[maxn], h[maxn], l[maxn], r[maxn]; vector<int> graph[maxn], v[maxn], v2[maxn]; BIT bit; segTree *tre[maxn]; int dfsh(int c) { sz[c] = 1, h[c] = -1; for (int i = 1; i < w; i++) { p[i][c] = ~p[i - 1][c] ? p[i - 1][p[i - 1][c]] : -1; } for (int i : graph[c]) { if (i == p[0][c]) continue; p[0][i] = c; d[i] = d[c] + 1; sz[c] += dfsh(i); if (!~h[c] || sz[i] > sz[h[c]]) h[c] = i; } return sz[c]; } int dfsh2(int c) { r[c] = l[c]; for (int i : graph[c]) { if (i == p[0][c] || i == h[c]) continue; l[i] = r[c] + 1; r[c] = dfsh2(i); } if (~h[c]) { l[h[c]] = r[c] + 1; r[c] = dfsh2(h[c]); } return r[c]; } int lft(int c, int x) { for (int i = 0; i < w; i++) { if (((x >> i) & 1) && ~c) c = p[i][c]; } return c; } int lca(int x, int y) { if (d[x] < d[y]) swap(x, y); x = lft(x, d[x] - d[y]); for (int i = w - 1; ~i; i--) { if (p[i][x] != p[i][y]) x = p[i][x], y = p[i][y]; } return x == y ? x : p[0][x]; } long long dfs2(int c, int rt) { long long ret = 0; tre[c] = new segTree(0, n - 1); for (int i : v2[c]) { int dd = max(0, k + d[rt] - d[q[i][0]]); int j = q[i][1]; if (d[j] - d[rt] >= dd) { j = lft(j, d[j] - d[rt] - dd); ret += tre[c]->qry(l[j], r[j]); } tre[c]->add(l[q[i][1]], 1); } for (int i : graph[c]) { if (i == p[0][c] || (c == rt && i == h[c])) continue; ret += dfs2(i, rt); if (v2[c].size() < v2[i].size()) { swap(v2[c], v2[i]); swap(tre[c], tre[i]); } for (int it : v2[i]) { int dd = max(0, k + d[rt] - d[c]); int j = q[it][1]; if (d[j] - d[rt] >= dd) { j = lft(j, d[j] - d[rt] - dd); ret += tre[c]->qry(l[j], r[j]); } v2[c].push_back(it); } v2[i].clear(); tre[c]->mrg(tre[i]); } return ret; } long long dfs(int c) { long long ret = 0; for (int i : graph[c]) if (i != p[0][c]) ret += dfs(i); for (int t = 0; t < 2; t++) { for (int i : v[c]) ret += bit.qry(l[q[i][t]]); for (int i : v[c]) { int j = q[i][t]; if (d[j] - d[c] >= k) { j = lft(j, d[j] - d[c] - k); bit.add(l[j], r[j], 1); } if (!t) v2[q[i][t]].push_back(i); } } ret += dfs2(c, c); v2[c].clear(); tre[c]->clear(); return ret; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; u--, v--; graph[u].push_back(v); graph[v].push_back(u); } p[0][0] = -1; dfsh(0), dfsh2(0); for (int i = 0; i < m; i++) { cin >> q[i][0] >> q[i][1]; q[i][0]--, q[i][1]--; if (l[q[i][0]] > l[q[i][1]]) swap(q[i][0], q[i][1]); v[lca(q[i][0], q[i][1])].push_back(i); } cout << dfs(0) << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int mod(int val) { return val > 0 ? val : -val; } int main() { int g[2], b[2]; cin >> g[0] >> g[1] >> b[0] >> b[1]; bool pass = false; if (((g[1] + 1) * 2 >= b[0]) && (b[0] - g[1]) > -2) { pass = true; } if ((g[0] + 1) * 2 >= b[1] && (b[1] - g[0]) > -2) { pass = true; } if (pass) { cout << YES << endl; } else { cout << NO << endl; } }
#include <bits/stdc++.h> int main() { int i, n, k; scanf( %d %d , &n, &k); int a[n]; int count = 0, j = 0; for (i = 0; i < n; i++) { scanf( %d , &a[i]); } for (i = 0; i < n; i++) { if (a[i] <= k) count++; else { j = 1; break; } } if (j != 0) { for (i = n - 1; i >= 0; i--) { if (a[i] <= k) count++; else break; } } printf( %d n , count); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(3) using namespace std; int n, m, a[1005][1005]; vector<int> row[1005]; vector<int> col[1005]; int ans[1005][1005]; int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { scanf( %d , &a[i][j]); row[i].push_back(a[i][j]); col[j].push_back(a[i][j]); } for (int i = 1; i <= n; i++) { sort(row[i].begin(), row[i].end()); row[i].erase(unique(row[i].begin(), row[i].end()), row[i].end()); } for (int i = 1; i <= m; i++) { sort(col[i].begin(), col[i].end()); col[i].erase(unique(col[i].begin(), col[i].end()), col[i].end()); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int l1 = lower_bound(row[i].begin(), row[i].end(), a[i][j]) - row[i].begin(); int l2 = lower_bound(col[j].begin(), col[j].end(), a[i][j]) - col[j].begin(); int r1 = row[i].end() - upper_bound(row[i].begin(), row[i].end(), a[i][j]); int r2 = col[j].end() - upper_bound(col[j].begin(), col[j].end(), a[i][j]); printf( %d , max(l1, l2) + 1 + max(r1, r2)); } puts( ); } return 0; }
#include <bits/stdc++.h> using namespace std; #define INF 200000000000000000LL typedef long long int ll; typedef pair<ll,ll> ii; typedef pair<ii,ll> iii; typedef vector<ll> vi; typedef vector<ii> vii; #define getbit(n,i) (((n)&(1LL<<(i)))!=0) #define setbit0(n,i) ((n)&(~(1LL<<(i)))) #define setbit1(n,i) ((n)|(1LL<<(i))) #define lastone(n) ((n)&(-(n))) #define read freopen( debug//in.txt , r ,stdin) #define write freopen( debug//out.txt , w ,stdout) #define DBG(a) cerr<<#a<< ->->->-> <<a<< n #define fi first #define se second #define PI (acos(-1)) #define fastread ios::sync_with_stdio(false);cin.tie(NULL) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define mod (1000000007) #define asz 500005 template<class T,class V> ostream& operator<<(ostream &s,pair<T,V> a) { s<<a.fi<< <<a.se; return s; } const int N = 1e3+5; int main() { fastread; #ifdef FILE_IO read; write; #endif int T=1; cin>>T; for(int qq=1; qq<=T; qq++) { int n; string s; cin>>n>>s; vi l(n+2),r(n+2); r[n] = 1; r[n-1] = 1 + (s.back() == R ); l[0] = 1; l[1] = 1 + (s[0] == L ); for(int i=2;i<=n;i++){ if(s[i-1] == L ){ if(s[i-2] == R )l[i] = 2+l[i-2]; else l[i] = 2; } else l[i] = 1; if(s[n-i] == R ){ if(s[n-i+1] == L )r[n-i] = 2+r[n-i+2]; else r[n-i] = 2; } else r[n-i] = 1; } for(int i=0;i<=n;i++)cout<<l[i]+r[i]-1<< ; cout<<endl; } }
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long qpow(long long x, long long tms) { long long tmp = 1; while (tms) { if (tms & 1) tmp = tmp * x % mod; tms >>= 1; x = x * x % mod; } return tmp; } int n, m, T; bool chk(int k) { if (n == 1) return 1; long long sum = 1, t = (long long)k * m; double C = 1; long long s = 1; for (int i = 1; i <= k; ++i) { long long x = t / i, y = mod; if (x == 0) return 0; C = C * (k - i + 1) / i; s = s * (k - i + 1) % mod * qpow(i, mod - 2) % mod; if (C < mod) y = s; x = min(x, y); sum += x; if (sum >= n) return 1; t -= x * i; } return 0; } int main() { scanf( %d , &T); while (T--) { scanf( %d%d , &n, &m); int l = 0, r = n; while (l < r) { int mid = (l + r) >> 1; if (chk(mid)) r = mid; else l = mid + 1; } printf( %d n , l); } }
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf( %d , &t); for (int i = 0; i < t; i++) { int cont = 0; int tamano; scanf( %d , &tamano); string x; cin >> x; for (int j = 0; j < tamano / 2; j++) { if (x[j] == x[tamano - j - 1]) { cont++; } else if ((x[j] + 1 == x[tamano - j - 1] + 1) || (x[j] + 1 == x[tamano - j - 1] - 1) || (x[j] - 1 == x[tamano - j - 1] - 1) || (x[j] - 1 == x[tamano - j - 1] + 1)) { cont++; } else { printf( NO n ); break; } } if (cont == tamano / 2) { printf( YES n ); } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[109], d; void nhap() { scanf( %d n , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); } void qs(int l, int r) { int x = a[(l + r) / 2]; int i = l; int j = r; while (i <= j) { while (a[i] < x) i++; while (a[j] > x) j--; if (i <= j) { int tmp = a[i]; a[i] = a[j]; a[j] = tmp; i++; j--; } } if (i < r) qs(i, r); if (j > l) qs(l, j); } void xuli() { qs(1, n); for (int i = 2; i <= n; i += 2) { int s = a[i] - a[i - 1]; if (s != 0) d = d + s; } printf( %d , d); } int main() { nhap(); xuli(); }
#include <bits/stdc++.h> int max(int x, int y) { return (x > y ? x : y); } int geGcd(int a, int b) { if (a == 0 || b == 0) return 0; for (int i = max(a, b); i >= 1; i--) if (a % i == 0 && b % i == 0) return i; } using namespace std; int main() { int a, b, n; cin >> a >> b >> n; int cnt = 0; while (n > 0) { if (geGcd(a, n)) { n -= geGcd(a, n); cnt++; } if (geGcd(b, n)) { n -= geGcd(b, n); cnt++; } } if (cnt % 2 == 0) cout << 1 ; else cout << 0 ; }
#include <bits/stdc++.h> using namespace std; long long lv[100001], lf[100001], par[100001], n; vector<long long> con[100001], cld[100001]; long long get_leaf(long long id) { if (cld[id].size() == 0) { lf[1]++; return 1; } long long x = 0, i; for (i = 0; i <= cld[id].size() - 1; i++) { x += get_leaf(cld[id][i]); } lf[x]++; return x; } int main() { ios_base::sync_with_stdio(0); long long int i, j, x, y, m, cnt = 0, k, g, flg, t, l, r, md; cin >> n; for (i = 2; i <= n; i++) { cin >> x; con[x].push_back(i); con[i].push_back(x); } queue<long long> q; q.push(1); while (!q.empty()) { x = q.front(); q.pop(); if (con[x].size() == 0) continue; for (i = 0; i <= con[x].size() - 1; i++) { y = con[x][i]; if (par[y] || y == 1) continue; par[y] = x; cld[x].push_back(y); q.push(y); } } x = get_leaf(1); for (i = 2; i <= n; i++) { lf[i] += lf[i - 1]; } for (i = 1; i <= n; i++) { l = 0; r = n; while (r - l > 1) { md = (l + r) / 2; if (lf[md] < i) l = md; else r = md; } cout << r << ; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int c = 0, f = 0; for (int i = 0; i < n; i++) { if (s[i] == x ) c++; else if (s[i] != x && c < 3) c = 0; if (c > 2) { while (1) { f++; c--; if (c < 3) break; } } } cout << f; }
#include <bits/stdc++.h> using namespace std; int main() { int k, d; cin >> k >> d; if (d == 9) { cout << 9; for (int i = 1; i < k; i++) cout << 0; } else if (d == 0 && k > 1) { cout << No solution ; } else { cout << d; for (int i = 1; i < k; i++) cout << 9; } return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return b / gcd(a, b) * a; } int n, g[5001]; int s; int l[5000][5001]; int best = 1 << 30, freq; int SWAP(int a, int b) { int B = g[b]; int A = g[a]; int res = s; res -= (b - a - 2) - (l[B][b] - l[B][a]); res += (l[B][b] - l[B][a]); res -= (l[A][b] - l[A][a]); res += (b - a - 2) - (l[A][b] - l[A][a]); return res; } int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) { scanf( %d , g + i); for (int j = i - 1; j; --j) s += g[i] < g[j]; } for (int(i) = 0; i < int(n); ++i) { l[i][1] = 0; for (int j = 2; j <= n; ++j) l[i][j] = l[i][j - 1] + (i > g[j - 1]); } for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { int cost = SWAP(i, j); if (g[i] < g[j]) ++cost; else --cost; if (cost == best) ++freq; else if (cost < best) { best = cost; freq = 1; } } } cout << best << << freq << endl; }
#include <bits/stdc++.h> using namespace std; int ucln(int a, int b) { int r; while (b > 0) { r = a % b; a = b; b = r; } return a; } int main() { int x, y, a, b, bc; cin >> x >> y >> a >> b; bc = x * y / ucln(x, y); int l = a, r = b; while (l % bc != 0) l++; while (r % bc != 0) r--; cout << (r - l) / bc + 1; }
#include <bits/stdc++.h> using namespace std; struct point { long long x, y, index; point(long long x, long long y, long long index) : x(x), y(y), index(index) {} long long operator*(const point p) { return x * p.x + y * p.y; } long long operator^(const point p) { return x * p.y - y * p.x; } bool operator<(const point p) const { return make_pair(x, y) < make_pair(p.x, p.y); } bool operator==(const point p) const { return x == p.x && y == p.y; } bool operator!=(const point p) const { return !(*this == p); } point operator*(long long scale) { return point(scale * x, scale * y, index); } point operator+(const point p) { return point(x + p.x, y + p.y, index); } point operator-(const point p) { return point(x - p.x, y - p.y, index); } long long norm() { return (*this) * (*this); } }; bool half(point p) { assert(p.x != 0 || p.y != 0); return p.x > 0 || (p.x == 0 && p.y > 0); } void polarSort(point o, vector<point> &v, int L, int R) { sort(v.begin() + L, v.begin() + R, [&](point v, point w) { return make_tuple(half(v - o), 0, (v - o).norm()) < make_tuple(half(w - o), ((v - o) ^ (w - o)), (w - o).norm()); }); } const int N = 1515; int n; int Ans[N]; int Sub[N]; vector<point> pts; vector<int> Tree[N]; int DFS(int u, int p) { Sub[u] = 1; for (auto v : Tree[u]) if (v != p) Sub[u] += DFS(v, u); return Sub[u]; } void PaintTree(int u, int p, int L, int R) { int idx = L; for (int i = L; i <= R; i++) if (pts[idx].y < pts[i].y) idx = i; swap(pts[L], pts[idx]); Ans[pts[L].index] = u; polarSort(pts[L], pts, L + 1, R + 1); int pre = 0; for (auto v : Tree[u]) if (v != p) { PaintTree(v, u, L + pre + 1, L + pre + Sub[v]); pre += Sub[v]; } } int main() { scanf( %d , &n); for (int i = 1; i < n; i++) { int u, v; scanf( %d%d , &u, &v); Tree[u].push_back(v); Tree[v].push_back(u); } for (int i = 0; i < n; i++) { int x, y; scanf( %d%d , &x, &y); pts.push_back(point(x, y, i + 1)); } DFS(1, 0); PaintTree(1, 0, 0, n - 1); for (int i = 1; i <= n; i++) printf( %d , Ans[i]); puts( ); return 0; }
#include <bits/stdc++.h> using namespace std; using u64 = unsigned long long; const int mod = 1e9 + 7, N = 2e3 + 10; char mp[N][N]; int f[N][N]; bool st[N][N]; unordered_map<char, pair<int, int>> mv = { { L , {0, -1}}, { R , {0, 1}}, { U , {-1, 0}}, { D , {1, 0}}}; int main(void) { int t; cin >> t; while (t--) { int n, m, tx, ty; cin >> n >> m; for (int i = 1; i <= n; i++) scanf( %s , &mp[i][1]); stack<pair<int, int>> q; for (int i = 0; i <= n + 1; i++) for (int j = 0; j <= m + 1; j++) f[i][j] = 1; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int x = i, y = j; bool ring = false; if (f[x][y] != 1) continue; do { q.push({x, y}); st[x][y] = true; tx = x + mv[mp[x][y]].first, ty = y + mv[mp[x][y]].second; x = tx, y = ty; if (x < 1 || x > n || y < 1 || y > m) break; if (st[x][y]) { ring = true; break; } if (f[x][y] != 1) { q.push({x, y}); break; } } while (true); pair<int, int> xy; int step; if (ring) { stack<pair<int, int>> rq; do { xy = q.top(); q.pop(); st[xy.first][xy.second] = false; rq.push(xy); if (xy.first == x && xy.second == y) break; } while (true); step = rq.size(); while (rq.size()) { xy = rq.top(); rq.pop(); f[xy.first][xy.second] = step; } step++; while (q.size()) { xy = q.top(); q.pop(); st[xy.first][xy.second] = false; f[xy.first][xy.second] = step++; } continue; } step = f[q.top().first][q.top().second]; while (q.size()) { xy = q.top(); q.pop(); st[xy.first][xy.second] = false; f[xy.first][xy.second] = step++; } } } int res = -1, x, y; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (res < f[i][j]) { x = i, y = j; res = f[i][j]; } } } cout << x << << y << << res << endl; } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:36777216 ) using namespace std; const int INF = ~(1 << 31); const double EPS = 1; const double PI = 3.141592653589793; int main() { int n, m; cin >> n >> m; if (m == 3 && n >= 5) { cout << -1 n ; return 0; } for (int i = 0; i < m; i++) cout << i << << i * i + 10000000 << n ; for (int i = 0; i < n - m; i++) cout << i << << -i * i - 10000000 << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int m, x, p[100005], h[100005], cnt; long long n; int ksm(int a, long long k) { if (!k) return 1; int p = ksm(a, k / 2); if (k & 1) return (long long)p * p % 1000000007 * a % 1000000007; return (long long)p * p % 1000000007; } int Getval(long long n, int x) { int ret = 1; while (n) { n /= x; ret = (long long)ret * ksm(x, n) % 1000000007; } return ret; } int main() { scanf( %d%I64d , &x, &n); int m = int(sqrt(x)) + 1, ans = 1; for (int i = 2; i <= m; i++) { if (x % i == 0) { while (x % i == 0) x /= i; ans = (long long)ans * Getval(n, i) % 1000000007; } } if (x > 1) ans = (long long)ans * Getval(n, x) % 1000000007; printf( %d n , ans); }
#include <bits/stdc++.h> using namespace std; const int inf = 0x0f0f0f0f; template <class S, class T> inline pair<S, T> MP(S a, T b) { return make_pair(a, b); } template <class T> inline auto LX(T &X) -> decltype((X.first)) { return X.first; } template <class T> inline auto LX(const T &X) -> const decltype((X.first)) { return X.first; } template <class T> inline auto RX(T &X) -> decltype((X.second)) { return X.second; } template <class T> inline auto RX(const T &X) -> const decltype((X.second)) { return X.second; } template <class T> inline void RST(T &a) { memset(a, 0, sizeof(a)); } template <class T> inline void RST1(T &a) { memset(a, -1, sizeof(a)); } template <class T> inline void SINF(T &a) { memset(a, 0x0f, sizeof(a)); } template <class S, class T> inline void RST(S &a, T b) { fill((T *)a, (T *)a + (sizeof(a) / sizeof(b)), b); } template <class T> inline void CLR(T &a) { a.clear(); } template <class S, class T> inline void CPY(S &a, T &b) { memcpy(a, b, sizeof(a)); } template <class S, class T> inline bool chkmin(S &a, T b) { return b < a ? a = b, 1 : 0; } template <class S, class T> inline bool chkmax(S &a, T b) { return a < b ? a = b, 1 : 0; } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline int SZ(T &a) { return (int)(a.size()); } template <class T> inline bool EPT(T &a) { return a.empty(); } template <class T> inline auto FRT(T &a) -> decltype((a.front())) { return a.front(); } template <class T> inline auto FRT(const T &a) -> const decltype((a.front())) { return a.front(); } template <class T> inline auto BK(T &a) -> decltype(a.back()) & { return a.back(); } template <class T> inline auto BK(const T &a) -> const decltype((a.back())) { return a.back(); } template <class T> inline auto TOP(T &a) -> decltype((a.top())) { return a.top(); } template <class T> inline auto TOP(const T &a) -> const decltype((a.top())) { return a.top(); } int n, m, nn, k; int a[310][310], b[310][310]; int r[200010], cr[200010], sz[200010]; vector<pair<int, int> > l[200010]; unordered_map<int, int> c; int find(int x) { return x == r[x] ? x : r[x] = find(r[x]); } int d[1010][1010]; deque<pair<int, int> > Q; int ox, oy; bool cmp(const pair<int, int> &a, const pair<int, int> &b) { return d[LX(a) - ox + k][RX(a) - oy + k] < d[LX(b) - ox + k][RX(b) - oy + k]; } int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int &x = a[i][j]; scanf( %d , &x); if (x) if (!c.count(x)) x = c[x] = ++nn; else x = c[x]; sz[x]++; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int &x = b[i][j]; scanf( %d , &x); if (x != -1) { if (x) if (!c.count(x)) x = c[x] = ++nn; else x = c[x]; l[a[i][j]].push_back(MP(i, j)); } } for (int i = 0; i <= nn; i++) r[i] = cr[i] = i; k = max(n, m); int x = k, y = k, t = 0; for (int i = 1; i < k; i++) { d[--x][y] = ++t; for (int j = 1; j < 2 * i; j++) d[x][++y] = ++t; for (int j = 0; j < 2 * i; j++) d[++x][y] = ++t; for (int j = 0; j < 2 * i; j++) d[x][--y] = ++t; for (int j = 0; j < 2 * i; j++) d[--x][y] = ++t; } scanf( %d%d , &x, &y); Q.push_back(MP(x, y)); long long ans = 0; while (!EPT(Q)) { int x = LX(FRT(Q)), y = RX(FRT(Q)); Q.pop_front(); int c1 = find(a[x][y]), c2 = find(b[x][y]); if (!cr[c1] || cr[c1] == b[x][y]) continue; ox = x, oy = y; sort(l[c1].begin(), l[c1].end(), cmp); for (pair<int, int> t : l[c1]) Q.push_back(t); CLR(l[c1]); ans += sz[c1]; if (c1 != c2) sz[c2] += sz[c1], r[c1] = c2; cr[c2] = b[x][y]; } printf( %I64d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int it = 0; int find(int l, int r, int k) { if (l == r) return l; int ras = (r - l + 1) / 2; int ll, rr; if (k == 0) { ll = l + ras - 1; rr = l + ras; } else { ll = r - ras; rr = ll + 1; } if (k == 0) { assert(it != 60); cout << 1 << << ll << << rr << endl; ++it; } else { assert(it != 60); cout << 1 << << rr << << ll << endl; ++it; } string s; cin >> s; if (s[0] == T ) { if (k == 0) { return find(l, rr - 1, k); } else { return find(ll + 1, r, k); } } else { if (k == 0) { return find(ll + 1, r, k); } else { return find(l, rr - 1, k); } } } signed main() { int n, k; cin >> n >> k; int cur = find(1, n, 0); set<int> sss; sss.insert(cur); sss.insert(find(1, cur, 0)); sss.insert(find(cur, n, 1)); cout << 2 << << *sss.begin() << << *(++sss.begin()) << endl; return 0; }
#include <bits/stdc++.h> unsigned long long int p[64][2][2], m, pi[64]; void mul_matrix(int size, unsigned long long int (*a)[2], unsigned long long int (*b)[2], unsigned long long int (*dest)[2]) { int i, j, k; for (i = 0; i < size; i++) for (j = 0; j < 2; j++) { dest[i][j] = 0; for (k = 0; k < 2; k++) dest[i][j] = (dest[i][j] + (a[i][k] * b[k][j]) % m) % m; } } void init_mat() { int i; p[0][0][0] = p[0][0][1] = p[0][1][0] = 1; p[0][1][1] = 0; pi[0] = 2; for (i = 1; i < 64; i++) { mul_matrix(2, p[i - 1], p[i - 1], p[i]); pi[i] = (pi[i - 1] * pi[i - 1]) % m; } } int main() { unsigned long long int n, k, t, zero = 0, one = 1, ans = 1, l, i; unsigned long long int base1[1][2] = {0, 1}, base2[1][2] = {1, 0}, dest[1][2]; scanf( %lld %lld %lld %lld , &n, &k, &l, &m); init_mat(); for (i = 0, t = n - 1; t; t >>= 1, i++) { if (t & 1) { mul_matrix(1, base1, p[i], dest); memcpy(base1, dest, sizeof(dest)); mul_matrix(1, base2, p[i], dest); memcpy(base2, dest, sizeof(dest)); } } for (i = 0, t = n; t; t >>= 1, i++) if (t & 1) one = (one * pi[i]) % m; zero = ((base1[0][0] + base1[0][1]) % m + (base2[0][0] + base2[0][1]) % m) % m; one = ((one + m) - zero) % m; for (t = k, i = 0; i < l; t >>= 1, i++) { if (t & 1) ans = (ans * one) % m; else ans = (ans * zero) % m; } if (t) { printf( 0 ); return 0; } printf( %u , ans % m); return 0; }
#include <bits/stdc++.h> int inp() { char c = getchar(); while (c < 0 || c > 9 ) c = getchar(); int sum = 0; while (c >= 0 && c <= 9 ) { sum = sum * 10 + c - 0 ; c = getchar(); } return sum; } char s[1000010], l[1000010], r[1000010]; long long f[1000010], sum[1000010]; int n, sl, sr, pl[1000010], pr[1000010], hshs[1000010], hshl[1000010], hshr[1000010], fpow[1000010]; bool ok1(int x) { if (x + sl - 1 > n) return false; if (sl == pl[x]) return true; return l[pl[x] + 1] < s[x + pl[x]]; } bool ok2(int x) { if (x + sr - 1 > n) return false; if (pr[x] == sr) return true; return r[pr[x] + 1] > s[x + pr[x]]; } int get_hsh(int *hsh, int l, int r) { return (hsh[r] - ((long long)(hsh[l - 1]) * (long long)(fpow[r - l + 1]) % 998244353) + 998244353) % 998244353; } void solve(int *s, int *hsh, int len) { for (int i = 1; i <= n - len + 1; i++) { int l = 0; int r = len; while (l < r) { int mid = (l + r + 1) >> 1; if (get_hsh(hshs, i, i + mid - 1) == get_hsh(hsh, 1, mid)) l = mid; else r = mid - 1; } s[i] = l; } } void gethash(char *str, int *hsh, int len) { for (int i = 1; i <= len; i++) hsh[i] = ((long long)(hsh[i - 1]) * (long long)(11) + str[i] - 0 + 1) % 998244353; } int main() { fpow[0] = 1; for (int i = 1; i <= 1000000; i++) fpow[i] = ((long long)(fpow[i - 1]) * (long long)(11) % 998244353); scanf( %s , s + 1); scanf( %s , l + 1); scanf( %s , r + 1); n = strlen(s + 1); sl = strlen(l + 1); sr = strlen(r + 1); gethash(l, hshl, sl); gethash(r, hshr, sr); gethash(s, hshs, n); f[0] = 1; solve(pl, hshl, sl); solve(pr, hshr, sr); for (int i = 0; i <= n; i++) { if (i) { sum[i] += sum[i - 1]; f[i] += sum[i]; f[i] %= 998244353; } if (s[i + 1] == 0 ) { if (sl == 1 && l[1] == 0 ) { f[i + 1] += f[i]; f[i + 1] %= 998244353; } continue; } if (sl < sr) { sum[sl + i + 1] += f[i]; sum[sl + i + 1] %= 998244353; sum[sr + i] += 998244353 - f[i]; sum[sr + i] %= 998244353; } if (sl == sr) { if (ok1(i + 1) && ok2(i + 1)) { f[i + sl] += f[i]; f[i + sl] %= 998244353; } } else { if (ok1(i + 1)) { f[i + sl] += f[i]; f[i + sl] %= 998244353; } if (ok2(i + 1)) { f[i + sr] += f[i]; f[i + sr] %= 998244353; } } } std::cout << f[n] << std::endl; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; t = 1; while (t--) { int n; cin >> n; long long arr[n], c4 = 0, c2 = 0; long long i; map<long long, long long> make_pair; for (i = 0; i < n; i++) { cin >> arr[i]; c4 -= make_pair[arr[i]] / 4; c2 -= make_pair[arr[i]] / 2; make_pair[arr[i]]++; c4 += make_pair[arr[i]] / 4; c2 += make_pair[arr[i]] / 2; } long long q; cin >> q; for (i = 0; i < q; i++) { char c; long long x; cin >> c >> x; c4 -= make_pair[x] / 4; c2 -= make_pair[x] / 2; if (c == + ) { make_pair[x]++; } else { make_pair[x]--; } c4 += make_pair[x] / 4; c2 += make_pair[x] / 2; if (c4 >= 1 && c2 >= 4) { cout << YES << endl; } else { cout << NO << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 505; int l[MAXN], r[MAXN], s[MAXN], w[MAXN], v[MAXN]; int dp[2 * MAXN][MAXN]; vector<int> wr[2 * MAXN]; int calc(int str, int i) { if (dp[str][i] != -1) return dp[str][i]; int L = l[i], R = r[i]; vector<int> prf(2 * MAXN); prf[L] = 0; for (int x = L + 1; x <= R; x++) { prf[x] = prf[x - 1]; for (auto j : wr[x]) { if (l[j] == L && r[j] == R) continue; if (l[j] < L || w[j] > str) continue; prf[x] = max(prf[x], calc(min(str - w[j], s[j]), j) + prf[l[j]]); } } return dp[str][i] = v[i] + prf[R]; } int main() { int n, S; cin >> n >> S; l[0] = 0; r[0] = 2 * n; s[0] = S; w[0] = 0; v[0] = 0; for (int i = 1; i <= n; i++) { cin >> l[i] >> r[i] >> w[i] >> s[i] >> v[i]; wr[r[i]].push_back(i); } for (int i = 0; i < 2 * MAXN; i++) for (int j = 0; j < MAXN; j++) dp[i][j] = -1; cout << calc(S, 0) << endl; }
#include <bits/stdc++.h> using namespace std; void read(long long &x) { x = 0; long long f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == - ) f = -f; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 0 ; x *= f; } void print(long long x) { if (x < 0) putchar( - ), x = -x; if (!x) return; print(x / 10), putchar(x % 10 + 48); } void write(long long x) { if (!x) putchar( 0 ); else print(x); putchar( n ); } const long long maxn = 2e5 + 10; const long long inf = 1e9; const long double eps = 1e-12; const long long mod = 1e9 + 7; long long add(long long x, long long y) { return x + y >= mod ? x + y - mod : x + y; } long long del(long long x, long long y) { return x - y < 0 ? x - y + mod : x - y; } long long mul(long long x, long long y) { return 1ll * x * y - 1ll * x * y / mod * mod; } void inc(long long &x, long long y) { x += y; x %= mod; } long long n, k, a[maxn], res, c; long long qpow(long long a, long long x) { long long res = 1; for (; x; x >>= 1, a = 1ll * a * a % mod) if (x & 1) res = 1ll * res * a % mod; return res; } struct Matrix { long long a, b, r[110][110]; Matrix() { a = b = 0, memset(r, 0, sizeof r); } Matrix operator*(const Matrix &t) const { Matrix res; res.a = a, res.b = t.b; for (long long i = 0; i <= a; i++) for (long long j = 0; j <= t.b; j++) for (long long k = 0; k <= b; k++) res.r[i][j] = add(res.r[i][j], mul(r[i][k], t.r[k][j])); return res; } } tr, ans; signed main() { read(n), read(k); for (long long i = 1; i <= n; i++) read(a[i]), res += !(a[i] & 1); for (long long i = 1; i <= res; i++) c += !a[i]; ans.a = 0, ans.b = res; ans.r[0][c] = 1; tr.a = tr.b = res; for (long long i = 0; i <= res; i++) { long long lw = i, lb = res - i, rw = res - lw, rb = n - res - lb; if (i != res) inc(tr.r[i][i + 1], lb * rw % mod); if (i != 0) inc(tr.r[i][i - 1], lw * rb % mod); inc(tr.r[i][i], (res * (res - 1) + (n - res) * (n - res - 1)) % mod * qpow(2, mod - 2) % mod); inc(tr.r[i][i], (lw * lb + rw * rb) % mod); } long long x = k, f = 0; for (; x; x >>= 1, tr = tr * tr) if (x & 1) ans = ans * tr; for (long long i = 0, i_r = res; i <= i_r; i++) f = (f + ans.r[0][i]) % mod; write(ans.r[0][res] * qpow(f, mod - 2) % mod); return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const long long INF = 1e17; vector<vector<long long>> g; vector<long long> used; void dfs(long long v) { used[v] = 1; for (auto u : g[v]) { if (!used[u]) { dfs(u); } } } void solve() { long long n, m; cin >> n >> m; long long q; cin >> q; long long N = n + m; g.resize(N); used.resize(N); while (q--) { long long x, y; cin >> x >> y; --x, --y; x += m; g[x].push_back(y); g[y].push_back(x); } long long ans = -1; for (long long i = 0; i < N; ++i) { if (!used[i]) { dfs(i); ans++; } } cout << ans; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long tt = 1; while (tt--) { solve(); cout << n ; } }
#include <bits/stdc++.h> using namespace std; int main(void) { int t; cin >> t; for (int z = 0; z < t; ++z) { int n; string s; cin >> n >> s; string ans = ; for (int i = 0; i < n; ++i) { if ((s[i] - 0 ) % 2 == 1) ans += s[i]; if (ans.size() == 2) break; } if (ans.size() < 2) { cout << -1 n ; } else { cout << ans << n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = (int)1e5 + 100; vector<int> g[maxn], G[maxn]; int type[maxn]; int cntIn[maxn]; void no() { printf( No ); exit(0); } void yes() { printf( Yes ); exit(0); } void dfsG(int v, int par = -1) { if ((int)G[v].size() > 2) no(); type[v] = 3; for (int i = 0; i < (int)G[v].size(); i++) { int nv = G[v][i]; if (nv == par) continue; dfsG(nv, v); break; } } void solve() { int n; scanf( %d , &n); if (n <= 2) yes(); for (int i = 0; i < n - 1; i++) { int a, b; scanf( %d%d , &a, &b); a--; b--; g[a].push_back(b); g[b].push_back(a); } bool fnd = false; for (int i = 0; i < n; i++) if ((int)g[i].size() >= 3) fnd = true; if (!fnd) yes(); for (int i = 0; i < n; i++) if ((int)g[i].size() == 1 && type[i] == 0) { type[i] = 1; int pv = i; int v = g[i][0]; while ((int)g[v].size() == 2) { type[v] = 1; int nv = g[v][0] + g[v][1] - pv; pv = v; v = nv; } cntIn[v]++; } for (int i = 0; i < n; i++) if ((int)g[i].size() == 3 && cntIn[i] == 2) { type[i] = 2; } for (int i = 0; i < n; i++) for (int j = 0; j < (int)g[i].size(); j++) { int ni = g[i][j]; if (type[i] == 0 && type[ni] == 0) G[i].push_back(ni); } int st = -1; for (int i = 0; i < n; i++) if (type[i] == 0 && (int)G[i].size() <= 2) st = i; if (st != -1) { type[st] = 3; for (int i = 0; i < (int)G[st].size(); i++) dfsG(G[st][i], st); } for (int i = 0; i < n; i++) if (type[i] == 0) no(); yes(); } int main(int, char **) { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 77; int main() { int a, b; cin >> a >> b; int ans = 0; while (1) { if (a <= 0 || b <= 0) break; if (a < b) { a++; b -= 2; } else { b++; a -= 2; } if (a < 0 || b < 0) break; ++ans; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int arr[1000005]; int main() { int n, q, a, b, x = 0, xPar = 0; scanf( %d %d , &n, &q); while (q--) { scanf( %d , &a); if (a == 1) { scanf( %d , &b); x += b; if (x >= n) { x = x % n; } else if (x < 0) { x = n - ((x * -1) % n); } } else { if ((x + xPar) % 2 == 1) { xPar = ((xPar + 1) % n); } else { if (--xPar < 0) { xPar += n; } } } } for (int i = 1; i <= n; i++) { int nTemp = i + x; if (i % 2) { nTemp -= xPar; } else { nTemp += xPar; } while (nTemp <= 0) { nTemp += n; } while (nTemp > n) { nTemp -= n; } arr[nTemp] = i; } for (int i = 1; i <= n; i++) { cout << arr[i]; if (i != n) { cout << ; } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; struct query { long long k, pos, ind; }; struct fenwick { long long n; vector<long long> f; fenwick(long long _n) { n = _n; f.resize(n, 0); } void add(long long val, long long i) { for (; i < n; i |= (i + 1)) f[i] += val; } long long sum(long long i) { long long ans = 0; for (; i >= 0; i &= (i + 1), --i) ans += f[i]; return ans; } }; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<pair<long long, long long> > a(n); vector<long long> start(n); for (long long i = 0; i < n; ++i) { cin >> a[i].first; start[i] = a[i].first; a[i].second = i; } sort(a.begin(), a.end(), [](pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) return a.second > b.second; return a.first < b.first; }); reverse(a.begin(), a.end()); long long q; cin >> q; vector<query> qs(q); for (long long t = 0; t < q; ++t) { cin >> qs[t].k >> qs[t].pos; qs[t].k--; qs[t].pos--; qs[t].ind = t; } sort(qs.begin(), qs.end(), [](query a, query b) { return a.k < b.k; }); fenwick f(n); long long l = -1; vector<long long> ans(q); for (long long i = 0; i < q; ++i) { while (l < qs[i].k) { ++l; f.add(1, a[l].second); } long long L = 0, R = n - 1; while (R - L > 1) { long long m = (R + L) / 2; if (f.sum(m) <= qs[i].pos) { L = m; } else R = m; } if (f.sum(L) < qs[i].pos + 1) swap(L, R); ans[qs[i].ind] = start[L]; } for (auto x : ans) cout << x << n ; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string x, y; cin >> x >> y; int n, m; n = x.size(), m = y.size(); if (n != m) { cout << NO ; return 0; } if (n == 1) { if (x == y) cout << YES ; else cout << NO ; return 0; } if (x == y) { cout << YES ; return 0; } int cnt = count(x.begin(), x.end(), 0 ); int cnt1 = count(y.begin(), y.end(), 0 ); if (cnt == n || cnt1 == n) cout << NO ; else cout << YES ; }
#include <bits/stdc++.h> using namespace std; int dp[2][316 + 1][316 + 2]; int addmod(int x, int y) { int sum = x + y; if (sum >= 1000000007) sum -= 1000000007; return sum; } int mulmod(int x, int y, int p) { long long prod = x; prod = (prod * y) % p; return (int)prod; } void add_to_mod(int &x, int y) { x = addmod(x, y); } int run_dp(int n, int m, int x) { int result = 0; if (n > m) return result; int next = 0; dp[next][0][0] = 1; for (int i = 1; i <= m; i++) { int prev = next; next ^= 1; for (int j = n; j > 0; j--) for (int k = j; k >= 0; k--) { dp[next][j][k] = addmod(dp[prev][j - 1][k], dp[next][j][k + 1]); if (m - i != x - k) { int s = dp[prev][j][k]; if (k > 0) add_to_mod(s, dp[next][j - 1][k - 1]); add_to_mod(dp[next][j][k], s); } } dp[next][0][0] = (m - i) > x; } result = dp[next][n][0]; for (int i = 2; i <= n; i++) result = mulmod(result, i, 1000000007); return result; } int solve_problem() { int n, m, x; if (scanf( %d %d %d , &n, &m, &x) != 3) return 1; --x; int result = run_dp(n, m, x); printf( %d n , result); return 0; } int main() { solve_problem(); return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> data(n + 1); int ans = 1e9; for (int i = 1; i <= n; i++) { cin >> data[i]; ans = min(ans, data[i] / max(i - 1, n - i)); } cout << ans << endl; } int main() { cin.sync_with_stdio(0); cin.tie(0); solve(); }
#include <bits/stdc++.h> int n, k, m, t, l; int a, b; int main() { scanf( %d %d %d %d , &n, &k, &m, &t); l = n; for (int i = 1; i <= t; i++) { scanf( %d %d , &a, &b); if (a == 1) { if (b <= k) l++, k++; else l++; } else { if (b < k) l -= b, k -= b; else l = b; } printf( %d %d n , l, k); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, k; int Graph[1025][1025], f[1025][1025]; int Tj(int x) { int tot = 0; while (x) tot++, x -= x & (-x); return tot; } void read(int &x) { x = 0; char c = getchar(); int f = 1; while (c < 0 || c > 9 ) { if (c == - ) f = -f; c = getchar(); } while (c >= 0 && c <= 9 ) { x = (x << 3) + (x << 1) + c - 0 ; c = getchar(); } x *= f; return; } void write(int x) { if (x < 0) { x = -x; putchar( - ); } if (x > 9) write(x / 10); putchar(x % 10 + 0 ); } signed main() { read(n), read(m), read(k); for (register int i = 1; i <= m; ++i) { int u, v; read(u), read(v); u--, v--; Graph[u][v] = Graph[v][u] = f[(1 << u) | (1 << v)][(1 << u) | (1 << v)] = 1; } for (register int i = 0; i < (1 << n); ++i) for (register int j = 0; j < (1 << n); ++j) if ((i & j) == j && f[i][j]) for (register int k1 = 0; k1 < n; ++k1) for (register int k2 = 0; k2 < n; ++k2) { if (Graph[k1][k2] && (i & (1 << k1)) && (~i & (1 << k2)) && !(((j & (~(1 << k1))) | (1 << k2)) >> (k2 + 1))) f[i | (1 << k2)][j & (~(1 << k1)) | (1 << k2)] += f[i][j]; } int ans = 0; for (register int i = 0; i < (1 << n); ++i) if (Tj(i) == k) ans += f[(1 << n) - 1][i]; write(ans), putchar( n ); return 0; }
#include <bits/stdc++.h> using namespace std; int gl, gr, bl, br; bool check(int g, int b) { if (g - 1 <= b && b <= 2 * (g + 1)) return true; else return false; } int main() { scanf( %d %d %d %d , &gl, &gr, &bl, &br); if (check(gl, br) || check(gr, bl)) cout << YES << endl; else cout << NO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; const int inf = 0x7f7f7f7f; const int mod = 1000000007; const double eps = 1e-8; int n, m; set<int> v; vector<pair<int, int> > ans; vector<int> vc[100007]; bool isok(int u, int v) { for (size_t i = 0; i < vc[u].size(); ++i) { if (vc[u][i] == v) return false; } return true; } bool dfs(int u) { if ((int)ans.size() == min(n - 1, m)) return true; v.erase(u); for (set<int>::iterator it = v.begin(); it != v.end(); ++it) { if (!isok(u, *it)) continue; ans.push_back(make_pair(u, *it)); if (dfs(*it)) return true; else { ans.pop_back(); it = v.find(*it); } } v.insert(u); return false; } int main() { scanf( %d%d , &n, &m); for (int i = 0; i <= n; ++i) vc[i].clear(); int x, y; for (int i = 0; i < m; ++i) { scanf( %d%d , &x, &y); vc[x].push_back(y); vc[y].push_back(x); } for (int i = 1; i <= n; ++i) v.insert(i); ans.clear(); for (int i = 1; i <= n; ++i) { if (dfs(i)) { if (m == n) ans.push_back(make_pair(ans[0].first, ans.back().second)); for (int i = 0; i < m; ++i) printf( %d %d n , ans[i].first, ans[i].second); return 0; } } printf( -1 n ); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long b, c, d, e, f, g, h, i, j = 0, k = 2, l, o = 0, m = 0, n = 0, q = 1, r = 1, s, t, u, v, x = 1, y = 1, z = 1111111; string a; cin >> a; b = a.size(); for (i = 0; i < b; i++) { if (a[i] == 4 ) n++; if (a[i] == 7 ) m++; } if (n == 0 && m == 0) { cout << -1; return 0; } if (m > n) cout << 7; else cout << 4; }
#include <bits/stdc++.h> using namespace std; using ll = long long; vector<vector<int> > pre; vector<int> starts; vector<int> coproc; vector<int> cnts; vector<int> nexts; void dfs(int v, int co) { for (int p : pre[v]) { cnts[p]--; if (cnts[p] == 0) { if (coproc[p] == co) { nexts.push_back(p); } else { dfs(p, co); } } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; coproc.resize(n); for (int i = 0; i < n; i++) { cin >> coproc[i]; } pre.resize(n); cnts.assign(n, 0); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; pre[v].push_back(u); cnts[u]++; } for (int i = 0; i < n; i++) { if (cnts[i] == 0) { if (coproc[i]) nexts.push_back(i); else starts.push_back(i); } } int cnt = 0; while (starts.size() + nexts.size()) { for (auto v : starts) { dfs(v, 1); } starts.swap(nexts); nexts.clear(); if (starts.size()) { cnt++; } for (auto v : starts) { dfs(v, 0); } starts.swap(nexts); nexts.clear(); } cout << cnt << n ; return false; }
#include <bits/stdc++.h> using namespace std; vector<long long> z_function(string s) { long long n = (long long)s.length(); vector<long long> z(n); for (long long i = 1, l = 0, r = 0; i < n; ++i) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i]; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } return z; } signed main() { ios_base::sync_with_stdio(false); string s, t; cin >> s >> t; string str = t + # + s; auto z = z_function(str); long long n = s.size(), m = t.size(); long long dp[n], pre1[n], pre2[n]; for (long long i = 0; i < n; i++) dp[i] = 0, pre1[i] = 0, pre2[i] = 0; for (long long i = m - 1; i < n; i++) { if (z[i + 2] != m) { if (i) dp[i] = dp[i - 1] % 1000000007; } else { long long pre; if (i == m - 1) pre = 0; else pre = pre2[i - m]; dp[i] = pre + i - m + 2; dp[i] %= 1000000007; } if (i == 0) pre1[i] = dp[i]; else pre1[i] = pre1[i - 1] + dp[i]; if (i == 0) pre2[i] = pre1[i]; else pre2[i] = pre2[i - 1] + pre1[i]; pre1[i] %= 1000000007; pre2[i] %= 1000000007; } long long ans = 0; for (long long i = 0; i < n; i++) { ans += dp[i]; ans %= 1000000007; } cout << ans % 1000000007; }
#include <bits/stdc++.h> using namespace std; vector<vector<int> > g; vector<char> used; vector<long long> distArr, fArr, sArr; long long A, B, diameter; long long answer = 0; struct step { int a, b, d; friend ostream& operator<<(ostream& s, const step& v); }; ostream& operator<<(ostream& s, const step& v) { s << v.a << << v.b << << v.d; return s; } vector<step> arr; void dfs(int v, long long dist, vector<long long>& dArr, long long& cur) { dArr[v] = dist; if (dArr[cur] < dist) cur = v; for (int i = 0; i < (int)g[v].size(); i++) { int to = g[v][i]; if (!used[to]) { used[to] = true; dfs(to, dist + 1, dArr, cur); } } } void dfs2(int v, long long dist, bool flag) { if (!flag) { sArr[v] = dist; } for (int i = 0; i < (int)g[v].size(); i++) { int to = g[v][i]; if (!used[to]) { used[to] = true; dfs2(to, dist + 1, flag); } } if (fArr[v] + sArr[v] > diameter) { step temp; if (fArr[v] > sArr[v]) { temp.a = A + 1; answer += fArr[v]; } else { temp.a = B + 1; answer += sArr[v]; } temp.b = v + 1; temp.d = v + 1; arr.push_back(temp); } if (fArr[v] + sArr[v] == diameter && flag && v != A) { step temp; temp.a = A + 1; temp.d = v + 1; temp.b = v + 1; answer += fArr[v]; arr.push_back(temp); } else if (fArr[v] + sArr[v] == diameter) used[v] = false; } int main() { int n; cin >> n; g.assign(n, vector<int>()); for (int i = 0; i < n - 1; i++) { int from, to; cin >> from >> to; g[from - 1].push_back(to - 1); g[to - 1].push_back(from - 1); } used.assign(n, false); used[0] = true; distArr.assign(n, 0); A = 0; B = 0; dfs(0, 0, distArr, A); used.assign(n, false); used[A] = true; fArr.assign(n, 0); sArr.assign(n, 0); dfs(A, 0, fArr, B); diameter = fArr[B]; used.assign(n, false); used[B] = true; dfs2(B, 0, false); dfs2(A, 0, true); cout << answer << endl; for (int i = 0; i < (int)arr.size(); i++) cout << arr[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> v; long long a, b, f, k; int cnt() { int c = 0; int n = v.size(); long long now = 0; for (int i = 0; i < n; i++) { if (now + b < v[i]) return -1; while (i < n && now + b >= v[i]) i++; i--; c++; now = v[i]; } return c - 1; } int main() { scanf( %I64d%I64d%I64d%I64d , &a, &b, &f, &k); v.push_back(f); for (long long i = 1; i < k; i++) { if (i & 1) v.push_back(v[i - 1] + 2 * (a - f)); else v.push_back(v[i - 1] + 2 * f); } v.push_back(a * k); printf( %d , cnt()); }
#include <bits/stdc++.h> using namespace std; int dp[2][1005][55][55], a[1005], b[1005]; int mx(int x) { return max(0, x); }; int main() { int n, p, k; scanf( %d %d %d , &n, &p, &k); if (p > 2 * (n + k - 1) / k) { p = 2 * (n + k - 1) / k; } int z1; scanf( %d , &z1); for (int i = 1; i <= z1; i++) { int x; scanf( %d , &x); a[x] = 1; } int z2; scanf( %d , &z2); for (int i = 1; i <= z2; i++) { int x; scanf( %d , &x); b[x] = 1; } memset(dp, -0x3f, sizeof(dp)); dp[0][0][0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= p; j++) { for (int x = 0; x < k; x++) { for (int y = 0; y < k; y++) { dp[i % 2][j + 1][k - 1][mx(y - 1)] = max(dp[i % 2][j + 1][k - 1][mx(y - 1)], dp[(i + 1) % 2][j][x][y] + (a[i] || (y && b[i]))); dp[i % 2][j + 1][mx(x - 1)][k - 1] = max(dp[i % 2][j + 1][mx(x - 1)][k - 1], dp[(i + 1) % 2][j][x][y] + (b[i] || (x && a[i]))); dp[i % 2][j][mx(x - 1)][mx(y - 1)] = max(dp[i % 2][j][mx(x - 1)][mx(y - 1)], dp[(i + 1) % 2][j][x][y] + ((a[i] && x) || (b[i] && y))); } } } memset(dp[(i + 1) % 2], -0x3f, sizeof(dp[(i + 1) % 2])); } int res = 0; for (int j = 0; j <= p; j++) { for (int x = 0; x < k; x++) { for (int y = 0; y < k; y++) { res = max(res, dp[n % 2][j][x][y]); } } } printf( %d n , res); }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); long long q; cin >> q; while (q--) { long long l1, r1, l2, r2; cin >> l1 >> r1 >> l2 >> r2; if (l1 == l2) { cout << l1 << << r2 << n ; continue; } if (l1 == r2) { cout << l1 << << l2 << n ; continue; } if (r1 == l2) { cout << r1 << << r2 << n ; continue; } if (r2 == r1) { cout << r1 << << l2 << n ; continue; } cout << l1 << << l2 << n ; } }
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 9; int n, m, a, b, k; char s[100005]; long long ans, q; inline long long f(char a) { return a == - ? -1 : 1; } inline long long fpow(long long a, long long b) { long long r = 1; for (; b; b >>= 1, a = (a * a) % M) if (b & 1) r = (r * a) % M; return r; } inline long long sum(long long a1, long long q, long long n) { if (q == 1) return a1 * n % M; long long p = (fpow(q, n) - 1 + M) % M; long long qq = (q - 1 + M) % M; qq = fpow(qq, M - 2); return a1 * p % M * qq % M; } int main() { scanf( %d %d %d %d , &n, &a, &b, &k); q = b * fpow(a, M - 2) % M; q = fpow(q, k); m = (n + 1) / k; scanf( %s , s); if (n < k) { for (int i = 0; i <= n; i++) { long long t = fpow(a, n - i) * fpow(b, i) % M; if (s[i] == - ) t = M - t; (ans += t) %= M; } return printf( %lld n , ans), 0; } for (int i = 0; i < k; i++) { long long x = sum(fpow(a, n - i) * fpow(b, i) % M, q, m); if (s[i] == - ) x = M - x; (ans += x) %= M; } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> int n, a[105], ans; int comp(const void* elem1, const void* elem2) { int f = *((int*)elem1); int s = *((int*)elem2); if (f > s) return 1; if (f < s) return -1; return 0; } int main(int argc, char* argv[]) { scanf( %d , &n); int i; for (i = 0; i < n; ++i) { int HH, MM; scanf( %d:%d , &HH, &MM); a[i] = HH * 60 + MM; } qsort(a, n, sizeof(a[0]), comp); ans = 0; for (i = 0; i < n - 1; ++i) { if (a[i + 1] - a[i] > ans) { ans = a[i + 1] - a[i]; } } if (a[0] + 24 * 60 - a[n - 1] > ans) { ans = a[0] + 24 * 60 - a[n - 1]; } ans--; int d1, d2; d1 = ans / 60; d2 = ans % 60; if (d1 < 10) { if (d2 < 10) { printf( 0%d:0%d n , d1, d2); } else { printf( 0%d:%d n , d1, d2); } } else { if (d2 < 10) { printf( %d:0%d n , d1, d2); } else { printf( %d:%d n , d1, d2); } } return 0; }
#include <bits/stdc++.h> const int maxn = 100100; const int bl = 512; static const int N = 600; int n, k, q; int s[maxn]; struct block { int pop[N], c0; int push[N], c1; int ok; } o[maxn / bl + 10]; inline int eql(const int *a, const int *b, const int len) { for (int i = 0; i < len; ++i) { if (a[i] != b[i]) return 0; } return 1; } inline bool ask(int l, int r) { static int st[maxn]; int top = 0; for (; l % bl && l <= r; ++l) if (s[l] > 0) { st[++top] = s[l]; } else { if (!top || st[top] != -s[l]) return 0; --top; }; for (; l + bl - 1 <= r; l += bl) { block &o = ::o[l / bl]; if (!o.ok) return 0; if (top < o.c0) return 0; if (!eql(st + top - o.c0 + 1, o.pop + N - o.c0, o.c0)) return 0; top -= o.c0; memcpy(st + top + 1, o.push + 1, o.c1 << 2); top += o.c1; } for (; l <= r; ++l) if (s[l] > 0) { st[++top] = s[l]; } else { if (!top || st[top] != -s[l]) return 0; --top; }; return top == 0; } inline void init(int id) { block &o = ::o[id]; o.c0 = o.c1 = 0; o.ok = 1; int top = 0; const int L = id * bl, R = std::min(n - 1, id * bl + bl - 1); for (int i = L; i <= R; ++i) { if (s[i] > 0) { o.push[++top] = s[i]; } else { if (top && o.push[top] != -s[i]) { o.ok = 0; return; } else { if (top) { --top; } else { o.pop[N - ++o.c0] = -s[i]; } } } } o.c1 = top; } int main() { std::ios::sync_with_stdio(false), std::cin.tie(0); std::cin >> n >> k; for (int i = 0; i < n; ++i) { std::cin >> s[i]; } for (int i = 0; i <= (n - 1) / bl; ++i) init(i); std::cin >> q; for (int i = 0; i < q; ++i) { int opt, pos, v, l, r; std::cin >> opt; if (opt == 1) { std::cin >> pos; --pos; std::cin >> s[pos]; init(pos / bl); } else { std::cin >> l >> r; std::cout << (ask(l - 1, r - 1) ? Yes : No ) << n ; } } }
#include <bits/stdc++.h> using namespace std; using ll = long long int; int main() { int n, k; cin >> n >> k; set<char> st; string s; cin >> s; for (int i = 0; i < n; ++i) st.insert(s[i]); int res = 0; char lastc = 0; int i; for (i = 0; i < k && !st.empty(); ++i) { auto it = st.begin(); char c = *it; st.erase(it); if (*it - lastc < 2) --i; else { res += int(c - a + 1); lastc = c; } } if (i == k) cout << res << n ; else cout << -1 << n ; return 0; }
#include <bits/stdc++.h> const int N = 1000001; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<pair<int, int>> a[n + 1]; int ans = 0; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; a[u].push_back(make_pair(v, i + 1)); a[v].push_back(make_pair(u, i + 1)); ans = max({ans, (int)a[u].size(), (int)a[v].size()}); } cout << ans << n ; int final[n], vis[n + 1]; memset(final, -1, sizeof final); memset(vis, 0, sizeof vis); queue<int> q; q.push(1); vis[1] = 1; while (!q.empty()) { int u = q.front(); q.pop(); map<int, int> d; for (auto [v, ind] : a[u]) { d[final[ind]] = 1; } int day = 1; for (auto [v, ind] : a[u]) { if (final[ind] == -1) { while (d[day]) { ++day; } final[ind] = day++; } if (vis[v] == 0) { vis[v] = 1; q.push(v); } } } vector<int> f1[ans + 1]; for (int i = 1; i < n; i++) { f1[final[i]].push_back(i); } for (int i = 1; i < ans + 1; i++) { cout << f1[i].size() << ; for (int e : f1[i]) { cout << e << ; } cout << n ; } }
#include <bits/stdc++.h> using namespace std; int n, m; struct column { int a[13]; int x; }; column c[2001]; bool cmp(column p, column q) { return p.x > q.x; } int s[13][4096], f[13][4096]; int mian() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf( %d , &c[j].a[i]); for (int j = 1; j <= m; j++) { c[j].x = 0; for (int i = 1; i <= n; i++) c[j].x = max(c[j].x, c[j].a[i]); } sort(c + 1, c + 1 + m, cmp); m = min(m, n); for (int i = 1; i <= m; i++) for (int j = 0; j < (1 << n); j++) { s[i][j] = 0; for (int p = 0; p < n; p++) { int sum = 0; for (int l = 1, k = p + 1; l <= n; l++, k = (k == n) ? 1 : (k + 1)) if (j & (1 << l - 1)) sum += c[i].a[k]; s[i][j] = max(s[i][j], sum); } } for (int i = 1; i <= m; i++) for (int j = 0; j < (1 << n); j++) { f[i][j] = f[i - 1][j]; for (int k = j; k; k = (k - 1) & j) f[i][j] = max(f[i][j], f[i - 1][j ^ k] + s[i][k]); } printf( %d n , f[m][(1 << n) - 1]); return 0; } int t; int main() { for (scanf( %d , &t); t; t--) mian(); return 0; }
#include <bits/stdc++.h> #pragma GCC target( avx2 ) #pragma GCC optimization( O3 ) #pragma GCC optimization( unroll-loops ) using namespace std; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << { << p.first << , << p.second << } ; return os; } const int N = 1e6 + 5; const long long oo = 998244353; long long fact[N]; long long fact_inv[N]; long long extended_euclidean(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long d = extended_euclidean(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } long long mod_inverse(long long a, long long m) { long long x, y; long long g = extended_euclidean(a, m, x, y); if (g != 1) { cout << No solution! ; return -1; } else { x = (x % m + m) % m; return x; } } long long ncr(int n, int r) { return n < r ? 0 : ((1LL * fact[n] % oo * fact_inv[r] % oo) % oo * fact_inv[n - r]) % oo; } void pre(int N) { fact[0] = 1; for (long long i = 1; i < N; i++) { fact[i] = (i * fact[i - 1]) % oo; } fact_inv[N - 1] = mod_inverse(fact[N - 1], oo); for (long long i = N - 2; i >= 0; i--) { fact_inv[i] = (i + 1LL) * fact_inv[i + 1] % oo; } } long long bigmod(long long a, long long p, long long m) { long long res = 1; long long x = a; while (p) { if (p & 1) { res = (res * x) % m; } x = (x * x) % m; p = p >> 1; } return res; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); pre(N); int n; cin >> n; int a = 0, b = 0; int totww = 0, totwb = 0; int totbb = 0, totbw = 0; int tot__ = 0; while (n--) { string s; cin >> s; if (s == WW ) a++; else if (s == W? or s == ?W ) b++; else if (s == B? or s == ?B ) a--, b++; else if (s == BB ) a--; else if (s == ?? ) b += 2, a--; totww += s == WW ; totwb += s == W? ; totwb += s == WB ; totwb += s == ?B ; totbb += s == BB ; totbw += s == B? ; totbw += s == BW ; totbw += s == ?W ; tot__ += s == ?? ; } a = -a; long long ans = 0; if (a <= b and a >= 0) { ans = ncr(b, a); if (totww == 0 and totbb == 0) { long long x = bigmod(2, tot__, oo); ans = (ans - x + oo) % oo; ans += totbw == 0; ans += totwb == 0; } } ans = (ans + oo) % oo; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const int INF = 1 << 30; const long long LINF = 1ll << 60; const int BUFFER_SZ = 1 << 15; const int MOD = 1000 * 1000 * 1000 + 7; char BUFFER[BUFFER_SZ]; int gi() { int x; scanf( %d , &x); return x; } double gd() { double x; scanf( %lf , &x); return x; } long long gll() { long long x; cin >> x; return x; } vector<int> gvi(int n) { vector<int> a; while (n-- > 0) a.push_back(gi()); return a; } string gs() { scanf( %s , BUFFER); return string(BUFFER); } template <class T> void print(vector<T> &x, string format) { format += %c ; for (int i = 0; i < x.size(); ++i) printf(format.c_str(), x[i], i + 1 == x.size() ? n : ); } int c_ins, c_del, c_rep, c_swp; int dp[4444][4444]; int main() { int c_ins = gi(); int c_del = gi(); int c_rep = gi(); int c_swp = gi(); string a = gs(); string b = gs(); int prev[256]; memset((prev), (0), sizeof(prev)); for (int i = 0; i <= a.size(); ++i) { int last = 0; for (int j = 0; j <= b.size(); ++j) { if (!i) dp[i][j] = j * c_ins; else if (!j) dp[i][j] = i * c_del; else { dp[i][j] = 1 << 30; int ii = j > 0 ? prev[b[j - 1]] : 0; int jj = last; if (a[i - 1] == b[j - 1]) { dp[i][j] = dp[i - 1][j - 1]; last = j; } int v = min(min(dp[i - 1][j] + c_del, dp[i][j - 1] + c_ins), dp[i - 1][j - 1] + c_rep); if (i && j && ii && jj) { v = min(v, dp[ii - 1][jj - 1] + c_swp + c_del * (i - ii - 1) + c_ins * (j - jj - 1)); } dp[i][j] = min(dp[i][j], v); } } if (i > 0) { prev[a[i - 1]] = i; } } printf( %d n , dp[a.size()][b.size()]); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; using P = pair<ll, ll>; const int INF = 1001001001; const ll mod = 998244353; const ll INFL = 1000000001000000000; int M = 100005; int main() { int n, q; cin >> n >> q; vector<ll> v(n); for (int i = 0; i < (n); i++) cin >> v[i]; vector<int> c(n); for (int i = 0; i < (n); i++) cin >> c[i]; for (int i = 0; i < (n); i++) c[i]--; while (q--) { ll a, b; cin >> a >> b; vector<ll> dp(M, -INFL); ll bigM1 = 0, bigM2 = 0; int bigC1 = -1, bigC2 = -1; for (int i = 0; i < (n); i++) { if (c[i] == bigC1) { ll nM = max(dp[c[i]] + v[i] * a, bigM2 + v[i] * b); dp[c[i]] = max(dp[c[i]], nM); if (bigM1 < nM) { bigM1 = nM; bigC1 = c[i]; } } else { ll nM = max(dp[c[i]] + v[i] * a, bigM1 + v[i] * b); dp[c[i]] = max(dp[c[i]], nM); if (bigM2 < nM) { bigM2 = nM; bigC2 = c[i]; } if (bigM1 < bigM2) { swap(bigM1, bigM2); swap(bigC1, bigC2); } } } ll ans = 0; for (int i = 0; i < (M); i++) ans = max(ans, dp[i]); printf( %lld n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a == 0) return b; else if (b == 0) return a; else { a = a % b; return gcd(b, a); } } int main() { int n; int sum = 0, odd = 0; scanf( %d , &n); int t; for (int i = 0; i < n; i++) { scanf( %d , &t); sum += t; if (t % 2) odd++; } if (sum % 2) cout << odd; else cout << n - odd; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; char arr1[n][m]; char arr2[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char a; cin >> a; arr1[i][j] = a; arr2[j][i] = a; } } string ans = ; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (count(arr1[i], arr1[i] + m, arr1[i][j]) == 1 && count(arr2[j], arr2[j] + n, arr1[i][j]) == 1) { ans += string(1, arr1[i][j]); } } } cout << ans; }
#include <bits/stdc++.h> static const int INF = std::numeric_limits<int>::max(); int main() { int n; std::cin >> n; std::vector<int> a(n + 1), b(n + 1); for (int i = 1; i <= n; ++i) { std::cin >> a[i]; } for (int i = 1; i <= n; ++i) { std::cin >> b[i]; } std::vector<int> res(n + 1); for (int i = 1; i <= n; ++i) { res[a[i]] = b[i]; } for (int i = 1; i <= n; ++i) { std::cout << res[i] << ; } std::cout << std::endl; }
#include <bits/stdc++.h> using namespace std; int n, m; char c[109][109]; bool ja = 1; bool useless = 1; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> c[i][j]; if (c[i][j] == . ) { if (i % 2 == 0 && j % 2 == 0 || i % 2 == 1 && j % 2 == 1) { c[i][j] = B ; } else { c[i][j] = W ; } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << c[i][j]; } cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e2 + 10; int main() { mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, i, in, last, ans = 0, prelast = -1; cin >> n; cin >> last; for (i = 1; i < n; i++) { cin >> in; if (last == 1 && in == 2) { ans += 3; if (prelast == 3) ans--; } else if (last == 1 && in == 3) ans += 4; else if (last == 2 && in == 1) ans += 3; else if (last == 2 && in == 3) { cout << Infinite n ; return 0; } else if (last == 3 && in == 2) { cout << Infinite n ; return 0; } else if (last == 3 && in == 1) ans += 4; prelast = last; last = in; } cout << Finite n ; cout << ans; }
#include <bits/stdc++.h> using namespace std; const long long e = 1e5 + 110; bool zs[e]; long long num[e]; void sai() { for (long long i = 2; i <= e - 99; i++) { if (zs[i]) continue; for (long long j = i + i; j <= e - 99; j += i) { zs[j] = true; } } for (long long tot = 0, i = 2; i <= e - 99; i++) { if (!zs[i]) tot++, num[tot] = i; } } struct fengjie { long long v, t; } f[e]; long long cnf = 0; void fj(long long x) { long long tmp = x; for (long long i = 2; i <= x; i++) { long long cnt = 0; while (tmp % i == 0) { tmp /= i; cnt++; } if (cnt == 0) continue; cnf++; f[cnf].v = i, f[cnf].t = cnt; } return; } long long ksm(long long a, long long z) { long long ret = 1; if (z == 0) return 1; while (z != 1) { if (z % 2 == 1) { ret *= a; a *= a; } else { a *= a; } z /= 2; } return a * ret; } fengjie an[e]; signed main() { long long n; cin >> n; sai(); fj(n); long long ans = 1, g = 0, cna = 0, minn; for (long long i = cnf; i > 0; i--) { for (long long j = 1; j <= f[i].t; j++) { if (cna == 0) { g++; ans *= ksm(num[g], f[i].v - 1); an[++cna].v = num[g]; an[cna].t = f[i].v - 1; continue; } long long pl = -1; minn = ans * ksm(num[g + 1], f[i].v - 1); for (long long l = 1; l <= cna; l++) { long long ct = f[i].v * (an[l].t + 1) - 1; long long tmp = ans / ksm(an[l].v, an[l].t); tmp *= ksm(an[l].v, ct); if (tmp < ans or ksm(an[l].v, ct) < an[l].v) continue; if (tmp < minn) { pl = l; minn = tmp; } } if (pl == -1) { g++; ans *= ksm(num[g], f[i].v - 1); an[++cna].v = num[g]; an[cna].t = f[i].v - 1; } if (pl != -1) { long long tmp = ans / ksm(an[pl].v, an[pl].t); long long ct = f[i].v * (an[pl].t + 1) - 1; ans = tmp * ksm(an[pl].v, ct); } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; stack<int> s; priority_queue<int> pq; long long maxm = numeric_limits<long long>::max(); const int maxn = 10005; struct node { int l, r; } q[1001]; bool cmp(node a, node b) { return a.l < b.l; } int main() { int n, sum = 0; scanf( %d , &n); int a[maxn], l[maxn], r[maxn]; for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); sum += a[i]; } int m, tmp; scanf( %d , &m); for (int i = 1; i <= m; i++) { scanf( %d%d , &q[i].l, &q[i].r); } sort(q + 1, q + 1 + m, cmp); if (sum > q[m].r) printf( -1 n ); else if (sum < q[1].l) printf( %d n , q[1].l); else { for (int i = 1; i <= m; i++) { if (sum > q[i].r && sum < q[i + 1].l) { printf( %d n , q[i + 1].l); break; } else if (sum >= q[i].l && sum <= q[i].r) { printf( %d n , sum); break; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; bool b[4][n][n]; for (int i = 0; i < 4; i++) { for (int j = 0; j < n; j++) { char c[n + 1]; scanf( %s , &c); for (int k = 0; k < n; k++) b[i][j][k] = c[k] == 1 ? 1 : 0; } } int s[] = {0, 1, 2, 3}, mi = 1e9, co = 0; bool o = 0; do { for (int k = 0; k < 4; k += 2) { for (int j = 0; j < n; j++) { for (int i = 0; i < n; i++) { if (b[s[k]][j][i] != o) co++; o ^= 1; } for (int i = 0; i < n; i++) { if (b[s[k + 1]][j][i] != o) co++; o ^= 1; } o ^= 1; } } mi = min(mi, co); co = 0; } while (next_permutation(s, s + 4)); cout << mi << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int N, M; int A[50050], X[50500]; int L[50050], R[50500]; int XX[1010101]; int ma[50505]; int ret[50505]; signed long long xorxor(signed long long val) { int i; signed long long ret = ((val + 1) / 2) % 2; for (i = 0; i <= 29; i++) if ((val >> i) % 2) ret |= (1 ^ (val % 2)) << i; return ret; } void solve() { int i, j, k, l, r, x, y; string s; for (i = 1; i <= 1000000; i++) XX[i] = XX[i - 1] ^ i; cin >> N >> M; for (i = 0; i < (N); i++) cin >> A[i], X[i] = XX[A[i]]; for (i = 0; i < (M); i++) cin >> L[i] >> R[i], L[i]--, R[i]--; for (x = 0; x < (N); x++) { ma[x - 1] = 0; for (y = x; y < N; y++) ma[y] = max(ma[y - 1], X[x] ^ X[y] ^ ((A[x] > A[y]) ? A[y] : A[x])); for (i = 0; i < (M); i++) if (L[i] <= x) ret[i] = max(ret[i], ma[R[i]]); } for (i = 0; i < (M); i++) cout << ret[i] << endl; } int main(int argc, char** argv) { string s; int i; if (argc == 1) ios::sync_with_stdio(false); for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += n ; for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int n; string a; int f[1005][5]; int main() { cin >> n >> a; string t = , s = ; char cur = 0 ; for (int i = 0; i <= n - 1; i++) { t += cur; s += 1 - cur + 0 ; cur = 1 - cur + 0 ; } int ans = (int)2e9; int res = 0, res1 = 0; for (int i = 0; i <= n - 1; i++) { if (a[i] != s[i]) res++; if (a[i] != t[i]) res1++; } ans = min(res1, res); cout << ans; }
#include <bits/stdc++.h> using namespace std; long long n, m, k; string s; int MOD = 1e9 + 7; const int INF = 1e9; const long long INF64 = 2e18; const double PI = 3.141592653589793238463; long long ar[1000004], ar1[1000004], vis[1000004], pre[1000004], dp[3004][3004]; vector<long long> gr[1000004], path; set<long long> myset; pair<long long, long long> seg[1000004]; map<long long, long long> mymap; long long c = 0; string st1[1000004], st2[1000004]; vector<long long> arr[1000004]; int comp(pair<long long, long long> a, pair<long long, long long> b) { return a.second < b.second; } int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); cin >> n; double w, cargo; cin >> w; for (int i = 0; i < int(n); i++) cin >> ar[i]; for (int i = 0; i < int(n); i++) cin >> ar1[i]; if (ar[0] == 1 || ar1[0] == 1) return cout << -1, 0; cargo = w; w += w / (ar1[0] - 1); for (int i = n - 1; i > 0; i--) { if (ar[i] == 1 || ar1[i] == 1) return cout << -1, 0; w += w / (ar[i] - 1); w += w / (ar1[i] - 1); } w += w / (ar[0] - 1); return printf( %10.10f , w - cargo), 0; }
#include <bits/stdc++.h> using namespace std; const int N = 7005; const int M = 200000; struct state { int l, r, val; state() {} state(int l, int r, int val) { this->l = l; this->r = r; this->val = val; } }; int n, m, dl[M], dr[M]; vector<state> v[N]; int solve(int d, int x, int y) { set<int> ans; for (int i = d; i <= n; i++) { for (int j = 0; j < v[i].size(); j++) { state& c = v[i][j]; if (c.l <= y && c.r >= x) ans.insert(c.val); } x = (dl[x] == -1 ? dr[x] : dl[x]); y = dr[y]; } return ans.size(); } void init() { int cnt = 1, p = 3; memset(dl, -1, sizeof(dl)); memset(dr, -1, sizeof(dr)); dl[1] = 1; dr[1] = 2; for (int i = 2; i < M; i++) { if ((1 << cnt) == i) { cnt++; dl[i] = p++; } dr[i] = p++; } } int main() { init(); int order, d, x, y, val; scanf( %d%d , &n, &m); for (int i = 0; i < m; i++) { scanf( %d , &order); if (order == 1) { scanf( %d%d%d%d , &d, &x, &y, &val); v[d].push_back(state(x, y, val)); } else { scanf( %d%d , &d, &x); printf( %d n , solve(d, x, x)); } } return 0; }
#include <bits/stdc++.h> using namespace std; long long modpow(long long base, long long exp, long long modulus) { base %= modulus; long long result = 1; while (exp > 0) { if (exp & 1) result = (result * base) % modulus; base = (base * base) % modulus; exp >>= 1; } return result; } int main() { long long n, k; cin >> n >> k; long long w[100001]; long long q[100001]; memset(q, 0, sizeof(q)); memset(w, 0, sizeof(w)); w[0] = 1; if (1 >= k) w[1] = 2; else w[1] = 1; q[0] = 0; q[1] += q[0] + w[1]; for (long long i = 2; i < 100001; i++) { if (k <= i) { w[i] += (w[i - 1] + w[i - k] - 1000000007) % 1000000007; } else w[i] = 1; q[i] += (q[i - 1] + w[i] - 1000000007) % 1000000007; q[i] %= 1000000007; } while (n--) { long long a, b; cin >> a >> b; cout << (q[b] - q[a - 1] + 1000000007) % 1000000007 << n ; } return 0; }
#include <bits/stdc++.h> const long long inf = 0x3f3f3f3f; using namespace std; const long long maxn = 2e5 + 100; long long n, k; long long a[maxn]; int32_t main() { long long q; scanf( %lld , &(q)); while (q--) { scanf( %lld%lld , &(n), &(k)); long long maxx = 0, minn = 0x3f3f3f3f; for (long long i = (1); i <= (n); i++) { scanf( %lld , &(a[i])); if (maxx < a[i]) maxx = a[i]; if (minn > a[i]) minn = a[i]; } long long x = minn + k; if (abs(x - maxx) > k) printf( -1 ); else printf( %lld , (x)); printf( n ); } return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { char c = getchar(); int x = 0, f = 1; while (c < 0 || c > 9 ) { if (c == - ) f = -1; c = getchar(); } while (c >= 0 && c <= 9 ) { x = x * 10 + c - 0 ; c = getchar(); } return x * f; } const int maxn = 1e6 + 10; const int inf = 2147483647; int main() { vector<int> v, prim; vector<bool> isprim; int lim = sqrt(1.0 * 1000000000) + 10; isprim.resize(lim); isprim[0] = isprim[1] = 1; for (int i = 2; i <= lim; ++i) { if (isprim[i] == false) { for (int j = i + i; j <= lim; j = j + i) isprim[j] = true; } } for (int i = 2; i <= lim; ++i) { if (isprim[i] == false) prim.push_back(i); } int sz = prim.size(); set<int> se; set<int>::iterator it; int T; scanf( %d , &T); while (T--) { int n = read(); bool flag = false; se.clear(); for (int i = 2; i * i <= n; ++i) { if (n % i == 0 && isprim[i] == false) { se.insert(i); n /= i; flag = true; break; } } if (!flag) { puts( NO ); continue; } flag = false; for (int i = sqrt(n); i >= 2; i--) { if (n % i == 0 && se.count(i) == 0 && se.count(n / i) == 0 && i != n / i) { flag = true; se.insert(i); se.insert(n / i); break; } } if (!flag) { puts( NO ); continue; } puts( YES ); for (it = se.begin(); it != se.end(); ++it) { printf( %d , *it); } puts( ); } return 0; }
#include <bits/stdc++.h> int max(int a, int b) { return a < b ? b : a; } int min(int a, int b) { return a < b ? a : b; } void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } int can(int x, int y, int a, int b) { if ((x <= a && y <= b) || (x <= b && y <= a)) return 1; return 0; } int cal(int x1, int y1, int x2, int y2, int a, int b) { if (x1 > y1) swap(&x1, &y1); if (x2 > y2) swap(&x2, &y2); if (can(x1 + x2, max(y1, y2), a, b)) return x1 * y1 + x2 * y2; if (can(max(x1, x2), y1 + y2, a, b)) return x1 * y1 + x2 * y2; if (can(max(x2, y1), x1 + y2, a, b)) return x1 * y1 + x2 * y2; if (can(max(x1, y2), y1 + x2, a, b)) return x1 * y1 + x2 * y2; return 0; } int main() { int maxx = 0, n, a, b, i, j, s[200][2]; scanf( %d %d %d , &n, &a, &b); for (i = 0; i < n; i++) scanf( %d %d , &s[i][0], &s[i][1]); for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { int k = cal(s[i][0], s[i][1], s[j][0], s[j][1], a, b); if (maxx < k) maxx = k; } } printf( %d n , maxx); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 300000; int x[N][2]; int ans[N]; bool hear[N]; vector<pair<int, int> > G[N]; int color[N]; void dfs1(int s) { color[s] = 1; for (int i = 0; i < (int)G[s].size(); i++) { int v = G[s][i].first; if (color[v] == 0) { if (G[s][i].second > 0) { ans[G[s][i].second] = 1; } else { ans[-G[s][i].second] = 0; } dfs1(v); } } color[s] = 2; } pair<int, int> cyc; void dfs2(int s, int per = -1) { color[s] = 1; for (int i = 0; i < (int)G[s].size(); i++) { int v = G[s][i].first; if (color[v] == 0) { dfs2(v, abs(G[s][i].second)); } if ((color[v] == 1) && (per != abs(G[s][i].second))) { cyc.first = s; cyc.second = i; } } color[s] = 2; } int color1[N]; void dfs3(int s, int per) { color1[s] = 1; for (int i = 0; i < (int)G[s].size(); i++) { int v = G[s][i].first; if (abs(G[s][i].second) == per) { continue; } if (color1[v] == 0) { ans[abs(G[s][i].second)] = (G[s][i].second > 0 ? 1 : 0); dfs3(v, abs(G[s][i].second)); } } color1[s] = 2; } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { int k; scanf( %d , &k); for (int j = 0; j < k; j++) { int r; scanf( %d , &r); if (x[abs(r)][0] == 0) { x[abs(r)][0] = i * (r > 0 ? 1 : -1); } else { x[abs(r)][1] = i * (r > 0 ? 1 : -1); } } } for (int i = 1; i <= m; i++) { if ((x[i][0] >= 0) && (x[i][1] >= 0)) { ans[i] = 1; hear[x[i][0]] = 1; hear[x[i][1]] = 1; } else if ((x[i][0] <= 0) && (x[i][1] <= 0)) { ans[i] = 0; hear[-x[i][0]] = 1; hear[-x[i][1]] = 1; } else { int v1 = abs(x[i][0]); int v2 = abs(x[i][1]); G[v1].push_back(make_pair(v2, i * (x[i][1] > 0 ? 1 : -1))); G[v2].push_back(make_pair(v1, i * (x[i][0] > 0 ? 1 : -1))); } } for (int i = 1; i <= n; i++) { if ((hear[i] == 1) && (color[i] == 0)) { dfs1(i); } } for (int i = 1; i <= n; i++) { if (color[i] == 0) { cyc.first = 0; dfs2(i); if (cyc.first == 0) { cout << NO << endl; return 0; } else { int s = cyc.first; int j = cyc.second; ans[abs(G[s][j].second)] = (G[s][j].second > 0 ? 1 : 0); dfs3(G[s][j].first, abs(G[s][j].second)); } } } cout << YES << endl; for (int i = 1; i <= m; i++) { cout << ans[i]; } cout << endl; }
#include <bits/stdc++.h> using namespace std; const int bits = 10; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<pair<string, int> > commands; for (int i = 0; i < n; ++i) { string tp; int x; cin >> tp >> x; commands.emplace_back(tp, x); } int ansxor = 0; int ansand = (1 << bits) - 1; int ansor = 0; for (int bit = 0; bit < bits; ++bit) { vector<int> f = {0, 1}; for (int index = 0; index < 2; ++index) { for (int i = 0; i < n; ++i) { string tp; int x; tie(tp, x) = commands[i]; x = (x >> bit) & 1; if (tp == & ) f[index] &= x; if (tp == | ) f[index] |= x; if (tp == ^ ) f[index] ^= x; } } if (f == vector<int>{0, 0}) { ansand ^= 1 << bit; } else if (f == vector<int>{1, 0}) { ansxor ^= 1 << bit; } else if (f == vector<int>{1, 1}) { ansor ^= 1 << bit; } } cout << 3 << endl; cout << ^ << ansxor << endl; cout << & << ansand << endl; cout << | << ansor << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<pair<int, int>> times; for (int i = 0; i < m; i++) { int s, e; cin >> s >> e; times.push_back(make_pair(s, e)); } sort(times.begin(), times.end(), [](pair<int, int> a, pair<int, int> b) { return (a.first == b.first) ? (a.second < b.second) : (a.first < b.first); }); vector<int> ans(n, 0); for (auto t : times) { for (int i = t.first + 1; i <= t.second; i++) { ans[i - 1] = !ans[i - 2]; } } for (auto a : ans) cout << a; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> adj[100001]; long long vis[100001] = {0}; long long n; long long nodecolor[100001]; long long treecolor[100001]; long long cc[100001]; map<long long, long long> colorcnt; long long dfs(long long root, long long col) { long long cnt = 1; vis[root] = 1; treecolor[root] = nodecolor[root]; for (auto x : adj[root]) { if (vis[x] == 0 && nodecolor[x] != col) treecolor[root] = -1; } long long flag = 0; for (auto x : adj[root]) { if (vis[x] == 0) { flag = 1; cnt += dfs(x, nodecolor[x]); if (treecolor[x] != nodecolor[root]) treecolor[root] = -1; } } if (flag == 0) { treecolor[root] = nodecolor[root]; } return cc[root] = cnt; } long long fun(long long root) { vis[root] = 1; long long flag = 0; set<long long> st; long long parentcolor = -1; for (auto x : adj[root]) { if (vis[x] == 0) { if (treecolor[x] == -1) flag = 1; } else { parentcolor = nodecolor[x]; } } if (flag == 0) { if (parentcolor == -1) return root; for (auto x : adj[root]) { if (vis[x] == 0) { colorcnt[treecolor[x]] -= cc[x]; } } colorcnt[nodecolor[root]]--; if (colorcnt[parentcolor] == n - cc[root]) { return root; } for (auto x : adj[root]) { if (vis[x] == 0) { colorcnt[treecolor[x]] += cc[x]; } } colorcnt[nodecolor[root]]++; } long long ans = -1; for (auto x : adj[root]) { if (vis[x] == 0) { ans = fun(x); if (ans != -1) return ans; } } return ans; } void solve(long long cno) { long long m, i, j, k, l; cin >> n; long long flag = 0; for (i = 0; i < n - 1; i++) { cin >> j >> k; j--; k--; adj[j].push_back(k); adj[k].push_back(j); } for (i = 0; i < n; i++) { cin >> nodecolor[i]; colorcnt[nodecolor[i]]++; } long long temp = dfs(0, nodecolor[0]); for (i = 0; i < n; i++) vis[i] = 0; long long ans = -1; ans = fun(0); if (ans == -1) cout << NO ; else { cout << YES << n << ans + 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; long long i = 1; while (TESTS--) { solve(i); i++; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; long long dp[N], f[N], ok[N], p[N]; vector<long long> node[N]; long long cur, now; void dfs(long long u, long long p) { for (auto j : node[u]) { if (j == p) continue; dfs(j, u); if (f[j]) { dp[u] += dp[j] + 1; f[u] = 1; } } if (ok[u]) f[u] = 1; } void find_dia(long long u, long long p, long long dis) { if (dis >= cur && ok[u]) { if (dis > cur) now = u; else now = min(now, u); cur = dis; } for (auto j : node[u]) { if (j == p) continue; find_dia(j, u, dis + 1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); if (fopen( A.INP , r )) { freopen( A.INP , r , stdin); freopen( A.OUT , w , stdout); } long long n, m; cin >> n >> m; for (int i = 1; i < n; i++) { long long u, v; cin >> u >> v; node[u].push_back(v); node[v].push_back(u); } for (int i = 1; i <= m; i++) cin >> p[i], ok[p[i]] = 1; dfs(p[1], p[1]); long long ans = dp[p[1]] * 2; cur = 0; now = 1e9; find_dia(p[1], p[1], 0); cur = 0; long long f = now; now = 1e9; find_dia(f, f, 0); cout << min(f, now) << n ; cout << ans - cur; }
#include <bits/stdc++.h> using namespace std; long long Set(long long N, int pos) { return N = N | (1LL << pos); } long long reset(long long N, int pos) { return N = N & ~(1LL << pos); } bool check(long long N, int pos) { return (bool)(N & (1LL << pos)); } struct node { bool endmark; node *next[2]; node() { endmark = 0; for (int i = 0; i < 2; i++) next[i] = NULL; } } * root; void insert(long long m) { node *cur = root; for (int i = 63; i >= 0; i--) { int id = check(m, i); if (cur->next[id] == NULL) cur->next[id] = new node(); cur = cur->next[id]; } cur->endmark = true; } long long query(long long m) { node *cur = root; long long ret = 0; for (int i = 63; i >= 0; i--) { int id = check(m, i); int a = 1 - id; if (cur->next[a] != NULL) { ret = Set(ret, i); cur = cur->next[a]; } else cur = cur->next[id]; } return ret; } long long ara[100005], lft[100005], rgt[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); root = new node(); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> ara[i]; for (int i = 1; i <= n; i++) lft[i] = lft[i - 1] ^ ara[i]; for (int i = n; i >= 1; i--) rgt[i] = rgt[i + 1] ^ ara[i]; long long ans = 0; insert(0); for (int i = 1; i <= n; i++) { long long temp = query(rgt[i]); ans = max(ans, temp); insert(lft[i]); } ans = max(ans, query(0)); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; string to_string(string c) { return string(c); } string to_string(char ch) { return string(1, ch); } template <size_t N> string to_string(bitset<N> B) { string second = ; for (long long i = 0; i < (long long)N; i++) second += (char) 0 + B[i]; return second; } template <typename T, typename U> string to_string(pair<T, U> P) { return { + to_string(P.first) + , + to_string(P.second) + } ; } template <typename T> string to_string(T A) { string second = [ ; bool first = 0; for (auto x : A) { if (first) second += ; first = 1; second += to_string(x); } return second + ] ; } template <typename T, size_t N> string to_string(T (&A)[N]) { string second = [ ; bool first = 0; for (long long i = 0; i < N; i++) { if (first) second += ; first = 1; second += to_string(A[i]); } return second + ] ; } void _deb() { cerr << ] << n ; } template <typename T, typename... U> void _deb(const T& t, const U&... u) { cerr << to_string(t); if (sizeof...(u)) cout << , ; _deb(u...); } long long ceil(long long a, long long b) { return a / b + (a % b && (a ^ b) > 0); } long long floor(long long a, long long b) { return a / b - (a % b && (a ^ b) < 0); } long long SBITS(long long n) { return __builtin_popcountll(n); } long long BITS(long long n) { return 63 - __builtin_clzll(n); } const long long MX = 3e5 + 5; const long long OO = 1e18; const long long MOD = 998244353; long long DX[8] = {1, 0, -1, 0, 1, -1, 1, -1}; long long DY[8] = {0, 1, 0, -1, 1, -1, -1, 1}; long long FAC[MX]; long long FACINV[MX]; long long nCr(long long n, long long r) { if (n < r) return 0; return FAC[n] * FACINV[r] % MOD * FACINV[n - r] % MOD; } long long fastExp(long long base, long long expo, long long mod) { if (expo == 0) return 1; if (expo == 1) return base % mod; long long t = fastExp(base, expo / 2, mod); t = (t * t) % mod; if (expo % 2 == 0) return t; else return ((base % mod) * t) % mod; } void calcFact() { FAC[1] = 1; for (long long i = 2; i < MX; i++) FAC[i] = i * FAC[i - 1] % MOD; FACINV[MX - 1] = fastExp(FAC[MX - 1], MOD - 2, MOD); for (long long i = MX - 1; i > 0; i--) FACINV[i - 1] = i * FACINV[i] % MOD; } void solve() { calcFact(); long long n, ans = 0; cin >> n; vector<long long> l(n), r(n); for (long long i = 0; i < n; i++) cin >> l[i]; for (long long i = 0; i < n; i++) cin >> r[i]; sort(l.begin(), l.end()); sort(r.begin(), r.end()); reverse(r.begin(), r.end()); for (long long i = 0; i < n; i++) ans += abs(l[i] - r[i]); ans %= MOD; ans *= nCr(2 * n, n); ans %= MOD; cout << ans << n ; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; char s[maxn], t[maxn], st[maxn]; long long cnts[maxn], cntt[maxn]; long long Next[maxn]; int n, m; long long ans; long long lent, lens; long long f, e; void init() { ans = 0; lent = lens = 0; s[0] = t[0] = 0; char c[2]; long long x; for (int i = 1; i <= n; i++) { scanf( %I64d-%s , &x, c); if (c[0] == s[lens]) cnts[lens] += x; else { cnts[++lens] = x; s[lens] = c[0]; } } for (int i = 1; i <= m; i++) { scanf( %I64d-%s , &x, c); if (c[0] == t[lent]) cntt[lent] += x; else { cntt[++lent] = x; t[lent] = c[0]; } } } void getnxt() { Next[0] = Next[1] = 0; long long j = 0; for (long long i = 2; i <= lent - 2; i++) { while (j && (st[j + 1] != st[i] || cntt[i] != cntt[j + 1])) j = Next[j]; if (st[j + 1] == st[i] && cntt[i] == cntt[j + 1]) j++; Next[i] = j; } } long long kmp() { long long sum = 0; long long j = 0; for (long long i = 2; i < lens; i++) { while (j && (st[j + 1] != s[i] || cnts[i] != cntt[j + 1])) j = Next[j]; if (st[j + 1] == s[i] && cnts[i] == cntt[j + 1]) j++; if (j == lent - 2) { if (s[i - (lent - 2)] == t[1] && s[i + 1] == t[lent] && cnts[i - (lent - 2)] >= f && cnts[i + 1] >= e) sum++; j = Next[j]; } } return sum; } int main() { while (scanf( %d %d , &n, &m) != EOF) { init(); if (lent == 1) { for (int i = 1; i <= lens; i++) { if (s[i] == t[lent] && cnts[i] >= cntt[lent]) ans += cnts[i] - cntt[lent] + 1; } } else if (lent == 2) { for (int i = 1; i < lens; i++) { if (s[i] == t[1] && s[i + 1] == t[2] && cnts[i] >= cntt[1] && cnts[i + 1] >= cntt[2]) ans++; } } else { strcpy(st + 1, t + 2); st[lent] = 0; f = cntt[1], e = cntt[lent]; for (int i = 1; i < lent; i++) cntt[i] = cntt[i + 1]; getnxt(); ans = kmp(); } printf( %I64d n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1010; int n, a[N], b[N]; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); sort(a + 1, a + 1 + n); for (int i = 1; i <= n / 2; i++) { b[i * 2 - 1] = a[i]; b[i * 2] = a[n - i + 1]; } if (n % 2) b[n] = a[n / 2 + 1]; int flag = 1; for (int i = 3; i <= n; i += 2) { if (b[i] > b[i - 1]) { flag = 0; break; } } for (int i = 2; i <= n && flag; i += 2) { if (b[i] < b[i - 1]) { flag = 0; break; } } if (!flag) { puts( Impossible ); return 0; } for (int i = 1; i <= n; i++) { printf( %d , b[i]); putchar(i == n ? n : ); } return 0; }
#include <bits/stdc++.h> using namespace std; pair<long long, long long> q[9]; char s[2]; int main() { long long n, x0, y0; scanf( %lld , &n); scanf( %lld%lld , &x0, &y0); for (long long m = 0; m < 9; m++) q[m] = make_pair(1E10, INT_MAX); while (n--) { long long x, y, i, j, k, g, m, d; scanf( %s , s); scanf( %lld%lld , &x, &y); i = x - x0, j = y - y0; if (abs(i) != abs(j) && i != 0 && j != 0) continue; k = max(abs(i), abs(j)); i /= k, j /= k; i++, j++; m = 3 * i + j; if (s[0] == Q ) k = 3; else k = (s[0] != R ) + 1; d = abs(x - x0) + abs(y - y0); q[m] = min(q[m], make_pair(d, k)); } for (long long m = 0; m < 9; m++) { long long i = m / 3, j = m % 3, check; i--, j--; if (i == 0 || j == 0) check = 0; else check = 1; if (q[m].first != 1E10 && (q[m].second & (1 << check))) { cout << YES n ; return 0; } } cout << NO n ; return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { long long n, k; cin >> n >> k; vector<long long> a(n); for (auto& i : a) cin >> i; while (a.back() <= k) a.pop_back(); reverse(a.begin(), a.end()); while (a.back() <= k) a.pop_back(); cout << n - a.size() << n ; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 30; signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); string s; cin >> s; int n = s.size(); int pref = 0, suf = n - 1; while (pref < n - 1 && s[pref] != s[pref + 1]) pref++; while (suf > 0 && s[suf] != s[suf - 1]) suf--; int ans = min(n, (s[n - 1] != s[0] ? pref + 1 + (n - suf) : 0)); for (int i = 0; i < n; i++) { int j = i; while (j < n - 1 && s[j] != s[j + 1]) j++; ans = max(ans, j - i + 1); i = j; } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1000000007; const long long INF = 1e18; int mod = 998244353; template <typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; } return 0; } template <typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; } return 0; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } template <typename T, typename U> T pow_(T a, U b) { return b ? pow_(a * a, b / 2) * (b % 2 ? a : 1) : 1; } long long modpow(long long a, long long b, long long _mod) { return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1; } template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << p.first << << p.second; return os; } template <class T> ostream& operator<<(ostream& os, const vector<T>& vec) { for (int i = 0; i < ((int)vec.size()); ++i) { if (i) os << ; os << vec[i]; } return os; } template <typename T> inline istream& operator>>(istream& is, vector<T>& v) { for (int j = 0; j < ((int)v.size()); ++j) is >> v[j]; return is; } template <class T, class T2> inline void add(T& a, T2 b) { a += b; if (a >= mod) a -= mod; } void solve(); int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed << setprecision(10); int T; T = 1; while (T--) solve(); } void solve() { int n; long long k; cin >> n >> k; vector<int> a(n); cin >> a; long long ma = *max_element(a.begin(), a.end()); int ans = 0; vector<int> b(n); auto Get = [&](long long limit) { long long sum = 0; for (int i = 0; i < (n); ++i) { auto binary_search = [&](int ok, int ng) { auto check = [&](int x) { if (a[i] - 3LL * x * (x + 1) - 1 >= limit) return 1; return 0; }; int d = 1; while ((ng - ok >= 0 ? ng - ok : -(ng - ok)) > d) { int mid = (ng + ok) / 2; (check(mid) ? ok : ng) = mid; } return ok; }; b[i] = binary_search(0, a[i] + 1); sum += b[i]; } return sum; }; auto binary_search = [&](long long ok, long long ng) { auto check = [&](long long x) { long long s = Get(x); if (s >= k) return 1; return 0; }; int d = 1; while ((ng - ok >= 0 ? ng - ok : -(ng - ok)) > d) { long long mid = (ng + ok) / 2; (check(mid) ? ok : ng) = mid; } return ok; }; long long L = binary_search(-4e18, 4e18); long long s = Get(L); for (int i = 0; i < (s - k); ++i) { long long mi = INF; int id = -1; for (int j = 0; j < (n); ++j) if (chmin(mi, a[j] - 3LL * b[j] * b[j] + 3 * b[j] - 1)) id = j; b[id]--; } cout << b << n ; }
#include <bits/stdc++.h> using namespace std; int len = 0, coun = 0; bool visited[1000006]; vector<int> check; vector<int> grid[1000006]; void dfs(int v) { visited[v] = true; for (int i = 0; i < grid[v].size(); i++) { if (!visited[grid[v][i]]) { dfs(grid[v][i]); } } check.push_back(v); } void toposort(int n) { memset(visited, false, sizeof(visited)); coun = 0; for (int i = 0; i < n; i++) { if (!visited[i + 1]) dfs(i + 1); } } int main() { int n, m, x, y; cin >> n >> m; map<pair<int, int>, int> ma; for (int i = 0; i < m; i++) { cin >> x >> y; ma[{x, y}] = i; grid[x].push_back(y); } toposort(n); reverse(check.begin(), check.end()); bool ans = true; int maxi = 0; for (int i = 0; i < check.size() - 1; i++) { if (ma.find(make_pair(check[i], check[i + 1])) == ma.end()) ans = false; else maxi = max(maxi, ma[make_pair(check[i], check[i + 1])]); } if (ans == false) cout << -1; else cout << maxi + 1; }
#include <bits/stdc++.h> using namespace std; int n, x, y; vector<vector<int> > adjList(100009, vector<int>()); bool vis[100009]; vector<int> ans; bool check_valid_edges(vector<vector<int> > &a, int n) { for (size_t i = 1; i <= n; i++) if (a[i].size() != 4) return false; return true; } int comp(int node1, int node2) { int c = 0; for (int i = 0; i < adjList[node1].size(); i++) { for (int j = 0; j < adjList[node2].size(); j++) { if (adjList[node1][i] == adjList[node2][j]) c++; } } return c; } void dfs(int node) { vis[node] = true; bool f = true; for (int i = 0; i < adjList[node].size() && f; i++) { if (comp(node, adjList[node][i]) == 2 && !vis[adjList[node][i]]) ans.push_back(adjList[node][i]), dfs(adjList[node][i]), f = false; } } int main() { cin >> n; for (int i = 1; i <= (2 * n); i++) { cin >> x >> y; adjList[x].push_back(y); adjList[y].push_back(x); } if (!check_valid_edges(adjList, n)) return cout << -1 << endl, 0; if (n == 5) return cout << 1 2 3 4 5 n << endl, 0; else if (n == 6) { int s_node = 21; for (int i = 0; i < adjList[1].size(); i++) s_node -= adjList[1][i]; s_node--; cout << 1 << adjList[1][0] << << adjList[1][1] << << s_node << << adjList[1][2] << << adjList[1][3] << endl; ; } else { dfs(1); ans.push_back(1); if (ans.size() == n) for (int i = 0; i < ans.size(); i++) cout << ans[i] << ; else cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int x = 0, y = 0; cin >> x >> y; int z = abs(x) + abs(y); if ((x > 0) && (y > 0)) cout << 0 << << z << << z << << 0; else if ((x < 0) && (y > 0)) cout << -z << << 0 << << 0 << << z; else if ((x > 0) && (y < 0)) cout << 0 << << -z << << z << << 0; else cout << -z << << 0 << << 0 << << -z; return 0; }
#include <bits/stdc++.h> using namespace std; using ii = pair<int, int>; using ll = long long int; using vi = vector<int>; using graph = vector<vi>; const int INF = 0x3f3f3f3f; const ll INFL = 0x3f3f3f3f3f3f3f3f; const int MAXN = 200005; int fq[4]; int main() { int n, m, k; string s; cin >> n; cin >> s; for (auto c : s) { int x = c - 0 ; fq[x]++; } if (n == 1) { cout << s << n ; } else if (n == 2) { if (fq[1] == 2) { cout << 1 << n ; } else { cout << s << n ; } } else { cout << 1; while (fq[0]--) { cout << 0; } } }