solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; char arr[10008]; void generateString(string &x, int beg, int ending) { for (int i = beg; i <= ending; i++) { x.push_back(arr[i]); } } int vis[10008][2]; string currentWord; int numberOfSuffixes = 0; bool wordPossibility[32][32][32]; void printConvertion(char x) { if (x >= 'a') { cout << x; } } void printWords() { int limit = 'z' + 1 - 'a'; for (int a = 0; a <= limit; a++) { for (int b = 0; b <= limit; b++) { for (int c = 0; c <= limit; c++) { if (wordPossibility[a][b][c]) { printConvertion(a + 'a' - 1); printConvertion(b + 'a' - 1); printConvertion(c + 'a' - 1); cout << "\n"; } } } } } int f(int n, bool isThree) { if (n <= 3) { return 0; } if (vis[n][isThree] != -1) { return vis[n][isThree]; } vis[n][isThree] = 1; string afterString; if (isThree) { generateString(afterString, n + 1, n + 3); } else { generateString(afterString, n + 1, n + 2); } if (n >= 5) { string twoString; generateString(twoString, n - 1, n); if (twoString != afterString) { if (f(n - 2, 0)) { vis[n][isThree] = 1; twoString.push_back('a' - 1); if (!wordPossibility[twoString.at(0) - 'a' + 1] [twoString.at(1) - 'a' + 1] [twoString.at(2) - 'a' + 1]) { wordPossibility[twoString.at(0) - 'a' + 1][twoString.at(1) - 'a' + 1] [twoString.at(2) - 'a' + 1] = 1; numberOfSuffixes++; } } } } if (n >= 6) { string threeString; generateString(threeString, n - 2, n); if (threeString != afterString) { if (f(n - 3, 1)) { vis[n][isThree] = 1; if (!wordPossibility[threeString.at(0) - 'a' + 1] [threeString.at(1) - 'a' + 1] [threeString.at(2) - 'a' + 1]) { wordPossibility[threeString.at(0) - 'a' + 1] [threeString.at(1) - 'a' + 1] [threeString.at(2) - 'a' + 1] = 1; numberOfSuffixes++; } } } } return vis[n][isThree]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); memset(vis, -1, sizeof(vis)); cin >> arr; f(strlen(arr) - 1, 0); cout << numberOfSuffixes << "\n"; printWords(); return 0; }
10
#include <bits/stdc++.h> using namespace std; long long spf[5000001]; long long ans[5000001]; int main() { long long t, l, r; cin >> t >> l >> r; for (int i = 1; i < 5000001; i++) { spf[i] = i; } for (long long i = 2; i * i < 5000001; i++) { if (spf[i] == i) { for (long long j = 2 * i; j < 5000001; j += i) { if (spf[j] == j) spf[j] = i; } } } int mod = 1e9 + 7; for (long long i = 2; i <= 5000000; i++) { if (spf[i] == i) ans[i] = (i * (i - 1) / 2) % mod; else { long long ss = (i / spf[i]); ans[i] = (((ans[spf[i]] * ss) % mod) + (ans[ss])) % mod; } } long long result = 0; long long tt = 1LL; for (int i = l; i <= r; i++) { result = (result + ((tt * ans[i]) % mod)) % mod; tt = (tt * t) % mod; } cout << result; }
10
#include <bits/stdc++.h> using namespace std; char M[42][42]; int contz[42][42]; int ans[42][42][42][42]; int main() { int n, m, q; scanf("%d %d %d", &n, &m, &q); for (int i = 0; i < n; ++i) scanf("%s", M[i]); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) contz[i][j] = (M[i - 1][j - 1] == '0' ? 1 : 0) + contz[i - 1][j] + contz[i][j - 1] - contz[i - 1][j - 1]; for (int r1 = 1; r1 <= n; ++r1) for (int c1 = 1; c1 <= m; ++c1) for (int r2 = r1; r2 <= n; ++r2) for (int c2 = c1; c2 <= m; ++c2) if (contz[r2][c2] - contz[r1 - 1][c2] - contz[r2][c1 - 1] + contz[r1 - 1][c1 - 1] == (r2 - r1 + 1) * (c2 - c1 + 1)) ++ans[r1][c1][r2][c2]; for (int r1 = 1; r1 <= n; ++r1) for (int c1 = 1; c1 <= m; ++c1) for (int r2 = r1; r2 <= n; ++r2) for (int c2 = c1; c2 <= m; ++c2) ans[r1][c1][r2][c2] += ans[r1][c1][r2 - 1][c2] + ans[r1][c1][r2][c2 - 1] - ans[r1][c1][r2 - 1][c2 - 1]; for (int r2 = 1; r2 <= n; ++r2) for (int c2 = 1; c2 <= m; ++c2) for (int r1 = r2; r1 >= 1; --r1) for (int c1 = c2; c1 >= 1; --c1) ans[r1][c1][r2][c2] += ans[r1 + 1][c1][r2][c2] + ans[r1][c1 + 1][r2][c2] - ans[r1 + 1][c1 + 1][r2][c2]; int r1, c1, r2, c2; while (q--) { scanf("%d %d %d %d", &r1, &c1, &r2, &c2); printf("%d\n", ans[r1][c1][r2][c2]); } return 0; }
11
#include <bits/stdc++.h> using namespace std; pair<int, int> p[205]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> p[i].first >> p[i].second; int ans = 0; for (int i = 1; i <= n; i++) { int flag[4] = {0}; for (int j = 1; j <= n; j++) { if (j == i) continue; if (p[j].first == p[i].first && p[j].second < p[i].second) flag[0] = 1; if (p[j].first == p[i].first && p[j].second > p[i].second) flag[1] = 1; if (p[j].first < p[i].first && p[j].second == p[i].second) flag[2] = 1; if (p[j].first > p[i].first && p[j].second == p[i].second) flag[3] = 1; } if (flag[0] + flag[1] + flag[2] + flag[3] == 4) ans++; } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); } template <class T1, class T2> inline void gmax(T1 &a, T2 b) { if (b > a) a = b; } template <class T1, class T2> inline void gmin(T1 &a, T2 b) { if (b < a) a = b; } const int N = 0, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f; long long m, n; long long X; int k; long long f[65][2][65]; long long dp(int p, bool tp, int num) { if (num == 0) return 1; if (p == -1) return 0; long long &ret = f[p][tp][num]; if (~ret) return ret; bool top = tp ? (X >> p & 1) : 1; ret = 0; for (int i = 0; i <= top; ++i) ret += dp(p - 1, tp && i == top, num - (i == 1)); return ret; } int main() { while (~scanf("%lld%d", &m, &k)) { long long l = 1; long long r = 1e18; while (l < r) { long long mid = (l + r) >> 1; long long cnt = 0; memset(f, -1, sizeof(f)); X = mid + mid; cnt += dp(63, 1, k); memset(f, -1, sizeof(f)); X = mid; cnt -= dp(63, 1, k); cnt >= m ? r = mid : l = mid + 1; } printf("%lld\n", l); } return 0; }
13
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 15, MAXL = 11; struct FN { int bit[MAXN][MAXL][MAXL][4]; int get(int p, int mod, int rem, int c) { int ret = 0; for (p++; p; p -= p & -p) ret += bit[p][mod][rem][c]; return ret; } void update(int p, int mod, int rem, int c, int val) { for (p++; p < MAXN; p += p & -p) bit[p][mod][rem][c] += val; } int _(char g) { if (g == 'A') return 0; if (g == 'T') return 1; if (g == 'G') return 2; return 3; } void upd(int p, const string& s, int val) { int c = _(s[p]); for (int mod = 1; mod < MAXL; mod++) update(p, mod, p % mod, c, val); } int query(int l, int r, string e, const string& s) { int ret = 0; r--, l--; if (r - l + 1 <= (int)e.size()) { for (int i = l; i <= r; i++) ret += (s[i] == e[i - l]); return ret; } int j = 0; while (l % (int)e.size()) { if (e[j] == s[l]) ret++; l++; j++; } string d = e.substr(0, j); e = e.substr(j, (int)e.size() - j); e = e + d; for (int i = 0; i < (int)e.size(); i++) { if (l) ret += get(r, (int)e.size(), i, _(e[i])) - get(l - 1, (int)e.size(), i, _(e[i])); else ret += get(r, (int)e.size(), i, _(e[i])); } return ret; } } tree; string s; int q; int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cerr << "HELLO WORLD :)\n"; cin >> s >> q; for (int i = 0; i < (int)s.size(); i++) tree.upd(i, s, 1); while (q--) { int t; cin >> t; if (t == 1) { int x; char c; cin >> x >> c; x--; tree.upd(x, s, -1); s[x] = c; tree.upd(x, s, 1); } else { int l, r; string e; cin >> l >> r >> e; cout << tree.query(l, r, e, s) << '\n'; } } return false; }
13
#include <bits/stdc++.h> using namespace std; using llu = unsigned long long; const int N = 1e5 + 10; int n, m; llu h[N], ngh[N]; int ans[N]; int main() { scanf("%d%d", &n, &m); llu All = 0; h[0] = 1; for (auto i = 1, _end = n; i <= _end; ++i) { h[i] = h[i - 1] * 2345678901239ll; All ^= h[i]; } while (m--) { int u, v; scanf("%d%d", &u, &v); ngh[u] ^= h[v]; ngh[v] ^= h[u]; } map<llu, int> S; for (auto u = 1, _end = n; u <= _end; ++u) S.insert({ngh[u], 0}); int cnt = 0; for (auto &s : S) s.second = ++cnt; if (cnt != 3) return puts("-1"), 0; llu g[4] = {}; for (auto u = 1, _end = n; u <= _end; ++u) { ans[u] = S[ngh[u]]; g[ans[u]] ^= h[u]; } assert((g[1] ^ g[2] ^ g[3]) == All); for (auto i = 1, _end = 3; i <= _end; ++i) if (S[All ^ g[i]] != i) return puts("-1"), 0; for (auto u = 1, _end = n; u <= _end; ++u) printf("%d%c", ans[u], " \n"[u == n]); }
11
#include <bits/stdc++.h> using namespace std; vector<int> arr; bool revisa(int n, int k) { vector<int> dv; if (k > n) swap(k, n); for (int i = 1; i * i <= k; i++) { if (k % i == 0) { int div2 = k / i; dv.push_back(i); if (div2 != i) dv.push_back(div2); } } for (int i = 1; i < dv.size(); i++) if (n % dv[i] == 0) return true; return false; } int main() { int n; cin >> n; int cuenta = 0; for (int i = 0; i < n; i++) { int t; cin >> t; arr.push_back(t); arr.push_back(0); } if (n == 1) { cout << 0 << endl << arr[0] << endl; } else { int y; if (n % 2 != 0) y = 2; else y = 3; for (int i = 0; i < arr.size() - y; i += 2) { if (arr[i] == arr[i + 2] && arr[i] != 1) { arr[i + 1] = 1; cuenta++; } else if (revisa(arr[i], arr[i + 2]) == true) { arr[i + 1] = 1; cuenta++; } } cout << cuenta << endl; for (int i = 0; i < arr.size(); i++) { if (arr[i] != 0) cout << arr[i] << " "; } } }
4
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("-O3") vector<long long> v[8000000]; long long isithBitSet(long long n, long long k) { if (n & (1LL << (k))) return 1; else return 0; } vector<long long> tempOne; vector<long long> tempZero; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; long long x; long long index = -1; vector<long long> finalAns; for (long long i = 0; i < n; i++) { cin >> x; v[index + 1].push_back(x); } long long ans = 0; long long count = 0; for (long long i = 31; i >= 0; i--) { long long temp = index + 1; index = count; long long ansOne = 0; long long ansZero = 0; for (long long j = temp; j <= index; j++) { long long len = v[j].size(); long long one = 0; long long zero = 0; tempOne.clear(); tempZero.clear(); for (long long k = (len - 1); k >= 0; k--) { if (isithBitSet(v[j][k], i)) { ansZero += zero; one++; tempZero.push_back(v[j][k]); } else { ansOne += one; zero++; tempOne.push_back(v[j][k]); } } if (tempOne.size() > 1) { v[count + 1] = tempOne; reverse((v[count + 1]).begin(), (v[count + 1]).end()); count++; } if (tempZero.size() > 1) { v[count + 1] = tempZero; reverse((v[count + 1]).begin(), (v[count + 1]).end()); count++; } } ans += min(ansOne, ansZero); if (ansZero <= ansOne) { finalAns.push_back(0); } else { finalAns.push_back(1); } } cout << ans << " "; ans = 0; long long power2 = 1; reverse((finalAns).begin(), (finalAns).end()); for (long long i = 0; i < (long long)finalAns.size(); i++) { ans += finalAns[i] * power2; power2 *= 2; } cout << ans; return 0; }
12
#include <bits/stdc++.h> using namespace std; map<long long, long long> m; int main() { long long n; cin >> n; long long a[n + 10]; long long cnt = 0; for (long long i = 1; i <= n; i++) { cin >> a[i]; while (m[a[i]]) { a[m[a[i]]] = 0; m[a[i]] = 0; a[i] = a[i] << 1; cnt++; } m[a[i]] = i; } cout << n - cnt << endl; for (long long i = 1; i <= n; i++) { if (a[i] != 0) cout << a[i] << " "; } }
8
#include <bits/stdc++.h> using namespace std; const long long int N = 2e5 + 5; const long long int mod = 1e9 + 7; bool flag; long long int ans = 0; vector<long long int> graph[N], degree(N, 0); vector<bool> vis(N, false); void dfs(long long int u) { if (degree[u] != 2) flag = false; vis[u] = true; for (long long int v : graph[u]) { if (!vis[v]) { dfs(v); } } } void solve() { long long int n, m, a, b; cin >> n >> m; for (long long int i = 0; i < m; i++) { cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); degree[a]++; degree[b]++; } for (long long int i = 1; i <= n; i++) { flag = true; if (!vis[i]) { dfs(i); if (flag) ans++; } } cout << ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); clock_t t1, t2; t1 = clock(); long long int t = 1; while (t--) { solve(); } return 0; }
7
#include <bits/stdc++.h> using namespace std; vector<int> dis(vector<vector<int> >& v, int x) { int n = v.size(); vector<int> visited(n, -1); queue<int> q; q.push(x); visited[x] = 0; while (q.size()) { int t = q.front(); int val = visited[t]; q.pop(); for (int i = 0; i < v[t].size(); i++) { if (visited[v[t][i]] == -1) { visited[v[t][i]] = val + 1; q.push(v[t][i]); } } } return visited; } void solve() { int n, m, a, b, c; cin >> n >> m >> a >> b >> c; a--; b--; c--; vector<vector<int> > v(n); vector<long long> p(m + 1); for (int i = 0; i < m; i++) { cin >> p[i + 1]; } sort(p.begin(), p.end()); for (int i = 1; i < p.size(); i++) { p[i] += p[i - 1]; } for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--; y--; v[x].push_back(y); v[y].push_back(x); } vector<int> aa = dis(v, a); vector<int> bb = dis(v, b); vector<int> cc = dis(v, c); long long R = 1e18; for (int i = 0; i < n; i++) { int a1 = aa[i]; int b1 = bb[i]; int c1 = cc[i]; if (a1 + b1 + c1 < p.size()) { long long r = p[b1] + p[a1 + b1 + c1]; R = min(R, r); } } cout << R << endl; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { solve(); } }
13
#include <bits/stdc++.h> template <typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; } template <typename First, typename... Rest> void MACRO_VAR_Scan(First& first, Rest&... rest) { std::cin >> first; MACRO_VAR_Scan(rest...); } template <typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); } template <typename First, typename... Rest> void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); } template <typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; } template <typename First, typename... Rest> void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); } template <class T> inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; } template <class T> using V = std::vector<T>; template <class T> using VV = V<V<T>>; template <typename S, typename T> std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } using ll = long long; using ull = unsigned long long; using ld = long double; using PAIR = std::pair<int, int>; using PAIRLL = std::pair<ll, ll>; constexpr int INFINT = 1 << 30; constexpr int INFINT_LIM = (1LL << 31) - 1; constexpr ll INFLL = 1LL << 60; constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); constexpr double EPS = 1e-6; constexpr int MOD = 1000000007; constexpr double PI = 3.141592653589793238462643383279; template <class T, size_t N> void FILL(T (&a)[N], const T& val) { for (auto& x : a) x = val; } template <class ARY, size_t N, size_t M, class T> void FILL(ARY (&a)[N][M], const T& val) { for (auto& b : a) FILL(b, val); } template <class T> void FILL(std::vector<T>& a, const T& val) { for (auto& x : a) x = val; } template <class ARY, class T> void FILL(std::vector<std::vector<ARY>>& a, const T& val) { for (auto& b : a) FILL(b, val); } struct Trie { std::vector<std::vector<int>> g; int p; Trie() : p(0) { g.emplace_back(std::vector<int>(2, -1)); ++p; } void add(const std::string& s) { size_t par = 0; for (size_t i = 0; i < s.size(); ++i) { int t = s[i] - '0'; if (g[par][t] == -1) { g.emplace_back(std::vector<int>(2, -1)); g[par][t] = p++; } par = g[par][t]; } } int next(size_t par, char c) { int t = c - '0'; return g[par][t]; } }; bool ng(std::string s) { return s == "1100" || s == "1010" || s == "0111" || s == "1111"; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); ; int n; MACRO_VAR_Scan(n); ; std::string s; for (int i = 0; i < int(n); ++i) { char c; MACRO_VAR_Scan(c); ; s += c; } Trie tr; std::vector<ll> c(n, 0); for (int i = 0; i < int(n); ++i) { auto S = s.substr(0, i + 1); std::reverse((S).begin(), (S).end()); std::vector<ll> dp(S.size() + 1, 0); dp[0] = 1; for (int j = 0; j < int(S.size()); ++j) { for (int k = 0; k < int(4); ++k) { if (j < k) break; if (k == 3) { if (ng(S.substr(j - 3, 4))) continue; } (dp[j + 1] += dp[j - k]) %= MOD; } } int p = 0; int len = 0; for (int j = 0; j < int(S.size()); ++j) { int ne = tr.next(p, S[j]); if (ne == -1) break; p = ne; dp[++len] = 0; } tr.add(S); for (int j = (1); j < (S.size() + 1); ++j) { (c[i] += dp[j]) %= MOD; } } for (int i = 0; i < int(n - 1); ++i) (c[i + 1] += c[i]) %= MOD; for (int i = 0; i < int(n); ++i) { std::cout << (c[i]); std::cout << "\n"; ; } return 0; }
16
#include <bits/stdc++.h> using namespace std; const int N = 2e3 + 10; const long long mod = 1e9 + 7; long long cal[N][N]; void init(int n) { cal[0][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= i; j++) { if (!j) cal[i][j] = 1; else cal[i][j] = (cal[i - 1][j - 1] + cal[i - 1][j]) % mod; } } long long sum[N][N]; long long query(int x1, int y1, int x2, int y2) { x1 = max(x1, 1); y1 = max(y1, 1); x2 = min(x2, 2000); y2 = min(y2, 2000); if (x1 > x2 || y1 > y2) return 0LL; return sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1]; } int main() { init(2e3); int n, m, r; scanf("%d %d %d", &n, &m, &r); vector<int> X(n + 1), Y(n + 1), B(n + 1); for (int i = 1; i <= n; i++) scanf("%d %d %d", &X[i], &Y[i], &B[i]); for (int i = 1; i <= n; i++) sum[X[i]][Y[i]]++; for (int i = 1; i <= 2000; i++) for (int j = 1; j <= 2000; j++) sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; long long ans = 0; for (int i = 1; i <= n; i++) { long long cnt = query(X[i] - r, Y[i] - r, X[i] + r, Y[i] + r); ans += 1LL * B[i] * B[i] % mod * ((cal[n][m] - cal[n - cnt][m] + mod) % mod) % mod; ans %= mod; } for (int i = 1; i <= n; i++) { long long cnt1 = query(X[i] - r, Y[i] - r, X[i] + r, Y[i] + r); for (int j = i + 1; j <= n; j++) { long long cnt2 = query(X[j] - r, Y[j] - r, X[j] + r, Y[j] + r); long long cnt3 = query(max(X[i], X[j]) - r, max(Y[i], Y[j]) - r, min(X[i], X[j]) + r, min(Y[i], Y[j]) + r); ans += 2LL * B[i] * B[j] % mod * (cal[n][m] - cal[n - cnt1][m] - cal[n - cnt2][m] + cal[n - cnt1 - cnt2 + cnt3][m] + mod) % mod; ans %= mod; } } printf("%lld\n", ans); return 0; }
15
#include <bits/stdc++.h> namespace my_std { using namespace std; const long long mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template <typename T> inline T rnd(T l, T r) { return uniform_int_distribution<T>(l, r)(rng); } template <typename T> inline void read(T& t) { t = 0; char f = 0, ch = getchar(); double d = 0.1; while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar(); while (ch <= '9' && ch >= '0') t = t * 10 + ch - 48, ch = getchar(); if (ch == '.') { ch = getchar(); while (ch <= '9' && ch >= '0') t += d * (ch ^ 48), d *= 0.1, ch = getchar(); } t = (f ? -t : t); } template <typename T, typename... Args> inline void read(T& t, Args&... args) { read(t); read(args...); } void file() {} } // namespace my_std using namespace my_std; int n, K; struct hh { int t, nxt; } edge[101010 << 1]; int head[101010], ecnt; void make_edge(int f, int t) { edge[++ecnt] = (hh){t, head[f]}; head[f] = ecnt; edge[++ecnt] = (hh){f, head[t]}; head[t] = ecnt; } inline long long add(long long x, long long y) { x += y; if (x >= mod) x -= mod; return x; } long long dp[101010][233], f[101010], ans[233]; int size[101010]; void dfs(int x, int fa) { size[x] = 1; dp[x][0] = 1; for (int i = head[x]; i; i = edge[i].nxt) if (edge[i].t != fa) { dfs(edge[i].t, x); for (int j = (0); j <= (K); j++) f[j] = dp[x][j]; for (int j = (1); j <= (min(K, size[edge[i].t])); j++) dp[x][j] = add(dp[x][j], add(dp[edge[i].t][j], dp[edge[i].t][j - 1])); dp[x][0] = add(dp[x][0], dp[edge[i].t][0]); for (int j = (0); j <= (min(K, size[x])); j++) for (int k = (0); k <= (min(K - j, size[edge[i].t])); k++) { long long val = f[j] * dp[edge[i].t][k] % mod; dp[x][j + k] = add(dp[x][j + k], val); ans[j + k] = add(ans[j + k], val); dp[x][j + k + 1] = add(dp[x][j + k + 1], val); ans[j + k + 1] = add(ans[j + k + 1], val); } size[x] += size[edge[i].t]; } } void solve() { dfs(1, 0); } long long S[233][233]; int main() { file(); int x, y; read(n, K); for (int i = (1); i <= (n - 1); i++) read(x, y), make_edge(x, y); S[1][1] = 1; for (int i = (2); i <= (K); i++) for (int j = (1); j <= (K); j++) S[i][j] = add(S[i - 1][j - 1], S[i - 1][j] * j % mod); int fac = 1, Ans = 0; solve(); for (int i = (1); i <= (K); i++) { fac = 1ll * fac * i % mod; Ans = add(Ans, 1ll * fac * S[K][i] % mod * ans[i] % mod); } cout << Ans; return 0; }
22
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ull = unsigned long long int; using dd = double; using ldd = long double; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll n, r; cin >> n >> r; vector<ll> arr((ll(1)) << n); ll sum = 0; n = ((ll)1) << n; for (int i = 0; i < n; ++i) { cin >> arr[i]; sum += arr[i]; } cout.precision(10); cout << ((dd)sum) / (dd(n)) << '\n'; for (int i = 0; i < r; ++i) { ll a, b; cin >> a >> b; sum -= arr[a]; sum += b; arr[a] = b; cout << ((dd)sum) / (dd(n)) << '\n'; } }
17
#include <bits/stdc++.h> using namespace std; const int N = 15e4 + 5, MAX = 1e6 + 5; int n, q, a[N], fa[N], upb, node[MAX]; set<pair<int, int> > s; struct factorer { int tot, p[9]; } fct[MAX]; int find(int u) { return fa[u] == u ? u : fa[u] = find(fa[u]); } void merge(int u, int v) { u = find(u), v = find(v); if (u != v) fa[u] = v; } void read(int &x) { int res = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) res = res * 10 + ch - '0', ch = getchar(); x = res; } void init() { for (register int i = 2; i <= upb; ++i) if (!fct[i].tot) for (int j = 1; j * i <= upb; ++j) fct[i * j].p[++fct[i * j].tot] = i; for (register int i = 1; i <= n; ++i) for (int j = 1, num; j <= fct[a[i]].tot; ++j) if (!node[num = fct[a[i]].p[j]]) node[num] = i; else merge(node[num], i); for (register int i = 1; i <= n; ++i) { int newnum = a[i] + 1, cnt = 0, mem[9] = {find(i)}; for (int j = 1; j <= fct[newnum].tot; ++j) { mem[++cnt] = find(node[fct[newnum].p[j]]); } for (register int j = 1; j <= cnt; ++j) for (int k = 0; k < j; ++k) { s.insert({mem[j], mem[k]}), s.insert({mem[k], mem[j]}); } } } int main() { read(n), read(q); for (register int i = 1; i <= n; ++i) read(a[i]), fa[i] = i, upb = max(upb, a[i] + 1); init(); for (register int i = 1, u, v; i <= q; ++i) { read(u), read(v); u = find(u), v = find(v); if (u == v) puts("0"); else if (s.count({u, v})) puts("1"); else puts("2"); } return 0; }
19
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int a[105]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } a[++n] = 1001; int ans = 0; int len = 1; int pre = 0; for (int i = 1; i <= n; ++i) { if (a[i] == pre + 1) { pre++; len++; } else { ans = max(ans, len - 2); len = 1; pre = a[i]; } } ans = max(ans, len - 2); printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { std: ios::sync_with_stdio(false); long long N; cin >> N; cout << N / 2520 << "\n"; }
3
#include <bits/stdc++.h> using namespace std; void Read(int &p) { p = 0; int f = 0; char c = getchar(); for (; c < '0' || c > '9'; c = getchar()) f |= (c == '-'); for (; c >= '0' && c <= '9'; c = getchar()) p = p * 10 + c - '0'; if (f) p = -p; } int n, d, k, A[200005]; int lst[200005], ans; int stk1[200005], top1; int stk2[200005], top2; pair<int, int> V[200005]; struct SGT { int laz, mnn, l, r; } S[200005 << 2]; void Pushup(int q) { S[q].mnn = min(S[q << 1].mnn, S[q << 1 | 1].mnn); } void Pushdown(int q) { if (S[q].laz) { S[q << 1].mnn += S[q].laz; S[q << 1 | 1].mnn += S[q].laz; S[q << 1].laz += S[q].laz; S[q << 1 | 1].laz += S[q].laz; } S[q].laz = 0; } void Build(int q, int l, int r) { S[q].l = l, S[q].r = r; int mid = (l + r) >> 1; if (l == r) { S[q].mnn = l; return; } Build(q << 1, l, mid); Build(q << 1 | 1, mid + 1, r); Pushup(q); } void Modify(int q, int L, int R, int v) { if (S[q].l >= L && S[q].r <= R) { S[q].mnn += v; S[q].laz += v; return; } int mid = (S[q].l + S[q].r) >> 1; Pushdown(q); if (L <= mid) Modify(q << 1, L, R, v); if (R > mid) Modify(q << 1 | 1, L, R, v); Pushup(q); } int Query(int q, int v) { if (S[q].mnn > v) return 0; if (S[q].l == S[q].r) return S[q].l; Pushdown(q); if (S[q << 1].mnn <= v) return Query(q << 1, v); return Query(q << 1 | 1, v); } void Find(int q, int L, int R, int v) { if (S[q].l >= L && S[q].r <= R) { if (!ans) ans = Query(q, v); return; } int mid = (S[q].l + S[q].r) >> 1; Pushdown(q); if (L <= mid) Find(q << 1, L, R, v); if (R > mid) Find(q << 1 | 1, L, R, v); } int main() { Read(n), Read(k), Read(d); for (int i = 1; i <= n; i++) Read(A[i]); if (!d) { int ans = 0; for (int i = n; i >= 1; i--) { if (A[i] == A[i + 1]) lst[i] = lst[i + 1]; else lst[i] = i; ans = max(ans, lst[i] - i); } for (int i = 1; i <= n; i++) if (lst[i] - i == ans) { printf("%d %d\n", i, lst[i]); break; } return 0; } for (int i = 1; i <= n; i++) V[i] = make_pair(A[i], i); sort(V + 1, V + n + 1); for (int i = 2; i <= n; i++) if (V[i - 1].first == V[i].first) lst[V[i].second] = V[i - 1].second; Build(1, 1, n); int lef = 0; int u = 1, v = 0; for (int i = 1; i <= n; i++) { if (i == 1 || (A[i] % d + d) % d != (A[i - 1] % d + d) % d) top1 = top2 = 0, lef = i; while (top1 && A[stk1[top1]] < A[i]) { Modify(1, stk1[top1 - 1] + 1, stk1[top1], (A[i] - A[stk1[top1]]) / d); top1--; } stk1[++top1] = i; while (top2 && A[stk2[top2]] > A[i]) { Modify(1, stk2[top2 - 1] + 1, stk2[top2], (A[stk2[top2]] - A[i]) / d); top2--; } stk2[++top2] = i; lef = max(lef, lst[i] + 1); ans = 0; Find(1, lef, i, i + k); if (i - ans > v - u) u = ans, v = i; } printf("%d %d\n", u, v); }
23
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; for (int i = 0; i < t; ++i) { long long int a, b, c, d; cin >> a >> b >> c >> d; if (a - c == 0) cout << abs(b - d) << endl; else if (b - d == 0) cout << abs(a - c) << endl; else { long long int sum = abs(a - c) + abs(b - d) + 2; cout << sum << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; const int mo = 1e9 + 7; int ans, f, n, m, h, s; int a[maxn], c[maxn], k, sum[maxn]; int b[maxn], flag, tmp; int dp[maxn]; int main() { int T; int cas = 1; while (cin >> n) { flag = 0; cout << n << " "; for (int i = 1; i <= n - 1; i++) cout << i << " "; cout << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; long long arr[n]; for (long long i = 0; i < n; i++) { cin >> arr[i]; } bool win[n]; win[n - 1] = true; for (long long j = n - 2; j >= 0; j--) { if (arr[j] == 1) { win[j] = !win[j + 1]; } if (arr[j] > 1) { win[j] = true; } } if (win[0]) cout << "First\n"; else cout << "Second\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 1e6 + 5; int gcd(int a, int b) { if (b == 0) return a; a %= b; return gcd(b, a); } bool compare(long long i, long long j) { return i > j; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); if (a[n - 1] == 0) { cout << "cslnb"; return 0; } int count = 0; for (int i = 0; i < n - 1; i++) { if (a[i] == a[i + 1]) { count++; } if (count == 1 && a[i] == 0) { cout << "cslnb"; return 0; } if (count == 1 && i > 0 && a[i - 1] == a[i] - 1) { cout << "cslnb"; return 0; } if (count >= 2) { cout << "cslnb"; return 0; } } long long sum = 0; for (int i = 0; i < n; i++) { sum += a[i] - i; } if (sum % 2 == 0) { cout << "cslnb"; } else cout << "sjfnb"; }
10
#include <bits/stdc++.h> using namespace std; int n, m; int a[205][205]; bool ok_red[205], cached[205]; bool proveri() { bool ok = true; for (int i = 1; i < n; i++) { if (!cached[i]) { int x[2] = {0, 0}; for (int j = 0; j < m; j++) { x[a[i][j] ^ a[0][j]]++; } ok_red[i] = !(x[0] && x[1]); cached[i] = true; } ok &= ok_red[i]; } return ok; } void promeni(int x, int y) { a[x][y] ^= 1; if (x == 0) { for (int i = 1; i < n; i++) cached[i] = false; } else { cached[x] = false; } } void probaj() { if (proveri()) { cout << "YES\n"; string r = "0", c; for (int j = 0; j < m; j++) { c += a[0][j] + 48; } for (int i = 1; i < n; i++) { r += 48 + (a[i][0] != a[0][0]); } cout << r << '\n' << c << '\n'; exit(0); } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } for (int i = n - 1; i >= 0; i--) { for (int j = m - 1; j >= 0; j--) { promeni(i, j); probaj(); } } cout << "NO\n"; }
14
#include <bits/stdc++.h> using namespace std; const long double PI = 3.141592653589793; const long long mod = 1e9 + 7; int CASE = 1; const int mxn = 1e5 + 1; const long long infll = 1e18; const int infi = 1e9; bool prime(long long n) { if (n <= 1) return false; if (n == 2 or n == 3) return true; if (n % 2 == 0 or n % 3 == 0) return false; for (long long i = 5; i * i <= n; i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long __gcd(long long a, long long b) { return !b ? a : __gcd(b, a % b); } long long power(long long a, long long b) { long long x = a, res = 1, p = b; while (p > 0) { if (p & 1) res *= x; x *= x; p >>= 1; } return res; } void solve() { int a, b; cin >> a >> b; set<int> s; if (a > b) swap(a, b); int n = (a + b) / 2; int m = a + b - n; for (int i = 0; i <= a; i++) { if (i <= n) { s.insert(i + b - (n - i)); } else { s.insert(i + m - (i - n)); } if (i <= m) { s.insert(i + b - (m - i)); } else { s.insert(i + n - (i - m)); } } cout << s.size() << '\n'; for (int i : s) cout << i << ' '; cout << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; while (T--) { solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 111111; int bit[maxn][40]; int num[maxn]; int l[maxn], r[maxn], p[maxn], tot; int main() { int n; scanf("%d", &n); int mx = 0; for (int i = 1; i <= n; i++) { scanf("%d", &num[i]); if (num[i] > mx) mx = num[i]; } tot = 30; while (!((1 << tot) & mx)) tot--; for (int i = 1; i <= n; i++) for (int j = tot; j >= 0; j--) if ((1 << j) & num[i]) bit[i][j] = 1; int tt = 0; while (1) { int flag = 0; for (int i = 1; i < n; i++) if (bit[i][tot] != bit[i + 1][tot]) { flag = 1; break; } if (flag) break; tot--; } for (int i = 1, j = 1; i <= n; i = j) { while (bit[i][tot] == 1 && i <= n) i++; if (i > n) break; l[++tt] = i; j = i; while (bit[j][tot] == 0 && j <= n) j++; r[tt] = j - 1; } int ans = 0; for (int i = 1; i <= tt; i++) { if (l[i] - 1 > 0) { int k = num[l[i] - 1]; int f = l[i]; int mm = num[l[i]]; for (int j = l[i] + 1; j <= r[i]; j++) { if (num[j] < mm) continue; mm = num[j]; if ((k ^ num[j]) > (k ^ num[f])) f = j; } if ((k ^ num[f]) > ans) ans = k ^ num[f]; } if (r[i] + 1 <= n) { int k = num[r[i] + 1]; int f = r[i]; int mm = num[r[i]]; for (int j = r[i] - 1; j >= l[i]; j--) { if (num[j] < mm) continue; mm = num[j]; if ((k ^ num[j]) > (k ^ num[f])) f = j; } if ((k ^ num[f]) > ans) ans = k ^ num[f]; } } printf("%d\n", ans); }
10
#include <bits/stdc++.h> using namespace std; long long int arr[501000], cum[501000]; vector<long long int> adj[501000]; long long int f(long long int i, long long int j) { if (i == j) return arr[i]; long long int ma = -1; for (long long int k = i; k <= j; k++) { ma = max(ma, f(i, k) + f(k + 1, j)); } if (i != 0) return ma + cum[j] - cum[i - 1]; else return ma + cum[j]; } int main() { { long long int n, ans = 0; cin >> n; for (long long int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); cum[n - 1] = arr[n - 1]; for (long long int i = n - 2; i >= 0; i--) { cum[i] = arr[i] + cum[i + 1]; } for (long long int i = 0; i < n; i++) { ans += cum[i]; if (i != 0) ans += arr[i - 1]; } cout << ans << "\n"; } }
4
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int ans = 0; for (int i = 0; i < s.length(); i++) { int l = 0, r = 0; for (int j = i; j < s.length(); j++) { if (s[j] == '(') l++; else if (s[j] == ')') l--; else { l--; r++; } if (l == 0) ans++; if (l < 0) { if (r == 0) break; if (r > 0) { l += 2; r--; } } } } cout << ans << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; int n, a, b, num, sum = 0, lev[200001], leaflev[101], r, c, k[200000], m, u[200000], flag = 0, cnt = 0, q, p, x, y; pair<int, int> sz[50]; long long int w; long long dist[100001], summ, suml, sumr; vector<int> vii[300001]; int ch[50][50]; bool cycle[101], vis[300001]; vector<int> cyclelist; string s[1000]; priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >, greater<pair<int, pair<int, int> > > > qi; int dfs(int index) { int sum = 1; for (int i = 0; i < vii[index].size(); i++) { if (index == x) flag = 0; if (vis[vii[index][i]] == false) { if (vii[index][i] == y) flag = 1; else { vis[vii[index][i]] = true; int ans = dfs(vii[index][i]); if (index == x) { if (flag == 1) { summ = ans; flag = 0; } else sum = sum + ans; } else sum = sum + ans; } } } return sum; } int main() { cin >> n >> x >> y; memset(vis, false, sizeof(vis)); for (int i = 0; i < n - 1; i++) { cin >> a >> b; vii[a].push_back(b); vii[b].push_back(a); } vis[x] = true; suml = dfs(x); sumr = n - suml - summ; long long int f = (suml + summ - 1) * (suml) + (n - 1) * (summ + sumr); cout << f; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int Inf = 1 << 30; const int N = 1e2 + 10; int n, k; struct node { int x; int id; } a[N]; int ans[N]; bool cmp(node a, node b) { return a.x < b.x; } map<int, int> q; int main() { while (~scanf("%d %d", &n, &k)) { q.clear(); memset(ans, 0, sizeof(ans)); memset(a, 0, sizeof(a)); for (int i = 0; i < n; i++) { scanf("%d", &a[i].x); a[i].id = i; } sort(a, a + n, cmp); for (int i = 0; i < n; i++) q[a[i].id] = i; int t = 0; bool flag = 0; for (int i = 0; i < n; i++) { if (a[i].x / 2 * 2 == a[i].x) t = a[i].x / 2; else t = a[i].x / 2 + 1; if (t <= k) { ans[i] = t; k -= t; } else { flag = 1; break; } } if (flag) puts("-1"); else { for (int i = n - 1; i >= 0; i--) { if (k >= a[i].x - ans[i]) { k -= a[i].x - ans[i]; ans[i] = a[i].x; } else { ans[i] += k; break; } } for (int i = 0; i < n; i++) printf("%d%c", ans[q[i]], i == n - 1 ? '\n' : ' '); } } return 0; }
6
#include <bits/stdc++.h> using namespace std; int x[103], y[103], vis[104]; vector<int> a[104]; void add(int i, int j) { a[i].push_back(j); a[j].push_back(i); } bool bl = true; void dfs(int i, int k) { vis[i] = k; if (k == 1) k = 2; else k = 1; for (int j = 0; j < a[i].size(); j++) { int ver = a[i][j]; if (vis[ver] == 0) dfs(ver, k); else if (vis[ver] != k) bl = false; } } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> x[i] >> y[i]; if (x[i] > y[i]) swap(x[i], y[i]); } for (int i = 1; i <= m; i++) { for (int j = i + 1; j <= m; j++) { if (x[i] > x[j] && x[i] < y[j] && y[i] > y[j]) add(i, j); else if (x[i] < x[j] && y[i] > x[j] && y[i] < y[j]) add(i, j); } } for (int i = 1; i <= m; i++) { if (vis[i] == 0) dfs(i, 1); } if (bl) { for (int i = 1; i <= m; i++) if (vis[i] == 1) cout << "o"; else cout << "i"; cout << '\n'; } else cout << "Impossible" << '\n'; }
14
#include <bits/stdc++.h> using namespace std; int main() { int a, b; scanf("%d%d", &a, &b); printf("%d", (b + a - 1) / a); }
0
#include <bits/stdc++.h> using namespace std; long long exgcd(long long a, long long b, long long &x, long long &y) { if (!b) { x = 1; y = 0; return a; } long long res = exgcd(b, a % b, y, x); y -= (a / b) * x; return res; } int n, m, x, y, vx, vy, refx, refy; long long a, b, coefa, coefb, gcd, c; int main() { scanf("%d%d%d%d%d%d", &n, &m, &x, &y, &vx, &vy); if (vx == 0) { if (x != n && x != 0) { puts("-1"); return 0; } printf("%d ", x); if (vy > 0) { printf("%d", m); } else { printf("%d", 0); } return 0; } if (vy == 0) { if (y != 0 && y != m) { puts("-1"); return 0; } if (vx > 0) { printf("%d ", n); } else { printf("%d ", 0); } printf("%d", y); return 0; } if (vx < 0) { refx = 1; x = n - x; } if (vy < 0) { refy = 1; y = m - y; } gcd = exgcd(n, m, a, b); c = x - y; if (c % gcd) { puts("-1"); return 0; } long long pdt = c / gcd; a *= pdt; b *= pdt; coefa = m / gcd; a = (a % coefa + coefa - 1) % coefa + 1; b = (c - a * n) / m; x = 0; y = 0; if (a % 2) { x = n; } if (b % 2) { y = m; } if (refx) { x = n - x; } if (refy) { y = m - y; } cout << x << " " << y << endl; }
18
#include <bits/stdc++.h> using namespace std; int n; int main() { cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<long long> sum(1 << n); vector<int> pos; for (int i = 0; i < 1 << n; i++) { for (int j = 0; j < n; j++) { if ((i >> j) & 1) sum[i] += a[j]; } } for (int i = 0; i < 1 << n; i++) { int p = __builtin_popcount(i); if (p == 1 && sum[i] == 0) { pos.push_back(i); continue; } if ((sum[i] + p - 1) % 2) continue; for (int j = i; ~j; j = (j == 0 ? -1 : (j - 1) & i)) { if (j == i) continue; if (j < (i ^ j)) break; if (abs(sum[j] - sum[i ^ j]) <= p - 1) { pos.push_back(i); break; } } } vector<int> res(1 << n); for (int i : pos) { for (int j = ((1 << n) - i - 1); ~j; j = (j == 0 ? -1 : (j - 1) & ((1 << n) - i - 1))) { res[j ^ i] = max(res[j ^ i], res[j] + 1); } } cout << n - res[(1 << n) - 1]; return 0; }
23
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, n; cin >> n; long long a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } long long max1 = INT_MIN; long long sum = 0; long long max1_id = -1; long long c[n][n]; for (j = 0; j < n; j++) { sum = 0; c[j][j] = a[j]; for (i = j; i >= 1; i--) { if (a[i - 1] > c[j][i]) { c[j][i - 1] = c[j][i]; } else { c[j][i - 1] = a[i - 1]; } } for (i = j; i < n - 1; i++) { if (a[i + 1] > c[j][i]) { c[j][i + 1] = c[j][i]; } else { c[j][i + 1] = a[i + 1]; } } for (i = 0; i < n; i++) { sum += c[j][i]; } if (sum > max1) { max1 = sum; max1_id = j; } } for (i = 0; i < n; i++) { cout << c[max1_id][i] << " "; } }
7
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 1e9 + 5; const long long LINF = LLONG_MAX; int main() { int n; cin >> n; int a[n]; int r1 = 0, r2 = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] <= 500000) { r1 = max(r1, a[i] - 1); } if (a[i] > 500000) { r2 = max(r2, 1000000 - a[i]); } } cout << max(r1, r2) << "\n"; }
3
#include <bits/stdc++.h> using namespace std; int main() { printf("\?9\?\?\?\?<>10\n"); for (int i = 0; i < 9; i++) printf("%d\?\?\?\?<>%d\n", i, (i + 1) % 10); printf("9\?\?\?\?>>\?\?\?\?0\n"); for (int i = 0; i < 10; i++) printf("\?\?%d>>%d\?\?\n", i, i); printf("\?\?>>\?\?\?\?\n"); for (int i = 0; i < 9; i++) printf("%d>>%d\?\?\n", i, i); printf("9>>\?9\?\?\n"); return 0; }
17
#include <bits/stdc++.h> using namespace std; int n; long long k; long long fib[57]; void input(); void solve(); int main() { ios::sync_with_stdio(false); cin.tie(NULL); input(); solve(); return 0; } void input() { scanf("%d%I64d", &n, &k); } void solve() { int i; fib[0] = fib[1] = 1; for (i = 2; i <= n; i++) { fib[i] = fib[i - 1] + fib[i - 2]; } int lft = n; int u = 1; while (lft != 0) { if (k > fib[lft - 1]) { k -= fib[lft - 1]; printf("%d %d ", u + 1, u); u += 2; lft -= 2; } else { lft--; printf("%d ", u); u++; } } printf("\n"); }
11
#include <bits/stdc++.h> using namespace std; int arr[55]; int main() { int t, n; cin >> t; while (t--) { cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); bool p = true; for (int i = 1; i < n; i++) { if (arr[i] - arr[i - 1] <= 1) continue; else { p = false; break; } } if (p == true) cout << "YES\n"; else cout << "NO\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int a[100]; int b[100]; int count = 0; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (b[i] == a[j]) { count++; } } } cout << count; } int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; inline long long read() { long long d = 0, f = 1; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { d = d * 10 + s - '0'; s = getchar(); } return d * f; } long long ksm(long long x, long long y) { long long s = 1; while (y) { if (y & 1) (s *= x) %= 1000000007; (x *= x) %= 1000000007; y >>= 1; } return s; } long long s[5005][5005]; int main() { long long n = read(), k = read(); s[0][0] = 1; for (long long i = 1; i <= k; i++) for (long long j = 1; j <= i; j++) s[i][j] = (s[i - 1][j - 1] + s[i - 1][j] * j % 1000000007) % 1000000007; long long p = ksm(2, n), pp = 1, ans = 0, inv = ksm(2, 1000000007 - 2); for (long long i = 0; i <= k; i++) { (ans += s[k][i] * pp % 1000000007 * p % 1000000007) %= 1000000007; (p *= inv) %= 1000000007; (pp *= (n - i + 1000000007) % 1000000007) %= 1000000007; } if (!k) ans = (ans - 1 + 1000000007) % 1000000007; cout << ans; return 0; }
16
#include <bits/stdc++.h> using namespace std; unordered_map<unsigned long long int, int> m; bool findPowerOf2(int n) { if ((m.find(n) != m.end()) && m[n] != 0) { m[n]--; return true; } if (n == 1) return false; return findPowerOf2(n / 2) && findPowerOf2(n / 2); } int main() { int q; cin >> q; while (q--) { int n; cin >> n; unsigned long long int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; m[arr[i]]++; } if (findPowerOf2(2048)) { cout << "YES\n"; } else { cout << "NO\n"; } m.clear(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; map<int, int> dct; void solve() { int n, p, k; cin >> n >> p >> k; const int mod = p; for (int i = 0; i != n; ++i) { int x; cin >> x; long long diff = 1LL * x * k % mod; x = (1LL * x * x) % mod; x = (1LL * x * x) % mod; x -= diff; if (x < 0) x += mod; if (dct.find(x) != dct.end()) dct[x] += 1; else dct[x] = 1; } long long ans = 0; for (auto tmp : dct) { long long x = tmp.second; ans += x * (x - 1) >> 1; } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) solve(); return 0; }
15
#include <bits/stdc++.h> const int maxn = 100000 + 100; char s[maxn]; int vis[maxn], d[maxn]; int main() { int n; scanf("%d", &n); scanf("%s", s); memset(d, 0, sizeof(d)); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { scanf("%d", &d[i]); } vis[1] = true; int now = 1, flag; while (1) { if (s[now - 1] == '>') { if (now + d[now] > n) { flag = false; break; } else { now += d[now]; if (vis[now]) { flag = true; break; } vis[now] = true; } } else if (s[now - 1] == '<') { if (now - d[now] < 1) { flag = false; break; } else { now -= d[now]; if (vis[now]) { flag = true; break; } vis[now] = true; } } } printf("%s\n", flag ? "INFINITE" : "FINITE"); return 0; }
2
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int h1[n], h2[n]; for (int i = 0; i < n; i++) cin >> h1[i]; for (int i = 0; i < n; i++) cin >> h2[i]; long long ax = 0, ay = 0; for (int i = 0; i < n; i++) { long long x = max(ax, ay + h1[i]); long long y = max(ay, ax + h2[i]); ax = x; ay = y; } cout << max(ax, ay) << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); }
6
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:64000000") const int infi = 1e9 + 7; const long long infl = 1e18 + 7; int ans[100]; int ar[101]; vector<string> name; int main() { cin.sync_with_stdio(false); cin.tie(0); for (int i = 0; i < (26); ++i) { for (int j = 0; j < (26); ++j) { string s; s.push_back('A' + i); s.push_back('a' + j); name.push_back(s); } } int n, k; cin >> n >> k; for (int i = 0; i < (n - k + 1); ++i) { string s; cin >> s; ar[i] = (s == "YES"); } for (int i = 0; i < (k - 1); ++i) ans[i] = i; for (int i = k - 1; i < n; ++i) { if (ar[i - (k - 1)]) ans[i] = i; else ans[i] = ans[i - (k - 1)]; } for (int i = 0; i < (n); ++i) cout << name[ans[i]] << ' '; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 77, L = 26; int n, S[N][L], t = 1, H[N], A; char s[N]; multiset<int> D[N]; void insert() { int m = strlen(s), id = 1; for (int i = 0; i < m; i++) { if (S[id][s[i] - 'a'] == 0) { ++t; S[id][s[i] - 'a'] = t; } id = S[id][s[i] - 'a']; } H[id]++; A += m; } void dfs(int v) { for (int i = 0; i < L; i++) { if (S[v][i] == 0) { continue; } dfs(S[v][i]); for (auto x : D[S[v][i]]) { D[v].insert(x + 1); } } if (H[v]) { D[v].insert(0); } else if (v > 1) { int P = *D[v].rbegin(); A -= P; D[v].erase(D[v].find(P)); D[v].insert(0); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%s", &s); insert(); } dfs(1); printf("%d\n", A); return 0; }
14
#include <bits/stdc++.h> using namespace std; const int N = 500005; struct edge { int to, nxt, col; } e[N]; int n, m, cnt = -1, head[N]; int ans[N], dis[N]; queue<int> q; bool vis[N]; void addedge(int x, int y, int z) { ++cnt; e[cnt].to = y; e[cnt].nxt = head[x]; e[cnt].col = z; head[x] = cnt; } void bfs(int st) { for (int i = 1; i <= n; ++i) ans[i] = -1; for (int i = 1; i <= n; ++i) vis[i] = 0; for (int i = 1; i <= n; ++i) dis[i] = -1; dis[n] = 0; q.push(n); vis[n] = 1; while (!q.empty()) { int x = q.front(); q.pop(); if (x == 1) break; for (int i = head[x]; ~i; i = e[i].nxt) { int v = e[i].to; if (vis[v]) continue; if ((ans[v] == -1) || (ans[v] == (1 - e[i].col))) ans[v] = 1 - e[i].col; else { dis[v] = dis[x] + 1; q.push(v); vis[v] = 1; } } } } int main() { memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { int x, y; scanf("%d%d", &x, &y); int col; scanf("%d", &col); addedge(y, x, col); } bfs(n); if (dis[1] >= 0) printf("%d\n", dis[1]); else puts("-1"); for (int i = 1; i <= n; ++i) { if (ans[i] == -1 || ans[i] == 0) putchar('0'); else putchar('1'); } return 0; }
17
#include <bits/stdc++.h> using namespace std; int m[50000], n[50000]; int main() { int x, y, a, b; while (~scanf("%d %d %d %d", &x, &y, &a, &b)) { memset(m, 0, sizeof(m)); memset(n, 0, sizeof(n)); if (x <= b) { printf("0\n"); continue; } int cnt = 0; if (a <= b) a = b + 1; for (int i = a; i <= x; i++) { for (int j = b; j <= y; j++) { if (i <= j) break; m[cnt] = i; n[cnt] = j; cnt++; } } printf("%d\n", cnt); for (int i = 0; i < cnt; i++) { printf("%d %d\n", m[i], n[i]); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { if (y == 0) return 1; while (x % y != 0) { int r = x % y; x = y; y = r; } return y; } int main() { int a, b, c, d, x, y, x1, y1, x2, y2, g; cin >> a >> b >> c >> d; x1 = abs((b * c) - (d * a)); y1 = c * b; x2 = abs((a * d) - c * b); y2 = d * a; if (x1 > y1) g = gcd(x1, y1); else g = gcd(y1, x1); x1 = x1 / g; y1 = y1 / g; if (x2 > y2) g = gcd(x2, y2); else g = gcd(y2, x2); x2 = x2 / g; y2 = y2 / g; if ((float)x1 / y1 < (float)x2 / y2) { x = x1; y = y1; } else { x = x2; y = y2; } if (x != 0) cout << x << "/" << y; else cout << x << "/" << 1; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int n, p; cin >> n >> p; string s; cin >> s; int i = n - 1; s[i]++; while (i >= 0 && i < n) { if (s[i] - 'a' >= p) { s[i] = 'a'; i--; if (i >= 0) s[i]++; } else if (i >= 1 && s[i - 1] == s[i]) { s[i]++; } else if (i >= 2 && s[i - 2] == s[i]) { s[i]++; } else { i++; } } if (i < 0) { cout << "NO\n"; return 0; } cout << s << endl; }
9
#include <bits/stdc++.h> using namespace std; int a[500005][2], n; bool check(double t) { int i; double d = -1e20; for (i = 0; i < n; i++) { if (a[i][1] > 0) d = max(d, a[i][0] + t * a[i][1]); else if (a[i][0] + t * a[i][1] <= d) return true; } return false; } int main() { while (~scanf("%d", &n)) { int i, t = 0; memset(a, 0, sizeof(a)); for (i = 0; i < n; i++) scanf("%d%d", &a[i][0], &a[i][1]); double l = 0, r = 1e10; while (t < 100) { t++; double mid = (l + r) / 2.0; if (check(mid)) r = mid; else l = mid; } if (r == 1e10) puts("-1"); else printf("%.20lf\n", l); } }
15
#include <bits/stdc++.h> using namespace std; const long long int mod = 1000000000 + 7; const long long int N = 10000000 + 6; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string st1; string st2; cin >> st1 >> st2; int n = st2.size(); int l = 0, r = 0; for (int i = 0; i < n; i++) { if (st1[i] == st2[i]) l++; else break; } for (int j = n - 1; j >= 0; j--) { if (st2[j] == st1[j + 1]) r++; else break; } if (l + 1 < n + 1 - r) cout << 0 << endl; else { int start = max(0, n - r); int end = min(n, l); if (start > end) swap(start, end); cout << end - start + 1 << '\n'; for (int i = start; i <= end; i++) cout << i + 1 << " "; } return 0; }
7
#include <bits/stdc++.h> using namespace std; inline long long next_int() { int32_t x; scanf("%d", &x); return x; } inline long long next_long() { long long x; scanf("%lld", &x); return x; } const long long maxn = 2e5 + 10; long long a[maxn], tp[maxn], r[maxn], maxi, suffix[maxn], nxt[maxn], b[maxn]; deque<pair<long long, long long> > q; deque<long long> dq; int32_t main() { long long n = next_int(), m = next_int(); for (long long i = 0; i < n; i++) a[i] = b[i] = next_int(); for (long long i = 0; i < m; i++) tp[i] = next_int(), r[i] = next_int(), maxi = max(maxi, r[i]); for (long long i = 0; i < m; i++) { while (q.size() && q.back().second <= r[i]) q.pop_back(); q.push_back({tp[i], r[i]}); } maxi = q.front().second; sort(b, b + maxi); for (long long i = 0; i < maxi; i++) dq.push_back(b[i]); q.push_back({0, 0}); for (long long i = 0; i + 1 < q.size(); i++) { long long add = q[i].second - q[i + 1].second; long long cur = q[i].second - 1; if (q[i].first == 2) { while (add--) { b[cur] = dq.front(); cur--; dq.pop_front(); } } else { while (add--) { b[cur] = dq.back(); cur--; dq.pop_back(); } } } for (long long i = 0; i < n; i++, cout << " ") cout << b[i]; return 0; }
9
#include <bits/stdc++.h> using namespace std; int ara[111]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int i, j, k, l, m, n, ans = 0; cin >> n; for (i = 1; i <= n; i++) cin >> ara[i]; for (i = 2; i < n; i++) { if (ara[i - 1] == ara[i + 1] && ara[i - 1] == 1 && ara[i] == 0) { ara[i + 1] = 0; ans++; } } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int nxt() { int x; cin >> x; return x; } int main() { int n = nxt(); vector<int> a(n); generate(a.begin(), a.end(), nxt); int mx = *max_element(a.begin(), a.end()); vector<int> p(mx + 1, n - 1); for (int i = 0; i < int(n); ++i) p[a[i]] = i; int ans = n - 1; for (int i = 0; i < int(mx + 1); ++i) { if (p[i] < ans) { ans = p[i]; } } cout << a[ans] << '\n'; return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; const int maxn = 2e5 + 5; const int mod = 1000000007; const long long INF = 1e18; const int MX = 2000000001; int main() { ios_base::sync_with_stdio(0), cin.tie(0); long long t, q, n, a, b, c, k = 0, l, mr, r, x = 0, y = 0, sum = 0, ans = 0, temp = 0, rees = 0; cin >> t; while (t--) { cin >> a >> b >> c >> k; cout << min(a - 1, abs(c - k) + b) << "\n"; } }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, d, m; vector<int> v[2]; int len[2]; vector<long long> pre[2]; int ceil(int a, int b) { return (a + b - 1) / b; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> d >> m; for (int i = 1; i <= n; i++) { int x; cin >> x; v[(x > m)].emplace_back(x); } for (int it = 0; it < 2; it++) { len[it] = v[it].size(); sort(v[it].begin(), v[it].end(), greater<int>()); pre[it].resize(len[it]); for (int j = 0; j < len[it]; j++) { pre[it][j] = (j == 0 ? v[it][j] : (1LL) * v[it][j] + pre[it][j - 1]); } } long long ans = 0; for (int i = 0; i <= len[0]; i++) { int num = min(len[1], ceil(len[1] + i, d + 1)); int mun = len[0] - i; long long res = 0; if (mun > 0) res += pre[0][mun - 1]; if (num > 0) res += pre[1][num - 1]; ans = max(ans, res); } cout << ans << '\n'; return 0; }
10
#include <bits/stdc++.h> using namespace std; long long n, m, k; long long series(long long a, long long b) { long long val1 = (b * (b + 1)) / 2; if (a > 0) a--; long long val2 = (a * (a + 1)) / 2; return val1 - val2; } long long solve(long long mid) { long long ret = 0; long long val = k; if (mid <= k) { ret += series(1, mid); } else { ret += series(mid - val + 1, mid); } val = n - k + 1; if (mid <= val) { ret += series(1, mid); } else { ret += series(mid - val + 1, mid); } return ret - mid; } int main() { cin >> n >> m >> k; m -= n; long long dodo = 0; long long l = 1; long long r = m; while (l <= r) { long long mid = (l + r) / 2; long long foo = solve(mid); if (foo <= m) { l = mid + 1; dodo = mid; } else { r = mid - 1; } } cout << dodo + 1 << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int big = 1e5 + 7; int l[big], r[big], record[big]; int main() { int n, num = 0, p = 0, q = 0, large, temp; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d %d", &l[i], &r[i]); p += l[i]; q += r[i]; } large = p - q; if (large < 0) large = -large; record[num++] = 0; for (int i = 1; i <= n; i++) { p += (r[i] - l[i]); q += (l[i] - r[i]); temp = p - q; if (temp < 0) temp = -temp; if (temp == large) record[num++] = i; else if (temp > large) { large = abs(p - q); num = 0; record[num++] = i; } p += (l[i] - r[i]); q += (r[i] - l[i]); } printf("%d", record[0]); return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, root = 0, curr = 0; vector<vector<int>> adj, cost; vector<bool> used; void dfs(int v) { used[v] = true; for (auto u : adj[v]) if (!used[u]) { curr += cost[v][u]; dfs(u); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; adj.resize(n); cost.assign(n, vector<int>(n)); int total = 0; for (int i = 0; i < n; i++) { int u, v, c; cin >> u >> v >> c; u -= 1; v -= 1; total += c; adj[u].push_back(v); adj[v].push_back(u); cost[u][v] = 0; cost[v][u] = c; } used.assign(n, false); dfs(root); curr += cost[adj[root][1]][root]; cout << min(curr, total - curr) << endl; }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 111; int last[maxn], n, k, a[maxn], b[maxn], c[maxn]; set<pair<int, int> > q; vector<int> pos[maxn]; bool was[maxn]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i], &b[i]); q.erase(make_pair(-last[a[i]], a[i])); last[a[i]] = i; q.insert(make_pair(-i, a[i])); was[a[i]] = true; pos[a[i]].push_back(i); } cin >> k; while (k--) { int m; scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &c[i]); if (was[c[i]]) q.erase(make_pair(-last[c[i]], c[i])); } if (q.empty()) printf("0 0\n"); else { pair<int, int> x = *q.begin(); q.erase(x); if (q.empty()) printf("%d %d\n", x.second, b[pos[x.second][0]]); else { pair<int, int> z = *q.begin(); int t = -z.first; int valx = x.second; int j = (lower_bound(pos[valx].begin(), pos[valx].end(), t) - pos[valx].begin()); j = pos[valx][j]; printf("%d %d\n", valx, b[j]); } q.insert(x); } for (int i = 0; i < m; i++) if (was[c[i]]) q.insert(make_pair(-last[c[i]], c[i])); } return 0; }
12
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long fact[1000001]; long long inv[1000001]; long long primes[100007]; long long arr[1000007]; long long modPower(long long b, long long p) { if (p == 0) return 1; long long halfpow = modPower(b, p / 2); long long toReturn = (halfpow * halfpow) % mod; if (p % 2) toReturn = (toReturn * b) % mod; return toReturn; } long long fastPower(long long b, long long p) { if (p == 0) return 1; long long ans = fastPower(b, p / 2); ans = (ans * ans); if (p % 2 != 0) ans = (ans * b); return ans; } long long GcdRecursive(long long a, long long b) { if (b == 0) return a; return GcdRecursive(b, a % b); } long long modLCM(long long a, long long b) { long long val = GcdRecursive(a, b); long long tmp = ((a % mod) * (b % mod)) % mod; long long finalVal = ((tmp % mod) * (arr[val] % mod)) % mod; return finalVal; } long long LCM(long long a, long long b) { return (a * b) / GcdRecursive(a, b); } void move1step(long long& a, long long& b, long long q) { long long c = a - q * b; a = b; b = c; } long long GcdIterative(long long a, long long b) { while (b) move1step(a, b, a / b); return a; } void pre(long long n) { fact[0] = 1; inv[0] = 1; for (long long i = 1; i <= n; i++) { fact[i] = (i * fact[i - 1]) % mod; inv[i] = modPower(fact[i], mod - 2); arr[i] = modPower(i, mod - 2); } } long long npr(long long n, long long r) { return ((fact[n] * inv[n - r]) % mod); } long long ncr(long long n, long long r) { return ((((fact[n] * inv[n - r]) % mod) * inv[r]) % mod); } void sieve(long long val) { memset(primes, 1, sizeof primes); primes[0] = primes[1] = false; for (int i = 2; i <= val; i++) { if (primes[i]) { for (int j = i * i; j <= val; j += i) { primes[j] = 0; } } } } long double dot(complex<long double> a, complex<long double> b) { return (conj(a) * b).real(); } long double cross(complex<long double> a, complex<long double> b) { return (conj(a) * b).imag(); } long double angleBetVec(complex<long double> a, complex<long double> b) { long double d = dot(a, b); d /= abs(a); d /= abs(b); return acos(d) * 180 / acos(-1); } complex<long double> RotateAbout(complex<long double> a, complex<long double> about, long double angle) { return (a - about) * polar((long double)1.0, angle) + about; } const long long MOD = 998244353; const long long N = 2e5 + 7; const long long inf = 1e18 + 5; long long t; int main() { ios::sync_with_stdio(0); cin.tie(0); ios_base::sync_with_stdio(0); cin >> t; while (t--) { long long n, m; cin >> n >> m; if (m == 0) { cout << 0 << "\n"; continue; } long long ans = n * (n + 1) / 2; long long diff = n - m; long long zeroSubSeq = floor(diff / (m + 1)); ans -= zeroSubSeq * (zeroSubSeq + 1) / 2 * (m + 1); ans -= (zeroSubSeq + 1) * (diff % (m + 1)); cout << ans << "\n"; } return 0; }
9
#include <bits/stdc++.h> using namespace std; const int Maxn = 100005; const int Inf = 1000000000; int n, m; vector<pair<int, int> > neigh[Maxn]; int zdist[Maxn], bfsdist[Maxn]; int zpar[Maxn], bfspar[Maxn]; pair<int, int> par[Maxn]; set<int> S, nS; void BFS(int v, int dist[], int par[], bool zer) { bool ok[10] = {}; ok[0] = true; if (!zer) fill(ok + 1, ok + 10, true); fill(dist, dist + n, Inf); dist[v] = 0; queue<int> Q; Q.push(v); while (!Q.empty()) { int v = Q.front(); Q.pop(); for (int i = 0; i < neigh[v].size(); i++) { pair<int, int> u = neigh[v][i]; if (ok[u.second] && dist[v] + 1 < dist[u.first]) { dist[u.first] = dist[v] + 1; par[u.first] = v; Q.push(u.first); } } } } int main() { scanf("%d%d", &n, &m); while (m--) { int a, b, len; scanf("%d%d%d", &a, &b, &len); neigh[a].push_back(pair<int, int>(b, len)); neigh[b].push_back(pair<int, int>(a, len)); } BFS(n - 1, zdist, zpar, true); BFS(0, bfsdist, bfspar, false); int rlen = Inf; for (int i = 0; i < n; i++) if (i == n - 1 || zdist[i] < Inf) if (bfsdist[i] < rlen) rlen = bfsdist[i]; fill(par, par + n, pair<int, int>(Inf, 0)); for (int i = 0; i < n; i++) if (i == n - 1 || zdist[i] < Inf) if (bfsdist[i] == rlen) { par[i] = pair<int, int>(zdist[i], i); S.insert(i); } for (int i = 0; i < rlen; i++) { nS.clear(); int best = 10; for (set<int>::iterator it = S.begin(); it != S.end(); it++) for (int j = 0; j < neigh[*it].size(); j++) { pair<int, int> u = neigh[*it][j]; if (bfsdist[*it] - 1 != bfsdist[u.first]) continue; if (u.second < best) { best = u.second; nS.clear(); } if (u.second == best) if (nS.find(u.first) != nS.end()) par[u.first] = min(par[u.first], pair<int, int>(par[*it].first, *it)); else { nS.insert(u.first); par[u.first] = pair<int, int>(par[*it].first, *it); } } printf("%d", best); S = nS; } if (rlen == 0) printf("0"); printf("\n"); int tlen = rlen + par[0].first; printf("%d\n", tlen + 1); int v = 0; for (int i = 0; i < rlen; i++) { printf("%d ", v); v = par[v].second; } for (int i = 0; i < tlen - rlen; i++) { printf("%d ", v); v = zpar[v]; } printf("%d\n", v); return 0; }
14
#include <bits/stdc++.h> using namespace std; const double pi = acos(0.0) * 2.0; const double eps = 1e-12; const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; template <class T> inline T abs1(T a) { return a < 0 ? -a : a; } template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) { T ans = a > b ? a : b; if (c != -1234567) ans = max1(ans, c); if (d != -1234567) ans = max1(ans, d); return ans; } template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) { T ans = a < b ? a : b; if (c != -1234567) ans = min(ans, c); if (d != -1234567) ans = min(ans, d); return ans; } template <class T> inline T gcd1(T a, T b) { if (a < b) swap(a, b); if (a % b == 0) return b; return gcd1(b, a % b); } template <class T> inline T lb(T num) { return num & (-num); } inline int jud(double a, double b) { if (abs(a) < eps && abs(b) < eps) return 0; else if (abs1(a - b) / abs1(a) < eps) return 0; if (a < b) return -1; return 1; } template <typename t> inline int jud(t a, t b) { if (a < b) return -1; if (a == b) return 0; return 1; } template <typename it, typename t1> inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) { int be = 0, en = na - 1; if (*a <= *(a + na - 1)) { if (f_lb == 0) while (be < en) { int mid = (be + en + 1) / 2; if (jud(*(a + mid), val) != 1) be = mid; else en = mid - 1; } else while (be < en) { int mid = (be + en) / 2; if (jud(*(a + mid), val) != -1) en = mid; else be = mid + 1; } if (f_small && jud(*(a + be), val) == 1) be--; if (!f_small && jud(*(a + be), val) == -1) be++; } else { if (f_lb) while (be < en) { int mid = (be + en + 1) / 2; if (jud(*(a + mid), val) != -1) be = mid; else en = mid - 1; } else while (be < en) { int mid = (be + en) / 2; if (jud(*(a + mid), val) != 1) en = mid; else be = mid + 1; } if (!f_small && jud(*(a + be), val) == -1) be--; if (f_small && jud(*(a + be), val) == 1) be++; } return be; } inline int bitnum(unsigned long long nValue) { nValue = ((0xaaaaaaaaaaaaaaaaull & nValue) >> 1) + (0x5555555555555555ull & nValue); nValue = ((0xccccccccccccccccull & nValue) >> 2) + (0x3333333333333333ull & nValue); nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue) >> 4) + (0x0f0f0f0f0f0f0f0full & nValue); nValue = ((0xff00ff00ff00ff00ull & nValue) >> 8) + (0x00ff00ff00ff00ffull & nValue); nValue = ((0xffff0000ffff0000ull & nValue) >> 16) + (0x0000ffff0000ffffull & nValue); nValue = ((0xffffffff00000000ull & nValue) >> 32) + (0x00000000ffffffffull & nValue); return nValue; } long long pow(long long n, long long m, long long mod = 0) { long long ans = 1; long long k = n; while (m) { if (m & 1) { ans *= k; if (mod) ans %= mod; } k *= k; if (mod) k %= mod; m >>= 1; } return ans; } const int maxn = 1100; int orig[maxn], n; int cnt[maxn]; int ans[maxn * maxn][2], lans = 0; bool f[maxn]; inline void findnxt(int &no) { while (f[no] == 0) no++; f[no] = 0; } inline void relax(int &a, int &b, int &c) { if (orig[b] < orig[a]) swap(a, b); if (orig[c] < orig[b]) swap(b, c); if (orig[b] < orig[a]) swap(a, b); } int main() { ios_base::sync_with_stdio(0); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", orig + i); int cnt = 0; for (int i = 0; i < n; ++i) { if (orig[i]) f[i] = 1, cnt++; else f[i] = 0; } if (cnt < 2) { cout << -1 << endl; return 0; } f[n] = f[n + 1] = f[n + 2] = 1; orig[n] = orig[n + 1] = orig[n + 2] = 100000007; f[1] = f[2] = 0; int a = 0, b = 1, c = 2; for (; c < n;) { findnxt(a); relax(a, b, c); if (c >= n) break; while (orig[a]) { int temp = orig[b] / orig[a]; for (; temp; temp >>= 1) { if (temp & 1) { ans[lans][1] = b; orig[b] -= orig[a]; } else { ans[lans][1] = c; orig[c] -= orig[a]; } ans[lans++][0] = a; orig[a] <<= 1; } relax(a, b, c); } } printf("%d\n", lans); for (int i = 0; i < lans; i++) printf("%d %d\n", ans[i][0] + 1, ans[i][1] + 1); return 0; }
22
#include <bits/stdc++.h> using namespace std; const long long SIZE = 1e5 * 3 + 10, MOD = 1e9 + 7, phi = 500000002; map<long long, long long> cnt; vector<long long> vec; long long gcd(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 = gcd(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } long long rev_mod(long long a, long long mod) { long long x, y; long long g = gcd(a, mod, x, y); if (g != 1) abort(); else { x = (x % mod + mod) % mod; return x; } } long long binpow(long long a, long long n, long long md = MOD) { if (n == 0) return 1; if (n % 2 == 1) return (binpow(a, n - 1) * a) % md; else { long long b = binpow(a, n / 2); return (b * b) % md; } } signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); ; long long m; cin >> m; vec.resize(m); long long s = 1, mul = 1; vector<long long> fact; fact.push_back(0); for (long long i = 1; i < m + 10; i++) fact.push_back((fact.back() + i) % (MOD - 1)); for (long long i = 0; i < m; i++) { cin >> vec[i]; cnt[vec[i]]++; } for (auto cur : cnt) { mul *= (cur.second + 1); mul %= MOD - 1; } long long nows = 1; for (auto cur : cnt) { long long pw = binpow(cur.first, (cur.second + 1) * cur.second / 2, MOD); s = binpow(s, (cur.second + 1), MOD) * binpow(pw, nows, MOD); nows = (nows * (cur.second + 1)) % (MOD - 1); s %= MOD; } cout << s; return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { int i, j; int m, l, h; cin >> m; int a[m]; for (i = 0; i < m; i++) cin >> a[i]; cin >> l >> h; int lbeg = -1, hbeg = 101, lint = -1, hint = 101; int sum = 0; for (i = 0; i < m; i++) { sum += a[i]; if (sum >= l && lbeg == -1) lbeg = i + 2; if (sum > h) { hbeg = i + 1; break; } } if (lbeg > hbeg || (lbeg == -1 && hbeg == 101)) { cout << '0'; return 0; } sum = 0; for (i = m - 1; i >= 0; i--) { sum += a[i]; if (sum >= l && hint == 101) hint = i + 1; if (sum > h) { lint = i + 2; break; } } if (lint > hint || (lint == -1 && hint == 101)) { cout << '0'; return 0; } if (hbeg < lint) cout << '0'; else { if (lint >= lbeg) { if (lint > m) cout << 0; else cout << lint; } else { if (lbeg <= hint) { if (lbeg > m) cout << 0; else cout << lbeg; } else cout << '0'; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m, q, u[105], v[105], a[105], b[105], c[105], bt[1 << 13]; long long f[13][1 << 13]; bool check(int x, int y, int s) { for (int i = 1; i <= m; i++) if (1 & (s >> u[i])) { if (!(1 & (s >> v[i]))) { if (y != u[i] || x != v[i]) return false; } } for (int i = 1; i <= m; i++) if (1 & (s >> v[i])) { if (!(1 & (s >> u[i]))) { if (y != v[i] || x != u[i]) return false; } } return true; } bool calc(int s) { for (int i = 1; i <= q; i++) { if (((s >> b[i]) & 1) && ((s >> a[i]) & 1) && !((s >> c[i]) & 1)) return false; if (((s >> c[i]) & 1) && (!((s >> a[i]) & 1) || !((s >> b[i]) & 1))) return false; } return true; } long long solve(int x, int s) { if (~f[x][s]) return f[x][s]; if (0 == (1 & (s >> x))) return f[x][s] = 0; if (s == (s & -s)) return f[x][s] = 1; f[x][s] = 0; int res = s ^ (1 << x); int hav = bt[res & -res]; res ^= 1 << hav; for (int y = 0; y <= n - 1; y++) { if (check(x, y, 1 << hav) && calc(1 << hav)) f[x][s] += solve(y, 1 << hav) * solve(x, s ^ (1 << hav)); for (int t = res; t; t = (t - 1) & res) { if (check(x, y, (1 << hav) | t) && calc((1 << hav) | t)) f[x][s] += solve(y, (1 << hav) | t) * solve(x, s ^ (1 << hav) ^ t); } } return f[x][s]; } int main() { scanf("%d%d%d", &n, &m, &q); bt[0] = ~0; for (int i = 1; i <= (1 << n) - 1; i++) bt[i] = bt[i >> 1] + 1; for (int i = 1; i <= m; i++) scanf("%d%d", &u[i], &v[i]), u[i]--, v[i]--; for (int i = 1; i <= q; i++) scanf("%d%d%d", &a[i], &b[i], &c[i]), a[i]--, b[i]--, c[i]--; memset(f, ~0, sizeof(f)); printf("%lld\n", solve(0, (1 << n) - 1)); return 0; }
18
#include <bits/stdc++.h> using namespace std; char buf[1 << 20], *p1, *p2; template <class T> inline void read(T &n) { char ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++); T w = 1, x = 0; while (!isdigit(ch)) { if (ch == '-') w = -1; ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++); } n = x * w; } const int maxn = 2e6 + 5; const int mod = 1e9 + 7; int n, k, s[maxn], Ans[maxn], ans, t[maxn]; inline int mul(int x, int y) { return 1ll * x * y % mod; } inline int add(int x, int y) { x += y; return x >= mod ? x - mod : x; } int quick_pow(int x, int y) { int res = 1; while (y) { if (y & 1) res = mul(res, x); x = mul(x, x); y >>= 1; } return res; } int last[maxn], Next[27326297], num[27326297], cnt; inline void Add(int x, int y) { Next[++cnt] = last[x]; last[x] = cnt; num[cnt] = y; } int main() { read(n); read(k); for (int i = 2; i <= k; i++) for (int j = i; j <= k; j += i) Add(j, i); for (int now = 1; now <= k; now++) { s[now] = s[now - 1]; for (int i = last[now]; i; i = Next[i]) s[now] = add(s[now], t[now / num[i]]); Ans[now] = add(quick_pow(now, n), mod - s[now]); t[now] = add(Ans[now], mod - Ans[now - 1]); } for (int i = 1; i <= k; i++) ans = add(ans, Ans[i] ^ i); cout << ans << endl; return 0; }
15
#include <bits/stdc++.h> using namespace std; int charcount[26]; int res[26]; int n, tempcount, tnum; char c; int main() { for (int i = 0; i < 1024; ++i) { scanf("%c", &c); if (!(c - 'a' >= 0) && (c - 'a' < 26)) { break; } ++charcount[c - 'a']; } scanf("%d", &n); for (int i = 0; i < 26; ++i) if (charcount[i] != 0) ++tnum; if (tnum > n) return printf("-1"), 0; for (int i = 1; i < 1001; ++i) { tempcount = 0; for (int j = 0; j < 26; ++j) { tempcount += res[j] = (charcount[j] + i - 1) / i; } if (tempcount <= n) { printf("%d\n", i); for (int j = 0; j < 26; ++j) for (int k = 0; k < res[j]; ++k) printf("%c", j + 'a'); for (int j = tempcount; j < n; ++j) printf("a"); printf("\n"); return 0; } } }
6
#include <bits/stdc++.h> using namespace std; struct __timestamper {}; struct myset { set<pair<long long, int>> s; map<int, long long> val; long long add_all; public: void add_to_all(long long x) { add_all += x; } void insert(int pos, long long x) { x -= add_all; val[pos] = x; s.insert({x, pos}); } long long get_max() { if (s.empty()) { return 0; } return s.rbegin()->first + add_all; } void add_one(int pos, long long x) { s.erase({val[pos], pos}); val[pos] += x; s.insert({val[pos], pos}); } pair<long long, int> pop_max() { if (s.empty()) { return {0, -1}; } auto res = *s.rbegin(); erase(res.second); res.first += add_all; return res; } void erase(int pos) { s.erase({val[pos], pos}); val.erase(pos); } }; void solve(int n, long long c) { myset tot; myset have; vector<int> v(2 * n - 1); for (int& x : v) { scanf("%d", &x); } long long res = 0; for (int i = 0; i < n; i++) { if (i) { have.add_to_all(v[2 * i - 1]); tot.add_to_all(v[2 * i - 1]); } have.insert(i, 0); tot.insert(i, 0); have.add_to_all(v[2 * i]); tot.add_to_all(v[2 * i]); vector<int> limited; while (have.get_max() >= c) { pair<long long, int> v = have.pop_max(); tot.add_one(v.second, c - v.first); limited.push_back(v.second); } long long cur = tot.get_max(); tot.add_to_all(-cur); res += cur; for (int pos : limited) { tot.erase(pos); } } printf("%lld\n", res); } int main() { int n; long long c; while (scanf("%d%lld", &n, &c) == 2) { solve(n, c); } return 0; }
20
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; std::vector<int> a(n); std::vector<int> b(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; int l = 0; int r = 2001; while (l < r) { int count = 0; int mid = (l + r) / 2; for (int i = 0; i < n; i++) { count += max(0, a[i] * (mid + 1) - b[i]); } if (count <= k) { l = mid + 1; } else { r = mid; } } if (l == r) { cout << l << endl; } else { cout << 0 << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int LIM = 50; const int MAX_BIT = 26; const int MAX_MASK = 1 << MAX_BIT; struct state { char s; int i, j, pi, pj; state() {} state(char s, int i, int j, int pi, int pj) : s(s), i(i), j(j), pi(pi), pj(pj) {} }; bool operator<(const state& a, const state& b) { return a.s > b.s; } string f[LIM], ans = ""; int used = 0; int us[LIM][LIM]; pair<int, int> p[LIM][LIM]; int n, m, k; int si, sj, fi, fj; vector<pair<int, int>> edges[LIM][LIM]; int di[] = {-1, 1, 0, 0}, dj[] = {0, 0, -1, 1}; bool bit(const int mask, const int b) { return (mask >> b) & 1; } int cnt_bits(const int mask) { int res = 0; for (int i = 0; i < MAX_BIT; ++i) res += bit(mask, i); return res; } void get_next(int& mask, const int k) { if (cnt_bits(mask) < k) { ++mask; } else { for (int i = 0; i < MAX_BIT; ++i) if (bit(mask, i)) { mask += (1 << i); return; } } } void init_edges() { for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { for (int id = 0; id < 4; ++id) if (i + di[id] >= 0 && i + di[id] < n && j + dj[id] >= 0 && j + dj[id] < m && f[i + di[id]][j + dj[id]] == 'T') edges[i][j].push_back(make_pair(i + di[id], j + dj[id])); for (char c = 'a'; c <= 'z'; ++c) for (int id = 0; id < 4; ++id) if (i + di[id] >= 0 && i + di[id] < n && j + dj[id] >= 0 && j + dj[id] < m && f[i + di[id]][j + dj[id]] == c) edges[i][j].push_back(make_pair(i + di[id], j + dj[id])); } } void show(int mask) { for (int i = 0; i < MAX_BIT; ++i) if (bit(mask, i)) cout << (char)('a' + i); cout << endl; } string get_res(int i, int j) { string res = ""; while (i != -1) { res += f[i][j]; int ni = p[i][j].first; j = p[i][j].second; i = ni; } reverse(res.begin(), res.end()); return res; } string bfs(int mask) { for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { us[i][j] = -1; } int num = 1; p[si][sj] = make_pair(-1, -1); us[si][sj] = num; queue<pair<int, int>> q; q.push(make_pair(si, sj)); priority_queue<state> pq; while (!q.empty()) { ++num; int i = q.front().first, j = q.front().second; while (!q.empty() && us[i][j] == us[q.front().first][q.front().second]) { i = q.front().first; j = q.front().second; for (int id = 0; id < (int)edges[i][j].size(); ++id) { int ni = edges[i][j][id].first, nj = edges[i][j][id].second; pq.push(state(f[ni][nj], ni, nj, i, j)); } q.pop(); } char last = pq.top().s; while (!pq.empty()) { int ni = pq.top().i, nj = pq.top().j; if (pq.top().s != last) { ++num; last = pq.top().s; } if ((f[ni][nj] == 'T' || bit(mask, f[ni][nj] - 'a')) && us[ni][nj] == -1) { p[ni][nj] = make_pair(pq.top().pi, pq.top().pj); us[ni][nj] = num; q.push(make_pair(ni, nj)); } pq.pop(); } if (us[fi][fj] != -1) break; } if (us[fi][fj] == -1) return ""; return get_res(fi, fj); } int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> k; for (int i = 0; i < n; ++i) cin >> f[i]; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { used |= (1 << (f[i][j] - 'a')); if (f[i][j] == 'S') { si = i; sj = j; } if (f[i][j] == 'T') { fi = i; fj = j; } } init_edges(); int mask = 0; while (mask < MAX_MASK) { get_next(mask, k); string tans = bfs(mask); if (tans != "") { if (ans == "") { ans = tans; } else { if (ans.length() > tans.length()) ans = tans; else if (ans.length() == tans.length()) ans = min(ans, tans); } } } if (ans == "") { cout << -1; } else { for (int i = 1; i + 1 < (int)ans.length(); ++i) cout << ans[i]; } return 0; }
16
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 50; char s[maxn], t[maxn], ans[maxn]; int main() { gets(s); gets(t); int k = 0, len = strlen(s); for (int i = 0; i < len; i++) if (s[i] != t[i]) k++; if (k % 2) { printf("impossible\n"); return 0; } int i = 0; k /= 2; for (; i < len; i++) { ans[i] = s[i]; if (k == 0) break; if (s[i] != t[i]) k--; } for (; i < len; i++) ans[i] = t[i]; cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; pair<pair<int, int>, pair<int, int> > intersect(pair<int, int> f1, pair<int, int> f2, pair<int, int> s1, pair<int, int> s2) { int x5, y5, x6, y6; x5 = max(min(f1.first, f2.first), min(s1.first, s2.first)); x6 = min(max(f1.first, f2.first), max(s1.first, s2.first)); y5 = max(min(f1.second, f2.second), min(s1.second, s2.second)); y6 = min(max(f1.second, f2.second), max(s1.second, s2.second)); if (x5 >= x6 || y5 >= y6) { return {{0, 0}, {0, 0}}; } return {{x5, y5}, {x6, y6}}; } bool isPrime(long long n) { if (n < 2) return 0; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return 0; } return 1; } void faster() { if (0) { freopen( "taskA" ".in", "r", stdin); freopen( "taskA" ".out", "w", stdout); } if (1) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } } const long long INF = 1e18; const int inf = 1e9; const int MAXN = (int)2e5 + 123; const int MOD = (int)1e9 + 7; const long double PI = acos(-1); const long double EPS = 3E-16; namespace MATH_CAL { const int mod = MOD; inline long long add(long long a, long long b) { return a + b >= mod ? a + b - mod : a + b; } inline long long sub(long long a, long long b) { return a - b < 0 ? a - b + mod : a - b; } inline long long mul(long long a, long long b) { return (long long)a * b % mod; } inline void Add(long long &a, long long b) { (a += b) >= mod ? a -= mod : 0; } inline void Sub(long long &a, long long b) { (a -= b) < 0 ? a += mod : 0; } inline long long qpow(long long first, long long n) { long long r = 1; for (; n; n /= 2, first = mul(first, first)) if (n & 1) r = mul(r, first); return r; } inline long long mod_inv(long long first) { return qpow(first, mod - 2); } inline long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } } // namespace MATH_CAL using namespace MATH_CAL; void solve() { int n, m; cin >> n >> m; int fact = 1; for (int i = 1; i <= 2 * m; i++) { fact = mul(fact, i); } int sec = 1; for (int i = 1; i <= n - 1; i++) { sec = mul(sec, i); } int up = 1; for (int i = 1; i <= 2 * m + n - 1; i++) { up = mul(up, i); } int down = mul(mod_inv(fact), mod_inv(sec)); cout << mul(up, down) << endl; } int main() { faster(); int t = 1; while (t--) { solve(); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 2e3; int d[N][N] = {0}, inq[N][N] = {0}; int n, m; bool check(vector<string> &a, int x, int y) { bool x1 = 0, x2 = 0, x3 = 0, x4 = 0; if (x < 0 || x > n - 1 || y < 0 || y > m - 1) return 0; if (inq[x][y] || a[x][y] == '.') return 0; if (x > 0 && y > 0) x1 = (a[x - 1][y] == '.') & (a[x - 1][y - 1] == '.') & (a[x][y - 1] == '.'); if (x < n - 1 && y > 0) x2 = (a[x][y - 1] == '.') & (a[x + 1][y - 1] == '.') & (a[x + 1][y] == '.'); if (x < n - 1 && y < m - 1) x3 = (a[x + 1][y] == '.') & (a[x + 1][y + 1] == '.') & (a[x][y + 1] == '.'); if (x > 0 && y < m - 1) x4 = (a[x - 1][y] == '.') & (a[x - 1][y + 1] == '.') & (a[x][y + 1] == '.'); x1 = x1 | x2 | x3 | x4; if (x1) inq[x][y] = 1; return x1; } int main() { cin >> n >> m; queue<pair<int, int> > q; vector<string> a(n); for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (check(a, i, j)) q.push(make_pair(i, j)); int x, y; while (!q.empty()) { x = (q.front()).first; y = (q.front()).second; a[x][y] = '.'; if (check(a, x - 1, y + 1)) q.push(make_pair(x - 1, y + 1)); if (check(a, x - 1, y)) q.push(make_pair(x - 1, y)); if (check(a, x - 1, y - 1)) q.push(make_pair(x - 1, y - 1)); if (check(a, x, y + 1)) q.push(make_pair(x, y + 1)); if (check(a, x, y - 1)) q.push(make_pair(x, y - 1)); if (check(a, x + 1, y + 1)) q.push(make_pair(x + 1, y + 1)); if (check(a, x + 1, y - 1)) q.push(make_pair(x + 1, y - 1)); if (check(a, x + 1, y)) q.push(make_pair(x + 1, y)); q.pop(); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << a[i][j]; cout << "\n"; } return 0; }
16
#include <bits/stdc++.h> using namespace std; int n, m; int main() { cin >> n >> m; if (n == m) cout << "Yes"; else cout << "No"; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxn = 5e4 + 10; const int SIZE = 256; int n, q; int a[maxn]; vector<int> adj[maxn]; int h[maxn]; int par[maxn]; int id; int trie[maxn][2]; int cnt[maxn]; int L[200 + 10][maxn]; int R[256 + 10][maxn]; int stk[maxn]; int top; int nx[maxn]; inline void add(int x) { int cur = 0; ++cnt[cur]; for (int bit = 7; bit >= 0; --bit) { int dir; if ((1 << bit) & x) dir = 1; else dir = 0; if (trie[cur][dir] == -1) trie[cur][dir] = ++id; cur = trie[cur][dir]; ++cnt[cur]; } } inline void remove(int x) { int cur = 0; --cnt[cur]; for (int bit = 7; bit >= 0; --bit) { int dir; if ((1 << bit) & x) dir = 1; else dir = 0; cur = trie[cur][dir]; --cnt[cur]; } } inline int query(int x) { int ans = 0; int cur = 0; if (!cnt[cur]) return 0; for (int bit = 7; bit >= 0; --bit) { if ((1 << bit) & x) { if (trie[cur][0] != -1 && cnt[trie[cur][0]]) { cur = trie[cur][0]; ans += (1 << bit); } else cur = trie[cur][1]; } else { if (trie[cur][1] != -1 && cnt[trie[cur][1]]) { cur = trie[cur][1]; ans += (1 << bit); } else cur = trie[cur][0]; } } return ans; } void dfs(int v) { stk[++top] = v; nx[v] = stk[max(0, top - SIZE)]; for (int i = top; i > 0 && i > top - SIZE; --i) { int val = a[stk[i]]; R[val >> 8][v] = max(R[val >> 8][v], (val & 255) ^ (top - i)); } if (nx[v]) remove(a[nx[v]] >> 8); add(a[v] >> 8); for (int i = 0; i <= 200; ++i) L[i][v] = query(i); for (int i = 0; i < adj[v].size(); ++i) { int u = adj[v][i]; if (u == par[v]) continue; par[u] = v; h[u] = h[v] + 1; dfs(u); } if (nx[v]) add(a[nx[v]] >> 8); remove(a[v] >> 8); --top; } inline int solve(int u, int v) { int ans = 0; int node = v; int block = 0; while (h[node] - h[u] >= SIZE) { int Ans = L[block][node]; int Xor = Ans ^ block; ans = max(ans, (Ans << 8) + R[Xor][node]); node = nx[node]; ++block; } while (node && h[node] >= h[u]) { ans = max(ans, a[node] ^ (h[v] - h[node])); node = par[node]; } return ans; } int main() { scanf("%d %d", &n, &q); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n - 1; ++i) { int u, v; scanf("%d %d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } memset(trie, -1, sizeof(trie)); dfs(1); while (q--) { int u, v; scanf("%d %d", &u, &v); printf("%d\n", solve(u, v)); } return 0; }
24
#include <bits/stdc++.h> int brojZaPal[512][512]; int sol[512][512]; int prev[512][512]; bool postoji[512][512]; char s[512]; int o; int res, solK; char solution[1024]; int starting; int min(int a, int b) { return a < b ? a : b; } int main() { scanf("%s", &s); scanf("%d", &starting); for (int i = 0; i < strlen(s); i++) { for (int j = i; j < strlen(s); j++) { o = 0; for (int k = i; k <= (i + j) / 2; k++) { if (s[k] != s[j + i - k]) { o++; } } brojZaPal[i][j] = o; } } for (int i = 0; i < strlen(s); i++) { sol[1][i] = brojZaPal[0][i]; postoji[1][i] = true; prev[1][i] = -1; for (int j = 1; j <= i; j++) { for (int k = 2; k <= 500; k++) { if (postoji[k - 1][j - 1]) { if (postoji[k][i]) { sol[k][i] = min(brojZaPal[j][i] + sol[k - 1][j - 1], sol[k][i]); if (sol[k][i] == brojZaPal[j][i] + sol[k - 1][j - 1]) { prev[k][i] = j - 1; } } else { sol[k][i] = brojZaPal[j][i] + sol[k - 1][j - 1]; postoji[k][i] = true; prev[k][i] = j - 1; } } } } } res = strlen(s); solK = res; for (int i = 1; i <= starting; i++) { if (postoji[i][strlen(s) - 1]) { if (sol[i][strlen(s) - 1] < res) { res = sol[i][strlen(s) - 1]; solK = i; } } } printf("%d\n", res); int position = strlen(s) - 1; int place = 0; int mem; int currNum = solK; int previous; while (position >= 0) { mem = prev[currNum][position]; previous = position; while (position > mem) { if (position > (previous + mem) / 2) { solution[place] = s[mem + 1 + (previous - position)]; } else { solution[place] = s[position]; } position--; place++; } currNum--; position = mem; solution[place] = '+'; place++; } for (int i = place - 2; i >= 0; i--) { printf("%c", solution[i]); } return 0; }
11
#include <bits/stdc++.h> using namespace std; template <typename T> void pfill(T* pst, const T* ped, T val) { for (; pst != ped; *(pst++) = val) ; } template <typename T> void pcopy(T* pst, const T* ped, T* pval) { for (; pst != ped; *(pst++) = *(pval++)) ; } const int N = 262144; const int Mod = 998244353; const int bzmax = 19; const int g = 3; void exgcd(int a, int b, int& x, int& y) { if (!b) { x = 1, y = 0; } else { exgcd(b, a % b, y, x); y -= (a / b) * x; } } int inv(int a, int Mod) { int x, y; exgcd(a, Mod, x, y); return (x < 0) ? (x + Mod) : (x); } template <const int Mod = ::Mod> class Z { public: int v; Z() : v(0) {} Z(int x) : v(x) {} Z(long long x) : v(x % Mod) {} friend Z operator+(const Z& a, const Z& b) { int x; return Z(((x = a.v + b.v) >= Mod) ? (x - Mod) : (x)); } friend Z operator-(const Z& a, const Z& b) { int x; return Z(((x = a.v - b.v) < 0) ? (x + Mod) : (x)); } friend Z operator*(const Z& a, const Z& b) { return Z(a.v * 1ll * b.v); } friend Z operator~(const Z& a) { return inv(a.v, Mod); } friend Z operator-(const Z& a) { return Z(0) - a; } Z& operator+=(Z b) { return *this = *this + b; } Z& operator-=(Z b) { return *this = *this - b; } Z& operator*=(Z b) { return *this = *this * b; } friend bool operator==(const Z& a, const Z& b) { return a.v == b.v; } }; Z<> qpow(Z<> a, int p) { if (p < Mod - 1) p += Mod - 1; Z<> rt = 1, pa = a; for (; p; p >>= 1, pa = pa * pa) { if (p & 1) { rt = rt * pa; } } return rt; } const Z<> inv2((Mod + 1) >> 1); class NTT { private: Z<> gn[bzmax + 4], _gn[bzmax + 4]; public: NTT() { for (int i = 0; i <= bzmax; i++) { gn[i] = qpow(Z<>(g), (Mod - 1) >> i); _gn[i] = qpow(Z<>(g), -((Mod - 1) >> i)); } } void operator()(Z<>* f, int len, int sgn) { for (int i = 1, j = len >> 1, k; i < len - 1; i++, j += k) { if (i < j) swap(f[i], f[j]); for (k = len >> 1; k <= j; j -= k, k >>= 1) ; } Z<>*wn = (sgn > 0) ? (gn + 1) : (_gn + 1), w, a, b; for (int l = 2, hl; l <= len; l <<= 1, wn++) { hl = l >> 1, w = 1; for (int i = 0; i < len; i += l, w = 1) { for (int j = 0; j < hl; j++, w *= *wn) { a = f[i + j], b = f[i + j + hl] * w; f[i + j] = a + b; f[i + j + hl] = a - b; } } } if (sgn < 0) { Z<> invlen = ~Z<>(len); for (int i = 0; i < len; i++) { f[i] *= invlen; } } } int correct_len(int len) { int m = 1; for (; m <= len; m <<= 1) ; return m; } } NTT; void pol_inverse(Z<>* f, Z<>* g, int n) { static Z<> A[N]; if (n == 1) { g[0] = ~f[0]; } else { int hn = (n + 1) >> 1, t = NTT.correct_len(n << 1 | 1); pol_inverse(f, g, hn); pcopy(A, A + n, f); pfill(A + n, A + t, Z<>(0)); pfill(g + hn, g + t, Z<>(0)); NTT(A, t, 1); NTT(g, t, 1); for (int i = 0; i < t; i++) { g[i] = g[i] * (Z<>(2) - g[i] * A[i]); } NTT(g, t, -1); pfill(g + n, g + t, Z<>(0)); } } void pol_sqrt(Z<>* f, Z<>* g, int n) { static Z<> A[N], B[N]; if (n == 1) { g[0] = f[0]; } else { int hn = (n + 1) >> 1, t = NTT.correct_len(n + n); pol_sqrt(f, g, hn); pfill(g + hn, g + n, Z<>(0)); for (int i = 0; i < hn; i++) A[i] = g[i] + g[i]; pfill(A + hn, A + t, Z<>(0)); pol_inverse(A, B, n); pcopy(A, A + n, f); pfill(A + n, A + t, Z<>(0)); NTT(A, t, 1); NTT(B, t, 1); for (int i = 0; i < t; i++) A[i] *= B[i]; NTT(A, t, -1); for (int i = 0; i < n; i++) g[i] = g[i] * inv2 + A[i]; } } typedef class Poly : public vector<Z<> > { public: using vector<Z<> >::vector; Poly& fix(int sz) { resize(sz); return *this; } } Poly; Poly operator+(Poly A, Poly B) { int n = A.size(), m = B.size(); int t = max(n, m); A.resize(t), B.resize(t); for (int i = 0; i < t; i++) { A[i] += B[i]; } return A; } Poly operator-(Poly A, Poly B) { int n = A.size(), m = B.size(); int t = max(n, m); A.resize(t), B.resize(t); for (int i = 0; i < t; i++) { A[i] -= B[i]; } return A; } Poly sqrt(Poly a) { Poly rt(a.size()); pol_sqrt(a.data(), rt.data(), a.size()); return rt; } Poly operator*(Poly A, Poly B) { int n = A.size(), m = B.size(); int k = NTT.correct_len(n + m - 1); if (n < 20 || m < 20) { Poly rt(n + m - 1); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { rt[i + j] += A[i] * B[j]; } } return rt; } A.resize(k), B.resize(k); NTT(A.data(), k, 1); NTT(B.data(), k, 1); for (int i = 0; i < k; i++) { A[i] *= B[i]; } NTT(A.data(), k, -1); A.resize(n + m - 1); return A; } Poly operator~(Poly f) { int n = f.size(), t = NTT.correct_len((n << 1) | 1); Poly rt(t); f.resize(t); pol_inverse(f.data(), rt.data(), n); rt.resize(n); return rt; } Poly operator/(Poly A, Poly B) { int n = A.size(), m = B.size(); if (n < m) { return Poly{0}; } int r = n - m + 1; reverse(A.begin(), A.end()); reverse(B.begin(), B.end()); A.resize(r), B.resize(r); A = A * ~B; A.resize(r); reverse(A.begin(), A.end()); return A; } Poly operator%(Poly A, Poly B) { int n = A.size(), m = B.size(); if (n < m) { return A; } if (m == 1) { return Poly{0}; } A = A - A / B * B; A.resize(m - 1); return A; } Z<> Inv[N]; void init_inv(int n) { Inv[0] = 0, Inv[1] = 1; for (int i = 2; i <= n; i++) { Inv[i] = Inv[Mod % i] * Z<>((Mod - (Mod / i))); } } void diff(Poly& f) { if (f.size() == 1) { f[0] = 0; return; } for (int i = 1; i < (signed)f.size(); i++) { f[i - 1] = f[i] * Z<>(i); } f.resize(f.size() - 1); } void integ(Poly& f) { f.resize(f.size() + 1); for (int i = (signed)f.size() - 1; i; i--) { f[i] = f[i - 1] * Inv[i]; } f[0] = 0; } Poly Diff(Poly f) { diff(f); return f; } Poly Integ(Poly f) { integ(f); return f; } Poly ln(Poly f) { int n = f.size(); Poly h = f; diff(h); f = h * ~f; f.resize(n - 1); integ(f); return f; } void pol_exp(Poly& f, Poly& g, int n) { Poly h; if (n == 1) { g.resize(1); g[0] = 1; } else { int hn = (n + 1) >> 1; pol_exp(f, g, hn); h.resize(n), g.resize(n); pcopy(h.data(), h.data() + n, f.data()); g = g * (Poly{1} - ln(g) + h); g.resize(n); } } Poly exp(Poly f) { int n = f.size(); Poly rt; pol_exp(f, rt, n); return rt; } Poly remove_front(Poly x) { assert(!x.empty()); x.erase(x.begin()); return x; } int n; Poly fac, _fac; void prepare(int n) { fac.resize(n + 1); _fac.resize(n + 1); fac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = fac[i - 1] * i; } _fac[n] = ~fac[n]; for (int i = n; i; i--) { _fac[i - 1] = _fac[i] * i; } } Poly work() { Poly f = ~remove_front(ln((Poly{1, 1}).fix(n + 3))); Poly fn = exp(ln(f) * Poly{n + 1}); Poly g = ~remove_front(Poly{1} - f); Poly a = (fn * g).fix(n + 1); g = (g * g).fix(n + 1); Poly b = ((fn * Diff(f)).fix(n + 1) * g).fix(n + 1); Poly h(n); Z<> coef = ~Z<>(n + 1); for (int i = 0; i < n; i++) { h[i] = (a[i + 1] * (n - i + 1) + b[i + 1]) * coef; } Poly F(_fac.begin() + 1, _fac.begin() + n + 2); h = remove_front((F * ~remove_front(Poly{1} - F)).fix(n + 1)) - h; h[n - 1] = _fac[n]; return h; } int main() { scanf("%d", &n); init_inv(n + 3); prepare(n + 3); Poly f = work(); Poly g(n); for (int i = 0; i < n; i++) { f[i] *= fac[i]; } for (int i = 0; i < n; i++) { g[i] = _fac[i]; if (i & 1) { g[i] = -g[i]; } } reverse(g.begin(), g.end()); f = f * g; for (int i = 0; i < n; i++) { Z<> ans = f[i + n - 1] * fac[n] * _fac[i]; printf("%d ", ans.v); } return 0; }
23
#include <bits/stdc++.h> using namespace std; int cnt = 0, cntq = 0, ans = 0; int forw[5001][5001], back[5001][5001]; string str; int main() { cin >> str; int len = str.length(); for (int i = 0; i < len; i++) { cnt = cntq = 0; if (str[i] == ')') { continue; } if (str[i] == '(') { cnt = 1; } else { cnt = 1; } for (int j = i + 1; j < len; j++) { if (str[j] == ')') { cnt--; } else if (str[j] == '(') { cnt++; } else { cnt++, cntq++; } if (cnt < 0) { break; } if (((cnt % 2) == 0) && (cnt / 2 <= cntq)) { forw[i][j] = 1; } } } for (int i = len - 1; i >= 0; i--) { cnt = cntq = 0; if (str[i] == '(') { continue; } if (str[i] == ')') { cnt = 1; } else { cnt = 1; } for (int j = i - 1; j >= 0; j--) { if (str[j] == '(') { cnt--; } else if (str[j] == ')') { cnt++; } else { cnt++, cntq++; } if (cnt < 0) { break; } if (((cnt % 2) == 0) && (cnt / 2 <= cntq)) { back[j][i] = 1; } } } for (int i = 0; i < len; i++) { for (int j = i + 1; j < len; j++) { if (forw[i][j] && back[i][j]) { ans++; } } } cout << ans << "\n"; return 0; }
10
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T t) { return t < 0 ? -t : t; } const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } int a[100009]; int main() { int i, j, n; char c; scanf("%d", &n); for (i = 0; i < n; i++) { scanf(" %c", &c); a[i] = c - '0'; } int p = a[0]; int sz = 1, empty = 0; for (i = 1; i < n; i++) { if (a[i] != p) { p = !p; sz++; } else { empty++; } } if (sz == n) { printf("%d\n", n); return 0; } printf("%d\n", sz + 1 + (empty > 1)); }
8
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {-1, 1, 0, 0, 1, -1, -1, 1}; int n, k; vector<int> adj[101], adj2[101]; const ll mod = 1e9 + 7; void dfs(int x, int from = -1) { for (int y : adj[x]) if (y != from) { adj2[x].push_back(y); dfs(y, x); } } int f(int x, int l, int d); int dp2[111][111][44][44][2]; int g(int x, int i, int l, int dlo, bool have) { l = min(l, k + 1); if (i == adj2[x].size()) return have; int& res = dp2[x][i][l][dlo][have]; if (res != -1) return res; int y = adj2[x][i]; res = 1ll * f(y, l, -1) * g(x, i + 1, l, dlo, have) % mod; for (int d = dlo; d <= 2 * k; ++d) res = (0ll + res + 1ll * f(y, l, d) * g(x, i + 1, l, dlo, have || dlo == d)) % mod; return res; } int dp3[111][22][44]; int h(int x, int l, int d) { if (d > 2 * k) return 0; l = min(l, k + 1); int& res = dp3[x][l][d]; if (res != -1) return res; return res = (f(x, l, d) + h(x, l, d + 1)) % mod; } int dp[111][22][44]; int f(int x, int l, int d) { l = min(l, k + 1); int cur = 1 << 30; if (l >= 0) cur = l; if (d >= 0) cur = min(cur, d); if (cur > k) return 0; int& res = dp[x][l][d]; if (res != -1) return res; if (d == 0) { res = 1; for (int y : adj2[x]) res = 1ll * res * h(y, 1, -1) % mod; } else if (d < 0) { res = 1; for (int y : adj2[x]) res = (1ll * res * f(y, l + 1, -1)) % mod; } else { l = (l < 0) ? (d + 1) : min(l + 1, d + 1); res = g(x, 0, l, d - 1, 0); } return res; } int main() { ios::sync_with_stdio(0); cin >> n >> k; for (int i = (0), __ee = (n - 1); i < __ee; ++i) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } dfs(1); memset(dp, -1, sizeof dp); memset(dp2, -1, sizeof dp2); memset(dp3, -1, sizeof dp3); cout << h(1, -1, -1) << "\n"; }
17
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; char s[maxn]; int main() { int n, a, b; cin >> n >> a >> b; scanf("%s", s); int ff = 0, ans = 0; for (int i = 0; i < n; i++) { if (a == 0 && b == 0) continue; if (s[i] == '.') { if (ff == 0) { if (a >= b) { a--; ans++; ff = 1; } else { b--; ans++; ff = 2; } } else if (ff == 1) { if (b > 0) { b--; ans++; ff = 2; } else { ff = 0; } } else { if (a > 0) { a--; ans++; ff = 1; } else { ff = 0; } } } else { ff = 0; } } printf("%d\n", ans); }
5
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; using PP = pair<ll, P>; const ll n_ = 111 + 100, inf = 1e18, mod = 1e9 + 7, sqrtN = 333, p = 27; ll dy[4] = {-1, 0, 1, 0}, dx[4] = {0, 1, 0, -1}; ll n, m, k, tc = 1, a, b, c, d, sum, x, y, z, w, base, ans; ll gcd(ll x, ll y) { if (!y) return x; return gcd(y, x % y); } vector<ll> v[n_]; ll checked[n_], cnt[n_], dp[n_][n_]; ll f(ll x, ll y) { for (int i = 0; i <= x; i++) for (int j = 0; j <= y; j++) dp[i][j] = 0; dp[0][0] = 1; for (int i = 0; i < x; i++) for (int j = 0; j <= y; j++) { dp[i + 1][j] = (dp[i + 1][j] + dp[i][j]) % mod; dp[i + 1][j + 1] = (dp[i + 1][j + 1] + (dp[i][j] * cnt[i]) % mod) % mod; } return dp[x][y]; } void solve() { cin >> n >> k; for (int i = 1; i <= n; i++) v[i].clear(); for (int i = 0; i < n - 1; i++) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } ans = 0; if (k == 2) { ans = n * (n - 1) / 2; cout << ans % mod << '\n'; return; } for (int i = 1; i <= n; i++) { memset(checked, 0, sizeof(checked)); checked[i] = true; vector<P> temp; for (int j = 0; j < v[i].size(); j++) { temp.push_back({v[i][j], j}); cnt[j] = 1; checked[v[i][j]] = true; } while (temp.size()) { ans += f((ll)v[i].size(), k); ans %= mod; vector<P> V; for (auto nxt : temp) { cnt[nxt.second]--; for (auto nxt2 : v[nxt.first]) { if (checked[nxt2]) continue; checked[nxt2] = true; cnt[nxt.second]++; V.push_back({nxt2, nxt.second}); } } temp = V; } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> tc; while (tc--) solve(); }
14
#include <bits/stdc++.h> using namespace std; char a[111][111], b[111][111]; int n, m; string s; int main() { cin >> n >> m; for (int(i) = 0; (i) < (n); ++(i)) for (int(j) = 0; (j) < (m); ++(j)) { cin >> a[i][j]; b[j][i] = a[i][j]; } for (int(i) = 0; (i) < (n); ++(i)) for (int(k) = 0; (k) < (26); ++(k)) if (count(a[i], a[i] + m, 'a' + k) > 1) for (int(j) = 0; (j) < (m); ++(j)) if (a[i][j] == 'a' + k) a[i][j] = 0; for (int(i) = 0; (i) < (m); ++(i)) for (int(k) = 0; (k) < (26); ++(k)) if (count(b[i], b[i] + n, (char)('a' + k)) > 1) for (int(j) = 0; (j) < (n); ++(j)) if (b[i][j] == 'a' + k) b[i][j] = 0; for (int(i) = 0; (i) < (n); ++(i)) for (int(j) = 0; (j) < (m); ++(j)) if (a[i][j] && b[j][i]) s += a[i][j]; cout << s << endl; }
3
#include <bits/stdc++.h> using namespace std; mt19937 rng((long long)chrono::steady_clock::now().time_since_epoch().count()); const long long mod = 1e9 + 7; long long n, a[200010]; void op_ida(long long Ini, vector<long long> &ans) { for (long long ini = Ini; ini <= n - 2; ini++) { long long meio = ini + 1, fim = ini + 2; if (a[meio] == 0) continue; if (a[fim] == 1) { ans.push_back(ini); a[meio] = a[fim] = 0; continue; } if (meio <= n - 2) { if (a[fim + 1] == 1) { ans.push_back(meio); a[meio] = a[fim + 1] = 0; } else { ans.push_back(meio); ans.push_back(ini); a[meio] = a[fim] = 0; a[fim + 1] = 1; } continue; } cout << "NO\n"; exit(0); } } void op_volta(long long Ini, vector<long long> &ans) { for (long long ini = Ini; ini >= 3; ini--) { long long meio = ini - 1, fim = ini - 2; if (a[meio] == 0) continue; if (a[fim] == 1) { ans.push_back(fim); a[meio] = a[fim] = 0; continue; } if (meio >= 3) { if (a[fim - 1] == 1) { ans.push_back(fim - 1); a[meio] = a[fim - 1] = 0; } else { ans.push_back(fim - 1); ans.push_back(fim); a[meio] = a[fim] = 0; a[fim - 1] = 1; } continue; } cout << "NO\n"; exit(0); } } void solve() { cin >> n; a[n + 1] = a[0] = 0; long long tot = 0; for (long long i = 1; i <= n; i++) { cin >> a[i]; tot += a[i]; } if (tot & 1 or tot == n) { cout << "NO\n"; return; } if (a[1] == 0) { vector<long long> ans; op_ida(1, ans); cout << "YES\n"; cout << ans.size() << "\n"; for (auto u : ans) cout << u << " "; cout << "\n"; return; } if (a[n] == 0) { vector<long long> ans; op_volta(n, ans); cout << "YES\n"; cout << ans.size() << "\n"; for (auto u : ans) cout << u << " "; cout << "\n"; return; } long long pref = 0; for (long long i = 1; i <= n; i++) { pref += a[i]; if (a[i] == 1) continue; if (pref % 2 == 0) { vector<long long> ans; op_ida(i, ans); op_volta(i, ans); cout << "YES\n"; cout << ans.size() << "\n"; for (auto u : ans) cout << u << " "; cout << "\n"; return; } else { long long j = i; while (j <= n and a[j] == 0) j++; j--; if ((j - i + 1) & 1) { vector<long long> ans; while (j != i) { a[j] = a[j - 1] = 1; ans.push_back(j - 1); j -= 2; } ans.push_back(i - 1); a[i + 1] = 0; a[i - 1] = 0; op_ida(i, ans); op_volta(i, ans); cout << "YES\n"; cout << ans.size() << "\n"; for (auto u : ans) cout << u << " "; cout << "\n"; return; } i = j; } } cout << "NO\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long t; cin >> t; for (long long i = 1; i <= t; i++) solve(); }
17
#include <bits/stdc++.h> using namespace std; long long n, k, s; void findAns() { cout << "YES" << "\n"; int p = 1; for (int i = 0; i < k; i++) { if (i < s % k) { if (i & 1) p -= s / k + 1; else p += s / k + 1; } else { if (i & 1) p -= s / k; else p += s / k; } cout << p << " "; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> s; if (k > s || (n - 1) * k < s) cout << "NO"; else findAns(); }
8
#include <bits/stdc++.h> using namespace std; int ss[100]; int main() { int n, m, h; cin >> n >> m >> h; int sum = 0; for (int i = 0; i < m; i++) { cin >> ss[i]; sum += ss[i]; } if (sum < n) { cout << "-1.0" << endl; return 0; } else if (ss[h - 1] == 1) { cout << "0.0" << endl; return 0; } else if (sum == n) { cout << "1.0" << endl; return 0; } if (ss[h - 1] == 1) { cout << "0.0" << endl; return 0; } double res = 1; for (int i = 0; i < ss[h - 1] - 1; i++) { res *= (double)(sum - n - i); res /= (double)(sum - i - 1); } res = 1.0 - res; printf("%.6lf\n", res); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 20; int dp[(1 << N) - 1] = {}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; string str; cin >> str; int L = str.length(); int val = 1; int mask; int c; for (int i = 0; i < L; i++) { mask = 0; for (int k = 0; k <= N && i + k < L; k++) { c = str[i + k] - 'a'; if ((mask & (1 << c))) break; mask = mask | (1 << c); dp[mask] = k + 1; } } for (int i = 0; i < (1 << N); i++) for (int j = 0; j < N; j++) if (i & (1 << j)) dp[i] = max(dp[i], dp[i ^ (1 << j)]); for (int i = 0; i < (1 << N); i++) val = max(val, dp[i] + dp[(~i) & ((1 << N) - 1)]); cout << val << '\n'; return 0; }
14
#include <bits/stdc++.h> using namespace std; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int BitSetUp(int x, int idx) { return x = x | (1 << idx); } int BitUnSet(int x, int idx) { return x = x & (~(1 << idx)); } bool BitCheking(int x, int idx) { return x & (1 << idx); } bool PowerOfTwo(int x) { return (x && !(x & (x - 1))); } void DenTofieM() { long long test; scanf("%lld", &test); while (test--) { long long ar[3], k = 1; for (long long i = 0; i < 3; i++) scanf("%lld", &ar[i]); sort(ar, ar + 3); cout << max(k, ar[2] - ar[1] - ar[0] + 1) << "\n"; } } int main() { DenTofieM(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, k; long long a[1000005]; long long dp[1000005]; long long cal(long long now) { long long ans = 0; int cnt = 1; while (now) { int res = now % 10; if (res % 3 == 0) ans += a[cnt] * res / 3ll; now /= 10; cnt++; } return ans; } int main() { scanf("%d", &k); for (int i = 1; i <= 6; i++) scanf("%I64d", &a[i]); for (int i = 0; i < 1000005; i++) dp[i] = cal(i); long long nowsum = 3; for (int i = 1; i <= 6; i++) { long long temp = 3 * (k - 1); for (int s = 1; s <= temp; s <<= 1) { temp -= s; for (int j = 1000005 - 1; j >= 0; j--) { if (1ll * j - nowsum * s < 0) break; dp[j] = max(dp[j], dp[j - nowsum * s] + s * a[i]); } } for (int j = 1000005 - 1; j >= 0; j--) { if (1ll * j - nowsum * temp < 0) break; dp[j] = max(dp[j], dp[j - nowsum * temp] + temp * a[i]); } nowsum = nowsum * 10; } int _; scanf("%d", &_); while (_--) { int x; scanf("%d", &x); printf("%I64d\n", dp[x]); } }
22
#include <bits/stdc++.h> using namespace std; int n, m; int off = 1; int a[1000006]; int b[1000006]; pair<int, int> t[4000005]; void ubaci(int pos, int koliko) { pos += off; t[pos].first += koliko; t[pos].second += koliko; pos /= 2; while (pos > 0) { t[pos].first = t[pos * 2 + 1].first + t[pos * 2].first; t[pos].second = max(t[pos * 2 + 1].second, t[pos * 2 + 1].first + t[pos * 2].second); pos /= 2; } } int query(int l, int r, int node, int suma) { if (l == r) { if (suma + t[node].first > 0) return l; else return -2; } if (t[node * 2 + 1].second + suma > 0) query((l + r) / 2 + 1, r, node * 2 + 1, suma); else query(l, (l + r) / 2, node * 2, suma + t[node * 2 + 1].first); } int main() { cin >> n >> m; while (off < 1000000) off *= 2; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); a[i]--; ubaci(a[i], 1); } for (int i = 0; i < m; i++) { scanf("%d", &b[i]); b[i]--; ubaci(b[i], -1); } int q; cin >> q; int a1, a2, a3; for (int i = 0; i < q; i++) { scanf("%d%d%d", &a1, &a2, &a3); a2--; a3--; if (a1 == 1) { ubaci(a[a2], -1); a[a2] = a3; ubaci(a[a2], 1); } else { ubaci(b[a2], 1); b[a2] = a3; ubaci(b[a2], -1); } int res = query(0, off - 1, 1, 0); printf("%d\n", res + 1); } return 0; }
14
#include <bits/stdc++.h> using namespace std; long long n, k, dp[500002]; string s, t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> s >> t; s = ';' + s; t = ';' + t; dp[0] = 1; long long ans = 0; for (int i = 1; i <= n; i++) { dp[i] = 2 * dp[i - 1]; if (s[i] == 'b') dp[i]--; if (t[i] == 'a') dp[i]--; if (dp[i] > k) { for (int j = i; j <= n; j++) ans += k; return cout << ans << endl, 0; } ans += dp[i]; } cout << ans << endl; }
12
#include <bits/stdc++.h> using namespace std; const int K = 5000; int n, num[K + 3], divp[K + 3][K + 3], ed[K + 3]; long long ans; int main() { scanf("%d", &n); for (int i = 1, k; i <= n; i++) scanf("%d", &k), num[k]++; for (int i = 1; i <= K; i++) { for (int j = 1; j <= K; j++) divp[i][j] += divp[i - 1][j]; int now = i; for (int x = 2; x * x <= now; x++) { while (now % x == 0) { divp[i][x]++; now /= x; } } if (now > 1) divp[i][now]++; } for (int i = 1; i <= K; i++) { if (num[i] == 0) { ed[i] = 1; continue; } for (int j = 1; j <= K; j++) { ans += (long long)divp[i][j] * num[i]; if (divp[i][j]) ed[i] = j; } } long long now = ans; while (*max_element(ed + 1, ed + 1 + K) > 1) { vector<int> cnt(K + 3, 0); for (int i = 1; i <= K; i++) cnt[ed[i]] += num[i]; int p = max_element(cnt.begin(), cnt.end()) - cnt.begin(), sz = cnt[p]; if (sz * 2 <= n || p == 1) break; now = ans - 2 * sz + n; ans = min(ans, now); for (int i = 1; i <= K; i++) { if (ed[i] != p) ed[i] = 1; if (ed[i] == 1) continue; divp[i][ed[i]]--; while (ed[i] > 1 && divp[i][ed[i]] == 0) ed[i]--; } } printf("%lld\n", ans); return 0; }
19
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; if (a % 4 == 1) cout << "0 A" << endl; else if (a % 4 == 2) cout << "1 B" << endl; else if (a % 4 == 3) cout << "2 A" << endl; else cout << "1 A" << endl; }
0
#include <bits/stdc++.h> using namespace std; string getMin(string a, string b) { if (a == "") return b; if (b == "") return a; if (a.length() < b.length()) return a; else if (a.length() > b.length()) return b; else return a < b ? a : b; } string gao(int n, int k) { string ans = ""; for (int bit0 = 0; bit0 < 10; bit0++) { for (int c9 = 0; c9 < 20; c9++) { int tmp = n; for (int i = 0; i <= k; i++) { if (bit0 + i < 10) { tmp -= bit0 + i; tmp -= c9 * 9; } else { tmp -= bit0 + i - 10; tmp--; } } if (tmp < 0) continue; if (tmp % (k + 1) != 0) continue; tmp /= k + 1; string res = ""; res = (char)('0' + bit0) + res; for (int i = 0; i < c9; i++) res = '9' + res; if (tmp > 8) { res = '8' + res; tmp -= 8; } while (tmp > 0) { res = (char)('0' + min(tmp, 9)) + res; tmp -= min(tmp, 9); } ans = getMin(ans, res); } } if (ans == "") ans = "-1"; return ans; } int main() { int T; scanf("%d", &T); while (T--) { int n, k; scanf("%d%d", &n, &k); cout << gao(n, k) << endl; } return 0; }
14
#include <bits/stdc++.h> using namespace std; class grav { private: char det; int n = 0; vector<vector<int> > gr; public: grav(string type) { this->n = 0; if (type == "lr") { this->det = 1; } else if (type == "ly") { this->det = 2; } } grav(string type, int a) { this->n = a; this->det = 0; for (int i = 0; i < this->n; i++) { this->gr.push_back(vector<int>()); for (int j = 1; j < this->n; j++) { this->gr[i].push_back(0); } } } void rpush(int a, int b) { --a; --b; if (this->det == 0) { this->gr[a][b] = 1; this->gr[b][a] = 1; } if (this->r(a + 1, b + 1)) { return; } if (this->det == 1) { if (a + 1 > this->n) { this->n = a + 1; } if (b + 1 > this->n) { this->n = b + 1; } this->gr.push_back(vector<int>()); this->gr[this->gr.size() - 1].push_back(a); this->gr[this->gr.size() - 1].push_back(b); } if (this->det == 2) { if (a + 1 > this->gr.size()) { for (int i = this->gr.size(); i < a + 1; i++) { this->gr.push_back(vector<int>()); } this->n = a + 1; } this->gr[a].push_back(b); if (b + 1 > this->gr.size()) { for (int i = this->gr.size(); i < b + 1; i++) { this->gr.push_back(vector<int>()); } this->n = b + 1; } this->gr[b].push_back(a); } } int size_info() { return this->n; } bool r(int a, int b) { --a; --b; if (this->det == 0) { return (this->gr[a][b]); } if (this->det == 1) { for (int i = 0; i < this->gr.size(); i++) { if ((this->gr[i][0] == a && this->gr[i][1] == b) || (this->gr[i][0] == b && this->gr[i][1] == a)) { return 1; } } return 0; } if (this->det == 2) { if (a + 1 > this->gr.size()) { return 0; } for (int i = 0; i < this->gr[a].size(); i++) { if (this->gr[a][i] == b) { return 1; } } return 0; } } void rdel(int a, int b) { --a; --b; if (this->det == 0) { this->gr[a][b] = 0; this->gr[b][a] = 0; } if (!this->r(a + 1, b + 1)) { return; } if (this->det == 1) { for (int i = 0; i < this->gr.size(); i++) { if ((this->gr[i][0] == a && this->gr[i][1] == b) || (this->gr[i][0] == b && this->gr[i][1] == a)) { this->gr.erase(this->gr.begin() + i); return; } } } if (this->det == 2) { for (int i = 0; i < this->gr[a].size(); i++) { if (this->gr[a][i] == b) { this->gr[a].erase(this->gr[a].begin() + i); break; } } for (int i = 0; i < this->gr[b].size(); i++) { if (this->gr[b][i] == a) { this->gr[b].erase(this->gr[b].begin() + i); return; } } } } void dfs(int s) { s--; stack<int> st; vector<bool> used(n, 0); st.push(s); used[s] = 1; if (det == 0) { } if (det == 1) { } if (det == 2) { while (!st.empty()) { s = st.top(); bool l = 0; for (int i = 0; i < gr[s].size(); i++) { if (!used[gr[s][i]]) { used[gr[s][i]] = 1; st.push(gr[s][i]); l = 1; break; } } if (l) { continue; } st.pop(); } } } void bfs(int s) { s--; queue<int> q; vector<bool> used(n, 0); q.push(s); used[s] = 1; if (det == 0) { } if (det == 1) { } if (det == 2) { while (!q.empty()) { s = q.front(); q.pop(); for (int i = 0; i < gr[s].size(); i++) { if (!used[gr[s][i]]) { used[gr[s][i]] = 1; q.push(gr[s][i]); } } } } } }; long long a[100005]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; for (; t >= 1; t--) { long long n, k = 0, sum = 0; string s; bool l = 0; cin >> n; vector<int> f(n); int ch = 0; for (int i = 0; i < n; i++) { cin >> f[i]; sum += f[i]; if (f[i] >= 2) { ch++; } if (k < f[i]) { k = f[i]; } } if (n == 1) { cout << "T" << endl; continue; } if (n == 2 && abs(f[0] - f[1]) != 0) { cout << "T" << endl; continue; } if (n == 2 && abs(f[0] - f[1]) == 0) { cout << "HL" << endl; continue; } if (k >= sum / 2 + (sum % 2 != 0) + 1) { cout << "T" << endl; continue; } else { if (sum % 2 == 0) { cout << "HL" << endl; continue; } if (sum % 2 != 0) { cout << "T" << endl; continue; } } } }
10
#include <bits/stdc++.h> using namespace std; string s; string answer; void In() { cin >> s; } void Solve() { answer = s; stack<pair<char, int>> stk; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') stk.push({s[i], i}); else { if (!stk.empty() && stk.top().first == '1') stk.pop(); else stk.push({s[i], i}); } } while (!stk.empty()) { answer[stk.top().second] = '0'; stk.pop(); } } void Out() { cout << answer << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); In(); Solve(); Out(); }
12