func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; void init_code() {} int main() { init_code(); long long t; cin >> t; while (t--) { long long n, k; cin >> n >> k; long long num = 1, res = 0; while (k) { if (k & 1) { res += num; res %= 1000000000 + 7; } k >>= 1; num *= n; num %= 1000000000 + 7; } cout << res << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { for (; b; a %= b, swap(a, b)) ; return a; } int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int main(void) { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; int ones = 0; vector<int> one(k); string st; cin >> st; for (int i = 0; i < n; i++) { if (st[i] == 1 ) { one[i % k]++; ones++; } } int ans = ones; for (int i = 0; i < k; i++) { int tmp = ones - one[i % k]; string t = ; vector<int> cnt; cnt.push_back(0); for (int j = i; j < n; j += k) { t += st[j]; if (cnt.size() == 0) cnt.push_back(st[j] - 0 ); else cnt.push_back(cnt.back() + st[j] - 0 ); } int len = t.size(); vector<int> dp(len + 1); int res = 1e9; for (int j = 1; j <= len; j++) { int p = (t[j - 1] == 0 ? 1 : 0); dp[j] = min(dp[j - 1] + p, cnt[j - 1] + p); } for (int j = 1; j <= len; j++) res = min(res, dp[j] + cnt[len] - cnt[j]); ans = min(ans, res + tmp); } cout << ans << n ; } }
#include <bits/stdc++.h> using namespace std; long long a[15], y, k2, k3, r1, z, k4, r, max1, k5, k1, min1, b[1005], n1, n, m, k, i, j; vector<char> f; int main() { string s, s1; cin >> k >> n; cin >> s; for (i = 0; i < n; i++) { for (j = 0; j < 10; j++) a[j] = 0; cin >> k1 >> k2; s1.clear(); for (j = k1 - 1; j <= k2 - 1; j++) s1.push_back(s[j]); j = 0; k3 = 1; z = 0; while (j >= 0 && j < s1.size()) { if ((int)s1[j] >= (int) 0 && (int)s1[j] <= (int) 9 ) { a[(int)s1[j] - (int) 0 ]++; if (s1[j] == 0 ) s1[j] = - ; else s1[j] = (char)((int)s1[j] - 1); j += k3; z = 0; } else if (s1[j] == > ) { if (z) s1[k4] = - ; k4 = j; z = 1; k3 = 1; j += k3; } else if (s1[j] == < ) { if (z) s1[k4] = - ; k4 = j; z = 1; k3 = -1; j += k3; } else j += k3; } for (j = 0; j < 10; j++) cout << a[j] << ; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 501; int a[N][N], b[N][N]; void solve() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> b[i][j]; } } for (int i = 0; i < m; i++) { vector<int> comp1, comp2; int x = 0, y = i; while (x < n && y >= 0) { comp1.push_back(a[x][y]); comp2.push_back(b[x][y]); x++, y--; } sort(comp1.begin(), comp1.end()); sort(comp2.begin(), comp2.end()); if (comp1 != comp2) { cout << NO n ; return; } } for (int i = 1; i < n; i++) { vector<int> comp1, comp2; int x = i, y = m - 1; while (x < n && y >= 0) { comp1.push_back(a[x][y]); comp2.push_back(b[x][y]); x++, y--; } sort(comp1.begin(), comp1.end()); sort(comp2.begin(), comp2.end()); if (comp1 != comp2) { cout << NO n ; return; } } cout << YES n ; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; for (int tt = 1; tt <= t; tt++) { solve(); } }
#include <bits/stdc++.h> using namespace std; int mirror(int num) { int res = 0; for (int i = 0; i < 4; ++i) { res |= (num & (1 << i)) << (7 - i - i); res |= (num & (1 << (7 - i))) >> (7 - i - i); } return res; } int main() { int num; string s; vector<int> res; getline(cin, s); num = (0 + 256 - mirror((int)s[0])) % 256; res.push_back(num); for (int i = 1; i < s.size(); ++i) { num = (-mirror(s[i]) + 256 + mirror(s[i - 1])) % 256; res.push_back(num); } for (int i = 0; i < res.size(); ++i) { cout << res[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; struct edge { int to, nxt, w; } e[N << 1]; int head[N], tot = 0; int n, m; int vis[N], mxd, id; int sum[N], dr[N], cnt = 0, pa[N], used[N]; int dp[N], a[N], b[N], siz[N], anc[N], mp[N]; void adde(int u, int v, int w) { e[++tot] = edge{v, head[u], w}, head[u] = tot; } void dfs(int u, int fa, int dep) { pa[u] = fa; if (vis[u] && dep > mxd) id = u, mxd = dep; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (v == fa) continue; dfs(v, u, dep + e[i].w); } return; } void gtag(int u, int fa, int *a, int dep) { a[u] = dep; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (v == fa) continue; gtag(v, u, a, dep + e[i].w); } return; } void Dfs(int u, int fa, int rt) { siz[u] = vis[u]; anc[u] = rt; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (v == fa) continue; if (used[v]) continue; Dfs(v, u, rt); siz[u] += siz[v]; dp[u] = max(dp[u], dp[v] + e[i].w); } return; } int main() { scanf( %d%d , &n, &m); int x; for (int i = 1; i <= m; i++) scanf( %d , &x), vis[x] = 1; int u, v, w; for (int i = 1; i < n; i++) { scanf( %d%d%d , &u, &v, &w); adde(u, v, w), adde(v, u, w); } id = mxd = -1; dfs(1, 0, 0); int s = id; id = mxd = -1; dfs(s, 0, 0); int t = id; swap(s, t); for (int now = s; now; now = pa[now]) dr[++cnt] = now, used[now] = 1, mp[now] = cnt; dr[cnt + 1] = n + 1; mp[n + 1] = cnt + 1; gtag(s, 0, a, 0); gtag(t, 0, b, 0); for (int i = 1; i <= cnt; i++) Dfs(dr[i], 0, dr[i]); int ans = 0; for (int i = 1; i <= n; i++) if (!used[i] && !vis[i]) ans = max(ans, siz[i]); for (int i = 1; i <= n; i++) { if (!vis[i]) continue; int u = anc[i]; int d = max(a[u], b[u]); int k = mp[u]; if (dp[u] == d || (a[u] == d && b[u] == d)) sum[k]++, sum[k + 1]--; else { if (a[u] == d) sum[0]++, sum[k + 1]--; else sum[k]++; } } for (int i = 1; i <= cnt; i++) sum[i] += sum[i - 1]; for (int i = 1; i <= cnt; i++) if (!vis[dr[i]]) ans = max(ans, sum[i]); int amou = 0; for (int i = 1; i <= n; i++) if (!used[i] && !vis[i] && siz[i] == ans) amou++; for (int i = 1; i <= cnt; i++) if (!vis[dr[i]] && sum[i] == ans) amou++; printf( %d %d n , ans, amou); return 0; }
#include <bits/stdc++.h> using std::abs; using std::bitset; using std::cerr; using std::cin; using std::copy; using std::cout; using std::deque; using std::endl; using std::fill; using std::fixed; using std::greater; using std::iota; using std::istream; using std::lower_bound; using std::make_pair; using std::make_tuple; using std::map; using std::max; using std::max_element; using std::min; using std::min_element; using std::multiset; using std::next_permutation; using std::nth_element; using std::ostream; using std::pair; using std::priority_queue; using std::queue; using std::reverse; using std::rotate; using std::set; using std::sort; using std::string; using std::swap; using std::tie; using std::tuple; using std::unique; using std::unordered_map; using std::unordered_set; using std::upper_bound; using std::vector; long long rnd(long long x, long long y) { static auto gen = std::bind(std::uniform_int_distribution<long long>(), std::mt19937(960172)); return gen() % (y - x + 1) + x; } long long gcd(long long a, long long b) { while (b > 0) { long long t = a % b; a = b; b = t; } return a; } template <typename T> T sqr(T const& a) { return a * a; } int const INF = 100 + (int)1e9; long long const INFL = 100 + (long long)1e18; long double const PI = 3.1415926535897932384626433832795028; int const N = 211; int const A = 26; int next[N][A]; int link[N]; int go[N][A]; int prev[N]; int back[N]; int leaf[N]; int sz; int getNode(int pr, int edge) { std::fill(next[sz], next[sz] + A, -1); link[sz] = -1; std::fill(go[sz], go[sz] + A, -1); prev[sz] = pr; back[sz] = edge; leaf[sz] = 0; return sz++; } int getGo(int, int); int getLink(int v) { if (link[v] == -1) { if (v == 0 || prev[v] == 0) { link[v] = 0; } else { link[v] = getGo(getLink(prev[v]), back[v]); } } return link[v]; } int getGo(int v, int x) { if (go[v][x] == -1) { if (next[v][x] != -1) { go[v][x] = next[v][x]; } else if (v == 0) { go[v][x] = v; } else { go[v][x] = getGo(getLink(v), x); } } return go[v][x]; } void mul(long long a[N][N], long long b[N][N], long long c[N][N]) { static long long t[N][N]; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { long long cur = -INFL; for (int k = 0; k < N; ++k) { cur = max(cur, a[i][k] + b[k][j]); } t[i][j] = cur; } } for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { c[i][j] = t[i][j]; } } } int a[N]; void solve() { int n; long long L; scanf( %d %lld , &n, &L); for (int i = 0; i < n; ++i) { scanf( %d , a + i); } static char buf[N]; gets(buf); getNode(0, 0); for (int i = 0; i < n; ++i) { gets(buf); int v = 0; for (int j = 0; buf[j]; ++j) { int x = buf[j] - a ; if (next[v][x] == -1) { next[v][x] = getNode(v, x); } v = next[v][x]; } leaf[v] += a[i]; } { vector<int> q = {0}; for (int head = 0; head < (int)q.size(); ++head) { int v = q[head]; leaf[v] += leaf[getLink(v)]; for (int x = 0; x < A; ++x) { int to = next[v][x]; if (to >= 0) { q.push_back(to); } } } } static long long mat[N][N]; for (int i = 0; i < N; ++i) { fill(mat[i], mat[i] + N, -INFL); } for (int from = 0; from < sz; ++from) { for (int x = 0; x < A; ++x) { int to = getGo(from, x); if (mat[to][from] == -INFL) { mat[to][from] = 0; } mat[to][from] += leaf[to]; } } static long long dp[N]; fill(dp, dp + N, -INFL); dp[0] = 0; for (long long l = L; l > 0; l /= 2) { if (l % 2 == 1) { static long long dp1[N]; for (int v = 0; v < N; ++v) { long long cur = -INFL; for (int u = 0; u < N; ++u) { cur = max(cur, dp[u] + mat[v][u]); } dp1[v] = cur; } for (int i = 0; i < N; ++i) { dp[i] = dp1[i]; } } mul(mat, mat, mat); } cout << *max_element(dp, dp + N) << endl; } int main() { cout.precision(15); cout << fixed; cerr.precision(6); cerr << fixed; solve(); }
#include <bits/stdc++.h> using namespace std; bool isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } void solve() { int n; cin >> n; long double pi = 2.0 * acos(0.0), ans, x; x = pi / (long double)(2.0 * n); ans = tan(x); cout << fixed << setprecision(9) << 1.0 / ans << n ; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ; int t; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimise( Ofast , unroll-loops) using namespace std; void solve() { int n, m, k; cin >> n >> m >> k; long long a[n][m], b[n * m + 3], c[n][m]; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } for (int i = 0; i < k; i++) { int x, y; cin >> x >> y; x--; y--; b[y]++; c[x][y]++; } for (int i = 0; i < n - 1; i++) { long long ans = 0; for (int j = 0; j < m; j++) { if (a[i][j] == 1) { ans += (b[j] - c[i][j]); } } cout << ans << ; } long long ans = 0; for (int j = 0; j < m; j++) { if (a[n - 1][j] == 1) { ans += (b[j] - c[n - 1][j]); } } cout << ans; return; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long a[14], tmp[14]; for (long long i = 0; i < 14; i++) cin >> a[i]; long long anss = 0; for (long long i = 0; i < 14; i++) { for (long long j = 0; j < 14; j++) tmp[j] = a[j]; long long ans = 0; if (tmp[i]) { long long k = tmp[i]; long long add = k / 14; long long rem = k % 14; tmp[i] = 0; long long p = i + 1; for (long long j = 0; j < 14; j++) { tmp[p % 14] += add; p++; } p = i + 1; while (rem) { tmp[p % 14]++; p++; rem--; } for (long long j = 0; j < 14; j++) if (tmp[j] % 2 == 0) ans += tmp[j]; } anss = max(anss, ans); } cout << anss; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 300002; int n, m, l[MAXN * 4], r[MAXN * 4], d[MAXN * 4], id[MAXN]; string s; void input() { scanf( %d%d , &n, &m); cin.ignore(1); getline(cin, s); s = 0 + s; } void init(int x, int left, int right) { l[x] = left; r[x] = right; if (left == right) { d[x] = 0; id[left] = x; return; } int mid = (left + right) / 2; init(x * 2, left, mid); init(x * 2 + 1, mid + 1, right); d[x] = d[x * 2] + d[x * 2 + 1]; if (s[mid] == . && s[mid + 1] == . ) d[x]++; } void update(int a) { int x = id[a]; x /= 2; while (x > 0) { d[x] = d[x * 2] + d[x * 2 + 1]; if (s[r[x * 2]] == . && s[l[x * 2 + 1]] == . ) d[x]++; x /= 2; } } int main() { input(); init(1, 1, n); int a; char b; for (int q = 1; q <= m; q++) { scanf( %d %c , &a, &b); if ((s[a] == . && b == . ) || (s[a] != . && b != . )) { printf( %d n , d[1]); continue; } s[a] = b; update(a); printf( %d n , d[1]); } }
#include <bits/stdc++.h> using namespace std; long long n, m, len; long long a, b, c, l; void solve() { long long tot = ((l + 3) * (l + 2) * (l + 1)) / 6; long long i; for (i = 0; i <= l; i++) { long long x = min(a - b - c + i, l - i); if (x < 0) continue; tot -= ((x + 2) * (x + 1)) >> 1; } for (i = 0; i <= l; i++) { long long x = min(b - a - c + i, l - i); if (x < 0) continue; tot -= ((x + 2) * (x + 1)) >> 1; } for (i = 0; i <= l; i++) { long long x = min(c - a - b + i, l - i); if (x < 0) continue; tot -= ((x + 2) * (x + 1)) >> 1; } cout << tot << n ; } int main() { long long i, j, k, t; ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> a >> b >> c >> l; solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1000000009; const int MAXN = 100000; int n, m; vector<pair<int, int> > graph[MAXN + 3]; bool vis[MAXN + 3]; bool color[MAXN + 3]; vector<int> ans[2], curr, output; int counter; void Read() { scanf( %d %d , &n, &m); for (int i = 0; i < m; i++) { int a, b, c; char col; scanf( %d %d %c , &a, &b, &col); if (col == B ) c = 1; else c = 0; graph[a].push_back(make_pair(b, c)); graph[b].push_back(make_pair(a, c)); } } void Clean() { for (int i = 1; i <= n; i++) vis[i] = false; for (int i = 1; i <= n; i++) color[i] = 0; for (int i = 1; i <= n; i++) for (int j = 0; j < (int)graph[i].size(); j++) graph[i][j].second = (graph[i][j].second + 1) % 2; curr.clear(); } bool Dfs(int v) { vis[v] = true; curr.push_back(v); if (color[v] == 0) counter++; for (int i = 0; i < (int)graph[v].size(); i++) { int u = graph[v][i].first; int sum = graph[v][i].second; if (!vis[u]) { color[u] = (sum - color[v] + 2) % 2; if (Dfs(u) == false) return false; } else { if (color[u] != (sum - color[v] + 2) % 2) return false; } } return true; } bool Solve(int ind) { for (int v = 1; v <= n; v++) { if (!vis[v]) { if (Dfs(v) == false) return false; } for (int i = 0; i < (int)curr.size(); i++) { if (color[curr[i]] == 0 && 2 * counter < (int)curr.size()) ans[ind].push_back(curr[i]); else if (color[curr[i]] == 1 && 2 * counter >= (int)curr.size()) ans[ind].push_back(curr[i]); } curr.clear(); counter = 0; } return true; } int main() { Read(); bool check0 = Solve(0); Clean(); bool check1 = Solve(1); if (!check0 && !check1) { puts( -1 ); return 0; } if (!check1 || (ans[0].size() < ans[1].size() && check0)) output = ans[0]; else output = ans[1]; printf( %d n , (int)output.size()); for (int i = 0; i < (int)output.size(); i++) { printf( %d , output[i]); } puts( ); return 0; }
#include <bits/stdc++.h> using namespace std; int n; string res; map<string, int> stv; map<int, string> vts; vector<string> a, s({ 1110111 , 0010010 , 1011101 , 1011011 , 0111010 , 1101011 , 1101111 , 1010010 , 1111111 , 1111011 }); vector<vector<int>> dp; int cnt(string f, string t) { int res = 0; for (int i = 0; i < 7; i++) { if (f[i] != t[i]) { ++res; if (f[i] == 1 ) return -1; } } return res; } int bt(int in, int k) { if (k < 0) return 0; if (in >= n) return k == 0; int& dpv = dp[in][k]; if (dpv == 1) return 0; for (int i = 9; i >= 0; i--) { int c = cnt(a[in], vts[i]); if (c >= 0 && bt(in + 1, k - c)) { res.push_back(i + 0 ); return 1; } } dpv = 1; return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int k; cin >> n >> k; a.resize(n); dp.assign(n, vector<int>(k + 1)); for (auto& i : a) cin >> i; for (int i = 0; i < 10; i++) { stv[s[i]] = i; vts[i] = s[i]; } if (bt(0, k)) { reverse(res.begin(), res.end()); cout << res; } else { cout << -1; } return 0; }
#include <bits/stdc++.h> using namespace std; bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = RDLU ; long long ln, lk, lm; void et(bool f = 0) { puts(f ? MAGIC : NOT MAGIC ); exit(0); } int a[2505][2505]; bitset<2505> bs[2505]; void fmain() { scanf( %d , &n); for (int(i) = 1; (i) <= (int)(n); (i)++) for (int(j) = 1; (j) <= (int)(n); (j)++) scanf( %d , &a[i][j]); for (int(i) = 1; (i) <= (int)(n); (i)++) if (a[i][i] != 0) et(); for (int(i) = 1; (i) <= (int)(n); (i)++) for (int j = i + 1; j <= n; j++) if (a[i][j] != a[j][i]) et(); vector<pair<int, pair<int, int>>> vp; for (int(i) = 1; (i) <= (int)(n); (i)++) for (int(j) = 1; (j) <= (int)(n); (j)++) vp.push_back({a[i][j], {i, j}}); int N = vp.size(); sort(vp.begin(), vp.end()); for (int i = 0, j = 0; i < N; i = j) { for (j = i; j < N && vp[i].first == vp[j].first; j++) ; for (int k = i; k < j; k++) { auto p = vp[k].second; if ((bs[p.first] & bs[p.second]).any()) et(); } for (int k = i; k < j; k++) { auto p = vp[k].second; bs[p.first][p.second] = 1; } } et(1); } int main() { fmain(); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100, inf = 0x3f3f3f3f, mod = 1e9 + 7, M = 1e6 + 1000; const long long INF = 0x3f3f3f3f3f3f3f3f; long long sp(long long a, long long b, long long d) { if (!b) return 1; return b % 2 ? sp(a * a, b / 2, d) * a % d : sp(a * a, b / 2, d); } long long sp1(long long a, long long b) { if (!b) return 1; return b % 2 ? sp1(a * a, b / 2) * a : sp1(a * a, b / 2); } long long lowbit(long long x) { return x & (-x); } long long exgcd(long long a, long long b, long long &x, long long &y) { if (!b) { x = 1; y = 0; return a; } else { long long d = exgcd(b, a % b, y, x); y -= (a / b) * x; return d; } } long long t, n; char str[N]; long long ck(char s) { long long l = 1, r = n, x = 0; while (l <= r) { if (str[l] == str[r]) { l++; r--; } else { if (str[l] == s) { l++; x++; } else if (str[r] == s) { r--; x++; } else { return -1; } } } return x; } int main() { cin >> t; while (t--) { scanf( %lld%s , &n, str + 1); long long ans = -1; for (int i = 0; i <= 25; i++) { char s = i + a ; long long x = ck(s); if (x == -1) continue; if (ans == -1 || ans > x) ans = x; } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; long long a[300005]; vector<long long> adjlist[1048585]; int main() { long long n, i; scanf( %lld , &n); for (i = 0; i < n; i++) { scanf( %lld , a + i); } for (i = 1; i < n; i++) { a[i] = a[i - 1] ^ a[i]; } adjlist[0].push_back(0); for (i = 0; i < n; i++) { adjlist[a[i]].push_back(i + 1); } long long ans = 0; for (i = 0; i < 1048580; i++) { if (adjlist[i].size() > 0) { long long od = 0, ev = 0; for (auto e : adjlist[i]) { if (e % 2 == 0) ev++; else od++; } ans += (ev * (ev - 1)) / 2; ans += (od * (od - 1)) / 2; } } printf( %lld n , ans); return 0; }
#include <bits/stdc++.h> int n, m; int d[250000]; int h[250000]; int c[250000]; int r[250000]; int diferencias[160000]; int a[401]; bool sirve(int v, int idCamion) { int litros = v; int cantRecargas = r[idCamion]; for (int i = d[idCamion] + 1; i <= h[idCamion]; i++) { if (v < a[i] - a[i - 1]) { return false; } if (litros < a[i] - a[i - 1]) { if (cantRecargas) { litros = v; cantRecargas--; } else { return false; } } litros -= a[i] - a[i - 1]; } return true; } int cmpfunc(const void* a, const void* b) { return (*(int*)a - *(int*)b); } int main() { scanf( %d %d , &n, &m); for (int i = 0; i < n; i++) scanf( %d , &a[i + 1]); for (int i = 0; i < m; i++) scanf( %d %d %d %d , &d[i], &h[i], &c[i], &r[i]); int tam2 = 0; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) diferencias[tam2++] = a[j] - a[i]; qsort(diferencias, tam2, sizeof(int), cmpfunc); int tam = 1; for (int i = 1; i < tam2; i++) if (diferencias[i] != diferencias[tam - 1]) diferencias[tam++] = diferencias[i]; long long minV = 0; int ini, fin, mid; for (int i = 0; i < m; i++) { ini = 0; fin = tam - 1; while (ini + 1 < fin) { mid = (ini + fin) / 2; if (sirve(diferencias[mid], i)) fin = mid; else ini = mid; } if (sirve(diferencias[ini], i)) minV = (minV > ((long long)diferencias[ini]) * ((long long)c[i]) ? minV : ((long long)diferencias[ini]) * ((long long)c[i])); else minV = (minV > ((long long)diferencias[fin]) * ((long long)c[i]) ? minV : ((long long)diferencias[fin]) * ((long long)c[i])); } printf( %I64d n , minV); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, a[300010][27]; int val[300010], cost[610]; int fail[300010]; string st[610]; char s[300010], p[300010]; int main() { int T, op, len, slen, i, j, k, x; long long ans; for (scanf( %d , &T); T; --T) { scanf( %d %s , &op, s); len = strlen(s); if (op <= 2) { if (len <= 500) { for (i = 0, j = 0; i < len; ++i) { if (!a[j][x = s[i] - a ]) a[j][x] = ++m; j = a[j][x]; } val[j] += op == 1 ? 1 : -1; } else { st[++n] = s; cost[n] = op == 1 ? 1 : -1; } } else { ans = 0; for (i = 0; i < len; ++i) for (k = i, j = 0; k < len; ++k) { if (!a[j][x = s[k] - a ]) break; j = a[j][x]; ans += val[j]; } for (i = 1; i <= n; ++i) { if ((slen = st[i].length()) > len) continue; fail[0] = fail[1] = 0; for (j = 1; j < slen; ++j) { for (k = fail[j]; k && st[i][k] != st[i][j]; k = fail[k]) ; fail[j + 1] = st[i][k] == st[i][j] ? k + 1 : 0; } for (j = 0, k = 0; j < len; ++j) { while (k && st[i][k] != s[j]) k = fail[k]; if (st[i][k] == s[j]) ++k; if (k == slen) { ans += cost[i]; k = fail[k]; } } } cout << ans << endl; } } return 0; }
#include <bits/stdc++.h> int n, i, x, p[1 << 20], q[1 << 20], t[1 << 20], k; int main() { scanf( %d , &n); for (i = 0; i < n && 1 == scanf( %d , &p[i]); ++i) ; for (scanf( %d , &i); i-- > 0 && 1 == scanf( %d , &x); q[x - 1] = 1) ; for (i = n; i-- > 0;) if (q[i] || !k || p[i] != t[k - 1]) t[k++] = p[i], p[i] *= -1; else --k; if (k) puts( NO ); else { puts( YES ); for (i = 0; i < n; ++i) printf( %d , p[i]); puts( ); } }
#include <bits/stdc++.h> using namespace std; const int N = 1234567; vector<int> rev[N]; char foo[N]; int on_cycle[N]; int digit[N], to[N]; bool found; void dfs(int v, int pos) { if (pos < 0) { found = true; } if (found) { return; } int sz = rev[v].size(); for (int j = 0; j < sz; j++) { int u = rev[v][j]; if (on_cycle[u] != -1) { continue; } dfs(u, pos - (digit[u] == foo[pos] - 0 )); } } int dx[42], dy[42]; int last[42]; bool vis[N]; const int NNN = 444; char s[NNN][NNN]; int main() { int n, m, q; scanf( %d %d %d , &n, &m, &q); for (int i = 0; i < n; i++) { scanf( %s , s[i]); } for (int d = 0; d <= 9; d++) { scanf( %d %d , dx + d, dy + d); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int d = s[i][j] - 0 ; int x = i + dx[d]; int y = j + dy[d]; if (x < 0 || y < 0 || x >= n || y >= m) { x = i; y = j; } digit[i * m + j] = d; to[i * m + j] = x * m + y; } } n = n * m; for (int i = 0; i < n; i++) { vis[i] = false; rev[i].clear(); on_cycle[i] = -1; } for (int i = 0; i < n; i++) { rev[to[i]].push_back(i); } for (int i = 0; i < n; i++) { if (vis[i]) { continue; } int p = i; vector<int> cycle; while (!vis[p]) { cycle.push_back(p); vis[p] = true; p = to[p]; } int id = -1; for (int i = 0; i < (int)cycle.size(); i++) { if (cycle[i] == p) { id = i; break; } } if (id == -1) { continue; } int mask = 0; for (int i = id; i < (int)cycle.size(); i++) { mask |= (1 << digit[cycle[i]]); } for (int i = id; i < (int)cycle.size(); i++) { on_cycle[cycle[i]] = mask; } } while (q--) { scanf( %s , foo); int len = strlen(foo); for (int d = 0; d <= 9; d++) { last[d] = -1; } for (int i = 0; i < len; i++) { last[foo[i] - 0 ] = i; } found = false; for (int start = 0; start < n; start++) { if (on_cycle[start] == -1) { continue; } int pos = -1; for (int d = 0; d <= 9; d++) { if (on_cycle[start] & (1 << d)) { continue; } pos = max(pos, last[d]); } dfs(start, pos); } puts(found ? YES : NO ); } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { vector<int> v; int n; cin >> n; for (int i = 0; i < n; i++) { int t; cin >> t; v.push_back(t); } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); v.erase(remove(v.begin(), v.end(), 0), v.end()); cout << v.size() << n ; }
#include <bits/stdc++.h> int a[40][40], b[40][40], ta[40][40]; int ansx[1010000], ansy[1010000]; int za[1010], zb[1010]; int aaa[40][40], bbb[40][40], aaaa[40][40], bbbb[40][40]; int c[40][40]; int main(void) { int n, m, i, j, j1, j2, nm, j3, j4; int ans; int temp; int z; int x, y; int aa, bb; int nn, mm; while (scanf( %d %d , &n, &m) != EOF) { for (i = 1; i <= 900; i++) { za[i] = 0; zb[i] = 0; } for (j1 = 1; j1 <= n; j1++) { for (j2 = 1; j2 <= m; j2++) { scanf( %d , &a[j1][j2]); aaaa[j1][j2] = aaa[j1][j2] = a[j1][j2]; za[a[j1][j2]]++; } } for (j1 = 1; j1 <= n; j1++) { for (j2 = 1; j2 <= m; j2++) { scanf( %d , &b[j1][j2]); bbbb[j1][j2] = bbb[j1][j2] = b[j1][j2]; zb[b[j1][j2]]++; } } nm = 0; if (n == 1 && m == 1) { ans = -1; } else if (n == 1 || m == 1) { if (m == 1) { for (i = 2; i <= n; i++) { a[1][i] = a[i][1]; b[1][i] = b[i][1]; } m = n; nm = 1; } ans = -1; for (j1 = 1; j1 <= m; j1++) { for (j2 = 1; j2 < j1; j2++) { for (i = 1; i <= m; i++) { ta[1][i] = a[1][i]; } for (i = j1; i > j2; i--) { temp = ta[1][i]; ta[1][i] = ta[1][i - 1]; ta[1][i - 1] = temp; } z = 1; for (i = 1; i <= m; i++) { if (b[1][i] != ta[1][i]) { z = 0; break; } } if (z) { ans = 0; ansx[0] = 1; ansy[0] = j1; for (i = j1 - 1; i >= j2; i--) { ++ans; ansx[ans] = 1; ansy[ans] = i; } break; } } if (ans < 0) { for (j2 = j1 + 1; j2 <= m; j2++) { for (i = 1; i <= m; i++) { ta[1][i] = a[1][i]; } for (i = j1; i < j2; i++) { temp = ta[1][i]; ta[1][i] = ta[1][i + 1]; ta[1][i + 1] = temp; } z = 1; for (i = 1; i <= m; i++) { if (b[1][i] != ta[1][i]) { z = 0; break; } } if (z) { ans = 0; ansx[0] = 1; ansy[0] = j1; for (i = j1 + 1; i <= j2; i++) { ++ans; ansx[ans] = 1; ansy[ans] = i; } break; } } } if (ans > 0) { break; } } } else { z = 1; for (i = 1; i <= 900; i++) { if (za[i] != zb[i]) { z = 0; break; } } ans = -1; if (z) { z = 0; bb = b[n][m]; for (j1 = 1; j1 <= n; j1++) { for (j2 = 1; j2 <= m; j2++) { if (bb == a[j1][j2]) { z = 1; break; } } if (z) { break; } } ans = 0; ansx[0] = j1; ansy[0] = j2; for (i = j2 - 1; i; i--) { temp = a[j1][i]; a[j1][i] = a[j1][i + 1]; a[j1][i + 1] = temp; ++ans; ansx[ans] = j1; ansy[ans] = i; } for (i = j1 - 1; i; i--) { temp = a[i][1]; a[i][1] = a[i + 1][1]; a[i + 1][1] = temp; ++ans; ansx[ans] = i; ansy[ans] = 1; } nn = n - 2; for (j1 = 1; j1 <= nn; j1++) { for (j2 = 1; j2 < m; j2++) { z = 0; bb = b[j1][j2]; for (j3 = j1; j3 <= n; j3++) { if (j3 == j1) { j4 = j2 + 1; } else { j4 = 1; } for (; j4 <= m; j4++) { if (bb == a[j3][j4]) { z = 1; break; } } if (z) { break; } } if (!z) while (1) ; if (j3 == j1) { c[j1][j2] = 1; for (i = j2 + 1; i <= j4; i++) { temp = a[j1][i]; a[j1][i] = a[j1][i - 1]; a[j1][i - 1] = temp; ++ans; ansx[ans] = j1; ansy[ans] = i; } } else if (j4 == m) { c[j1][j2] = 2; for (i = j2 + 1; i <= m; i++) { temp = a[j1][i]; a[j1][i] = a[j1][i - 1]; a[j1][i - 1] = temp; ++ans; ansx[ans] = j1; ansy[ans] = i; } for (i = j1 + 1; i <= j3; i++) { temp = a[i][m]; a[i][m] = a[i - 1][m]; a[i - 1][m] = temp; ++ans; ansx[ans] = i; ansy[ans] = m; } } else { c[j1][j2] = 3; for (i = j2 + 1; i <= m; i++) { temp = a[j1][i]; a[j1][i] = a[j1][i - 1]; a[j1][i - 1] = temp; ++ans; ansx[ans] = j1; ansy[ans] = i; } for (i = j1 + 1; i <= j3; i++) { temp = a[i][m]; a[i][m] = a[i - 1][m]; a[i - 1][m] = temp; ++ans; ansx[ans] = i; ansy[ans] = m; } for (i = m - 1; i >= j4; i--) { temp = a[j3][i]; a[j3][i] = a[j3][i + 1]; a[j3][i + 1] = temp; ++ans; ansx[ans] = j3; ansy[ans] = i; } if (j3 == n) { for (i = j4 + 1; i < m; i++) { temp = a[j3][i - 1]; a[j3][i - 1] = a[j3 - 1][i]; a[j3 - 1][i] = temp; temp = a[j3][i + 1]; a[j3][i + 1] = a[j3 - 1][i]; a[j3 - 1][i] = temp; temp = a[j3][i + 1]; a[j3][i + 1] = a[j3][i]; a[j3][i] = temp; ++ans; ansx[ans] = j3 - 1; ansy[ans] = i; ++ans; ansx[ans] = j3; ansy[ans] = i + 1; ++ans; ansx[ans] = j3; ansy[ans] = i; } } else { for (i = j4 + 1; i < m; i++) { temp = a[j3][i - 1]; a[j3][i - 1] = a[j3 + 1][i]; a[j3 + 1][i] = temp; temp = a[j3][i + 1]; a[j3][i + 1] = a[j3 + 1][i]; a[j3 + 1][i] = temp; temp = a[j3][i + 1]; a[j3][i + 1] = a[j3][i]; a[j3][i] = temp; ++ans; ansx[ans] = j3 + 1; ansy[ans] = i; ++ans; ansx[ans] = j3; ansy[ans] = i + 1; ++ans; ansx[ans] = j3; ansy[ans] = i; } } temp = a[j3][m - 1]; a[j3][m - 1] = a[j3 - 1][m]; a[j3 - 1][m] = temp; ++ans; ansx[ans] = j3 - 1; ansy[ans] = m; temp = a[j3][m]; a[j3][m] = a[j3 - 1][m]; a[j3 - 1][m] = temp; ++ans; ansx[ans] = j3; ansy[ans] = m; } if (j3 != j1) { for (i = j3 - 1; i > j1; i--) { temp = a[i + 1][m]; a[i + 1][m] = a[i][m - 1]; a[i][m - 1] = temp; temp = a[i - 1][m]; a[i - 1][m] = a[i][m - 1]; a[i][m - 1] = temp; temp = a[i - 1][m]; a[i - 1][m] = a[i][m]; a[i][m] = temp; ++ans; ansx[ans] = i; ansy[ans] = m - 1; ++ans; ansx[ans] = i - 1; ansy[ans] = m; ++ans; ansx[ans] = i; ansy[ans] = m; } temp = a[j1][m - 1]; a[j1][m - 1] = a[j1 + 1][m]; a[j1 + 1][m] = temp; ++ans; ansx[ans] = j1; ansy[ans] = m - 1; temp = a[j1][m - 1]; a[j1][m - 1] = a[j1][m]; a[j1][m] = temp; ++ans; ansx[ans] = j1; ansy[ans] = m; j4 = m; } for (i = j4 - 1; i > j2; i--) { temp = a[j1][i + 1]; a[j1][i + 1] = a[j1 + 1][i]; a[j1 + 1][i] = temp; temp = a[j1][i - 1]; a[j1][i - 1] = a[j1 + 1][i]; a[j1 + 1][i] = temp; temp = a[j1][i - 1]; a[j1][i - 1] = a[j1][i]; a[j1][i] = temp; ++ans; ansx[ans] = j1 + 1; ansy[ans] = i; ++ans; ansx[ans] = j1; ansy[ans] = i - 1; ++ans; ansx[ans] = j1; ansy[ans] = i; } if (a[j1][j2] != b[j1][j2]) { printf( %d %d , j1, j2); for (j3 = 1; j3 <= j1; j3++) { for (j4 = 1; j4 <= m; j4++) { printf( %d , c[j3][j4]); } printf( n ); } return 0; } } z = 0; bb = b[j1][j2]; for (j3 = j1 + 1; j3 <= n; j3++) { for (j4 = 1; j4 <= m; j4++) { if (bb == a[j3][j4]) { z = 1; break; } } if (z) { break; } } if (j4 == m) { for (i = j1 + 1; i <= j3; i++) { temp = a[i][m]; a[i][m] = a[i - 1][m]; a[i - 1][m] = temp; ++ans; ansx[ans] = i; ansy[ans] = m; } } else { for (i = j1 + 1; i <= j3; i++) { temp = a[i][m]; a[i][m] = a[i - 1][m]; a[i - 1][m] = temp; ++ans; ansx[ans] = i; ansy[ans] = m; } for (i = m - 1; i >= j4; i--) { temp = a[j3][i]; a[j3][i] = a[j3][i + 1]; a[j3][i + 1] = temp; ++ans; ansx[ans] = j3; ansy[ans] = i; } if (j3 == n) { for (i = j4 + 1; i < m; i++) { temp = a[j3][i - 1]; a[j3][i - 1] = a[j3 - 1][i]; a[j3 - 1][i] = temp; temp = a[j3][i + 1]; a[j3][i + 1] = a[j3 - 1][i]; a[j3 - 1][i] = temp; temp = a[j3][i + 1]; a[j3][i + 1] = a[j3][i]; a[j3][i] = temp; ++ans; ansx[ans] = j3 - 1; ansy[ans] = i; ++ans; ansx[ans] = j3; ansy[ans] = i + 1; ++ans; ansx[ans] = j3; ansy[ans] = i; } } else { for (i = j4 + 1; i < m; i++) { temp = a[j3][i - 1]; a[j3][i - 1] = a[j3 + 1][i]; a[j3 + 1][i] = temp; temp = a[j3][i + 1]; a[j3][i + 1] = a[j3 + 1][i]; a[j3 + 1][i] = temp; temp = a[j3][i + 1]; a[j3][i + 1] = a[j3][i]; a[j3][i] = temp; ++ans; ansx[ans] = j3 + 1; ansy[ans] = i; ++ans; ansx[ans] = j3; ansy[ans] = i + 1; ++ans; ansx[ans] = j3; ansy[ans] = i; } } temp = a[j3][m - 1]; a[j3][m - 1] = a[j3 - 1][m]; a[j3 - 1][m] = temp; ++ans; ansx[ans] = j3 - 1; ansy[ans] = m; temp = a[j3][m]; a[j3][m] = a[j3 - 1][m]; a[j3 - 1][m] = temp; ++ans; ansx[ans] = j3; ansy[ans] = m; } for (i = j3 - 1; i > j1; i--) { temp = a[i + 1][m]; a[i + 1][m] = a[i][m - 1]; a[i][m - 1] = temp; temp = a[i - 1][m]; a[i - 1][m] = a[i][m - 1]; a[i][m - 1] = temp; temp = a[i - 1][m]; a[i - 1][m] = a[i][m]; a[i][m] = temp; ++ans; ansx[ans] = i; ansy[ans] = m - 1; ++ans; ansx[ans] = i - 1; ansy[ans] = m; ++ans; ansx[ans] = i; ansy[ans] = m; } for (i = m - 1; i; i--) { temp = a[j1 + 1][i]; a[j1 + 1][i] = a[j1 + 1][i + 1]; a[j1 + 1][i + 1] = temp; ++ans; ansx[ans] = j1 + 1; ansy[ans] = i; } if (a[j1][j2] != b[j1][j2]) { printf( %d %d , j1, j2); return 0; } } nn = n - 1; j3 = ansx[0]; j4 = ansy[0]; for (i = 1; i <= ans; i++) { x = ansx[i]; y = ansy[i]; temp = aaaa[x][y]; aaaa[x][y] = aaaa[j3][j4]; aaaa[j3][j4] = temp; j3 = x; j4 = y; } z = 0; for (j1 = 1; j1 < nn; j1++) { for (j2 = 1; j2 <= m; j2++) { if (aaaa[j1][j2] != bbbb[j1][j2]) { z = 1; break; } } if (z) { break; } } if (z) { while (1) ; } mm = m - 2; for (j2 = 1; j2 <= mm; j2++) { bb = b[nn][j2]; if (bb == a[n][j2]) { temp = a[n][j2]; a[n][j2] = a[nn][j2]; a[nn][j2] = temp; ans++; ansx[ans] = n; ansy[ans] = j2; } else { for (j4 = j2 + 1; j4 <= m; j4++) { if (bb == a[nn][j4]) { j3 = nn; break; } if (bb == a[n][j4]) { j3 = n; break; } } if (j3 == nn) { for (i = j2 + 1; i <= j4; i++) { temp = a[nn][i]; a[nn][i] = a[nn][i - 1]; a[nn][i - 1] = temp; ans++; ansx[ans] = nn; ansy[ans] = i; } } else { for (i = j2 + 1; i < j4; i++) { temp = a[nn][i]; a[nn][i] = a[nn][i - 1]; a[nn][i - 1] = temp; ans++; ansx[ans] = nn; ansy[ans] = i; } temp = a[n][j4]; a[n][j4] = a[nn][j4 - 1]; a[nn][j4 - 1] = temp; ans++; ansx[ans] = n; ansy[ans] = j4; temp = a[n][j4]; a[n][j4] = a[nn][j4]; a[nn][j4] = temp; ans++; ansx[ans] = nn; ansy[ans] = j4; } for (j4--; j4 > j2; j4--) { temp = a[n][j4]; a[n][j4] = a[nn][j4 + 1]; a[nn][j4 + 1] = temp; ans++; ansx[ans] = n; ansy[ans] = j4; temp = a[n][j4]; a[n][j4] = a[nn][j4 - 1]; a[nn][j4 - 1] = temp; ans++; ansx[ans] = nn; ansy[ans] = j4 - 1; temp = a[nn][j4]; a[nn][j4] = a[nn][j4 - 1]; a[nn][j4 - 1] = temp; ans++; ansx[ans] = nn; ansy[ans] = j4; } temp = a[n][j2]; a[n][j2] = a[nn][j2 + 1]; a[nn][j2 + 1] = temp; ans++; ansx[ans] = n; ansy[ans] = j2; } bb = b[n][j2]; for (j4 = j2 + 1; j4 <= m; j4++) { if (bb == a[n][j4]) { j3 = n; break; } if (bb == a[nn][j4]) { j3 = nn; break; } } if (j3 == n) { for (i = j2 + 1; i <= j4; i++) { temp = a[n][i]; a[n][i] = a[n][i - 1]; a[n][i - 1] = temp; ans++; ansx[ans] = n; ansy[ans] = i; } } else { for (i = j2 + 1; i < j4; i++) { temp = a[n][i]; a[n][i] = a[n][i - 1]; a[n][i - 1] = temp; ans++; ansx[ans] = n; ansy[ans] = i; } temp = a[nn][j4]; a[nn][j4] = a[n][j4 - 1]; a[n][j4 - 1] = temp; ans++; ansx[ans] = nn; ansy[ans] = j4; temp = a[n][j4]; a[n][j4] = a[nn][j4]; a[nn][j4] = temp; ans++; ansx[ans] = n; ansy[ans] = j4; } for (j4--; j4 > j2; j4--) { temp = a[nn][j4]; a[nn][j4] = a[n][j4 + 1]; a[n][j4 + 1] = temp; ans++; ansx[ans] = nn; ansy[ans] = j4; temp = a[nn][j4]; a[nn][j4] = a[n][j4 - 1]; a[n][j4 - 1] = temp; ans++; ansx[ans] = n; ansy[ans] = j4 - 1; temp = a[n][j4]; a[n][j4] = a[n][j4 - 1]; a[n][j4 - 1] = temp; ans++; ansx[ans] = n; ansy[ans] = j4; } temp = a[nn][j2 + 1]; a[nn][j2 + 1] = a[n][j2 + 1]; a[n][j2 + 1] = temp; ans++; ansx[ans] = nn; ansy[ans] = j2 + 1; } mm = m - 1; if (b[nn][mm] == a[nn][m]) { j1 = nn; j2 = m; } else if (b[nn][mm] == a[n][mm]) { j1 = n; j2 = mm; } else { j1 = n; j2 = m; } temp = a[nn][mm]; a[nn][mm] = a[j1][j2]; a[j1][j2] = temp; ans++; ansx[ans] = j1; ansy[ans] = j2; z = 0; if (a[n][m] != b[n][m]) { z = 1; } else if (a[nn][m] != b[nn][m]) { z = 1; } else if (a[n][mm] != b[n][mm]) { z = 1; } while (z) { if (j1 == n && j2 == m) { temp = a[n][m]; a[n][m] = a[n][mm]; a[n][mm] = temp; ans++; ansx[ans] = n; ansy[ans] = mm; j1 = n; j2 = mm; } else if (j1 == n) { temp = a[n][mm]; a[n][mm] = a[nn][m]; a[nn][m] = temp; ans++; ansx[ans] = nn; ansy[ans] = m; j1 = nn; j2 = m; } else { temp = a[nn][m]; a[nn][m] = a[n][m]; a[n][m] = temp; ans++; ansx[ans] = n; ansy[ans] = m; j1 = n; j2 = m; } z = 0; if (a[n][m] != b[n][m]) { z = 1; } else if (a[nn][m] != b[nn][m]) { z = 1; } else if (a[n][mm] != b[n][mm]) { z = 1; } } } } if (ans != -1) { if (nm) { j4 = ansx[0]; j3 = ansy[0]; } else { j3 = ansx[0]; j4 = ansy[0]; } for (i = 1; i <= ans; i++) { if (nm) { y = ansx[i]; x = ansy[i]; } else { x = ansx[i]; y = ansy[i]; } temp = aaa[x][y]; aaa[x][y] = aaa[j3][j4]; aaa[j3][j4] = temp; j3 = x; j4 = y; } z = 0; for (j1 = 1; j1 <= n; j1++) { for (j2 = 1; j2 <= m; j2++) { if (aaa[j1][j2] != bbb[j1][j2]) { z = 1; break; } } if (z) { break; } } if (z) { printf( %d n , 5 / 0); } } printf( %d n , ans); if (ans > 1000000) while (1) ; if (nm) { for (i = 0; i <= ans; i++) { printf( %d %d n , ansy[i], ansx[i]); } } else { for (i = 0; i <= ans; i++) { printf( %d %d n , ansx[i], ansy[i]); } } printf( n ); } return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int d, f, t; long long c; } a[100005]; int cmp(node a, node b) { return a.d < b.d; } long long INF = 0x3f3f3f3f3f3f; long long cost[100005]; int vis[100005]; int cnt1[1000006]; int cnt2[1000006]; long long sum2[1000006]; long long sum1[1000006]; int main() { int n, m, k; memset(cnt1, 0, sizeof(cnt1)); memset(cnt2, 0, sizeof(cnt2)); memset(vis, 0, sizeof(vis)); memset(cost, INF, sizeof(cost)); memset(sum1, 0, sizeof(sum1)); memset(sum2, 0, sizeof(sum2)); scanf( %d%d%d , &n, &m, &k); for (int i = 1; i <= m; i++) { scanf( %d%d%d%I64d , &a[i].d, &a[i].f, &a[i].t, &a[i].c); } sort(a + 1, a + 1 + m, cmp); int pre = 0; for (int i = 1; i <= m; i++) { if (a[i].f == 0) continue; int f = a[i].f; int d = a[i].d; sum1[d] = sum1[pre]; cnt1[d] = cnt1[pre]; if (cost[f] > a[i].c) { if (vis[f] == 1) sum1[d] = sum1[d] - cost[f] + a[i].c; else sum1[d] += a[i].c; cost[f] = a[i].c; if (vis[f] == 0) { cnt1[d]++; vis[f] = 1; } } pre = d; } for (int i = 1; i <= 1000000; i++) { if (cnt1[i] == 0) cnt1[i] = cnt1[i - 1]; if (sum1[i] == 0) sum1[i] = sum1[i - 1]; } memset(vis, 0, sizeof(vis)); pre = 1000000; memset(cost, INF, sizeof(cost)); for (int i = m; i >= 1; i--) { if (a[i].t == 0) continue; int d = a[i].d; int t = a[i].t; sum2[d] = sum2[pre]; cnt2[d] = cnt2[pre]; if (cost[t] > a[i].c) { if (vis[t] == 1) sum2[d] = sum2[d] - cost[t] + a[i].c; else sum2[d] += a[i].c; cost[t] = a[i].c; if (vis[t] == 0) { cnt2[d]++; vis[t] = 1; } } pre = d; } for (int i = 1000000; i >= 0; i--) { if (cnt2[i] == 0) cnt2[i] = cnt2[i + 1]; if (sum2[i] == 0) sum2[i] = sum2[i + 1]; } long long ans = INF; for (int i = 1; i <= 1000000; i++) { if (i + k + 1 > 1000000) break; if (cnt1[i] == n && cnt2[i + k + 1] == n) ans = min(ans, (long long)sum1[i] + sum2[i + k + 1]); } if (ans == INF) { printf( -1 n ); return 0; } printf( %I64d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e6; long long n, b, s[MAXN], m[MAXN], t = 0; long long i, j, d = 0; bool q[MAXN]; int main() { cin >> n >> b; fill(q, q + n, false); for (i = 0; i < n; i++) { cin >> s[i] >> m[i]; } j = 0; for (i = 0; i < n; i++) { if (i > 0) cout << endl; if (j == i && q[j] == false) { q[j] = true; t = s[i]; d++; j++; } if (q[i] == true) { t += m[i]; d--; cout << t; } else cout << -1; while (q[j] == false && t > s[j] && j < n) { if (d + 1 > b) d--; else q[j] = true; j++; d++; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2111; const int mod = 1e9 + 7; int n; int pri[N], tot; long long a[N], b[N]; struct node { long long k, b; node(long long _k = 0, long long _b = 0) { k = _k; b = _b; } } f[N], g[N]; long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); } long long ksm(long long base, long long x) { long long ans = 1; while (x) { if (x & 1) ans = ans * base % mod; base = base * base % mod; x >>= 1; } return ans; } void get(int x) { for (int i = 2; i * i <= x; i++) { if (x % i == 0) { pri[++tot] = i; while (x % i == 0) x /= i; } } if (x > 1) pri[++tot] = x; } void get_g(int id) { for (int i = 1; i <= tot; i++) { g[i].b = g[i].k = 0; while (a[id] % pri[i] == 0) { g[i].b++; a[id] /= pri[i]; } while (b[id] % pri[i] == 0) { g[i].k++; b[id] /= pri[i]; } } } long long ex_gcd(long long A, long long B, long long &x, long long &y) { if (!B) { x = 1; y = 0; return A; } long long xx, yy; long long d = ex_gcd(B, A % B, xx, yy); x = yy; y = xx - A / B * yy; return d; } bool merge() { bool flag = 0; for (int t = 0; t < 3; t++) { for (int i = 1; i <= tot; i++) { long long fk = f[i].k, fb = f[i].b; long long gk = g[i].k, gb = g[i].b; bool fl = 0; if (!fk && !gk) { if (fb != gb) return 0; } else { if (!gk || !fk) { if (!fk) swap(fk, gk), swap(fb, gb), fl = 1; if (fb > gb || (gb - fb) % fk != 0) return 0; long long k = (gb - fb) / fk; if (fl) for (int j = 1; j <= tot; j++) g[j].b += g[j].k * k, g[j].k = 0; else for (int j = 1; j <= tot; j++) f[j].b += f[j].k * k, f[j].k = 0; flag = 1; } } } } if (flag) return 1; int fl = 0; long long A = 0, B = 0, C = 0; for (int i = 1; i <= tot; i++) { if (!f[i].k || !g[i].k) continue; if (!fl) A = f[i].k, B = -g[i].k, C = g[i].b - f[i].b, fl = 1; else if (fl == 1) { long long nA = f[i].k, nB = -g[i].k, nC = g[i].b - f[i].b; long long d = gcd(nA, A); A *= nA / d; B *= nA / d; C *= nA / d; nA *= A / d; nB *= A / d; nC *= A / d; if (B == nB) { if (C != nC) return 0; long long d = gcd(A, gcd(abs(B), abs(C))); A /= d; B /= d; C /= d; continue; } else { if ((C - nC) % (B - nB)) return 0; long long y = (C - nC) / (B - nB); if (y < 0) return 0; if ((C - B * y) % A) return 0; long long x = (C - B * y) / A; if (x < 0) return 0; for (int j = 1; j <= tot; j++) f[j].b += f[j].k * x, f[j].k = 0; for (int j = i + 1; j <= tot; j++) { nA = f[j].k, nB = -g[j].k, nC = g[j].b - f[j].b; if (nA * x + nB * y != nC) return 0; } return 1; } } } long long x = -1, y = -1; long long d = ex_gcd(A, -B, x, y); if (C % d) return 0; y *= -1; long long kx = (-B) / d; long long ky = A / d; x *= C / d, y *= C / d; long long tx = x < 0 ? -(-x + kx - 1) / kx : x / kx; long long ty = y < 0 ? -(-y + ky - 1) / ky : y / ky; x -= min(tx, ty) * kx; y -= min(tx, ty) * ky; for (int i = 1; i <= tot; i++) { f[i].b += f[i].k * x; f[i].k = f[i].k * kx; } return 1; } int main() { cin >> n; for (int i = 1; i <= n; i++) { scanf( %d%d , &a[i], &b[i]); get(a[i]); get(b[i]); } sort(pri + 1, pri + 1 + tot); tot = unique(pri + 1, pri + 1 + tot) - (pri + 1); for (int i = 1; i <= tot; i++) { while (a[1] % pri[i] == 0) { f[i].b++; a[1] /= pri[i]; } while (b[1] % pri[i] == 0) { f[i].k++; b[1] /= pri[i]; } } for (int i = 2; i <= n; i++) { get_g(i); if (!merge()) { puts( -1 ); return 0; } } int ans = 1; for (int i = 1; i <= tot; i++) { ans = 1LL * ans * ksm(pri[i], f[i].b) % mod; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> int main() { long long u, v, a, b; scanf( %lld%lld , &u, &v); if (v == 0) printf( 0 n ); else if (u > v) printf( -1 n ); else if (u == v) { printf( 1 n ); printf( %lld n , u); } else if ((v - u) % 2 == 0) { a = u, b = (v - u) / 2; if ((a ^ b) == (a + b)) { printf( 2 n ); printf( %lld %lld n , a + b, b); } else { printf( 3 n ); printf( %lld %lld %lld n , a, b, b); } } else printf( -1 n ); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, k = 0, c = -1, x, z; cin >> n; while (true) { c += 1; x = pow(10, c); z = 9 * x; if (n - z > 0) { n -= z; k += (z * (c + 1)); } else { n *= (c + 1); k += n; break; } } cout << k; return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[27], sum, gcd; int odd; int _gcd(int x, int y) { if (x == 0 or x == y) return y; if (x > y) swap(x, y); while (y % x) { y %= x; swap(x, y); } return x; } int main() { scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d , a + i); if (a[i] & 1) odd++; sum += a[i]; gcd = _gcd(gcd, a[i]); } if (odd > 1) { printf( %d n , 0); int k = 0; string s(sum, 0 ); for (int i = 0; i < n; i++) { int j = 0; while (j < a[i]) { s[j + k] = a + i; j++; } k += a[i]; } cout << s << endl; return 0; } for (int i = 0; i < n; i++) a[i] /= gcd; sum /= gcd; printf( %d n , gcd); if (odd) { string s(sum, 0 ); int k = 0; for (int i = 0; i < n; i++) { int j = 0; while (j < a[i] / 2) { s[j + k] = a + i; s[sum - 1 - j - k] = a + i; j++; } if (a[i] & 1) odd = i; k += a[i] / 2; } s[k] = odd + a ; for (int i = 0; i < gcd; i++) cout << s; cout << endl; } else { string s(sum, 0 ); string t(sum, 0 ); int k = 0; for (int i = 0; i < n; i++) { int j = 0; while (j < a[i]) { s[j + k] = a + i; t[sum - 1 - j - k] = a + i; t[sum - 1 - j - k] = a + i; j++; } k += a[i]; } for (int i = 0; i < gcd / 2; i++) cout << s << t; cout << endl; } }
#include<bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 void solve() { int k,n,m; cin >> k >> n >> m; vector<int> a(n); for(auto &i:a) { cin >> i; } vector<int> b(m); for(auto &i:b) { cin >> i; } vector<int> res(n+m); int i=0,j=0,ind=0; while(i!=n && j!=m) { if(a[i]==0) { res[ind]=0; ind++; i++; k++; } else if(b[j]==0) { res[ind]=0; ind++; j++; k++; } else if(a[i]<=b[j] && a[i]<=k) { res[ind]=a[i]; ind++; i++; } else if(a[i]>=b[j] && b[j]<=k) { res[ind]=b[j]; ind++; j++; } else { cout << -1 << endl; return; } } while(i!=n) { if(a[i]==0) { res[ind]=0; ind++; i++; k++; } else{ if(a[i]>k) { cout << -1 << endl; return; } res[ind]=a[i]; ind++; i++; } } while(j!=m) { if(b[j]==0) { res[ind]=0; ind++; j++; k++; } else{ if(b[j]>k) { cout << -1 << endl; return; } res[ind]=b[j]; ind++; j++;} } for(auto x:res) cout << x << ; cout << endl; } int main() { int t; cin >> t; while(t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); ; int n; cin >> n; int odd = 0, even = 0; for (int i = 0; i < n; i++) { int val; cin >> val; if (val % 2) odd++; else even++; } if (odd > even) { cout << even + (odd - even) / 3 << n ; } else cout << odd << n ; return 0; }
#include <bits/stdc++.h> using namespace std; struct TrieNode { TrieNode *arr[2]; TrieNode() { arr[0] = arr[1] = nullptr; } }; void insert(TrieNode *root, int num) { TrieNode *curr = root; for (int i = 20; i >= 0; i--) { if (curr->arr[(num >> i) & 1] == nullptr) curr->arr[(num >> i) & 1] = new TrieNode(); curr = curr->arr[(num >> i) & 1]; } return; } int query(TrieNode *root, int xr) { int ans = 0; TrieNode *curr = root; for (int i = 20; i >= 0; i--) { if (curr->arr[(xr >> i) & 1] == nullptr) { ans |= (1 << i); curr = curr->arr[((xr >> i) & 1) ^ 1]; } else { curr = curr->arr[(xr >> i) & 1]; } } return ans; } const int N = 1 << 20 + 1; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; bool present[N]; memset(present, false, sizeof(present)); vector<int> a(n); for (int &i : a) cin >> i, present[i] = true; TrieNode *root = new TrieNode(); for (int i = 0; i < N; i++) if (!present[i]) insert(root, i); int curr_xor = 0; while (m--) { int x; cin >> x; curr_xor ^= x; cout << query(root, curr_xor) << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long bp(long long x, long long y) { long long ans = 1; x %= mod; while (y) { if (y & 1) ans = ans * x % mod; x = x * x % mod; y >>= 1; } return ans; } long long mod_inverse(long long x) { return bp(x, mod - 2); } long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } long long max(long long a, long long b) { if (a < b) swap(a, b); return a; } long long min(long long a, long long b) { if (a > b) swap(a, b); return a; } long long countSetBits(long long n) { long long count = 0; while (n) { count += n & 1; n >>= 1; } return count; } void solve() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n], b[n]; for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 0; i < n; i++) cin >> b[i]; if (n % 2 == 1 && a[n / 2] != b[n / 2]) { cout << No << endl; continue; } map<pair<long long, long long>, long long> m; for (long long i = 0; i < n / 2; i++) { m[make_pair(min(a[i], a[n - 1 - i]), max(a[i], a[n - 1 - i]))]++; } long long flag = 0; for (long long i = 0; i < n / 2; i++) { pair<long long, long long> p = {min(b[i], b[n - 1 - i]), max(b[i], b[n - 1 - i])}; if (m[p] <= 0) { flag = 1; break; } m[p]--; } if (flag == 1) cout << No << endl; else cout << yes << endl; } } signed main() { solve(); }
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int modInverse(long long int n, long long int p) { return power(n, p - 2, p) % p; } void solve() { long long int n; cin >> n; long long int a[n]; for (long long int i = 0; i < n; i++) cin >> a[i]; string s = , s1 = , s2 = ; for (long long int i = 0; i < 200; i++) { s += a ; s1 += b ; s2 += a ; } cout << s << n ; for (long long int i = 0; i < n; i++) { if (s[a[i]] == a ) { s = s.substr(0, a[i]) + s1.substr(0, 200 - a[i]); } else { s = s.substr(0, a[i]) + s2.substr(0, 200 - a[i]); } cout << s << n ; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; string s[8] = { vaporeon , jolteon , flareon , espeon , umbreon , leafeon , glaceon , sylveon }; int f[8] = {0}; int main() { string x; int n, k; scanf( %d , &n); cin >> x; for (int i = 0; i < 8; i++) { if (s[i].size() != n) { f[i] = -1; } else { f[i] = 1; for (int j = 0; j < n; j++) { if (s[i][j] == x[j]) { f[i]++; } else if (s[i][j] != x[j] && x[j] != . ) { f[i] = -1; break; } } } } for (int i = 0; i < 8; i++) { if (f[i] > -1) { k = i; } } cout << s[k]; return 0; }
#include <bits/stdc++.h> using namespace std; inline int next_samebit(int mask) { int lowbit = mask & -mask; int slack = lowbit + mask; return slack | (mask ^ slack) / lowbit / 4; } int n, a[16], ans[4][4], at, row[4], col[4], zig[3], tag[2]; const int mt[] = {1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1}; const int mr[] = {0, 0, 0, 0, 1, 2, 3, 2, 1, 1, 1, 3, 2, 2, 3, 3}; const int mc[] = {0, 1, 2, 3, 0, 0, 0, 1, 2, 1, 3, 1, 2, 3, 2, 3}; const int mv[] = {1, 0, 0, 2, 0, 0, 2, 2, 2, 1, 0, 0, 1, 0, 0, 1}; const int fr[] = {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1}; const int fc[] = {0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1}; const int fv[] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1}; bool dfs(int i) { if (i == 16) return true; int r = mr[i], c = mc[i], v = mv[i]; for (int s = tag[mt[i]]; s; s &= s - 1) { int x = __builtin_ctz(s); ans[r][c] = a[x]; tag[mt[i]] ^= 1 << x, row[r] += a[x], col[c] += a[x], zig[v] += a[x]; if ((!fr[i] || row[r] == at) && (!fc[i] || col[c] == at) && (!fv[i] || zig[v] == at) && dfs(i + 1)) return true; tag[mt[i]] ^= 1 << x, row[r] -= a[x], col[c] -= a[x], zig[v] -= a[x]; } return false; } int main() { scanf( %d , &n); int m = n * n, all = 0, sum = 0; for (int i = 0; i < m; all += a[i++]) scanf( %d , a + i); if (n == 1) { sum = a[0]; } else if (n == 2) { sort(a, a + m); do { sum = a[0] + a[1]; if (sum == a[3] + a[4] && sum == a[0] + a[3] && sum == a[1] + a[4] && sum == a[0] + a[4] && sum == a[1] + a[3]) break; } while (next_permutation(a, a + m)); } else if (n == 3) { sort(a, a + m); do { sum = a[0] + a[1] + a[2]; if (sum == a[3] + a[4] + a[5] && sum == a[6] + a[7] + a[8] && sum == a[0] + a[3] + a[6] && sum == a[1] + a[4] + a[7] && sum == a[2] + a[5] + a[8] && sum == a[0] + a[4] + a[8] && sum == a[2] + a[4] + a[6]) break; } while (next_permutation(a, a + m)); } else if (n == 4) { int mask = (1 << m) - 1; for (int s = 255; s <= mask; s = next_samebit(s)) { for (int i = sum = 0; i < m; i++) if (s & 1 << i) sum += a[i]; if ((all * 2 + sum) % 10) continue; at = (all * 2 + sum) / 10; tag[0] = mask ^ s; tag[1] = s; if (dfs(0)) break; } for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) a[i * n + j] = ans[i][j]; sum = a[0] + a[1] + a[2] + a[3]; } printf( %d n , sum); for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) printf( %d , a[i * n + j]); printf( %d n , a[i * n + n - 1]); } }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-9; const int inf = 2000000000; const long long infLL = 9000000000000000000; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n; cin >> n; string second; cin >> second; int c = 0, d = 0; for (auto u : second) { if (u == n ) c++; else if (u == z ) d++; } while (c--) cout << 1 << ; while (d--) cout << 0 << ; cout << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int t = 1; while (t--) { long long int n, k; cin >> n >> k; long long int ans = -1; for (long long int i = 1;; i++) { long long int p = n + ((-1) * (i)*k); if (p < i) break; long long int c = 0; while (p > 0) { if (p % 2 == 1) c++; p /= 2; } if (c <= i) { ans = i; break; } } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int s1, s11, s12; int s2 = 0; s1 = s11 = s12 = 0; string s, c; s = c = ; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; if (i == 0) { s1 = a; s2 = 0; s = A ; } else { if (abs(s1 + a - s2) < abs(s2 + b - s1)) { s1 += a; s += A ; } else { s2 += b; s += G ; } } if (i == 0) { s11 = 0; s12 = b; c = G ; } else { if (abs(s11 + a - s12) < abs(s12 + b - s11)) { s11 += a; c += A ; } else { s12 += b; c += G ; } } } if (abs(s1 - s2) <= 500) { cout << s; return 0; } if (abs(s11 - s12) <= 500) { cout << c; return 0; } cout << -1; return 0; }
#include <bits/stdc++.h> #pragma gcc optimize(O3) using namespace std; long long INF = static_cast<long long>(2e18); long long inf = static_cast<long long>(1e9 + 7); long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } vector<vector<set<long long> > > dp; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k; cin >> n >> k; vector<long long> vals(n); dp.resize(k, vector<set<long long> >(n)); for (auto& it : vals) cin >> it; for (long long i = n - 2; i >= 0; --i) vals[i] += vals[i + 1]; for (long long i = n - 1; i >= 0; --i) dp[0][i].insert(vals[i]); if (k == 1) { cout << vals[0]; return 0; } long long res = 0; for (long long i = 1; i < k; i++) { for (long long j = n - 1 - i; j >= k - i - 1; --j) { for (long long q = j + 1; q < n - i + 1; ++q) { for (auto& it : dp[i - 1][q]) { if (i == (k - 1) && (!j)) { res = max(res, ((vals[j] - vals[q]) & it)); } else dp[i][j].insert((vals[j] - vals[q]) & it); } } } } cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; double dp[2010][2010][2][2]; int a[2010]; pair<int, int> le[2010], ri[2010]; int ll[2010][2], rr[2010][2]; int h; double p, q; double go(int l, int r, int Li, int Ri) { if (l > r) return 0; if (dp[l][r][Li][Ri] > -1) { return dp[l][r][Li][Ri]; } int L = ll[l][Li], R = rr[r][Ri]; double res = 0; int d; if (L < le[l].second) { d = h; } else { d = a[l] - L; } res += 0.5 * p * (d + go(l + 1, r, 0, Ri)); if (R > ri[r].second) { d = h; } else { d = R - a[r]; } res += 0.5 * q * (d + go(l, r - 1, Li, 1)); if (ri[l].second > R) { d = R - a[l]; } else { d = ri[l].second - a[l]; } res += 0.5 * q * (d + go(ri[l].first + 1, r, 1, Ri)); if (le[r].second < L) { d = a[r] - L; } else { d = a[r] - le[r].second; } res += 0.5 * p * (d + go(l, le[r].first - 1, Li, 0)); return dp[l][r][Li][Ri] = res; } int main() { int n; scanf( %d%d%lf , &n, &h, &p); q = 1 - p; for (int i = 0; i < n; i++) { scanf( %d , &a[i]); } sort(a, a + n); for (int i = 0; i < n; i++) { le[i] = make_pair(0, a[0] - h); ri[i] = make_pair(i, a[i] + h); } for (int i = n - 2; i >= 0; i--) { if (a[i] + h > a[i + 1]) { ri[i] = ri[i + 1]; } else { ri[i] = make_pair(i, a[i] + h); } } for (int i = 1; i < n; i++) { if (a[i] - h < a[i - 1]) { le[i] = le[i - 1]; } else { le[i] = make_pair(i, a[i] - h); } } ll[0][0] = ll[0][1] = -1001001001; for (int i = 1; i < n; i++) { ll[i][0] = a[i - 1]; ll[i][1] = ri[i - 1].second; } rr[n - 1][0] = rr[n - 1][1] = 1001001001; for (int i = n - 2; i >= 0; i--) { rr[i][1] = a[i + 1]; rr[i][0] = le[i + 1].second; } for (int i = 0; i < 2010; i++) { for (int j = 0; j < 2010; j++) { for (int k = 0; k < 2; k++) { for (int l = 0; l < 2; l++) { dp[i][j][k][l] = -1; } } } } printf( %.12f n , go(0, n - 1, 0, 1)); return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> vis; vector<pair<long long, long long>> v; void dfs(long long x, long long y) { pair<long long, long long> p = v[x]; vis[x] = 1; if (x == y) return; for (long long i = 0; i < v.size(); i++) { if (vis[i]) continue; if (p.first > v[i].first && p.first < v[i].second) { dfs(i, y); } else if (p.second > v[i].first && p.second < v[i].second) { dfs(i, y); } } } signed main() { ios::sync_with_stdio(0); long long n, m, b, c; while (cin >> n) { v.clear(); long long mp[105][105]; for (long long i = 0; i < n; i++) { cin >> m >> b >> c; if (m == 2) { vis.assign(100, 0); dfs(b - 1, c - 1); if (vis[c - 1]) { cout << YES n ; } else { cout << NO n ; } } if (m == 1) { v.push_back(make_pair(b, c)); } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1000000 + 10; const int mod = 1e9 + 7; int n, k, a[maxn], f[maxn], g[maxn]; int fast_pow(int a, int b) { int ret = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) ret = 1ll * ret * a % mod; return ret; } int main() { scanf( %d%d , &n, &k); for (int i = 1; i <= min(k + 2, n); i++) a[i] = (a[i - 1] + fast_pow(i, k)) % mod; if (n <= k + 2) { printf( %d n , a[n]); return 0; } int now, ans = 0; now = f[0] = g[0] = 1; for (int i = 1; i <= k + 2; i++) { now = 1ll * now * (n - i) % mod; f[i] = 1ll * f[i - 1] * i % mod; g[i] = 1ll * -g[i - 1] * i % mod; } for (int i = 1; i <= k + 2; i++) ans = (ans + 1ll * a[i] * now % mod * fast_pow(n - i, mod - 2) % mod * fast_pow(1ll * f[i - 1] * g[k + 2 - i] % mod, mod - 2) % mod) % mod; ans = (ans + mod) % mod; printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; long long bigmod(long long a, long long p) { long long ret = 1; while (p > 0) { if ((p & 1)) ret = (ret * a) % 998244353LL; p >>= 1; a = (a * a) % 998244353LL; } return ret; } long long modinverse(long long a) { return bigmod(a, 998244353LL - 2); } long long n, fre[300009], eng[300009], mat[300009]; vector<int> graph[300009]; void dfs(int u, int par) { fre[u] = eng[u] = mat[u] = 0; long long mul = 1, mul2 = 1; for (int i = 0; i < graph[u].size(); i++) { int v = graph[u][i]; if (v == par) continue; dfs(v, u); mul *= (fre[v] + eng[v]); mul2 *= (fre[v] + 2 * eng[v]); mul %= 998244353LL; mul2 %= 998244353LL; } fre[u] = mul; mat[u] = mul2; for (int i = 0; i < graph[u].size(); i++) { int v = graph[u][i]; if (v == par) continue; eng[u] += (modinverse((fre[v] + 2 * eng[v]) % 998244353LL) * mat[v]) % 998244353LL; } eng[u] %= 998244353LL; eng[u] *= mul2; eng[u] %= 998244353LL; } int main() { scanf( %lld , &n); int u, v; for (int i = 1; i < n; i++) { scanf( %d %d , &u, &v); graph[u].push_back(v); graph[v].push_back(u); } dfs(1, -1); long long ans = (fre[1] + eng[1]) % 998244353LL; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> m >> n; char a[n][m]; int i, j, c; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> a[i][j]; } } char b[m][n]; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { b[i][j] = a[j][i]; } } for (i = 0; i < m; i++) { c = 2; while (c--) { for (j = 0; j < n; j++) { cout << b[i][j] << b[i][j]; } cout << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int cnt = 0, cur = 0; do { cur++; } while (cur * 5 < n); cnt = cur; cur = 0; do { cur++; } while (cur * 4 < n); cnt = max(cnt, cur); for (int i = 0; i < cnt; i++) cout << 5; cout << 1; cout << endl; for (int i = 0; i < cnt; i++) cout << 4; cout << 9; }
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 5, inf = 1e9 + 7; int n, m, x, y, l, r, f[N]; struct tt { int l, r; } a[100]; int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) { scanf( %d%d , &x, &y); l = max(1, x - y), r = min(m, x + y); a[i].l = l, a[i].r = r; } for (int i = 1; i <= m; i++) { f[i] = i; for (int j = 1; j <= n; j++) { if (a[j].l <= i && i <= a[j].r) f[i] = min(f[i], f[i - 1]); else if (a[j].r < i) f[i] = min(f[i], i - a[j].r + f[max(0, a[j].l - i + a[j].r - 1)]); } } printf( %d n , f[m]); return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const int N = 300005; vector<pair<int, int>> g[N]; int pre[N], vis[N], sz[N]; long long d[N]; void dfs1(int u, int fa) { pre[u] = fa; for (auto [v, w] : g[u]) { if (v != fa) { d[v] = d[u] + w; dfs1(v, u); } } } long long dfs2(int u) { long long ret = 0; sz[u] = 1; for (auto [v, w] : g[u]) { if (!vis[v]) { vis[v] = true; ret = max(ret, dfs2(v) + w); sz[u] += sz[v]; } } return ret; } int main(int argc, char *argv[]) { std::ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } dfs1(1, 0); vector<int> path; for (int i = n; i; i = pre[i]) path.push_back(i), vis[i] = 1; reverse(path.begin(), path.end()); long long mx = -inf, now = dfs2(path[0]), now2 = -inf; for (int i = 1; i < path.size(); i++) { long long x = dfs2(path[i]); if (sz[path[i]] > 2) x = inf; if (now == 0 && x == 0) { if (i > 1) { mx = max(mx, now2 + x - (d[path[i]] - d[path[i - 1]])); } } else { mx = max(mx, now + x - (d[path[i]] - d[path[i - 1]])); } now = now - (d[path[i]] - d[path[i - 1]]); now2 = now; now = max(now, x); } while (m--) { long long x; cin >> x; if (x + mx >= 0) cout << d[n] << endl; else cout << d[n] + x + mx << endl; } return 0; }
//doing a 800 difficulty question using dynammic programming #include<bits/stdc++.h> #define ll long long using namespace std; int main(){ int t; cin>>t; while(t--){ ll n,m,k; cin>>n>>m>>k; ll dp[n][m]; for(ll i=0;i<n;i++){ for(ll j=0;j<m;j++){ if(i==0){ dp[i][j]=j; } if(i!=0){ dp[i][j]=dp[i-1][j]+(j+1); }}} if(dp[n-1][m-1]==k){ cout<< YES << n ; } else{ cout<< NO << n ; } }}
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> c; vector<bool> used; vector<vector<int> > e; vector<vector<int> > ices; int colors = 1; int cur = 1; int used_colors[1000000]; void dfs(int v) { if (used[v]) return; used[v] = 1; if (ices[v].size() > colors) { colors = ices[v].size(); } cur++; for (int j = 0; j < ices[v].size(); j++) { int t = ices[v][j]; if (c[t]) { used_colors[c[t]] = cur; } } int cur_unused = 1; for (int j = 0; j < ices[v].size(); j++) { int t = ices[v][j]; if (!c[t]) { while (used_colors[cur_unused] == cur) cur_unused++; c[t] = cur_unused++; } } for (int i = 0; i < e[v].size(); i++) { dfs(e[v][i]); } } int main() { for (int i = 0; i < 1e6; i++) { used_colors[i] = 0; } ios_base::sync_with_stdio(0); cin >> n >> m; c.resize(m + 1); e.resize(n); used.assign(n, 0); ices.resize(n); int cnt; for (int i = 0; i < n; i++) { cin >> cnt; if (cnt == 0) continue; ices[i].resize(cnt); for (int j = 0; j < cnt; j++) { cin >> ices[i][j]; } } for (int i = 0; i < n - 1; i++) { int v, u; cin >> v >> u; v--, u--; e[v].push_back(u); e[u].push_back(v); } dfs(0); cout << colors << n ; for (int i = 0; i < m; i++) { cout << max(1, c[i + 1]) << ; } cout << n ; }
#include <bits/stdc++.h> namespace my_std { using namespace std; inline int read() { char c = getchar(); int ans = 0, f = 1; while (c > 9 || c < 0 ) { f ^= (c == - ); c = getchar(); } for (; c <= 9 && c >= 0 ; c = getchar()) ans = (ans << 1) + (ans << 3) + (c ^ 48); return f ? ans : -ans; } const int mod = 1e9 + 7, N = 5010; inline void inc(int &x, const int &y) { x += y; if (x >= mod) x -= mod; } inline int ksm(int x, long long y) { int res = 1; for (; y; y >>= 1, x = 1ll * x * x % mod) if (y & 1) res = 1ll * res * x % mod; return res; } inline int gcd(int x, int y) { if (x > y) swap(y, x); return y ? gcd(y, x % y) : x; } } // namespace my_std using namespace my_std; vector<int> e[N << 1]; int n, m, d, lv[N], ans[N], wt[N << 1], vis[N << 1]; struct People_CF1139E { int pot, cl, lf; } st[N]; inline bool find(int u) { register int v; for (register int i = (0); i <= (int(e[u].size()) - 1); ++i) { v = e[u][i]; if (vis[v]) continue; vis[v] = 1; if (wt[v] == -1 || find(wt[v])) { wt[v] = u; return 1; } } return 0; } int main() { n = read(), m = read(); for (register int i = (1); i <= (n); ++i) st[i].pot = read(); for (register int i = (1); i <= (n); ++i) st[i].cl = read(); d = read(); for (register int i = (1); i <= (d); ++i) lv[i] = read(), st[lv[i]].lf = 1; for (register int i = (1); i <= (n); ++i) { if (!st[i].lf && st[i].pot <= m) { e[st[i].pot].push_back(st[i].cl + m); e[st[i].cl + m].push_back(st[i].pot); } } memset(wt, -1, sizeof(wt)); for (register int i = (d); i >= (1); --i) { for (register int j = (ans[i + 1]); j <= (m); ++j) { memset(vis, 0, sizeof(vis)); if (!find(j)) { ans[i] = j; break; } } if (st[lv[i]].pot > m) continue; e[st[lv[i]].pot].push_back(st[lv[i]].cl + m); e[st[lv[i]].cl + m].push_back(st[lv[i]].pot); } for (register int i = (1); i <= (d); ++i) printf( %d n , ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; struct query { int l, r, id, rng; bool operator<(const query &tmp) const { return rng != tmp.rng ? rng < tmp.rng : r < tmp.r; } } Q[100100]; int n, q, a[100100], b[100100], c[100100], cnt[100100], ans[100100]; int D; vector<int> v; void add(int x) { c[cnt[x]++]--, c[cnt[x]]++; } void del(int x) { c[cnt[x]--]--, c[cnt[x]]++; } int calc() { int rlt = 0; for (int i = 1; i <= D; i++) b[i] = c[i]; priority_queue<int> pq; for (int i = 0; i < v.size(); i++) if (cnt[v[i]] > D) pq.push(-cnt[v[i]]); for (int i = 1, j = 2; i <= D; i++) { rlt += b[i] / 2 * (2 * i); if (i * 2 <= D) b[i * 2] += b[i] / 2; else { for (int k = 1; k <= b[i] / 2; k++) pq.push(-i * 2); } if (b[i] & 1) { if (j <= i) j = i + 1; while (j <= D && !b[j]) j++; if (j > D) { pq.push(-i); break; } rlt += i + j; b[j]--; if (i + j <= D) b[i + j]++; else pq.push(-(i + j)); } } while (!pq.empty()) { int x = pq.top(); pq.pop(); if (pq.empty()) break; int y = pq.top(); pq.pop(); rlt -= x + y; pq.push(x + y); } return rlt; } int main() { scanf( %d , &n); D = 2 * sqrt(n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]), cnt[a[i]]++; for (int i = 1; i <= 100000; i++) { if (cnt[i] > D) v.push_back(i); cnt[i] = 0; } scanf( %d , &q); for (int i = 1; i <= q; i++) scanf( %d %d , &Q[i].l, &Q[i].r), Q[i].id = i, Q[i].rng = Q[i].l / D; sort(Q + 1, Q + q + 1); int l = 1, r = 0; for (int i = 1; i <= q; i++) { while (r < Q[i].r) add(a[++r]); while (l < Q[i].l) del(a[l++]); while (r > Q[i].r) del(a[r--]); while (l > Q[i].l) add(a[--l]); ans[Q[i].id] = calc(); } for (int i = 1; i <= q; i++) printf( %d n , ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; struct Graph { struct Edge { int u, v; int cost; Edge(int _u, int _v, int _cost) : u(_u), v(_v), cost(_cost) {} }; int n, m; bool is_digraph; vector<vector<int>> g; vector<Edge> e; Graph(int _n) : n(_n), m(0) { assert(n >= 1); g.resize(n); set_di(false); } Graph(int _n, int _m, bool _di = false) : n(_n) { assert(n >= 1); g.resize(n); e.reserve(_m); m = 0; set_di(_di); input(_m); } void set_di(bool _di) { pos = vector<int>(n, 0); deg = vector<int>(n, 0); is_digraph = _di; } vector<int> deg; inline void add(int u, int v, int cost = 1) { assert(0 <= u && u < n); assert(0 <= v && v < n); if (is_digraph) { g[u].emplace_back(m); } else { deg[u]++; deg[v]++; g[u].emplace_back(m); g[v].emplace_back(m); } e.emplace_back(u, v, cost); m++; } void input(int _m) { int u, v; for (int i = 0; i < _m; i++) { cin >> u >> v; u--; v--; add(u, v); } } vector<int> pos; void dfs(int u) { for (; pos[u] < g[u].size(); pos[u]++) { int i = g[u][pos[u]]; int v = e[i].u ^ e[i].v ^ u; dfs(v); } } int k; void dele() { int need_del = m - (n + m + 1) / 2; if (need_del > 0) { int cnt = 0; random_shuffle(e.begin(), e.end()); vector<int> d = deg; for (auto& x : e) { if (cnt < need_del && d[x.u] - 1 >= (deg[x.u] + 1) / 2 && d[x.v] - 1 >= (deg[x.v] + 1) / 2) { d[x.u]--; d[x.v]--; x.cost = 0; cnt++; } } assert(cnt == need_del); k = (n + m + 1) / 2; } else { k = m; } } void pri() { cout << k << n ; for (auto& x : e) { if (x.cost) cout << x.u + 1 << << x.v + 1 << n ; } } }; void solve() { int n, m; cin >> n >> m; Graph g(n, m); g.dele(); g.pri(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); cout << endl; }
#include<bits/stdc++.h> using namespace std; #define N 1500009 #define INF 5e8 #define mymin(x,y) (x>=y?y:x) #define mymax(x,y) (x>=y?x:y) typedef long long int LL; map<LL, int> mp[3]; int a[4][N]; struct din{ int ap, id; }dn[4][N]; bool cmp(din af, din bf){ return af.ap < bf.ap; } int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); int n[4]; for(int i = 0; i < 4; i++) cin >> n[i]; for(int i = 0; i < 4; i++){ for(int j = 0; j < n[i]; j++){ cin >> dn[i][j].ap; dn[i][j].id = j + 1; } } for(int i = 0; i < 3; i++){ int m; cin >> m; for(int j = 0; j < m; j++){ int x, y; cin >> x >> y; mp[i][1ll * x * N + y] = 1; } } for(int i = 2; i >= 0; i--){ // cout << i << << n[i] << endl; // cout << dn[3][0].id << << mp[2][0 * N + 1] << endl; sort(dn[i + 1], dn[i + 1] + n[i + 1], cmp); for(int j = 0; j < n[i]; j++){ int ind = 0; while(ind < n[i + 1] && mp[i][1ll * (j + 1) * N + dn[i + 1][ind].id]) ind++; if(ind == n[i + 1]) dn[i][j].ap = INF; else dn[i][j].ap += dn[i + 1][ind].ap; // cout << dn[i][j].ap << ; } // cout << endl; } sort(dn[0], dn[0] + n[0], cmp); if(dn[0][0].ap >= INF) cout << -1 << endl; else cout << dn[0][0].ap << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int di[] = {-1, -1, -1, 0, 1, 1, 1, 0, 0}, dj[] = {-1, 0, 1, 1, 1, 0, -1, -1, 0}; int st[20][20][20] = {}; bool fr[20][20][20 * 20] = {}; int n; void file_open() { freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); } void xuli() { queue<int> q1, q2, q3; q1.push(7); q2.push(0); q3.push(0); while (!q1.empty()) { int u = q1.front(), v = q2.front(), k = q3.front() + 1, tK = k; if (k > 9) k = 9; q1.pop(); q2.pop(); q3.pop(); for (int i = 0; i < 9; ++i) { int d = u + di[i], e = v + dj[i]; if (d >= 0 && d < 8 && e >= 0 && e < 8) { if (st[d][e][k] == 0 && st[d][e][k - 1] == 0 && !fr[d][e][tK]) { fr[d][e][tK] = 1; q1.push(d); q2.push(e); q3.push(tK); if (d == 0 && e == 7) { printf( WIN ); return; } } } } } printf( LOSE ); } int main() { char c[20][20] = {}; for (int i = 0; i < 8; ++i) { for (int j = 0; j < 8; ++j) { scanf( %c , &c[i][j]); if (c[i][j] == S ) { int u = 0; while (u + i < 8) { st[i + u][j][u] = 1; ++u; } } } scanf( n ); } xuli(); return 0; }
#include <bits/stdc++.h> using namespace std; long long arr1[100][10000], arr[10000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t, i, j, k, n, m, x, y, z, a, b, mx; cin >> t; while (t--) { cin >> n; for (i = 0; i < n; i++) { cin >> arr[i]; } for (i = 1; i < 27; i++) { x = 0; for (j = 0; j < n; j++) { if (arr[j] == i) { x++; arr1[i][j] = x; } else { arr1[i][j] = x; } } } mx = 1; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (arr[j] == arr[i] && (arr1[arr[i]][i]) == (arr1[arr[i]][n - 1] - arr1[arr[i]][j] + 1)) { x = arr1[arr[i]][i] + arr1[arr[i]][n - 1] - arr1[arr[i]][j] + 1; for (k = 1; k <= 26; k++) { y = arr1[k][j - 1] - arr1[k][i]; z = x + y; mx = max(z, mx); } } } } cout << mx << endl; } }
#include <bits/stdc++.h> using namespace std; int n, a[1001][10], cnt, ans[1001]; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d%d%d%d%d , &a[i][0], &a[i][1], &a[i][2], &a[i][3], &a[i][4]); } if (n > 11) { printf( 0 ); return 0; } for (int i = 1; i <= n; i++) { bool flg = 0; for (int j = 1; j <= n; j++) if (i != j) for (int k = j + 1; k <= n; k++) if (k != i) { int mx = 0; double mmx = 0, mmy = 0; for (int p = 0; p < 5; p++) { mx += (a[i][p] - a[j][p]) * (a[i][p] - a[k][p]); mmx += (a[i][p] - a[j][p]) * (a[i][p] - a[j][p]); mmy += (a[i][p] - a[k][p]) * (a[i][p] - a[k][p]); } mmx = sqrt(mmx); mmy = sqrt(mmy); if (acos((double)mx / (mmx * mmy)) * 2 <= acos(-1) - 1e-8) flg = 1; } if (!flg) ans[++cnt] = i; } printf( %d n , cnt); for (int i = 1; i <= cnt; i++) printf( %d n , ans[i]); }
#include <bits/stdc++.h> long long int mod = 1000000000 + 7; long long int inf = 1000000000000000000; using namespace std; int main() { iostream::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int ans = 1, a; cin >> a; for (int i = 0; i < 31; i++) { if ((a >> i) & 1) ans *= 2; } cout << ans << n ; } }
#include <bits/stdc++.h> using namespace std; const int MAXN = 5002, NP = 1e9; int Add[MAXN], MyResult[MAXN], MyCheck[MAXN]; bool check(int n, vector<pair<int, pair<int, pair<int, int> > > > A) { for (int i = 0; i < A.size(); i++) { int t = A[i].first, s = A[i].second.first, e = A[i].second.second.first, v = A[i].second.second.second; if (t == 1) { for (int i = s - 1; i <= e - 1; i++) { MyCheck[i] += v; } } else { int mx = MyCheck[s - 1]; for (int i = s - 1; i <= e - 1; i++) mx = max(mx, MyCheck[i]); if (mx != v) return 0; } } return 1; } int main() { int t, n, m, s, e, v; vector<pair<int, pair<int, pair<int, int> > > > A; scanf( %d%d , &n, &m); for (int i = 0; i < n; i++) MyCheck[i] = MyResult[i] = NP; while (m--) { scanf( %d%d%d%d , &t, &s, &e, &v); A.push_back(make_pair(t, make_pair(s, make_pair(e, v)))); if (t == 1) { for (int i = s - 1; i <= e - 1; i++) Add[i] += v; } else { for (int i = s - 1; i <= e - 1; i++) MyResult[i] = min(MyResult[i], v - Add[i]); } } for (int i = 0; i < n; i++) MyCheck[i] = MyResult[i]; if (check(n, A)) { printf( YES n ); for (int i = 0; i < n; i++) { if (i) printf( ); printf( %d , MyResult[i]); } printf( n ); } else printf( NO n ); }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; for (; t; t--) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<int> gist(3); for (int i = 0; i < n; i++) gist[a[i] % 3]++; while (gist[1] > 0 && gist[2] > 0) { gist[1]--; gist[2]--; gist[0]++; } while (gist[1] - 3 >= 0) { gist[1] -= 3; gist[0]++; } while (gist[2] - 3 >= 0) { gist[2] -= 3; gist[0]++; } cout << gist[0] << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = 2 * acos(0.0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count()); const string DIGITS = 0123456789 ; const string ALPH = abcdefghijklmnopqrstuvwxyz ; template <class T0, class T1> inline ostream &operator<<(ostream &out, pair<T0, T1> &a) { return out << { << a.first << , << a.second << } ; } template <class T0, class T1, class T2> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) { return out << { << get<0>(a) << , << get<1>(a) << , << get<2>(a) << } ; } template <class T0, class T1, class T2, class T3> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) { return out << { << get<0>(a) << , << get<1>(a) << , << get<2>(a) << , << get<3>(a) << } ; } template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) { out << [ ; for (int i = 0; i < (int)(a.size()); ++i) out << a[i] << vector<string>{ , , ] }[i + 1 == a.size()]; return out; } void smain(); signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(10) << fixed; smain(); return 0; } const int N = 510; int n, m; vector<string> t; vector<vector<int> > glav[4]; void kek(char p, int u) { vector<vector<int> > a(n, vector<int>(m)); auto b = a, c = a; for (int i = 0; i < n; i++) { int u = 0; string s = t[i]; for (int j = 0; j < m; j++) { if (s[j] == p) { a[i][j] = 1; u++; } else { a[i][j] = 0; u = 0; } b[i][j] = u; } } for (int i = 0; i < m; i++) { int u = 0; for (int j = n - 1; j >= 0; j--) { if (a[j][i] == 1) u++; else u = 0; c[j][i] = u; } } vector<vector<int> > ans(n, vector<int>(m)); for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < m; j++) { if (a[i][j] == 0) continue; if (j == 0 || i == n - 1) { ans[i][j] = 1; } else { int v = min(b[i][j], c[i][j]); ans[i][j] = min(ans[i + 1][j - 1] + 1, v); } } } glav[u] = ans; } int flex[N][N]; int lol[N][N][251]; int get(int r, int c, int k) { if (r < 0 || c < 0) return 0; return lol[r][c][k]; } int get(int r1, int c1, int r2, int c2, int k) { return get(r2, c2, k) - get(r1 - 1, c2, k) - get(r2, c1 - 1, k) + get(r1 - 1, c1 - 1, k); } void smain() { int q; cin >> n >> m >> q; string s; for (int i = 0; i < n; i++) { cin >> s; t.push_back(s); } kek( G , 0); kek( R , 1); kek( B , 2); kek( Y , 3); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int k = 1;; k++) { if (i + k >= n || j - k < 0) break; if (glav[0][i][j] >= k && glav[1][i][j - k] >= k && glav[2][i + k][j] >= k && glav[3][i + k][j - k] >= k) { flex[i][j] = k; } } } } for (int k = 1; k <= 250; k++) { for (int i = 0; i < n; i++) { int prev = 0; for (int j = 0; j < m; j++) { prev += (flex[i][j] == k); if (i) { lol[i][j][k] += lol[i - 1][j][k]; } lol[i][j][k] += prev; } } } while (q--) { int r1, c1, r2, c2; cin >> r1 >> c1 >> r2 >> c2; r1--; c1--; r2--; c2--; swap(c1, c2); int l = 0, r = 251; while (l + 1 < r) { int mid = l + r >> 1; int r3 = r2 - mid * 2 + 1; int c3 = c2 + mid * 2 - 1; bool flag = true; if (r3 < r1 || c3 > c1) flag = false; if (flag) { if (get(r1, c3, r3, c1, mid) == 0) flag = false; } if (flag) l = mid; else r = mid; } cout << 1LL * l * 2 * l * 2 << n ; } }
#include <bits/stdc++.h> using namespace std; const int inf = 1000000000; long long mod = 1000000007LL; long long mod2 = 998244353LL; int n; bool mat[2005][2005]; int cntr[2005], cntc[2005]; int cnt[2005]; int main() { cin >> n; memset(mat, false, sizeof(mat)); memset(cntr, 0, sizeof(cntr)); memset(cntc, 0, sizeof(cntc)); memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= n; ++i) { string s; cin >> s; for (int j = 1; j <= n; ++j) { mat[i][j] = (s[j - 1] == 1 ); } } int ans = 0; for (int i = 1; i <= n; ++i) { for (int j = n; j >= i; --j) { if (j == i) continue; bool x = ((cntr[i] + cntc[j]) % 2 > 0); if (mat[i][j] != x) { ans++; cntr[i]++; cntc[j]++; } } } for (int i = 1; i <= n; ++i) { cnt[i] += cntr[i] + cntc[i]; } memset(cntr, 0, sizeof(cntr)); memset(cntc, 0, sizeof(cntc)); for (int i = n; i >= 1; --i) { for (int j = 1; j <= i; ++j) { if (j == i) continue; bool x = ((cntr[i] + cntc[j]) % 2 > 0); if (mat[i][j] != x) { ans++; cntr[i]++; cntc[j]++; } } } for (int i = 1; i <= n; ++i) { cnt[i] += cntr[i] + cntc[i]; } for (int i = 1; i <= n; ++i) { bool x = (cnt[i] % 2 > 0); if (mat[i][i] != x) { ans++; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename _T> inline void _DBG(const char *s, _T x) { cerr << s << = << x << n ; } template <typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while (*s != , ) cerr << *s++; cerr << = << x << , ; _DBG(s + 1, a...); } const int N = 1e5 + 7; vector<int> G[N]; int dist[N]; bool vis[N]; void dfs(int v, int d) { vis[v] = true; dist[v] = d; for (auto &u : G[v]) { if (!vis[u]) { dfs(u, d + 1); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n; cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } int root = -1; for (int i = 1; i <= n; i++) { if ((int)(G[i]).size() == 1) { root = i; break; } } dfs(root, 0); int parity = dist[root] % 2; bool good = true; for (int i = 2; i <= n; i++) { if ((int)(G[i]).size() == 1 && dist[i] % 2 != parity) { good = false; } } if (good) { cout << 1 << ; } else { cout << 3 << ; } int edges = n - 1; int leaves = 0; set<int> leaf_parents; for (int i = 1; i <= n; i++) { if ((int)(G[i]).size() == 1) { leaves++; } else { for (auto &u : G[i]) { if ((int)(G[u]).size() == 1) { leaf_parents.insert(i); } } } } cout << edges - leaves + (int)(leaf_parents).size() << n ; return 0; }
#include <bits/stdc++.h> int N, Q; int a[300005]; int p[300005]; int oop[1100005]; int ui, uv; void update(int n, int nl, int nr) { if (nl == ui && ui + 1 == nr) { oop[n] = uv; } else { int mid = (nl + nr) / 2; if (ui < mid) update(n * 2 + 1, nl, mid); else update(n * 2 + 2, mid, nr); oop[n] = oop[n * 2 + 1] + oop[n * 2 + 2]; } } int ql, qr; int query(int n, int nl, int nr) { if (qr <= nl || nr <= ql) return 0; else if (ql <= nl && nr <= qr) return oop[n]; else { int mid = (nl + nr) / 2; return query(n * 2 + 1, nl, mid) + query(n * 2 + 2, mid, nr); } } int main() { scanf( %d , &N); a[0] = p[0] = 0; for (int i = 1; i <= N; i++) { scanf( %d , &a[i]); p[a[i]] = i; } a[N + 1] = p[N + 1] = N + 1; for (int i = 1; i < N; i++) { ui = i; uv = p[i] > p[i + 1]; update(0, 0, N + 2); } scanf( %d , &Q); for (int i = 0; i < Q; i++) { int t, x, y; scanf( %d%d%d , &t, &x, &y); if (t == 1) { ql = x, qr = y; printf( %d n , query(0, 0, N + 2) + 1); } else { std::swap(a[x], a[y]); p[a[x]] = x; p[a[y]] = y; ui = a[x] - 1; uv = p[a[x] - 1] > p[a[x]]; update(0, 0, N + 2); ui = a[x]; uv = p[a[x]] > p[a[x] + 1]; update(0, 0, N + 2); ui = a[y] - 1; uv = p[a[y] - 1] > p[a[y]]; update(0, 0, N + 2); ui = a[y]; uv = p[a[y]] > p[a[y] + 1]; update(0, 0, N + 2); } } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; constexpr int MAXN = 5 + 50; constexpr ll INF = 1000000000000000002LL; namespace dsu { int f[MAXN], sz[MAXN]; void build(int n) { fill(sz, sz + n, 1); for (int i = (int)0; i < (int)n; ++i) f[i] = i; } int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); } bool join(int x, int y) { if ((x = find(x)) == (y = find(y))) return false; if (sz[x] < sz[y]) swap(x, y); sz[x] += sz[y]; f[y] = x; return true; } bool connected(int i, int j) { return find(i) == find(j); } } // namespace dsu ll memo[MAXN], f[MAXN]; int res[MAXN]; bitset<MAXN> used; ll add(ll a, ll b) { if (a + b >= INF) return INF; return a + b; } ll mul(ll a, ll b) { long double ans = (long double)(a) * (long double)(b); if (ans >= INF) return INF; return a * b; } ll dp(int len) { ll& ans = memo[len]; if (~ans) { assert(ans >= 0); return ans; } if (len == 0) return ans = 1; ans = 0; for (int i = 1; i <= len; ++i) ans = add(ans, mul(dp(len - i), f[max(0, i - 2)])); return ans; } void rebuild(int len, ll k, int pos, int n) { if (len == 0) { assert(pos == n); return; } ll ans = 0; for (int i = 1; i <= len; ++i) { ll ol = ans; ans = add(ans, mul(dp(len - i), f[max(0, i - 2)])); if (k <= ans) { k -= ol; ll j = k / dp(len - i); while (mul(j, dp(len - i)) < k) ++j; k -= mul(j - 1, dp(len - i)); res[pos] = i + pos; dsu::build(MAXN); used.reset(); for (int posin = 2; posin <= i; ++posin) { int put = -1; ll ac = 0, olac; for (int num = 1; num < i; ++num) { if (dsu::connected(num, posin) || used[num]) continue; olac = ac; ac = add(ac, f[max(0, i - posin - 1)]); if (j <= ac) { put = num; break; } } assert(put != -1); res[pos + posin - 1] = put + pos; used[put] = true; dsu::join(put, posin); j -= olac; } rebuild(len - i, k, i + pos, n); return; } } assert(false); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); f[0] = 1; for (int i = (int)1; i < (int)MAXN; ++i) { if (f[i - 1] == INF) { f[i] = f[i - 1]; } else { f[i] = min(INF, 1LL * i * f[i - 1]); } } memset(memo, -1, sizeof memo); int tc; cin >> tc; while (tc--) { int n; ll k; cin >> n >> k; if (k <= dp(n)) { rebuild(n, k, 0, n); for (int i = (int)0; i < (int)n; ++i) cout << res[i] << ; } else { cout << -1; } cout << n ; } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:1024000000,1024000000 ) using namespace std; vector<int> a[100005]; int dp[100005], n, t; int main() { for (int i = 2; i <= 100000; i++) for (int j = 1; j * i <= 100000; j++) a[i * j].push_back(i); while (cin >> n) { memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { cin >> t; int mx = 0; for (int j = 0; j < a[t].size(); j++) { mx = max(mx, dp[a[t][j]]); } for (int j = 0; j < a[t].size(); j++) { dp[a[t][j]] = mx + 1; } } cout << max(1, *max_element(dp, dp + 100001)) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n1, n2, k1, k2; cin >> n1 >> n2 >> k1 >> k2; if (n1 <= n2) cout << Second ; else cout << First ; }
#include <bits/stdc++.h> using namespace std; int n, sum, cnt[10]; bool check; void ghi() { bool check = false; for (int i = 9; i > -1; i--) { if (cnt[i] == 0) continue; if (i != 0) check = true; if (!i && !check) cnt[i] = 1; for (int j = 1; j <= cnt[i]; j++) printf( %d , i); } printf( n ); } int main() { if (0) { freopen( a.inp , r , stdin); }; scanf( %d , &n); int x; for (int i = 0; i < n; i++) { scanf( %d , &x); cnt[x]++; sum += x; if (x == 0) check = true; } if (!check) { printf( -1 n ); return 0; } if (sum % 3 == 1) { for (int i = 0; i < 10; i++) { if (i % 3 != 1 || cnt[i] == 0) continue; cnt[i]--; ghi(); return 0; } for (int i = 0; i < 10; i++) { if (i % 3 != 2 || cnt[i] == 0) continue; cnt[i]--; for (int j = i; j < 10; j++) { if (j % 3 != 2 || cnt[j] == 0) continue; cnt[j]--; ghi(); return 0; } } } else if (sum % 3 == 2) { for (int i = 0; i < 10; i++) { if (i % 3 != 2 || cnt[i] == 0) continue; cnt[i]--; ghi(); return 0; } for (int i = 0; i < 10; i++) { if (i % 3 != 1 || cnt[i] == 0) continue; cnt[i]--; for (int j = i; j < 10; j++) { if (j % 3 != 1 || cnt[j] == 0) continue; cnt[j]--; ghi(); return 0; } } } else ghi(); }
#include <bits/stdc++.h> using namespace std; int nA, nB, m, k, ans, x, y, c1, c2, cnt; int deg[1310 << 1], be[190010], w[1310], w2[1310]; int id[10005][1310], f[10005][1310]; void rv(int x, int c1, int c2) { swap(f[x][c1], f[x][c2]); swap(id[x][c1], id[x][c2]); be[id[x][c2]] = c2; if (f[x][c2]) rv(f[x][c2], c2, c1); } int main() { scanf( %d%d%d , &nA, &m, &k); nB = nA; int foo; for (int i = 1; i <= nA; i++) scanf( %d , &foo); for (int i = 1; i <= m; ++i) { scanf( %d%d , &x, &y); if (x < y) swap(x, y); if (deg[x] % k == 0) w[x] = ++cnt; if (deg[nA + y] % k == 0) w2[y] = ++cnt; deg[x]++, deg[nA + y]++; x = w[x], y = w2[y]; c1 = 0, c2 = 0; for (int j = 1; j <= k; ++j) if (!f[x][j]) { c1 = j; break; } for (int j = 1; j <= k; ++j) if (!f[y][j]) { c2 = j; break; } if (f[y][c1]) rv(y, c1, c2); f[x][c1] = y; id[x][c1] = i; f[y][c1] = x; id[y][c1] = i; be[i] = c1; } for (int i = 1; i <= m; ++i) printf( %d , be[i]); putchar( n ); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100100; int N, M; int app[MAXN]; char type[MAXN]; int P[MAXN]; bool check(int leader) { set<int> users; users.insert(leader); for (int i = 0; i < M; i++) { if (type[i] == + ) users.insert(P[i]); else users.erase(P[i]); if (!users.empty() && users.find(leader) == users.end()) return false; } return true; } int main() { cin >> N >> M; for (int i = 0; i < M; i++) cin >> type[i] >> P[i]; int offp = 0; for (int i = 0; i < M; i++) { if (type[i] == - && !app[P[i]]) offp = P[i]; app[P[i]] = true; } if (offp != 0) { if (check(offp)) app[offp] = false; } else { if (check(P[0])) app[P[0]] = false; } int total = 0; for (int i = 1; i <= N; i++) total += !app[i]; cout << total << n ; for (int i = 1; i <= N; i++) if (!app[i]) cout << i << ; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e6 + 9; int main () { int t; cin >> t; while (t--) { ll n, last; cin >> n; for (ll i = 0; i < 33; i++) { if (n & (1ll << i)) last = i; } cout << ((1ll << last) - 1ll) << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int total, n; cin >> total >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; int ans = 0; for (int i = 0; i < n - 1; i++) { ans += (total - v[i]); } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v; int total, num, i, koita = 0; scanf( %d , &total); for (i = 0; i < total; i++) { scanf( %d , &num); v.push_back(num); } int maximum = v[0]; int minimum = v[0]; for (i = 1; i < total; i++) { num = v[i]; if (num > maximum) { maximum = num; koita++; } if (num < minimum) { minimum = num; koita++; } } printf( %d n , koita); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], b[n], c = 0, x = 0, y = 0, z = 0, s = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 1) { x++; } } for (int i = 0; i < n; i++) { cin >> b[i]; if (a[i] != b[i]) { c = 1; } if (b[i] == 1) { y++; } } for (int i = 0; i < n; i++) { if (a[i] == 1 && b[i] == 0) { z++; } } if (c == 0 || x == 0 || z == 0) { cout << -1 << endl; } else { for (int i = 1;;) { if (s > y) { cout << i - 1 << endl; break; } else { s = z * i + (x - z); i++; } } } return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { char ch = getchar(); long long nega = 1; while (!isdigit(ch)) { if (ch == - ) nega = -1; ch = getchar(); } long long ans = 0; while (isdigit(ch)) { ans = ans * 10 + ch - 48; ch = getchar(); } if (nega == -1) return -ans; return ans; } inline long long min(long long x, long long y, long long z) { return min(x, min(y, z)); } inline long long max(long long x, long long y, long long z) { return max(x, max(y, z)); } inline long long add(long long x, long long y) { return x + y >= 998244353 ? x + y - 998244353 : x + y; } inline long long add(long long x, long long y, long long z) { return add(add(x, y), z); } inline long long sub(long long x, long long y) { return x - y < 0 ? x - y + 998244353 : x - y; } inline long long mul(long long x, long long y) { return 1LL * x * y % 998244353; } inline long long mul(long long x, long long y, long long z) { return mul(mul(x, y), z); } vector<long long> a, b; vector<char> ans; long long n; void solve1() { if (a[0] == b[0]) cout << SMALL n0 n ; else cout << IMPOSSIBLE n ; } bool rsame(vector<long long> x, vector<long long> y) { reverse(x.begin(), x.end()); return x == y; } void fail() { cout << IMPOSSIBLE n , exit(0); } bool push_back(char s) { if ((long long)ans.size() <= 200000) { ans.push_back(s); return 1; } return 0; } void solve2() { long long tot = 0, sum = 0; while (a != b && !rsame(a, b)) { if (b[0] > b[1]) swap(b[0], b[1]), tot++, push_back( R ); if (a[0] > a[1] && b[0] == a[1]) { if (b[0] == 0) fail(); if (b[1] < a[0]) fail(); long long ned = b[1] - a[0]; if (ned % b[0] != 0) fail(); sum += ned / b[0], tot += ned / b[0] + 1; if (sum <= 200000) { for (long long i = 1; i <= ned / b[0]; i++) push_back( P ); push_back( R ); } if (sum <= 200000) { cout << SMALL n ; reverse(ans.begin(), ans.end()); cout << (long long)ans.size() << endl; for (auto i : ans) putchar(i); } else { cout << BIG n ; cout << sum << endl; } exit(0); } if (b[0] == a[0]) { if (b[0] == 0) fail(); if (b[1] < a[1]) fail(); long long ned = b[1] - a[1]; if (ned % b[0] != 0) fail(); sum += ned / b[0], tot += ned / b[0] + 1; if (sum <= 200000) { for (long long i = 1; i <= ned / b[0]; i++) push_back( P ); push_back( R ); } if (sum <= 200000) { cout << SMALL n ; reverse(ans.begin(), ans.end()); cout << (long long)ans.size() << endl; for (auto i : ans) putchar(i); } else { cout << BIG n ; cout << sum << endl; } exit(0); } if (b[0] == 0) fail(); tot += b[1] / b[0]; sum += b[1] / b[0]; if (sum <= 200000) { for (long long i = 1; i <= b[1] / b[0]; i++) push_back( P ); } b[1] %= b[0]; } if (a != b) swap(b[0], b[1]), tot++, push_back( R ); if (sum <= 200000) { cout << SMALL n ; reverse(ans.begin(), ans.end()); cout << (long long)ans.size() << endl; for (auto i : ans) putchar(i); } else { cout << BIG n ; cout << sum << endl; } exit(0); } bool inc(vector<long long> x) { for (long long i = 1; i < n; i++) { if (x[i] < x[i - 1]) return 0; } return 1; } bool dec(vector<long long> x) { for (long long i = 1; i < n; i++) { if (x[i] > x[i - 1]) return 0; } return 1; } bool alz(vector<long long> x) { for (long long i = 0; i < n - 1; i++) if (x[i] != 0) return 0; return 1; } void print(vector<long long> x) { for (long long i : x) printf( %lld , i); cout << n ; } void solve3() { long long tot = 0, sum = 0; while (a != b && !rsame(a, b)) { if (alz(b)) fail(); if (dec(b)) reverse(b.begin(), b.end()), tot++, push_back( R ); if (!inc(b)) fail(); sum++; tot++; push_back( P ); for (long long i = n - 1; i > 0; i--) b[i] -= b[i - 1]; } if (a != b) reverse(b.begin(), b.end()), tot++, push_back( R ); if (sum <= 200000) { cout << SMALL n ; cout << ans.size() << endl; reverse(ans.begin(), ans.end()); for (auto i : ans) putchar(i); } else { cout << BIG n ; cout << sum << endl; } } signed main() { cin >> n; a.resize(n), b.resize(n); for (long long i = 0; i < n; i++) a[i] = read(); for (long long i = 0; i < n; i++) b[i] = read(); if (n >= 3) solve3(); else if (n == 2) solve2(); else solve1(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 9; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long s[maxn]; int f[maxn], F[maxn], p[maxn]; int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; ++i) { scanf( %lld , &s[i]); } for (int i = 2; i <= n; ++i) { scanf( %d , &p[i]); } for (int i = n; i > 1; --i) { s[p[i]] += s[i]; } for (int i = n; i; --i) { long long t = s[1] / gcd(s[1], s[i]); if (t <= n) { ++f[t]; } } for (int i = n; i; --i) { for (int j = i; (j += i) <= n;) { f[j] += f[i]; } } int ans = 0; F[1] = 1; for (int i = 1; i <= n; ++i) { if (f[i] == i) { ans += F[i]; for (int j = i * 2; j <= n; j += i) { F[j] += F[i]; } } } printf( %d , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int i, j, k, n, m; int fa[200010], an[200010]; vector<int> a[200010]; pair<int, int> f[200010]; inline void P(int &x, int y) { x = 1ll * x * y % 1000000007; } inline void merge(pair<int, int> &a, pair<int, int> b) { P(a.first, b.first); int g = (2ll * b.first + b.second) % 1000000007; P(a.second, g); } inline void gao(pair<int, int> &a) { a.second = (a.second - a.first) % 1000000007; } inline void ch(pair<int, int> &a, pair<int, int> b) { a.first = 1ll * a.first * b.first % 1000000007; a.second = 1ll * a.second * b.second % 1000000007; } void dfs(int x) { f[x] = make_pair(1, 1); for (auto i : a[x]) { if (fa[x] == i) continue; fa[i] = x; dfs(i); merge(f[x], f[i]); } gao(f[x]); } void huan(int x) { an[x] = (f[x].first + f[x].second) % 1000000007; int t = 0, i, len = a[x].size() + 5; vector<pair<int, int> > g(len), f1(len), f2(len); for (auto i : a[x]) g[++t] = f[i]; f1[0] = make_pair(1, 1); for (i = 1; i <= t; i++) f1[i] = f1[i - 1], merge(f1[i], g[i]); f2[t + 1] = make_pair(1, 1); for (i = t; i; i--) f2[i] = f2[i + 1], merge(f2[i], g[i]); int _ = 0; for (auto i : a[x]) { _++; if (fa[x] == i) continue; pair<int, int> v = f1[_ - 1]; ch(v, f2[_ + 1]); gao(v); swap(f[x], v); pair<int, int> tmp = f[i]; f[i].second = (f[i].second + f[i].first) % 1000000007; merge(f[i], f[x]); gao(f[i]); huan(i); swap(f[x], v); f[i] = tmp; } } int main() { scanf( %d , &n); for (i = 1; i <= n - 1; i++) { int x; scanf( %d , &x); a[x].push_back(i + 1); a[i + 1].push_back(x); } dfs(1); huan(1); for (i = 1; i <= n; i++) printf( %d , (an[i] + 1000000007) % 1000000007); printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; int n; int ans[200005]; void query(int nl, int nr) { if (nl > nr) return; printf( ? %d %d n , nl, nr); fflush(stdout); int x, f; scanf( %d%d , &x, &f); if (nr - f + 1 <= nl + f - 1) { for (int i = nr - f + 1; i <= nl + f - 1; i++) ans[i] = x; query(nl, nr - f); query(nl + f, nr); } else { query(nl, ((nl + nr) >> 1)); query(((nl + nr) >> 1) + 1, nr); } } int main() { scanf( %d , &n); query(1, n); printf( ! %d , ans[1]); for (int i = 2; i <= n; i++) printf( %d , ans[i]); printf( n ); }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.length(); for (int i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) { if (i + 2 < n) { if (s[i + 1] == z && s[i + 2] != a ) s[i + 1] = a ; else if (s[i + 1] == z && s[i + 2] == a ) s[i + 1] = b ; else if (s[i + 1] != z && s[i + 2] != z ) s[i + 1] = z ; else { if (s[i + 1] == y ) s[i + 1] = x ; else s[i + 1] = y ; } } else { if (s[i + 1] == z ) s[i + 1] = a ; else s[i + 1] = z ; } } } cout << s; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> ostream& operator<<(ostream& cout, vector<T> V) { cout << [ ; for (auto v : V) cout << v << ; return cout << ] ; } template <class L, class R> ostream& operator<<(ostream& cout, pair<L, R> P) { return cout << ( << P.first << , << P.second << ) ; } long long int n; vector<long long int> a, b, c; vector<vector<long long int>> conn; vector<long long int> c0, c1; long long int ans = 0; void dfs1(long long int node, long long int par, long long int val) { a[node] = min(a[node], val); for (long long int i : conn[node]) { if (i == par) continue; dfs1(i, node, a[node]); } } void dfs2(long long int node, long long int par) { for (long long int i : conn[node]) { if (i == par) continue; dfs2(i, node); c0[node] += c0[i]; c1[node] += c1[i]; } if (b[node] == 0 && c[node] == 1) { c0[node]++; } if (b[node] == 1 && c[node] == 0) { c1[node]++; } } long long int dfs3(long long int node, long long int par) { long long int ct0 = 0, ct1 = 0; if (b[node] == 0 && c[node] == 1) ct0 = 1; if (b[node] == 1 && c[node] == 0) ct1 = 1; for (long long int i : conn[node]) { if (i == par) continue; long long int temp = dfs3(i, node); if (temp > 0) { ct0 += temp; } else { ct1 -= temp; } } long long int m = min(ct0, ct1); ans += a[node] * m; ct0 -= m; ct1 -= m; if (ct0) return ct0; return -ct1; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; a.resize(n); b.resize(n); c.resize(n); conn.resize(n); for (long long int i = 0; i < (n); i++) cin >> a[i] >> b[i] >> c[i]; for (long long int i = 0; i < (n - 1); i++) { long long int x; cin >> x; long long int y; cin >> y; x--, y--; conn[x].push_back(y); conn[y].push_back(x); } dfs1(0, -1, (long long int)1e18); c0.resize(n); c1.resize(n); dfs2(0, -1); if (dfs3(0, -1)) { cout << -1 << n ; } else { cout << 2 * ans << n ; } cerr << Time : << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC << ms n ; ; }
#include <bits/stdc++.h> long long n, md; void rid(int n, int m) { int i; long long sol = 1, p = 1; for (i = 0, p = n; i <= 30; i++) { if ((m & (1 << i)) == (1 << i)) { sol = (long long)sol * p; sol = (long long)sol % md; } p = (long long)p * p; p = (long long)p % md; } if (sol == 0) sol = md - 1; else sol--; printf( %I64d , sol); } int main() { scanf( %I64d%I64d , &n, &md); rid(3, n); }
#include <bits/stdc++.h> using namespace std; int main() { int a[11], mx, n; scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); printf( %d , *max_element(a + 1, a + n + 1) ^ a[n]); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 510; char g[maxn][maxn]; int col[maxn][maxn], v[maxn * maxn], b[maxn * maxn], cnt, n, k, sum; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; void dfs(int x, int y, int c) { col[x][y] = c; ++cnt; for (int i = 0; i < 4; ++i) { int tx = x + dx[i], ty = y + dy[i]; if (tx < 0 || ty < 0 || tx >= n || ty >= n || g[tx][ty] == X || col[tx][ty]) continue; dfs(tx, ty, c); } } void add(int x, int y) { if (col[x][y] && ++v[col[x][y]] == 1) sum += b[col[x][y]]; } void dec(int x, int y) { if (col[x][y] && --v[col[x][y]] == 0) sum -= b[col[x][y]]; } int main() { scanf( %d %d , &n, &k); for (int i = 0; i < n; ++i) scanf( %s , g[i]); int c = 1; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) if (g[i][j] == . && !col[i][j]) { cnt = 0; dfs(i, j, c); b[c++] = cnt; } } int ans = 0; for (int i = 0; i + k <= n; ++i) { memset(v, 0, sizeof v); sum = 0; for (int x = 0; x < k; ++x) { for (int y = 0; y < k; ++y) { add(x + i, y); if (!col[x + i][y]) ++sum; } if (i + k < n) add(i + k, x); if (k < n) add(x + i, k); if (i) add(i - 1, x); } ans = max(ans, sum); for (int j = 1; j + k <= n; ++j) { if (i) { if (j - 1 >= 0) dec(i - 1, j - 1); add(i - 1, j + k - 1); } if (i + k < n) { if (j - 1 >= 0) dec(i + k, j - 1); add(i + k, j + k - 1); } if (j - 1 >= 0) for (int t = 0; t < k; ++t) if (!col[i + t][j - 1]) --sum; if (j - 2 >= 0) for (int t = 0; t < k; ++t) dec(i + t, j - 2); if (j + k - 1 < n) for (int t = 0; t < k; ++t) if (!col[i + t][j + k - 1]) ++sum; if (j + k <= n) for (int t = 0; t < k; ++t) add(i + t, j + k); ans = max(ans, sum); } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000100; int N; long long K; long long arr[MAXN], anew[MAXN], tcur[MAXN], tnew[MAXN]; int main() { scanf( %d %I64d , &N, &K); arr[0] = 0; for (int i = 1; i < N; i++) { int p; long long w; scanf( %d %I64d , &p, &w); p--; arr[i] = arr[p] ^ w; } sort(arr, arr + N); for (int i = 0; i < N; i++) anew[i] = arr[i]; long long ans = 0; for (int i = 61; i >= 0; i--) { for (int j = 0; j < N; j++) { tcur[j] = (arr[j] >> i); tnew[j] = (anew[j] >> i); } long long pp = 0; int lloc = 0, rloc = 0; while (lloc < N) { long long cv = tcur[lloc]; while (rloc < N && tnew[rloc] < cv) rloc++; long long nl = 0, nr = 0; while (lloc < N && tcur[lloc] == cv) { lloc++; nl++; } while (rloc < N && tnew[rloc] == cv) { rloc++; nr++; } pp += nl * nr; } if (pp < K) { ans += (1LL << i); K -= pp; for (int j = 0; j < N; j++) anew[j] ^= (1LL << i); for (int j = 0; j < N - 1; j++) { if (anew[j] > anew[j + 1]) { int cj = j, ej = j + 1; while (cj && anew[cj - 1] > anew[j + 1]) cj--; while (ej < N - 1 && anew[ej + 1] < anew[j]) ej++; reverse(anew + cj, anew + j + 1); reverse(anew + j + 1, anew + ej + 1); reverse(anew + cj, anew + ej + 1); } } } } cout << ans << n ; }
#include <bits/stdc++.h> using namespace std; vector<int> by; vector<int> sl; int main() { long long int a, b, c, d, m, n, ck = 0, ans, s, r; cin >> n >> m >> r; for (int i = 0; i < n; i++) { cin >> s; by.push_back(s); } sort(by.begin(), by.end()); for (int j = 0; j < m; j++) { cin >> b; sl.push_back(b); } sort(sl.begin(), sl.end(), greater<int>()); long long int buy = r / by[0]; long long int rem = r % by[0]; long long int sell = buy * sl[0]; ans = sell + rem; if (ans > r) { cout << ans << endl; } else { cout << r << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; long long int M = 1e9 + 7; long long int MOD = 998244353; const double PI = 3.141592653589793238460; long long int power(long long int a, long long int b) { long long int res = 1; if (a == 0) return 0; if (a == 1) return 1; for (; b > 0; b >>= 1) { if (b & 1) { res = (res * a); if (res > M) res %= M; } a = (a * a); if (a > M) a = a % M; } return res; } int main() { long long int maxi = -1e18; long long int n; cin >> n; long long int a[n], b[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < n; i++) { long long int x = a[i]; long long int y = b[i]; for (int j = i; j < n; j++) { x |= (a[j]); y |= (b[j]); maxi = max(maxi, x + y); } } cout << maxi; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; int arr[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> arr[i][j]; } a[0] = 0; for (int j = 1; j < n; j++) { a[0] = a[0] | arr[0][j]; a[j] = arr[0][j]; } for (int i = 1; i < n; i++) { for (int j = i + 1; j < n; j++) { a[i] = a[i] | arr[i][j]; a[j] = arr[i][j] | a[j]; } } for (int i = 0; i < n; i++) cout << a[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long p, long long mod) { long long ret = 1; while (p) { if (p & 1) ret = (ret * a) % mod; a = (a * a) % mod; p /= 2; } return ret; } long long DP[1000]; int A[1000]; int main() { int n; cin >> n; for (__typeof((__typeof(n))0) i((__typeof(n))0); i < n; i++) { cin >> A[i]; A[i]--; } for (int i = 0; i < n; i++) { DP[i] = 2; for (int j = A[i]; j < i; j++) DP[i] = (DP[i] + DP[j]) % 1000000007; } long long ans = 0; for (int i = 0; i < n; i++) ans = (ans + DP[i]) % 1000000007; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1511, M = 3010100, inf = 0x7fffffff; const long long llinf = 0x7fffffff7fffffffll; long long x[N], y[N], r[N]; long long c[N]; double dist(int i, int j) { return sqrtl((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); } double area(int i) { return 3.14159265358979323846264338327950288 * r[i] * r[i]; } int main(int argc, const char* argv[]) { int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> x[i] >> y[i] >> r[i]; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (r[i] < r[j] && dist(i, j) <= r[j]) { ++c[i]; } } } double sum = 0; for (int i = 1; i <= n; ++i) { if (c[i] == 0) { sum += area(i); } else if (c[i] % 2 == 1) { sum += area(i); } else { sum -= area(i); } } printf( %.10f , sum); return 0; }
#include <bits/stdc++.h> // #include <atcoder/all> //tabaicho see https://boostjp.github.io/tips/multiprec-int.html // #include <boost/multiprecision/cpp_int.hpp> // using namespace boost::multiprecision; // cpp_int // int128_t // int256_t // int512_t // int1024_t // uint128_t // uint256_t // uint512_t // uint1024_t #define int long long #define inf 1000000007 // #define inf 998244353 #define pa pair<int,int> #define ll long long #define PI 3.14159265358979323846 #define mp make_pair #define pb push_back #define EPS (1e-8) using namespace std; int dx[8]={0,1,0,-1,1,1,-1,-1}; int dy[8]={1,0,-1,0,-1,1,1,-1}; class pa3{ public: int x; int y,z; pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {} bool operator < (const pa3 &p) const{ if(x!=p.x) return x<p.x; if(y!=p.y) return y<p.y; return z<p.z; //return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa3 &p) const{ if(x!=p.x) return x>p.x; if(y!=p.y) return y>p.y; return z>p.z; //return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa3 &p) const{ return x==p.x && y==p.y && z==p.z; } bool operator != (const pa3 &p) const{ return !( x==p.x && y==p.y && z==p.z); } }; class pa4{ public: int x; int y,z,w; pa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {} bool operator < (const pa4 &p) const{ if(x!=p.x) return x<p.x; if(y!=p.y) return y<p.y; if(z!=p.z)return z<p.z; return w<p.w; //return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa4 &p) const{ if(x!=p.x) return x>p.x; if(y!=p.y) return y>p.y; if(z!=p.z)return z>p.z; return w>p.w; //return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa4 &p) const{ return x==p.x && y==p.y && z==p.z &&w==p.w; } }; class pa2{ public: int x,y; pa2(int x=0,int y=0):x(x),y(y) {} pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);} pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);} bool operator < (const pa2 &p) const{ return y != p.y ? y<p.y: x<p.x; } bool operator > (const pa2 &p) const{ return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa2 &p) const{ return abs(x-p.x)==0 && abs(y-p.y)==0; } bool operator != (const pa2 &p) const{ return !(abs(x-p.x)==0 && abs(y-p.y)==0); } }; string itos( int i ) { ostringstream s ; s << i ; return s.str() ; } int Gcd(int v,int b){ if(v==0) return b; if(b==0) return v; if(v>b) return Gcd(b,v); if(v==b) return b; if(b%v==0) return v; return Gcd(v,b%v); } int mod; int extgcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int d = extgcd(b, a%b, y, x); y -= a/b * x; return d; } pa operator+(const pa & l,const pa & r) { return {l.first+r.first,l.second+r.second}; } pa operator-(const pa & l,const pa & r) { return {l.first-r.first,l.second-r.second}; } ostream& operator<<(ostream& os, const vector<int>& VEC){ for(auto v:VEC)os<<v<< ; return os; } ostream& operator<<(ostream& os, const pa& PAI){ os<<PAI.first<< : <<PAI.second; return os; } ostream& operator<<(ostream& os, const pa3& PAI){ os<<PAI.x<< : <<PAI.y<< : <<PAI.z; return os; } ostream& operator<<(ostream& os, const pa4& PAI){ os<<PAI.x<< : <<PAI.y<< : <<PAI.z<< : <<PAI.w; return os; } ostream& operator<<(ostream& os, const vector<pa>& VEC){ for(auto v:VEC)os<<v<< ; return os; } int beki(int wa,int rr,int warukazu){ if(rr==0) return 1%warukazu; if(rr==1) return wa%warukazu; wa%=warukazu; if(rr%2==1) return ((ll)beki(wa,rr-1,warukazu)*(ll)wa)%warukazu; ll zx=beki(wa,rr/2,warukazu); return (zx*zx)%warukazu; } int pr[1]; int inv[1]; int comb(int nn,int rr){ if(rr<0 || rr>nn || nn<0) return 0; int r=pr[nn]*inv[rr]; r%=mod; r*=inv[nn-rr]; r%=mod; return r; } void gya(int ert){ pr[0]=1; for(int i=1;i<=ert;i++){ pr[i]=((ll)pr[i-1]*i)%mod; } inv[ert]=beki(pr[ert],mod-2,mod); for(int i=ert-1;i>=0;i--){ inv[i]=(ll)inv[i+1]*(i+1)%mod; } } // cin.tie(0); // ios::sync_with_stdio(false); //priority_queue<pa3,vector<pa3>,greater<pa3>> pq; //sort(ve.begin(),ve.end(),greater<int>()); // mt19937(clock_per_sec); // mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()) ; void solve(){ int n; cin>>n; vector<int>dp(n,0); vector<int>id(n),s(n); //int xn=0; for(auto &v:id){ cin>>v; v--; // pos[v].pb(xn); // xn++; } for(auto &v:s)cin>>v; int ans=0; for(int i=n-1;i>=0;i--){ int pos=0; for(int j=0;j<i;j++){ if(id[i]!=id[j]){ int g=dp[j]+abs(s[i]-s[j]); int f=max(dp[i],pos)+abs(s[i]-s[j]); dp[j]=max(dp[j],f); pos=max(pos,g); ans=max(ans,g); ans=max(ans,f); } } } cout<<ans<<endl; } signed main(){ mod=inf; cin.tie(0); ios::sync_with_stdio(false); int n=1; cin>>n; for(int i=0;i<n;i++)solve(); return 0; }
#include <bits/stdc++.h> using namespace std; vector<string> prices; enum State { name, price }; double to_num(string s) { double n = 0; int dots = 0, dot = 0; bool frac = false; for (char c : s) if (c == . ) ++dots; if (s[s.size() - 3] == . ) frac = true; for (int i = 0; i < s.size(); ++i) { if (s[i] >= 0 && s[i] <= 9 ) { n *= 10; n += s[i] - 0 ; } else if (s[i] == . ) ++dot; if (dot == dots && frac) { n += double(s[i + 1] - 0 ) / 10.; n += double(s[i + 2] - 0 ) / 100.; break; } } return n; } string to_price(double n) { string s = to_string(n); string fraction; int dot = 0; for (int i = 0; i < s.size(); ++i) if (s[i] == . ) dot = i; if (s[dot + 1] != 0 || s[dot + 2] != 0 ) { fraction.push_back( . ); fraction.push_back(s[dot + 1]); fraction.push_back(s[dot + 2]); } string c = s.substr(0, dot); s = c; string integer = to_string(int(n)); string res; for (int i = integer.size() - 1, k = 0; i >= 0; --i) { ++k; res.push_back(integer[i]); if (k % 3 == 0 && i != 0) res.push_back( . ); } reverse(res.begin(), res.end()); res += fraction; return res; } int main() { string input; getline(cin, input); string s; State state = name; for (char c : input) { if (isalpha(c) && state == name) continue; else if (!isalpha(c) && state == name) { state = price; s.push_back(c); } else if (!isalpha(c) && state == price) s.push_back(c); else if (isalpha(c) && state == price) { state = name; prices.push_back(s); s.clear(); } } prices.push_back(s); double sum = 0; for (int i = 0; i < prices.size(); ++i) sum += to_num(prices[i]); cout << to_price(sum); }
#include <bits/stdc++.h> const int fin = 0, maxn = 400000, maxm = maxn << 1; int n, m, cent, tot; int fs[maxn], e[maxm], next[maxm]; int bel[maxn], pos[maxn], len[maxn], left[maxn]; int c[maxn], deg[maxn], b[maxn]; bool v[maxn]; struct rec { int x, y; } g[maxn]; int sum(int *a, int n, int x) { int s = 0; for (; x; x -= ((x) & (-(x)))) s += a[x]; return s; } void upd(int *a, int n, int x, int w) { for (; x <= n; x += ((x) & (-(x)))) a[x] += w; } inline void addedge(int x, int y) { e[++m] = y; next[m] = fs[x]; fs[x] = m; e[++m] = x; next[m] = fs[y]; fs[y] = m; } void init() { int i; scanf( %d , &n); m = 0; for (i = 1; i < n; ++i) { scanf( %d%d , &g[i].x, &g[i].y); addedge(g[i].x, g[i].y); deg[g[i].x]++; deg[g[i].y]++; } cent = 0; for (i = 1; i <= n; ++i) if (deg[i] > 2) { cent = i; break; } if (!cent) cent = 1; } void build() { int i, j, p, k, t; i = fs[cent]; tot = 0; v[cent] = true; bel[cent] = 0; for (j = 1; j <= deg[cent]; ++j) { p = e[i]; bel[p] = j; v[p] = true; b[++tot] = p; pos[p] = 1; left[j] = tot; while (next[fs[p]]) { if (v[e[fs[p]]]) t = e[next[fs[p]]]; else t = e[fs[p]]; v[t] = true; b[++tot] = t; bel[t] = j; pos[t] = pos[p] + 1; p = t; } len[j] = tot - left[j] + 1; i = next[i]; for (k = 1; k <= len[j]; ++k) upd(c + left[j] - 1, len[j], k, 1); } } void solve() { int que, x, y, j, t, type, p, k; scanf( %d , &que); while (que--) { scanf( %d , &type); if (type == 1) { scanf( %d , &p); x = g[p].x; y = g[p].y; if ((!bel[x]) || (!bel[y])) { j = bel[y] + bel[x]; upd(c + left[j] - 1, len[j], 1, 1); } else { j = bel[x]; if (pos[x] > pos[y]) k = pos[x]; else k = pos[y]; upd(c + left[j] - 1, len[j], k, 1); } } else if (type == 2) { scanf( %d , &p); x = g[p].x; y = g[p].y; if ((!bel[x]) || (!bel[y])) { j = bel[y] + bel[x]; upd(c + left[j] - 1, len[j], 1, -1); } else { j = bel[x]; if (pos[x] > pos[y]) k = pos[x]; else k = pos[y]; upd(c + left[j] - 1, len[j], k, -1); } } else { scanf( %d%d , &x, &y); if (x == y) printf( 0 n ); else { if ((!bel[x]) || (!bel[y])) { j = bel[x] + bel[y]; if (!bel[x]) k = pos[y]; else k = pos[x]; t = sum(c + left[j] - 1, len[j], k); if (t == k) printf( %d n , t); else printf( -1 n ); } else if (bel[x] == bel[y]) { j = bel[x]; if (pos[x] > pos[y]) { t = x; x = y; y = t; } t = sum(c + left[j] - 1, len[j], pos[y]) - sum(c + left[j] - 1, len[j], pos[x]); if (t == pos[y] - pos[x]) printf( %d n , t); else printf( -1 n ); } else { j = bel[x]; t = sum(c + left[j] - 1, len[j], pos[x]); j = bel[y]; t += sum(c + left[j] - 1, len[j], pos[y]); if (t == pos[x] + pos[y]) printf( %d n , t); else printf( -1 n ); } } } } } int main() { if (fin) { freopen( t.in , r , stdin); freopen( t.out , w , stdout); } init(); build(); solve(); if (fin) { fclose(stdin); fclose(stdout); } return 0; }
#include <bits/stdc++.h> using namespace std; struct dot { int value, i, j; }; bool operator<(dot a, dot b) { return a.value < b.value; } int main() { int t; scanf( %d , &t); while (t--) { int n, m; scanf( %d %d , &n, &m); int num[n][m]; priority_queue<dot> pq; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { scanf( %d , &num[i][j]); pq.push({num[i][j], i, j}); } } dot d[n]; for (int i = 0; i < n; ++i) { d[i] = pq.top(); pq.pop(); } if (n == 4) { bool did = false; dot dsave; if (d[0].j == d[1].j && d[2].j == d[3].j && d[0].j != d[2].j) { bool b1 = abs(d[0].i - d[1].i) == 2; bool b2 = abs(d[2].i - d[3].i) == 2; if (b1 ^ b2) { dsave = d[3]; d[3] = pq.top(); did = true; } } else if (d[0].j == d[2].j && d[1].j == d[3].j && d[0].j != d[1].j) { bool b1 = abs(d[0].i - d[2].i) == 2; bool b2 = abs(d[1].i - d[3].i) == 2; if (b1 ^ b2) { dsave = d[3]; d[3] = pq.top(); did = true; } } else if (d[0].j == d[3].j && d[1].j == d[2].j && d[0].j != d[1].j) { bool b1 = abs(d[0].i - d[3].i) == 2; bool b2 = abs(d[1].i - d[2].i) == 2; if (b1 ^ b2) { dsave = d[3]; d[3] = pq.top(); did = true; } } if (did) { bool done = true; if (d[0].j == d[1].j && d[2].j == d[3].j && d[0].j != d[2].j) { bool b1 = abs(d[0].i - d[1].i) == 2; bool b2 = abs(d[2].i - d[3].i) == 2; if (b1 ^ b2) done = false; } else if (d[0].j == d[2].j && d[1].j == d[3].j && d[0].j != d[1].j) { bool b1 = abs(d[0].i - d[2].i) == 2; bool b2 = abs(d[1].i - d[3].i) == 2; if (b1 ^ b2) done = false; } else if (d[0].j == d[3].j && d[1].j == d[2].j && d[0].j != d[1].j) { bool b1 = abs(d[0].i - d[3].i) == 2; bool b2 = abs(d[1].i - d[2].i) == 2; if (b1 ^ b2) done = false; } if (done) { } else { d[2] = dsave; } } } int ret = 0; for (int i = 0; i < n; ++i) { ret += d[i].value; } printf( %d n , ret); } }
#include <bits/stdc++.h> using namespace std; int n, m, sum, ans; int cnt[1000010], pre[1000010]; char a[1000010], b[1000010]; int main() { scanf( %s%s , a + 1, b + 1); n = strlen(a + 1); m = strlen(b + 1); for (int i = 1; i < n; i++) if (a[i] != a[i + 1]) cnt[i] = 1; for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + cnt[i]; for (int i = 1; i <= m; i++) { if (a[i] != b[i]) sum++; } if (sum % 2 == 0) ans = 1; for (int i = m + 1; i <= n; i++) { sum += pre[i - 1] - pre[i - m - 1]; if (sum % 2 == 0) ans++; } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, sum, count; cin >> n; int a[n][n]; int row[n], col[n]; for (i = 0; i < n; i++) { sum = 0; for (j = 0; j < n; j++) { cin >> a[i][j]; sum += a[i][j]; } row[i] = sum; } for (j = 0; j < n; j++) { sum = 0; for (i = 0; i < n; i++) { sum += a[i][j]; } col[j] = sum; } count = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (col[i] > row[j]) count++; } } cout << count << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int mo = 1000000007; inline void gn(long long &x) { int sg = 1; char c; while (((c = getchar()) < 0 || c > 9 ) && c != - ) ; c == - ? (sg = -1, x = 0) : (x = c - 0 ); while ((c = getchar()) >= 0 && c <= 9 ) x = x * 10 + c - 0 ; x *= sg; } inline void gn(int &x) { long long t; gn(t); x = t; } inline void gn(unsigned long long &x) { long long t; gn(t); x = t; } inline void gn(double &x) { double t; scanf( %lf , &t); x = t; } inline void gn(long double &x) { double t; scanf( %lf , &t); x = t; } template <class T1, class T2> inline void gn(T1 &r, T2 &s) { gn(r), gn(s); } template <class T1, class T2, class T3> inline void gn(T1 &r, T2 &s, T3 &t) { gn(r), gn(s), gn(t); } template <class T1, class T2, class T3, class T4> inline void gn(T1 &r, T2 &s, T3 &t, T4 &u) { gn(r), gn(s), gn(t), gn(u); } inline void gs(char *s) { scanf( %s , s); } inline void gc(char &c) { while ((c = getchar()) > 126 || c < 33) ; } inline void pc(char c) { putchar(c); } const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1}; long long powmod(long long a, long long b) { long long res = 1; a %= mo; for (; b; b >>= 1) { if (b & 1) res = res * a % mo; a = a * a % mo; } return res; } long long powmod(long long a, long long b, long long mo) { long long res = 1; a %= mo; for (; b; b >>= 1) { if (b & 1) res = res * a % mo; a = a * a % mo; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } const int N = 5055, M = 111111; int l, m, n, t, C, x, y, prex; vector<int> a[N]; int dis[N][N]; void bfs(int k) { for (int i = (1); i <= (n); ++i) dis[k][i] = inf; dis[k][k] = 0; queue<int> Q; Q.push(k); while (!Q.empty()) { int p = Q.front(); Q.pop(); for (auto i : a[p]) { if (dis[k][i] == inf) { dis[k][i] = dis[k][p] + 1; Q.push(i); } } } } int p[9]; void update() { printf( %d %d %d n , p[0], p[1], p[2]); fflush(stdout); if (p[0] == x || p[1] == x || p[2] == x) exit(0); prex = x; scanf( %d , &x); if (p[0] == x || p[1] == x || p[2] == x) exit(0); } void go(int &x, int y) { for (auto i : a[x]) if (dis[i][y] == dis[x][y] - 1) { x = i; break; } } int main() { scanf( %d%d , &n, &m); for (int i = (1); i <= (m); ++i) scanf( %d%d , &x, &y), a[x].push_back(y), a[y].push_back(x); for (int i = (1); i <= (n); ++i) bfs(i); p[0] = p[1] = p[2] = 1; x = -1; while (1) { update(); int q[9]; int ans = inf; for (int i = (0); i <= (((int)(a[x]).size()) - 1); ++i) for (int j = (0); j <= (((int)(a[x]).size()) - 1); ++j) for (int k = (0); k <= (((int)(a[x]).size()) - 1); ++k) { int res = dis[p[0]][a[x][i]] + dis[p[1]][a[x][j]] + dis[p[2]][a[x][k]]; if (i != j) res -= n; if (j != k) res -= n; if (i != k) res -= n; if (res < ans) ans = res, q[0] = a[x][i], q[1] = a[x][j], q[2] = a[x][k]; } for (int i = (0); i <= (2); ++i) if (dis[p[i]][x] == 1) q[i] = x; for (int i = (0); i <= (2); ++i) go(p[i], q[i]); } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, q; pair<int, int> a[400004]; long long ans[200004], d[400004], sum; int fa[400004], l[400004], r[400004], s[400004]; vector<pair<int, int> > v, ask; int F(int x) { return fa[x] == x ? x : fa[x] = F(fa[x]); } long long got(int v) { return d[r[v]] - d[r[v] - s[v]]; } int main() { scanf( %d%d%d , &n, &m, &q); for (int i = 1; i <= n + m; i++) scanf( %d , &a[i].first), a[i].second = i; sort(a + 1, a + n + m + 1); for (int i = 1; i <= n + m; i++) { d[i] = d[i - 1] + a[i].first; fa[i] = i, s[i] = (a[i].second <= n), l[i] = r[i] = i; if (s[i] == 1) sum += a[i].first; if (i > 1) v.push_back({a[i].first - a[i - 1].first, i}); } for (int i = 1; i <= q; i++) { int k; scanf( %d , &k); ask.push_back({k, i}); } sort(v.begin(), v.end()); sort(ask.begin(), ask.end()); int it = 0; for (auto qr : ask) { while (it < v.size() && v[it].first <= qr.first) { int p = v[it].second; int U = F(p - 1), V = F(p); sum -= got(U) + got(V); fa[U] = V, s[V] += s[U], l[V] = l[U]; sum += got(V); it++; } ans[qr.second] = sum; } for (int i = 1; i <= q; i++) printf( %lld n , ans[i]); }
#include <bits/stdc++.h> using namespace std; int main() { unsigned int n, c, i, ans = 1, arr[100001]; cin >> n >> c; for (i = 0; i < n; i++) { cin >> arr[i]; } for (i = 1; i < n; i++) { if (arr[i] - arr[i - 1] <= c) ans++; else ans = 1; } cout << ans; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 111 * 1000; int par[MAXN], sz[MAXN]; int find(int i) { if (par[i] == i) return i; return par[i] = find(par[i]); } void un(int a, int b) { sz[find(a)] += sz[find(b)]; par[find(b)] = find(a); } int n; int mark[MAXN]; int sum[MAXN], tot[MAXN]; vector<int> adj[MAXN]; int dfs(int pos, int par = -1) { if (mark[pos]) return 0; mark[pos]++; for (__typeof((adj[pos]).begin()) it = (adj[pos]).begin(); it != (adj[pos]).end(); ++it) if (*it != par) sum[pos] += dfs(*it, pos); return sum[pos]; } void stot(int pos, int val) { if (mark[pos] != 1) return; mark[pos]++; tot[pos] = val; for (__typeof((adj[pos]).begin()) it = (adj[pos]).begin(); it != (adj[pos]).end(); ++it) stot(*it, val); } int main() { for (register int i = (0); i < (int)(MAXN); ++i) par[i] = i, sz[i] = 1; cin >> n; vector<pair<pair<int, int>, pair<int, int> > > E(n - 1); for (register int i = (0); i < (int)(n - 1); ++i) cin >> E[i].second.first >> E[i].second.second >> E[i].first.first, E[i].second.first--, E[i].second.second--, E[i].first.second = i + 1; sort((E).begin(), (E).end()); int lo = 0; long long mb = 0; vector<long long> res; while (lo < ((int)(E).size())) { int hi = lo; while (hi < ((int)(E).size()) && E[lo].first.first == E[hi].first.first) hi++; set<int> st; for (register int i = (lo); i < (int)(hi); ++i) { int x = find(E[i].second.first), y = find(E[i].second.second); if (x != y) { st.insert(x); st.insert(y); adj[x].push_back(y); adj[y].push_back(x); sum[x] = sz[x]; sum[y] = sz[y]; } } for (__typeof((st).begin()) it = (st).begin(); it != (st).end(); ++it) { if (!mark[*it]) { int tmp = dfs(*it); stot(*it, tmp); } } for (__typeof((st).begin()) it = (st).begin(); it != (st).end(); ++it) mark[*it] = false, adj[*it].clear(); for (register int i = (lo); i < (int)(hi); ++i) { int x = E[i].second.first, y = E[i].second.second; if (find(x) == find(y)) continue; int mn = min(sum[find(x)], sum[find(y)]); long long cnt = 2LL * mn * (tot[find(x)] - mn); if (cnt > mb) { mb = cnt; res.clear(); } if (cnt == mb) res.push_back(E[i].first.second); } for (register int i = (lo); i < (int)(hi); ++i) { int x = E[i].second.first, y = E[i].second.second; if (find(x) == find(y)) continue; un(x, y); } lo = hi; } cout << mb << << ((int)(res).size()) << endl; for (register int i = (0); i < (int)(((int)(res).size())); ++i) cout << res[i] << ; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void __print(int x) { cout << x; } void __print(long x) { cout << x; } void __print(long long x) { cout << x; } void __print(unsigned x) { cout << x; } void __print(unsigned long x) { cout << x; } void __print(unsigned long long x) { cout << x; } void __print(float x) { cout << x; } void __print(double x) { cout << x; } void __print(long double x) { cerr << x; } void __print(char x) { cout << << x << ; } void __print(const char *x) { cout << << x << ; } void __print(const string &x) { cout << << x << ; } void __print(bool x) { cout << (x ? true : false ); } template <typename T, typename V> void __print(const pair<T, V> &x) { cout << { ; __print(x.first); cout << , ; __print(x.second); cout << } ; } template <typename T> void __print(const T &x) { int f = 0; cout << { ; for (auto &i : x) cout << (f++ ? , : ), __print(i); cout << } ; } void _print() { cout << ] n ; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << , ; _print(v...); } void c_p_c(); int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int testcase = 1; while (testcase--) c_p_c(); return 0; } void c_p_c() { long long int n, m; cin >> n >> m; vector<vector<long long int>> a(n, vector<long long int>(m, 0)); ; vector<vector<long long int>> b(n, vector<long long int>(m, 1)); ; for (long long int i = 0; i < n; ++i) { for (long long int j = 0; j < m; ++j) { cin >> a[i][j]; if (!a[i][j]) { for (long long int k = 0; k < n; ++k) { b[k][j] = 0; } for (long long int k = 0; k < m; ++k) { b[i][k] = 0; } } } } for (long long int i = 0; i < n; ++i) { for (long long int j = 0; j < m; ++j) { if (a[i][j]) { long long int ans = 0; for (long long int k = 0; k < n; ++k) { ans += b[k][j]; } for (long long int k = 0; k < m; ++k) { ans += b[i][k]; } if ((bool)(ans) == 0) { cout << NO ; return; } } } } cout << YES n ; for (long long int i = 0; i < n; ++i) { for (long long int j = 0; j < m; ++j) { cout << (bool)(b[i][j]) << ; } cout << endl; } }